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