svn commit: r200870 - user/edwin/calendar

Edwin Groothuis edwin at FreeBSD.org
Tue Dec 22 20:37:40 UTC 2009


Author: edwin
Date: Tue Dec 22 20:37:40 2009
New Revision: 200870
URL: http://svn.freebsd.org/changeset/base/200870

Log:
  Split the current (and partly new) code into modules with functions
  with similar functionalities.

Added:
  user/edwin/calendar/events.c
  user/edwin/calendar/locale.c
  user/edwin/calendar/parsedata.c
Modified:
  user/edwin/calendar/Makefile
  user/edwin/calendar/calendar.c
  user/edwin/calendar/calendar.h
  user/edwin/calendar/day.c
  user/edwin/calendar/io.c

Modified: user/edwin/calendar/Makefile
==============================================================================
--- user/edwin/calendar/Makefile	Tue Dec 22 20:33:27 2009	(r200869)
+++ user/edwin/calendar/Makefile	Tue Dec 22 20:37:40 2009	(r200870)
@@ -4,7 +4,7 @@
 CFLAGS=	 -pipe  -g -std=gnu99 -fstack-protector   -Wall
 
 PROG=	calendar
-SRCS=   calendar.c io.c day.c ostern.c paskha.c
+SRCS=   calendar.c locale.c events.c parsedata.c io.c day.c ostern.c paskha.c
 INTER=          de_AT.ISO_8859-15 de_DE.ISO8859-1 fr_FR.ISO8859-1 \
 		hr_HR.ISO8859-2 hu_HU.ISO8859-2 ru_RU.KOI8-R uk_UA.KOI8-U
 DE_LINKS=       de_DE.ISO8859-15

Modified: user/edwin/calendar/calendar.c
==============================================================================
--- user/edwin/calendar/calendar.c	Tue Dec 22 20:33:27 2009	(r200869)
+++ user/edwin/calendar/calendar.c	Tue Dec 22 20:37:40 2009	(r200870)
@@ -153,6 +153,6 @@ usage(void)
 	fprintf(stderr, "%s\n%s\n",
 	    "usage: calendar [-a] [-A days] [-B days] [-F friday] "
 	    "[-f calendarfile]",
-	    "                [-t dd[.mm[.year]]] [-W days]");
+	    "                [-d] [-t dd[.mm[.year]]] [-W days]");
 	exit(1);
 }

Modified: user/edwin/calendar/calendar.h
==============================================================================
--- user/edwin/calendar/calendar.h	Tue Dec 22 20:33:27 2009	(r200869)
+++ user/edwin/calendar/calendar.h	Tue Dec 22 20:37:40 2009	(r200870)
@@ -38,6 +38,8 @@
 
 #define	SECSPERDAY	24 * 60 * 60
 
+/* Not yet categorized */
+
 extern struct passwd *pw;
 extern int doall;
 extern struct iovec header[];
@@ -48,28 +50,9 @@ extern int *cumdays;
 extern int yrdays;
 extern struct fixs neaster, npaskha, ncny;
 
-void	cal(void);
-void	closecal(FILE *);
-int	checkdayofweek(char *, int *len, int *offset, char **dow);
-char *	getdayofweekname(int);
-int	checkmonth(char *, int *len, int *offset, char **month);
-char *	getmonthname(int);
-int	getday(char *);
-int	getdayvar(char *);
-int	getfield(char *, int *);
-int	getmonth(char *);
-int	geteaster(char *, int);
-int	getpaskha(char *, int);
-int	easter(int);
-int	parsedaymonth(char *, int *, int *, int *);
-FILE	*opencal(void);
-void	settimes(time_t,int, int);
-time_t	Mktime(char *);
-void	setnnames(void);
-
 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
 
-/* Flags to determine the returned values by determinestyle() */
+/* Flags to determine the returned values by determinestyle() in parsedata.c */
 #define	F_NONE			0x000
 #define	F_MONTH			0x001
 #define	F_DAYOFWEEK		0x002
@@ -93,7 +76,57 @@ extern int	f_dayAfter;	/* days after cur
 extern int	f_dayBefore;	/* days before current date */
 extern int	Friday;		/* day before weekend */
 
