diff options
author | sjg <sjg@FreeBSD.org> | 2013-10-13 02:35:19 +0000 |
---|---|---|
committer | sjg <sjg@FreeBSD.org> | 2013-10-13 02:35:19 +0000 |
commit | 7fcd33c1faf567506b5c0b4148c7a15a10788a5d (patch) | |
tree | 2c6f4d1ca5d1c643faea64e1f4c90105a1ab406a /usr.bin/calendar | |
parent | 2a59274eda20cc626e28052fff7aa8b7bf6a3683 (diff) | |
parent | 5cca672bb0892f1c5da630c34a1f98e2de4d7064 (diff) | |
download | FreeBSD-src-7fcd33c1faf567506b5c0b4148c7a15a10788a5d.zip FreeBSD-src-7fcd33c1faf567506b5c0b4148c7a15a10788a5d.tar.gz |
Merge head@256284
Diffstat (limited to 'usr.bin/calendar')
-rw-r--r-- | usr.bin/calendar/Makefile | 2 | ||||
-rw-r--r-- | usr.bin/calendar/calcpp.c | 229 | ||||
-rw-r--r-- | usr.bin/calendar/calendar.1 | 19 | ||||
-rw-r--r-- | usr.bin/calendar/calendar.h | 7 | ||||
-rw-r--r-- | usr.bin/calendar/calendars/calendar.dutch | 22 | ||||
-rw-r--r-- | usr.bin/calendar/calendars/calendar.freebsd | 1 | ||||
-rw-r--r-- | usr.bin/calendar/io.c | 86 | ||||
-rw-r--r-- | usr.bin/calendar/pathnames.h | 1 |
8 files changed, 291 insertions, 76 deletions
diff --git a/usr.bin/calendar/Makefile b/usr.bin/calendar/Makefile index c7bd544..66403fe 100644 --- a/usr.bin/calendar/Makefile +++ b/usr.bin/calendar/Makefile @@ -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 \ diff --git a/usr.bin/calendar/calcpp.c b/usr.bin/calendar/calcpp.c new file mode 100644 index 0000000..6e3b339 --- /dev/null +++ b/usr.bin/calendar/calcpp.c @@ -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); +} diff --git a/usr.bin/calendar/calendar.1 b/usr.bin/calendar/calendar.1 index 6531bba..374fbef 100644 --- a/usr.bin/calendar/calendar.1 +++ b/usr.bin/calendar/calendar.1 @@ -177,12 +177,15 @@ if the line does not contain a <tab> character, it is not displayed. 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. -If the shared file is not referenced by a full pathname, +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 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 solar and lunar events. .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. diff --git a/usr.bin/calendar/calendar.h b/usr.bin/calendar/calendar.h index 87e705c..0138fd2 100644 --- a/usr.bin/calendar/calendar.h +++ b/usr.bin/calendar/calendar.h @@ -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); diff --git a/usr.bin/calendar/calendars/calendar.dutch b/usr.bin/calendar/calendars/calendar.dutch index edabe10..13a456b 100644 --- a/usr.bin/calendar/calendars/calendar.dutch +++ b/usr.bin/calendar/calendars/calendar.dutch @@ -22,11 +22,11 @@ nov/01 Allerheiligen nov/02 Allerzielen nov/11 Sint Maarten nov/11 Elfde-van-de-elfde -dec/05 Sinterklaas avond +dec/05 Sinterklaasavond dec/15 Koninkrijksdag dec/24 Kerstavond -dec/25 Eerste kerstdag -dec/26 Tweede kerstdag +dec/25 Eerste Kerstdag +dec/26 Tweede Kerstdag dec/28 Feest der Onnozele Kinderen dec/31 Oudjaar @@ -40,10 +40,10 @@ Pasen-47 Carnaval (Vastenavond) Pasen-46 Aswoensdag Pasen-7 Palmzondag Pasen-3 Witte Donderdag -Pasen-2 Goede vrijdag -Pasen-1 Stille zaterdag -Pasen Eerste paasdag -Pasen+1 Tweede paasdag +Pasen-2 Goede Vrijdag +Pasen-1 Stille Zaterdag +Pasen Eerste Paasdag +Pasen+1 Tweede Paasdag Pasen+39 Hemelvaartsdag Pasen+49 Eerste Pinksterdag Pasen+50 Tweede Pinksterdag @@ -65,15 +65,15 @@ feb/17 Prins Willem III (1817 - 1890) feb/18 Prinses Christina (1947) apr/10 Prinses Ariane (2007) apr/19 Prins Hendrik (1876 - 1934) -apr/27 Koning Willem Alexander (1967) +apr/27 Koning Willem-Alexander (1967) apr/30 Prinses Juliana (1909 - 2004) apr/30 Mr. Pieter van Vollenhoven (1939) -mei/17 Prinses Maxima (1971) +mei/17 Koningin Maxima (1971) jun/26 Prinses Alexia (2005) jun/29 Prins Bernhard (1911 - 2004) aug/05 Prinses Irene (1939) aug/31 Prinses Wilhelmina (1880 - 1962) sep/06 Prins Claus (1925 - 2002) -sep/25 Prins Johan Friso (1968) +sep/25 Prins Johan Friso (1968 - 2013) okt/11 Prins Constantijn (1969) -dec/07 Prinses Catharina-Amalia (2003) +dec/07 Kroonprinses Catharina-Amalia (2003) diff --git a/usr.bin/calendar/calendars/calendar.freebsd b/usr.bin/calendar/calendars/calendar.freebsd index 6cf0346..4222d7b 100644 --- a/usr.bin/calendar/calendars/calendar.freebsd +++ b/usr.bin/calendar/calendars/calendar.freebsd @@ -329,6 +329,7 @@ 11/09 Coleman Kane <cokane@FreeBSD.org> born in Cincinnati, Ohio, United States, 1980 11/09 Antoine Brodin <antoine@FreeBSD.org> born in Bagnolet, France, 1981 11/10 Gregory Neil Shapiro <gshapiro@FreeBSD.org> born in Providence, Rhode Island, United States, 1970 +11/11 Danilo E. Gondolfo <danilo@FreeBSD.org> born in Lobato, Parana, Brazil, 1987 11/13 John Baldwin <jhb@FreeBSD.org> born in Stuart, Virginia, United States, 1977 11/14 Jeremie Le Hen <jlh@FreeBSD.org> born in Nancy, France, 1980 11/15 Lars Engels <lme@FreeBSD.org> born in Hilden, Nordrhein-Westfalen, Germany, 1980 diff --git a/usr.bin/calendar/io.c b/usr.bin/calendar/io.c index 2ea0865..0f34882 100644 --- a/usr.bin/calendar/io.c +++ b/usr.bin/calendar/io.c @@ -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+")); diff --git a/usr.bin/calendar/pathnames.h b/usr.bin/calendar/pathnames.h index a46913c..e5507a1 100644 --- a/usr.bin/calendar/pathnames.h +++ b/usr.bin/calendar/pathnames.h @@ -32,5 +32,4 @@ #include <paths.h> -#define _PATH_CPP "/usr/bin/gcpp" #define _PATH_INCLUDE "/usr/share/calendar" |