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