feat(cron): some bug fixes & formatting

Jef Roosens 2023-01-14 15:03:11 +01:00
parent c2e6d168e5
commit dce00bfab6
5 changed files with 266 additions and 238 deletions

View File

@ -5,6 +5,5 @@ root = true
end_of_line = lf end_of_line = lf
insert_final_newline = true insert_final_newline = true
[*.v] [*.{v,c,h}]
# vfmt wants it :(
indent_style = tab indent_style = tab

View File

@ -4,15 +4,15 @@
const uint8_t month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const uint8_t month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
struct cron_expression *ce_init() { struct cron_expression *ce_init() {
return malloc(sizeof(struct cron_expression)); return malloc(sizeof(struct cron_expression));
} }
void ce_free(struct cron_expression *ce) { void ce_free(struct cron_expression *ce) {
free(ce->months); free(ce->months);
free(ce->days); free(ce->days);
free(ce->hours); free(ce->hours);
free(ce->minutes); free(ce->minutes);
free(ce); free(ce);
} }
int ce_next(struct cron_simple_time *out, struct cron_expression *ce, struct cron_simple_time *ref) { int ce_next(struct cron_simple_time *out, struct cron_expression *ce, struct cron_simple_time *ref) {
@ -21,100 +21,101 @@ int ce_next(struct cron_simple_time *out, struct cron_expression *ce, struct cro
// means we've looped back around. This means that the "bigger" value has // means we've looped back around. This means that the "bigger" value has
// to be incremented by one. For example, if the minutes have looped // to be incremented by one. For example, if the minutes have looped
// around, that means that the hour has to be incremented as well. // around, that means that the hour has to be incremented as well.
uint8_t month_index = 0; uint8_t month_index = 0;
uint8_t day_index = 0; uint8_t day_index = 0;
uint8_t hour_index = 0; uint8_t hour_index = 0;
uint8_t minute_index = 0; uint8_t minute_index = 0;
// This chain is the same logic multiple times, namely that if a "bigger" // This chain is the same logic multiple times, namely that if a "bigger"
// value loops around, then the smaller value will always reset as well. // value loops around, then the smaller value will always reset as well.
// For example, if we're going to a new day, the hour & minute will always // For example, if we're going to a new day, the hour & minute will always
// be their smallest value again. // be their smallest value again.
while (month_index < ce->month_count && ref->month > ce->months[month_index]) { while (month_index < ce->month_count && ref->month > ce->months[month_index]) {
month_index++; month_index++;
} }
if (month_index < ce->month_count && ref->month == ce->months[month_index]) { if (month_index < ce->month_count && ref->month == ce->months[month_index]) {
while (day_index < ce->day_count && ref->day > ce->days[day_index]) { while (day_index < ce->day_count && ref->day > ce->days[day_index]) {
day_index++; day_index++;
} }
if (day_index < ce->day_count && ref->day == ce->days[day_index]) { if (day_index < ce->day_count && ref->day == ce->days[day_index]) {
while (hour_index < ce->hour_count && ref->hour > ce->hours[hour_index]) { while (hour_index < ce->hour_count && ref->hour > ce->hours[hour_index]) {
hour_index++; hour_index++;
} }
if (hour_index < ce->hour_count && ref->hour == ce->hours[hour_index]) { if (hour_index < ce->hour_count && ref->hour == ce->hours[hour_index]) {
// Minute is the only value where we explicitely make sure we // Minute is the only value where we explicitely make sure we
// can't match sref's value exactly. This is to ensure we only // can't match sref's value exactly. This is to ensure we only
// return values in the future. // return values in the future.
while (minute_index < ce->minute_count && ref->minute >= ce->minutes[minute_index]) { while (minute_index < ce->minute_count && ref->minute >= ce->minutes[minute_index]) {
minute_index++; minute_index++;
} }
} }
} }
} }
// Here, we increment the "bigger" values by one if the smaller ones loop // Here, we increment the "bigger" values by one if the smaller ones loop
// around. The order is important, as it allows a sort-of waterfall effect // around. The order is important, as it allows a sort-of waterfall effect
// to occur which updates all values if required. // to occur which updates all values if required.
if (minute_index == ce->minute_count && hour_index < ce->hour_count) { if (minute_index == ce->minute_count && hour_index < ce->hour_count) {
hour_index++; hour_index++;
} }
if (hour_index == ce->hour_count && day_index < ce->day_count) { if (hour_index == ce->hour_count && day_index < ce->day_count) {
day_index++; day_index++;
} }
if (day_index == ce->day_count && month_index < ce->month_count) { if (day_index == ce->day_count && month_index < ce->month_count) {
month_index++; month_index++;
} }
out->minute = ce->minutes[minute_index % ce->minute_count]; out->minute = ce->minutes[minute_index % ce->minute_count];
out->hour = ce->hours[hour_index % ce->hour_count]; out->hour = ce->hours[hour_index % ce->hour_count];
out->day = ce->days[day_index % ce->day_count]; out->day = ce->days[day_index % ce->day_count];
// Sometimes, we end up with a day that does not exist within the selected // Sometimes, we end up with a day that does not exist within the selected
// month, e.g. day 30 in February. When this occurs, we reset day back to // month, e.g. day 30 in February. When this occurs, we reset day back to
// the smallest value & loop over to the next month that does have this // the smallest value & loop over to the next month that does have this
// day. // day.
if (out->day > month_days[ce->months[month_index % ce->month_count] - 1]) { if (out->day > month_days[ce->months[month_index % ce->month_count] - 1]) {
out->day = ce->days[0]; out->day = ce->days[0];
month_index++; month_index++;
while (out->day > month_days[ce->months[month_index % ce->month_count] - 1]) { while (out->day > month_days[ce->months[month_index % ce->month_count] - 1]) {
month_index++; month_index++;
if (month_index == 2 * ce->month_count) {
return 1;
}
}
}
out->month = ce->months[month_index % ce->month_count]; // TODO find out if this can happen
if (month_index == 2 * ce->month_count) {
return 1;
}
}
}
if (month_index >= ce->month_count) { out->month = ce->months[month_index % ce->month_count];
out->year = ref->year + 1;
} else {
out->year = ref->year;
}
return 0; if (month_index >= ce->month_count) {
out->year = ref->year + 1;
} else {
out->year = ref->year;
}
return 0;
} }
int ce_next_from_now(struct cron_simple_time *out, struct cron_expression *ce) { int ce_next_from_now(struct cron_simple_time *out, struct cron_expression *ce) {
time_t t = time(NULL); time_t t = time(NULL);
struct tm gm; struct tm gm;
gmtime_r(&t, &gm); gmtime_r(&t, &gm);
struct cron_simple_time ref = { struct cron_simple_time ref = {
.year = gm.tm_year, .year = gm.tm_year,
// tm_mon goes from 0 to 11 // tm_mon goes from 0 to 11
.month = gm.tm_mon + 1, .month = gm.tm_mon + 1,
.day = gm.tm_mday, .day = gm.tm_mday,
.hour = gm.tm_hour, .hour = gm.tm_hour,
.minute = gm.tm_min .minute = gm.tm_min
}; };
return ce_next(out, ce, &ref); return ce_next(out, ce, &ref);
} }

View File

@ -4,29 +4,29 @@
#include <string.h> #include <string.h>
enum cron_parse_error { enum cron_parse_error {
CPEParseOk = 0, CPEParseOk = 0,
CPEParseInvalidExpression = 1, CPEParseInvalidExpression = 1,
CPEParseInvalidNumber = 2, CPEParseInvalidNumber = 2,
CPEParseOutOfRange = 3 CPEParseOutOfRange = 3
}; };
struct cron_expression { struct cron_expression {
uint8_t *minutes; uint8_t *minutes;
uint8_t *hours; uint8_t *hours;
uint8_t *days; uint8_t *days;
uint8_t *months; uint8_t *months;
uint8_t minute_count; uint8_t minute_count;
uint8_t hour_count; uint8_t hour_count;
uint8_t day_count; uint8_t day_count;
uint8_t month_count; uint8_t month_count;
}; };
struct cron_simple_time { struct cron_simple_time {
int year; int year;
int month; int month;
int day; int day;
int hour; int hour;
int minute; int minute;
}; };
struct cron_expression *ce_init(); struct cron_expression *ce_init();

View File

@ -1,25 +1,28 @@
#include "expression.h" #include "expression.h"
// Allowed value ranges for the minute, hour, day and month field
const uint8_t min[4] = {0, 0, 1, 1}; const uint8_t min[4] = {0, 0, 1, 1};
const uint8_t max[4] = {59, 23, 31, 12}; const uint8_t max[4] = {59, 23, 31, 12};
// Convert a string a uint8_t value by parsing it using atoi and checking
// whether it's contained within the given range
#define SAFE_ATOI(v,s,min,max) \ #define SAFE_ATOI(v,s,min,max) \
int _##v = atoi(s); \ int _##v = atoi(s); \
if ((_##v) == 0 && strcmp((s), "0") != 0) { \ if ((_##v) == 0 && strcmp((s), "0") != 0) { \
return CPEParseInvalidNumber; \ return CPEParseInvalidNumber; \
} \ } \
if (v < (min) || v > (max)) { \ if (v < (min) || v > (max)) { \
return CPEParseOutOfRange; \ return CPEParseOutOfRange; \
} \ } \
v = (uint8_t) (_##v); v = (uint8_t) (_##v);
/** /**
* Given a range expression, produce a bit field defining what numbers in the * Given a range expression, produce a bit field defining what numbers in the
* min-max range the expression represents. The first bit (starting from the * min-max range the expression represents. Bit 0 (starting from the
* right) corresponds to min, the max - min + 1'th bit to max. All trailing bits * right) corresponds to min, the bit max - min to max. All trailing bits
* after this should be ignored. The given bitfield is modified in-place, so * after this should be ignored. The given bitfield is modified in-place, so
* multiple calls of this function can be performed on the same value to create * multiple calls of this function can be performed on the same value to create
* the effect of ORing their values: * the effect of ORing their values.
* *
* A range expression has one of the following forms: * A range expression has one of the following forms:
* *
@ -30,185 +33,210 @@ const uint8_t max[4] = {59, 23, 31, 12};
* - a-b/c * - a-b/c
*/ */
enum cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_t max) { enum cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_t max) {
// The * expression means "every possible value" // The * expression means "every possible value"
if (s[0] == '*') { if (s[0] == '*') {
// A '*' is only valid on its own // A '*' is only valid on its own
if (s[1] != '\0') { if (s[1] != '\0') {
return CPEParseInvalidExpression; return CPEParseInvalidExpression;
} }
*out = ~0; *out = ~0;
return CPEParseOk; return CPEParseOk;
} }
size_t slash_index = 0; size_t slash_index = 0;
size_t dash_index = 0; size_t dash_index = 0;
size_t i = 0; size_t i = 0;
// We first iterate over the string to determine whether it contains a slash // We first iterate over the string to determine whether it contains a slash
// and/or a dash. We know the dash can only be valid if it appears before // and/or a dash. We know the dash can only be valid if it appears before
// the slash. // the slash.
while (s[i] != '\0' && slash_index == 0) { while (s[i] != '\0' && slash_index == 0) {
if (s[i] == '/') { if (s[i] == '/') {
slash_index = i; slash_index = i;
s[i] = '\0'; s[i] = '\0';
} else if (s[i] == '-') { } else if (s[i] == '-') {
dash_index = i; dash_index = i;
s[i] = '\0'; s[i] = '\0';
} }
i++; i++;
} }
// Parse the three possible numbers in the pattern // Parse the three possible numbers in the pattern
uint8_t start = 0; uint8_t start = 0;
uint8_t end = 0; uint8_t end = max;
uint8_t interval = 1; uint8_t interval = 1;
SAFE_ATOI(start, s, min, max); SAFE_ATOI(start, s, min, max);
if (dash_index > 0) { if (dash_index > 0) {
SAFE_ATOI(end, &s[dash_index + 1], min, max); SAFE_ATOI(end, &s[dash_index + 1], min, max);
} }
if (slash_index > 0) { if (slash_index > 0) {
SAFE_ATOI(interval, &s[slash_index + 1], 1, max - min); SAFE_ATOI(interval, &s[slash_index + 1], 1, max - min);
} }
// Single number doesn't need to loop if (dash_index == 0 && slash_index == 0) {
if (end == 0 && slash_index == 0) { *out |= ((uint64_t) 1) << (start - min);
*out |= ((uint64_t) 1) << (start - min); } else {
} else { while (start <= end) {
for (;start <= end; start += interval) { *out |= ((uint64_t) 1) << (start - min);
*out |= ((uint64_t) 1) << (start - min); start += interval;
start += interval; }
} }
}
return CPEParseOk; return CPEParseOk;
} }
/*
* Given an expression part, produce a bitfield defining what numbers in the
* min-max range the part represents. A part consists of one or more range
* expressions, separated by commas.
*/
enum cron_parse_error ce_parse_part(uint64_t *out, char *s, uint8_t min, uint8_t max) { enum cron_parse_error ce_parse_part(uint64_t *out, char *s, uint8_t min, uint8_t max) {
*out = 0; *out = 0;
char *next; char *next;
enum cron_parse_error res; enum cron_parse_error res;
while ((next = strchr(s, ',')) != NULL) {
next[0] = '\0';
res = ce_parse_range(out, s, min, max);
if (res != CPEParseOk) { while ((next = strchr(s, ',')) != NULL) {
return res; next[0] = '\0';
} res = ce_parse_range(out, s, min, max);
s = next + 1; if (res != CPEParseOk) {
} return res;
}
// Make sure to parse the final range as well s = next + 1;
return ce_parse_range(out, s, min, max); }
// Make sure to parse the final range as well
return ce_parse_range(out, s, min, max);
} }
/*
* Return how many bits are set in the bitfield, better known as popcount. I
* added my own implementation (taken from my algorithms course) as I don't want
* to be dependent on GCC-specific extensions.
*/
uint8_t uint64_t_popcount(uint64_t n) {
uint8_t c = 0;
while (n != 0) {
// This sets the least significant bit to zero (very cool)
n &= n - 1;
c++;
}
return c;
}
/*
* Convert a bitfield into an array containing the numbers in the min-max range
* it represents.
*/
uint8_t bf_to_nums(uint8_t **out, uint64_t bf, uint8_t min, uint8_t max) { uint8_t bf_to_nums(uint8_t **out, uint64_t bf, uint8_t min, uint8_t max) {
uint8_t capacity = 8; // Each bit field only has `max - min + 1` meaningful bits. All other bits
uint8_t size = 0; // should be ignored, and can be any value. By shifting the bit field back and
// forth, we set these excessive bits to zero, ensuring popcount returns the
// correct value.
uint8_t excess_bits = 64 - (max - min + 1);
bf = (bf << excess_bits) >> excess_bits;
uint8_t size = uint64_t_popcount(bf);
uint8_t *buf = malloc(size * sizeof(uint8_t));
uint8_t *buf = malloc(capacity * sizeof(uint8_t)); uint8_t i = 0, j = 0;
for (uint8_t i = 0; i <= max - min; i++) { while (j < size && i <= max - min) {
if (((uint64_t) 1 << i) & bf) { if (((uint64_t)1 << i) & bf) {
// Resize buffer if needed // Resize buffer if needed
if (size == capacity) { buf[j] = min + i;
capacity *= 2; j++;
buf = realloc(buf, capacity * sizeof(uint8_t)); }
}
buf[size] = min + i; i++;
size++; }
}
}
// Resize buffer once more to remove any trailing unused bytes *out = buf;
if (size < capacity) {
buf = realloc(buf, size * sizeof(uint8_t));
}
*out = buf; return size;
return size;
} }
/*
* Parse a cron expression string into a cron_expression struct.
*/
enum cron_parse_error ce_parse_expression(struct cron_expression *out, char *s) { enum cron_parse_error ce_parse_expression(struct cron_expression *out, char *s) {
// The parsing functions modify the input string in-place // The parsing functions modify the input string in-place
s = strdup(s); s = strdup(s);
char *orig_s = s; char *orig_s = s;
uint8_t part_count = 0; uint8_t part_count = 0;
char *next; char *next;
enum cron_parse_error res = CPEParseOk; enum cron_parse_error res = CPEParseOk;
uint64_t bfs[4]; uint64_t bfs[4];
// Skip leading spaces // Skip leading spaces
while (s[0] == ' ') { while (s[0] == ' ') {
s++; s++;
} }
while (part_count < 4 && ((next = strchr(s, ' ')) != NULL)) {
next[0] = '\0';
res = ce_parse_part(&bfs[part_count], s, min[part_count], max[part_count]);
if (res != CPEParseOk) { while (part_count < 4 && ((next = strchr(s, ' ')) != NULL)) {
goto end; next[0] = '\0';
} res = ce_parse_part(&bfs[part_count], s, min[part_count], max[part_count]);
size_t offset = 1; if (res != CPEParseOk) {
goto end;
}
// Skip multiple spaces size_t offset = 1;
while (next[offset] == ' ') {
offset++;
}
s = next + offset;
part_count++; // Skip multiple spaces
} while (next[offset] == ' ') {
offset++;
}
s = next + offset;
// Parse final trailing part part_count++;
if (part_count < 4 && s[0] != '\0') { }
// Make sure to parse the final range as well
res = ce_parse_part(&bfs[part_count], s, min[part_count], max[part_count]);
if (res != CPEParseOk) { // Parse final trailing part
goto end; if (part_count < 4 && s[0] != '\0') {
} res = ce_parse_part(&bfs[part_count], s, min[part_count], max[part_count]);
part_count++; if (res != CPEParseOk) {
} goto end;
}
// At least two parts need to be provided part_count++;
if (part_count < 2) { }
res = CPEParseInvalidExpression;
goto end;
}
// Ensure there's always 4 parts, as expressions can have between 2 and 4 parts // At least two parts need to be provided
while (part_count < 4) { if (part_count < 2) {
// Expression is augmented with '*' expressions res = CPEParseInvalidExpression;
bfs[part_count] = ~0; goto end;
part_count++; }
}
out->minute_count = bf_to_nums(&out->minutes, bfs[0], min[0], max[0]); // Ensure there's always 4 parts, as expressions can have between 2 and 4 parts
out->hour_count = bf_to_nums(&out->hours, bfs[1], min[1], max[1]); while (part_count < 4) {
out->day_count = bf_to_nums(&out->days, bfs[2], min[2], max[2]); // Expression is augmented with '*' expressions
out->month_count = bf_to_nums(&out->months, bfs[3], min[3], max[3]); bfs[part_count] = ~0;
part_count++;
}
out->minute_count = bf_to_nums(&out->minutes, bfs[0], min[0], max[0]);
out->hour_count = bf_to_nums(&out->hours, bfs[1], min[1], max[1]);
out->day_count = bf_to_nums(&out->days, bfs[2], min[2], max[2]);
out->month_count = bf_to_nums(&out->months, bfs[3], min[3], max[3]);
end: end:
// s is cloned // s is cloned
free(orig_s); free(orig_s);
return res; return res;
} }

