[Midnightbsd-cvs] src [11587] trunk/usr.bin/calendar: sync with freebsd

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Sun Jul 8 11:41:59 EDT 2018


Revision: 11587
          http://svnweb.midnightbsd.org/src/?rev=11587
Author:   laffer1
Date:     2018-07-08 11:41:59 -0400 (Sun, 08 Jul 2018)
Log Message:
-----------
sync with freebsd

Modified Paths:
--------------
    trunk/usr.bin/calendar/Makefile
    trunk/usr.bin/calendar/calendar.c
    trunk/usr.bin/calendar/calendar.h
    trunk/usr.bin/calendar/dates.c
    trunk/usr.bin/calendar/day.c
    trunk/usr.bin/calendar/events.c
    trunk/usr.bin/calendar/io.c
    trunk/usr.bin/calendar/locale.c
    trunk/usr.bin/calendar/ostern.c
    trunk/usr.bin/calendar/parsedata.c
    trunk/usr.bin/calendar/sunpos.c

Modified: trunk/usr.bin/calendar/Makefile
===================================================================
--- trunk/usr.bin/calendar/Makefile	2018-07-08 15:40:58 UTC (rev 11586)
+++ trunk/usr.bin/calendar/Makefile	2018-07-08 15:41:59 UTC (rev 11587)
@@ -1,6 +1,9 @@
+# $MidnightBSD$
 #	@(#)Makefile	8.1 (Berkeley) 6/6/93
-# $MidnightBSD$
+# $FreeBSD: stable/10/usr.bin/calendar/Makefile 285291 2015-07-08 21:06:19Z bapt $
 
+.include <bsd.own.mk>
+
 PROG=	calendar
 SRCS=	calendar.c locale.c events.c dates.c parsedata.c io.c day.c \
 	ostern.c paskha.c pom.c sunpos.c
@@ -7,13 +10,12 @@
 DPADD=	${LIBM}
 LDADD=	-lm
 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
+		hr_HR.ISO8859-2 hu_HU.ISO8859-2 pt_BR.ISO8859-1 \
+		pt_BR.UTF-8 ru_RU.KOI8-R ru_RU.UTF-8 uk_UA.KOI8-U
 DE_LINKS=       de_DE.ISO8859-15
 FR_LINKS=       fr_FR.ISO8859-15
 TEXTMODE?=	444
 
-WARNS?=		7
-
 beforeinstall:
 	${INSTALL} -o ${BINOWN} -g ${BINGRP} -m ${TEXTMODE} \
 	    ${.CURDIR}/calendars/calendar.* ${DESTDIR}${SHAREDIR}/calendar
@@ -32,4 +34,8 @@
 	ln -s fr_FR.ISO8859-1 ${DESTDIR}${SHAREDIR}/calendar/${link}
 .endfor
 
+.if ${MK_TESTS} != "no"
+SUBDIR+= tests
+.endif
+
 .include <bsd.prog.mk>

Modified: trunk/usr.bin/calendar/calendar.c
===================================================================
--- trunk/usr.bin/calendar/calendar.c	2018-07-08 15:40:58 UTC (rev 11586)
+++ trunk/usr.bin/calendar/calendar.c	2018-07-08 15:41:59 UTC (rev 11587)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 1989, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -35,12 +36,12 @@
 
 #if 0
 #ifndef lint
-static char sccsid[] = "@(#)calendar.c  8.3 (Berkeley) 3/25/94";
+static char sccsid[] = "@(#)calendar.c	8.3 (Berkeley) 3/25/94";
 #endif
 #endif
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/usr.bin/calendar/calendar.c 285291 2015-07-08 21:06:19Z bapt $");
 
 #include <err.h>
 #include <errno.h>
@@ -60,8 +61,8 @@
 struct passwd	*pw;
 int		doall = 0;
 int		debug = 0;
-char		*DEBUG = NULL;
-time_t		f_time = 0;
+static char	*DEBUG = NULL;
+static time_t	f_time = 0;
 double		UTCOffset = UTCOFFSET_NOTSET;
 int		EastLongitude = LONGITUDE_NOTSET;
 
@@ -79,7 +80,7 @@
 
 	(void)setlocale(LC_ALL, "");
 
