xref: /illumos-gate/usr/src/cmd/backup/lib/getdate.y (revision 8b80e8cb6855118d46f605e91b5ed4ce83417395)
1 %{
2 /*
3  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
4  * Use is subject to license terms.
5  */
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 /* $OrigRevision: 2.1 $
9 **
10 **  Originally written by Steven M. Bellovin <smb@research.att.com> while
11 **  at the University of North Carolina at Chapel Hill.  Later tweaked by
12 **  a couple of people on Usenet.  Completely overhauled by Rich $alz
13 **  <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
14 **  send any email to Rich.
15 **
16 **  This grammar has eight shift/reduce conflicts.
17 **
18 **  This code is in the public domain and has no copyright.
19 */
20 /* SUPPRESS 287 on yaccpar_sccsid *//* Unusd static variable */
21 /* SUPPRESS 288 on yyerrlab *//* Label unused */
22 #include <stdio.h>
23 #include <ctype.h>
24 
25 #include <sys/types.h>
26 #define NEED_TZSET
27 struct timeb {
28     time_t		time;		/* Seconds since the epoch	*/
29     unsigned short	millitm;	/* Field not used		*/
30     short		timezone;
31     short		dstflag;	/* Field not used		*/
32 };
33 #include <time.h>
34 
35 #include <locale.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <note.h>
39 #include <libintl.h>
40 
41 #if	!defined(lint) && !defined(SABER)
42 static char RCS[] =
43 	"$Header: /home/laramie/berliner/ws/backup/usr/src/cmd/backup/lib/getdate.y,v 1.5 1992/06/09 21:46:21 sam Exp $";
44 #endif	/* !defined(lint) && !defined(SABER) */
45 
46 
47 #define EPOCH		1970
48 #define HOURN(x)	(x * 60)
49 #define SECSPERDAY	(24L * 60L * 60L)
50 
51 #define CHECK_TM(y) (((y) % 100) < 70 ? (y) + 2000 : (y) + 1900)
52 
53 /*
54 **  An entry in the lexical lookup table.
55 */
56 typedef struct _TABLE {
57     char	*name;
58     int		type;
59     time_t	value;
60 } TABLE;
61 
62 
63 /*
64 **  Daylight-savings mode:  on, off, or not yet known.
65 */
66 typedef enum _DSTMODE {
67     DSTon, DSToff, DSTmaybe
68 } DSTMODE;
69 
70 /*
71 **  Meridian:  am, pm, or 24-hour style.
72 */
73 typedef enum _MERIDIAN {
74     MERam, MERpm, MER24
75 } MERIDIAN;
76 
77 
78 /*
79 **  Global variables.  We could get rid of most of these by using a good
80 **  union as the yacc stack.  (This routine was originally written before
81 **  yacc had the %union construct.)  Maybe someday; right now we only use
82 **  the %union very rarely.
83 */
84 static char	*yyInput;
85 static DSTMODE	yyDSTmode;
86 static time_t	yyDayOrdinal;
87 static time_t	yyDayNumber;
88 static int	yyHaveDate;
89 static int	yyHaveDay;
90 static int	yyHaveRel;
91 static int	yyHaveTime;
92 static int	yyHaveZone;
93 static time_t	yyTimezone;
94 static time_t	yyDay;
95 static time_t	yyHour;
96 static time_t	yyMinutes;
97 static time_t	yyMonth;
98 static time_t	yySeconds;
99 static time_t	yyYear;
100 static MERIDIAN	yyMeridian;
101 static time_t	yyRelMonth;
102 static time_t	yyRelSeconds;
103 
104 static char	*domainname = "hsm_libdump";	/* for dgettext() */
105 
106 #define yylex 1					/* suppress yacc's definition */
107 %}
108 
109 %union {
110     time_t		Number;
111     enum _MERIDIAN	Meridian;
112 }
113 
114 %token	tAGO tDAY tDAYZONE tID tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
115 %token	tSEC_UNIT tSNUMBER tUNUMBER tZONE
116 
117 %type	<Number>	tDAY tDAYZONE tMINUTE_UNIT tMONTH tMONTH_UNIT
118 %type	<Number>	tSEC_UNIT tSNUMBER tUNUMBER tZONE
119 %type	<Meridian>	tMERIDIAN o_merid
120 
121 %%
122 
123 spec	: /* NULL */
124 	| spec item
125 	;
126 
127 item	: time {
128 	    yyHaveTime++;
129 	}
130 	| zone {
131 	    yyHaveZone++;
132 	}
133 	| date {
134 	    yyHaveDate++;
135 	}
136 	| day {
137 	    yyHaveDay++;
138 	}
139 	| rel {
140 	    yyHaveRel++;
141 	}
142 	| number
143 	;
144 
145 time	: tUNUMBER tMERIDIAN {
146 	    yyHour = $1;
147 	    yyMinutes = 0;
148 	    yySeconds = 0;
149 	    yyMeridian = $2;
150 	}
151 	| tUNUMBER ':' tUNUMBER o_merid {
152 	    yyHour = $1;
153 	    yyMinutes = $3;
154 	    yySeconds = 0;
155 	    yyMeridian = $4;
156 	}
157 	| tUNUMBER ':' tUNUMBER tSNUMBER {
158 	    yyHour = $1;
159 	    yyMinutes = $3;
160 	    yyMeridian = MER24;
161 	    yyDSTmode = DSToff;
162 	    yyTimezone = - ($4 % 100 + ($4 / 100) * 60);
163 	}
164 	| tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
165 	    yyHour = $1;
166 	    yyMinutes = $3;
167 	    yySeconds = $5;
168 	    yyMeridian = $6;
169 	}
170 	| tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
171 	    yyHour = $1;
172 	    yyMinutes = $3;
173 	    yySeconds = $5;
174 	    yyMeridian = MER24;
175 	    yyDSTmode = DSToff;
176 	    yyTimezone = - ($6 % 100 + ($6 / 100) * 60);
177 	}
178 	;
179 
180 zone	: tZONE {
181 	    yyTimezone = $1;
182 	    yyDSTmode = DSToff;
183 	}
184 	| tDAYZONE {
185 	    yyTimezone = $1;
186 	    yyDSTmode = DSTon;
187 	}
188 	;
189 
190 day	: tDAY {
191 	    yyDayOrdinal = 1;
192 	    yyDayNumber = $1;
193 	}
194 	| tDAY ',' {
195 	    yyDayOrdinal = 1;
196 	    yyDayNumber = $1;
197 	}
198 	| tUNUMBER tDAY {
199 	    yyDayOrdinal = $1;
200 	    yyDayNumber = $2;
201 	}
202 	;
203 
204 date	: tUNUMBER '/' tUNUMBER {
205 	    yyMonth = $1;
206 	    yyDay = $3;
207 	}
208 	| tUNUMBER '/' tUNUMBER '/' tUNUMBER {
209 	    yyMonth = $1;
210 	    yyDay = $3;
211 	    yyYear = $5;
212 	}
213 	| tMONTH tUNUMBER {
214 	    yyMonth = $1;
215 	    yyDay = $2;
216 	}
217 	| tMONTH tUNUMBER ',' tUNUMBER {
218 	    yyMonth = $1;
219 	    yyDay = $2;
220 	    yyYear = $4;
221 	}
222 	| tUNUMBER tMONTH {
223 	    yyMonth = $2;
224 	    yyDay = $1;
225 	}
226 	| tUNUMBER tMONTH tUNUMBER {
227 	    yyMonth = $2;
228 	    yyDay = $1;
229 	    yyYear = $3;
230 	}
231 	;
232 
233 rel	: relunit tAGO {
234 	    yyRelSeconds = -yyRelSeconds;
235 	    yyRelMonth = -yyRelMonth;
236 	}
237 	| relunit
238 	;
239 
240 relunit	: tUNUMBER tMINUTE_UNIT {
241 	    yyRelSeconds += $1 * $2 * 60L;
242 	}
243 	| tSNUMBER tMINUTE_UNIT {
244 	    yyRelSeconds += $1 * $2 * 60L;
245 	}
246 	| tMINUTE_UNIT {
247 	    yyRelSeconds += $1 * 60L;
248 	}
249 	| tSNUMBER tSEC_UNIT {
250 	    yyRelSeconds += $1;
251 	}
252 	| tUNUMBER tSEC_UNIT {
253 	    yyRelSeconds += $1;
254 	}
255 	| tSEC_UNIT {
256 	    yyRelSeconds++;
257 	}
258 	| tSNUMBER tMONTH_UNIT {
259 	    yyRelMonth += $1 * $2;
260 	}
261 	| tUNUMBER tMONTH_UNIT {
262 	    yyRelMonth += $1 * $2;
263 	}
264 	| tMONTH_UNIT {
265 	    yyRelMonth += $1;
266 	}
267 	;
268 
269 number	: tUNUMBER {
270 	    if (yyHaveTime && yyHaveDate && !yyHaveRel)
271 		yyYear = $1;
272 	    else {
273 		yyHaveTime++;
274 		if ($1 < 100) {
275 		    yyHour = $1;
276 		    yyMinutes = 0;
277 		}
278 		else {
279 		    yyHour = $1 / 100;
280 		    yyMinutes = $1 % 100;
281 		}
282 		yySeconds = 0;
283 		yyMeridian = MER24;
284 	    }
285 	}
286 	;
287 
288 o_merid	: /* NULL */ {
289 	    $$ = MER24;
290 	}
291 	| tMERIDIAN {
292 	    $$ = $1;
293 	}
294 	;
295 
296 %%
297 
298 /* Month and day table. */
299 static TABLE	MonthDayTable[] = {
300     { "january",	tMONTH,  1 },
301     { "february",	tMONTH,  2 },
302     { "march",		tMONTH,  3 },
303     { "april",		tMONTH,  4 },
304     { "may",		tMONTH,  5 },
305     { "june",		tMONTH,  6 },
306     { "july",		tMONTH,  7 },
307     { "august",		tMONTH,  8 },
308     { "september",	tMONTH,  9 },
309     { "sept",		tMONTH,  9 },
310     { "october",	tMONTH, 10 },
311     { "november",	tMONTH, 11 },
312     { "december",	tMONTH, 12 },
313     { "sunday",		tDAY, 0 },
314     { "monday",		tDAY, 1 },
315     { "tuesday",	tDAY, 2 },
316     { "tues",		tDAY, 2 },
317     { "wednesday",	tDAY, 3 },
318     { "wednes",		tDAY, 3 },
319     { "thursday",	tDAY, 4 },
320     { "thur",		tDAY, 4 },
321     { "thurs",		tDAY, 4 },
322     { "friday",		tDAY, 5 },
323     { "saturday",	tDAY, 6 },
324     { NULL }
325 };
326 
327 /* Time units table. */
328 static TABLE	UnitsTable[] = {
329     { "year",		tMONTH_UNIT,	12 },
330     { "month",		tMONTH_UNIT,	1 },
331     { "fortnight",	tMINUTE_UNIT,	14 * 24 * 60 },
332     { "week",		tMINUTE_UNIT,	7 * 24 * 60 },
333     { "day",		tMINUTE_UNIT,	1 * 24 * 60 },
334     { "hour",		tMINUTE_UNIT,	60 },
335     { "minute",		tMINUTE_UNIT,	1 },
336     { "min",		tMINUTE_UNIT,	1 },
337     { "second",		tSEC_UNIT,	1 },
338     { "sec",		tSEC_UNIT,	1 },
339     { NULL }
340 };
341 
342 /* Assorted relative-time words. */
343 static TABLE	OtherTable[] = {
344     { "tomorrow",	tMINUTE_UNIT,	1 * 24 * 60 },
345     { "yesterday",	tMINUTE_UNIT,	-1 * 24 * 60 },
346     { "today",		tMINUTE_UNIT,	0 },
347     { "now",		tMINUTE_UNIT,	0 },
348     { "last",		tUNUMBER,	-1 },
349     { "this",		tMINUTE_UNIT,	0 },
350     { "next",		tUNUMBER,	2 },
351     { "first",		tUNUMBER,	1 },
352 /*  { "second",		tUNUMBER,	2 }, */
353     { "third",		tUNUMBER,	3 },
354     { "fourth",		tUNUMBER,	4 },
355     { "fifth",		tUNUMBER,	5 },
356     { "sixth",		tUNUMBER,	6 },
357     { "seventh",	tUNUMBER,	7 },
358     { "eighth",		tUNUMBER,	8 },
359     { "ninth",		tUNUMBER,	9 },
360     { "tenth",		tUNUMBER,	10 },
361     { "eleventh",	tUNUMBER,	11 },
362     { "twelfth",	tUNUMBER,	12 },
363     { "ago",		tAGO,	1 },
364     { NULL }
365 };
366 
367 /* The timezone table. */
368 static TABLE	TimezoneTable[] = {
369     { "gmt",	tZONE,     HOURN( 0) },	/* Greenwich Mean */
370     { "ut",	tZONE,     HOURN( 0) },	/* Universal (Coordinated) */
371     { "utc",	tZONE,     HOURN( 0) },
372     { "wet",	tZONE,     HOURN( 0) },	/* Western European */
373     { "bst",	tDAYZONE,  HOURN( 0) },	/* British Summer */
374     { "wat",	tZONE,     HOURN( 1) },	/* West Africa */
375     { "at",	tZONE,     HOURN( 2) },	/* Azores */
376 #if	0
377     /* For completeness.  BST is also British Summer, and GST is
378      * also Guam Standard. */
379     { "bst",	tZONE,     HOURN( 3) },	/* Brazil Standard */
380     { "gst",	tZONE,     HOURN( 3) },	/* Greenland Standard */
381 #endif
382     { "nft",	tZONE,     HOURN(3.5) },	/* Newfoundland */
383     { "nst",	tZONE,     HOURN(3.5) },	/* Newfoundland Standard */
384     { "ndt",	tDAYZONE,  HOURN(3.5) },	/* Newfoundland Daylight */
385     { "ast",	tZONE,     HOURN( 4) },	/* Atlantic Standard */
386     { "adt",	tDAYZONE,  HOURN( 4) },	/* Atlantic Daylight */
387     { "est",	tZONE,     HOURN( 5) },	/* Eastern Standard */
388     { "edt",	tDAYZONE,  HOURN( 5) },	/* Eastern Daylight */
389     { "cst",	tZONE,     HOURN( 6) },	/* Central Standard */
390     { "cdt",	tDAYZONE,  HOURN( 6) },	/* Central Daylight */
391     { "mst",	tZONE,     HOURN( 7) },	/* Mountain Standard */
392     { "mdt",	tDAYZONE,  HOURN( 7) },	/* Mountain Daylight */
393     { "pst",	tZONE,     HOURN( 8) },	/* Pacific Standard */
394     { "pdt",	tDAYZONE,  HOURN( 8) },	/* Pacific Daylight */
395     { "yst",	tZONE,     HOURN( 9) },	/* Yukon Standard */
396     { "ydt",	tDAYZONE,  HOURN( 9) },	/* Yukon Daylight */
397     { "hst",	tZONE,     HOURN(10) },	/* Hawaii Standard */
398     { "hdt",	tDAYZONE,  HOURN(10) },	/* Hawaii Daylight */
399     { "cat",	tZONE,     HOURN(10) },	/* Central Alaska */
400     { "ahst",	tZONE,     HOURN(10) },	/* Alaska-Hawaii Standard */
401     { "nt",	tZONE,     HOURN(11) },	/* Nome */
402     { "idlw",	tZONE,     HOURN(12) },	/* International Date Line West */
403     { "cet",	tZONE,     -HOURN(1) },	/* Central European */
404     { "met",	tZONE,     -HOURN(1) },	/* Middle European */
405     { "mewt",	tZONE,     -HOURN(1) },	/* Middle European Winter */
406     { "mest",	tDAYZONE,  -HOURN(1) },	/* Middle European Summer */
407     { "swt",	tZONE,     -HOURN(1) },	/* Swedish Winter */
408     { "sst",	tDAYZONE,  -HOURN(1) },	/* Swedish Summer */
409     { "fwt",	tZONE,     -HOURN(1) },	/* French Winter */
410     { "fst",	tDAYZONE,  -HOURN(1) },	/* French Summer */
411     { "eet",	tZONE,     -HOURN(2) },	/* Eastern Europe, USSR Zone 1 */
412     { "bt",	tZONE,     -HOURN(3) },	/* Baghdad, USSR Zone 2 */
413     { "it",	tZONE,     -HOURN(3.5) },/* Iran */
414     { "zp4",	tZONE,     -HOURN(4) },	/* USSR Zone 3 */
415     { "zp5",	tZONE,     -HOURN(5) },	/* USSR Zone 4 */
416     { "ist",	tZONE,     -HOURN(5.5) },/* Indian Standard */
417     { "zp6",	tZONE,     -HOURN(6) },	/* USSR Zone 5 */
418 #if	0
419     /* For completeness.  NST is also Newfoundland Stanard, nad SST is
420      * also Swedish Summer. */
421     { "nst",	tZONE,     -HOURN(6.5) },/* North Sumatra */
422     { "sst",	tZONE,     -HOURN(7) },	/* South Sumatra, USSR Zone 6 */
423 #endif	/* 0 */
424     { "wast",	tZONE,     -HOURN(7) },	/* West Australian Standard */
425     { "wadt",	tDAYZONE,  -HOURN(7) },	/* West Australian Daylight */
426     { "jt",	tZONE,     -HOURN(7.5) },/* Java (3pm in Cronusland!) */
427     { "cct",	tZONE,     -HOURN(8) },	/* China Coast, USSR Zone 7 */
428     { "jst",	tZONE,     -HOURN(9) },	/* Japan Standard, USSR Zone 8 */
429     { "cast",	tZONE,     -HOURN(9.5) },/* Central Australian Standard */
430     { "cadt",	tDAYZONE,  -HOURN(9.5) },/* Central Australian Daylight */
431     { "east",	tZONE,     -HOURN(10) },	/* Eastern Australian Standard */
432     { "eadt",	tDAYZONE,  -HOURN(10) },	/* Eastern Australian Daylight */
433     { "gst",	tZONE,     -HOURN(10) },	/* Guam Standard, USSR Zone 9 */
434     { "nzt",	tZONE,     -HOURN(12) },	/* New Zealand */
435     { "nzst",	tZONE,     -HOURN(12) },	/* New Zealand Standard */
436     { "nzdt",	tDAYZONE,  -HOURN(12) },	/* New Zealand Daylight */
437     { "idle",	tZONE,     -HOURN(12) },	/* International Date Line East */
438     {  NULL  }
439 };
440 
441 /* Military timezone table. */
442 static TABLE	MilitaryTable[] = {
443     { "a",	tZONE,	HOURN(  1) },
444     { "b",	tZONE,	HOURN(  2) },
445     { "c",	tZONE,	HOURN(  3) },
446     { "d",	tZONE,	HOURN(  4) },
447     { "e",	tZONE,	HOURN(  5) },
448     { "f",	tZONE,	HOURN(  6) },
449     { "g",	tZONE,	HOURN(  7) },
450     { "h",	tZONE,	HOURN(  8) },
451     { "i",	tZONE,	HOURN(  9) },
452     { "k",	tZONE,	HOURN( 10) },
453     { "l",	tZONE,	HOURN( 11) },
454     { "m",	tZONE,	HOURN( 12) },
455     { "n",	tZONE,	HOURN(- 1) },
456     { "o",	tZONE,	HOURN(- 2) },
457     { "p",	tZONE,	HOURN(- 3) },
458     { "q",	tZONE,	HOURN(- 4) },
459     { "r",	tZONE,	HOURN(- 5) },
460     { "s",	tZONE,	HOURN(- 6) },
461     { "t",	tZONE,	HOURN(- 7) },
462     { "u",	tZONE,	HOURN(- 8) },
463     { "v",	tZONE,	HOURN(- 9) },
464     { "w",	tZONE,	HOURN(-10) },
465     { "x",	tZONE,	HOURN(-11) },
466     { "y",	tZONE,	HOURN(-12) },
467     { "z",	tZONE,	HOURN(  0) },
468     { NULL }
469 };
470 
471 
472 
473 
474 /* ARGSUSED */
475 static void
476 yyerror(s)
477     char	*s;
478 {
479 }
480 
481 
482 static time_t
483 ToSeconds(Hours, Minutes, Seconds, Meridian)
484     time_t	Hours;
485     time_t	Minutes;
486     time_t	Seconds;
487     MERIDIAN	Meridian;
488 {
489     if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59)
490 	return -1;
491     switch (Meridian) {
492     case MER24:
493 	if (Hours < 0 || Hours > 23)
494 	    return -1;
495 	return (Hours * 60L + Minutes) * 60L + Seconds;
496     case MERam:
497 	if (Hours < 1 || Hours > 12)
498 	    return -1;
499 	if (Hours != 12)
500 	    return (Hours * 60L + Minutes) * 60L + Seconds;
501 	else
502 	    return Minutes * 60L + Seconds;
503     case MERpm:
504 	if (Hours < 1 || Hours > 12)
505 	    return -1;
506 	if (Hours != 12)
507 	    return ((Hours + 12) * 60L + Minutes) * 60L + Seconds;
508 	else
509 	    return (720L + Minutes) * 60L + Seconds;
510     }
511     /* NOTREACHED */
512     return (-1);
513 }
514 
515 
516 static time_t
517 Convert(Month, Day, Year, Hours, Minutes, Seconds, Meridian, DSTmode)
518     time_t	Month;
519     time_t	Day;
520     time_t	Year;
521     time_t	Hours;
522     time_t	Minutes;
523     time_t	Seconds;
524     MERIDIAN	Meridian;
525     DSTMODE	DSTmode;
526 {
527     static int	DaysInMonth[12] = {
528 	31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
529     };
530     time_t	tod;
531     time_t	Julian;
532     time_t	i;
533 
534     if (Year < 0)
535 	Year = -Year;
536     if (Year < 138)
537 	Year += 1900;
538     DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
539 		    ? 29 : 28;
540     if (Year < EPOCH || Year > 2037
541      || Month < 1 || Month > 12
542      /* LINTED Month is a time_t so intermediate results aren't truncated */
543      || Day < 1 || Day > DaysInMonth[(int)--Month])
544 	return -1;
545 
546     for (Julian = Day - 1, i = 0; i < Month; i++)
547 	Julian += DaysInMonth[i];
548     for (i = EPOCH; i < Year; i++)
549 	Julian += 365 + (i % 4 == 0);
550     Julian *= SECSPERDAY;
551     Julian += yyTimezone * 60L;
552     if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0)
553 	return -1;
554     Julian += tod;
555     if (DSTmode == DSTon
556      || (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
557 	Julian -= 60 * 60;
558     return Julian;
559 }
560 
561 
562 static time_t
563 DSTcorrect(Start, Future)
564     time_t	Start;
565     time_t	Future;
566 {
567     time_t	StartDay;
568     time_t	FutureDay;
569 
570     StartDay = (localtime(&Start)->tm_hour + 1) % 24;
571     FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
572     return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
573 }
574 
575 
576 static time_t
577 RelativeDate(Start, DayOrdinal, DayNumber)
578     time_t	Start;
579     time_t	DayOrdinal;
580     time_t	DayNumber;
581 {
582     struct tm	*tm;
583     time_t	now;
584 
585     now = Start;
586     tm = localtime(&now);
587     now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
588     now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
589     return DSTcorrect(Start, now);
590 }
591 
592 
593 static time_t
594 RelativeMonth(Start, RelMonth)
595     time_t	Start;
596     time_t	RelMonth;
597 {
598     struct tm	*tm;
599     time_t	Month;
600     time_t	Year;
601 
602     if (RelMonth == 0)
603 	return 0;
604     tm = localtime(&Start);
605     Month = 12 * tm->tm_year + tm->tm_mon + RelMonth;
606     Year = Month / 12;
607     Month = Month % 12 + 1;
608     return DSTcorrect(Start,
609 	    Convert(Month, (time_t)tm->tm_mday, Year,
610 		(time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
611 		MER24, DSTmaybe));
612 }
613 
614 
615 static int
616 LookupWord(buff)
617     char		*buff;
618 {
619     char	*p;
620     char	*q;
621     TABLE	*tp;
622     uint_t	i;
623     int		abbrev;
624 
625     /* Make it lowercase. */
626     for (p = buff; *p; p++)
627 	if (isupper((u_char)*p))
628 	    *p = tolower(*p);
629 
630     if (strcmp(buff, "am") == 0 || strcmp(buff, "a.m.") == 0) {
631 	yylval.Meridian = MERam;
632 	return tMERIDIAN;
633     }
634     if (strcmp(buff, "pm") == 0 || strcmp(buff, "p.m.") == 0) {
635 	yylval.Meridian = MERpm;
636 	return tMERIDIAN;
637     }
638 
639     /* See if we have an abbreviation for a month. */
640     if (strlen(buff) == 3)
641 	abbrev = 1;
642     else if (strlen(buff) == 4 && buff[3] == '.') {
643 	abbrev = 1;
644 	buff[3] = '\0';
645     }
646     else
647 	abbrev = 0;
648 
649     for (tp = MonthDayTable; tp->name; tp++) {
650 	if (abbrev) {
651 	    if (strncmp(buff, tp->name, 3) == 0) {
652 		yylval.Number = tp->value;
653 		return tp->type;
654 	    }
655 	}
656 	else if (strcmp(buff, tp->name) == 0) {
657 	    yylval.Number = tp->value;
658 	    return tp->type;
659 	}
660     }
661 
662     for (tp = TimezoneTable; tp->name; tp++)
663 	if (strcmp(buff, tp->name) == 0) {
664 	    yylval.Number = tp->value;
665 	    return tp->type;
666 	}
667 
668     for (tp = UnitsTable; tp->name; tp++)
669 	if (strcmp(buff, tp->name) == 0) {
670 	    yylval.Number = tp->value;
671 	    return tp->type;
672 	}
673 
674     /* Strip off any plural and try the units table again. */
675     i = strlen(buff) - 1;
676     if (buff[i] == 's') {
677 	buff[i] = '\0';
678 	for (tp = UnitsTable; tp->name; tp++)
679 	    if (strcmp(buff, tp->name) == 0) {
680 		yylval.Number = tp->value;
681 		return tp->type;
682 	    }
683     }
684 
685     for (tp = OtherTable; tp->name; tp++)
686 	if (strcmp(buff, tp->name) == 0) {
687 	    yylval.Number = tp->value;
688 	    return tp->type;
689 	}
690 
691     /* Military timezones. */
692     if (buff[1] == '\0' && isalpha((u_char)*buff)) {
693 	for (tp = MilitaryTable; tp->name; tp++)
694 	    if (strcmp(buff, tp->name) == 0) {
695 		yylval.Number = tp->value;
696 		return tp->type;
697 	    }
698     }
699 
700     /* Drop out any periods and try the timezone table again. */
701     for (i = 0, p = q = buff; *q; q++)
702 	if (*q != '.')
703 	    *p++ = *q;
704 	else
705 	    i++;
706     *p = '\0';
707     if (i)
708 	for (tp = TimezoneTable; tp->name; tp++)
709 	    if (strcmp(buff, tp->name) == 0) {
710 		yylval.Number = tp->value;
711 		return tp->type;
712 	    }
713 
714     return tID;
715 }
716 
717 void
718 pdateerr(p)
719     char	*p;
720 {
721     char	*name = "DATEMSK";	/* env variable for date format */
722     char	*value;
723     char	fmt[256], line[256];
724     FILE	*fp;
725     time_t	now;
726     struct tm	*tm;
727 
728     value = getenv(name);
729     if (value == (char *)0) {
730 	fprintf(stderr,
731 	    dgettext(domainname, "%s: Environment variable %s not set\n"),
732 		p, name);
733 	return;
734     }
735     switch (getdate_err) {
736 	case 0:
737 	default:
738 	    fprintf(stderr,
739 		dgettext(domainname, "%s: Unkown getdate() error\n"), p);
740 	    break;
741 	case 1:
742 	    fprintf(stderr,
743 		dgettext(domainname, "%s: %s null or undefined\n"), p, name);
744 	    break;
745 	case 2:
746 	    fprintf(stderr, dgettext(domainname,
747 		"%s: Cannot read template file %s\n"), p, value);
748 	    break;
749 	case 3:
750 	    fprintf(stderr, dgettext(domainname,
751 		"%s: Failed to get file status information\n"), p);
752 	    break;
753 	case 4:
754 	    fprintf(stderr, dgettext(domainname,
755 		"%s: Template file %s not a regular file\n"), p, value);
756 	    break;
757 	case 5:
758 	    fprintf(stderr, dgettext(domainname,
759 		"%s: Error reading template file %s\n"), p, value);
760 	    break;
761 	case 6:
762 	    fprintf(stderr, dgettext(domainname,
763 		"%s: %s failed\n"), p, "malloc()");
764 	    break;
765 	case 7:
766 	    fprintf(stderr, dgettext(domainname,
767 		"%s: Bad date/time format\n"), p);
768 	    fp = fopen(value, "r");
769 	    if (fp == (FILE *)0)
770 		break;
771 	    now = time((time_t *)0);
772 	    tm = localtime(&now);
773 	    fprintf(stderr, dgettext(domainname,
774 		"The following are examples of valid formats:\n"));
775 	    while (fgets(fmt, sizeof (fmt), fp)) {
776 		if (strchr(fmt, '%') == (char *)0)
777 		    continue;
778 		fprintf(stderr, "    ");
779 	        (void) strftime(line, sizeof (line), fmt, tm);
780 		fprintf(stderr, "%s", line);
781 	    }
782 	    (void) fclose(fp);
783 	    break;
784 	case 8:
785 	    (void) fprintf(stderr, dgettext(domainname,
786 		"%s: Invalid date specification\n"), p);
787 	    break;
788     }
789 }
790 
791 #undef yylex
792 static int
793 yylex()
794 {
795     char	c;
796     char	*p;
797     char	buff[20];
798     int		Count;
799     int		sign;
800 
801     for ( ; ; ) {
802 	while (isspace((u_char)*yyInput))
803 	    yyInput++;
804 
805 	if (isdigit((u_char)(c = *yyInput)) || c == '-' || c == '+') {
806 	    if (c == '-' || c == '+') {
807 		sign = c == '-' ? -1 : 1;
808 		if (!isdigit((u_char)*++yyInput))
809 		    /* skip the '-' sign */
810 		    continue;
811 	    }
812 	    else
813 		sign = 0;
814 	    yylval.Number = 0;
815 	    while (isdigit((u_char)(c = *yyInput++))) {
816 		int n;
817 		char digit = c;
818 		(void) sscanf(&digit, "%1d", &n);
819 		yylval.Number = 10 * yylval.Number + n;
820 	    }
821 	    yyInput--;
822 	    if (sign < 0)
823 		yylval.Number = -yylval.Number;
824 	    return sign ? tSNUMBER : tUNUMBER;
825 	}
826 	if (isalpha((u_char)c)) {
827 	    for (p = buff; isalpha((u_char)(c = *yyInput++)) || c == '.'; )
828 		if (p < &buff[sizeof (buff) - 1])
829 		    *p++ = c;
830 	    *p = '\0';
831 	    yyInput--;
832 	    return LookupWord(buff);
833 	}
834 	if (c != '(')
835 	    return *yyInput++;
836 	Count = 0;
837 	do {
838 	    c = *yyInput++;
839 	    if (c == '\0')
840 		return c;
841 	    if (c == '(')
842 		Count++;
843 	    else if (c == ')')
844 		Count--;
845 	} while (Count > 0);
846     }
847 }
848 
849 
850 time_t
851 getreldate(p, now)
852     char		*p;
853     struct timeb	*now;
854 {
855     struct tm		*tm;
856     struct timeb	ftz;
857     time_t		Start;
858     time_t		tod;
859 
860     if (strcmp(setlocale(LC_TIME, NULL), "C")) {
861 	static char localedate[24];
862 	struct tm ltm;
863 
864 	tm = getdate(p);
865 	if (getdate_err == 1 /* NODATEMASK */) {
866 	    char buffy[BUFSIZ];
867 	    time_t current;
868 
869 	    printf(gettext("environment variable %s not set\n"), "DATEMSK");
870 	    do {
871 		time(&current);
872 		tm = localtime(&current);
873 		memcpy(&ltm, tm, sizeof(ltm));
874 		tm = &ltm;
875 
876 		(void) fputs(gettext("Enter date as mmddhhmm[yy]: "), stdout);
877 		(void) fflush(stdout);
878 		if (fgets(buffy, sizeof (buffy), stdin) == NULL) {
879 			(void) printf(gettext("Encountered EOF on stdin\n"));
880 			return(-1);
881 		}
882 	    } while (sscanf(buffy, "%2d%2d%2d%2d%2d",
883 		&(tm->tm_mon), &(tm->tm_mday), &(tm->tm_hour),
884 		&(tm->tm_min), &(tm->tm_year)) < 4);
885 
886 	    (tm->tm_mon)--;
887 	} else if (tm == NULL)
888 	    return(-1);
889 
890 	(void)sprintf(localedate, "%d:%2.2d %d/%d %d",
891 	    tm->tm_hour, tm->tm_min, tm->tm_mon + 1,
892 	    tm->tm_mday, CHECK_TM(tm->tm_year));
893 	p = localedate;
894     }
895 
896     yyInput = p;
897     if (now == NULL) {
898 	now = &ftz;
899 	(void) time(&ftz.time);
900 	/* Set the timezone global. */
901 	tzset();
902 	/* LINTED timezone is time_t so intermediate results aren't truncated */
903 	ftz.timezone = (int) timezone / 60;
904     }
905 
906     tm = localtime(&now->time);
907     yyYear = tm->tm_year;
908     yyMonth = tm->tm_mon + 1;
909     yyDay = tm->tm_mday;
910     yyTimezone = now->timezone;
911     yyDSTmode = DSTmaybe;
912     yyHour = tm->tm_hour;
913     yyMinutes = tm->tm_min;
914     yySeconds = tm->tm_sec;
915     yyMeridian = MER24;
916     yyRelSeconds = 0;
917     yyRelMonth = 0;
918     yyHaveDate = 0;
919     yyHaveDay = 0;
920     yyHaveRel = 0;
921     yyHaveTime = 0;
922     yyHaveZone = 0;
923 
924     if (yyparse()
925      || yyHaveTime > 1 || yyHaveZone > 1 || yyHaveDate > 1 || yyHaveDay > 1)
926 	return -1;
927 
928     if (yyHaveDate || yyHaveTime || yyHaveDay) {
929 	Start = Convert(yyMonth, yyDay, yyYear, yyHour, yyMinutes, yySeconds,
930 		    yyMeridian, yyDSTmode);
931 	if (Start < 0)
932 	    return -1;
933     }
934     else {
935 	Start = now->time;
936 	if (!yyHaveRel)
937 	    Start -= ((tm->tm_hour * 60L) + tm->tm_min * 60L) + tm->tm_sec;
938     }
939 
940     Start += yyRelSeconds;
941     Start += RelativeMonth(Start, yyRelMonth);
942 
943     if (yyHaveDay && !yyHaveDate) {
944 	tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber);
945 	Start += tod;
946     }
947 
948     /* Have to do *something* with a legitimate -1 so it's distinguishable
949      * from the error return value.  (Alternately could set errno on error.) */
950     return Start == -1 ? 0 : Start;
951 }
952 
953 #if	defined(TEST)
954 
955 /* ARGSUSED */
956 main(ac, av)
957     int		ac;
958     char	*av[];
959 {
960     char	buff[128];
961     time_t	d;
962 
963     (void) setlocale(LC_ALL, "");
964 #if !defined(TEXT_DOMAIN)
965 #define	TEXT_DOMAIN "SYS_TEST"
966 #endif
967     (void) textdomain(TEXT_DOMAIN);
968 
969     (void) printf(gettext("Enter date, or blank line to exit.\n\t> "));
970     (void) fflush(stdout);
971     while (gets(buff) && buff[0]) {
972 	d = getreldate(buff, (struct timeb *)NULL);
973 	if (d == -1)
974 	    (void) printf(gettext("Bad format - couldn't convert.\n"));
975 	else {
976 	    (void) cftime(buff, "%c\n", &d);
977 	    (void) printf("%s", buff);
978 	}
979 	(void) printf("\t> ");
980 	(void) fflush(stdout);
981     }
982     exit(0);
983     /* NOTREACHED */
984 }
985 #endif	/* defined(TEST) */
986