chore: switch to defualt clang-format formatting

main
Jef Roosens 2023-01-18 11:38:34 +01:00
parent 4d72e60515
commit be97472dcc
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 319 additions and 314 deletions

View File

@ -5,6 +5,7 @@ SRC_DIR ?= src
INC_DIRS ?= include INC_DIRS ?= include
SRCS != find '$(SRC_DIR)' -iname '*.c' SRCS != find '$(SRC_DIR)' -iname '*.c'
SRCS_H != find $(INC_DIRS) -iname '*.h'
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o) OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(SRCS:%=$(BUILD_DIR)/%.d) DEPS := $(SRCS:%=$(BUILD_DIR)/%.d)
@ -32,6 +33,15 @@ $(BUILD_DIR)/%.c.o: %.c
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) -c $< -o $@
# =====MAINTENANCE=====
.PHONY: lint
lint:
clang-format -n --Werror $(SRCS) $(SRCS_H)
.PHONY: fmt
fmt:
clang-format -i $(SRCS) $(SRCS_H)
.PHONY: clean .PHONY: clean
clean: clean:
rm -rf $(BUILD_DIR) rm -rf $(BUILD_DIR)

View File

@ -8,31 +8,31 @@
#include <time.h> #include <time.h>
typedef enum cron_parse_error { typedef enum cron_parse_error {
cron_parse_ok = 0, cron_parse_ok = 0,
cron_parse_invalid_expression = 1, cron_parse_invalid_expression = 1,
cron_parse_invalid_number = 2, cron_parse_invalid_number = 2,
cron_parse_out_of_range = 3, cron_parse_out_of_range = 3,
cron_parse_too_many_parts = 4, cron_parse_too_many_parts = 4,
cron_parse_not_enough_parts = 5 cron_parse_not_enough_parts = 5
} cron_parse_error; } cron_parse_error;
typedef struct cron_expression { typedef 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;
} cron_expression; } cron_expression;
typedef struct cron_simple_time { typedef struct cron_simple_time {
int year; int year;
int month; int month;
int day; int day;
int hour; int hour;
int minute; int minute;
} cron_simple_time; } cron_simple_time;
cron_expression *ce_init(); cron_expression *ce_init();
@ -40,11 +40,11 @@ cron_expression *ce_init();
void cron_ce_free(cron_expression *ce); void cron_ce_free(cron_expression *ce);
void cron_ce_next(cron_simple_time *out, cron_expression *ce, void cron_ce_next(cron_simple_time *out, cron_expression *ce,
cron_simple_time *ref); cron_simple_time *ref);
void cron_ce_next_from_now(cron_simple_time *out, cron_expression *ce); void cron_ce_next_from_now(cron_simple_time *out, cron_expression *ce);
enum cron_parse_error cron_ce_parse_expression(cron_expression *out, enum cron_parse_error cron_ce_parse_expression(cron_expression *out,
const char *expression); const char *expression);
#endif #endif

View File

@ -6,113 +6,110 @@ const uint8_t month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
cron_expression *ce_init() { return malloc(sizeof(cron_expression)); } cron_expression *ce_init() { return malloc(sizeof(cron_expression)); }
void ce_free(cron_expression *ce) { void ce_free(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);
} }
void ce_next(cron_simple_time *out, cron_expression *ce, void ce_next(cron_simple_time *out, cron_expression *ce,
cron_simple_time *ref) { cron_simple_time *ref) {
// For all of these values, the rule is the following: if their value is // For all of these values, the rule is the following: if their value is
// the length of their respective array in the CronExpression object, that // the length of their respective array in the CronExpression object, that
// 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 && while (month_index < ce->month_count &&
ref->month > ce->months[month_index]) { ref->month > ce->months[month_index]) {
month_index++; month_index++;
} }
if (month_index < ce->month_count && if (month_index < ce->month_count && ref->month == ce->months[month_index]) {
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 && while (hour_index < ce->hour_count && ref->hour > ce->hours[hour_index]) {
ref->hour > ce->hours[hour_index]) { hour_index++;
hour_index++; }
}
if (hour_index < ce->hour_count && if (hour_index < ce->hour_count && ref->hour == ce->hours[hour_index]) {
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 &&
while (minute_index < ce->minute_count && ref->minute >= ce->minutes[minute_index]) {
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 > while (out->day >
month_days[ce->months[month_index % ce->month_count] - 1]) { month_days[ce->months[month_index % ce->month_count] - 1]) {
month_index++; month_index++;
} }
} }
out->month = ce->months[month_index % ce->month_count]; out->month = ce->months[month_index % ce->month_count];
if (month_index >= ce->month_count) { if (month_index >= ce->month_count) {
out->year = ref->year + 1; out->year = ref->year + 1;
} else { } else {
out->year = ref->year; out->year = ref->year;
} }
} }
void ce_next_from_now(cron_simple_time *out, cron_expression *ce) { void ce_next_from_now(cron_simple_time *out, 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);
cron_simple_time ref = {// tm_year contains years since 1900 cron_simple_time ref = {// tm_year contains years since 1900
.year = 1900 + gm.tm_year, .year = 1900 + 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};
ce_next(out, ce, &ref); ce_next(out, ce, &ref);
} }

