Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/man - centralize the snprintf code.



details:   https://anonhg.NetBSD.org/src/rev/31e81dec8e93
branches:  trunk
changeset: 755141:31e81dec8e93
user:      christos <christos%NetBSD.org@localhost>
date:      Sun May 23 22:04:36 2010 +0000

description:
- centralize the snprintf code.
- use err where appropriate.
- add machclass which should be x86 when i386 and amd64 and can be specified
  in man.conf as:
        _i386   x86
        _amd64  x86
  so that we can support merged pages. Nothing uses this yet.

diffstat:

 usr.bin/man/man.c |  96 ++++++++++++++++++++++++++++--------------------------
 1 files changed, 50 insertions(+), 46 deletions(-)

diffs (199 lines):

diff -r 17df807e284d -r 31e81dec8e93 usr.bin/man/man.c
--- a/usr.bin/man/man.c Sun May 23 21:35:35 2010 +0000
+++ b/usr.bin/man/man.c Sun May 23 22:04:36 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: man.c,v 1.39 2009/10/07 08:30:31 cegger Exp $  */
+/*     $NetBSD: man.c,v 1.40 2010/05/23 22:04:36 christos Exp $        */
 
 /*
  * Copyright (c) 1987, 1993, 1994, 1995
@@ -40,7 +40,7 @@
 #if 0
 static char sccsid[] = "@(#)man.c      8.17 (Berkeley) 1/31/95";
 #else
-__RCSID("$NetBSD: man.c,v 1.39 2009/10/07 08:30:31 cegger Exp $");
+__RCSID("$NetBSD: man.c,v 1.40 2010/05/23 22:04:36 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -60,6 +60,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <util.h>
+#include <locale.h>
 
 #include "manconf.h"
 #include "pathnames.h"
@@ -98,6 +99,8 @@
 
        /* other misc stuff */
        const char *pager;      /* pager to use */
+       const char *machine;    /* machine */
+       const char *machclass;  /* machine class */
        size_t pagerlen;        /* length of the above */
 };
 
@@ -112,7 +115,9 @@
 static void     jump(char **, char *, char *);
 static int      manual(char *, struct manstate *, glob_t *);
 static void     onsig(int);
-static void     usage(void);
+static void     usage(void) __attribute__((__noreturn__));
+static void     addpath(struct manstate *, const char *, size_t, const char *);
+static const char *getclass(const char *);
 
 /*
  * main function
@@ -122,12 +127,13 @@
 {
        static struct manstate m = { 0 };       /* init to zero */
        int ch, abs_section, found;
-       const char *machine;
        ENTRY *esubd, *epath;
-       char *p, **ap, *cmd, buf[MAXPATHLEN * 2];
+       char *p, **ap, *cmd;
        size_t len;
        glob_t pg;
 
+       setprogname(argv[0]);
+       setlocale(LC_ALL, "");
        /*
         * parse command line...
         */
@@ -191,16 +197,16 @@
         */
        config(m.conffile);    /* exits on error ... */
 
