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