Source-Changes-HG archive

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

[src/trunk]: src/sbin/sysctl Some lint cleaning, strip leading (and sometimes...



details:   https://anonhg.NetBSD.org/src/rev/f6a563e9a2a0
branches:  trunk
changeset: 565868:f6a563e9a2a0
user:      atatat <atatat%NetBSD.org@localhost>
date:      Thu Apr 22 03:56:31 2004 +0000

description:
Some lint cleaning, strip leading (and sometimes trailing) whitespace
from values to be assigned when processing a file.  Clean up error
reporting (print the name of the file and the line number), and tidy
some numeric conversions.  Continue after most "errors" when
processing a -f argument.

Addresses PR bin/25115.

diffstat:

 sbin/sysctl/sysctl.c |  365 +++++++++++++++++++++++++++-----------------------
 1 files changed, 200 insertions(+), 165 deletions(-)

diffs (truncated from 766 to 300 lines):

diff -r c318d620141c -r f6a563e9a2a0 sbin/sysctl/sysctl.c
--- a/sbin/sysctl/sysctl.c      Thu Apr 22 03:47:58 2004 +0000
+++ b/sbin/sysctl/sysctl.c      Thu Apr 22 03:56:31 2004 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sysctl.c,v 1.91 2004/04/14 05:13:50 atatat Exp $ */
+/*     $NetBSD: sysctl.c,v 1.92 2004/04/22 03:56:31 atatat Exp $ */
 
 /*-
  * Copyright (c) 2003 The NetBSD Foundation, Inc.
@@ -72,7 +72,7 @@
 #if 0
 static char sccsid[] = "@(#)sysctl.c   8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: sysctl.c,v 1.91 2004/04/14 05:13:50 atatat Exp $");
+__RCSID("$NetBSD: sysctl.c,v 1.92 2004/04/22 03:56:31 atatat Exp $");
 #endif
 #endif /* not lint */
 
@@ -143,7 +143,12 @@
 static void parse_describe(char *);
 static void getdesc1(int *, u_int, struct sysctlnode *);
 static void getdesc(int *, u_int, struct sysctlnode *);
+static void trim_whitespace(char *, int);
 static void sysctlerror(int);
+static void sysctlparseerror(u_int, const char *);
+static void sysctlperror(const char *, ...);
+#define EXIT(n) do { \
+       if (fn == NULL) exit(n); else return; } while (/*CONSTCOND*/0)
 
 /*
  * "borrowed" from libc:sysctlgetmibinfo.c
@@ -235,6 +240,8 @@
 };
 
 int    Aflag, aflag, dflag, Mflag, nflag, qflag, rflag, wflag, xflag;
+size_t nr;
+char   *fn;
 int    req;
 FILE   *warnfp = stderr;
 
@@ -255,7 +262,6 @@
 int
 main(int argc, char *argv[])
 {
-       char *fn = NULL;
        int name[CTL_MAXNAME];
        int ch;
 
@@ -333,7 +339,8 @@
                if (fp == NULL) {
                        err(1, "%s", fn);
                } else {
-                       while ((l = fparseln(fp, NULL, NULL, NULL, 0)) != NULL)
+                       nr = 0;
+                       while ((l = fparseln(fp, NULL, &nr, NULL, 0)) != NULL)
                        {
                                if (*l) {
                                        parse(l);
@@ -739,10 +746,10 @@
                        value[-1] = '=';
                if (strncmp(key + 2, "create", 6) == 0 &&
                    (key[8] == '=' || key[8] == sep[0]))
-                       parse_create(key + 8 + (key[8] == '='));
+                       parse_create(key + 8 + (key[8] == '=' ? 1 : 0));
                else if (strncmp(key + 2, "destroy", 7) == 0 &&
                         (key[9] == '=' || key[9] == sep[0]))
-                       parse_destroy(key + 9 + (key[9] == '='));
+                       parse_destroy(key + 9 + (key[9] == '=' ? 1 : 0));
                else if (strncmp(key + 2, "describe", 8) == 0 &&
                         (key[10] == '=' || key[10] == sep[0])) {
                        key += 10 + (key[10] == '=');
@@ -755,8 +762,7 @@
                        }
                }
                else
-                       fprintf(warnfp, "%s: unable to parse '%s'\n",
-                               getprogname(), key);
+                       sysctlperror("unable to parse '%s'\n", key);
                return;
        }
 
@@ -766,9 +772,8 @@
 
        if (sysctlgetmibinfo(key, &name[0], &namelen, gsname, &sz, &node,
                             SYSCTL_VERSION) == -1) {
-               fprintf(warnfp, "%s: %s level name '%s' in '%s' is invalid\n",
-                       getprogname(), lname[namelen], gsname, l);
-               exit(1);
+               sysctlparseerror(namelen, l);
+               EXIT(1);
        }
 
        type = SYSCTL_TYPE(node->sysctl_flags);
@@ -783,9 +788,11 @@
                return;
        }
 
+       if (fn)
+               trim_whitespace(value, 1);
+
        if (!wflag) {
-               fprintf(warnfp, "%s: Must specify -w to set variables\n",
-                       getprogname());
+               sysctlperror("Must specify -w to set variables\n");
                exit(1);
        }
 
@@ -851,12 +858,11 @@
        char *nname, *key, *value, *data, *addr, *c, *t;
        int name[CTL_MAXNAME], i, rc, method, flags, rw;
        u_int namelen, type;
-       u_quad_t q;
-       long li, lo;
+       u_quad_t uq;
+       quad_t q;
 
        if (!wflag) {
-               fprintf(warnfp, "%s: Must specify -w to create nodes\n",
-                       getprogname());
+               sysctlperror("Must specify -w to create nodes\n");
                exit(1);
        }
 
@@ -879,7 +885,7 @@
         * misc stuff used when constructing
         */
        i = 0;
