2 * parsetime.c - parse time for at(1)
3 * Copyright (C) 1993, 1994 Thomas Koenig
5 * modifications for English-language times
6 * Copyright (C) 1993 David Parsons
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. The name of the author(s) may not be used to endorse or promote
14 * products derived from this software without specific prior written
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * at [NOW] PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS
29 * /NUMBER [DOT NUMBER] [AM|PM]\ /[MONTH NUMBER [NUMBER]] \
30 * |NOON | |[TOMORROW] |
31 * |MIDNIGHT | |[DAY OF WEEK] |
32 * \TEATIME / |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
33 * \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
41 #include <sys/types.h>
58 #include "parsetime.h"
61 /* Structures and unions */
64 MIDNIGHT, NOON, TEATIME,
65 PM, AM, TOMORROW, TODAY, NOW,
66 MINUTES, HOURS, DAYS, WEEKS, MONTHS, YEARS,
67 NUMBER, PLUS, DOT, SLASH, ID, JUNK,
68 JAN, FEB, MAR, APR, MAY, JUN,
69 JUL, AUG, SEP, OCT, NOV, DEC,
70 SUN, MON, TUE, WED, THU, FRI, SAT
73 /* parse translation table - table driven parsers can be your FRIEND!
76 const char *name; /* token name */
77 int value; /* token id */
78 int plural; /* is this plural? */
80 { "midnight", MIDNIGHT,0 }, /* 00:00:00 of today or tomorrow */
81 { "noon", NOON,0 }, /* 12:00:00 of today or tomorrow */
82 { "teatime", TEATIME,0 }, /* 16:00:00 of today or tomorrow */
83 { "am", AM,0 }, /* morning times for 0-12 clock */
84 { "pm", PM,0 }, /* evening times for 0-12 clock */
85 { "tomorrow", TOMORROW,0 }, /* execute 24 hours from time */
86 { "today", TODAY, 0 }, /* execute today - don't advance time */
87 { "now", NOW,0 }, /* opt prefix for PLUS */
89 { "minute", MINUTES,0 }, /* minutes multiplier */
90 { "minutes", MINUTES,1 }, /* (pluralized) */
91 { "hour", HOURS,0 }, /* hours ... */
92 { "hours", HOURS,1 }, /* (pluralized) */
93 { "day", DAYS,0 }, /* days ... */
94 { "days", DAYS,1 }, /* (pluralized) */
95 { "week", WEEKS,0 }, /* week ... */
96 { "weeks", WEEKS,1 }, /* (pluralized) */
97 { "month", MONTHS,0 }, /* month ... */
98 { "months", MONTHS,1 }, /* (pluralized) */
99 { "year", YEARS,0 }, /* year ... */
100 { "years", YEARS,1 }, /* (pluralized) */
113 { "january", JAN,0 },
114 { "february", FEB,0 },
121 { "september", SEP,0 },
122 { "october", OCT,0 },
123 { "november", NOV,0 },
124 { "december", DEC,0 },
125 { "sunday", SUN, 0 },
127 { "monday", MON, 0 },
129 { "tuesday", TUE, 0 },
131 { "wednesday", WED, 0 },
133 { "thursday", THU, 0 },
135 { "friday", FRI, 0 },
137 { "saturday", SAT, 0 },
141 /* File scope variables */
143 static char **scp; /* scanner - pointer at arglist */
144 static char scc; /* scanner - count of remaining arguments */
145 static char *sct; /* scanner - next char pointer in current argument */
146 static int need; /* scanner - need to advance to next argument */
148 static char *sc_token; /* scanner - token buffer */
149 static size_t sc_len; /* scanner - length of token buffer */
150 static int sc_tokid; /* scanner - token id */
151 static int sc_tokplur; /* scanner - is token plural? */
153 /* Local functions */
156 * parse a token, checking if it's something special to us
159 parse_token(char *arg)
163 for (i=0; i<(sizeof Specials/sizeof Specials[0]); i++)
164 if (strcasecmp(Specials[i].name, arg) == 0) {
165 sc_tokplur = Specials[i].plural;
166 return sc_tokid = Specials[i].value;
169 /* not special - must be some random id */
175 * init_scanner() sets up the scanner to eat arguments
178 init_scanner(int argc, char **argv)
185 sc_len += strlen(*argv++);
187 if ((sc_token = malloc(sc_len)) == NULL)
188 errx(EXIT_FAILURE, "virtual memory exhausted");
192 * token() fetches a token from the input stream
200 memset(sc_token, 0, sc_len);
205 /* if we need to read another argument, walk along the argument list;
206 * when we fall off the arglist, we'll just return EOF forever
216 /* eat whitespace now - if we walk off the end of the argument,
217 * we'll continue, which puts us up at the top of the while loop
218 * to fetch the next argument in
220 while (isspace(*sct))
227 /* preserve the first character of the new token
229 sc_token[0] = *sct++;
231 /* then see what it is
233 if (isdigit(sc_token[0])) {
234 while (isdigit(*sct))
235 sc_token[++idx] = *sct++;
237 return sc_tokid = NUMBER;
239 else if (isalpha(sc_token[0])) {
240 while (isalpha(*sct))
241 sc_token[++idx] = *sct++;
243 return parse_token(sc_token);
245 else if (sc_token[0] == ':' || sc_token[0] == '.')
246 return sc_tokid = DOT;
247 else if (sc_token[0] == '+')
248 return sc_tokid = PLUS;
249 else if (sc_token[0] == '/')
250 return sc_tokid = SLASH;
252 return sc_tokid = JUNK;
258 * plonk() gives an appropriate error message if a token is incorrect
263 panic((tok == EOF) ? "incomplete time"
269 * expect() gets a token and dies most horribly if it's not the token we want
274 if (token() != desired)
275 plonk(sc_tokid); /* and we die here... */
280 * plus() parses a now + time
282 * at [NOW] PLUS NUMBER [MINUTES|HOURS|DAYS|WEEKS|MONTHS|YEARS]
294 delay = atoi(sc_token);
295 expectplur = (delay != 1) ? 1 : 0;
299 tm->tm_year += delay;
307 tm->tm_mday += delay;
310 tm->tm_hour += delay;
320 if (expectplur != sc_tokplur)
321 warnx("pluralization is wrong");
331 * tod() computes the time of day
332 * [NUMBER [DOT NUMBER] [AM|PM]]
337 int hour, minute = 0;
340 hour = atoi(sc_token);
341 tlen = strlen(sc_token);
343 /* first pick out the time of day - if it's 4 digits, we assume
344 * a HHMM time, otherwise it's HH DOT MM time
346 if (token() == DOT) {
348 minute = atoi(sc_token);
350 panic("garbled time");
353 else if (tlen == 4) {
356 panic("garbled time");
360 /* check if an AM or PM specifier was given
362 if (sc_tokid == AM || sc_tokid == PM) {
364 panic("garbled time");
366 if (sc_tokid == PM) {
367 if (hour != 12) /* 12:xx PM is 12:xx, not 24:xx */
370 if (hour == 12) /* 12:xx AM is 00:xx, not 12:xx */
376 panic("garbled time");
378 /* if we specify an absolute time, we don't want to bump the day even
379 * if we've gone past that time - but if we're specifying a time plus
380 * a relative offset, it's okay to bump things
382 if ((sc_tokid == EOF || sc_tokid == PLUS) && tm->tm_hour > hour) {
389 if (tm->tm_hour == 24) {
397 * assign_date() assigns a date, wrapping to next year if needed
400 assign_date(struct tm *tm, long mday, long mon, long year)
404 * Convert year into tm_year format (year - 1900).
405 * We may be given the year in 2 digit, 4 digit, or tm_year format.
409 year -= 1900; /* convert from 4 digit year */
410 else if (year < 100) {
411 /* convert from 2 digit year */
416 lt = localtime(&now);
418 /* Convert to tm_year assuming current century */
419 year += (lt->tm_year / 100) * 100;
421 if (year == lt->tm_year - 1) year++;
422 else if (year < lt->tm_year)
423 year += 100; /* must be in next century */
428 (tm->tm_mon > mon ||(tm->tm_mon == mon && tm->tm_mday > mday)))
429 year = tm->tm_year + 1;
440 * month() picks apart a month specification
442 * /[<month> NUMBER [NUMBER]] \
445 * |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
446 * \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/
452 long mday = 0, wday, mon;
461 /* do something tomorrow */
464 case TODAY: /* force ourselves to stay in today - no further processing */
468 case JAN: case FEB: case MAR: case APR: case MAY: case JUN:
469 case JUL: case AUG: case SEP: case OCT: case NOV: case DEC:
470 /* do month mday [year]
472 mon = (sc_tokid-JAN);
474 mday = atol(sc_token);
475 if (token() == NUMBER) {
476 year = atol(sc_token);
479 assign_date(tm, mday, mon, year);
482 case SUN: case MON: case TUE:
483 case WED: case THU: case FRI:
485 /* do a particular day of the week
487 wday = (sc_tokid-SUN);
491 /* if this day is < today, then roll to next week
493 if (wday < tm->tm_wday)
494 mday += 7 - (tm->tm_wday - wday);
496 mday += (wday - tm->tm_wday);
500 assign_date(tm, mday, tm->tm_mon, tm->tm_year);
504 /* get numeric MMDDYY, mm/dd/yy, or dd.mm.yy
506 tlen = strlen(sc_token);
507 mon = atol(sc_token);
510 if (sc_tokid == SLASH || sc_tokid == DOT) {
515 mday = atol(sc_token);
516 if (token() == sep) {
518 year = atol(sc_token);
522 /* flip months and days for European timing
530 else if (tlen == 6 || tlen == 8) {
532 year = (mon % 10000) - 1900;
543 panic("garbled time");
546 if (mon < 0 || mon > 11 || mday < 1 || mday > 31)
547 panic("garbled time");
549 assign_date(tm, mday, mon, year);
555 /* Global functions */
558 parsetime(int argc, char **argv)
560 /* Do the argument parsing, die if necessary, and return the time the job
563 time_t nowtimer, runtimer;
564 struct tm nowtime, runtime;
566 /* this MUST be initialized to zero for midnight/noon/teatime */
568 nowtimer = time(NULL);
569 nowtime = *localtime(&nowtimer);
573 runtime.tm_isdst = 0;
578 init_scanner(argc-optind, argv+optind);
585 /* now is optional prefix for PLUS tree */
596 /* evil coding for TEATIME|NOON|MIDNIGHT - we've initialised
597 * hr to zero up above, then fall into this case in such a
598 * way so we add +12 +4 hours to it for teatime, +12 hours
599 * to it for noon, and nothing at all for midnight, then
600 * set our runtime to that hour before leaping into the
608 if (runtime.tm_hour >= hr) {
612 runtime.tm_hour = hr;
615 /* FALLTHROUGH to month setting */
619 } /* ugly case statement */
622 /* convert back to time_t
624 runtime.tm_isdst = -1;
625 runtimer = mktime(&runtime);
628 panic("garbled time");
630 if (nowtimer > runtimer)
631 panic("trying to travel back in time");