-	while ((ch = getopt(argc, argv, "-A:aB:dD:F:f:l:t:U:W:")) != -1)
+	while ((ch = getopt(argc, argv, "-A:aB:D:dF:f:l:t:U:W:?")) != -1)
 		switch (ch) {
 		case '-':		/* backward contemptible */
 		case 'a':
@@ -90,10 +91,6 @@
 			doall = 1;
 			break;
 
-		case 'f': /* other calendar file */
-			calendarFile = optarg;
-			break;
-
 		case 'W': /* we don't need no steenking Fridays */
 			Friday = -1;
 			/* FALLTHROUGH */
@@ -100,32 +97,44 @@
 
 		case 'A': /* days after current date */
 			f_dayAfter = atoi(optarg);
+			if (f_dayAfter < 0)
+				errx(1, "number of days must be positive");
 			break;
 
 		case 'B': /* days before current date */
 			f_dayBefore = atoi(optarg);
+			if (f_dayBefore < 0)
+				errx(1, "number of days must be positive");
 			break;
 
+		case 'D': /* debug output of sun and moon info */
+			DEBUG = optarg;
+			break;
+
+		case 'd': /* debug output of current date */
+			debug = 1;
+			break;
+
 		case 'F': /* Change the time: When does weekend start? */
 			Friday = atoi(optarg);
 			break;
+
+		case 'f': /* other calendar file */
+			calendarFile = optarg;
+			break;
+
 		case 'l': /* Change longitudal position */
 			EastLongitude = strtol(optarg, NULL, 10);
 			break;
+
+		case 't': /* other date, for tests */
+			f_time = Mktime(optarg);
+			break;
+
 		case 'U': /* Change UTC offset */
 			UTCOffset = strtod(optarg, NULL);
 			break;
 
-		case 'd':
-			debug = 1;
-			break;
-		case 'D':
-			DEBUG = optarg;
-			break;
-		case 't': /* other date, undocumented, for tests */
-			f_time = Mktime(optarg);
-			break;
-
 		case '?':
 		default:
 			usage();
@@ -216,10 +225,9 @@
 {
 
 	fprintf(stderr, "%s\n%s\n%s\n",
-	    "usage: calendar [-a] [-A days] [-B days] [-F friday] "
-	    "[-f calendarfile]",
-	    "                [-d] [-t dd[.mm[.year]]] [-W days]",
-	    "                [-U utcoffset] [-l longitude]"
+	    "usage: calendar [-A days] [-a] [-B days] [-D sun|moon] [-d]",
+	    "		     [-F friday] [-f calendarfile] [-l longitude]",
+	    "		     [-t dd[.mm[.year]]] [-U utcoffset] [-W days]"
 	    );
 	exit(1);
 }

Modified: trunk/usr.bin/calendar/calendar.h
===================================================================
--- trunk/usr.bin/calendar/calendar.h	2018-07-08 15:40:58 UTC (rev 11586)
+++ trunk/usr.bin/calendar/calendar.h	2018-07-08 15:41:59 UTC (rev 11587)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 1989, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -26,7 +27,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $MidnightBSD$
+ * $FreeBSD: stable/10/usr.bin/calendar/calendar.h 285291 2015-07-08 21:06:19Z bapt $
  */
 
 #include <sys/types.h>
@@ -101,11 +102,11 @@
 					 * program wrong.
 					 */
 
-/* 
+/*
  * All the astronomical calculations are carried out for the meridian 120
  * degrees east of Greenwich.
  */
-#define UTCOFFSET_CNY		8.0		
+#define UTCOFFSET_CNY		8.0
 
 extern int	debug;		/* show parsing of the input */
 extern int	year1, year2;
@@ -165,7 +166,8 @@
 /* io.c */
 void	cal(void);
 void	closecal(FILE *);
-FILE	*opencal(void);
+FILE	*opencalin(void);
+FILE	*opencalout(void);
 
 /* ostern.c / paskha.c */
 int	paskha(int);
@@ -174,7 +176,7 @@
 
 /* dates.c */
 extern int cumdaytab[][14];
-extern int mondaytab[][14];
+extern int monthdaytab[][14];
 extern int debug_remember;
 void	generatedates(struct tm *tp1, struct tm *tp2);
 void	dumpdates(void);

Modified: trunk/usr.bin/calendar/dates.c
===================================================================
--- trunk/usr.bin/calendar/dates.c	2018-07-08 15:40:58 UTC (rev 11586)
+++ trunk/usr.bin/calendar/dates.c	2018-07-08 15:41:59 UTC (rev 11587)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 1992-2009 Edwin Groothuis <edwin at FreeBSD.org>.
  * All rights reserved.
@@ -10,7 +11,7 @@
  * 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.
- * 
+ *
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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
@@ -22,11 +23,11 @@
  * 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>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/usr.bin/calendar/dates.c 251647 2013-06-12 07:52:49Z grog $");
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -43,7 +44,7 @@
 	int firstdayofweek; /* 0 .. 6 */
 	struct cal_month *months;
 	struct cal_year	*nextyear;
-} cal_year;
+};
 
 struct cal_month {
 	int month;			/* 01 .. 12 */
@@ -52,7 +53,7 @@
 	struct cal_year *year;		/* points back */
 	struct cal_day *days;
 	struct cal_month *nextmonth;
-} cal_month;
+};
 
 struct cal_day {
 	int dayofmonth;			/* 01 .. 31 */
@@ -62,20 +63,19 @@
 	struct cal_month *month;	/* points back */
 	struct cal_year	*year;		/* points back */
 	struct event *events;
-} cal_day;
+};
 
 int debug_remember = 0;
-struct cal_year	*hyear = NULL;
+static struct cal_year *hyear = NULL;
 
 /* 1-based month, 0-based days, cumulative */
