2 * parsetime.c - parse time for at(1)
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (C) 1993, 1994 Thomas Koenig
8 * modifications for English-language times
9 * Copyright (C) 1993 David Parsons
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. The name of the author(s) may not be used to endorse or promote
17 * products derived from this software without specific prior written
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * at [NOW] PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS
32 * /NUMBER [DOT NUMBER] [AM|PM]\ /[MONTH NUMBER [NUMBER]] \
33 * |NOON | |[TOMORROW] |
34 * |MIDNIGHT | |[DAY OF WEEK] |
35 * \TEATIME / |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
36 * \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
44 #include <sys/types.h>
61 #include "parsetime.h"
64 /* Structures and unions */
67 MIDNIGHT, NOON, TEATIME,
68 PM, AM, TOMORROW, TODAY, NOW,
69 MINUTES, HOURS, DAYS, WEEKS, MONTHS, YEARS,
70 NUMBER, PLUS, MINUS, DOT, SLASH, ID, JUNK,
71 JAN, FEB, MAR, APR, MAY, JUN,
72 JUL, AUG, SEP, OCT, NOV, DEC,
73 SUN, MON, TUE, WED, THU, FRI, SAT
76 /* parse translation table - table driven parsers can be your FRIEND!
79 const char *name; /* token name */
80 int value; /* token id */
81 int plural; /* is this plural? */
83 { "midnight", MIDNIGHT,0 }, /* 00:00:00 of today or tomorrow */
84 { "noon", NOON,0 }, /* 12:00:00 of today or tomorrow */
85 { "teatime", TEATIME,0 }, /* 16:00:00 of today or tomorrow */
86 { "am", AM,0 }, /* morning times for 0-12 clock */
87 { "pm", PM,0 }, /* evening times for 0-12 clock */
88 { "tomorrow", TOMORROW,0 }, /* execute 24 hours from time */
89 { "today", TODAY, 0 }, /* execute today - don't advance time */
90 { "now", NOW,0 }, /* opt prefix for PLUS */
92 { "minute", MINUTES,0 }, /* minutes multiplier */
93 { "minutes", MINUTES,1 }, /* (pluralized) */
94 { "hour", HOURS,0 }, /* hours ... */
95 { "hours", HOURS,1 }, /* (pluralized) */
96 { "day", DAYS,0 }, /* days ... */
97 { "days", DAYS,1 }, /* (pluralized) */
98 { "week", WEEKS,0 }, /* week ... */
99 { "weeks", WEEKS,1 }, /* (pluralized) */
100 { "month", MONTHS,0 }, /* month ... */
101 { "months", MONTHS,1 }, /* (pluralized) */
102 { "year", YEARS,0 }, /* year ... */
103 { "years", YEARS,1 }, /* (pluralized) */
116 { "january", JAN,0 },
117 { "february", FEB,0 },
124 { "september", SEP,0 },
125 { "october", OCT,0 },
126 { "november", NOV,0 },
127 { "december", DEC,0 },
128 { "sunday", SUN, 0 },
130 { "monday", MON, 0 },
132 { "tuesday", TUE, 0 },
134 { "wednesday", WED, 0 },
136 { "thursday", THU, 0 },
138 { "friday", FRI, 0 },
140 { "saturday", SAT, 0 },
144 /* File scope variables */
146 static char **scp; /* scanner - pointer at arglist */
147 static char scc; /* scanner - count of remaining arguments */
148 static char *sct; /* scanner - next char pointer in current argument */
149 static int need; /* scanner - need to advance to next argument */
151 static char *sc_token; /* scanner - token buffer */
152 static size_t sc_len; /* scanner - length of token buffer */
153 static int sc_tokid; /* scanner - token id */
154 static int sc_tokplur; /* scanner - is token plural? */
156 /* Local functions */
159 * parse a token, checking if it's something special to us
162 parse_token(char *arg)
166 for (i=0; i<(sizeof Specials/sizeof Specials[0]); i++)
167 if (strcasecmp(Specials[i].name, arg) == 0) {
168 sc_tokplur = Specials[i].plural;
169 return sc_tokid = Specials[i].value;
172 /* not special - must be some random id */
178 * init_scanner() sets up the scanner to eat arguments
181 init_scanner(int argc, char **argv)
188 sc_len += strlen(*argv++);
190 if ((sc_token = malloc(sc_len)) == NULL)
191 errx(EXIT_FAILURE, "virtual memory exhausted");
195 * token() fetches a token from the input stream
203 memset(sc_token, 0, sc_len);
208 /* if we need to read another argument, walk along the argument list;
209 * when we fall off the arglist, we'll just return EOF forever
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
223 while (isspace(*sct))
230 /* preserve the first character of the new token
232 sc_token[0] = *sct++;
234 /* then see what it is
236 if (isdigit(sc_token[0])) {
237 while (isdigit(*sct))
238 sc_token[++idx] = *sct++;
240 return sc_tokid = NUMBER;
242 else if (isalpha(sc_token[0])) {
243 while (isalpha(*sct))
244 sc_token[++idx] = *sct++;
246 return parse_token(sc_token);
248 else if (sc_token[0] == ':' || sc_token[0] == '.')
249 return sc_tokid = DOT;
250 else if (sc_token[0] == '+')
251 return sc_tokid = PLUS;
252 else if (sc_token[0] == '-')
253 return sc_tokid = MINUS;
254 else if (sc_token[0] == '/')
255 return sc_tokid = SLASH;
257 return sc_tokid = JUNK;
263 * plonk() gives an appropriate error message if a token is incorrect
268 panic((tok == EOF) ? "incomplete time"
274 * expect() gets a token and dies most horribly if it's not the token we want
279 if (token() != desired)
280 plonk(sc_tokid); /* and we die here... */
285 * plus_or_minus() holds functionality common to plus() and minus()
288 plus_or_minus(struct tm *tm, int delay)
292 expectplur = (delay != 1 && delay != -1) ? 1 : 0;
296 tm->tm_year += delay;
304 tm->tm_mday += delay;
307 tm->tm_hour += delay;
317 if (expectplur != sc_tokplur)
318 warnx("pluralization is wrong");
323 } /* plus_or_minus */
327 * plus() parses a now + time
329 * at [NOW] PLUS NUMBER [MINUTES|HOURS|DAYS|WEEKS|MONTHS|YEARS]
339 delay = atoi(sc_token);
340 plus_or_minus(tm, delay);
345 * minus() is like plus but can not be used with NOW
354 delay = -atoi(sc_token);
355 plus_or_minus(tm, delay);
360 * tod() computes the time of day
361 * [NUMBER [DOT NUMBER] [AM|PM]]
366 int hour, minute = 0;
369 hour = atoi(sc_token);
370 tlen = strlen(sc_token);
372 /* first pick out the time of day - if it's 4 digits, we assume
373 * a HHMM time, otherwise it's HH DOT MM time
375 if (token() == DOT) {
377 minute = atoi(sc_token);
379 panic("garbled time");
382 else if (tlen == 4) {
385 panic("garbled time");
389 /* check if an AM or PM specifier was given
391 if (sc_tokid == AM || sc_tokid == PM) {
393 panic("garbled time");
395 if (sc_tokid == PM) {
396 if (hour != 12) /* 12:xx PM is 12:xx, not 24:xx */
399 if (hour == 12) /* 12:xx AM is 00:xx, not 12:xx */
405 panic("garbled time");
407 /* if we specify an absolute time, we don't want to bump the day even
408 * if we've gone past that time - but if we're specifying a time plus
409 * a relative offset, it's okay to bump things
411 if ((sc_tokid == EOF || sc_tokid == PLUS || sc_tokid == MINUS) &&
412 tm->tm_hour > hour) {
419 if (tm->tm_hour == 24) {
427 * assign_date() assigns a date, wrapping to next year if needed
430 assign_date(struct tm *tm, long mday, long mon, long year)
434 * Convert year into tm_year format (year - 1900).
435 * We may be given the year in 2 digit, 4 digit, or tm_year format.
439 year -= 1900; /* convert from 4 digit year */
440 else if (year < 100) {
441 /* convert from 2 digit year */
446 lt = localtime(&now);
448 /* Convert to tm_year assuming current century */
449 year += (lt->tm_year / 100) * 100;
451 if (year == lt->tm_year - 1) year++;
452 else if (year < lt->tm_year)
453 year += 100; /* must be in next century */
458 (tm->tm_mon > mon ||(tm->tm_mon == mon && tm->tm_mday > mday)))
459 year = tm->tm_year + 1;
470 * month() picks apart a month specification
472 * /[<month> NUMBER [NUMBER]] \
475 * |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
476 * \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/
482 long mday = 0, wday, mon;
494 /* do something tomorrow */
497 case TODAY: /* force ourselves to stay in today - no further processing */
501 case JAN: case FEB: case MAR: case APR: case MAY: case JUN:
502 case JUL: case AUG: case SEP: case OCT: case NOV: case DEC:
503 /* do month mday [year]
505 mon = (sc_tokid-JAN);
507 mday = atol(sc_token);
508 if (token() == NUMBER) {
509 year = atol(sc_token);
512 assign_date(tm, mday, mon, year);
515 case SUN: case MON: case TUE:
516 case WED: case THU: case FRI:
518 /* do a particular day of the week
520 wday = (sc_tokid-SUN);
524 /* if this day is < today, then roll to next week
526 if (wday < tm->tm_wday)
527 mday += 7 - (tm->tm_wday - wday);
529 mday += (wday - tm->tm_wday);
533 assign_date(tm, mday, tm->tm_mon, tm->tm_year);
537 /* get numeric MMDDYY, mm/dd/yy, or dd.mm.yy
539 tlen = strlen(sc_token);
540 mon = atol(sc_token);
543 if (sc_tokid == SLASH || sc_tokid == DOT) {
548 mday = atol(sc_token);
549 if (token() == sep) {
551 year = atol(sc_token);
555 /* flip months and days for European timing
563 else if (tlen == 6 || tlen == 8) {
565 year = (mon % 10000) - 1900;
576 panic("garbled time");
579 if (mon < 0 || mon > 11 || mday < 1 || mday > 31)
580 panic("garbled time");
582 assign_date(tm, mday, mon, year);
588 /* Global functions */
591 parsetime(int argc, char **argv)
593 /* Do the argument parsing, die if necessary, and return the time the job
596 time_t nowtimer, runtimer;
597 struct tm nowtime, runtime;
599 /* this MUST be initialized to zero for midnight/noon/teatime */
601 nowtimer = time(NULL);
602 nowtime = *localtime(&nowtimer);
606 runtime.tm_isdst = 0;
611 init_scanner(argc-optind, argv+optind);
618 /* now is optional prefix for PLUS tree */
625 /* MINUS is different from PLUS in that NOW is not
626 * an optional prefix for it
636 /* evil coding for TEATIME|NOON|MIDNIGHT - we've initialised
637 * hr to zero up above, then fall into this case in such a
638 * way so we add +12 +4 hours to it for teatime, +12 hours
639 * to it for noon, and nothing at all for midnight, then
640 * set our runtime to that hour before leaping into the
650 if (runtime.tm_hour >= hr) {
654 runtime.tm_hour = hr;
657 /* FALLTHROUGH to month setting */
661 } /* ugly case statement */
664 /* convert back to time_t
666 runtime.tm_isdst = -1;
667 runtimer = mktime(&runtime);
670 panic("garbled time");
672 if (nowtimer > runtimer)
673 panic("trying to travel back in time");