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