f703ef47be25cccd3ef7e692c28c8e36910a5b05
[citadel.git] / citadel / parsedate.y
1 %{
2 /* $Revision$
3 **
4 **  Originally written by Steven M. Bellovin <smb@research.att.com> while
5 **  at the University of North Carolina at Chapel Hill.  Later tweaked by
6 **  a couple of people on Usenet.  Completely overhauled by Rich $alz
7 **  <rsalz@osf.org> and Jim Berets <jberets@bbn.com> in August, 1990.
8 **  Further revised (removed obsolete constructs and cleaned up timezone
9 **  names) in August, 1991, by Rich.  Paul Eggert <eggert@twinsun.com>
10 **  helped in September, 1992.  Art Cancro <ajc@uncnsrd.mt-kisco.ny.us> cleaned
11 **  it up for ANSI C in December, 1999.
12 **
13 **  This grammar has six shift/reduce conflicts.
14 **
15 **  This code is in the public domain and has no copyright.
16 */
17 /* SUPPRESS 530 *//* Empty body for statement */
18 /* SUPPRESS 593 on yyerrlab *//* Label was not used */
19 /* SUPPRESS 593 on yynewstate *//* Label was not used */
20 /* SUPPRESS 595 on yypvt *//* Automatic variable may be used before set */
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <ctype.h>
24 #include <time.h>
25 #include "parsedate.h"
26
27 int date_lex(void);
28
29 #define yyparse         date_parse
30 #define yylex           date_lex
31 #define yyerror         date_error
32
33
34     /* See the LeapYears table in Convert. */
35 #define EPOCH           1970
36 #define END_OF_TIME     2038
37     /* Constants for general time calculations. */
38 #define DST_OFFSET      1
39 #define SECSPERDAY      (24L * 60L * 60L)
40     /* Readability for TABLE stuff. */
41 #define HOUR(x)         (x * 60)
42
43 #define LPAREN          '('
44 #define RPAREN          ')'
45 #define IS7BIT(x)       ((unsigned int)(x) < 0200)
46
47 #define SIZEOF(array)   ((int)(sizeof array / sizeof array[0]))
48 #define ENDOF(array)    (&array[SIZEOF(array)])
49
50
51 /*
52 **  An entry in the lexical lookup table.
53 */
54 typedef struct _TABLE {
55     char        *name;
56     int         type;
57     time_t      value;
58 } TABLE;
59
60 /*
61 **  Daylight-savings mode:  on, off, or not yet known.
62 */
63 typedef enum _DSTMODE {
64     DSTon, DSToff, DSTmaybe
65 } DSTMODE;
66
67 /*
68 **  Meridian:  am, pm, or 24-hour style.
69 */
70 typedef enum _MERIDIAN {
71     MERam, MERpm, MER24
72 } MERIDIAN;
73
74
75 /*
76 **  Global variables.  We could get rid of most of them by using a yacc
77 **  union, but this is more efficient.  (This routine predates the
78 **  yacc %union construct.)
79 */
80 static char     *yyInput;
81 static DSTMODE  yyDSTmode;
82 static int      yyHaveDate;
83 static int      yyHaveRel;
84 static int      yyHaveTime;
85 static time_t   yyTimezone;
86 static time_t   yyDay;
87 static time_t   yyHour;
88 static time_t   yyMinutes;
89 static time_t   yyMonth;
90 static time_t   yySeconds;
91 static time_t   yyYear;
92 static MERIDIAN yyMeridian;
93 static time_t   yyRelMonth;
94 static time_t   yyRelSeconds;
95
96
97 static void             date_error(char *);
98 %}
99
100 %union {
101     time_t              Number;
102     enum _MERIDIAN      Meridian;
103 }
104
105 %token  tDAY tDAYZONE tMERIDIAN tMONTH tMONTH_UNIT tSEC_UNIT tSNUMBER
106 %token  tUNUMBER tZONE
107
108 %type   <Number>        tDAYZONE tMONTH tMONTH_UNIT tSEC_UNIT
109 %type   <Number>        tSNUMBER tUNUMBER tZONE numzone zone
110 %type   <Meridian>      tMERIDIAN o_merid
111
112 %%
113
114 spec    : /* NULL */
115         | spec item
116         ;
117
118 item    : time {
119             yyHaveTime++;
120 #ifdef lint
121             /* I am compulsive about lint natterings... */
122             if (yyHaveTime == -1) {
123                 YYERROR;
124             }
125 #endif /* lint */
126         }
127         | time zone {
128             yyHaveTime++;
129             yyTimezone = $2;
130         }
131         | date {
132             yyHaveDate++;
133         }
134         | rel {
135             yyHaveRel = 1;
136         }
137         ;
138
139 time    : tUNUMBER o_merid {
140             if ($1 < 100) {
141                 yyHour = $1;
142                 yyMinutes = 0;
143             }
144             else {
145                 yyHour = $1 / 100;
146                 yyMinutes = $1 % 100;
147             }
148             yySeconds = 0;
149             yyMeridian = $2;
150         }
151         | tUNUMBER ':' tUNUMBER o_merid {
152             yyHour = $1;
153             yyMinutes = $3;
154             yySeconds = 0;
155             yyMeridian = $4;
156         }
157         | tUNUMBER ':' tUNUMBER numzone {
158             yyHour = $1;
159             yyMinutes = $3;
160             yyTimezone = $4;
161             yyMeridian = MER24;
162             yyDSTmode = DSToff;
163         }
164         | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
165             yyHour = $1;
166             yyMinutes = $3;
167             yySeconds = $5;
168             yyMeridian = $6;
169         }
170         | tUNUMBER ':' tUNUMBER ':' tUNUMBER numzone {
171             yyHour = $1;
172             yyMinutes = $3;
173             yySeconds = $5;
174             yyTimezone = $6;
175             yyMeridian = MER24;
176             yyDSTmode = DSToff;
177         }
178         ;
179
180 zone    : tZONE {
181             $$ = $1;
182             yyDSTmode = DSToff;
183         }
184         | tDAYZONE {
185             $$ = $1;
186             yyDSTmode = DSTon;
187         }
188         | tZONE numzone {
189             /* Only allow "GMT+300" and "GMT-0800" */
190             if ($1 != 0) {
191                 YYABORT;
192             }
193             $$ = $2;
194             yyDSTmode = DSToff;
195         }
196         | numzone {
197             $$ = $1;
198             yyDSTmode = DSToff;
199         }
200         ;
201
202 numzone : tSNUMBER {
203             int         i;
204
205             /* Unix and GMT and numeric timezones -- a little confusing. */
206             if ($1 < 0) {
207                 /* Don't work with negative modulus. */
208                 $1 = -$1;
209                 if ($1 > 9999 || (i = $1 % 100) >= 60) {
210                     YYABORT;
211                 }
212                 $$ = ($1 / 100) * 60 + i;
213             }
214             else {
215                 if ($1 > 9999 || (i = $1 % 100) >= 60) {
216                     YYABORT;
217                 }
218                 $$ = -(($1 / 100) * 60 + i);
219             }
220         }
221         ;
222
223 date    : tUNUMBER '/' tUNUMBER {
224             yyMonth = $1;
225             yyDay = $3;
226         }
227         | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
228             if ($1 > 100) {
229                 yyYear = $1;
230                 yyMonth = $3;
231                 yyDay = $5;
232             }
233             else {
234                 yyMonth = $1;
235                 yyDay = $3;
236                 yyYear = $5;
237             }
238         }
239         | tMONTH tUNUMBER {
240             yyMonth = $1;
241             yyDay = $2;
242         }
243         | tMONTH tUNUMBER ',' tUNUMBER {
244             yyMonth = $1;
245             yyDay = $2;
246             yyYear = $4;
247         }
248         | tUNUMBER tMONTH {
249             yyDay = $1;
250             yyMonth = $2;
251         }
252         | tUNUMBER tMONTH tUNUMBER {
253             yyDay = $1;
254             yyMonth = $2;
255             yyYear = $3;
256         }
257         | tDAY ',' tUNUMBER tMONTH tUNUMBER {
258             yyDay = $3;
259             yyMonth = $4;
260             yyYear = $5;
261         }
262         ;
263
264 rel     : tSNUMBER tSEC_UNIT {
265             yyRelSeconds += $1 * $2;
266         }
267         | tUNUMBER tSEC_UNIT {
268             yyRelSeconds += $1 * $2;
269         }
270         | tSNUMBER tMONTH_UNIT {
271             yyRelMonth += $1 * $2;
272         }
273         | tUNUMBER tMONTH_UNIT {
274             yyRelMonth += $1 * $2;
275         }
276         ;
277
278 o_merid : /* NULL */ {
279             $$ = MER24;
280         }
281         | tMERIDIAN {
282             $$ = $1;
283         }
284         ;
285
286 %%
287
288 /* Month and day table. */
289 static TABLE    MonthDayTable[] = {
290     { "january",        tMONTH,  1 },
291     { "february",       tMONTH,  2 },
292     { "march",          tMONTH,  3 },
293     { "april",          tMONTH,  4 },
294     { "may",            tMONTH,  5 },
295     { "june",           tMONTH,  6 },
296     { "july",           tMONTH,  7 },
297     { "august",         tMONTH,  8 },
298     { "september",      tMONTH,  9 },
299     { "october",        tMONTH, 10 },
300     { "november",       tMONTH, 11 },
301     { "december",       tMONTH, 12 },
302         /* The value of the day isn't used... */
303     { "sunday",         tDAY, 0 },
304     { "monday",         tDAY, 0 },
305     { "tuesday",        tDAY, 0 },
306     { "wednesday",      tDAY, 0 },
307     { "thursday",       tDAY, 0 },
308     { "friday",         tDAY, 0 },
309     { "saturday",       tDAY, 0 },
310 };
311
312 /* Time units table. */
313 static TABLE    UnitsTable[] = {
314     { "year",           tMONTH_UNIT,    12 },
315     { "month",          tMONTH_UNIT,    1 },
316     { "week",           tSEC_UNIT,      7L * 24 * 60 * 60 },
317     { "day",            tSEC_UNIT,      1L * 24 * 60 * 60 },
318     { "hour",           tSEC_UNIT,      60 * 60 },
319     { "minute",         tSEC_UNIT,      60 },
320     { "min",            tSEC_UNIT,      60 },
321     { "second",         tSEC_UNIT,      1 },
322     { "sec",            tSEC_UNIT,      1 },
323 };
324
325 /* Timezone table. */
326 static TABLE    TimezoneTable[] = {
327     { "gmt",    tZONE,     HOUR( 0) },  /* Greenwich Mean */
328     { "ut",     tZONE,     HOUR( 0) },  /* Universal */
329     { "utc",    tZONE,     HOUR( 0) },  /* Universal Coordinated */
330     { "cut",    tZONE,     HOUR( 0) },  /* Coordinated Universal */
331     { "z",      tZONE,     HOUR( 0) },  /* Greenwich Mean */
332     { "wet",    tZONE,     HOUR( 0) },  /* Western European */
333     { "bst",    tDAYZONE,  HOUR( 0) },  /* British Summer */
334     { "nst",    tZONE,     HOUR(3)+30 }, /* Newfoundland Standard */
335     { "ndt",    tDAYZONE,  HOUR(3)+30 }, /* Newfoundland Daylight */
336     { "ast",    tZONE,     HOUR( 4) },  /* Atlantic Standard */
337     { "adt",    tDAYZONE,  HOUR( 4) },  /* Atlantic Daylight */
338     { "est",    tZONE,     HOUR( 5) },  /* Eastern Standard */
339     { "edt",    tDAYZONE,  HOUR( 5) },  /* Eastern Daylight */
340     { "cst",    tZONE,     HOUR( 6) },  /* Central Standard */
341     { "cdt",    tDAYZONE,  HOUR( 6) },  /* Central Daylight */
342     { "mst",    tZONE,     HOUR( 7) },  /* Mountain Standard */
343     { "mdt",    tDAYZONE,  HOUR( 7) },  /* Mountain Daylight */
344     { "pst",    tZONE,     HOUR( 8) },  /* Pacific Standard */
345     { "pdt",    tDAYZONE,  HOUR( 8) },  /* Pacific Daylight */
346     { "yst",    tZONE,     HOUR( 9) },  /* Yukon Standard */
347     { "ydt",    tDAYZONE,  HOUR( 9) },  /* Yukon Daylight */
348     { "akst",   tZONE,     HOUR( 9) },  /* Alaska Standard */
349     { "akdt",   tDAYZONE,  HOUR( 9) },  /* Alaska Daylight */
350     { "hst",    tZONE,     HOUR(10) },  /* Hawaii Standard */
351     { "hast",   tZONE,     HOUR(10) },  /* Hawaii-Aleutian Standard */
352     { "hadt",   tDAYZONE,  HOUR(10) },  /* Hawaii-Aleutian Daylight */
353     { "ces",    tDAYZONE,  -HOUR(1) },  /* Central European Summer */
354     { "cest",   tDAYZONE,  -HOUR(1) },  /* Central European Summer */
355     { "mez",    tZONE,     -HOUR(1) },  /* Middle European */
356     { "mezt",   tDAYZONE,  -HOUR(1) },  /* Middle European Summer */
357     { "cet",    tZONE,     -HOUR(1) },  /* Central European */
358     { "met",    tZONE,     -HOUR(1) },  /* Middle European */
359     { "eet",    tZONE,     -HOUR(2) },  /* Eastern Europe */
360     { "msk",    tZONE,     -HOUR(3) },  /* Moscow Winter */
361     { "msd",    tDAYZONE,  -HOUR(3) },  /* Moscow Summer */
362     { "wast",   tZONE,     -HOUR(8) },  /* West Australian Standard */
363     { "wadt",   tDAYZONE,  -HOUR(8) },  /* West Australian Daylight */
364     { "hkt",    tZONE,     -HOUR(8) },  /* Hong Kong */
365     { "cct",    tZONE,     -HOUR(8) },  /* China Coast */
366     { "jst",    tZONE,     -HOUR(9) },  /* Japan Standard */
367     { "kst",    tZONE,     -HOUR(9) },  /* Korean Standard */
368     { "kdt",    tZONE,     -HOUR(9) },  /* Korean Daylight */
369     { "cast",   tZONE,     -(HOUR(9)+30) }, /* Central Australian Standard */
370     { "cadt",   tDAYZONE,  -(HOUR(9)+30) }, /* Central Australian Daylight */
371     { "east",   tZONE,     -HOUR(10) }, /* Eastern Australian Standard */
372     { "eadt",   tDAYZONE,  -HOUR(10) }, /* Eastern Australian Daylight */
373     { "nzst",   tZONE,     -HOUR(12) }, /* New Zealand Standard */
374     { "nzdt",   tDAYZONE,  -HOUR(12) }, /* New Zealand Daylight */
375
376     /* For completeness we include the following entries. */
377 #if 0
378
379     /* Duplicate names.  Either they conflict with a zone listed above
380      * (which is either more likely to be seen or just been in circulation
381      * longer), or they conflict with another zone in this section and
382      * we could not reasonably choose one over the other. */
383     { "fst",    tZONE,     HOUR( 2) },  /* Fernando De Noronha Standard */
384     { "fdt",    tDAYZONE,  HOUR( 2) },  /* Fernando De Noronha Daylight */
385     { "bst",    tZONE,     HOUR( 3) },  /* Brazil Standard */
386     { "est",    tZONE,     HOUR( 3) },  /* Eastern Standard (Brazil) */
387     { "edt",    tDAYZONE,  HOUR( 3) },  /* Eastern Daylight (Brazil) */
388     { "wst",    tZONE,     HOUR( 4) },  /* Western Standard (Brazil) */
389     { "wdt",    tDAYZONE,  HOUR( 4) },  /* Western Daylight (Brazil) */
390     { "cst",    tZONE,     HOUR( 5) },  /* Chile Standard */
391     { "cdt",    tDAYZONE,  HOUR( 5) },  /* Chile Daylight */
392     { "ast",    tZONE,     HOUR( 5) },  /* Acre Standard */
393     { "adt",    tDAYZONE,  HOUR( 5) },  /* Acre Daylight */
394     { "cst",    tZONE,     HOUR( 5) },  /* Cuba Standard */
395     { "cdt",    tDAYZONE,  HOUR( 5) },  /* Cuba Daylight */
396     { "est",    tZONE,     HOUR( 6) },  /* Easter Island Standard */
397     { "edt",    tDAYZONE,  HOUR( 6) },  /* Easter Island Daylight */
398     { "sst",    tZONE,     HOUR(11) },  /* Samoa Standard */
399     { "ist",    tZONE,     -HOUR(2) },  /* Israel Standard */
400     { "idt",    tDAYZONE,  -HOUR(2) },  /* Israel Daylight */
401     { "idt",    tDAYZONE,  -(HOUR(3)+30) }, /* Iran Daylight */
402     { "ist",    tZONE,     -(HOUR(3)+30) }, /* Iran Standard */
403     { "cst",     tZONE,     -HOUR(8) }, /* China Standard */
404     { "cdt",     tDAYZONE,  -HOUR(8) }, /* China Daylight */
405     { "sst",     tZONE,     -HOUR(8) }, /* Singapore Standard */
406
407     /* Dubious (e.g., not in Olson's TIMEZONE package) or obsolete. */
408     { "gst",    tZONE,     HOUR( 3) },  /* Greenland Standard */
409     { "wat",    tZONE,     -HOUR(1) },  /* West Africa */
410     { "at",     tZONE,     HOUR( 2) },  /* Azores */
411     { "gst",    tZONE,     -HOUR(10) }, /* Guam Standard */
412     { "nft",    tZONE,     HOUR(3)+30 }, /* Newfoundland */
413     { "idlw",   tZONE,     HOUR(12) },  /* International Date Line West */
414     { "mewt",   tZONE,     -HOUR(1) },  /* Middle European Winter */
415     { "mest",   tDAYZONE,  -HOUR(1) },  /* Middle European Summer */
416     { "swt",    tZONE,     -HOUR(1) },  /* Swedish Winter */
417     { "sst",    tDAYZONE,  -HOUR(1) },  /* Swedish Summer */
418     { "fwt",    tZONE,     -HOUR(1) },  /* French Winter */
419     { "fst",    tDAYZONE,  -HOUR(1) },  /* French Summer */
420     { "bt",     tZONE,     -HOUR(3) },  /* Baghdad */
421     { "it",     tZONE,     -(HOUR(3)+30) }, /* Iran */
422     { "zp4",    tZONE,     -HOUR(4) },  /* USSR Zone 3 */
423     { "zp5",    tZONE,     -HOUR(5) },  /* USSR Zone 4 */
424     { "ist",    tZONE,     -(HOUR(5)+30) }, /* Indian Standard */
425     { "zp6",    tZONE,     -HOUR(6) },  /* USSR Zone 5 */
426     { "nst",    tZONE,     -HOUR(7) },  /* North Sumatra */
427     { "sst",    tZONE,     -HOUR(7) },  /* South Sumatra */
428     { "jt",     tZONE,     -(HOUR(7)+30) }, /* Java (3pm in Cronusland!) */
429     { "nzt",    tZONE,     -HOUR(12) }, /* New Zealand */
430     { "idle",   tZONE,     -HOUR(12) }, /* International Date Line East */
431     { "cat",    tZONE,     HOUR(10) },  /* -- expired 1967 */
432     { "nt",     tZONE,     HOUR(11) },  /* -- expired 1967 */
433     { "ahst",   tZONE,     HOUR(10) },  /* -- expired 1983 */
434     { "hdt",    tDAYZONE,  HOUR(10) },  /* -- expired 1986 */
435 #endif /* 0 */
436 };
437
438
439 /* ARGSUSED */
440 static void
441 date_error(char *s)
442 {
443     /* NOTREACHED */
444 }
445
446
447 static time_t
448 ToSeconds(time_t Hours, time_t Minutes, time_t Seconds, MERIDIAN Meridian)
449 {
450     if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 61)
451         return -1;
452     if (Meridian == MER24) {
453         if (Hours < 0 || Hours > 23)
454             return -1;
455     }
456     else {
457         if (Hours < 1 || Hours > 12)
458             return -1;
459         if (Hours == 12)
460             Hours = 0;
461         if (Meridian == MERpm)
462             Hours += 12;
463     }
464     return (Hours * 60L + Minutes) * 60L + Seconds;
465 }
466
467
468 static time_t
469 Convert(time_t Month, time_t Day, time_t Year,
470         time_t Hours, time_t Minutes, time_t Seconds,
471         MERIDIAN Meridian, DSTMODE dst)
472 {
473     static int  DaysNormal[13] = {
474         0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
475     };
476     static int  DaysLeap[13] = {
477         0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
478     };
479     static int  LeapYears[] = {
480         1972, 1976, 1980, 1984, 1988, 1992, 1996,
481         2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036
482     };
483     register int        *yp;
484     register int        *mp;
485     register time_t     Julian;
486     register int        i;
487     time_t              tod;
488
489     if (Year < 0)
490         Year = -Year;
491     if (Year < 100)
492         Year += 1900;
493     if (Year < EPOCH)
494         Year += 100;
495     for (mp = DaysNormal, yp = LeapYears; yp < ENDOF(LeapYears); yp++)
496         if (Year == *yp) {
497             mp = DaysLeap;
498             break;
499         }
500     if (Year < EPOCH || Year > END_OF_TIME
501      || Month < 1 || Month > 12
502      /* NOSTRICT *//* conversion from long may lose accuracy */
503      || Day < 1 || Day > mp[(int)Month])
504         return -1;
505
506     Julian = Day - 1 + (Year - EPOCH) * 365;
507     for (yp = LeapYears; yp < ENDOF(LeapYears); yp++, Julian++)
508         if (Year <= *yp)
509             break;
510     for (i = 1; i < Month; i++)
511         Julian += *++mp;
512     Julian *= SECSPERDAY;
513     Julian += yyTimezone * 60L;
514     if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0)
515         return -1;
516     Julian += tod;
517     tod = Julian;
518     if (dst == DSTon || (dst == DSTmaybe && localtime(&tod)->tm_isdst))
519         Julian -= DST_OFFSET * 60L * 60L;
520     return Julian;
521 }
522
523
524 static time_t
525 DSTcorrect(time_t Start, time_t Future)
526 {
527     time_t      StartDay;
528     time_t      FutureDay;
529
530     StartDay = (localtime(&Start)->tm_hour + 1) % 24;
531     FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
532     return (Future - Start) + (StartDay - FutureDay) * DST_OFFSET * 60L * 60L;
533 }
534
535
536 static time_t
537 RelativeMonth(time_t Start, time_t RelMonth)
538 {
539     struct tm   *tm;
540     time_t      Month;
541     time_t      Year;
542
543     tm = localtime(&Start);
544     Month = 12 * tm->tm_year + tm->tm_mon + RelMonth;
545     Year = Month / 12;
546     Month = Month % 12 + 1;
547     return DSTcorrect(Start,
548             Convert(Month, (time_t)tm->tm_mday, Year,
549                 (time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
550                 MER24, DSTmaybe));
551 }
552
553
554 static int
555 LookupWord(char *buff, register int length)
556 {
557     register char       *p;
558     register char       *q;
559     register TABLE      *tp;
560     register int        c;
561
562     p = buff;
563     c = p[0];
564
565     /* See if we have an abbreviation for a month. */
566     if (length == 3 || (length == 4 && p[3] == '.'))
567         for (tp = MonthDayTable; tp < ENDOF(MonthDayTable); tp++) {
568             q = tp->name;
569             if (c == q[0] && p[1] == q[1] && p[2] == q[2]) {
570                 yylval.Number = tp->value;
571                 return tp->type;
572             }
573         }
574     else
575         for (tp = MonthDayTable; tp < ENDOF(MonthDayTable); tp++)
576             if (c == tp->name[0] && strcmp(p, tp->name) == 0) {
577                 yylval.Number = tp->value;
578                 return tp->type;
579             }
580
581     /* Try for a timezone. */
582     for (tp = TimezoneTable; tp < ENDOF(TimezoneTable); tp++)
583         if (c == tp->name[0] && p[1] == tp->name[1]
584          && strcmp(p, tp->name) == 0) {
585             yylval.Number = tp->value;
586             return tp->type;
587         }
588
589     /* Try the units table. */
590     for (tp = UnitsTable; tp < ENDOF(UnitsTable); tp++)
591         if (c == tp->name[0] && strcmp(p, tp->name) == 0) {
592             yylval.Number = tp->value;
593             return tp->type;
594         }
595
596     /* Strip off any plural and try the units table again. */
597     if (--length > 0 && p[length] == 's') {
598         p[length] = '\0';
599         for (tp = UnitsTable; tp < ENDOF(UnitsTable); tp++)
600             if (c == tp->name[0] && strcmp(p, tp->name) == 0) {
601                 p[length] = 's';
602                 yylval.Number = tp->value;
603                 return tp->type;
604             }
605         p[length] = 's';
606     }
607     length++;
608
609     /* Drop out any periods. */
610     for (p = buff, q = (char*)buff; *q; q++)
611         if (*q != '.')
612             *p++ = *q;
613     *p = '\0';
614
615     /* Try the meridians. */
616     if (buff[1] == 'm' && buff[2] == '\0') {
617         if (buff[0] == 'a') {
618             yylval.Meridian = MERam;
619             return tMERIDIAN;
620         }
621         if (buff[0] == 'p') {
622             yylval.Meridian = MERpm;
623             return tMERIDIAN;
624         }
625     }
626
627     /* If we saw any periods, try the timezones again. */
628     if (p - buff != length) {
629         c = buff[0];
630         for (p = buff, tp = TimezoneTable; tp < ENDOF(TimezoneTable); tp++)
631             if (c == tp->name[0] && p[1] == tp->name[1]
632             && strcmp(p, tp->name) == 0) {
633                 yylval.Number = tp->value;
634                 return tp->type;
635             }
636     }
637
638     /* Unknown word -- assume GMT timezone. */
639     yylval.Number = 0;
640     return tZONE;
641 }
642
643
644 int
645 date_lex(void)
646 {
647     register char       c;
648     register char       *p;
649     char                buff[20];
650     register int        sign;
651     register int        i;
652     register int        nesting;
653
654     for ( ; ; ) {
655         /* Get first character after the whitespace. */
656         for ( ; ; ) {
657             while (isspace(*yyInput))
658                 yyInput++;
659             c = *yyInput;
660
661             /* Ignore RFC 822 comments, typically time zone names. */
662             if (c != LPAREN)
663                 break;
664             for (nesting = 1; (c = *++yyInput) != RPAREN || --nesting; )
665                 if (c == LPAREN)
666                     nesting++;
667                 else if (!IS7BIT(c) || c == '\0' || c == '\r'
668                      || (c == '\\' && ((c = *++yyInput) == '\0' || !IS7BIT(c))))
669                     /* Lexical error: bad comment. */
670                     return '?';
671             yyInput++;
672         }
673
674         /* A number? */
675         if (isdigit(c) || c == '-' || c == '+') {
676             if (c == '-' || c == '+') {
677                 sign = c == '-' ? -1 : 1;
678                 yyInput++;
679                 if (!isdigit(*yyInput))
680                     /* Skip the plus or minus sign. */
681                     continue;
682             }
683             else
684                 sign = 0;
685             for (i = 0; (c = *yyInput++) != '\0' && isdigit(c); )
686                 i = 10 * i + c - '0';
687             yyInput--;
688             yylval.Number = sign < 0 ? -i : i;
689             return sign ? tSNUMBER : tUNUMBER;
690         }
691
692         /* A word? */
693         if (isalpha(c)) {
694             for (p = buff; (c = *yyInput++) == '.' || isalpha(c); )
695                 if (p < &buff[sizeof buff - 1])
696                     *p++ = isupper(c) ? tolower(c) : c;
697             *p = '\0';
698             yyInput--;
699             return LookupWord(buff, p - buff);
700         }
701
702         return *yyInput++;
703     }
704 }
705
706
707 time_t
708 parsedate(char *p)
709 {
710     extern int          date_parse(void);
711     time_t              Start;
712
713     yyInput = p;
714
715     yyYear = 0;
716     yyMonth = 0;
717     yyDay = 0;
718     yyTimezone = 0;
719     yyDSTmode = DSTmaybe;
720     yyHour = 0;
721     yyMinutes = 0;
722     yySeconds = 0;
723     yyMeridian = MER24;
724     yyRelSeconds = 0;
725     yyRelMonth = 0;
726     yyHaveDate = 0;
727     yyHaveRel = 0;
728     yyHaveTime = 0;
729
730     if (date_parse() || yyHaveTime > 1 || yyHaveDate > 1)
731         return -1;
732
733     if (yyHaveDate || yyHaveTime) {
734         Start = Convert(yyMonth, yyDay, yyYear, yyHour, yyMinutes, yySeconds,
735                     yyMeridian, yyDSTmode);
736         if (Start < 0)
737             return -1;
738     }
739     else
740         return -1;
741
742     Start += yyRelSeconds;
743     if (yyRelMonth)
744         Start += RelativeMonth(Start, yyRelMonth);
745
746     /* Have to do *something* with a legitimate -1 so it's distinguishable
747      * from the error return value.  (Alternately could set errno on error.) */
748     return Start == -1 ? 0 : Start;
749 }
750
751
752 #ifdef TEST
753
754 #if YYDEBUG
755 extern int      yydebug;
756 #endif /* YYDEBUG */
757
758 /* ARGSUSED */
759 int
760 main(int ac, char *av[])
761 {
762     char        buff[128];
763     time_t      d;
764
765 #if YYDEBUG
766     yydebug = 1;
767 #endif /* YYDEBUG */
768
769     (void)printf("Enter date, or blank line to exit.\n\t> ");
770     for ( ; ; ) {
771         (void)printf("\t> ");
772         (void)fflush(stdout);
773         if (gets(buff) == NULL || buff[0] == '\n')
774             break;
775 #if YYDEBUG
776         if (strcmp(buff, "yydebug") == 0) {
777             yydebug = !yydebug;
778             printf("yydebug = %s\n", yydebug ? "on" : "off");
779             continue;
780         }
781 #endif /* YYDEBUG */
782         d = parsedate(buff, (TIMEINFO *)NULL);
783         if (d == -1)
784             (void)printf("Bad format - couldn't convert.\n");
785         else
786             (void)printf("%s", ctime(&d));
787     }
788
789     exit(0);
790     /* NOTREACHED */
791 }
792 #endif /* TEST */