]> diplodocus.org Git - nmh/blob - sbr/dtimep.l
699e90e44f2abc3b4634f6a4e8325100a1a53e7a
[nmh] / sbr / dtimep.l
1 /* dtimep.l -- parse dates and times.
2 *
3 * This exceeds the default table capacities for some old versions
4 * of lex (and the minimum defaults as specified by POSIX). The following
5 * choices meet or exceed the lex defaults for older SunOS4.x, Solaris,
6 * HPUX, and AIX.
7 */
8 %e4000
9 %p7000
10 %n2500
11 %a5000
12 %{
13 #include "h/nmh.h"
14 #include "h/mh.h"
15 #include "dtime.h"
16 #include "h/utils.h"
17 #include "h/tws.h"
18
19 /* Since we're looking at a string at a time, don't worry about
20 * wrapping to the next buffer.
21 */
22 #if YY_FLEX_MAJOR_VERSION == 2 && \
23 YY_FLEX_MINOR_VERSION == 6 && \
24 YY_FLEX_SUBMINOR_VERSION == 3
25 /* https://github.com/westes/flex/issues/162 */
26 #undef yywrap
27 #endif
28 #define yywrap() 1
29 #define YY_SKIP_YYWRAP
30
31 #define YY_NO_INPUT
32
33 /* This is the tricky thing that makes this function cool. We
34 * replace the traditional int yylex(void) declaration with our
35 * dparsetime() declaration, essentially piggy-backing off the
36 * utility of the yylex() function and adding what we need to make
37 * the parsing function useful to us.
38 */
39 #define YY_DECL struct tws *dparsetime(char *lexstr)
40
41 /* yyterminate() is called after the input string is matched to
42 * completion (actually, when the lexer reaches an EOF). The only
43 * thing that really needs to be in this macro function is the
44 * return call, which must be substituted inline into dparsetime.
45 */
46
47 #define yyterminate() (void)yy_delete_buffer(lexhandle); \
48 if(!(tw.tw_flags & TW_SUCC)) { \
49 return NULL; \
50 } \
51 if(tw.tw_year < 1970) \
52 tw.tw_year += 1900; \
53 if(tw.tw_year < 1970) \
54 tw.tw_year += 100; \
55 return &tw
56
57 /*
58 * Table to convert month names to numeric month. We use the
59 * fact that the low order 5 bits of the sum of the 2nd & 3rd
60 * characters of the name is a hash with no collisions for the 12
61 * valid month names. (The mask to 5 bits maps any combination of
62 * upper and lower case into the same hash value).
63 */
64 static int month_map[] = {
65 0,
66 6, /* 1 - Jul */
67 3, /* 2 - Apr */
68 5, /* 3 - Jun */
69 0,
70 10, /* 5 - Nov */
71 0,
72 1, /* 7 - Feb */
73 11, /* 8 - Dec */
74 0,
75 0,
76 0,
77 0,
78 0,
79 0,
80 0, /*15 - Jan */
81 0,
82 0,
83 0,
84 2, /*19 - Mar */
85 0,
86 8, /*21 - Sep */
87 0,
88 9, /*23 - Oct */
89 0,
90 0,
91 4, /*26 - May */
92 0,
93 7 /*28 - Aug */
94 };
95
96 /*
97 * Lookup table for day-of-week using the same hash trick as for above name-of-
98 * month table, but using the first and second character, not second and third.
99 *
100 * Compute index into table using: (day_name[0] & 7) + (day_name[1] & 4)
101 */
102 static int day_map[] = {
103 0,
104 0,
105 0,
106 6, /* 3 - Sat */
107 4, /* 4 - Thu */
108 0,
109 5, /* 6 - Fri */
110 0, /* 7 - Sun */
111 2, /* 8 - Tue */
112 1 /* 9 - Mon */,
113 0,
114 3 /*11 - Wed */
115 };
116
117 /* The SET* macros will parse for the appropriate field, and leave the
118 * cp pointer at the first character after the desired field. Be
119 * careful with variable-length fields or alpha-num mixes.
120
121 * The SKIP* macros skip over characters of a particular class and
122 * leave cp at the position of the first character that doesn't match
123 * that class. Correspondingly, SKIPTO* skips until it reaches a
124 * character of a particular class.
125 */
126
127 #define INIT() { cp = yytext;}
128 #define SETWDAY() { tw.tw_wday= day_map[(cp[0] & 7) + (cp[1] & 4)]; \
129 tw.tw_flags &= ~TW_SDAY; tw.tw_flags |= TW_SEXP; \
130 SKIPA(); }
131 #define SETMON() { cp++; \
132 tw.tw_mon = month_map[(((unsigned char) cp[0]) + ((unsigned char) cp[1])) & 0x1f]; \
133 SKIPA(); }
134 #define SETMON_NUM() { tw.tw_mon = atoi(cp)-1; \
135 SKIPD(); }
136 #define SETYEAR() { tw.tw_year = atoi(cp); \
137 SKIPD(); }
138 #define SETDAY() { tw.tw_mday = atoi(cp); \
139 tw.tw_flags |= TW_SUCC; \
140 SKIPD(); }
141 #define SETTIME() { tw.tw_hour = atoi(cp); \
142 cp += 2; \
143 SKIPTOD(); \
144 tw.tw_min = atoi(cp); \
145 cp += 2; \
146 if(*cp == ':') { \
147 tw.tw_sec = atoi(++cp); SKIPD(); } }
148 #define SETZONE(x) { tw.tw_zone = ((x)/100)*60+(x)%100; \
149 tw.tw_flags |= TW_SZEXP; \
150 SKIPD(); }
151 #define SETDST() { tw.tw_flags |= TW_DST; }
152 #define SKIPD() { while ( isdigit((unsigned char) *cp++) ) ; \
153 --cp; }
154 #define SKIPTOD() { while ( !isdigit((unsigned char) *cp++) ) ; \
155 --cp; }
156 #define SKIPA() { while ( isalpha((unsigned char) *cp++) ) ; \
157 --cp; }
158 #define SKIPTOA() { while ( !isalpha((unsigned char) *cp++) ) ; \
159 --cp; }
160
161 # ifdef HAVE_SYS_TIME_H
162 # include <sys/time.h>
163 # endif
164 #include <time.h>
165
166 static void
167 zonehack (struct tws *tw)
168 {
169 struct tm *tm;
170
171 if (dmktime (tw) == (time_t) -1)
172 return;
173
174 tm = localtime (&tw->tw_clock);
175 if (tm->tm_isdst) {
176 tw->tw_flags |= TW_DST;
177 tw->tw_zone -= 60;
178 }
179 }
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]une?
198 jul [Jj]uly?
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 char *cp;
231 static struct tws tw;
232
233 ZERO(&tw);
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 /* MM/DD/YY */
316 SETMON_NUM();
317 SKIPTOD();
318 SETDAY();
319 SKIPTOD();
320 SETYEAR();
321 SKIPTOD();
322 SETTIME();
323 }
324 {D}("-"|"/"){D}("-"|"/"){YEAR} {
325 INIT();
326 /* MM/DD/YY */
327 SETMON_NUM();
328 SKIPTOD();
329 SETDAY();
330 SKIPTOD();
331 SETYEAR();
332 }
333
334 "[Aa][Mm]"
335 "[Pp][Mm]" tw.tw_hour += 12;
336
337 "+"{D}{d}{d} {
338 INIT();
339 SKIPTOD();
340 SETZONE(atoi(cp));
341 zonehack (&tw);
342 yyterminate();
343 }
344 "-"{D}{d}{d} {
345 INIT();
346 SKIPTOD();
347 SETZONE(-atoi(cp));
348 zonehack (&tw);
349 yyterminate();
350
351 }
352 {nl}("ut"|"UT") INIT(); SETZONE(0); yyterminate();
353 {nl}("gmt"|"GMT") INIT(); SETZONE(0); yyterminate();
354 {nl}("est"|"EST") INIT(); SETZONE(-500); yyterminate();
355 {nl}("edt"|"EDT") { INIT(); SETDST(); SETZONE(-500);
356 yyterminate(); }
357 {nl}("cst"|"CST") INIT(); SETZONE(-600); yyterminate();
358 {nl}("cdt"|"CDT") { INIT(); SETDST(); SETZONE(-600);
359 yyterminate(); }
360 {nl}("mst"|"MST") INIT(); SETZONE(-700); yyterminate();
361 {nl}("mdt"|"MDT") { INIT(); SETDST(); SETZONE(-700);
362 yyterminate(); }
363 {nl}("pst"|"PST") INIT(); SETZONE(-800); yyterminate();
364 {nl}("pdt"|"PDT") { INIT(); SETDST(); SETZONE(-800);
365 yyterminate(); }
366 {nl}("nst"|"NST") INIT(); SETZONE(-330); yyterminate();
367 {nl}("ast"|"AST") INIT(); SETZONE(-400); yyterminate();
368 {nl}("adt"|"ADT") { INIT(); SETDST(); SETZONE(-400);
369 yyterminate(); }
370 {nl}("hst"|"HST") INIT(); SETZONE(-1000); yyterminate();
371 {nl}("hdt"|"HDT") { INIT(); SETDST(); SETZONE(-1000);
372 yyterminate(); }
373 .|\n
374
375 %%
376 /* This is a portable way to squash a warning about the yyunput()
377 * function being static but never used. It costs us a tiny amount
378 * of extra code in the binary but the other options are:
379 * "%option nounput" which is flex-specific
380 * makefile hackery just to compile dtimep.c with different flags
381 */
382 void
383 dtimep_yyunput(int c)
384 {
385 unput(c);
386 }