]>
git.saurik.com Git - apple/system_cmds.git/blob - at.tproj/parsetime.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * parsetime.c - parse time for at(1)
26 * Copyright (C) 1993 Thomas Koenig
28 * modifications for english-language times
29 * Copyright (C) 1993 David Parsons
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. The name of the author(s) may not be used to endorse or promote
38 * products derived from this software without specific prior written
41 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
42 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
44 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
45 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48 * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
50 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 * at [NOW] PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS
53 * /NUMBER [DOT NUMBER] [AM|PM]\ /[MONTH NUMBER [NUMBER]] \
54 * |NOON | |[TOMORROW] |
55 * |MIDNIGHT | |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
56 * \TEATIME / \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/
61 #include <sys/types.h>
76 /* Structures and unions */
79 MIDNIGHT
, NOON
, TEATIME
,
80 PM
, AM
, TOMORROW
, TODAY
, NOW
,
81 MINUTES
, HOURS
, DAYS
, WEEKS
,
82 NUMBER
, PLUS
, DOT
, SLASH
, ID
, JUNK
,
83 JAN
, FEB
, MAR
, APR
, MAY
, JUN
,
84 JUL
, AUG
, SEP
, OCT
, NOV
, DEC
88 * parse translation table - table driven parsers can be your FRIEND!
91 char *name
; /* token name */
92 int value
; /* token id */
94 { "midnight", MIDNIGHT
}, /* 00:00:00 of today or tomorrow */
95 { "noon", NOON
}, /* 12:00:00 of today or tomorrow */
96 { "teatime", TEATIME
}, /* 16:00:00 of today or tomorrow */
97 { "am", AM
}, /* morning times for 0-12 clock */
98 { "pm", PM
}, /* evening times for 0-12 clock */
99 { "tomorrow", TOMORROW
}, /* execute 24 hours from time */
100 { "today", TODAY
}, /* execute today - don't advance time */
101 { "now", NOW
}, /* opt prefix for PLUS */
103 { "minute", MINUTES
}, /* minutes multiplier */
106 { "minutes", MINUTES
}, /* (pluralized) */
107 { "hour", HOURS
}, /* hours ... */
108 { "hr", HOURS
}, /* abbreviated */
110 { "hours", HOURS
}, /* (pluralized) */
111 { "day", DAYS
}, /* days ... */
113 { "days", DAYS
}, /* (pluralized) */
114 { "week", WEEKS
}, /* week ... */
116 { "weeks", WEEKS
}, /* (pluralized) */
131 /* File scope variables */
133 static char **scp
; /* scanner - pointer at arglist */
134 static char scc
; /* scanner - count of remaining arguments */
135 static char *sct
; /* scanner - next char pointer in current argument */
136 static int need
; /* scanner - need to advance to next argument */
138 static char *sc_token
; /* scanner - token buffer */
139 static size_t sc_len
; /* scanner - lenght of token buffer */
140 static int sc_tokid
; /* scanner - token id */
142 static char rcsid
[] = "$Id: parsetime.c,v 1.1.1.2 2000/01/11 02:10:05 wsanchez Exp $";
144 /* Local functions */
147 * parse a token, checking if it's something special to us
155 for (i
=0; i
<(sizeof Specials
/sizeof Specials
[0]); i
++)
156 if (strcasecmp(Specials
[i
].name
, arg
) == 0) {
157 return sc_tokid
= Specials
[i
].value
;
160 /* not special - must be some random id */
166 * init_scanner() sets up the scanner to eat arguments
169 init_scanner(argc
, argv
)
178 sc_len
+= strlen(*++argv
);
180 sc_token
= (char *) malloc(sc_len
);
181 if (sc_token
== NULL
)
182 panic("Insufficient virtual memory");
186 * token() fetches a token from the input stream
194 memset(sc_token
, 0, sc_len
);
199 * if we need to read another argument, walk along the argument list;
200 * when we fall off the arglist, we'll just return EOF forever
211 * eat whitespace now - if we walk off the end of the argument,
212 * we'll continue, which puts us up at the top of the while loop
213 * to fetch the next argument in
215 while (isspace(*sct
))
223 * preserve the first character of the new token
225 sc_token
[0] = *sct
++;
228 * then see what it is
230 if (isdigit(sc_token
[0])) {
231 while (isdigit(*sct
))
232 sc_token
[++idx
] = *sct
++;
234 return sc_tokid
= NUMBER
;
235 } else if (isalpha(sc_token
[0])) {
236 while (isalpha(*sct
))
237 sc_token
[++idx
] = *sct
++;
239 return parse_token(sc_token
);
241 else if (sc_token
[0] == ':' || sc_token
[0] == '.')
242 return sc_tokid
= DOT
;
243 else if (sc_token
[0] == '+')
244 return sc_tokid
= PLUS
;
245 else if (sc_token
[0] == '/')
246 return sc_tokid
= SLASH
;
248 return sc_tokid
= JUNK
;
254 * plonk() gives an appropriate error message if a token is incorrect
260 panic((tok
== EOF
) ? "incomplete time"
266 * expect() gets a token and dies most horribly if it's not the token we want
272 if (token() != desired
)
273 plonk(sc_tokid
); /* and we die here... */
278 * dateadd() adds a number of minutes to a date. It is extraordinarily
279 * stupid regarding day-of-month overflow, and will most likely not
289 while (minutes
> 24*60) {
294 /* increment hours */
295 while (minutes
> 60) {
298 if (tm
->tm_hour
> 23) {
304 /* increment minutes */
305 tm
->tm_min
+= minutes
;
307 if (tm
->tm_min
> 59) {
311 if (tm
->tm_hour
> 23) {
320 * plus() parses a now + time
322 * at [NOW] PLUS NUMBER [MINUTES|HOURS|DAYS|WEEKS]
333 delay
= atoi(sc_token
);
351 * tod() computes the time of day
352 * [NUMBER [DOT NUMBER] [AM|PM]]
358 int hour
, minute
= 0;
361 hour
= atoi(sc_token
);
362 tlen
= strlen(sc_token
);
365 * first pick out the time of day - if it's 4 digits, we assume
366 * a HHMM time, otherwise it's HH DOT MM time
368 if (token() == DOT
) {
370 minute
= atoi(sc_token
);
372 panic("garbled time");
374 } else if (tlen
== 4) {
377 panic("garbeld time");
382 * check if an AM or PM specifier was given
384 if (sc_tokid
== AM
|| sc_tokid
== PM
) {
386 panic("garbled time");
391 } else if (hour
> 23)
392 panic("garbled time");
395 * if we specify an absolute time, we don't want to bump the day even
396 * if we've gone past that time - but if we're specifying a time plus
397 * a relative offset, it's okay to bump things
399 if ((sc_tokid
== EOF
|| sc_tokid
== PLUS
) && tm
->tm_hour
> hour
)
404 if (tm
->tm_hour
== 24) {
412 * assign_date() assigns a date, wrapping to next year if needed
415 assign_date(tm
, mday
, mon
, year
)
417 long mday
, mon
, year
;
423 panic("garbled time");
427 (tm
->tm_mon
> mon
||(tm
->tm_mon
== mon
&& tm
->tm_mday
> mday
)))
428 year
= tm
->tm_year
+ 1;
439 * month() picks apart a month specification
441 * /[<month> NUMBER [NUMBER]] \
443 * |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
444 * \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/
460 /* do something tomorrow */
462 case TODAY
: /* force ourselves to stay in today - no further processing */
466 case JAN
: case FEB
: case MAR
: case APR
: case MAY
: case JUN
:
467 case JUL
: case AUG
: case SEP
: case OCT
: case NOV
: case DEC
:
469 * do month mday [year]
471 mon
= (sc_tokid
-JAN
);
473 mday
= atol(sc_token
);
474 if (token() == NUMBER
) {
475 year
= atol(sc_token
);
478 assign_date(tm
, mday
, mon
, year
);
483 * get numeric MMDDYY, mm/dd/yy, or dd.mm.yy
485 tlen
= strlen(sc_token
);
486 mon
= atol(sc_token
);
489 if (sc_tokid
== SLASH
|| sc_tokid
== DOT
) {
494 mday
= atol(sc_token
);
495 if (token() == sep
) {
497 year
= atol(sc_token
);
502 * flip months and days for european timing
509 } else if (tlen
== 6 || tlen
== 8) {
511 year
= (mon
% 10000) - 1900;
520 panic("garbled time");
523 if (mon
< 0 || mon
> 11 || mday
< 1 || mday
> 31)
524 panic("garbled time");
526 assign_date(tm
, mday
, mon
, year
);
532 /* Global functions */
535 parsetime(argc
, argv
)
540 * Do the argument parsing, die if necessary, and return the time the job
543 time_t nowtimer
, runtimer
;
544 struct tm nowtime
, runtime
;
546 /* this MUST be initialized to zero for midnight/noon/teatime */
548 nowtimer
= time(NULL
);
549 nowtime
= *localtime(&nowtimer
);
553 runtime
.tm_isdst
= 0;
558 init_scanner(argc
-optind
, argv
+optind
);
561 case NOW
: /* now is optional prefix for PLUS tree */
573 * evil coding for TEATIME|NOON|MIDNIGHT - we've initialised
574 * hr to zero up above, then fall into this case in such a
575 * way so we add +12 +4 hours to it for teatime, +12 hours
576 * to it for noon, and nothing at all for midnight, then
577 * set our runtime to that hour before leaping into the
585 if (runtime
.tm_hour
>= hr
)
587 runtime
.tm_hour
= hr
;
590 /* fall through to month setting */
594 } /* ugly case statement */
598 * adjust for daylight savings time
600 runtime
.tm_isdst
= -1;
601 runtimer
= mktime(&runtime
);
602 if (runtime
.tm_isdst
> 0) {
604 runtimer
= mktime(&runtime
);
608 panic("garbled time");
610 if (nowtimer
> runtimer
)
611 panic("Trying to travel back in time");