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