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