parsetime.c revision 1.15.8.1 1 /* $NetBSD: parsetime.c,v 1.15.8.1 2008/03/24 07:16:34 keiichi Exp $ */
2
3 /*
4 * parsetime.c - parse time for at(1)
5 * Copyright (C) 1993, 1994 Thomas Koenig
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.
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.
29 *
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/
36 */
37
38 /* System Headers */
39
40 #include <sys/types.h>
41 #include <err.h>
42 #include <ctype.h>
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <time.h>
48 #include <tzfile.h>
49 #include <unistd.h>
50
51 /* Local headers */
52
53 #include "at.h"
54 #include "panic.h"
55 #include "parsetime.h"
56
57
58 /* Structures and unions */
59
60 enum { /* symbols */
61 MIDNIGHT, NOON, TEATIME,
62 PM, AM, TOMORROW, TODAY, NOW,
63 MINUTES, HOURS, DAYS, WEEKS, MONTHS, YEARS,
64 NUMBER, PLUS, DOT, SLASH, ID, JUNK,
65 JAN, FEB, MAR, APR, MAY, JUN,
66 JUL, AUG, SEP, OCT, NOV, DEC,
67 SUN, MON, TUE, WED, THU, FRI, SAT
68 };
69
70 /*
71 * parse translation table - table driven parsers can be your FRIEND!
72 */
73 struct {
74 char *name; /* token name */
75 int value; /* token id */
76 int plural; /* is this plural? */
77 } Specials[] = {
78 { "midnight", MIDNIGHT, 0 }, /* 00:00:00 of today or tomorrow */
79 { "noon", NOON, 0 }, /* 12:00:00 of today or tomorrow */
80 { "teatime", TEATIME, 0 }, /* 16:00:00 of today or tomorrow */
81 { "am", AM, 0 }, /* morning times for 0-12 clock */
82 { "pm", PM, 0 }, /* evening times for 0-12 clock */
83 { "tomorrow", TOMORROW, 0 }, /* execute 24 hours from time */
84 { "today", TODAY, 0 }, /* execute today - don't advance time */
85 { "now", NOW, 0 }, /* opt prefix for PLUS */
86
87 { "minute", MINUTES, 0 }, /* minutes multiplier */
88 { "min", MINUTES, 0 },
89 { "m", MINUTES, 0 },
90 { "minutes", MINUTES, 1 }, /* (pluralized) */
91 { "hour", HOURS, 0 }, /* hours ... */
92 { "hr", HOURS, 0 }, /* abbreviated */
93 { "h", HOURS, 0 },
94 { "hours", HOURS, 1 }, /* (pluralized) */
95 { "day", DAYS, 0 }, /* days ... */
96 { "d", DAYS, 0 },
97 { "days", DAYS, 1 }, /* (pluralized) */
98 { "week", WEEKS, 0 }, /* week ... */
99 { "w", WEEKS, 0 },
100 { "weeks", WEEKS, 1 }, /* (pluralized) */
101 { "month", MONTHS, 0 }, /* month ... */
102 { "months", MONTHS, 1 }, /* (pluralized) */
103 { "year", YEARS, 0 }, /* year ... */
104 { "years", YEARS, 1 }, /* (pluralized) */
105 { "jan", JAN, 0 },
106 { "feb", FEB, 0 },
107 { "mar", MAR, 0 },
108 { "apr", APR, 0 },
109 { "may", MAY, 0 },
110 { "jun", JUN, 0 },
111 { "jul", JUL, 0 },
112 { "aug", AUG, 0 },
113 { "sep", SEP, 0 },
114 { "oct", OCT, 0 },
115 { "nov", NOV, 0 },
116 { "dec", DEC, 0 },
117 { "january", JAN, 0 },
118 { "february", FEB, 0 },
119 { "march", MAR, 0 },
120 { "april", APR, 0 },
121 { "may", MAY, 0 },
122 { "june", JUN, 0 },
123 { "july", JUL, 0 },
124 { "august", AUG, 0 },
125 { "september", SEP, 0 },
126 { "october", OCT, 0 },
127 { "november", NOV, 0 },
128 { "december", DEC, 0 },
129 { "sunday", SUN, 0 },
130 { "sun", SUN, 0 },
131 { "monday", MON, 0 },
132 { "mon", MON, 0 },
133 { "tuesday", TUE, 0 },
134 { "tue", TUE, 0 },
135 { "wednesday", WED, 0 },
136 { "wed", WED, 0 },
137 { "thursday", THU, 0 },
138 { "thu", THU, 0 },
139 { "friday", FRI, 0 },
140 { "fri", FRI, 0 },
141 { "saturday", SAT, 0 },
142 { "sat", SAT, 0 },
143 };
144
145 /* File scope variables */
146
147 static char **scp; /* scanner - pointer at arglist */
148 static char scc; /* scanner - count of remaining arguments */
149 static char *sct; /* scanner - next char pointer in current argument */
150 static int need; /* scanner - need to advance to next argument */
151
152 static char *sc_token; /* scanner - token buffer */
153 static size_t sc_len; /* scanner - length of token buffer */
154 static int sc_tokid; /* scanner - token id */
155 static int sc_tokplur; /* scanner - is token plural? */
156
157 #ifndef lint
158 #if 0
159 static char rcsid[] = "$OpenBSD: parsetime.c,v 1.4 1997/03/01 23:40:10 millert Exp $";
160 #else
161 __RCSID("$NetBSD: parsetime.c,v 1.15.8.1 2008/03/24 07:16:34 keiichi Exp $");
162 #endif
163 #endif
164
165 /* Local functions */
166 static void assign_date (struct tm *, long, long, long);
167 static void expect (int);
168 static void init_scanner (int, char **);
169 static void month (struct tm *);
170 static int parse_token (char *);
171 static void plonk (int);
172 static void plus (struct tm *);
173 static void tod (struct tm *);
174 static int token (void);
175
176 /*
177 * parse a token, checking if it's something special to us
178 */
179 static int
180 parse_token(char *arg)
181 {
182 int i;
183
184 for (i=0; i < sizeof(Specials) / sizeof(Specials[0]); i++) {
185 if (strcasecmp(Specials[i].name, arg) == 0) {
186 sc_tokplur = Specials[i].plural;
187 return (sc_tokid = Specials[i].value);
188 }
189 }
190
191 /* not special - must be some random id */
192 return (ID);
193 } /* parse_token */
194
195
196 /*
197 * init_scanner() sets up the scanner to eat arguments
198 */
199 static void
200 init_scanner(int argc, char **argv)
201 {
202 scp = argv;
203 scc = argc;
204 need = 1;
205 sc_len = 1;
206 while (argc-- > 0)
207 sc_len += strlen(*argv++);
208
209 if ((sc_token = (char *) malloc(sc_len)) == NULL)
210 panic("Insufficient virtual memory");
211 } /* init_scanner */
212
213 /*
214 * token() fetches a token from the input stream
215 */
216 static int
217 token(void)
218 {
219 int idx;
220
221 while (1) {
222 (void)memset(sc_token, 0, sc_len);
223 sc_tokid = EOF;
224 sc_tokplur = 0;
225 idx = 0;
226
227 /*
228 * if we need to read another argument, walk along the
229 * argument list; when we fall off the arglist, we'll
230 * just return EOF forever
231 */
232 if (need) {
233 if (scc < 1)
234 return (sc_tokid);
235 sct = *scp;
236 scp++;
237 scc--;
238 need = 0;
239 }
240 /*
241 * eat whitespace now - if we walk off the end of the argument,
242 * we'll continue, which puts us up at the top of the while loop
243 * to fetch the next argument in
244 */
245 while (isspace((unsigned char)*sct))
246 ++sct;
247 if (!*sct) {
248 need = 1;
249 continue;
250 }
251
252 /*
253 * preserve the first character of the new token
254 */
255 sc_token[0] = *sct++;
256
257 /*
258 * then see what it is
259 */
260 if (isdigit((unsigned char)sc_token[0])) {
261 while (isdigit((unsigned char)*sct))
262 sc_token[++idx] = *sct++;
263 sc_token[++idx] = 0;
264 return ((sc_tokid = NUMBER));
265 } else if (isalpha((unsigned char)sc_token[0])) {
266 while (isalpha((unsigned char)*sct))
267 sc_token[++idx] = *sct++;
268 sc_token[++idx] = 0;
269 return (parse_token(sc_token));
270 }
271 else if (sc_token[0] == ':' || sc_token[0] == '.')
272 return ((sc_tokid = DOT));
273 else if (sc_token[0] == '+')
274 return ((sc_tokid = PLUS));
275 else if (sc_token[0] == '/')
276 return ((sc_tokid = SLASH));
277 else
278 return ((sc_tokid = JUNK));
279 } /* while (1) */
280 } /* token */
281
282
283 /*
284 * plonk() gives an appropriate error message if a token is incorrect
285 */
286 static void
287 plonk(int tok)
288 {
289 panic((tok == EOF) ? "incomplete time" : "garbled time");
290 } /* plonk */
291
292
293 /*
294 * expect() gets a token and dies most horribly if it's not the token we want
295 */
296 static void
297 expect(int desired)
298 {
299 if (token() != desired)
300 plonk(sc_tokid); /* and we die here... */
301 } /* expect */
302
303
304 /*
305 * plus() parses a now + time
306 *
307 * at [NOW] PLUS NUMBER [MINUTES|HOURS|DAYS|WEEKS|MONTHS|YEARS]
308 *
309 */
310 static void
311 plus(struct tm *tm)
312 {
313 int delay;
314 int expectplur;
315
316 expect(NUMBER);
317
318 delay = atoi(sc_token);
319 expectplur = (delay != 1) ? 1 : 0;
320
321 switch (token()) {
322 case YEARS:
323 tm->tm_year += delay;
324 break;
325 case MONTHS:
326 tm->tm_mon += delay;
327 break;
328 case WEEKS:
329 delay *= 7;
330 case DAYS:
331 tm->tm_mday += delay;
332 break;
333 case HOURS:
334 tm->tm_hour += delay;
335 break;
336 case MINUTES:
337 tm->tm_min += delay;
338 break;
339 default:
340 plonk(sc_tokid);
341 break;
342 }
343
344 if (expectplur != sc_tokplur)
345 warnx("pluralization is wrong");
346
347 tm->tm_isdst = -1;
348 if (mktime(tm) == -1)
349 plonk(sc_tokid);
350 } /* plus */
351
352
353 /*
354 * tod() computes the time of day
355 * [NUMBER [DOT NUMBER] [AM|PM]]
356 */
357 static void
358 tod(struct tm *tm)
359 {
360 int hour, minute = 0;
361 size_t tlen;
362
363 hour = atoi(sc_token);
364 tlen = strlen(sc_token);
365
366 /*
367 * first pick out the time of day - if it's 4 digits, we assume
368 * a HHMM time, otherwise it's HH DOT MM time
369 */
370 if (token() == DOT) {
371 expect(NUMBER);
372 minute = atoi(sc_token);
373 token();
374 } else if (tlen == 4) {
375 minute = hour % 100;
376 hour = hour / 100;
377 }
378
379 if (minute > 59)
380 panic("garbled time");
381
382 /*
383 * check if an AM or PM specifier was given
384 */
385 if (sc_tokid == AM || sc_tokid == PM) {
386 if (hour > 12)
387 panic("garbled time");
388
389 if (sc_tokid == PM) {
390 if (hour != 12) /* 12:xx PM is 12:xx, not 24:xx */
391 hour += 12;
392 } else {
393 if (hour == 12) /* 12:xx AM is 00:xx, not 12:xx */
394 hour = 0;
395 }
396 token();
397 } else if (hour > 23)
398 panic("garbled time");
399
400 /*
401 * if we specify an absolute time, we don't want to bump the day even
402 * if we've gone past that time - but if we're specifying a time plus
403 * a relative offset, it's okay to bump things
404 */
405 if ((sc_tokid == EOF || sc_tokid == PLUS) && tm->tm_hour > hour) {
406 tm->tm_mday++;
407 tm->tm_wday++;
408 }
409
410 tm->tm_hour = hour;
411 tm->tm_min = minute;
412 } /* tod */
413
414
415 /*
416 * assign_date() assigns a date, wrapping to next year if needed
417 */
418 static void
419 assign_date(struct tm *tm, long mday, long mon, long year)
420 {
421 if (year > 99) {
422 if (year >= TM_YEAR_BASE)
423 year -= TM_YEAR_BASE;
424 else
425 panic("garbled time");
426 }
427
428 if (year >= 0) {
429 if (year < 70)
430 tm->tm_year = year + 2000 - TM_YEAR_BASE;
431 else
432 tm->tm_year = year + 1900 - TM_YEAR_BASE;
433 }
434 else { /* year < 0 */
435 if (tm->tm_mon > mon ||
436 (tm->tm_mon == mon && tm->tm_mday > mday))
437 tm->tm_year++;
438 }
439
440 tm->tm_mday = mday;
441 tm->tm_mon = mon;
442 } /* assign_date */
443
444
445 /*
446 * month() picks apart a month specification
447 *
448 * /[<month> NUMBER [NUMBER]] \
449 * |[TOMORROW] |
450 * |[DAY OF WEEK] |
451 * |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
452 * \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/
453 */
454 static void
455 month(struct tm *tm)
456 {
457 int year = (-1);
458 int mday, wday, mon;
459 size_t tlen;
460
461 mday = 0;
462 switch (sc_tokid) {
463 case PLUS:
464 plus(tm);
465 break;
466
467 case TOMORROW:
468 /* do something tomorrow */
469 tm->tm_mday++;
470 tm->tm_wday++;
471 case TODAY:
472 /* force ourselves to stay in today - no further processing */
473 token();
474 break;
475
476 case JAN: case FEB: case MAR: case APR: case MAY: case JUN:
477 case JUL: case AUG: case SEP: case OCT: case NOV: case DEC:
478 /*
479 * do month mday [year]
480 */
481 mon = sc_tokid - JAN;
482 expect(NUMBER);
483 mday = atoi(sc_token);
484 if (token() == NUMBER) {
485 year = atoi(sc_token);
486 token();
487 }
488 assign_date(tm, mday, mon, year);
489 break;
490
491 case SUN: case MON: case TUE:
492 case WED: case THU: case FRI:
493 case SAT:
494 /* do a particular day of the week */
495 wday = sc_tokid - SUN;
496
497 mday = tm->tm_mday;
498
499 /* if this day is < today, then roll to next week */
500 if (wday < tm->tm_wday)
501 mday += 7 - (tm->tm_wday - wday);
502 else
503 mday += (wday - tm->tm_wday);
504
505 tm->tm_wday = wday;
506
507 assign_date(tm, mday, tm->tm_mon, tm->tm_year + TM_YEAR_BASE);
508 break;
509
510 case NUMBER:
511 /*
512 * get numeric MMDDYY, mm/dd/yy, or dd.mm.yy
513 */
514 tlen = strlen(sc_token);
515 mon = atoi(sc_token);
516 token();
517
518 if (sc_tokid == SLASH || sc_tokid == DOT) {
519 int sep;
520
521 sep = sc_tokid;
522 expect(NUMBER);
523 mday = atoi(sc_token);
524 if (token() == sep) {
525 expect(NUMBER);
526 year = atoi(sc_token);
527 token();
528 }
529
530 /*
531 * flip months and days for european timing
532 */
533 if (sep == DOT) {
534 int x = mday;
535 mday = mon;
536 mon = x;
537 }
538 } else if (tlen == 6 || tlen == 8) {
539 if (tlen == 8) {
540 year = (mon % 10000) - 1900;
541 mon /= 10000;
542 } else {
543 year = mon % 100;
544 mon /= 100;
545 }
546 mday = mon % 100;
547 mon /= 100;
548 } else
549 panic("garbled time");
550
551 mon--;
552 if (mon < 0 || mon > 11 || mday < 1 || mday > 31)
553 panic("garbled time");
554
555 assign_date(tm, mday, mon, year);
556 break;
557 } /* case */
558 } /* month */
559
560
561 /* Global functions */
562
563 time_t
564 parsetime(int argc, char **argv)
565 {
566 /*
567 * Do the argument parsing, die if necessary, and return the
568 * time the job should be run.
569 */
570 time_t nowtimer, runtimer;
571 struct tm nowtime, runtime;
572 int hr = 0; /* this MUST be initialized to zero for
573 midnight/noon/teatime */
574
575 nowtimer = time(NULL);
576 nowtime = *localtime(&nowtimer);
577
578 runtime = nowtime;
579 runtime.tm_sec = 0;
580
581 if (argc <= optind)
582 usage();
583
584 init_scanner(argc - optind, argv + optind);
585
586 switch (token()) {
587 case NOW:
588 if (scc < 1)
589 return nowtimer;
590
591 /* now is optional prefix for PLUS tree */
592 expect(PLUS);
593 /*FALLTHROUGH*/
594 case PLUS:
595 plus(&runtime);
596 break;
597
598 case NUMBER:
599 tod(&runtime);
600 month(&runtime);
601 break;
602
603 /*
604 * evil coding for TEATIME|NOON|MIDNIGHT - we've initialised
605 * hr to zero up above, then fall into this case in such a
606 * way so we add +12 +4 hours to it for teatime, +12 hours
607 * to it for noon, and nothing at all for midnight, then
608 * set our runtime to that hour before leaping into the
609 * month scanner
610 */
611 case TEATIME:
612 hr += 4;
613 case NOON:
614 hr += 12;
615 case MIDNIGHT:
616 if (runtime.tm_hour >= hr) {
617 runtime.tm_mday++;
618 runtime.tm_wday++;
619 }
620 runtime.tm_hour = hr;
621 runtime.tm_min = 0;
622 token();
623 /* fall through to month setting */
624 default:
625 month(&runtime);
626 break;
627 } /* ugly case statement */
628 expect(EOF);
629
630 /*
631 * convert back to time_t
632 */
633 runtime.tm_isdst = -1;
634 runtimer = mktime(&runtime);
635
636 if (runtimer < 0)
637 panic("Invalid time");
638
639 if (nowtimer > runtimer)
640 panic("Trying to travel back in time");
641
642 return (runtimer);
643 } /* parsetime */
644