xref: /illumos-gate/usr/src/cmd/backup/lib/getdate.y (revision d48be21240dfd051b689384ce2b23479d757f2d8)
1 %{
2 /*
3  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
4  * Use is subject to license terms.
5  */
6 
7 /* $OrigRevision: 2.1 $
8 **
9 **  Originally written by Steven M. Bellovin <smb@research.att.com> while
10 **  at the University of North Carolina at Chapel Hill.  Later tweaked by
11 **  a couple of people on Usenet.  Completely overhauled by Rich $alz
12 **  <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
13 **  send any email to Rich.
14 **
15 **  This grammar has eight shift/reduce conflicts.
16 **
17 **  This code is in the public domain and has no copyright.
18 */
19 /* SUPPRESS 287 on yaccpar_sccsid *//* Unusd static variable */
20 /* SUPPRESS 288 on yyerrlab *//* Label unused */
21 #include <stdio.h>
22 #include <ctype.h>
23 
24 #include <sys/types.h>
25 #define NEED_TZSET
26 struct timeb {
27     time_t		time;		/* Seconds since the epoch	*/
28     unsigned short	millitm;	/* Field not used		*/
29     short		timezone;
30     short		dstflag;	/* Field not used		*/
31 };
32 #include <time.h>
33 
34 #include <locale.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <note.h>
38 #include <libintl.h>
39 
40 #if	!defined(lint) && !defined(SABER)
41 static char RCS[] =
42 	"$Header: /home/laramie/berliner/ws/backup/usr/src/cmd/backup/lib/getdate.y,v 1.5 1992/06/09 21:46:21 sam Exp $";
43 #endif	/* !defined(lint) && !defined(SABER) */
44 
45 
46 #define EPOCH		1970
47 #define HOURN(x)	(x * 60)
48 #define SECSPERDAY	(24L * 60L * 60L)
49 
50 #define CHECK_TM(y) (((y) % 100) < 70 ? (y) + 2000 : (y) + 1900)
51 
52 /*
53 **  An entry in the lexical lookup table.
54 */
55 typedef struct _TABLE {
56     char	*name;
57     int		type;
58     time_t	value;
59 } TABLE;
60 
61 
62 /*
63 **  Daylight-savings mode:  on, off, or not yet known.
64 */
65 typedef enum _DSTMODE {
66     DSTon, DSToff, DSTmaybe
67 } DSTMODE;
68 
69 /*
70 **  Meridian:  am, pm, or 24-hour style.
71 */
72 typedef enum _MERIDIAN {
73     MERam, MERpm, MER24
74 } MERIDIAN;
75 
76 
77 /*
78 **  Global variables.  We could get rid of most of these by using a good
79 **  union as the yacc stack.  (This routine was originally written before
80 **  yacc had the %union construct.)  Maybe someday; right now we only use
81 **  the %union very rarely.
82 */
83 static char	*yyInput;
84 static DSTMODE	yyDSTmode;
85 static time_t	yyDayOrdinal;
86 static time_t	yyDayNumber;
87 static int	yyHaveDate;
88 static int	yyHaveDay;
89 static int	yyHaveRel;
90 static int	yyHaveTime;
91 static int	yyHaveZone;
92 static time_t	yyTimezone;
93 static time_t	yyDay;
94 static time_t	yyHour;
95 static time_t	yyMinutes;
96 static time_t	yyMonth;
97 static time_t	yySeconds;
98 static time_t	yyYear;
99 static MERIDIAN	yyMeridian;
100 static time_t	yyRelMonth;
101 static time_t	yyRelSeconds;
102 
103 static char	*domainname = "hsm_libdump";	/* for dgettext() */
104 
105 #define yylex 1					/* suppress yacc's definition */
106 %}
107 
108 %union {
109     time_t		Number;
110     enum _MERIDIAN	Meridian;
111 }
112 
113 %token	tAGO tDAY tDAYZONE tID tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
114 %token	tSEC_UNIT tSNUMBER tUNUMBER tZONE
115 
116 %type	<Number>	tDAY tDAYZONE tMINUTE_UNIT tMONTH tMONTH_UNIT
117 %type	<Number>	tSEC_UNIT tSNUMBER tUNUMBER tZONE
118 %type	<Meridian>	tMERIDIAN o_merid
119 
120 %%
121 
122 spec	: /* NULL */
123 	| spec item
124 	;
125 
126 item	: time {
127 	    yyHaveTime++;
128 	}
129 	| zone {
130 	    yyHaveZone++;
131 	}
132 	| date {
133 	    yyHaveDate++;
134 	}
135 	| day {
136 	    yyHaveDay++;
137 	}
138 	| rel {
139 	    yyHaveRel++;
140 	}
141 	| number
142 	;
143 
144 time	: tUNUMBER tMERIDIAN {
145 	    yyHour = $1;
146 	    yyMinutes = 0;
147 	    yySeconds = 0;
148 	    yyMeridian = $2;
149 	}
150 	| tUNUMBER ':' tUNUMBER o_merid {
151 	    yyHour = $1;
152 	    yyMinutes = $3;
153 	    yySeconds = 0;
154 	    yyMeridian = $4;
155 	}
156 	| tUNUMBER ':' tUNUMBER tSNUMBER {
157 	    yyHour = $1;
158 	    yyMinutes = $3;
159 	    yyMeridian = MER24;
160 	    yyDSTmode = DSToff;
161 	    yyTimezone = - ($4 % 100 + ($4 / 100) * 60);
162 	}
163 	| tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
164 	    yyHour = $1;
165 	    yyMinutes = $3;
166 	    yySeconds = $5;
167 	    yyMeridian = $6;
168 	}
169 	| tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
170 	    yyHour = $1;
171 	    yyMinutes = $3;
172 	    yySeconds = $5;
173 	    yyMeridian = MER24;
174 	    yyDSTmode = DSToff;
175 	    yyTimezone = - ($6 % 100 + ($6 / 100) * 60);
176 	}
177 	;
178 
179 zone	: tZONE {
180 	    yyTimezone = $1;
181 	    yyDSTmode = DSToff;
182 	}
183 	| tDAYZONE {
184 	    yyTimezone = $1;
185 	    yyDSTmode = DSTon;
186 	}
187 	;
188 
189 day	: tDAY {
190 	    yyDayOrdinal = 1;
191 	    yyDayNumber = $1;
192 	}
193 	| tDAY ',' {
194 	    yyDayOrdinal = 1;
195 	    yyDayNumber = $1;
196 	}
197 	| tUNUMBER tDAY {
198 	    yyDayOrdinal = $1;
199 	    yyDayNumber = $2;
200 	}
201 	;
202 
203 date	: tUNUMBER '/' tUNUMBER {
204 	    yyMonth = $1;
205 	    yyDay = $3;
206 	}
207 	| tUNUMBER '/' tUNUMBER '/' tUNUMBER {
208 	    yyMonth = $1;
209 	    yyDay = $3;
210 	    yyYear = $5;
211 	}
212 	| tMONTH tUNUMBER {
213 	    yyMonth = $1;
214 	    yyDay = $2;
215 	}
216 	| tMONTH tUNUMBER ',' tUNUMBER {
217 	    yyMonth = $1;
218 	    yyDay = $2;
219 	    yyYear = $4;
220 	}
221 	| tUNUMBER tMONTH {
222 	    yyMonth = $2;
223 	    yyDay = $1;
224 	}
225 	| tUNUMBER tMONTH tUNUMBER {
226 	    yyMonth = $2;
227 	    yyDay = $1;
228 	    yyYear = $3;
229 	}
230 	;
231 
232 rel	: relunit tAGO {
233 	    yyRelSeconds = -yyRelSeconds;
234 	    yyRelMonth = -yyRelMonth;
235 	}
236 	| relunit
237 	;
238 
239 relunit	: tUNUMBER tMINUTE_UNIT {
240 	    yyRelSeconds += $1 * $2 * 60L;
241 	}
242 	| tSNUMBER tMINUTE_UNIT {
243 	    yyRelSeconds += $1 * $2 * 60L;
244 	}
245 	| tMINUTE_UNIT {
246 	    yyRelSeconds += $1 * 60L;
247 	}
248 	| tSNUMBER tSEC_UNIT {
249 	    yyRelSeconds += $1;
250 	}
251 	| tUNUMBER tSEC_UNIT {
252 	    yyRelSeconds += $1;
253 	}
254 	| tSEC_UNIT {
255 	    yyRelSeconds++;
256 	}
257 	| tSNUMBER tMONTH_UNIT {
258 	    yyRelMonth += $1 * $2;
259 	}
260 	| tUNUMBER tMONTH_UNIT {
261 	    yyRelMonth += $1 * $2;
262 	}
263 	| tMONTH_UNIT {
264 	    yyRelMonth += $1;
265 	}
266 	;
267 
268 number	: tUNUMBER {
269 	    if (yyHaveTime && yyHaveDate && !yyHaveRel)
270 		yyYear = $1;
271 	    else {
272 		yyHaveTime++;
273 		if ($1 < 100) {
274 		    yyHour = $1;
275 		    yyMinutes = 0;
276 		}
277 		else {
278 		    yyHour = $1 / 100;
279 		    yyMinutes = $1 % 100;
280 		}
281 		yySeconds = 0;
282 		yyMeridian = MER24;
283 	    }
284 	}
285 	;
286 
287 o_merid	: /* NULL */ {
288 	    $$ = MER24;
289 	}
290 	| tMERIDIAN {
291 	    $$ = $1;
292 	}
293 	;
294 
295 %%
296 
297 /* Month and day table. */
298 static TABLE	MonthDayTable[] = {
299     { "january",	tMONTH,  1 },
300     { "february",	tMONTH,  2 },
301     { "march",		tMONTH,  3 },
302     { "april",		tMONTH,  4 },
303     { "may",		tMONTH,  5 },
304     { "june",		tMONTH,  6 },
305     { "july",		tMONTH,  7 },
306     { "august",		tMONTH,  8 },
307     { "september",	tMONTH,  9 },
308     { "sept",		tMONTH,  9 },
309     { "october",	tMONTH, 10 },
310     { "november",	tMONTH, 11 },
311     { "december",	tMONTH, 12 },
312     { "sunday",		tDAY, 0 },
313     { "monday",		tDAY, 1 },
314     { "tuesday",	tDAY, 2 },
315     { "tues",		tDAY, 2 },
316     { "wednesday",	tDAY, 3 },
317     { "wednes",		tDAY, 3 },
318     { "thursday",	tDAY, 4 },
319     { "thur",		tDAY, 4 },
320     { "thurs",		tDAY, 4 },
321     { "friday",		tDAY, 5 },
322     { "saturday",	tDAY, 6 },
323     { NULL }
324 };
325 
326 /* Time units table. */
327 static TABLE	UnitsTable[] = {
328     { "year",		tMONTH_UNIT,	12 },
329     { "month",		tMONTH_UNIT,	1 },
330     { "fortnight",	tMINUTE_UNIT,	14 * 24 * 60 },
331     { "week",		tMINUTE_UNIT,	7 * 24 * 60 },
332     { "day",		tMINUTE_UNIT,	1 * 24 * 60 },
333     { "hour",		tMINUTE_UNIT,	60 },
334     { "minute",		tMINUTE_UNIT,	1 },
335     { "min",		tMINUTE_UNIT,	1 },
336     { "second",		tSEC_UNIT,	1 },
337     { "sec",		tSEC_UNIT,	1 },
338     { NULL }
339 };
340 
341 /* Assorted relative-time words. */
342 static TABLE	OtherTable[] = {
343     { "tomorrow",	tMINUTE_UNIT,	1 * 24 * 60 },
344     { "yesterday",	tMINUTE_UNIT,	-1 * 24 * 60 },
345     { "today",		tMINUTE_UNIT,	0 },
346     { "now",		tMINUTE_UNIT,	0 },
347     { "last",		tUNUMBER,	-1 },
348     { "this",		tMINUTE_UNIT,	0 },
349     { "next",		tUNUMBER,	2 },
350     { "first",		tUNUMBER,	1 },
351 /*  { "second",		tUNUMBER,	2 }, */
352     { "third",		tUNUMBER,	3 },
353     { "fourth",		tUNUMBER,	4 },
354     { "fifth",		tUNUMBER,	5 },
355     { "sixth",		tUNUMBER,	6 },
356     { "seventh",	tUNUMBER,	7 },
357     { "eighth",		tUNUMBER,	8 },
358     { "ninth",		tUNUMBER,	9 },
359     { "tenth",		tUNUMBER,	10 },
360     { "eleventh",	tUNUMBER,	11 },
361     { "twelfth",	tUNUMBER,	12 },
362     { "ago",		tAGO,	1 },
363     { NULL }
364 };
365 
366 /* The timezone table. */
367 static TABLE	TimezoneTable[] = {
368     { "gmt",	tZONE,     HOURN( 0) },	/* Greenwich Mean */
369     { "ut",	tZONE,     HOURN( 0) },	/* Universal (Coordinated) */
370     { "utc",	tZONE,     HOURN( 0) },
371     { "wet",	tZONE,     HOURN( 0) },	/* Western European */
372     { "bst",	tDAYZONE,  HOURN( 0) },	/* British Summer */
373     { "wat",	tZONE,     HOURN( 1) },	/* West Africa */
374     { "at",	tZONE,     HOURN( 2) },	/* Azores */
375 #if	0
376     /* For completeness.  BST is also British Summer, and GST is
377      * also Guam Standard. */
378     { "bst",	tZONE,     HOURN( 3) },	/* Brazil Standard */
379     { "gst",	tZONE,     HOURN( 3) },	/* Greenland Standard */
380 #endif
381     { "nft",	tZONE,     HOURN(3.5) },	/* Newfoundland */
382     { "nst",	tZONE,     HOURN(3.5) },	/* Newfoundland Standard */
383     { "ndt",	tDAYZONE,  HOURN(3.5) },	/* Newfoundland Daylight */
384     { "ast",	tZONE,     HOURN( 4) },	/* Atlantic Standard */
385     { "adt",	tDAYZONE,  HOURN( 4) },	/* Atlantic Daylight */
386     { "est",	tZONE,     HOURN( 5) },	/* Eastern Standard */
387     { "edt",	tDAYZONE,  HOURN( 5) },	/* Eastern Daylight */
388     { "cst",	tZONE,     HOURN( 6) },	/* Central Standard */
389     { "cdt",	tDAYZONE,  HOURN( 6) },	/* Central Daylight */
390     { "mst",	tZONE,     HOURN( 7) },	/* Mountain Standard */
391     { "mdt",	tDAYZONE,  HOURN( 7) },	/* Mountain Daylight */
392     { "pst",	tZONE,     HOURN( 8) },	/* Pacific Standard */
393     { "pdt",	tDAYZONE,  HOURN( 8) },	/* Pacific Daylight */
394     { "yst",	tZONE,     HOURN( 9) },	/* Yukon Standard */
395     { "ydt",	tDAYZONE,  HOURN( 9) },	/* Yukon Daylight */
396     { "hst",	tZONE,     HOURN(10) },	/* Hawaii Standard */
397     { "hdt",	tDAYZONE,  HOURN(10) },	/* Hawaii Daylight */
398     { "cat",	tZONE,     HOURN(10) },	/* Central Alaska */
399     { "ahst",	tZONE,     HOURN(10) },	/* Alaska-Hawaii Standard */
400     { "nt",	tZONE,     HOURN(11) },	/* Nome */
401     { "idlw",	tZONE,     HOURN(12) },	/* International Date Line West */
402     { "cet",	tZONE,     -HOURN(1) },	/* Central European */
403     { "met",	tZONE,     -HOURN(1) },	/* Middle European */
404     { "mewt",	tZONE,     -HOURN(1) },	/* Middle European Winter */
405     { "mest",	tDAYZONE,  -HOURN(1) },	/* Middle European Summer */
406     { "swt",	tZONE,     -HOURN(1) },	/* Swedish Winter */
407     { "sst",	tDAYZONE,  -HOURN(1) },	/* Swedish Summer */
408     { "fwt",	tZONE,     -HOURN(1) },	/* French Winter */
409     { "fst",	tDAYZONE,  -HOURN(1) },	/* French Summer */
410     { "eet",	tZONE,     -HOURN(2) },	/* Eastern Europe, USSR Zone 1 */
411     { "bt",	tZONE,     -HOURN(3) },	/* Baghdad, USSR Zone 2 */
412     { "it",	tZONE,     -HOURN(3.5) },/* Iran */
413     { "zp4",	tZONE,     -HOURN(4) },	/* USSR Zone 3 */
414     { "zp5",	tZONE,     -HOURN(5) },	/* USSR Zone 4 */
415     { "ist",	tZONE,     -HOURN(5.5) },/* Indian Standard */
416     { "zp6",	tZONE,     -HOURN(6) },	/* USSR Zone 5 */
417 #if	0
418     /* For completeness.  NST is also Newfoundland Stanard, nad SST is
419      * also Swedish Summer. */
420     { "nst",	tZONE,     -HOURN(6.5) },/* North Sumatra */
421     { "sst",	tZONE,     -HOURN(7) },	/* South Sumatra, USSR Zone 6 */
422 #endif	/* 0 */
423     { "wast",	tZONE,     -HOURN(7) },	/* West Australian Standard */
424     { "wadt",	tDAYZONE,  -HOURN(7) },	/* West Australian Daylight */
425     { "jt",	tZONE,     -HOURN(7.5) },/* Java (3pm in Cronusland!) */
426     { "cct",	tZONE,     -HOURN(8) },	/* China Coast, USSR Zone 7 */
427     { "jst",	tZONE,     -HOURN(9) },	/* Japan Standard, USSR Zone 8 */
428     { "cast",	tZONE,     -HOURN(9.5) },/* Central Australian Standard */
429     { "cadt",	tDAYZONE,  -HOURN(9.5) },/* Central Australian Daylight */
430     { "east",	tZONE,     -HOURN(10) },	/* Eastern Australian Standard */
431     { "eadt",	tDAYZONE,  -HOURN(10) },	/* Eastern Australian Daylight */
432     { "gst",	tZONE,     -HOURN(10) },	/* Guam Standard, USSR Zone 9 */
433     { "nzt",	tZONE,     -HOURN(12) },	/* New Zealand */
434     { "nzst",	tZONE,     -HOURN(12) },	/* New Zealand Standard */
435     { "nzdt",	tDAYZONE,  -HOURN(12) },	/* New Zealand Daylight */
436     { "idle",	tZONE,     -HOURN(12) },	/* International Date Line East */
437     {  NULL  }
438 };
439 
440 /* Military timezone table. */
441 static TABLE	MilitaryTable[] = {
442     { "a",	tZONE,	HOURN(  1) },
443     { "b",	tZONE,	HOURN(  2) },
444     { "c",	tZONE,	HOURN(  3) },
445     { "d",	tZONE,	HOURN(  4) },
446     { "e",	tZONE,	HOURN(  5) },
447     { "f",	tZONE,	HOURN(  6) },
448     { "g",	tZONE,	HOURN(  7) },
449     { "h",	tZONE,	HOURN(  8) },
450     { "i",	tZONE,	HOURN(  9) },
451     { "k",	tZONE,	HOURN( 10) },
452     { "l",	tZONE,	HOURN( 11) },
453     { "m",	tZONE,	HOURN( 12) },
454     { "n",	tZONE,	HOURN(- 1) },
455     { "o",	tZONE,	HOURN(- 2) },
456     { "p",	tZONE,	HOURN(- 3) },
457     { "q",	tZONE,	HOURN(- 4) },
458     { "r",	tZONE,	HOURN(- 5) },
459     { "s",	tZONE,	HOURN(- 6) },
460     { "t",	tZONE,	HOURN(- 7) },
461     { "u",	tZONE,	HOURN(- 8) },
462     { "v",	tZONE,	HOURN(- 9) },
463     { "w",	tZONE,	HOURN(-10) },
464     { "x",	tZONE,	HOURN(-11) },
465     { "y",	tZONE,	HOURN(-12) },
466     { "z",	tZONE,	HOURN(  0) },
467     { NULL }
468 };
469 
470 
471 
472 
473 /* ARGSUSED */
474 static void
475 yyerror(s)
476     char	*s;
477 {
478 }
479 
480 
481 static time_t
482 ToSeconds(Hours, Minutes, Seconds, Meridian)
483     time_t	Hours;
484     time_t	Minutes;
485     time_t	Seconds;
486     MERIDIAN	Meridian;
487 {
488     if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59)
489 	return -1;
490     switch (Meridian) {
491     case MER24:
492 	if (Hours < 0 || Hours > 23)
493 	    return -1;
494 	return (Hours * 60L + Minutes) * 60L + Seconds;
495     case MERam:
496 	if (Hours < 1 || Hours > 12)
497 	    return -1;
498 	if (Hours != 12)
499 	    return (Hours * 60L + Minutes) * 60L + Seconds;
500 	else
501 	    return Minutes * 60L + Seconds;
502     case MERpm:
503 	if (Hours < 1 || Hours > 12)
504 	    return -1;
505 	if (Hours != 12)
506 	    return ((Hours + 12) * 60L + Minutes) * 60L + Seconds;
507 	else
508 	    return (720L + Minutes) * 60L + Seconds;
509     }
510     /* NOTREACHED */
511     return (-1);
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