-int *cumdays;
 int	cumdaytab[][14] = {
 	{0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364},
 	{0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
 };
 /* 1-based month, individual */
-int *mondays;
-int	mondaytab[][14] = {
+static int *monthdays;
+int	monthdaytab[][14] = {
 	{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30},
 	{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30},
 };
@@ -193,11 +193,11 @@
 		 * - Take all days from <m1 .. m2>
 		 * - Take the first days from m2
 		 */
-		mondays = mondaytab[isleap(y1)];
-		for (d = d1; d <= mondays[m1]; d++)
+		monthdays = monthdaytab[isleap(y1)];
+		for (d = d1; d <= monthdays[m1]; d++)
 			createdate(y1, m1, d);
 		for (m = m1 + 1; m < m2; m++)
-			for (d = 1; d <= mondays[m]; d++)
+			for (d = 1; d <= monthdays[m]; d++)
 				createdate(y1, m, d);
 		for (d = 1; d <= d2; d++)
 			createdate(y1, m2, d);
@@ -211,21 +211,21 @@
 	 * - Take all days from y2-[1 .. m2>
 	 * - Take the first days of y2-m2
 	 */
-	mondays = mondaytab[isleap(y1)];
-	for (d = d1; d <= mondays[m1]; d++)
+	monthdays = monthdaytab[isleap(y1)];
+	for (d = d1; d <= monthdays[m1]; d++)
 		createdate(y1, m1, d);
 	for (m = m1 + 1; m <= 12; m++)
-		for (d = 1; d <= mondays[m]; d++)
+		for (d = 1; d <= monthdays[m]; d++)
 			createdate(y1, m, d);
 	for (y = y1 + 1; y < y2; y++) {
-		mondays = mondaytab[isleap(y)];
+		monthdays = monthdaytab[isleap(y)];
 		for (m = 1; m <= 12; m++)
-			for (d = 1; d <= mondays[m]; d++)
+			for (d = 1; d <= monthdays[m]; d++)
 				createdate(y, m, d);
 	}
-	mondays = mondaytab[isleap(y2)];
+	monthdays = monthdaytab[isleap(y2)];
 	for (m = 1; m < m2; m++)
-		for (d = 1; d <= mondays[m]; d++)
+		for (d = 1; d <= monthdays[m]; d++)
 			createdate(y2, m, d);
 	for (d = 1; d <= d2; d++)
 		createdate(y2, m2, d);
@@ -361,12 +361,12 @@
 				return (m->firstdayofweek);
 			m = m->nextmonth;
 		}
-		/* Should not happen */
+		/* No data for this month */
 		return (-1);
 	}
 
-	/* Should not happen */
-	return (-1);
+	/* No data for this year.  Error? */
+        return (-1);
 }
 
 int

Modified: trunk/usr.bin/calendar/day.c
===================================================================
--- trunk/usr.bin/calendar/day.c	2018-07-08 15:40:58 UTC (rev 11586)
+++ trunk/usr.bin/calendar/day.c	2018-07-08 15:41:59 UTC (rev 11587)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 1989, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -28,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/usr.bin/calendar/day.c 241737 2012-10-19 14:49:42Z ed $");
 
 #include <err.h>
 #include <locale.h>
@@ -39,7 +40,7 @@
 
 #include "calendar.h"
 
-time_t			time1, time2;
+static time_t		time1, time2;
 const struct tm		tm0;
 char			dayname[100];
 int			year1, year2;

Modified: trunk/usr.bin/calendar/events.c
===================================================================
--- trunk/usr.bin/calendar/events.c	2018-07-08 15:40:58 UTC (rev 11586)
+++ trunk/usr.bin/calendar/events.c	2018-07-08 15:41:59 UTC (rev 11587)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 1992-2009 Edwin Groothuis <edwin at FreeBSD.org>.
  * All rights reserved.
@@ -26,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/usr.bin/calendar/events.c 205872 2010-03-30 06:42:01Z edwin $");
 
 #include <sys/time.h>
 #include <err.h>

Modified: trunk/usr.bin/calendar/io.c
===================================================================
--- trunk/usr.bin/calendar/io.c	2018-07-08 15:40:58 UTC (rev 11586)
+++ trunk/usr.bin/calendar/io.c	2018-07-08 15:41:59 UTC (rev 11587)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 1989, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -40,7 +41,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/usr.bin/calendar/io.c 285291 2015-07-08 21:06:19Z bapt $");
 
 #include <sys/param.h>
 #include <sys/stat.h>
@@ -51,23 +52,173 @@
 #include <langinfo.h>
 #include <locale.h>
 #include <pwd.h>
+#include <stdbool.h>
+#define _WITH_GETLINE
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stringlist.h>
 #include <unistd.h>
 
 #include "pathnames.h"
 #include "calendar.h"
 
+enum {
+	T_OK = 0,
+	T_ERR,
+	T_PROCESS,
+};
+
 const char *calendarFile = "calendar";	/* default calendar file */
-const char *calendarHomes[] = {".calendar", _PATH_INCLUDE};	/* HOME */
-const char *calendarNoMail = "nomail";	/* don't sent mail if this file exist */
+static const char *calendarHomes[] = {".calendar", _PATH_INCLUDE}; /* HOME */
+static const char *calendarNoMail = "nomail";/* don't sent mail if file exist */
 
-char	path[MAXPATHLEN];
+static char path[MAXPATHLEN];
 
 struct fixs neaster, npaskha, ncny, nfullmoon, nnewmoon;
 struct fixs nmarequinox, nsepequinox, njunsolstice, ndecsolstice;
 
