| 1 |
|
| #ifndef TKR_RULE_H |
| 2 |
|
| #define TKR_RULE_H |
| 3 |
|
| |
| 4 |
|
| /* One compiled rule, whatever spelling it came from: an RFC 5545 RRULE, a |
| 5 |
|
| * cron expression or named arguments. The walk (tkr_walk.h) reads only |
| 6 |
|
| * this. Masks are bit sets; the lists that can hold any of hundreds of |
| 7 |
|
| * values are short arrays with a count. Weekdays are 0 Monday .. 6 Sunday |
| 8 |
|
| * inside the struct, 1 .. 7 at the surface, as Time::Keeper has them. */ |
| 9 |
|
| |
| 10 |
|
| #define TKR_SECONDLY 0 |
| 11 |
|
| #define TKR_MINUTELY 1 |
| 12 |
|
| #define TKR_HOURLY 2 |
| 13 |
|
| #define TKR_DAILY 3 |
| 14 |
|
| #define TKR_WEEKLY 4 |
| 15 |
|
| #define TKR_MONTHLY 5 |
| 16 |
|
| #define TKR_YEARLY 6 |
| 17 |
|
| |
| 18 |
|
| #define TKR_F_VIXIE 0x01 /* cron: a day matching EITHER restricted day field fires */ |
| 19 |
|
| #define TKR_F_EVERY 0x02 /* cron @every: an interval from the last occurrence */ |
| 20 |
|
| #define TKR_F_HAS_START 0x04 /* start holds the rule's start */ |
| 21 |
|
| #define TKR_F_CRON 0x08 /* came through the cron door: from is exclusive, the start is no occurrence */ |
| 22 |
|
| #define TKR_F_EXRULE 0x10 /* an exclusion: the start is an occurrence only when it fits */ |
| 23 |
|
| #define TKR_F_RSCALE 0x20 /* RFC 7529: walked in calendar (0 the Gregorian, with SKIP) */ |
| 24 |
|
| |
| 25 |
|
| #define TKR_UNTIL_NONE 0 |
| 26 |
|
| #define TKR_UNTIL_UTC 1 /* an instant */ |
| 27 |
|
| #define TKR_UNTIL_FLOATING 2 /* a wall time, seconds as if UTC */ |
| 28 |
|
| #define TKR_UNTIL_DATE 3 /* a day, the end of it in the rule's zone */ |
| 29 |
|
| |
| 30 |
|
| #define TKR_GAP_FORWARD 0 |
| 31 |
|
| #define TKR_GAP_BACKWARD 1 |
| 32 |
|
| #define TKR_GAP_SKIP 2 |
| 33 |
|
| #define TKR_GAP_FAIL 3 |
| 34 |
|
| |
| 35 |
|
| #define TKR_OVERLAP_EARLIER 0 |
| 36 |
|
| #define TKR_OVERLAP_LATER 1 |
| 37 |
|
| #define TKR_OVERLAP_BOTH 2 |
| 38 |
|
| |
| 39 |
|
| #define TKR_SKIP_OMIT 0 |
| 40 |
|
| #define TKR_SKIP_BACKWARD 1 |
| 41 |
|
| #define TKR_SKIP_FORWARD 2 |
| 42 |
|
| |
| 43 |
|
| static const char *const tkr_skip_names[3] = { "omit", "backward", "forward" }; |
| 44 |
|
| static const char *const tkr_skip_upper[3] = { "OMIT", "BACKWARD", "FORWARD" }; |
| 45 |
|
| |
| 46 |
|
| #define TKR_MAX_LIST 64 |
| 47 |
|
| |
| 48 |
|
| typedef struct tkr_rule { |
| 49 |
|
| uint8_t freq; |
| 50 |
|
| uint8_t wkst; /* 0 Monday .. 6 Sunday */ |
| 51 |
|
| uint8_t flags; |
| 52 |
|
| uint8_t until_kind; |
| 53 |
|
| uint8_t gap, overlap; |
| 54 |
|
| uint8_t calendar; /* with RSCALE: Time::Keeper's calendar id, 0 the Gregorian */ |
| 55 |
|
| uint8_t skip; /* with RSCALE: TKR_SKIP_OMIT, BACKWARD or FORWARD */ |
| 56 |
|
| uint32_t interval; |
| 57 |
|
| int64_t count; /* 0: none */ |
| 58 |
|
| int64_t until_sec; |
| 59 |
|
| uint64_t by_second; /* bits 0 .. 60 */ |
| 60 |
|
| uint64_t by_minute; /* bits 0 .. 59 */ |
| 61 |
|
| uint32_t by_hour; /* bits 0 .. 23 */ |
| 62 |
|
| uint64_t by_monthday; /* bits 1 .. 31 for 1 .. 31, bits 33 .. 63 for -1 .. -31 */ |
| 63 |
|
| uint16_t by_month; /* bits 1 .. 12; with RSCALE, month-code numbers 1 .. 13 */ |
| 64 |
|
| uint16_t by_month_leap; /* with RSCALE: the leap months, "5L" as bit 5 */ |
| 65 |
|
| uint8_t by_day_any; /* bits 0 .. 6: the weekday with no ordinal */ |
| 66 |
|
| uint64_t by_day_pos[7]; /* bits 1 .. 53: the nth such weekday */ |
| 67 |
|
| uint64_t by_day_neg[7]; /* bits 1 .. 53: the nth from the end */ |
| 68 |
|
| int16_t by_yearday[TKR_MAX_LIST]; |
| 69 |
|
| int8_t by_weekno[TKR_MAX_LIST]; |
| 70 |
|
| int16_t by_setpos[TKR_MAX_LIST]; |
| 71 |
|
| uint8_t n_yearday, n_weekno, n_setpos; |
| 72 |
|
| int64_t every_sec; |
| 73 |
|
| time_keeper_instant start; |
| 74 |
|
| time_keeper_zone zone; |
| 75 |
|
| int locale; |
| 76 |
|
| } tkr_rule; |
| 77 |
|
| |
| 78 |
|
| static const char *const tkr_freq_names[7] = { "SECONDLY", "MINUTELY", "HOURLY", "DAILY", "WEEKLY", "MONTHLY", "YEARLY" }; |
| 79 |
|
| static const char *const tkr_freq_lower[7] = { "secondly", "minutely", "hourly", "daily", "weekly", "monthly", "yearly" }; |
| 80 |
|
| static const char *const tkr_day_names[7] = { "MO", "TU", "WE", "TH", "FR", "SA", "SU" }; |
| 81 |
|
| static const char *const tkr_gap_names[4] = { "forward", "backward", "skip", "fail" }; |
| 82 |
|
| static const char *const tkr_overlap_names[3] = { "earlier", "later", "both" }; |
| 83 |
|
| |
| 84 |
|
| static void |
| 85 |
5066036 |
| tkr_rule_init(tkr_rule *r) |
| 86 |
|
| { |
| 87 |
5066036 |
| memset(r, 0, sizeof *r); |
| 88 |
5066036 |
| r->interval = 1; |
| 89 |
5066036 |
| r->zone.kind = TIME_KEEPER_ZONE_UTC; |
| 90 |
5066036 |
| } |
| 91 |
|
| |
| 92 |
|
| /* True when a BY part is set, for the RFC's rules about which may meet. */ |
| 93 |
53299579 |
| static int tkr_has_by_day(const tkr_rule *r) |
| 94 |
|
| { |
| 95 |
|
| int i; |
| 96 |
53299579 |
▶ | if (r->by_day_any) |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->by_day_any) |
23219488 |
30080091 |
53299579 |
|
| 97 |
23219488 |
| return 1; |
| 98 |
240578448 |
▶ | for (i = 0; i < 7; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 7; i++) |
210508772 |
30069676 |
240578448 |
|
| 99 |
210508772 |
▶ | if (r->by_day_pos[i] || r->by_day_neg[i]) |
Branch: 2 branches
| Branch | True | False | Total |
| if (r->by_day_pos[i] || r->by_day_neg[i]) |
210499097 |
9675 |
210508772 |
| if (r->by_day_pos[i] || r->by_day_neg[i]) |
740 |
210498357 |
210499097 |
|
| 100 |
10415 |
| return 1; |
| 101 |
30069676 |
| return 0; |
| 102 |
|
| } |
| 103 |
|
| |
| 104 |
6540 |
| static int tkr_has_by_day_ordinal(const tkr_rule *r) |
| 105 |
|
| { |
| 106 |
|
| int i; |
| 107 |
51875 |
▶ | for (i = 0; i < 7; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 7; i++) |
45432 |
6443 |
51875 |
|
| 108 |
45432 |
▶ | if (r->by_day_pos[i] || r->by_day_neg[i]) |
Branch: 2 branches
| Branch | True | False | Total |
| if (r->by_day_pos[i] || r->by_day_neg[i]) |
45353 |
79 |
45432 |
| if (r->by_day_pos[i] || r->by_day_neg[i]) |
18 |
45335 |
45353 |
|
| 109 |
97 |
| return 1; |
| 110 |
6443 |
| return 0; |
| 111 |
|
| } |
| 112 |
|
| |
| 113 |
20 |
| static int tkr_has_any_by(const tkr_rule *r) |
| 114 |
|
| { |
| 115 |
20 |
▶ | return r->by_second || r->by_minute || r->by_hour || r->by_monthday || r->by_month |
Branch: 4 branches
| Branch | True | False | Total |
| return r->by_second || r->by_minute || r->by_hour || r->by_monthday || r->by_month |
19 |
1 |
20 |
| return r->by_second || r->by_minute || r->by_hour || r->by_monthday || r->by_month |
18 |
1 |
19 |
| return r->by_second || r->by_minute || r->by_hour || r->by_monthday || r->by_month |
18 |
0 |
18 |
| return r->by_second || r->by_minute || r->by_hour || r->by_monthday || r->by_month |
18 |
0 |
18 |
|
| 116 |
40 |
▶ | || tkr_has_by_day(r) || r->n_yearday || r->n_weekno; |
Branch: 4 branches
| Branch | True | False | Total |
| || tkr_has_by_day(r) || r->n_yearday || r->n_weekno; |
20 |
0 |
20 |
| || tkr_has_by_day(r) || r->n_yearday || r->n_weekno; |
1 |
17 |
18 |
| || tkr_has_by_day(r) || r->n_yearday || r->n_weekno; |
1 |
0 |
1 |
| || tkr_has_by_day(r) || r->n_yearday || r->n_weekno; |
0 |
1 |
1 |
|
| 117 |
|
| } |
| 118 |
|
| |
| 119 |
4 |
| static int tkr_is_finite(const tkr_rule *r) |
| 120 |
|
| { |
| 121 |
4 |
▶ | return r->count > 0 || r->until_kind != TKR_UNTIL_NONE; |
Branch: 2 branches
| Branch | True | False | Total |
| return r->count > 0 || r->until_kind != TKR_UNTIL_NONE; |
3 |
1 |
4 |
| return r->count > 0 || r->until_kind != TKR_UNTIL_NONE; |
0 |
3 |
3 |
|
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
|
| /* A sorted, deduplicated short list. */ |
| 125 |
|
| static int |
| 126 |
112 |
| tkr_list_add(int16_t *list, uint8_t *n, int v) |
| 127 |
|
| { |
| 128 |
|
| int i, j; |
| 129 |
2216 |
▶ | for (i = 0; i < *n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < *n; i++) { |
2105 |
111 |
2216 |
|
| 130 |
2105 |
▶ | if (list[i] == v) |
Branch: 1 branch
| Branch | True | False | Total |
| if (list[i] == v) |
0 |
2105 |
2105 |
|
| 131 |
0 |
| return 1; |
| 132 |
2105 |
▶ | if (list[i] > v) |
Branch: 1 branch
| Branch | True | False | Total |
| if (list[i] > v) |
1 |
2104 |
2105 |
|
| 133 |
1 |
| break; |
| 134 |
|
| } |
| 135 |
112 |
▶ | if (*n >= TKR_MAX_LIST) |
Branch: 1 branch
| Branch | True | False | Total |
| if (*n >= TKR_MAX_LIST) |
1 |
111 |
112 |
|
| 136 |
1 |
| return 0; |
| 137 |
112 |
▶ | for (j = *n; j > i; j--) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = *n; j > i; j--) |
1 |
111 |
112 |
|
| 138 |
1 |
| list[j] = list[j - 1]; |
| 139 |
111 |
| list[i] = (int16_t)v; |
| 140 |
111 |
| (*n)++; |
| 141 |
111 |
| return 1; |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
|
| static int |
| 145 |
12 |
| tkr_list8_add(int8_t *list, uint8_t *n, int v) |
| 146 |
|
| { |
| 147 |
|
| int i, j; |
| 148 |
12 |
▶ | for (i = 0; i < *n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < *n; i++) { |
0 |
12 |
12 |
|
| 149 |
0 |
▶ | if (list[i] == v) |
Branch: 1 branch
| Branch | True | False | Total |
| if (list[i] == v) |
0 |
0 |
0 |
|
| 150 |
0 |
| return 1; |
| 151 |
0 |
▶ | if (list[i] > v) |
Branch: 1 branch
| Branch | True | False | Total |
| if (list[i] > v) |
0 |
0 |
0 |
|
| 152 |
0 |
| break; |
| 153 |
|
| } |
| 154 |
12 |
▶ | if (*n >= TKR_MAX_LIST) |
Branch: 1 branch
| Branch | True | False | Total |
| if (*n >= TKR_MAX_LIST) |
0 |
12 |
12 |
|
| 155 |
0 |
| return 0; |
| 156 |
12 |
▶ | for (j = *n; j > i; j--) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = *n; j > i; j--) |
0 |
12 |
12 |
|
| 157 |
0 |
| list[j] = list[j - 1]; |
| 158 |
12 |
| list[i] = (int8_t)v; |
| 159 |
12 |
| (*n)++; |
| 160 |
12 |
| return 1; |
| 161 |
|
| } |
| 162 |
|
| |
| 163 |
|
| #endif |