From db@db.net  Thu May  9 20:06:18 2013
Return-Path: <db@db.net>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1])
	by hub.freebsd.org (Postfix) with ESMTP id 8A9AE78E
	for <FreeBSD-gnats-submit@freebsd.org>; Thu,  9 May 2013 20:06:18 +0000 (UTC)
	(envelope-from db@db.net)
Received: from diana.db.net (unknown [IPv6:2620:64:0:1:223:7dff:fea2:c8f2])
	by mx1.freebsd.org (Postfix) with ESMTP id 54F69F3B
	for <FreeBSD-gnats-submit@freebsd.org>; Thu,  9 May 2013 20:06:18 +0000 (UTC)
Received: from odyssey.db.net (localhost [127.0.0.1])
	by diana.db.net (Postfix) with ESMTP id 003202AA335
	for <FreeBSD-gnats-submit@freebsd.org>; Thu,  9 May 2013 14:06:16 -0600 (MDT)
Received: by odyssey.db.net (Postfix, from userid 1000)
	id 40B50FA; Thu,  9 May 2013 16:05:23 -0400 (EDT)
Message-Id: <20130509200523.40B50FA@odyssey.db.net>
Date: Thu,  9 May 2013 16:05:23 -0400 (EDT)
From: Diane Bruce <db@db.net>
Reply-To: Diane Bruce <db@db.net>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: Remove cpp usage from calendar
X-Send-Pr-Version: 3.114
X-GNATS-Notify:

>Number:         178463
>Category:       bin
>Synopsis:       [patch] Remove cpp usage from calendar(1)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Thu May 09 20:10:00 UTC 2013
>Closed-Date:    Mon Sep 23 14:12:05 UTC 2013
>Last-Modified:  Mon Sep 23 14:12:05 UTC 2013
>Originator:     Diane Bruce
>Release:        FreeBSD 10.0-CURRENT amd64
>Organization:
No, since I am terrible at it
>Environment:
System: FreeBSD odyssey.db.net 10.0-CURRENT FreeBSD 10.0-CURRENT #95 r250311M: Mon May 6 17:18:02 EDT 2013 root@odyssey:/usr/obj/home/db/svn/system/head/sys/GENERIC amd64


	
>Description:
	
>How-To-Repeat:

calendar uses cpp internally, this diff removes this usage and
substitutes a limited subset cpp processor internally.

	
>Fix:

	


Index: usr.bin/calendar/Makefile
===================================================================
--- usr.bin/calendar/Makefile	(revision 250419)
+++ usr.bin/calendar/Makefile	(working copy)
@@ -3,7 +3,7 @@
 
 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
+	ostern.c paskha.c pom.c sunpos.c calcpp.c
 DPADD=	${LIBM}
 LDADD=	-lm
 INTER=          de_AT.ISO_8859-15 de_DE.ISO8859-1 fr_FR.ISO8859-1 \
Index: usr.bin/calendar/calendar.1
===================================================================
--- usr.bin/calendar/calendar.1	(revision 250419)
+++ usr.bin/calendar/calendar.1	(working copy)
@@ -177,12 +177,14 @@
 If the first character in the line is a <tab> character, it is treated as
 a continuation of the previous line.
 .Pp
-The ``calendar'' file is preprocessed by
-.Xr cpp 1 ,
-allowing the inclusion of shared files such as lists of company holidays or
-meetings.
+The
+.Nm
+file is preprocessed by a limited subset of
+.Xr cpp 1 
+internally, allowing the inclusion of shared files such as
+lists of company holidays or meetings.
 If the shared file is not referenced by a full pathname,
-.Xr cpp 1
+.Xr calendar 1
 searches in the current (or home) directory first, and then in the
 directory
 .Pa /usr/share/calendar .
@@ -321,7 +323,11 @@
 .Sh BUGS
 The
 .Nm
-utility does not handle Jewish holidays.
+internal cpp does not correctly do #ifndef and will discard the rest
+of the file if a #ifndef is triggered.
+It also has a maximum of 50 include file and/or 100 #defines
+and only recognises #include, #define and
+#ifndef.
 .Pp
 There is no possibility to properly specify the local position
 needed for solar and lunar calculations.
