- 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
.include <bsd.own.mk>
@ -8,5 +8,7 @@ PROG= calendar
FILESDIR=/usr/share/calendar
FILES!= echo ${.CURDIR}/calendars/calendar.*
.endif
DPADD+=${LIBUTIL}
LDADD+=-lutil
.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
@ -43,7 +43,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
#if 0
static char sccsid[] = "@(#)calendar.c 8.4 (Berkeley) 1/7/95";
#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 */
#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 <tzfile.h>
#include <unistd.h>
#include <util.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
#endif
unsigned short lookahead = 1, weekend = 2;
char *fname = "calendar", *datestr = NULL;
struct passwd *pw;
int doall;
static unsigned short lookahead = 1, weekend = 2;
static char *fname = "calendar", *datestr = NULL;
static struct passwd *pw;
static int doall;
static char path[MAXPATHLEN + 1];
void atodays __P((char, char *, unsigned short *));
void cal __P((void));
void closecal __P((FILE *));
int getday __P((char *));
int getfield __P((char *, char **, int *));
void getmmdd(struct tm *tp, char *ds);
int getmonth __P((char *));
int isnow __P((char *));
int main __P((int, char **));
FILE *opencal __P((void));
void settime __P((void));
void usage __P((void));
/* 1-based month, 0-based days, cumulative */
static int daytab[][14] = {
{ 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 },
};
static struct tm *tp;
static int *cumdays, offset, yrdays;
static char dayname[10];
static struct iovec header[] = {
{ "From: ", 6 },
{ 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
main(argc, argv)
@ -132,7 +163,7 @@ main(argc, argv)
usage();
settime();
if (doall)
if (doall) {
while ((pw = getpwent()) != NULL) {
(void)setegid(pw->pw_gid);
(void)seteuid(pw->pw_uid);
@ -140,61 +171,40 @@ main(argc, argv)
cal();
(void)seteuid(0);
}
else if ((caldir = getenv("CALENDAR_DIR")) != NULL) {
if(!chdir(caldir))
cal();
} else
} else if ((caldir = getenv("CALENDAR_DIR")) != NULL) {
if(!chdir(caldir))
cal();
} else {
cal();
}
exit(0);
}
void
cal()
static void
cal(void)
{
int printing;
char *p;
FILE *fp;
int ch;
char buf[2048 + 1];
char *line;
if ((fp = opencal()) == NULL)
return;
for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
if ((p = strchr(buf, '\n')) != NULL)
*p = '\0';
else
while ((ch = getchar()) != '\n' && ch != EOF);
if (buf[0] == '\0')
while ((line = fparseln(stdin, NULL, NULL, NULL, 0)) != NULL) {
if (line[0] == '\0')
continue;
if (buf[0] != '\t')
printing = isnow(buf) ? 1 : 0;
if (line[0] != '\t')
printing = isnow(line) ? 1 : 0;
if (printing)
(void)fprintf(fp, "%s\n", buf);
(void)fprintf(fp, "%s\n", line);
free(line);
}
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 */
int daytab[][14] = {
{ 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()
static void
settime(void)
{
time_t now;
@ -226,7 +236,7 @@ settime()
* following a line that is matched, that starts with "whitespace", is shown
* along with the matched line.
*/
int
static int
isnow(endp)
char *endp;
{
@ -272,7 +282,7 @@ isnow(endp)
return (0);
}
int
static int
getfield(p, endp, flags)
char *p, **endp;
int *flags;
@ -284,7 +294,7 @@ getfield(p, endp, flags)
!isalpha((unsigned char)*p) && *p != '*')
for (; FLDCHAR(*p); ++p)
;
continue;
if (*p == '*') { /* `*' is current month */
*flags |= F_ISMONTH;
*endp = p+1;
@ -293,32 +303,30 @@ getfield(p, endp, flags)
if (isdigit((unsigned char)*p)) {
val = strtol(p, &p, 10); /* if 0, it's failure */
for (; FLDCHAR(*p); ++p)
;
continue;
*endp = p;
return (val);
}
for (start = p; *p != '\0' && isalpha((unsigned char)*++p);)
;
continue;
savech = *p;
*p = '\0';
if ((val = getmonth(start)) != 0)
if ((val = getmonth(start)) != 0) {
*flags |= F_ISMONTH;
else if ((val = getday(start)) != 0)
} else if ((val = getday(start)) != 0) {
*flags |= F_ISDAY;
else {
} else {
*p = savech;
return (0);
}
for (*p = savech; FLDCHAR(*p); ++p)
;
continue;
*endp = p;
return (val);
}
char path[MAXPATHLEN + 1];
FILE *
opencal()
static FILE *
opencal(void)
{
int fd, pdes[2];
@ -328,9 +336,11 @@ opencal()
return (NULL);
err(1, "Cannot open `%s'", fname);
}
if (pipe(pdes) < 0)
if (pipe(pdes) < 0) {
warn("Cannot open pipe");
return (NULL);
switch (vfork()) {
}
switch (fork()) {
case -1: /* error */
(void)close(pdes[0]);
(void)close(pdes[1]);
@ -342,9 +352,10 @@ opencal()
(void)close(pdes[1]);
}
(void)close(pdes[0]);
execl(_PATH_CPP, "cpp", "-P", "-I.", "-I" _PATH_CALENDARS, NULL);
warn("execl: %s", _PATH_CPP);
_exit(1);
(void)execl(_PATH_CPP, "cpp", "-traditional", "-P", "-I.",
"-I" _PATH_CALENDARS, NULL);
err(1, "Cannot exec `%s'", _PATH_CPP);
/*NOTREACHED*/
}
/* parent -- set stdin to pipe output */
(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 */
(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 (fdopen(fd, "w+"));
}
void
static void
closecal(fp)
FILE *fp;
{
@ -378,7 +391,7 @@ closecal(fp)
goto done;
if (pipe(pdes) < 0)
goto done;
switch (vfork()) {
switch (fork()) {
case -1: /* error */
(void)close(pdes[0]);
(void)close(pdes[1]);
@ -390,10 +403,10 @@ closecal(fp)
(void)close(pdes[0]);
}
(void)close(pdes[1]);
execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
(void)execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
"\"Reminder Service\"", "-f", "root", NULL);
warn("execl: %s", _PATH_SENDMAIL);
_exit(1);
err(1, "Cannot exec `%s'", _PATH_SENDMAIL);
/*NOTREACHED*/
}
/* parent -- write to pipe input */
(void)close(pdes[0]);
@ -404,17 +417,14 @@ closecal(fp)
while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
(void)write(pdes[1], buf, nread);
(void)close(pdes[1]);
done: (void)fclose(fp);
(void)unlink(path);
while (wait(&status) >= 0);
while (wait(&status) >= 0)
continue;
}
static char *months[] = {
"jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec", NULL,
};
int
static int
getmonth(s)
char *s;
{
@ -426,11 +436,7 @@ getmonth(s)
return (0);
}
static char *days[] = {
"sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL,
};
int
static int
getday(s)
char *s;
{
@ -442,16 +448,14 @@ getday(s)
return (0);
}
void
atodays(char ch, char *optarg, unsigned short *days)
static void
atodays(int ch, char *optarg, unsigned short *days)
{
int u;
u = atoi(optarg);
if ((u < 0) || (u > 366)) {
fprintf(stderr,
"%s: warning: -%c %d out of range 0-366, ignored.\n",
getprogname(), ch, u);
warnx("-%c %d out of range 0-366, ignored.", ch, u);
} else {
*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 ISDIG2(x) (isdigit((unsigned char)(x)[0]) && isdigit((unsigned char)(x)[1]))
void
static void
getmmdd(struct tm *tp, char *ds)
{
int ok = FALSE;
@ -503,17 +507,15 @@ getmmdd(struct tm *tp, char *ds)
if (ok) {
*tp = ttm;
} else {
fprintf(stderr,
"%s: warning: can't convert %s to date, ignored.\n",
getprogname(), ds);
warnx("Can't convert `%s' to date, ignored.", ds);
usage();
}
}
void
usage()
static void
usage(void)
{
(void)fprintf(stderr, "Usage: %s [-a] [-d MMDD[[YY]YY]" \
" [-f fname] [-l days] [-w days]\n", getprogname());
(void)fprintf(stderr, "Usage: %s [-a] [-d MMDD[[YY]YY]"
" [-f fname] [-l days] [-w days]\n", getprogname());
exit(1);
}