#include <sys/types.h>
#include <err.h>
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <tzfile.h>
#include <unistd.h>
#include "at.h"
#include "panic.h"
#include "parsetime.h"
#include "stime.h"
typedef enum {
MIDNIGHT, NOON, TEATIME,
PM, AM, TOMORROW, TODAY, NOW,
MINUTES, HOURS, DAYS, WEEKS, MONTHS, YEARS,
NUMBER, PLUS, DOT, SLASH, ID, JUNK,
JAN, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC,
SUN, MON, TUE, WED, THU, FRI, SAT,
TOKEOF
} tokid_t;
static const struct {
const char *name;
tokid_t value;
bool plural;
} Specials[] = {
{"midnight", MIDNIGHT, false},
{"noon", NOON, false},
{"teatime", TEATIME, false},
{"am", AM, false},
{"pm", PM, false},
{"tomorrow", TOMORROW, false},
{"today", TODAY, false},
{"now", NOW, false},
{"minute", MINUTES, false},
{"min", MINUTES, false},
{"m", MINUTES, false},
{"minutes", MINUTES, true},
{"hour", HOURS, false},
{"hr", HOURS, false},
{"h", HOURS, false},
{"hours", HOURS, true},
{"day", DAYS, false},
{"d", DAYS, false},
{"days", DAYS, true},
{"week", WEEKS, false},
{"w", WEEKS, false},
{"weeks", WEEKS, true},
{ "month", MONTHS, 0 },
{ "months", MONTHS, 1 },
{ "year", YEARS, 0 },
{ "years", YEARS, 1 },
{"jan", JAN, false},
{"feb", FEB, false},
{"mar", MAR, false},
{"apr", APR, false},
{"may", MAY, false},
{"jun", JUN, false},
{"jul", JUL, false},
{"aug", AUG, false},
{"sep", SEP, false},
{"oct", OCT, false},
{"nov", NOV, false},
{"dec", DEC, false},
{"january", JAN, false},
{"february", FEB, false},
{"march", MAR, false},
{"april", APR, false},
{"may", MAY, false},
{"june", JUN, false},
{"july", JUL, false},
{"august", AUG, false},
{"september", SEP, false},
{"october", OCT, false},
{"november", NOV, false},
{"december", DEC, false},
{"sunday", SUN, false},
{"sun", SUN, false},
{"monday", MON, false},
{"mon", MON, false},
{"tuesday", TUE, false},
{"tue", TUE, false},
{"wednesday", WED, false},
{"wed", WED, false},
{"thursday", THU, false},
{"thu", THU, false},
{"friday", FRI, false},
{"fri", FRI, false},
{"saturday", SAT, false},
{"sat", SAT, false}
};
static char **scp;
static char scc;
static char *sct;
static bool need;
static char *sc_token;
static size_t sc_len;
static tokid_t sc_tokid;
static bool sc_tokplur;
#ifndef lint
#if 0
static char rcsid[] = "$OpenBSD: parsetime.c,v 1.4 1997/03/01 23:40:10 millert Exp $";
#else
__RCSID("$NetBSD: parsetime.c,v 1.20 2019/02/16 17:56:57 kre Exp $");
#endif
#endif
static void assign_date(struct tm *, int, int, int);
static void expect(tokid_t);
static void init_scanner(int, char **);
static void month(struct tm *);
static tokid_t parse_token(char *);
static void plonk(tokid_t) __dead;
static void plus(struct tm *);
static void tod(struct tm *);
static tokid_t token(void);
static tokid_t
parse_token(char *arg)
{
size_t i;
for (i=0; i < __arraycount(Specials); i++) {
if (strcasecmp(Specials[i].name, arg) == 0) {
sc_tokplur = Specials[i].plural;
return sc_tokid = Specials[i].value;
}
}
return ID;
}
static void
init_scanner(int argc, char **argv)
{
scp = argv;
scc = argc;
need = true;
sc_len = 1;
while (argc-- > 0)
sc_len += strlen(*argv++);
if ((sc_token = malloc(sc_len)) == NULL)
panic("Insufficient virtual memory");
}
static tokid_t
token(void)
{
int idx;
for(;;) {
(void)memset(sc_token, 0, sc_len);
sc_tokid = TOKEOF;
sc_tokplur = false;
idx = 0;
if (need) {
if (scc < 1)
return sc_tokid;
sct = *scp;
scp++;
scc--;
need = false;
}
while (isspace((unsigned char)*sct))
++sct;
if (!*sct) {
need = true;
continue;
}
sc_token[0] = *sct++;
if (isdigit((unsigned char)sc_token[0])) {
while (isdigit((unsigned char)*sct))
sc_token[++idx] = *sct++;
sc_token[++idx] = 0;
return sc_tokid = NUMBER;
} else if (isalpha((unsigned char)sc_token[0])) {
while (isalpha((unsigned char)*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;
else if (sc_token[0] == '/')
return sc_tokid = SLASH;
else
return sc_tokid = JUNK;
}
}
__dead
static void
plonk(tokid_t tok)
{
panic(tok == TOKEOF ? "incomplete time" : "garbled time");
}
static void
expect(tokid_t desired)
{
if (token() != desired)
plonk(sc_tokid);
}
static void
plus(struct tm *tm)
{
int delay;
int expectplur;
expect(NUMBER);
delay = atoi(sc_token);
expectplur = delay != 1;
switch (token()) {
case YEARS:
tm->tm_year += delay;
break;
case MONTHS:
tm->tm_mon += delay;
break;
case WEEKS:
delay *= 7;
case DAYS:
tm->tm_mday += delay;
break;
case HOURS:
tm->tm_hour += delay;
break;
case MINUTES:
tm->tm_min += delay;
break;
default:
plonk(sc_tokid);
break;
}
if (expectplur != sc_tokplur)
warnx("pluralization is wrong");
tm->tm_isdst = -1;
if (mktime(tm) == -1)
plonk(sc_tokid);
}
static void
tod(struct tm *tm)
{
int hour, minute;
size_t tlen;
minute = 0;
hour = atoi(sc_token);
tlen = strlen(sc_token);
if (token() == DOT) {
expect(NUMBER);
minute = atoi(sc_token);
(void)token();
} else if (tlen == 4) {
minute = hour % 100;
hour = hour / 100;
}
if (minute > 59)
panic("garbled time");
if (sc_tokid == AM || sc_tokid == PM) {
if (hour > 12)
panic("garbled time");
if (sc_tokid == PM) {
if (hour != 12)
hour += 12;
} else {
if (hour == 12)
hour = 0;
}
(void)token();
} else if (hour > 23)
panic("garbled time");
if ((sc_tokid == TOKEOF || sc_tokid == PLUS) && (tm->tm_hour > hour ||
(tm->tm_hour == hour && tm->tm_min > minute))) {
tm->tm_mday++;
tm->tm_wday++;
}
tm->tm_hour = hour;
tm->tm_min = minute;
}
static void
assign_date(struct tm *tm, int mday, int mon, int year)
{
if (year > 99) {
if (year >= TM_YEAR_BASE)
tm->tm_year = year - TM_YEAR_BASE;
else
panic("garbled time");
}
else if (year >= 0) {
tm->tm_year = conv_2dig_year(year) - TM_YEAR_BASE;
}
else if (year == -1) {
if (tm->tm_mon > mon ||
(tm->tm_mon == mon && tm->tm_mday > mday))
tm->tm_year++;
}
else
panic("invalid year");
tm->tm_mday = mday;
tm->tm_mon = mon;
}
static void
month(struct tm *tm)
{
int year;
int mday, wday, mon;
size_t tlen;
year = -1;
mday = 0;
switch (sc_tokid) {
case PLUS:
plus(tm);
break;
case TOMORROW:
tm->tm_mday++;
tm->tm_wday++;
case TODAY:
(void)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:
mon = sc_tokid - JAN;
expect(NUMBER);
mday = atoi(sc_token);
if (token() == NUMBER) {
year = atoi(sc_token);
(void)token();
}
assign_date(tm, mday, mon, year);
break;
case SUN: case MON: case TUE:
case WED: case THU: case FRI:
case SAT:
wday = sc_tokid - SUN;
mday = tm->tm_mday;
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 + TM_YEAR_BASE);
break;
case NUMBER:
tlen = strlen(sc_token);
mon = atoi(sc_token);
(void)token();
if (sc_tokid == SLASH || sc_tokid == DOT) {
tokid_t sep;
sep = sc_tokid;
expect(NUMBER);
mday = atoi(sc_token);
if (token() == sep) {
expect(NUMBER);
year = atoi(sc_token);
(void)token();
}
if (sep == DOT) {
int x = mday;
mday = mon;
mon = x;
}
} else if (tlen == 6 || tlen == 8) {
if (tlen == 8) {
year = (mon % 10000) - 1900;
mon /= 10000;
} else {
year = mon % 100;
mon /= 100;
}
mday = mon % 100;
mon /= 100;
} else
panic("garbled time");
mon--;
if (mon < 0 || mon > 11 || mday < 1 || mday > 31)
panic("garbled time");
assign_date(tm, mday, mon, year);
break;
default:
break;
}
}
time_t
parsetime(int argc, char **argv)
{
time_t nowtimer, runtimer;
struct tm nowtime, runtime;
int hr = 0;
nowtimer = time(NULL);
nowtime = *localtime(&nowtimer);
runtime = nowtime;
runtime.tm_sec = 0;
if (argc <= optind)
usage();
init_scanner(argc - optind, argv + optind);
switch (token()) {
case NOW:
if (scc < 1)
return nowtimer;
expect(PLUS);
case PLUS:
plus(&runtime);
break;
case NUMBER:
tod(&runtime);
month(&runtime);
break;
case TEATIME:
hr += 4;
case NOON:
hr += 12;
case MIDNIGHT:
if (runtime.tm_hour >= hr) {
runtime.tm_mday++;
runtime.tm_wday++;
}
runtime.tm_hour = hr;
runtime.tm_min = 0;
(void)token();
default:
month(&runtime);
break;
}
expect(TOKEOF);
runtime.tm_isdst = -1;
runtimer = mktime(&runtime);
if (runtimer == (time_t)-1)
panic("Invalid time");
if (nowtimer > runtimer)
panic("Trying to travel back in time");
return runtimer;
}