Index: usr.bin/calendar/calendar.h
===================================================================
--- usr.bin/calendar/calendar.h	(revision 250419)
+++ usr.bin/calendar/calendar.h	(working copy)
@@ -165,8 +165,13 @@
 /* io.c */
 void	cal(void);
 void	closecal(FILE *);
-FILE	*opencal(void);
+FILE	*opencalin(void);
+FILE	*opencalout(void);
 
+/* calcpp.c */
+void	initcpp(void);
+FILE	*fincludegets(char *buf, int size, FILE *fp);
+
 /* ostern.c / paskha.c */
 int	paskha(int);
 int	easter(int);
Index: usr.bin/calendar/io.c
===================================================================
--- usr.bin/calendar/io.c	(revision 250419)
+++ usr.bin/calendar/io.c	(working copy)
@@ -81,8 +81,9 @@
 cal(void)
 {
 	char *pp, p;
-	FILE *fp;
-	int ch, l;
+	FILE *fpin;
+	FILE *fpout;
+	int l;
 	int count, i;
 	int month[MAXCOUNT];
 	int day[MAXCOUNT];
@@ -95,6 +96,7 @@
 	struct tm tm;
 	char dbuf[80];
 
+	initcpp();
 	extradata = (char **)calloc(MAXCOUNT, sizeof(char *));
 	for (i = 0; i < MAXCOUNT; i++) {
 		extradata[i] = (char *)calloc(1, 20);
@@ -107,16 +109,18 @@
 	tm.tm_wday = 0;
 
 	count = 0;
-	if ((fp = opencal()) == NULL) {
+	if ((fpin = opencalin()) == 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);
+	if ((fpout = opencalout()) == NULL) {
+		fclose(fpin);
+		free(extradata);
+		return;
+	}
+	while ((fpin = fincludegets(buf, sizeof(buf), fpin)) != NULL) {
+		if (*buf == '\0')
+			continue;
 		for (l = strlen(buf);
 		     l > 0 && isspace((unsigned char)buf[l - 1]);
 		     l--)
@@ -204,27 +208,27 @@
 		}
 	}
 
-	event_print_all(fp);
-	closecal(fp);
+	event_print_all(fpout);
+	closecal(fpout);
 	free(extradata);
 }
 
 FILE *
-opencal(void)
+opencalin(void)
 {
-	uid_t uid;
 	size_t i;
-	int fd, found, pdes[2];
+	int found;
 	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");
@@ -235,7 +239,7 @@
 			for (found = i = 0; i < sizeof(calendarHomes) /
 			    sizeof(calendarHomes[0]); i++)
 				if (chdir(calendarHomes[i]) == 0 &&
-				    freopen(calendarFile, "r", stdin)) {
+				    (fpin = fopen(calendarFile, "r")) != NULL) {
 					found = 1;
 					break;
 				}
@@ -245,50 +249,20 @@
 				    calendarFile, strerror(errno), errno);
 		}
 	}
-	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+"));
Index: usr.bin/calendar/pathnames.h
===================================================================
--- usr.bin/calendar/pathnames.h	(revision 250419)
+++ usr.bin/calendar/pathnames.h	(working copy)
@@ -32,5 +32,4 @@
 
 #include <paths.h>
 
-#define	_PATH_CPP	"/usr/bin/cpp"
 #define	_PATH_INCLUDE	"/usr/share/calendar"
