Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2001-2003 FhG Fokus |
3 | | * Copyright (C) 2020 OpenSIPS Solutions |
4 | | * |
5 | | * This file is part of opensips, a free SIP server. |
6 | | * |
7 | | * opensips is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 2 of the License, or |
10 | | * (at your option) any later version |
11 | | * |
12 | | * opensips is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | |
22 | | #include <stdio.h> |
23 | | #include <stdint.h> |
24 | | #include <string.h> |
25 | | #include <time.h> |
26 | | |
27 | | #include "mem/mem.h" |
28 | | #include "mem/shm_mem.h" |
29 | | #include "lib/osips_malloc.h" |
30 | | #include "time_rec.h" |
31 | | #include "ut.h" |
32 | | |
33 | | /* |
34 | | * USE_YWEEK_U -- Sunday system - see strftime %U |
35 | | * USE_YWEEK_V -- ISO 8601 - see strftime %V |
36 | | * USE_YWEEK_W -- Monday system - see strftime %W |
37 | | */ |
38 | | |
39 | | #ifndef USE_YWEEK_U |
40 | | #ifndef USE_YWEEK_V |
41 | | #ifndef USE_YWEEK_W |
42 | | #define USE_YWEEK_W /* Monday system */ |
43 | | #endif |
44 | | #endif |
45 | | #endif |
46 | | |
47 | 0 | #define WDAY_SU 0 |
48 | 0 | #define WDAY_MO 1 |
49 | 0 | #define WDAY_TU 2 |
50 | 0 | #define WDAY_WE 3 |
51 | 0 | #define WDAY_TH 4 |
52 | 0 | #define WDAY_FR 5 |
53 | 0 | #define WDAY_SA 6 |
54 | | #define WDAY_NU 7 |
55 | | |
56 | 0 | #define SEC_DAILY (60 * 60 * 24) |
57 | 0 | #define SEC_WEEKLY (7 * SEC_DAILY) |
58 | 0 | #define SEC_MONTHLY_MAX (31 * SEC_DAILY) |
59 | 0 | #define SEC_YEARLY_MAX (366 * SEC_DAILY) |
60 | | |
61 | | /* Keep the open-ended recurrence horizon representable on both 32-bit and |
62 | | * 64-bit platforms. Do not cast UINT32_MAX directly to a 32-bit time_t: it |
63 | | * becomes -1, which is TR_NOVAL. */ |
64 | | #define TR_DTEND_MAX \ |
65 | 0 | ((time_t)(sizeof(time_t) > sizeof(uint32_t) ? UINT32_MAX : INT32_MAX)) |
66 | | |
67 | | #ifdef USE_YWEEK_U |
68 | | #define SUN_WEEK(t) (int)(((t)->tm_yday + 7 - \ |
69 | | ((t)->tm_wday)) / 7) |
70 | | #else |
71 | 0 | #define MON_WEEK(t) (int)(((t)->tm_yday + 7 - \ |
72 | 0 | ((t)->tm_wday ? (t)->tm_wday - 1 : 6)) / 7) |
73 | | #endif |
74 | | |
75 | 0 | #define ac_get_wday_yr(t) (int)((t)->tm_yday/7) |
76 | 0 | #define ac_get_wday_mr(t) (int)(((t)->tm_mday-1)/7) |
77 | | |
78 | 0 | #define is_leap_year(yyyy) ((((yyyy)%400))?(((yyyy)%100)?(((yyyy)%4)?0:1):0):1) |
79 | | |
80 | | #define REC_ERR -1 |
81 | 0 | #define REC_MATCH 0 |
82 | 0 | #define REC_NOMATCH 1 |
83 | | |
84 | 0 | #define _IS_SET(x) ((x) != TR_NOVAL) |
85 | 0 | #define _D(c) ((c) -'0') |
86 | | |
87 | 0 | #define TR_SEPARATOR '|' |
88 | | |
89 | | #define load_TR_value( _p,_s, _tr, _func, _err, _done) \ |
90 | 0 | do{ \ |
91 | 0 | int _rc = 0; \ |
92 | 0 | _s = strchr(_p, (int)TR_SEPARATOR); \ |
93 | 0 | if (_s) \ |
94 | 0 | *_s = 0; \ |
95 | 0 | /* LM_DBG("----parsing tr param <%s>\n",_p); \ */\ |
96 | 0 | if(_s != _p) {\ |
97 | 0 | _rc = _func( _tr, _p); \ |
98 | 0 | if (_rc < 0) {\ |
99 | 0 | LM_DBG("func error\n"); \ |
100 | 0 | if (_s) *_s = TR_SEPARATOR; \ |
101 | 0 | goto _err; \ |
102 | 0 | } \ |
103 | 0 | } \ |
104 | 0 | if (_s) { \ |
105 | 0 | *_s = TR_SEPARATOR; \ |
106 | 0 | if (_rc == 0) \ |
107 | 0 | _p = _s+1; /* rc > 1 means: "input not consumed" */ \ |
108 | 0 | if ( *(_p)==0 ) \ |
109 | 0 | goto _done; \ |
110 | 0 | } else if (_rc == 0) { /* if all "input is consumed" */ \ |
111 | 0 | goto _done; \ |
112 | 0 | }\ |
113 | 0 | } while(0) |
114 | | |
115 | | #if 0 /* debugging mode */ |
116 | | #define LM_DEV LM_ERR |
117 | | #else |
118 | | #define LM_DEV(...) |
119 | | #endif |
120 | | |
121 | | typedef struct _ac_maxval |
122 | | { |
123 | | int yweek; |
124 | | int yday; /* current year's max days (365-366) */ |
125 | | int ywday; |
126 | | int mweek; /* current month's max number of weeks (4-5) */ |
127 | | int mday; /* current month's max days (28-31) */ |
128 | | int mwday; /* current month's max occurrences of current day (4-5) */ |
129 | | } ac_maxval_t, *ac_maxval_p; |
130 | | |
131 | | typedef struct _ac_tm |
132 | | { |
133 | | time_t time; |
134 | | struct tm t; |
135 | | int mweek; |
136 | | int yweek; |
137 | | int ywday; |
138 | | int wom; /* current day's week of the month (0-4) */ |
139 | | char flags; |
140 | | } ac_tm_t, *ac_tm_p; |
141 | | |
142 | 0 | #define TR_OP_NUL 0 |
143 | 0 | #define TR_OP_AND 1 |
144 | 0 | #define TR_OP_OR 2 |
145 | | |
146 | | int ac_tm_reset(ac_tm_p); |
147 | | |
148 | | int ac_get_mweek(struct tm*); |
149 | | int ac_get_yweek(struct tm*); |
150 | | int ac_get_wkst(); |
151 | | |
152 | | int ac_print(ac_tm_p); |
153 | | |
154 | | time_t ic_parse_datetime(char*,struct tm*); |
155 | | time_t ic_parse_duration(char*); |
156 | | |
157 | | tr_byxxx_p ic_parse_byday(char*, char); |
158 | | tr_byxxx_p ic_parse_byxxx(char*, char); |
159 | | int ic_parse_wkst(char*); |
160 | | |
161 | | |
162 | | static inline int strz2int(char *_bp) |
163 | 0 | { |
164 | 0 | int _v; |
165 | 0 | char *_p; |
166 | |
|
167 | 0 | _v = 0; |
168 | 0 | _p = _bp; |
169 | 0 | while(*_p && *_p>='0' && *_p<='9') |
170 | 0 | { |
171 | 0 | _v += *_p - '0'; |
172 | 0 | _p++; |
173 | 0 | } |
174 | 0 | return _v; |
175 | 0 | } |
176 | | |
177 | | |
178 | | /** |
179 | | * Check if @x falls within the recurring [@bgn, @end) time interval, |
180 | | * according to @freq. |
181 | | * |
182 | | * @x: value to check |
183 | | * @bgn: interval start |
184 | | * @end: interval end |
185 | | * @dur: duration of the interval (effectively: @end - @bgn) |
186 | | * @freq: FREQ_WEEKLY / FREQ_MONTHLY / FREQ_YEARLY |
187 | | * |
188 | | * Return: REC_MATCH or REC_NOMATCH |
189 | | */ |
190 | | int check_recur_itv(struct tm *x, struct tm *bgn, struct tm *end, |
191 | | time_t dur, int freq); |
192 | | |
193 | | |
194 | | static inline void ac_tm_fill(ac_tm_p _atp, struct tm* _tm) |
195 | 0 | { |
196 | 0 | _atp->t = *_tm; |
197 | |
|
198 | | #if 0 |
199 | | _atp->mweek = ac_get_mweek(_tm); |
200 | | #endif |
201 | 0 | _atp->yweek = ac_get_yweek(_tm); |
202 | 0 | _atp->ywday = ac_get_wday_yr(_tm); |
203 | 0 | _atp->wom = ac_get_wday_mr(_tm); |
204 | 0 | } |
205 | | |
206 | | |
207 | 0 | #define TZ_INTACT ((char *)-1) |
208 | | static char *old_tz = TZ_INTACT; |
209 | | |
210 | | void tz_set(const str *tz) |
211 | 0 | { |
212 | 0 | #define TZBUF_SZ 50 |
213 | 0 | char tzbuf[TZBUF_SZ]; |
214 | |
|
215 | 0 | if (tz->len >= TZBUF_SZ) |
216 | 0 | return; |
217 | | |
218 | 0 | memcpy(tzbuf, tz->s, tz->len); |
219 | 0 | tzbuf[tz->len] = '\0'; |
220 | |
|
221 | 0 | _tz_set(tzbuf); |
222 | 0 | #undef TZBUF_SZ |
223 | 0 | } |
224 | | |
225 | | |
226 | | void _tz_set(const char *tz) |
227 | 0 | { |
228 | 0 | LM_DBG("setting timezone to: '%s'\n", tz); |
229 | |
|
230 | 0 | old_tz = getenv("TZ"); |
231 | |
|
232 | 0 | setenv("TZ", tz, 1); |
233 | 0 | tzset(); |
234 | 0 | } |
235 | | |
236 | | |
237 | | void tz_reset(void) |
238 | 0 | { |
239 | 0 | if (old_tz == TZ_INTACT) |
240 | 0 | return; |
241 | | |
242 | 0 | if (!old_tz) { |
243 | 0 | LM_DBG("resetting timezone to system default\n"); |
244 | 0 | unsetenv("TZ"); |
245 | 0 | } else { |
246 | 0 | LM_DBG("resetting timezone to '%s'\n", old_tz); |
247 | 0 | setenv("TZ", old_tz, 1); |
248 | 0 | } |
249 | |
|
250 | 0 | tzset(); |
251 | 0 | old_tz = TZ_INTACT; |
252 | 0 | } |
253 | | |
254 | | |
255 | | time_t tz_adjust_ts(time_t unix_time, const str *tz) |
256 | 0 | { |
257 | 0 | struct tm local_tm; |
258 | 0 | time_t adj_ts; |
259 | |
|
260 | 0 | tz_set(_str("UTC")); |
261 | 0 | localtime_r(&unix_time, &local_tm ); |
262 | 0 | tz_reset(); |
263 | |
|
264 | 0 | if (tz) |
265 | 0 | tz_set(tz); |
266 | |
|
267 | 0 | adj_ts = mktime(&local_tm); |
268 | 0 | tz_reset(); |
269 | |
|
270 | 0 | if (local_tm.tm_isdst > 0) |
271 | 0 | adj_ts -= 3600; |
272 | |
|
273 | 0 | LM_DBG("UNIX ts: %ld, local-adjusted ts: %ld (tz: '%.*s', DST: %s)\n", |
274 | 0 | (long int)unix_time, (long int)adj_ts, tz ? tz->len : 4, |
275 | 0 | tz ? tz->s : "null", local_tm.tm_isdst == 1 ? "on" : |
276 | 0 | local_tm.tm_isdst == 0 ? "off" : "unavail"); |
277 | |
|
278 | 0 | return adj_ts; |
279 | 0 | } |
280 | | |
281 | | |
282 | | static inline void ac_tm_set_time(ac_tm_p _atp, time_t _t) |
283 | 0 | { |
284 | 0 | struct tm ltime; |
285 | |
|
286 | 0 | memset(_atp, 0, sizeof *_atp); |
287 | 0 | _atp->time = _t; |
288 | |
|
289 | 0 | localtime_r(&_t, <ime); |
290 | 0 | ac_tm_fill(_atp, <ime); |
291 | 0 | } |
292 | | |
293 | | int ac_get_mweek(struct tm* _tm) |
294 | 0 | { |
295 | 0 | if(!_tm) |
296 | 0 | return -1; |
297 | | #ifdef USE_YWEEK_U |
298 | | return ((_tm->tm_mday-1)/7 + (7-_tm->tm_wday+(_tm->tm_mday-1)%7)/7); |
299 | | #else |
300 | 0 | return ((_tm->tm_mday-1)/7 + (7-(6+_tm->tm_wday)%7+(_tm->tm_mday-1)%7)/7); |
301 | 0 | #endif |
302 | 0 | } |
303 | | |
304 | | |
305 | | int ac_get_yweek(struct tm* _tm) |
306 | 0 | { |
307 | 0 | int week = -1; |
308 | | #ifdef USE_YWEEK_V |
309 | | int days; |
310 | | #endif |
311 | |
|
312 | 0 | if(!_tm) |
313 | 0 | return -1; |
314 | | |
315 | | #ifdef USE_YWEEK_U |
316 | | week = SUN_WEEK(_tm); |
317 | | #else |
318 | 0 | week = MON_WEEK(_tm); |
319 | 0 | #endif |
320 | |
|
321 | | #ifdef USE_YWEEK_V |
322 | | days = ((_tm->tm_yday + 7 - (_tm->tm_wday ? _tm->tm_wday-1 : 6)) % 7); |
323 | | |
324 | | if(days >= 4) |
325 | | week++; |
326 | | else |
327 | | if(week == 0) |
328 | | week = 53; |
329 | | #endif |
330 | 0 | return week; |
331 | 0 | } |
332 | | |
333 | | int ac_get_wkst(void) |
334 | 0 | { |
335 | | #ifdef USE_YWEEK_U |
336 | | return 0; |
337 | | #else |
338 | 0 | return 1; |
339 | 0 | #endif |
340 | 0 | } |
341 | | |
342 | | int ac_tm_reset(ac_tm_p _atp) |
343 | 0 | { |
344 | 0 | if(!_atp) |
345 | 0 | return -1; |
346 | 0 | memset(_atp, 0, sizeof(ac_tm_t)); |
347 | 0 | return 0; |
348 | 0 | } |
349 | | |
350 | | static ac_maxval_p ac_get_maxval(ac_tm_p _atp) |
351 | 0 | { |
352 | 0 | static ac_maxval_t _amp; |
353 | 0 | struct tm _tm; |
354 | 0 | int _v; |
355 | | |
356 | | /* the number of the days in the year */ |
357 | 0 | _amp.yday = 365 + is_leap_year(_atp->t.tm_year+1900); |
358 | | |
359 | | /* the number of the days in the month */ |
360 | 0 | switch(_atp->t.tm_mon) |
361 | 0 | { |
362 | 0 | case 1: |
363 | 0 | if(_amp.yday == 366) |
364 | 0 | _amp.mday = 29; |
365 | 0 | else |
366 | 0 | _amp.mday = 28; |
367 | 0 | break; |
368 | 0 | case 3: case 5: case 8: case 10: |
369 | 0 | _amp.mday = 30; |
370 | 0 | break; |
371 | 0 | default: |
372 | 0 | _amp.mday = 31; |
373 | 0 | } |
374 | | |
375 | | /* maximum occurrences of a week day in the year */ |
376 | 0 | memset(&_tm, 0, sizeof(struct tm)); |
377 | 0 | _tm.tm_year = _atp->t.tm_year; |
378 | 0 | _tm.tm_mon = 11; |
379 | 0 | _tm.tm_mday = 31; |
380 | 0 | mktime(&_tm); |
381 | 0 | _v = 0; |
382 | 0 | if(_atp->t.tm_wday > _tm.tm_wday) |
383 | 0 | _v = _atp->t.tm_wday - _tm.tm_wday + 1; |
384 | 0 | else |
385 | 0 | _v = _tm.tm_wday - _atp->t.tm_wday; |
386 | 0 | _amp.ywday = (int)((_tm.tm_yday-_v)/7) + 1; |
387 | | |
388 | | /* maximum number of weeks in the year */ |
389 | 0 | _amp.yweek = ac_get_yweek(&_tm) + 1; |
390 | | |
391 | | /* maximum number of the week day in the month */ |
392 | 0 | _amp.mwday=(int)((_amp.mday-1-(_amp.mday-_atp->t.tm_mday)%7)/7)+1; |
393 | |
|
394 | | #if 0 |
395 | | /* maximum number of weeks in the month */ |
396 | | _v = (_atp->t.tm_wday + (_amp.mday - _atp->t.tm_mday)%7)%7; |
397 | | #ifdef USE_YWEEK_U |
398 | | _amp.mweek = (int)((_amp.mday-1)/7+(7-_v+(_amp.mday-1)%7)/7)+1; |
399 | | #else |
400 | | _amp.mweek = (int)((_amp.mday-1)/7+(7-(6+_v)%7+(_amp.mday-1)%7)/7)+1; |
401 | | #endif |
402 | | #endif |
403 | |
|
404 | 0 | return &_amp; |
405 | 0 | } |
406 | | |
407 | | |
408 | | int ac_print(ac_tm_p _atp) |
409 | 0 | { |
410 | 0 | static char *_wdays[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"}; |
411 | 0 | if(!_atp) |
412 | 0 | { |
413 | 0 | printf("\n(null)\n"); |
414 | 0 | return -1; |
415 | 0 | } |
416 | | |
417 | 0 | printf("\nSys time: %d\nTime: %02d:%02d:%02d\n", (int)_atp->time, |
418 | 0 | _atp->t.tm_hour, _atp->t.tm_min, _atp->t.tm_sec); |
419 | 0 | printf("Date: %s, %04d-%02d-%02d\n", _wdays[_atp->t.tm_wday], |
420 | 0 | _atp->t.tm_year+1900, _atp->t.tm_mon+1, _atp->t.tm_mday); |
421 | 0 | printf("Year day: %d\nYear week-day: %d\nYear week: %d\n", _atp->t.tm_yday, |
422 | 0 | _atp->ywday, _atp->yweek); |
423 | 0 | printf("Month week: %d\nMonth week-day: %d\n", _atp->mweek, _atp->wom); |
424 | 0 | return 0; |
425 | 0 | } |
426 | | |
427 | | |
428 | | |
429 | | |
430 | | |
431 | | |
432 | | tr_byxxx_p tr_byxxx_new(char alloc) |
433 | 0 | { |
434 | 0 | tr_byxxx_p _bxp = NULL; |
435 | 0 | if (alloc & PKG_ALLOC) |
436 | 0 | _bxp = (tr_byxxx_p)pkg_malloc(sizeof(tr_byxxx_t)); |
437 | 0 | else |
438 | 0 | _bxp = (tr_byxxx_p)shm_malloc(sizeof(tr_byxxx_t)); |
439 | 0 | if(!_bxp) |
440 | 0 | return NULL; |
441 | 0 | memset(_bxp, 0, sizeof(tr_byxxx_t)); |
442 | 0 | _bxp->flags = alloc; |
443 | 0 | return _bxp; |
444 | 0 | } |
445 | | |
446 | | int tr_byxxx_init(tr_byxxx_p _bxp, int _nr) |
447 | 0 | { |
448 | 0 | _bxp->nr = _nr; |
449 | 0 | if (_bxp->flags & PKG_ALLOC) { |
450 | 0 | _bxp->xxx = (int*)pkg_malloc(_nr*sizeof(int)); |
451 | 0 | _bxp->req = (int*)pkg_malloc(_nr*sizeof(int)); |
452 | 0 | } else { |
453 | 0 | _bxp->xxx = (int*)shm_malloc(_nr*sizeof(int)); |
454 | 0 | _bxp->req = (int*)shm_malloc(_nr*sizeof(int)); |
455 | 0 | } |
456 | |
|
457 | 0 | if (!_bxp->xxx || !_bxp->req) |
458 | 0 | goto oom; |
459 | | |
460 | 0 | memset(_bxp->xxx, 0, _nr*sizeof(int)); |
461 | 0 | memset(_bxp->req, 0, _nr*sizeof(int)); |
462 | |
|
463 | 0 | return 0; |
464 | 0 | oom: |
465 | 0 | LM_ERR("oom\n"); |
466 | 0 | if (_bxp->flags & PKG_ALLOC) { |
467 | 0 | pkg_free(_bxp->xxx); |
468 | 0 | pkg_free(_bxp->req); |
469 | 0 | } else { |
470 | 0 | shm_free(_bxp->xxx); |
471 | 0 | shm_free(_bxp->req); |
472 | 0 | } |
473 | 0 | _bxp->xxx = NULL; |
474 | 0 | _bxp->req = NULL; |
475 | 0 | return -1; |
476 | 0 | } |
477 | | |
478 | | |
479 | | int tr_byxxx_free(tr_byxxx_p _bxp) |
480 | 0 | { |
481 | 0 | if (!_bxp) |
482 | 0 | return -1; |
483 | | |
484 | 0 | if (_bxp->flags & PKG_ALLOC) { |
485 | 0 | pkg_free(_bxp->xxx); |
486 | 0 | pkg_free(_bxp->req); |
487 | 0 | pkg_free(_bxp); |
488 | 0 | } else { |
489 | 0 | shm_free(_bxp->xxx); |
490 | 0 | shm_free(_bxp->req); |
491 | 0 | shm_free(_bxp); |
492 | 0 | } |
493 | |
|
494 | 0 | return 0; |
495 | 0 | } |
496 | | |
497 | | void tmrec_init(tmrec_p t) |
498 | 0 | { |
499 | 0 | memset(t, 0, sizeof *t); |
500 | | |
501 | | /* these values may be legitimately set to 0 (i.e. UNIX start time) */ |
502 | 0 | t->dtstart = TR_NOVAL; |
503 | 0 | t->dtend = TR_NOVAL; |
504 | 0 | t->duration = TR_NOVAL; |
505 | |
|
506 | 0 | t->freq = TR_NOVAL; |
507 | 0 | t->until = TR_NOVAL; |
508 | 0 | t->interval = TR_NOVAL; |
509 | 0 | } |
510 | | |
511 | | static inline void tmrec_expr_init(tmrec_expr_t *e) |
512 | 0 | { |
513 | 0 | memset(e, 0, sizeof *e); |
514 | 0 | INIT_LIST_HEAD(&e->operands); |
515 | 0 | tmrec_init(&e->tr); |
516 | 0 | } |
517 | | |
518 | | static void tmrec_free_members(tmrec_p, char); |
519 | | |
520 | | tmrec_p tmrec_new(char alloc) |
521 | 0 | { |
522 | 0 | tmrec_p _trp; |
523 | 0 | if (alloc & PKG_ALLOC) |
524 | 0 | _trp = (tmrec_p)pkg_malloc(sizeof(tmrec_t)); |
525 | 0 | else |
526 | 0 | _trp = (tmrec_p)shm_malloc(sizeof(tmrec_t)); |
527 | 0 | if(!_trp) |
528 | 0 | return NULL; |
529 | | |
530 | 0 | tmrec_init(_trp); |
531 | 0 | _trp->flags = alloc; |
532 | 0 | return _trp; |
533 | 0 | } |
534 | | |
535 | | void tmrec_free(tmrec *tr) |
536 | 0 | { |
537 | 0 | tmrec_p _trp = (tmrec_p)tr; |
538 | |
|
539 | 0 | if(!_trp) |
540 | 0 | return; |
541 | | |
542 | 0 | tmrec_free_members(_trp, _trp->flags); |
543 | |
|
544 | 0 | if (_trp->flags & PKG_ALLOC) { |
545 | 0 | pkg_free(_trp); |
546 | 0 | } else { |
547 | 0 | shm_free(_trp); |
548 | 0 | } |
549 | 0 | } |
550 | | |
551 | | static void tmrec_free_members(tmrec_p _trp, char alloc) |
552 | 0 | { |
553 | 0 | if (!_trp) |
554 | 0 | return; |
555 | | |
556 | 0 | tr_byxxx_free(_trp->byday); |
557 | 0 | tr_byxxx_free(_trp->bymday); |
558 | 0 | tr_byxxx_free(_trp->byyday); |
559 | 0 | tr_byxxx_free(_trp->bymonth); |
560 | 0 | tr_byxxx_free(_trp->byweekno); |
561 | |
|
562 | 0 | if (alloc & PKG_ALLOC) |
563 | 0 | pkg_free(_trp->tz); |
564 | 0 | else |
565 | 0 | shm_free(_trp->tz); |
566 | |
|
567 | 0 | _trp->byday = NULL; |
568 | 0 | _trp->bymday = NULL; |
569 | 0 | _trp->byyday = NULL; |
570 | 0 | _trp->bymonth = NULL; |
571 | 0 | _trp->byweekno = NULL; |
572 | 0 | _trp->tz = NULL; |
573 | 0 | } |
574 | | |
575 | | int tr_parse_tz(tmrec_p _trp, char *_in) |
576 | 0 | { |
577 | 0 | if (!_trp || !_in) |
578 | 0 | return -1; |
579 | | |
580 | 0 | if (*_in < 'A' || *_in > 'Z') |
581 | 0 | return 1; |
582 | | |
583 | 0 | if (_trp->flags & PKG_ALLOC) |
584 | 0 | _trp->tz = pkg_strdup(_in); |
585 | 0 | else |
586 | 0 | _trp->tz = shm_strdup(_in); |
587 | |
|
588 | 0 | if (!_trp->tz) { |
589 | 0 | LM_ERR("oom\n"); |
590 | 0 | return -1; |
591 | 0 | } |
592 | | |
593 | 0 | return 0; |
594 | 0 | } |
595 | | |
596 | | int tr_parse_dtstart(tmrec_p _trp, char *_in) |
597 | 0 | { |
598 | 0 | if (!_in) |
599 | 0 | return -1; |
600 | 0 | _trp->dtstart = ic_parse_datetime(_in, &(_trp->ts)); |
601 | 0 | return (_trp->dtstart == TR_NOVAL) ? -1 : 0; |
602 | 0 | } |
603 | | |
604 | | int tr_parse_dtend(tmrec_p _trp, char *_in) |
605 | 0 | { |
606 | 0 | struct tm _tm; |
607 | 0 | if (!_in) |
608 | 0 | return -1; |
609 | 0 | _trp->dtend = ic_parse_datetime(_in,&_tm); |
610 | 0 | return (_trp->dtend == TR_NOVAL) ? -1 : 0; |
611 | 0 | } |
612 | | |
613 | | int tr_parse_duration(tmrec_p _trp, char *_in) |
614 | 0 | { |
615 | 0 | if (!_in) |
616 | 0 | return -1; |
617 | 0 | _trp->duration = ic_parse_duration(_in); |
618 | 0 | return (_trp->duration == TR_NOVAL) ? -1 : 0; |
619 | 0 | } |
620 | | |
621 | | int tr_parse_until(tmrec_p _trp, char *_in) |
622 | 0 | { |
623 | 0 | struct tm _tm; |
624 | 0 | if (!_in) |
625 | 0 | return -1; |
626 | 0 | _trp->until = ic_parse_datetime(_in, &_tm); |
627 | 0 | return (_trp->until == TR_NOVAL) ? -1 : 0; |
628 | 0 | } |
629 | | |
630 | | int tr_parse_freq(tmrec_p _trp, char *_in) |
631 | 0 | { |
632 | 0 | if (!_in) |
633 | 0 | return -1; |
634 | | |
635 | 0 | if(strlen(_in)<5) |
636 | 0 | { |
637 | 0 | _trp->freq = FREQ_NOFREQ; |
638 | 0 | return 0; |
639 | 0 | } |
640 | 0 | if(!strcasecmp(_in, "daily")) |
641 | 0 | { |
642 | 0 | _trp->freq = FREQ_DAILY; |
643 | 0 | return 0; |
644 | 0 | } |
645 | 0 | if(!strcasecmp(_in, "weekly")) |
646 | 0 | { |
647 | 0 | _trp->freq = FREQ_WEEKLY; |
648 | 0 | return 0; |
649 | 0 | } |
650 | 0 | if(!strcasecmp(_in, "monthly")) |
651 | 0 | { |
652 | 0 | _trp->freq = FREQ_MONTHLY; |
653 | 0 | return 0; |
654 | 0 | } |
655 | 0 | if(!strcasecmp(_in, "yearly")) |
656 | 0 | { |
657 | 0 | _trp->freq = FREQ_YEARLY; |
658 | 0 | return 0; |
659 | 0 | } |
660 | | |
661 | 0 | _trp->freq = FREQ_NOFREQ; |
662 | 0 | return 0; |
663 | 0 | } |
664 | | |
665 | | int tr_parse_interval(tmrec_p _trp, char *_in) |
666 | 0 | { |
667 | 0 | if (!_in) |
668 | 0 | return -1; |
669 | 0 | _trp->interval = strz2int(_in); |
670 | 0 | return 0; |
671 | 0 | } |
672 | | |
673 | | int tr_parse_byday(tmrec_p _trp, char *_in) |
674 | 0 | { |
675 | 0 | tr_byxxx_p byday; |
676 | |
|
677 | 0 | if (!_in) |
678 | 0 | return -1; |
679 | | |
680 | 0 | byday = ic_parse_byday(_in, _trp->flags); |
681 | 0 | if (!byday) |
682 | 0 | return -1; |
683 | 0 | tr_byxxx_free(_trp->byday); |
684 | 0 | _trp->byday = byday; |
685 | 0 | _trp->flags |= TR_BYXXX; |
686 | 0 | return 0; |
687 | 0 | } |
688 | | |
689 | | int tr_parse_bymday(tmrec_p _trp, char *_in) |
690 | 0 | { |
691 | 0 | tr_byxxx_p bymday; |
692 | |
|
693 | 0 | if (!_in) |
694 | 0 | return -1; |
695 | | |
696 | 0 | bymday = ic_parse_byxxx(_in, _trp->flags); |
697 | 0 | if (!bymday) |
698 | 0 | return -1; |
699 | 0 | tr_byxxx_free(_trp->bymday); |
700 | 0 | _trp->bymday = bymday; |
701 | 0 | _trp->flags |= TR_BYXXX; |
702 | 0 | return 0; |
703 | 0 | } |
704 | | |
705 | | int tr_parse_byyday(tmrec_p _trp, char *_in) |
706 | 0 | { |
707 | 0 | tr_byxxx_p byyday; |
708 | |
|
709 | 0 | if (!_in) |
710 | 0 | return -1; |
711 | | |
712 | 0 | byyday = ic_parse_byxxx(_in, _trp->flags); |
713 | 0 | if (!byyday) |
714 | 0 | return -1; |
715 | 0 | tr_byxxx_free(_trp->byyday); |
716 | 0 | _trp->byyday = byyday; |
717 | 0 | _trp->flags |= TR_BYXXX; |
718 | 0 | return 0; |
719 | 0 | } |
720 | | |
721 | | int tr_parse_bymonth(tmrec_p _trp, char *_in) |
722 | 0 | { |
723 | 0 | tr_byxxx_p bymonth; |
724 | |
|
725 | 0 | if (!_in) |
726 | 0 | return -1; |
727 | | |
728 | 0 | bymonth = ic_parse_byxxx(_in, _trp->flags); |
729 | 0 | if (!bymonth) |
730 | 0 | return -1; |
731 | 0 | tr_byxxx_free(_trp->bymonth); |
732 | 0 | _trp->bymonth = bymonth; |
733 | 0 | _trp->flags |= TR_BYXXX; |
734 | 0 | return 0; |
735 | 0 | } |
736 | | |
737 | | int tr_parse_byweekno(tmrec_p _trp, char *_in) |
738 | 0 | { |
739 | 0 | tr_byxxx_p byweekno; |
740 | |
|
741 | 0 | if (!_in) |
742 | 0 | return -1; |
743 | | |
744 | 0 | byweekno = ic_parse_byxxx(_in, _trp->flags); |
745 | 0 | if (!byweekno) |
746 | 0 | return -1; |
747 | 0 | tr_byxxx_free(_trp->byweekno); |
748 | 0 | _trp->byweekno = byweekno; |
749 | 0 | _trp->flags |= TR_BYXXX; |
750 | 0 | return 0; |
751 | 0 | } |
752 | | |
753 | | int tr_parse_wkst(tmrec_p _trp, char *_in) |
754 | 0 | { |
755 | 0 | if (!_in) |
756 | 0 | return -1; |
757 | | |
758 | 0 | _trp->wkst = ic_parse_wkst(_in); |
759 | 0 | return 0; |
760 | 0 | } |
761 | | |
762 | | int tmrec_print(const tmrec *tr) |
763 | 0 | { |
764 | 0 | static char *_wdays[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"}; |
765 | 0 | tmrec_p _trp = (tmrec_p)tr; |
766 | 0 | int i; |
767 | |
|
768 | 0 | if(!_trp) |
769 | 0 | { |
770 | 0 | printf("\n(null)\n"); |
771 | 0 | return -1; |
772 | 0 | } |
773 | 0 | printf("Recurrence definition\n-- start time ---\n"); |
774 | 0 | printf("Sys time: %d\n", (int)_trp->dtstart); |
775 | 0 | printf("Time: %02d:%02d:%02d\n", _trp->ts.tm_hour, |
776 | 0 | _trp->ts.tm_min, _trp->ts.tm_sec); |
777 | 0 | printf("Date: %s, %04d-%02d-%02d\n", _wdays[_trp->ts.tm_wday], |
778 | 0 | _trp->ts.tm_year+1900, _trp->ts.tm_mon+1, _trp->ts.tm_mday); |
779 | 0 | printf("---\n"); |
780 | 0 | printf("End time: %d\n", (int)_trp->dtend); |
781 | 0 | printf("Duration: %d\n", (int)_trp->duration); |
782 | 0 | printf("Until: %d\n", (int)_trp->until); |
783 | 0 | printf("Freq: %d\n", (int)_trp->freq); |
784 | 0 | printf("Interval: %d\n", (int)_trp->interval); |
785 | 0 | if(_trp->byday) |
786 | 0 | { |
787 | 0 | printf("Byday: "); |
788 | 0 | for(i=0; i<_trp->byday->nr; i++) |
789 | 0 | printf(" %d%s", _trp->byday->req[i], _wdays[_trp->byday->xxx[i]]); |
790 | 0 | printf("\n"); |
791 | 0 | } |
792 | 0 | if(_trp->bymday) |
793 | 0 | { |
794 | 0 | printf("Bymday: %d:", _trp->bymday->nr); |
795 | 0 | for(i=0; i<_trp->bymday->nr; i++) |
796 | 0 | printf(" %d", _trp->bymday->xxx[i]*_trp->bymday->req[i]); |
797 | 0 | printf("\n"); |
798 | 0 | } |
799 | 0 | if(_trp->byyday) |
800 | 0 | { |
801 | 0 | printf("Byyday:"); |
802 | 0 | for(i=0; i<_trp->byyday->nr; i++) |
803 | 0 | printf(" %d", _trp->byyday->xxx[i]*_trp->byyday->req[i]); |
804 | 0 | printf("\n"); |
805 | 0 | } |
806 | 0 | if(_trp->bymonth) |
807 | 0 | { |
808 | 0 | printf("Bymonth: %d:", _trp->bymonth->nr); |
809 | 0 | for(i=0; i< _trp->bymonth->nr; i++) |
810 | 0 | printf(" %d", _trp->bymonth->xxx[i]*_trp->bymonth->req[i]); |
811 | 0 | printf("\n"); |
812 | 0 | } |
813 | 0 | if(_trp->byweekno) |
814 | 0 | { |
815 | 0 | printf("Byweekno: "); |
816 | 0 | for(i=0; i<_trp->byweekno->nr; i++) |
817 | 0 | printf(" %d", _trp->byweekno->xxx[i]*_trp->byweekno->req[i]); |
818 | 0 | printf("\n"); |
819 | 0 | } |
820 | 0 | printf("Weekstart: %d\n", _trp->wkst); |
821 | 0 | return 0; |
822 | 0 | } |
823 | | |
824 | | time_t ic_parse_datetime(char *_in, struct tm *_tm) |
825 | 0 | { |
826 | 0 | struct tm t; |
827 | |
|
828 | 0 | if (!_in || strlen(_in)!=15) |
829 | 0 | return TR_NOVAL; |
830 | | |
831 | 0 | t.tm_year = _D(_in[0])*1000 + _D(_in[1])*100 |
832 | 0 | + _D(_in[2])*10 + _D(_in[3]); |
833 | 0 | if (t.tm_year < 1970) { |
834 | 0 | LM_ERR("invalid year in Date-Time: '%s'\n", _in); |
835 | 0 | return TR_NOVAL; |
836 | 0 | } |
837 | | |
838 | 0 | t.tm_year -= 1900; /* per man ctime(3) */ |
839 | 0 | t.tm_mon = _D(_in[4])*10 + _D(_in[5]) - 1; |
840 | 0 | t.tm_mday = _D(_in[6])*10 + _D(_in[7]); |
841 | 0 | if (t.tm_mon == -1 || t.tm_mday == 0) { |
842 | 0 | LM_ERR("month or month day cannot be zero in Date-Time: '%s'\n", _in); |
843 | 0 | return TR_NOVAL; |
844 | 0 | } |
845 | | |
846 | 0 | t.tm_hour = _D(_in[9])*10 + _D(_in[10]); |
847 | 0 | t.tm_min = _D(_in[11])*10 + _D(_in[12]); |
848 | 0 | t.tm_sec = _D(_in[13])*10 + _D(_in[14]); |
849 | 0 | t.tm_isdst = -1 /*daylight*/; |
850 | |
|
851 | 0 | *_tm = t; |
852 | 0 | return mktime(_tm); |
853 | 0 | } |
854 | | |
855 | | time_t ic_parse_duration(char *_in) |
856 | 0 | { |
857 | 0 | time_t _t, _ft; |
858 | 0 | char *_p; |
859 | 0 | int _fl; |
860 | |
|
861 | 0 | if(!_in || strlen(_in)<2) |
862 | 0 | return TR_NOVAL; |
863 | | |
864 | 0 | if(*_in == 'P' || *_in=='p') |
865 | 0 | { |
866 | 0 | _p = _in+1; |
867 | 0 | _fl = 1; |
868 | 0 | } else { |
869 | 0 | _p = _in; |
870 | 0 | _fl = 0; |
871 | 0 | } |
872 | |
|
873 | 0 | _t = _ft = 0; |
874 | |
|
875 | 0 | while(*_p) |
876 | 0 | { |
877 | 0 | switch(*_p) |
878 | 0 | { |
879 | 0 | case '0': case '1': case '2': |
880 | 0 | case '3': case '4': case '5': |
881 | 0 | case '6': case '7': case '8': |
882 | 0 | case '9': |
883 | 0 | _t = _t*10 + *_p - '0'; |
884 | 0 | break; |
885 | | |
886 | 0 | case 'w': |
887 | 0 | case 'W': |
888 | 0 | if(!_fl) |
889 | 0 | { |
890 | 0 | LM_ERR("week duration not allowed" |
891 | 0 | " here (%d) [%s]\n", (int)(_p-_in), _in); |
892 | 0 | return 0; |
893 | 0 | } |
894 | 0 | _ft += _t*7*24*3600; |
895 | 0 | _t = 0; |
896 | 0 | break; |
897 | 0 | case 'd': |
898 | 0 | case 'D': |
899 | 0 | if(!_fl) |
900 | 0 | { |
901 | 0 | LM_ERR("day duration not allowed" |
902 | 0 | " here (%d) [%s]\n", (int)(_p-_in), _in); |
903 | 0 | return 0; |
904 | 0 | } |
905 | 0 | _ft += _t*24*3600; |
906 | 0 | _t = 0; |
907 | 0 | break; |
908 | 0 | case 'h': |
909 | 0 | case 'H': |
910 | 0 | if(_fl) |
911 | 0 | { |
912 | 0 | LM_ERR("hour duration not allowed" |
913 | 0 | " here (%d) [%s]\n", (int)(_p-_in), _in); |
914 | 0 | return 0; |
915 | 0 | } |
916 | 0 | _ft += _t*3600; |
917 | 0 | _t = 0; |
918 | 0 | break; |
919 | 0 | case 'm': |
920 | 0 | case 'M': |
921 | 0 | if(_fl) |
922 | 0 | { |
923 | 0 | LM_ERR("minute duration not allowed" |
924 | 0 | " here (%d) [%s]\n", (int)(_p-_in), _in); |
925 | 0 | return 0; |
926 | 0 | } |
927 | 0 | _ft += _t*60; |
928 | 0 | _t = 0; |
929 | 0 | break; |
930 | 0 | case 's': |
931 | 0 | case 'S': |
932 | 0 | if(_fl) |
933 | 0 | { |
934 | 0 | LM_ERR("second duration not allowed" |
935 | 0 | " here (%d) [%s]\n", (int)(_p-_in), _in); |
936 | 0 | return 0; |
937 | 0 | } |
938 | 0 | _ft += _t; |
939 | 0 | _t = 0; |
940 | 0 | break; |
941 | 0 | case 't': |
942 | 0 | case 'T': |
943 | 0 | if(!_fl) |
944 | 0 | { |
945 | 0 | LM_ERR("'T' not allowed" |
946 | 0 | " here (%d) [%s]\n", (int)(_p-_in), _in); |
947 | 0 | return 0; |
948 | 0 | } |
949 | 0 | _fl = 0; |
950 | 0 | break; |
951 | 0 | default: |
952 | 0 | LM_ERR("bad character here (%d) [%s]\n", |
953 | 0 | (int)(_p-_in), _in); |
954 | 0 | return 0; |
955 | 0 | } |
956 | 0 | _p++; |
957 | 0 | } |
958 | | |
959 | 0 | return _ft; |
960 | 0 | } |
961 | | |
962 | | tr_byxxx_p ic_parse_byday(char *_in, char type) |
963 | 0 | { |
964 | 0 | tr_byxxx_p _bxp = NULL; |
965 | 0 | int _nr, _s, _v; |
966 | 0 | char *_p; |
967 | |
|
968 | 0 | if(!_in) |
969 | 0 | return NULL; |
970 | 0 | _bxp = tr_byxxx_new(type); |
971 | 0 | if(!_bxp) |
972 | 0 | return NULL; |
973 | 0 | _p = _in; |
974 | 0 | _nr = 1; |
975 | 0 | while(*_p) |
976 | 0 | { |
977 | 0 | if(*_p == ',') |
978 | 0 | _nr++; |
979 | 0 | _p++; |
980 | 0 | } |
981 | 0 | if(tr_byxxx_init(_bxp, _nr) < 0) |
982 | 0 | { |
983 | 0 | tr_byxxx_free(_bxp); |
984 | 0 | return NULL; |
985 | 0 | } |
986 | 0 | _p = _in; |
987 | 0 | _nr = _v = 0; |
988 | 0 | _s = 1; |
989 | 0 | while(*_p && _nr < _bxp->nr) |
990 | 0 | { |
991 | 0 | switch(*_p) |
992 | 0 | { |
993 | 0 | case '0': case '1': case '2': |
994 | 0 | case '3': case '4': case '5': |
995 | 0 | case '6': case '7': case '8': |
996 | 0 | case '9': |
997 | 0 | _v = _v*10 + *_p - '0'; |
998 | 0 | break; |
999 | | |
1000 | 0 | case 's': |
1001 | 0 | case 'S': |
1002 | 0 | _p++; |
1003 | 0 | switch(*_p) |
1004 | 0 | { |
1005 | 0 | case 'a': |
1006 | 0 | case 'A': |
1007 | 0 | _bxp->xxx[_nr] = WDAY_SA; |
1008 | 0 | break; |
1009 | 0 | case 'u': |
1010 | 0 | case 'U': |
1011 | 0 | _bxp->xxx[_nr] = WDAY_SU; |
1012 | 0 | break; |
1013 | 0 | default: |
1014 | 0 | goto error; |
1015 | 0 | } |
1016 | | |
1017 | 0 | _bxp->req[_nr] = _s * _v; |
1018 | 0 | if (_bxp->req[_nr] > 0) |
1019 | 0 | _bxp->req[_nr]--; |
1020 | 0 | _s = 1; |
1021 | 0 | _v = 0; |
1022 | 0 | break; |
1023 | 0 | case 'm': |
1024 | 0 | case 'M': |
1025 | 0 | _p++; |
1026 | 0 | if(*_p!='o' && *_p!='O') |
1027 | 0 | goto error; |
1028 | 0 | _bxp->xxx[_nr] = WDAY_MO; |
1029 | |
|
1030 | 0 | _bxp->req[_nr] = _s * _v; |
1031 | 0 | if (_bxp->req[_nr] > 0) |
1032 | 0 | _bxp->req[_nr]--; |
1033 | 0 | _s = 1; |
1034 | 0 | _v = 0; |
1035 | 0 | break; |
1036 | 0 | case 't': |
1037 | 0 | case 'T': |
1038 | 0 | _p++; |
1039 | 0 | switch(*_p) |
1040 | 0 | { |
1041 | 0 | case 'h': |
1042 | 0 | case 'H': |
1043 | 0 | _bxp->xxx[_nr] = WDAY_TH; |
1044 | 0 | break; |
1045 | 0 | case 'u': |
1046 | 0 | case 'U': |
1047 | 0 | _bxp->xxx[_nr] = WDAY_TU; |
1048 | 0 | break; |
1049 | 0 | default: |
1050 | 0 | goto error; |
1051 | 0 | } |
1052 | | |
1053 | 0 | _bxp->req[_nr] = _s * _v; |
1054 | 0 | if (_bxp->req[_nr] > 0) |
1055 | 0 | _bxp->req[_nr]--; |
1056 | 0 | _s = 1; |
1057 | 0 | _v = 0; |
1058 | 0 | break; |
1059 | 0 | case 'w': |
1060 | 0 | case 'W': |
1061 | 0 | _p++; |
1062 | 0 | if(*_p!='e' && *_p!='E') |
1063 | 0 | goto error; |
1064 | 0 | _bxp->xxx[_nr] = WDAY_WE; |
1065 | 0 | _s = 1; |
1066 | 0 | _v = 0; |
1067 | |
|
1068 | 0 | _bxp->req[_nr] = _s * _v; |
1069 | 0 | if (_bxp->req[_nr] > 0) |
1070 | 0 | _bxp->req[_nr]--; |
1071 | 0 | break; |
1072 | 0 | case 'f': |
1073 | 0 | case 'F': |
1074 | 0 | _p++; |
1075 | 0 | if(*_p!='r' && *_p!='R') |
1076 | 0 | goto error; |
1077 | 0 | _bxp->xxx[_nr] = WDAY_FR; |
1078 | |
|
1079 | 0 | _bxp->req[_nr] = _s * _v; |
1080 | 0 | if (_bxp->req[_nr] > 0) |
1081 | 0 | _bxp->req[_nr]--; |
1082 | 0 | _s = 1; |
1083 | 0 | _v = 0; |
1084 | 0 | break; |
1085 | 0 | case '-': |
1086 | 0 | _s = -1; |
1087 | 0 | break; |
1088 | 0 | case '+': |
1089 | 0 | case ' ': |
1090 | 0 | case '\t': |
1091 | 0 | break; |
1092 | 0 | case ',': |
1093 | 0 | _nr++; |
1094 | 0 | break; |
1095 | 0 | default: |
1096 | 0 | goto error; |
1097 | 0 | } |
1098 | 0 | _p++; |
1099 | 0 | } |
1100 | | |
1101 | 0 | return _bxp; |
1102 | | |
1103 | 0 | error: |
1104 | 0 | tr_byxxx_free(_bxp); |
1105 | 0 | return NULL; |
1106 | 0 | } |
1107 | | |
1108 | | tr_byxxx_p ic_parse_byxxx(char *_in, char type) |
1109 | 0 | { |
1110 | 0 | tr_byxxx_p _bxp = NULL; |
1111 | 0 | int _nr, _s, _v; |
1112 | 0 | char *_p; |
1113 | |
|
1114 | 0 | if(!_in) |
1115 | 0 | return NULL; |
1116 | 0 | _bxp = tr_byxxx_new(type); |
1117 | 0 | if(!_bxp) |
1118 | 0 | return NULL; |
1119 | 0 | _p = _in; |
1120 | 0 | _nr = 1; |
1121 | 0 | while(*_p) |
1122 | 0 | { |
1123 | 0 | if(*_p == ',') |
1124 | 0 | _nr++; |
1125 | 0 | _p++; |
1126 | 0 | } |
1127 | 0 | if(tr_byxxx_init(_bxp, _nr) < 0) |
1128 | 0 | { |
1129 | 0 | tr_byxxx_free(_bxp); |
1130 | 0 | return NULL; |
1131 | 0 | } |
1132 | | |
1133 | 0 | _nr = _v = 0; |
1134 | 0 | _s = 1; |
1135 | |
|
1136 | 0 | for (_p = _in; *_p; _p++) { |
1137 | 0 | switch (*_p) { |
1138 | 0 | case '0': case '1': case '2': |
1139 | 0 | case '3': case '4': case '5': |
1140 | 0 | case '6': case '7': case '8': |
1141 | 0 | case '9': |
1142 | 0 | _v = _v*10 + *_p - '0'; |
1143 | 0 | break; |
1144 | | |
1145 | 0 | case '-': |
1146 | 0 | _s = -1; |
1147 | 0 | break; |
1148 | 0 | case '+': |
1149 | 0 | case ' ': |
1150 | 0 | case '\t': |
1151 | 0 | break; |
1152 | | |
1153 | 0 | case ',': |
1154 | 0 | _bxp->xxx[_nr] = _v; |
1155 | 0 | _bxp->req[_nr] = _s; |
1156 | 0 | _s = 1; |
1157 | 0 | _v = 0; |
1158 | 0 | _nr++; |
1159 | 0 | break; |
1160 | | |
1161 | 0 | default: |
1162 | 0 | goto error; |
1163 | 0 | } |
1164 | 0 | } |
1165 | | |
1166 | | /* store the last item of the list */ |
1167 | 0 | _bxp->xxx[_nr] = _v; |
1168 | 0 | _bxp->req[_nr] = _s; |
1169 | |
|
1170 | 0 | return _bxp; |
1171 | | |
1172 | 0 | error: |
1173 | 0 | tr_byxxx_free(_bxp); |
1174 | 0 | return NULL; |
1175 | 0 | } |
1176 | | |
1177 | | int ic_parse_wkst(char *_in) |
1178 | 0 | { |
1179 | 0 | if(!_in || strlen(_in)!=2) |
1180 | 0 | goto error; |
1181 | | |
1182 | 0 | switch(_in[0]) |
1183 | 0 | { |
1184 | 0 | case 's': |
1185 | 0 | case 'S': |
1186 | 0 | switch(_in[1]) |
1187 | 0 | { |
1188 | 0 | case 'a': |
1189 | 0 | case 'A': |
1190 | 0 | return WDAY_SA; |
1191 | 0 | case 'u': |
1192 | 0 | case 'U': |
1193 | 0 | return WDAY_SU; |
1194 | 0 | default: |
1195 | 0 | goto error; |
1196 | 0 | } |
1197 | 0 | case 'm': |
1198 | 0 | case 'M': |
1199 | 0 | if(_in[1]!='o' && _in[1]!='O') |
1200 | 0 | goto error; |
1201 | 0 | return WDAY_MO; |
1202 | 0 | case 't': |
1203 | 0 | case 'T': |
1204 | 0 | switch(_in[1]) |
1205 | 0 | { |
1206 | 0 | case 'h': |
1207 | 0 | case 'H': |
1208 | 0 | return WDAY_TH; |
1209 | 0 | case 'u': |
1210 | 0 | case 'U': |
1211 | 0 | return WDAY_TU; |
1212 | 0 | default: |
1213 | 0 | goto error; |
1214 | 0 | } |
1215 | 0 | case 'w': |
1216 | 0 | case 'W': |
1217 | 0 | if(_in[1]!='e' && _in[1]!='E') |
1218 | 0 | goto error; |
1219 | 0 | return WDAY_WE; |
1220 | 0 | case 'f': |
1221 | 0 | case 'F': |
1222 | 0 | if(_in[1]!='r' && _in[1]!='R') |
1223 | 0 | goto error; |
1224 | 0 | return WDAY_FR; |
1225 | 0 | break; |
1226 | 0 | default: |
1227 | 0 | goto error; |
1228 | 0 | } |
1229 | | |
1230 | 0 | error: |
1231 | | #ifdef USE_YWEEK_U |
1232 | | return WDAY_SU; |
1233 | | #else |
1234 | 0 | return WDAY_MO; |
1235 | 0 | #endif |
1236 | 0 | } |
1237 | | |
1238 | | |
1239 | | |
1240 | | /*** local headers ***/ |
1241 | | int check_min_unit(tmrec_p _trp, ac_tm_p _atp); |
1242 | | int check_freq_interval(tmrec_p _trp, ac_tm_p _atp); |
1243 | | int check_byxxx(tmrec_p, ac_tm_p); |
1244 | | |
1245 | | /** |
1246 | | * |
1247 | | * return 0/REC_MATCH - the time falls in |
1248 | | * -1/REC_ERR - error |
1249 | | * 1/REC_NOMATCH - the time falls out |
1250 | | */ |
1251 | | int check_tmrec(const tmrec_p _trp, ac_tm_p _atp) |
1252 | 0 | { |
1253 | | /* it is before the start date or outside a non-recurring interval? */ |
1254 | 0 | if (_atp->time < _trp->dtstart || |
1255 | 0 | (!_IS_SET(_trp->freq) && _atp->time >= _trp->dtend)) |
1256 | 0 | return REC_NOMATCH; |
1257 | | |
1258 | 0 | LM_DEV("1) %ld + %ld = %ld\n", _trp->dtstart, _trp->duration, _trp->dtend); |
1259 | |
|
1260 | 0 | if (!_IS_SET(_trp->freq) && _atp->time < _trp->dtend) |
1261 | 0 | return REC_MATCH; |
1262 | | |
1263 | 0 | LM_DEV("2) check freq\n"); |
1264 | | |
1265 | | /* check if the instance of recurrence matches the 'interval' */ |
1266 | 0 | if (check_freq_interval(_trp, _atp) != REC_MATCH) |
1267 | 0 | return REC_NOMATCH; |
1268 | | |
1269 | 0 | LM_DEV("3) check min unit\n"); |
1270 | |
|
1271 | 0 | if (check_min_unit(_trp, _atp) != REC_MATCH) |
1272 | 0 | return REC_NOMATCH; |
1273 | | |
1274 | 0 | LM_DEV("4) check byxxx\n"); |
1275 | |
|
1276 | 0 | if (check_byxxx(_trp, _atp) != REC_MATCH) |
1277 | 0 | return REC_NOMATCH; |
1278 | | |
1279 | 0 | LM_DEV("5) check until\n"); |
1280 | |
|
1281 | 0 | return (!_IS_SET(_trp->until) || _atp->time <= _trp->until) ? |
1282 | 0 | REC_MATCH : REC_NOMATCH; |
1283 | 0 | } |
1284 | | |
1285 | | |
1286 | | int check_freq_interval(tmrec_p _trp, ac_tm_p _atp) |
1287 | 0 | { |
1288 | 0 | int _t0, _t1; |
1289 | 0 | struct tm _tm; |
1290 | |
|
1291 | 0 | if(!_IS_SET(_trp->freq)) |
1292 | 0 | return REC_NOMATCH; |
1293 | | |
1294 | 0 | LM_DEV("have freq\n"); |
1295 | |
|
1296 | 0 | if(!_IS_SET(_trp->interval) || _trp->interval==1) |
1297 | 0 | return REC_MATCH; |
1298 | | |
1299 | 0 | LM_DEV("have interval (%d)\n", _trp->interval); |
1300 | |
|
1301 | 0 | switch(_trp->freq) |
1302 | 0 | { |
1303 | 0 | case FREQ_DAILY: |
1304 | 0 | case FREQ_WEEKLY: |
1305 | 0 | memset(&_tm, 0, sizeof(struct tm)); |
1306 | 0 | _tm.tm_year = _trp->ts.tm_year; |
1307 | 0 | _tm.tm_mon = _trp->ts.tm_mon; |
1308 | 0 | _tm.tm_mday = _trp->ts.tm_mday; |
1309 | 0 | _t0 = (int)mktime(&_tm); |
1310 | 0 | memset(&_tm, 0, sizeof(struct tm)); |
1311 | 0 | _tm.tm_year = _atp->t.tm_year; |
1312 | 0 | _tm.tm_mon = _atp->t.tm_mon; |
1313 | 0 | _tm.tm_mday = _atp->t.tm_mday; |
1314 | 0 | _t1 = (int)mktime(&_tm); |
1315 | 0 | if(_trp->freq == FREQ_DAILY) |
1316 | 0 | return (((_t1-_t0)/(24*3600))%_trp->interval==0)? |
1317 | 0 | REC_MATCH:REC_NOMATCH; |
1318 | | #ifdef USE_YWEEK_U |
1319 | | _t0 -= _trp->ts.tm_wday*24*3600; |
1320 | | _t1 -= _atp->t.tm_wday*24*3600; |
1321 | | #else |
1322 | 0 | _t0 -= ((_trp->ts.tm_wday+6)%7)*24*3600; |
1323 | 0 | _t1 -= ((_atp->t.tm_wday+6)%7)*24*3600; |
1324 | 0 | #endif |
1325 | 0 | return (((_t1-_t0)/(7*24*3600))%_trp->interval==0)? |
1326 | 0 | REC_MATCH:REC_NOMATCH; |
1327 | 0 | case FREQ_MONTHLY: |
1328 | 0 | _t0 = (_atp->t.tm_year-_trp->ts.tm_year)*12 |
1329 | 0 | + _atp->t.tm_mon-_trp->ts.tm_mon; |
1330 | 0 | return (_t0%_trp->interval==0)?REC_MATCH:REC_NOMATCH; |
1331 | 0 | case FREQ_YEARLY: |
1332 | 0 | return ((_atp->t.tm_year-_trp->ts.tm_year)%_trp->interval==0)? |
1333 | 0 | REC_MATCH:REC_NOMATCH; |
1334 | 0 | } |
1335 | | |
1336 | 0 | return REC_NOMATCH; |
1337 | 0 | } |
1338 | | |
1339 | | static inline int get_min_interval(tmrec_p _trp) |
1340 | 0 | { |
1341 | 0 | if(_trp->freq == FREQ_DAILY || _trp->byday || _trp->bymday || _trp->byyday) |
1342 | 0 | return FREQ_DAILY; |
1343 | 0 | if(_trp->freq == FREQ_WEEKLY || _trp->byweekno) |
1344 | 0 | return FREQ_WEEKLY; |
1345 | 0 | if(_trp->freq == FREQ_MONTHLY || _trp->bymonth) |
1346 | 0 | return FREQ_MONTHLY; |
1347 | 0 | if(_trp->freq == FREQ_YEARLY) |
1348 | 0 | return FREQ_YEARLY; |
1349 | | |
1350 | 0 | return FREQ_NOFREQ; |
1351 | 0 | } |
1352 | | |
1353 | | |
1354 | | int check_recur_itv(struct tm *x, struct tm *bgn, struct tm *end, |
1355 | | time_t dur, int freq) |
1356 | 0 | { |
1357 | 0 | int d1, d2, dx; |
1358 | 0 | long diff; |
1359 | |
|
1360 | 0 | switch (freq) { |
1361 | 0 | case FREQ_YEARLY: |
1362 | 0 | LM_DEV("YEARLY\n"); |
1363 | 0 | d1 = bgn->tm_yday; |
1364 | 0 | d2 = end->tm_yday; |
1365 | 0 | dx = x->tm_yday; |
1366 | 0 | break; |
1367 | | |
1368 | 0 | case FREQ_MONTHLY: |
1369 | 0 | LM_DEV("MONTHLY\n"); |
1370 | 0 | d1 = bgn->tm_mday; |
1371 | 0 | d2 = end->tm_mday; |
1372 | 0 | dx = x->tm_mday; |
1373 | 0 | break; |
1374 | | |
1375 | 0 | case FREQ_WEEKLY: |
1376 | 0 | LM_DEV("WEEKLY\n"); |
1377 | 0 | d1 = bgn->tm_wday; |
1378 | 0 | d2 = end->tm_wday; |
1379 | 0 | dx = x->tm_wday; |
1380 | 0 | break; |
1381 | | |
1382 | 0 | case FREQ_DAILY: |
1383 | 0 | default: |
1384 | 0 | LM_DEV("DAILY\n"); |
1385 | 0 | if (bgn->tm_mday == end->tm_mday) { |
1386 | 0 | LM_DEV("DAILY-1\n"); |
1387 | 0 | diff = x->tm_hour*3600 + x->tm_min*60 + x->tm_sec - |
1388 | 0 | (bgn->tm_hour*3600 + bgn->tm_min*60 + bgn->tm_sec); |
1389 | 0 | if (diff < 0) |
1390 | 0 | return REC_NOMATCH; |
1391 | | |
1392 | 0 | diff = end->tm_hour*3600 + end->tm_min*60 + end->tm_sec - |
1393 | 0 | (x->tm_hour*3600 + x->tm_min*60 + x->tm_sec); |
1394 | 0 | if (diff <= 0) |
1395 | 0 | return REC_NOMATCH; |
1396 | | |
1397 | 0 | LM_DEV("MATCH\n"); |
1398 | 0 | return REC_MATCH; |
1399 | |
|
1400 | 0 | } else { |
1401 | 0 | LM_DEV("DAILY-2\n"); |
1402 | 0 | diff = bgn->tm_hour*3600 + bgn->tm_min*60 + bgn->tm_sec - |
1403 | 0 | (x->tm_hour*3600 + x->tm_min*60 + x->tm_sec); |
1404 | 0 | if (diff <= 0) |
1405 | 0 | return REC_MATCH; |
1406 | | |
1407 | 0 | diff = x->tm_hour*3600 + x->tm_min*60 + x->tm_sec - |
1408 | 0 | (end->tm_hour*3600 + end->tm_min*60 + end->tm_sec); |
1409 | 0 | if (diff < 0) |
1410 | 0 | return REC_MATCH; |
1411 | | |
1412 | 0 | LM_DEV("NOMATCH\n"); |
1413 | 0 | return REC_NOMATCH; |
1414 | 0 | } |
1415 | 0 | } |
1416 | | |
1417 | 0 | LM_DEV("check intervals\n"); |
1418 | | |
1419 | | /* continuous interval (e.g. "M [ T W T F ] S S") */ |
1420 | 0 | if (d1 < d2) { |
1421 | 0 | LM_DEV("CI-1\n"); |
1422 | 0 | if (dx < d1 || dx > d2) |
1423 | 0 | return REC_NOMATCH; |
1424 | | |
1425 | 0 | if (dx > d1 && dx < d2) |
1426 | 0 | return REC_MATCH; |
1427 | | |
1428 | | /* overlapping interval (e.g. "1 2 ... 20 ] 21 ... 29 [ 30 31") */ |
1429 | 0 | } else if (d2 < d1) { |
1430 | 0 | LM_DEV("CI-2\n"); |
1431 | 0 | if (dx > d2 && dx < d1) |
1432 | 0 | return REC_NOMATCH; |
1433 | | |
1434 | 0 | if (dx < d2 || dx > d1) |
1435 | 0 | return REC_MATCH; |
1436 | |
|
1437 | 0 | } else if (dx != d1) { |
1438 | 0 | LM_DEV("CI-3\n"); |
1439 | 0 | if (dur <= SEC_DAILY) |
1440 | 0 | return REC_NOMATCH; |
1441 | 0 | else |
1442 | 0 | return REC_MATCH; |
1443 | |
|
1444 | 0 | } else { |
1445 | 0 | LM_DEV("CI-4\n"); |
1446 | 0 | diff = x->tm_hour*3600 + x->tm_min*60 + x->tm_sec - |
1447 | 0 | (bgn->tm_hour*3600 + bgn->tm_min*60 + bgn->tm_sec); |
1448 | 0 | if (diff < 0) |
1449 | 0 | return REC_NOMATCH; |
1450 | | |
1451 | 0 | diff = end->tm_hour*3600 + end->tm_min*60 + end->tm_sec - |
1452 | 0 | (x->tm_hour*3600 + x->tm_min*60 + x->tm_sec); |
1453 | 0 | if (diff <= 0) |
1454 | 0 | return REC_NOMATCH; |
1455 | | |
1456 | 0 | return REC_MATCH; |
1457 | 0 | } |
1458 | | |
1459 | 0 | if (dx == d1) |
1460 | 0 | diff = x->tm_hour*3600 + x->tm_min*60 + x->tm_sec - |
1461 | 0 | (bgn->tm_hour*3600 + bgn->tm_min*60 + bgn->tm_sec); |
1462 | 0 | else |
1463 | 0 | diff = end->tm_hour*3600 + end->tm_min*60 + end->tm_sec - |
1464 | 0 | (x->tm_hour*3600 + x->tm_min*60 + x->tm_sec); |
1465 | |
|
1466 | 0 | return diff > 0 ? REC_MATCH : REC_NOMATCH; |
1467 | 0 | } |
1468 | | |
1469 | | |
1470 | | int check_min_unit(tmrec_p _trp, ac_tm_p _atp) |
1471 | 0 | { |
1472 | 0 | int min_itv; |
1473 | 0 | struct tm end; |
1474 | |
|
1475 | 0 | min_itv = get_min_interval(_trp); |
1476 | 0 | LM_DEV("min_itv: %d\n", min_itv); |
1477 | |
|
1478 | 0 | switch (min_itv) { |
1479 | 0 | case FREQ_DAILY: |
1480 | 0 | if (_trp->duration >= SEC_DAILY) |
1481 | 0 | return REC_MATCH; |
1482 | 0 | break; |
1483 | | |
1484 | 0 | case FREQ_WEEKLY: |
1485 | 0 | if (_trp->duration >= SEC_WEEKLY) |
1486 | 0 | return REC_MATCH; |
1487 | 0 | break; |
1488 | | |
1489 | 0 | case FREQ_MONTHLY: |
1490 | 0 | if (_trp->duration >= SEC_MONTHLY_MAX) |
1491 | 0 | return REC_MATCH; |
1492 | 0 | break; |
1493 | | |
1494 | 0 | case FREQ_YEARLY: |
1495 | 0 | if (_trp->duration >= SEC_YEARLY_MAX) |
1496 | 0 | return REC_MATCH; |
1497 | 0 | break; |
1498 | | |
1499 | 0 | default: |
1500 | 0 | return REC_NOMATCH; |
1501 | 0 | } |
1502 | | |
1503 | 0 | LM_DEV("check recur...\n"); |
1504 | |
|
1505 | 0 | localtime_r(&_trp->dtend, &end); |
1506 | 0 | return check_recur_itv(&_atp->t, &_trp->ts, &end, _trp->duration, min_itv); |
1507 | 0 | } |
1508 | | |
1509 | | int check_byxxx(tmrec_p _trp, ac_tm_p _atp) |
1510 | 0 | { |
1511 | 0 | int i; |
1512 | 0 | ac_maxval_p _amp; |
1513 | |
|
1514 | 0 | if (!(_trp->flags & TR_BYXXX)) |
1515 | 0 | return REC_MATCH; |
1516 | | |
1517 | 0 | _amp = ac_get_maxval(_atp); |
1518 | |
|
1519 | 0 | if(_trp->bymonth) |
1520 | 0 | { |
1521 | 0 | for(i=0; i<_trp->bymonth->nr; i++) |
1522 | 0 | { |
1523 | 0 | if(_atp->t.tm_mon == |
1524 | 0 | ((_trp->bymonth->xxx[i] - 1)*_trp->bymonth->req[i]+12)%12) |
1525 | 0 | break; |
1526 | 0 | } |
1527 | 0 | if(i>=_trp->bymonth->nr) |
1528 | 0 | return REC_NOMATCH; |
1529 | 0 | } |
1530 | 0 | if(_trp->freq==FREQ_YEARLY && _trp->byweekno) |
1531 | 0 | { |
1532 | 0 | for(i=0; i<_trp->byweekno->nr; i++) |
1533 | 0 | { |
1534 | 0 | if(_atp->yweek == ((_trp->byweekno->xxx[i] - 1) *_trp->byweekno->req[i]+ |
1535 | 0 | _amp->yweek)%_amp->yweek) |
1536 | 0 | break; |
1537 | 0 | } |
1538 | 0 | if(i>=_trp->byweekno->nr) |
1539 | 0 | return REC_NOMATCH; |
1540 | 0 | } |
1541 | 0 | if(_trp->byyday) |
1542 | 0 | { |
1543 | 0 | for(i=0; i<_trp->byyday->nr; i++) |
1544 | 0 | { |
1545 | 0 | if(_atp->t.tm_yday == ((_trp->byyday->xxx[i] - 1)*_trp->byyday->req[i]+ |
1546 | 0 | _amp->yday)%_amp->yday) |
1547 | 0 | break; |
1548 | 0 | } |
1549 | 0 | if(i>=_trp->byyday->nr) |
1550 | 0 | return REC_NOMATCH; |
1551 | 0 | } |
1552 | 0 | if(_trp->bymday) |
1553 | 0 | { |
1554 | 0 | for(i=0; i<_trp->bymday->nr; i++) |
1555 | 0 | { |
1556 | | #ifdef EXTRA_DEBUG |
1557 | | LM_DBG("%d == %d\n", _atp->t.tm_mday, |
1558 | | (_trp->bymday->xxx[i]*_trp->bymday->req[i]+ |
1559 | | _amp->mday)%_amp->mday + ((_trp->bymday->req[i]<0)?1:0)); |
1560 | | #endif |
1561 | 0 | if(_atp->t.tm_mday == (_trp->bymday->xxx[i]*_trp->bymday->req[i]+ |
1562 | 0 | _amp->mday)%_amp->mday + ((_trp->bymday->req[i]<0)?1:0)) |
1563 | 0 | break; |
1564 | 0 | } |
1565 | 0 | if(i>=_trp->bymday->nr) |
1566 | 0 | return REC_NOMATCH; |
1567 | 0 | } |
1568 | 0 | if(_trp->byday) |
1569 | 0 | { |
1570 | 0 | for(i=0; i<_trp->byday->nr; i++) |
1571 | 0 | { |
1572 | 0 | if(_trp->freq==FREQ_YEARLY) |
1573 | 0 | { |
1574 | | #ifdef EXTRA_DEBUG |
1575 | | LM_DBG("%d==%d && %d==%d\n", _atp->t.tm_wday, |
1576 | | _trp->byday->xxx[i], _atp->ywday, |
1577 | | (_trp->byday->req[i]+_amp->ywday)%_amp->ywday); |
1578 | | #endif |
1579 | 0 | if(_atp->t.tm_wday == _trp->byday->xxx[i] && |
1580 | 0 | _atp->ywday == (_trp->byday->req[i]+_amp->ywday)% |
1581 | 0 | _amp->ywday) |
1582 | 0 | break; |
1583 | 0 | } |
1584 | 0 | else |
1585 | 0 | { |
1586 | 0 | if(_trp->freq==FREQ_MONTHLY) |
1587 | 0 | { |
1588 | | #ifdef EXTRA_DEBUG |
1589 | | LM_DBG("%d==%d && %d==%d [%d]\n", _atp->t.tm_wday, |
1590 | | _trp->byday->xxx[i], _atp->wom, |
1591 | | (_trp->byday->req[i]+_amp->mwday)%_amp->mwday, |
1592 | | _amp->mwday); |
1593 | | #endif |
1594 | 0 | if(_atp->t.tm_wday == _trp->byday->xxx[i] && |
1595 | 0 | _atp->wom==(_trp->byday->req[i]+ |
1596 | 0 | _amp->mwday)%_amp->mwday) |
1597 | 0 | break; |
1598 | 0 | } |
1599 | 0 | else |
1600 | 0 | { |
1601 | 0 | if(_atp->t.tm_wday == _trp->byday->xxx[i]) |
1602 | 0 | break; |
1603 | 0 | } |
1604 | 0 | } |
1605 | 0 | } |
1606 | 0 | if(i>=_trp->byday->nr) |
1607 | 0 | return REC_NOMATCH; |
1608 | 0 | } |
1609 | | |
1610 | 0 | return REC_MATCH; |
1611 | 0 | } |
1612 | | |
1613 | | |
1614 | | static inline int _tmrec_parse(const char *tr, tmrec_t *time_rec) |
1615 | 0 | { |
1616 | 0 | char *p, *s; |
1617 | 0 | osips_free_t free_f = (time_rec->flags & PKG_ALLOC ? |
1618 | 0 | osips_pkg_free : osips_shm_free); |
1619 | | |
1620 | | /* empty definition? */ |
1621 | 0 | if (!tr || *tr == '\0') |
1622 | 0 | return 0; |
1623 | | |
1624 | 0 | p = (char *)tr; |
1625 | |
|
1626 | 0 | load_TR_value(p, s, time_rec, tr_parse_tz, parse_error, done); |
1627 | | |
1628 | | /* Important: make sure to set the tz NOW, before more parsing... */ |
1629 | 0 | if (time_rec->tz) |
1630 | 0 | _tz_set(time_rec->tz); |
1631 | |
|
1632 | 0 | load_TR_value(p, s, time_rec, tr_parse_dtstart, parse_error, done); |
1633 | 0 | load_TR_value(p, s, time_rec, tr_parse_dtend, parse_error, done); |
1634 | 0 | load_TR_value(p, s, time_rec, tr_parse_duration, parse_error, done); |
1635 | 0 | load_TR_value(p, s, time_rec, tr_parse_freq, parse_error, done); |
1636 | 0 | load_TR_value(p, s, time_rec, tr_parse_until, parse_error, done); |
1637 | 0 | load_TR_value(p, s, time_rec, tr_parse_interval, parse_error, done); |
1638 | 0 | load_TR_value(p, s, time_rec, tr_parse_byday, parse_error, done); |
1639 | 0 | load_TR_value(p, s, time_rec, tr_parse_bymday, parse_error, done); |
1640 | 0 | load_TR_value(p, s, time_rec, tr_parse_byyday, parse_error, done); |
1641 | 0 | load_TR_value(p, s, time_rec, tr_parse_byweekno, parse_error, done); |
1642 | 0 | load_TR_value(p, s, time_rec, tr_parse_bymonth, parse_error, done); |
1643 | | |
1644 | 0 | done: |
1645 | 0 | if (time_rec->tz) |
1646 | 0 | tz_reset(); |
1647 | |
|
1648 | 0 | if (!_IS_SET(time_rec->dtstart)) |
1649 | 0 | time_rec->dtstart = 0; /* invalid; auto-fix to 19700101T000000 */ |
1650 | |
|
1651 | 0 | if (!_IS_SET(time_rec->duration)) { |
1652 | 0 | if (!_IS_SET(time_rec->dtend)) { |
1653 | | /* invalid; since we don't support mementos, we need a duration */ |
1654 | 0 | switch (get_min_interval(time_rec)) { |
1655 | 0 | case FREQ_DAILY: |
1656 | 0 | time_rec->dtend = time_rec->dtstart + SEC_DAILY - |
1657 | 0 | (time_rec->ts.tm_hour * 3600 + time_rec->ts.tm_min * 60 |
1658 | 0 | + time_rec->ts.tm_sec); /* until the end of the day */ |
1659 | 0 | break; |
1660 | 0 | case FREQ_WEEKLY: |
1661 | 0 | time_rec->dtend = time_rec->dtstart + SEC_WEEKLY - |
1662 | 0 | (time_rec->ts.tm_wday * SEC_DAILY |
1663 | 0 | + time_rec->ts.tm_hour * 3600 + time_rec->ts.tm_min * 60 |
1664 | 0 | + time_rec->ts.tm_sec); /* until the end of the week */ |
1665 | 0 | break; |
1666 | 0 | case FREQ_MONTHLY: |
1667 | 0 | time_rec->dtend = time_rec->dtstart + SEC_MONTHLY_MAX - |
1668 | 0 | (time_rec->ts.tm_mday * SEC_DAILY |
1669 | 0 | + time_rec->ts.tm_hour * 3600 + time_rec->ts.tm_min * 60 |
1670 | 0 | + time_rec->ts.tm_sec); /* until the end of the month */ |
1671 | 0 | break; |
1672 | 0 | case FREQ_YEARLY: |
1673 | 0 | time_rec->dtend = time_rec->dtstart + SEC_YEARLY_MAX - |
1674 | 0 | (time_rec->ts.tm_yday * SEC_DAILY |
1675 | 0 | + time_rec->ts.tm_hour * 3600 + time_rec->ts.tm_min * 60 |
1676 | 0 | + time_rec->ts.tm_sec); /* until the end of the year */ |
1677 | 0 | break; |
1678 | 0 | default: |
1679 | 0 | time_rec->dtend = TR_DTEND_MAX; |
1680 | 0 | } |
1681 | 0 | } |
1682 | | |
1683 | 0 | time_rec->duration = time_rec->dtend - time_rec->dtstart; |
1684 | 0 | } else { |
1685 | 0 | time_rec->dtend = time_rec->dtstart + time_rec->duration; |
1686 | 0 | } |
1687 | | |
1688 | 0 | if (!_IS_SET(time_rec->freq) && |
1689 | 0 | (_IS_SET(time_rec->until) || _IS_SET(time_rec->interval) |
1690 | 0 | || time_rec->flags & TR_BYXXX)) { |
1691 | 0 | LM_ERR("missing FREQ component in time rec: '%s'\n", tr); |
1692 | 0 | return -1; |
1693 | 0 | } |
1694 | | |
1695 | 0 | return 0; |
1696 | | |
1697 | 0 | parse_error: |
1698 | 0 | LM_ERR("parse error in <%s> around position %i\n", |
1699 | 0 | tr, (int)(long)(p-tr)); |
1700 | 0 | if (time_rec->tz) { |
1701 | 0 | free_f(time_rec->tz); |
1702 | 0 | tz_reset(); |
1703 | 0 | time_rec->tz = NULL; |
1704 | 0 | } |
1705 | |
|
1706 | 0 | return -1; |
1707 | 0 | } |
1708 | | |
1709 | | |
1710 | | tmrec *tmrec_parse(const char *tr, char alloc) |
1711 | 0 | { |
1712 | 0 | tmrec_p time_rec; |
1713 | |
|
1714 | 0 | time_rec = tmrec_new(alloc); |
1715 | 0 | if (!time_rec) { |
1716 | 0 | LM_ERR("oom\n"); |
1717 | 0 | return NULL; |
1718 | 0 | } |
1719 | | |
1720 | 0 | if (_tmrec_parse(tr, time_rec) < 0) { |
1721 | 0 | tmrec_free(time_rec); |
1722 | 0 | return NULL; |
1723 | 0 | } |
1724 | | |
1725 | 0 | return (tmrec *)time_rec; |
1726 | 0 | } |
1727 | | |
1728 | | |
1729 | | int _tmrec_check(const tmrec *_tr, time_t time) |
1730 | 0 | { |
1731 | 0 | tmrec_t *tr = (tmrec_p)_tr; |
1732 | 0 | ac_tm_t att; |
1733 | 0 | int rc; |
1734 | | |
1735 | | /* shortcut: if there is no dstart, timerec is valid */ |
1736 | 0 | if (!_IS_SET(tr->dtstart)) |
1737 | 0 | return 1; |
1738 | | |
1739 | 0 | if (tr->tz) |
1740 | 0 | _tz_set(tr->tz); |
1741 | | |
1742 | | /* set current time */ |
1743 | 0 | ac_tm_set_time(&att, time); |
1744 | | |
1745 | | /* does the recv_time match the specified interval? */ |
1746 | 0 | rc = check_tmrec(tr, &att); |
1747 | |
|
1748 | 0 | if (tr->tz) |
1749 | 0 | tz_reset(); |
1750 | |
|
1751 | 0 | return rc == 0; |
1752 | 0 | } |
1753 | | |
1754 | | |
1755 | | int _tmrec_check_str(const char *tr, time_t check_time) |
1756 | 0 | { |
1757 | 0 | tmrec_t time_rec; |
1758 | 0 | int rc; |
1759 | |
|
1760 | 0 | LM_DBG("checking: '%s'\n", tr); |
1761 | 0 | tmrec_init(&time_rec); |
1762 | 0 | time_rec.flags = PKG_ALLOC; |
1763 | |
|
1764 | 0 | if (_tmrec_parse(tr, &time_rec) < 0) { |
1765 | 0 | LM_ERR("failed to parse time rec\n"); |
1766 | 0 | tmrec_free_members(&time_rec, time_rec.flags); |
1767 | 0 | return -2; |
1768 | 0 | } |
1769 | | |
1770 | 0 | rc = _tmrec_check(&time_rec, check_time) ? 1 : -1; |
1771 | |
|
1772 | 0 | tmrec_free_members(&time_rec, time_rec.flags); |
1773 | |
|
1774 | 0 | return rc; |
1775 | 0 | } |
1776 | | |
1777 | | |
1778 | | int _tmrec_expr_check_str(const char *trx, time_t check_time) |
1779 | 0 | { |
1780 | 0 | char *p, *q, bkp, tmp = 77, need_close, invert_next = 0, op = 0; |
1781 | 0 | str aux; |
1782 | 0 | enum { |
1783 | 0 | NEED_OPERAND, |
1784 | 0 | NEED_OPERATOR, |
1785 | 0 | } state = NEED_OPERAND; |
1786 | |
|
1787 | 0 | int rc = 0, _rc; |
1788 | 0 | char is_valid; |
1789 | |
|
1790 | 0 | LM_DBG("checking: %s\n", trx); |
1791 | | |
1792 | | /* NULL input -> nothing to match against -> no match! */ |
1793 | 0 | if (!trx) |
1794 | 0 | return -1; |
1795 | | |
1796 | 0 | for (p = (char *)trx; *p != '\0'; p++) { |
1797 | 0 | if (is_ws(*p)) |
1798 | 0 | continue; |
1799 | | |
1800 | 0 | switch (state) { |
1801 | 0 | case NEED_OPERAND: |
1802 | 0 | switch (*p) { |
1803 | 0 | case ')': |
1804 | 0 | case '&': |
1805 | 0 | case '/': |
1806 | 0 | LM_ERR("failed to parse time rec (unexpected '%c')\n", *p); |
1807 | 0 | goto parse_err; |
1808 | | |
1809 | 0 | case '!': |
1810 | 0 | invert_next = !invert_next; |
1811 | 0 | continue; |
1812 | | |
1813 | 0 | case '(': |
1814 | 0 | for (need_close = 1, q = p + 1; *q != '\0'; q++) { |
1815 | 0 | switch (*q) { |
1816 | 0 | case '(': |
1817 | 0 | need_close++; |
1818 | 0 | break; |
1819 | | |
1820 | 0 | case ')': |
1821 | 0 | need_close--; |
1822 | 0 | break; |
1823 | | |
1824 | 0 | default: |
1825 | 0 | continue; |
1826 | 0 | } |
1827 | | |
1828 | 0 | if (!need_close) |
1829 | 0 | break; |
1830 | 0 | } |
1831 | | |
1832 | 0 | if (need_close) { |
1833 | 0 | LM_ERR("failed to parse time rec (bad parentheses)\n"); |
1834 | 0 | goto parse_err; |
1835 | 0 | } |
1836 | | |
1837 | 0 | aux.s = p + 1; |
1838 | 0 | aux.len = q - aux.s; |
1839 | 0 | trim(&aux); |
1840 | |
|
1841 | 0 | bkp = aux.s[aux.len]; |
1842 | 0 | aux.s[aux.len] = '\0'; |
1843 | 0 | _rc = _tmrec_expr_check_str(aux.s, check_time) + 1; |
1844 | 0 | aux.s[aux.len] = bkp; |
1845 | |
|
1846 | 0 | if (_rc < 0) |
1847 | 0 | goto parse_err; |
1848 | | |
1849 | 0 | p = q; |
1850 | |
|
1851 | 0 | if (invert_next) { |
1852 | 0 | invert_next = 0; |
1853 | 0 | _rc = (_rc + 2) % 4; |
1854 | 0 | } |
1855 | |
|
1856 | 0 | if (op == 1) |
1857 | 0 | rc &= _rc; |
1858 | 0 | else if (op == 2) |
1859 | 0 | rc |= _rc; |
1860 | 0 | else |
1861 | 0 | rc = tmp = _rc; |
1862 | |
|
1863 | 0 | state = NEED_OPERATOR; |
1864 | 0 | break; |
1865 | | |
1866 | 0 | default: |
1867 | 0 | for (is_valid = 0, q = p + 1; *q != '\0'; q++) { |
1868 | 0 | if (*q == '!' || *q == '(' || *q == ')') { |
1869 | 0 | LM_ERR("failed to parse multi time rec at '%c' " |
1870 | 0 | "(unexpected character)\n", *q); |
1871 | 0 | goto parse_err; |
1872 | 0 | } else if (*q == TR_SEPARATOR) { |
1873 | 0 | is_valid = 1; |
1874 | 0 | } |
1875 | | |
1876 | 0 | if (is_ws(*q)) { |
1877 | 0 | state = NEED_OPERATOR; |
1878 | 0 | break; |
1879 | 0 | } else if (*q == '&' || (*q == '/' && is_valid)) { |
1880 | 0 | break; |
1881 | 0 | } |
1882 | 0 | } |
1883 | | |
1884 | 0 | aux.s = p; |
1885 | 0 | aux.len = q - aux.s; |
1886 | 0 | trim(&aux); |
1887 | |
|
1888 | 0 | bkp = aux.s[aux.len]; |
1889 | 0 | aux.s[aux.len] = '\0'; |
1890 | 0 | _rc = _tmrec_check_str(aux.s, check_time) + 1; |
1891 | 0 | aux.s[aux.len] = bkp; |
1892 | |
|
1893 | 0 | if (_rc < 0) { |
1894 | 0 | LM_ERR("failed to parse single time rec: '%.*s'\n", |
1895 | 0 | aux.len, aux.s); |
1896 | 0 | return _rc - 1; |
1897 | 0 | } |
1898 | | |
1899 | 0 | if (invert_next) { |
1900 | 0 | invert_next = 0; |
1901 | 0 | _rc = (_rc + 2) % 4; |
1902 | 0 | } |
1903 | |
|
1904 | 0 | if (*q == '&') { |
1905 | 0 | if (op == 2) { |
1906 | 0 | LM_ERR("failed to parse rec at '&' (only 1 operator " |
1907 | 0 | "type is allowed within an expression)\n"); |
1908 | 0 | goto parse_err; |
1909 | 0 | } |
1910 | | |
1911 | 0 | if (op == 0) |
1912 | 0 | rc = _rc; |
1913 | 0 | op = 1; |
1914 | 0 | } else if (*q == '/') { |
1915 | 0 | if (op == 1) { |
1916 | 0 | LM_ERR("failed to parse rec at '/' (only 1 operator " |
1917 | 0 | "type is allowed within an expression)\n"); |
1918 | 0 | goto parse_err; |
1919 | 0 | } |
1920 | | |
1921 | 0 | op = 2; |
1922 | 0 | } |
1923 | | |
1924 | 0 | if (op == 1) |
1925 | 0 | rc &= _rc; |
1926 | 0 | else if (op == 2) |
1927 | 0 | rc |= _rc; |
1928 | 0 | else |
1929 | 0 | rc = tmp = _rc; |
1930 | |
|
1931 | 0 | if (*q == '\0') |
1932 | 0 | return rc - 1; |
1933 | | |
1934 | 0 | p = q; |
1935 | 0 | } |
1936 | 0 | break; |
1937 | | |
1938 | 0 | case NEED_OPERATOR: |
1939 | 0 | switch (*p) { |
1940 | 0 | case '&': |
1941 | 0 | if (op == 2) { |
1942 | 0 | LM_ERR("failed to parse rec at '&' (only 1 operator " |
1943 | 0 | "type is allowed within an expression)\n"); |
1944 | 0 | goto parse_err; |
1945 | 0 | } else if (op == 0) { |
1946 | 0 | rc = tmp; |
1947 | 0 | } |
1948 | | |
1949 | 0 | op = 1; |
1950 | 0 | state = NEED_OPERAND; |
1951 | 0 | break; |
1952 | | |
1953 | 0 | case '/': |
1954 | 0 | if (op == 1) { |
1955 | 0 | LM_ERR("failed to parse rec at '/' (only 1 operator " |
1956 | 0 | "type is allowed within an expression)\n"); |
1957 | 0 | goto parse_err; |
1958 | 0 | } |
1959 | | |
1960 | 0 | op = 2; |
1961 | 0 | state = NEED_OPERAND; |
1962 | 0 | break; |
1963 | | |
1964 | 0 | default: |
1965 | 0 | LM_ERR("failed to parse the rec string (bad char: '%c', " |
1966 | 0 | "expected operator)\n", *p); |
1967 | 0 | goto parse_err; |
1968 | 0 | } |
1969 | 0 | } |
1970 | 0 | } |
1971 | | |
1972 | 0 | if (state == NEED_OPERAND && op != 0) { |
1973 | 0 | LM_ERR("failed to parse the rec string (missing operand)\n"); |
1974 | 0 | LM_ERR("input given: '%s'\n", trx); |
1975 | 0 | return -2; |
1976 | 0 | } |
1977 | | |
1978 | 0 | if (invert_next) |
1979 | 0 | return (rc + 2) % 4 - 1; |
1980 | 0 | else |
1981 | 0 | return rc - 1; |
1982 | | |
1983 | 0 | return rc; |
1984 | | |
1985 | 0 | parse_err: |
1986 | 0 | LM_ERR("input given: '%s'\n", trx); |
1987 | 0 | return -2; |
1988 | 0 | } |
1989 | | |
1990 | | |
1991 | | tmrec_expr *tmrec_expr_parse(const char *trx, char alloc_type) |
1992 | 0 | { |
1993 | 0 | enum { |
1994 | 0 | NEED_OPERAND, |
1995 | 0 | NEED_OPERATOR, |
1996 | 0 | } state = NEED_OPERAND; |
1997 | |
|
1998 | 0 | tmrec_expr_t *exp, *e; |
1999 | 0 | osips_malloc_t malloc_f; |
2000 | 0 | char *p, *q, bkp, need_close, invert_next = 0, is_valid; |
2001 | 0 | str aux; |
2002 | 0 | int rc; |
2003 | |
|
2004 | 0 | if (!trx) |
2005 | 0 | return NULL; |
2006 | | |
2007 | 0 | LM_DBG("checking: %s\n", trx); |
2008 | |
|
2009 | 0 | malloc_f = (alloc_type & PKG_ALLOC ? |
2010 | 0 | osips_pkg_malloc : osips_shm_malloc); |
2011 | 0 | exp = malloc_f(sizeof *exp); |
2012 | 0 | if (!exp) { |
2013 | 0 | LM_ERR("oom\n"); |
2014 | 0 | return NULL; |
2015 | 0 | } |
2016 | | |
2017 | 0 | tmrec_expr_init(exp); |
2018 | 0 | exp->flags = alloc_type; |
2019 | |
|
2020 | 0 | for (p = (char *)trx; *p != '\0'; p++) { |
2021 | 0 | if (is_ws(*p)) |
2022 | 0 | continue; |
2023 | | |
2024 | 0 | switch (state) { |
2025 | 0 | case NEED_OPERAND: |
2026 | 0 | switch (*p) { |
2027 | 0 | case ')': |
2028 | 0 | case '&': |
2029 | 0 | case '/': |
2030 | 0 | LM_ERR("failed to parse time rec (unexpected '%c')\n", *p); |
2031 | 0 | goto parse_err; |
2032 | | |
2033 | 0 | case '!': |
2034 | 0 | invert_next = !invert_next; |
2035 | 0 | continue; |
2036 | | |
2037 | 0 | case '(': |
2038 | 0 | for (need_close = 1, q = p + 1; *q != '\0'; q++) { |
2039 | 0 | switch (*q) { |
2040 | 0 | case '(': |
2041 | 0 | need_close++; |
2042 | 0 | break; |
2043 | | |
2044 | 0 | case ')': |
2045 | 0 | need_close--; |
2046 | 0 | break; |
2047 | | |
2048 | 0 | default: |
2049 | 0 | continue; |
2050 | 0 | } |
2051 | | |
2052 | 0 | if (!need_close) |
2053 | 0 | break; |
2054 | 0 | } |
2055 | | |
2056 | 0 | if (need_close) { |
2057 | 0 | LM_ERR("failed to parse time rec (bad parentheses)\n"); |
2058 | 0 | goto parse_err; |
2059 | 0 | } |
2060 | | |
2061 | 0 | aux.s = p + 1; |
2062 | 0 | aux.len = q - aux.s; |
2063 | 0 | trim(&aux); |
2064 | |
|
2065 | 0 | bkp = aux.s[aux.len]; |
2066 | 0 | aux.s[aux.len] = '\0'; |
2067 | 0 | e = tmrec_expr_parse(aux.s, alloc_type); |
2068 | 0 | aux.s[aux.len] = bkp; |
2069 | |
|
2070 | 0 | if (!e) |
2071 | 0 | goto parse_err; |
2072 | | |
2073 | 0 | if (invert_next) { |
2074 | 0 | invert_next = 0; |
2075 | 0 | e->inverted = 1; |
2076 | 0 | } |
2077 | |
|
2078 | 0 | list_add_tail(&e->list, &exp->operands); |
2079 | 0 | p = q; |
2080 | 0 | state = NEED_OPERATOR; |
2081 | 0 | break; |
2082 | | |
2083 | 0 | default: |
2084 | 0 | for (is_valid = 0, q = p + 1; *q != '\0'; q++) { |
2085 | 0 | if (*q == '!' || *q == '(' || *q == ')') { |
2086 | 0 | LM_ERR("failed to parse multi time rec at '%c' " |
2087 | 0 | "(unexpected character)\n", *q); |
2088 | 0 | goto parse_err; |
2089 | 0 | } else if (*q == TR_SEPARATOR) { |
2090 | 0 | is_valid = 1; |
2091 | 0 | } |
2092 | | |
2093 | 0 | if (is_ws(*q)) { |
2094 | 0 | state = NEED_OPERATOR; |
2095 | 0 | break; |
2096 | 0 | } else if (*q == '&' || (*q == '/' && is_valid)) { |
2097 | 0 | break; |
2098 | 0 | } |
2099 | 0 | } |
2100 | | |
2101 | 0 | e = malloc_f(sizeof *e); |
2102 | 0 | if (!e) { |
2103 | 0 | LM_ERR("oom\n"); |
2104 | 0 | goto parse_err; |
2105 | 0 | } |
2106 | | |
2107 | 0 | tmrec_expr_init(e); |
2108 | 0 | e->flags = e->tr.flags = alloc_type; |
2109 | 0 | e->is_leaf = 1; |
2110 | |
|
2111 | 0 | list_add_tail(&e->list, &exp->operands); |
2112 | |
|
2113 | 0 | aux.s = p; |
2114 | 0 | aux.len = q - aux.s; |
2115 | 0 | trim(&aux); |
2116 | |
|
2117 | 0 | bkp = aux.s[aux.len]; |
2118 | 0 | aux.s[aux.len] = '\0'; |
2119 | 0 | rc = _tmrec_parse(aux.s, &e->tr); |
2120 | 0 | aux.s[aux.len] = bkp; |
2121 | |
|
2122 | 0 | if (rc < 0) |
2123 | 0 | goto parse_err; |
2124 | | |
2125 | 0 | if (invert_next) { |
2126 | 0 | invert_next = 0; |
2127 | 0 | e->inverted = 1; |
2128 | 0 | } |
2129 | |
|
2130 | 0 | if (*q == '&') { |
2131 | 0 | if (exp->op == TR_OP_OR) { |
2132 | 0 | LM_ERR("failed to parse rec at '&' (only 1 operator " |
2133 | 0 | "type is allowed within an expression)\n"); |
2134 | 0 | goto parse_err; |
2135 | 0 | } |
2136 | | |
2137 | 0 | exp->op = TR_OP_AND; |
2138 | 0 | } else if (*q == '/') { |
2139 | 0 | if (exp->op == TR_OP_AND) { |
2140 | 0 | LM_ERR("failed to parse rec at '/' (only 1 operator " |
2141 | 0 | "type is allowed within an expression)\n"); |
2142 | 0 | goto parse_err; |
2143 | 0 | } |
2144 | | |
2145 | 0 | exp->op = TR_OP_OR; |
2146 | 0 | } |
2147 | | |
2148 | 0 | if (*q == '\0') |
2149 | 0 | return exp; |
2150 | | |
2151 | 0 | p = q; |
2152 | 0 | } |
2153 | 0 | break; |
2154 | | |
2155 | 0 | case NEED_OPERATOR: |
2156 | 0 | switch (*p) { |
2157 | 0 | case '&': |
2158 | 0 | if (exp->op == TR_OP_OR) { |
2159 | 0 | LM_ERR("failed to parse rec at '&' (only 1 operator " |
2160 | 0 | "type is allowed within an expression)\n"); |
2161 | 0 | goto parse_err; |
2162 | 0 | } |
2163 | | |
2164 | 0 | exp->op = TR_OP_AND; |
2165 | 0 | state = NEED_OPERAND; |
2166 | 0 | break; |
2167 | | |
2168 | 0 | case '/': |
2169 | 0 | if (exp->op == TR_OP_AND) { |
2170 | 0 | LM_ERR("failed to parse rec at '/' (only 1 operator " |
2171 | 0 | "type is allowed within an expression)\n"); |
2172 | 0 | goto parse_err; |
2173 | 0 | } |
2174 | | |
2175 | 0 | exp->op = TR_OP_OR; |
2176 | 0 | state = NEED_OPERAND; |
2177 | 0 | break; |
2178 | | |
2179 | 0 | default: |
2180 | 0 | LM_ERR("failed to parse the rec string (bad char: '%c', " |
2181 | 0 | "expected operator)\n", *p); |
2182 | 0 | goto parse_err; |
2183 | 0 | } |
2184 | 0 | } |
2185 | 0 | } |
2186 | | |
2187 | 0 | if (state == NEED_OPERAND && exp->op != TR_OP_NUL) { |
2188 | 0 | LM_ERR("failed to parse the rec string (missing operand)\n"); |
2189 | 0 | goto parse_err; |
2190 | 0 | } |
2191 | | |
2192 | 0 | if (invert_next) |
2193 | 0 | exp->inverted = 1; |
2194 | |
|
2195 | 0 | return exp; |
2196 | | |
2197 | 0 | parse_err: |
2198 | 0 | tmrec_expr_free(exp); |
2199 | 0 | LM_ERR("parsing failed, input given: '%s'\n", trx); |
2200 | 0 | return NULL; |
2201 | 0 | } |
2202 | | |
2203 | | |
2204 | | int _tmrec_expr_check(const tmrec_expr *_trx, time_t check_time) |
2205 | 0 | { |
2206 | 0 | struct list_head *el; |
2207 | 0 | const tmrec_expr_t *exp, *trx = (const tmrec_expr_t *)_trx; |
2208 | 0 | int rc = 0; |
2209 | |
|
2210 | 0 | if (!trx) |
2211 | 0 | return -1; |
2212 | | |
2213 | 0 | if (trx->is_leaf) { |
2214 | 0 | rc = _tmrec_check(&trx->tr, check_time) ? 2 : 0; |
2215 | 0 | goto out; |
2216 | 0 | } |
2217 | | |
2218 | 0 | if (list_empty(&trx->operands)) |
2219 | 0 | goto out; |
2220 | | |
2221 | 0 | rc = (trx->op == TR_OP_AND ? 2 : 0); |
2222 | |
|
2223 | 0 | list_for_each (el, &trx->operands) { |
2224 | 0 | exp = list_entry(el, tmrec_expr_t, list); |
2225 | |
|
2226 | 0 | if (trx->op == TR_OP_AND) |
2227 | 0 | rc = rc & (_tmrec_expr_check(exp, check_time) + 1); |
2228 | 0 | else |
2229 | 0 | rc = rc | (_tmrec_expr_check(exp, check_time) + 1); |
2230 | 0 | } |
2231 | |
|
2232 | 0 | out: |
2233 | 0 | if (trx->inverted) { |
2234 | 0 | LM_DEV("result: %d\n", (rc + 2) % 4 - 1); |
2235 | 0 | return (rc + 2) % 4 - 1; |
2236 | 0 | } |
2237 | | |
2238 | 0 | LM_DEV("result: %d\n", rc - 1); |
2239 | 0 | return rc - 1; |
2240 | 0 | } |
2241 | | |
2242 | | |
2243 | | void tmrec_expr_free(tmrec_expr *_trx) |
2244 | 0 | { |
2245 | 0 | struct list_head *el, *next; |
2246 | 0 | tmrec_expr_t *exp, *trx = (tmrec_expr_t *)_trx; |
2247 | 0 | osips_free_t free_f; |
2248 | |
|
2249 | 0 | if (!trx) |
2250 | 0 | return; |
2251 | | |
2252 | 0 | free_f = (trx->flags & PKG_ALLOC ? |
2253 | 0 | osips_pkg_free : osips_shm_free); |
2254 | |
|
2255 | 0 | list_for_each_safe (el, next, &trx->operands) { |
2256 | 0 | exp = list_entry(el, tmrec_expr_t, list); |
2257 | 0 | tmrec_expr_free(exp); |
2258 | 0 | } |
2259 | |
|
2260 | 0 | tmrec_free_members(&trx->tr, trx->flags); |
2261 | 0 | free_f(trx); |
2262 | 0 | } |
2263 | | |
2264 | | |
2265 | | int _tz_offset(const char *tz, time_t t) |
2266 | 0 | { |
2267 | 0 | struct tm lt = {0}; |
2268 | |
|
2269 | 0 | _tz_set(tz); |
2270 | 0 | localtime_r(&t, <); |
2271 | 0 | tz_reset(); |
2272 | |
|
2273 | 0 | return lt.tm_gmtoff; |
2274 | 0 | } |