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