--- /dev/null	2013-05-09 16:01:59.000000000 -0400
+++ usr.bin/calendar/calcpp.c	2013-04-28 18:56:04.719821288 -0400
@@ -0,0 +1,230 @@
+/*-
+ * Copyright (c) 2013 Diane Bruce
+ * 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.
+ *
+ * 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
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR 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.
+ *
+ * $FreeBSD$
+ */
+
+/* calendar fake cpp does a very limited cpp version */
+
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <assert.h>
+#include <ctype.h>
+#include <err.h>
+#include <errno.h>
+#include <langinfo.h>
+#include <locale.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "pathnames.h"
+#include "calendar.h"
+
+#define MAXFPSTACK	50
+static FILE *fpstack[MAXFPSTACK];
+static int curfpi;
+static void pushfp(FILE *fp);
+static FILE *popfp(void);
+static int tokenscpp(char *buf, char *string);
+
+#define T_INVALID	-1
+#define T_INCLUDE	0
+#define T_DEFINE	1
+#define T_IFDEF		2
+#define T_IFNDEF	3
+#define T_ENDIF		4
+
+#define MAXSYMS		100
+static char *symtable[MAXSYMS];
+static void addsym(const char *name);
+static int findsym(const char *name);
+
+FILE *
+fincludegets(char *buf, int size, FILE *fp)
+{
+	char name[MAXPATHLEN];
+	FILE *nfp=NULL;
+	char *p;
+	int ch;
+
+	if (fp == NULL)
+		return(NULL);
+
+	if (fgets(buf, size, fp) == NULL) {
+		*buf = '\0';
+		fclose(fp);
+		fp = popfp();
+		return (fp);
+	}
+	if ((p = strchr(buf, '\n')) != NULL)
+		*p = '\0';
+	else {
+		/* Flush this line */
+		while ((ch = fgetc(fp)) != '\n' && ch != EOF);
+		if (ch == EOF) {
+			*buf = '\0';
+			fclose(fp);
+			fp = popfp();
+			return(fp);
+		}
+	}
+	switch (tokenscpp(buf, name)) {
+	case T_INCLUDE:
+		*buf = '\0';
+		if ((nfp = fopen(name, "r")) != NULL) {
+			pushfp(fp);
+			fp = nfp;
+		}
+		break;
+	case T_DEFINE:
+		addsym(name);
+		break;
+	case T_IFNDEF:
+		if (findsym(name)) {
+			fclose(fp);
+			fp = popfp();
+			*buf = '\0';
+		}
+		break;
+	case T_ENDIF:
+		*buf = '\0';
+		break;
+	default:
+		break;
+	}
+	return (fp);
+}
+
+static int
+tokenscpp(char *buf, char *string)
+{
+	char *p;
+	char *s;
+
+	if ((p = strstr(buf, "#define")) != NULL) {
+		p += 8;
+		while (isspace((unsigned char)*p))
+			p++;
+		s = p;
+		while(!isspace((unsigned char)*p))
+			p++;
+		strncpy(string, s, MAXPATHLEN);
+		return(T_DEFINE);
+	} else if ((p = strstr(buf, "#ifndef")) != NULL) {
+		p += 8;
+		while (isspace((unsigned char)*p))
+			p++;
+		s = p;
+		while(!isspace((unsigned char)*p))
+			p++;
+		*p = '\0';
+		strncpy(string, s, MAXPATHLEN);
+		return(T_IFNDEF);
+	} else if ((p = strstr(buf, "#endif")) != NULL) {
+		return(T_ENDIF);
+	} if ((p = strstr(buf, "#include")) != NULL) {
+		p += 8;
+		while (isspace((unsigned char)*p))
+			p++;
+		if (*p == '<') {
+			s = p+1;
+			if ((p = strchr(s, '>')) != NULL)
+				*p = '\0';
+			snprintf (string, MAXPATHLEN, "%s/%s",
+				_PATH_INCLUDE, s);
+		} else if (*p == '(') {
+			s = p+1;
+			if ((p = strchr(p, '>')) != NULL)
+				*p = '\0';
+			snprintf (string, MAXPATHLEN, "%s", s);
+		}
+		return(T_INCLUDE);
+	}
+	return(T_INVALID);
+}
+
+static void
+pushfp(FILE *fp)
+{
+	curfpi++;
+	if (curfpi == MAXFPSTACK)
+		errx(1, "Max #include reached");
+	fpstack[curfpi] = fp;
+}
+
+static
+FILE *popfp(void)
+{
+	FILE *tmp;
+
+	assert(curfpi >= 0);
+	tmp = fpstack[curfpi];
+	curfpi--;
+	return(tmp);
+}
+
+void
+initcpp(void)
+{
+	int i;
+
+	for (i=0; i < MAXSYMS; i++)
+		symtable[i] = NULL;
+	fpstack[0] = NULL;
+	curfpi = 0;
+}
+
+
+static void
+addsym(const char *name)
+{
+	int i;
+	
+	if (!findsym(name))
+		for (i=0; i < MAXSYMS; i++) {
+			if (symtable[i] == NULL) {
+				symtable[i] = strdup(name);
+				if (symtable[i] == NULL)
+					errx(1, "malloc error in addsym");
+				return;
+			}
+		}
+	errx(1, "symbol table full\n");
+}
+
+static int
+findsym(const char *name)
+{
+	int i;
+	
+	for (i=0; i < MAXSYMS; i++)
+		if (symtable[i] != NULL && strcmp(symtable[i],name) == 0)
+			return (1);
+	return (0);
+}
>Release-Note:
>Audit-Trail:

