Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/calendar - knf



details:   https://anonhg.NetBSD.org/src/rev/5928fb13f13c
branches:  trunk
changeset: 518702:5928fb13f13c
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Dec 04 15:55:32 2001 +0000

description:
- knf
- 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]

diffstat:

 usr.bin/calendar/Makefile   |    4 +-
 usr.bin/calendar/calendar.c |  214 ++++++++++++++++++++++---------------------
 2 files changed, 111 insertions(+), 107 deletions(-)

diffs (truncated from 424 to 300 lines):

diff -r 55c703bee5bb -r 5928fb13f13c usr.bin/calendar/Makefile
--- a/usr.bin/calendar/Makefile Tue Dec 04 15:27:35 2001 +0000
+++ b/usr.bin/calendar/Makefile Tue Dec 04 15:55:32 2001 +0000
@@ -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 @@
 FILESDIR=/usr/share/calendar
 FILES!= echo ${.CURDIR}/calendars/calendar.*
 .endif
+DPADD+=${LIBUTIL}
+LDADD+=-lutil
 
 .include <bsd.prog.mk>
diff -r 55c703bee5bb -r 5928fb13f13c usr.bin/calendar/calendar.c
--- a/usr.bin/calendar/calendar.c       Tue Dec 04 15:27:35 2001 +0000
+++ b/usr.bin/calendar/calendar.c       Tue Dec 04 15:55:32 2001 +0000
@@ -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 @@
 #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 @@
 #include <time.h>
 #include <tzfile.h>
 #include <unistd.h>
+#include <util.h>
 
 #include "pathnames.h"
 
@@ -73,23 +74,53 @@
 #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];
+
+/* 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];
 
-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));
+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 @@
                usage();
 
        settime();
-       if (doall)
+       if (doall) {
                while ((pw = getpwent()) != NULL) {
                        (void)setegid(pw->pw_gid);
                        (void)seteuid(pw->pw_uid);
@@ -140,61 +171,40 @@
                                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 @@
  * 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 @@
        return (0);
 }
 
-int
+static int
 getfield(p, endp, flags)
        char *p, **endp;
        int *flags;
@@ -284,7 +294,7 @@
     !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 @@
        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 @@
                        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 @@
                        (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 @@
 
        /* set output to a temporary file, so if no output don't send mail */



Home | Main Index | Thread Index | Old Index