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