Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/make Add .export-literal to avoid the need for $$ da...



details:   https://anonhg.NetBSD.org/src/rev/e1c6197cdf45
branches:  trunk
changeset: 343645:e1c6197cdf45
user:      sjg <sjg%NetBSD.org@localhost>
date:      Thu Feb 18 20:25:08 2016 +0000

description:
Add .export-literal to avoid the need for $$ dance when trying to put
unexpanded variables into environment.

Reviewed by: christos

diffstat:

 usr.bin/make/main.c                    |  67 +++++++++++++++++++++------------
 usr.bin/make/make.1                    |   8 +++-
 usr.bin/make/nonints.h                 |   3 +-
 usr.bin/make/unit-tests/export-env.exp |   2 +
 usr.bin/make/unit-tests/export-env.mk  |   9 +++-
 usr.bin/make/var.c                     |  30 +++++++++-----
 6 files changed, 77 insertions(+), 42 deletions(-)

diffs (288 lines):

diff -r 2f9471cb0ce0 -r e1c6197cdf45 usr.bin/make/main.c
--- a/usr.bin/make/main.c       Thu Feb 18 18:29:14 2016 +0000
+++ b/usr.bin/make/main.c       Thu Feb 18 20:25:08 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.239 2016/02/18 18:29:14 christos Exp $      */
+/*     $NetBSD: main.c,v 1.240 2016/02/18 20:25:08 sjg Exp $   */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,7 +69,7 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: main.c,v 1.239 2016/02/18 18:29:14 christos Exp $";
+static char rcsid[] = "$NetBSD: main.c,v 1.240 2016/02/18 20:25:08 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
@@ -81,7 +81,7 @@
 #if 0
 static char sccsid[] = "@(#)main.c     8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: main.c,v 1.239 2016/02/18 18:29:14 christos Exp $");
+__RCSID("$NetBSD: main.c,v 1.240 2016/02/18 20:25:08 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -1950,6 +1950,44 @@
     return fd;
 }
 
+/*
+ * Convert a string representation of a boolean.
+ * Anything that looks like "No", "False", "Off", "0" etc,
+ * is FALSE, otherwise TRUE.
+ */
+Boolean
+s2Boolean(const char *s, Boolean bf)
+{
+    if (s) {
+       switch(*s) {
+       case '\0':                      /* not set - the default wins */
+           break;
+       case '0':
+       case 'F':
+       case 'f':
+       case 'N':
+       case 'n':
+           bf = FALSE;
+           break;
+       case 'O':
+       case 'o':
+           switch (s[1]) {
+           case 'F':
+           case 'f':
+               bf = FALSE;
+               break;
+           default:
+               bf = TRUE;
+               break;
+           }
+           break;
+       default:
+           bf = TRUE;
+           break;
+       }
+    }
+    return (bf);
+}
 
 /*
  * Return a Boolean based on setting of a knob.
@@ -1968,28 +2006,7 @@
        cp = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
 
        if (cp) {
-           switch(*cp) {
-           case '\0':                  /* not set - the default wins */
-               break;
-           case '0':
-           case 'f':
-           case 'n':
-               bf = FALSE;
-               break;
-           case 'o':
-               switch (cp[1]) {
-               case 'f':
-                   bf = FALSE;
-                   break;
-               default:
-                   bf = TRUE;
-                   break;
-               }
-               break;
-           default:
-               bf = TRUE;
-               break;
-           }
+           bf = s2Boolean(cp, bf);
            free(cp);
        }
     }
