-
Notifications
You must be signed in to change notification settings - Fork 1
/
ccronexpr.c
1291 lines (1160 loc) · 36.7 KB
/
ccronexpr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2015, alex at staticlibs.net
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* File: ccronexpr.c
* Author: alex
*
* Created on February 24, 2015, 9:35 AM
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <math.h>
#include "ccronexpr.h"
#define CRON_MAX_SECONDS 60
#define CRON_MAX_MINUTES 60
#define CRON_MAX_HOURS 24
#define CRON_MAX_DAYS_OF_WEEK 8
#define CRON_MAX_DAYS_OF_MONTH 32
#define CRON_MAX_MONTHS 12
#define CRON_MAX_YEARS_DIFF 4
#define CRON_CF_SECOND 0
#define CRON_CF_MINUTE 1
#define CRON_CF_HOUR_OF_DAY 2
#define CRON_CF_DAY_OF_WEEK 3
#define CRON_CF_DAY_OF_MONTH 4
#define CRON_CF_MONTH 5
#define CRON_CF_YEAR 6
#define CRON_CF_ARR_LEN 7
#define CRON_INVALID_INSTANT ((time_t) -1)
static const char* const DAYS_ARR[] = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
#define CRON_DAYS_ARR_LEN 7
static const char* const MONTHS_ARR[] = { "FOO", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
#define CRON_MONTHS_ARR_LEN 13
#define CRON_MAX_STR_LEN_TO_SPLIT 256
#define CRON_MAX_NUM_TO_SRING 1000000000
/* computes number of digits in decimal number */
#define CRON_NUM_OF_DIGITS(num) (abs(num) < 10 ? 1 : \
(abs(num) < 100 ? 2 : \
(abs(num) < 1000 ? 3 : \
(abs(num) < 10000 ? 4 : \
(abs(num) < 100000 ? 5 : \
(abs(num) < 1000000 ? 6 : \
(abs(num) < 10000000 ? 7 : \
(abs(num) < 100000000 ? 8 : \
(abs(num) < 1000000000 ? 9 : 10)))))))))
#ifndef CRON_TEST_MALLOC
#define cron_malloc(x) malloc(x);
#define cron_free(x) free(x);
#else /* CRON_TEST_MALLOC */
void* cron_malloc(size_t n);
void cron_free(void* p);
#endif /* CRON_TEST_MALLOC */
/**
* Time functions from standard library.
* This part defines: cron_mktime: create time_t from tm
* cron_time: create tm from time_t
*/
/* forward declarations for platforms that may need them */
/* can be hidden in time.h */
#if !defined(_WIN32) && !defined(__AVR__) && !defined(ESP8266) && !defined(ANDROID)
struct tm *gmtime_r(const time_t *timep, struct tm *result);
time_t timegm(struct tm* __tp);
struct tm *localtime_r(const time_t *timep, struct tm *result);
#endif /* PLEASE CHECK _WIN32 AND ANDROID NEEDS FOR THESE DECLARATIONS */
#ifdef __MINGW32__
/* To avoid warning when building with mingw */
time_t _mkgmtime(struct tm* tm);
#endif /* __MINGW32__ */
/* function definitions */
time_t cron_mktime_gm(struct tm* tm) {
#if defined(_WIN32)
/* http://stackoverflow.com/a/22557778 */
return _mkgmtime(tm);
#elif defined(__AVR__)
/* https://www.nongnu.org/avr-libc/user-manual/group__avr__time.html */
return mk_gmtime(tm);
#elif defined(ESP8266)
/* https://linux.die.net/man/3/timegm */
/* http://www.catb.org/esr/time-programming/ */
/* portable version of timegm() */
time_t ret;
char *tz;
tz = getenv("TZ");
if (tz)
tz = strdup(tz);
setenv("TZ", "UTC+0", 1);
tzset();
ret = mktime(tm);
if (tz) {
setenv("TZ", tz, 1);
free(tz);
} else
unsetenv("TZ");
tzset();
return ret;
#elif defined(ANDROID)
/* https://github.com/adobe/chromium/blob/cfe5bf0b51b1f6b9fe239c2a3c2f2364da9967d7/base/os_compat_android.cc#L20 */
static const time_t kTimeMax = ~(1L << (sizeof (time_t) * CHAR_BIT - 1));
static const time_t kTimeMin = (1L << (sizeof (time_t) * CHAR_BIT - 1));
time64_t result = timegm64(tm);
if (result < kTimeMin || result > kTimeMax) return -1;
return result;
#else
return timegm(tm);
#endif
}
struct tm* cron_time_gm(time_t* date, struct tm* out) {
#if defined(__MINGW32__)
(void)(out); /* To avoid unused warning */
return gmtime(date);
#elif defined(_WIN32)
errno_t err = gmtime_s(out, date);
return 0 == err ? out : NULL;
#elif defined(__AVR__)
/* https://www.nongnu.org/avr-libc/user-manual/group__avr__time.html */
gmtime_r(date, out);
return out;
#else
return gmtime_r(date, out);
#endif
}
time_t cron_mktime_local(struct tm* tm) {
tm->tm_isdst = -1;
return mktime(tm);
}
struct tm* cron_time_local(time_t* date, struct tm* out) {
#if defined(_WIN32)
errno_t err = localtime_s(out, date);
return 0 == err ? out : NULL;
#elif defined(__AVR__)
/* https://www.nongnu.org/avr-libc/user-manual/group__avr__time.html */
localtime_r(date, out);
return out;
#else
return localtime_r(date, out);
#endif
}
/* Defining 'cron_' time functions to use use UTC (default) or local time */
#ifndef CRON_USE_LOCAL_TIME
time_t cron_mktime(struct tm* tm) {
return cron_mktime_gm(tm);
}
struct tm* cron_time(time_t* date, struct tm* out) {
return cron_time_gm(date, out);
}
#else /* CRON_USE_LOCAL_TIME */
time_t cron_mktime(struct tm* tm) {
return cron_mktime_local(tm);
}
struct tm* cron_time(time_t* date, struct tm* out) {
return cron_time_local(date, out);
}
#endif /* CRON_USE_LOCAL_TIME */
/**
* Functions.
*/
void cron_set_bit(uint8_t* rbyte, int idx) {
uint8_t j = (uint8_t) (idx / 8);
uint8_t k = (uint8_t) (idx % 8);
rbyte[j] |= (1 << k);
}
void cron_del_bit(uint8_t* rbyte, int idx) {
uint8_t j = (uint8_t) (idx / 8);
uint8_t k = (uint8_t) (idx % 8);
rbyte[j] &= ~(1 << k);
}
uint8_t cron_get_bit(uint8_t* rbyte, int idx) {
uint8_t j = (uint8_t) (idx / 8);
uint8_t k = (uint8_t) (idx % 8);
if (rbyte[j] & (1 << k)) {
return 1;
} else {
return 0;
}
}
static void free_splitted(char** splitted, size_t len) {
size_t i;
if (!splitted) return;
for (i = 0; i < len; i++) {
if (splitted[i]) {
cron_free(splitted[i]);
}
}
cron_free(splitted);
}
static char* strdupl(const char* str, size_t len) {
if (!str) return NULL;
char* res = (char*) cron_malloc(len + 1);
if (!res) return NULL;
memset(res, 0, len + 1);
memcpy(res, str, len);
return res;
}
static unsigned int next_set_bit(uint8_t* bits, unsigned int max, unsigned int from_index, int* notfound) {
unsigned int i;
if (!bits) {
*notfound = 1;
return 0;
}
for (i = from_index; i < max; i++) {
if (cron_get_bit(bits, i)) return i;
}
*notfound = 1;
return 0;
}
static void push_to_fields_arr(int* arr, int fi) {
int i;
if (!arr || -1 == fi) {
return;
}
for (i = 0; i < CRON_CF_ARR_LEN; i++) {
if (arr[i] == fi) return;
}
for (i = 0; i < CRON_CF_ARR_LEN; i++) {
if (-1 == arr[i]) {
arr[i] = fi;
return;
}
}
}
static int add_to_field(struct tm* calendar, int field, int val) {
if (!calendar || -1 == field) {
return 1;
}
switch (field) {
case CRON_CF_SECOND:
calendar->tm_sec = calendar->tm_sec + val;
break;
case CRON_CF_MINUTE:
calendar->tm_min = calendar->tm_min + val;
break;
case CRON_CF_HOUR_OF_DAY:
calendar->tm_hour = calendar->tm_hour + val;
break;
case CRON_CF_DAY_OF_WEEK: /* mkgmtime ignores this field */
case CRON_CF_DAY_OF_MONTH:
calendar->tm_mday = calendar->tm_mday + val;
break;
case CRON_CF_MONTH:
calendar->tm_mon = calendar->tm_mon + val;
break;
case CRON_CF_YEAR:
calendar->tm_year = calendar->tm_year + val;
break;
default:
return 1; /* unknown field */
}
time_t res = cron_mktime(calendar);
if (CRON_INVALID_INSTANT == res) {
return 1;
}
return 0;
}
/**
* Reset the calendar setting all the fields provided to zero.
*/
static int reset_min(struct tm* calendar, int field) {
if (!calendar || -1 == field) {
return 1;
}
switch (field) {
case CRON_CF_SECOND:
calendar->tm_sec = 0;
break;
case CRON_CF_MINUTE:
calendar->tm_min = 0;
break;
case CRON_CF_HOUR_OF_DAY:
calendar->tm_hour = 0;
break;
case CRON_CF_DAY_OF_WEEK:
calendar->tm_wday = 0;
break;
case CRON_CF_DAY_OF_MONTH:
calendar->tm_mday = 1;
break;
case CRON_CF_MONTH:
calendar->tm_mon = 0;
break;
case CRON_CF_YEAR:
calendar->tm_year = 0;
break;
default:
return 1; /* unknown field */
}
time_t res = cron_mktime(calendar);
if (CRON_INVALID_INSTANT == res) {
return 1;
}
return 0;
}
static int reset_all_min(struct tm* calendar, int* fields) {
int i;
int res = 0;
if (!calendar || !fields) {
return 1;
}
for (i = 0; i < CRON_CF_ARR_LEN; i++) {
if (-1 != fields[i]) {
res = reset_min(calendar, fields[i]);
if (0 != res) return res;
}
}
return 0;
}
static int set_field(struct tm* calendar, int field, int val) {
if (!calendar || -1 == field) {
return 1;
}
switch (field) {
case CRON_CF_SECOND:
calendar->tm_sec = val;
break;
case CRON_CF_MINUTE:
calendar->tm_min = val;
break;
case CRON_CF_HOUR_OF_DAY:
calendar->tm_hour = val;
break;
case CRON_CF_DAY_OF_WEEK:
calendar->tm_wday = val;
break;
case CRON_CF_DAY_OF_MONTH:
calendar->tm_mday = val;
break;
case CRON_CF_MONTH:
calendar->tm_mon = val;
break;
case CRON_CF_YEAR:
calendar->tm_year = val;
break;
default:
return 1; /* unknown field */
}
time_t res = cron_mktime(calendar);
if (CRON_INVALID_INSTANT == res) {
return 1;
}
return 0;
}
/**
* Search the bits provided for the next set bit after the value provided,
* and reset the calendar.
*/
static unsigned int find_next(uint8_t* bits, unsigned int max, unsigned int value, struct tm* calendar, unsigned int field, unsigned int nextField, int* lower_orders, int* res_out) {
int notfound = 0;
int err = 0;
unsigned int next_value = next_set_bit(bits, max, value, ¬found);
/* roll over if needed */
if (notfound) {
err = add_to_field(calendar, nextField, 1);
if (err) goto return_error;
err = reset_min(calendar, field);
if (err) goto return_error;
notfound = 0;
next_value = next_set_bit(bits, max, 0, ¬found);
}
if (notfound || next_value != value) {
err = set_field(calendar, field, next_value);
if (err) goto return_error;
err = reset_all_min(calendar, lower_orders);
if (err) goto return_error;
}
return next_value;
return_error:
*res_out = 1;
return 0;
}
static unsigned int find_next_day(struct tm* calendar, uint8_t* days_of_month, unsigned int day_of_month, uint8_t* days_of_week, unsigned int day_of_week, int* resets, int* res_out) {
int err;
unsigned int count = 0;
unsigned int max = 366;
while ((!cron_get_bit(days_of_month, day_of_month) || !cron_get_bit(days_of_week, day_of_week)) && count++ < max) {
err = add_to_field(calendar, CRON_CF_DAY_OF_MONTH, 1);
if (err) goto return_error;
day_of_month = calendar->tm_mday;
day_of_week = calendar->tm_wday;
reset_all_min(calendar, resets);
}
return day_of_month;
return_error:
*res_out = 1;
return 0;
}
static int do_next(cron_expr* expr, struct tm* calendar, unsigned int dot) {
int i;
int res = 0;
int* resets = NULL;
int* empty_list = NULL;
unsigned int second = 0;
unsigned int update_second = 0;
unsigned int minute = 0;
unsigned int update_minute = 0;
unsigned int hour = 0;
unsigned int update_hour = 0;
unsigned int day_of_week = 0;
unsigned int day_of_month = 0;
unsigned int update_day_of_month = 0;
unsigned int month = 0;
unsigned int update_month = 0;
resets = (int*) cron_malloc(CRON_CF_ARR_LEN * sizeof(int));
if (!resets) goto return_result;
empty_list = (int*) cron_malloc(CRON_CF_ARR_LEN * sizeof(int));
if (!empty_list) goto return_result;
for (i = 0; i < CRON_CF_ARR_LEN; i++) {
resets[i] = -1;
empty_list[i] = -1;
}
second = calendar->tm_sec;
update_second = find_next(expr->seconds, CRON_MAX_SECONDS, second, calendar, CRON_CF_SECOND, CRON_CF_MINUTE, empty_list, &res);
if (0 != res) goto return_result;
if (second == update_second) {
push_to_fields_arr(resets, CRON_CF_SECOND);
}
minute = calendar->tm_min;
update_minute = find_next(expr->minutes, CRON_MAX_MINUTES, minute, calendar, CRON_CF_MINUTE, CRON_CF_HOUR_OF_DAY, resets, &res);
if (0 != res) goto return_result;
if (minute == update_minute) {
push_to_fields_arr(resets, CRON_CF_MINUTE);
} else {
res = do_next(expr, calendar, dot);
if (0 != res) goto return_result;
}
hour = calendar->tm_hour;
update_hour = find_next(expr->hours, CRON_MAX_HOURS, hour, calendar, CRON_CF_HOUR_OF_DAY, CRON_CF_DAY_OF_WEEK, resets, &res);
if (0 != res) goto return_result;
if (hour == update_hour) {
push_to_fields_arr(resets, CRON_CF_HOUR_OF_DAY);
} else {
res = do_next(expr, calendar, dot);
if (0 != res) goto return_result;
}
day_of_week = calendar->tm_wday;
day_of_month = calendar->tm_mday;
update_day_of_month = find_next_day(calendar, expr->days_of_month, day_of_month, expr->days_of_week, day_of_week, resets, &res);
if (0 != res) goto return_result;
if (day_of_month == update_day_of_month) {
push_to_fields_arr(resets, CRON_CF_DAY_OF_MONTH);
} else {
res = do_next(expr, calendar, dot);
if (0 != res) goto return_result;
}
month = calendar->tm_mon; /*day already adds one if no day in same month is found*/
update_month = find_next(expr->months, CRON_MAX_MONTHS, month, calendar, CRON_CF_MONTH, CRON_CF_YEAR, resets, &res);
if (0 != res) goto return_result;
if (month != update_month) {
if (calendar->tm_year - dot > 4) {
res = -1;
goto return_result;
}
res = do_next(expr, calendar, dot);
if (0 != res) goto return_result;
}
goto return_result;
return_result:
if (!resets || !empty_list) {
res = -1;
}
if (resets) {
cron_free(resets);
}
if (empty_list) {
cron_free(empty_list);
}
return res;
}
static int to_upper(char* str) {
if (!str) return 1;
int i;
for (i = 0; '\0' != str[i]; i++) {
int c = (int)str[i];
str[i] = (char) toupper(c);
}
return 0;
}
static char* to_string(int num) {
if (abs(num) >= CRON_MAX_NUM_TO_SRING) return NULL;
char* str = (char*) cron_malloc(CRON_NUM_OF_DIGITS(num) + 1);
if (!str) return NULL;
int res = sprintf(str, "%d", num);
if (res < 0) {
cron_free(str);
return NULL;
}
return str;
}
static char* str_replace(char *orig, const char *rep, const char *with) {
char *result; /* the return string */
char *ins; /* the next insert point */
char *tmp; /* varies */
size_t len_rep; /* length of rep */
size_t len_with; /* length of with */
size_t len_front; /* distance between rep and end of last rep */
int count; /* number of replacements */
if (!orig) return NULL;
if (!rep) rep = "";
if (!with) with = "";
len_rep = strlen(rep);
len_with = strlen(with);
ins = orig;
for (count = 0; NULL != (tmp = strstr(ins, rep)); ++count) {
ins = tmp + len_rep;
}
/* first time through the loop, all the variable are set correctly
from here on,
tmp points to the end of the result string
ins points to the next occurrence of rep in orig
orig points to the remainder of orig after "end of rep"
*/
tmp = result = (char*) cron_malloc(strlen(orig) + (len_with - len_rep) * count + 1);
if (!result) return NULL;
while (count--) {
ins = strstr(orig, rep);
len_front = ins - orig;
tmp = strncpy(tmp, orig, len_front) + len_front;
tmp = strcpy(tmp, with) + len_with;
orig += len_front + len_rep; /* move to next "end of rep" */
}
strcpy(tmp, orig);
return result;
}
static unsigned int parse_uint(const char* str, int* errcode) {
char* endptr;
errno = 0;
long int l = strtol(str, &endptr, 0);
if (errno == ERANGE || *endptr != '\0' || l < 0 || l > INT_MAX) {
*errcode = 1;
return 0;
} else {
*errcode = 0;
return (unsigned int) l;
}
}
static char** split_str(const char* str, char del, size_t* len_out) {
size_t i;
size_t stlen = 0;
size_t len = 0;
int accum = 0;
char* buf = NULL;
char** res = NULL;
size_t bi = 0;
size_t ri = 0;
char* tmp;
if (!str) goto return_error;
for (i = 0; '\0' != str[i]; i++) {
stlen += 1;
if (stlen >= CRON_MAX_STR_LEN_TO_SPLIT) goto return_error;
}
for (i = 0; i < stlen; i++) {
int c = str[i];
if (del == str[i]) {
if (accum > 0) {
len += 1;
accum = 0;
}
} else if (!isspace(c)) {
accum += 1;
}
}
/* tail */
if (accum > 0) {
len += 1;
}
if (0 == len) return NULL;
buf = (char*) cron_malloc(stlen + 1);
if (!buf) goto return_error;
memset(buf, 0, stlen + 1);
res = (char**) cron_malloc(len * sizeof(char*));
if (!res) goto return_error;
memset(res, 0, len * sizeof(char*));
for (i = 0; i < stlen; i++) {
int c = str[i];
if (del == str[i]) {
if (bi > 0) {
tmp = strdupl(buf, bi);
if (!tmp) goto return_error;
res[ri++] = tmp;
memset(buf, 0, stlen + 1);
bi = 0;
}
} else if (!isspace(c)) {
buf[bi++] = str[i];
}
}
/* tail */
if (bi > 0) {
tmp = strdupl(buf, bi);
if (!tmp) goto return_error;
res[ri++] = tmp;
}
cron_free(buf);
*len_out = len;
return res;
return_error:
if (buf) {
cron_free(buf);
}
free_splitted(res, len);
*len_out = 0;
return NULL;
}
static char* replace_ordinals(char* value, const char* const * arr, size_t arr_len) {
size_t i;
char* cur = value;
char* res = NULL;
int first = 1;
for (i = 0; i < arr_len; i++) {
char* strnum = to_string((int) i);
if (!strnum) {
if (!first) {
cron_free(cur);
}
return NULL;
}
res = str_replace(cur, arr[i], strnum);
cron_free(strnum);
if (!first) {
cron_free(cur);
}
if (!res) {
return NULL;
}
cur = res;
if (first) {
first = 0;
}
}
return res;
}
static int has_char(char* str, char ch) {
size_t i;
size_t len = 0;
if (!str) return 0;
len = strlen(str);
for (i = 0; i < len; i++) {
if (str[i] == ch) return 1;
}
return 0;
}
static unsigned int* get_range(char* field, unsigned int min, unsigned int max, const char** error) {
char** parts = NULL;
size_t len = 0;
unsigned int* res = (unsigned int*) cron_malloc(2 * sizeof(unsigned int));
if (!res) goto return_error;
if (!field) goto return_error;
res[0] = 0;
res[1] = 0;
if (1 == strlen(field) && '*' == field[0]) {
res[0] = min;
res[1] = max - 1;
} else if (!(has_char(field, '-') || has_char(field, '~'))) {
int err = 0;
unsigned int val = parse_uint(field, &err);
if (err) {
*error = "Unsigned integer parse error 1";
goto return_error;
}
res[0] = val;
res[1] = val;
} else {
if (has_char(field, '-')) {
parts = split_str(field, '-', &len);
}
else if (has_char(field, '~')) {
parts = split_str(field, '~', &len);
}
if (2 != len) {
*error = "Specified range requires two fields";
goto return_error;
}
int err = 0;
res[0] = parse_uint(parts[0], &err);
if (err) {
*error = "Unsigned integer parse error 2";
goto return_error;
}
res[1] = parse_uint(parts[1], &err);
if (err) {
*error = "Unsigned integer parse error 3";
goto return_error;
}
}
if (res[0] >= max || res[1] >= max) {
*error = "Specified range exceeds maximum";
goto return_error;
}
if (res[0] < min || res[1] < min) {
*error = "Specified range is less than minimum";
goto return_error;
}
if (res[0] > res[1]) {
*error = "Specified range start exceeds range end";
goto return_error;
}
free_splitted(parts, len);
*error = NULL;
return res;
return_error:
free_splitted(parts, len);
if (res) {
cron_free(res);
}
return NULL;
}
static void set_number_hits(const char* value, uint8_t* target, unsigned int min, unsigned int max, const char** error) {
size_t i;
unsigned int i1;
size_t len = 0;
char** fields = split_str(value, ',', &len);
if (!fields) {
*error = "Comma split error";
goto return_result;
}
for (i = 0; i < len; i++) {
if (!has_char(fields[i], '/')) {
/* Not an incrementer so it must be a range (possibly empty) */
unsigned int* range = get_range(fields[i], min, max, error);
if (*error) {
if (range) {
cron_free(range);
}
goto return_result;
}
if (has_char(fields[i], '~')) {
i1 = (random() % (range[1] - range[0] + 1)) + range[0];
cron_set_bit(target, i1);
}
else {
for (i1 = range[0]; i1 <= range[1]; i1++) {
cron_set_bit(target, i1);
}
}
cron_free(range);
} else {
size_t len2 = 0;
char** split = split_str(fields[i], '/', &len2);
if (2 != len2) {
*error = "Incrementer must have two fields";
free_splitted(split, len2);
goto return_result;
}
unsigned int* range = get_range(split[0], min, max, error);
if (*error) {
if (range) {
cron_free(range);
}
free_splitted(split, len2);
goto return_result;
}
if (!(has_char(split[0], '-') || has_char(split[0], '~'))) {
range[1] = max - 1;
}
int err = 0;
unsigned int delta = parse_uint(split[1], &err);
if (err) {
*error = "Unsigned integer parse error 4";
cron_free(range);
free_splitted(split, len2);
goto return_result;
}
if (0 == delta) {
*error = "Incrementer may not be zero";
cron_free(range);
free_splitted(split, len2);
goto return_result;
}
if (has_char(fields[i], '~')) {
i1 = ((random() % (range[1] - range[0] + 1)) / delta) * delta +
range[0];
cron_set_bit(target, i1);
}
else {
for (i1 = range[0]; i1 <= range[1]; i1 += delta) {
cron_set_bit(target, i1);
}
}
free_splitted(split, len2);
cron_free(range);
}
}
goto return_result;
return_result:
free_splitted(fields, len);
}
static void set_months(char* value, uint8_t* targ, const char** error) {
unsigned int i;
unsigned int max = 12;
char* replaced = NULL;
to_upper(value);
replaced = replace_ordinals(value, MONTHS_ARR, CRON_MONTHS_ARR_LEN);
if (!replaced) {
*error = "Invalid month format";
return;
}
set_number_hits(replaced, targ, 1, max + 1, error);
cron_free(replaced);
/* ... and then rotate it to the front of the months */
for (i = 1; i <= max; i++) {
if (cron_get_bit(targ, i)) {
cron_set_bit(targ, i - 1);
cron_del_bit(targ, i);
}
}
}
static void set_days_of_week(char* field, uint8_t* targ, const char** error) {
unsigned int max = 7;
char* replaced = NULL;
if (1 == strlen(field) && '?' == field[0]) {
field[0] = '*';
}
to_upper(field);
replaced = replace_ordinals(field, DAYS_ARR, CRON_DAYS_ARR_LEN);
if (!replaced) {
*error = "Invalid day format";
return;
}
set_number_hits(replaced, targ, 0, max + 1, error);
cron_free(replaced);
if (cron_get_bit(targ, 7)) {
/* Sunday can be represented as 0 or 7*/
cron_set_bit(targ, 0);
cron_del_bit(targ, 7);
}
}
static void set_days_of_month(char* field, uint8_t* targ, const char** error) {
/* Days of month start with 1 (in Cron and Calendar) so add one */
if (1 == strlen(field) && '?' == field[0]) {
field[0] = '*';
}
set_number_hits(field, targ, 1, CRON_MAX_DAYS_OF_MONTH, error);
}
void cron_parse_expr(const char* expression, cron_expr* target, const char** error) {
const char* err_local;
size_t len = 0;
char** fields = NULL;
if (!error) {
error = &err_local;
}
*error = NULL;
if (!expression) {
*error = "Invalid NULL expression";
goto return_res;
}
if (!target) {
*error = "Invalid NULL target";
goto return_res;
}
fields = split_str(expression, ' ', &len);
if (len != 6) {
*error = "Invalid number of fields, expression must consist of 6 fields";
goto return_res;
}
memset(target, 0, sizeof(*target));
set_number_hits(fields[0], target->seconds, 0, 60, error);
if (*error) goto return_res;
set_number_hits(fields[1], target->minutes, 0, 60, error);
if (*error) goto return_res;
set_number_hits(fields[2], target->hours, 0, 24, error);
if (*error) goto return_res;
set_days_of_month(fields[3], target->days_of_month, error);
if (*error) goto return_res;
set_months(fields[4], target->months, error);
if (*error) goto return_res;
set_days_of_week(fields[5], target->days_of_week, error);
if (*error) goto return_res;
goto return_res;
return_res:
free_splitted(fields, len);
}
time_t cron_next(cron_expr* expr, time_t date) {
/*
The plan:
1 Round up to the next whole second
2 If seconds match move on, otherwise find the next match:
2.1 If next match is in the next minute then roll forwards
3 If minute matches move on, otherwise find the next match
3.1 If next match is in the next hour then roll forwards
3.2 Reset the seconds and go to 2
4 If hour matches move on, otherwise find the next match
4.1 If next match is in the next day then roll forwards,
4.2 Reset the minutes and seconds and go to 2
...
*/
if (!expr) return CRON_INVALID_INSTANT;