1 /* $NetBSD: parsetime.c,v 1.18 2008/04/05 16:26:57 christos Exp $ */
4 * parsetime.c - parse time for at(1)
5 * Copyright (C) 1993, 1994 Thomas Koenig
7 * modifications for english-language times
8 * Copyright (C) 1993 David Parsons
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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.
22 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
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
26 * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
30 * at [NOW] PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS
31 * /NUMBER [DOT NUMBER] [AM|PM]\ /[MONTH NUMBER [NUMBER]] \
32 * |NOON | |[TOMORROW] |
33 * |MIDNIGHT | |[DAY OF WEEK] |
34 * \TEATIME / |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
35 * \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/
40 #include <sys/types.h>
56 #include "parsetime.h"
59 /* Structures and unions */
61 typedef enum { /* symbols */
62 MIDNIGHT
, NOON
, TEATIME
,
63 PM
, AM
, TOMORROW
, TODAY
, NOW
,
64 MINUTES
, HOURS
, DAYS
, WEEKS
, MONTHS
, YEARS
,
65 NUMBER
, PLUS
, DOT
, SLASH
, ID
, JUNK
,
66 JAN
, FEB
, MAR
, APR
, MAY
, JUN
,
67 JUL
, AUG
, SEP
, OCT
, NOV
, DEC
,
68 SUN
, MON
, TUE
, WED
, THU
, FRI
, SAT
,
69 TOKEOF
/* EOF marker */
73 * parse translation table - table driven parsers can be your FRIEND!
76 const char *name
; /* token name */
77 tokid_t value
; /* token id */
78 bool plural
; /* is this plural? */
80 {"midnight", MIDNIGHT
, false}, /* 00:00:00 of today or tomorrow */
81 {"noon", NOON
, false}, /* 12:00:00 of today or tomorrow */
82 {"teatime", TEATIME
, false}, /* 16:00:00 of today or tomorrow */
83 {"am", AM
, false}, /* morning times for 0-12 clock */
84 {"pm", PM
, false}, /* evening times for 0-12 clock */
85 {"tomorrow", TOMORROW
, false}, /* execute 24 hours from time */
86 {"today", TODAY
, false}, /* execute today - don't advance time */
87 {"now", NOW
, false}, /* opt prefix for PLUS */
89 {"minute", MINUTES
, false}, /* minutes multiplier */
90 {"min", MINUTES
, false},
91 {"m", MINUTES
, false},
92 {"minutes", MINUTES
, true}, /* (pluralized) */
93 {"hour", HOURS
, false}, /* hours ... */
94 {"hr", HOURS
, false}, /* abbreviated */
96 {"hours", HOURS
, true}, /* (pluralized) */
97 {"day", DAYS
, false}, /* days ... */
99 {"days", DAYS
, true}, /* (pluralized) */
100 {"week", WEEKS
, false}, /* week ... */
102 {"weeks", WEEKS
, true}, /* (pluralized) */
103 { "month", MONTHS
, 0 }, /* month ... */
104 { "months", MONTHS
, 1 }, /* (pluralized) */
105 { "year", YEARS
, 0 }, /* year ... */
106 { "years", YEARS
, 1 }, /* (pluralized) */
119 {"january", JAN
, false},
120 {"february", FEB
, false},
121 {"march", MAR
, false},
122 {"april", APR
, false},
124 {"june", JUN
, false},
125 {"july", JUL
, false},
126 {"august", AUG
, false},
127 {"september", SEP
, false},
128 {"october", OCT
, false},
129 {"november", NOV
, false},
130 {"december", DEC
, false},
131 {"sunday", SUN
, false},
133 {"monday", MON
, false},
135 {"tuesday", TUE
, false},
137 {"wednesday", WED
, false},
139 {"thursday", THU
, false},
141 {"friday", FRI
, false},
143 {"saturday", SAT
, false},
147 /* File scope variables */
149 static char **scp
; /* scanner - pointer at arglist */
150 static char scc
; /* scanner - count of remaining arguments */
151 static char *sct
; /* scanner - next char pointer in current argument */
152 static bool need
; /* scanner - need to advance to next argument */
154 static char *sc_token
; /* scanner - token buffer */
155 static size_t sc_len
; /* scanner - length of token buffer */
156 static tokid_t sc_tokid
;/* scanner - token id */
157 static bool sc_tokplur
; /* scanner - is token plural? */
161 static char rcsid
[] = "$OpenBSD: parsetime.c,v 1.4 1997/03/01 23:40:10 millert Exp $";
163 __RCSID("$NetBSD: parsetime.c,v 1.18 2008/04/05 16:26:57 christos Exp $");
167 /* Local functions */
168 static void assign_date(struct tm
*, int, int, int);
169 static void expect(tokid_t
);
170 static void init_scanner(int, char **);
171 static void month(struct tm
*);
172 static tokid_t
parse_token(char *);
173 static void plonk(tokid_t
) __dead
;
174 static void plus(struct tm
*);
175 static void tod(struct tm
*);
176 static tokid_t
token(void);
179 * parse a token, checking if it's something special to us
182 parse_token(char *arg
)
186 for (i
=0; i
< __arraycount(Specials
); i
++) {
187 if (strcasecmp(Specials
[i
].name
, arg
) == 0) {
188 sc_tokplur
= Specials
[i
].plural
;
189 return sc_tokid
= Specials
[i
].value
;
193 /* not special - must be some random id */
198 * init_scanner() sets up the scanner to eat arguments
201 init_scanner(int argc
, char **argv
)
209 sc_len
+= strlen(*argv
++);
211 if ((sc_token
= malloc(sc_len
)) == NULL
)
212 panic("Insufficient virtual memory");
216 * token() fetches a token from the input stream
224 (void)memset(sc_token
, 0, sc_len
);
230 * if we need to read another argument, walk along the
231 * argument list; when we fall off the arglist, we'll
232 * just return TOKEOF forever
243 * eat whitespace now - if we walk off the end of the argument,
244 * we'll continue, which puts us up at the top of the while loop
245 * to fetch the next argument in
247 while (isspace((unsigned char)*sct
))
255 * preserve the first character of the new token
257 sc_token
[0] = *sct
++;
260 * then see what it is
262 if (isdigit((unsigned char)sc_token
[0])) {
263 while (isdigit((unsigned char)*sct
))
264 sc_token
[++idx
] = *sct
++;
266 return sc_tokid
= NUMBER
;
267 } else if (isalpha((unsigned char)sc_token
[0])) {
268 while (isalpha((unsigned char)*sct
))
269 sc_token
[++idx
] = *sct
++;
271 return parse_token(sc_token
);
273 else if (sc_token
[0] == ':' || sc_token
[0] == '.')
274 return sc_tokid
= DOT
;
275 else if (sc_token
[0] == '+')
276 return sc_tokid
= PLUS
;
277 else if (sc_token
[0] == '/')
278 return sc_tokid
= SLASH
;
280 return sc_tokid
= JUNK
;
285 * plonk() gives an appropriate error message if a token is incorrect
292 panic(tok
== TOKEOF
? "incomplete time" : "garbled time");
296 * expect() gets a token and dies most horribly if it's not the token we want
299 expect(tokid_t desired
)
302 if (token() != desired
)
303 plonk(sc_tokid
); /* and we die here... */
307 * plus() parses a now + time
309 * at [NOW] PLUS NUMBER [MINUTES|HOURS|DAYS|WEEKS|MONTHS|YEARS]
320 delay
= atoi(sc_token
);
321 expectplur
= delay
!= 1;
325 tm
->tm_year
+= delay
;
334 tm
->tm_mday
+= delay
;
337 tm
->tm_hour
+= delay
;
347 if (expectplur
!= sc_tokplur
)
348 warnx("pluralization is wrong");
351 if (mktime(tm
) == -1)
356 * tod() computes the time of day
357 * [NUMBER [DOT NUMBER] [AM|PM]]
366 hour
= atoi(sc_token
);
367 tlen
= strlen(sc_token
);
370 * first pick out the time of day - if it's 4 digits, we assume
371 * a HHMM time, otherwise it's HH DOT MM time
373 if (token() == DOT
) {
375 minute
= atoi(sc_token
);
377 } else if (tlen
== 4) {
383 panic("garbled time");
386 * check if an AM or PM specifier was given
388 if (sc_tokid
== AM
|| sc_tokid
== PM
) {
390 panic("garbled time");
392 if (sc_tokid
== PM
) {
393 if (hour
!= 12) /* 12:xx PM is 12:xx, not 24:xx */
396 if (hour
== 12) /* 12:xx AM is 00:xx, not 12:xx */
400 } else if (hour
> 23)
401 panic("garbled time");
404 * if we specify an absolute time, we don't want to bump the day even
405 * if we've gone past that time - but if we're specifying a time plus
406 * a relative offset, it's okay to bump things
408 if ((sc_tokid
== TOKEOF
|| sc_tokid
== PLUS
) && tm
->tm_hour
> hour
) {
418 * assign_date() assigns a date, wrapping to next year if needed.
419 * Accept years in 4-digit, 2-digit, or current year (-1).
422 assign_date(struct tm
*tm
, int mday
, int mon
, int year
)
425 if (year
> 99) { /* four digit year */
426 if (year
>= TM_YEAR_BASE
)
427 tm
->tm_year
= year
- TM_YEAR_BASE
;
429 panic("garbled time");
431 else if (year
>= 0) { /* two digit year */
432 tm
->tm_year
= conv_2dig_year(year
) - TM_YEAR_BASE
;
434 else if (year
== -1) { /* year not given (use default in tm) */
435 /* allow for 1 year range from current date */
436 if (tm
->tm_mon
> mon
||
437 (tm
->tm_mon
== mon
&& tm
->tm_mday
> mday
))
441 panic("invalid year");
448 * month() picks apart a month specification
450 * /[<month> NUMBER [NUMBER]] \
453 * |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
454 * \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/
471 /* do something tomorrow */
476 /* force ourselves to stay in today - no further processing */
480 case JAN
: case FEB
: case MAR
: case APR
: case MAY
: case JUN
:
481 case JUL
: case AUG
: case SEP
: case OCT
: case NOV
: case DEC
:
483 * do month mday [year]
485 mon
= sc_tokid
- JAN
;
487 mday
= atoi(sc_token
);
488 if (token() == NUMBER
) {
489 year
= atoi(sc_token
);
492 assign_date(tm
, mday
, mon
, year
);
495 case SUN
: case MON
: case TUE
:
496 case WED
: case THU
: case FRI
:
498 /* do a particular day of the week */
499 wday
= sc_tokid
- SUN
;
503 /* if this day is < today, then roll to next week */
504 if (wday
< tm
->tm_wday
)
505 mday
+= 7 - (tm
->tm_wday
- wday
);
507 mday
+= (wday
- tm
->tm_wday
);
511 assign_date(tm
, mday
, tm
->tm_mon
, tm
->tm_year
+ TM_YEAR_BASE
);
516 * get numeric MMDDYY, mm/dd/yy, or dd.mm.yy
518 tlen
= strlen(sc_token
);
519 mon
= atoi(sc_token
);
522 if (sc_tokid
== SLASH
|| sc_tokid
== DOT
) {
527 mday
= atoi(sc_token
);
528 if (token() == sep
) {
530 year
= atoi(sc_token
);
535 * flip months and days for european timing
542 } else if (tlen
== 6 || tlen
== 8) {
544 year
= (mon
% 10000) - 1900;
553 panic("garbled time");
556 if (mon
< 0 || mon
> 11 || mday
< 1 || mday
> 31)
557 panic("garbled time");
559 assign_date(tm
, mday
, mon
, year
);
562 /* plonk(sc_tokid); */ /* XXX - die here? */
568 /* Global functions */
571 parsetime(int argc
, char **argv
)
574 * Do the argument parsing, die if necessary, and return the
575 * time the job should be run.
577 time_t nowtimer
, runtimer
;
578 struct tm nowtime
, runtime
;
579 int hr
= 0; /* this MUST be initialized to zero for
580 midnight/noon/teatime */
582 nowtimer
= time(NULL
);
583 nowtime
= *localtime(&nowtimer
);
591 init_scanner(argc
- optind
, argv
+ optind
);
598 /* now is optional prefix for PLUS tree */
611 * evil coding for TEATIME|NOON|MIDNIGHT - we've initialised
612 * hr to zero up above, then fall into this case in such a
613 * way so we add +12 +4 hours to it for teatime, +12 hours
614 * to it for noon, and nothing at all for midnight, then
615 * set our runtime to that hour before leaping into the
625 if (runtime
.tm_hour
>= hr
) {
629 runtime
.tm_hour
= hr
;
632 /*FALLTHROUGH*/ /* fall through to month setting */
640 * adjust for daylight savings time
642 runtime
.tm_isdst
= -1;
643 runtimer
= mktime(&runtime
);
645 if (runtimer
== (time_t)-1)
646 panic("Invalid time");
648 if (nowtimer
> runtimer
)
649 panic("Trying to travel back in time");