2017-11-27 15:37:16 +00:00
|
|
|
/*-
|
1995-04-12 02:42:39 +00:00
|
|
|
* parsetime.c - parse time for at(1)
|
2017-11-27 15:37:16 +00:00
|
|
|
*
|
2023-05-10 15:40:58 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2017-11-27 15:37:16 +00:00
|
|
|
*
|
1995-04-12 02:42:39 +00:00
|
|
|
* Copyright (C) 1993, 1994 Thomas Koenig
|
1994-01-05 01:09:14 +00:00
|
|
|
*
|
1999-12-05 19:57:14 +00:00
|
|
|
* modifications for English-language times
|
1995-04-12 02:42:39 +00:00
|
|
|
* Copyright (C) 1993 David Parsons
|
1994-01-05 01:09:14 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. The name of the author(s) may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
* permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
1995-08-21 12:34:18 +00:00
|
|
|
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
1994-01-05 01:09:14 +00:00
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* at [NOW] PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS
|
|
|
|
* /NUMBER [DOT NUMBER] [AM|PM]\ /[MONTH NUMBER [NUMBER]] \
|
|
|
|
* |NOON | |[TOMORROW] |
|
1995-04-12 02:42:39 +00:00
|
|
|
* |MIDNIGHT | |[DAY OF WEEK] |
|
|
|
|
* \TEATIME / |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
|
|
|
|
* \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/
|
1994-01-05 01:09:14 +00:00
|
|
|
*/
|
|
|
|
|
2001-12-02 20:23:02 +00:00
|
|
|
#include <sys/cdefs.h>
|
1999-12-05 19:57:14 +00:00
|
|
|
/* System Headers */
|
1995-04-12 02:42:39 +00:00
|
|
|
|
1994-01-05 01:09:14 +00:00
|
|
|
#include <sys/types.h>
|
1999-12-05 19:57:14 +00:00
|
|
|
#include <ctype.h>
|
1997-06-24 06:26:32 +00:00
|
|
|
#include <err.h>
|
1994-01-05 01:09:14 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
1995-04-12 02:42:39 +00:00
|
|
|
#ifndef __FreeBSD__
|
|
|
|
#include <getopt.h>
|
|
|
|
#endif
|
1994-01-05 01:09:14 +00:00
|
|
|
|
|
|
|
/* Local headers */
|
|
|
|
|
|
|
|
#include "at.h"
|
|
|
|
#include "panic.h"
|
2001-12-02 12:26:18 +00:00
|
|
|
#include "parsetime.h"
|
1994-01-05 01:09:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Structures and unions */
|
|
|
|
|
1995-08-21 12:34:18 +00:00
|
|
|
enum { /* symbols */
|
1994-01-05 01:09:14 +00:00
|
|
|
MIDNIGHT, NOON, TEATIME,
|
|
|
|
PM, AM, TOMORROW, TODAY, NOW,
|
1999-08-26 16:18:38 +00:00
|
|
|
MINUTES, HOURS, DAYS, WEEKS, MONTHS, YEARS,
|
2012-10-05 17:54:27 +00:00
|
|
|
NUMBER, PLUS, MINUS, DOT, SLASH, ID, JUNK,
|
1994-01-05 01:09:14 +00:00
|
|
|
JAN, FEB, MAR, APR, MAY, JUN,
|
1995-04-12 02:42:39 +00:00
|
|
|
JUL, AUG, SEP, OCT, NOV, DEC,
|
|
|
|
SUN, MON, TUE, WED, THU, FRI, SAT
|
|
|
|
};
|
1994-01-05 01:09:14 +00:00
|
|
|
|
1995-04-12 02:42:39 +00:00
|
|
|
/* parse translation table - table driven parsers can be your FRIEND!
|
1994-01-05 01:09:14 +00:00
|
|
|
*/
|
2011-11-06 17:32:29 +00:00
|
|
|
static const struct {
|
2001-12-02 12:26:18 +00:00
|
|
|
const char *name; /* token name */
|
1995-08-21 12:34:18 +00:00
|
|
|
int value; /* token id */
|
1995-04-12 02:42:39 +00:00
|
|
|
int plural; /* is this plural? */
|
1994-01-05 01:09:14 +00:00
|
|
|
} Specials[] = {
|
1995-04-12 02:42:39 +00:00
|
|
|
{ "midnight", MIDNIGHT,0 }, /* 00:00:00 of today or tomorrow */
|
|
|
|
{ "noon", NOON,0 }, /* 12:00:00 of today or tomorrow */
|
|
|
|
{ "teatime", TEATIME,0 }, /* 16:00:00 of today or tomorrow */
|
|
|
|
{ "am", AM,0 }, /* morning times for 0-12 clock */
|
|
|
|
{ "pm", PM,0 }, /* evening times for 0-12 clock */
|
|
|
|
{ "tomorrow", TOMORROW,0 }, /* execute 24 hours from time */
|
|
|
|
{ "today", TODAY, 0 }, /* execute today - don't advance time */
|
|
|
|
{ "now", NOW,0 }, /* opt prefix for PLUS */
|
|
|
|
|
|
|
|
{ "minute", MINUTES,0 }, /* minutes multiplier */
|
|
|
|
{ "minutes", MINUTES,1 }, /* (pluralized) */
|
|
|
|
{ "hour", HOURS,0 }, /* hours ... */
|
|
|
|
{ "hours", HOURS,1 }, /* (pluralized) */
|
|
|
|
{ "day", DAYS,0 }, /* days ... */
|
|
|
|
{ "days", DAYS,1 }, /* (pluralized) */
|
|
|
|
{ "week", WEEKS,0 }, /* week ... */
|
|
|
|
{ "weeks", WEEKS,1 }, /* (pluralized) */
|
1999-08-26 16:18:38 +00:00
|
|
|
{ "month", MONTHS,0 }, /* month ... */
|
|
|
|
{ "months", MONTHS,1 }, /* (pluralized) */
|
|
|
|
{ "year", YEARS,0 }, /* year ... */
|
|
|
|
{ "years", YEARS,1 }, /* (pluralized) */
|
1995-04-12 02:42:39 +00:00
|
|
|
{ "jan", JAN,0 },
|
|
|
|
{ "feb", FEB,0 },
|
|
|
|
{ "mar", MAR,0 },
|
|
|
|
{ "apr", APR,0 },
|
|
|
|
{ "may", MAY,0 },
|
|
|
|
{ "jun", JUN,0 },
|
|
|
|
{ "jul", JUL,0 },
|
|
|
|
{ "aug", AUG,0 },
|
|
|
|
{ "sep", SEP,0 },
|
|
|
|
{ "oct", OCT,0 },
|
|
|
|
{ "nov", NOV,0 },
|
|
|
|
{ "dec", DEC,0 },
|
1998-07-09 22:51:59 +00:00
|
|
|
{ "january", JAN,0 },
|
|
|
|
{ "february", FEB,0 },
|
|
|
|
{ "march", MAR,0 },
|
|
|
|
{ "april", APR,0 },
|
|
|
|
{ "may", MAY,0 },
|
|
|
|
{ "june", JUN,0 },
|
|
|
|
{ "july", JUL,0 },
|
|
|
|
{ "august", AUG,0 },
|
|
|
|
{ "september", SEP,0 },
|
|
|
|
{ "october", OCT,0 },
|
|
|
|
{ "november", NOV,0 },
|
|
|
|
{ "december", DEC,0 },
|
1995-04-12 02:42:39 +00:00
|
|
|
{ "sunday", SUN, 0 },
|
|
|
|
{ "sun", SUN, 0 },
|
|
|
|
{ "monday", MON, 0 },
|
|
|
|
{ "mon", MON, 0 },
|
|
|
|
{ "tuesday", TUE, 0 },
|
|
|
|
{ "tue", TUE, 0 },
|
|
|
|
{ "wednesday", WED, 0 },
|
|
|
|
{ "wed", WED, 0 },
|
|
|
|
{ "thursday", THU, 0 },
|
|
|
|
{ "thu", THU, 0 },
|
|
|
|
{ "friday", FRI, 0 },
|
|
|
|
{ "fri", FRI, 0 },
|
|
|
|
{ "saturday", SAT, 0 },
|
|
|
|
{ "sat", SAT, 0 },
|
1994-01-05 01:09:14 +00:00
|
|
|
} ;
|
|
|
|
|
|
|
|
/* File scope variables */
|
|
|
|
|
|
|
|
static char **scp; /* scanner - pointer at arglist */
|
|
|
|
static char scc; /* scanner - count of remaining arguments */
|
|
|
|
static char *sct; /* scanner - next char pointer in current argument */
|
|
|
|
static int need; /* scanner - need to advance to next argument */
|
|
|
|
|
|
|
|
static char *sc_token; /* scanner - token buffer */
|
1999-12-05 19:57:14 +00:00
|
|
|
static size_t sc_len; /* scanner - length of token buffer */
|
1995-08-21 12:34:18 +00:00
|
|
|
static int sc_tokid; /* scanner - token id */
|
1995-04-12 02:42:39 +00:00
|
|
|
static int sc_tokplur; /* scanner - is token plural? */
|
1994-01-05 01:09:14 +00:00
|
|
|
|
|
|
|
/* Local functions */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse a token, checking if it's something special to us
|
|
|
|
*/
|
1995-08-21 12:34:18 +00:00
|
|
|
static int
|
|
|
|
parse_token(char *arg)
|
1994-01-05 01:09:14 +00:00
|
|
|
{
|
2001-12-02 12:26:18 +00:00
|
|
|
size_t i;
|
1994-01-05 01:09:14 +00:00
|
|
|
|
|
|
|
for (i=0; i<(sizeof Specials/sizeof Specials[0]); i++)
|
|
|
|
if (strcasecmp(Specials[i].name, arg) == 0) {
|
1995-04-12 02:42:39 +00:00
|
|
|
sc_tokplur = Specials[i].plural;
|
1994-01-05 01:09:14 +00:00
|
|
|
return sc_tokid = Specials[i].value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not special - must be some random id */
|
|
|
|
return ID;
|
|
|
|
} /* parse_token */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* init_scanner() sets up the scanner to eat arguments
|
|
|
|
*/
|
|
|
|
static void
|
1995-04-12 02:42:39 +00:00
|
|
|
init_scanner(int argc, char **argv)
|
1994-01-05 01:09:14 +00:00
|
|
|
{
|
|
|
|
scp = argv;
|
|
|
|
scc = argc;
|
|
|
|
need = 1;
|
|
|
|
sc_len = 1;
|
1995-08-21 12:34:18 +00:00
|
|
|
while (argc-- > 0)
|
|
|
|
sc_len += strlen(*argv++);
|
1994-01-05 01:09:14 +00:00
|
|
|
|
2001-07-24 14:15:51 +00:00
|
|
|
if ((sc_token = malloc(sc_len)) == NULL)
|
|
|
|
errx(EXIT_FAILURE, "virtual memory exhausted");
|
1994-01-05 01:09:14 +00:00
|
|
|
} /* init_scanner */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* token() fetches a token from the input stream
|
|
|
|
*/
|
1995-08-21 12:34:18 +00:00
|
|
|
static int
|
2001-12-02 12:26:18 +00:00
|
|
|
token(void)
|
1994-01-05 01:09:14 +00:00
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
memset(sc_token, 0, sc_len);
|
|
|
|
sc_tokid = EOF;
|
1995-04-12 02:42:39 +00:00
|
|
|
sc_tokplur = 0;
|
1994-01-05 01:09:14 +00:00
|
|
|
idx = 0;
|
|
|
|
|
1995-04-12 02:42:39 +00:00
|
|
|
/* if we need to read another argument, walk along the argument list;
|
1994-01-05 01:09:14 +00:00
|
|
|
* when we fall off the arglist, we'll just return EOF forever
|
|
|
|
*/
|
|
|
|
if (need) {
|
|
|
|
if (scc < 1)
|
|
|
|
return sc_tokid;
|
|
|
|
sct = *scp;
|
|
|
|
scp++;
|
|
|
|
scc--;
|
|
|
|
need = 0;
|
|
|
|
}
|
1995-04-12 02:42:39 +00:00
|
|
|
/* eat whitespace now - if we walk off the end of the argument,
|
1994-01-05 01:09:14 +00:00
|
|
|
* we'll continue, which puts us up at the top of the while loop
|
|
|
|
* to fetch the next argument in
|
|
|
|
*/
|
|
|
|
while (isspace(*sct))
|
|
|
|
++sct;
|
|
|
|
if (!*sct) {
|
|
|
|
need = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1995-04-12 02:42:39 +00:00
|
|
|
/* preserve the first character of the new token
|
1994-01-05 01:09:14 +00:00
|
|
|
*/
|
|
|
|
sc_token[0] = *sct++;
|
|
|
|
|
1995-04-12 02:42:39 +00:00
|
|
|
/* then see what it is
|
1994-01-05 01:09:14 +00:00
|
|
|
*/
|
|
|
|
if (isdigit(sc_token[0])) {
|
|
|
|
while (isdigit(*sct))
|
|
|
|
sc_token[++idx] = *sct++;
|
|
|
|
sc_token[++idx] = 0;
|
|
|
|
return sc_tokid = NUMBER;
|
1995-04-12 02:42:39 +00:00
|
|
|
}
|
|
|
|
else if (isalpha(sc_token[0])) {
|
1994-01-05 01:09:14 +00:00
|
|
|
while (isalpha(*sct))
|
|
|
|
sc_token[++idx] = *sct++;
|
|
|
|
sc_token[++idx] = 0;
|
|
|
|
return parse_token(sc_token);
|
|
|
|
}
|
|
|
|
else if (sc_token[0] == ':' || sc_token[0] == '.')
|
|
|
|
return sc_tokid = DOT;
|
|
|
|
else if (sc_token[0] == '+')
|
|
|
|
return sc_tokid = PLUS;
|
2012-10-05 17:54:27 +00:00
|
|
|
else if (sc_token[0] == '-')
|
|
|
|
return sc_tokid = MINUS;
|
1995-04-12 02:42:39 +00:00
|
|
|
else if (sc_token[0] == '/')
|
1994-01-05 01:09:14 +00:00
|
|
|
return sc_tokid = SLASH;
|
|
|
|
else
|
|
|
|
return sc_tokid = JUNK;
|
|
|
|
} /* while (1) */
|
|
|
|
} /* token */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* plonk() gives an appropriate error message if a token is incorrect
|
|
|
|
*/
|
|
|
|
static void
|
1995-04-12 02:42:39 +00:00
|
|
|
plonk(int tok)
|
1994-01-05 01:09:14 +00:00
|
|
|
{
|
|
|
|
panic((tok == EOF) ? "incomplete time"
|
|
|
|
: "garbled time");
|
|
|
|
} /* plonk */
|
|
|
|
|
|
|
|
|
1995-08-21 12:34:18 +00:00
|
|
|
/*
|
1994-01-05 01:09:14 +00:00
|
|
|
* expect() gets a token and dies most horribly if it's not the token we want
|
|
|
|
*/
|
|
|
|
static void
|
1995-08-21 12:34:18 +00:00
|
|
|
expect(int desired)
|
1994-01-05 01:09:14 +00:00
|
|
|
{
|
|
|
|
if (token() != desired)
|
|
|
|
plonk(sc_tokid); /* and we die here... */
|
|
|
|
} /* expect */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-10-05 17:54:27 +00:00
|
|
|
* plus_or_minus() holds functionality common to plus() and minus()
|
1994-01-05 01:09:14 +00:00
|
|
|
*/
|
|
|
|
static void
|
2012-10-05 17:54:27 +00:00
|
|
|
plus_or_minus(struct tm *tm, int delay)
|
1994-01-05 01:09:14 +00:00
|
|
|
{
|
1995-04-12 02:42:39 +00:00
|
|
|
int expectplur;
|
1994-01-05 01:09:14 +00:00
|
|
|
|
2012-10-05 17:54:27 +00:00
|
|
|
expectplur = (delay != 1 && delay != -1) ? 1 : 0;
|
1994-01-05 01:09:14 +00:00
|
|
|
|
|
|
|
switch (token()) {
|
1999-08-26 16:18:38 +00:00
|
|
|
case YEARS:
|
|
|
|
tm->tm_year += delay;
|
|
|
|
break;
|
|
|
|
case MONTHS:
|
|
|
|
tm->tm_mon += delay;
|
|
|
|
break;
|
1994-01-05 01:09:14 +00:00
|
|
|
case WEEKS:
|
|
|
|
delay *= 7;
|
|
|
|
case DAYS:
|
1999-08-26 16:18:38 +00:00
|
|
|
tm->tm_mday += delay;
|
|
|
|
break;
|
1994-01-05 01:09:14 +00:00
|
|
|
case HOURS:
|
1999-08-26 16:18:38 +00:00
|
|
|
tm->tm_hour += delay;
|
|
|
|
break;
|
1994-01-05 01:09:14 +00:00
|
|
|
case MINUTES:
|
1999-08-26 16:18:38 +00:00
|
|
|
tm->tm_min += delay;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
plonk(sc_tokid);
|
|
|
|
break;
|
1994-01-05 01:09:14 +00:00
|
|
|
}
|
1999-08-26 16:18:38 +00:00
|
|
|
|
|
|
|
if (expectplur != sc_tokplur)
|
|
|
|
warnx("pluralization is wrong");
|
|
|
|
|
|
|
|
tm->tm_isdst = -1;
|
|
|
|
if (mktime(tm) < 0)
|
|
|
|
plonk(sc_tokid);
|
2012-10-05 17:54:27 +00:00
|
|
|
} /* plus_or_minus */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* plus() parses a now + time
|
|
|
|
*
|
|
|
|
* at [NOW] PLUS NUMBER [MINUTES|HOURS|DAYS|WEEKS|MONTHS|YEARS]
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
plus(struct tm *tm)
|
|
|
|
{
|
|
|
|
int delay;
|
|
|
|
|
|
|
|
expect(NUMBER);
|
1999-08-26 16:18:38 +00:00
|
|
|
|
2012-10-05 17:54:27 +00:00
|
|
|
delay = atoi(sc_token);
|
|
|
|
plus_or_minus(tm, delay);
|
1994-01-05 01:09:14 +00:00
|
|
|
} /* plus */
|
|
|
|
|
|
|
|
|
2012-10-05 17:54:27 +00:00
|
|
|
/*
|
|
|
|
* minus() is like plus but can not be used with NOW
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
minus(struct tm *tm)
|
|
|
|
{
|
|
|
|
int delay;
|
|
|
|
|
|
|
|
expect(NUMBER);
|
|
|
|
|
|
|
|
delay = -atoi(sc_token);
|
|
|
|
plus_or_minus(tm, delay);
|
|
|
|
} /* minus */
|
|
|
|
|
|
|
|
|
1994-01-05 01:09:14 +00:00
|
|
|
/*
|
|
|
|
* tod() computes the time of day
|
|
|
|
* [NUMBER [DOT NUMBER] [AM|PM]]
|
|
|
|
*/
|
|
|
|
static void
|
1995-04-12 02:42:39 +00:00
|
|
|
tod(struct tm *tm)
|
1994-01-05 01:09:14 +00:00
|
|
|
{
|
|
|
|
int hour, minute = 0;
|
|
|
|
int tlen;
|
|
|
|
|
|
|
|
hour = atoi(sc_token);
|
|
|
|
tlen = strlen(sc_token);
|
|
|
|
|
1995-04-12 02:42:39 +00:00
|
|
|
/* first pick out the time of day - if it's 4 digits, we assume
|
1994-01-05 01:09:14 +00:00
|
|
|
* a HHMM time, otherwise it's HH DOT MM time
|
|
|
|
*/
|
|
|
|
if (token() == DOT) {
|
|
|
|
expect(NUMBER);
|
|
|
|
minute = atoi(sc_token);
|
|
|
|
if (minute > 59)
|
|
|
|
panic("garbled time");
|
|
|
|
token();
|
1995-04-12 02:42:39 +00:00
|
|
|
}
|
|
|
|
else if (tlen == 4) {
|
1994-01-05 01:09:14 +00:00
|
|
|
minute = hour%100;
|
|
|
|
if (minute > 59)
|
1998-08-30 17:33:05 +00:00
|
|
|
panic("garbled time");
|
1994-01-05 01:09:14 +00:00
|
|
|
hour = hour/100;
|
|
|
|
}
|
|
|
|
|
1995-04-12 02:42:39 +00:00
|
|
|
/* check if an AM or PM specifier was given
|
1994-01-05 01:09:14 +00:00
|
|
|
*/
|
|
|
|
if (sc_tokid == AM || sc_tokid == PM) {
|
|
|
|
if (hour > 12)
|
|
|
|
panic("garbled time");
|
|
|
|
|
1996-07-19 00:44:55 +00:00
|
|
|
if (sc_tokid == PM) {
|
|
|
|
if (hour != 12) /* 12:xx PM is 12:xx, not 24:xx */
|
1997-06-23 06:44:18 +00:00
|
|
|
hour += 12;
|
1996-07-19 00:44:55 +00:00
|
|
|
} else {
|
|
|
|
if (hour == 12) /* 12:xx AM is 00:xx, not 12:xx */
|
1997-06-23 06:44:18 +00:00
|
|
|
hour = 0;
|
1996-07-19 00:44:55 +00:00
|
|
|
}
|
1994-01-05 01:09:14 +00:00
|
|
|
token();
|
1995-04-12 02:42:39 +00:00
|
|
|
}
|
|
|
|
else if (hour > 23)
|
1994-01-05 01:09:14 +00:00
|
|
|
panic("garbled time");
|
|
|
|
|
1995-04-12 02:42:39 +00:00
|
|
|
/* if we specify an absolute time, we don't want to bump the day even
|
1994-01-05 01:09:14 +00:00
|
|
|
* if we've gone past that time - but if we're specifying a time plus
|
|
|
|
* a relative offset, it's okay to bump things
|
|
|
|
*/
|
2012-10-05 17:54:27 +00:00
|
|
|
if ((sc_tokid == EOF || sc_tokid == PLUS || sc_tokid == MINUS) &&
|
|
|
|
tm->tm_hour > hour) {
|
1994-01-05 01:09:14 +00:00
|
|
|
tm->tm_mday++;
|
1995-04-12 02:42:39 +00:00
|
|
|
tm->tm_wday++;
|
|
|
|
}
|
1994-01-05 01:09:14 +00:00
|
|
|
|
|
|
|
tm->tm_hour = hour;
|
|
|
|
tm->tm_min = minute;
|
|
|
|
if (tm->tm_hour == 24) {
|
|
|
|
tm->tm_hour = 0;
|
|
|
|
tm->tm_mday++;
|
|
|
|
}
|
|
|
|
} /* tod */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* assign_date() assigns a date, wrapping to next year if needed
|
|
|
|
*/
|
|
|
|
static void
|
1995-04-12 02:42:39 +00:00
|
|
|
assign_date(struct tm *tm, long mday, long mon, long year)
|
1994-01-05 01:09:14 +00:00
|
|
|
{
|
1998-05-05 01:53:15 +00:00
|
|
|
|
2000-03-27 09:32:23 +00:00
|
|
|
/*
|
|
|
|
* Convert year into tm_year format (year - 1900).
|
|
|
|
* We may be given the year in 2 digit, 4 digit, or tm_year format.
|
|
|
|
*/
|
|
|
|
if (year != -1) {
|
|
|
|
if (year >= 1900)
|
|
|
|
year -= 1900; /* convert from 4 digit year */
|
|
|
|
else if (year < 100) {
|
|
|
|
/* convert from 2 digit year */
|
|
|
|
struct tm *lt;
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
time(&now);
|
|
|
|
lt = localtime(&now);
|
|
|
|
|
|
|
|
/* Convert to tm_year assuming current century */
|
|
|
|
year += (lt->tm_year / 100) * 100;
|
|
|
|
|
|
|
|
if (year == lt->tm_year - 1) year++;
|
|
|
|
else if (year < lt->tm_year)
|
|
|
|
year += 100; /* must be in next century */
|
|
|
|
}
|
1994-01-05 01:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (year < 0 &&
|
|
|
|
(tm->tm_mon > mon ||(tm->tm_mon == mon && tm->tm_mday > mday)))
|
|
|
|
year = tm->tm_year + 1;
|
|
|
|
|
|
|
|
tm->tm_mday = mday;
|
|
|
|
tm->tm_mon = mon;
|
|
|
|
|
|
|
|
if (year >= 0)
|
|
|
|
tm->tm_year = year;
|
|
|
|
} /* assign_date */
|
|
|
|
|
|
|
|
|
1995-08-21 12:34:18 +00:00
|
|
|
/*
|
1994-01-05 01:09:14 +00:00
|
|
|
* month() picks apart a month specification
|
|
|
|
*
|
|
|
|
* /[<month> NUMBER [NUMBER]] \
|
|
|
|
* |[TOMORROW] |
|
1995-04-12 02:42:39 +00:00
|
|
|
* |[DAY OF WEEK] |
|
1994-01-05 01:09:14 +00:00
|
|
|
* |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
|
|
|
|
* \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/
|
|
|
|
*/
|
|
|
|
static void
|
1995-04-12 02:42:39 +00:00
|
|
|
month(struct tm *tm)
|
1994-01-05 01:09:14 +00:00
|
|
|
{
|
|
|
|
long year= (-1);
|
1998-12-06 07:42:09 +00:00
|
|
|
long mday = 0, wday, mon;
|
1994-01-05 01:09:14 +00:00
|
|
|
int tlen;
|
|
|
|
|
|
|
|
switch (sc_tokid) {
|
|
|
|
case PLUS:
|
|
|
|
plus(tm);
|
|
|
|
break;
|
2012-10-05 17:54:27 +00:00
|
|
|
case MINUS:
|
|
|
|
minus(tm);
|
|
|
|
break;
|
1994-01-05 01:09:14 +00:00
|
|
|
|
|
|
|
case TOMORROW:
|
|
|
|
/* do something tomorrow */
|
|
|
|
tm->tm_mday ++;
|
1995-04-12 02:42:39 +00:00
|
|
|
tm->tm_wday ++;
|
1994-01-05 01:09:14 +00:00
|
|
|
case TODAY: /* force ourselves to stay in today - no further processing */
|
|
|
|
token();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JAN: case FEB: case MAR: case APR: case MAY: case JUN:
|
|
|
|
case JUL: case AUG: case SEP: case OCT: case NOV: case DEC:
|
1995-04-12 02:42:39 +00:00
|
|
|
/* do month mday [year]
|
1994-01-05 01:09:14 +00:00
|
|
|
*/
|
|
|
|
mon = (sc_tokid-JAN);
|
|
|
|
expect(NUMBER);
|
1995-01-31 17:54:39 +00:00
|
|
|
mday = atol(sc_token);
|
1994-01-05 01:09:14 +00:00
|
|
|
if (token() == NUMBER) {
|
|
|
|
year = atol(sc_token);
|
|
|
|
token();
|
|
|
|
}
|
|
|
|
assign_date(tm, mday, mon, year);
|
|
|
|
break;
|
|
|
|
|
1995-04-12 02:42:39 +00:00
|
|
|
case SUN: case MON: case TUE:
|
|
|
|
case WED: case THU: case FRI:
|
|
|
|
case SAT:
|
|
|
|
/* do a particular day of the week
|
|
|
|
*/
|
|
|
|
wday = (sc_tokid-SUN);
|
|
|
|
|
|
|
|
mday = tm->tm_mday;
|
|
|
|
|
|
|
|
/* if this day is < today, then roll to next week
|
|
|
|
*/
|
|
|
|
if (wday < tm->tm_wday)
|
|
|
|
mday += 7 - (tm->tm_wday - wday);
|
|
|
|
else
|
|
|
|
mday += (wday - tm->tm_wday);
|
|
|
|
|
|
|
|
tm->tm_wday = wday;
|
|
|
|
|
|
|
|
assign_date(tm, mday, tm->tm_mon, tm->tm_year);
|
|
|
|
break;
|
|
|
|
|
1994-01-05 01:09:14 +00:00
|
|
|
case NUMBER:
|
1995-04-12 02:42:39 +00:00
|
|
|
/* get numeric MMDDYY, mm/dd/yy, or dd.mm.yy
|
1994-01-05 01:09:14 +00:00
|
|
|
*/
|
|
|
|
tlen = strlen(sc_token);
|
|
|
|
mon = atol(sc_token);
|
|
|
|
token();
|
|
|
|
|
|
|
|
if (sc_tokid == SLASH || sc_tokid == DOT) {
|
1995-08-21 12:34:18 +00:00
|
|
|
int sep;
|
1994-01-05 01:09:14 +00:00
|
|
|
|
|
|
|
sep = sc_tokid;
|
|
|
|
expect(NUMBER);
|
|
|
|
mday = atol(sc_token);
|
|
|
|
if (token() == sep) {
|
|
|
|
expect(NUMBER);
|
|
|
|
year = atol(sc_token);
|
|
|
|
token();
|
|
|
|
}
|
|
|
|
|
1999-12-05 19:57:14 +00:00
|
|
|
/* flip months and days for European timing
|
1994-01-05 01:09:14 +00:00
|
|
|
*/
|
|
|
|
if (sep == DOT) {
|
|
|
|
int x = mday;
|
|
|
|
mday = mon;
|
|
|
|
mon = x;
|
|
|
|
}
|
1995-04-12 02:42:39 +00:00
|
|
|
}
|
|
|
|
else if (tlen == 6 || tlen == 8) {
|
1994-01-05 01:09:14 +00:00
|
|
|
if (tlen == 8) {
|
|
|
|
year = (mon % 10000) - 1900;
|
|
|
|
mon /= 10000;
|
1995-04-12 02:42:39 +00:00
|
|
|
}
|
|
|
|
else {
|
1994-01-05 01:09:14 +00:00
|
|
|
year = mon % 100;
|
|
|
|
mon /= 100;
|
|
|
|
}
|
|
|
|
mday = mon % 100;
|
|
|
|
mon /= 100;
|
1995-04-12 02:42:39 +00:00
|
|
|
}
|
|
|
|
else
|
1994-01-05 01:09:14 +00:00
|
|
|
panic("garbled time");
|
|
|
|
|
|
|
|
mon--;
|
|
|
|
if (mon < 0 || mon > 11 || mday < 1 || mday > 31)
|
|
|
|
panic("garbled time");
|
|
|
|
|
|
|
|
assign_date(tm, mday, mon, year);
|
|
|
|
break;
|
|
|
|
} /* case */
|
|
|
|
} /* month */
|
|
|
|
|
|
|
|
|
|
|
|
/* Global functions */
|
|
|
|
|
|
|
|
time_t
|
1995-04-12 02:42:39 +00:00
|
|
|
parsetime(int argc, char **argv)
|
1994-01-05 01:09:14 +00:00
|
|
|
{
|
1995-04-12 02:42:39 +00:00
|
|
|
/* Do the argument parsing, die if necessary, and return the time the job
|
1994-01-05 01:09:14 +00:00
|
|
|
* should be run.
|
|
|
|
*/
|
|
|
|
time_t nowtimer, runtimer;
|
|
|
|
struct tm nowtime, runtime;
|
|
|
|
int hr = 0;
|
|
|
|
/* this MUST be initialized to zero for midnight/noon/teatime */
|
|
|
|
|
|
|
|
nowtimer = time(NULL);
|
|
|
|
nowtime = *localtime(&nowtimer);
|
|
|
|
|
|
|
|
runtime = nowtime;
|
|
|
|
runtime.tm_sec = 0;
|
|
|
|
runtime.tm_isdst = 0;
|
|
|
|
|
|
|
|
if (argc <= optind)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
init_scanner(argc-optind, argv+optind);
|
|
|
|
|
|
|
|
switch (token()) {
|
2001-11-24 10:43:53 +00:00
|
|
|
case NOW:
|
|
|
|
if (scc < 1) {
|
|
|
|
return nowtimer;
|
|
|
|
}
|
|
|
|
/* now is optional prefix for PLUS tree */
|
1994-01-05 01:09:14 +00:00
|
|
|
expect(PLUS);
|
2017-12-08 17:15:20 +00:00
|
|
|
/* FALLTHROUGH */
|
1994-01-05 01:09:14 +00:00
|
|
|
case PLUS:
|
|
|
|
plus(&runtime);
|
|
|
|
break;
|
|
|
|
|
2012-10-05 17:54:27 +00:00
|
|
|
/* MINUS is different from PLUS in that NOW is not
|
|
|
|
* an optional prefix for it
|
|
|
|
*/
|
|
|
|
case MINUS:
|
|
|
|
minus(&runtime);
|
|
|
|
break;
|
1994-01-05 01:09:14 +00:00
|
|
|
case NUMBER:
|
|
|
|
tod(&runtime);
|
|
|
|
month(&runtime);
|
|
|
|
break;
|
|
|
|
|
1995-04-12 02:42:39 +00:00
|
|
|
/* evil coding for TEATIME|NOON|MIDNIGHT - we've initialised
|
1994-01-05 01:09:14 +00:00
|
|
|
* hr to zero up above, then fall into this case in such a
|
|
|
|
* way so we add +12 +4 hours to it for teatime, +12 hours
|
|
|
|
* to it for noon, and nothing at all for midnight, then
|
|
|
|
* set our runtime to that hour before leaping into the
|
|
|
|
* month scanner
|
|
|
|
*/
|
|
|
|
case TEATIME:
|
|
|
|
hr += 4;
|
2017-12-08 17:15:20 +00:00
|
|
|
/* FALLTHROUGH */
|
1994-01-05 01:09:14 +00:00
|
|
|
case NOON:
|
|
|
|
hr += 12;
|
2017-12-08 17:15:20 +00:00
|
|
|
/* FALLTHROUGH */
|
1994-01-05 01:09:14 +00:00
|
|
|
case MIDNIGHT:
|
1995-04-12 02:42:39 +00:00
|
|
|
if (runtime.tm_hour >= hr) {
|
1994-01-05 01:09:14 +00:00
|
|
|
runtime.tm_mday++;
|
1995-04-12 02:42:39 +00:00
|
|
|
runtime.tm_wday++;
|
|
|
|
}
|
1994-01-05 01:09:14 +00:00
|
|
|
runtime.tm_hour = hr;
|
|
|
|
runtime.tm_min = 0;
|
|
|
|
token();
|
1999-12-05 19:57:14 +00:00
|
|
|
/* FALLTHROUGH to month setting */
|
1994-01-05 01:09:14 +00:00
|
|
|
default:
|
|
|
|
month(&runtime);
|
|
|
|
break;
|
|
|
|
} /* ugly case statement */
|
|
|
|
expect(EOF);
|
|
|
|
|
2005-08-18 08:18:02 +00:00
|
|
|
/* convert back to time_t
|
1994-01-05 01:09:14 +00:00
|
|
|
*/
|
|
|
|
runtime.tm_isdst = -1;
|
|
|
|
runtimer = mktime(&runtime);
|
|
|
|
|
|
|
|
if (runtimer < 0)
|
|
|
|
panic("garbled time");
|
|
|
|
|
|
|
|
if (nowtimer > runtimer)
|
1999-12-05 19:57:14 +00:00
|
|
|
panic("trying to travel back in time");
|
1994-01-05 01:09:14 +00:00
|
|
|
|
|
|
|
return runtimer;
|
|
|
|
} /* parsetime */
|