/usr.bin/at/parsetime.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 670 lines · 439 code · 90 blank · 141 comment · 113 complexity · 4fd6be20a16b061b2016ee2d014049c2 MD5 · raw file

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