Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 19 Sep 2013 20:17:50 +0000 (UTC)
From:      Diane Bruce <db@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r255715 - head/usr.bin/calendar
Message-ID:  <201309192017.r8JKHoMj000974@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
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"



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