-       q = 0;
+       uq = 0;
        key = NULL;
        value = NULL;
 
@@ -933,29 +939,29 @@
                         * (or if the address is invalid).
                         */
                        if (method != 0) {
-                               fprintf(warnfp,
-                                   "%s: %s: already have %s for new node\n",
-                                   getprogname(), nname,
+                               sysctlperror(
+                                   "%s: already have %s for new node\n",
+                                   nname,
                                    method == CTL_CREATE ? "addr" : "symbol");
-                               exit(1);
+                               EXIT(1);
                        }
                        errno = 0;
                        addr = (void*)strtoul(value, &t, 0);
                        if (*t != '\0' || errno != 0) {
-                               fprintf(warnfp,
-                                   "%s: %s: '%s' is not a valid address\n",
-                                   getprogname(), nname, value);
-                               exit(1);
+                               sysctlperror(
+                                   "%s: '%s' is not a valid address\n",
+                                   nname, value);
+                               EXIT(1);
                        }
                        method = CTL_CREATE;
                }
                else if (strcmp(key, "symbol") == 0) {
                        if (method != 0) {
-                               fprintf(warnfp,
-                                   "%s: %s: already have %s for new node\n",
-                                   getprogname(), nname,
+                               sysctlperror(
+                                   "%s: already have %s for new node\n",
+                                   nname,
                                    method == CTL_CREATE ? "addr" : "symbol");
-                               exit(1);
+                               EXIT(1);
                        }
                        addr = value;
                        method = CTL_CREATESYM;
@@ -976,10 +982,10 @@
                        else if (strcmp(value, "struct") == 0)
                                type = CTLTYPE_STRUCT;
                        else {
-                               fprintf(warnfp,
-                                       "%s: %s: '%s' is not a valid type\n",
-                                       getprogname(), nname, value);
-                               exit(1);
+                               sysctlperror(
+                                       "%s: '%s' is not a valid type\n",
+                                       nname, value);
+                               EXIT(1);
                        }
                }
                else if (strcmp(key, "size") == 0) {
@@ -991,23 +997,23 @@
                         */
                        sz = strtoul(value, &t, 0);
                        if (*t != '\0' || errno != 0) {
-                               fprintf(warnfp,
-                                       "%s: %s: '%s' is not a valid size\n",
-                                       getprogname(), nname, value);
-                               exit(1);
+                               sysctlperror(
+                                       "%s: '%s' is not a valid size\n",
+                                       nname, value);
+                               EXIT(1);
                        }
                }
                else if (strcmp(key, "n") == 0) {
                        errno = 0;
-                       li = strtol(value, &t, 0);
-                       node.sysctl_num = li;
-                       lo = node.sysctl_num;
-                       if (*t != '\0' || errno != 0 || li != lo || lo < 0) {
-                               fprintf(warnfp,
-                                   "%s: %s: '%s' is not a valid mib number\n",
-                                   getprogname(), nname, value);
-                               exit(1);
+                       q = strtoq(value, &t, 0);
+                       if (*t != '\0' || errno != 0 ||
+                           q < INT_MIN || q > UINT_MAX) {
+                               sysctlperror(
+                                   "%s: '%s' is not a valid mib number\n",
+                                   nname, value);
+                               EXIT(1);
                        }
+                       node.sysctl_num = (int)q;
                }
                else if (strcmp(key, "flags") == 0) {
                        t = value;
@@ -1045,18 +1051,18 @@
                                        rw = CTLFLAG_READWRITE;
                                        break;
                                default:
-                                       fprintf(warnfp,
-                                          "%s: %s: '%c' is not a valid flag\n",
-                                           getprogname(), nname, *t);
-                                       exit(1);
+                                       sysctlperror(
+                                          "%s: '%c' is not a valid flag\n",
+                                           nname, *t);
+                                       EXIT(1);
                                }
                                t++;
                        }
                }
                else {
-                       fprintf(warnfp, "%s: %s: unrecognized keyword '%s'\n",
-                               getprogname(), nname, key);
-                       exit(1);
+                       sysctlperror("%s: unrecognized keyword '%s'\n",
+                                    nname, key);
+                       EXIT(1);
                }
        }
 
