include/tkr/tkr_cron.h

filter: Statementstmt 97.7% 171 / 175 Branchbran 89.2% 225 / 252 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.7% 396 / 427 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/tkr/tkr_walk.h ↑ Summary include/tkr/tkr_zone.h »
1 #ifndef TKR_CRON_H
2 #define TKR_CRON_H
3  
4 /* Cron expressions in Punk::Queue's grammar, read into a tkr_rule and
5 * written back. Five fields, minute hour day-of-month month day-of-week:
6 * `*`, ranges a-b, steps `*`/n and a-b/n (a/n is a-hi/n), lists a,b,c,
7 * case-insensitive three-letter month and day names, day-of-week 0 to 7
8 * with 0 and 7 both Sunday, the aliases @yearly @annually @monthly @weekly
9 * @daily @midnight @hourly, and `@every <n>(s|m|h|d)`, an interval from the
10 * last occurrence rather than a wall-clock grid. L, W, # and ?, and a sixth
11 * or seventh field, are refused by name rather than misread.
12 *
13 * The rule it makes is FREQ=MINUTELY with the restricted fields as BY parts
14 * (a field matching everything is left unset), TKR_F_CRON, and TKR_F_VIXIE
15 * when BOTH day fields are restricted: then a day matching EITHER fires,
16 * the cron reading, where an RRULE's two day parts must both match. The
17 * error texts are Punk::Queue's, so the two engines say the same thing. */
18  
19 static const char *const tkr_cron_months[12] = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };
20 static const char *const tkr_cron_days[7] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };
21  
22 #define TKR_CRON_ALL(lo, hi) ((((UINT64_C(1) << ((hi) - (lo) + 1)) - 1)) << (lo))
23  
24 static int
25 1852 tkr_cron_atom(const char **pp, const char *end, const char *const *names, int nnames, int base, const char **errp)
26 {
27 1852 const char *p = *pp;
28 1852 ▶ if (p < end && isALPHA(*p)) {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && isALPHA(*p)) { 1852 0 1852
if (p < end && isALPHA(*p)) { 19 1833 1852
29 char buf[4];
30 int i;
31 19 ▶ if (end - p < 3) { *errp = p; return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 3) { *errp = p; return -1; } 0 19 19
32 76 ▶ for (i = 0; i < 3; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) 57 19 76
33 57 ▶ buf[i] = (char)toLOWER(p[i]);
Branch: 1 branch
BranchTrueFalseTotal
buf[i] = (char)toLOWER(p[i]); 0 57 57
34 19 buf[3] = 0;
35 64 ▶ for (i = 0; i < nnames; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nnames; i++) 62 2 64
36 62 ▶ if (strEQ(buf, names[i])) { *pp = p + 3; return i + base; }
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(buf, names[i])) { *pp = p + 3; return i + base; } 17 45 62
37 2 *errp = p;
38 2 return -1;
39 }
40 1833 ▶ if (p < end && isDIGIT(*p)) {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && isDIGIT(*p)) { 1833 0 1833
if (p < end && isDIGIT(*p)) { 1833 0 1833
41 1833 int v = 0;
42 4358 ▶ while (p < end && isDIGIT(*p) && v < 100000)
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && isDIGIT(*p) && v < 100000) 3104 1254 4358
while (p < end && isDIGIT(*p) && v < 100000) 2525 579 3104
while (p < end && isDIGIT(*p) && v < 100000) 2525 0 2525
43 2525 v = v * 10 + (*p++ - '0');
44 1833 *pp = p;
45 1833 return v;
46 }
47 0 *errp = p;
48 0 return -1;
49 }
50  
51 static int
52 2167 tkr_cron_field(const char *s, const char *end, int lo, int hi, const char *const *names, int nnames, uint64_t *mask, const char **errp)
53 {
54 2167 uint64_t out = 0;
55 2167 const char *p = s;
56 2500 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 2500 0 2500
57 2500 int a, b, step = 1, v;
58 2500 ▶ if (*p == '*') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '*') { 1120 1380 2500
59 1120 p++;
60 1120 a = lo;
61 1120 b = hi;
62 }
63 else {
64 1380 a = tkr_cron_atom(&p, end, names, nnames, lo, errp);
65 1380 ▶ if (a < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (a < 0) return -1; 0 1380 1380
66 1380 ▶ if (p < end && *p == '-') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '-') { 580 800 1380
if (p < end && *p == '-') { 244 336 580
67 244 p++;
68 244 b = tkr_cron_atom(&p, end, names, nnames, lo, errp);
69 244 ▶ if (b < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (b < 0) return -1; 2 242 244
70 }
71 else
72 1136 b = a;
73 }
74 2498 ▶ if (p < end && *p == '/') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '/') { 561 1937 2498
if (p < end && *p == '/') { 228 333 561
75 228 p++;
76 228 step = tkr_cron_atom(&p, end, NULL, 0, 0, errp);
77 228 ▶ if (step <= 0) { *errp = s; return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (step <= 0) { *errp = s; return -1; } 4 224 228
78 224 ▶ if (b == a) b = hi; /* a/n means a-hi/n */
Branch: 1 branch
BranchTrueFalseTotal
if (b == a) b = hi; /* a/n means a-hi/n */ 3 221 224
79 }
80 2494 ▶ if (a < lo || a > hi || b > hi) { *errp = s; return -1; }
Branch: 3 branches
BranchTrueFalseTotal
if (a < lo || a > hi || b > hi) { *errp = s; return -1; } 2492 2 2494
if (a < lo || a > hi || b > hi) { *errp = s; return -1; } 2481 11 2492
if (a < lo || a > hi || b > hi) { *errp = s; return -1; } 0 2481 2481
81 2481 ▶ if (a <= b)
Branch: 1 branch
BranchTrueFalseTotal
if (a <= b) 2477 4 2481
82 26979 ▶ for (v = a; v <= b; v += step) out |= UINT64_C(1) << v;
Branch: 1 branch
BranchTrueFalseTotal
for (v = a; v <= b; v += step) out |= UINT64_C(1) << v; 24502 2477 26979
83 else {
84 16 ▶ for (v = a; v <= hi; v += step) out |= UINT64_C(1) << v; /* a wrapped range (fri-mon): both arcs */
Branch: 1 branch
BranchTrueFalseTotal
for (v = a; v <= hi; v += step) out |= UINT64_C(1) << v; /* a wrapped range (fri-mon): both arcs */ 12 4 16
85 12 ▶ for (v = lo; v <= b; v += step) out |= UINT64_C(1) << v;
Branch: 1 branch
BranchTrueFalseTotal
for (v = lo; v <= b; v += step) out |= UINT64_C(1) << v; 8 4 12
86 }
87 2481 ▶ if (p < end && *p == ',') { p++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == ',') { p++; continue; } 333 2148 2481
if (p < end && *p == ',') { p++; continue; } 333 0 333
88 2148 break;
89 }
90 2148 ▶ if (p != end) { *errp = p; return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (p != end) { *errp = p; return -1; } 0 2148 2148
91 2148 *mask = out;
92 2148 return 0;
93 }
94  
95 /* 1, or 0 with err: the message after "Time::Keeper::Recurrence->cron: ". */
96 static int
97 511 tkr_cron_parse(const char *expr, size_t len, tkr_rule *r, char *err)
98 {
99 511 const char *p = expr, *end = expr + len, *fs[5], *fe[5], *q, *at = NULL;
100 uint64_t minute, hour, dom, mon, dow;
101 int i;
102 523 ▶ while (p < end && isSPACE(*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isSPACE(*p)) p++; 519 4 523
while (p < end && isSPACE(*p)) p++; 12 507 519
103 517 ▶ while (end > p && isSPACE(end[-1])) end--;
Branch: 2 branches
BranchTrueFalseTotal
while (end > p && isSPACE(end[-1])) end--; 513 4 517
while (end > p && isSPACE(end[-1])) end--; 6 507 513
104 511 ▶ if (p >= end) {
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) { 4 507 511
105 4 tkr_err(err, "empty cron expression");
106 4 return 0;
107 }
108 7367 ▶ for (q = p; q < end; q++)
Branch: 1 branch
BranchTrueFalseTotal
for (q = p; q < end; q++) 6868 499 7367
109 6868 ▶ if (*q == 'L' || *q == 'W' || *q == '#' || *q == '?') {
Branch: 4 branches
BranchTrueFalseTotal
if (*q == 'L' || *q == 'W' || *q == '#' || *q == '?') { 6866 2 6868
if (*q == 'L' || *q == 'W' || *q == '#' || *q == '?') { 6864 2 6866
if (*q == 'L' || *q == 'W' || *q == '#' || *q == '?') { 6862 2 6864
if (*q == 'L' || *q == 'W' || *q == '#' || *q == '?') { 2 6860 6862
110 8 tkr_err(err, "cron token '%c' is not supported (L, W, # and ? are excluded in this release)", *q);
111 8 return 0;
112 }
113 499 r->freq = TKR_MINUTELY;
114 499 r->interval = 1;
115 499 r->flags |= TKR_F_CRON;
116 499 ▶ if (*p == '@') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '@') { 54 445 499
117 static const struct { const char *name; const char *expr; } aliases[] = {
118 { "@yearly", "0 0 1 1 *" }, { "@annually", "0 0 1 1 *" }, { "@monthly", "0 0 1 * *" }, { "@weekly", "0 0 * * 0" },
119 { "@daily", "0 0 * * *" }, { "@midnight", "0 0 * * *" }, { "@hourly", "0 * * * *" },
120 };
121 54 size_t n = (size_t)(end - p);
122 unsigned k;
123 316 ▶ for (k = 0; k < sizeof aliases / sizeof *aliases; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < sizeof aliases / sizeof *aliases; k++) 292 24 316
124 292 ▶ if (n == strlen(aliases[k].name) && memEQ(p, aliases[k].name, n))
Branch: 2 branches
BranchTrueFalseTotal
if (n == strlen(aliases[k].name) && memEQ(p, aliases[k].name, n)) 63 229 292
if (n == strlen(aliases[k].name) && memEQ(p, aliases[k].name, n)) 30 33 63
125 30 return tkr_cron_parse(aliases[k].expr, strlen(aliases[k].expr), r, err);
126 24 ▶ if (n > 7 && memEQ(p, "@every ", 7)) {
Branch: 2 branches
BranchTrueFalseTotal
if (n > 7 && memEQ(p, "@every ", 7)) { 22 2 24
if (n > 7 && memEQ(p, "@every ", 7)) { 20 2 22
127 20 IV v = 0;
128 20 q = p + 7;
129 57 ▶ while (q < end && isDIGIT(*q) && v < 100000000)
Branch: 3 branches
BranchTrueFalseTotal
while (q < end && isDIGIT(*q) && v < 100000000) 57 0 57
while (q < end && isDIGIT(*q) && v < 100000000) 37 20 57
while (q < end && isDIGIT(*q) && v < 100000000) 37 0 37
130 37 v = v * 10 + (*q++ - '0');
131 20 ▶ if (v > 0 && q + 1 == end) {
Branch: 2 branches
BranchTrueFalseTotal
if (v > 0 && q + 1 == end) { 20 0 20
if (v > 0 && q + 1 == end) { 20 0 20
132 20 ▶ IV mult = *q == 's' ? 1 : *q == 'm' ? 60 : *q == 'h' ? 3600 : *q == 'd' ? 86400 : 0;
Branch: 4 branches
BranchTrueFalseTotal
IV mult = *q == 's' ? 1 : *q == 'm' ? 60 : *q == 'h' ? 3600 : *q == 'd' ? 86400 : 0; 17 3 20
IV mult = *q == 's' ? 1 : *q == 'm' ? 60 : *q == 'h' ? 3600 : *q == 'd' ? 86400 : 0; 7 10 17
IV mult = *q == 's' ? 1 : *q == 'm' ? 60 : *q == 'h' ? 3600 : *q == 'd' ? 86400 : 0; 3 4 7
IV mult = *q == 's' ? 1 : *q == 'm' ? 60 : *q == 'h' ? 3600 : *q == 'd' ? 86400 : 0; 1 2 3
133 20 ▶ if (mult) {
Branch: 1 branch
BranchTrueFalseTotal
if (mult) { 18 2 20
134 18 r->flags |= TKR_F_EVERY;
135 18 r->every_sec = v * mult;
136 18 return 1;
137 }
138 }
139 2 tkr_err(err, "@every wants '<n>(s|m|h|d)', not '%.*s'", (int)(end - p), p);
140 2 return 0;
141 }
142 4 tkr_err(err, "unknown cron alias '%.*s'", (int)(end - p), p);
143 4 return 0;
144 }
145 2668 ▶ for (i = 0; i < 5; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 5; i++) { 2225 443 2668
146 4009 ▶ while (p < end && isSPACE(*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isSPACE(*p)) p++; 4007 2 4009
while (p < end && isSPACE(*p)) p++; 1784 2223 4007
147 2225 ▶ if (p >= end) {
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) { 2 2223 2225
148 2 tkr_err(err, "cron expression has %d field(s), needs 5 (minute hour day-of-month month day-of-week)", i);
149 2 return 0;
150 }
151 2223 fs[i] = p;
152 6786 ▶ while (p < end && !isSPACE(*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && !isSPACE(*p)) p++; 6343 443 6786
while (p < end && !isSPACE(*p)) p++; 4563 1780 6343
153 2223 fe[i] = p;
154 }
155 445 ▶ while (p < end && isSPACE(*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isSPACE(*p)) p++; 4 441 445
while (p < end && isSPACE(*p)) p++; 2 2 4
156 443 ▶ if (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) { 2 441 443
157 2 tkr_err(err, "cron expression has more than 5 fields - second and year fields are not supported");
158 2 return 0;
159 }
160 441 ▶ if (tkr_cron_field(fs[0], fe[0], 0, 59, NULL, 0, &minute, &at)) { tkr_err(err, "bad cron minute field near '%.8s'", at); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (tkr_cron_field(fs[0], fe[0], 0, 59, NULL, 0, &minute, &at)) { tkr_err(err, "bad cron minute field near '%.8s'", at); return 0; } 5 436 441
161 436 ▶ if (tkr_cron_field(fs[1], fe[1], 0, 23, NULL, 0, &hour, &at)) { tkr_err(err, "bad cron hour field near '%.8s'", at); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (tkr_cron_field(fs[1], fe[1], 0, 23, NULL, 0, &hour, &at)) { tkr_err(err, "bad cron hour field near '%.8s'", at); return 0; } 2 434 436
162 434 ▶ if (tkr_cron_field(fs[2], fe[2], 1, 31, NULL, 0, &dom, &at)) { tkr_err(err, "bad cron day-of-month field near '%.8s'", at); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (tkr_cron_field(fs[2], fe[2], 1, 31, NULL, 0, &dom, &at)) { tkr_err(err, "bad cron day-of-month field near '%.8s'", at); return 0; } 4 430 434
163 430 ▶ if (tkr_cron_field(fs[3], fe[3], 1, 12, tkr_cron_months, 12, &mon, &at)) { tkr_err(err, "bad cron month field near '%.8s'", at); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (tkr_cron_field(fs[3], fe[3], 1, 12, tkr_cron_months, 12, &mon, &at)) { tkr_err(err, "bad cron month field near '%.8s'", at); return 0; } 4 426 430
164 426 ▶ if (tkr_cron_field(fs[4], fe[4], 0, 7, tkr_cron_days, 7, &dow, &at)) { tkr_err(err, "bad cron day-of-week field near '%.8s'", at); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (tkr_cron_field(fs[4], fe[4], 0, 7, tkr_cron_days, 7, &dow, &at)) { tkr_err(err, "bad cron day-of-week field near '%.8s'", at); return 0; } 4 422 426
165 /* dow 7 is Sunday in half the world's crontabs: fold bit 7 into bit 0
166 * AFTER the mask is built; folding per atom turned `*` into Sunday only */
167 422 dow |= (dow >> 7) & 1;
168 422 dow &= TKR_CRON_ALL(0, 6);
169 422 minute &= TKR_CRON_ALL(0, 59);
170 422 hour &= TKR_CRON_ALL(0, 23);
171 422 dom &= TKR_CRON_ALL(1, 31);
172 422 mon &= TKR_CRON_ALL(1, 12);
173 422 ▶ if (!minute || !hour || !dom || !mon || !dow) {
Branch: 5 branches
BranchTrueFalseTotal
if (!minute || !hour || !dom || !mon || !dow) { 422 0 422
if (!minute || !hour || !dom || !mon || !dow) { 422 0 422
if (!minute || !hour || !dom || !mon || !dow) { 422 0 422
if (!minute || !hour || !dom || !mon || !dow) { 422 0 422
if (!minute || !hour || !dom || !mon || !dow) { 0 422 422
174 0 tkr_err(err, "a cron field matches nothing");
175 0 return 0;
176 }
177 /* a field matching everything is no restriction, and is left unset */
178 422 ▶ if (minute != TKR_CRON_ALL(0, 59)) r->by_minute = minute;
Branch: 1 branch
BranchTrueFalseTotal
if (minute != TKR_CRON_ALL(0, 59)) r->by_minute = minute; 344 78 422
179 422 ▶ if (hour != TKR_CRON_ALL(0, 23)) r->by_hour = (uint32_t)hour;
Branch: 1 branch
BranchTrueFalseTotal
if (hour != TKR_CRON_ALL(0, 23)) r->by_hour = (uint32_t)hour; 315 107 422
180 422 ▶ if (dom != TKR_CRON_ALL(1, 31)) r->by_monthday = dom;
Branch: 1 branch
BranchTrueFalseTotal
if (dom != TKR_CRON_ALL(1, 31)) r->by_monthday = dom; 193 229 422
181 422 ▶ if (mon != TKR_CRON_ALL(1, 12)) r->by_month = (uint16_t)mon;
Branch: 1 branch
BranchTrueFalseTotal
if (mon != TKR_CRON_ALL(1, 12)) r->by_month = (uint16_t)mon; 172 250 422
182 422 ▶ if (dow != TKR_CRON_ALL(0, 6)) {
Branch: 1 branch
BranchTrueFalseTotal
if (dow != TKR_CRON_ALL(0, 6)) { 209 213 422
183 1672 ▶ for (i = 0; i < 7; i++) /* cron 0 Sunday .. 6 Saturday to 0 Monday .. 6 Sunday */
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 7; i++) /* cron 0 Sunday .. 6 Saturday to 0 Monday .. 6 Sunday */ 1463 209 1672
184 1463 ▶ if (dow & (UINT64_C(1) << i))
Branch: 1 branch
BranchTrueFalseTotal
if (dow & (UINT64_C(1) << i)) 481 982 1463
185 481 ▶ r->by_day_any |= (uint8_t)(1u << (i == 0 ? 6 : i - 1));
Branch: 1 branch
BranchTrueFalseTotal
r->by_day_any |= (uint8_t)(1u << (i == 0 ? 6 : i - 1)); 381 100 481
186 }
187 422 ▶ if (r->by_monthday && r->by_day_any)
Branch: 2 branches
BranchTrueFalseTotal
if (r->by_monthday && r->by_day_any) 193 229 422
if (r->by_monthday && r->by_day_any) 111 82 193
188 111 r->flags |= TKR_F_VIXIE;
189 422 return 1;
190 }
191  
192 /* ---- writing ------------------------------------------------------------- */
193  
194 /* A mask as a cron field: * when it is everything, else runs a-b and lone
195 * values, comma separated, ascending. */
196 static void
197 300 tkr_cron_field_write(pTHX_ SV *out, uint64_t mask, int lo, int hi)
198 {
199 300 int v, first = 1;
200 300 ▶ if (!mask || mask == TKR_CRON_ALL(lo, hi)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!mask || mask == TKR_CRON_ALL(lo, hi)) { 150 150 300
if (!mask || mask == TKR_CRON_ALL(lo, hi)) { 0 150 150
201 150 sv_catpvs(out, "*");
202 150 return;
203 }
204 5372 ▶ for (v = lo; v <= hi; v++) {
Branch: 1 branch
BranchTrueFalseTotal
for (v = lo; v <= hi; v++) { 5222 150 5372
205 int e;
206 5222 ▶ if (!(mask & (UINT64_C(1) << v)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(mask & (UINT64_C(1) << v))) 5039 183 5222
207 5039 continue;
208 183 e = v;
209 228 ▶ while (e + 1 <= hi && (mask & (UINT64_C(1) << (e + 1))))
Branch: 2 branches
BranchTrueFalseTotal
while (e + 1 <= hi && (mask & (UINT64_C(1) << (e + 1)))) 225 3 228
while (e + 1 <= hi && (mask & (UINT64_C(1) << (e + 1)))) 45 180 225
210 45 e++;
211 183 ▶ if (e > v + 1) sv_catpvf(out, first ? "%d-%d" : ",%d-%d", v, e);
Branch: 2 branches
BranchTrueFalseTotal
if (e > v + 1) sv_catpvf(out, first ? "%d-%d" : ",%d-%d", v, e); 10 173 183
if (e > v + 1) sv_catpvf(out, first ? "%d-%d" : ",%d-%d", v, e); 10 0 10
212 173 ▶ else if (e == v + 1) sv_catpvf(out, first ? "%d,%d" : ",%d,%d", v, e);
Branch: 2 branches
BranchTrueFalseTotal
else if (e == v + 1) sv_catpvf(out, first ? "%d,%d" : ",%d,%d", v, e); 4 169 173
else if (e == v + 1) sv_catpvf(out, first ? "%d,%d" : ",%d,%d", v, e); 2 2 4
213 169 ▶ else sv_catpvf(out, first ? "%d" : ",%d", v);
Branch: 1 branch
BranchTrueFalseTotal
else sv_catpvf(out, first ? "%d" : ",%d", v); 138 31 169
214 183 first = 0;
215 183 v = e;
216 }
217 }
218  
219 /* 1 with the expression in out, or 0 with err saying why the rule has no
220 * cron spelling. A rule is a cron when it steps minutes (or days with its
221 * hour and minute given, or hours with its minute given), has no seconds,
222 * ordinals, year days, week numbers, set positions, UNTIL or COUNT, and
223 * either one restricted day field or the cron reading of both. */
224 static int
225 77 tkr_cron_write(pTHX_ const tkr_rule *r, SV *out, char *err)
226 {
227 77 uint64_t dow = 0;
228 int i;
229 77 ▶ if (r->flags & TKR_F_RSCALE) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->flags & TKR_F_RSCALE) { 1 76 77
230 1 tkr_err(err, "a rule in a calendar (RSCALE) has no cron spelling");
231 1 return 0;
232 }
233 76 ▶ if (r->flags & TKR_F_EVERY) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->flags & TKR_F_EVERY) { 7 69 76
234 7 int64_t s = r->every_sec;
235 7 ▶ if (s % 86400 == 0) sv_setpvf(out, "@every %" IVdf "d", (IV)(s / 86400));
Branch: 1 branch
BranchTrueFalseTotal
if (s % 86400 == 0) sv_setpvf(out, "@every %" IVdf "d", (IV)(s / 86400)); 1 6 7
236 6 ▶ else if (s % 3600 == 0) sv_setpvf(out, "@every %" IVdf "h", (IV)(s / 3600));
Branch: 1 branch
BranchTrueFalseTotal
else if (s % 3600 == 0) sv_setpvf(out, "@every %" IVdf "h", (IV)(s / 3600)); 1 5 6
237 5 ▶ else if (s % 60 == 0) sv_setpvf(out, "@every %" IVdf "m", (IV)(s / 60));
Branch: 1 branch
BranchTrueFalseTotal
else if (s % 60 == 0) sv_setpvf(out, "@every %" IVdf "m", (IV)(s / 60)); 4 1 5
238 1 else sv_setpvf(out, "@every %" IVdf "s", (IV)s);
239 7 return 1;
240 }
241 69 ▶ if (r->freq == TKR_DAILY && !(r->by_hour && r->by_minute)) { tkr_err(err, "a daily rule is a cron only with BYHOUR and BYMINUTE given"); return 0; }
Branch: 3 branches
BranchTrueFalseTotal
if (r->freq == TKR_DAILY && !(r->by_hour && r->by_minute)) { tkr_err(err, "a daily rule is a cron only with BYHOUR and BYMINUTE given"); return 0; } 3 66 69
if (r->freq == TKR_DAILY && !(r->by_hour && r->by_minute)) { tkr_err(err, "a daily rule is a cron only with BYHOUR and BYMINUTE given"); return 0; } 2 1 3
if (r->freq == TKR_DAILY && !(r->by_hour && r->by_minute)) { tkr_err(err, "a daily rule is a cron only with BYHOUR and BYMINUTE given"); return 0; } 0 2 2
242 68 ▶ if (r->freq == TKR_HOURLY && !r->by_minute) { tkr_err(err, "an hourly rule is a cron only with BYMINUTE given"); return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (r->freq == TKR_HOURLY && !r->by_minute) { tkr_err(err, "an hourly rule is a cron only with BYMINUTE given"); return 0; } 2 66 68
if (r->freq == TKR_HOURLY && !r->by_minute) { tkr_err(err, "an hourly rule is a cron only with BYMINUTE given"); return 0; } 1 1 2
243 67 ▶ if (r->freq != TKR_MINUTELY && r->freq != TKR_HOURLY && r->freq != TKR_DAILY) { tkr_err(err, "a cron steps minutes, hours or days, not %s", tkr_freq_lower[r->freq]); return 0; }
Branch: 3 branches
BranchTrueFalseTotal
if (r->freq != TKR_MINUTELY && r->freq != TKR_HOURLY && r->freq != TKR_DAILY) { tkr_err(err, "a cron steps minutes, hours or days, not %s", tkr_freq_lower[r->freq]); return 0; } 4 63 67
if (r->freq != TKR_MINUTELY && r->freq != TKR_HOURLY && r->freq != TKR_DAILY) { tkr_err(err, "a cron steps minutes, hours or days, not %s", tkr_freq_lower[r->freq]); return 0; } 3 1 4
if (r->freq != TKR_MINUTELY && r->freq != TKR_HOURLY && r->freq != TKR_DAILY) { tkr_err(err, "a cron steps minutes, hours or days, not %s", tkr_freq_lower[r->freq]); return 0; } 1 2 3
244 66 ▶ if (r->interval != 1) { tkr_err(err, "a cron has no INTERVAL"); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (r->interval != 1) { tkr_err(err, "a cron has no INTERVAL"); return 0; } 1 65 66
245 65 ▶ if (r->by_second) { tkr_err(err, "a cron has no seconds"); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (r->by_second) { tkr_err(err, "a cron has no seconds"); return 0; } 1 64 65
246 64 ▶ if (tkr_has_by_day_ordinal(r)) { tkr_err(err, "a cron has no ordinal weekdays (the nth Friday)"); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (tkr_has_by_day_ordinal(r)) { tkr_err(err, "a cron has no ordinal weekdays (the nth Friday)"); return 0; } 0 64 64
247 64 ▶ if (r->n_yearday || r->n_weekno || r->n_setpos) { tkr_err(err, "a cron has no BYYEARDAY, BYWEEKNO or BYSETPOS"); return 0; }
Branch: 3 branches
BranchTrueFalseTotal
if (r->n_yearday || r->n_weekno || r->n_setpos) { tkr_err(err, "a cron has no BYYEARDAY, BYWEEKNO or BYSETPOS"); return 0; } 64 0 64
if (r->n_yearday || r->n_weekno || r->n_setpos) { tkr_err(err, "a cron has no BYYEARDAY, BYWEEKNO or BYSETPOS"); return 0; } 64 0 64
if (r->n_yearday || r->n_weekno || r->n_setpos) { tkr_err(err, "a cron has no BYYEARDAY, BYWEEKNO or BYSETPOS"); return 0; } 1 63 64
248 63 ▶ if (r->count || r->until_kind != TKR_UNTIL_NONE) { tkr_err(err, "a cron has no end: no COUNT or UNTIL"); return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (r->count || r->until_kind != TKR_UNTIL_NONE) { tkr_err(err, "a cron has no end: no COUNT or UNTIL"); return 0; } 62 1 63
if (r->count || r->until_kind != TKR_UNTIL_NONE) { tkr_err(err, "a cron has no end: no COUNT or UNTIL"); return 0; } 0 62 62
249 62 ▶ if (r->by_monthday & ~TKR_CRON_ALL(1, 31)) { tkr_err(err, "a cron has no days counted from the end of the month"); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (r->by_monthday & ~TKR_CRON_ALL(1, 31)) { tkr_err(err, "a cron has no days counted from the end of the month"); return 0; } 1 61 62
250 61 ▶ if (r->by_monthday && r->by_day_any && !(r->flags & TKR_F_VIXIE)) {
Branch: 3 branches
BranchTrueFalseTotal
if (r->by_monthday && r->by_day_any && !(r->flags & TKR_F_VIXIE)) { 17 44 61
if (r->by_monthday && r->by_day_any && !(r->flags & TKR_F_VIXIE)) { 10 7 17
if (r->by_monthday && r->by_day_any && !(r->flags & TKR_F_VIXIE)) { 1 9 10
251 1 tkr_err(err, "this rule has no cron spelling: cron reads both day fields as either, not both");
252 1 return 0;
253 }
254 60 sv_setpvs(out, "");
255 60 tkr_cron_field_write(aTHX_ out, r->by_minute, 0, 59);
256 60 sv_catpvs(out, " ");
257 60 tkr_cron_field_write(aTHX_ out, r->by_hour, 0, 23);
258 60 sv_catpvs(out, " ");
259 60 tkr_cron_field_write(aTHX_ out, r->by_monthday, 1, 31);
260 60 sv_catpvs(out, " ");
261 60 tkr_cron_field_write(aTHX_ out, r->by_month, 1, 12);
262 60 sv_catpvs(out, " ");
263 480 ▶ for (i = 0; i < 7; i++) /* 0 Monday .. 6 Sunday back to cron's 0 Sunday .. 6 Saturday */
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 7; i++) /* 0 Monday .. 6 Sunday back to cron's 0 Sunday .. 6 Saturday */ 420 60 480
264 420 ▶ if (r->by_day_any & (1u << i))
Branch: 1 branch
BranchTrueFalseTotal
if (r->by_day_any & (1u << i)) 72 348 420
265 72 ▶ dow |= UINT64_C(1) << (i == 6 ? 0 : i + 1);
Branch: 1 branch
BranchTrueFalseTotal
dow |= UINT64_C(1) << (i == 6 ? 0 : i + 1); 63 9 72
266 60 tkr_cron_field_write(aTHX_ out, dow, 0, 6);
267 60 return 1;
268 }
269  
270 #endif
« include/tkr/tkr_walk.h ↑ Summary include/tkr/tkr_zone.h »