Clean up time handling; always assume we have tzset().
[mmh] / sbr / dtimep.lex
1 /* dtimep.lex exceeds the default table capacities for some old versions
2  * of lex (and the minimum defaults as specified by POSIX).  The following
3  * choices meet or exceed the lex defaults for older SunOS4.x, Solaris,
4  * HPUX, and AIX.
5  */
6 %e4000
7 %p7000
8 %n2500
9 %a5000
10 %{
11 #include <h/nmh.h>
12 #include <h/tws.h>
13 #include <time.h>
14
15   /* Since we're looking at a string at a time, don't worry about
16    *  wrapping to the next buffer.
17    */
18 #define yywrap() 1
19 #define YY_SKIP_YYWRAP
20
21 #define YY_NO_INPUT
22
23   /* This is the tricky thing that makes this function cool.  We
24    *  replace the traditional int yylex(void) declaration with our
25    *  dparsetime() declaration, essentially piggy-backing off the
26    *  utility of the yylex() function and adding what we need to make
27    *  the parsing function useful to us.
28    */
29 #define YY_DECL struct tws *dparsetime(char *lexstr)
30
31   /* yyerminate() is called after the input string is matched to
32    * completion (actually, when the lexer reaches an EOF).  The only
33    * thing that really needs to be in this macro function is the
34    * return call, which must be substituted inline into dparsetime.
35    */
36
37 #define yyterminate() (void)yy_delete_buffer(lexhandle); \
38         if(!(tw.tw_flags & TW_SUCC)) { \
39                 return (struct tws *)NULL; \
40         } \
41         if(tw.tw_year < 1970) \
42                 tw.tw_year += 1900; \
43         if(tw.tw_year < 1970) \
44                 tw.tw_year += 100; \
45         return(&tw)
46
47 /*
48  * Patchable flag that says how to interpret NN/NN/NN dates. When
49  * true, we do it European style: DD/MM/YY. When false, we do it
50  * American style: MM/DD/YY.  Of course, these are all non-RFC822
51  * compliant.
52  */
53 int europeandate = 0;
54
55 /*
56  * Table to convert month names to numeric month.  We use the
57  * fact that the low order 5 bits of the sum of the 2nd & 3rd
58  * characters of the name is a hash with no collisions for the 12
59  * valid month names.  (The mask to 5 bits maps any combination of
60  * upper and lower case into the same hash value).
61  */
62 static int month_map[] = {
63         0,
64         6,      /* 1 - Jul */
65         3,      /* 2 - Apr */
66         5,      /* 3 - Jun */
67         0,
68         10,     /* 5 - Nov */
69         0,
70         1,      /* 7 - Feb */
71         11,     /* 8 - Dec */
72         0,
73         0,
74         0,
75         0,
76         0,
77         0,
78         0,      /*15 - Jan */
79         0,
80         0,
81         0,
82         2,      /*19 - Mar */
83         0,
84         8,      /*21 - Sep */
85         0,
86         9,      /*23 - Oct */
87         0,
88         0,
89         4,      /*26 - May */
90         0,
91         7       /*28 - Aug */
92 };
93
94 /*
95  * Lookup table for day-of-week using the same hash trick as for above name-of-
96  * month table, but using the first and second character, not second and third.
97  *
98  * Compute index into table using: (day_name[0] & 7) + (day_name[1] & 4)
99  */
100 static int day_map[] = {
101         0,
102         0,
103         0,
104         6,      /* 3 - Sat */
105         4,      /* 4 - Thu */
106         0,
107         5,      /* 6 - Fri */
108         0,      /* 7 - Sun */
109         2,      /* 8 - Tue */
110         1       /* 9 - Mon */,
111         0,
112         3       /*11 - Wed */
113 };
114
115 /* The SET* macros will parse for the appropriate field, and leave the
116  * cp pointer at the first character after the desired field. Be
117  * careful with variable-length fields or alpha-num mixes.
118
119  * The SKIP* macros skip over characters of a particular class and
120  * leave cp at the position of the first character that doesn't match
121  * that class. Correspondingly, SKIPTO* skips until it reaches a
122  * character of a particular class.
123  */
124
125 #define INIT() { cp = yytext;}
126 #define SETWDAY()  { tw.tw_wday= day_map[(cp[0] & 7) + (cp[1] & 4)]; \
127         tw.tw_flags &= ~TW_SDAY; tw.tw_flags |= TW_SEXP; SKIPA(); }
128 #define SETMON()  { cp++; tw.tw_mon = month_map[(cp[0] + cp[1]) & 0x1f]; \
129         SKIPA(); }
130 #define SETMON_NUM()  { tw.tw_mon = atoi(cp)-1; SKIPD(); }
131 #define SETYEAR()  { tw.tw_year = atoi(cp); SKIPD(); }
132 #define SETDAY()  { tw.tw_mday = atoi(cp); tw.tw_flags |= TW_YES; SKIPD(); }
133 #define SETTIME()  { tw.tw_hour = atoi(cp); cp += 2; SKIPTOD(); \
134         tw.tw_min = atoi(cp); cp += 2; if(*cp == ':') { \
135         tw.tw_sec = atoi(++cp); SKIPD(); } }
136 #define SETZONE(x)  { tw.tw_zone = ((x)/100)*60+(x)%100; \
137         tw.tw_flags |= TW_SZEXP; SKIPD(); }
138 #define SETDST()  { tw.tw_flags |= TW_DST; }
139 #define SKIPD()  { while ( isdigit(*cp++) ) ; --cp; }
140 #define SKIPTOD()  { while ( !isdigit(*cp++) ) ; --cp; }
141 #define SKIPA()  { while ( isalpha(*cp++) ) ; --cp; }
142 #define SKIPTOA()  { while ( !isalpha(*cp++) ) ; --cp; }
143 #define SKIPSP()  { while ( isspace(*cp++) ) ; --cp; }
144 #define SKIPTOSP()  { while ( !isspace(*cp++) ) ; --cp; }
145
146 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
147 # ifdef TIME_WITH_SYS_TIME
148 #  include <sys/time.h>
149 #  include <time.h>
150 # else
151 #  ifdef HAVE_SYS_TIME_H
152 #   include <sys/time.h>
153 #  else
154 #   include <time.h>
155 #  endif
156 # endif
157
158 static void
159 zonehack (struct tws *tw)
160 {
161         register struct tm *tm;
162
163         if (dmktime (tw) == (time_t) -1)
164                 return;
165
166         tm = localtime (&tw->tw_clock);
167         if (tm->tm_isdst) {
168                 tw->tw_flags |= TW_DST;
169                 tw->tw_zone -= 60;
170         }
171 }
172 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
173 %}
174
175 sun     ([Ss]un(day)?)
176 mon     ([Mm]on(day)?)
177 tue     ([Tt]ue(sday)?)
178 wed     ([Ww]ed(nesday)?)
179 thu     ([Tt]hu(rsday)?)
180 fri     ([Ff]ri(day)?)
181 sat     ([Ss]at(urday)?)
182
183 DAY     ({sun}|{mon}|{tue}|{wed}|{thu}|{fri}|{sat})
184
185 jan     ([Jj]an(uary)?)
186 feb     ([Ff]eb(ruary)?)
187 mar     ([Mm]ar(ch)?)
188 apr     ([Aa]pr(il)?)
189 may     ([Mm]ay)
190 jun     ([Jj]un(e)?)
191 jul     ([Jj]ul(y)?)
192 aug     ([Aa]ug(ust)?)
193 sep     ([Ss]ep(tember)?)
194 oct     ([Oo]ct(ober)?)
195 nov     ([Nn]ov(ember)?)
196 dec     ([Dd]ec(ember)?)
197
198 MONTH   ({jan}|{feb}|{mar}|{apr}|{may}|{jun}|{jul}|{aug}|{sep}|{oct}|{nov}|{dec})
199
200 TIME    ({D}:{d}{d}(:{d}{d})?)
201
202 /*
203  * The year can either be 2 digits, or 4. However, after
204  * Y2K, we found that some MUA were reporting the year 100, hence
205  * the middle term here. yyterminate() resolves the actual
206  * issues with 2-digit years.
207  */
208
209 YEAR    (({d}{d})|(1{d}{d})|({d}{4}))
210
211 w       ([ \t]*)
212 W       ([ \t]+)
213 D       ([0-9]?[0-9])
214 d       [0-9]
215 nl      [ \t\n()]
216
217 %%
218 %{
219         /* This section begins the definition of dparsetime().
220          * Put here any local variable definitions and initializations */
221
222         YY_BUFFER_STATE lexhandle;
223
224         register unsigned char *cp;
225         static struct tws tw;
226
227         memset(&tw,0,sizeof(struct tws));
228
229         lexhandle = yy_scan_string(lexstr);
230 %}
231
232 {DAY}","?{W}{MONTH}{W}{D}{W}{TIME}{W}{YEAR}  {
233         INIT();
234         SETWDAY();
235         SKIPTOA();
236         SETMON();
237         SKIPTOD();
238         SETDAY();
239         SKIPTOD();
240         SETTIME();
241         SKIPTOD();
242         SETYEAR();
243 }
244
245 {DAY}","?{W}{D}{W}{MONTH}{W}{YEAR}{W}{TIME}  {
246         INIT();
247         SETWDAY();
248         SKIPTOD();
249         SETDAY();
250         SKIPTOA();
251         SETMON();
252         SKIPTOD();
253         SETYEAR();
254         SKIPTOD();
255         SETTIME();
256 }
257 {D}{W}{MONTH}{W}{YEAR}{W}{TIME}  {
258         INIT();
259         SETDAY();
260         SKIPTOA();
261         SETMON();
262         SKIPTOD();
263         SETYEAR();
264         SKIPTOD();
265         SETTIME();
266 }
267 {DAY}","?{W}{MONTH}{W}{D}","?{W}{YEAR}","?{W}{TIME}  {
268         INIT();
269         SETWDAY();
270         SKIPTOA();
271         SETMON();
272         SKIPTOD();
273         SETDAY();
274         SKIPTOD();
275         SETYEAR();
276         SKIPTOD();
277         SETTIME();
278 }
279 {DAY}","?{W}{MONTH}{W}{D}","?{W}{YEAR}  {
280         INIT();
281         SETWDAY();
282         SKIPTOA();
283         SETMON();
284         SKIPTOD();
285         SETDAY();
286         SKIPTOD();
287         SETYEAR();
288 }
289 {MONTH}{W}{D}","?{W}{YEAR}","?{W}{DAY}  {
290         INIT();
291         SETMON();
292         SKIPTOD();
293         SETDAY();
294         SKIPTOD();
295         SETYEAR();
296         SKIPTOA();
297         SETWDAY();
298 }
299 {MONTH}{W}{D}","?{W}{YEAR}  {
300         INIT();
301         SETMON();
302         SKIPTOD();
303         SETDAY();
304         SKIPTOD();
305         SETYEAR();
306 }
307 {D}("-"|"/"){D}("-"|"/"){YEAR}{W}{TIME}  {
308         INIT();
309         if(europeandate) {
310           /* DD/MM/YY */
311         SETDAY();
312         SKIPTOD();
313         SETMON_NUM();
314         } else {
315           /* MM/DD/YY */
316         SETMON_NUM();
317         SKIPTOD();
318         SETDAY();
319         }
320         SKIPTOD();
321         SETYEAR();
322         SKIPTOD();
323         SETTIME();
324 }
325 {D}("-"|"/"){D}("-"|"/"){YEAR}  {
326         INIT();
327         if(europeandate) {
328                 /* DD/MM/YY */
329                 SETDAY();
330                 SKIPTOD();
331                 SETMON_NUM();
332         } else {
333                 /* MM/DD/YY */
334                 SETMON_NUM();
335                 SKIPTOD();
336                 SETDAY();
337         }
338         SKIPTOD();
339         SETYEAR();
340 }
341
342 "[Aa][Mm]"
343 "[Pp][Mm]"  tw.tw_hour += 12;
344
345 "+"{D}{d}{d}  {
346         INIT();
347         SKIPTOD();
348         SETZONE(atoi(cp));
349 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
350         zonehack (&tw);
351 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
352         yyterminate();
353 }
354 "-"{D}{d}{d}  {
355         INIT();
356         SKIPTOD();
357         SETZONE(-atoi(cp));
358 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
359         zonehack (&tw);
360 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
361         yyterminate();
362
363 }
364 {nl}("ut"|"UT")         INIT(); SETZONE(0); yyterminate();
365 {nl}("gmt"|"GMT")       INIT(); SETZONE(0); yyterminate();
366 {nl}("est"|"EST")       INIT(); SETZONE(-500); yyterminate();
367 {nl}("edt"|"EDT")       { INIT(); SETDST(); SETZONE(-500); yyterminate(); }
368 {nl}("cst"|"CST")       INIT(); SETZONE(-600); yyterminate();
369 {nl}("cdt"|"CDT")       { INIT(); SETDST(); SETZONE(-600); yyterminate(); }
370 {nl}("mst"|"MST")       INIT(); SETZONE(-700); yyterminate();
371 {nl}("mdt"|"MDT")       { INIT(); SETDST(); SETZONE(-700); yyterminate(); }
372 {nl}("pst"|"PST")       INIT(); SETZONE(-800); yyterminate();
373 {nl}("pdt"|"PDT")       { INIT(); SETDST(); SETZONE(-800); yyterminate(); }
374 {nl}("nst"|"NST")       INIT(); SETZONE(-330); yyterminate();
375 {nl}("ast"|"AST")       INIT(); SETZONE(-400); yyterminate();
376 {nl}("adt"|"ADT")       { INIT(); SETDST(); SETZONE(-400); yyterminate(); }
377 {nl}("hst"|"HST")       INIT(); SETZONE(-1000); yyterminate();
378 {nl}("hdt"|"HDT")       { INIT(); SETDST(); SETZONE(-1000); yyterminate(); }
379 .|\n
380
381 %%
382 /* This is a portable way to squash a warning about the yyunput()
383  * function being static but never used. It costs us a tiny amount
384  * of extra code in the binary but the other options are:
385  * "%option nounput" which is flex-specific
386  * makefile hackery just to compile dtimep.c with different flags
387  */
388 void dtimep_yyunput(int c)
389 {
390         unput(c);
391 }