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