+static int cal_parse(FILE *in, FILE *out);
+
+static StringList *definitions = NULL;
+static struct event *events[MAXCOUNT];
+static char *extradata[MAXCOUNT];
+
+static void
+trimlr(char **buf)
+{
+	char *walk = *buf;
+
+	while (isspace(*walk))
+		walk++;
+	while (isspace(walk[strlen(walk) -1]))
+		walk[strlen(walk) -1] = '\0';
+
+	*buf = walk;
+}
+
+static FILE *
+cal_fopen(const char *file)
+{
+	FILE *fp;
+	char *home = getenv("HOME");
+	unsigned int i;
+
+	if (home == NULL || *home == '\0') {
+		warnx("Cannot get home directory");
+		return (NULL);
+	}
+
+	if (chdir(home) != 0) {
+		warnx("Cannot enter home directory");
+		return (NULL);
+	}
+
+	for (i = 0; i < sizeof(calendarHomes)/sizeof(calendarHomes[0]) ; i++) {
+		if (chdir(calendarHomes[i]) != 0)
+			continue;
+
+		if ((fp = fopen(file, "r")) != NULL)
+			return (fp);
+	}
+
+	warnx("can't open calendar file \"%s\"", file);
+
+	return (NULL);
+}
+
+static int
+token(char *line, FILE *out, bool *skip)
+{
+	char *walk, c, a;
+
+	if (strncmp(line, "endif", 5) == 0) {
+		*skip = false;
+		return (T_OK);
+	}
+
+	if (*skip)
+		return (T_OK);
+
+	if (strncmp(line, "include", 7) == 0) {
+		walk = line + 7;
+
+		trimlr(&walk);
+
+		if (*walk == '\0') {
+			warnx("Expecting arguments after #include");
+			return (T_ERR);
+		}
+
+		if (*walk != '<' && *walk != '\"') {
+			warnx("Excecting '<' or '\"' after #include");
+			return (T_ERR);
+		}
+
+		a = *walk;
+		walk++;
+		c = walk[strlen(walk) - 1];
+
+		switch(c) {
+		case '>':
+			if (a != '<') {
+				warnx("Unterminated include expecting '\"'");
+				return (T_ERR);
+			}
+			break;
+		case '\"':
+			if (a != '\"') {
+				warnx("Unterminated include expecting '>'");
+				return (T_ERR);
+			}
+			break;
+		default:
+			warnx("Unterminated include expecting '%c'",
+			    a == '<' ? '>' : '\"' );
+			return (T_ERR);
+		}
+		walk[strlen(walk) - 1] = '\0';
+
+		if (cal_parse(cal_fopen(walk), out))
+			return (T_ERR);
+
+		return (T_OK);
+	}
+
+	if (strncmp(line, "define", 6) == 0) {
+		if (definitions == NULL)
+			definitions = sl_init();
+		walk = line + 6;
+		trimlr(&walk);
+
+		if (*walk == '\0') {
+			warnx("Expecting arguments after #define");
+			return (T_ERR);
+		}
+
+		sl_add(definitions, strdup(walk));
+		return (T_OK);
+	}
+
+	if (strncmp(line, "ifndef", 6) == 0) {
+		walk = line + 6;
+		trimlr(&walk);
+
+		if (*walk == '\0') {
+			warnx("Expecting arguments after #ifndef");
+			return (T_ERR);
+		}
+
+		if (definitions != NULL && sl_find(definitions, walk) != NULL)
+			*skip = true;
+
+		return (T_OK);
+	}
+
+	return (T_PROCESS);
+
+}
+
 #define	REPLACE(string, slen, struct_) \
 		if (strncasecmp(buf, (string), (slen)) == 0 && buf[(slen)]) { \
 			if (struct_.name != NULL)			      \
@@ -77,29 +228,26 @@
 			struct_.len = strlen(buf + (slen));		      \
 			continue;					      \
 		}