-       if ((machine = getenv("MACHINE")) == NULL) {
+       if ((m.machine = getenv("MACHINE")) == NULL) {
                struct utsname utsname;
 
-               if (uname(&utsname) == -1) {
-                       perror("uname");
-                       exit(EXIT_FAILURE);
-               }
-               machine = utsname.machine;
+               if (uname(&utsname) == -1)
+                       err(EXIT_FAILURE, "uname");
+               m.machine = utsname.machine;
        }
 
+       m.machclass = getclass(m.machine);
+
        if (!m.cat && !m.how && !m.where) {  /* if we need a pager ... */
                if (!isatty(STDOUT_FILENO)) {
                        m.cat = 1;
@@ -315,38 +321,21 @@
                        len = strlen(p);
                        if (len < 1)
                                continue;
-                       TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q) {
-                               snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
-                                        p, (p[len-1] == '/') ? "" : "/",
-                                        esubd->s, machine);
-                               if (addentry(m.mymanpath, buf, 0) < 0)
-                                       errx(EXIT_FAILURE, "malloc failed");
-                       }
+                       TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q)
+                               addpath(&m, p, len, esubd->s);
                }
 
        } else {
 
                TAILQ_FOREACH(epath, &m.defaultpath->entrylist, q) {
                        /* handle trailing "/" magic here ... */
-                       if (abs_section &&
-                           epath->s[epath->len - 1] != '/') {
-
-                               (void)snprintf(buf, sizeof(buf),
-                                   "%s{/%s,}", epath->s, machine);
-                               if (addentry(m.mymanpath, buf, 0) < 0)
-                                       errx(EXIT_FAILURE, "malloc failed");
+                       if (abs_section && epath->s[epath->len - 1] != '/') {
+                               addpath(&m, "", 1, epath->s);
                                continue;
                        }
 
-                       TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q) {
-                               snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
-                                        epath->s, 
-                                        (epath->s[epath->len-1] == '/') ? ""
-                                                                        : "/",
-                                        esubd->s, machine);
-                               if (addentry(m.mymanpath, buf, 0) < 0)
-                                       errx(EXIT_FAILURE, "malloc failed");
-                       }
+                       TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q)
+                               addpath(&m, epath->s, epath->len, esubd->s);
                }
 
        }
@@ -363,14 +352,8 @@
                        len = strlen(p);
                        if (len < 1)
                                continue;
-                       TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q) {
-                               snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
-                                        p, (p[len-1] == '/') ? "" : "/",
-                                        esubd->s, machine);
-                               /* add at front */
-                               if (addentry(m.mymanpath, buf, 1) < 0)
-                                       errx(EXIT_FAILURE, "malloc failed");
-                       }
+                       TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q)
+                               addpath(&m, p, len, esubd->s);
                }
 
        }
@@ -917,8 +900,7 @@
        for (; *arg; ++arg)
                arg[0] = arg[1];
        execvp(name, argv);
-       (void)fprintf(stderr, "%s: Command not found.\n", name);
-       exit(EXIT_FAILURE);
+       err(EXIT_FAILURE, "Cannot execute `%s'", name);
 }
 
 /* 
@@ -967,6 +949,28 @@
        return rval;
 }
 
+static const char *
+getclass(const char *machine)
+{
+       char buf[BUFSIZ];
+       TAG *t;
+       snprintf(buf, sizeof(buf), "_%s", machine);
+       t = gettag(buf, 0);
+       return t != NULL && t->s ? t->s : NULL;
+}
+
+static void
+addpath(struct manstate *m, const char *dir, size_t len, const char *sub)
+{
+       char buf[2 * MAXPATHLEN + 1];
+       (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,%s%s%s}",
+            dir, (dir[len - 1] == '/') ? "" : "/", sub, m->machine,
+            m->machclass ? "/" : "", m->machclass ? m->machclass : "",
+            m->machclass ? "," : "");
+       if (addentry(m->mymanpath, buf, 0) < 0)
+               errx(EXIT_FAILURE, "malloc failed");
+}
+
 /*
  * usage --
  *     print usage message and die
@@ -974,10 +978,10 @@
 static void
 usage(void)
 {
-       (void)fprintf(stderr, "usage: %s [-acw|-h] [-C cfg] [-M path] "
+       (void)fprintf(stderr, "Usage: %s [-acw|-h] [-C cfg] [-M path] "
            "[-m path] [-S srch] [[-s] sect] name ...\n", getprogname());
        (void)fprintf(stderr, 
-           "usage: %s -k [-C cfg] [-M path] [-m path] keyword ...\n", 
+           "Usage: %s -k [-C cfg] [-M path] [-m path] keyword ...\n", 
            getprogname());
        exit(EXIT_FAILURE);
 }



Home | Main Index | Thread Index | Old Index