add UNLEASHED_OBJ to unleashed.mk
[unleashed/tickless.git] / usr / src / cmd / backup / lib / getdate.y
blob9321d36f5aee0fdf6134582df672db7888bf887d
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"
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.
16 ** This grammar has eight shift/reduce conflicts.
18 ** This code is in the public domain and has no copyright.
20 /* SUPPRESS 287 on yaccpar_sccsid *//* Unusd static variable */
21 /* SUPPRESS 288 on yyerrlab *//* Label unused */
22 #include <stdio.h>
23 #include <ctype.h>
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 */
33 #include <time.h>
35 #include <locale.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <note.h>
39 #include <libintl.h>
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 $";
45 #define EPOCH 1970
46 #define HOURN(x) (x * 60)
47 #define SECSPERDAY (24L * 60L * 60L)
49 #define CHECK_TM(y) (((y) % 100) < 70 ? (y) + 2000 : (y) + 1900)
52 ** An entry in the lexical lookup table.
54 typedef struct _TABLE {
55 char *name;
56 int type;
57 time_t value;
58 } TABLE;
62 ** Daylight-savings mode: on, off, or not yet known.
64 typedef enum _DSTMODE {
65 DSTon, DSToff, DSTmaybe
66 } DSTMODE;
69 ** Meridian: am, pm, or 24-hour style.
71 typedef enum _MERIDIAN {
72 MERam, MERpm, MER24
73 } MERIDIAN;
77 ** Global variables. We could get rid of most of these by using a good
78 ** union as the yacc stack. (This routine was originally written before
79 ** yacc had the %union construct.) Maybe someday; right now we only use
80 ** the %union very rarely.
82 static char *yyInput;
83 static DSTMODE yyDSTmode;
84 static time_t yyDayOrdinal;
85 static time_t yyDayNumber;
86 static int yyHaveDate;
87 static int yyHaveDay;
88 static int yyHaveRel;
89 static int yyHaveTime;
90 static int yyHaveZone;
91 static time_t yyTimezone;
92 static time_t yyDay;
93 static time_t yyHour;
94 static time_t yyMinutes;
95 static time_t yyMonth;
96 static time_t yySeconds;
97 static time_t yyYear;
98 static MERIDIAN yyMeridian;
99 static time_t yyRelMonth;
100 static time_t yyRelSeconds;
102 static char *domainname = "hsm_libdump"; /* for dgettext() */
104 #define yylex 1 /* suppress yacc's definition */
107 %union {
108 time_t Number;
109 enum _MERIDIAN Meridian;
112 %token tAGO tDAY tDAYZONE tID tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
113 %token tSEC_UNIT tSNUMBER tUNUMBER tZONE
115 %type <Number> tDAY tDAYZONE tMINUTE_UNIT tMONTH tMONTH_UNIT
116 %type <Number> tSEC_UNIT tSNUMBER tUNUMBER tZONE
117 %type <Meridian> tMERIDIAN o_merid
121 spec : /* NULL */
122 | spec item
125 item : time {
126 yyHaveTime++;
128 | zone {
129 yyHaveZone++;
131 | date {
132 yyHaveDate++;
134 | day {
135 yyHaveDay++;
137 | rel {
138 yyHaveRel++;
140 | number
143 time : tUNUMBER tMERIDIAN {
144 yyHour = $1;
145 yyMinutes = 0;
146 yySeconds = 0;
147 yyMeridian = $2;
149 | tUNUMBER ':' tUNUMBER o_merid {
150 yyHour = $1;
151 yyMinutes = $3;
152 yySeconds = 0;
153 yyMeridian = $4;
155 | tUNUMBER ':' tUNUMBER tSNUMBER {
156 yyHour = $1;
157 yyMinutes = $3;
158 yyMeridian = MER24;
159 yyDSTmode = DSToff;
160 yyTimezone = - ($4 % 100 + ($4 / 100) * 60);
162 | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
163 yyHour = $1;
164 yyMinutes = $3;
165 yySeconds = $5;
166 yyMeridian = $6;
168 | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
169 yyHour = $1;
170 yyMinutes = $3;
171 yySeconds = $5;
172 yyMeridian = MER24;
173 yyDSTmode = DSToff;
174 yyTimezone = - ($6 % 100 + ($6 / 100) * 60);
178 zone : tZONE {
179 yyTimezone = $1;
180 yyDSTmode = DSToff;
182 | tDAYZONE {
183 yyTimezone = $1;
184 yyDSTmode = DSTon;
188 day : tDAY {
189 yyDayOrdinal = 1;
190 yyDayNumber = $1;
192 | tDAY ',' {
193 yyDayOrdinal = 1;
194 yyDayNumber = $1;
196 | tUNUMBER tDAY {
197 yyDayOrdinal = $1;
198 yyDayNumber = $2;
202 date : tUNUMBER '/' tUNUMBER {
203 yyMonth = $1;
204 yyDay = $3;
206 | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
207 yyMonth = $1;
208 yyDay = $3;
209 yyYear = $5;
211 | tMONTH tUNUMBER {
212 yyMonth = $1;
213 yyDay = $2;
215 | tMONTH tUNUMBER ',' tUNUMBER {
216 yyMonth = $1;
217 yyDay = $2;
218 yyYear = $4;
220 | tUNUMBER tMONTH {
221 yyMonth = $2;
222 yyDay = $1;
224 | tUNUMBER tMONTH tUNUMBER {
225 yyMonth = $2;
226 yyDay = $1;
227 yyYear = $3;
231 rel : relunit tAGO {
232 yyRelSeconds = -yyRelSeconds;
233 yyRelMonth = -yyRelMonth;
235 | relunit
238 relunit : tUNUMBER tMINUTE_UNIT {
239 yyRelSeconds += $1 * $2 * 60L;
241 | tSNUMBER tMINUTE_UNIT {
242 yyRelSeconds += $1 * $2 * 60L;
244 | tMINUTE_UNIT {
245 yyRelSeconds += $1 * 60L;
247 | tSNUMBER tSEC_UNIT {
248 yyRelSeconds += $1;
250 | tUNUMBER tSEC_UNIT {
251 yyRelSeconds += $1;
253 | tSEC_UNIT {
254 yyRelSeconds++;
256 | tSNUMBER tMONTH_UNIT {
257 yyRelMonth += $1 * $2;
259 | tUNUMBER tMONTH_UNIT {
260 yyRelMonth += $1 * $2;
262 | tMONTH_UNIT {
263 yyRelMonth += $1;
267 number : tUNUMBER {
268 if (yyHaveTime && yyHaveDate && !yyHaveRel)
269 yyYear = $1;
270 else {
271 yyHaveTime++;
272 if ($1 < 100) {
273 yyHour = $1;
274 yyMinutes = 0;
276 else {
277 yyHour = $1 / 100;
278 yyMinutes = $1 % 100;
280 yySeconds = 0;
281 yyMeridian = MER24;
286 o_merid : /* NULL */ {
287 $$ = MER24;
289 | tMERIDIAN {
290 $$ = $1;
296 /* Month and day table. */
297 static TABLE MonthDayTable[] = {
298 { "january", tMONTH, 1 },
299 { "february", tMONTH, 2 },
300 { "march", tMONTH, 3 },
301 { "april", tMONTH, 4 },
302 { "may", tMONTH, 5 },
303 { "june", tMONTH, 6 },
304 { "july", tMONTH, 7 },
305 { "august", tMONTH, 8 },
306 { "september", tMONTH, 9 },
307 { "sept", tMONTH, 9 },
308 { "october", tMONTH, 10 },
309 { "november", tMONTH, 11 },
310 { "december", tMONTH, 12 },
311 { "sunday", tDAY, 0 },
312 { "monday", tDAY, 1 },
313 { "tuesday", tDAY, 2 },
314 { "tues", tDAY, 2 },
315 { "wednesday", tDAY, 3 },
316 { "wednes", tDAY, 3 },
317 { "thursday", tDAY, 4 },
318 { "thur", tDAY, 4 },
319 { "thurs", tDAY, 4 },
320 { "friday", tDAY, 5 },
321 { "saturday", tDAY, 6 },
322 { NULL }
325 /* Time units table. */
326 static TABLE UnitsTable[] = {
327 { "year", tMONTH_UNIT, 12 },
328 { "month", tMONTH_UNIT, 1 },
329 { "fortnight", tMINUTE_UNIT, 14 * 24 * 60 },
330 { "week", tMINUTE_UNIT, 7 * 24 * 60 },
331 { "day", tMINUTE_UNIT, 1 * 24 * 60 },
332 { "hour", tMINUTE_UNIT, 60 },
333 { "minute", tMINUTE_UNIT, 1 },
334 { "min", tMINUTE_UNIT, 1 },
335 { "second", tSEC_UNIT, 1 },
336 { "sec", tSEC_UNIT, 1 },
337 { NULL }
340 /* Assorted relative-time words. */
341 static TABLE OtherTable[] = {
342 { "tomorrow", tMINUTE_UNIT, 1 * 24 * 60 },
343 { "yesterday", tMINUTE_UNIT, -1 * 24 * 60 },
344 { "today", tMINUTE_UNIT, 0 },
345 { "now", tMINUTE_UNIT, 0 },
346 { "last", tUNUMBER, -1 },
347 { "this", tMINUTE_UNIT, 0 },
348 { "next", tUNUMBER, 2 },
349 { "first", tUNUMBER, 1 },
350 /* { "second", tUNUMBER, 2 }, */
351 { "third", tUNUMBER, 3 },
352 { "fourth", tUNUMBER, 4 },
353 { "fifth", tUNUMBER, 5 },
354 { "sixth", tUNUMBER, 6 },
355 { "seventh", tUNUMBER, 7 },
356 { "eighth", tUNUMBER, 8 },
357 { "ninth", tUNUMBER, 9 },
358 { "tenth", tUNUMBER, 10 },
359 { "eleventh", tUNUMBER, 11 },
360 { "twelfth", tUNUMBER, 12 },
361 { "ago", tAGO, 1 },
362 { NULL }
365 /* The timezone table. */
366 static TABLE TimezoneTable[] = {
367 { "gmt", tZONE, HOURN( 0) }, /* Greenwich Mean */
368 { "ut", tZONE, HOURN( 0) }, /* Universal (Coordinated) */
369 { "utc", tZONE, HOURN( 0) },
370 { "wet", tZONE, HOURN( 0) }, /* Western European */
371 { "bst", tDAYZONE, HOURN( 0) }, /* British Summer */
372 { "wat", tZONE, HOURN( 1) }, /* West Africa */
373 { "at", tZONE, HOURN( 2) }, /* Azores */
374 #if 0
375 /* For completeness. BST is also British Summer, and GST is
376 * also Guam Standard. */
377 { "bst", tZONE, HOURN( 3) }, /* Brazil Standard */
378 { "gst", tZONE, HOURN( 3) }, /* Greenland Standard */
379 #endif
380 { "nft", tZONE, HOURN(3.5) }, /* Newfoundland */
381 { "nst", tZONE, HOURN(3.5) }, /* Newfoundland Standard */
382 { "ndt", tDAYZONE, HOURN(3.5) }, /* Newfoundland Daylight */
383 { "ast", tZONE, HOURN( 4) }, /* Atlantic Standard */
384 { "adt", tDAYZONE, HOURN( 4) }, /* Atlantic Daylight */
385 { "est", tZONE, HOURN( 5) }, /* Eastern Standard */
386 { "edt", tDAYZONE, HOURN( 5) }, /* Eastern Daylight */
387 { "cst", tZONE, HOURN( 6) }, /* Central Standard */
388 { "cdt", tDAYZONE, HOURN( 6) }, /* Central Daylight */
389 { "mst", tZONE, HOURN( 7) }, /* Mountain Standard */
390 { "mdt", tDAYZONE, HOURN( 7) }, /* Mountain Daylight */
391 { "pst", tZONE, HOURN( 8) }, /* Pacific Standard */
392 { "pdt", tDAYZONE, HOURN( 8) }, /* Pacific Daylight */
393 { "yst", tZONE, HOURN( 9) }, /* Yukon Standard */
394 { "ydt", tDAYZONE, HOURN( 9) }, /* Yukon Daylight */
395 { "hst", tZONE, HOURN(10) }, /* Hawaii Standard */
396 { "hdt", tDAYZONE, HOURN(10) }, /* Hawaii Daylight */
397 { "cat", tZONE, HOURN(10) }, /* Central Alaska */
398 { "ahst", tZONE, HOURN(10) }, /* Alaska-Hawaii Standard */
399 { "nt", tZONE, HOURN(11) }, /* Nome */
400 { "idlw", tZONE, HOURN(12) }, /* International Date Line West */
401 { "cet", tZONE, -HOURN(1) }, /* Central European */
402 { "met", tZONE, -HOURN(1) }, /* Middle European */
403 { "mewt", tZONE, -HOURN(1) }, /* Middle European Winter */
404 { "mest", tDAYZONE, -HOURN(1) }, /* Middle European Summer */
405 { "swt", tZONE, -HOURN(1) }, /* Swedish Winter */
406 { "sst", tDAYZONE, -HOURN(1) }, /* Swedish Summer */
407 { "fwt", tZONE, -HOURN(1) }, /* French Winter */
408 { "fst", tDAYZONE, -HOURN(1) }, /* French Summer */
409 { "eet", tZONE, -HOURN(2) }, /* Eastern Europe, USSR Zone 1 */
410 { "bt", tZONE, -HOURN(3) }, /* Baghdad, USSR Zone 2 */
411 { "it", tZONE, -HOURN(3.5) },/* Iran */
412 { "zp4", tZONE, -HOURN(4) }, /* USSR Zone 3 */
413 { "zp5", tZONE, -HOURN(5) }, /* USSR Zone 4 */
414 { "ist", tZONE, -HOURN(5.5) },/* Indian Standard */
415 { "zp6", tZONE, -HOURN(6) }, /* USSR Zone 5 */
416 #if 0
417 /* For completeness. NST is also Newfoundland Stanard, nad SST is
418 * also Swedish Summer. */
419 { "nst", tZONE, -HOURN(6.5) },/* North Sumatra */
420 { "sst", tZONE, -HOURN(7) }, /* South Sumatra, USSR Zone 6 */
421 #endif /* 0 */
422 { "wast", tZONE, -HOURN(7) }, /* West Australian Standard */
423 { "wadt", tDAYZONE, -HOURN(7) }, /* West Australian Daylight */
424 { "jt", tZONE, -HOURN(7.5) },/* Java (3pm in Cronusland!) */
425 { "cct", tZONE, -HOURN(8) }, /* China Coast, USSR Zone 7 */
426 { "jst", tZONE, -HOURN(9) }, /* Japan Standard, USSR Zone 8 */
427 { "cast", tZONE, -HOURN(9.5) },/* Central Australian Standard */
428 { "cadt", tDAYZONE, -HOURN(9.5) },/* Central Australian Daylight */
429 { "east", tZONE, -HOURN(10) }, /* Eastern Australian Standard */
430 { "eadt", tDAYZONE, -HOURN(10) }, /* Eastern Australian Daylight */
431 { "gst", tZONE, -HOURN(10) }, /* Guam Standard, USSR Zone 9 */
432 { "nzt", tZONE, -HOURN(12) }, /* New Zealand */
433 { "nzst", tZONE, -HOURN(12) }, /* New Zealand Standard */
434 { "nzdt", tDAYZONE, -HOURN(12) }, /* New Zealand Daylight */
435 { "idle", tZONE, -HOURN(12) }, /* International Date Line East */
436 { NULL }
439 /* Military timezone table. */
440 static TABLE MilitaryTable[] = {
441 { "a", tZONE, HOURN( 1) },
442 { "b", tZONE, HOURN( 2) },
443 { "c", tZONE, HOURN( 3) },
444 { "d", tZONE, HOURN( 4) },
445 { "e", tZONE, HOURN( 5) },
446 { "f", tZONE, HOURN( 6) },
447 { "g", tZONE, HOURN( 7) },
448 { "h", tZONE, HOURN( 8) },
449 { "i", tZONE, HOURN( 9) },
450 { "k", tZONE, HOURN( 10) },
451 { "l", tZONE, HOURN( 11) },
452 { "m", tZONE, HOURN( 12) },
453 { "n", tZONE, HOURN(- 1) },
454 { "o", tZONE, HOURN(- 2) },
455 { "p", tZONE, HOURN(- 3) },
456 { "q", tZONE, HOURN(- 4) },
457 { "r", tZONE, HOURN(- 5) },
458 { "s", tZONE, HOURN(- 6) },
459 { "t", tZONE, HOURN(- 7) },
460 { "u", tZONE, HOURN(- 8) },
461 { "v", tZONE, HOURN(- 9) },
462 { "w", tZONE, HOURN(-10) },
463 { "x", tZONE, HOURN(-11) },
464 { "y", tZONE, HOURN(-12) },
465 { "z", tZONE, HOURN( 0) },
466 { NULL }
472 /* ARGSUSED */
473 static void
474 yyerror(s)
475 char *s;
480 static time_t
481 ToSeconds(Hours, Minutes, Seconds, Meridian)
482 time_t Hours;
483 time_t Minutes;
484 time_t Seconds;
485 MERIDIAN Meridian;
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;
509 /* NOTREACHED */
510 return (-1);
514 static time_t
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;
525 static int DaysInMonth[12] = {
526 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
528 time_t tod;
529 time_t Julian;
530 time_t i;
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;
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;
560 static time_t
561 DSTcorrect(Start, Future)
562 time_t Start;
563 time_t Future;
565 time_t StartDay;
566 time_t FutureDay;
568 StartDay = (localtime(&Start)->tm_hour + 1) % 24;
569 FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
570 return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
574 static time_t
575 RelativeDate(Start, DayOrdinal, DayNumber)
576 time_t Start;
577 time_t DayOrdinal;
578 time_t DayNumber;
580 struct tm *tm;
581 time_t now;
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);
591 static time_t
592 RelativeMonth(Start, RelMonth)
593 time_t Start;
594 time_t RelMonth;
596 struct tm *tm;
597 time_t Month;
598 time_t Year;
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));
613 static int
614 LookupWord(buff)
615 char *buff;
617 char *p;
618 char *q;
619 TABLE *tp;
620 uint_t i;
621 int abbrev;
623 /* Make it lowercase. */
624 for (p = buff; *p; p++)
625 if (isupper((u_char)*p))
626 *p = tolower(*p);
628 if (strcmp(buff, "am") == 0 || strcmp(buff, "a.m.") == 0) {
629 yylval.Meridian = MERam;
630 return tMERIDIAN;
632 if (strcmp(buff, "pm") == 0 || strcmp(buff, "p.m.") == 0) {
633 yylval.Meridian = MERpm;
634 return tMERIDIAN;
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';
644 else
645 abbrev = 0;
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;
654 else if (strcmp(buff, tp->name) == 0) {
655 yylval.Number = tp->value;
656 return tp->type;
660 for (tp = TimezoneTable; tp->name; tp++)
661 if (strcmp(buff, tp->name) == 0) {
662 yylval.Number = tp->value;
663 return tp->type;
666 for (tp = UnitsTable; tp->name; tp++)
667 if (strcmp(buff, tp->name) == 0) {
668 yylval.Number = tp->value;
669 return tp->type;
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;
683 for (tp = OtherTable; tp->name; tp++)
684 if (strcmp(buff, tp->name) == 0) {
685 yylval.Number = tp->value;
686 return tp->type;
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;
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;
712 return tID;
715 void
716 pdateerr(p)
717 char *p;
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;
726 value = getenv(name);
727 if (value == NULL) {
728 fprintf(stderr,
729 dgettext(domainname, "%s: Environment variable %s not set\n"),
730 p, name);
731 return;
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, '%') == NULL)
775 continue;
776 fprintf(stderr, " ");
777 (void) strftime(line, sizeof (line), fmt, tm);
778 fprintf(stderr, "%s", line);
780 (void) fclose(fp);
781 break;
782 case 8:
783 (void) fprintf(stderr, dgettext(domainname,
784 "%s: Invalid date specification\n"), p);
785 break;
789 #undef yylex
790 static int
791 yylex()
793 char c;
794 char *p;
795 char buff[20];
796 int Count;
797 int sign;
799 for ( ; ; ) {
800 while (isspace((u_char)*yyInput))
801 yyInput++;
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;
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;
819 yyInput--;
820 if (sign < 0)
821 yylval.Number = -yylval.Number;
822 return sign ? tSNUMBER : tUNUMBER;
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);
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);
848 time_t
849 getreldate(p, now)
850 char *p;
851 struct timeb *now;
853 struct tm *tm;
854 struct timeb ftz;
855 time_t Start;
856 time_t tod;
858 if (strcmp(setlocale(LC_TIME, NULL), "C")) {
859 static char localedate[24];
860 struct tm ltm;
862 tm = getdate(p);
863 if (getdate_err == 1 /* NODATEMASK */) {
864 char buffy[BUFSIZ];
865 time_t current;
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;
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);
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);
884 (tm->tm_mon)--;
885 } else if (tm == NULL)
886 return(-1);
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;
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;
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;
922 if (yyparse()
923 || yyHaveTime > 1 || yyHaveZone > 1 || yyHaveDate > 1 || yyHaveDay > 1)
924 return -1;
926 if (yyHaveDate || yyHaveTime || yyHaveDay) {
927 Start = Convert(yyMonth, yyDay, yyYear, yyHour, yyMinutes, yySeconds,
928 yyMeridian, yyDSTmode);
929 if (Start < 0)
930 return -1;
932 else {
933 Start = now->time;
934 if (!yyHaveRel)
935 Start -= ((tm->tm_hour * 60L) + tm->tm_min * 60L) + tm->tm_sec;
938 Start += yyRelSeconds;
939 Start += RelativeMonth(Start, yyRelMonth);
941 if (yyHaveDay && !yyHaveDate) {
942 tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber);
943 Start += tod;
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;
951 #if defined(TEST)
953 /* ARGSUSED */
954 main(ac, av)
955 int ac;
956 char *av[];
958 char buff[128];
959 time_t d;
961 (void) setlocale(LC_ALL, "");
962 #if !defined(TEXT_DOMAIN)
963 #define TEXT_DOMAIN "SYS_TEST"
964 #endif
965 (void) textdomain(TEXT_DOMAIN);
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, 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);
977 (void) printf("\t> ");
978 (void) fflush(stdout);
980 exit(0);
981 /* NOTREACHED */
983 #endif /* defined(TEST) */