View File

@ -2,7 +2,7 @@
// This prefix is needed to properly compile // This prefix is needed to properly compile
const uint8_t parse_month_days[] = {31, 28, 31, 30, 31, 30, const uint8_t parse_month_days[] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
// Allowed value ranges for the minute, hour, day and month field // 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};
@ -14,14 +14,14 @@ const uint8_t max_parts = 4;
// Convert a string into a uint8_t value by parsing it using atoi and checking // Convert a string into a uint8_t value by parsing it using atoi and checking
// whether it's contained within the given range // 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 cron_parse_invalid_number; \ return cron_parse_invalid_number; \
} \ } \
if (((_##v) < (min)) || ((_##v) > (max))) { \ if (((_##v) < (min)) || ((_##v) > (max))) { \
return cron_parse_out_of_range; \ return cron_parse_out_of_range; \
} \ } \
v = (uint8_t)(_##v); v = (uint8_t)(_##v);
#define MAX(x, y) (((x) > (y)) ? (x) : (y)) #define MAX(x, y) (((x) > (y)) ? (x) : (y))
@ -42,77 +42,77 @@ const uint8_t max_parts = 4;
* - a-b/c * - a-b/c
*/ */
cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min,
uint8_t max) { uint8_t max) {
size_t slash_index = 0, dash_index = 0; size_t slash_index = 0, dash_index = 0;
size_t s_index = 0; size_t s_index = 0;
char cur_char; char cur_char;
bool is_valid_character; bool is_valid_character;
while ((cur_char = s[s_index]) != '\0') { while ((cur_char = s[s_index]) != '\0') {
is_valid_character = cur_char == '/' || cur_char == '-' || is_valid_character = cur_char == '/' || cur_char == '-' ||
cur_char == '*' || cur_char == '*' ||
(cur_char >= '0' && cur_char <= '9'); (cur_char >= '0' && cur_char <= '9');
if (!is_valid_character) { if (!is_valid_character) {
return cron_parse_invalid_expression; return cron_parse_invalid_expression;
} }
if (cur_char == '/') { if (cur_char == '/') {
if (s_index == 0 || slash_index != 0) { if (s_index == 0 || slash_index != 0) {
return cron_parse_invalid_expression; return cron_parse_invalid_expression;
} }
slash_index = s_index; slash_index = s_index;
s[s_index] = '\0'; s[s_index] = '\0';
} else if (cur_char == '-') { } else if (cur_char == '-') {
// At most one dash is allowed, and it must be before the slash // At most one dash is allowed, and it must be before the slash
if (s_index == 0 || dash_index != 0 || slash_index != 0) { if (s_index == 0 || dash_index != 0 || slash_index != 0) {
return cron_parse_invalid_expression; return cron_parse_invalid_expression;
} }
dash_index = s_index; dash_index = s_index;
s[s_index] = '\0'; s[s_index] = '\0';
} }
s_index++; s_index++;
} }
uint8_t start; uint8_t start;
uint8_t end = max; uint8_t end = max;
uint8_t interval = 0; uint8_t interval = 0;
if (s[0] == '*') { if (s[0] == '*') {
if (s[1] != '\0' || dash_index != 0) { if (s[1] != '\0' || dash_index != 0) {
return cron_parse_invalid_expression; return cron_parse_invalid_expression;
} }
start = min; start = min;
interval = 1; interval = 1;
} else { } else {
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);
interval = 1; interval = 1;
} }
} }
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);
} }
if (interval == 0) { if (interval == 0) {
*out |= ((uint64_t)1) << (start - min); *out |= ((uint64_t)1) << (start - min);
} else { } else {
while (start <= end) { while (start <= end) {
*out |= ((uint64_t)1) << (start - min); *out |= ((uint64_t)1) << (start - min);
start += interval; start += interval;
} }
} }
return cron_parse_ok; return cron_parse_ok;
} }
/* /*
@ -121,26 +121,26 @@ cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min,
* expressions, separated by commas. * expressions, separated by commas.
*/ */
cron_parse_error ce_parse_part(uint64_t *out, char *s, uint8_t min, cron_parse_error ce_parse_part(uint64_t *out, char *s, uint8_t min,
uint8_t max) { uint8_t max) {
*out = 0; *out = 0;
char *next; char *next;
cron_parse_error res; cron_parse_error res;
while ((next = strchr(s, ',')) != NULL) { while ((next = strchr(s, ',')) != NULL) {
next[0] = '\0'; next[0] = '\0';
res = ce_parse_range(out, s, min, max); res = ce_parse_range(out, s, min, max);
if (res != cron_parse_ok) { if (res != cron_parse_ok) {
return res; return res;
} }
s = next + 1; s = next + 1;
} }
// Make sure to parse the final range as well // Make sure to parse the final range as well
return ce_parse_range(out, s, min, max); return ce_parse_range(out, s, min, max);
} }
/* /*
@ -149,16 +149,16 @@ cron_parse_error ce_parse_part(uint64_t *out, char *s, uint8_t min,
* to be dependent on GCC-specific extensions. * to be dependent on GCC-specific extensions.
*/ */
uint8_t uint64_t_popcount(uint64_t n) { uint8_t uint64_t_popcount(uint64_t n) {
uint8_t set_bits = 0; uint8_t set_bits = 0;
while (n != 0) { while (n != 0) {
// This sets the least significant bit to zero (very cool) // This sets the least significant bit to zero (very cool)
n &= n - 1; n &= n - 1;
set_bits++; set_bits++;
} }
return set_bits; return set_bits;
} }
/* /*
@ -166,170 +166,168 @@ uint8_t uint64_t_popcount(uint64_t n) {
* it represents. * 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) {
// Each bit field only has `max - min + 1` meaningful bits. All other bits // 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 // 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 // and forth, we set these excessive bits to zero, ensuring popcount returns
// the correct value. // the correct value.
uint8_t excess_bits = 64 - (max - min + 1); uint8_t excess_bits = 64 - (max - min + 1);
bf = (bf << excess_bits) >> excess_bits; bf = (bf << excess_bits) >> excess_bits;
uint8_t size = uint64_t_popcount(bf); uint8_t size = uint64_t_popcount(bf);
uint8_t *buf = malloc(size * sizeof(uint8_t)); uint8_t *buf = malloc(size * sizeof(uint8_t));
uint8_t bit_index = 0, buf_index = 0; uint8_t bit_index = 0, buf_index = 0;
while (buf_index < size && bit_index <= max - min) { while (buf_index < size && bit_index <= max - min) {
if (((uint64_t)1 << bit_index) & bf) { if (((uint64_t)1 << bit_index) & bf) {
// Resize buffer if needed // Resize buffer if needed
buf[buf_index] = min + bit_index; buf[buf_index] = min + bit_index;
buf_index++; buf_index++;
} }
bit_index++; bit_index++;
} }
*out = buf; *out = buf;
return size; return size;
} }
/* /*
* Parse a cron expression string into a cron_expression struct. * Parse a cron expression string into a cron_expression struct.
*/ */
cron_parse_error ce_parse_expression(cron_expression *out, cron_parse_error ce_parse_expression(cron_expression *out,
const char *expression) { const char *expression) {
// The parsing functions modify the input string in-place // The parsing functions modify the input string in-place
char *s = strdup(expression); char *s = strdup(expression);
char *orig_s = s; char *orig_s = s;
cron_parse_error res = cron_parse_ok; cron_parse_error res = cron_parse_ok;
// First we divide the input string into its parts, divided by spaces. // First we divide the input string into its parts, divided by spaces.
// Each part is delimited by a NULL byte. // Each part is delimited by a NULL byte.
uint8_t part_count = 0; uint8_t part_count = 0;
char *parts[max_parts]; char *parts[max_parts];
char *next_space; char *next_space;
// Skip leading spaces // Skip leading spaces
size_t offset = 0; size_t offset = 0;
while (s[offset] == ' ') { while (s[offset] == ' ') {
offset++; offset++;
} }
s += offset; s += offset;
while (part_count < max_parts && ((next_space = strchr(s, ' ')) != NULL)) { while (part_count < max_parts && ((next_space = strchr(s, ' ')) != NULL)) {
next_space[0] = '\0'; next_space[0] = '\0';
parts[part_count] = s; parts[part_count] = s;
part_count++; part_count++;
// Skip multiple spaces // Skip multiple spaces
offset = 1; offset = 1;
while (next_space[offset] == ' ') { while (next_space[offset] == ' ') {
offset++; offset++;
} }
s = next_space + offset; s = next_space + offset;
} }
// Each iteration of the loop skips all trailing spaces. This means that, if // 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. // s[0] isn't '\0', there's still another part before the end of the string.
if (s[0] != '\0') { if (s[0] != '\0') {
if (part_count == max_parts) { if (part_count == max_parts) {
res = cron_parse_too_many_parts; res = cron_parse_too_many_parts;
goto end; goto end;
} }
parts[part_count] = s; parts[part_count] = s;
part_count++; part_count++;
} }
if (part_count < min_parts) { if (part_count < min_parts) {
res = cron_parse_not_enough_parts; res = cron_parse_not_enough_parts;
goto end; goto end;
} }
// We now parse the parts in reverse. This is because the month part // We now parse the parts in reverse. This is because the month part
// determines the maximum value of the day part. // determines the maximum value of the day part.
uint64_t bit_field = 0; uint64_t bit_field = 0;
// Months // Months
if (part_count >= 4) { if (part_count >= 4) {
res = ce_parse_part(&bit_field, parts[3], min[3], max[3]); res = ce_parse_part(&bit_field, parts[3], min[3], max[3]);
if (res != cron_parse_ok) { if (res != cron_parse_ok) {
goto end; goto end;
} }
out->month_count = bf_to_nums(&out->months, bit_field, min[3], max[3]); out->month_count = bf_to_nums(&out->months, bit_field, min[3], max[3]);
} }
// If months aren't provided, they're replaced with a * // If months aren't provided, they're replaced with a *
else { else {
out->month_count = bf_to_nums(&out->months, ~0, min[3], max[3]); out->month_count = bf_to_nums(&out->months, ~0, min[3], max[3]);
} }
// Determine what the largest allowed day value is, given the months // Determine what the largest allowed day value is, given the months
uint8_t max_day_value = 0; uint8_t max_day_value = 0;
for (uint8_t i = 0; i < out->month_count; i++) { for (uint8_t i = 0; i < out->month_count; i++) {
max_day_value = max_day_value = MAX(max_day_value, parse_month_days[out->months[i] - 1]);
MAX(max_day_value, parse_month_days[out->months[i] - 1]); }
}
// Days // Days
if (part_count >= 3) { if (part_count >= 3) {
bit_field = 0; bit_field = 0;
res = ce_parse_part(&bit_field, parts[2], min[2], max_day_value); res = ce_parse_part(&bit_field, parts[2], min[2], max_day_value);
if (res != cron_parse_ok) { if (res != cron_parse_ok) {
free(out->months); free(out->months);
goto end; goto end;
} }
out->day_count = out->day_count = bf_to_nums(&out->days, bit_field, min[2], max_day_value);
bf_to_nums(&out->days, bit_field, min[2], max_day_value); }
} // If days aren't provided, they're replaced with a *
// If days aren't provided, they're replaced with a * else {
else { out->day_count = bf_to_nums(&out->days, ~0, min[2], max_day_value);
out->day_count = bf_to_nums(&out->days, ~0, min[2], max_day_value); }
}
// Hours // Hours
bit_field = 0; bit_field = 0;
res = ce_parse_part(&bit_field, parts[1], min[1], max[1]); res = ce_parse_part(&bit_field, parts[1], min[1], max[1]);
if (res != cron_parse_ok) { if (res != cron_parse_ok) {
free(out->months); free(out->months);
free(out->days); free(out->days);
goto end; goto end;
} }
out->hour_count = bf_to_nums(&out->hours, bit_field, min[1], max[1]); out->hour_count = bf_to_nums(&out->hours, bit_field, min[1], max[1]);
// Minutes // Minutes
bit_field = 0; bit_field = 0;
res = ce_parse_part(&bit_field, parts[0], min[0], max[0]); res = ce_parse_part(&bit_field, parts[0], min[0], max[0]);
if (res != cron_parse_ok) { if (res != cron_parse_ok) {
free(out->months); free(out->months);
free(out->days); free(out->days);
free(out->hours); free(out->hours);
goto end; goto end;
} }
out->minute_count = bf_to_nums(&out->minutes, bit_field, min[0], max[0]); out->minute_count = bf_to_nums(&out->minutes, bit_field, min[0], max[0]);
end: end:
// s is cloned // s is cloned
free(orig_s); free(orig_s);
return res; return res;
} }