@@ -1073,24 +1079,24 @@
         */
        if (data != NULL) {
                if (addr != NULL) {
-                       fprintf(warnfp,
-                               "%s: %s: cannot specify both value and "
-                               "address\n", getprogname(), nname);
-                       exit(1);
+                       sysctlperror(
+                               "%s: cannot specify both value and "
+                               "address\n", nname);
+                       EXIT(1);
                }
 
                switch (type) {
                case CTLTYPE_INT:
                        errno = 0;
-                       li = strtol(data, &t, 0);
-                       i = li;
-                       lo = i;
-                       if (*t != '\0' || errno != 0 || li != lo || lo < 0) {
-                               fprintf(warnfp,
-                                       "%s: %s: '%s' is not a valid integer\n",
-                                       getprogname(), nname, value);
-                               exit(1);
+                       q = strtoq(data, &t, 0);
+                       if (*t != '\0' || errno != 0 ||
+                               q < INT_MIN || q > UINT_MAX) {
+                               sysctlperror(
+                                   "%s: '%s' is not a valid integer\n",
+                                   nname, value);
+                               EXIT(1);
                        }
+                       i = (int)q;
                        if (!(flags & CTLFLAG_OWNDATA)) {
                                flags |= CTLFLAG_IMMEDIATE;
                                node.sysctl_idata = i;
@@ -1106,35 +1112,34 @@
                        if (sz == 0)
                                sz = strlen(data) + 1;
                        else if (sz < strlen(data) + 1) {
-                               fprintf(warnfp, "%s: %s: ignoring size=%zu for "
+                               sysctlperror("%s: ignoring size=%zu for "
                                        "string node, too small for given "
-                                       "value\n", getprogname(), nname, sz);
+                                       "value\n", nname, sz);
                                sz = strlen(data) + 1;
                        }
                        break;



Home | Main Index | Thread Index | Old Index