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