feat(cron): rest of parser in C

Jef Roosens 2023-01-12 21:08:58 +01:00
parent c3f7f11686
commit ee262a2fbc
4 changed files with 70 additions and 12 deletions

View File

@ -3,6 +3,13 @@
#include <stdlib.h>
#include <string.h>
typedef enum parse_error {
ParseOk = 0,
ParseInvalidExpression = 1,
ParseInvalidNumber = 2,
ParseOutOfRange = 3
} ParseError;
typedef struct cron_expression {
uint8_t *minutes;
uint8_t *hours;
@ -19,4 +26,4 @@ typedef struct cron_expression {
*/
int ce_next(struct tm *out, struct tm *ref);
int ce_parse(CronExpression *out, char *s);
ParseError ce_parse_expression(CronExpression *out, char *s);

View File

@ -3,13 +3,6 @@
const uint8_t min[4] = {0, 0, 1, 1};
const uint8_t max[4] = {59, 23, 31, 12};
typedef enum parse_error {
ParseOk = 0,
ParseInvalidExpression = 1,
ParseInvalidNumber = 2,
ParseOutOfRange = 3
} ParseError;
#define SAFE_ATOI(v,s,min,max) \
int _##v = atoi(s); \
if ((_##v) == 0 && strcmp((s), "0") != 0) { \
@ -125,11 +118,41 @@ ParseError ce_parse_part(uint64_t *out, char *s, uint8_t min, uint8_t max) {
return ParseOk;
}
ParseError ce_parse_expression(uint64_t *out, char *s) {
uint8_t bf_to_nums(uint8_t **out, uint64_t bf, uint8_t min, uint8_t max) {
uint8_t capacity = 8;
uint8_t size = 0;
uint8_t *buf = malloc(capacity * sizeof(uint8_t));
for (uint8_t i = 0; i <= max - min; i++) {
if ((1 << i) & bf) {
// Resize buffer if needed
if (size == capacity) {
capacity *= 2;
buf = realloc(buf, capacity * sizeof(uint8_t));
}
buf[size] = min + i;
size++;
}
}
// Resize buffer once more to remove any trailing unused bytes
if (size < capacity) {
buf = realloc(buf, size * sizeof(uint8_t));
}
*out = buf;
return size;
}
ParseError ce_parse_expression(CronExpression *out, char *s) {
uint8_t part_count = 0;
char *next;
ParseError res;
uint64_t bfs[4];
// Skip leading spaces
while (s[0] == ' ') {
@ -138,7 +161,7 @@ ParseError ce_parse_expression(uint64_t *out, char *s) {
while (part_count < 4 && (next = strchr(s, ' ')) != NULL) {
next[0] = '\0';
res = ce_parse_part(&out[part_count], s, min[part_count], max[part_count]);
res = ce_parse_part(&bfs[part_count], s, min[part_count], max[part_count]);
if (res != ParseOk) {
return res;
@ -159,7 +182,7 @@ ParseError ce_parse_expression(uint64_t *out, char *s) {
// Parse final trailing part
if (part_count < 4 && s[0] != '\0') {
// Make sure to parse the final range as well
res = ce_parse_part(&out[part_count], s, min[part_count], max[part_count]);
res = ce_parse_part(&bfs[part_count], s, min[part_count], max[part_count]);
if (res != ParseOk) {
return res;
@ -168,12 +191,22 @@ ParseError ce_parse_expression(uint64_t *out, char *s) {
part_count++;
}
// At least two parts need to be provided
if (part_count < 2) {
return ParseInvalidExpression;
}
// Ensure there's always 4 parts, as expressions can have between 2 and 4 parts
while (part_count < 4) {
// Expression is augmented with '*' expressions
out[part_count] = ~0;
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]);
return ParseOk;
}

View File

@ -0,0 +1,18 @@
module expression
#flag -I @VMODROOT/c
#flag @VMODROOT/c/parse.o
#include "expression.h"
pub struct C.CronExpression {
minutes &u8
hours &u8
days &u8
months &u8
minute_count u8
hour_count u8
day_count u8
month_count u8
}
/* pub type CronExpression = C.CronExpression */

View File