- use cpp -traditional, since the default has now changed. We want to allow
  unmatched single quotes!
- use fparseln, instead of a fixed 2k buffer.
- make all locals static and move to the top. This is so we can eventually
  make calendar understand languages other than us_en
- add braces and continue's to clarify things.
- replace gratuitous fprintf uses with warnx.
- replace vforks() with forks() since we tried to print errors with stdio.
- add more warnings so that we know how things fail.

XXX: Eventually we'll have to remove the cpp dependency, and we should:
	- make it use m4 instead [bad, breaks compatibility]
    or
	- add a small cpp like parser for #include [bad, too much code]
This commit is contained in:
christos 2001-12-04 15:55:32 +00:00
parent 239aeafb2e
commit bb58ba64b2
2 changed files with 111 additions and 107 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.10 1999/02/13 02:54:54 lukem Exp $ # $NetBSD: Makefile,v 1.11 2001/12/04 15:55:32 christos Exp $
# @(#)Makefile 8.1 (Berkeley) 6/6/93 # @(#)Makefile 8.1 (Berkeley) 6/6/93
.include <bsd.own.mk> .include <bsd.own.mk>
@ -8,5 +8,7 @@ PROG= calendar
FILESDIR=/usr/share/calendar FILESDIR=/usr/share/calendar
FILES!= echo ${.CURDIR}/calendars/calendar.* FILES!= echo ${.CURDIR}/calendars/calendar.*
.endif .endif
DPADD+=${LIBUTIL}
LDADD+=-lutil
.include <bsd.prog.mk> .include <bsd.prog.mk>

View File

