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