#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "globals.h"
#include "at.h"
enum {
MIDNIGHT, NOON, TEATIME,
PM, AM, TOMORROW, TODAY, NOW,
MINUTES, HOURS, DAYS, WEEKS, MONTHS, YEARS,
NUMBER, NEXT, PLUS, DOT, SLASH, ID, JUNK,
JAN, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC,
SUN, MON, TUE, WED, THU, FRI, SAT
};
struct {
char *name;
int value;
int plural;
} Specials[] = {
{ "midnight", MIDNIGHT, 0 },
{ "noon", NOON, 0 },
{ "teatime", TEATIME, 0 },
{ "am", AM, 0 },
{ "pm", PM, 0 },
{ "tomorrow", TOMORROW, 0 },
{ "today", TODAY, 0 },
{ "now", NOW, 0 },
{ "next", NEXT, 0 },
{ "minute", MINUTES, 0 },
{ "min", MINUTES, 0 },
{ "m", MINUTES, 0 },
{ "minutes", MINUTES, 1 },
{ "hour", HOURS, 0 },
{ "hr", HOURS, 0 },
{ "h", HOURS, 0 },
{ "hours", HOURS, 1 },
{ "day", DAYS, 0 },
{ "d", DAYS, 0 },
{ "days", DAYS, 1 },
{ "week", WEEKS, 0 },
{ "w", WEEKS, 0 },
{ "weeks", WEEKS, 1 },
{ "month", MONTHS, 0 },
{ "mo", MONTHS, 0 },
{ "mth", MONTHS, 0 },
{ "months", MONTHS, 1 },
{ "year", YEARS, 0 },
{ "y", YEARS, 0 },
{ "years", YEARS, 1 },
{ "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 },
{ "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 },
{ "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 },
};
static char **scp;
static int scc;
static char *sct;
static int need;
static char *sc_token;
static size_t sc_len;
static int sc_tokid;
static int sc_tokplur;
static int
parse_token(char *arg)
{
int i;
for (i=0; i < sizeof(Specials) / sizeof(Specials[0]); i++) {
if (strcasecmp(Specials[i].name, arg) == 0) {
sc_tokplur = Specials[i].plural;
return (sc_tokid = Specials[i].value);
}
}
return (ID);
}
static int
init_scanner(int argc, char **argv)
{
scp = argv;
scc = argc;
need = 1;
sc_len = 1;
while (argc-- > 0)
sc_len += strlen(*argv++);
if ((sc_token = malloc(sc_len)) == NULL) {
warn(NULL);
return (-1);
}
return (0);
}
static int
token(void)
{
int idx;
for (;;) {
bzero(sc_token, sc_len);
sc_tokid = EOF;
sc_tokplur = 0;
idx = 0;
if (need) {
if (scc < 1)
return (sc_tokid);
sct = *scp;
scp++;
scc--;
need = 0;
}
while (isspace((unsigned char)*sct))
++sct;
if (!*sct) {
need = 1;
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));
}
}
static void
plonk(int tok)
{
warnx("%s time", (tok == EOF) ? "incomplete" : "garbled");
}
static int
expect(int desired)
{
if (token() != desired) {
plonk(sc_tokid);
return (-1);
}
return (0);
}
static void
dateadd(int minutes, struct tm *tm)
{
while (minutes > 24*60) {
minutes -= 24*60;
tm->tm_mday++;
}
while (minutes > 60) {
minutes -= 60;
tm->tm_hour++;
if (tm->tm_hour > 23) {
tm->tm_mday++;
tm->tm_hour = 0;
}
}
tm->tm_min += minutes;
if (tm->tm_min > 59) {
tm->tm_hour++;
tm->tm_min -= 60;
if (tm->tm_hour > 23) {
tm->tm_mday++;
tm->tm_hour = 0;
}
}
}
static int
plus(struct tm *tm)
{
int increment;
int expectplur;
if (sc_tokid == NEXT) {
increment = 1;
expectplur = 0;
} else {
if (expect(NUMBER) != 0)
return (-1);
increment = atoi(sc_token);
expectplur = (increment != 1) ? 1 : 0;
}
switch (token()) {
case YEARS:
tm->tm_year += increment;
return (0);
case MONTHS:
tm->tm_mon += increment;
while (tm->tm_mon >= 12) {
tm->tm_year++;
tm->tm_mon -= 12;
}
return (0);
case WEEKS:
increment *= 7;
case DAYS:
increment *= 24;
case HOURS:
increment *= 60;
case MINUTES:
if (expectplur != sc_tokplur)
warnx("pluralization is wrong");
dateadd(increment, tm);
return (0);
}
plonk(sc_tokid);
return (-1);
}
static int
tod(struct tm *tm)
{
int hour, minute = 0;
size_t tlen;
hour = atoi(sc_token);
tlen = strlen(sc_token);
if (token() == DOT) {
if (expect(NUMBER) != 0)
return (-1);
minute = atoi(sc_token);
if (minute > 59)
goto bad;
token();
} else if (tlen == 4) {
minute = hour % 100;
if (minute > 59)
goto bad;
hour = hour / 100;
}
if (sc_tokid == AM || sc_tokid == PM) {
if (hour > 12)
goto bad;
if (sc_tokid == PM) {
if (hour != 12)
hour += 12;
} else {
if (hour == 12)
hour = 0;
}
token();
} else if (hour > 23)
goto bad;
if ((sc_tokid == EOF || sc_tokid == PLUS || sc_tokid == NEXT) &&
(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;
if (tm->tm_hour == 24) {
tm->tm_hour = 0;
tm->tm_mday++;
}
return (0);
bad:
warnx("garbled time");
return (-1);
}
static void
assign_date(struct tm *tm, int mday, int mon, int year)
{
if (year != -1) {
if (year >= 1900)
year -= 1900;
else if (year < 100) {
year += (tm->tm_year / 100) * 100;
if (year == tm->tm_year - 1)
year++;
else if (year < tm->tm_year)
year += 100;
}
}
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;
}
static int
month(struct tm *tm)
{
int year = (-1);
int mday, wday, mon;
size_t tlen;
switch (sc_tokid) {
case NEXT:
case PLUS:
if (plus(tm) != 0)
return (-1);
break;
case TOMORROW:
tm->tm_mday++;
tm->tm_wday++;
case TODAY:
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;
if (expect(NUMBER) != 0)
return (-1);
mday = atoi(sc_token);
if (token() == NUMBER) {
year = atoi(sc_token);
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);
break;
case NUMBER:
tlen = strlen(sc_token);
mon = atoi(sc_token);
token();
if (sc_tokid == SLASH || sc_tokid == DOT) {
int sep;
sep = sc_tokid;
if (expect(NUMBER) != 0)
return (-1);
mday = atoi(sc_token);
if (token() == sep) {
if (expect(NUMBER) != 0)
return (-1);
year = atoi(sc_token);
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
goto bad;
mon--;
if (mon < 0 || mon > 11 || mday < 1 || mday > 31)
goto bad;
assign_date(tm, mday, mon, year);
break;
}
return (0);
bad:
warnx("garbled time");
return (-1);
}
time_t
parsetime(int argc, char **argv)
{
time_t nowtimer, runtimer;
struct tm nowtime, runtime;
int hr = 0;
if (argc == 0)
return (-1);
nowtimer = time(NULL);
nowtime = *localtime(&nowtimer);
runtime = nowtime;
runtime.tm_sec = 0;
runtime.tm_isdst = 0;
if (init_scanner(argc, argv) == -1)
return (-1);
switch (token()) {
case NOW:
token();
if (sc_tokid == EOF) {
runtime = nowtime;
break;
}
else if (sc_tokid != PLUS && sc_tokid != NEXT)
plonk(sc_tokid);
case NEXT:
case PLUS:
if (plus(&runtime) != 0)
return (-1);
break;
case NUMBER:
if (tod(&runtime) != 0 || month(&runtime) != 0)
return (-1);
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;
token();
default:
if (month(&runtime) != 0)
return (-1);
break;
}
if (expect(EOF) != 0)
return (-1);
runtime.tm_isdst = -1;
runtimer = mktime(&runtime);
if (runtimer < 0) {
warnx("garbled time");
return (-1);
}
if (nowtimer > runtimer) {
warnx("cannot schedule jobs in the past");
return (-1);
}
return (runtimer);
}