diff -r 2f9471cb0ce0 -r e1c6197cdf45 usr.bin/make/make.1
--- a/usr.bin/make/make.1       Thu Feb 18 18:29:14 2016 +0000
+++ b/usr.bin/make/make.1       Thu Feb 18 20:25:08 2016 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: make.1,v 1.251 2016/02/18 06:18:58 sjg Exp $
+.\"    $NetBSD: make.1,v 1.252 2016/02/18 20:25:08 sjg Exp $
 .\"
 .\" Copyright (c) 1990, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"    from: @(#)make.1        8.4 (Berkeley) 3/19/94
 .\"
-.Dd February 17, 2016
+.Dd February 18, 2016
 .Dt MAKE 1
 .Os
 .Sh NAME
@@ -1579,6 +1579,10 @@
 used by
 .Nm
 internally.
+.It Ic .export-literal Ar variable ...
+The same as
+.Ql .export-env ,
+except that variables in the value are not expanded.
 .It Ic .info Ar message
 The message is printed along with the name of the makefile and line number.
 .It Ic .undef Ar variable
diff -r 2f9471cb0ce0 -r e1c6197cdf45 usr.bin/make/nonints.h
--- a/usr.bin/make/nonints.h    Thu Feb 18 18:29:14 2016 +0000
+++ b/usr.bin/make/nonints.h    Thu Feb 18 20:25:08 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nonints.h,v 1.71 2016/02/18 18:29:14 christos Exp $    */
+/*     $NetBSD: nonints.h,v 1.72 2016/02/18 20:25:08 sjg Exp $ */
 
 /*-
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -118,6 +118,7 @@
 int eunlink(const char *);
 void execError(const char *, const char *);
 char *getTmpdir(void);
+Boolean s2Boolean(const char *, Boolean);
 Boolean getBoolean(const char *, Boolean);
 
 /* parse.c */
diff -r 2f9471cb0ce0 -r e1c6197cdf45 usr.bin/make/unit-tests/export-env.exp
--- a/usr.bin/make/unit-tests/export-env.exp    Thu Feb 18 18:29:14 2016 +0000
+++ b/usr.bin/make/unit-tests/export-env.exp    Thu Feb 18 20:25:08 2016 +0000
@@ -2,8 +2,10 @@
 UT_TEST=export-env.mk
 UT_ENV=not-exported
 UT_EXP=not-exported
+UT_LIT=literal export-env.mk
 env:
 UT_TEST=export-env.mk
 UT_ENV=exported
 UT_EXP=exported
+UT_LIT=literal ${UT_TEST}
 exit status 0
diff -r 2f9471cb0ce0 -r e1c6197cdf45 usr.bin/make/unit-tests/export-env.mk
--- a/usr.bin/make/unit-tests/export-env.mk     Thu Feb 18 18:29:14 2016 +0000
+++ b/usr.bin/make/unit-tests/export-env.mk     Thu Feb 18 20:25:08 2016 +0000
@@ -1,4 +1,4 @@
-# $Id: export-env.mk,v 1.1 2014/08/21 13:44:51 apb Exp $
+# $Id: export-env.mk,v 1.2 2016/02/18 20:25:08 sjg Exp $
 
 # our normal .export, subsequent changes affect the environment
 UT_TEST=this
@@ -15,9 +15,12 @@
 export UT_EXP=exported
 UT_EXP=not-exported
 
+UT_LIT= literal ${UT_TEST}
+.export-literal UT_LIT
+
 all:
-       @echo make:; ${UT_TEST UT_ENV UT_EXP:L:@v@echo $v=${$v};@}
-       @echo env:; ${UT_TEST UT_ENV UT_EXP:L:@v@echo $v=$${$v};@}
+       @echo make:; ${UT_TEST UT_ENV UT_EXP UT_LIT:L:@v@echo $v=${$v};@}
+       @echo env:; ${UT_TEST UT_ENV UT_EXP UT_LIT:L:@v@echo $v=$${$v};@}
 
 
 
diff -r 2f9471cb0ce0 -r e1c6197cdf45 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Thu Feb 18 18:29:14 2016 +0000
+++ b/usr.bin/make/var.c        Thu Feb 18 20:25:08 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.202 2016/02/18 18:29:14 christos Exp $       */
+/*     $NetBSD: var.c,v 1.203 2016/02/18 20:25:08 sjg Exp $    */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.202 2016/02/18 18:29:14 christos Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.203 2016/02/18 20:25:08 sjg 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.202 2016/02/18 18:29:14 christos Exp $");
+__RCSID("$NetBSD: var.c,v 1.203 2016/02/18 20:25:08 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -217,7 +217,11 @@
  * We pass this to Var_Export when doing the initial export
  * or after updating an exported var.
  */
-#define VAR_EXPORT_PARENT 1
+#define VAR_EXPORT_PARENT      1
+/*
+ * We pass this to Var_Export1 to tell it to leave the value alone.
+ */
+#define VAR_EXPORT_LITERAL     2
 
 /* Var*Pattern flags */
 #define VAR_SUB_GLOBAL 0x01    /* Apply substitution globally */
@@ -581,12 +585,13 @@
  * We only manipulate flags of vars if 'parent' is set.
  */
 static int
-Var_Export1(const char *name, int parent)
+Var_Export1(const char *name, int flags)
 {
     char tmp[BUFSIZ];
     Var *v;
     char *val = NULL;
     int n;
+    int parent = (flags & VAR_EXPORT_PARENT);
 
     if (*name == '.')
        return 0;                       /* skip internals */
@@ -614,7 +619,7 @@
        return 0;                       /* nothing to do */
     }
     val = Buf_GetAll(&v->val, NULL);
-    if (strchr(val, '$')) {
+    if ((flags & VAR_EXPORT_LITERAL) == 0 && strchr(val, '$')) {
        if (parent) {
            /*
             * Flag this as something we need to re-export.
@@ -726,7 +731,7 @@
     char *val;
     char **av;
     char *as;
-    int track;
+    int flags;
     int ac;
     int i;
 
@@ -735,11 +740,14 @@
        return;
     }
 
+    flags = 0;
     if (strncmp(str, "-env", 4) == 0) {
-       track = 0;
        str += 4;
+    } else if (strncmp(str, "-literal", 8) == 0) {
+       str += 8;
+       flags |= VAR_EXPORT_LITERAL;
     } else {
-       track = VAR_EXPORT_PARENT;
+       flags |= VAR_EXPORT_PARENT;
     }
     val = Var_Subst(NULL, str, VAR_GLOBAL, VARF_WANTRES);
     if (*val) {
@@ -761,10 +769,10 @@
                    continue;
                }
            }
-           if (Var_Export1(name, track)) {
+           if (Var_Export1(name, flags)) {
                if (VAR_EXPORTED_ALL != var_exportedVars)
                    var_exportedVars = VAR_EXPORTED_YES;
-               if (isExport && track) {
+               if (isExport && (flags & VAR_EXPORT_PARENT)) {
                    Var_Append(MAKE_EXPORTED, name, VAR_GLOBAL);
                }
            }



Home | Main Index | Thread Index | Old Index