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