-void
-cal(void)
+static int
+cal_parse(FILE *in, FILE *out)
 {
-	char *pp, p;
-	FILE *fp;
-	int ch, l;
-	int count, i;
+	char *line = NULL;
+	char *buf;
+	size_t linecap = 0;
+	ssize_t linelen;
+	ssize_t l;
+	static int d_first = -1;
+	static int count = 0;
+	int i;
 	int month[MAXCOUNT];
 	int day[MAXCOUNT];
 	int year[MAXCOUNT];
-	char **extradata;	/* strings of 20 length */
+	bool skip = false;
+	char dbuf[80];
+	char *pp, p;
+	struct tm tm;
 	int flags;
-	static int d_first = -1;
-	char buf[2048 + 1];
-	struct event *events[MAXCOUNT];
-	struct tm tm;
-	char dbuf[80];
 
-	extradata = (char **)calloc(MAXCOUNT, sizeof(char *));
-	for (i = 0; i < MAXCOUNT; i++) {
-		extradata[i] = (char *)calloc(1, 20);
-	}
-
 	/* Unused */
 	tm.tm_sec = 0;
 	tm.tm_min = 0;
@@ -106,18 +254,32 @@
 	tm.tm_hour = 0;
 	tm.tm_wday = 0;
 
-	count = 0;
-	if ((fp = opencal()) == NULL) {
-		free(extradata);
-		return;
-	}
-	while (fgets(buf, sizeof(buf), stdin) != NULL) {
-		if ((pp = strchr(buf, '\n')) != NULL)
-			*pp = '\0';
-		else
-			/* Flush this line */
-			while ((ch = getchar()) != '\n' && ch != EOF);
-		for (l = strlen(buf);
+	if (in == NULL)
+		return (1);
+
+	while ((linelen = getline(&line, &linecap, in)) > 0) {
+		if (linelen == 0)
+			continue;
+
+		if (*line == '#') {
+			switch (token(line+1, out, &skip)) {
+			case T_ERR:
+				free(line);
+				return (1);
+			case T_OK:
+				continue;
+			case T_PROCESS:
+				break;
+			default:
+				break;
+			}
+		}
+
+		if (skip)
+			continue;
+
+		buf = line;
+		for (l = linelen;
 		     l > 0 && isspace((unsigned char)buf[l - 1]);
 		     l--)
 			;
@@ -176,7 +338,8 @@
 		*pp = p;
 		if (count < 0) {
 			/* Show error status based on return value */
-			fprintf(stderr, "Ignored: %s\n", buf);
+			if (debug)
+				fprintf(stderr, "Ignored: %s\n", buf);
 			if (count == -1)
 				continue;
 			count = -count + 1;
@@ -203,91 +366,71 @@
 		}
 	}
 
-	event_print_all(fp);
-	closecal(fp);
-	free(extradata);
+	free(line);
+	fclose(in);
+
+	return (0);
 }
 
+void
+cal(void)
+{
+	FILE *fpin;
+	FILE *fpout;
+	int i;
+
+	for (i = 0; i < MAXCOUNT; i++)
+		extradata[i] = (char *)calloc(1, 20);
+
+
+	if ((fpin = opencalin()) == NULL)
+		return;
+
+	if ((fpout = opencalout()) == NULL) {
+		fclose(fpin);
+		return;
+	}
+
+	if (cal_parse(fpin, fpout))
+		return;
+
+	event_print_all(fpout);
+	closecal(fpout);
+}
+
 FILE *
-opencal(void)
+opencalin(void)
 {
-	uid_t uid;
-	size_t i;
-	int fd, found, pdes[2];
 	struct stat sbuf;
+	FILE *fpin;
 
-	/* open up calendar file as stdin */
-	if (!freopen(calendarFile, "r", stdin)) {
+	/* open up calendar file */
+	if ((fpin = fopen(calendarFile, "r")) == NULL) {
 		if (doall) {
 			if (chdir(calendarHomes[0]) != 0)
 				return (NULL);
 			if (stat(calendarNoMail, &sbuf) == 0)
 				return (NULL);
-			if (!freopen(calendarFile, "r", stdin))
+			if ((fpin = fopen(calendarFile, "r")) == NULL)
 				return (NULL);
 		} else {
-			char *home = getenv("HOME");
-			if (home == NULL || *home == '\0')
-				errx(1, "cannot get home directory");
-			if (chdir(home) != 0)
-				errx(1, "cannot enter home directory");
-			for (found = i = 0; i < sizeof(calendarHomes) /
-			    sizeof(calendarHomes[0]); i++)
-				if (chdir(calendarHomes[i]) == 0 &&
-				    freopen(calendarFile, "r", stdin)) {
-					found = 1;
-					break;
-				}
-			if (!found)
-				errx(1,
-				    "can't open calendar file \"%s\": %s (%d)",
-				    calendarFile, strerror(errno), errno);
+			fpin = cal_fopen(calendarFile);
 		}
 	}
-	if (pipe(pdes) < 0)
-		return (NULL);
-	switch (fork()) {
-	case -1:			/* error */
-		(void)close(pdes[0]);
-		(void)close(pdes[1]);
-		return (NULL);
-	case 0:
-		/* child -- stdin already setup, set stdout to pipe input */
-		if (pdes[1] != STDOUT_FILENO) {
-			(void)dup2(pdes[1], STDOUT_FILENO);
-			(void)close(pdes[1]);
-		}
-		(void)close(pdes[0]);
-		uid = geteuid();
-		if (setuid(getuid()) < 0) {
-			warnx("first setuid failed");
-			_exit(1);
-		};
-		if (setgid(getegid()) < 0) {
-			warnx("setgid failed");
-			_exit(1);
-		}
-		if (setuid(uid) < 0) {
-			warnx("setuid failed");
-			_exit(1);
-		}
-		execl(_PATH_CPP, "cpp", "-P",
-		    "-traditional", "-nostdinc",	/* GCC specific opts */
-		    "-I.", "-I", _PATH_INCLUDE, (char *)NULL);
-		warn(_PATH_CPP);
-		_exit(1);
-	}
-	/* parent -- set stdin to pipe output */
-	(void)dup2(pdes[0], STDIN_FILENO);
-	(void)close(pdes[0]);
-	(void)close(pdes[1]);
+	return (fpin);
+}
 
+FILE *
+opencalout(void)
+{
+	int fd;
+
 	/* not reading all calendar files, just set output to stdout */
 	if (!doall)
 		return (stdout);
 
 	/* set output to a temporary file, so if no output don't send mail */
-	(void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
+	snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
 	if ((fd = mkstemp(path)) < 0)
 		return (NULL);
 	return (fdopen(fd, "w+"));

Modified: trunk/usr.bin/calendar/locale.c
===================================================================
--- trunk/usr.bin/calendar/locale.c	2018-07-08 15:40:58 UTC (rev 11586)
+++ trunk/usr.bin/calendar/locale.c	2018-07-08 15:41:59 UTC (rev 11587)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 1989, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -28,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/usr.bin/calendar/locale.c 211517 2010-08-19 21:59:39Z edwin $");
 
 #include <ctype.h>
 #include <err.h>

Modified: trunk/usr.bin/calendar/ostern.c
===================================================================
--- trunk/usr.bin/calendar/ostern.c	2018-07-08 15:40:58 UTC (rev 11586)
+++ trunk/usr.bin/calendar/ostern.c	2018-07-08 15:41:59 UTC (rev 11587)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 1996 Wolfram Schneider <wosch at FreeBSD.org>. Berlin.
  * All rights reserved.
@@ -25,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/usr.bin/calendar/ostern.c 205821 2010-03-29 06:49:20Z edwin $");
 
 #include <stdio.h>
 #include <stdlib.h>

Modified: trunk/usr.bin/calendar/parsedata.c
===================================================================
--- trunk/usr.bin/calendar/parsedata.c	2018-07-08 15:40:58 UTC (rev 11586)
+++ trunk/usr.bin/calendar/parsedata.c	2018-07-08 15:41:59 UTC (rev 11587)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 1992-2009 Edwin Groothuis <edwin at FreeBSD.org>.
  * All rights reserved.
@@ -10,7 +11,7 @@
  * 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.
- * 
+ *
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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
@@ -22,11 +23,11 @@
  * 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>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/usr.bin/calendar/parsedata.c 293290 2016-01-07 00:40:51Z bdrewery $");
 
 #include <ctype.h>
 #include <math.h>
@@ -47,6 +48,7 @@
 static int parseoffset(char *s);
 static char *floattoday(int year, double f);
 static char *floattotime(double f);
+static int wdayom (int day, int offset, int month, int year);
 
 /*
  * Expected styles:
@@ -79,7 +81,7 @@
  *				'300' ... '359' | '360' ... '365'
  * ModifierIndex	::=	'Second' | 'Third' | 'Fourth' | 'Fifth' |
  *				'First' | 'Last'
- * 
+ *
  * SpecialDay		::=	'Easter' | 'Paskha' | 'ChineseNewYear'
  *
  */
@@ -184,7 +186,7 @@
 	}
 
 	/*
-	 * AFTER this, leave by goto-ing to "allfine" or "fail" to restore the
+	 * After this, leave by goto-ing to "allfine" or "fail" to restore the
 	 * original data in `date'.
 	 */
 	pold = *p;
@@ -203,15 +205,9 @@
 		*flags |= F_YEAR;
 	}
 
-	/*
-	printf("p1: %s\n", p1);
-	printf("p2: %s\n", p2);
-	printf("year: %s\n", year);
-	*/
-
 	/* 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))) {
+	    || (checkmonth(p2, &len, &offset, &pmonth) != 0 && (p2 = p1))) {
 		/* p2 is the non-month part */
 		*flags |= F_MONTH;
 		*imonth = offset;
@@ -239,13 +235,12 @@
 			*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))) {
+	    || (strcmp(p2, "*") == 0 && isonlydigits(p1, 1) && (p2 = p1))) {
 		int d;
 
 		*flags |= F_ALLMONTH;
@@ -258,7 +253,7 @@
 
 	/* Month as a number, then a weekday */
 	if (isonlydigits(p1, 1)
-	 && checkdayofweek(p2, &len, &offset, &dow) != 0) {
+	    && checkdayofweek(p2, &len, &offset, &dow) != 0) {
 		int d;
 
 		*flags |= F_MONTH;
@@ -280,7 +275,7 @@
 
 	/* If both the month and date are specified as numbers */
 	if (isonlydigits(p1, 1) && isonlydigits(p2, 0)) {
-		/* Now who wants to be this ambigious? :-( */
+		/* Now who wants to be this ambiguous? :-( */
 		int m, d;
 
 		if (strchr(p2, '*') != NULL)
@@ -313,11 +308,14 @@
 allfine:
 	*p = pold;
 	return (1);
-	
+
 }
 
-static void
+void
 remember(int *rememberindex, int *y, int *m, int *d, char **ed, int yy, int mm,
+    int dd, char *extra);
+void
+remember(int *rememberindex, int *y, int *m, int *d, char **ed, int yy, int mm,
     int dd, char *extra)
 {
 	static int warned = 0;
@@ -367,7 +365,7 @@
 		printf("specialday: |%s|\n", specialday);
 }
 
-struct yearinfo {
+static struct yearinfo {
 	int year;
 	int ieaster, ipaskha, firstcnyday;
 	double ffullmoon[MAXMOONS], fnewmoon[MAXMOONS];
@@ -374,10 +372,54 @@
 	double ffullmooncny[MAXMOONS], fnewmooncny[MAXMOONS];
 	int ichinesemonths[MAXMOONS];
 	double equinoxdays[2], solsticedays[2];
-	int *mondays;
+	int *monthdays;
 	struct yearinfo *next;
-};
+} *years, *yearinfo;
+
 /*
+ * Calculate dates with offset from weekdays, like Thurs-3, Wed+2, etc.
+ * day is the day of the week,
+ * offset the ordinal number of the weekday in the month.
+ */
+static int
+wdayom (int day, int offset, int month, int year)
+{
+/* Weekday of first day in month */
+	int wday1;                                /* first day of month */
+/* Weekday of last day in month */
+	int wdayn;
+	int d;
+
+	wday1 = first_dayofweek_of_month(year, month);
+	if (wday1 < 0)                          /* not set */
+		return (wday1);
+	/*
+	 * Date of zeroth or first of our weekday in month, depending on the
+	 * relationship with the first of the month.  The range is -6:6.
+	 */
+	d = (day - wday1 + 1) % 7;
+	/*
+	 * Which way are we counting?  Offset 0 is invalid, abs (offset) > 5 is
+	 * meaningless, but that's OK.  Offset 5 may or may not be meaningless,
+	 * so there's no point in complaining for complaining's sake.
+	 */
+	if (offset < 0) {			/* back from end of month */
+						/* FIXME */
+		wdayn = d;
+		while (wdayn <= yearinfo->monthdays[month])
+			wdayn += 7;
+		d = offset * 7 + wdayn;
+	} else if (offset > 0){
+		if (d > 0)
+			d += offset * 7 - 7;
+		else
+			d += offset * 7;
+	} else
+		warnx ("Invalid offset 0");
+	return (d);
+}
+
+/*
  * Possible date formats include any combination of:
  *	3-charmonth			(January, Jan, Jan)
  *	3-charweekday			(Friday, Monday, mon.)
@@ -400,14 +442,12 @@
 	char *ed;
 	int retvalsign = 1;
 
-	static struct yearinfo *years, *yearinfo;
-
 	/*
 	 * CONVENTION
 	 *
 	 * Month:     1-12
 	 * Monthname: Jan .. Dec
-	 * Day:       1-31
+	 * Day:	      1-31
 	 * Weekday:   Mon .. Sun
 	 *
 	 */
@@ -419,8 +459,8 @@
 		    dayofmonth, idayofmonth, dayofweek, idayofweek,
 		    modifieroffset, modifierindex, specialday, syear, iyear);
 	if (determinestyle(date, flags, month, &imonth, dayofmonth,
-	    &idayofmonth, dayofweek, &idayofweek, modifieroffset,
-	    modifierindex, specialday, syear, &iyear) == 0) {
+		&idayofmonth, dayofweek, &idayofweek, modifieroffset,
+		modifierindex, specialday, syear, &iyear) == 0) {
 		if (debug)
 			printf("Failed!\n");
 		return (0);
@@ -457,7 +497,7 @@
 			yearinfo->next = years;
 			years = yearinfo;
 
-			yearinfo->mondays = mondaytab[isleap(year)];
+			yearinfo->monthdays = monthdaytab[isleap(year)];
 			yearinfo->ieaster = easter(year);
 			yearinfo->ipaskha = paskha(year);
 			fpom(year, UTCOffset, yearinfo->ffullmoon,
@@ -514,7 +554,7 @@
 
 		/* Every day of a month */
 		if (lflags == (F_ALLDAY | F_MONTH)) {
-			for (d = 1; d <= yearinfo->mondays[imonth]; d++) {
+			for (d = 1; d <= yearinfo->monthdays[imonth]; d++) {
 				if (!remember_ymd(year, imonth, d))
 					continue;
 				remember(&remindex, yearp, monthp, dayp, edp,
@@ -548,14 +588,15 @@
 			continue;
 		}
 
-		/* Every so-manied dayofweek of every month of the year */
+		/*
+	         * Every so-manied dayofweek of every month of the year:
+	         * Thu-3
+	         */
 		if (lflags == (F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) {
 			offset = indextooffset(modifierindex);
 
-			for (m = 0; m < 12; m++) {
-				dow = first_dayofweek_of_month(year, m);
-				d = (idayofweek - dow + 8) % 7;
-				d += (offset - 1) * 7;
+			for (m = 0; m <= 12; m++) {
+	                        d = wdayom (idayofweek, offset, m, year);
 				if (remember_ymd(year, m, d)) {
 					remember(&remindex,
 					    yearp, monthp, dayp, edp,
@@ -566,7 +607,10 @@
 			continue;
 		}
 
-		/* A certain dayofweek of a month */
+		/*
+	         * A certain dayofweek of a month
+	         * Jan/Thu-3
+	         */
 		if (lflags ==
 		    (F_MONTH | F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) {
 			offset = indextooffset(modifierindex);
@@ -574,9 +618,9 @@
 			d = (idayofweek - dow + 8) % 7;
 
 			if (offset > 0) {
-				while (d <= yearinfo->mondays[imonth]) {
+				while (d <= yearinfo->monthdays[imonth]) {
 					if (--offset == 0
-					 && remember_ymd(year, imonth, d)) {
+					    && remember_ymd(year, imonth, d)) {
 						remember(&remindex,
 						    yearp, monthp, dayp, edp,
 						    year, imonth, d, NULL);
@@ -587,7 +631,7 @@
 				continue;
 			}
 			if (offset < 0) {
-				while (d <= yearinfo->mondays[imonth])
+				while (d <= yearinfo->monthdays[imonth])
 					d += 7;
 				while (offset != 0) {
 					offset++;
@@ -606,7 +650,7 @@
 		if (lflags == (F_DAYOFWEEK | F_MONTH | F_VARIABLE)) {
 			dow = first_dayofweek_of_month(year, imonth);
 			d = (idayofweek - dow + 8) % 7;
-			while (d <= yearinfo->mondays[imonth]) {
+			while (d <= yearinfo->monthdays[imonth]) {
 				if (remember_ymd(year, imonth, d))
 					remember(&remindex,
 					    yearp, monthp, dayp, edp,
@@ -623,7 +667,7 @@
 			if ((lflags & F_MODIFIEROFFSET) != 0)
 				offset = parseoffset(modifieroffset);
 			if (remember_yd(year, yearinfo->ieaster + offset,
-			    &rm, &rd))
+	                        &rm, &rd))
 				remember(&remindex, yearp, monthp, dayp, edp,
 				    year, rm, rd, NULL);
 			continue;
@@ -636,7 +680,7 @@
 			if ((lflags & F_MODIFIEROFFSET) != 0)
 				offset = parseoffset(modifieroffset);
 			if (remember_yd(year, yearinfo->ipaskha + offset,
-			    &rm, &rd))
+	                        &rm, &rd))
 				remember(&remindex, yearp, monthp, dayp, edp,
 				    year, rm, rd, NULL);
 			continue;
@@ -649,7 +693,7 @@
 			if ((lflags & F_MODIFIEROFFSET) != 0)
 				offset = parseoffset(modifieroffset);
 			if (remember_yd(year, yearinfo->firstcnyday + offset,
-			    &rm, &rd))
+	                        &rm, &rd))
 				remember(&remindex, yearp, monthp, dayp, edp,
 				    year, rm, rd, NULL);
 			continue;
@@ -665,7 +709,7 @@
 				offset = parseoffset(modifieroffset);
 			for (i = 0; yearinfo->ffullmoon[i] > 0; i++) {
 				if (remember_yd(year,
-				    floor(yearinfo->ffullmoon[i]) + offset,
+	                                floor(yearinfo->ffullmoon[i]) + offset,
 					&rm, &rd)) {
 					ed = floattotime(
 					    yearinfo->ffullmoon[i]);
@@ -687,8 +731,8 @@
 				offset = parseoffset(modifieroffset);
 			for (i = 0; yearinfo->ffullmoon[i] > 0; i++) {
 				if (remember_yd(year,
-				    floor(yearinfo->fnewmoon[i]) + offset,
-				    &rm, &rd)) {
+					floor(yearinfo->fnewmoon[i]) + offset,
+					&rm, &rd)) {
 					ed = floattotime(yearinfo->fnewmoon[i]);
 					remember(&remindex,
 					    yearp, monthp, dayp, edp,
@@ -705,7 +749,7 @@
 			if ((lflags & F_MODIFIEROFFSET) != 0)
 				offset = parseoffset(modifieroffset);
 			if (remember_yd(year, yearinfo->equinoxdays[0] + offset,
-			    &rm, &rd)) {
+				&rm, &rd)) {
 				ed = floattotime(yearinfo->equinoxdays[0]);
 				remember(&remindex, yearp, monthp, dayp, edp,
 				    year, rm, rd, ed);
@@ -733,7 +777,7 @@
 			if ((lflags & F_MODIFIEROFFSET) != 0)
 				offset = parseoffset(modifieroffset);
 			if (remember_yd(year,
-			    yearinfo->solsticedays[0] + offset, &rm, &rd)) {
+				yearinfo->solsticedays[0] + offset, &rm, &rd)) {
 				ed = floattotime(yearinfo->solsticedays[0]);
 				remember(&remindex, yearp, monthp, dayp, edp,
 				    year, rm, rd, ed);
@@ -746,7 +790,7 @@
 			if ((lflags & F_MODIFIEROFFSET) != 0)
 				offset = parseoffset(modifieroffset);
 			if (remember_yd(year,
-			    yearinfo->solsticedays[1] + offset, &rm, &rd)) {
+				yearinfo->solsticedays[1] + offset, &rm, &rd)) {
 				ed = floattotime(yearinfo->solsticedays[1]);
 				remember(&remindex, yearp, monthp, dayp, edp,
 				    year, rm, rd, ed);
@@ -754,10 +798,13 @@
 			continue;
 		}
 
-		printf("Unprocessed:\n");
-		debug_determinestyle(2, date, lflags, month, imonth,
-		    dayofmonth, idayofmonth, dayofweek, idayofweek,
-		    modifieroffset, modifierindex, specialday, syear, iyear);
+		if (debug) {
+			printf("Unprocessed:\n");
+			debug_determinestyle(2, date, lflags, month, imonth,
+			    dayofmonth, idayofmonth, dayofweek, idayofweek,
+			    modifieroffset, modifierindex, specialday, syear,
+			    iyear);
+		}
 		retvalsign = -1;
 	}
 
@@ -937,15 +984,13 @@
 {
 	int i;
 	struct fixs *n;
+	char *es;
 
 	if (s[0] == '+' || s[0] == '-') {
-		char ss[9];
-		for (i = -100; i < 100; i++) {
-			sprintf(ss, "%s%d", (i > 0) ? "+" : "", i);
-			if (strcmp(ss, s) == 0)
-				return (i);
-		}
-		return (0);
+		i = strtol (s, &es, 10);
+		if (*es != '\0')                      /* trailing junk */
+			errx (1, "Invalid specifier format: %s\n", s);
+		return (i);
 	}
 
 	for (i = 0; i < 6; i++) {
@@ -971,7 +1016,6 @@
 static int
 parseoffset(char *s)
 {
-
 	return strtol(s, NULL, 10);
 }
 
@@ -1045,7 +1089,7 @@
 			printf("\n");
 
 		}
-	
+
 		return;
 	}
 

Modified: trunk/usr.bin/calendar/sunpos.c
===================================================================
--- trunk/usr.bin/calendar/sunpos.c	2018-07-08 15:40:58 UTC (rev 11586)
+++ trunk/usr.bin/calendar/sunpos.c	2018-07-08 15:41:59 UTC (rev 11587)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 2009-2010 Edwin Groothuis <edwin at FreeBSD.org>.
  * All rights reserved.
@@ -10,7 +11,7 @@
  * 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.
- * 
+ *
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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
@@ -22,11 +23,11 @@
  * 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>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/usr.bin/calendar/sunpos.c 251647 2013-06-12 07:52:49Z grog $");
 
 /*
  * This code is created to match the formulas available at:
@@ -373,7 +374,7 @@
 	int firstmonth330 = -1;
 
 	cumdays = cumdaytab[isleap(year)];
-	monthdays = mondaytab[isleap(year)];
+	monthdays = monthdaytab[isleap(year)];
 	pichinesemonths = ichinesemonths;
 
 	h = 0;



More information about the Midnightbsd-cvs mailing list