Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/make make(1): clean up code in var.c



details:   https://anonhg.NetBSD.org/src/rev/a2c9b3c18179
branches:  trunk
changeset: 1011988:a2c9b3c18179
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Jul 19 10:28:44 2020 +0000

description:
make(1): clean up code in var.c

* use canonical comparison to zero in more cases
* omit unnecessary snprintf for string literal concatenation
* omit a few redundant parentheses and braces

diffstat:

 usr.bin/make/var.c |  109 +++++++++++++++++++++++-----------------------------
 1 files changed, 49 insertions(+), 60 deletions(-)

diffs (truncated from 381 to 300 lines):

diff -r 8b45eb1069f7 -r a2c9b3c18179 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Sun Jul 19 09:30:08 2020 +0000
+++ b/usr.bin/make/var.c        Sun Jul 19 10:28:44 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.256 2020/07/19 09:26:18 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.257 2020/07/19 10:28:44 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.256 2020/07/19 09:26:18 rillig Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.257 2020/07/19 10:28:44 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)var.c      8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: var.c,v 1.256 2020/07/19 09:26:18 rillig Exp $");
+__RCSID("$NetBSD: var.c,v 1.257 2020/07/19 10:28:44 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -339,29 +339,29 @@
     if (*name == '.' && isupper((unsigned char) name[1])) {
        switch (name[1]) {
        case 'A':
-           if (!strcmp(name, ".ALLSRC"))
+           if (strcmp(name, ".ALLSRC") == 0)
                name = ALLSRC;
-           if (!strcmp(name, ".ARCHIVE"))
+           if (strcmp(name, ".ARCHIVE") == 0)
                name = ARCHIVE;
            break;
        case 'I':
-           if (!strcmp(name, ".IMPSRC"))
+           if (strcmp(name, ".IMPSRC") == 0)
                name = IMPSRC;
            break;
        case 'M':
-           if (!strcmp(name, ".MEMBER"))
+           if (strcmp(name, ".MEMBER") == 0)
                name = MEMBER;
            break;
        case 'O':
-           if (!strcmp(name, ".OODATE"))
+           if (strcmp(name, ".OODATE") == 0)
                name = OODATE;
            break;
        case 'P':
-           if (!strcmp(name, ".PREFIX"))
+           if (strcmp(name, ".PREFIX") == 0)
                name = PREFIX;
            break;
        case 'T':
-           if (!strcmp(name, ".TARGET"))
+           if (strcmp(name, ".TARGET") == 0)
                name = TARGET;
            break;
        }
@@ -396,7 +396,7 @@
        char *env;
 
        if ((env = getenv(name)) != NULL) {
-           int         len;
+           int len;
 
            v = bmake_malloc(sizeof(Var));
            v->name = bmake_strdup(name);
@@ -449,7 +449,7 @@
 static Boolean
 VarFreeEnv(Var *v, Boolean destroy)
 {
-    if ((v->flags & VAR_FROM_ENV) == 0)
+    if (!(v->flags & VAR_FROM_ENV))
        return FALSE;
     free(v->name);
     Buf_Destroy(&v->val, destroy);
@@ -482,7 +482,7 @@
 
     v = bmake_malloc(sizeof(Var));
 
-    len = val ? strlen(val) : 0;
+    len = val != NULL ? strlen(val) : 0;
     Buf_Init(&v->val, len + 1);
     Buf_AddBytes(&v->val, len, val);
 
@@ -491,7 +491,7 @@
     h = Hash_CreateEntry(&ctxt->context, name, NULL);
     Hash_SetValue(h, v);
     v->name = h->name;
-    if (DEBUG(VAR) && (ctxt->flags & INTERNAL) == 0) {
+    if (DEBUG(VAR) && !(ctxt->flags & INTERNAL)) {
        fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val);
     }
 }
@@ -512,24 +512,21 @@
     Hash_Entry           *ln;
     char *cp;
 
-    if (strchr(name, '$')) {
+    if (strchr(name, '$') != NULL) {
        cp = Var_Subst(NULL, name, VAR_GLOBAL, VARF_WANTRES);
     } else {
-       cp = (char *)name;
+       cp = UNCONST(name);
     }
     ln = Hash_FindEntry(&ctxt->context, cp);
     if (DEBUG(VAR)) {
        fprintf(debug_file, "%s:delete %s%s\n",
            ctxt->name, cp, ln ? "" : " (not found)");
     }
-    if (cp != name) {
+    if (cp != name)
        free(cp);
-    }
     if (ln != NULL) {
-       Var       *v;
-
-       v = (Var *)Hash_GetValue(ln);
-       if ((v->flags & VAR_EXPORTED)) {
+       Var *v = (Var *)Hash_GetValue(ln);
+       if (v->flags & VAR_EXPORTED) {
            unsetenv(v->name);
        }
        if (strcmp(MAKE_EXPORTED, v->name) == 0) {
@@ -578,9 +575,8 @@
        }
     }
     v = VarFind(name, VAR_GLOBAL, 0);
-    if (v == NULL) {
+    if (v == NULL)
        return 0;
-    }
     if (!parent &&
        (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) {
        return 0;                       /* nothing to do */
@@ -673,9 +669,8 @@
        val = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
        if (*val) {
            av = brk_string(val, &ac, FALSE, &as);
-           for (i = 0; i < ac; i++) {
+           for (i = 0; i < ac; i++)
                Var_Export1(av[i], 0);
-           }
            free(as);
            free(av);
        }
@@ -748,11 +743,11 @@
 }
 
 
+extern char **environ;
+
 /*
  * This is called when .unexport[-env] is seen.
  */
-extern char **environ;
-
 void
 Var_UnExport(char *str)
 {
@@ -762,9 +757,8 @@
     Boolean unexport_env;
     int n;
 
-    if (!str || !str[0]) {
+    if (str == NULL || str[0] == '\0')
        return;                 /* assert? */
-    }
 
     vlist = NULL;
 
@@ -802,10 +796,8 @@
 
     if (!vlist) {
        /* Using .MAKE.EXPORTED */
-       n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}");
-       if (n < (int)sizeof(tmp)) {
-           vlist = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
-       }
+       vlist = Var_Subst(NULL, "${" MAKE_EXPORTED ":O:u}", VAR_GLOBAL,
+                         VARF_WANTRES);
     }
     if (vlist) {
        Var *v;
@@ -820,7 +812,8 @@
            if (!v)
                continue;
            if (!unexport_env &&
-               (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) {
+               (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED)
+           {
                unsetenv(v->name);
            }
            v->flags &= ~(VAR_EXPORTED|VAR_REEXPORT);
@@ -863,7 +856,7 @@
      */
     if (strchr(name, '$') != NULL) {
        expanded_name = Var_Subst(NULL, name, ctxt, VARF_WANTRES);
-       if (expanded_name[0] == 0) {
+       if (expanded_name[0] == '\0') {
            if (DEBUG(VAR)) {
                fprintf(debug_file, "Var_Set(\"%s\", \"%s\", ...) "
                        "name expands to empty string - ignored\n",
@@ -1006,7 +999,7 @@
 
     if (strchr(name, '$') != NULL) {
        expanded_name = Var_Subst(NULL, name, ctxt, VARF_WANTRES);
-       if (expanded_name[0] == 0) {
+       if (expanded_name[0] == '\0') {
            if (DEBUG(VAR)) {
                fprintf(debug_file, "Var_Append(\"%s\", \"%s\", ...) "
                        "name expands to empty string - ignored\n",
@@ -1069,14 +1062,12 @@
     Var                  *v;
     char          *cp;
 
-    if ((cp = strchr(name, '$')) != NULL) {
+    if ((cp = strchr(name, '$')) != NULL)
        cp = Var_Subst(NULL, name, ctxt, VARF_WANTRES);
-    }
     v = VarFind(cp ? cp : name, ctxt, FIND_CMD|FIND_GLOBAL|FIND_ENV);
     free(cp);
-    if (v == NULL) {
+    if (v == NULL)
        return FALSE;
-    }
 
     (void)VarFreeEnv(v, TRUE);
     return TRUE;
@@ -1101,14 +1092,12 @@
 char *
 Var_Value(const char *name, GNode *ctxt, char **frp)
 {
-    Var *v;
-
-    v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
+    Var *v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
     *frp = NULL;
     if (v == NULL)
        return NULL;
 
-    char *p = (Buf_GetAll(&v->val, NULL));
+    char *p = Buf_GetAll(&v->val, NULL);
     if (VarFreeEnv(v, FALSE))
        *frp = p;
     return p;
@@ -1157,7 +1146,7 @@
     const char *slash = strrchr(word, '/');
     const char *base = slash != NULL ? slash + 1 : word;
 
-    if (addSpace && vpstate->varSpace)
+    if (addSpace && vpstate->varSpace != '\0')
        Buf_AddByte(buf, vpstate->varSpace);
     Buf_AddBytes(buf, strlen(base), base);
     return TRUE;
@@ -1174,7 +1163,7 @@
     if (dot == NULL)
        return addSpace;
 
-    if (addSpace && vpstate->varSpace)
+    if (addSpace && vpstate->varSpace != '\0')
        Buf_AddByte(buf, vpstate->varSpace);
     Buf_AddBytes(buf, strlen(dot + 1), dot + 1);
     return TRUE;
@@ -1190,7 +1179,7 @@
     char *dot = strrchr(word, '.');
     size_t len = dot != NULL ? (size_t)(dot - word) : strlen(word);
 
-    if (addSpace && vpstate->varSpace)
+    if (addSpace && vpstate->varSpace != '\0')
        Buf_AddByte(buf, vpstate->varSpace);
     Buf_AddBytes(buf, len, word);
     return TRUE;
@@ -1208,7 +1197,7 @@
        fprintf(debug_file, "VarMatch [%s] [%s]\n", word, pattern);
     if (!Str_Match(word, pattern))
        return addSpace;
-    if (addSpace && vpstate->varSpace)
+    if (addSpace && vpstate->varSpace != '\0')
        Buf_AddByte(buf, vpstate->varSpace);
     Buf_AddBytes(buf, strlen(word), word);
     return TRUE;
@@ -1328,7 +1317,7 @@
     Boolean hasPercent;
     VarPattern *pat = data;
 
-    if (addSpace && vpstate->varSpace)
+    if (addSpace && vpstate->varSpace != '\0')
        Buf_AddByte(buf, vpstate->varSpace);
 
     if ((ptr = Str_SYSVMatch(word, pat->lhs, &len, &hasPercent)) != NULL) {
@@ -1353,7 +1342,7 @@
     const char *pattern = data;
     if (Str_Match(word, pattern))
        return addSpace;



Home | Main Index | Thread Index | Old Index