Skip site navigation (1)Skip section navigation (2)
Date:      Thu,  9 May 2013 16:05:23 -0400 (EDT)
From:      Diane Bruce <db@db.net>
To:        FreeBSD-gnats-submit@freebsd.org
Subject:   bin/178463: Remove cpp usage from calendar
Message-ID:  <20130509200523.40B50FA@odyssey.db.net>
Resent-Message-ID: <201305092010.r49KA0f6024158@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         178463
>Category:       bin
>Synopsis:       Remove cpp usage from calendar
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Thu May 09 20:10:00 UTC 2013
>Closed-Date:
>Last-Modified:
>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:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20130509200523.40B50FA>