1 /*
2 ** dtimep.lex exceeds the default table capacities for some old versions
3 ** of lex (and the minimum defaults as specified by POSIX).  The following
4 ** choices meet or exceed the lex defaults for older SunOS4.x, Solaris,
5 ** HPUX, and AIX.
6 */
7 %e4000
8 %p7000
9 %n2500
10 %a5000
11 %{
12 #include <time.h>
13 #include <ctype.h>
14 #include <h/tws.h>
15 
16 /*
17 ** Since we're looking at a string at a time, don't worry about
18 ** wrapping to the next buffer.
19 */
20 #define yywrap() 1
21 #define YY_SKIP_YYWRAP
22 
23 #define YY_NO_INPUT
24 
25 /*
26 ** This is the tricky thing that makes this function cool.  We
27 ** replace the traditional int yylex(void) declaration with our
28 ** dparsetime() declaration, essentially piggy-backing off the
29 ** utility of the yylex() function and adding what we need to make
30 ** the parsing function useful to us.
31 */
32 #define YY_DECL struct tws *dparsetime(char *lexstr)
33 
34 /*
35 ** yyerminate() is called after the input string is matched to
36 ** completion (actually, when the lexer reaches an EOF).  The only
37 ** thing that really needs to be in this macro function is the
38 ** return call, which must be substituted inline into dparsetime.
39 */
40 
41 #define yyterminate() (void)yy_delete_buffer(lexhandle); \
42 	if(!(tw.tw_flags & TW_SUCC)) { \
43 		return (struct tws *)NULL; \
44 	} \
45 	if(tw.tw_year < 1970) \
46 		tw.tw_year += 1900; \
47 	if(tw.tw_year < 1970) \
48 		tw.tw_year += 100; \
49 	return(&tw)
50 
51 /*
52 ** Patchable flag that says how to interpret NN/NN/NN dates. When
53 ** true, we do it European style: DD/MM/YY. When false, we do it
54 ** American style: MM/DD/YY.  Of course, these are all non-RFC822
55 ** compliant.
56 */
57 int europeandate = 0;
58 
59 static int
name2num(char * name,char * names[])60 name2num(char *name, char *names[])
61 {
62 	int i;
63 
64 	for (i=0; names[i]; i++) {
65 		if (strncasecmp(name, names[i], strlen(names[i]))==0) {
66 			return i;
67 		}
68 	}
69 	return 0;
70 }
71 
72 /*
73 ** The SET* macros will parse for the appropriate field, and leave the
74 ** cp pointer at the first character after the desired field. Be
75 ** careful with variable-length fields or alpha-num mixes.
76 **
77 ** The SKIP* macros skip over characters of a particular class and
78 ** leave cp at the position of the first character that doesn't match
79 ** that class. Correspondingly, SKIPTO* skips until it reaches a
80 ** character of a particular class.
81 */
82 
83 #define INIT() { cp = yytext;}
84 #define SETWDAY()  { tw.tw_wday = name2num(cp, tw_dotw); \
85 	tw.tw_flags &= ~TW_SDAY; tw.tw_flags |= TW_SEXP; SKIPA(); }
86 #define SETMON()  { tw.tw_mon = name2num(cp, tw_moty); SKIPA(); }
87 #define SETMON_NUM()  { tw.tw_mon = atoi(cp)-1; SKIPD(); }
88 #define SETYEAR()  { tw.tw_year = atoi(cp); SKIPD(); }
89 #define SETDAY()  { tw.tw_mday = atoi(cp); tw.tw_flags |= TW_YES; SKIPD(); }
90 #define SETTIME()  { tw.tw_hour = atoi(cp); cp += 2; SKIPTOD(); \
91 	tw.tw_min = atoi(cp); cp += 2; if(*cp == ':') { \
92 	tw.tw_sec = atoi(++cp); SKIPD(); } }
93 #define SETZONE(x)  { tw.tw_zone = ((x)/100)*60+(x)%100; \
94 	tw.tw_flags |= TW_SZEXP; SKIPD(); }
95 #define SETZONEC(h, m)  { tw.tw_zone = (h)*60+(m); \
96 	tw.tw_flags |= TW_SZEXP; SKIPD(); }
97 #define SETDST()  { tw.tw_flags |= TW_DST; }
98 #define SKIPD()  { while ( isdigit(*cp++) ) ; --cp; }
99 #define SKIPTOD()  { while ( !isdigit(*cp++) ) ; --cp; }
100 #define SKIPA()  { while ( isalpha(*cp++) ) ; --cp; }
101 #define SKIPTOA()  { while ( !isalpha(*cp++) ) ; --cp; }
102 #define SKIPSP()  { while ( isspace(*cp++) ) ; --cp; }
103 #define SKIPTOSP()  { while ( !isspace(*cp++) ) ; --cp; }
104 
105 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
106 # ifdef HAVE_SYS_TIME_H
107 #  include <sys/time.h>
108 # endif
109 #include <time.h>
110 
111 static void
zonehack(struct tws * tw)112 zonehack (struct tws *tw)
113 {
114 	struct tm *tm;
115 
116 	if (dmktime (tw) == (time_t) -1)
117 		return;
118 
119 	tm = localtime (&tw->tw_clock);
120 	if (tm->tm_isdst) {
121 		tw->tw_flags |= TW_DST;
122 		tw->tw_zone -= 60;
123 	}
124 }
125 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
126 %}
127 
128 sun	([Ss][Uu][Nn]([Dd][Aa][Yy])?)
129 mon	([Mm][Oo][Nn]([Dd][Aa][Yy])?)
130 tue	([Tt][Uu][Ee]([Ss][Dd][Aa][Yy])?)
131 wed	([Ww][Ee][Dd]([Nn][Ee][Ss][Dd][Aa][Yy])?)
132 thu	([Tt][Hh][Uu]([Rr][Ss][Dd][Aa][Yy])?)
133 fri	([Ff][Rr][Ii]([Dd][Aa][Yy])?)
134 sat	([Ss][Aa][Tt]([Uu][Rr][Dd][Aa][Yy])?)
135 
136 DAY	({sun}|{mon}|{tue}|{wed}|{thu}|{fri}|{sat})
137 
138 jan	([Jj][Aa][Nn]([Uu][Aa][Rr][Yy])?)
139 feb	([Ff][Ee][Bb]([Rr][Uu][Aa][Rr][Yy])?)
140 mar	([Mm][Aa][Rr]([Cc][Hh])?)
141 apr	([Aa][Pp][Rr]([Ii][Ll])?)
142 may	([Mm][Aa][Yy])
143 jun	([Jj][Uu][Nn]([Ee])?)
144 jul	([Jj][Uu][Ll]([Yy])?)
145 aug	([Aa][Uu][Gg]([Uu][Ss][Tt])?)
146 sep	([Ss][Ee][Pp]([Tt][Ee][Mm][Bb][Ee][Rr])?)
147 oct	([Oo][Cc][Tt]([Oo][Bb][Ee][Rr])?)
148 nov	([Nn][Oo][Vv]([Ee][Mm][Bb][Ee][Rr])?)
149 dec	([Dd][Ee][Cc]([Ee][Mm][Bb][Ee][Rr])?)
150 
151 MONTH	({jan}|{feb}|{mar}|{apr}|{may}|{jun}|{jul}|{aug}|{sep}|{oct}|{nov}|{dec})
152 
153 TIME	({D}:{d}{d}(:{d}{d})?)
154 
155 /*
156 ** The year can either be 2 digits, or 4. However, after
157 ** Y2K, we found that some MUA were reporting the year 100, hence
158 ** the middle term here. yyterminate() resolves the actual
159 ** issues with 2-digit years.
160 */
161 
162 YEAR	(({d}{d})|(1{d}{d})|({d}{4}))
163 
164 w	([ \t]*)
165 W	([ \t]+)
166 D	([0-9]?[0-9])
167 d	[0-9]
168 nl	[ \t\n()]
169 
170 %%
171 %{
172 	/*
173 	** This section begins the definition of dparsetime().
174 	** Put here any local variable definitions and initializations
175 	*/
176 	YY_BUFFER_STATE lexhandle;
177 
178 	unsigned char *cp;
179 	static struct tws tw;
180 
181 	memset(&tw,0,sizeof(struct tws));
182 
183 	lexhandle = yy_scan_string(lexstr);
184 %}
185 
186 {DAY}","?{W}{MONTH}{W}{D}{W}{TIME}{W}{YEAR}  {
187 	INIT();
188 	SETWDAY();
189 	SKIPTOA();
190 	SETMON();
191 	SKIPTOD();
192 	SETDAY();
193 	SKIPTOD();
194 	SETTIME();
195 	SKIPTOD();
196 	SETYEAR();
197 }
198 
199 {DAY}","?{W}{D}{W}{MONTH}{W}{YEAR}{W}{TIME}  {
200 	INIT();
201 	SETWDAY();
202 	SKIPTOD();
203 	SETDAY();
204 	SKIPTOA();
205 	SETMON();
206 	SKIPTOD();
207 	SETYEAR();
208 	SKIPTOD();
209 	SETTIME();
210 }
211 {D}{W}{MONTH}{W}{YEAR}{W}{TIME}  {
212 	INIT();
213 	SETDAY();
214 	SKIPTOA();
215 	SETMON();
216 	SKIPTOD();
217 	SETYEAR();
218 	SKIPTOD();
219 	SETTIME();
220 }
221 {DAY}","?{W}{MONTH}{W}{D}","?{W}{YEAR}","?{W}{TIME}  {
222 	INIT();
223 	SETWDAY();
224 	SKIPTOA();
225 	SETMON();
226 	SKIPTOD();
227 	SETDAY();
228 	SKIPTOD();
229 	SETYEAR();
230 	SKIPTOD();
231 	SETTIME();
232 }
233 {DAY}","?{W}{MONTH}{W}{D}","?{W}{YEAR}  {
234 	INIT();
235 	SETWDAY();
236 	SKIPTOA();
237 	SETMON();
238 	SKIPTOD();
239 	SETDAY();
240 	SKIPTOD();
241 	SETYEAR();
242 }
243 {MONTH}{W}{D}","?{W}{YEAR}","?{W}{DAY}  {
244 	INIT();
245 	SETMON();
246 	SKIPTOD();
247 	SETDAY();
248 	SKIPTOD();
249 	SETYEAR();
250 	SKIPTOA();
251 	SETWDAY();
252 }
253 {MONTH}{W}{D}","?{W}{YEAR}  {
254 	INIT();
255 	SETMON();
256 	SKIPTOD();
257 	SETDAY();
258 	SKIPTOD();
259 	SETYEAR();
260 }
261 {d}{4}"-"{d}{2}"-"{d}{2}(" "|"T"){TIME}  {
262 	INIT();
263 	SETYEAR();
264 	SKIPTOD();
265 	SETMON_NUM();
266 	SKIPTOD();
267 	SETDAY();
268 	SKIPTOD();
269 	SETTIME();
270 }
271 {d}{4}"-"{d}{2}"-"{d}{2}  {
272 	INIT();
273 	SETYEAR();
274 	SKIPTOD();
275 	SETMON_NUM();
276 	SKIPTOD();
277 	SETDAY();
278 }
279 {d}{2}"-"{d}{2}"-"{d}{2}  {
280 	fprintf(stderr, "the highly ambiguous date format XX-XX-XX..."
281 			" is no longer supported\n");
282 }
283 {D}"/"{D}"/"{YEAR}{W}{TIME}  {
284 	INIT();
285 	if(europeandate) {
286 		/* DD/MM/YY */
287 		SETDAY();
288 		SKIPTOD();
289 		SETMON_NUM();
290 	} else {
291 		/* MM/DD/YY */
292 		SETMON_NUM();
293 		SKIPTOD();
294 		SETDAY();
295 	}
296 	SKIPTOD();
297 	SETYEAR();
298 	SKIPTOD();
299 	SETTIME();
300 }
301 {D}"/"{D}"/"{YEAR}  {
302 	INIT();
303 	if(europeandate) {
304 		/* DD/MM/YY */
305 		SETDAY();
306 		SKIPTOD();
307 		SETMON_NUM();
308 	} else {
309 		/* MM/DD/YY */
310 		SETMON_NUM();
311 		SKIPTOD();
312 		SETDAY();
313 	}
314 	SKIPTOD();
315 	SETYEAR();
316 }
317 
318 "[Aa][Mm]"
319 "[Pp][Mm]"  tw.tw_hour += 12;
320 
321 "+"{D}{d}{d}  {
322 	INIT();
323 	SKIPTOD();
324 	SETZONE(atoi(cp));
325 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
326 	zonehack (&tw);
327 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
328 	yyterminate();
329 }
330 "-"{D}{d}{d}  {
331 	INIT();
332 	SKIPTOD();
333 	SETZONE(-atoi(cp));
334 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
335 	zonehack (&tw);
336 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
337 	yyterminate();
338 
339 }
340 "+"{d}{d}":"{d}{d}  {
341 	INIT();
342 	SKIPTOD();
343 	SETZONEC(atoi(cp), atoi(cp+3));
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}{d}  {
350 	INIT();
351 	SKIPTOD();
352 	SETZONEC(-atoi(cp), -atoi(cp+3));
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); yyterminate(); }
363 {nl}("cst"|"CST")	INIT(); SETZONE(-600); yyterminate();
364 {nl}("cdt"|"CDT")	{ INIT(); SETDST(); SETZONE(-600); yyterminate(); }
365 {nl}("mst"|"MST")	INIT(); SETZONE(-700); yyterminate();
366 {nl}("mdt"|"MDT")	{ INIT(); SETDST(); SETZONE(-700); yyterminate(); }
367 {nl}("pst"|"PST")	INIT(); SETZONE(-800); yyterminate();
368 {nl}("pdt"|"PDT")	{ INIT(); SETDST(); SETZONE(-800); yyterminate(); }
369 {nl}("nst"|"NST")	INIT(); SETZONE(-330); yyterminate();
370 {nl}("ast"|"AST")	INIT(); SETZONE(-400); yyterminate();
371 {nl}("adt"|"ADT")	{ INIT(); SETDST(); SETZONE(-400); yyterminate(); }
372 {nl}("hst"|"HST")	INIT(); SETZONE(-1000); yyterminate();
373 {nl}("hdt"|"HDT")	{ INIT(); SETDST(); SETZONE(-1000); yyterminate(); }
374 .|\n
375 
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 }
388