+/* events.c */
+/*
+ * Event sorting related functions:
+ * - Use event_add() to create a new event
+ * - Use event_continue() to add more text to the last added event
+ * - Use event_print_all() to display them in time chronological order
+ */
+struct event *event_add(struct event *, int, int, char *, int, char *);
+void	event_continue(struct event *events, char *txt);
+void	event_print_all(FILE *fp, struct event *events);
+struct event {
+	int	month;
+	int	day;
+	int	var;
+	char	*date;
+	char	*text;
+	struct event *next;
+};
+
+/* locale.c */
+
 struct fixs {
 	char	*name;
 	int	len;
 };
+
+extern struct fixs fndays[8];		/* full national days names */
+extern struct fixs ndays[8];		/* short national days names */
+extern struct fixs fnmonths[13];	/* full national months names */
+extern struct fixs nmonths[13];		/* short national month names */
+extern const char *months[];
+extern const char *fmonths[];
+extern const char *days[];
+extern const char *fdays[];
+
+void	setnnames(void);
+
+/* day.c */
+void	settimes(time_t,int, int);
+time_t	Mktime(char *);
+
+/* parsedata.c */
+int	parsedaymonth(char *, int *, int *, int *);
+
+/* io.c */
+void	cal(void);
+void	closecal(FILE *);
+FILE	*opencal(void);
+
+/* ostern.c / pashka.c */
+int	geteaster(char *, int);
+int	getpaskha(char *, int);
+int	easter(int);
+

Modified: user/edwin/calendar/day.c
==============================================================================
--- user/edwin/calendar/day.c	Tue Dec 22 20:33:27 2009	(r200869)
+++ user/edwin/calendar/day.c	Tue Dec 22 20:37:40 2009	(r200870)
@@ -34,9 +34,6 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
-#include <sys/types.h>
-#include <sys/uio.h>
-#include <ctype.h>
 #include <err.h>
 #include <locale.h>
 #include <stdio.h>
@@ -59,96 +56,6 @@ int	daytab[][14] = {
 	{0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
 };
 
-static char const *fdays[] = {
-	"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
-	"Saturday", NULL,
-};
-
-static char const *days[] = {
-	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL,
-};
-
-static const char *fmonths[] = {
-	"January", "February", "March", "April", "May", "June", "Juli",
-	"August", "September", "October", "November", "December", NULL,
-};
-
-static const char *months[] = {
-	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
-	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL,
-};
-
-static struct fixs fndays[8];		/* full national days names */
-static struct fixs ndays[8];		/* short national days names */
-
-static struct fixs fnmonths[13];	/* full national months names */
-static struct fixs nmonths[13];		/* short national month names */
-
-static char *showflags(int flags);
-static int isonlydigits(char *s, int star);
-
-
-void
-setnnames(void)
-{
-	char buf[80];
-	int i, l;
-	struct tm tm;
-
-	for (i = 0; i < 7; i++) {
-		tm.tm_wday = i;
-		strftime(buf, sizeof(buf), "%a", &tm);
-		for (l = strlen(buf);
-		     l > 0 && isspace((unsigned char)buf[l - 1]);
-		     l--)
-			;
-		buf[l] = '\0';
-		if (ndays[i].name != NULL)
-			free(ndays[i].name);
-		if ((ndays[i].name = strdup(buf)) == NULL)
-			errx(1, "cannot allocate memory");
-		ndays[i].len = strlen(buf);
-
-		strftime(buf, sizeof(buf), "%A", &tm);
-		for (l = strlen(buf);
-		     l > 0 && isspace((unsigned char)buf[l - 1]);
-		     l--)
-			;
-		buf[l] = '\0';
-		if (fndays[i].name != NULL)
-			free(fndays[i].name);
-		if ((fndays[i].name = strdup(buf)) == NULL)
-			errx(1, "cannot allocate memory");
-		fndays[i].len = strlen(buf);
-	}
-
-	for (i = 0; i < 12; i++) {
-		tm.tm_mon = i;
-		strftime(buf, sizeof(buf), "%b", &tm);
-		for (l = strlen(buf);
-		     l > 0 && isspace((unsigned char)buf[l - 1]);
-		     l--)
-			;
-		buf[l] = '\0';
-		if (nmonths[i].name != NULL)
-			free(nmonths[i].name);
-		if ((nmonths[i].name = strdup(buf)) == NULL)
-			errx(1, "cannot allocate memory");
-		nmonths[i].len = strlen(buf);
-
-		strftime(buf, sizeof(buf), "%B", &tm);
-		for (l = strlen(buf);
-		     l > 0 && isspace((unsigned char)buf[l - 1]);
-		     l--)
-			;
-		buf[l] = '\0';
-		if (fnmonths[i].name != NULL)
-			free(fnmonths[i].name);
-		if ((fnmonths[i].name = strdup(buf)) == NULL)
-			errx(1, "cannot allocate memory");
-		fnmonths[i].len = strlen(buf);
-	}
-}
 
 void
 settimes(time_t now, int before, int after)
@@ -226,599 +133,7 @@ Mktime(char *dp)
 	return (mktime(&tm));
 }
 
