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