1
0
Fork 0
mirror of https://github.com/pygos/cron synced 2024-12-22 09:20:49 +01:00

Simplify interval parsing

Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
This commit is contained in:
David Oberhollenzer 2018-11-24 14:02:34 +01:00
parent a4791171cd
commit 1ebed3157a

View file

@ -38,7 +38,7 @@ static const struct {
crontab_t tab; crontab_t tab;
} intervals[] = { } intervals[] = {
{ {
.macro = "yearly", .macro = "@yearly",
.tab = { .tab = {
.minute = 0x01, .minute = 0x01,
.hour = 0x01, .hour = 0x01,
@ -47,7 +47,7 @@ static const struct {
.dayofweek = 0xFF .dayofweek = 0xFF
}, },
}, { }, {
.macro = "annually", .macro = "@annually",
.tab = { .tab = {
.minute = 0x01, .minute = 0x01,
.hour = 0x01, .hour = 0x01,
@ -56,7 +56,7 @@ static const struct {
.dayofweek = 0xFF .dayofweek = 0xFF
}, },
}, { }, {
.macro = "monthly", .macro = "@monthly",
.tab = { .tab = {
.minute = 0x01, .minute = 0x01,
.hour = 0x01, .hour = 0x01,
@ -65,7 +65,7 @@ static const struct {
.dayofweek = 0xFF .dayofweek = 0xFF
}, },
}, { }, {
.macro = "weekly", .macro = "@weekly",
.tab = { .tab = {
.minute = 0x01, .minute = 0x01,
.hour = 0x01, .hour = 0x01,
@ -74,7 +74,7 @@ static const struct {
.dayofweek = 0x01 .dayofweek = 0x01
}, },
}, { }, {
.macro = "daily", .macro = "@daily",
.tab = { .tab = {
.minute = 0x01, .minute = 0x01,
.hour = 0x01, .hour = 0x01,
@ -83,7 +83,7 @@ static const struct {
.dayofweek = 0xFF .dayofweek = 0xFF
}, },
}, { }, {
.macro = "hourly", .macro = "@hourly",
.tab = { .tab = {
.minute = 0x01, .minute = 0x01,
.hour = 0xFFFFFFFF, .hour = 0xFFFFFFFF,
@ -209,34 +209,27 @@ fail:
static char *cron_interval(crontab_t *cron, rdline_t *rd) static char *cron_interval(crontab_t *cron, rdline_t *rd)
{ {
char *arg = rd->line;
size_t i, j; size_t i, j;
if (*(arg++) != '@') for (j = 1; isalpha(rd->line[j]); ++j)
goto fail;
for (j = 0; isalpha(arg[j]); ++j)
; ;
if (j == 0 || !isspace(arg[j])) if (j == 1 || !isspace(rd->line[j]))
goto fail; goto fail;
for (i = 0; i < ARRAY_SIZE(intervals); ++i) { for (i = 0; i < ARRAY_SIZE(intervals); ++i) {
if (strlen(intervals[i].macro) != j) if (strlen(intervals[i].macro) != j)
continue; continue;
if (strncmp(intervals[i].macro, arg, j) == 0) if (strncmp(intervals[i].macro, rd->line, j) == 0)
break; break;
} }
if (i == ARRAY_SIZE(intervals)) if (i == ARRAY_SIZE(intervals))
goto fail; goto fail;
cron->minute = intervals[i].tab.minute; *cron = intervals[i].tab;
cron->hour = intervals[i].tab.hour; return rd->line + j;
cron->dayofmonth = intervals[i].tab.dayofmonth;
cron->month = intervals[i].tab.month;
cron->dayofweek = intervals[i].tab.dayofweek;
return arg + j;
fail: fail:
rdline_complain(rd, "unknown interval '%s'", arg); rdline_complain(rd, "unknown interval '%.*s'", (int)j, rd->line);
return NULL; return NULL;
} }