@ -1,4 +1,4 @@
/* $NetBSD: calendar.c,v 1.25 2001/02/19 23:03:44 cgd Exp $ */ /* $NetBSD: calendar.c,v 1.26 2001/12/04 15:55:32 christos Exp $ */
/* /*
* Copyright (c) 1989, 1993, 1994 * Copyright (c) 1989, 1993, 1994
@ -43,7 +43,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
#if 0 #if 0
static char sccsid[] = "@(#)calendar.c 8.4 (Berkeley) 1/7/95"; static char sccsid[] = "@(#)calendar.c 8.4 (Berkeley) 1/7/95";
#endif #endif
__RCSID("$NetBSD: calendar.c,v 1.25 2001/02/19 23:03:44 cgd Exp $"); __RCSID("$NetBSD: calendar.c,v 1.26 2001/12/04 15:55:32 christos Exp $");
#endif /* not lint */ #endif /* not lint */
#include <sys/param.h> #include <sys/param.h>
@ -63,6 +63,7 @@ __RCSID("$NetBSD: calendar.c,v 1.25 2001/02/19 23:03:44 cgd Exp $");
#include <time.h> #include <time.h>
#include <tzfile.h> #include <tzfile.h>
#include <unistd.h> #include <unistd.h>
#include <util.h>
#include "pathnames.h" #include "pathnames.h"
@ -73,23 +74,53 @@ __RCSID("$NetBSD: calendar.c,v 1.25 2001/02/19 23:03:44 cgd Exp $");
#define FALSE 0 #define FALSE 0
#endif #endif
unsigned short lookahead = 1, weekend = 2; static unsigned short lookahead = 1, weekend = 2;
char *fname = "calendar", *datestr = NULL; static char *fname = "calendar", *datestr = NULL;
struct passwd *pw; static struct passwd *pw;
int doall; static int doall;
static char path[MAXPATHLEN + 1];
void atodays __P((char, char *, unsigned short *)); /* 1-based month, 0-based days, cumulative */
void cal __P((void)); static int daytab[][14] = {
void closecal __P((FILE *)); { 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
int getday __P((char *)); { 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
int getfield __P((char *, char **, int *)); };
void getmmdd(struct tm *tp, char *ds); static struct tm *tp;
int getmonth __P((char *)); static int *cumdays, offset, yrdays;
int isnow __P((char *)); static char dayname[10];
int main __P((int, char **));
FILE *opencal __P((void)); static struct iovec header[] = {
void settime __P((void)); { "From: ", 6 },
void usage __P((void)); { NULL, 0 },
{ " (Reminder Service)\nTo: ", 24 },
{ NULL, 0 },
{ "\nSubject: ", 10 },
{ NULL, 0 },
{ "'s Calendar\nPrecedence: bulk\n\n", 30 },
};
static char *days[] = {
"sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL,
};
static char *months[] = {
"jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec", NULL,
};
int main(int, char **);
static void atodays(int, char *, unsigned short *);
static void cal(void);
static void closecal(FILE *);
static int getday(char *);
static int getfield(char *, char **, int *);
static void getmmdd(struct tm *, char *);
static int getmonth(char *);
static int isnow(char *);
static FILE *opencal(void);
static void settime(void);
static void usage(void) __attribute__((__noreturn__));
int int
main(argc, argv) main(argc, argv)
@ -132,7 +163,7 @@ main(argc, argv)
usage(); usage();
settime(); settime();
if (doall) if (doall) {
while ((pw = getpwent()) != NULL) { while ((pw = getpwent()) != NULL) {
(void)setegid(pw->pw_gid); (void)setegid(pw->pw_gid);
(void)seteuid(pw->pw_uid); (void)seteuid(pw->pw_uid);
@ -140,61 +171,40 @@ main(argc, argv)
cal(); cal();
(void)seteuid(0); (void)seteuid(0);
} }
else if ((caldir = getenv("CALENDAR_DIR")) != NULL) { } else if ((caldir = getenv("CALENDAR_DIR")) != NULL) {
if(!chdir(caldir)) if(!chdir(caldir))
cal(); cal();
} else } else {
cal(); cal();
}
exit(0); exit(0);
} }
void static void
cal() cal(void)
{ {
int printing; int printing;
char *p;
FILE *fp; FILE *fp;
int ch; char *line;
char buf[2048 + 1];
if ((fp = opencal()) == NULL) if ((fp = opencal()) == NULL)
return; return;
for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) { while ((line = fparseln(stdin, NULL, NULL, NULL, 0)) != NULL) {
if ((p = strchr(buf, '\n')) != NULL) if (line[0] == '\0')
*p = '\0';
else
while ((ch = getchar()) != '\n' && ch != EOF);
if (buf[0] == '\0')
continue; continue;
if (buf[0] != '\t') if (line[0] != '\t')
printing = isnow(buf) ? 1 : 0; printing = isnow(line) ? 1 : 0;
if (printing) if (printing)
(void)fprintf(fp, "%s\n", buf); (void)fprintf(fp, "%s\n", line);
free(line);
} }
closecal(fp); closecal(fp);
} }
struct iovec header[] = {
{ "From: ", 6 },
{ NULL, 0 },
{ " (Reminder Service)\nTo: ", 24 },
{ NULL, 0 },
{ "\nSubject: ", 10 },
{ NULL, 0 },
{ "'s Calendar\nPrecedence: bulk\n\n", 30 },
};
/* 1-based month, 0-based days, cumulative */ static void
int daytab[][14] = { settime(void)
{ 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
{ 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
};
struct tm *tp;
int *cumdays, offset, yrdays;
char dayname[10];
void
settime()
{ {
time_t now; time_t now;
@ -226,7 +236,7 @@ settime()
* following a line that is matched, that starts with "whitespace", is shown * following a line that is matched, that starts with "whitespace", is shown
* along with the matched line. * along with the matched line.
*/ */
int static int
isnow(endp) isnow(endp)
char *endp; char *endp;
{ {
@ -272,7 +282,7 @@ isnow(endp)
return (0); return (0);
} }
int static int
getfield(p, endp, flags) getfield(p, endp, flags)
char *p, **endp; char *p, **endp;
int *flags; int *flags;
@ -284,7 +294,7 @@ getfield(p, endp, flags)
!isalpha((unsigned char)*p) && *p != '*') !isalpha((unsigned char)*p) && *p != '*')
for (; FLDCHAR(*p); ++p) for (; FLDCHAR(*p); ++p)
; continue;
if (*p == '*') { /* `*' is current month */ if (*p == '*') { /* `*' is current month */
*flags |= F_ISMONTH; *flags |= F_ISMONTH;
*endp = p+1; *endp = p+1;
@ -293,32 +303,30 @@ getfield(p, endp, flags)
if (isdigit((unsigned char)*p)) { if (isdigit((unsigned char)*p)) {
val = strtol(p, &p, 10); /* if 0, it's failure */ val = strtol(p, &p, 10); /* if 0, it's failure */
for (; FLDCHAR(*p); ++p) for (; FLDCHAR(*p); ++p)
; continue;
*endp = p; *endp = p;
return (val); return (val);
} }
for (start = p; *p != '\0' && isalpha((unsigned char)*++p);) for (start = p; *p != '\0' && isalpha((unsigned char)*++p);)
; continue;
savech = *p; savech = *p;
*p = '\0'; *p = '\0';
if ((val = getmonth(start)) != 0) if ((val = getmonth(start)) != 0) {
*flags |= F_ISMONTH; *flags |= F_ISMONTH;
else if ((val = getday(start)) != 0) } else if ((val = getday(start)) != 0) {
*flags |= F_ISDAY; *flags |= F_ISDAY;
else { } else {
*p = savech; *p = savech;
return (0); return (0);
} }
for (*p = savech; FLDCHAR(*p); ++p) for (*p = savech; FLDCHAR(*p); ++p)
; continue;
*endp = p; *endp = p;
return (val); return (val);
} }
char path[MAXPATHLEN + 1]; static FILE *
opencal(void)
FILE *
opencal()
{ {
int fd, pdes[2]; int fd, pdes[2];
@ -328,9 +336,11 @@ opencal()
return (NULL); return (NULL);
err(1, "Cannot open `%s'", fname); err(1, "Cannot open `%s'", fname);
} }
if (pipe(pdes) < 0) if (pipe(pdes) < 0) {
warn("Cannot open pipe");
return (NULL); return (NULL);
switch (vfork()) { }
switch (fork()) {
case -1: /* error */ case -1: /* error */
(void)close(pdes[0]); (void)close(pdes[0]);
(void)close(pdes[1]); (void)close(pdes[1]);
@ -342,9 +352,10 @@ opencal()
(void)close(pdes[1]); (void)close(pdes[1]);
} }
(void)close(pdes[0]); (void)close(pdes[0]);
execl(_PATH_CPP, "cpp", "-P", "-I.", "-I" _PATH_CALENDARS, NULL); (void)execl(_PATH_CPP, "cpp", "-traditional", "-P", "-I.",
warn("execl: %s", _PATH_CPP); "-I" _PATH_CALENDARS, NULL);
_exit(1); err(1, "Cannot exec `%s'", _PATH_CPP);
/*NOTREACHED*/
} }
/* parent -- set stdin to pipe output */ /* parent -- set stdin to pipe output */
(void)dup2(pdes[0], STDIN_FILENO); (void)dup2(pdes[0], STDIN_FILENO);
@ -357,12 +368,14 @@ opencal()
/* set output to a temporary file, so if no output don't send mail */ /* set output to a temporary file, so if no output don't send mail */
(void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP); (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
if ((fd = mkstemp(path)) < 0) if ((fd = mkstemp(path)) < 0) {
warn("Cannot create temporary file");
return (NULL); return (NULL);
}
return (fdopen(fd, "w+")); return (fdopen(fd, "w+"));
} }
void static void
closecal(fp) closecal(fp)
FILE *fp; FILE *fp;
{ {
@ -378,7 +391,7 @@ closecal(fp)
goto done; goto done;
if (pipe(pdes) < 0) if (pipe(pdes) < 0)
goto done; goto done;
switch (vfork()) { switch (fork()) {
case -1: /* error */ case -1: /* error */
(void)close(pdes[0]); (void)close(pdes[0]);
(void)close(pdes[1]); (void)close(pdes[1]);
@ -390,10 +403,10 @@ closecal(fp)
(void)close(pdes[0]); (void)close(pdes[0]);
} }
(void)close(pdes[1]); (void)close(pdes[1]);
execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F", (void)execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
"\"Reminder Service\"", "-f", "root", NULL); "\"Reminder Service\"", "-f", "root", NULL);
warn("execl: %s", _PATH_SENDMAIL); err(1, "Cannot exec `%s'", _PATH_SENDMAIL);
_exit(1); /*NOTREACHED*/
} }
/* parent -- write to pipe input */ /* parent -- write to pipe input */
(void)close(pdes[0]); (void)close(pdes[0]);
@ -404,17 +417,14 @@ closecal(fp)
while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0) while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
(void)write(pdes[1], buf, nread); (void)write(pdes[1], buf, nread);
(void)close(pdes[1]); (void)close(pdes[1]);
done: (void)fclose(fp); done: (void)fclose(fp);
(void)unlink(path); (void)unlink(path);
while (wait(&status) >= 0); while (wait(&status) >= 0)
continue;
} }
static char *months[] = { static int
"jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec", NULL,
};
int
getmonth(s) getmonth(s)
char *s; char *s;
{ {
@ -426,11 +436,7 @@ getmonth(s)
return (0); return (0);
} }
static char *days[] = { static int
"sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL,
};
int
getday(s) getday(s)
char *s; char *s;
{ {
@ -442,16 +448,14 @@ getday(s)
return (0); return (0);
} }
void static void
atodays(char ch, char *optarg, unsigned short *days) atodays(int ch, char *optarg, unsigned short *days)
{ {
int u; int u;
u = atoi(optarg); u = atoi(optarg);
if ((u < 0) || (u > 366)) { if ((u < 0) || (u > 366)) {
fprintf(stderr, warnx("-%c %d out of range 0-366, ignored.", ch, u);
"%s: warning: -%c %d out of range 0-366, ignored.\n",
getprogname(), ch, u);
} else { } else {
*days = u; *days = u;
} }
@ -461,7 +465,7 @@ atodays(char ch, char *optarg, unsigned short *days)
#define ATOI2(x) (todigit((x)[0]) * 10 + todigit((x)[1])) #define ATOI2(x) (todigit((x)[0]) * 10 + todigit((x)[1]))
#define ISDIG2(x) (isdigit((unsigned char)(x)[0]) && isdigit((unsigned char)(x)[1])) #define ISDIG2(x) (isdigit((unsigned char)(x)[0]) && isdigit((unsigned char)(x)[1]))
void static void
getmmdd(struct tm *tp, char *ds) getmmdd(struct tm *tp, char *ds)
{ {
int ok = FALSE; int ok = FALSE;
@ -503,17 +507,15 @@ getmmdd(struct tm *tp, char *ds)
if (ok) { if (ok) {
*tp = ttm; *tp = ttm;
} else { } else {
fprintf(stderr, warnx("Can't convert `%s' to date, ignored.", ds);
"%s: warning: can't convert %s to date, ignored.\n",
getprogname(), ds);
usage(); usage();
} }
} }
void static void
usage() usage(void)
{ {
(void)fprintf(stderr, "Usage: %s [-a] [-d MMDD[[YY]YY]" \ (void)fprintf(stderr, "Usage: %s [-a] [-d MMDD[[YY]YY]"
" [-f fname] [-l days] [-w days]\n", getprogname()); " [-f fname] [-l days] [-w days]\n", getprogname());
exit(1); exit(1);
} }