xref: /freebsd/usr.bin/calendar/parsedata.c (revision 56e53cb8ef000c3ef72337a4095987a932cdedef)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1992-2009 Edwin Groothuis <edwin@FreeBSD.org>.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <ctype.h>
34 #include <math.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <err.h>
39 
40 #include "calendar.h"
41 
42 static char *showflags(int flags);
43 static int isonlydigits(char *s, int nostar);
44 static const char *getmonthname(int i);
45 static int checkmonth(char *s, size_t *len, size_t *offset, const char **month);
46 static const char *getdayofweekname(int i);
47 static int checkdayofweek(char *s, size_t *len, size_t *offset, const char **dow);
48 static int indextooffset(char *s);
49 static int parseoffset(char *s);
50 static char *floattoday(int year, double f);
51 static char *floattotime(double f);
52 static int wdayom (int day, int offset, int month, int year);
53 
54 /*
55  * Expected styles:
56  *
57  * Date			::=	Month . ' ' . DayOfMonth |
58  *				Month . ' ' . DayOfWeek . ModifierIndex |
59  *				Month . '/' . DayOfMonth |
60  *				Month . '/' . DayOfWeek . ModifierIndex |
61  *				DayOfMonth . ' ' . Month |
62  *				DayOfMonth . '/' . Month |
63  *				DayOfWeek . ModifierIndex . ' ' .Month |
64  *				DayOfWeek . ModifierIndex . '/' .Month |
65  *				DayOfWeek . ModifierIndex |
66  *				SpecialDay . ModifierOffset
67  *
68  * Month		::=	MonthName | MonthNumber | '*'
69  * MonthNumber		::=	'0' ... '9' | '00' ... '09' | '10' ... '12'
70  * MonthName		::=	MonthNameShort | MonthNameLong
71  * MonthNameLong	::=	'January' ... 'December'
72  * MonthNameShort	::=	'Jan' ... 'Dec' | 'Jan.' ... 'Dec.'
73  *
74  * DayOfWeek		::=	DayOfWeekShort | DayOfWeekLong
75  * DayOfWeekShort	::=	'Mon' .. 'Sun'
76  * DayOfWeekLong	::=	'Monday' .. 'Sunday'
77  * DayOfMonth		::=	'0' ... '9' | '00' ... '09' | '10' ... '29' |
78  *				'30' ... '31' | '*'
79  *
80  * ModifierOffset	::=	'' | '+' . ModifierNumber | '-' . ModifierNumber
81  * ModifierNumber	::=	'0' ... '9' | '00' ... '99' | '000' ... '299' |
82  *				'300' ... '359' | '360' ... '365'
83  * ModifierIndex	::=	'Second' | 'Third' | 'Fourth' | 'Fifth' |
84  *				'First' | 'Last'
85  *
86  * SpecialDay		::=	'Easter' | 'Paskha' | 'ChineseNewYear'
87  *
88  */
89 static int
90 determinestyle(char *date, int *flags,
91     char *month, int *imonth, char *dayofmonth, int *idayofmonth,
92     char *dayofweek, int *idayofweek, char *modifieroffset,
93     char *modifierindex, char *specialday, char *year, int *iyear)
94 {
95 	char *p, *p1, *p2, *py;
96 	const char *dow, *pmonth;
97 	char pold;
98 	size_t len, offset;
99 
100 	*flags = F_NONE;
101 	*month = '\0';
102 	*imonth = 0;
103 	*year = '\0';
104 	*iyear = 0;
105 	*dayofmonth = '\0';
106 	*idayofmonth = 0;
107 	*dayofweek = '\0';
108 	*idayofweek = 0;
109 	*modifieroffset = '\0';
110 	*modifierindex = '\0';
111 	*specialday = '\0';
112 
113 #define CHECKSPECIAL(s1, s2, lens2, type)				\
114 	if (s2 != NULL && strncmp(s1, s2, lens2) == 0) {		\
115 		*flags |= F_SPECIALDAY;					\
116 		*flags |= type;						\
117 		*flags |= F_VARIABLE;					\
118 		if (strlen(s1) == lens2) {				\
119 			strcpy(specialday, s1);				\
120 			return (1);					\
121 		}							\
122 		strncpy(specialday, s1, lens2);				\
123 		specialday[lens2] = '\0';				\
124 		strcpy(modifieroffset, s1 + lens2);			\
125 		*flags |= F_MODIFIEROFFSET;				\
126 		return (1);						\
127 	}
128 
129 	if ((p = strchr(date, ' ')) == NULL) {
130 		if ((p = strchr(date, '/')) == NULL) {
131 			CHECKSPECIAL(date, STRING_CNY, strlen(STRING_CNY),
132 			    F_CNY);
133 			CHECKSPECIAL(date, ncny.name, ncny.len, F_CNY);
134 			CHECKSPECIAL(date, STRING_NEWMOON,
135 			    strlen(STRING_NEWMOON), F_NEWMOON);
136 			CHECKSPECIAL(date, nnewmoon.name, nnewmoon.len,
137 			    F_NEWMOON);
138 			CHECKSPECIAL(date, STRING_FULLMOON,
139 			    strlen(STRING_FULLMOON), F_FULLMOON);
140 			CHECKSPECIAL(date, nfullmoon.name, nfullmoon.len,
141 			    F_FULLMOON);
142 			CHECKSPECIAL(date, STRING_PASKHA,
143 			    strlen(STRING_PASKHA), F_PASKHA);
144 			CHECKSPECIAL(date, npaskha.name, npaskha.len, F_PASKHA);
145 			CHECKSPECIAL(date, STRING_EASTER,
146 			    strlen(STRING_EASTER), F_EASTER);
147 			CHECKSPECIAL(date, neaster.name, neaster.len, F_EASTER);
148 			CHECKSPECIAL(date, STRING_MAREQUINOX,
149 			    strlen(STRING_MAREQUINOX), F_MAREQUINOX);
150 			CHECKSPECIAL(date, nmarequinox.name, nmarequinox.len,
151 			    F_SEPEQUINOX);
152 			CHECKSPECIAL(date, STRING_SEPEQUINOX,
153 			    strlen(STRING_SEPEQUINOX), F_SEPEQUINOX);
154 			CHECKSPECIAL(date, nsepequinox.name, nsepequinox.len,
155 			    F_SEPEQUINOX);
156 			CHECKSPECIAL(date, STRING_JUNSOLSTICE,
157 			    strlen(STRING_JUNSOLSTICE), F_JUNSOLSTICE);
158 			CHECKSPECIAL(date, njunsolstice.name, njunsolstice.len,
159 			    F_JUNSOLSTICE);
160 			CHECKSPECIAL(date, STRING_DECSOLSTICE,
161 			    strlen(STRING_DECSOLSTICE), F_DECSOLSTICE);
162 			CHECKSPECIAL(date, ndecsolstice.name, ndecsolstice.len,
163 			    F_DECSOLSTICE);
164 			if (checkdayofweek(date, &len, &offset, &dow) != 0) {
165 				*flags |= F_DAYOFWEEK;
166 				*flags |= F_VARIABLE;
167 				*idayofweek = offset;
168 				if (strlen(date) == len) {
169 					strcpy(dayofweek, date);
170 					return (1);
171 				}
172 				strncpy(dayofweek, date, len);
173 				dayofweek[len] = '\0';
174 				strcpy(modifierindex, date + len);
175 				*flags |= F_MODIFIERINDEX;
176 				return (1);
177 			}
178 			if (isonlydigits(date, 1)) {
179 				/* Assume month number only */
180 				*flags |= F_MONTH;
181 				*imonth = (int)strtol(date, (char **)NULL, 10);
182 				strcpy(month, getmonthname(*imonth));
183 				return(1);
184 			}
185 			return (0);
186 		}
187 	}
188 
189 	/*
190 	 * After this, leave by goto-ing to "allfine" or "fail" to restore the
191 	 * original data in `date'.
192 	 */
193 	pold = *p;
194 	*p = 0;
195 	p1 = date;
196 	p2 = p + 1;
197 	/* Now p2 points to the next field and p1 to the first field */
198 
199 	if ((py = strchr(p2, '/')) != NULL) {
200 		/* We have a year in the string. Now this is getting tricky */
201 		strcpy(year, p1);
202 		*iyear = (int)strtol(year, NULL, 10);
203 		p1 = p2;
204 		p2 = py + 1;
205 		*py = 0;
206 		*flags |= F_YEAR;
207 	}
208 
209 	/* Check if there is a month-string in the date */
210 	if ((checkmonth(p1, &len, &offset, &pmonth) != 0)
211 	    || (checkmonth(p2, &len, &offset, &pmonth) != 0 && (p2 = p1))) {
212 		/* p2 is the non-month part */
213 		*flags |= F_MONTH;
214 		*imonth = offset;
215 
216 		strcpy(month, getmonthname(offset));
217 		if (isonlydigits(p2, 1)) {
218 			strcpy(dayofmonth, p2);
219 			*idayofmonth = (int)strtol(p2, (char **)NULL, 10);
220 			*flags |= F_DAYOFMONTH;
221 			goto allfine;
222 		}
223 		if (strcmp(p2, "*") == 0) {
224 			*flags |= F_ALLDAY;
225 			goto allfine;
226 		}
227 
228 		if (checkdayofweek(p2, &len, &offset, &dow) != 0) {
229 			*flags |= F_DAYOFWEEK;
230 			*flags |= F_VARIABLE;
231 			*idayofweek = offset;
232 			strcpy(dayofweek, getdayofweekname(offset));
233 			if (strlen(p2) == len)
234 				goto allfine;
235 			strcpy(modifierindex, p2 + len);
236 			*flags |= F_MODIFIERINDEX;
237 			goto allfine;
238 		}
239 		goto fail;
240 	}
241 
242 	/* Check if there is an every-day or every-month in the string */
243 	if ((strcmp(p1, "*") == 0 && isonlydigits(p2, 1))
244 	    || (strcmp(p2, "*") == 0 && isonlydigits(p1, 1) && (p2 = p1))) {
245 		int d;
246 
247 		*flags |= F_ALLMONTH;
248 		*flags |= F_DAYOFMONTH;
249 		d = (int)strtol(p2, (char **)NULL, 10);
250 		*idayofmonth = d;
251 		sprintf(dayofmonth, "%d", d);
252 		goto allfine;
253 	}
254 
255 	/* Month as a number, then a weekday */
256 	if (isonlydigits(p1, 1)
257 	    && checkdayofweek(p2, &len, &offset, &dow) != 0) {
258 		int d;
259 
260 		*flags |= F_MONTH;
261 		*flags |= F_DAYOFWEEK;
262 		*flags |= F_VARIABLE;
263 
264 		*idayofweek = offset;
265 		d = (int)strtol(p1, (char **)NULL, 10);
266 		*imonth = d;
267 		strcpy(month, getmonthname(d));
268 
269 		strcpy(dayofweek, getdayofweekname(offset));
270 		if (strlen(p2) == len)
271 			goto allfine;
272 		strcpy(modifierindex, p2 + len);
273 		*flags |= F_MODIFIERINDEX;
274 		goto allfine;
275 	}
276 
277 	/* If both the month and date are specified as numbers */
278 	if (isonlydigits(p1, 1) && isonlydigits(p2, 0)) {
279 		/* Now who wants to be this ambiguous? :-( */
280 		int m, d;
281 
282 		if (strchr(p2, '*') != NULL)
283 			*flags |= F_VARIABLE;
284 
285 		m = (int)strtol(p1, (char **)NULL, 10);
286 		d = (int)strtol(p2, (char **)NULL, 10);
287 
288 		*flags |= F_MONTH;
289 		*flags |= F_DAYOFMONTH;
290 
291 		if (m > 12) {
292 			*imonth = d;
293 			*idayofmonth = m;
294 			strcpy(month, getmonthname(d));
295 			sprintf(dayofmonth, "%d", m);
296 		} else {
297 			*imonth = m;
298 			*idayofmonth = d;
299 			strcpy(month, getmonthname(m));
300 			sprintf(dayofmonth, "%d", d);
301 		}
302 		goto allfine;
303 	}
304 
305 	/* FALLTHROUGH */
306 fail:
307 	*p = pold;
308 	return (0);
309 allfine:
310 	*p = pold;
311 	return (1);
312 
313 }
314 
315 void
316 remember(int *rememberindex, int *y, int *m, int *d, char **ed, int yy, int mm,
317     int dd, char *extra);
318 void
319 remember(int *rememberindex, int *y, int *m, int *d, char **ed, int yy, int mm,
320     int dd, char *extra)
321 {
322 	static int warned = 0;
323 
324 	if (*rememberindex >= MAXCOUNT - 1) {
325 		if (warned == 0)
326 			warnx("Index > %d, ignored", MAXCOUNT);
327 		warned++;
328 		return;
329 	}
330 	y[*rememberindex] = yy;
331 	m[*rememberindex] = mm;
332 	d[*rememberindex] = dd;
333 	if (extra != NULL)
334 		strcpy(ed[*rememberindex], extra);
335 	else
336 		ed[*rememberindex][0] = '\0';
337 	*rememberindex += 1;
338 }
339 
340 static void
341 debug_determinestyle(int dateonly, char *date, int flags, char *month,
342     int imonth, char *dayofmonth, int idayofmonth, char *dayofweek,
343     int idayofweek, char *modifieroffset, char *modifierindex, char *specialday,
344     char *year, int iyear)
345 {
346 
347 	if (dateonly != 0) {
348 		printf("-------\ndate: |%s|\n", date);
349 		if (dateonly == 1)
350 			return;
351 	}
352 	printf("flags: %x - %s\n", flags, showflags(flags));
353 	if (modifieroffset[0] != '\0')
354 		printf("modifieroffset: |%s|\n", modifieroffset);
355 	if (modifierindex[0] != '\0')
356 		printf("modifierindex: |%s|\n", modifierindex);
357 	if (year[0] != '\0')
358 		printf("year: |%s| (%d)\n", year, iyear);
359 	if (month[0] != '\0')
360 		printf("month: |%s| (%d)\n", month, imonth);
361 	if (dayofmonth[0] != '\0')
362 		printf("dayofmonth: |%s| (%d)\n", dayofmonth, idayofmonth);
363 	if (dayofweek[0] != '\0')
364 		printf("dayofweek: |%s| (%d)\n", dayofweek, idayofweek);
365 	if (specialday[0] != '\0')
366 		printf("specialday: |%s|\n", specialday);
367 }
368 
369 static struct yearinfo {
370 	int year;
371 	int ieaster, ipaskha, firstcnyday;
372 	double ffullmoon[MAXMOONS], fnewmoon[MAXMOONS];
373 	double ffullmooncny[MAXMOONS], fnewmooncny[MAXMOONS];
374 	int ichinesemonths[MAXMOONS];
375 	double equinoxdays[2], solsticedays[2];
376 	int *monthdays;
377 	struct yearinfo *next;
378 } *years, *yearinfo;
379 
380 /*
381  * Calculate dates with offset from weekdays, like Thurs-3, Wed+2, etc.
382  * day is the day of the week,
383  * offset the ordinal number of the weekday in the month.
384  */
385 static int
386 wdayom (int day, int offset, int month, int year)
387 {
388 /* Weekday of first day in month */
389 	int wday1;                                /* first day of month */
390 /* Weekday of last day in month */
391 	int wdayn;
392 	int d;
393 
394 	wday1 = first_dayofweek_of_month(year, month);
395 	if (wday1 < 0)                          /* not set */
396 		return (wday1);
397 	/*
398 	 * Date of zeroth or first of our weekday in month, depending on the
399 	 * relationship with the first of the month.  The range is -6:6.
400 	 */
401 	d = (day - wday1 + 1) % 7;
402 	/*
403 	 * Which way are we counting?  Offset 0 is invalid, abs (offset) > 5 is
404 	 * meaningless, but that's OK.  Offset 5 may or may not be meaningless,
405 	 * so there's no point in complaining for complaining's sake.
406 	 */
407 	if (offset < 0) {			/* back from end of month */
408 						/* FIXME */
409 		wdayn = d;
410 		while (wdayn <= yearinfo->monthdays[month])
411 			wdayn += 7;
412 		d = offset * 7 + wdayn;
413 	} else if (offset > 0){
414 		if (d > 0)
415 			d += offset * 7 - 7;
416 		else
417 			d += offset * 7;
418 	} else
419 		warnx ("Invalid offset 0");
420 	return (d);
421 }
422 
423 /*
424  * Possible date formats include any combination of:
425  *	3-charmonth			(January, Jan, Jan)
426  *	3-charweekday			(Friday, Monday, mon.)
427  *	numeric month or day		(1, 2, 04)
428  *
429  * Any character may separate them, or they may not be separated.  Any line,
430  * following a line that is matched, that starts with "whitespace", is shown
431  * along with the matched line.
432  */
433 int
434 parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags,
435     char **edp)
436 {
437 	char month[100], dayofmonth[100], dayofweek[100], modifieroffset[100];
438 	char syear[100];
439 	char modifierindex[100], specialday[100];
440 	int idayofweek = -1, imonth = -1, idayofmonth = -1, iyear = -1;
441 	int year, remindex;
442 	int d, m, dow, rm, rd, offset;
443 	char *ed;
444 	int retvalsign = 1;
445 
446 	/*
447 	 * CONVENTION
448 	 *
449 	 * Month:     1-12
450 	 * Monthname: Jan .. Dec
451 	 * Day:	      1-31
452 	 * Weekday:   Mon .. Sun
453 	 *
454 	 */
455 
456 	*flags = 0;
457 
458 	if (debug)
459 		debug_determinestyle(1, date, *flags, month, imonth,
460 		    dayofmonth, idayofmonth, dayofweek, idayofweek,
461 		    modifieroffset, modifierindex, specialday, syear, iyear);
462 	if (determinestyle(date, flags, month, &imonth, dayofmonth,
463 		&idayofmonth, dayofweek, &idayofweek, modifieroffset,
464 		modifierindex, specialday, syear, &iyear) == 0) {
465 		if (debug)
466 			printf("Failed!\n");
467 		return (0);
468 	}
469 
470 	if (debug)
471 		debug_determinestyle(0, date, *flags, month, imonth,
472 		    dayofmonth, idayofmonth, dayofweek, idayofweek,
473 		    modifieroffset, modifierindex, specialday, syear, iyear);
474 
475 	remindex = 0;
476 	for (year = year1; year <= year2; year++) {
477 
478 		int lflags = *flags;
479 		/* If the year is specified, only do it if it is this year! */
480 		if ((lflags & F_YEAR) != 0)
481 			if (iyear != year)
482 				continue;
483 		lflags &= ~F_YEAR;
484 
485 		/* Get important dates for this year */
486 		yearinfo = years;
487 		while (yearinfo != NULL) {
488 			if (yearinfo->year == year)
489 				break;
490 			yearinfo = yearinfo -> next;
491 		}
492 		if (yearinfo == NULL) {
493 			yearinfo = (struct yearinfo *)calloc(1,
494 			    sizeof(struct yearinfo));
495 			if (yearinfo == NULL)
496 				errx(1, "Unable to allocate more years");
497 			yearinfo->year = year;
498 			yearinfo->next = years;
499 			years = yearinfo;
500 
501 			yearinfo->monthdays = monthdaytab[isleap(year)];
502 			yearinfo->ieaster = easter(year);
503 			yearinfo->ipaskha = paskha(year);
504 			fpom(year, UTCOffset, yearinfo->ffullmoon,
505 			    yearinfo->fnewmoon);
506 			fpom(year, UTCOFFSET_CNY, yearinfo->ffullmooncny,
507 			    yearinfo->fnewmooncny);
508 			fequinoxsolstice(year, UTCOffset,
509 			    yearinfo->equinoxdays, yearinfo->solsticedays);
510 
511 			/*
512 			 * CNY: Match day with sun longitude at 330` with new
513 			 * moon
514 			 */
515 			yearinfo->firstcnyday = calculatesunlongitude30(year,
516 			    UTCOFFSET_CNY, yearinfo->ichinesemonths);
517 			for (m = 0; yearinfo->fnewmooncny[m] >= 0; m++) {
518 				if (yearinfo->fnewmooncny[m] >
519 				    yearinfo->firstcnyday) {
520 					yearinfo->firstcnyday =
521 					    floor(yearinfo->fnewmooncny[m - 1]);
522 					break;
523 				}
524 			}
525 		}
526 
527 		/* Same day every year */
528 		if (lflags == (F_MONTH | F_DAYOFMONTH)) {
529 			if (!remember_ymd(year, imonth, idayofmonth))
530 				continue;
531 			remember(&remindex, yearp, monthp, dayp, edp,
532 			    year, imonth, idayofmonth, NULL);
533 			continue;
534 		}
535 
536 		/* XXX Same day every year, but variable */
537 		if (lflags == (F_MONTH | F_DAYOFMONTH | F_VARIABLE)) {
538 			if (!remember_ymd(year, imonth, idayofmonth))
539 				continue;
540 			remember(&remindex, yearp, monthp, dayp, edp,
541 			    year, imonth, idayofmonth, NULL);
542 			continue;
543 		}
544 
545 		/* Same day every month */
546 		if (lflags == (F_ALLMONTH | F_DAYOFMONTH)) {
547 			for (m = 1; m <= 12; m++) {
548 				if (!remember_ymd(year, m, idayofmonth))
549 					continue;
550 				remember(&remindex, yearp, monthp, dayp, edp,
551 				    year, m, idayofmonth, NULL);
552 			}
553 			continue;
554 		}
555 
556 		/* Every day of a month */
557 		if (lflags == (F_ALLDAY | F_MONTH)) {
558 			for (d = 1; d <= yearinfo->monthdays[imonth]; d++) {
559 				if (!remember_ymd(year, imonth, d))
560 					continue;
561 				remember(&remindex, yearp, monthp, dayp, edp,
562 				    year, imonth, d, NULL);
563 			}
564 			continue;
565 		}
566 
567 		/* One day of every month */
568 		if (lflags == (F_ALLMONTH | F_DAYOFWEEK)) {
569 			for (m = 1; m <= 12; m++) {
570 				if (!remember_ymd(year, m, idayofmonth))
571 					continue;
572 				remember(&remindex, yearp, monthp, dayp, edp,
573 				    year, m, idayofmonth, NULL);
574 			}
575 			continue;
576 		}
577 
578 		/* Every dayofweek of the year */
579 		if (lflags == (F_DAYOFWEEK | F_VARIABLE)) {
580 			dow = first_dayofweek_of_year(year);
581 			d = (idayofweek - dow + 8) % 7;
582 			while (d <= 366) {
583 				if (remember_yd(year, d, &rm, &rd))
584 					remember(&remindex,
585 					    yearp, monthp, dayp, edp,
586 					    year, rm, rd, NULL);
587 				d += 7;
588 			}
589 			continue;
590 		}
591 
592 		/*
593 	         * Every so-manied dayofweek of every month of the year:
594 	         * Thu-3
595 	         */
596 		if (lflags == (F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) {
597 			offset = indextooffset(modifierindex);
598 
599 			for (m = 0; m <= 12; m++) {
600 	                        d = wdayom (idayofweek, offset, m, year);
601 				if (remember_ymd(year, m, d)) {
602 					remember(&remindex,
603 					    yearp, monthp, dayp, edp,
604 					    year, m, d, NULL);
605 					continue;
606 				}
607 			}
608 			continue;
609 		}
610 
611 		/*
612 	         * A certain dayofweek of a month
613 	         * Jan/Thu-3
614 	         */
615 		if (lflags ==
616 		    (F_MONTH | F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) {
617 			offset = indextooffset(modifierindex);
618 			dow = first_dayofweek_of_month(year, imonth);
619 			d = (idayofweek - dow + 8) % 7;
620 
621 			if (offset > 0) {
622 				while (d <= yearinfo->monthdays[imonth]) {
623 					if (--offset == 0
624 					    && remember_ymd(year, imonth, d)) {
625 						remember(&remindex,
626 						    yearp, monthp, dayp, edp,
627 						    year, imonth, d, NULL);
628 						continue;
629 					}
630 					d += 7;
631 				}
632 				continue;
633 			}
634 			if (offset < 0) {
635 				while (d <= yearinfo->monthdays[imonth])
636 					d += 7;
637 				while (offset != 0) {
638 					offset++;
639 					d -= 7;
640 				}
641 				if (remember_ymd(year, imonth, d))
642 					remember(&remindex,
643 					    yearp, monthp, dayp, edp,
644 					    year, imonth, d, NULL);
645 				continue;
646 			}
647 			continue;
648 		}
649 
650 		/* Every dayofweek of the month */
651 		if (lflags == (F_DAYOFWEEK | F_MONTH | F_VARIABLE)) {
652 			dow = first_dayofweek_of_month(year, imonth);
653 			d = (idayofweek - dow + 8) % 7;
654 			while (d <= yearinfo->monthdays[imonth]) {
655 				if (remember_ymd(year, imonth, d))
656 					remember(&remindex,
657 					    yearp, monthp, dayp, edp,
658 					    year, imonth, d, NULL);
659 				d += 7;
660 			}
661 			continue;
662 		}
663 
664 		/* Easter */
665 		if ((lflags & ~F_MODIFIEROFFSET) ==
666 		    (F_SPECIALDAY | F_VARIABLE | F_EASTER)) {
667 			offset = 0;
668 			if ((lflags & F_MODIFIEROFFSET) != 0)
669 				offset = parseoffset(modifieroffset);
670 			if (remember_yd(year, yearinfo->ieaster + offset,
671 	                        &rm, &rd))
672 				remember(&remindex, yearp, monthp, dayp, edp,
673 				    year, rm, rd, NULL);
674 			continue;
675 		}
676 
677 		/* Paskha */
678 		if ((lflags & ~F_MODIFIEROFFSET) ==
679 		    (F_SPECIALDAY | F_VARIABLE | F_PASKHA)) {
680 			offset = 0;
681 			if ((lflags & F_MODIFIEROFFSET) != 0)
682 				offset = parseoffset(modifieroffset);
683 			if (remember_yd(year, yearinfo->ipaskha + offset,
684 	                        &rm, &rd))
685 				remember(&remindex, yearp, monthp, dayp, edp,
686 				    year, rm, rd, NULL);
687 			continue;
688 		}
689 
690 		/* Chinese New Year */
691 		if ((lflags & ~F_MODIFIEROFFSET) ==
692 		    (F_SPECIALDAY | F_VARIABLE | F_CNY)) {
693 			offset = 0;
694 			if ((lflags & F_MODIFIEROFFSET) != 0)
695 				offset = parseoffset(modifieroffset);
696 			if (remember_yd(year, yearinfo->firstcnyday + offset,
697 	                        &rm, &rd))
698 				remember(&remindex, yearp, monthp, dayp, edp,
699 				    year, rm, rd, NULL);
700 			continue;
701 		}
702 
703 		/* FullMoon */
704 		if ((lflags & ~F_MODIFIEROFFSET) ==
705 		    (F_SPECIALDAY | F_VARIABLE | F_FULLMOON)) {
706 			int i;
707 
708 			offset = 0;
709 			if ((lflags & F_MODIFIEROFFSET) != 0)
710 				offset = parseoffset(modifieroffset);
711 			for (i = 0; yearinfo->ffullmoon[i] > 0; i++) {
712 				if (remember_yd(year,
713 	                                floor(yearinfo->ffullmoon[i]) + offset,
714 					&rm, &rd)) {
715 					ed = floattotime(
716 					    yearinfo->ffullmoon[i]);
717 					remember(&remindex,
718 					    yearp, monthp, dayp, edp,
719 					    year, rm, rd, ed);
720 				}
721 			}
722 			continue;
723 		}
724 
725 		/* NewMoon */
726 		if ((lflags & ~F_MODIFIEROFFSET) ==
727 		    (F_SPECIALDAY | F_VARIABLE | F_NEWMOON)) {
728 			int i;
729 
730 			offset = 0;
731 			if ((lflags & F_MODIFIEROFFSET) != 0)
732 				offset = parseoffset(modifieroffset);
733 			for (i = 0; yearinfo->ffullmoon[i] > 0; i++) {
734 				if (remember_yd(year,
735 					floor(yearinfo->fnewmoon[i]) + offset,
736 					&rm, &rd)) {
737 					ed = floattotime(yearinfo->fnewmoon[i]);
738 					remember(&remindex,
739 					    yearp, monthp, dayp, edp,
740 					    year, rm, rd, ed);
741 				}
742 			}
743 			continue;
744 		}
745 
746 		/* (Mar|Sep)Equinox */
747 		if ((lflags & ~F_MODIFIEROFFSET) ==
748 		    (F_SPECIALDAY | F_VARIABLE | F_MAREQUINOX)) {
749 			offset = 0;
750 			if ((lflags & F_MODIFIEROFFSET) != 0)
751 				offset = parseoffset(modifieroffset);
752 			if (remember_yd(year, yearinfo->equinoxdays[0] + offset,
753 				&rm, &rd)) {
754 				ed = floattotime(yearinfo->equinoxdays[0]);
755 				remember(&remindex, yearp, monthp, dayp, edp,
756 				    year, rm, rd, ed);
757 			}
758 			continue;
759 		}
760 		if ((lflags & ~F_MODIFIEROFFSET) ==
761 		    (F_SPECIALDAY | F_VARIABLE | F_SEPEQUINOX)) {
762 			offset = 0;
763 			if ((lflags & F_MODIFIEROFFSET) != 0)
764 				offset = parseoffset(modifieroffset);
765 			if (remember_yd(year, yearinfo->equinoxdays[1] + offset,
766 			    &rm, &rd)) {
767 				ed = floattotime(yearinfo->equinoxdays[1]);
768 				remember(&remindex, yearp, monthp, dayp, edp,
769 				    year, rm, rd, ed);
770 			}
771 			continue;
772 		}
773 
774 		/* (Jun|Dec)Solstice */
775 		if ((lflags & ~F_MODIFIEROFFSET) ==
776 		    (F_SPECIALDAY | F_VARIABLE | F_JUNSOLSTICE)) {
777 			offset = 0;
778 			if ((lflags & F_MODIFIEROFFSET) != 0)
779 				offset = parseoffset(modifieroffset);
780 			if (remember_yd(year,
781 				yearinfo->solsticedays[0] + offset, &rm, &rd)) {
782 				ed = floattotime(yearinfo->solsticedays[0]);
783 				remember(&remindex, yearp, monthp, dayp, edp,
784 				    year, rm, rd, ed);
785 			}
786 			continue;
787 		}
788 		if ((lflags & ~F_MODIFIEROFFSET) ==
789 		    (F_SPECIALDAY | F_VARIABLE | F_DECSOLSTICE)) {
790 			offset = 0;
791 			if ((lflags & F_MODIFIEROFFSET) != 0)
792 				offset = parseoffset(modifieroffset);
793 			if (remember_yd(year,
794 				yearinfo->solsticedays[1] + offset, &rm, &rd)) {
795 				ed = floattotime(yearinfo->solsticedays[1]);
796 				remember(&remindex, yearp, monthp, dayp, edp,
797 				    year, rm, rd, ed);
798 			}
799 			continue;
800 		}
801 
802 		if (debug) {
803 			printf("Unprocessed:\n");
804 			debug_determinestyle(2, date, lflags, month, imonth,
805 			    dayofmonth, idayofmonth, dayofweek, idayofweek,
806 			    modifieroffset, modifierindex, specialday, syear,
807 			    iyear);
808 		}
809 		retvalsign = -1;
810 	}
811 
812 	if (retvalsign == -1)
813 		return (-remindex - 1);
814 	else
815 		return (remindex);
816 }
817 
818 static char *
819 showflags(int flags)
820 {
821 	static char s[1000];
822 	s[0] = '\0';
823 
824 	if ((flags & F_YEAR) != 0)
825 		strcat(s, "year ");
826 	if ((flags & F_MONTH) != 0)
827 		strcat(s, "month ");
828 	if ((flags & F_DAYOFWEEK) != 0)
829 		strcat(s, "dayofweek ");
830 	if ((flags & F_DAYOFMONTH) != 0)
831 		strcat(s, "dayofmonth ");
832 	if ((flags & F_MODIFIERINDEX) != 0)
833 		strcat(s, "modifierindex ");
834 	if ((flags & F_MODIFIEROFFSET) != 0)
835 		strcat(s, "modifieroffset ");
836 	if ((flags & F_SPECIALDAY) != 0)
837 		strcat(s, "specialday ");
838 	if ((flags & F_ALLMONTH) != 0)
839 		strcat(s, "allmonth ");
840 	if ((flags & F_ALLDAY) != 0)
841 		strcat(s, "allday ");
842 	if ((flags & F_VARIABLE) != 0)
843 		strcat(s, "variable ");
844 	if ((flags & F_CNY) != 0)
845 		strcat(s, "chinesenewyear ");
846 	if ((flags & F_PASKHA) != 0)
847 		strcat(s, "paskha ");
848 	if ((flags & F_EASTER) != 0)
849 		strcat(s, "easter ");
850 	if ((flags & F_FULLMOON) != 0)
851 		strcat(s, "fullmoon ");
852 	if ((flags & F_NEWMOON) != 0)
853 		strcat(s, "newmoon ");
854 	if ((flags & F_MAREQUINOX) != 0)
855 		strcat(s, "marequinox ");
856 	if ((flags & F_SEPEQUINOX) != 0)
857 		strcat(s, "sepequinox ");
858 	if ((flags & F_JUNSOLSTICE) != 0)
859 		strcat(s, "junsolstice ");
860 	if ((flags & F_DECSOLSTICE) != 0)
861 		strcat(s, "decsolstice ");
862 
863 	return s;
864 }
865 
866 static const char *
867 getmonthname(int i)
868 {
869 	if (i <= 0 || i > 12)
870 		return ("");
871 	if (nmonths[i - 1].len != 0 && nmonths[i - 1].name != NULL)
872 		return (nmonths[i - 1].name);
873 	return (months[i - 1]);
874 }
875 
876 static int
877 checkmonth(char *s, size_t *len, size_t *offset, const char **month)
878 {
879 	struct fixs *n;
880 	int i;
881 
882 	for (i = 0; fnmonths[i].name != NULL; i++) {
883 		n = fnmonths + i;
884 		if (strncasecmp(s, n->name, n->len) == 0) {
885 			*len = n->len;
886 			*month = n->name;
887 			*offset = i + 1;
888 			return (1);
889 		}
890 	}
891 	for (i = 0; nmonths[i].name != NULL; i++) {
892 		n = nmonths + i;
893 		if (strncasecmp(s, n->name, n->len) == 0) {
894 			*len = n->len;
895 			*month = n->name;
896 			*offset = i + 1;
897 			return (1);
898 		}
899 	}
900 	for (i = 0; fmonths[i] != NULL; i++) {
901 		*len = strlen(fmonths[i]);
902 		if (strncasecmp(s, fmonths[i], *len) == 0) {
903 			*month = fmonths[i];
904 			*offset = i + 1;
905 			return (1);
906 		}
907 	}
908 	for (i = 0; months[i] != NULL; i++) {
909 		if (strncasecmp(s, months[i], 3) == 0) {
910 			*len = 3;
911 			*month = months[i];
912 			*offset = i + 1;
913 			return (1);
914 		}
915 	}
916 	return (0);
917 }
918 
919 static const char *
920 getdayofweekname(int i)
921 {
922 	if (ndays[i].len != 0 && ndays[i].name != NULL)
923 		return (ndays[i].name);
924 	return (days[i]);
925 }
926 
927 static int
928 checkdayofweek(char *s, size_t *len, size_t *offset, const char **dow)
929 {
930 	struct fixs *n;
931 	int i;
932 
933 	for (i = 0; fndays[i].name != NULL; i++) {
934 		n = fndays + i;
935 		if (strncasecmp(s, n->name, n->len) == 0) {
936 			*len = n->len;
937 			*dow = n->name;
938 			*offset = i;
939 			return (1);
940 		}
941 	}
942 	for (i = 0; ndays[i].name != NULL; i++) {
943 		n = ndays + i;
944 		if (strncasecmp(s, n->name, n->len) == 0) {
945 			*len = n->len;
946 			*dow = n->name;
947 			*offset = i;
948 			return (1);
949 		}
950 	}
951 	for (i = 0; fdays[i] != NULL; i++) {
952 		*len = strlen(fdays[i]);
953 		if (strncasecmp(s, fdays[i], *len) == 0) {
954 			*dow = fdays[i];
955 			*offset = i;
956 			return (1);
957 		}
958 	}
959 	for (i = 0; days[i] != NULL; i++) {
960 		if (strncasecmp(s, days[i], 3) == 0) {
961 			*len = 3;
962 			*dow = days[i];
963 			*offset = i;
964 			return (1);
965 		}
966 	}
967 	return (0);
968 }
969 
970 static int
971 isonlydigits(char *s, int nostar)
972 {
973 	int i;
974 	for (i = 0; s[i] != '\0'; i++) {
975 		if (nostar == 0 && s[i] == '*' && s[i + 1] == '\0')
976 			return 1;
977 		if (!isdigit((unsigned char)s[i]))
978 			return (0);
979 	}
980 	return (1);
981 }
982 
983 static int
984 indextooffset(char *s)
985 {
986 	int i;
987 	struct fixs *n;
988 	char *es;
989 
990 	if (s[0] == '+' || s[0] == '-') {
991 		i = strtol (s, &es, 10);
992 		if (*es != '\0')                      /* trailing junk */
993 			errx (1, "Invalid specifier format: %s\n", s);
994 		return (i);
995 	}
996 
997 	for (i = 0; i < 6; i++) {
998 		if (strcasecmp(s, sequences[i]) == 0) {
999 			if (i == 5)
1000 				return (-1);
1001 			return (i + 1);
1002 		}
1003 	}
1004 	for (i = 0; i < 6; i++) {
1005 		n = nsequences + i;
1006 		if (n->len == 0)
1007 			continue;
1008 		if (strncasecmp(s, n->name, n->len) == 0) {
1009 			if (i == 5)
1010 				return (-1);
1011 			return (i + 1);
1012 		}
1013 	}
1014 	return (0);
1015 }
1016 
1017 static int
1018 parseoffset(char *s)
1019 {
1020 	return strtol(s, NULL, 10);
1021 }
1022 
1023 static char *
1024 floattotime(double f)
1025 {
1026 	static char buf[100];
1027 	int hh, mm, ss, i;
1028 
1029 	f -= floor(f);
1030 	i = f * SECSPERDAY;
1031 
1032 	hh = i / SECSPERHOUR;
1033 	i %= SECSPERHOUR;
1034 	mm = i / SECSPERMINUTE;
1035 	i %= SECSPERMINUTE;
1036 	ss = i;
1037 
1038 	sprintf(buf, "%02d:%02d:%02d", hh, mm, ss);
1039 	return (buf);
1040 }
1041 
1042 static char *
1043 floattoday(int year, double f)
1044 {
1045 	static char buf[100];
1046 	int i, m, d, hh, mm, ss;
1047 	int *cumdays = cumdaytab[isleap(year)];
1048 
1049 	for (i = 0; 1 + cumdays[i] < f; i++)
1050 		;
1051 	m = --i;
1052 	d = floor(f - 1 - cumdays[i]);
1053 	f -= floor(f);
1054 	i = f * SECSPERDAY;
1055 
1056 	hh = i / SECSPERHOUR;
1057 	i %= SECSPERHOUR;
1058 	mm = i / SECSPERMINUTE;
1059 	i %= SECSPERMINUTE;
1060 	ss = i;
1061 
1062 	sprintf(buf, "%02d-%02d %02d:%02d:%02d", m, d, hh, mm, ss);
1063 	return (buf);
1064 }
1065 
1066 void
1067 dodebug(char *what)
1068 {
1069 	int year;
1070 
1071 	printf("UTCOffset: %g\n", UTCOffset);
1072 	printf("eastlongitude: %d\n", EastLongitude);
1073 
1074 	if (strcmp(what, "moon") == 0) {
1075 		double ffullmoon[MAXMOONS], fnewmoon[MAXMOONS];
1076 		int i;
1077 
1078 		for (year = year1; year <= year2; year++) {
1079 			fpom(year, UTCOffset, ffullmoon, fnewmoon);
1080 			printf("Full moon %d:\t", year);
1081 			for (i = 0; ffullmoon[i] >= 0; i++) {
1082 				printf("%g (%s) ", ffullmoon[i],
1083 				    floattoday(year, ffullmoon[i]));
1084 			}
1085 			printf("\nNew moon %d:\t", year);
1086 			for (i = 0; fnewmoon[i] >= 0; i++) {
1087 				printf("%g (%s) ", fnewmoon[i],
1088 				    floattoday(year, fnewmoon[i]));
1089 			}
1090 			printf("\n");
1091 
1092 		}
1093 
1094 		return;
1095 	}
1096 
1097 	if (strcmp(what, "sun") == 0) {
1098 		double equinoxdays[2], solsticedays[2];
1099 		for (year = year1; year <= year2; year++) {
1100 			printf("Sun in %d:\n", year);
1101 			fequinoxsolstice(year, UTCOffset, equinoxdays,
1102 			    solsticedays);
1103 			printf("e[0] - %g (%s)\n",
1104 			    equinoxdays[0],
1105 			    floattoday(year, equinoxdays[0]));
1106 			printf("e[1] - %g (%s)\n",
1107 			    equinoxdays[1],
1108 			    floattoday(year, equinoxdays[1]));
1109 			printf("s[0] - %g (%s)\n",
1110 			    solsticedays[0],
1111 			    floattoday(year, solsticedays[0]));
1112 			printf("s[1] - %g (%s)\n",
1113 			    solsticedays[1],
1114 			    floattoday(year, solsticedays[1]));
1115 		}
1116 		return;
1117 	}
1118 }
1119