vary.c (d3e240cb4503475c3a5ccbf957ed12a135a102ce) vary.c (7ca215a69037714e5f3cb65c394d3467621daa4c)
1/*-
2 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30#include <err.h>
31#include <time.h>
32#include <string.h>
33#include <stdlib.h>
34#include "vary.h"
35
36struct trans {
37 int val;
1#include <time.h>
2#include <string.h>
3#include <stdlib.h>
4#include "vary.h"
5
6struct trans {
7 int val;
38 const char *str;
8 char *str;
39};
40
41static struct trans trans_mon[] = {
9};
10
11static struct trans trans_mon[] = {
12 { 1, "jan" }, { 2, "feb" }, { 3, "mar" }, { 4, "apr" }, { 5, "may" },
13 { 6, "jun" }, { 7, "jul" }, { 8, "aug" }, { 9, "sep" }, { 10, "oct" },
14 { 11, "nov" }, { 12, "dec" },
42 { 1, "january" }, { 2, "february" }, { 3, "march" }, { 4, "april" },
15 { 1, "january" }, { 2, "february" }, { 3, "march" }, { 4, "april" },
43 { 5, "may"}, { 6, "june" }, { 7, "july" }, { 8, "august" },
44 { 9, "september" }, { 10, "october" }, { 11, "november" }, { 12, "december" },
16 { 6, "june" }, { 7, "july" }, { 8, "august" }, { 9, "september" },
17 { 10, "october" }, { 11, "november" }, { 12, "december" },
45 { -1, NULL }
46};
47
48static struct trans trans_wday[] = {
18 { -1, NULL }
19};
20
21static struct trans trans_wday[] = {
22 { 0, "sun" }, { 1, "mon" }, { 2, "tue" }, { 3, "wed" }, { 4, "thr" },
23 { 4, "thu" }, { 5, "fri" }, { 6, "sat" },
49 { 0, "sunday" }, { 1, "monday" }, { 2, "tuesday" }, { 3, "wednesday" },
50 { 4, "thursday" }, { 5, "friday" }, { 6, "saturday" },
51 { -1, NULL }
52};
53
54static char digits[] = "0123456789";
24 { 0, "sunday" }, { 1, "monday" }, { 2, "tuesday" }, { 3, "wednesday" },
25 { 4, "thursday" }, { 5, "friday" }, { 6, "saturday" },
26 { -1, NULL }
27};
28
29static char digits[] = "0123456789";
55static int adjhour(struct tm *, char, int, int);
56
57static int
30
31static int
58domktime(struct tm *t, char type)
59{
60 time_t ret;
61
62 while ((ret = mktime(t)) == -1 && t->tm_year > 68 && t->tm_year < 138)
63 /* While mktime() fails, adjust by an hour */
64 adjhour(t, type == '-' ? type : '+', 1, 0);
65
66 return ret;
67}
68
69static int
70trans(const struct trans t[], const char *arg)
71{
72 int f;
73
32trans(const struct trans t[], const char *arg)
33{
34 int f;
35
36 if (strspn(arg, digits) == strlen(arg))
37 return atoi(arg);
38
74 for (f = 0; t[f].val != -1; f++)
39 for (f = 0; t[f].val != -1; f++)
75 if (!strncasecmp(t[f].str, arg, 3) ||
76 !strncasecmp(t[f].str, arg, strlen(t[f].str)))
40 if (!strcasecmp(t[f].str, arg))
77 return t[f].val;
78
79 return -1;
80}
81
82struct vary *
41 return t[f].val;
42
43 return -1;
44}
45
46struct vary *
83vary_append(struct vary *v, char *arg)
47vary_append(struct vary *v, char flag, char *arg)
84{
85 struct vary *result, **nextp;
86
48{
49 struct vary *result, **nextp;
50
51 if (!strchr("DWMY", flag))
52 return 0;
53
87 if (v) {
88 result = v;
89 while (v->next)
90 v = v->next;
91 nextp = &v->next;
92 } else
93 nextp = &result;
94
54 if (v) {
55 result = v;
56 while (v->next)
57 v = v->next;
58 nextp = &v->next;
59 } else
60 nextp = &result;
61
95 if ((*nextp = (struct vary *)malloc(sizeof(struct vary))) == NULL)
96 err(1, "malloc");
62 *nextp = (struct vary *)malloc(sizeof(struct vary));
63 (*nextp)->flag = flag;
97 (*nextp)->arg = arg;
98 (*nextp)->next = NULL;
99 return result;
100}
101
102static int mdays[12] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
103
104static int

--- 15 unchanged lines hidden (view full) ---

120 else if (t->tm_mon >= 0 && t->tm_mon < 12)
121 return mdays[t->tm_mon];
122
123 return 0;
124}
125
126
127static int
64 (*nextp)->arg = arg;
65 (*nextp)->next = NULL;
66 return result;
67}
68
69static int mdays[12] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
70
71static int

