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