Index: usr.bin/calendar/Makefile =================================================================== --- usr.bin/calendar/Makefile (revision 251419) +++ 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 251419) +++ usr.bin/calendar/calendar.1 (working copy) @@ -177,12 +177,14 @@ If the first character in the line is a 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 251419) +++ 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 251419) +++ 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 251419) +++ usr.bin/calendar/pathnames.h (working copy) @@ -32,5 +32,4 @@ #include -#define _PATH_CPP "/usr/bin/cpp" #define _PATH_INCLUDE "/usr/share/calendar"