xref: /freebsd/usr.bin/calendar/parsedata.c (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #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 #define	SLEN	100	/* maximum length of date spec. part strings */
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 			strlcpy(specialday, s1, SLEN);			\
120 			return (1);					\
121 		}							\
122 		strncpy(specialday, s1, lens2);				\
123 		specialday[lens2] = '\0';				\
124 		strlcpy(modifieroffset, s1 + lens2, SLEN);		\
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 					strlcpy(dayofweek, date, SLEN);
170 					return (1);
171 				}
172 				strncpy(dayofweek, date, len);
173 				dayofweek[len] = '\0';
174 				strlcpy(modifierindex, date + len, SLEN);
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 				strlcpy(month, getmonthname(*imonth), SLEN);
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 		strlcpy(year, p1, SLEN);
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 		strlcpy(month, getmonthname(offset), SLEN);
217 		if (isonlydigits(p2, 1)) {
218 			strlcpy(dayofmonth, p2, SLEN);
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 			strlcpy(dayofweek, getdayofweekname(offset), SLEN);
233 			if (strlen(p2) == len)
234 				goto allfine;
235 			strlcpy(modifierindex, p2 + len, SLEN);
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 		snprintf(dayofmonth, SLEN, "%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 		strlcpy(month, getmonthname(d), SLEN);
268 
269 		strlcpy(dayofweek, getdayofweekname(offset), SLEN);
270 		if (strlen(p2) == len)
271 			goto allfine;
272 		strlcpy(modifierindex, p2 + len, SLEN);
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 			strlcpy(month, getmonthname(d), SLEN);
295 			snprintf(dayofmonth, SLEN, "%d", m);
296 		} else {
297 			*imonth = m;
298 			*idayofmonth = d;
299 			strlcpy(month, getmonthname(m), SLEN);
300 			snprintf(dayofmonth, SLEN, "%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 static void
316 remember(int *rememberindex, int *y, int *m, int *d, char **ed, int yy, int mm,
317     int dd, char *extra)
318 {
319 	static int warned = 0;
320 
321 	if (*rememberindex >= MAXCOUNT - 1) {
322 		if (warned == 0)
323 			warnx("Index > %d, ignored", MAXCOUNT);
324 		warned++;
325 		return;
326 	}
327 	y[*rememberindex] = yy;
328 	m[*rememberindex] = mm;
329 	d[*rememberindex] = dd;
330 	if (extra != NULL)
331 		strlcpy(ed[*rememberindex], extra, SLEN);
332 	else
333 		ed[*rememberindex][0] = '\0';
334 	*rememberindex += 1;
335 }
336 
337 static void
338 debug_determinestyle(int dateonly, char *date, int flags, char *month,
339     int imonth, char *dayofmonth, int idayofmonth, char *dayofweek,
340     int idayofweek, char *modifieroffset, char *modifierindex, char *specialday,
341     char *year, int iyear)
342 {
343 
344 	if (dateonly != 0) {
345 		printf("-------\ndate: |%s|\n", date);
346 		if (dateonly == 1)
347 			return;
348 	}
349 	printf("flags: %x - %s\n", flags, showflags(flags));
350 	if (modifieroffset[0] != '\0')
351 		printf("modifieroffset: |%s|\n", modifieroffset);
352 	if (modifierindex[0] != '\0')
353 		printf("modifierindex: |%s|\n", modifierindex);
354 	if (year[0] != '\0')
355 		printf("year: |%s| (%d)\n", year, iyear);
356 	if (month[0] != '\0')
357 		printf("month: |%s| (%d)\n", month, imonth);
358 	if (dayofmonth[0] != '\0')
359 		printf("dayofmonth: |%s| (%d)\n", dayofmonth, idayofmonth);
360 	if (dayofweek[0] != '\0')
361 		printf("dayofweek: |%s| (%d)\n", dayofweek, idayofweek);
362 	if (specialday[0] != '\0')
363 		printf("specialday: |%s|\n", specialday);
364 }
365 
366 static struct yearinfo {
367 	int year;
368 	int ieaster, ipaskha, firstcnyday;
369 	double ffullmoon[MAXMOONS], fnewmoon[MAXMOONS];
370 	double ffullmooncny[MAXMOONS], fnewmooncny[MAXMOONS];
371 	int ichinesemonths[MAXMOONS];
372 	double equinoxdays[2], solsticedays[2];
373 	int *monthdays;
374 	struct yearinfo *next;
375 } *years, *yearinfo;
376 
377 /*
378  * Calculate dates with offset from weekdays, like Thurs-3, Wed+2, etc.
379  * day is the day of the week,
380  * offset the ordinal number of the weekday in the month.
381  */
382 static int
383 wdayom (int day, int offset, int month, int year)
384 {
385 /* Weekday of first day in month */
386 	int wday1;                                /* first day of month */
387 /* Weekday of last day in month */
388 	int wdayn;
389 	int d;
390 
391 	wday1 = first_dayofweek_of_month(year, month);
392 	if (wday1 < 0)                          /* not set */
393 		return (wday1);
394 	/*
395 	 * Date of zeroth or first of our weekday in month, depending on the
396 	 * relationship with the first of the month.  The range is -6:6.
397 	 */
398 	d = (day - wday1 + 1) % 7;
399 	/*
400 	 * Which way are we counting?  Offset 0 is invalid, abs (offset) > 5 is
401 	 * meaningless, but that's OK.  Offset 5 may or may not be meaningless,
402 	 * so there's no point in complaining for complaining's sake.
403 	 */
404 	if (offset < 0) {			/* back from end of month */
405 						/* FIXME */
406 		wdayn = d;
407 		while (wdayn <= yearinfo->monthdays[month])
408 			wdayn += 7;
409 		d = offset * 7 + wdayn;
410 	} else if (offset > 0){
411 		if (d > 0)
412 			d += offset * 7 - 7;
413 		else
414 			d += offset * 7;
415 	} else
416 		warnx ("Invalid offset 0");
417 	return (d);
418 }
419 
420 /*
421  * Possible date formats include any combination of:
422  *	3-charmonth			(January, Jan, Jan)
423  *	3-charweekday			(Friday, Monday, mon.)
424  *	numeric month or day		(1, 2, 04)
425  *
426  * Any character may separate them, or they may not be separated.  Any line,
427  * following a line that is matched, that starts with "whitespace", is shown
428  * along with the matched line.
429  */
430 int
431 parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags,
432     char **edp)
433 {
434 	char month[SLEN], dayofmonth[SLEN], dayofweek[SLEN], modifieroffset[SLEN];
435 	char syear[SLEN];
436 	char modifierindex[SLEN], specialday[SLEN];
437 	int idayofweek = -1, imonth = -1, idayofmonth = -1, iyear = -1;
438 	int year, remindex;
439 	int d, m, dow, rm, rd, offset;
440 	char *ed;
441 	int retvalsign = 1;
442 
443 	/*
444 	 * CONVENTION
445 	 *
446 	 * Month:     1-12
447 	 * Monthname: Jan .. Dec
448 	 * Day:	      1-31
449 	 * Weekday:   Mon .. Sun
450 	 *
451 	 */
452 
453 	*flags = 0;
454 
455 	if (debug)
456 		debug_determinestyle(1, date, *flags, month, imonth,
457 		    dayofmonth, idayofmonth, dayofweek, idayofweek,
458 		    modifieroffset, modifierindex, specialday, syear, iyear);
459 	if (determinestyle(date, flags, month, &imonth, dayofmonth,
460 		&idayofmonth, dayofweek, &idayofweek, modifieroffset,
461 		modifierindex, specialday, syear, &iyear) == 0) {
462 		if (debug)
463 			printf("Failed!\n");
464 		return (0);
465 	}
466 
467 	if (debug)
468 		debug_determinestyle(0, date, *flags, month, imonth,
469 		    dayofmonth, idayofmonth, dayofweek, idayofweek,
470 		    modifieroffset, modifierindex, specialday, syear, iyear);
471 
472 	remindex = 0;
473 	for (year = year1; year <= year2; year++) {
474 
475 		int lflags = *flags;
476 		/* If the year is specified, only do it if it is this year! */
477 		if ((lflags & F_YEAR) != 0)
478 			if (iyear != year)
479 				continue;
480 		lflags &= ~F_YEAR;
481 
482 		/* Get important dates for this year */
483 		yearinfo = years;
484 		while (yearinfo != NULL) {
485 			if (yearinfo->year == year)
486 				break;
487 			yearinfo = yearinfo -> next;
488 		}
489 		if (yearinfo == NULL) {
490 			yearinfo = (struct yearinfo *)calloc(1,
491 			    sizeof(struct yearinfo));
492 			if (yearinfo == NULL)
493 				errx(1, "Unable to allocate more years");
494 			yearinfo->year = year;
495 			yearinfo->next = years;
496 			years = yearinfo;
497 
498 			yearinfo->monthdays = monthdaytab[isleap(year)];
499 			yearinfo->ieaster = easter(year);
500 			yearinfo->ipaskha = paskha(year);
501 			fpom(year, UTCOffset, yearinfo->ffullmoon,
502 			    yearinfo->fnewmoon);
503 			fpom(year, UTCOFFSET_CNY, yearinfo->ffullmooncny,
504 			    yearinfo->fnewmooncny);
505 			fequinoxsolstice(year, UTCOffset,
506 			    yearinfo->equinoxdays, yearinfo->solsticedays);
507 
508 			/*
509 			 * CNY: Match day with sun longitude at 330` with new
510 			 * moon
511 			 */
512 			yearinfo->firstcnyday = calculatesunlongitude30(year,
513 			    UTCOFFSET_CNY, yearinfo->ichinesemonths);
514 			for (m = 0; yearinfo->fnewmooncny[m] >= 0; m++) {
515 				if (yearinfo->fnewmooncny[m] >
516 				    yearinfo->firstcnyday) {
517 					yearinfo->firstcnyday =
518 					    floor(yearinfo->fnewmooncny[m - 1]);
519 					break;
520 				}
521 			}
522 		}
523 
524 		/* Same day every year */
525 		if (lflags == (F_MONTH | F_DAYOFMONTH)) {
526 			if (!remember_ymd(year, imonth, idayofmonth))
527 				continue;
528 			remember(&remindex, yearp, monthp, dayp, edp,
529 			    year, imonth, idayofmonth, NULL);
530 			continue;
531 		}
532 
533 		/* XXX Same day every year, but variable */
534 		if (lflags == (F_MONTH | F_DAYOFMONTH | F_VARIABLE)) {
535 			if (!remember_ymd(year, imonth, idayofmonth))
536 				continue;
537 			remember(&remindex, yearp, monthp, dayp, edp,
538 			    year, imonth, idayofmonth, NULL);
539 			continue;
540 		}
541 
542 		/* Same day every month */
543 		if (lflags == (F_ALLMONTH | F_DAYOFMONTH)) {
544 			for (m = 1; m <= 12; m++) {
545 				if (!remember_ymd(year, m, idayofmonth))
546 					continue;
547 				remember(&remindex, yearp, monthp, dayp, edp,
548 				    year, m, idayofmonth, NULL);
549 			}
550 			continue;
551 		}
552 
553 		/* Every day of a month */
554 		if (lflags == (F_ALLDAY | F_MONTH)) {
555 			for (d = 1; d <= yearinfo->monthdays[imonth]; d++) {
556 				if (!remember_ymd(year, imonth, d))
557 					continue;
558 				remember(&remindex, yearp, monthp, dayp, edp,
559 				    year, imonth, d, NULL);
560 			}
561 			continue;
562 		}
563 
564 		/* One day of every month */
565 		if (lflags == (F_ALLMONTH | F_DAYOFWEEK)) {
566 			for (m = 1; m <= 12; m++) {
567 				if (!remember_ymd(year, m, idayofmonth))
568 					continue;
569 				remember(&remindex, yearp, monthp, dayp, edp,
570 				    year, m, idayofmonth, NULL);
571 			}
572 			continue;
573 		}
574 
575 		/* Every dayofweek of the year */
576 		if (lflags == (F_DAYOFWEEK | F_VARIABLE)) {
577 			dow = first_dayofweek_of_year(year);
578 			if (dow < 0)
579 				continue;
580 			d = (idayofweek - dow + 7) % 7 + 1;
581 			while (d <= 366) {
582 				if (remember_yd(year, d, &rm, &rd))
583 					remember(&remindex,
584 					    yearp, monthp, dayp, edp,
585 					    year, rm, rd, NULL);
586 				d += 7;
587 			}
588 			continue;
589 		}
590 
591 		/*
592 	         * Every so-manied dayofweek of every month of the year:
593 	         * Thu-3
594 	         */
595 		if (lflags == (F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) {
596 			offset = indextooffset(modifierindex);
597 
598 			for (m = 0; m <= 12; m++) {
599 	                        d = wdayom (idayofweek, offset, m, year);
600 				if (remember_ymd(year, m, d)) {
601 					remember(&remindex,
602 					    yearp, monthp, dayp, edp,
603 					    year, m, d, NULL);
604 					continue;
605 				}
606 			}
607 			continue;
608 		}
609 
610 		/*
611 	         * A certain dayofweek of a month
612 	         * Jan/Thu-3
613 	         */
614 		if (lflags ==
615 		    (F_MONTH | F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) {
616 			offset = indextooffset(modifierindex);
617 			dow = first_dayofweek_of_month(year, imonth);
618 			if (dow < 0)
619 				continue;
620 			d = (idayofweek - dow + 7) % 7 + 1;
621 
622 			if (offset > 0) {
623 				while (d <= yearinfo->monthdays[imonth]) {
624 					if (--offset == 0
625 					    && remember_ymd(year, imonth, d)) {
626 						remember(&remindex,
627 						    yearp, monthp, dayp, edp,
628 						    year, imonth, d, NULL);
629 						continue;
630 					}
631 					d += 7;
632 				}
633 				continue;
634 			}
635 			if (offset < 0) {
636 				while (d <= yearinfo->monthdays[imonth])
637 					d += 7;
638 				while (offset != 0) {
639 					offset++;
640 					d -= 7;
641 				}
642 				if (remember_ymd(year, imonth, d))
643 					remember(&remindex,
644 					    yearp, monthp, dayp, edp,
645 					    year, imonth, d, NULL);
646 				continue;
647 			}
648 			continue;
649 		}
650 
651 		/* Every dayofweek of the month */
652 		if (lflags == (F_DAYOFWEEK | F_MONTH | F_VARIABLE)) {
653 			dow = first_dayofweek_of_month(year, imonth);
654 			if (dow < 0)
655 				continue;
656 			d = (idayofweek - dow + 7) % 7 + 1;
657 			while (d <= yearinfo->monthdays[imonth]) {
658 				if (remember_ymd(year, imonth, d))
659 					remember(&remindex,
660 					    yearp, monthp, dayp, edp,
661 					    year, imonth, d, NULL);
662 				d += 7;
663 			}
664 			continue;
665 		}
666 
667 		/* Easter */
668 		if ((lflags & ~F_MODIFIEROFFSET) ==
669 		    (F_SPECIALDAY | F_VARIABLE | F_EASTER)) {
670 			offset = 0;
671 			if ((lflags & F_MODIFIEROFFSET) != 0)
672 				offset = parseoffset(modifieroffset);
673 			if (remember_yd(year, yearinfo->ieaster + offset,
674 	                        &rm, &rd))
675 				remember(&remindex, yearp, monthp, dayp, edp,
676 				    year, rm, rd, NULL);
677 			continue;
678 		}
679 
680 		/* Paskha */
681 		if ((lflags & ~F_MODIFIEROFFSET) ==
682 		    (F_SPECIALDAY | F_VARIABLE | F_PASKHA)) {
683 			offset = 0;
684 			if ((lflags & F_MODIFIEROFFSET) != 0)
685 				offset = parseoffset(modifieroffset);
686 			if (remember_yd(year, yearinfo->ipaskha + offset,
687 	                        &rm, &rd))
688 				remember(&remindex, yearp, monthp, dayp, edp,
689 				    year, rm, rd, NULL);
690 			continue;
691 		}
692 
693 		/* Chinese New Year */
694 		if ((lflags & ~F_MODIFIEROFFSET) ==
695 		    (F_SPECIALDAY | F_VARIABLE | F_CNY)) {
696 			offset = 0;
697 			if ((lflags & F_MODIFIEROFFSET) != 0)
698 				offset = parseoffset(modifieroffset);
699 			if (remember_yd(year, yearinfo->firstcnyday + offset,
700 	                        &rm, &rd))
701 				remember(&remindex, yearp, monthp, dayp, edp,
702 				    year, rm, rd, NULL);
703 			continue;
704 		}
705 
706 		/* FullMoon */
707 		if ((lflags & ~F_MODIFIEROFFSET) ==
708 		    (F_SPECIALDAY | F_VARIABLE | F_FULLMOON)) {
709 			int i;
710 
711 			offset = 0;
712 			if ((lflags & F_MODIFIEROFFSET) != 0)
713 				offset = parseoffset(modifieroffset);
714 			for (i = 0; yearinfo->ffullmoon[i] > 0; i++) {
715 				if (remember_yd(year,
716 	                                floor(yearinfo->ffullmoon[i]) + offset,
717 					&rm, &rd)) {
718 					ed = floattotime(
719 					    yearinfo->ffullmoon[i]);
720 					remember(&remindex,
721 					    yearp, monthp, dayp, edp,
722 					    year, rm, rd, ed);
723 				}
724 			}
725 			continue;
726 		}
727 
728 		/* NewMoon */
729 		if ((lflags & ~F_MODIFIEROFFSET) ==
730 		    (F_SPECIALDAY | F_VARIABLE | F_NEWMOON)) {
731 			int i;
732 
733 			offset = 0;
734 			if ((lflags & F_MODIFIEROFFSET) != 0)
735 				offset = parseoffset(modifieroffset);
736 			for (i = 0; yearinfo->ffullmoon[i] > 0; i++) {
737 				if (remember_yd(year,
738 					floor(yearinfo->fnewmoon[i]) + offset,
739 					&rm, &rd)) {
740 					ed = floattotime(yearinfo->fnewmoon[i]);
741 					remember(&remindex,
742 					    yearp, monthp, dayp, edp,
743 					    year, rm, rd, ed);
744 				}
745 			}
746 			continue;
747 		}
748 
749 		/* (Mar|Sep)Equinox */
750 		if ((lflags & ~F_MODIFIEROFFSET) ==
751 		    (F_SPECIALDAY | F_VARIABLE | F_MAREQUINOX)) {
752 			offset = 0;
753 			if ((lflags & F_MODIFIEROFFSET) != 0)
754 				offset = parseoffset(modifieroffset);
755 			if (remember_yd(year, yearinfo->equinoxdays[0] + offset,
756 				&rm, &rd)) {
757 				ed = floattotime(yearinfo->equinoxdays[0]);
758 				remember(&remindex, yearp, monthp, dayp, edp,
759 				    year, rm, rd, ed);
760 			}
761 			continue;
762 		}
763 		if ((lflags & ~F_MODIFIEROFFSET) ==
764 		    (F_SPECIALDAY | F_VARIABLE | F_SEPEQUINOX)) {
765 			offset = 0;
766 			if ((lflags & F_MODIFIEROFFSET) != 0)
767 				offset = parseoffset(modifieroffset);
768 			if (remember_yd(year, yearinfo->equinoxdays[1] + offset,
769 			    &rm, &rd)) {
770 				ed = floattotime(yearinfo->equinoxdays[1]);
771 				remember(&remindex, yearp, monthp, dayp, edp,
772 				    year, rm, rd, ed);
773 			}
774 			continue;
775 		}
776 
777 		/* (Jun|Dec)Solstice */
778 		if ((lflags & ~F_MODIFIEROFFSET) ==
779 		    (F_SPECIALDAY | F_VARIABLE | F_JUNSOLSTICE)) {
780 			offset = 0;
781 			if ((lflags & F_MODIFIEROFFSET) != 0)
782 				offset = parseoffset(modifieroffset);
783 			if (remember_yd(year,
784 				yearinfo->solsticedays[0] + offset, &rm, &rd)) {
785 				ed = floattotime(yearinfo->solsticedays[0]);
786 				remember(&remindex, yearp, monthp, dayp, edp,
787 				    year, rm, rd, ed);
788 			}
789 			continue;
790 		}
791 		if ((lflags & ~F_MODIFIEROFFSET) ==
792 		    (F_SPECIALDAY | F_VARIABLE | F_DECSOLSTICE)) {
793 			offset = 0;
794 			if ((lflags & F_MODIFIEROFFSET) != 0)
795 				offset = parseoffset(modifieroffset);
796 			if (remember_yd(year,
797 				yearinfo->solsticedays[1] + offset, &rm, &rd)) {
798 				ed = floattotime(yearinfo->solsticedays[1]);
799 				remember(&remindex, yearp, monthp, dayp, edp,
800 				    year, rm, rd, ed);
801 			}
802 			continue;
803 		}
804 
805 		if (debug) {
806 			printf("Unprocessed:\n");
807 			debug_determinestyle(2, date, lflags, month, imonth,
808 			    dayofmonth, idayofmonth, dayofweek, idayofweek,
809 			    modifieroffset, modifierindex, specialday, syear,
810 			    iyear);
811 		}
812 		retvalsign = -1;
813 	}
814 
815 	if (retvalsign == -1)
816 		return (-remindex - 1);
817 	else
818 		return (remindex);
819 }
820 
821 static char *
822 showflags(int flags)
823 {
824 	static char s[SLEN];
825 	s[0] = '\0';
826 
827 	if ((flags & F_YEAR) != 0)
828 		strlcat(s, "year ", SLEN);
829 	if ((flags & F_MONTH) != 0)
830 		strlcat(s, "month ", SLEN);
831 	if ((flags & F_DAYOFWEEK) != 0)
832 		strlcat(s, "dayofweek ", SLEN);
833 	if ((flags & F_DAYOFMONTH) != 0)
834 		strlcat(s, "dayofmonth ", SLEN);
835 	if ((flags & F_MODIFIERINDEX) != 0)
836 		strlcat(s, "modifierindex ", SLEN);
837 	if ((flags & F_MODIFIEROFFSET) != 0)
838 		strlcat(s, "modifieroffset ", SLEN);
839 	if ((flags & F_SPECIALDAY) != 0)
840 		strlcat(s, "specialday ", SLEN);
841 	if ((flags & F_ALLMONTH) != 0)
842 		strlcat(s, "allmonth ", SLEN);
843 	if ((flags & F_ALLDAY) != 0)
844 		strlcat(s, "allday ", SLEN);
845 	if ((flags & F_VARIABLE) != 0)
846 		strlcat(s, "variable ", SLEN);
847 	if ((flags & F_CNY) != 0)
848 		strlcat(s, "chinesenewyear ", SLEN);
849 	if ((flags & F_PASKHA) != 0)
850 		strlcat(s, "paskha ", SLEN);
851 	if ((flags & F_EASTER) != 0)
852 		strlcat(s, "easter ", SLEN);
853 	if ((flags & F_FULLMOON) != 0)
854 		strlcat(s, "fullmoon ", SLEN);
855 	if ((flags & F_NEWMOON) != 0)
856 		strlcat(s, "newmoon ", SLEN);
857 	if ((flags & F_MAREQUINOX) != 0)
858 		strlcat(s, "marequinox ", SLEN);
859 	if ((flags & F_SEPEQUINOX) != 0)
860 		strlcat(s, "sepequinox ", SLEN);
861 	if ((flags & F_JUNSOLSTICE) != 0)
862 		strlcat(s, "junsolstice ", SLEN);
863 	if ((flags & F_DECSOLSTICE) != 0)
864 		strlcat(s, "decsolstice ", SLEN);
865 
866 	return s;
867 }
868 
869 static const char *
870 getmonthname(int i)
871 {
872 	if (i <= 0 || i > 12)
873 		return ("");
874 	if (nmonths[i - 1].len != 0 && nmonths[i - 1].name != NULL)
875 		return (nmonths[i - 1].name);
876 	return (months[i - 1]);
877 }
878 
879 static int
880 checkmonth(char *s, size_t *len, size_t *offset, const char **month)
881 {
882 	struct fixs *n;
883 	int i;
884 
885 	for (i = 0; fnmonths[i].name != NULL; i++) {
886 		n = fnmonths + i;
887 		if (strncasecmp(s, n->name, n->len) == 0) {
888 			*len = n->len;
889 			*month = n->name;
890 			*offset = i + 1;
891 			return (1);
892 		}
893 	}
894 	for (i = 0; nmonths[i].name != NULL; i++) {
895 		n = nmonths + i;
896 		if (strncasecmp(s, n->name, n->len) == 0) {
897 			*len = n->len;
898 			*month = n->name;
899 			*offset = i + 1;
900 			return (1);
901 		}
902 	}
903 	for (i = 0; fmonths[i] != NULL; i++) {
904 		*len = strlen(fmonths[i]);
905 		if (strncasecmp(s, fmonths[i], *len) == 0) {
906 			*month = fmonths[i];
907 			*offset = i + 1;
908 			return (1);
909 		}
910 	}
911 	for (i = 0; months[i] != NULL; i++) {
912 		if (strncasecmp(s, months[i], 3) == 0) {
913 			*len = 3;
914 			*month = months[i];
915 			*offset = i + 1;
916 			return (1);
917 		}
918 	}
919 	return (0);
920 }
921 
922 static const char *
923 getdayofweekname(int i)
924 {
925 	if (ndays[i].len != 0 && ndays[i].name != NULL)
926 		return (ndays[i].name);
927 	return (days[i]);
928 }
929 
930 static int
931 checkdayofweek(char *s, size_t *len, size_t *offset, const char **dow)
932 {
933 	struct fixs *n;
934 	int i;
935 
936 	for (i = 0; fndays[i].name != NULL; i++) {
937 		n = fndays + i;
938 		if (strncasecmp(s, n->name, n->len) == 0) {
939 			*len = n->len;
940 			*dow = n->name;
941 			*offset = i;
942 			return (1);
943 		}
944 	}
945 	for (i = 0; ndays[i].name != NULL; i++) {
946 		n = ndays + i;
947 		if (strncasecmp(s, n->name, n->len) == 0) {
948 			*len = n->len;
949 			*dow = n->name;
950 			*offset = i;
951 			return (1);
952 		}
953 	}
954 	for (i = 0; fdays[i] != NULL; i++) {
955 		*len = strlen(fdays[i]);
956 		if (strncasecmp(s, fdays[i], *len) == 0) {
957 			*dow = fdays[i];
958 			*offset = i;
959 			return (1);
960 		}
961 	}
962 	for (i = 0; days[i] != NULL; i++) {
963 		if (strncasecmp(s, days[i], 3) == 0) {
964 			*len = 3;
965 			*dow = days[i];
966 			*offset = i;
967 			return (1);
968 		}
969 	}
970 	return (0);
971 }
972 
973 static int
974 isonlydigits(char *s, int nostar)
975 {
976 	int i;
977 	for (i = 0; s[i] != '\0'; i++) {
978 		if (nostar == 0 && s[i] == '*' && s[i + 1] == '\0')
979 			return 1;
980 		if (!isdigit((unsigned char)s[i]))
981 			return (0);
982 	}
983 	return (1);
984 }
985 
986 static int
987 indextooffset(char *s)
988 {
989 	int i;
990 	struct fixs *n;
991 	char *es;
992 
993 	if (s[0] == '+' || s[0] == '-') {
994 		i = strtol (s, &es, 10);
995 		if (*es != '\0')                      /* trailing junk */
996 			errx (1, "Invalid specifier format: %s\n", s);
997 		return (i);
998 	}
999 
1000 	for (i = 0; i < 6; i++) {
1001 		if (strcasecmp(s, sequences[i]) == 0) {
1002 			if (i == 5)
1003 				return (-1);
1004 			return (i + 1);
1005 		}
1006 	}
1007 	for (i = 0; i < 6; i++) {
1008 		n = nsequences + i;
1009 		if (n->len == 0)
1010 			continue;
1011 		if (strncasecmp(s, n->name, n->len) == 0) {
1012 			if (i == 5)
1013 				return (-1);
1014 			return (i + 1);
1015 		}
1016 	}
1017 	return (0);
1018 }
1019 
1020 static int
1021 parseoffset(char *s)
1022 {
1023 	return strtol(s, NULL, 10);
1024 }
1025 
1026 static char *
1027 floattotime(double f)
1028 {
1029 	static char buf[SLEN];
1030 	int hh, mm, ss, i;
1031 
1032 	f -= floor(f);
1033 	i = f * SECSPERDAY;
1034 
1035 	hh = i / SECSPERHOUR;
1036 	i %= SECSPERHOUR;
1037 	mm = i / SECSPERMINUTE;
1038 	i %= SECSPERMINUTE;
1039 	ss = i;
1040 
1041 	snprintf(buf, SLEN, "%02d:%02d:%02d", hh, mm, ss);
1042 	return (buf);
1043 }
1044 
1045 static char *
1046 floattoday(int year, double f)
1047 {
1048 	static char buf[SLEN];
1049 	int i, m, d, hh, mm, ss;
1050 	int *cumdays = cumdaytab[isleap(year)];
1051 
1052 	for (i = 0; 1 + cumdays[i] < f; i++)
1053 		;
1054 	m = --i;
1055 	d = floor(f - 1 - cumdays[i]);
1056 	f -= floor(f);
1057 	i = f * SECSPERDAY;
1058 
1059 	hh = i / SECSPERHOUR;
1060 	i %= SECSPERHOUR;
1061 	mm = i / SECSPERMINUTE;
1062 	i %= SECSPERMINUTE;
1063 	ss = i;
1064 
1065 	snprintf(buf, SLEN, "%02d-%02d %02d:%02d:%02d", m, d, hh, mm, ss);
1066 	return (buf);
1067 }
1068 
1069 void
1070 dodebug(char *what)
1071 {
1072 	int year;
1073 
1074 	printf("UTCOffset: %g\n", UTCOffset);
1075 	printf("eastlongitude: %d\n", EastLongitude);
1076 
1077 	if (strcmp(what, "moon") == 0) {
1078 		double ffullmoon[MAXMOONS], fnewmoon[MAXMOONS];
1079 		int i;
1080 
1081 		for (year = year1; year <= year2; year++) {
1082 			fpom(year, UTCOffset, ffullmoon, fnewmoon);
1083 			printf("Full moon %d:\t", year);
1084 			for (i = 0; ffullmoon[i] >= 0; i++) {
1085 				printf("%g (%s) ", ffullmoon[i],
1086 				    floattoday(year, ffullmoon[i]));
1087 			}
1088 			printf("\nNew moon %d:\t", year);
1089 			for (i = 0; fnewmoon[i] >= 0; i++) {
1090 				printf("%g (%s) ", fnewmoon[i],
1091 				    floattoday(year, fnewmoon[i]));
1092 			}
1093 			printf("\n");
1094 
1095 		}
1096 
1097 		return;
1098 	}
1099 
1100 	if (strcmp(what, "sun") == 0) {
1101 		double equinoxdays[2], solsticedays[2];
1102 		for (year = year1; year <= year2; year++) {
1103 			printf("Sun in %d:\n", year);
1104 			fequinoxsolstice(year, UTCOffset, equinoxdays,
1105 			    solsticedays);
1106 			printf("e[0] - %g (%s)\n",
1107 			    equinoxdays[0],
1108 			    floattoday(year, equinoxdays[0]));
1109 			printf("e[1] - %g (%s)\n",
1110 			    equinoxdays[1],
1111 			    floattoday(year, equinoxdays[1]));
1112 			printf("s[0] - %g (%s)\n",
1113 			    solsticedays[0],
1114 			    floattoday(year, solsticedays[0]));
1115 			printf("s[1] - %g (%s)\n",
1116 			    solsticedays[1],
1117 			    floattoday(year, solsticedays[1]));
1118 		}
1119 		return;
1120 	}
1121 }
1122