]> diplodocus.org Git - nmh/blob - sbr/dtimep.lex
Updating mh_sequence
[nmh] / sbr / dtimep.lex
1 %{
2 #include <h/nmh.h>
3 #include <h/tws.h>
4
5 /* Since we're looking at a string at a time, don't worry about
6 * wrapping to the next buffer.
7 */
8 #define yywrap() 1
9 #define YY_SKIP_YYWRAP
10
11 #define YY_NO_UNPUT
12
13 /* This is the tricky thing that makes this function cool. We
14 * replace the traditional int yylex(void) declaration with our
15 * dparsetime() declaration, essentially piggy-backing off the
16 * utility of the yylex() function and adding what we need to make
17 * the parsing function useful to us.
18 */
19 #define YY_DECL struct tws *dparsetime(char *lexstr)
20
21 /* yyerminate() is called after the input string is matched to
22 * completion (actually, when the lexer reaches an EOF). The only
23 * thing that really needs to be in this macro function is the
24 * return call, which must be substituted inline into dparsetime.
25 */
26
27 #define yyterminate() (void)yy_delete_buffer(lexhandle); \
28 if(!(tw.tw_flags & TW_SUCC)) { \
29 return (struct tws *)NULL; \
30 } \
31 if(tw.tw_year < 1960) \
32 tw.tw_year += 1900; \
33 if(tw.tw_year < 1960) \
34 tw.tw_year += 100; \
35 return(&tw)
36
37 /*
38 * Patchable flag that says how to interpret NN/NN/NN dates. When
39 * true, we do it European style: DD/MM/YY. When false, we do it
40 * American style: MM/DD/YY. Of course, these are all non-RFC822
41 * compliant.
42 */
43 int europeandate = 0;
44
45 /*
46 * Table to convert month names to numeric month. We use the
47 * fact that the low order 5 bits of the sum of the 2nd & 3rd
48 * characters of the name is a hash with no collisions for the 12
49 * valid month names. (The mask to 5 bits maps any combination of
50 * upper and lower case into the same hash value).
51 */
52 static int month_map[] = {
53 0,
54 6, /* 1 - Jul */
55 3, /* 2 - Apr */
56 5, /* 3 - Jun */
57 0,
58 10, /* 5 - Nov */
59 0,
60 1, /* 7 - Feb */
61 11, /* 8 - Dec */
62 0,
63 0,
64 0,
65 0,
66 0,
67 0,
68 0, /*15 - Jan */
69 0,
70 0,
71 0,
72 2, /*19 - Mar */
73 0,
74 8, /*21 - Sep */
75 0,
76 9, /*23 - Oct */
77 0,
78 0,
79 4, /*26 - May */
80 0,
81 7 /*28 - Aug */
82 };
83
84 /*
85 * Lookup table for day-of-week using the same hash trick as for above name-of-
86 * month table, but using the first and second character, not second and third.
87 *
88 * Compute index into table using: (day_name[0] & 7) + (day_name[1] & 4)
89 */
90 static int day_map[] = {
91 0,
92 0,
93 0,
94 6, /* 3 - Sat */
95 4, /* 4 - Thu */
96 0,
97 5, /* 6 - Fri */
98 0, /* 7 - Sun */
99 2, /* 8 - Tue */
100 1 /* 9 - Mon */,
101 0,
102 3 /*11 - Wed */
103 };
104
105 /* The SET* macros will parse for the appropriate field, and leave the
106 * cp pointer at the first character after the desired field. Be
107 * careful with variable-length fields or alpha-num mixes.
108
109 * The SKIP* macros skip over characters of a particular class and
110 * leave cp at the position of the first character that doesn't match
111 * that class. Correspondingly, SKIPTO* skips until it reaches a
112 * character of a particular class.
113 */
114
115 #define INIT() { cp = yytext;}
116 #define SETWDAY() { tw.tw_wday= day_map[(cp[0] & 7) + (cp[1] & 4)]; \
117 tw.tw_flags &= ~TW_SDAY; tw.tw_flags |= TW_SEXP; \
118 SKIPA(); }
119 #define SETMON() { cp++; \
120 tw.tw_mon = month_map[(cp[0] + cp[1]) & 0x1f]; \
121 SKIPA(); }
122 #define SETMON_NUM() { tw.tw_mon = atoi(cp)-1; \
123 SKIPD(); }
124 #define SETYEAR() { tw.tw_year = atoi(cp); \
125 SKIPD(); }
126 #define SETDAY() { tw.tw_mday = atoi(cp); \
127 tw.tw_flags |= TW_YES; \
128 SKIPD(); }
129 #define SETTIME() { tw.tw_hour = atoi(cp); \
130 cp += 2; \
131 SKIPTOD(); \
132 tw.tw_min = atoi(cp); \
133 cp += 2; \
134 if(*cp == ':') { \
135 tw.tw_sec = atoi(++cp); SKIPD(); } }
136 #define SETZONE(x) { tw.tw_zone = ((x)/100)*60+(x)%100; \
137 tw.tw_flags |= TW_SZEXP; \
138 SKIPD(); }
139 #define SETDST() { tw.tw_flags |= TW_DST; }
140 #define SKIPD() { while ( isdigit(*cp++) ) ; \
141 --cp; }
142 #define SKIPTOD() { while ( !isdigit(*cp++) ) ; \
143 --cp; }
144 #define SKIPA() { while ( isalpha(*cp++) ) ; \
145 --cp; }
146 #define SKIPTOA() { while ( !isalpha(*cp++) ) ; \
147 --cp; }
148 #define SKIPSP() { while ( isspace(*cp++) ) ; \
149 --cp; }
150 #define SKIPTOSP() { while ( !isspace(*cp++) ) ; \
151 --cp; }
152
153 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
154 # ifdef TIME_WITH_SYS_TIME
155 # include <sys/time.h>
156 # include <time.h>
157 # else
158 # ifdef HAVE_SYS_TIME_H
159 # include <sys/time.h>
160 # else
161 # include <time.h>
162 # endif
163 # endif
164
165 static void
166 zonehack (struct tws *tw)
167 {
168 register 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 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
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]un(e)?)
198 jul ([Jj]ul(y)?)
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
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 register char *cp;
230 static struct tws tw;
231
232 memset(&tw,0,sizeof(struct tws));
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 if(europeandate) {
315 /* DD/MM/YY */
316 SETDAY();
317 SKIPTOD();
318 SETMON_NUM();
319 } else {
320 /* MM/DD/YY */
321 SETMON_NUM();
322 SKIPTOD();
323 SETDAY();
324 }
325 SKIPTOD();
326 SETYEAR();
327 SKIPTOD();
328 SETTIME();
329 }
330 {D}("-"|"/"){D}("-"|"/"){YEAR} {
331 INIT();
332 if(europeandate) {
333 /* DD/MM/YY */
334 SETDAY();
335 SKIPTOD();
336 SETMON_NUM();
337 } else {
338 /* MM/DD/YY */
339 SETMON_NUM();
340 SKIPTOD();
341 SETDAY();
342 }
343 SKIPTOD();
344 SETYEAR();
345 }
346
347 "[Aa][Mm]"
348 "[Pp][Mm]" tw.tw_hour += 12;
349
350 "+"{D}{d}{d} {
351 INIT();
352 SKIPTOD();
353 SETZONE(atoi(cp));
354 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
355 zonehack (&tw);
356 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
357 // yyterminate();
358 }
359 "-"{D}{d}{d} {
360 INIT();
361 SKIPTOD();
362 SETZONE(-atoi(cp));
363 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
364 zonehack (&tw);
365 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
366 // yyterminate();
367
368 }
369 "-"?("ut"|"UT") INIT(); SETZONE(0);
370 "-"?("gmt"|"GMT") INIT(); SETZONE(0);
371 "-"?("est"|"EST") INIT(); SETZONE(-500);
372 "-"?("edt"|"EDT") INIT(); SETDST(); SETZONE(-500);
373 "-"?("cst"|"CST") INIT(); SETZONE(-600);
374 "-"?("cdt"|"CDT") INIT(); SETDST(); SETZONE(-600);
375 "-"?("mst"|"MST") INIT(); SETZONE(-700);
376 "-"?("mdt"|"MDT") INIT(); SETDST(); SETZONE(-700);
377 "-"?("pst"|"PST") INIT(); SETZONE(-800);
378 "-"?("pdt"|"PDT") INIT(); SETDST(); SETZONE(-800);
379 "-"?("nst"|"NST") INIT(); SETZONE(-330);
380 "-"?("ast"|"AST") INIT(); SETZONE(-400);
381 "-"?("adt"|"ADT") INIT(); SETDST(); SETZONE(-400);
382 "-"?("hst"|"HST") INIT(); SETZONE(-1000);
383 "-"?("hdt"|"HDT") INIT(); SETDST(); SETZONE(-1000);
384 "-"?([a-z]{3}|[A-Z]{3}) ;
385 .|\n