--- 15 unchanged lines hidden (view full) ---

87 else if (t->tm_mon >= 0 && t->tm_mon < 12)
88 return mdays[t->tm_mon];
89
90 return 0;
91}
92
93
94static int
128adjyear(struct tm *t, char type, int val, int mk)
95adjyear(struct tm *t, char type, int val)
129{
130 switch (type) {
131 case '+':
132 t->tm_year += val;
133 break;
134 case '-':
135 t->tm_year -= val;
136 break;
137 default:
138 t->tm_year = val;
139 if (t->tm_year < 69)
140 t->tm_year += 100; /* as per date.c */
141 else if (t->tm_year > 1900)
142 t->tm_year -= 1900; /* struct tm holds years since 1900 */
143 break;
144 }
96{
97 switch (type) {
98 case '+':
99 t->tm_year += val;
100 break;
101 case '-':
102 t->tm_year -= val;
103 break;
104 default:
105 t->tm_year = val;
106 if (t->tm_year < 69)
107 t->tm_year += 100; /* as per date.c */
108 else if (t->tm_year > 1900)
109 t->tm_year -= 1900; /* struct tm holds years since 1900 */
110 break;
111 }
145 return !mk || domktime(t, type) != -1;
112 return mktime(t) != -1;
146}
147
148static int
113}
114
115static int
149adjmon(struct tm *t, char type, int val, int istext, int mk)
116sadjyear(struct tm *t, char *arg)
150{
117{
151 int lmdays;
118 switch (*arg) {
119 case '+':
120 case '-':
121 return adjyear(t, *arg, atoi(arg+1));
122 default:
123 return adjyear(t, '\0', atoi(arg));
124 }
125}
152
126
127static int
128adjmon(struct tm *t, char type, int val, int istext)
129{
153 if (val < 0)
154 return 0;
155
156 switch (type) {
157 case '+':
130 if (val < 0)
131 return 0;
132
133 switch (type) {
134 case '+':
158 if (istext) {
135 if (istext)
159 if (val <= t->tm_mon)
160 val += 11 - t->tm_mon; /* early next year */
161 else
162 val -= t->tm_mon + 1; /* later this year */
136 if (val <= t->tm_mon)
137 val += 11 - t->tm_mon; /* early next year */
138 else
139 val -= t->tm_mon + 1; /* later this year */
163 }
164 if (val) {
165 if (!adjyear(t, '+', (t->tm_mon + val) / 12, 0))
166 return 0;
167 val %= 12;
168 t->tm_mon += val;
169 if (t->tm_mon > 11)
170 t->tm_mon -= 12;
171 }
140 if (!adjyear(t, '+', (t->tm_mon + val) / 12))
141 return 0;
142 val %= 12;
143 t->tm_mon += val;
144 if (t->tm_mon > 11)
145 t->tm_mon -= 12;
172 break;
173
174 case '-':
146 break;
147
148 case '-':
175 if (istext) {
149 if (istext)
176 if (val-1 > t->tm_mon)
177 val = 13 - val + t->tm_mon; /* later last year */
178 else
179 val = t->tm_mon - val + 1; /* early this year */
150 if (val-1 > t->tm_mon)
151 val = 13 - val + t->tm_mon; /* later last year */
152 else
153 val = t->tm_mon - val + 1; /* early this year */
180 }
181 if (val) {
182 if (!adjyear(t, '-', val / 12, 0))
154 if (!adjyear(t, '-', val / 12))
155 return 0;
156 val %= 12;
157 if (val > t->tm_mon) {
158 if (!adjyear(t, '-', 1))
183 return 0;
159 return 0;
184 val %= 12;
185 if (val > t->tm_mon) {
186 if (!adjyear(t, '-', 1, 0))
187 return 0;
188 val -= 12;
189 }
190 t->tm_mon -= val;
160 val -= 12;
191 }
161 }
162 t->tm_mon -= val;
192 break;
193
194 default:
195 if (val > 12 || val < 1)
196 return 0;
197 t->tm_mon = --val;
198 }
199
163 break;
164
165 default:
166 if (val > 12 || val < 1)
167 return 0;
168 t->tm_mon = --val;
169 }
170
200 /* e.g., -v-1m on March, 31 is the last day of February in common sense */
201 lmdays = daysinmonth(t);
202 if (t->tm_mday > lmdays)
203 t->tm_mday = lmdays;
204
205 return !mk || domktime(t, type) != -1;
171 return mktime(t) != -1;
206}
207
208static int
172}
173
174static int
209adjday(struct tm *t, char type, int val, int mk)
175sadjmon(struct tm *t, char *arg)
210{
176{
211 int lmdays;
177 int istext;
178 int val;
212
179
180 switch (*arg) {
181 case '+':
182 case '-':
183 istext = strspn(arg+1, digits) != strlen(arg+1);
184 val = trans(trans_mon, arg+1);
185 return adjmon(t, *arg, val, istext);
186 default:
187 istext = strspn(arg, digits) != strlen(arg);
188 val = trans(trans_mon, arg);
189 return adjmon(t, '\0', val, istext);
190 }
191}
192
193static int
194adjday(struct tm *t, char type, int val)
195{
196 int mdays;
213 switch (type) {
214 case '+':
215 while (val) {
197 switch (type) {
198 case '+':
199 while (val) {
216 lmdays = daysinmonth(t);
217 if (val > lmdays - t->tm_mday) {
218 val -= lmdays - t->tm_mday + 1;
200 mdays = daysinmonth(t);
201 if (val > mdays - t->tm_mday) {
202 val -= mdays - t->tm_mday + 1;
219 t->tm_mday = 1;
203 t->tm_mday = 1;
220 if (!adjmon(t, '+', 1, 0, 0))
204 if (!adjmon(t, '+', 1, 0))
221 return 0;
222 } else {
223 t->tm_mday += val;
224 val = 0;
225 }
226 }
227 break;
228 case '-':
229 while (val)
230 if (val >= t->tm_mday) {
231 val -= t->tm_mday;
232 t->tm_mday = 1;
205 return 0;
206 } else {
207 t->tm_mday += val;
208 val = 0;
209 }
210 }
211 break;
212 case '-':
213 while (val)
214 if (val >= t->tm_mday) {
215 val -= t->tm_mday;
216 t->tm_mday = 1;
233 if (!adjmon(t, '-', 1, 0, 0))
217 if (!adjmon(t, '-', 1, 0))
234 return 0;
235 t->tm_mday = daysinmonth(t);
236 } else {
237 t->tm_mday -= val;
238 val = 0;
239 }
240 break;
241 default:
242 if (val > 0 && val <= daysinmonth(t))
243 t->tm_mday = val;
244 else
245 return 0;
246 break;
247 }
248
218 return 0;
219 t->tm_mday = daysinmonth(t);
220 } else {
221 t->tm_mday -= val;
222 val = 0;
223 }
224 break;
225 default:
226 if (val > 0 && val <= daysinmonth(t))
227 t->tm_mday = val;
228 else
229 return 0;
230 break;
231 }
232
249 return !mk || domktime(t, type) != -1;
233 return mktime(t) != -1;
250}
251
252static int
234}
235
236static int
253adjwday(struct tm *t, char type, int val, int istext, int mk)
237sadjwday(struct tm *t, char *arg)
254{
238{
239 int istext;
240 int val;
241
242 switch (*arg) {
243 case '+':
244 case '-':
245 istext = strspn(arg+1, digits) != strlen(arg+1);
246 val = trans(trans_wday, arg+1);
247 break;
248 default:
249 istext = 0;
250 val = trans(trans_wday, arg);
251 break;
252 }
253
255 if (val < 0)
256 return 0;
257
254 if (val < 0)
255 return 0;
256
258 switch (type) {
257 switch (*arg) {
259 case '+':
260 if (istext)
261 if (val < t->tm_wday)
262 val = 7 - t->tm_wday + val; /* early next week */
263 else
264 val -= t->tm_wday; /* later this week */
265 else
258 case '+':
259 if (istext)
260 if (val < t->tm_wday)
261 val = 7 - t->tm_wday + val; /* early next week */
262 else
263 val -= t->tm_wday; /* later this week */
264 else
266 val *= 7; /* "-v+5w" == "5 weeks in the future" */
267 return !val || adjday(t, '+', val, mk);
265 val *= 7; /* "-W +5" == "5 weeks in the future" */
266 return adjday(t, '+', val);
268 case '-':
267 case '-':
269 if (istext) {
268 if (istext)
270 if (val > t->tm_wday)
271 val = 7 - val + t->tm_wday; /* later last week */
272 else
273 val = t->tm_wday - val; /* early this week */
269 if (val > t->tm_wday)
270 val = 7 - val + t->tm_wday; /* later last week */
271 else
272 val = t->tm_wday - val; /* early this week */
274 } else
275 val *= 7; /* "-v-5w" == "5 weeks ago" */
276 return !val || adjday(t, '-', val, mk);
273 else
274 val *= 7; /* "-W -5" == "5 weeks ago" */
275 return adjday(t, '-', val);
277 default:
278 if (val < t->tm_wday)
276 default:
277 if (val < t->tm_wday)
279 return adjday(t, '-', t->tm_wday - val, mk);
278 return adjday(t, '-', t->tm_wday - val);
280 else if (val > 6)
281 return 0;
282 else if (val > t->tm_wday)
279 else if (val > 6)
280 return 0;
281 else if (val > t->tm_wday)
283 return adjday(t, '+', val - t->tm_wday, mk);
282 return adjday(t, '+', val - t->tm_wday);
284 }
285 return 1;
286}
287
288static int
283 }
284 return 1;
285}
286
287static int
289adjhour(struct tm *t, char type, int val, int mk)
288sadjday(struct tm *t, char *arg)
290{
289{
291 if (val < 0)
292 return 0;
293
294 switch (type) {
290 switch (*arg) {
295 case '+':
291 case '+':
296 if (val) {
297 int days;
298
299 days = (t->tm_hour + val) / 24;
300 val %= 24;
301 t->tm_hour += val;
302 t->tm_hour %= 24;
303 if (!adjday(t, '+', days, 0))
304 return 0;
305 }
306 break;
307
308 case '-':
292 case '-':
309 if (val) {
310 int days;
311
312 days = val / 24;
313 val %= 24;
314 if (val > t->tm_hour) {
315 days++;
316 val -= 24;
317 }
318 t->tm_hour -= val;
319 if (!adjday(t, '-', days, 0))
320 return 0;
321 }
322 break;
323
293 return adjday(t, *arg, atoi(arg+1));
324 default:
294 default:
325 if (val > 23)
326 return 0;
327 t->tm_hour = val;
295 return adjday(t, '\0', atoi(arg));
328 }
296 }
329
330 return !mk || domktime(t, type) != -1;
331}
332
297}
298
333static int
334adjmin(struct tm *t, char type, int val, int mk)
335{
336 if (val < 0)
337 return 0;
338
339 switch (type) {
340 case '+':
341 if (val) {
342 if (!adjhour(t, '+', (t->tm_min + val) / 60, 0))
343 return 0;
344 val %= 60;
345 t->tm_min += val;
346 if (t->tm_min > 59)
347 t->tm_min -= 60;
348 }
349 break;
350
351 case '-':
352 if (val) {
353 if (!adjhour(t, '-', val / 60, 0))
354 return 0;
355 val %= 60;
356 if (val > t->tm_min) {
357 if (!adjhour(t, '-', 1, 0))
358 return 0;
359 val -= 60;
360 }
361 t->tm_min -= val;
362 }
363 break;
364
365 default:
366 if (val > 59)
367 return 0;
368 t->tm_min = val;
369 }
370
371 return !mk || domktime(t, type) != -1;
372}
373
374static int
375adjsec(struct tm *t, char type, int val, int mk)
376{
377 if (val < 0)
378 return 0;
379
380 switch (type) {
381 case '+':
382 if (val) {
383 if (!adjmin(t, '+', (t->tm_sec + val) / 60, 0))
384 return 0;
385 val %= 60;
386 t->tm_sec += val;
387 if (t->tm_sec > 59)
388 t->tm_sec -= 60;
389 }
390 break;
391
392 case '-':
393 if (val) {
394 if (!adjmin(t, '-', val / 60, 0))
395 return 0;
396 val %= 60;
397 if (val > t->tm_sec) {
398 if (!adjmin(t, '-', 1, 0))
399 return 0;
400 val -= 60;
401 }
402 t->tm_sec -= val;
403 }
404 break;
405
406 default:
407 if (val > 59)
408 return 0;
409 t->tm_sec = val;
410 }
411
412 return !mk || domktime(t, type) != -1;
413}
414
415const struct vary *
416vary_apply(const struct vary *v, struct tm *t)
417{
299const struct vary *
300vary_apply(const struct vary *v, struct tm *t)
301{
418 char type;
419 char which;
420 char *arg;
421 size_t len;
422 int val;
423
424 for (; v; v = v->next) {
302 for (; v; v = v->next) {
425 type = *v->arg;
426 arg = v->arg;
427 if (type == '+' || type == '-')
428 arg++;
429 else
430 type = '\0';
431 len = strlen(arg);
432 if (len < 2)
433 return v;
434
435 if (type == '\0')
436 t->tm_isdst = -1;
437
438 if (strspn(arg, digits) != len-1) {
439 val = trans(trans_wday, arg);
440 if (val != -1) {
441 if (!adjwday(t, type, val, 1, 1))
442 return v;
443 } else {
444 val = trans(trans_mon, arg);
445 if (val != -1) {
446 if (!adjmon(t, type, val, 1, 1))
447 return v;
448 } else
303 switch (v->flag) {
304 case 'D':
305 if (!sadjday(t, v->arg))
449 return v;
306 return v;
450 }
451 } else {
452 val = atoi(arg);
453 which = arg[len-1];
454
455 switch (which) {
456 case 'S':
457 if (!adjsec(t, type, val, 1))
458 return v;
459 break;
460 case 'M':
461 if (!adjmin(t, type, val, 1))
462 return v;
463 break;
464 case 'H':
465 if (!adjhour(t, type, val, 1))
466 return v;
467 break;
468 case 'd':
469 t->tm_isdst = -1;
470 if (!adjday(t, type, val, 1))
471 return v;
472 break;
473 case 'w':
474 t->tm_isdst = -1;
475 if (!adjwday(t, type, val, 0, 1))
476 return v;
477 break;
478 case 'm':
479 t->tm_isdst = -1;
480 if (!adjmon(t, type, val, 0, 1))
481 return v;
482 break;
483 case 'y':
484 t->tm_isdst = -1;
485 if (!adjyear(t, type, val, 1))
486 return v;
487 break;
488 default:
307 break;
308 case 'W':
309 if (!sadjwday(t, v->arg))
489 return v;
310 return v;
490 }
311 break;
312 case 'M':
313 if (!sadjmon(t, v->arg))
314 return v;
315 break;
316 case 'Y':
317 if (!sadjyear(t, v->arg))
318 return v;
319 break;
320 default:
321 return v;
491 }
492 }
493 return 0;
494}
495
496void
497vary_destroy(struct vary *v)
498{
499 struct vary *n;
500
501 while (v) {
502 n = v->next;
503 free(v);
504 v = n;
505 }
506}
322 }
323 }
324 return 0;
325}
326
327void
328vary_destroy(struct vary *v)
329{
330 struct vary *n;
331
332 while (v) {
333 n = v->next;
334 free(v);
335 v = n;
336 }
337}