From: Baptiste Daroussin <bapt@freebsd.org>
To: bug-followup@freebsd.org, db@db.net
Cc:  
Subject: Re: bin/178463: [patch] Remove cpp usage from calendar(1)
Date: Tue, 11 Jun 2013 21:03:36 +0200

 --001a11c1bb2a138ae204dee58f5c
 Content-Type: text/plain; charset=ISO-8859-1
 
 Netbsd is working on tradcpp, I have been working with them to use it in
 ports for imake so that it works properly with or without gcpp.
 
 I plan to import it in base so that calendar(1) could directly use it.
 
 regards,
 Bapt
 
 --001a11c1bb2a138ae204dee58f5c
 Content-Type: text/html; charset=ISO-8859-1
 
 <div dir="ltr"><div><div>Netbsd is working on tradcpp, I have been working with them to use it in ports for imake so that it works properly with or without gcpp.<br><br></div>I plan to import it in base so that calendar(1) could directly use it.<br>
 
 <br></div><div>regards,<br>Bapt<br></div></div>
 
 --001a11c1bb2a138ae204dee58f5c--

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/178463: commit references a PR
Date: Thu, 19 Sep 2013 20:18:00 +0000 (UTC)

 Author: db (ports committer)
 Date: Thu Sep 19 20:17:50 2013
 New Revision: 255715
 URL: http://svnweb.freebsd.org/changeset/base/255715
 
 Log:
   - calendar uses cpp internally, this diff removes this usage and
     substitutes a limited subset cpp processor internally.
   
   PR:		src/178463
   Approved by:	re (gjb)
 
 Added:
   head/usr.bin/calendar/calcpp.c   (contents, props changed)
 Modified:
   head/usr.bin/calendar/Makefile   (contents, props changed)
   head/usr.bin/calendar/calendar.1   (contents, props changed)
   head/usr.bin/calendar/calendar.h   (contents, props changed)
   head/usr.bin/calendar/io.c   (contents, props changed)
   head/usr.bin/calendar/pathnames.h   (contents, props changed)
 
 Modified: head/usr.bin/calendar/Makefile
 ==============================================================================
 --- head/usr.bin/calendar/Makefile	Thu Sep 19 20:15:24 2013	(r255714)
 +++ head/usr.bin/calendar/Makefile	Thu Sep 19 20:17:50 2013	(r255715)
 @@ -3,7 +3,7 @@
  
  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
 +	ostern.c paskha.c pom.c sunpos.c calcpp.c
  DPADD=	${LIBM}
  LDADD=	-lm
  INTER=          de_AT.ISO_8859-15 de_DE.ISO8859-1 fr_FR.ISO8859-1 \
 
 Added: head/usr.bin/calendar/calcpp.c
 ==============================================================================
 --- /dev/null	00:00:00 1970	(empty, because file is newly added)
 +++ head/usr.bin/calendar/calcpp.c	Thu Sep 19 20:17:50 2013	(r255715)
 @@ -0,0 +1,229 @@
 +/*-
 + * Copyright (c) 2013 Diane Bruce
 + * 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.
 + *
 + * 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
 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR 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.
 + *
 + * $FreeBSD$
 + */
 +
 +/* calendar fake cpp does a very limited cpp version */
 +
 +#include <sys/param.h>
 +#include <sys/stat.h>
 +#include <sys/wait.h>
 +#include <assert.h>
 +#include <ctype.h>
 +#include <err.h>
 +#include <errno.h>
 +#include <langinfo.h>
 +#include <locale.h>
 +#include <pwd.h>
 +#include <stdio.h>
 +#include <stdlib.h>
 +#include <string.h>
 +#include <unistd.h>
 +
 +#include "pathnames.h"
 +#include "calendar.h"
 +
 +#define MAXFPSTACK	50
 +static FILE *fpstack[MAXFPSTACK];
 +static int curfpi;
 +static void pushfp(FILE *fp);
 +static FILE *popfp(void);
 +static int tokenscpp(char *buf, char *string);
 +
 +#define T_INVALID	-1
 +#define T_INCLUDE	0
 +#define T_DEFINE	1
 +#define T_IFNDEF	2
 +#define T_ENDIF		3
 +
 +#define MAXSYMS		100
 +static char *symtable[MAXSYMS];
 +static void addsym(const char *name);
 +static int findsym(const char *name);
 +
 +FILE *
 +fincludegets(char *buf, int size, FILE *fp)
 +{
 +	char name[MAXPATHLEN];
 +	FILE *nfp=NULL;
 +	char *p;
 +	int ch;
 +
 +	if (fp == NULL)
 +		return(NULL);
 +
 +	if (fgets(buf, size, fp) == NULL) {
 +		*buf = '\0';
 +		fclose(fp);
 +		fp = popfp();
 +		return (fp);
 +	}
 +	if ((p = strchr(buf, '\n')) != NULL)
 +		*p = '\0';
 +	else {
 +		/* Flush this line */
 +		while ((ch = fgetc(fp)) != '\n' && ch != EOF);
 +		if (ch == EOF) {
 +			*buf = '\0';
 +			fclose(fp);
 +			fp = popfp();
 +			return(fp);
 +		}
 +	}
 +	switch (tokenscpp(buf, name)) {
 +	case T_INCLUDE:
 +		*buf = '\0';
 +		if ((nfp = fopen(name, "r")) != NULL) {
 +			pushfp(fp);
 +			fp = nfp;
 +		}
 +		break;
 +	case T_DEFINE:
 +		addsym(name);
 +		break;
 +	case T_IFNDEF:
 +		if (findsym(name)) {
 +			fclose(fp);
 +			fp = popfp();
 +			*buf = '\0';
 +		}
 +		break;
 +	case T_ENDIF:
 +		*buf = '\0';
 +		break;
 +	default:
 +		break;
 +	}
 +	return (fp);
 +}
 +
 +static int
 +tokenscpp(char *buf, char *string)
 +{
 +	char *p;
 +	char *s;
 +
 +	if ((p = strstr(buf, "#define")) != NULL) {
 +		p += 8;
 +		while (isspace((unsigned char)*p))
 +			p++;
 +		s = p;
 +		while(!isspace((unsigned char)*p))
 +			p++;
 +		strncpy(string, s, MAXPATHLEN);
 +		return(T_DEFINE);
 +	} else if ((p = strstr(buf, "#ifndef")) != NULL) {
 +		p += 8;
 +		while (isspace((unsigned char)*p))
 +			p++;
 +		s = p;
 +		while(!isspace((unsigned char)*p))
 +			p++;
 +		*p = '\0';
 +		strncpy(string, s, MAXPATHLEN);
 +		return(T_IFNDEF);
 +	} else if ((p = strstr(buf, "#endif")) != NULL) {
 +		return(T_ENDIF);
 +	} if ((p = strstr(buf, "#include")) != NULL) {
 +		p += 8;
 +		while (isspace((unsigned char)*p))
 +			p++;
 +		if (*p == '<') {
 +			s = p+1;
 +			if ((p = strchr(s, '>')) != NULL)
 +				*p = '\0';
 +			snprintf (string, MAXPATHLEN, "%s/%s",
 +				_PATH_INCLUDE, s);
 +		} else if (*p == '(') {
 +			s = p+1;
 +			if ((p = strchr(p, '>')) != NULL)
 +				*p = '\0';
 +			snprintf (string, MAXPATHLEN, "%s", s);
 +		}
 +		return(T_INCLUDE);
 +	}
 +	return(T_INVALID);
 +}
 +
 +static void
 +pushfp(FILE *fp)
 +{
 +	curfpi++;
 +	if (curfpi == MAXFPSTACK)
 +		errx(1, "Max #include reached");
 +	fpstack[curfpi] = fp;
 +}
 +
 +static
 +FILE *popfp(void)
 +{
 +	FILE *tmp;
 +
 +	assert(curfpi >= 0);
 +	tmp = fpstack[curfpi];
 +	curfpi--;
 +	return(tmp);
 +}
 +
 +void
 +initcpp(void)
 +{
 +	int i;
 +
 +	for (i=0; i < MAXSYMS; i++)
 +		symtable[i] = NULL;
 +	fpstack[0] = NULL;
 +	curfpi = 0;
 +}
 +
 +
 +static void
 +addsym(const char *name)
 +{
 +	int i;
 +	
 +	if (!findsym(name))
 +		for (i=0; i < MAXSYMS; i++) {
 +			if (symtable[i] == NULL) {
 +				symtable[i] = strdup(name);
 +				if (symtable[i] == NULL)
 +					errx(1, "malloc error in addsym");
 +				return;
 +			}
 +		}
 +	errx(1, "symbol table full\n");
 +}
 +
 +static int
 +findsym(const char *name)
 +{
 +	int i;
 +	
 +	for (i=0; i < MAXSYMS; i++)
 +		if (symtable[i] != NULL && strcmp(symtable[i],name) == 0)
 +			return (1);
 +	return (0);
 +}
 
 Modified: head/usr.bin/calendar/calendar.1
 ==============================================================================
 --- head/usr.bin/calendar/calendar.1	Thu Sep 19 20:15:24 2013	(r255714)
 +++ head/usr.bin/calendar/calendar.1	Thu Sep 19 20:17:50 2013	(r255715)
 @@ -177,12 +177,15 @@ if the line does not contain a <tab> cha
  If the first character in the line is a <tab> character, it is treated as
  a continuation of the previous line.
  .Pp
 -The ``calendar'' file is preprocessed by
 -.Xr cpp 1 ,
 -allowing the inclusion of shared files such as lists of company holidays or
 -meetings.
 +The
 +.Nm
 +file is preprocessed by a limited subset of
 +.Xr cpp 1 
 +internally, allowing the inclusion of shared files such as
 +lists of company holidays or meetings.
 +This limited subset consists of \fB#include #ifndef #endif\fR and \fB#define\fR.
  If the shared file is not referenced by a full pathname,
 -.Xr cpp 1
 +.Xr calendar 1
  searches in the current (or home) directory first, and then in the
  directory
  .Pa /usr/share/calendar .
 @@ -321,7 +324,11 @@ double-check the start and end time of s
  .Sh BUGS
  The
  .Nm
 -utility does not handle Jewish holidays.
 +internal cpp does not correctly do #ifndef and will discard the rest
 +of the file if a #ifndef is triggered.
 +It also has a maximum of 50 include file and/or 100 #defines
 +and only recognises #include, #define and
 +#ifndef.
  .Pp
  There is no possibility to properly specify the local position
  needed for solar and lunar calculations.
 
 Modified: head/usr.bin/calendar/calendar.h
 ==============================================================================
 --- head/usr.bin/calendar/calendar.h	Thu Sep 19 20:15:24 2013	(r255714)
 +++ head/usr.bin/calendar/calendar.h	Thu Sep 19 20:17:50 2013	(r255715)
 @@ -165,7 +165,12 @@ void	dodebug(char *type);
  /* io.c */
  void	cal(void);
  void	closecal(FILE *);
 -FILE	*opencal(void);
 +FILE	*opencalin(void);
 +FILE	*opencalout(void);
 +
 +/* calcpp.c */
 +void	initcpp(void);
 +FILE	*fincludegets(char *buf, int size, FILE *fp);
  
  /* ostern.c / paskha.c */
  int	paskha(int);
 
 Modified: head/usr.bin/calendar/io.c
 ==============================================================================
 --- head/usr.bin/calendar/io.c	Thu Sep 19 20:15:24 2013	(r255714)
 +++ head/usr.bin/calendar/io.c	Thu Sep 19 20:17:50 2013	(r255715)
 @@ -81,8 +81,9 @@ void
  cal(void)
  {
  	char *pp, p;
 -	FILE *fp;
 -	int ch, l;
 +	FILE *fpin;
 +	FILE *fpout;
 +	int l;
  	int count, i;
  	int month[MAXCOUNT];
  	int day[MAXCOUNT];
 @@ -95,6 +96,7 @@ cal(void)
  	struct tm tm;
  	char dbuf[80];
  
 +	initcpp();
  	extradata = (char **)calloc(MAXCOUNT, sizeof(char *));
  	for (i = 0; i < MAXCOUNT; i++) {
  		extradata[i] = (char *)calloc(1, 20);
 @@ -107,16 +109,18 @@ cal(void)
  	tm.tm_wday = 0;
  
  	count = 0;
 -	if ((fp = opencal()) == NULL) {
 +	if ((fpin = opencalin()) == 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);
 +	if ((fpout = opencalout()) == NULL) {
 +		fclose(fpin);
 +		free(extradata);
 +		return;
 +	}
 +	while ((fpin = fincludegets(buf, sizeof(buf), fpin)) != NULL) {
 +		if (*buf == '\0')
 +			continue;
  		for (l = strlen(buf);
  		     l > 0 && isspace((unsigned char)buf[l - 1]);
  		     l--)
 @@ -204,27 +208,27 @@ cal(void)
  		}
  	}
  
 -	event_print_all(fp);
 -	closecal(fp);
 +	event_print_all(fpout);
 +	closecal(fpout);
  	free(extradata);
  }
  
  FILE *
 -opencal(void)
 +opencalin(void)
  {
 -	uid_t uid;
  	size_t i;
 -	int fd, found, pdes[2];
 +	int found;
  	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");
 @@ -235,7 +239,7 @@ opencal(void)
  			for (found = i = 0; i < sizeof(calendarHomes) /
  			    sizeof(calendarHomes[0]); i++)
  				if (chdir(calendarHomes[i]) == 0 &&
 -				    freopen(calendarFile, "r", stdin)) {
 +				    (fpin = fopen(calendarFile, "r")) != NULL) {
  					found = 1;
  					break;
  				}
 @@ -245,50 +249,20 @@ opencal(void)
  				    calendarFile, strerror(errno), errno);
  		}
  	}
 -	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-cpp", "-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: head/usr.bin/calendar/pathnames.h
 ==============================================================================
 --- head/usr.bin/calendar/pathnames.h	Thu Sep 19 20:15:24 2013	(r255714)
 +++ head/usr.bin/calendar/pathnames.h	Thu Sep 19 20:17:50 2013	(r255715)
 @@ -32,5 +32,4 @@
  
  #include <paths.h>
  
 -#define	_PATH_CPP	"/usr/bin/gcpp"
  #define	_PATH_INCLUDE	"/usr/share/calendar"
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->closed 
State-Changed-By: db 
State-Changed-When: Mon Sep 23 14:12:04 UTC 2013 
State-Changed-Why:  
Commited myself approved by re@ (gjb) 

http://www.freebsd.org/cgi/query-pr.cgi?pr=178463 
>Unformatted:
