#include <err.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include "vary.h"
struct trans {
long val;
const char *str;
};
static struct trans trans_mon[] = {
{ 1, "january" }, { 2, "february" }, { 3, "march" }, { 4, "april" },
{ 5, "may"}, { 6, "june" }, { 7, "july" }, { 8, "august" },
{ 9, "september" }, { 10, "october" }, { 11, "november" }, { 12, "december" },
{ -1, NULL }
};
static struct trans trans_wday[] = {
{ 0, "sunday" }, { 1, "monday" }, { 2, "tuesday" }, { 3, "wednesday" },
{ 4, "thursday" }, { 5, "friday" }, { 6, "saturday" },
{ -1, NULL }
};
static int adjhour(struct tm *, char, long, int);
static int
domktime(struct tm *t, char type)
{
time_t ret;
while ((ret = mktime(t)) == -1 && t->tm_year > 68 && t->tm_year < 138)
adjhour(t, type == '-' ? type : '+', 1, 0);
return ret;
}
static int
trans(const struct trans t[], const char *arg)
{
int f;
for (f = 0; t[f].val != -1; f++)
if (!strncasecmp(t[f].str, arg, 3) ||
!strncasecmp(t[f].str, arg, strlen(t[f].str)))
return t[f].val;
return -1;
}
struct vary *
vary_append(struct vary *v, char *arg)
{
struct vary *result, **nextp;
if (v) {
result = v;
while (v->next)
v = v->next;
nextp = &v->next;
} else
nextp = &result;
if ((*nextp = (struct vary *)malloc(sizeof(struct vary))) == NULL)
err(1, "malloc");
(*nextp)->arg = arg;
(*nextp)->next = NULL;
return result;
}
static int mdays[12] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static int
daysinmonth(const struct tm *t)
{
int year;
year = t->tm_year + 1900;
if (t->tm_mon == 1)
if (!(year % 400))
return 29;
else if (!(year % 100))
return 28;
else if (!(year % 4))
return 29;
else
return 28;
else if (t->tm_mon >= 0 && t->tm_mon < 12)
return mdays[t->tm_mon];
return 0;
}
static int
adjyear(struct tm *t, char type, long val, int mk)
{
switch (type) {
case '+':
t->tm_year += val;
break;
case '-':
t->tm_year -= val;
break;
default:
t->tm_year = val;
if (t->tm_year < 69)
t->tm_year += 100;
else if (t->tm_year > 1900)
t->tm_year -= 1900;
break;
}
return !mk || domktime(t, type) != -1;
}
static int
adjmon(struct tm *t, char type, long val, int istext, int mk)
{
int lmdays;
if (val < 0)
return 0;
switch (type) {
case '+':
if (istext) {
if (val <= t->tm_mon)
val += 11 - t->tm_mon;
else
val -= t->tm_mon + 1;
}
if (val) {
if (!adjyear(t, '+', (t->tm_mon + val) / 12, 0))
return 0;
val %= 12;
t->tm_mon += val;
if (t->tm_mon > 11)
t->tm_mon -= 12;
}
break;
case '-':
if (istext) {
if (val-1 > t->tm_mon)
val = 13 - val + t->tm_mon;
else
val = t->tm_mon - val + 1;
}
if (val) {
if (!adjyear(t, '-', val / 12, 0))
return 0;
val %= 12;
if (val > t->tm_mon) {
if (!adjyear(t, '-', 1, 0))
return 0;
val -= 12;
}
t->tm_mon -= val;
}
break;
default:
if (val > 12 || val < 1)
return 0;
t->tm_mon = --val;
}
lmdays = daysinmonth(t);
if (t->tm_mday > lmdays)
t->tm_mday = lmdays;
return !mk || domktime(t, type) != -1;
}
static int
adjday(struct tm *t, char type, long val, int mk)
{
int daycount;
switch (type) {
case '+':
while (val) {
daycount = daysinmonth(t);
if (val > daycount - t->tm_mday) {
val -= daycount - t->tm_mday + 1;
t->tm_mday = 1;
if (!adjmon(t, '+', 1, 0, 0))
return 0;
} else {
t->tm_mday += val;
val = 0;
}
}
break;
case '-':
while (val)
if (val >= t->tm_mday) {
val -= t->tm_mday;
t->tm_mday = 1;
if (!adjmon(t, '-', 1, 0, 0))
return 0;
t->tm_mday = daysinmonth(t);
} else {
t->tm_mday -= val;
val = 0;
}
break;
default:
if (val > 0 && val <= daysinmonth(t))
t->tm_mday = val;
else
return 0;
break;
}
return !mk || domktime(t, type) != -1;
}
static int
adjwday(struct tm *t, char type, long val, int istext, int mk)
{
if (val < 0)
return 0;
switch (type) {
case '+':
if (istext)
if (val < t->tm_wday)
val = 7 - t->tm_wday + val;
else
val -= t->tm_wday;
else
val *= 7;
return !val || adjday(t, '+', val, mk);
case '-':
if (istext) {
if (val > t->tm_wday)
val = 7 - val + t->tm_wday;
else
val = t->tm_wday - val;
} else
val *= 7;
return !val || adjday(t, '-', val, mk);
default:
if (val < t->tm_wday)
return adjday(t, '-', t->tm_wday - val, mk);
else if (val > 6)
return 0;
else if (val > t->tm_wday)
return adjday(t, '+', val - t->tm_wday, mk);
}
return 1;
}
static int
adjhour(struct tm *t, char type, long val, int mk)
{
if (val < 0)
return 0;
switch (type) {
case '+':
if (val) {
int days;
days = (t->tm_hour + val) / 24;
val %= 24;
t->tm_hour += val;
t->tm_hour %= 24;
if (!adjday(t, '+', days, 0))
return 0;
}
break;
case '-':
if (val) {
int days;
days = val / 24;
val %= 24;
if (val > t->tm_hour) {
days++;
val -= 24;
}
t->tm_hour -= val;
if (!adjday(t, '-', days, 0))
return 0;
}
break;
default:
if (val > 23)
return 0;
t->tm_hour = val;
}
return !mk || domktime(t, type) != -1;
}
static int
adjmin(struct tm *t, char type, long val, int mk)
{
if (val < 0)
return 0;
switch (type) {
case '+':
if (val) {
if (!adjhour(t, '+', (t->tm_min + val) / 60, 0))
return 0;
val %= 60;
t->tm_min += val;
if (t->tm_min > 59)
t->tm_min -= 60;
}
break;
case '-':
if (val) {
if (!adjhour(t, '-', val / 60, 0))
return 0;
val %= 60;
if (val > t->tm_min) {
if (!adjhour(t, '-', 1, 0))
return 0;
val -= 60;
}
t->tm_min -= val;
}
break;
default:
if (val > 59)
return 0;
t->tm_min = val;
}
return !mk || domktime(t, type) != -1;
}
static int
adjsec(struct tm *t, char type, long val, int mk)
{
if (val < 0)
return 0;
switch (type) {
case '+':
if (val) {
if (!adjmin(t, '+', (t->tm_sec + val) / 60, 0))
return 0;
val %= 60;
t->tm_sec += val;
if (t->tm_sec > 59)
t->tm_sec -= 60;
}
break;
case '-':
if (val) {
if (!adjmin(t, '-', val / 60, 0))
return 0;
val %= 60;
if (val > t->tm_sec) {
if (!adjmin(t, '-', 1, 0))
return 0;
val -= 60;
}
t->tm_sec -= val;
}
break;
default:
if (val > 59)
return 0;
t->tm_sec = val;
}
return !mk || domktime(t, type) != -1;
}
const struct vary *
vary_apply(const struct vary *vb, time_t tval, struct tm *t)
{
const struct vary *v;
char type;
char *arg;
char *tmp;
size_t len;
long val;
*t = *localtime(&tval);
for (v = vb; v; v = v->next) {
int dohms = 1;
int doyear = 1;
int dotzone = 1;
arg = v->arg;
type = *arg;
if (type == '@') {
time_t tt;
tt = strtoul(arg + 1, NULL, 10);
*t = *gmtime(&tt);
continue;
}
if (type == '+' || type == '-') {
arg++;
} else if (strncmp(arg, "next", 4) == 0) {
type = '+';
arg += 4;
} else if (strncmp(arg, "last", 4) == 0) {
type = '-';
arg += 4;
} else {
type = '\0';
}
while (isspace(arg[0]))
++arg;
len = strlen(arg);
if (len < 2)
return v;
if (type == '\0')
t->tm_isdst = -1;
val = strtoul(arg, &tmp, 10);
if (tmp != arg && isalpha(tmp[0])) {
if (strcmp(tmp, "S") == 0 || strncmp(tmp, "sec", 3) == 0) {
if (!adjsec(t, type, val, 1))
return v;
} else if (strcmp(tmp, "M") == 0 || strncmp(tmp, "min", 3) == 0) {
if (!adjmin(t, type, val, 1))
return v;
} else if (strcmp(tmp, "H") == 0 || strncmp(tmp, "hour", 4) == 0) {
if (!adjhour(t, type, val, 1))
return v;
} else if (strcmp(tmp, "d") == 0 || strncmp(tmp, "day", 3) == 0) {
t->tm_isdst = -1;
if (!adjday(t, type, val, 1))
return v;
} else if (strcmp(tmp, "w") == 0 || strncmp(tmp, "week", 4) == 0) {
t->tm_isdst = -1;
if (!adjwday(t, type, val, 0, 1))
return v;
} else if (strcmp(tmp, "m") == 0 || strncmp(tmp, "mon", 3) == 0) {
t->tm_isdst = -1;
if (!adjmon(t, type, val, 0, 1))
return v;
} else if (strcmp(tmp, "y") == 0 || strncmp(tmp, "year", 4) == 0) {
t->tm_isdst = -1;
if (!adjyear(t, type, val, 1))
return v;
} else {
return v;
}
continue;
}
if ((val = trans(trans_wday, arg)) != -1) {
if (!adjwday(t, type, val, 1, 1))
return v;
while (isalpha(*arg))
++arg;
while (isspace(arg[0]))
++arg;
}
if ((val = trans(trans_mon, arg)) != -1) {
if (!adjmon(t, type, val, 1, 1))
return v;
while (isalpha(*arg))
++arg;
while (isspace(*arg))
++arg;
val = strtoul(arg, &tmp, 10);
if (tmp != arg && (isspace(*tmp) || *tmp == 0)) {
t->tm_isdst = -1;
if (!adjday(t, 0, val, 1))
return v;
arg = tmp;
while (isspace(arg[0]))
++arg;
}
}
if (doyear) {
val = strtol(arg, &tmp, 10);
if (tmp != arg && (*tmp == 0 || isspace(*tmp))) {
if (!adjyear(t, type, val, 1))
return v;
arg = tmp;
doyear = 0;
while (isspace(arg[0]))
++arg;
}
}
val = strtol(arg, &tmp, 10);
if (dohms && tmp != arg && *tmp == ':') {
int hr = -1;
int mi = -1;
int se = -1;
char zflag = 0;
sscanf(arg, "%d:%d:%d%c", &hr, &mi, &se, &zflag);
if (hr >= 0) {
if (!adjhour(t, type, hr, 1))
return v;
}
if (mi >= 0) {
if (!adjmin(t, type, mi, 1))
return v;
}
if (se >= 0) {
if (!adjsec(t, type, se, 1))
return v;
}
arg = tmp;
dohms = 0;
if (zflag) {
}
while (arg[0] && !isspace(arg[0]))
++arg;
while (isspace(arg[0]))
++arg;
}
if (doyear) {
val = strtol(arg, &tmp, 10);
if (tmp != arg && *tmp == 0) {
if (!adjyear(t, type, val, 1))
return v;
arg = tmp;
doyear = 0;
while (isspace(arg[0]))
++arg;
}
}
if (dotzone) {
}
#if 0
val = strtol(arg, NULL, 10);
which = arg[len-1];
#endif
}
return 0;
}
void
vary_destroy(struct vary *v)
{
struct vary *n;
while (v) {
n = v->next;
free(v);
v = n;
}
}