chore: add C code linting & formatting
parent
7ec0c96f5f
commit
6d1b4aadb6
|
@ -0,0 +1,4 @@
|
||||||
|
# To stay consistent with the V formatting style, we use tabs
|
||||||
|
UseTab: Always
|
||||||
|
IndentWidth: 4
|
||||||
|
TabWidth: 4
|
26
Makefile
26
Makefile
|
@ -70,22 +70,42 @@ man: vieter
|
||||||
|
|
||||||
|
|
||||||
# =====OTHER=====
|
# =====OTHER=====
|
||||||
|
# Linting
|
||||||
.PHONY: lint
|
.PHONY: lint
|
||||||
lint:
|
lint: lint-v lint-c
|
||||||
|
|
||||||
|
.PHONY: lint-v
|
||||||
|
lint-v:
|
||||||
$(V) fmt -verify $(SRC_DIR)
|
$(V) fmt -verify $(SRC_DIR)
|
||||||
$(V) vet -W $(SRC_DIR)
|
$(V) vet -W $(SRC_DIR)
|
||||||
$(V_PATH) missdoc -p $(SRC_DIR)
|
$(V_PATH) missdoc -p $(SRC_DIR)
|
||||||
@ [ $$($(V_PATH) missdoc -p $(SRC_DIR) | wc -l) = 0 ]
|
@ [ $$($(V_PATH) missdoc -p $(SRC_DIR) | wc -l) = 0 ]
|
||||||
|
|
||||||
# Format the V codebase
|
.PHONY: lint-c
|
||||||
|
lint-c:
|
||||||
|
clang-format --Werror -n $(SRCS_C) $(SRCS_H)
|
||||||
|
|
||||||
|
|
||||||
|
# Formatting
|
||||||
.PHONY: fmt
|
.PHONY: fmt
|
||||||
fmt:
|
fmt: fmt-v fmt-c
|
||||||
|
|
||||||
|
.PHONY: fmt-v
|
||||||
|
fmt-v:
|
||||||
$(V) fmt -w $(SRC_DIR)
|
$(V) fmt -w $(SRC_DIR)
|
||||||
|
|
||||||
|
.PHONY: fmt-c
|
||||||
|
fmt-c:
|
||||||
|
clang-format -i $(SRCS_C) $(SRCS_H)
|
||||||
|
|
||||||
|
|
||||||
|
# Testing
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test:
|
test:
|
||||||
$(V) -g test $(SRC_DIR)
|
$(V) -g test $(SRC_DIR)
|
||||||
|
|
||||||
|
|
||||||
|
# Cleaning
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
rm -rf 'data' 'vieter' 'dvieter' 'pvieter' 'vieter.c' 'pkg' 'src/vieter' *.pkg.tar.zst 'suvieter' 'afvieter' '$(SRC_DIR)/_docs' 'docs/public'
|
rm -rf 'data' 'vieter' 'dvieter' 'pvieter' 'vieter.c' 'pkg' 'src/vieter' *.pkg.tar.zst 'suvieter' 'afvieter' '$(SRC_DIR)/_docs' 'docs/public'
|
||||||
|
|
|
@ -3,9 +3,7 @@
|
||||||
|
|
||||||
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};
|
||||||
|
|
||||||
cron_expression *ce_init() {
|
cron_expression *ce_init() { return malloc(sizeof(cron_expression)); }
|
||||||
return malloc(sizeof(cron_expression));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ce_free(cron_expression *ce) {
|
void ce_free(cron_expression *ce) {
|
||||||
free(ce->months);
|
free(ce->months);
|
||||||
|
@ -15,7 +13,8 @@ void ce_free(cron_expression *ce) {
|
||||||
free(ce);
|
free(ce);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ce_next(cron_simple_time *out, cron_expression *ce, cron_simple_time *ref) {
|
void ce_next(cron_simple_time *out, cron_expression *ce,
|
||||||
|
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
|
||||||
|
@ -30,25 +29,30 @@ void ce_next(cron_simple_time *out, cron_expression *ce, cron_simple_time *ref)
|
||||||
// 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++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -82,7 +86,8 @@ void ce_next(cron_simple_time *out, cron_expression *ce, cron_simple_time *ref)
|
||||||
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++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -101,15 +106,13 @@ void ce_next_from_now(cron_simple_time *out, cron_expression *ce) {
|
||||||
struct tm gm;
|
struct tm gm;
|
||||||
gmtime_r(&t, &gm);
|
gmtime_r(&t, &gm);
|
||||||
|
|
||||||
cron_simple_time ref = {
|
cron_simple_time ref = {// tm_year contains years since 1900
|
||||||
// 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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
#ifndef VIETER_CRON
|
#ifndef VIETER_CRON
|
||||||
#define VIETER_CRON
|
#define VIETER_CRON
|
||||||
|
|
||||||
#include <time.h>
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
typedef enum cron_parse_error {
|
typedef enum cron_parse_error {
|
||||||
cron_parse_ok = 0,
|
cron_parse_ok = 0,
|
||||||
|
@ -37,7 +37,8 @@ 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, cron_simple_time *ref);
|
void cron_ce_next(cron_simple_time *out, cron_expression *ce,
|
||||||
|
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);
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
#include "expression.h"
|
#include "expression.h"
|
||||||
|
|
||||||
// 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, 31, 31, 30, 31, 30, 31};
|
const uint8_t parse_month_days[] = {31, 28, 31, 30, 31, 30,
|
||||||
|
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};
|
||||||
|
@ -12,15 +13,15 @@ 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))
|
||||||
|
|
||||||
|
@ -40,7 +41,8 @@ const uint8_t max_parts = 4;
|
||||||
* - a/c
|
* - a/c
|
||||||
* - a-b/c
|
* - a-b/c
|
||||||
*/
|
*/
|
||||||
cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_t max) {
|
cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min,
|
||||||
|
uint8_t max) {
|
||||||
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;
|
||||||
|
@ -75,7 +77,7 @@ cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_t max
|
||||||
} else {
|
} else {
|
||||||
return cron_parse_invalid_expression;
|
return cron_parse_invalid_expression;
|
||||||
}
|
}
|
||||||
}else {
|
} else {
|
||||||
SAFE_ATOI(start, s, min, max);
|
SAFE_ATOI(start, s, min, max);
|
||||||
|
|
||||||
if (dash_index > 0) {
|
if (dash_index > 0) {
|
||||||
|
@ -89,10 +91,10 @@ cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_t max
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -105,7 +107,8 @@ cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_t max
|
||||||
* min-max range the part represents. A part consists of one or more range
|
* min-max range the part represents. A part consists of one or more range
|
||||||
* expressions, separated by commas.
|
* expressions, separated by commas.
|
||||||
*/
|
*/
|
||||||
cron_parse_error ce_parse_part(uint64_t *out, char *s, uint8_t min, uint8_t max) {
|
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;
|
||||||
|
@ -149,9 +152,9 @@ uint8_t uint64_t_popcount(uint64_t n) {
|
||||||
*/
|
*/
|
||||||
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 and
|
// should be ignored, and can be any value. By shifting the bit field back
|
||||||
// forth, we set these excessive bits to zero, ensuring popcount returns the
|
// and forth, we set these excessive bits to zero, ensuring popcount returns
|
||||||
// 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);
|
||||||
|
@ -230,12 +233,11 @@ enum cron_parse_error ce_parse_expression(cron_expression *out, char *s) {
|
||||||
part_count++;
|
part_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 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]);
|
||||||
|
@ -255,9 +257,10 @@ enum cron_parse_error ce_parse_expression(cron_expression *out, char *s) {
|
||||||
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(max_day_value, parse_month_days[out->months[i] - 1]);
|
max_day_value =
|
||||||
|
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;
|
||||||
|
@ -268,7 +271,8 @@ enum cron_parse_error ce_parse_expression(cron_expression *out, char *s) {
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
out->day_count = bf_to_nums(&out->days, bit_field, min[2], max_day_value);
|
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 *
|
// If days aren't provided, they're replaced with a *
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -20,6 +20,10 @@ fn test_not_allowed() {
|
||||||
res = false
|
res = false
|
||||||
parse_expression('0 0 30 2 0') or { res = true }
|
parse_expression('0 0 30 2 0') or { res = true }
|
||||||
assert res
|
assert res
|
||||||
|
|
||||||
|
res = false
|
||||||
|
parse_expression('0 /5') or { res = true }
|
||||||
|
assert res
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_leading_star() {
|
fn test_leading_star() {
|
||||||
|
|
Loading…
Reference in New Issue