parsetime.c revision 1.6
1/* $OpenBSD: parsetime.c,v 1.6 1998/07/09 20:40:59 mickey Exp $ */ 2/* $NetBSD: parsetime.c,v 1.3 1995/03/25 18:13:36 glass Exp $ */ 3 4/* 5 * parsetime.c - parse time for at(1) 6 * Copyright (C) 1993, 1994 Thomas Koenig 7 * 8 * modifications for english-language times 9 * Copyright (C) 1993 David Parsons 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. The name of the author(s) may not be used to endorse or promote 17 * products derived from this software without specific prior written 18 * permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * at [NOW] PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS 32 * /NUMBER [DOT NUMBER] [AM|PM]\ /[MONTH NUMBER [NUMBER]] \ 33 * |NOON | |[TOMORROW] | 34 * |MIDNIGHT | |[DAY OF WEEK] | 35 * \TEATIME / |NUMBER [SLASH NUMBER [SLASH NUMBER]]| 36 * \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/ 37 */ 38 39/* System Headers */ 40 41#include <sys/types.h> 42#include <errno.h> 43#include <stdio.h> 44#include <stdlib.h> 45#include <string.h> 46#include <time.h> 47#include <unistd.h> 48#include <ctype.h> 49#include <err.h> 50 51/* Local headers */ 52 53#include "at.h" 54#include "panic.h" 55 56 57/* Structures and unions */ 58 59enum { /* 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 */ 72struct { 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 130static char **scp; /* scanner - pointer at arglist */ 131static char scc; /* scanner - count of remaining arguments */ 132static char *sct; /* scanner - next char pointer in current argument */ 133static int need; /* scanner - need to advance to next argument */ 134 135static char *sc_token; /* scanner - token buffer */ 136static size_t sc_len; /* scanner - lenght of token buffer */ 137static int sc_tokid; /* scanner - token id */ 138static int sc_tokplur; /* scanner - is token plural? */ 139 140#ifndef lint 141static char rcsid[] = "$OpenBSD: parsetime.c,v 1.6 1998/07/09 20:40:59 mickey Exp $"; 142#endif 143 144/* Local functions */ 145 146/* 147 * parse a token, checking if it's something special to us 148 */ 149static int 150parse_token(arg) 151 char *arg; 152{ 153 int i; 154 155 for (i=0; i < sizeof(Specials) / sizeof(Specials[0]); i++) { 156 if (strcasecmp(Specials[i].name, arg) == 0) { 157 sc_tokplur = Specials[i].plural; 158 return (sc_tokid = Specials[i].value); 159 } 160 } 161 162 /* not special - must be some random id */ 163 return (ID); 164} /* parse_token */ 165 166 167/* 168 * init_scanner() sets up the scanner to eat arguments 169 */ 170static void 171init_scanner(argc, argv) 172 int argc; 173 char **argv; 174{ 175 scp = argv; 176 scc = argc; 177 need = 1; 178 sc_len = 1; 179 while (argc-- > 0) 180 sc_len += strlen(*argv++); 181 182 if ((sc_token = (char *) malloc(sc_len)) == NULL) 183 panic("Insufficient virtual memory"); 184} /* init_scanner */ 185 186/* 187 * token() fetches a token from the input stream 188 */ 189static int 190token() 191{ 192 int idx; 193 194 while (1) { 195 (void)memset(sc_token, 0, sc_len); 196 sc_tokid = EOF; 197 sc_tokplur = 0; 198 idx = 0; 199 200 /* 201 * if we need to read another argument, walk along the 202 * argument list; when we fall off the arglist, we'll 203 * just return EOF forever 204 */ 205 if (need) { 206 if (scc < 1) 207 return (sc_tokid); 208 sct = *scp; 209 scp++; 210 scc--; 211 need = 0; 212 } 213 /* 214 * eat whitespace now - if we walk off the end of the argument, 215 * we'll continue, which puts us up at the top of the while loop 216 * to fetch the next argument in 217 */ 218 while (isspace(*sct)) 219 ++sct; 220 if (!*sct) { 221 need = 1; 222 continue; 223 } 224 225 /* 226 * preserve the first character of the new token 227 */ 228 sc_token[0] = *sct++; 229 230 /* 231 * then see what it is 232 */ 233 if (isdigit(sc_token[0])) { 234 while (isdigit(*sct)) 235 sc_token[++idx] = *sct++; 236 sc_token[++idx] = 0; 237 return ((sc_tokid = NUMBER)); 238 } else if (isalpha(sc_token[0])) { 239 while (isalpha(*sct)) 240 sc_token[++idx] = *sct++; 241 sc_token[++idx] = 0; 242 return (parse_token(sc_token)); 243 } 244 else if (sc_token[0] == ':' || sc_token[0] == '.') 245 return ((sc_tokid = DOT)); 246 else if (sc_token[0] == '+') 247 return ((sc_tokid = PLUS)); 248 else if (sc_token[0] == '/') 249 return ((sc_tokid = SLASH)); 250 else 251 return ((sc_tokid = JUNK)); 252 } /* while (1) */ 253} /* token */ 254 255 256/* 257 * plonk() gives an appropriate error message if a token is incorrect 258 */ 259static void 260plonk(tok) 261 int tok; 262{ 263 panic((tok == EOF) ? "incomplete time" : "garbled time"); 264} /* plonk */ 265 266 267/* 268 * expect() gets a token and dies most horribly if it's not the token we want 269 */ 270static void 271expect(desired) 272 int desired; 273{ 274 if (token() != desired) 275 plonk(sc_tokid); /* and we die here... */ 276} /* expect */ 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 */ 284static void 285dateadd(minutes, tm) 286 int minutes; 287 struct tm *tm; 288{ 289 /* increment days */ 290 291 while (minutes > 24*60) { 292 minutes -= 24*60; 293 tm->tm_mday++; 294 } 295 296 /* increment hours */ 297 while (minutes > 60) { 298 minutes -= 60; 299 tm->tm_hour++; 300 if (tm->tm_hour > 23) { 301 tm->tm_mday++; 302 tm->tm_hour = 0; 303 } 304 } 305 306 /* increment minutes */ 307 tm->tm_min += minutes; 308 309 if (tm->tm_min > 59) { 310 tm->tm_hour++; 311 tm->tm_min -= 60; 312 313 if (tm->tm_hour > 23) { 314 tm->tm_mday++; 315 tm->tm_hour = 0; 316 } 317 } 318} /* dateadd */ 319 320 321/* 322 * plus() parses a now + time 323 * 324 * at [NOW] PLUS NUMBER [MINUTES|HOURS|DAYS|WEEKS] 325 * 326 */ 327static void 328plus(tm) 329 struct tm *tm; 330{ 331 int delay; 332 int expectplur; 333 334 expect(NUMBER); 335 336 delay = atoi(sc_token); 337 expectplur = (delay != 1) ? 1 : 0; 338 339 switch (token()) { 340 case WEEKS: 341 delay *= 7; 342 case DAYS: 343 delay *= 24; 344 case HOURS: 345 delay *= 60; 346 case MINUTES: 347 if (expectplur != sc_tokplur) 348 warnx("pluralization is wrong"); 349 dateadd(delay, tm); 350 return; 351 } 352 353 plonk(sc_tokid); 354} /* plus */ 355 356 357/* 358 * tod() computes the time of day 359 * [NUMBER [DOT NUMBER] [AM|PM]] 360 */ 361static void 362tod(tm) 363 struct tm *tm; 364{ 365 int hour, minute = 0; 366 size_t tlen; 367 368 hour = atoi(sc_token); 369 tlen = strlen(sc_token); 370 371 /* 372 * first pick out the time of day - if it's 4 digits, we assume 373 * a HHMM time, otherwise it's HH DOT MM time 374 */ 375 if (token() == DOT) { 376 expect(NUMBER); 377 minute = atoi(sc_token); 378 if (minute > 59) 379 panic("garbled time"); 380 token(); 381 } else if (tlen == 4) { 382 minute = hour % 100; 383 if (minute > 59) 384 panic("garbled time"); 385 hour = hour / 100; 386 } 387 388 /* 389 * check if an AM or PM specifier was given 390 */ 391 if (sc_tokid == AM || sc_tokid == PM) { 392 if (hour > 12) 393 panic("garbled time"); 394 395 if (sc_tokid == PM) { 396 if (hour != 12) /* 12:xx PM is 12:xx, not 24:xx */ 397 hour += 12; 398 } else { 399 if (hour == 12) /* 12:xx AM is 00:xx, not 12:xx */ 400 hour = 0; 401 } 402 token(); 403 } else if (hour > 23) 404 panic("garbled time"); 405 406 /* 407 * if we specify an absolute time, we don't want to bump the day even 408 * if we've gone past that time - but if we're specifying a time plus 409 * a relative offset, it's okay to bump things 410 */ 411 if ((sc_tokid == EOF || sc_tokid == PLUS) && tm->tm_hour > hour) { 412 tm->tm_mday++; 413 tm->tm_wday++; 414 } 415 416 tm->tm_hour = hour; 417 tm->tm_min = minute; 418 if (tm->tm_hour == 24) { 419 tm->tm_hour = 0; 420 tm->tm_mday++; 421 } 422} /* tod */ 423 424 425/* 426 * assign_date() assigns a date, wrapping to next year if needed 427 */ 428static void 429assign_date(tm, mday, mon, year) 430 struct tm *tm; 431 int mday, mon, year; 432{ 433 if (year > 99) { 434 if (year > 1899) 435 year -= 1900; 436 else 437 panic("garbled time"); 438 } 439 440 if (year < 0 && 441 (tm->tm_mon > mon ||(tm->tm_mon == mon && tm->tm_mday > mday))) 442 year = tm->tm_year + 1; 443 444 tm->tm_mday = mday; 445 tm->tm_mon = mon; 446 447 if (year >= 0) 448 tm->tm_year = year; 449} /* assign_date */ 450 451 452/* 453 * month() picks apart a month specification 454 * 455 * /[<month> NUMBER [NUMBER]] \ 456 * |[TOMORROW] | 457 * |[DAY OF WEEK] | 458 * |NUMBER [SLASH NUMBER [SLASH NUMBER]]| 459 * \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/ 460 */ 461static void 462month(tm) 463 struct tm *tm; 464{ 465 int year = (-1); 466 int mday, wday, mon; 467 size_t tlen; 468 469 switch (sc_tokid) { 470 case PLUS: 471 plus(tm); 472 break; 473 474 case TOMORROW: 475 /* do something tomorrow */ 476 tm->tm_mday++; 477 tm->tm_wday++; 478 case TODAY: 479 /* force ourselves to stay in today - no further processing */ 480 token(); 481 break; 482 483 case JAN: case FEB: case MAR: case APR: case MAY: case JUN: 484 case JUL: case AUG: case SEP: case OCT: case NOV: case DEC: 485 /* 486 * do month mday [year] 487 */ 488 mon = sc_tokid - JAN; 489 expect(NUMBER); 490 mday = atoi(sc_token); 491 if (token() == NUMBER) { 492 year = atoi(sc_token); 493 token(); 494 } 495 assign_date(tm, mday, mon, year); 496 break; 497 498 case SUN: case MON: case TUE: 499 case WED: case THU: case FRI: 500 case SAT: 501 /* do a particular day of the week */ 502 wday = sc_tokid - SUN; 503 504 mday = tm->tm_mday; 505 506 /* if this day is < today, then roll to next week */ 507 if (wday < tm->tm_wday) 508 mday += 7 - (tm->tm_wday - wday); 509 else 510 mday += (wday - tm->tm_wday); 511 512 tm->tm_wday = wday; 513 514 assign_date(tm, mday, tm->tm_mon, tm->tm_year); 515 break; 516 517 case NUMBER: 518 /* 519 * get numeric MMDDYY, mm/dd/yy, or dd.mm.yy 520 */ 521 tlen = strlen(sc_token); 522 mon = atoi(sc_token); 523 token(); 524 525 if (sc_tokid == SLASH || sc_tokid == DOT) { 526 int sep; 527 528 sep = sc_tokid; 529 expect(NUMBER); 530 mday = atoi(sc_token); 531 if (token() == sep) { 532 expect(NUMBER); 533 year = atoi(sc_token); 534 token(); 535 } 536 537 /* 538 * flip months and days for european timing 539 */ 540 if (sep == DOT) { 541 int x = mday; 542 mday = mon; 543 mon = x; 544 } 545 } else if (tlen == 6 || tlen == 8) { 546 if (tlen == 8) { 547 year = (mon % 10000) - 1900; 548 mon /= 10000; 549 } else { 550 year = mon % 100; 551 mon /= 100; 552 } 553 mday = mon % 100; 554 mon /= 100; 555 } else 556 panic("garbled time"); 557 558 mon--; 559 if (mon < 0 || mon > 11 || mday < 1 || mday > 31) 560 panic("garbled time"); 561 562 assign_date(tm, mday, mon, year); 563 break; 564 } /* case */ 565} /* month */ 566 567 568/* Global functions */ 569 570time_t 571parsetime(argc, argv) 572 int argc; 573 char **argv; 574{ 575 /* 576 * Do the argument parsing, die if necessary, and return the 577 * time the job should be run. 578 */ 579 time_t nowtimer, runtimer; 580 struct tm nowtime, runtime; 581 int hr = 0; 582 /* this MUST be initialized to zero for midnight/noon/teatime */ 583 584 nowtimer = time(NULL); 585 nowtime = *localtime(&nowtimer); 586 587 runtime = nowtime; 588 runtime.tm_sec = 0; 589 runtime.tm_isdst = 0; 590 591 if (argc <= optind) 592 usage(); 593 594 init_scanner(argc - optind, argv + optind); 595 596 switch (token()) { 597 case NOW: /* now is optional prefix for PLUS tree */ 598 token(); 599 if (sc_tokid == EOF) { 600 runtime = nowtime; 601 break; 602 } 603 else if (sc_tokid != PLUS) 604 plonk(sc_tokid); 605 case PLUS: 606 plus(&runtime); 607 break; 608 609 case NUMBER: 610 tod(&runtime); 611 month(&runtime); 612 break; 613 614 /* 615 * evil coding for TEATIME|NOON|MIDNIGHT - we've initialised 616 * hr to zero up above, then fall into this case in such a 617 * way so we add +12 +4 hours to it for teatime, +12 hours 618 * to it for noon, and nothing at all for midnight, then 619 * set our runtime to that hour before leaping into the 620 * month scanner 621 */ 622 case TEATIME: 623 hr += 4; 624 case NOON: 625 hr += 12; 626 case MIDNIGHT: 627 if (runtime.tm_hour >= hr) { 628 runtime.tm_mday++; 629 runtime.tm_wday++; 630 } 631 runtime.tm_hour = hr; 632 runtime.tm_min = 0; 633 token(); 634 /* fall through to month setting */ 635 default: 636 month(&runtime); 637 break; 638 } /* ugly case statement */ 639 expect(EOF); 640 641 /* 642 * adjust for daylight savings time 643 */ 644 runtime.tm_isdst = -1; 645 runtimer = mktime(&runtime); 646 if (runtime.tm_isdst > 0) { 647 runtimer -= 3600; 648 runtimer = mktime(&runtime); 649 } 650 651 if (runtimer < 0) 652 panic("garbled time"); 653 654 if (nowtimer > runtimer) 655 panic("Trying to travel back in time"); 656 657 return (runtimer); 658} /* parsetime */ 659