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/
37 static const char rcsid[] =
38 "$FreeBSD: src/usr.bin/at/parsetime.c,v 1.19.2.3 2001/12/19 11:19:16 brian Exp $";
43 #include <sys/types.h>
62 /* Structures and unions */
65 MIDNIGHT, NOON, TEATIME,
66 PM, AM, TOMORROW, TODAY, NOW,
67 MINUTES, HOURS, DAYS, WEEKS, MONTHS, YEARS,
68 NUMBER, PLUS, DOT, SLASH, ID, JUNK,
69 JAN, FEB, MAR, APR, MAY, JUN,
70 JUL, AUG, SEP, OCT, NOV, DEC,
71 SUN, MON, TUE, WED, THU, FRI, SAT
74 /* parse translation table - table driven parsers can be your FRIEND!
77 char *name; /* token name */
78 int value; /* token id */
79 int plural; /* is this plural? */
81 { "midnight", MIDNIGHT,0 }, /* 00:00:00 of today or tomorrow */
82 { "noon", NOON,0 }, /* 12:00:00 of today or tomorrow */
83 { "teatime", TEATIME,0 }, /* 16:00:00 of today or tomorrow */
84 { "am", AM,0 }, /* morning times for 0-12 clock */
85 { "pm", PM,0 }, /* evening times for 0-12 clock */
86 { "tomorrow", TOMORROW,0 }, /* execute 24 hours from time */
87 { "today", TODAY, 0 }, /* execute today - don't advance time */
88 { "now", NOW,0 }, /* opt prefix for PLUS */
90 { "minute", MINUTES,0 }, /* minutes multiplier */
91 { "minutes", MINUTES,1 }, /* (pluralized) */
92 { "hour", HOURS,0 }, /* hours ... */
93 { "hours", HOURS,1 }, /* (pluralized) */
94 { "day", DAYS,0 }, /* days ... */
95 { "days", DAYS,1 }, /* (pluralized) */
96 { "week", WEEKS,0 }, /* week ... */
97 { "weeks", WEEKS,1 }, /* (pluralized) */
98 { "month", MONTHS,0 }, /* month ... */
99 { "months", MONTHS,1 }, /* (pluralized) */
100 { "year", YEARS,0 }, /* year ... */
101 { "years", YEARS,1 }, /* (pluralized) */
114 { "january", JAN,0 },
115 { "february", FEB,0 },
122 { "september", SEP,0 },
123 { "october", OCT,0 },
124 { "november", NOV,0 },
125 { "december", DEC,0 },
126 { "sunday", SUN, 0 },
128 { "monday", MON, 0 },
130 { "tuesday", TUE, 0 },
132 { "wednesday", WED, 0 },
134 { "thursday", THU, 0 },
136 { "friday", FRI, 0 },
138 { "saturday", SAT, 0 },
142 /* File scope variables */
144 static char **scp; /* scanner - pointer at arglist */
145 static char scc; /* scanner - count of remaining arguments */
146 static char *sct; /* scanner - next char pointer in current argument */
147 static int need; /* scanner - need to advance to next argument */
149 static char *sc_token; /* scanner - token buffer */
150 static size_t sc_len; /* scanner - length of token buffer */
151 static int sc_tokid; /* scanner - token id */
152 static int sc_tokplur; /* scanner - is token plural? */
154 /* Local functions */
157 * parse a token, checking if it's something special to us
160 parse_token(char *arg)
164 for (i=0; i<(sizeof Specials/sizeof Specials[0]); i++)
165 if (strcasecmp(Specials[i].name, arg) == 0) {
166 sc_tokplur = Specials[i].plural;
167 return sc_tokid = Specials[i].value;
170 /* not special - must be some random id */
176 * init_scanner() sets up the scanner to eat arguments
179 init_scanner(int argc, char **argv)
186 sc_len += strlen(*argv++);
188 if ((sc_token = malloc(sc_len)) == NULL)
189 errx(EXIT_FAILURE, "virtual memory exhausted");
193 * token() fetches a token from the input stream
201 memset(sc_token, 0, sc_len);
206 /* if we need to read another argument, walk along the argument list;
207 * when we fall off the arglist, we'll just return EOF forever
217 /* eat whitespace now - if we walk off the end of the argument,
218 * we'll continue, which puts us up at the top of the while loop
219 * to fetch the next argument in
221 while (isspace(*sct))
228 /* preserve the first character of the new token
230 sc_token[0] = *sct++;
232 /* then see what it is
234 if (isdigit(sc_token[0])) {
235 while (isdigit(*sct))
236 sc_token[++idx] = *sct++;
238 return sc_tokid = NUMBER;
240 else if (isalpha(sc_token[0])) {
241 while (isalpha(*sct))
242 sc_token[++idx] = *sct++;
244 return parse_token(sc_token);
246 else if (sc_token[0] == ':' || sc_token[0] == '.')
247 return sc_tokid = DOT;
248 else if (sc_token[0] == '+')
249 return sc_tokid = PLUS;
250 else if (sc_token[0] == '/')
251 return sc_tokid = SLASH;
253 return sc_tokid = JUNK;
259 * plonk() gives an appropriate error message if a token is incorrect
264 panic((tok == EOF) ? "incomplete time"
270 * expect() gets a token and dies most horribly if it's not the token we want
275 if (token() != desired)
276 plonk(sc_tokid); /* and we die here... */
281 * plus() parses a now + time
283 * at [NOW] PLUS NUMBER [MINUTES|HOURS|DAYS|WEEKS|MONTHS|YEARS]
295 delay = atoi(sc_token);
296 expectplur = (delay != 1) ? 1 : 0;
300 tm->tm_year += delay;
308 tm->tm_mday += delay;
311 tm->tm_hour += delay;
321 if (expectplur != sc_tokplur)
322 warnx("pluralization is wrong");
332 * tod() computes the time of day
333 * [NUMBER [DOT NUMBER] [AM|PM]]
338 int hour, minute = 0;
341 hour = atoi(sc_token);
342 tlen = strlen(sc_token);
344 /* first pick out the time of day - if it's 4 digits, we assume
345 * a HHMM time, otherwise it's HH DOT MM time
347 if (token() == DOT) {
349 minute = atoi(sc_token);
351 panic("garbled time");
354 else if (tlen == 4) {
357 panic("garbled time");
361 /* check if an AM or PM specifier was given
363 if (sc_tokid == AM || sc_tokid == PM) {
365 panic("garbled time");
367 if (sc_tokid == PM) {
368 if (hour != 12) /* 12:xx PM is 12:xx, not 24:xx */
371 if (hour == 12) /* 12:xx AM is 00:xx, not 12:xx */
377 panic("garbled time");
379 /* if we specify an absolute time, we don't want to bump the day even
380 * if we've gone past that time - but if we're specifying a time plus
381 * a relative offset, it's okay to bump things
383 if ((sc_tokid == EOF || sc_tokid == PLUS) && tm->tm_hour > hour) {
390 if (tm->tm_hour == 24) {
398 * assign_date() assigns a date, wrapping to next year if needed
401 assign_date(struct tm *tm, long mday, long mon, long year)
405 * Convert year into tm_year format (year - 1900).
406 * We may be given the year in 2 digit, 4 digit, or tm_year format.
410 year -= 1900; /* convert from 4 digit year */
411 else if (year < 100) {
412 /* convert from 2 digit year */
417 lt = localtime(&now);
419 /* Convert to tm_year assuming current century */
420 year += (lt->tm_year / 100) * 100;
422 if (year == lt->tm_year - 1) year++;
423 else if (year < lt->tm_year)
424 year += 100; /* must be in next century */
429 (tm->tm_mon > mon ||(tm->tm_mon == mon && tm->tm_mday > mday)))
430 year = tm->tm_year + 1;
441 * month() picks apart a month specification
443 * /[<month> NUMBER [NUMBER]] \
446 * |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
447 * \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/
453 long mday = 0, wday, mon;
462 /* do something tomorrow */
465 case TODAY: /* force ourselves to stay in today - no further processing */
469 case JAN: case FEB: case MAR: case APR: case MAY: case JUN:
470 case JUL: case AUG: case SEP: case OCT: case NOV: case DEC:
471 /* do month mday [year]
473 mon = (sc_tokid-JAN);
475 mday = atol(sc_token);
476 if (token() == NUMBER) {
477 year = atol(sc_token);
480 assign_date(tm, mday, mon, year);
483 case SUN: case MON: case TUE:
484 case WED: case THU: case FRI:
486 /* do a particular day of the week
488 wday = (sc_tokid-SUN);
492 /* if this day is < today, then roll to next week
494 if (wday < tm->tm_wday)
495 mday += 7 - (tm->tm_wday - wday);
497 mday += (wday - tm->tm_wday);
501 assign_date(tm, mday, tm->tm_mon, tm->tm_year);
505 /* get numeric MMDDYY, mm/dd/yy, or dd.mm.yy
507 tlen = strlen(sc_token);
508 mon = atol(sc_token);
511 if (sc_tokid == SLASH || sc_tokid == DOT) {
516 mday = atol(sc_token);
517 if (token() == sep) {
519 year = atol(sc_token);
523 /* flip months and days for European timing
531 else if (tlen == 6 || tlen == 8) {
533 year = (mon % 10000) - 1900;
544 panic("garbled time");
547 if (mon < 0 || mon > 11 || mday < 1 || mday > 31)
548 panic("garbled time");
550 assign_date(tm, mday, mon, year);
556 /* Global functions */
559 parsetime(int argc, char **argv)
561 /* Do the argument parsing, die if necessary, and return the time the job
564 time_t nowtimer, runtimer;
565 struct tm nowtime, runtime;
567 /* this MUST be initialized to zero for midnight/noon/teatime */
569 nowtimer = time(NULL);
570 nowtime = *localtime(&nowtimer);
574 runtime.tm_isdst = 0;
579 init_scanner(argc-optind, argv+optind);
586 /* now is optional prefix for PLUS tree */
597 /* evil coding for TEATIME|NOON|MIDNIGHT - we've initialised
598 * hr to zero up above, then fall into this case in such a
599 * way so we add +12 +4 hours to it for teatime, +12 hours
600 * to it for noon, and nothing at all for midnight, then
601 * set our runtime to that hour before leaping into the
609 if (runtime.tm_hour >= hr) {
613 runtime.tm_hour = hr;
616 /* FALLTHROUGH to month setting */
620 } /* ugly case statement */
623 /* adjust for daylight savings time
625 runtime.tm_isdst = -1;
626 runtimer = mktime(&runtime);
627 if (runtime.tm_isdst > 0) {
629 runtimer = mktime(&runtime);
633 panic("garbled time");
635 if (nowtimer > runtimer)
636 panic("trying to travel back in time");