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