xref: /titanic_41/usr/src/cmd/backup/lib/getdate.y (revision 6dfee4834394825da35b977ca71cdc965bc7b6a4)
1 %{
2 /*
3  * Copyright (c) 1998 by Sun Microsystems, Inc.
4  * All rights reserved.
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 }
513 
514 
515 static time_t
516 Convert(Month, Day, Year, Hours, Minutes, Seconds, Meridian, DSTmode)
517     time_t	Month;
518     time_t	Day;
519     time_t	Year;
520     time_t	Hours;
521     time_t	Minutes;
522     time_t	Seconds;
523     MERIDIAN	Meridian;
524     DSTMODE	DSTmode;
525 {
526     static int	DaysInMonth[12] = {
527 	31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
528     };
529     time_t	tod;
530     time_t	Julian;
531     time_t	i;
532 
533     if (Year < 0)
534 	Year = -Year;
535     if (Year < 138)
536 	Year += 1900;
537     DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
538 		    ? 29 : 28;
539     if (Year < EPOCH || Year > 2037
540      || Month < 1 || Month > 12
541      /* LINTED Month is a time_t so intermediate results aren't truncated */
542      || Day < 1 || Day > DaysInMonth[(int)--Month])
543 	return -1;
544 
545     for (Julian = Day - 1, i = 0; i < Month; i++)
546 	Julian += DaysInMonth[i];
547     for (i = EPOCH; i < Year; i++)
548 	Julian += 365 + (i % 4 == 0);
549     Julian *= SECSPERDAY;
550     Julian += yyTimezone * 60L;
551     if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0)
552 	return -1;
553     Julian += tod;
554     if (DSTmode == DSTon
555      || (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
556 	Julian -= 60 * 60;
557     return Julian;
558 }
559 
560 
561 static time_t
562 DSTcorrect(Start, Future)
563     time_t	Start;
564     time_t	Future;
565 {
566     time_t	StartDay;
567     time_t	FutureDay;
568 
569     StartDay = (localtime(&Start)->tm_hour + 1) % 24;
570     FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
571     return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
572 }
573 
574 
575 static time_t
576 RelativeDate(Start, DayOrdinal, DayNumber)
577     time_t	Start;
578     time_t	DayOrdinal;
579     time_t	DayNumber;
580 {
581     struct tm	*tm;
582     time_t	now;
583 
584     now = Start;
585     tm = localtime(&now);
586     now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
587     now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
588     return DSTcorrect(Start, now);
589 }
590 
591 
592 static time_t
593 RelativeMonth(Start, RelMonth)
594     time_t	Start;
595     time_t	RelMonth;
596 {
597     struct tm	*tm;
598     time_t	Month;
599     time_t	Year;
600 
601     if (RelMonth == 0)
602 	return 0;
603     tm = localtime(&Start);
604     Month = 12 * tm->tm_year + tm->tm_mon + RelMonth;
605     Year = Month / 12;
606     Month = Month % 12 + 1;
607     return DSTcorrect(Start,
608 	    Convert(Month, (time_t)tm->tm_mday, Year,
609 		(time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
610 		MER24, DSTmaybe));
611 }
612 
613 
614 static int
615 LookupWord(buff)
616     char		*buff;
617 {
618     char	*p;
619     char	*q;
620     TABLE	*tp;
621     uint_t	i;
622     int		abbrev;
623 
624     /* Make it lowercase. */
625     for (p = buff; *p; p++)
626 	if (isupper((u_char)*p))
627 	    *p = tolower(*p);
628 
629     if (strcmp(buff, "am") == 0 || strcmp(buff, "a.m.") == 0) {
630 	yylval.Meridian = MERam;
631 	return tMERIDIAN;
632     }
633     if (strcmp(buff, "pm") == 0 || strcmp(buff, "p.m.") == 0) {
634 	yylval.Meridian = MERpm;
635 	return tMERIDIAN;
636     }
637 
638     /* See if we have an abbreviation for a month. */
639     if (strlen(buff) == 3)
640 	abbrev = 1;
641     else if (strlen(buff) == 4 && buff[3] == '.') {
642 	abbrev = 1;
643 	buff[3] = '\0';
644     }
645     else
646 	abbrev = 0;
647 
648     for (tp = MonthDayTable; tp->name; tp++) {
649 	if (abbrev) {
650 	    if (strncmp(buff, tp->name, 3) == 0) {
651 		yylval.Number = tp->value;
652 		return tp->type;
653 	    }
654 	}
655 	else if (strcmp(buff, tp->name) == 0) {
656 	    yylval.Number = tp->value;
657 	    return tp->type;
658 	}
659     }
660 
661     for (tp = TimezoneTable; tp->name; tp++)
662 	if (strcmp(buff, tp->name) == 0) {
663 	    yylval.Number = tp->value;
664 	    return tp->type;
665 	}
666 
667     for (tp = UnitsTable; tp->name; tp++)
668 	if (strcmp(buff, tp->name) == 0) {
669 	    yylval.Number = tp->value;
670 	    return tp->type;
671 	}
672 
673     /* Strip off any plural and try the units table again. */
674     i = strlen(buff) - 1;
675     if (buff[i] == 's') {
676 	buff[i] = '\0';
677 	for (tp = UnitsTable; tp->name; tp++)
678 	    if (strcmp(buff, tp->name) == 0) {
679 		yylval.Number = tp->value;
680 		return tp->type;
681 	    }
682     }
683 
684     for (tp = OtherTable; tp->name; tp++)
685 	if (strcmp(buff, tp->name) == 0) {
686 	    yylval.Number = tp->value;
687 	    return tp->type;
688 	}
689 
690     /* Military timezones. */
691     if (buff[1] == '\0' && isalpha((u_char)*buff)) {
692 	for (tp = MilitaryTable; tp->name; tp++)
693 	    if (strcmp(buff, tp->name) == 0) {
694 		yylval.Number = tp->value;
695 		return tp->type;
696 	    }
697     }
698 
699     /* Drop out any periods and try the timezone table again. */
700     for (i = 0, p = q = buff; *q; q++)
701 	if (*q != '.')
702 	    *p++ = *q;
703 	else
704 	    i++;
705     *p = '\0';
706     if (i)
707 	for (tp = TimezoneTable; tp->name; tp++)
708 	    if (strcmp(buff, tp->name) == 0) {
709 		yylval.Number = tp->value;
710 		return tp->type;
711 	    }
712 
713     return tID;
714 }
715 
716 void
717 pdateerr(p)
718     char	*p;
719 {
720     char	*name = "DATEMSK";	/* env variable for date format */
721     char	*value;
722     char	fmt[256], line[256];
723     FILE	*fp;
724     time_t	now;
725     struct tm	*tm;
726 
727     value = getenv(name);
728     if (value == (char *)0) {
729 	fprintf(stderr,
730 	    dgettext(domainname, "%s: Environment variable %s not set\n"),
731 		p, name);
732 	return;
733     }
734     switch (getdate_err) {
735 	case 0:
736 	default:
737 	    fprintf(stderr,
738 		dgettext(domainname, "%s: Unkown getdate() error\n"), p);
739 	    break;
740 	case 1:
741 	    fprintf(stderr,
742 		dgettext(domainname, "%s: %s null or undefined\n"), p, name);
743 	    break;
744 	case 2:
745 	    fprintf(stderr, dgettext(domainname,
746 		"%s: Cannot read template file %s\n"), p, value);
747 	    break;
748 	case 3:
749 	    fprintf(stderr, dgettext(domainname,
750 		"%s: Failed to get file status information\n"), p);
751 	    break;
752 	case 4:
753 	    fprintf(stderr, dgettext(domainname,
754 		"%s: Template file %s not a regular file\n"), p, value);
755 	    break;
756 	case 5:
757 	    fprintf(stderr, dgettext(domainname,
758 		"%s: Error reading template file %s\n"), p, value);
759 	    break;
760 	case 6:
761 	    fprintf(stderr, dgettext(domainname,
762 		"%s: %s failed\n"), p, "malloc()");
763 	    break;
764 	case 7:
765 	    fprintf(stderr, dgettext(domainname,
766 		"%s: Bad date/time format\n"), p);
767 	    fp = fopen(value, "r");
768 	    if (fp == (FILE *)0)
769 		break;
770 	    now = time((time_t *)0);
771 	    tm = localtime(&now);
772 	    fprintf(stderr, dgettext(domainname,
773 		"The following are examples of valid formats:\n"));
774 	    while (fgets(fmt, sizeof (fmt), fp)) {
775 		if (strchr(fmt, '%') == (char *)0)
776 		    continue;
777 		fprintf(stderr, "    ");
778 	        (void) strftime(line, sizeof (line), fmt, tm);
779 		fprintf(stderr, "%s", line);
780 	    }
781 	    (void) fclose(fp);
782 	    break;
783 	case 8:
784 	    (void) fprintf(stderr, dgettext(domainname,
785 		"%s: Invalid date specification\n"), p);
786 	    break;
787     }
788 }
789 
790 #undef yylex
791 static int
792 yylex()
793 {
794     char	c;
795     char	*p;
796     char	buff[20];
797     int		Count;
798     int		sign;
799 
800     for ( ; ; ) {
801 	while (isspace((u_char)*yyInput))
802 	    yyInput++;
803 
804 	if (isdigit((u_char)(c = *yyInput)) || c == '-' || c == '+') {
805 	    if (c == '-' || c == '+') {
806 		sign = c == '-' ? -1 : 1;
807 		if (!isdigit((u_char)*++yyInput))
808 		    /* skip the '-' sign */
809 		    continue;
810 	    }
811 	    else
812 		sign = 0;
813 	    yylval.Number = 0;
814 	    while (isdigit((u_char)(c = *yyInput++))) {
815 		int n;
816 		char digit = c;
817 		(void) sscanf(&digit, "%1d", &n);
818 		yylval.Number = 10 * yylval.Number + n;
819 	    }
820 	    yyInput--;
821 	    if (sign < 0)
822 		yylval.Number = -yylval.Number;
823 	    return sign ? tSNUMBER : tUNUMBER;
824 	}
825 	if (isalpha((u_char)c)) {
826 	    for (p = buff; isalpha((u_char)(c = *yyInput++)) || c == '.'; )
827 		if (p < &buff[sizeof (buff) - 1])
828 		    *p++ = c;
829 	    *p = '\0';
830 	    yyInput--;
831 	    return LookupWord(buff);
832 	}
833 	if (c != '(')
834 	    return *yyInput++;
835 	Count = 0;
836 	do {
837 	    c = *yyInput++;
838 	    if (c == '\0')
839 		return c;
840 	    if (c == '(')
841 		Count++;
842 	    else if (c == ')')
843 		Count--;
844 	} while (Count > 0);
845     }
846 }
847 
848 
849 time_t
850 getreldate(p, now)
851     char		*p;
852     struct timeb	*now;
853 {
854     struct tm		*tm;
855     struct timeb	ftz;
856     time_t		Start;
857     time_t		tod;
858 
859     if (strcmp(setlocale(LC_TIME, NULL), "C")) {
860 	static char localedate[24];
861 	struct tm ltm;
862 
863 	tm = getdate(p);
864 	if (getdate_err == 1 /* NODATEMASK */) {
865 	    char buffy[BUFSIZ];
866 	    time_t current;
867 
868 	    printf(gettext("environment variable %s not set\n"), "DATEMSK");
869 	    do {
870 		time(&current);
871 		tm = localtime(&current);
872 		memcpy(&ltm, tm, sizeof(ltm));
873 		tm = &ltm;
874 
875 		(void) fputs(gettext("Enter date as mmddhhmm[yy]: "), stdout);
876 		(void) fflush(stdout);
877 		if (fgets(buffy, sizeof (buffy), stdin) == NULL) {
878 			(void) printf(gettext("Encountered EOF on stdin\n"));
879 			return(-1);
880 		}
881 	    } while (sscanf(buffy, "%2d%2d%2d%2d%2d",
882 		&(tm->tm_mon), &(tm->tm_mday), &(tm->tm_hour),
883 		&(tm->tm_min), &(tm->tm_year)) < 4);
884 
885 	    (tm->tm_mon)--;
886 	} else if (tm == NULL)
887 	    return(-1);
888 
889 	(void)sprintf(localedate, "%d:%2.2d %d/%d %d",
890 	    tm->tm_hour, tm->tm_min, tm->tm_mon + 1,
891 	    tm->tm_mday, CHECK_TM(tm->tm_year));
892 	p = localedate;
893     }
894 
895     yyInput = p;
896     if (now == NULL) {
897 	now = &ftz;
898 	(void) time(&ftz.time);
899 	/* Set the timezone global. */
900 	tzset();
901 	/* LINTED timezone is time_t so intermediate results aren't truncated */
902 	ftz.timezone = (int) timezone / 60;
903     }
904 
905     tm = localtime(&now->time);
906     yyYear = tm->tm_year;
907     yyMonth = tm->tm_mon + 1;
908     yyDay = tm->tm_mday;
909     yyTimezone = now->timezone;
910     yyDSTmode = DSTmaybe;
911     yyHour = tm->tm_hour;
912     yyMinutes = tm->tm_min;
913     yySeconds = tm->tm_sec;
914     yyMeridian = MER24;
915     yyRelSeconds = 0;
916     yyRelMonth = 0;
917     yyHaveDate = 0;
918     yyHaveDay = 0;
919     yyHaveRel = 0;
920     yyHaveTime = 0;
921     yyHaveZone = 0;
922 
923     if (yyparse()
924      || yyHaveTime > 1 || yyHaveZone > 1 || yyHaveDate > 1 || yyHaveDay > 1)
925 	return -1;
926 
927     if (yyHaveDate || yyHaveTime || yyHaveDay) {
928 	Start = Convert(yyMonth, yyDay, yyYear, yyHour, yyMinutes, yySeconds,
929 		    yyMeridian, yyDSTmode);
930 	if (Start < 0)
931 	    return -1;
932     }
933     else {
934 	Start = now->time;
935 	if (!yyHaveRel)
936 	    Start -= ((tm->tm_hour * 60L) + tm->tm_min * 60L) + tm->tm_sec;
937     }
938 
939     Start += yyRelSeconds;
940     Start += RelativeMonth(Start, yyRelMonth);
941 
942     if (yyHaveDay && !yyHaveDate) {
943 	tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber);
944 	Start += tod;
945     }
946 
947     /* Have to do *something* with a legitimate -1 so it's distinguishable
948      * from the error return value.  (Alternately could set errno on error.) */
949     return Start == -1 ? 0 : Start;
950 }
951 
952 #if	defined(TEST)
953 
954 /* ARGSUSED */
955 main(ac, av)
956     int		ac;
957     char	*av[];
958 {
959     char	buff[128];
960     time_t	d;
961 
962     (void) setlocale(LC_ALL, "");
963 #if !defined(TEXT_DOMAIN)
964 #define	TEXT_DOMAIN "SYS_TEST"
965 #endif
966     (void) textdomain(TEXT_DOMAIN);
967 
968     (void) printf(gettext("Enter date, or blank line to exit.\n\t> "));
969     (void) fflush(stdout);
970     while (gets(buff) && buff[0]) {
971 	d = getreldate(buff, (struct timeb *)NULL);
972 	if (d == -1)
973 	    (void) printf(gettext("Bad format - couldn't convert.\n"));
974 	else {
975 	    (void) cftime(buff, "%c\n", &d);
976 	    (void) printf("%s", buff);
977 	}
978 	(void) printf("\t> ");
979 	(void) fflush(stdout);
980     }
981     exit(0);
982     /* NOTREACHED */
983 }
984 #endif	/* defined(TEST) */
985