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