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