-/*
- * Expected styles:
- *
- * Date			::=	Month . ' ' . DayOfMonth |
- *				Month . ' ' . DayOfWeek . ModifierIndex |
- *				Month . '/' . DayOfMonth |
- *				Month . '/' . DayOfWeek . ModifierIndex |
- *				DayOfMonth . ' ' . Month |
- *				DayOfMonth . '/' . Month |
- *				DayOfWeek . ModifierIndex . ' ' .Month |
- *				DayOfWeek . ModifierIndex . '/' .Month |
- *				DayOfWeek . ModifierIndex |
- *				SpecialDay . ModifierOffset
- *
- * Month		::=	MonthName | MonthNumber | '*'
- * MonthNumber		::=	'0' ... '9' | '00' ... '09' | '10' ... '12'
- * MonthName		::=	MonthNameShort | MonthNameLong
- * MonthNameLong	::=	'January' ... 'December'
- * MonthNameShort	::=	'Jan' ... 'Dec' | 'Jan.' ... 'Dec.'
- *
- * DayOfWeek		::=	DayOfWeekShort | DayOfWeekLong
- * DayOfWeekShort	::=	'Mon' .. 'Sun'
- * DayOfWeekLong	::=	'Monday' .. 'Sunday'
- * DayOfMonth		::=	'0' ... '9' | '00' ... '09' | '10' ... '29' |
- *				'30' ... '31' | '*'
- *
- * ModifierOffset	::=	'' | '+' . ModifierNumber | '-' . ModifierNumber
- * ModifierNumber	::=	'0' ... '9' | '00' ... '99' | '000' ... '299' |
- *				'300' ... '359' | '360' ... '365'
- * ModifierIndex	::=	'Second' | 'Third' | 'Fourth' | 'Fifth' |
- *				'First' | 'Last'
- * 
- * SpecialDay		::=	'Easter' | 'Pashka' | 'ChineseNewYear'
- *
- */
-int
-determinestyle(char *date, int *flags,
-    char *month, int *imonth, char *dayofmonth, int *idayofmonth,
-    char *dayofweek, int *idayofweek, char *modifieroffset,
-    char *modifierindex, char *specialday)
-{
-	char *p, *dow, *pmonth, *p1, *p2;
-	char pold;
-	int len, offset;
-
-	*flags = F_NONE;
-	*month = '\0';
-	*imonth = 0;
-	*dayofmonth = '\0';
-	*idayofmonth = 0;
-	*dayofweek = '\0';
-	*idayofweek = 0;
-	*modifieroffset = '\0';
-	*modifierindex = '\0';
-	*specialday = '\0';
-
-#define CHECKSPECIAL(s1, s2, lens2, type)				\
-	if (s2 != NULL && strncmp(s1, s2, lens2) == 0) {		\
-		*flags |= F_SPECIALDAY;					\
-		*flags |= type;						\
-		*flags |= F_VARIABLE;					\
-		if (strlen(s1) == lens2) {				\
-			strcpy(specialday, s1);				\
-			return (1);					\
-		}							\
-		strncpy(specialday, s1, lens2);				\
-		specialday[lens2] = '\0';				\
-		strcpy(modifieroffset, s1 + lens2);			\
-		*flags |= F_MODIFIEROFFSET;				\
-		return (1);						\
-	}
-
-	if ((p = strchr(date, ' ')) == NULL) {
-		if ((p = strchr(date, '/')) == NULL) {
-			CHECKSPECIAL(date, STRING_CNY, strlen(STRING_CNY),
-			    F_CNY);
-			CHECKSPECIAL(date, ncny.name, ncny.len, F_CNY);
-			CHECKSPECIAL(date, STRING_PASKHA,
-			    strlen(STRING_PASKHA), F_PASKHA);
-			CHECKSPECIAL(date, npaskha.name, npaskha.len, F_PASKHA);
-			CHECKSPECIAL(date, STRING_EASTER,
-			    strlen(STRING_EASTER), F_EASTER);
-			CHECKSPECIAL(date, neaster.name, neaster.len, F_EASTER);
-			if (checkdayofweek(date, &len, &offset, &dow) != 0) {
-				*flags |= F_DAYOFWEEK;
-				*flags |= F_VARIABLE;
-				*idayofweek = offset;
-				if (strlen(date) == len) {
-					strcpy(dayofweek, date);
-					return (1);
-				}
-				strncpy(dayofweek, date, len);
-				dayofweek[len] = '\0';
-				strcpy(modifierindex, date + len);
-				*flags |= F_MODIFIERINDEX;
-				return (1);
-			}
-			if (isonlydigits(date, 1)) {
-				/* Assume month number only */
-				*flags |= F_MONTH;
-				*imonth = (int)strtol(date, (char **)NULL, 10);
-				strcpy(month, getmonthname(*imonth));
-				return(1);
-			}
-			return (0);
-		}
-	}
-
-	/*
-	 * After this, leave by goto-ing to "allfine" or "fail" to restore the
-	 * original data in `date'.
-	 */
-	pold = *p;
-	*p = 0;
-	p1 = date;
-	p2 = p + 1;
-	/* Now p2 points to the next field and p1 to the first field */
-
-	/* Check if there is a month-string in the date */
-	if ((checkmonth(p1, &len, &offset, &pmonth) != 0)
-	 || (checkmonth(p2, &len, &offset, &pmonth) != 0 && (p2 = p1))) {
-		/* p2 is the non-month part */
-		*flags |= F_MONTH;
-		*imonth = offset;
-
-		strcpy(month, getmonthname(offset));
-		if (isonlydigits(p2, 1)) {
-			strcpy(dayofmonth, p2);
-			*idayofmonth = (int)strtol(p2, (char **)NULL, 10);
-			*flags |= F_DAYOFMONTH;
-			goto allfine;
-		}
-		if (strcmp(p2, "*") == 0) {
-			*flags |= F_ALLDAY;
-			goto allfine;
-		}
-
-		if (checkdayofweek(p2, &len, &offset, &dow) != 0) {
-			*flags |= F_DAYOFWEEK;
-			*flags |= F_VARIABLE;
-			*idayofweek = offset;
-			strcpy(dayofweek, getdayofweekname(offset));
-			if (strlen(p2) == len)
-				goto allfine;
-			strcpy(modifierindex, p2 + len);
-			*flags |= F_MODIFIERINDEX;
-			goto allfine;
-		}
-
-		goto fail;
-	}
-
-	/* Check if there is an every-day or every-month in the string */
-	if ((strcmp(p1, "*") == 0 && isonlydigits(p2, 1))
-	 || (strcmp(p2, "*") == 0 && isonlydigits(p1, 1) && (p2 = p1))) {
-		int d;
-
-		*flags |= F_ALLMONTH;
-		*flags |= F_DAYOFMONTH;
-		d = (int)strtol(p2, (char **)NULL, 10);
-		*idayofmonth = d;
-		sprintf(dayofmonth, "%d", d);
-		goto allfine;
-	}
-
-	/* Month as a number, then a weekday */
-	if (isonlydigits(p1, 1)
-	 && checkdayofweek(p2, &len, &offset, &dow) != 0) {
-		int d;
-
-		*flags |= F_MONTH;
-		*flags |= F_DAYOFWEEK;
-		*flags |= F_VARIABLE;
-
-		*idayofweek = offset;
-		d = (int)strtol(p1, (char **)NULL, 10);
-		*imonth = d;
-		strcpy(month, getmonthname(d));
-
-		strcpy(dayofweek, getdayofweekname(offset));
-		if (strlen(p2) == len)
-			goto allfine;
-		strcpy(modifierindex, p2 + len);
-		*flags |= F_MODIFIERINDEX;
-		goto allfine;
-	}
-
-	/* If both the month and date are specified as numbers */
-	if (isonlydigits(p1, 1) && isonlydigits(p2, 0)) {
-		/* Now who wants to be this ambigious? :-( */
-		int m, d;
-
-		if (strchr(p2, '*') != NULL)
-			*flags |= F_VARIABLE;
-
-		m = (int)strtol(p1, (char **)NULL, 10);
-		d = (int)strtol(p2, (char **)NULL, 10);
-
-		*flags |= F_MONTH;
-		*flags |= F_DAYOFMONTH;
-
-		if (m > 12) {
-			*imonth = d;
-			*idayofmonth = m;
-			strcpy(month, getmonthname(d));
-			sprintf(dayofmonth, "%d", m);
-		} else {
-			*imonth = m;
-			*idayofmonth = d;
-			strcpy(month, getmonthname(m));
-			sprintf(dayofmonth, "%d", d);
-		}
-		goto allfine;
-	}
-
-	/* FALLTHROUGH */
-fail:
-	*p = pold;
-	return (0);
-allfine:
-	*p = pold;
-	return (1);
-	
-}
-
-/*
- * Possible date formats include any combination of:
- *	3-charmonth			(January, Jan, Jan)
- *	3-charweekday			(Friday, Monday, mon.)
- *	numeric month or day		(1, 2, 04)
- *
- * Any character may separate them, or they may not be separated.  Any line,
- * following a line that is matched, that starts with "whitespace", is shown
- * along with the matched line.
- */
-int
-parsedaymonth(char *date, int *monthp, int *dayp, int *flags)
-{
-	char month[100], dayofmonth[100], dayofweek[100], modifieroffset[100];
-	char modifierindex[100], specialday[100];
-	int idayofweek, imonth, idayofmonth;
-
-	/*
-	 * CONVENTION
-	 *
-	 * Month:     1-12
-	 * Monthname: Jan .. Dec
-	 * Day:       1-31
-	 * Weekday:   Mon .. Sun
-	 *
-	 */
-
-	*flags = 0;
-
-	if (debug)
-		printf("-------\ndate: |%s|\n", date);
-	if (determinestyle(date, flags, month, &imonth, dayofmonth,
-	    &idayofmonth, dayofweek, &idayofweek, modifieroffset,
-	    modifierindex, specialday) == 0) {
-		if (debug)
-			printf("Failed!\n");
-		return (0);
-	}
-
-	if (debug) {
-		printf("flags: %x - %s\n", *flags, showflags(*flags));
-		if (modifieroffset[0] != '\0')
-			printf("modifieroffset: |%s|\n", modifieroffset);
-		if (modifierindex[0] != '\0')
-			printf("modifierindex: |%s|\n", modifierindex);
-		if (month[0] != '\0')
-			printf("month: |%s| (%d)\n", month, imonth);
-		if (dayofmonth[0] != '\0')
-			printf("dayofmonth: |%s| (%d)\n", dayofmonth,
-			    idayofmonth);
-		if (dayofweek[0] != '\0')
-			printf("dayofweek: |%s| (%d)\n", dayofweek, idayofweek);
-		if (specialday[0] != '\0')
-			printf("specialday: |%s|\n", specialday);
-	}
-
-	if ((*flags & !F_VARIABLE) == (F_MONTH | F_DAYOFMONTH)) {
-	}
-
-	return (0);
-
-#ifdef NOTDEF
-	if (!(v1 = getfield(date, &flags)))
-		return (0);
-
-	/* Easter or Easter depending days */
-	if (flags & F_EASTER)
-		day = v1 - 1; /* days since January 1 [0-365] */
-
-	 /*
-	  * 1. {Weekday,Day} XYZ ...
-	  *
-	  *    where Day is > 12
-	  */
-	else if (flags & F_ISDAY || v1 > 12) {
-
-		/* found a day; day: 1-31 or weekday: 1-7 */
-		day = v1;
-
-		/* {Day,Weekday} {Month,Monthname} ... */
-		/* if no recognizable month, assume just a day alone
-		 * in other words, find month or use current month */
-		if (!(month = getfield(endp, &flags)))
-			month = tp->tm_mon + 1;
-	}
-
-	/* 2. {Monthname} XYZ ... */
-	else if (flags & F_ISMONTH) {
-		month = v1;
-
-		/* Monthname {day,weekday} */
-		/* if no recognizable day, assume the first day in month */
-		if (!(day = getfield(endp, &flags)))
-			day = 1;
-	}
-
-	/* Hm ... */
-	else {
-		v2 = getfield(endp, &flags);
-
-		/*
-		 * {Day} {Monthname} ...
-		 * where Day <= 12
-		 */
-		if (flags & F_ISMONTH) {
-			day = v1;
-			month = v2;
-			*varp = 0;
-		}
-
-		/* {Month} {Weekday,Day} ...  */
-		else {
-			/* F_ISDAY set, v2 > 12, or no way to tell */
-			month = v1;
-			/* if no recognizable day, assume the first */
-			day = v2 ? v2 : 1;
-			*varp = 0;
-		}
-	}
-
-	/* convert Weekday into *next*  Day,
-	 * e.g.: 'Sunday' -> 22
-	 *       'SundayLast' -> ??
-	 */
-	if (flags & F_ISDAY) {
-#ifdef DEBUG
-		fprintf(stderr, "\nday: %d %s month %d\n", day, endp, month);
-#endif
-
-		*varp = 1;
-		/* variable weekday, SundayLast, MondayFirst ... */
-		if (day < 0 || day >= 10) {
-
-			/* negative offset; last, -4 .. -1 */
-			if (day < 0) {
-				v1 = day / 10 - 1;	/* offset -4 ... -1 */
-				day = 10 + (day % 10);	/* day 1 ... 7 */
-
-				/* day, eg '22nd' */
-				v2 = tp->tm_mday +
-				    (((day - 1) - tp->tm_wday + 7) % 7);
-
-				/* (month length - day)	/ 7 + 1 */
-				if (cumdays[month + 1] - cumdays[month] >= v2
-				    && ((int)((cumdays[month + 1] -
-				    cumdays[month] - v2) / 7) + 1) == -v1)
-					day = v2;	/* bingo ! */
-
-				/* set to yesterday */
-				else {
-					day = tp->tm_mday - 1;
-					if (day == 0)
-						return (0);
-				}
-			}
-
-			/* first, second ... +1 ... +5 */
-			else {
-				/* offset: +1 (first Sunday) ... */
-				v1 = day / 10;
-				day = day % 10;
-
-				/* day, eg '22th' */
-				v2 = tp->tm_mday +
-				    (((day - 1) - tp->tm_wday + 7) % 7);
-
-				/* Hurrah! matched */
-				if (((v2 - 1 + 7) / 7) == v1 )
-					day = v2;
-
-				else {
-					/* set to yesterday */
-					day = tp->tm_mday - 1;
-					if (day == 0)
-						return (0);
-				}
-			}
-		} else {
-			/* wired */
-			day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
-			*varp = 1;
-		}
-	}
-
-	if (!(flags & F_EASTER)) {
-		if (day + cumdays[month] > cumdays[month + 1]) {
-			/* off end of month, adjust */
-			day -= (cumdays[month + 1] - cumdays[month]);
-			/* next year */
-			if (++month > 12)
-				month = 1;
-		}
-		*monthp = month;
-		*dayp = day;
-		day = cumdays[month] + day;
-	} else {
-		for (v1 = 0; day > cumdays[v1]; v1++)
-			;
-		*monthp = v1 - 1;
-		*dayp = day - cumdays[v1 - 1];
-		*varp = 1;
-	}
-
-#ifdef DEBUG
-	fprintf(stderr, "day2: day %d(%d-%d) yday %d\n",
-	    *dayp, day, cumdays[month], tp->tm_yday);
-#endif
-
-	/* When days before or days after is specified */
-	/* no year rollover */
-	if (day >= tp->tm_yday - f_dayBefore &&
-	    day <= tp->tm_yday + f_dayAfter)
-		return (1);
-
-	/* next year */
-	if (tp->tm_yday + f_dayAfter >= yrdays) {
-		int end = tp->tm_yday + f_dayAfter - yrdays;
-		if (day <= end)
-			return (1);
-	}
-
-	/* previous year */
-	if (tp->tm_yday - f_dayBefore < 0) {
-		int before = yrdays + (tp->tm_yday - f_dayBefore);
-		if (day >= before)
-			return (1);
-	}
-#endif
-	return (0);
-}
-
-
-static char *
-showflags(int flags)
-{
-	static char s[1000];
-	s[0] = '\0';
-
-	if ((flags & F_MONTH) != 0)
-		strcat(s, "month ");
-	if ((flags & F_DAYOFWEEK) != 0)
-		strcat(s, "dayofweek ");
-	if ((flags & F_DAYOFMONTH) != 0)
-		strcat(s, "dayofmonth ");
-	if ((flags & F_MODIFIERINDEX) != 0)
-		strcat(s, "modifierindex ");
-	if ((flags & F_MODIFIEROFFSET) != 0)
-		strcat(s, "modifieroffset ");
-	if ((flags & F_SPECIALDAY) != 0)
-		strcat(s, "specialday ");
-	if ((flags & F_ALLMONTH) != 0)
-		strcat(s, "allmonth ");
-	if ((flags & F_ALLDAY) != 0)
-		strcat(s, "allday ");
-	if ((flags & F_VARIABLE) != 0)
-		strcat(s, "variable ");
-	if ((flags & F_CNY) != 0)
-		strcat(s, "chinesenewyear ");
-	if ((flags & F_PASKHA) != 0)
-		strcat(s, "paskha ");
-	if ((flags & F_EASTER) != 0)
-		strcat(s, "easter ");
-
-	return s;
-}
-
-char *
-getmonthname(int i)
-{
-	if (nmonths[i - 1].len != 0 && nmonths[i - 1].name != NULL)
-		return (nmonths[i - 1].name);
-	return ((char *)months[i - 1]);
-}
-
-int
-checkmonth(char *s, int *len, int *offset, char **month)
-{
-	struct fixs *n;
-	int i;
 
-	for (i = 0; fnmonths[i].name != NULL; i++) {
-		n = fnmonths + i;
-		if (strncasecmp(s, n->name, n->len) == 0) {
-			*len = n->len;
-			*month = n->name;
-			*offset = i + 1;
-			return (1);
-		}
-	}
-	for (i = 0; nmonths[i].name != NULL; i++) {
-		n = nmonths + i;
-		if (strncasecmp(s, n->name, n->len) == 0) {
-			*len = n->len;
-			*month = n->name;
-			*offset = i + 1;
-			return (1);
-		}
-	}
-	for (i = 0; fmonths[i] != NULL; i++) {
-		*len = strlen(fmonths[i]);
-		if (strncasecmp(s, fmonths[i], *len) == 0) {
-			*month = (char *)fmonths[i];
-			*offset = i + 1;
-			return (1);
-		}
-	}
-	for (i = 0; months[i] != NULL; i++) {
-		if (strncasecmp(s, months[i], 3) == 0) {
-			*len = 3;
-			*month = (char *)months[i];
-			*offset = i + 1;
-			return (1);
-		}
-	}
-	return (0);
-}
-
-
-char *
-getdayofweekname(int i)
-{
-	if (ndays[i].len != 0 && ndays[i].name != NULL)
-		return (ndays[i].name);
-	return ((char *)days[i]);
-}
-
-int
-checkdayofweek(char *s, int *len, int *offset, char **dow)
-{
-	struct fixs *n;
-	int i;
-
-	for (i = 0; fndays[i].name != NULL; i++) {
-		n = fndays + i;
-		if (strncasecmp(s, n->name, n->len) == 0) {
-			*len = n->len;
-			*dow = n->name;
-			*offset = i;
-			return (1);
-		}
-	}
-	for (i = 0; ndays[i].name != NULL; i++) {
-		n = ndays + i;
-		if (strncasecmp(s, n->name, n->len) == 0) {
-			*len = n->len;
-			*dow = n->name;
-			*offset = i;
-			return (1);
-		}
-	}
-	for (i = 0; fdays[i] != NULL; i++) {
-		*len = strlen(fdays[i]);
-		if (strncasecmp(s, fdays[i], *len) == 0) {
-			*dow = (char *)fdays[i];
-			*offset = i;
-			return (1);
-		}
-	}
-	for (i = 0; days[i] != NULL; i++) {
-		if (strncasecmp(s, days[i], 3) == 0) {
-			*len = 3;
-			*dow = (char *)days[i];
-			*offset = i;
-			return (1);
-		}
-	}
-	return (0);
-}
 
 /* return offset for variable weekdays
  * -1 -> last weekday in month
@@ -863,17 +178,3 @@ getdayvar(char *s)
 	return (0);
 }
 
-
-static int
-isonlydigits(char *s, int nostar)
-{
-	int i;
-	for (i = 0; s[i] != '\0'; i++) {
-		if (nostar == 0 && s[i] == '*' && s[i + 1] == '\0')
-			return 1;
-		if (!isdigit(s[i]))
-			return (0);
-	}
-	return (1);
-}
-

Added: user/edwin/calendar/events.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/edwin/calendar/events.c	Tue Dec 22 20:37:40 2009	(r200870)
@@ -0,0 +1,160 @@
+/*
+ * Copyright (c) 1989, 1993, 1994
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by the University of
+ *	California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: user/edwin/calendar/io.c 200813 2009-12-21 21:17:59Z edwin $");
+
+#include <sys/time.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "pathnames.h"
+#include "calendar.h"
+
+struct event *
+event_add(struct event *events, int month, int day,
+    char *date, int var, char *txt)
+{
+	struct event *e;
+
+	/*
+	 * Creating a new event:
+	 * - Create a new event
+	 * - Copy the machine readable day and month
+	 * - Copy the human readable and language specific date
+	 * - Copy the text of the event
+	 */
+	e = (struct event *)calloc(1, sizeof(struct event));
+	if (e == NULL)
+		errx(1, "event_add: cannot allocate memory");
+	e->month = month;
+	e->day = day;
+	e->var = var;
+	e->date = strdup(date);
+	if (e->date == NULL)
+		errx(1, "event_add: cannot allocate memory");
+	e->text = strdup(txt);
+	if (e->text == NULL)
+		errx(1, "event_add: cannot allocate memory");
+	e->next = events;
+
+	return e;
+}
+
+void
+event_continue(struct event *e, char *txt)
+{
+	char *text;
+
+	/*
+	 * Adding text to the event:
+	 * - Save a copy of the old text (unknown length, so strdup())
+	 * - Allocate enough space for old text + \n + new text + 0
+	 * - Store the old text + \n + new text
+	 * - Destroy the saved copy.
+	 */
+	text = strdup(e->text);
+	if (text == NULL)
+		errx(1, "event_continue: cannot allocate memory");
+
+	free(e->text);
+	e->text = (char *)malloc(strlen(text) + strlen(txt) + 3);
+	if (e->text == NULL)
+		errx(1, "event_continue: cannot allocate memory");
+	strcpy(e->text, text);
+	strcat(e->text, "\n");
+	strcat(e->text, txt);
+	free(text);
+
+	return;
+}
+
+void
+event_print_all(FILE *fp, struct event *events)
+{
+	struct event *e, *e_next;
+	int daycounter;
+	int day, month;
+
+	/*
+	 * Print all events:
+	 * - We know the number of days to be counted (f_dayAfter + f_dayBefore)
+	 * - We know the current day of the year ("now" - f_dayBefore + counter)
+	 * - We know the number of days in the year (yrdays, set in settime())
+	 * - So we know the date on which the current daycounter is on the
+	 *   calendar in days and months.
+	 * - Go through the list of events, and print all matching dates
+	 */
+	for (daycounter = 0; daycounter <= f_dayAfter + f_dayBefore;
+	    daycounter++) {
+		day = tp1.tm_yday - f_dayBefore + daycounter;
+		if (day < 0)
+			day += yrdays;
+		if (day >= yrdays)
+			day -= yrdays;
+
+		/*
+		 * When we know the day of the year, we can determine the day
+		 * of the month and the month.
+		 */
+		month = 1;
+		while (month <= 12) {
+			if (day <= cumdays[month])
+				break;
+			month++;
+		}
+		month--;
+		day -= cumdays[month];
+
+#ifdef DEBUG
+		fprintf(stderr, "event_print_allmonth: %d, day: %d\n",
+		    month, day);
+#endif
+
+		/*
+		 * Go through all events and print the text of the matching
+		 * dates
+		 */
+		for (e = events; e != NULL; e = e_next) {

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***


More information about the svn-src-user mailing list