]>
git.saurik.com Git - apple/system_cmds.git/blob - at.tproj/parsetime.c
2 * parsetime.c - parse time for at(1)
3 * Copyright (C) 1993, 1994 Thomas Koenig
5 * modifications for English-language times
6 * Copyright (C) 1993 David Parsons
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
28 * at [NOW] PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS
30 * /NUMBER [DOT NUMBER] [AM|PM]\ /[MONTH NUMBER [NUMBER]] \
31 * |NOON | |[TOMORROW] |
32 * |MIDNIGHT | |[DAY OF WEEK] |
33 * \TEATIME / |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
34 * \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/
37 #include <sys/cdefs.h>
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 $");
42 #include <sys/types.h>
60 #include "parsetime.h"
63 /* Structures and unions */
66 MIDNIGHT
, NOON
, TEATIME
,
67 PM
, AM
, TOMORROW
, TODAY
, NOW
,
68 MINUTES
, HOURS
, DAYS
, WEEKS
, MONTHS
, YEARS
,
69 NUMBER
, PLUS
, DOT
, COMMA
, 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
,
76 /* parse translation table - table driven parsers can be your FRIEND!
79 const char *name
; /* token name */
80 int value
; /* token id */
81 int plural
; /* is this plural? */
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 */
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) */
116 { "january", JAN
,0 },
117 { "february", FEB
,0 },
124 { "september", SEP
,0 },
125 { "october", OCT
,0 },
126 { "november", NOV
,0 },
127 { "december", DEC
,0 },
128 { "sunday", SUN
, 0 },
130 { "monday", MON
, 0 },
132 { "tuesday", TUE
, 0 },
134 { "wednesday", WED
, 0 },
136 { "thursday", THU
, 0 },
138 { "friday", FRI
, 0 },
140 { "saturday", SAT
, 0 },
145 /* File scope variables */
147 static char **scp
; /* scanner - pointer at arglist */
148 static char scc
; /* scanner - count of remaining arguments */
149 static char *sct
; /* scanner - next char pointer in current argument */
150 static int need
; /* scanner - need to advance to next argument */
152 static char *sc_token
; /* scanner - token buffer */
153 static size_t sc_len
; /* scanner - length of token buffer */
154 static int sc_tokid
; /* scanner - token id */
155 static int sc_tokplur
; /* scanner - is token plural? */
157 /* Local functions */
160 * parse a token, checking if it's something special to us
163 parse_token(char *arg
)
167 for (i
=0; i
<(sizeof Specials
/sizeof Specials
[0]); i
++)
168 if (strcasecmp(Specials
[i
].name
, arg
) == 0) {
169 sc_tokplur
= Specials
[i
].plural
;
170 return sc_tokid
= Specials
[i
].value
;
173 /* not special - must be some random id */
179 * init_scanner() sets up the scanner to eat arguments
182 init_scanner(int argc
, char **argv
)
189 sc_len
+= strlen(*argv
++);
191 if ((sc_token
= malloc(sc_len
)) == NULL
)
192 errx(EXIT_FAILURE
, "virtual memory exhausted");
196 * token() fetches a token from the input stream
204 memset(sc_token
, 0, sc_len
);
209 /* if we need to read another argument, walk along the argument list;
210 * when we fall off the arglist, we'll just return EOF forever
220 /* eat whitespace now - if we walk off the end of the argument,
221 * we'll continue, which puts us up at the top of the while loop
222 * to fetch the next argument in
224 while (isspace(*sct
))
231 /* preserve the first character of the new token
233 sc_token
[0] = *sct
++;
235 /* then see what it is
237 if (isdigit(sc_token
[0])) {
238 while (isdigit(*sct
))
239 sc_token
[++idx
] = *sct
++;
241 return sc_tokid
= NUMBER
;
243 else if (isalpha(sc_token
[0])) {
244 while (isalpha(*sct
))
245 sc_token
[++idx
] = *sct
++;
247 return parse_token(sc_token
);
249 else if (sc_token
[0] == ':' || sc_token
[0] == '.')
250 return sc_tokid
= DOT
;
251 else if (sc_token
[0] == '+')
252 return sc_tokid
= PLUS
;
253 else if (sc_token
[0] == '/')
254 return sc_tokid
= SLASH
;
255 else if (sc_token
[0] == ',')
256 return sc_tokid
= COMMA
;
258 return sc_tokid
= JUNK
;
264 * plonk() gives an appropriate error message if a token is incorrect
269 panic((tok
== EOF
) ? "incomplete time"
275 * expect() gets a token and dies most horribly if it's not the token we want
280 if (token() != desired
)
281 plonk(sc_tokid
); /* and we die here... */
286 * plus() parses a now + time
288 * at [NOW] PLUS NUMBER [MINUTES|HOURS|DAYS|WEEKS|MONTHS|YEARS]
300 delay
= atoi(sc_token
);
301 expectplur
= (delay
!= 1) ? 1 : 0;
305 tm
->tm_year
+= delay
;
313 tm
->tm_mday
+= delay
;
316 tm
->tm_hour
+= delay
;
326 if (expectplur
!= sc_tokplur
)
327 warnx("pluralization is wrong");
337 * tod() computes the time of day
338 * [NUMBER [DOT NUMBER] [AM|PM]] [UTC]
343 int hour
, minute
= 0;
346 hour
= atoi(sc_token
);
347 tlen
= strlen(sc_token
);
349 /* first pick out the time of day - if it's 4 digits, we assume
350 * a HHMM time, otherwise it's HH DOT MM time
352 if (token() == DOT
) {
354 minute
= atoi(sc_token
);
356 panic("garbled time");
359 else if (tlen
== 4) {
362 panic("garbled time");
366 /* check if an AM or PM specifier was given
372 panic("garbled time");
374 if (sc_tokid
== PM
) {
375 if (hour
!= 12) /* 12:xx PM is 12:xx, not 24:xx */
378 if (hour
== 12) /* 12:xx AM is 00:xx, not 12:xx */
382 break; /* else fallthrough */
385 hour
+= tm
->tm_gmtoff
/(60*60);
388 minute
+= (tm
->tm_gmtoff
/60);
396 panic("garbled time");
400 /* if we specify an absolute time, we don't want to bump the day even
401 * if we've gone past that time - but if we're specifying a time plus
402 * a relative offset, it's okay to bump things
403 * If minutes are the same assume tomorrow was meant
405 if ((sc_tokid
== EOF
|| sc_tokid
== PLUS
) &&
406 ((tm
->tm_hour
> hour
) || ((tm
->tm_hour
== hour
) && (tm
->tm_min
>= minute
)))) {
413 if (tm
->tm_hour
== 24) {
421 * assign_date() assigns a date, wrapping to next year if needed
424 assign_date(struct tm
*tm
, long mday
, long mon
, long year
)
427 * Convert year into tm_year format (year - 1900).
428 * We may be given the year in 2 digit, 4 digit, or tm_year format.
431 if (year
>= TM_YEAR_BASE
)
432 year
-= TM_YEAR_BASE
; /* convert from 4 digit year */
433 else if (year
< 100) {
434 /* convert from 2 digit year */
439 lt
= localtime(&now
);
441 /* Convert to tm_year assuming current century */
442 year
+= (lt
->tm_year
/ 100) * 100;
444 if (year
== lt
->tm_year
- 1) year
++;
445 else if (year
< lt
->tm_year
)
446 year
+= 100; /* must be in next century */
451 (tm
->tm_mon
> mon
||(tm
->tm_mon
== mon
&& tm
->tm_mday
> mday
)))
452 year
= tm
->tm_year
+ 1;
463 * month() picks apart a month specification
465 * /[<month> NUMBER [NUMBER]] \
468 * |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
469 * \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/
475 long mday
= 0, wday
, mon
;
484 /* do something tomorrow */
487 case TODAY
: /* force ourselves to stay in today - no further processing */
491 case JAN
: case FEB
: case MAR
: case APR
: case MAY
: case JUN
:
492 case JUL
: case AUG
: case SEP
: case OCT
: case NOV
: case DEC
:
493 /* do month mday [,year]
495 mon
= (sc_tokid
-JAN
);
497 mday
= atol(sc_token
);
498 if (token() == COMMA
) {
499 if (token() == NUMBER
) {
500 year
= atol(sc_token
);
504 assign_date(tm
, mday
, mon
, year
);
505 if (sc_tokid
== PLUS
)
509 case SUN
: case MON
: case TUE
:
510 case WED
: case THU
: case FRI
:
512 /* do a particular day of the week
514 wday
= (sc_tokid
-SUN
);
518 /* if this day is < today, then roll to next week
520 if (wday
< tm
->tm_wday
)
521 mday
+= 7 - (tm
->tm_wday
- wday
);
523 mday
+= (wday
- tm
->tm_wday
);
527 assign_date(tm
, mday
, tm
->tm_mon
, tm
->tm_year
);
531 /* get numeric MMDDYY, mm/dd/yy, or dd.mm.yy
533 tlen
= strlen(sc_token
);
534 mon
= atol(sc_token
);
537 if (sc_tokid
== SLASH
|| sc_tokid
== DOT
) {
542 mday
= atol(sc_token
);
543 if (token() == sep
) {
545 year
= atol(sc_token
);
549 /* flip months and days for European timing
557 else if (tlen
== 6 || tlen
== 8) {
559 year
= (mon
% 10000) - TM_YEAR_BASE
;
570 panic("garbled time");
573 if (mon
< 0 || mon
> 11 || mday
< 1 || mday
> 31)
574 panic("garbled time");
576 assign_date(tm
, mday
, mon
, year
);
582 /* Global functions */
585 parsetime(int argc
, char **argv
)
587 /* Do the argument parsing, die if necessary, and return the time the job
590 time_t nowtimer
, runtimer
;
591 struct tm nowtime
, runtime
;
593 /* this MUST be initialized to zero for midnight/noon/teatime */
595 nowtimer
= time(NULL
);
596 nowtime
= *localtime(&nowtimer
);
600 runtime
.tm_isdst
= 0;
605 init_scanner(argc
-optind
, argv
+optind
);
612 /* now is optional prefix for PLUS tree */
623 /* evil coding for TEATIME|NOON|MIDNIGHT - we've initialised
624 * hr to zero up above, then fall into this case in such a
625 * way so we add +12 +4 hours to it for teatime, +12 hours
626 * to it for noon, and nothing at all for midnight, then
627 * set our runtime to that hour before leaping into the
635 if (runtime
.tm_hour
>= hr
) {
639 runtime
.tm_hour
= hr
;
642 /* FALLTHROUGH to month setting */
646 } /* ugly case statement */
649 /* convert back to time_t
651 runtime
.tm_isdst
= -1;
652 runtimer
= mktime(&runtime
);
655 panic("garbled time");
657 if (nowtimer
> runtimer
)
658 panic("trying to travel back in time");