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