View File

@ -22,15 +22,15 @@ fn test_next_simple() ! {
/* util_test_time('0 3', '2002-01-01 00:00:00', '2002-01-01 03:00:00')! */ /* util_test_time('0 3', '2002-01-01 00:00:00', '2002-01-01 03:00:00')! */
// Overlap to next day // Overlap to next day
mut exp := '0 3' mut exp := '0 3 '
util_test_time(exp, '2002-01-01 03:00:00', '2002-01-02 03:00:00')! util_test_time(exp, '2002-01-01 03:00:00', '2002-01-02 03:00:00')!
util_test_time(exp, '2002-01-01 04:00:00', '2002-01-02 03:00:00')! util_test_time(exp, '2002-01-01 04:00:00', '2002-01-02 03:00:00')!
/* util_test_time('0 3/4', '2002-01-01 04:00:00', '2002-01-01 07:00:00')! */ util_test_time('0 3/4', '2002-01-01 04:00:00', '2002-01-01 07:00:00')!
/* // Overlap to next month */ /* // Overlap to next month */
/* util_test_time('0 3', '2002-11-31 04:00:00', '2002-12-01 03:00:00')! */ util_test_time('0 3', '2002-11-31 04:00:00', '2002-12-01 03:00:00')!
/* // Overlap to next year */ /* // Overlap to next year */
/* util_test_time('0 3', '2002-12-31 04:00:00', '2003-01-01 03:00:00')! */ util_test_time('0 3', '2002-12-31 04:00:00', '2003-01-01 03:00:00')!
} }