libvieter/src/cron/parse.c

334 lines
8.4 KiB
C
Raw Normal View History

2023-01-18 11:29:16 +01:00
#include "vieter_cron.h"
2023-01-17 23:09:06 +01:00
// This prefix is needed to properly compile
const uint8_t parse_month_days[] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
2023-01-17 23:09:06 +01:00
// Allowed value ranges for the minute, hour, day and month field
const uint8_t min[4] = {0, 0, 1, 1};
const uint8_t max[4] = {59, 23, 31, 12};
const uint8_t min_parts = 2;
const uint8_t max_parts = 4;
// Convert a string into 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) \
int _##v = atoi(s); \
if ((_##v) == 0 && strcmp((s), "0") != 0) { \
return cron_parse_invalid_number; \
} \
if (((_##v) < (min)) || ((_##v) > (max))) { \
return cron_parse_out_of_range; \
} \
v = (uint8_t)(_##v);
2023-01-17 23:09:06 +01:00
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
/**
* Given a range expression, produce a bit field defining what numbers in the
* min-max range the expression represents. Bit 0 (starting from the
* 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
* multiple calls of this function can be performed on the same value to create
* the effect of ORing their values.
*
* A range expression has one of the following forms:
*
* - *
* - a
* - a-b
* - a/c
* - a-b/c
*/
cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min,
uint8_t max) {
size_t slash_index = 0, dash_index = 0;
size_t s_index = 0;
char cur_char;
bool is_valid_character;
while ((cur_char = s[s_index]) != '\0') {
is_valid_character = cur_char == '/' || cur_char == '-' ||
cur_char == '*' ||
(cur_char >= '0' && cur_char <= '9');
if (!is_valid_character) {
return cron_parse_invalid_expression;
}
if (cur_char == '/') {
if (s_index == 0 || slash_index != 0) {
return cron_parse_invalid_expression;
}
slash_index = s_index;
s[s_index] = '\0';
} else if (cur_char == '-') {
// At most one dash is allowed, and it must be before the slash
if (s_index == 0 || dash_index != 0 || slash_index != 0) {
return cron_parse_invalid_expression;
}
dash_index = s_index;
s[s_index] = '\0';
}
s_index++;
}
uint8_t start;
uint8_t end = max;
uint8_t interval = 0;
if (s[0] == '*') {
if (s[1] != '\0' || dash_index != 0) {
return cron_parse_invalid_expression;
}
start = min;
interval = 1;
} else {
SAFE_ATOI(start, s, min, max);
if (dash_index > 0) {
SAFE_ATOI(end, &s[dash_index + 1], min, max);
interval = 1;
}
}
if (slash_index > 0) {
SAFE_ATOI(interval, &s[slash_index + 1], 1, max - min);
}
if (interval == 0) {
*out |= ((uint64_t)1) << (start - min);
} else {
while (start <= end) {
*out |= ((uint64_t)1) << (start - min);
start += interval;
}
}
return cron_parse_ok;
2023-01-17 23:09:06 +01:00
}
/*
* 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.
*/
cron_parse_error ce_parse_part(uint64_t *out, char *s, uint8_t min,
uint8_t max) {
*out = 0;
2023-01-17 23:09:06 +01:00
char *next;
cron_parse_error res;
2023-01-17 23:09:06 +01:00
while ((next = strchr(s, ',')) != NULL) {
next[0] = '\0';
2023-01-17 23:09:06 +01:00
res = ce_parse_range(out, s, min, max);
2023-01-17 23:09:06 +01:00
if (res != cron_parse_ok) {
return res;
}
2023-01-17 23:09:06 +01:00
s = next + 1;
}
2023-01-17 23:09:06 +01:00
// Make sure to parse the final range as well
return ce_parse_range(out, s, min, max);
2023-01-17 23:09:06 +01:00
}
/*
* 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 set_bits = 0;
2023-01-17 23:09:06 +01:00
while (n != 0) {
// This sets the least significant bit to zero (very cool)
n &= n - 1;
2023-01-17 23:09:06 +01:00
set_bits++;
}
2023-01-17 23:09:06 +01:00
return set_bits;
2023-01-17 23:09:06 +01:00
}
/*
* 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) {
// Each bit field only has `max - min + 1` meaningful bits. All other bits
// 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;
2023-01-17 23:09:06 +01:00
uint8_t size = uint64_t_popcount(bf);
uint8_t *buf = malloc(size * sizeof(uint8_t));
2023-01-17 23:09:06 +01:00
uint8_t bit_index = 0, buf_index = 0;
2023-01-17 23:09:06 +01:00
while (buf_index < size && bit_index <= max - min) {
if (((uint64_t)1 << bit_index) & bf) {
// Resize buffer if needed
buf[buf_index] = min + bit_index;
buf_index++;
}
2023-01-17 23:09:06 +01:00
bit_index++;
}
2023-01-17 23:09:06 +01:00
*out = buf;
2023-01-17 23:09:06 +01:00
return size;
2023-01-17 23:09:06 +01:00
}
/*
* Parse a cron expression string into a cron_expression struct.
*/
cron_parse_error ce_parse_expression(cron_expression *out,
const char *expression) {
// The parsing functions modify the input string in-place
char *s = strdup(expression);
char *orig_s = s;
2023-01-17 23:09:06 +01:00
cron_parse_error res = cron_parse_ok;
2023-01-17 23:09:06 +01:00
// First we divide the input string into its parts, divided by spaces.
// Each part is delimited by a NULL byte.
uint8_t part_count = 0;
char *parts[max_parts];
char *next_space;
2023-01-17 23:09:06 +01:00
// Skip leading spaces
size_t offset = 0;
2023-01-17 23:09:06 +01:00
while (s[offset] == ' ') {
offset++;
}
2023-01-17 23:09:06 +01:00
s += offset;
2023-01-17 23:09:06 +01:00
while (part_count < max_parts && ((next_space = strchr(s, ' ')) != NULL)) {
next_space[0] = '\0';
2023-01-17 23:09:06 +01:00
parts[part_count] = s;
part_count++;
2023-01-17 23:09:06 +01:00
// Skip multiple spaces
offset = 1;
while (next_space[offset] == ' ') {
offset++;
}
s = next_space + offset;
}
2023-01-17 23:09:06 +01:00
// Each iteration of the loop skips all trailing spaces. This means that, if
// s[0] isn't '\0', there's still another part before the end of the string.
if (s[0] != '\0') {
if (part_count == max_parts) {
res = cron_parse_too_many_parts;
goto end;
}
2023-01-17 23:09:06 +01:00
parts[part_count] = s;
part_count++;
}
2023-01-17 23:09:06 +01:00
if (part_count < min_parts) {
res = cron_parse_not_enough_parts;
goto end;
}
2023-01-17 23:09:06 +01:00
// We now parse the parts in reverse. This is because the month part
// determines the maximum value of the day part.
2023-01-17 23:09:06 +01:00
uint64_t bit_field = 0;
2023-01-17 23:09:06 +01:00
// Months
if (part_count >= 4) {
res = ce_parse_part(&bit_field, parts[3], min[3], max[3]);
2023-01-17 23:09:06 +01:00
if (res != cron_parse_ok) {
goto end;
}
2023-01-17 23:09:06 +01:00
out->month_count = bf_to_nums(&out->months, bit_field, min[3], max[3]);
}
// If months aren't provided, they're replaced with a *
else {
out->month_count = bf_to_nums(&out->months, ~0, min[3], max[3]);
}
2023-01-17 23:09:06 +01:00
// Determine what the largest allowed day value is, given the months
uint8_t max_day_value = 0;
2023-01-17 23:09:06 +01:00
for (uint8_t i = 0; i < out->month_count; i++) {
max_day_value = MAX(max_day_value, parse_month_days[out->months[i] - 1]);
}
2023-01-17 23:09:06 +01:00
// Days
if (part_count >= 3) {
bit_field = 0;
2023-01-17 23:09:06 +01:00
res = ce_parse_part(&bit_field, parts[2], min[2], max_day_value);
2023-01-17 23:09:06 +01:00
if (res != cron_parse_ok) {
free(out->months);
2023-01-17 23:09:06 +01:00
goto end;
}
2023-01-17 23:09:06 +01:00
out->day_count = bf_to_nums(&out->days, bit_field, min[2], max_day_value);
}
// If days aren't provided, they're replaced with a *
else {
out->day_count = bf_to_nums(&out->days, ~0, min[2], max_day_value);
}
2023-01-17 23:09:06 +01:00
// Hours
bit_field = 0;
2023-01-17 23:09:06 +01:00
res = ce_parse_part(&bit_field, parts[1], min[1], max[1]);
2023-01-17 23:09:06 +01:00
if (res != cron_parse_ok) {
free(out->months);
free(out->days);
2023-01-17 23:09:06 +01:00
goto end;
}
2023-01-17 23:09:06 +01:00
out->hour_count = bf_to_nums(&out->hours, bit_field, min[1], max[1]);
2023-01-17 23:09:06 +01:00
// Minutes
bit_field = 0;
2023-01-17 23:09:06 +01:00
res = ce_parse_part(&bit_field, parts[0], min[0], max[0]);
2023-01-17 23:09:06 +01:00
if (res != cron_parse_ok) {
free(out->months);
free(out->days);
free(out->hours);
2023-01-17 23:09:06 +01:00
goto end;
}
out->minute_count = bf_to_nums(&out->minutes, bit_field, min[0], max[0]);
2023-01-17 23:09:06 +01:00
end:
// s is cloned
free(orig_s);
2023-01-17 23:09:06 +01:00
return res;
2023-01-17 23:09:06 +01:00
}