Annotation of src/usr.bin/at/parsetime.c, Revision 1.21
1.21 ! millert 1: /* $OpenBSD: parsetime.c,v 1.20 2014/01/13 23:18:57 millert Exp $ */
1.1 deraadt 2:
1.14 millert 3: /*
1.1 deraadt 4: * parsetime.c - parse time for at(1)
1.4 millert 5: * Copyright (C) 1993, 1994 Thomas Koenig
1.1 deraadt 6: *
7: * modifications for english-language times
8: * Copyright (C) 1993 David Parsons
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. The name of the author(s) may not be used to endorse or promote
16: * products derived from this software without specific prior written
17: * permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
20: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1.4 millert 22: * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1.1 deraadt 23: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1.18 krw 26: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1.1 deraadt 27: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29: *
1.20 millert 30: * at [NOW] PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS|MONTHS|YEARS
1.1 deraadt 31: * /NUMBER [DOT NUMBER] [AM|PM]\ /[MONTH NUMBER [NUMBER]] \
32: * |NOON | |[TOMORROW] |
1.4 millert 33: * |MIDNIGHT | |[DAY OF WEEK] |
34: * \TEATIME / |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
1.20 millert 35: * \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS|MONTHS|YEARS/
1.1 deraadt 36: */
37:
38: #include <sys/types.h>
39: #include <errno.h>
1.11 millert 40: #include <ctype.h>
1.1 deraadt 41: #include <stdio.h>
42: #include <stdlib.h>
43: #include <string.h>
44: #include <time.h>
45: #include <unistd.h>
46:
1.13 millert 47: #include "globals.h"
1.1 deraadt 48: #include "at.h"
49:
50: /* Structures and unions */
51:
52: enum { /* symbols */
1.4 millert 53: MIDNIGHT, NOON, TEATIME,
54: PM, AM, TOMORROW, TODAY, NOW,
1.20 millert 55: MINUTES, HOURS, DAYS, WEEKS, MONTHS, YEARS,
56: NUMBER, NEXT, PLUS, DOT, SLASH, ID, JUNK,
1.4 millert 57: JAN, FEB, MAR, APR, MAY, JUN,
58: JUL, AUG, SEP, OCT, NOV, DEC,
59: SUN, MON, TUE, WED, THU, FRI, SAT
1.1 deraadt 60: };
61:
62: /*
63: * parse translation table - table driven parsers can be your FRIEND!
64: */
65: struct {
1.4 millert 66: char *name; /* token name */
67: int value; /* token id */
68: int plural; /* is this plural? */
1.1 deraadt 69: } Specials[] = {
1.4 millert 70: { "midnight", MIDNIGHT, 0 }, /* 00:00:00 of today or tomorrow */
71: { "noon", NOON, 0 }, /* 12:00:00 of today or tomorrow */
72: { "teatime", TEATIME, 0 }, /* 16:00:00 of today or tomorrow */
73: { "am", AM, 0 }, /* morning times for 0-12 clock */
74: { "pm", PM, 0 }, /* evening times for 0-12 clock */
75: { "tomorrow", TOMORROW, 0 }, /* execute 24 hours from time */
76: { "today", TODAY, 0 }, /* execute today - don't advance time */
77: { "now", NOW, 0 }, /* opt prefix for PLUS */
1.20 millert 78: { "next", NEXT, 0 }, /* opt prefix for + 1 */
1.4 millert 79:
80: { "minute", MINUTES, 0 }, /* minutes multiplier */
81: { "min", MINUTES, 0 },
82: { "m", MINUTES, 0 },
83: { "minutes", MINUTES, 1 }, /* (pluralized) */
84: { "hour", HOURS, 0 }, /* hours ... */
85: { "hr", HOURS, 0 }, /* abbreviated */
86: { "h", HOURS, 0 },
87: { "hours", HOURS, 1 }, /* (pluralized) */
88: { "day", DAYS, 0 }, /* days ... */
89: { "d", DAYS, 0 },
90: { "days", DAYS, 1 }, /* (pluralized) */
91: { "week", WEEKS, 0 }, /* week ... */
92: { "w", WEEKS, 0 },
93: { "weeks", WEEKS, 1 }, /* (pluralized) */
1.20 millert 94: { "month", MONTHS, 0 }, /* month ... */
95: { "mo", MONTHS, 0 },
96: { "mth", MONTHS, 0 },
97: { "months", MONTHS, 1 }, /* (pluralized) */
98: { "year", YEARS, 0 }, /* year ... */
99: { "y", YEARS, 0 },
100: { "years", YEARS, 1 }, /* (pluralized) */
1.4 millert 101: { "jan", JAN, 0 },
102: { "feb", FEB, 0 },
103: { "mar", MAR, 0 },
104: { "apr", APR, 0 },
105: { "may", MAY, 0 },
106: { "jun", JUN, 0 },
107: { "jul", JUL, 0 },
108: { "aug", AUG, 0 },
109: { "sep", SEP, 0 },
110: { "oct", OCT, 0 },
111: { "nov", NOV, 0 },
112: { "dec", DEC, 0 },
1.7 deraadt 113: { "january", JAN,0 },
114: { "february", FEB,0 },
115: { "march", MAR,0 },
116: { "april", APR,0 },
117: { "may", MAY,0 },
118: { "june", JUN,0 },
119: { "july", JUL,0 },
120: { "august", AUG,0 },
121: { "september", SEP,0 },
122: { "october", OCT,0 },
123: { "november", NOV,0 },
124: { "december", DEC,0 },
1.4 millert 125: { "sunday", SUN, 0 },
126: { "sun", SUN, 0 },
127: { "monday", MON, 0 },
128: { "mon", MON, 0 },
129: { "tuesday", TUE, 0 },
130: { "tue", TUE, 0 },
131: { "wednesday", WED, 0 },
132: { "wed", WED, 0 },
133: { "thursday", THU, 0 },
134: { "thu", THU, 0 },
135: { "friday", FRI, 0 },
136: { "fri", FRI, 0 },
137: { "saturday", SAT, 0 },
138: { "sat", SAT, 0 },
139: };
1.1 deraadt 140:
141: static char **scp; /* scanner - pointer at arglist */
1.15 cloder 142: static int scc; /* scanner - count of remaining arguments */
1.1 deraadt 143: static char *sct; /* scanner - next char pointer in current argument */
144: static int need; /* scanner - need to advance to next argument */
145: static char *sc_token; /* scanner - token buffer */
1.12 todd 146: static size_t sc_len; /* scanner - length of token buffer */
1.1 deraadt 147: static int sc_tokid; /* scanner - token id */
1.4 millert 148: static int sc_tokplur; /* scanner - is token plural? */
1.1 deraadt 149:
150: /*
151: * parse a token, checking if it's something special to us
152: */
153: static int
1.10 millert 154: parse_token(char *arg)
1.1 deraadt 155: {
1.4 millert 156: int i;
1.1 deraadt 157:
1.4 millert 158: for (i=0; i < sizeof(Specials) / sizeof(Specials[0]); i++) {
159: if (strcasecmp(Specials[i].name, arg) == 0) {
160: sc_tokplur = Specials[i].plural;
161: return (sc_tokid = Specials[i].value);
162: }
1.1 deraadt 163: }
164:
1.4 millert 165: /* not special - must be some random id */
166: return (ID);
1.11 millert 167: }
1.1 deraadt 168:
169:
170: /*
171: * init_scanner() sets up the scanner to eat arguments
172: */
1.13 millert 173: static int
1.10 millert 174: init_scanner(int argc, char **argv)
1.1 deraadt 175: {
1.4 millert 176: scp = argv;
177: scc = argc;
178: need = 1;
179: sc_len = 1;
180: while (argc-- > 0)
181: sc_len += strlen(*argv++);
182:
1.13 millert 183: if ((sc_token = (char *) malloc(sc_len)) == NULL) {
184: fprintf(stderr, "%s: Insufficient virtual memory\n",
185: ProgramName);
186: return (-1);
187: }
188: return (0);
1.11 millert 189: }
1.1 deraadt 190:
191: /*
192: * token() fetches a token from the input stream
193: */
194: static int
1.10 millert 195: token(void)
1.1 deraadt 196: {
1.4 millert 197: int idx;
1.1 deraadt 198:
1.10 millert 199: for (;;) {
1.13 millert 200: bzero(sc_token, sc_len);
1.4 millert 201: sc_tokid = EOF;
202: sc_tokplur = 0;
203: idx = 0;
1.1 deraadt 204:
1.4 millert 205: /*
206: * if we need to read another argument, walk along the
207: * argument list; when we fall off the arglist, we'll
208: * just return EOF forever
209: */
210: if (need) {
211: if (scc < 1)
212: return (sc_tokid);
213: sct = *scp;
214: scp++;
215: scc--;
216: need = 0;
217: }
218: /*
219: * eat whitespace now - if we walk off the end of the argument,
220: * we'll continue, which puts us up at the top of the while loop
221: * to fetch the next argument in
222: */
1.19 deraadt 223: while (isspace((unsigned char)*sct))
1.4 millert 224: ++sct;
225: if (!*sct) {
226: need = 1;
227: continue;
228: }
1.1 deraadt 229:
1.4 millert 230: /*
231: * preserve the first character of the new token
232: */
233: sc_token[0] = *sct++;
1.1 deraadt 234:
1.4 millert 235: /*
236: * then see what it is
237: */
1.19 deraadt 238: if (isdigit((unsigned char)sc_token[0])) {
239: while (isdigit((unsigned char)*sct))
1.4 millert 240: sc_token[++idx] = *sct++;
241: sc_token[++idx] = 0;
242: return ((sc_tokid = NUMBER));
1.19 deraadt 243: } else if (isalpha((unsigned char)sc_token[0])) {
244: while (isalpha((unsigned char)*sct))
1.4 millert 245: sc_token[++idx] = *sct++;
246: sc_token[++idx] = 0;
247: return (parse_token(sc_token));
248: }
249: else if (sc_token[0] == ':' || sc_token[0] == '.')
250: return ((sc_tokid = DOT));
251: else if (sc_token[0] == '+')
252: return ((sc_tokid = PLUS));
253: else if (sc_token[0] == '/')
254: return ((sc_tokid = SLASH));
255: else
256: return ((sc_tokid = JUNK));
1.11 millert 257: }
258: }
1.1 deraadt 259:
260:
261: /*
262: * plonk() gives an appropriate error message if a token is incorrect
263: */
264: static void
1.10 millert 265: plonk(int tok)
1.1 deraadt 266: {
1.13 millert 267: fprintf(stderr, "%s: %s time\n", ProgramName,
268: (tok == EOF) ? "incomplete" : "garbled");
1.11 millert 269: }
1.1 deraadt 270:
271:
1.14 millert 272: /*
1.13 millert 273: * expect() gets a token and returns -1 if it's not the token we want
1.1 deraadt 274: */
1.13 millert 275: static int
1.10 millert 276: expect(int desired)
1.1 deraadt 277: {
1.13 millert 278: if (token() != desired) {
279: plonk(sc_tokid);
280: return (-1);
281: }
282: return (0);
1.11 millert 283: }
1.1 deraadt 284:
285:
286: /*
287: * dateadd() adds a number of minutes to a date. It is extraordinarily
288: * stupid regarding day-of-month overflow, and will most likely not
289: * work properly
290: */
291: static void
1.10 millert 292: dateadd(int minutes, struct tm *tm)
1.1 deraadt 293: {
1.4 millert 294: /* increment days */
1.1 deraadt 295:
1.4 millert 296: while (minutes > 24*60) {
297: minutes -= 24*60;
298: tm->tm_mday++;
1.1 deraadt 299: }
300:
1.4 millert 301: /* increment hours */
302: while (minutes > 60) {
303: minutes -= 60;
304: tm->tm_hour++;
305: if (tm->tm_hour > 23) {
306: tm->tm_mday++;
307: tm->tm_hour = 0;
308: }
309: }
310:
311: /* increment minutes */
312: tm->tm_min += minutes;
1.1 deraadt 313:
1.4 millert 314: if (tm->tm_min > 59) {
315: tm->tm_hour++;
316: tm->tm_min -= 60;
317:
318: if (tm->tm_hour > 23) {
319: tm->tm_mday++;
320: tm->tm_hour = 0;
321: }
1.1 deraadt 322: }
1.11 millert 323: }
1.1 deraadt 324:
325:
326: /*
327: * plus() parses a now + time
328: *
1.20 millert 329: * at [NOW] PLUS NUMBER [MINUTES|HOURS|DAYS|WEEKS|MONTHS|YEARS]
1.1 deraadt 330: *
331: */
1.13 millert 332: static int
1.10 millert 333: plus(struct tm *tm)
1.1 deraadt 334: {
1.20 millert 335: int increment;
1.4 millert 336: int expectplur;
337:
1.20 millert 338: if (sc_tokid == NEXT) {
339: increment = 1;
340: expectplur = 0;
341: } else {
342: if (expect(NUMBER) != 0)
343: return (-1);
344: increment = atoi(sc_token);
345: expectplur = (increment != 1) ? 1 : 0;
346: }
1.1 deraadt 347:
1.4 millert 348: switch (token()) {
1.20 millert 349: case YEARS:
350: tm->tm_year += increment;
351: return (0);
352: case MONTHS:
353: tm->tm_mon += increment;
354: while (tm->tm_mon >= 12) {
355: tm->tm_year++;
356: tm->tm_mon -= 12;
357: }
358: return (0);
1.4 millert 359: case WEEKS:
1.20 millert 360: increment *= 7;
1.15 cloder 361: /* FALLTHROUGH */
1.4 millert 362: case DAYS:
1.20 millert 363: increment *= 24;
1.15 cloder 364: /* FALLTHROUGH */
1.4 millert 365: case HOURS:
1.20 millert 366: increment *= 60;
1.15 cloder 367: /* FALLTHROUGH */
1.4 millert 368: case MINUTES:
369: if (expectplur != sc_tokplur)
1.13 millert 370: fprintf(stderr, "%s: pluralization is wrong\n",
371: ProgramName);
1.20 millert 372: dateadd(increment, tm);
1.13 millert 373: return (0);
1.4 millert 374: }
1.1 deraadt 375:
1.4 millert 376: plonk(sc_tokid);
1.13 millert 377: return (-1);
1.11 millert 378: }
1.1 deraadt 379:
380:
381: /*
382: * tod() computes the time of day
383: * [NUMBER [DOT NUMBER] [AM|PM]]
384: */
1.13 millert 385: static int
1.10 millert 386: tod(struct tm *tm)
1.1 deraadt 387: {
1.4 millert 388: int hour, minute = 0;
389: size_t tlen;
390:
391: hour = atoi(sc_token);
392: tlen = strlen(sc_token);
393:
394: /*
395: * first pick out the time of day - if it's 4 digits, we assume
396: * a HHMM time, otherwise it's HH DOT MM time
397: */
398: if (token() == DOT) {
1.13 millert 399: if (expect(NUMBER) != 0)
400: return (-1);
1.4 millert 401: minute = atoi(sc_token);
402: if (minute > 59)
1.13 millert 403: goto bad;
1.4 millert 404: token();
405: } else if (tlen == 4) {
406: minute = hour % 100;
407: if (minute > 59)
1.13 millert 408: goto bad;
1.4 millert 409: hour = hour / 100;
410: }
1.1 deraadt 411:
1.4 millert 412: /*
413: * check if an AM or PM specifier was given
414: */
415: if (sc_tokid == AM || sc_tokid == PM) {
416: if (hour > 12)
1.13 millert 417: goto bad;
1.4 millert 418:
419: if (sc_tokid == PM) {
420: if (hour != 12) /* 12:xx PM is 12:xx, not 24:xx */
421: hour += 12;
422: } else {
423: if (hour == 12) /* 12:xx AM is 00:xx, not 12:xx */
424: hour = 0;
425: }
426: token();
427: } else if (hour > 23)
1.13 millert 428: goto bad;
1.1 deraadt 429:
1.4 millert 430: /*
431: * if we specify an absolute time, we don't want to bump the day even
432: * if we've gone past that time - but if we're specifying a time plus
433: * a relative offset, it's okay to bump things
434: */
1.20 millert 435: if ((sc_tokid == EOF || sc_tokid == PLUS || sc_tokid == NEXT) &&
436: tm->tm_hour > hour) {
1.4 millert 437: tm->tm_mday++;
438: tm->tm_wday++;
439: }
1.1 deraadt 440:
1.4 millert 441: tm->tm_hour = hour;
442: tm->tm_min = minute;
443: if (tm->tm_hour == 24) {
444: tm->tm_hour = 0;
445: tm->tm_mday++;
446: }
1.13 millert 447: return (0);
448: bad:
449: fprintf(stderr, "%s: garbled time\n", ProgramName);
450: return (-1);
1.11 millert 451: }
1.1 deraadt 452:
453:
454: /*
455: * assign_date() assigns a date, wrapping to next year if needed
456: */
457: static void
1.10 millert 458: assign_date(struct tm *tm, int mday, int mon, int year)
1.1 deraadt 459: {
1.9 millert 460:
461: /*
462: * Convert year into tm_year format (year - 1900).
463: * We may be given the year in 2 digit, 4 digit, or tm_year format.
464: */
465: if (year != -1) {
466: if (year >= TM_YEAR_BASE)
467: year -= TM_YEAR_BASE; /* convert from 4 digit year */
468: else if (year < 100) {
469: /* Convert to tm_year assuming current century */
470: year += (tm->tm_year / 100) * 100;
471:
472: if (year == tm->tm_year - 1)
473: year++; /* Common off by one error */
474: else if (year < tm->tm_year)
475: year += 100; /* must be in next century */
476: }
1.4 millert 477: }
478:
479: if (year < 0 &&
480: (tm->tm_mon > mon ||(tm->tm_mon == mon && tm->tm_mday > mday)))
481: year = tm->tm_year + 1;
1.1 deraadt 482:
1.4 millert 483: tm->tm_mday = mday;
484: tm->tm_mon = mon;
1.1 deraadt 485:
1.4 millert 486: if (year >= 0)
487: tm->tm_year = year;
1.11 millert 488: }
1.1 deraadt 489:
490:
1.14 millert 491: /*
1.1 deraadt 492: * month() picks apart a month specification
493: *
494: * /[<month> NUMBER [NUMBER]] \
495: * |[TOMORROW] |
1.4 millert 496: * |[DAY OF WEEK] |
1.1 deraadt 497: * |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
1.20 millert 498: * \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS|MONTHS|YEARS/
1.1 deraadt 499: */
1.13 millert 500: static int
1.10 millert 501: month(struct tm *tm)
1.1 deraadt 502: {
1.4 millert 503: int year = (-1);
504: int mday, wday, mon;
505: size_t tlen;
506:
507: switch (sc_tokid) {
1.20 millert 508: case NEXT:
1.4 millert 509: case PLUS:
1.13 millert 510: if (plus(tm) != 0)
511: return (-1);
1.4 millert 512: break;
513:
514: case TOMORROW:
515: /* do something tomorrow */
516: tm->tm_mday++;
517: tm->tm_wday++;
518: case TODAY:
519: /* force ourselves to stay in today - no further processing */
1.1 deraadt 520: token();
1.4 millert 521: break;
1.1 deraadt 522:
1.4 millert 523: case JAN: case FEB: case MAR: case APR: case MAY: case JUN:
524: case JUL: case AUG: case SEP: case OCT: case NOV: case DEC:
525: /*
526: * do month mday [year]
527: */
528: mon = sc_tokid - JAN;
1.13 millert 529: if (expect(NUMBER) != 0)
530: return (-1);
1.4 millert 531: mday = atoi(sc_token);
532: if (token() == NUMBER) {
533: year = atoi(sc_token);
534: token();
1.1 deraadt 535: }
1.4 millert 536: assign_date(tm, mday, mon, year);
537: break;
538:
539: case SUN: case MON: case TUE:
540: case WED: case THU: case FRI:
541: case SAT:
542: /* do a particular day of the week */
543: wday = sc_tokid - SUN;
544:
545: mday = tm->tm_mday;
546:
547: /* if this day is < today, then roll to next week */
548: if (wday < tm->tm_wday)
549: mday += 7 - (tm->tm_wday - wday);
550: else
551: mday += (wday - tm->tm_wday);
552:
553: tm->tm_wday = wday;
554:
555: assign_date(tm, mday, tm->tm_mon, tm->tm_year);
556: break;
1.1 deraadt 557:
1.4 millert 558: case NUMBER:
1.1 deraadt 559: /*
1.4 millert 560: * get numeric MMDDYY, mm/dd/yy, or dd.mm.yy
1.1 deraadt 561: */
1.4 millert 562: tlen = strlen(sc_token);
563: mon = atoi(sc_token);
564: token();
1.1 deraadt 565:
1.4 millert 566: if (sc_tokid == SLASH || sc_tokid == DOT) {
567: int sep;
1.1 deraadt 568:
1.4 millert 569: sep = sc_tokid;
1.13 millert 570: if (expect(NUMBER) != 0)
571: return (-1);
1.4 millert 572: mday = atoi(sc_token);
573: if (token() == sep) {
1.13 millert 574: if (expect(NUMBER) != 0)
575: return (-1);
1.4 millert 576: year = atoi(sc_token);
577: token();
578: }
579:
580: /*
581: * flip months and days for european timing
582: */
583: if (sep == DOT) {
584: int x = mday;
585: mday = mon;
586: mon = x;
587: }
588: } else if (tlen == 6 || tlen == 8) {
589: if (tlen == 8) {
1.8 alex 590: year = (mon % 10000) - TM_YEAR_BASE;
1.4 millert 591: mon /= 10000;
592: } else {
593: year = mon % 100;
594: mon /= 100;
595: }
596: mday = mon % 100;
597: mon /= 100;
598: } else
1.13 millert 599: goto bad;
1.4 millert 600:
601: mon--;
602: if (mon < 0 || mon > 11 || mday < 1 || mday > 31)
1.13 millert 603: goto bad;
1.4 millert 604:
605: assign_date(tm, mday, mon, year);
606: break;
1.13 millert 607: }
608: return (0);
609: bad:
610: fprintf(stderr, "%s: garbled time\n", ProgramName);
611: return (-1);
1.11 millert 612: }
1.1 deraadt 613:
614:
615: time_t
1.10 millert 616: parsetime(int argc, char **argv)
1.1 deraadt 617: {
1.4 millert 618: /*
619: * Do the argument parsing, die if necessary, and return the
620: * time the job should be run.
621: */
622: time_t nowtimer, runtimer;
623: struct tm nowtime, runtime;
624: int hr = 0;
625: /* this MUST be initialized to zero for midnight/noon/teatime */
626:
1.13 millert 627: if (argc == 0)
628: return (-1);
629:
1.4 millert 630: nowtimer = time(NULL);
631: nowtime = *localtime(&nowtimer);
632:
633: runtime = nowtime;
634: runtime.tm_sec = 0;
635: runtime.tm_isdst = 0;
636:
1.13 millert 637: if (init_scanner(argc, argv) == -1)
638: return (-1);
1.4 millert 639:
640: switch (token()) {
641: case NOW: /* now is optional prefix for PLUS tree */
1.5 millert 642: token();
643: if (sc_tokid == EOF) {
644: runtime = nowtime;
645: break;
646: }
1.20 millert 647: else if (sc_tokid != PLUS && sc_tokid != NEXT)
1.5 millert 648: plonk(sc_tokid);
1.20 millert 649: case NEXT:
1.4 millert 650: case PLUS:
1.13 millert 651: if (plus(&runtime) != 0)
652: return (-1);
1.4 millert 653: break;
654:
655: case NUMBER:
1.13 millert 656: if (tod(&runtime) != 0 || month(&runtime) != 0)
657: return (-1);
1.4 millert 658: break;
659:
660: /*
661: * evil coding for TEATIME|NOON|MIDNIGHT - we've initialised
662: * hr to zero up above, then fall into this case in such a
663: * way so we add +12 +4 hours to it for teatime, +12 hours
664: * to it for noon, and nothing at all for midnight, then
665: * set our runtime to that hour before leaping into the
666: * month scanner
667: */
668: case TEATIME:
669: hr += 4;
1.15 cloder 670: /* FALLTHROUGH */
1.4 millert 671: case NOON:
672: hr += 12;
1.15 cloder 673: /* FALLTHROUGH */
1.4 millert 674: case MIDNIGHT:
675: if (runtime.tm_hour >= hr) {
676: runtime.tm_mday++;
677: runtime.tm_wday++;
678: }
679: runtime.tm_hour = hr;
680: runtime.tm_min = 0;
681: token();
682: /* fall through to month setting */
1.15 cloder 683: /* FALLTHROUGH */
1.4 millert 684: default:
1.13 millert 685: if (month(&runtime) != 0)
686: return (-1);
1.4 millert 687: break;
688: } /* ugly case statement */
1.13 millert 689: if (expect(EOF) != 0)
690: return (-1);
1.4 millert 691:
692: /*
693: * adjust for daylight savings time
694: */
695: runtime.tm_isdst = -1;
1.1 deraadt 696: runtimer = mktime(&runtime);
1.4 millert 697: if (runtime.tm_isdst > 0) {
698: runtimer -= 3600;
699: runtimer = mktime(&runtime);
700: }
1.1 deraadt 701:
1.13 millert 702: if (runtimer < 0) {
703: fprintf(stderr, "%s: garbled time\n", ProgramName);
704: return (-1);
705: }
1.1 deraadt 706:
1.13 millert 707: if (nowtimer > runtimer) {
1.16 millert 708: fprintf(stderr, "%s: cannot schedule jobs in the past\n",
1.13 millert 709: ProgramName);
710: return (-1);
711: }
1.1 deraadt 712:
1.4 millert 713: return (runtimer);
1.11 millert 714: }