[BACK]Return to var.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / usr.bin / make

Annotation of src/usr.bin/make/var.c, Revision 1.209

1.209   ! sjg         1: /*     $NetBSD: var.c,v 1.208 2016/06/03 01:21:59 sjg Exp $    */
1.11      christos    2:
1.1       cgd         3: /*
1.15      christos    4:  * Copyright (c) 1988, 1989, 1990, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
1.80      agc         6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * Adam de Boor.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. Neither the name of the University nor the names of its contributors
                     19:  *    may be used to endorse or promote products derived from this software
                     20:  *    without specific prior written permission.
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     23:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     24:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     26:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     32:  * SUCH DAMAGE.
                     33:  */
                     34:
                     35: /*
1.1       cgd        36:  * Copyright (c) 1989 by Berkeley Softworks
                     37:  * All rights reserved.
                     38:  *
                     39:  * This code is derived from software contributed to Berkeley by
                     40:  * Adam de Boor.
                     41:  *
                     42:  * Redistribution and use in source and binary forms, with or without
                     43:  * modification, are permitted provided that the following conditions
                     44:  * are met:
                     45:  * 1. Redistributions of source code must retain the above copyright
                     46:  *    notice, this list of conditions and the following disclaimer.
                     47:  * 2. Redistributions in binary form must reproduce the above copyright
                     48:  *    notice, this list of conditions and the following disclaimer in the
                     49:  *    documentation and/or other materials provided with the distribution.
                     50:  * 3. All advertising materials mentioning features or use of this software
                     51:  *    must display the following acknowledgement:
                     52:  *     This product includes software developed by the University of
                     53:  *     California, Berkeley and its contributors.
                     54:  * 4. Neither the name of the University nor the names of its contributors
                     55:  *    may be used to endorse or promote products derived from this software
                     56:  *    without specific prior written permission.
                     57:  *
                     58:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     59:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     60:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     61:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     62:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     63:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     64:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     65:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     66:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     67:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     68:  * SUCH DAMAGE.
                     69:  */
                     70:
1.88      ross       71: #ifndef MAKE_NATIVE
1.209   ! sjg        72: static char rcsid[] = "$NetBSD: var.c,v 1.208 2016/06/03 01:21:59 sjg Exp $";
1.23      lukem      73: #else
1.19      christos   74: #include <sys/cdefs.h>
1.1       cgd        75: #ifndef lint
1.11      christos   76: #if 0
1.15      christos   77: static char sccsid[] = "@(#)var.c      8.3 (Berkeley) 3/19/94";
1.11      christos   78: #else
1.209   ! sjg        79: __RCSID("$NetBSD: var.c,v 1.208 2016/06/03 01:21:59 sjg Exp $");
1.11      christos   80: #endif
1.1       cgd        81: #endif /* not lint */
1.23      lukem      82: #endif
1.1       cgd        83:
                     84: /*-
                     85:  * var.c --
                     86:  *     Variable-handling functions
                     87:  *
                     88:  * Interface:
1.138     dsl        89:  *     Var_Set             Set the value of a variable in the given
                     90:  *                         context. The variable is created if it doesn't
                     91:  *                         yet exist. The value and variable name need not
                     92:  *                         be preserved.
1.1       cgd        93:  *
                     94:  *     Var_Append          Append more characters to an existing variable
1.138     dsl        95:  *                         in the given context. The variable needn't
                     96:  *                         exist already -- it will be created if it doesn't.
                     97:  *                         A space is placed between the old value and the
                     98:  *                         new one.
1.1       cgd        99:  *
                    100:  *     Var_Exists          See if a variable exists.
                    101:  *
                    102:  *     Var_Value           Return the value of a variable in a context or
1.138     dsl       103:  *                         NULL if the variable is undefined.
1.1       cgd       104:  *
1.5       cgd       105:  *     Var_Subst           Substitute named variable, or all variables if
                    106:  *                         NULL in a string using
1.138     dsl       107:  *                         the given context as the top-most one. If the
                    108:  *                         third argument is non-zero, Parse_Error is
                    109:  *                         called if any variables are undefined.
1.1       cgd       110:  *
                    111:  *     Var_Parse           Parse a variable expansion from a string and
1.138     dsl       112:  *                         return the result and the number of characters
                    113:  *                         consumed.
1.1       cgd       114:  *
                    115:  *     Var_Delete          Delete a variable in a context.
                    116:  *
                    117:  *     Var_Init            Initialize this module.
                    118:  *
                    119:  * Debugging:
                    120:  *     Var_Dump            Print out all variables defined in the given
1.138     dsl       121:  *                         context.
1.1       cgd       122:  *
                    123:  * XXX: There's a lot of duplication in these functions.
                    124:  */
                    125:
1.157     sjg       126: #include    <sys/stat.h>
1.31      gwr       127: #ifndef NO_REGEX
1.28      wsanchez  128: #include    <sys/types.h>
1.16      christos  129: #include    <regex.h>
1.17      christos  130: #endif
1.70      wiz       131: #include    <ctype.h>
1.163     joerg     132: #include    <inttypes.h>
1.17      christos  133: #include    <stdlib.h>
1.82      jmc       134: #include    <limits.h>
1.166     tsutsui   135: #include    <time.h>
1.70      wiz       136:
1.1       cgd       137: #include    "make.h"
                    138: #include    "buf.h"
1.103     sjg       139: #include    "dir.h"
1.111     rillig    140: #include    "job.h"
1.193     christos  141: #include    "metachar.h"
1.1       cgd       142:
1.182     christos  143: extern int makelevel;
1.1       cgd       144: /*
1.170     sjg       145:  * This lets us tell if we have replaced the original environ
                    146:  * (which we cannot free).
                    147:  */
                    148: char **savedEnv = NULL;
                    149:
                    150: /*
1.1       cgd       151:  * This is a harmless return value for Var_Parse that can be used by Var_Subst
                    152:  * to determine if there was an error in parsing -- easier than returning
                    153:  * a flag, as things outside this module don't give a hoot.
                    154:  */
1.126     christos  155: char   var_Error[] = "";
1.1       cgd       156:
                    157: /*
1.202     christos  158:  * Similar to var_Error, but returned when the 'VARF_UNDEFERR' flag for
                    159:  * Var_Parse is not set. Why not just use a constant? Well, gcc likes
                    160:  * to condense identical string instances...
1.1       cgd       161:  */
1.126     christos  162: static char    varNoError[] = "";
1.1       cgd       163:
                    164: /*
1.205     sjg       165:  * Traditionally we consume $$ during := like any other expansion.
                    166:  * Other make's do not.
                    167:  * This knob allows controlling the behavior.
                    168:  * FALSE for old behavior.
                    169:  * TRUE for new compatible.
                    170:  */
                    171: #define SAVE_DOLLARS ".MAKE.SAVE_DOLLARS"
                    172: static Boolean save_dollars = TRUE;
                    173:
                    174: /*
1.1       cgd       175:  * Internally, variables are contained in four different contexts.
                    176:  *     1) the environment. They may not be changed. If an environment
                    177:  *         variable is appended-to, the result is placed in the global
                    178:  *         context.
                    179:  *     2) the global context. Variables set in the Makefile are located in
                    180:  *         the global context. It is the penultimate context searched when
                    181:  *         substituting.
                    182:  *     3) the command-line context. All variables set on the command line
                    183:  *        are placed in this context. They are UNALTERABLE once placed here.
                    184:  *     4) the local context. Each target has associated with it a context
                    185:  *        list. On this list are located the structures describing such
                    186:  *        local variables as $(@) and $(*)
                    187:  * The four contexts are searched in the reverse order from which they are
                    188:  * listed.
                    189:  */
1.184     sjg       190: GNode          *VAR_INTERNAL; /* variables from make itself */
1.1       cgd       191: GNode          *VAR_GLOBAL;   /* variables from the makefile */
                    192: GNode          *VAR_CMD;      /* variables defined on the command-line */
                    193:
                    194: #define FIND_CMD       0x1   /* look in VAR_CMD when searching */
                    195: #define FIND_GLOBAL    0x2   /* look in VAR_GLOBAL as well */
                    196: #define FIND_ENV       0x4   /* look in the environment also */
                    197:
                    198: typedef struct Var {
                    199:     char          *name;       /* the variable's name */
1.138     dsl       200:     Buffer       val;          /* its value */
                    201:     int                  flags;        /* miscellaneous status flags */
1.1       cgd       202: #define VAR_IN_USE     1           /* Variable's value currently being used.
                    203:                                     * Used to avoid recursion */
                    204: #define VAR_FROM_ENV   2           /* Variable comes from the environment */
                    205: #define VAR_JUNK       4           /* Variable is a junk variable that
                    206:                                     * should be destroyed when done with
                    207:                                     * it. Used by Var_Parse for undefined,
                    208:                                     * modified variables */
1.40      sjg       209: #define VAR_KEEP       8           /* Variable is VAR_JUNK, but we found
                    210:                                     * a use for it in some modifier and
                    211:                                     * the value is therefore valid */
1.118     sjg       212: #define VAR_EXPORTED   16          /* Variable is exported */
                    213: #define VAR_REEXPORT   32          /* Indicate if var needs re-export.
                    214:                                     * This would be true if it contains $'s
                    215:                                     */
1.131     sjg       216: #define VAR_FROM_CMD   64          /* Variable came from command line */
1.1       cgd       217: }  Var;
                    218:
1.118     sjg       219: /*
                    220:  * Exporting vars is expensive so skip it if we can
                    221:  */
                    222: #define VAR_EXPORTED_NONE      0
                    223: #define VAR_EXPORTED_YES       1
                    224: #define VAR_EXPORTED_ALL       2
                    225: static int var_exportedVars = VAR_EXPORTED_NONE;
                    226: /*
                    227:  * We pass this to Var_Export when doing the initial export
                    228:  * or after updating an exported var.
                    229:  */
1.203     sjg       230: #define VAR_EXPORT_PARENT      1
                    231: /*
                    232:  * We pass this to Var_Export1 to tell it to leave the value alone.
                    233:  */
                    234: #define VAR_EXPORT_LITERAL     2
1.16      christos  235:
                    236: /* Var*Pattern flags */
                    237: #define VAR_SUB_GLOBAL 0x01    /* Apply substitution globally */
                    238: #define VAR_SUB_ONE    0x02    /* Apply substitution to one word */
                    239: #define VAR_SUB_MATCHED        0x04    /* There was a match */
                    240: #define VAR_MATCH_START        0x08    /* Match at start of word */
                    241: #define VAR_MATCH_END  0x10    /* Match at end of word */
1.40      sjg       242: #define VAR_NOSUBST    0x20    /* don't expand vars in VarGetPattern */
1.16      christos  243:
1.65      sjg       244: /* Var_Set flags */
                    245: #define VAR_NO_EXPORT  0x01    /* do not export */
                    246:
1.5       cgd       247: typedef struct {
1.81      sjg       248:     /*
                    249:      * The following fields are set by Var_Parse() when it
                    250:      * encounters modifiers that need to keep state for use by
                    251:      * subsequent modifiers within the same variable expansion.
                    252:      */
                    253:     Byte       varSpace;       /* Word separator in expansions */
                    254:     Boolean    oneBigWord;     /* TRUE if we will treat the variable as a
                    255:                                 * single big word, even if it contains
                    256:                                 * embedded spaces (as opposed to the
                    257:                                 * usual behaviour of treating it as
                    258:                                 * several space-separated words). */
                    259: } Var_Parse_State;
                    260:
1.148     dsl       261: /* struct passed as 'void *' to VarSubstitute() for ":S/lhs/rhs/",
1.81      sjg       262:  * to VarSYSVMatch() for ":lhs=rhs". */
                    263: typedef struct {
1.73      christos  264:     const char   *lhs;     /* String to match */
1.138     dsl       265:     int                  leftLen; /* Length of string */
1.73      christos  266:     const char   *rhs;     /* Replacement string (w/ &'s removed) */
1.138     dsl       267:     int                  rightLen; /* Length of replacement */
                    268:     int                  flags;
1.5       cgd       269: } VarPattern;
                    270:
1.148     dsl       271: /* struct passed as 'void *' to VarLoopExpand() for ":@tvar@str@" */
1.41      sjg       272: typedef struct {
                    273:     GNode      *ctxt;          /* variable context */
                    274:     char       *tvar;          /* name of temp var */
1.142     dsl       275:     int                tvarLen;
1.41      sjg       276:     char       *str;           /* string to expand */
1.142     dsl       277:     int                strLen;
1.113     christos  278:     int                errnum;         /* errnum for not defined */
1.41      sjg       279: } VarLoop_t;
                    280:
1.31      gwr       281: #ifndef NO_REGEX
1.148     dsl       282: /* struct passed as 'void *' to VarRESubstitute() for ":C///" */
1.16      christos  283: typedef struct {
                    284:     regex_t       re;
1.127     christos  285:     int                   nsub;
1.16      christos  286:     regmatch_t           *matches;
                    287:     char         *replace;
                    288:     int                   flags;
                    289: } VarREPattern;
1.17      christos  290: #endif
1.16      christos  291:
1.81      sjg       292: /* struct passed to VarSelectWords() for ":[start..end]" */
                    293: typedef struct {
                    294:     int                start;          /* first word to select */
                    295:     int                end;            /* last word to select */
                    296: } VarSelectWords_t;
                    297:
1.73      christos  298: static Var *VarFind(const char *, GNode *, int);
                    299: static void VarAdd(const char *, const char *, GNode *);
1.81      sjg       300: static Boolean VarHead(GNode *, Var_Parse_State *,
1.148     dsl       301:                        char *, Boolean, Buffer *, void *);
1.81      sjg       302: static Boolean VarTail(GNode *, Var_Parse_State *,
1.148     dsl       303:                        char *, Boolean, Buffer *, void *);
1.81      sjg       304: static Boolean VarSuffix(GNode *, Var_Parse_State *,
1.148     dsl       305:                        char *, Boolean, Buffer *, void *);
1.81      sjg       306: static Boolean VarRoot(GNode *, Var_Parse_State *,
1.148     dsl       307:                        char *, Boolean, Buffer *, void *);
1.81      sjg       308: static Boolean VarMatch(GNode *, Var_Parse_State *,
1.148     dsl       309:                        char *, Boolean, Buffer *, void *);
1.13      christos  310: #ifdef SYSVVARSUB
1.81      sjg       311: static Boolean VarSYSVMatch(GNode *, Var_Parse_State *,
1.148     dsl       312:                        char *, Boolean, Buffer *, void *);
1.13      christos  313: #endif
1.81      sjg       314: static Boolean VarNoMatch(GNode *, Var_Parse_State *,
1.148     dsl       315:                        char *, Boolean, Buffer *, void *);
1.31      gwr       316: #ifndef NO_REGEX
1.70      wiz       317: static void VarREError(int, regex_t *, const char *);
1.81      sjg       318: static Boolean VarRESubstitute(GNode *, Var_Parse_State *,
1.148     dsl       319:                        char *, Boolean, Buffer *, void *);
1.17      christos  320: #endif
1.81      sjg       321: static Boolean VarSubstitute(GNode *, Var_Parse_State *,
1.148     dsl       322:                        char *, Boolean, Buffer *, void *);
1.81      sjg       323: static Boolean VarLoopExpand(GNode *, Var_Parse_State *,
1.148     dsl       324:                        char *, Boolean, Buffer *, void *);
1.81      sjg       325: static char *VarGetPattern(GNode *, Var_Parse_State *,
1.127     christos  326:                           int, const char **, int, int *, int *,
1.70      wiz       327:                           VarPattern *);
                    328: static char *VarQuote(char *);
1.163     joerg     329: static char *VarHash(char *);
1.81      sjg       330: static char *VarModify(GNode *, Var_Parse_State *,
                    331:     const char *,
1.148     dsl       332:     Boolean (*)(GNode *, Var_Parse_State *, char *, Boolean, Buffer *, void *),
                    333:     void *);
1.93      sjg       334: static char *VarOrder(const char *, const char);
1.73      christos  335: static char *VarUniq(const char *);
1.70      wiz       336: static int VarWordCompare(const void *, const void *);
1.148     dsl       337: static void VarPrintVar(void *);
1.1       cgd       338:
1.100     christos  339: #define BROPEN '{'
                    340: #define BRCLOSE        '}'
                    341: #define PROPEN '('
                    342: #define PRCLOSE        ')'
                    343:
1.1       cgd       344: /*-
                    345:  *-----------------------------------------------------------------------
                    346:  * VarFind --
                    347:  *     Find the given variable in the given context and any other contexts
                    348:  *     indicated.
                    349:  *
1.70      wiz       350:  * Input:
                    351:  *     name            name to find
                    352:  *     ctxt            context in which to find it
                    353:  *     flags           FIND_GLOBAL set means to look in the
                    354:  *                     VAR_GLOBAL context as well. FIND_CMD set means
                    355:  *                     to look in the VAR_CMD context also. FIND_ENV
                    356:  *                     set means to look in the environment
                    357:  *
1.1       cgd       358:  * Results:
                    359:  *     A pointer to the structure describing the desired variable or
1.136     dsl       360:  *     NULL if the variable does not exist.
1.1       cgd       361:  *
                    362:  * Side Effects:
                    363:  *     None
                    364:  *-----------------------------------------------------------------------
                    365:  */
                    366: static Var *
1.73      christos  367: VarFind(const char *name, GNode *ctxt, int flags)
1.1       cgd       368: {
1.36      mycroft   369:     Hash_Entry                 *var;
1.138     dsl       370:     Var                        *v;
1.1       cgd       371:
                    372:        /*
                    373:         * If the variable name begins with a '.', it could very well be one of
                    374:         * the local ones.  We check the name against all the local variables
                    375:         * and substitute the short version in for 'name' if it matches one of
                    376:         * them.
                    377:         */
1.6       jtc       378:        if (*name == '.' && isupper((unsigned char) name[1]))
1.1       cgd       379:                switch (name[1]) {
                    380:                case 'A':
                    381:                        if (!strcmp(name, ".ALLSRC"))
                    382:                                name = ALLSRC;
                    383:                        if (!strcmp(name, ".ARCHIVE"))
                    384:                                name = ARCHIVE;
                    385:                        break;
                    386:                case 'I':
                    387:                        if (!strcmp(name, ".IMPSRC"))
                    388:                                name = IMPSRC;
                    389:                        break;
                    390:                case 'M':
                    391:                        if (!strcmp(name, ".MEMBER"))
                    392:                                name = MEMBER;
                    393:                        break;
                    394:                case 'O':
                    395:                        if (!strcmp(name, ".OODATE"))
                    396:                                name = OODATE;
                    397:                        break;
                    398:                case 'P':
                    399:                        if (!strcmp(name, ".PREFIX"))
                    400:                                name = PREFIX;
                    401:                        break;
                    402:                case 'T':
                    403:                        if (!strcmp(name, ".TARGET"))
                    404:                                name = TARGET;
                    405:                        break;
                    406:                }
1.160     christos  407: #ifdef notyet
                    408:     /* for compatibility with gmake */
                    409:     if (name[0] == '^' && name[1] == '\0')
                    410:            name = ALLSRC;
                    411: #endif
                    412:
1.1       cgd       413:     /*
                    414:      * First look for the variable in the given context. If it's not there,
                    415:      * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order,
                    416:      * depending on the FIND_* flags in 'flags'
                    417:      */
1.92      christos  418:     var = Hash_FindEntry(&ctxt->context, name);
1.1       cgd       419:
1.36      mycroft   420:     if ((var == NULL) && (flags & FIND_CMD) && (ctxt != VAR_CMD)) {
1.92      christos  421:        var = Hash_FindEntry(&VAR_CMD->context, name);
1.1       cgd       422:     }
1.36      mycroft   423:     if (!checkEnvFirst && (var == NULL) && (flags & FIND_GLOBAL) &&
1.1       cgd       424:        (ctxt != VAR_GLOBAL))
                    425:     {
1.92      christos  426:        var = Hash_FindEntry(&VAR_GLOBAL->context, name);
1.184     sjg       427:        if ((var == NULL) && (ctxt != VAR_INTERNAL)) {
                    428:            /* VAR_INTERNAL is subordinate to VAR_GLOBAL */
                    429:            var = Hash_FindEntry(&VAR_INTERNAL->context, name);
                    430:        }
1.1       cgd       431:     }
1.36      mycroft   432:     if ((var == NULL) && (flags & FIND_ENV)) {
1.1       cgd       433:        char *env;
                    434:
1.97      christos  435:        if ((env = getenv(name)) != NULL) {
1.138     dsl       436:            int         len;
1.15      christos  437:
1.134     joerg     438:            v = bmake_malloc(sizeof(Var));
                    439:            v->name = bmake_strdup(name);
1.1       cgd       440:
                    441:            len = strlen(env);
1.15      christos  442:
1.146     dsl       443:            Buf_Init(&v->val, len + 1);
                    444:            Buf_AddBytes(&v->val, len, env);
1.15      christos  445:
1.1       cgd       446:            v->flags = VAR_FROM_ENV;
                    447:            return (v);
                    448:        } else if (checkEnvFirst && (flags & FIND_GLOBAL) &&
                    449:                   (ctxt != VAR_GLOBAL))
                    450:        {
1.92      christos  451:            var = Hash_FindEntry(&VAR_GLOBAL->context, name);
1.184     sjg       452:            if ((var == NULL) && (ctxt != VAR_INTERNAL)) {
                    453:                var = Hash_FindEntry(&VAR_INTERNAL->context, name);
                    454:            }
1.36      mycroft   455:            if (var == NULL) {
1.136     dsl       456:                return NULL;
1.1       cgd       457:            } else {
1.36      mycroft   458:                return ((Var *)Hash_GetValue(var));
1.1       cgd       459:            }
                    460:        } else {
1.136     dsl       461:            return NULL;
1.1       cgd       462:        }
1.36      mycroft   463:     } else if (var == NULL) {
1.136     dsl       464:        return NULL;
1.1       cgd       465:     } else {
1.97      christos  466:        return ((Var *)Hash_GetValue(var));
1.1       cgd       467:     }
                    468: }
                    469:
                    470: /*-
                    471:  *-----------------------------------------------------------------------
1.105     christos  472:  * VarFreeEnv  --
                    473:  *     If the variable is an environment variable, free it
                    474:  *
                    475:  * Input:
                    476:  *     v               the variable
                    477:  *     destroy         true if the value buffer should be destroyed.
                    478:  *
                    479:  * Results:
                    480:  *     1 if it is an environment variable 0 ow.
                    481:  *
                    482:  * Side Effects:
                    483:  *     The variable is free'ed if it is an environent variable.
                    484:  *-----------------------------------------------------------------------
                    485:  */
                    486: static Boolean
                    487: VarFreeEnv(Var *v, Boolean destroy)
                    488: {
                    489:     if ((v->flags & VAR_FROM_ENV) == 0)
                    490:        return FALSE;
                    491:     free(v->name);
1.146     dsl       492:     Buf_Destroy(&v->val, destroy);
1.105     christos  493:     free(v);
                    494:     return TRUE;
                    495: }
                    496:
                    497: /*-
                    498:  *-----------------------------------------------------------------------
1.1       cgd       499:  * VarAdd  --
                    500:  *     Add a new variable of name name and value val to the given context
                    501:  *
1.70      wiz       502:  * Input:
                    503:  *     name            name of variable to add
                    504:  *     val             value to set it to
                    505:  *     ctxt            context in which to set it
                    506:  *
1.1       cgd       507:  * Results:
                    508:  *     None
                    509:  *
                    510:  * Side Effects:
                    511:  *     The new variable is placed at the front of the given context
                    512:  *     The name and val arguments are duplicated so they may
                    513:  *     safely be freed.
                    514:  *-----------------------------------------------------------------------
                    515:  */
1.5       cgd       516: static void
1.73      christos  517: VarAdd(const char *name, const char *val, GNode *ctxt)
1.1       cgd       518: {
1.70      wiz       519:     Var          *v;
1.138     dsl       520:     int                  len;
1.70      wiz       521:     Hash_Entry    *h;
1.1       cgd       522:
1.134     joerg     523:     v = bmake_malloc(sizeof(Var));
1.1       cgd       524:
1.5       cgd       525:     len = val ? strlen(val) : 0;
1.146     dsl       526:     Buf_Init(&v->val, len+1);
                    527:     Buf_AddBytes(&v->val, len, val);
1.1       cgd       528:
                    529:     v->flags = 0;
                    530:
1.92      christos  531:     h = Hash_CreateEntry(&ctxt->context, name, NULL);
1.36      mycroft   532:     Hash_SetValue(h, v);
1.37      sommerfe  533:     v->name = h->name;
1.208     sjg       534:     if (DEBUG(VAR) && (ctxt->flags & INTERNAL) == 0) {
1.114     dsl       535:        fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val);
1.1       cgd       536:     }
                    537: }
                    538:
                    539: /*-
                    540:  *-----------------------------------------------------------------------
                    541:  * Var_Delete --
                    542:  *     Remove a variable from a context.
                    543:  *
                    544:  * Results:
                    545:  *     None.
                    546:  *
                    547:  * Side Effects:
                    548:  *     The Var structure is removed and freed.
                    549:  *
                    550:  *-----------------------------------------------------------------------
                    551:  */
                    552: void
1.73      christos  553: Var_Delete(const char *name, GNode *ctxt)
1.1       cgd       554: {
1.36      mycroft   555:     Hash_Entry           *ln;
1.174     sjg       556:     char *cp;
                    557:
                    558:     if (strchr(name, '$')) {
1.202     christos  559:        cp = Var_Subst(NULL, name, VAR_GLOBAL, VARF_WANTRES);
1.174     sjg       560:     } else {
                    561:        cp = (char *)name;
                    562:     }
                    563:     ln = Hash_FindEntry(&ctxt->context, cp);
1.1       cgd       564:     if (DEBUG(VAR)) {
1.117     dsl       565:        fprintf(debug_file, "%s:delete %s%s\n",
1.174     sjg       566:            ctxt->name, cp, ln ? "" : " (not found)");
                    567:     }
                    568:     if (cp != name) {
                    569:        free(cp);
1.1       cgd       570:     }
1.36      mycroft   571:     if (ln != NULL) {
1.70      wiz       572:        Var       *v;
1.1       cgd       573:
1.36      mycroft   574:        v = (Var *)Hash_GetValue(ln);
1.118     sjg       575:        if ((v->flags & VAR_EXPORTED)) {
                    576:            unsetenv(v->name);
                    577:        }
1.155     sjg       578:        if (strcmp(MAKE_EXPORTED, v->name) == 0) {
                    579:            var_exportedVars = VAR_EXPORTED_NONE;
                    580:        }
1.37      sommerfe  581:        if (v->name != ln->name)
                    582:                free(v->name);
1.36      mycroft   583:        Hash_DeleteEntry(&ctxt->context, ln);
1.146     dsl       584:        Buf_Destroy(&v->val, TRUE);
1.98      christos  585:        free(v);
1.1       cgd       586:     }
                    587: }
                    588:
1.118     sjg       589:
                    590: /*
                    591:  * Export a var.
                    592:  * We ignore make internal variables (those which start with '.')
                    593:  * Also we jump through some hoops to avoid calling setenv
                    594:  * more than necessary since it can leak.
1.152     sjg       595:  * We only manipulate flags of vars if 'parent' is set.
1.118     sjg       596:  */
                    597: static int
1.203     sjg       598: Var_Export1(const char *name, int flags)
1.118     sjg       599: {
                    600:     char tmp[BUFSIZ];
                    601:     Var *v;
                    602:     char *val = NULL;
                    603:     int n;
1.203     sjg       604:     int parent = (flags & VAR_EXPORT_PARENT);
1.118     sjg       605:
                    606:     if (*name == '.')
                    607:        return 0;                       /* skip internals */
                    608:     if (!name[1]) {
                    609:        /*
                    610:         * A single char.
                    611:         * If it is one of the vars that should only appear in
                    612:         * local context, skip it, else we can get Var_Subst
                    613:         * into a loop.
                    614:         */
                    615:        switch (name[0]) {
                    616:        case '@':
                    617:        case '%':
                    618:        case '*':
                    619:        case '!':
                    620:            return 0;
                    621:        }
                    622:     }
                    623:     v = VarFind(name, VAR_GLOBAL, 0);
1.136     dsl       624:     if (v == NULL) {
1.118     sjg       625:        return 0;
                    626:     }
1.152     sjg       627:     if (!parent &&
1.118     sjg       628:        (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) {
                    629:        return 0;                       /* nothing to do */
                    630:     }
1.146     dsl       631:     val = Buf_GetAll(&v->val, NULL);
1.203     sjg       632:     if ((flags & VAR_EXPORT_LITERAL) == 0 && strchr(val, '$')) {
1.152     sjg       633:        if (parent) {
1.118     sjg       634:            /*
1.152     sjg       635:             * Flag this as something we need to re-export.
1.118     sjg       636:             * No point actually exporting it now though,
                    637:             * the child can do it at the last minute.
                    638:             */
1.152     sjg       639:            v->flags |= (VAR_EXPORTED|VAR_REEXPORT);
1.118     sjg       640:            return 1;
                    641:        }
1.158     sjg       642:        if (v->flags & VAR_IN_USE) {
                    643:            /*
                    644:             * We recursed while exporting in a child.
                    645:             * This isn't going to end well, just skip it.
                    646:             */
                    647:            return 0;
                    648:        }
1.118     sjg       649:        n = snprintf(tmp, sizeof(tmp), "${%s}", name);
1.147     lukem     650:        if (n < (int)sizeof(tmp)) {
1.202     christos  651:            val = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
1.118     sjg       652:            setenv(name, val, 1);
                    653:            free(val);
                    654:        }
                    655:     } else {
1.152     sjg       656:        if (parent) {
                    657:            v->flags &= ~VAR_REEXPORT;  /* once will do */
                    658:        }
                    659:        if (parent || !(v->flags & VAR_EXPORTED)) {
1.118     sjg       660:            setenv(name, val, 1);
                    661:        }
                    662:     }
                    663:     /*
                    664:      * This is so Var_Set knows to call Var_Export again...
                    665:      */
1.152     sjg       666:     if (parent) {
                    667:        v->flags |= VAR_EXPORTED;
                    668:     }
1.118     sjg       669:     return 1;
                    670: }
                    671:
                    672: /*
                    673:  * This gets called from our children.
                    674:  */
                    675: void
                    676: Var_ExportVars(void)
                    677: {
                    678:     char tmp[BUFSIZ];
                    679:     Hash_Entry                 *var;
                    680:     Hash_Search        state;
                    681:     Var *v;
                    682:     char *val;
                    683:     int n;
                    684:
1.182     christos  685:     /*
                    686:      * Several make's support this sort of mechanism for tracking
                    687:      * recursion - but each uses a different name.
                    688:      * We allow the makefiles to update MAKELEVEL and ensure
                    689:      * children see a correctly incremented value.
                    690:      */
                    691:     snprintf(tmp, sizeof(tmp), "%d", makelevel + 1);
                    692:     setenv(MAKE_LEVEL_ENV, tmp, 1);
                    693:
1.118     sjg       694:     if (VAR_EXPORTED_NONE == var_exportedVars)
                    695:        return;
                    696:
                    697:     if (VAR_EXPORTED_ALL == var_exportedVars) {
                    698:        /*
                    699:         * Ouch! This is crazy...
                    700:         */
                    701:        for (var = Hash_EnumFirst(&VAR_GLOBAL->context, &state);
                    702:             var != NULL;
                    703:             var = Hash_EnumNext(&state)) {
                    704:            v = (Var *)Hash_GetValue(var);
                    705:            Var_Export1(v->name, 0);
                    706:        }
                    707:        return;
                    708:     }
                    709:     /*
                    710:      * We have a number of exported vars,
                    711:      */
                    712:     n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}");
1.147     lukem     713:     if (n < (int)sizeof(tmp)) {
1.118     sjg       714:        char **av;
                    715:        char *as;
1.127     christos  716:        int ac;
1.118     sjg       717:        int i;
1.142     dsl       718:
1.202     christos  719:        val = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
1.200     sjg       720:        if (*val) {
                    721:            av = brk_string(val, &ac, FALSE, &as);
                    722:            for (i = 0; i < ac; i++) {
                    723:                Var_Export1(av[i], 0);
                    724:            }
                    725:            free(as);
                    726:            free(av);
1.118     sjg       727:        }
                    728:        free(val);
                    729:     }
                    730: }
                    731:
                    732: /*
                    733:  * This is called when .export is seen or
                    734:  * .MAKE.EXPORTED is modified.
                    735:  * It is also called when any exported var is modified.
                    736:  */
                    737: void
                    738: Var_Export(char *str, int isExport)
                    739: {
                    740:     char *name;
                    741:     char *val;
                    742:     char **av;
                    743:     char *as;
1.203     sjg       744:     int flags;
1.127     christos  745:     int ac;
1.118     sjg       746:     int i;
                    747:
                    748:     if (isExport && (!str || !str[0])) {
                    749:        var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */
                    750:        return;
                    751:     }
                    752:
1.203     sjg       753:     flags = 0;
1.159     sjg       754:     if (strncmp(str, "-env", 4) == 0) {
                    755:        str += 4;
1.203     sjg       756:     } else if (strncmp(str, "-literal", 8) == 0) {
                    757:        str += 8;
                    758:        flags |= VAR_EXPORT_LITERAL;
1.159     sjg       759:     } else {
1.203     sjg       760:        flags |= VAR_EXPORT_PARENT;
1.159     sjg       761:     }
1.202     christos  762:     val = Var_Subst(NULL, str, VAR_GLOBAL, VARF_WANTRES);
1.200     sjg       763:     if (*val) {
                    764:        av = brk_string(val, &ac, FALSE, &as);
                    765:        for (i = 0; i < ac; i++) {
                    766:            name = av[i];
                    767:            if (!name[1]) {
                    768:                /*
                    769:                 * A single char.
                    770:                 * If it is one of the vars that should only appear in
                    771:                 * local context, skip it, else we can get Var_Subst
                    772:                 * into a loop.
                    773:                 */
                    774:                switch (name[0]) {
                    775:                case '@':
                    776:                case '%':
                    777:                case '*':
                    778:                case '!':
                    779:                    continue;
                    780:                }
1.118     sjg       781:            }
1.203     sjg       782:            if (Var_Export1(name, flags)) {
1.200     sjg       783:                if (VAR_EXPORTED_ALL != var_exportedVars)
                    784:                    var_exportedVars = VAR_EXPORTED_YES;
1.203     sjg       785:                if (isExport && (flags & VAR_EXPORT_PARENT)) {
1.200     sjg       786:                    Var_Append(MAKE_EXPORTED, name, VAR_GLOBAL);
                    787:                }
1.118     sjg       788:            }
                    789:        }
1.200     sjg       790:        free(as);
                    791:        free(av);
1.118     sjg       792:     }
                    793:     free(val);
                    794: }
                    795:
1.155     sjg       796:
                    797: /*
                    798:  * This is called when .unexport[-env] is seen.
                    799:  */
1.169     sjg       800: extern char **environ;
                    801:
1.155     sjg       802: void
                    803: Var_UnExport(char *str)
                    804: {
                    805:     char tmp[BUFSIZ];
                    806:     char *vlist;
                    807:     char *cp;
                    808:     Boolean unexport_env;
                    809:     int n;
                    810:
                    811:     if (!str || !str[0]) {
                    812:        return;                         /* assert? */
                    813:     }
                    814:
                    815:     vlist = NULL;
                    816:
                    817:     str += 8;
                    818:     unexport_env = (strncmp(str, "-env", 4) == 0);
                    819:     if (unexport_env) {
                    820:        char **newenv;
                    821:
1.179     sjg       822:        cp = getenv(MAKE_LEVEL_ENV);    /* we should preserve this */
1.170     sjg       823:        if (environ == savedEnv) {
1.155     sjg       824:            /* we have been here before! */
                    825:            newenv = bmake_realloc(environ, 2 * sizeof(char *));
                    826:        } else {
1.170     sjg       827:            if (savedEnv) {
                    828:                free(savedEnv);
                    829:                savedEnv = NULL;
1.155     sjg       830:            }
                    831:            newenv = bmake_malloc(2 * sizeof(char *));
                    832:        }
                    833:        if (!newenv)
                    834:            return;
                    835:        /* Note: we cannot safely free() the original environ. */
1.170     sjg       836:        environ = savedEnv = newenv;
1.155     sjg       837:        newenv[0] = NULL;
                    838:        newenv[1] = NULL;
1.179     sjg       839:        setenv(MAKE_LEVEL_ENV, cp, 1);
1.155     sjg       840:     } else {
                    841:        for (; *str != '\n' && isspace((unsigned char) *str); str++)
                    842:            continue;
                    843:        if (str[0] && str[0] != '\n') {
                    844:            vlist = str;
                    845:        }
                    846:     }
                    847:
                    848:     if (!vlist) {
                    849:        /* Using .MAKE.EXPORTED */
                    850:        n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}");
                    851:        if (n < (int)sizeof(tmp)) {
1.202     christos  852:            vlist = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
1.155     sjg       853:        }
                    854:     }
                    855:     if (vlist) {
                    856:        Var *v;
                    857:        char **av;
                    858:        char *as;
                    859:        int ac;
                    860:        int i;
                    861:
                    862:        av = brk_string(vlist, &ac, FALSE, &as);
                    863:        for (i = 0; i < ac; i++) {
                    864:            v = VarFind(av[i], VAR_GLOBAL, 0);
                    865:            if (!v)
                    866:                continue;
                    867:            if (!unexport_env &&
                    868:                (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) {
                    869:                unsetenv(v->name);
                    870:            }
                    871:            v->flags &= ~(VAR_EXPORTED|VAR_REEXPORT);
                    872:            /*
                    873:             * If we are unexporting a list,
                    874:             * remove each one from .MAKE.EXPORTED.
                    875:             * If we are removing them all,
                    876:             * just delete .MAKE.EXPORTED below.
                    877:             */
                    878:            if (vlist == str) {
                    879:                n = snprintf(tmp, sizeof(tmp),
                    880:                             "${" MAKE_EXPORTED ":N%s}", v->name);
                    881:                if (n < (int)sizeof(tmp)) {
1.202     christos  882:                    cp = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
1.155     sjg       883:                    Var_Set(MAKE_EXPORTED, cp, VAR_GLOBAL, 0);
                    884:                    free(cp);
                    885:                }
                    886:            }
                    887:        }
                    888:        free(as);
                    889:        free(av);
                    890:        if (vlist != str) {
                    891:            Var_Delete(MAKE_EXPORTED, VAR_GLOBAL);
                    892:            free(vlist);
                    893:        }
                    894:     }
                    895: }
                    896:
1.1       cgd       897: /*-
                    898:  *-----------------------------------------------------------------------
                    899:  * Var_Set --
                    900:  *     Set the variable name to the value val in the given context.
                    901:  *
1.70      wiz       902:  * Input:
                    903:  *     name            name of variable to set
                    904:  *     val             value to give to the variable
                    905:  *     ctxt            context in which to set it
                    906:  *
1.1       cgd       907:  * Results:
                    908:  *     None.
                    909:  *
                    910:  * Side Effects:
                    911:  *     If the variable doesn't yet exist, a new record is created for it.
                    912:  *     Else the old value is freed and the new one stuck in its place
                    913:  *
                    914:  * Notes:
                    915:  *     The variable is searched for only in its context before being
                    916:  *     created in that context. I.e. if the context is VAR_GLOBAL,
                    917:  *     only VAR_GLOBAL->context is searched. Likewise if it is VAR_CMD, only
                    918:  *     VAR_CMD->context is searched. This is done to avoid the literally
                    919:  *     thousands of unnecessary strcmp's that used to be done to
                    920:  *     set, say, $(@) or $(<).
1.131     sjg       921:  *     If the context is VAR_GLOBAL though, we check if the variable
                    922:  *     was set in VAR_CMD from the command line and skip it if so.
1.1       cgd       923:  *-----------------------------------------------------------------------
                    924:  */
                    925: void
1.73      christos  926: Var_Set(const char *name, const char *val, GNode *ctxt, int flags)
1.1       cgd       927: {
1.70      wiz       928:     Var   *v;
1.139     dsl       929:     char *expanded_name = NULL;
1.142     dsl       930:
1.1       cgd       931:     /*
                    932:      * We only look for a variable in the given context since anything set
                    933:      * here will override anything in a lower context, so there's not much
                    934:      * point in searching them all just to save a bit of memory...
                    935:      */
1.139     dsl       936:     if (strchr(name, '$') != NULL) {
1.202     christos  937:        expanded_name = Var_Subst(NULL, name, ctxt, VARF_WANTRES);
1.139     dsl       938:        if (expanded_name[0] == 0) {
                    939:            if (DEBUG(VAR)) {
                    940:                fprintf(debug_file, "Var_Set(\"%s\", \"%s\", ...) "
                    941:                        "name expands to empty string - ignored\n",
                    942:                        name, val);
                    943:            }
                    944:            free(expanded_name);
                    945:            return;
                    946:        }
                    947:        name = expanded_name;
                    948:     }
1.129     sjg       949:     if (ctxt == VAR_GLOBAL) {
                    950:        v = VarFind(name, VAR_CMD, 0);
1.136     dsl       951:        if (v != NULL) {
1.131     sjg       952:            if ((v->flags & VAR_FROM_CMD)) {
                    953:                if (DEBUG(VAR)) {
                    954:                    fprintf(debug_file, "%s:%s = %s ignored!\n", ctxt->name, name, val);
                    955:                }
                    956:                goto out;
                    957:            }
                    958:            VarFreeEnv(v, TRUE);
1.129     sjg       959:        }
                    960:     }
1.92      christos  961:     v = VarFind(name, ctxt, 0);
1.136     dsl       962:     if (v == NULL) {
1.183     sjg       963:        if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) {
                    964:            /*
                    965:             * This var would normally prevent the same name being added
                    966:             * to VAR_GLOBAL, so delete it from there if needed.
                    967:             * Otherwise -V name may show the wrong value.
                    968:             */
                    969:            Var_Delete(name, VAR_GLOBAL);
                    970:        }
1.92      christos  971:        VarAdd(name, val, ctxt);
1.1       cgd       972:     } else {
1.146     dsl       973:        Buf_Empty(&v->val);
                    974:        Buf_AddBytes(&v->val, strlen(val), val);
1.1       cgd       975:
                    976:        if (DEBUG(VAR)) {
1.114     dsl       977:            fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val);
1.1       cgd       978:        }
1.118     sjg       979:        if ((v->flags & VAR_EXPORTED)) {
1.152     sjg       980:            Var_Export1(name, VAR_EXPORT_PARENT);
1.118     sjg       981:        }
1.1       cgd       982:     }
                    983:     /*
                    984:      * Any variables given on the command line are automatically exported
                    985:      * to the environment (as per POSIX standard)
                    986:      */
1.65      sjg       987:     if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) {
1.136     dsl       988:        if (v == NULL) {
1.131     sjg       989:            /* we just added it */
                    990:            v = VarFind(name, ctxt, 0);
                    991:        }
1.151     christos  992:        if (v != NULL)
1.150     christos  993:            v->flags |= VAR_FROM_CMD;
1.71      thorpej   994:        /*
                    995:         * If requested, don't export these in the environment
                    996:         * individually.  We still put them in MAKEOVERRIDES so
                    997:         * that the command-line settings continue to override
                    998:         * Makefile settings.
                    999:         */
                   1000:        if (varNoExportEnv != TRUE)
                   1001:            setenv(name, val, 1);
1.62      sjg      1002:
1.64      sjg      1003:        Var_Append(MAKEOVERRIDES, name, VAR_GLOBAL);
1.1       cgd      1004:     }
1.205     sjg      1005:     if (*name == '.') {
                   1006:        if (strcmp(name, SAVE_DOLLARS) == 0)
                   1007:            save_dollars = s2Boolean(val, save_dollars);
                   1008:     }
                   1009:
1.129     sjg      1010:  out:
1.196     christos 1011:     free(expanded_name);
1.136     dsl      1012:     if (v != NULL)
1.107     christos 1013:        VarFreeEnv(v, TRUE);
1.1       cgd      1014: }
                   1015:
                   1016: /*-
                   1017:  *-----------------------------------------------------------------------
                   1018:  * Var_Append --
                   1019:  *     The variable of the given name has the given value appended to it in
                   1020:  *     the given context.
                   1021:  *
1.70      wiz      1022:  * Input:
                   1023:  *     name            name of variable to modify
                   1024:  *     val             String to append to it
                   1025:  *     ctxt            Context in which this should occur
                   1026:  *
1.1       cgd      1027:  * Results:
                   1028:  *     None
                   1029:  *
                   1030:  * Side Effects:
                   1031:  *     If the variable doesn't exist, it is created. Else the strings
                   1032:  *     are concatenated (with a space in between).
                   1033:  *
                   1034:  * Notes:
                   1035:  *     Only if the variable is being sought in the global context is the
                   1036:  *     environment searched.
                   1037:  *     XXX: Knows its calling circumstances in that if called with ctxt
                   1038:  *     an actual target, it will only search that context since only
                   1039:  *     a local variable could be being appended to. This is actually
                   1040:  *     a big win and must be tolerated.
                   1041:  *-----------------------------------------------------------------------
                   1042:  */
                   1043: void
1.73      christos 1044: Var_Append(const char *name, const char *val, GNode *ctxt)
1.1       cgd      1045: {
1.70      wiz      1046:     Var                   *v;
1.36      mycroft  1047:     Hash_Entry    *h;
1.139     dsl      1048:     char *expanded_name = NULL;
1.1       cgd      1049:
1.139     dsl      1050:     if (strchr(name, '$') != NULL) {
1.202     christos 1051:        expanded_name = Var_Subst(NULL, name, ctxt, VARF_WANTRES);
1.139     dsl      1052:        if (expanded_name[0] == 0) {
                   1053:            if (DEBUG(VAR)) {
                   1054:                fprintf(debug_file, "Var_Append(\"%s\", \"%s\", ...) "
                   1055:                        "name expands to empty string - ignored\n",
                   1056:                        name, val);
                   1057:            }
                   1058:            free(expanded_name);
                   1059:            return;
                   1060:        }
                   1061:        name = expanded_name;
                   1062:     }
1.142     dsl      1063:
1.92      christos 1064:     v = VarFind(name, ctxt, (ctxt == VAR_GLOBAL) ? FIND_ENV : 0);
1.1       cgd      1065:
1.136     dsl      1066:     if (v == NULL) {
1.92      christos 1067:        VarAdd(name, val, ctxt);
1.1       cgd      1068:     } else {
1.146     dsl      1069:        Buf_AddByte(&v->val, ' ');
                   1070:        Buf_AddBytes(&v->val, strlen(val), val);
1.1       cgd      1071:
                   1072:        if (DEBUG(VAR)) {
1.114     dsl      1073:            fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name,
1.146     dsl      1074:                   Buf_GetAll(&v->val, NULL));
1.1       cgd      1075:        }
                   1076:
                   1077:        if (v->flags & VAR_FROM_ENV) {
                   1078:            /*
                   1079:             * If the original variable came from the environment, we
                   1080:             * have to install it in the global context (we could place
                   1081:             * it in the environment, but then we should provide a way to
                   1082:             * export other variables...)
                   1083:             */
                   1084:            v->flags &= ~VAR_FROM_ENV;
1.92      christos 1085:            h = Hash_CreateEntry(&ctxt->context, name, NULL);
1.36      mycroft  1086:            Hash_SetValue(h, v);
1.1       cgd      1087:        }
                   1088:     }
1.196     christos 1089:     free(expanded_name);
1.1       cgd      1090: }
                   1091:
                   1092: /*-
                   1093:  *-----------------------------------------------------------------------
                   1094:  * Var_Exists --
                   1095:  *     See if the given variable exists.
                   1096:  *
1.70      wiz      1097:  * Input:
                   1098:  *     name            Variable to find
                   1099:  *     ctxt            Context in which to start search
                   1100:  *
1.1       cgd      1101:  * Results:
                   1102:  *     TRUE if it does, FALSE if it doesn't
                   1103:  *
                   1104:  * Side Effects:
                   1105:  *     None.
                   1106:  *
                   1107:  *-----------------------------------------------------------------------
                   1108:  */
                   1109: Boolean
1.73      christos 1110: Var_Exists(const char *name, GNode *ctxt)
1.1       cgd      1111: {
1.138     dsl      1112:     Var                  *v;
1.128     sjg      1113:     char          *cp;
1.1       cgd      1114:
1.128     sjg      1115:     if ((cp = strchr(name, '$')) != NULL) {
1.202     christos 1116:        cp = Var_Subst(NULL, name, ctxt, VARF_WANTRES);
1.128     sjg      1117:     }
                   1118:     v = VarFind(cp ? cp : name, ctxt, FIND_CMD|FIND_GLOBAL|FIND_ENV);
1.196     christos 1119:     free(cp);
1.136     dsl      1120:     if (v == NULL) {
1.1       cgd      1121:        return(FALSE);
1.105     christos 1122:     } else {
                   1123:        (void)VarFreeEnv(v, TRUE);
1.1       cgd      1124:     }
                   1125:     return(TRUE);
                   1126: }
                   1127:
                   1128: /*-
                   1129:  *-----------------------------------------------------------------------
                   1130:  * Var_Value --
                   1131:  *     Return the value of the named variable in the given context
                   1132:  *
1.70      wiz      1133:  * Input:
                   1134:  *     name            name to find
                   1135:  *     ctxt            context in which to search for it
                   1136:  *
1.1       cgd      1137:  * Results:
                   1138:  *     The value if the variable exists, NULL if it doesn't
                   1139:  *
                   1140:  * Side Effects:
                   1141:  *     None
                   1142:  *-----------------------------------------------------------------------
                   1143:  */
                   1144: char *
1.73      christos 1145: Var_Value(const char *name, GNode *ctxt, char **frp)
1.1       cgd      1146: {
                   1147:     Var            *v;
                   1148:
1.92      christos 1149:     v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
1.6       jtc      1150:     *frp = NULL;
1.136     dsl      1151:     if (v != NULL) {
1.146     dsl      1152:        char *p = (Buf_GetAll(&v->val, NULL));
1.105     christos 1153:        if (VarFreeEnv(v, FALSE))
1.6       jtc      1154:            *frp = p;
                   1155:        return p;
1.1       cgd      1156:     } else {
1.136     dsl      1157:        return NULL;
1.1       cgd      1158:     }
                   1159: }
                   1160:
                   1161: /*-
                   1162:  *-----------------------------------------------------------------------
                   1163:  * VarHead --
                   1164:  *     Remove the tail of the given word and place the result in the given
                   1165:  *     buffer.
                   1166:  *
1.70      wiz      1167:  * Input:
                   1168:  *     word            Word to trim
                   1169:  *     addSpace        True if need to add a space to the buffer
                   1170:  *                     before sticking in the head
                   1171:  *     buf             Buffer in which to store it
                   1172:  *
1.1       cgd      1173:  * Results:
                   1174:  *     TRUE if characters were added to the buffer (a space needs to be
                   1175:  *     added to the buffer before the next word).
                   1176:  *
                   1177:  * Side Effects:
                   1178:  *     The trimmed word is added to the buffer.
                   1179:  *
                   1180:  *-----------------------------------------------------------------------
                   1181:  */
                   1182: static Boolean
1.171     joerg    1183: VarHead(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1.146     dsl      1184:        char *word, Boolean addSpace, Buffer *buf,
1.148     dsl      1185:        void *dummy)
1.1       cgd      1186: {
1.70      wiz      1187:     char *slash;
1.1       cgd      1188:
1.97      christos 1189:     slash = strrchr(word, '/');
1.99      christos 1190:     if (slash != NULL) {
1.81      sjg      1191:        if (addSpace && vpstate->varSpace) {
1.97      christos 1192:            Buf_AddByte(buf, vpstate->varSpace);
1.1       cgd      1193:        }
                   1194:        *slash = '\0';
1.146     dsl      1195:        Buf_AddBytes(buf, strlen(word), word);
1.1       cgd      1196:        *slash = '/';
                   1197:        return (TRUE);
                   1198:     } else {
                   1199:        /*
                   1200:         * If no directory part, give . (q.v. the POSIX standard)
                   1201:         */
1.81      sjg      1202:        if (addSpace && vpstate->varSpace)
                   1203:            Buf_AddByte(buf, vpstate->varSpace);
1.146     dsl      1204:        Buf_AddByte(buf, '.');
1.1       cgd      1205:     }
1.6       jtc      1206:     return(dummy ? TRUE : TRUE);
1.1       cgd      1207: }
                   1208:
                   1209: /*-
                   1210:  *-----------------------------------------------------------------------
                   1211:  * VarTail --
                   1212:  *     Remove the head of the given word and place the result in the given
                   1213:  *     buffer.
                   1214:  *
1.70      wiz      1215:  * Input:
                   1216:  *     word            Word to trim
                   1217:  *     addSpace        True if need to add a space to the buffer
                   1218:  *                     before adding the tail
                   1219:  *     buf             Buffer in which to store it
                   1220:  *
1.1       cgd      1221:  * Results:
                   1222:  *     TRUE if characters were added to the buffer (a space needs to be
                   1223:  *     added to the buffer before the next word).
                   1224:  *
                   1225:  * Side Effects:
                   1226:  *     The trimmed word is added to the buffer.
                   1227:  *
                   1228:  *-----------------------------------------------------------------------
                   1229:  */
                   1230: static Boolean
1.171     joerg    1231: VarTail(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1.146     dsl      1232:        char *word, Boolean addSpace, Buffer *buf,
1.148     dsl      1233:        void *dummy)
1.1       cgd      1234: {
1.70      wiz      1235:     char *slash;
1.1       cgd      1236:
1.81      sjg      1237:     if (addSpace && vpstate->varSpace) {
1.97      christos 1238:        Buf_AddByte(buf, vpstate->varSpace);
1.1       cgd      1239:     }
                   1240:
1.97      christos 1241:     slash = strrchr(word, '/');
1.99      christos 1242:     if (slash != NULL) {
1.1       cgd      1243:        *slash++ = '\0';
1.146     dsl      1244:        Buf_AddBytes(buf, strlen(slash), slash);
1.1       cgd      1245:        slash[-1] = '/';
                   1246:     } else {
1.146     dsl      1247:        Buf_AddBytes(buf, strlen(word), word);
1.1       cgd      1248:     }
1.6       jtc      1249:     return (dummy ? TRUE : TRUE);
1.1       cgd      1250: }
                   1251:
                   1252: /*-
                   1253:  *-----------------------------------------------------------------------
                   1254:  * VarSuffix --
                   1255:  *     Place the suffix of the given word in the given buffer.
                   1256:  *
1.70      wiz      1257:  * Input:
                   1258:  *     word            Word to trim
                   1259:  *     addSpace        TRUE if need to add a space before placing the
                   1260:  *                     suffix in the buffer
                   1261:  *     buf             Buffer in which to store it
                   1262:  *
1.1       cgd      1263:  * Results:
                   1264:  *     TRUE if characters were added to the buffer (a space needs to be
                   1265:  *     added to the buffer before the next word).
                   1266:  *
                   1267:  * Side Effects:
                   1268:  *     The suffix from the word is placed in the buffer.
                   1269:  *
                   1270:  *-----------------------------------------------------------------------
                   1271:  */
                   1272: static Boolean
1.171     joerg    1273: VarSuffix(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1.146     dsl      1274:          char *word, Boolean addSpace, Buffer *buf,
1.148     dsl      1275:          void *dummy)
1.1       cgd      1276: {
1.70      wiz      1277:     char *dot;
1.1       cgd      1278:
1.97      christos 1279:     dot = strrchr(word, '.');
1.99      christos 1280:     if (dot != NULL) {
1.81      sjg      1281:        if (addSpace && vpstate->varSpace) {
1.97      christos 1282:            Buf_AddByte(buf, vpstate->varSpace);
1.1       cgd      1283:        }
                   1284:        *dot++ = '\0';
1.146     dsl      1285:        Buf_AddBytes(buf, strlen(dot), dot);
1.1       cgd      1286:        dot[-1] = '.';
1.6       jtc      1287:        addSpace = TRUE;
1.1       cgd      1288:     }
1.6       jtc      1289:     return (dummy ? addSpace : addSpace);
1.1       cgd      1290: }
                   1291:
                   1292: /*-
                   1293:  *-----------------------------------------------------------------------
                   1294:  * VarRoot --
                   1295:  *     Remove the suffix of the given word and place the result in the
                   1296:  *     buffer.
                   1297:  *
1.70      wiz      1298:  * Input:
                   1299:  *     word            Word to trim
                   1300:  *     addSpace        TRUE if need to add a space to the buffer
                   1301:  *                     before placing the root in it
                   1302:  *     buf             Buffer in which to store it
                   1303:  *
1.1       cgd      1304:  * Results:
                   1305:  *     TRUE if characters were added to the buffer (a space needs to be
                   1306:  *     added to the buffer before the next word).
                   1307:  *
                   1308:  * Side Effects:
                   1309:  *     The trimmed word is added to the buffer.
                   1310:  *
                   1311:  *-----------------------------------------------------------------------
                   1312:  */
                   1313: static Boolean
1.171     joerg    1314: VarRoot(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1.146     dsl      1315:        char *word, Boolean addSpace, Buffer *buf,
1.148     dsl      1316:        void *dummy)
1.1       cgd      1317: {
1.70      wiz      1318:     char *dot;
1.1       cgd      1319:
1.81      sjg      1320:     if (addSpace && vpstate->varSpace) {
1.97      christos 1321:        Buf_AddByte(buf, vpstate->varSpace);
1.1       cgd      1322:     }
                   1323:
1.97      christos 1324:     dot = strrchr(word, '.');
1.99      christos 1325:     if (dot != NULL) {
1.1       cgd      1326:        *dot = '\0';
1.146     dsl      1327:        Buf_AddBytes(buf, strlen(word), word);
1.1       cgd      1328:        *dot = '.';
                   1329:     } else {
1.146     dsl      1330:        Buf_AddBytes(buf, strlen(word), word);
1.1       cgd      1331:     }
1.6       jtc      1332:     return (dummy ? TRUE : TRUE);
1.1       cgd      1333: }
                   1334:
                   1335: /*-
                   1336:  *-----------------------------------------------------------------------
                   1337:  * VarMatch --
                   1338:  *     Place the word in the buffer if it matches the given pattern.
                   1339:  *     Callback function for VarModify to implement the :M modifier.
1.15      christos 1340:  *
1.70      wiz      1341:  * Input:
                   1342:  *     word            Word to examine
                   1343:  *     addSpace        TRUE if need to add a space to the buffer
                   1344:  *                     before adding the word, if it matches
                   1345:  *     buf             Buffer in which to store it
                   1346:  *     pattern         Pattern the word must match
                   1347:  *
1.1       cgd      1348:  * Results:
                   1349:  *     TRUE if a space should be placed in the buffer before the next
                   1350:  *     word.
                   1351:  *
                   1352:  * Side Effects:
                   1353:  *     The word may be copied to the buffer.
                   1354:  *
                   1355:  *-----------------------------------------------------------------------
                   1356:  */
                   1357: static Boolean
1.171     joerg    1358: VarMatch(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1.146     dsl      1359:         char *word, Boolean addSpace, Buffer *buf,
1.148     dsl      1360:         void *pattern)
1.1       cgd      1361: {
1.137     christos 1362:     if (DEBUG(VAR))
                   1363:        fprintf(debug_file, "VarMatch [%s] [%s]\n", word, (char *)pattern);
1.99      christos 1364:     if (Str_Match(word, (char *)pattern)) {
1.81      sjg      1365:        if (addSpace && vpstate->varSpace) {
                   1366:            Buf_AddByte(buf, vpstate->varSpace);
1.1       cgd      1367:        }
                   1368:        addSpace = TRUE;
1.146     dsl      1369:        Buf_AddBytes(buf, strlen(word), word);
1.1       cgd      1370:     }
                   1371:     return(addSpace);
                   1372: }
                   1373:
1.13      christos 1374: #ifdef SYSVVARSUB
1.5       cgd      1375: /*-
                   1376:  *-----------------------------------------------------------------------
                   1377:  * VarSYSVMatch --
                   1378:  *     Place the word in the buffer if it matches the given pattern.
                   1379:  *     Callback function for VarModify to implement the System V %
                   1380:  *     modifiers.
1.15      christos 1381:  *
1.70      wiz      1382:  * Input:
                   1383:  *     word            Word to examine
                   1384:  *     addSpace        TRUE if need to add a space to the buffer
                   1385:  *                     before adding the word, if it matches
                   1386:  *     buf             Buffer in which to store it
                   1387:  *     patp            Pattern the word must match
                   1388:  *
1.5       cgd      1389:  * Results:
                   1390:  *     TRUE if a space should be placed in the buffer before the next
                   1391:  *     word.
                   1392:  *
                   1393:  * Side Effects:
                   1394:  *     The word may be copied to the buffer.
                   1395:  *
                   1396:  *-----------------------------------------------------------------------
                   1397:  */
                   1398: static Boolean
1.81      sjg      1399: VarSYSVMatch(GNode *ctx, Var_Parse_State *vpstate,
1.146     dsl      1400:             char *word, Boolean addSpace, Buffer *buf,
1.148     dsl      1401:             void *patp)
1.5       cgd      1402: {
1.127     christos 1403:     int len;
1.5       cgd      1404:     char *ptr;
1.99      christos 1405:     VarPattern           *pat = (VarPattern *)patp;
1.61      explorer 1406:     char *varexp;
1.5       cgd      1407:
1.81      sjg      1408:     if (addSpace && vpstate->varSpace)
                   1409:        Buf_AddByte(buf, vpstate->varSpace);
1.5       cgd      1410:
                   1411:     addSpace = TRUE;
                   1412:
1.61      explorer 1413:     if ((ptr = Str_SYSVMatch(word, pat->lhs, &len)) != NULL) {
1.202     christos 1414:         varexp = Var_Subst(NULL, pat->rhs, ctx, VARF_WANTRES);
1.61      explorer 1415:        Str_SYSVSubst(buf, varexp, ptr, len);
                   1416:        free(varexp);
                   1417:     } else {
1.146     dsl      1418:        Buf_AddBytes(buf, strlen(word), word);
1.61      explorer 1419:     }
1.5       cgd      1420:
                   1421:     return(addSpace);
                   1422: }
1.13      christos 1423: #endif
1.5       cgd      1424:
                   1425:
1.1       cgd      1426: /*-
                   1427:  *-----------------------------------------------------------------------
                   1428:  * VarNoMatch --
                   1429:  *     Place the word in the buffer if it doesn't match the given pattern.
                   1430:  *     Callback function for VarModify to implement the :N modifier.
1.15      christos 1431:  *
1.70      wiz      1432:  * Input:
                   1433:  *     word            Word to examine
                   1434:  *     addSpace        TRUE if need to add a space to the buffer
                   1435:  *                     before adding the word, if it matches
                   1436:  *     buf             Buffer in which to store it
                   1437:  *     pattern         Pattern the word must match
                   1438:  *
1.1       cgd      1439:  * Results:
                   1440:  *     TRUE if a space should be placed in the buffer before the next
                   1441:  *     word.
                   1442:  *
                   1443:  * Side Effects:
                   1444:  *     The word may be copied to the buffer.
                   1445:  *
                   1446:  *-----------------------------------------------------------------------
                   1447:  */
                   1448: static Boolean
1.171     joerg    1449: VarNoMatch(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1.146     dsl      1450:           char *word, Boolean addSpace, Buffer *buf,
1.148     dsl      1451:           void *pattern)
1.1       cgd      1452: {
1.99      christos 1453:     if (!Str_Match(word, (char *)pattern)) {
1.81      sjg      1454:        if (addSpace && vpstate->varSpace) {
                   1455:            Buf_AddByte(buf, vpstate->varSpace);
1.1       cgd      1456:        }
                   1457:        addSpace = TRUE;
1.146     dsl      1458:        Buf_AddBytes(buf, strlen(word), word);
1.1       cgd      1459:     }
                   1460:     return(addSpace);
                   1461: }
                   1462:
                   1463:
                   1464: /*-
                   1465:  *-----------------------------------------------------------------------
                   1466:  * VarSubstitute --
                   1467:  *     Perform a string-substitution on the given word, placing the
                   1468:  *     result in the passed buffer.
                   1469:  *
1.70      wiz      1470:  * Input:
                   1471:  *     word            Word to modify
                   1472:  *     addSpace        True if space should be added before
                   1473:  *                     other characters
                   1474:  *     buf             Buffer for result
                   1475:  *     patternp        Pattern for substitution
                   1476:  *
1.1       cgd      1477:  * Results:
                   1478:  *     TRUE if a space is needed before more characters are added.
                   1479:  *
                   1480:  * Side Effects:
                   1481:  *     None.
                   1482:  *
                   1483:  *-----------------------------------------------------------------------
                   1484:  */
                   1485: static Boolean
1.171     joerg    1486: VarSubstitute(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1.146     dsl      1487:              char *word, Boolean addSpace, Buffer *buf,
1.148     dsl      1488:              void *patternp)
1.1       cgd      1489: {
1.70      wiz      1490:     int        wordLen;    /* Length of word */
                   1491:     char       *cp;        /* General pointer */
1.99      christos 1492:     VarPattern *pattern = (VarPattern *)patternp;
1.1       cgd      1493:
                   1494:     wordLen = strlen(word);
1.16      christos 1495:     if ((pattern->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) !=
                   1496:        (VAR_SUB_ONE|VAR_SUB_MATCHED)) {
1.1       cgd      1497:        /*
1.16      christos 1498:         * Still substituting -- break it down into simple anchored cases
1.1       cgd      1499:         * and if none of them fits, perform the general substitution case.
                   1500:         */
                   1501:        if ((pattern->flags & VAR_MATCH_START) &&
                   1502:            (strncmp(word, pattern->lhs, pattern->leftLen) == 0)) {
                   1503:                /*
                   1504:                 * Anchored at start and beginning of word matches pattern
                   1505:                 */
                   1506:                if ((pattern->flags & VAR_MATCH_END) &&
                   1507:                    (wordLen == pattern->leftLen)) {
                   1508:                        /*
                   1509:                         * Also anchored at end and matches to the end (word
                   1510:                         * is same length as pattern) add space and rhs only
                   1511:                         * if rhs is non-null.
                   1512:                         */
                   1513:                        if (pattern->rightLen != 0) {
1.81      sjg      1514:                            if (addSpace && vpstate->varSpace) {
                   1515:                                Buf_AddByte(buf, vpstate->varSpace);
1.1       cgd      1516:                            }
                   1517:                            addSpace = TRUE;
1.146     dsl      1518:                            Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1.1       cgd      1519:                        }
1.16      christos 1520:                        pattern->flags |= VAR_SUB_MATCHED;
1.1       cgd      1521:                } else if (pattern->flags & VAR_MATCH_END) {
                   1522:                    /*
                   1523:                     * Doesn't match to end -- copy word wholesale
                   1524:                     */
                   1525:                    goto nosub;
                   1526:                } else {
                   1527:                    /*
                   1528:                     * Matches at start but need to copy in trailing characters
                   1529:                     */
                   1530:                    if ((pattern->rightLen + wordLen - pattern->leftLen) != 0){
1.81      sjg      1531:                        if (addSpace && vpstate->varSpace) {
                   1532:                            Buf_AddByte(buf, vpstate->varSpace);
1.1       cgd      1533:                        }
                   1534:                        addSpace = TRUE;
                   1535:                    }
1.146     dsl      1536:                    Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1.1       cgd      1537:                    Buf_AddBytes(buf, wordLen - pattern->leftLen,
1.146     dsl      1538:                                 (word + pattern->leftLen));
1.16      christos 1539:                    pattern->flags |= VAR_SUB_MATCHED;
1.1       cgd      1540:                }
                   1541:        } else if (pattern->flags & VAR_MATCH_START) {
                   1542:            /*
                   1543:             * Had to match at start of word and didn't -- copy whole word.
                   1544:             */
                   1545:            goto nosub;
                   1546:        } else if (pattern->flags & VAR_MATCH_END) {
                   1547:            /*
                   1548:             * Anchored at end, Find only place match could occur (leftLen
                   1549:             * characters from the end of the word) and see if it does. Note
                   1550:             * that because the $ will be left at the end of the lhs, we have
                   1551:             * to use strncmp.
                   1552:             */
                   1553:            cp = word + (wordLen - pattern->leftLen);
                   1554:            if ((cp >= word) &&
                   1555:                (strncmp(cp, pattern->lhs, pattern->leftLen) == 0)) {
                   1556:                /*
                   1557:                 * Match found. If we will place characters in the buffer,
                   1558:                 * add a space before hand as indicated by addSpace, then
                   1559:                 * stuff in the initial, unmatched part of the word followed
                   1560:                 * by the right-hand-side.
                   1561:                 */
                   1562:                if (((cp - word) + pattern->rightLen) != 0) {
1.81      sjg      1563:                    if (addSpace && vpstate->varSpace) {
                   1564:                        Buf_AddByte(buf, vpstate->varSpace);
1.1       cgd      1565:                    }
                   1566:                    addSpace = TRUE;
                   1567:                }
1.146     dsl      1568:                Buf_AddBytes(buf, cp - word, word);
                   1569:                Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1.16      christos 1570:                pattern->flags |= VAR_SUB_MATCHED;
1.1       cgd      1571:            } else {
                   1572:                /*
                   1573:                 * Had to match at end and didn't. Copy entire word.
                   1574:                 */
                   1575:                goto nosub;
                   1576:            }
                   1577:        } else {
                   1578:            /*
                   1579:             * Pattern is unanchored: search for the pattern in the word using
                   1580:             * String_FindSubstring, copying unmatched portions and the
                   1581:             * right-hand-side for each match found, handling non-global
1.15      christos 1582:             * substitutions correctly, etc. When the loop is done, any
1.1       cgd      1583:             * remaining part of the word (word and wordLen are adjusted
                   1584:             * accordingly through the loop) is copied straight into the
                   1585:             * buffer.
                   1586:             * addSpace is set FALSE as soon as a space is added to the
                   1587:             * buffer.
                   1588:             */
1.70      wiz      1589:            Boolean done;
1.1       cgd      1590:            int origSize;
                   1591:
                   1592:            done = FALSE;
                   1593:            origSize = Buf_Size(buf);
                   1594:            while (!done) {
                   1595:                cp = Str_FindSubstring(word, pattern->lhs);
1.99      christos 1596:                if (cp != NULL) {
1.127     christos 1597:                    if (addSpace && (((cp - word) + pattern->rightLen) != 0)){
1.81      sjg      1598:                        Buf_AddByte(buf, vpstate->varSpace);
1.1       cgd      1599:                        addSpace = FALSE;
                   1600:                    }
1.146     dsl      1601:                    Buf_AddBytes(buf, cp-word, word);
                   1602:                    Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1.127     christos 1603:                    wordLen -= (cp - word) + pattern->leftLen;
1.1       cgd      1604:                    word = cp + pattern->leftLen;
1.16      christos 1605:                    if (wordLen == 0) {
                   1606:                        done = TRUE;
                   1607:                    }
                   1608:                    if ((pattern->flags & VAR_SUB_GLOBAL) == 0) {
1.1       cgd      1609:                        done = TRUE;
                   1610:                    }
1.16      christos 1611:                    pattern->flags |= VAR_SUB_MATCHED;
1.1       cgd      1612:                } else {
                   1613:                    done = TRUE;
                   1614:                }
                   1615:            }
                   1616:            if (wordLen != 0) {
1.81      sjg      1617:                if (addSpace && vpstate->varSpace) {
                   1618:                    Buf_AddByte(buf, vpstate->varSpace);
1.1       cgd      1619:                }
1.146     dsl      1620:                Buf_AddBytes(buf, wordLen, word);
1.1       cgd      1621:            }
                   1622:            /*
                   1623:             * If added characters to the buffer, need to add a space
                   1624:             * before we add any more. If we didn't add any, just return
                   1625:             * the previous value of addSpace.
                   1626:             */
                   1627:            return ((Buf_Size(buf) != origSize) || addSpace);
                   1628:        }
                   1629:        return (addSpace);
                   1630:     }
                   1631:  nosub:
1.81      sjg      1632:     if (addSpace && vpstate->varSpace) {
                   1633:        Buf_AddByte(buf, vpstate->varSpace);
1.1       cgd      1634:     }
1.146     dsl      1635:     Buf_AddBytes(buf, wordLen, word);
1.1       cgd      1636:     return(TRUE);
                   1637: }
                   1638:
1.31      gwr      1639: #ifndef NO_REGEX
1.16      christos 1640: /*-
                   1641:  *-----------------------------------------------------------------------
                   1642:  * VarREError --
                   1643:  *     Print the error caused by a regcomp or regexec call.
                   1644:  *
                   1645:  * Results:
                   1646:  *     None.
                   1647:  *
                   1648:  * Side Effects:
                   1649:  *     An error gets printed.
                   1650:  *
                   1651:  *-----------------------------------------------------------------------
                   1652:  */
                   1653: static void
1.202     christos 1654: VarREError(int reerr, regex_t *pat, const char *str)
1.16      christos 1655: {
                   1656:     char *errbuf;
1.127     christos 1657:     int errlen;
1.16      christos 1658:
1.202     christos 1659:     errlen = regerror(reerr, pat, 0, 0);
1.134     joerg    1660:     errbuf = bmake_malloc(errlen);
1.202     christos 1661:     regerror(reerr, pat, errbuf, errlen);
1.16      christos 1662:     Error("%s: %s", str, errbuf);
                   1663:     free(errbuf);
                   1664: }
                   1665:
                   1666:
                   1667: /*-
                   1668:  *-----------------------------------------------------------------------
                   1669:  * VarRESubstitute --
                   1670:  *     Perform a regex substitution on the given word, placing the
                   1671:  *     result in the passed buffer.
                   1672:  *
                   1673:  * Results:
                   1674:  *     TRUE if a space is needed before more characters are added.
                   1675:  *
                   1676:  * Side Effects:
                   1677:  *     None.
                   1678:  *
                   1679:  *-----------------------------------------------------------------------
                   1680:  */
                   1681: static Boolean
1.171     joerg    1682: VarRESubstitute(GNode *ctx MAKE_ATTR_UNUSED,
                   1683:                Var_Parse_State *vpstate MAKE_ATTR_UNUSED,
1.146     dsl      1684:                char *word, Boolean addSpace, Buffer *buf,
1.148     dsl      1685:                void *patternp)
1.16      christos 1686: {
                   1687:     VarREPattern *pat;
                   1688:     int xrv;
                   1689:     char *wp;
                   1690:     char *rp;
                   1691:     int added;
1.20      christos 1692:     int flags = 0;
1.16      christos 1693:
                   1694: #define MAYBE_ADD_SPACE()              \
                   1695:        if (addSpace && !added)         \
                   1696:            Buf_AddByte(buf, ' ');      \
                   1697:        added = 1
                   1698:
                   1699:     added = 0;
                   1700:     wp = word;
                   1701:     pat = patternp;
                   1702:
                   1703:     if ((pat->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) ==
                   1704:        (VAR_SUB_ONE|VAR_SUB_MATCHED))
                   1705:        xrv = REG_NOMATCH;
                   1706:     else {
                   1707:     tryagain:
1.21      christos 1708:        xrv = regexec(&pat->re, wp, pat->nsub, pat->matches, flags);
1.16      christos 1709:     }
                   1710:
                   1711:     switch (xrv) {
                   1712:     case 0:
                   1713:        pat->flags |= VAR_SUB_MATCHED;
                   1714:        if (pat->matches[0].rm_so > 0) {
                   1715:            MAYBE_ADD_SPACE();
1.127     christos 1716:            Buf_AddBytes(buf, pat->matches[0].rm_so, wp);
1.16      christos 1717:        }
                   1718:
                   1719:        for (rp = pat->replace; *rp; rp++) {
                   1720:            if ((*rp == '\\') && ((rp[1] == '&') || (rp[1] == '\\'))) {
                   1721:                MAYBE_ADD_SPACE();
1.127     christos 1722:                Buf_AddByte(buf,rp[1]);
1.16      christos 1723:                rp++;
                   1724:            }
1.30      christos 1725:            else if ((*rp == '&') ||
                   1726:                ((*rp == '\\') && isdigit((unsigned char)rp[1]))) {
1.16      christos 1727:                int n;
1.73      christos 1728:                const char *subbuf;
1.16      christos 1729:                int sublen;
                   1730:                char errstr[3];
                   1731:
                   1732:                if (*rp == '&') {
                   1733:                    n = 0;
                   1734:                    errstr[0] = '&';
                   1735:                    errstr[1] = '\0';
                   1736:                } else {
                   1737:                    n = rp[1] - '0';
                   1738:                    errstr[0] = '\\';
                   1739:                    errstr[1] = rp[1];
                   1740:                    errstr[2] = '\0';
                   1741:                    rp++;
                   1742:                }
                   1743:
                   1744:                if (n > pat->nsub) {
                   1745:                    Error("No subexpression %s", &errstr[0]);
                   1746:                    subbuf = "";
                   1747:                    sublen = 0;
                   1748:                } else if ((pat->matches[n].rm_so == -1) &&
                   1749:                           (pat->matches[n].rm_eo == -1)) {
                   1750:                    Error("No match for subexpression %s", &errstr[0]);
                   1751:                    subbuf = "";
                   1752:                    sublen = 0;
                   1753:                } else {
1.127     christos 1754:                    subbuf = wp + pat->matches[n].rm_so;
                   1755:                    sublen = pat->matches[n].rm_eo - pat->matches[n].rm_so;
1.16      christos 1756:                }
                   1757:
                   1758:                if (sublen > 0) {
                   1759:                    MAYBE_ADD_SPACE();
                   1760:                    Buf_AddBytes(buf, sublen, subbuf);
                   1761:                }
                   1762:            } else {
                   1763:                MAYBE_ADD_SPACE();
                   1764:                Buf_AddByte(buf, *rp);
                   1765:            }
                   1766:        }
1.127     christos 1767:        wp += pat->matches[0].rm_eo;
1.20      christos 1768:        if (pat->flags & VAR_SUB_GLOBAL) {
                   1769:            flags |= REG_NOTBOL;
                   1770:            if (pat->matches[0].rm_so == 0 && pat->matches[0].rm_eo == 0) {
                   1771:                MAYBE_ADD_SPACE();
                   1772:                Buf_AddByte(buf, *wp);
                   1773:                wp++;
                   1774:
                   1775:            }
                   1776:            if (*wp)
                   1777:                goto tryagain;
                   1778:        }
1.16      christos 1779:        if (*wp) {
                   1780:            MAYBE_ADD_SPACE();
                   1781:            Buf_AddBytes(buf, strlen(wp), wp);
                   1782:        }
                   1783:        break;
                   1784:     default:
                   1785:        VarREError(xrv, &pat->re, "Unexpected regex error");
1.127     christos 1786:        /* fall through */
1.16      christos 1787:     case REG_NOMATCH:
                   1788:        if (*wp) {
                   1789:            MAYBE_ADD_SPACE();
1.127     christos 1790:            Buf_AddBytes(buf,strlen(wp),wp);
1.16      christos 1791:        }
                   1792:        break;
                   1793:     }
1.127     christos 1794:     return(addSpace||added);
1.16      christos 1795: }
1.17      christos 1796: #endif
1.16      christos 1797:
                   1798:
1.40      sjg      1799:
                   1800: /*-
                   1801:  *-----------------------------------------------------------------------
                   1802:  * VarLoopExpand --
                   1803:  *     Implements the :@<temp>@<string>@ modifier of ODE make.
                   1804:  *     We set the temp variable named in pattern.lhs to word and expand
                   1805:  *     pattern.rhs storing the result in the passed buffer.
                   1806:  *
1.70      wiz      1807:  * Input:
                   1808:  *     word            Word to modify
                   1809:  *     addSpace        True if space should be added before
                   1810:  *                     other characters
                   1811:  *     buf             Buffer for result
                   1812:  *     pattern         Datafor substitution
                   1813:  *
1.40      sjg      1814:  * Results:
                   1815:  *     TRUE if a space is needed before more characters are added.
                   1816:  *
                   1817:  * Side Effects:
                   1818:  *     None.
                   1819:  *
                   1820:  *-----------------------------------------------------------------------
                   1821:  */
                   1822: static Boolean
1.171     joerg    1823: VarLoopExpand(GNode *ctx MAKE_ATTR_UNUSED,
                   1824:              Var_Parse_State *vpstate MAKE_ATTR_UNUSED,
1.146     dsl      1825:              char *word, Boolean addSpace, Buffer *buf,
1.148     dsl      1826:              void *loopp)
1.40      sjg      1827: {
1.99      christos 1828:     VarLoop_t  *loop = (VarLoop_t *)loopp;
1.40      sjg      1829:     char *s;
1.60      sjg      1830:     int slen;
1.64      sjg      1831:
                   1832:     if (word && *word) {
1.65      sjg      1833:         Var_Set(loop->tvar, word, loop->ctxt, VAR_NO_EXPORT);
1.202     christos 1834:         s = Var_Subst(NULL, loop->str, loop->ctxt, loop->errnum | VARF_WANTRES);
1.64      sjg      1835:         if (s != NULL && *s != '\0') {
                   1836:             if (addSpace && *s != '\n')
                   1837:                 Buf_AddByte(buf, ' ');
1.146     dsl      1838:             Buf_AddBytes(buf, (slen = strlen(s)), s);
1.64      sjg      1839:             addSpace = (slen > 0 && s[slen - 1] != '\n');
                   1840:             free(s);
                   1841:         }
1.40      sjg      1842:     }
                   1843:     return addSpace;
                   1844: }
                   1845:
1.81      sjg      1846:
                   1847: /*-
                   1848:  *-----------------------------------------------------------------------
                   1849:  * VarSelectWords --
                   1850:  *     Implements the :[start..end] modifier.
                   1851:  *     This is a special case of VarModify since we want to be able
                   1852:  *     to scan the list backwards if start > end.
                   1853:  *
                   1854:  * Input:
                   1855:  *     str             String whose words should be trimmed
                   1856:  *     seldata         words to select
                   1857:  *
                   1858:  * Results:
                   1859:  *     A string of all the words selected.
                   1860:  *
                   1861:  * Side Effects:
                   1862:  *     None.
                   1863:  *
                   1864:  *-----------------------------------------------------------------------
                   1865:  */
                   1866: static char *
1.171     joerg    1867: VarSelectWords(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1.81      sjg      1868:               const char *str, VarSelectWords_t *seldata)
                   1869: {
1.138     dsl      1870:     Buffer       buf;              /* Buffer for the new string */
1.81      sjg      1871:     Boolean      addSpace;         /* TRUE if need to add a space to the
                   1872:                                     * buffer before adding the trimmed
                   1873:                                     * word */
                   1874:     char **av;                     /* word list */
                   1875:     char *as;                      /* word list memory */
1.127     christos 1876:     int ac, i;
1.81      sjg      1877:     int start, end, step;
1.142     dsl      1878:
1.146     dsl      1879:     Buf_Init(&buf, 0);
1.81      sjg      1880:     addSpace = FALSE;
                   1881:
                   1882:     if (vpstate->oneBigWord) {
                   1883:        /* fake what brk_string() would do if there were only one word */
                   1884:        ac = 1;
1.134     joerg    1885:        av = bmake_malloc((ac + 1) * sizeof(char *));
                   1886:        as = bmake_strdup(str);
1.81      sjg      1887:        av[0] = as;
                   1888:        av[1] = NULL;
                   1889:     } else {
                   1890:        av = brk_string(str, &ac, FALSE, &as);
                   1891:     }
                   1892:
                   1893:     /*
                   1894:      * Now sanitize seldata.
                   1895:      * If seldata->start or seldata->end are negative, convert them to
                   1896:      * the positive equivalents (-1 gets converted to argc, -2 gets
                   1897:      * converted to (argc-1), etc.).
                   1898:      */
                   1899:     if (seldata->start < 0)
                   1900:        seldata->start = ac + seldata->start + 1;
                   1901:     if (seldata->end < 0)
                   1902:        seldata->end = ac + seldata->end + 1;
                   1903:
                   1904:     /*
                   1905:      * We avoid scanning more of the list than we need to.
                   1906:      */
                   1907:     if (seldata->start > seldata->end) {
                   1908:        start = MIN(ac, seldata->start) - 1;
                   1909:        end = MAX(0, seldata->end - 1);
                   1910:        step = -1;
                   1911:     } else {
                   1912:        start = MAX(0, seldata->start - 1);
                   1913:        end = MIN(ac, seldata->end);
                   1914:        step = 1;
                   1915:     }
                   1916:
                   1917:     for (i = start;
                   1918:         (step < 0 && i >= end) || (step > 0 && i < end);
                   1919:         i += step) {
                   1920:        if (av[i] && *av[i]) {
                   1921:            if (addSpace && vpstate->varSpace) {
1.146     dsl      1922:                Buf_AddByte(&buf, vpstate->varSpace);
1.81      sjg      1923:            }
1.146     dsl      1924:            Buf_AddBytes(&buf, strlen(av[i]), av[i]);
1.81      sjg      1925:            addSpace = TRUE;
                   1926:        }
                   1927:     }
                   1928:
                   1929:     free(as);
                   1930:     free(av);
                   1931:
1.146     dsl      1932:     return Buf_Destroy(&buf, FALSE);
1.81      sjg      1933: }
                   1934:
1.156     sjg      1935:
                   1936: /*-
                   1937:  * VarRealpath --
                   1938:  *     Replace each word with the result of realpath()
                   1939:  *     if successful.
                   1940:  */
                   1941: static Boolean
1.171     joerg    1942: VarRealpath(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1.156     sjg      1943:            char *word, Boolean addSpace, Buffer *buf,
1.171     joerg    1944:            void *patternp MAKE_ATTR_UNUSED)
1.156     sjg      1945: {
1.157     sjg      1946:        struct stat st;
1.156     sjg      1947:        char rbuf[MAXPATHLEN];
                   1948:        char *rp;
                   1949:
                   1950:        if (addSpace && vpstate->varSpace) {
                   1951:            Buf_AddByte(buf, vpstate->varSpace);
                   1952:        }
                   1953:        addSpace = TRUE;
1.208     sjg      1954:        rp = cached_realpath(word, rbuf);
1.157     sjg      1955:        if (rp && *rp == '/' && stat(rp, &st) == 0)
1.156     sjg      1956:                word = rp;
                   1957:
                   1958:        Buf_AddBytes(buf, strlen(word), word);
                   1959:        return(addSpace);
                   1960: }
                   1961:
1.1       cgd      1962: /*-
                   1963:  *-----------------------------------------------------------------------
                   1964:  * VarModify --
                   1965:  *     Modify each of the words of the passed string using the given
                   1966:  *     function. Used to implement all modifiers.
                   1967:  *
1.70      wiz      1968:  * Input:
                   1969:  *     str             String whose words should be trimmed
                   1970:  *     modProc         Function to use to modify them
                   1971:  *     datum           Datum to pass it
                   1972:  *
1.1       cgd      1973:  * Results:
                   1974:  *     A string of all the words modified appropriately.
                   1975:  *
                   1976:  * Side Effects:
                   1977:  *     None.
                   1978:  *
                   1979:  *-----------------------------------------------------------------------
                   1980:  */
                   1981: static char *
1.81      sjg      1982: VarModify(GNode *ctx, Var_Parse_State *vpstate,
                   1983:     const char *str,
                   1984:     Boolean (*modProc)(GNode *, Var_Parse_State *, char *,
1.148     dsl      1985:                       Boolean, Buffer *, void *),
                   1986:     void *datum)
1.1       cgd      1987: {
1.138     dsl      1988:     Buffer       buf;              /* Buffer for the new string */
1.1       cgd      1989:     Boolean      addSpace;         /* TRUE if need to add a space to the
                   1990:                                     * buffer before adding the trimmed
                   1991:                                     * word */
1.81      sjg      1992:     char **av;                     /* word list */
1.24      christos 1993:     char *as;                      /* word list memory */
1.127     christos 1994:     int ac, i;
1.7       jtc      1995:
1.146     dsl      1996:     Buf_Init(&buf, 0);
1.1       cgd      1997:     addSpace = FALSE;
                   1998:
1.81      sjg      1999:     if (vpstate->oneBigWord) {
                   2000:        /* fake what brk_string() would do if there were only one word */
                   2001:        ac = 1;
1.134     joerg    2002:        av = bmake_malloc((ac + 1) * sizeof(char *));
                   2003:        as = bmake_strdup(str);
1.81      sjg      2004:        av[0] = as;
                   2005:        av[1] = NULL;
                   2006:     } else {
                   2007:        av = brk_string(str, &ac, FALSE, &as);
                   2008:     }
1.1       cgd      2009:
1.81      sjg      2010:     for (i = 0; i < ac; i++) {
1.146     dsl      2011:        addSpace = (*modProc)(ctx, vpstate, av[i], addSpace, &buf, datum);
1.81      sjg      2012:     }
1.24      christos 2013:
                   2014:     free(as);
                   2015:     free(av);
1.15      christos 2016:
1.146     dsl      2017:     return Buf_Destroy(&buf, FALSE);
1.1       cgd      2018: }
                   2019:
1.35      christos 2020:
                   2021: static int
1.70      wiz      2022: VarWordCompare(const void *a, const void *b)
1.35      christos 2023: {
1.79      scw      2024:        int r = strcmp(*(const char * const *)a, *(const char * const *)b);
1.35      christos 2025:        return r;
                   2026: }
                   2027:
                   2028: /*-
                   2029:  *-----------------------------------------------------------------------
1.93      sjg      2030:  * VarOrder --
                   2031:  *     Order the words in the string.
1.35      christos 2032:  *
1.70      wiz      2033:  * Input:
1.93      sjg      2034:  *     str             String whose words should be sorted.
                   2035:  *     otype           How to order: s - sort, x - random.
1.70      wiz      2036:  *
1.35      christos 2037:  * Results:
1.93      sjg      2038:  *     A string containing the words ordered.
1.35      christos 2039:  *
                   2040:  * Side Effects:
                   2041:  *     None.
                   2042:  *
                   2043:  *-----------------------------------------------------------------------
                   2044:  */
                   2045: static char *
1.93      sjg      2046: VarOrder(const char *str, const char otype)
1.35      christos 2047: {
1.138     dsl      2048:     Buffer       buf;              /* Buffer for the new string */
1.35      christos 2049:     char **av;                     /* word list [first word does not count] */
                   2050:     char *as;                      /* word list memory */
1.127     christos 2051:     int ac, i;
1.35      christos 2052:
1.146     dsl      2053:     Buf_Init(&buf, 0);
1.35      christos 2054:
                   2055:     av = brk_string(str, &ac, FALSE, &as);
                   2056:
                   2057:     if (ac > 0)
1.93      sjg      2058:        switch (otype) {
                   2059:        case 's':       /* sort alphabetically */
                   2060:            qsort(av, ac, sizeof(char *), VarWordCompare);
                   2061:            break;
                   2062:        case 'x':       /* randomize */
                   2063:        {
                   2064:            int rndidx;
                   2065:            char *t;
                   2066:
                   2067:            /*
                   2068:             * We will use [ac..2] range for mod factors. This will produce
                   2069:             * random numbers in [(ac-1)..0] interval, and minimal
                   2070:             * reasonable value for mod factor is 2 (the mod 1 will produce
                   2071:             * 0 with probability 1).
                   2072:             */
                   2073:            for (i = ac-1; i > 0; i--) {
                   2074:                rndidx = random() % (i + 1);
                   2075:                if (i != rndidx) {
                   2076:                    t = av[i];
                   2077:                    av[i] = av[rndidx];
                   2078:                    av[rndidx] = t;
                   2079:                }
                   2080:            }
                   2081:        }
                   2082:        } /* end of switch */
1.35      christos 2083:
                   2084:     for (i = 0; i < ac; i++) {
1.146     dsl      2085:        Buf_AddBytes(&buf, strlen(av[i]), av[i]);
1.35      christos 2086:        if (i != ac - 1)
1.146     dsl      2087:            Buf_AddByte(&buf, ' ');
1.35      christos 2088:     }
                   2089:
                   2090:     free(as);
                   2091:     free(av);
                   2092:
1.146     dsl      2093:     return Buf_Destroy(&buf, FALSE);
1.35      christos 2094: }
                   2095:
                   2096:
1.1       cgd      2097: /*-
                   2098:  *-----------------------------------------------------------------------
1.55      christos 2099:  * VarUniq --
                   2100:  *     Remove adjacent duplicate words.
                   2101:  *
1.70      wiz      2102:  * Input:
                   2103:  *     str             String whose words should be sorted
                   2104:  *
1.55      christos 2105:  * Results:
                   2106:  *     A string containing the resulting words.
                   2107:  *
                   2108:  * Side Effects:
                   2109:  *     None.
                   2110:  *
                   2111:  *-----------------------------------------------------------------------
                   2112:  */
                   2113: static char *
1.73      christos 2114: VarUniq(const char *str)
1.55      christos 2115: {
1.138     dsl      2116:     Buffer       buf;              /* Buffer for new string */
1.55      christos 2117:     char       **av;               /* List of words to affect */
                   2118:     char        *as;               /* Word list memory */
1.127     christos 2119:     int          ac, i, j;
1.55      christos 2120:
1.146     dsl      2121:     Buf_Init(&buf, 0);
1.55      christos 2122:     av = brk_string(str, &ac, FALSE, &as);
                   2123:
                   2124:     if (ac > 1) {
                   2125:        for (j = 0, i = 1; i < ac; i++)
                   2126:            if (strcmp(av[i], av[j]) != 0 && (++j != i))
                   2127:                av[j] = av[i];
                   2128:        ac = j + 1;
                   2129:     }
                   2130:
                   2131:     for (i = 0; i < ac; i++) {
1.146     dsl      2132:        Buf_AddBytes(&buf, strlen(av[i]), av[i]);
1.55      christos 2133:        if (i != ac - 1)
1.146     dsl      2134:            Buf_AddByte(&buf, ' ');
1.55      christos 2135:     }
                   2136:
                   2137:     free(as);
                   2138:     free(av);
                   2139:
1.146     dsl      2140:     return Buf_Destroy(&buf, FALSE);
1.55      christos 2141: }
                   2142:
                   2143:
                   2144: /*-
                   2145:  *-----------------------------------------------------------------------
1.16      christos 2146:  * VarGetPattern --
                   2147:  *     Pass through the tstr looking for 1) escaped delimiters,
                   2148:  *     '$'s and backslashes (place the escaped character in
                   2149:  *     uninterpreted) and 2) unescaped $'s that aren't before
1.40      sjg      2150:  *     the delimiter (expand the variable substitution unless flags
                   2151:  *     has VAR_NOSUBST set).
1.16      christos 2152:  *     Return the expanded string or NULL if the delimiter was missing
1.25      christos 2153:  *     If pattern is specified, handle escaped ampersands, and replace
1.16      christos 2154:  *     unescaped ampersands with the lhs of the pattern.
                   2155:  *
                   2156:  * Results:
                   2157:  *     A string of all the words modified appropriately.
                   2158:  *     If length is specified, return the string length of the buffer
                   2159:  *     If flags is specified and the last character of the pattern is a
                   2160:  *     $ set the VAR_MATCH_END bit of flags.
                   2161:  *
                   2162:  * Side Effects:
                   2163:  *     None.
                   2164:  *-----------------------------------------------------------------------
                   2165:  */
                   2166: static char *
1.171     joerg    2167: VarGetPattern(GNode *ctxt, Var_Parse_State *vpstate MAKE_ATTR_UNUSED,
1.202     christos 2168:              int flags, const char **tstr, int delim, int *vflags,
1.127     christos 2169:              int *length, VarPattern *pattern)
1.16      christos 2170: {
1.73      christos 2171:     const char *cp;
1.146     dsl      2172:     char *rstr;
                   2173:     Buffer buf;
1.127     christos 2174:     int junk;
1.202     christos 2175:     int errnum = flags & VARF_UNDEFERR;
1.146     dsl      2176:
                   2177:     Buf_Init(&buf, 0);
1.16      christos 2178:     if (length == NULL)
                   2179:        length = &junk;
                   2180:
                   2181: #define IS_A_MATCH(cp, delim) \
                   2182:     ((cp[0] == '\\') && ((cp[1] == delim) ||  \
                   2183:      (cp[1] == '\\') || (cp[1] == '$') || (pattern && (cp[1] == '&'))))
                   2184:
                   2185:     /*
                   2186:      * Skim through until the matching delimiter is found;
                   2187:      * pick up variable substitutions on the way. Also allow
                   2188:      * backslashes to quote the delimiter, $, and \, but don't
                   2189:      * touch other backslashes.
                   2190:      */
                   2191:     for (cp = *tstr; *cp && (*cp != delim); cp++) {
                   2192:        if (IS_A_MATCH(cp, delim)) {
1.146     dsl      2193:            Buf_AddByte(&buf, cp[1]);
1.16      christos 2194:            cp++;
                   2195:        } else if (*cp == '$') {
                   2196:            if (cp[1] == delim) {
1.202     christos 2197:                if (vflags == NULL)
1.146     dsl      2198:                    Buf_AddByte(&buf, *cp);
1.16      christos 2199:                else
                   2200:                    /*
                   2201:                     * Unescaped $ at end of pattern => anchor
                   2202:                     * pattern at end.
                   2203:                     */
1.202     christos 2204:                    *vflags |= VAR_MATCH_END;
1.40      sjg      2205:            } else {
1.202     christos 2206:                if (vflags == NULL || (*vflags & VAR_NOSUBST) == 0) {
1.40      sjg      2207:                    char   *cp2;
1.127     christos 2208:                    int     len;
1.104     christos 2209:                    void   *freeIt;
1.40      sjg      2210:
                   2211:                    /*
                   2212:                     * If unescaped dollar sign not before the
                   2213:                     * delimiter, assume it's a variable
                   2214:                     * substitution and recurse.
                   2215:                     */
1.202     christos 2216:                    cp2 = Var_Parse(cp, ctxt, errnum | VARF_WANTRES, &len,
                   2217:                        &freeIt);
1.146     dsl      2218:                    Buf_AddBytes(&buf, strlen(cp2), cp2);
1.196     christos 2219:                    free(freeIt);
1.40      sjg      2220:                    cp += len - 1;
                   2221:                } else {
1.73      christos 2222:                    const char *cp2 = &cp[1];
1.16      christos 2223:
1.100     christos 2224:                    if (*cp2 == PROPEN || *cp2 == BROPEN) {
1.40      sjg      2225:                        /*
                   2226:                         * Find the end of this variable reference
                   2227:                         * and suck it in without further ado.
                   2228:                         * It will be interperated later.
                   2229:                         */
                   2230:                        int have = *cp2;
1.100     christos 2231:                        int want = (*cp2 == PROPEN) ? PRCLOSE : BRCLOSE;
1.40      sjg      2232:                        int depth = 1;
                   2233:
                   2234:                        for (++cp2; *cp2 != '\0' && depth > 0; ++cp2) {
                   2235:                            if (cp2[-1] != '\\') {
                   2236:                                if (*cp2 == have)
                   2237:                                    ++depth;
                   2238:                                if (*cp2 == want)
                   2239:                                    --depth;
                   2240:                            }
                   2241:                        }
1.146     dsl      2242:                        Buf_AddBytes(&buf, cp2 - cp, cp);
1.40      sjg      2243:                        cp = --cp2;
                   2244:                    } else
1.146     dsl      2245:                        Buf_AddByte(&buf, *cp);
1.40      sjg      2246:                }
1.16      christos 2247:            }
                   2248:        }
                   2249:        else if (pattern && *cp == '&')
1.146     dsl      2250:            Buf_AddBytes(&buf, pattern->leftLen, pattern->lhs);
1.16      christos 2251:        else
1.146     dsl      2252:            Buf_AddByte(&buf, *cp);
1.16      christos 2253:     }
                   2254:
                   2255:     if (*cp != delim) {
                   2256:        *tstr = cp;
                   2257:        *length = 0;
                   2258:        return NULL;
                   2259:     }
1.146     dsl      2260:
                   2261:     *tstr = ++cp;
                   2262:     *length = Buf_Size(&buf);
                   2263:     rstr = Buf_Destroy(&buf, FALSE);
                   2264:     if (DEBUG(VAR))
                   2265:        fprintf(debug_file, "Modifier pattern: \"%s\"\n", rstr);
                   2266:     return rstr;
1.16      christos 2267: }
                   2268:
                   2269: /*-
                   2270:  *-----------------------------------------------------------------------
                   2271:  * VarQuote --
1.194     mlelstv  2272:  *     Quote shell meta-characters and space characters in the string
1.16      christos 2273:  *
                   2274:  * Results:
                   2275:  *     The quoted string
                   2276:  *
                   2277:  * Side Effects:
                   2278:  *     None.
                   2279:  *
                   2280:  *-----------------------------------------------------------------------
                   2281:  */
                   2282: static char *
1.70      wiz      2283: VarQuote(char *str)
1.16      christos 2284: {
                   2285:
                   2286:     Buffer       buf;
1.111     rillig   2287:     const char *newline;
1.193     christos 2288:     size_t nlen;
1.111     rillig   2289:
1.133     joerg    2290:     if ((newline = Shell_GetNewline()) == NULL)
                   2291:            newline = "\\\n";
                   2292:     nlen = strlen(newline);
1.16      christos 2293:
1.146     dsl      2294:     Buf_Init(&buf, 0);
1.193     christos 2295:
                   2296:     for (; *str != '\0'; str++) {
                   2297:        if (*str == '\n') {
1.146     dsl      2298:            Buf_AddBytes(&buf, nlen, newline);
1.193     christos 2299:            continue;
                   2300:        }
1.195     christos 2301:        if (isspace((unsigned char)*str) || ismeta((unsigned char)*str))
1.146     dsl      2302:            Buf_AddByte(&buf, '\\');
1.193     christos 2303:        Buf_AddByte(&buf, *str);
1.16      christos 2304:     }
1.193     christos 2305:
1.146     dsl      2306:     str = Buf_Destroy(&buf, FALSE);
1.137     christos 2307:     if (DEBUG(VAR))
                   2308:        fprintf(debug_file, "QuoteMeta: [%s]\n", str);
1.16      christos 2309:     return str;
                   2310: }
                   2311:
1.68      pk       2312: /*-
                   2313:  *-----------------------------------------------------------------------
1.163     joerg    2314:  * VarHash --
                   2315:  *      Hash the string using the MurmurHash3 algorithm.
                   2316:  *      Output is computed using 32bit Little Endian arithmetic.
                   2317:  *
                   2318:  * Input:
                   2319:  *     str             String to modify
                   2320:  *
                   2321:  * Results:
                   2322:  *      Hash value of str, encoded as 8 hex digits.
                   2323:  *
                   2324:  * Side Effects:
                   2325:  *      None.
                   2326:  *
                   2327:  *-----------------------------------------------------------------------
                   2328:  */
                   2329: static char *
                   2330: VarHash(char *str)
                   2331: {
                   2332:     static const char    hexdigits[16] = "0123456789abcdef";
                   2333:     Buffer         buf;
                   2334:     size_t         len, len2;
                   2335:     unsigned char  *ustr = (unsigned char *)str;
                   2336:     uint32_t       h, k, c1, c2;
                   2337:
                   2338:     h  = 0x971e137bU;
                   2339:     c1 = 0x95543787U;
                   2340:     c2 = 0x2ad7eb25U;
                   2341:     len2 = strlen(str);
                   2342:
                   2343:     for (len = len2; len; ) {
                   2344:        k = 0;
                   2345:        switch (len) {
                   2346:        default:
                   2347:            k = (ustr[3] << 24) | (ustr[2] << 16) | (ustr[1] << 8) | ustr[0];
                   2348:            len -= 4;
                   2349:            ustr += 4;
                   2350:            break;
                   2351:        case 3:
                   2352:            k |= (ustr[2] << 16);
                   2353:        case 2:
                   2354:            k |= (ustr[1] << 8);
                   2355:        case 1:
                   2356:            k |= ustr[0];
                   2357:            len = 0;
                   2358:        }
                   2359:        c1 = c1 * 5 + 0x7b7d159cU;
                   2360:        c2 = c2 * 5 + 0x6bce6396U;
                   2361:        k *= c1;
                   2362:        k = (k << 11) ^ (k >> 21);
                   2363:        k *= c2;
                   2364:        h = (h << 13) ^ (h >> 19);
                   2365:        h = h * 5 + 0x52dce729U;
                   2366:        h ^= k;
1.175     sjg      2367:    }
1.163     joerg    2368:    h ^= len2;
                   2369:    h *= 0x85ebca6b;
                   2370:    h ^= h >> 13;
                   2371:    h *= 0xc2b2ae35;
                   2372:    h ^= h >> 16;
                   2373:
                   2374:    Buf_Init(&buf, 0);
                   2375:    for (len = 0; len < 8; ++len) {
                   2376:        Buf_AddByte(&buf, hexdigits[h & 15]);
                   2377:        h >>= 4;
                   2378:    }
                   2379:
                   2380:    return Buf_Destroy(&buf, FALSE);
                   2381: }
                   2382:
1.164     sjg      2383: static char *
1.209   ! sjg      2384: VarStrftime(const char *fmt, int zulu, time_t utc)
1.164     sjg      2385: {
                   2386:     char buf[BUFSIZ];
                   2387:
1.209   ! sjg      2388:     if (!utc)
        !          2389:        time(&utc);
1.164     sjg      2390:     if (!*fmt)
                   2391:        fmt = "%c";
                   2392:     strftime(buf, sizeof(buf), fmt, zulu ? gmtime(&utc) : localtime(&utc));
                   2393:
                   2394:     buf[sizeof(buf) - 1] = '\0';
                   2395:     return bmake_strdup(buf);
                   2396: }
                   2397:
1.108     sjg      2398: /*
                   2399:  * Now we need to apply any modifiers the user wants applied.
                   2400:  * These are:
                   2401:  *       :M<pattern>   words which match the given <pattern>.
1.138     dsl      2402:  *                     <pattern> is of the standard file
                   2403:  *                     wildcarding form.
1.108     sjg      2404:  *       :N<pattern>   words which do not match the given <pattern>.
                   2405:  *       :S<d><pat1><d><pat2><d>[1gW]
1.138     dsl      2406:  *                     Substitute <pat2> for <pat1> in the value
1.108     sjg      2407:  *       :C<d><pat1><d><pat2><d>[1gW]
1.138     dsl      2408:  *                     Substitute <pat2> for regex <pat1> in the value
                   2409:  *       :H            Substitute the head of each word
                   2410:  *       :T            Substitute the tail of each word
                   2411:  *       :E            Substitute the extension (minus '.') of
                   2412:  *                     each word
                   2413:  *       :R            Substitute the root of each word
                   2414:  *                     (pathname minus the suffix).
                   2415:  *       :O            ("Order") Alphabeticaly sort words in variable.
                   2416:  *       :Ox           ("intermiX") Randomize words in variable.
                   2417:  *       :u            ("uniq") Remove adjacent duplicate words.
                   2418:  *       :tu           Converts the variable contents to uppercase.
                   2419:  *       :tl           Converts the variable contents to lowercase.
                   2420:  *       :ts[c]        Sets varSpace - the char used to
                   2421:  *                     separate words to 'c'. If 'c' is
                   2422:  *                     omitted then no separation is used.
                   2423:  *       :tW           Treat the variable contents as a single
                   2424:  *                     word, even if it contains spaces.
                   2425:  *                     (Mnemonic: one big 'W'ord.)
                   2426:  *       :tw           Treat the variable contents as multiple
                   2427:  *                     space-separated words.
                   2428:  *                     (Mnemonic: many small 'w'ords.)
                   2429:  *       :[index]      Select a single word from the value.
                   2430:  *       :[start..end] Select multiple words from the value.
                   2431:  *       :[*] or :[0]  Select the entire value, as a single
                   2432:  *                     word.  Equivalent to :tW.
                   2433:  *       :[@]          Select the entire value, as multiple
                   2434:  *                     words.  Undoes the effect of :[*].
                   2435:  *                     Equivalent to :tw.
                   2436:  *       :[#]          Returns the number of words in the value.
                   2437:  *
                   2438:  *       :?<true-value>:<false-value>
                   2439:  *                     If the variable evaluates to true, return
                   2440:  *                     true value, else return the second value.
                   2441:  *       :lhs=rhs      Like :S, but the rhs goes to the end of
                   2442:  *                     the invocation.
                   2443:  *       :sh           Treat the current value as a command
                   2444:  *                     to be run, new value is its output.
1.108     sjg      2445:  * The following added so we can handle ODE makefiles.
1.138     dsl      2446:  *       :@<tmpvar>@<newval>@
                   2447:  *                     Assign a temporary local variable <tmpvar>
                   2448:  *                     to the current value of each word in turn
                   2449:  *                     and replace each word with the result of
                   2450:  *                     evaluating <newval>
                   2451:  *       :D<newval>    Use <newval> as value if variable defined
                   2452:  *       :U<newval>    Use <newval> as value if variable undefined
                   2453:  *       :L            Use the name of the variable as the value.
                   2454:  *       :P            Use the path of the node that has the same
                   2455:  *                     name as the variable as the value.  This
                   2456:  *                     basically includes an implied :L so that
                   2457:  *                     the common method of refering to the path
                   2458:  *                     of your dependent 'x' in a rule is to use
                   2459:  *                     the form '${x:P}'.
                   2460:  *       :!<cmd>!      Run cmd much the same as :sh run's the
                   2461:  *                     current value of the variable.
1.108     sjg      2462:  * The ::= modifiers, actually assign a value to the variable.
                   2463:  * Their main purpose is in supporting modifiers of .for loop
                   2464:  * iterators and other obscure uses.  They always expand to
                   2465:  * nothing.  In a target rule that would otherwise expand to an
                   2466:  * empty line they can be preceded with @: to keep make happy.
                   2467:  * Eg.
1.142     dsl      2468:  *
1.108     sjg      2469:  * foo:        .USE
                   2470:  * .for i in ${.TARGET} ${.TARGET:R}.gz
1.138     dsl      2471:  *     @: ${t::=$i}
                   2472:  *     @echo blah ${t:T}
1.108     sjg      2473:  * .endfor
1.142     dsl      2474:  *
1.138     dsl      2475:  *       ::=<str>      Assigns <str> as the new value of variable.
                   2476:  *       ::?=<str>     Assigns <str> as value of variable if
                   2477:  *                     it was not already set.
                   2478:  *       ::+=<str>     Appends <str> to variable.
                   2479:  *       ::!=<cmd>     Assigns output of <cmd> as the new value of
                   2480:  *                     variable.
1.1       cgd      2481:  */
1.108     sjg      2482:
1.164     sjg      2483: /* we now have some modifiers with long names */
                   2484: #define STRMOD_MATCH(s, want, n) \
                   2485:     (strncmp(s, want, n) == 0 && (s[n] == endc || s[n] == ':'))
1.209   ! sjg      2486: #define STRMOD_MATCHX(s, want, n) \
        !          2487:     (strncmp(s, want, n) == 0 && (s[n] == endc || s[n] == ':' || s[n] == '='))
1.164     sjg      2488:
1.108     sjg      2489: static char *
                   2490: ApplyModifiers(char *nstr, const char *tstr,
                   2491:               int startc, int endc,
1.202     christos 2492:               Var *v, GNode *ctxt, int flags,
1.127     christos 2493:               int *lengthPtr, void **freePtr)
1.1       cgd      2494: {
1.108     sjg      2495:     const char            *start;
1.73      christos 2496:     const char     *cp;        /* Secondary pointer into str (place marker
1.1       cgd      2497:                                 * for tstr) */
1.123     apb      2498:     char          *newStr;     /* New value to return */
1.209   ! sjg      2499:     char          *ep;
1.123     apb      2500:     char           termc;      /* Character which terminated scan */
1.9       christos 2501:     int             cnt;       /* Used to count brace pairs when variable in
                   2502:                                 * in parens or braces */
1.108     sjg      2503:     char       delim;
1.112     sjg      2504:     int                modifier;       /* that we are processing */
1.89      jmc      2505:     Var_Parse_State parsestate; /* Flags passed to helper functions */
1.209   ! sjg      2506:     time_t     utc;            /* for VarStrftime */
1.15      christos 2507:
1.108     sjg      2508:     delim = '\0';
1.81      sjg      2509:     parsestate.oneBigWord = FALSE;
                   2510:     parsestate.varSpace = ' '; /* word separator */
1.15      christos 2511:
1.108     sjg      2512:     start = cp = tstr;
1.142     dsl      2513:
1.108     sjg      2514:     while (*tstr && *tstr != endc) {
1.15      christos 2515:
1.108     sjg      2516:        if (*tstr == '$') {
1.142     dsl      2517:            /*
1.167     sjg      2518:             * We may have some complex modifiers in a variable.
1.1       cgd      2519:             */
1.108     sjg      2520:            void *freeIt;
                   2521:            char *rval;
1.127     christos 2522:            int rlen;
1.167     sjg      2523:            int c;
1.22      christos 2524:
1.202     christos 2525:            rval = Var_Parse(tstr, ctxt, flags, &rlen, &freeIt);
1.1       cgd      2526:
1.167     sjg      2527:            /*
                   2528:             * If we have not parsed up to endc or ':',
                   2529:             * we are not interested.
                   2530:             */
                   2531:            if (rval != NULL && *rval &&
                   2532:                (c = tstr[rlen]) != '\0' &&
                   2533:                c != ':' &&
                   2534:                c != endc) {
1.196     christos 2535:                free(freeIt);
1.167     sjg      2536:                goto apply_mods;
                   2537:            }
                   2538:
1.108     sjg      2539:            if (DEBUG(VAR)) {
1.114     dsl      2540:                fprintf(debug_file, "Got '%s' from '%.*s'%.*s\n",
1.127     christos 2541:                       rval, rlen, tstr, rlen, tstr + rlen);
1.22      christos 2542:            }
1.1       cgd      2543:
1.108     sjg      2544:            tstr += rlen;
1.15      christos 2545:
1.108     sjg      2546:            if (rval != NULL && *rval) {
1.127     christos 2547:                int used;
1.15      christos 2548:
1.108     sjg      2549:                nstr = ApplyModifiers(nstr, rval,
1.202     christos 2550:                                      0, 0, v, ctxt, flags, &used, freePtr);
1.108     sjg      2551:                if (nstr == var_Error
1.202     christos 2552:                    || (nstr == varNoError && (flags & VARF_UNDEFERR) == 0)
1.111     rillig   2553:                    || strlen(rval) != (size_t) used) {
1.196     christos 2554:                    free(freeIt);
1.108     sjg      2555:                    goto out;           /* error already reported */
1.1       cgd      2556:                }
                   2557:            }
1.196     christos 2558:            free(freeIt);
1.108     sjg      2559:            if (*tstr == ':')
                   2560:                tstr++;
1.119     sjg      2561:            else if (!*tstr && endc) {
1.145     dsl      2562:                Error("Unclosed variable specification after complex modifier (expecting '%c') for %s", endc, v->name);
1.119     sjg      2563:                goto out;
                   2564:            }
1.108     sjg      2565:            continue;
                   2566:        }
1.167     sjg      2567:     apply_mods:
1.108     sjg      2568:        if (DEBUG(VAR)) {
1.172     christos 2569:            fprintf(debug_file, "Applying[%s] :%c to \"%s\"\n", v->name,
                   2570:                *tstr, nstr);
1.1       cgd      2571:        }
1.108     sjg      2572:        newStr = var_Error;
1.112     sjg      2573:        switch ((modifier = *tstr)) {
1.108     sjg      2574:        case ':':
1.1       cgd      2575:            {
1.45      sjg      2576:                if (tstr[1] == '=' ||
                   2577:                    (tstr[2] == '=' &&
                   2578:                     (tstr[1] == '!' || tstr[1] == '+' || tstr[1] == '?'))) {
1.81      sjg      2579:                    /*
                   2580:                     * "::=", "::!=", "::+=", or "::?="
                   2581:                     */
1.44      sjg      2582:                    GNode *v_ctxt;              /* context where v belongs */
1.73      christos 2583:                    const char *emsg;
1.86      dsl      2584:                    char *sv_name;
1.44      sjg      2585:                    VarPattern  pattern;
                   2586:                    int how;
1.202     christos 2587:                    int vflags;
1.45      sjg      2588:
1.135     dsl      2589:                    if (v->name[0] == 0)
                   2590:                        goto bad_modifier;
                   2591:
1.95      lukem    2592:                    v_ctxt = ctxt;
                   2593:                    sv_name = NULL;
1.45      sjg      2594:                    ++tstr;
1.44      sjg      2595:                    if (v->flags & VAR_JUNK) {
                   2596:                        /*
1.134     joerg    2597:                         * We need to bmake_strdup() it incase
1.44      sjg      2598:                         * VarGetPattern() recurses.
                   2599:                         */
1.86      dsl      2600:                        sv_name = v->name;
1.134     joerg    2601:                        v->name = bmake_strdup(v->name);
1.44      sjg      2602:                    } else if (ctxt != VAR_GLOBAL) {
1.109     christos 2603:                        Var *gv = VarFind(v->name, ctxt, 0);
1.136     dsl      2604:                        if (gv == NULL)
1.44      sjg      2605:                            v_ctxt = VAR_GLOBAL;
1.109     christos 2606:                        else
                   2607:                            VarFreeEnv(gv, TRUE);
1.44      sjg      2608:                    }
1.142     dsl      2609:
1.45      sjg      2610:                    switch ((how = *tstr)) {
1.44      sjg      2611:                    case '+':
                   2612:                    case '?':
                   2613:                    case '!':
                   2614:                        cp = &tstr[2];
                   2615:                        break;
                   2616:                    default:
                   2617:                        cp = ++tstr;
                   2618:                        break;
                   2619:                    }
1.161     christos 2620:                    delim = startc == PROPEN ? PRCLOSE : BRCLOSE;
1.44      sjg      2621:                    pattern.flags = 0;
                   2622:
1.202     christos 2623:                    vflags = (flags & VARF_WANTRES) ? 0 : VAR_NOSUBST;
                   2624:                    pattern.rhs = VarGetPattern(ctxt, &parsestate, flags,
                   2625:                                                &cp, delim, &vflags,
1.108     sjg      2626:                                                &pattern.rightLen,
                   2627:                                                NULL);
1.86      dsl      2628:                    if (v->flags & VAR_JUNK) {
                   2629:                        /* restore original name */
                   2630:                        free(v->name);
                   2631:                        v->name = sv_name;
                   2632:                    }
                   2633:                    if (pattern.rhs == NULL)
1.44      sjg      2634:                        goto cleanup;
1.86      dsl      2635:
1.44      sjg      2636:                    termc = *--cp;
                   2637:                    delim = '\0';
                   2638:
1.202     christos 2639:                    if (flags & VARF_WANTRES) {
1.197     sjg      2640:                        switch (how) {
                   2641:                        case '+':
                   2642:                            Var_Append(v->name, pattern.rhs, v_ctxt);
                   2643:                            break;
                   2644:                        case '!':
                   2645:                            newStr = Cmd_Exec(pattern.rhs, &emsg);
                   2646:                            if (emsg)
                   2647:                                Error(emsg, nstr);
                   2648:                            else
                   2649:                                Var_Set(v->name, newStr,  v_ctxt, 0);
                   2650:                            free(newStr);
                   2651:                            break;
                   2652:                        case '?':
                   2653:                            if ((v->flags & VAR_JUNK) == 0)
                   2654:                                break;
                   2655:                            /* FALLTHROUGH */
                   2656:                        default:
                   2657:                            Var_Set(v->name, pattern.rhs, v_ctxt, 0);
1.47      sjg      2658:                            break;
1.197     sjg      2659:                        }
1.44      sjg      2660:                    }
1.73      christos 2661:                    free(UNCONST(pattern.rhs));
1.186     sjg      2662:                    newStr = varNoError;
1.44      sjg      2663:                    break;
                   2664:                }
1.81      sjg      2665:                goto default_case; /* "::<unrecognised>" */
1.108     sjg      2666:            }
                   2667:        case '@':
                   2668:            {
                   2669:                VarLoop_t       loop;
1.202     christos 2670:                int vflags = VAR_NOSUBST;
1.40      sjg      2671:
1.108     sjg      2672:                cp = ++tstr;
                   2673:                delim = '@';
1.202     christos 2674:                if ((loop.tvar = VarGetPattern(ctxt, &parsestate, flags,
1.108     sjg      2675:                                               &cp, delim,
1.202     christos 2676:                                               &vflags, &loop.tvarLen,
1.108     sjg      2677:                                               NULL)) == NULL)
                   2678:                    goto cleanup;
                   2679:
1.202     christos 2680:                if ((loop.str = VarGetPattern(ctxt, &parsestate, flags,
1.108     sjg      2681:                                              &cp, delim,
1.202     christos 2682:                                              &vflags, &loop.strLen,
1.108     sjg      2683:                                              NULL)) == NULL)
                   2684:                    goto cleanup;
                   2685:
                   2686:                termc = *cp;
                   2687:                delim = '\0';
                   2688:
1.202     christos 2689:                loop.errnum = flags & VARF_UNDEFERR;
1.108     sjg      2690:                loop.ctxt = ctxt;
                   2691:                newStr = VarModify(ctxt, &parsestate, nstr, VarLoopExpand,
1.115     dsl      2692:                                   &loop);
1.204     sjg      2693:                Var_Delete(loop.tvar, ctxt);
1.108     sjg      2694:                free(loop.tvar);
                   2695:                free(loop.str);
                   2696:                break;
                   2697:            }
                   2698:        case 'D':
                   2699:        case 'U':
                   2700:            {
                   2701:                Buffer  buf;            /* Buffer for patterns */
1.202     christos 2702:                int     nflags;
1.40      sjg      2703:
1.202     christos 2704:                if (flags & VARF_WANTRES) {
                   2705:                    int wantres;
1.197     sjg      2706:                    if (*tstr == 'U')
1.202     christos 2707:                        wantres = ((v->flags & VAR_JUNK) != 0);
1.197     sjg      2708:                    else
1.202     christos 2709:                        wantres = ((v->flags & VAR_JUNK) == 0);
                   2710:                    nflags = flags & ~VARF_WANTRES;
                   2711:                    if (wantres)
                   2712:                        nflags |= VARF_WANTRES;
1.197     sjg      2713:                } else
1.202     christos 2714:                    nflags = flags;
1.108     sjg      2715:                /*
                   2716:                 * Pass through tstr looking for 1) escaped delimiters,
                   2717:                 * '$'s and backslashes (place the escaped character in
                   2718:                 * uninterpreted) and 2) unescaped $'s that aren't before
                   2719:                 * the delimiter (expand the variable substitution).
                   2720:                 * The result is left in the Buffer buf.
                   2721:                 */
1.146     dsl      2722:                Buf_Init(&buf, 0);
1.108     sjg      2723:                for (cp = tstr + 1;
                   2724:                     *cp != endc && *cp != ':' && *cp != '\0';
                   2725:                     cp++) {
                   2726:                    if ((*cp == '\\') &&
                   2727:                        ((cp[1] == ':') ||
                   2728:                         (cp[1] == '$') ||
                   2729:                         (cp[1] == endc) ||
                   2730:                         (cp[1] == '\\')))
1.49      mycroft  2731:                        {
1.146     dsl      2732:                            Buf_AddByte(&buf, cp[1]);
1.49      mycroft  2733:                            cp++;
                   2734:                        } else if (*cp == '$') {
                   2735:                            /*
                   2736:                             * If unescaped dollar sign, assume it's a
                   2737:                             * variable substitution and recurse.
                   2738:                             */
                   2739:                            char    *cp2;
1.127     christos 2740:                            int     len;
1.104     christos 2741:                            void    *freeIt;
1.49      mycroft  2742:
1.202     christos 2743:                            cp2 = Var_Parse(cp, ctxt, nflags, &len, &freeIt);
1.146     dsl      2744:                            Buf_AddBytes(&buf, strlen(cp2), cp2);
1.196     christos 2745:                            free(freeIt);
1.49      mycroft  2746:                            cp += len - 1;
                   2747:                        } else {
1.146     dsl      2748:                            Buf_AddByte(&buf, *cp);
1.49      mycroft  2749:                        }
1.108     sjg      2750:                }
1.40      sjg      2751:
1.108     sjg      2752:                termc = *cp;
1.40      sjg      2753:
1.108     sjg      2754:                if ((v->flags & VAR_JUNK) != 0)
                   2755:                    v->flags |= VAR_KEEP;
1.202     christos 2756:                if (nflags & VARF_WANTRES) {
1.146     dsl      2757:                    newStr = Buf_Destroy(&buf, FALSE);
1.108     sjg      2758:                } else {
                   2759:                    newStr = nstr;
1.146     dsl      2760:                    Buf_Destroy(&buf, TRUE);
1.108     sjg      2761:                }
                   2762:                break;
                   2763:            }
                   2764:        case 'L':
                   2765:            {
                   2766:                if ((v->flags & VAR_JUNK) != 0)
                   2767:                    v->flags |= VAR_KEEP;
1.134     joerg    2768:                newStr = bmake_strdup(v->name);
1.108     sjg      2769:                cp = ++tstr;
                   2770:                termc = *tstr;
                   2771:                break;
                   2772:            }
                   2773:        case 'P':
                   2774:            {
                   2775:                GNode *gn;
1.142     dsl      2776:
1.108     sjg      2777:                if ((v->flags & VAR_JUNK) != 0)
                   2778:                    v->flags |= VAR_KEEP;
                   2779:                gn = Targ_FindNode(v->name, TARG_NOCREATE);
1.136     dsl      2780:                if (gn == NULL || gn->type & OP_NOPATH) {
1.108     sjg      2781:                    newStr = NULL;
                   2782:                } else if (gn->path) {
1.134     joerg    2783:                    newStr = bmake_strdup(gn->path);
1.108     sjg      2784:                } else {
                   2785:                    newStr = Dir_FindFile(v->name, Suff_FindPath(gn));
1.40      sjg      2786:                }
1.108     sjg      2787:                if (!newStr) {
1.134     joerg    2788:                    newStr = bmake_strdup(v->name);
1.40      sjg      2789:                }
1.108     sjg      2790:                cp = ++tstr;
                   2791:                termc = *tstr;
                   2792:                break;
                   2793:            }
                   2794:        case '!':
                   2795:            {
                   2796:                const char *emsg;
                   2797:                VarPattern          pattern;
                   2798:                pattern.flags = 0;
                   2799:
                   2800:                delim = '!';
1.199     sjg      2801:                emsg = NULL;
1.108     sjg      2802:                cp = ++tstr;
1.202     christos 2803:                if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, flags,
1.108     sjg      2804:                                                 &cp, delim,
                   2805:                                                 NULL, &pattern.rightLen,
                   2806:                                                 NULL)) == NULL)
                   2807:                    goto cleanup;
1.202     christos 2808:                if (flags & VARF_WANTRES)
1.197     sjg      2809:                    newStr = Cmd_Exec(pattern.rhs, &emsg);
                   2810:                else
                   2811:                    newStr = varNoError;
1.108     sjg      2812:                free(UNCONST(pattern.rhs));
                   2813:                if (emsg)
                   2814:                    Error(emsg, nstr);
                   2815:                termc = *cp;
                   2816:                delim = '\0';
                   2817:                if (v->flags & VAR_JUNK) {
                   2818:                    v->flags |= VAR_KEEP;
                   2819:                }
                   2820:                break;
                   2821:            }
                   2822:        case '[':
                   2823:            {
                   2824:                /*
                   2825:                 * Look for the closing ']', recursively
                   2826:                 * expanding any embedded variables.
                   2827:                 *
                   2828:                 * estr is a pointer to the expanded result,
                   2829:                 * which we must free().
                   2830:                 */
                   2831:                char *estr;
1.142     dsl      2832:
1.108     sjg      2833:                cp = tstr+1; /* point to char after '[' */
                   2834:                delim = ']'; /* look for closing ']' */
                   2835:                estr = VarGetPattern(ctxt, &parsestate,
1.202     christos 2836:                                     flags, &cp, delim,
1.108     sjg      2837:                                     NULL, NULL, NULL);
                   2838:                if (estr == NULL)
                   2839:                    goto cleanup; /* report missing ']' */
                   2840:                /* now cp points just after the closing ']' */
                   2841:                delim = '\0';
                   2842:                if (cp[0] != ':' && cp[0] != endc) {
                   2843:                    /* Found junk after ']' */
                   2844:                    free(estr);
                   2845:                    goto bad_modifier;
                   2846:                }
                   2847:                if (estr[0] == '\0') {
                   2848:                    /* Found empty square brackets in ":[]". */
                   2849:                    free(estr);
                   2850:                    goto bad_modifier;
                   2851:                } else if (estr[0] == '#' && estr[1] == '\0') {
                   2852:                    /* Found ":[#]" */
                   2853:
                   2854:                    /*
                   2855:                     * We will need enough space for the decimal
                   2856:                     * representation of an int.  We calculate the
                   2857:                     * space needed for the octal representation,
                   2858:                     * and add enough slop to cope with a '-' sign
                   2859:                     * (which should never be needed) and a '\0'
                   2860:                     * string terminator.
                   2861:                     */
1.127     christos 2862:                    int newStrSize =
1.108     sjg      2863:                        (sizeof(int) * CHAR_BIT + 2) / 3 + 2;
                   2864:
1.134     joerg    2865:                    newStr = bmake_malloc(newStrSize);
1.108     sjg      2866:                    if (parsestate.oneBigWord) {
1.127     christos 2867:                        strncpy(newStr, "1", newStrSize);
1.103     sjg      2868:                    } else {
1.108     sjg      2869:                        /* XXX: brk_string() is a rather expensive
                   2870:                         * way of counting words. */
                   2871:                        char **av;
                   2872:                        char *as;
1.127     christos 2873:                        int ac;
1.108     sjg      2874:
                   2875:                        av = brk_string(nstr, &ac, FALSE, &as);
1.127     christos 2876:                        snprintf(newStr, newStrSize,  "%d", ac);
1.108     sjg      2877:                        free(as);
                   2878:                        free(av);
1.103     sjg      2879:                    }
1.108     sjg      2880:                    termc = *cp;
                   2881:                    free(estr);
                   2882:                    break;
                   2883:                } else if (estr[0] == '*' && estr[1] == '\0') {
                   2884:                    /* Found ":[*]" */
                   2885:                    parsestate.oneBigWord = TRUE;
                   2886:                    newStr = nstr;
                   2887:                    termc = *cp;
                   2888:                    free(estr);
1.40      sjg      2889:                    break;
1.108     sjg      2890:                } else if (estr[0] == '@' && estr[1] == '\0') {
                   2891:                    /* Found ":[@]" */
                   2892:                    parsestate.oneBigWord = FALSE;
                   2893:                    newStr = nstr;
1.40      sjg      2894:                    termc = *cp;
1.108     sjg      2895:                    free(estr);
1.68      pk       2896:                    break;
1.108     sjg      2897:                } else {
1.81      sjg      2898:                    /*
1.108     sjg      2899:                     * We expect estr to contain a single
                   2900:                     * integer for :[N], or two integers
                   2901:                     * separated by ".." for :[start..end].
1.81      sjg      2902:                     */
1.108     sjg      2903:                    VarSelectWords_t seldata = { 0, 0 };
                   2904:
                   2905:                    seldata.start = strtol(estr, &ep, 0);
                   2906:                    if (ep == estr) {
                   2907:                        /* Found junk instead of a number */
1.81      sjg      2908:                        free(estr);
                   2909:                        goto bad_modifier;
1.108     sjg      2910:                    } else if (ep[0] == '\0') {
                   2911:                        /* Found only one integer in :[N] */
                   2912:                        seldata.end = seldata.start;
                   2913:                    } else if (ep[0] == '.' && ep[1] == '.' &&
                   2914:                               ep[2] != '\0') {
                   2915:                        /* Expecting another integer after ".." */
                   2916:                        ep += 2;
                   2917:                        seldata.end = strtol(ep, &ep, 0);
                   2918:                        if (ep[0] != '\0') {
                   2919:                            /* Found junk after ".." */
                   2920:                            free(estr);
                   2921:                            goto bad_modifier;
                   2922:                        }
                   2923:                    } else {
                   2924:                        /* Found junk instead of ".." */
1.81      sjg      2925:                        free(estr);
                   2926:                        goto bad_modifier;
1.108     sjg      2927:                    }
                   2928:                    /*
                   2929:                     * Now seldata is properly filled in,
                   2930:                     * but we still have to check for 0 as
                   2931:                     * a special case.
                   2932:                     */
                   2933:                    if (seldata.start == 0 && seldata.end == 0) {
                   2934:                        /* ":[0]" or perhaps ":[0..0]" */
1.81      sjg      2935:                        parsestate.oneBigWord = TRUE;
                   2936:                        newStr = nstr;
                   2937:                        termc = *cp;
                   2938:                        free(estr);
                   2939:                        break;
1.108     sjg      2940:                    } else if (seldata.start == 0 ||
                   2941:                               seldata.end == 0) {
                   2942:                        /* ":[0..N]" or ":[N..0]" */
1.81      sjg      2943:                        free(estr);
1.108     sjg      2944:                        goto bad_modifier;
                   2945:                    }
                   2946:                    /*
                   2947:                     * Normal case: select the words
                   2948:                     * described by seldata.
                   2949:                     */
                   2950:                    newStr = VarSelectWords(ctxt, &parsestate,
                   2951:                                            nstr, &seldata);
                   2952:
                   2953:                    termc = *cp;
                   2954:                    free(estr);
                   2955:                    break;
                   2956:                }
                   2957:
                   2958:            }
1.164     sjg      2959:        case 'g':
                   2960:            cp = tstr + 1;      /* make sure it is set */
1.209   ! sjg      2961:            if (STRMOD_MATCHX(tstr, "gmtime", 6)) {
        !          2962:                if (tstr[6] == '=') {
        !          2963:                    utc = strtoul(&tstr[7], &ep, 10);
        !          2964:                    cp = ep;
        !          2965:                } else {
        !          2966:                    utc = 0;
        !          2967:                    cp = tstr + 6;
        !          2968:                }
        !          2969:                newStr = VarStrftime(nstr, 1, utc);
1.164     sjg      2970:                termc = *cp;
                   2971:            } else {
1.165     sjg      2972:                goto default_case;
1.164     sjg      2973:            }
                   2974:            break;
1.163     joerg    2975:        case 'h':
                   2976:            cp = tstr + 1;      /* make sure it is set */
1.164     sjg      2977:            if (STRMOD_MATCH(tstr, "hash", 4)) {
1.163     joerg    2978:                newStr = VarHash(nstr);
                   2979:                cp = tstr + 4;
                   2980:                termc = *cp;
                   2981:            } else {
1.165     sjg      2982:                goto default_case;
1.163     joerg    2983:            }
                   2984:            break;
1.164     sjg      2985:        case 'l':
                   2986:            cp = tstr + 1;      /* make sure it is set */
1.209   ! sjg      2987:            if (STRMOD_MATCHX(tstr, "localtime", 9)) {
        !          2988:                if (tstr[9] == '=') {
        !          2989:                    utc = strtoul(&tstr[10], &ep, 10);
        !          2990:                    cp = ep;
        !          2991:                } else {
        !          2992:                    utc = 0;
        !          2993:                    cp = tstr + 9;
        !          2994:                }
        !          2995:                newStr = VarStrftime(nstr, 0, utc);
1.164     sjg      2996:                termc = *cp;
                   2997:            } else {
1.165     sjg      2998:                goto default_case;
1.164     sjg      2999:            }
                   3000:            break;
1.108     sjg      3001:        case 't':
                   3002:            {
                   3003:                cp = tstr + 1;  /* make sure it is set */
                   3004:                if (tstr[1] != endc && tstr[1] != ':') {
                   3005:                    if (tstr[1] == 's') {
1.81      sjg      3006:                        /*
1.108     sjg      3007:                         * Use the char (if any) at tstr[2]
                   3008:                         * as the word separator.
1.81      sjg      3009:                         */
1.108     sjg      3010:                        VarPattern pattern;
1.81      sjg      3011:
1.108     sjg      3012:                        if (tstr[2] != endc &&
                   3013:                            (tstr[3] == endc || tstr[3] == ':')) {
                   3014:                            /* ":ts<unrecognised><endc>" or
                   3015:                             * ":ts<unrecognised>:" */
                   3016:                            parsestate.varSpace = tstr[2];
                   3017:                            cp = tstr + 3;
                   3018:                        } else if (tstr[2] == endc || tstr[2] == ':') {
                   3019:                            /* ":ts<endc>" or ":ts:" */
                   3020:                            parsestate.varSpace = 0; /* no separator */
                   3021:                            cp = tstr + 2;
                   3022:                        } else if (tstr[2] == '\\') {
1.206     sjg      3023:                            const char *xp = &tstr[3];
                   3024:                            int base = 8; /* assume octal */
                   3025:
1.108     sjg      3026:                            switch (tstr[3]) {
                   3027:                            case 'n':
                   3028:                                parsestate.varSpace = '\n';
                   3029:                                cp = tstr + 4;
                   3030:                                break;
                   3031:                            case 't':
                   3032:                                parsestate.varSpace = '\t';
                   3033:                                cp = tstr + 4;
                   3034:                                break;
1.206     sjg      3035:                            case 'x':
                   3036:                                base = 16;
                   3037:                                xp++;
                   3038:                                goto get_numeric;
                   3039:                            case '0':
                   3040:                                base = 0;
                   3041:                                goto get_numeric;
1.108     sjg      3042:                            default:
                   3043:                                if (isdigit((unsigned char)tstr[3])) {
1.142     dsl      3044:
1.206     sjg      3045:                                get_numeric:
1.127     christos 3046:                                    parsestate.varSpace =
1.206     sjg      3047:                                        strtoul(xp, &ep, base);
1.108     sjg      3048:                                    if (*ep != ':' && *ep != endc)
                   3049:                                        goto bad_modifier;
                   3050:                                    cp = ep;
                   3051:                                } else {
                   3052:                                    /*
                   3053:                                     * ":ts<backslash><unrecognised>".
                   3054:                                     */
                   3055:                                    goto bad_modifier;
                   3056:                                }
                   3057:                                break;
1.81      sjg      3058:                            }
                   3059:                        } else {
1.108     sjg      3060:                            /*
                   3061:                             * Found ":ts<unrecognised><unrecognised>".
                   3062:                             */
1.81      sjg      3063:                            goto bad_modifier;
                   3064:                        }
1.108     sjg      3065:
                   3066:                        termc = *cp;
                   3067:
1.81      sjg      3068:                        /*
1.108     sjg      3069:                         * We cannot be certain that VarModify
                   3070:                         * will be used - even if there is a
                   3071:                         * subsequent modifier, so do a no-op
                   3072:                         * VarSubstitute now to for str to be
                   3073:                         * re-expanded without the spaces.
1.81      sjg      3074:                         */
1.108     sjg      3075:                        pattern.flags = VAR_SUB_ONE;
                   3076:                        pattern.lhs = pattern.rhs = "\032";
                   3077:                        pattern.leftLen = pattern.rightLen = 1;
                   3078:
                   3079:                        newStr = VarModify(ctxt, &parsestate, nstr,
                   3080:                                           VarSubstitute,
1.115     dsl      3081:                                           &pattern);
1.108     sjg      3082:                    } else if (tstr[2] == endc || tstr[2] == ':') {
1.81      sjg      3083:                        /*
1.108     sjg      3084:                         * Check for two-character options:
                   3085:                         * ":tu", ":tl"
1.81      sjg      3086:                         */
1.156     sjg      3087:                        if (tstr[1] == 'A') { /* absolute path */
                   3088:                            newStr = VarModify(ctxt, &parsestate, nstr,
                   3089:                                               VarRealpath, NULL);
                   3090:                            cp = tstr + 2;
                   3091:                            termc = *cp;
1.173     christos 3092:                        } else if (tstr[1] == 'u') {
                   3093:                            char *dp = bmake_strdup(nstr);
                   3094:                            for (newStr = dp; *dp; dp++)
                   3095:                                *dp = toupper((unsigned char)*dp);
                   3096:                            cp = tstr + 2;
                   3097:                            termc = *cp;
                   3098:                        } else if (tstr[1] == 'l') {
                   3099:                            char *dp = bmake_strdup(nstr);
                   3100:                            for (newStr = dp; *dp; dp++)
                   3101:                                *dp = tolower((unsigned char)*dp);
1.108     sjg      3102:                            cp = tstr + 2;
                   3103:                            termc = *cp;
                   3104:                        } else if (tstr[1] == 'W' || tstr[1] == 'w') {
                   3105:                            parsestate.oneBigWord = (tstr[1] == 'W');
                   3106:                            newStr = nstr;
                   3107:                            cp = tstr + 2;
1.74      sjg      3108:                            termc = *cp;
1.81      sjg      3109:                        } else {
1.108     sjg      3110:                            /* Found ":t<unrecognised>:" or
                   3111:                             * ":t<unrecognised><endc>". */
1.95      lukem    3112:                            goto bad_modifier;
1.74      sjg      3113:                        }
1.81      sjg      3114:                    } else {
                   3115:                        /*
1.108     sjg      3116:                         * Found ":t<unrecognised><unrecognised>".
1.81      sjg      3117:                         */
1.95      lukem    3118:                        goto bad_modifier;
1.40      sjg      3119:                    }
1.108     sjg      3120:                } else {
                   3121:                    /*
                   3122:                     * Found ":t<endc>" or ":t:".
                   3123:                     */
                   3124:                    goto bad_modifier;
1.40      sjg      3125:                }
1.108     sjg      3126:                break;
                   3127:            }
                   3128:        case 'N':
                   3129:        case 'M':
                   3130:            {
                   3131:                char    *pattern;
1.123     apb      3132:                const char *endpat; /* points just after end of pattern */
1.108     sjg      3133:                char    *cp2;
1.123     apb      3134:                Boolean copy;   /* pattern should be, or has been, copied */
1.153     sjg      3135:                Boolean needSubst;
1.108     sjg      3136:                int nest;
1.1       cgd      3137:
1.108     sjg      3138:                copy = FALSE;
1.153     sjg      3139:                needSubst = FALSE;
1.108     sjg      3140:                nest = 1;
                   3141:                /*
                   3142:                 * In the loop below, ignore ':' unless we are at
                   3143:                 * (or back to) the original brace level.
                   3144:                 * XXX This will likely not work right if $() and ${}
                   3145:                 * are intermixed.
                   3146:                 */
                   3147:                for (cp = tstr + 1;
                   3148:                     *cp != '\0' && !(*cp == ':' && nest == 1);
                   3149:                     cp++)
1.1       cgd      3150:                    {
1.43      sjg      3151:                        if (*cp == '\\' &&
                   3152:                            (cp[1] == ':' ||
                   3153:                             cp[1] == endc || cp[1] == startc)) {
1.153     sjg      3154:                            if (!needSubst) {
                   3155:                                copy = TRUE;
                   3156:                            }
1.1       cgd      3157:                            cp++;
1.43      sjg      3158:                            continue;
                   3159:                        }
1.153     sjg      3160:                        if (*cp == '$') {
                   3161:                            needSubst = TRUE;
                   3162:                        }
1.143     dsl      3163:                        if (*cp == '(' || *cp == '{')
1.43      sjg      3164:                            ++nest;
1.143     dsl      3165:                        if (*cp == ')' || *cp == '}') {
1.43      sjg      3166:                            --nest;
                   3167:                            if (nest == 0)
                   3168:                                break;
1.1       cgd      3169:                        }
                   3170:                    }
1.108     sjg      3171:                termc = *cp;
1.123     apb      3172:                endpat = cp;
1.108     sjg      3173:                if (copy) {
                   3174:                    /*
                   3175:                     * Need to compress the \:'s out of the pattern, so
                   3176:                     * allocate enough room to hold the uncompressed
                   3177:                     * pattern (note that cp started at tstr+1, so
                   3178:                     * cp - tstr takes the null byte into account) and
                   3179:                     * compress the pattern into the space.
                   3180:                     */
1.134     joerg    3181:                    pattern = bmake_malloc(cp - tstr);
1.108     sjg      3182:                    for (cp2 = pattern, cp = tstr + 1;
1.123     apb      3183:                         cp < endpat;
1.108     sjg      3184:                         cp++, cp2++)
1.1       cgd      3185:                        {
1.123     apb      3186:                            if ((*cp == '\\') && (cp+1 < endpat) &&
1.1       cgd      3187:                                (cp[1] == ':' || cp[1] == endc)) {
1.108     sjg      3188:                                cp++;
1.1       cgd      3189:                            }
                   3190:                            *cp2 = *cp;
                   3191:                        }
1.108     sjg      3192:                    *cp2 = '\0';
1.123     apb      3193:                    endpat = cp2;
1.108     sjg      3194:                } else {
1.123     apb      3195:                    /*
                   3196:                     * Either Var_Subst or VarModify will need a
                   3197:                     * nul-terminated string soon, so construct one now.
                   3198:                     */
1.134     joerg    3199:                    pattern = bmake_strndup(tstr+1, endpat - (tstr + 1));
1.108     sjg      3200:                }
1.153     sjg      3201:                if (needSubst) {
1.123     apb      3202:                    /*
                   3203:                     * pattern contains embedded '$', so use Var_Subst to
                   3204:                     * expand it.
                   3205:                     */
1.108     sjg      3206:                    cp2 = pattern;
1.202     christos 3207:                    pattern = Var_Subst(NULL, cp2, ctxt, flags | VARF_WANTRES);
1.144     dsl      3208:                    free(cp2);
1.108     sjg      3209:                }
1.137     christos 3210:                if (DEBUG(VAR))
1.172     christos 3211:                    fprintf(debug_file, "Pattern[%s] for [%s] is [%s]\n",
                   3212:                        v->name, nstr, pattern);
1.144     dsl      3213:                if (*tstr == 'M') {
1.108     sjg      3214:                    newStr = VarModify(ctxt, &parsestate, nstr, VarMatch,
1.115     dsl      3215:                                       pattern);
1.108     sjg      3216:                } else {
                   3217:                    newStr = VarModify(ctxt, &parsestate, nstr, VarNoMatch,
1.115     dsl      3218:                                       pattern);
1.108     sjg      3219:                }
1.144     dsl      3220:                free(pattern);
1.108     sjg      3221:                break;
                   3222:            }
                   3223:        case 'S':
                   3224:            {
                   3225:                VarPattern          pattern;
                   3226:                Var_Parse_State tmpparsestate;
                   3227:
                   3228:                pattern.flags = 0;
                   3229:                tmpparsestate = parsestate;
                   3230:                delim = tstr[1];
                   3231:                tstr += 2;
                   3232:
                   3233:                /*
                   3234:                 * If pattern begins with '^', it is anchored to the
                   3235:                 * start of the word -- skip over it and flag pattern.
                   3236:                 */
                   3237:                if (*tstr == '^') {
                   3238:                    pattern.flags |= VAR_MATCH_START;
                   3239:                    tstr += 1;
                   3240:                }
                   3241:
                   3242:                cp = tstr;
1.202     christos 3243:                if ((pattern.lhs = VarGetPattern(ctxt, &parsestate, flags,
1.108     sjg      3244:                                                 &cp, delim,
                   3245:                                                 &pattern.flags,
                   3246:                                                 &pattern.leftLen,
                   3247:                                                 NULL)) == NULL)
                   3248:                    goto cleanup;
                   3249:
1.202     christos 3250:                if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, flags,
1.108     sjg      3251:                                                 &cp, delim, NULL,
                   3252:                                                 &pattern.rightLen,
                   3253:                                                 &pattern)) == NULL)
                   3254:                    goto cleanup;
                   3255:
                   3256:                /*
                   3257:                 * Check for global substitution. If 'g' after the final
                   3258:                 * delimiter, substitution is global and is marked that
                   3259:                 * way.
                   3260:                 */
                   3261:                for (;; cp++) {
                   3262:                    switch (*cp) {
                   3263:                    case 'g':
                   3264:                        pattern.flags |= VAR_SUB_GLOBAL;
                   3265:                        continue;
                   3266:                    case '1':
                   3267:                        pattern.flags |= VAR_SUB_ONE;
                   3268:                        continue;
                   3269:                    case 'W':
                   3270:                        tmpparsestate.oneBigWord = TRUE;
                   3271:                        continue;
1.1       cgd      3272:                    }
                   3273:                    break;
                   3274:                }
                   3275:
1.108     sjg      3276:                termc = *cp;
                   3277:                newStr = VarModify(ctxt, &tmpparsestate, nstr,
                   3278:                                   VarSubstitute,
1.115     dsl      3279:                                   &pattern);
1.108     sjg      3280:
                   3281:                /*
                   3282:                 * Free the two strings.
                   3283:                 */
                   3284:                free(UNCONST(pattern.lhs));
                   3285:                free(UNCONST(pattern.rhs));
                   3286:                delim = '\0';
                   3287:                break;
1.142     dsl      3288:            }
1.108     sjg      3289:        case '?':
                   3290:            {
                   3291:                VarPattern      pattern;
                   3292:                Boolean value;
1.197     sjg      3293:                int cond_rc;
                   3294:                int lhs_flags, rhs_flags;
                   3295:
1.108     sjg      3296:                /* find ':', and then substitute accordingly */
1.202     christos 3297:                if (flags & VARF_WANTRES) {
1.198     sjg      3298:                    cond_rc = Cond_EvalExpression(NULL, v->name, &value, 0, FALSE);
                   3299:                    if (cond_rc == COND_INVALID) {
                   3300:                        lhs_flags = rhs_flags = VAR_NOSUBST;
                   3301:                    } else if (value) {
                   3302:                        lhs_flags = 0;
                   3303:                        rhs_flags = VAR_NOSUBST;
                   3304:                    } else {
                   3305:                        lhs_flags = VAR_NOSUBST;
                   3306:                        rhs_flags = 0;
                   3307:                    }
                   3308:                } else {
                   3309:                    /* we are just consuming and discarding */
                   3310:                    cond_rc = value = 0;
1.197     sjg      3311:                    lhs_flags = rhs_flags = VAR_NOSUBST;
                   3312:                }
1.108     sjg      3313:                pattern.flags = 0;
                   3314:
                   3315:                cp = ++tstr;
                   3316:                delim = ':';
1.202     christos 3317:                if ((pattern.lhs = VarGetPattern(ctxt, &parsestate, flags,
1.197     sjg      3318:                                                 &cp, delim, &lhs_flags,
1.108     sjg      3319:                                                 &pattern.leftLen,
                   3320:                                                 NULL)) == NULL)
                   3321:                    goto cleanup;
                   3322:
                   3323:                /* BROPEN or PROPEN */
                   3324:                delim = endc;
1.202     christos 3325:                if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, flags,
1.197     sjg      3326:                                                 &cp, delim, &rhs_flags,
1.108     sjg      3327:                                                 &pattern.rightLen,
                   3328:                                                 NULL)) == NULL)
                   3329:                    goto cleanup;
                   3330:
                   3331:                termc = *--cp;
                   3332:                delim = '\0';
1.197     sjg      3333:                if (cond_rc == COND_INVALID) {
1.108     sjg      3334:                    Error("Bad conditional expression `%s' in %s?%s:%s",
                   3335:                          v->name, v->name, pattern.lhs, pattern.rhs);
                   3336:                    goto cleanup;
                   3337:                }
                   3338:
                   3339:                if (value) {
                   3340:                    newStr = UNCONST(pattern.lhs);
                   3341:                    free(UNCONST(pattern.rhs));
                   3342:                } else {
                   3343:                    newStr = UNCONST(pattern.rhs);
                   3344:                    free(UNCONST(pattern.lhs));
                   3345:                }
                   3346:                if (v->flags & VAR_JUNK) {
                   3347:                    v->flags |= VAR_KEEP;
                   3348:                }
                   3349:                break;
                   3350:            }
                   3351: #ifndef NO_REGEX
                   3352:        case 'C':
                   3353:            {
                   3354:                VarREPattern    pattern;
                   3355:                char           *re;
                   3356:                int             error;
                   3357:                Var_Parse_State tmpparsestate;
                   3358:
                   3359:                pattern.flags = 0;
                   3360:                tmpparsestate = parsestate;
                   3361:                delim = tstr[1];
                   3362:                tstr += 2;
                   3363:
                   3364:                cp = tstr;
                   3365:
1.202     christos 3366:                if ((re = VarGetPattern(ctxt, &parsestate, flags, &cp, delim,
1.108     sjg      3367:                                        NULL, NULL, NULL)) == NULL)
                   3368:                    goto cleanup;
                   3369:
                   3370:                if ((pattern.replace = VarGetPattern(ctxt, &parsestate,
1.202     christos 3371:                                                     flags, &cp, delim, NULL,
1.108     sjg      3372:                                                     NULL, NULL)) == NULL){
                   3373:                    free(re);
                   3374:                    goto cleanup;
                   3375:                }
1.16      christos 3376:
1.108     sjg      3377:                for (;; cp++) {
                   3378:                    switch (*cp) {
                   3379:                    case 'g':
                   3380:                        pattern.flags |= VAR_SUB_GLOBAL;
                   3381:                        continue;
                   3382:                    case '1':
                   3383:                        pattern.flags |= VAR_SUB_ONE;
                   3384:                        continue;
                   3385:                    case 'W':
                   3386:                        tmpparsestate.oneBigWord = TRUE;
                   3387:                        continue;
1.1       cgd      3388:                    }
1.108     sjg      3389:                    break;
                   3390:                }
1.1       cgd      3391:
1.108     sjg      3392:                termc = *cp;
1.16      christos 3393:
1.108     sjg      3394:                error = regcomp(&pattern.re, re, REG_EXTENDED);
                   3395:                free(re);
                   3396:                if (error)  {
                   3397:                    *lengthPtr = cp - start + 1;
                   3398:                    VarREError(error, &pattern.re, "RE substitution error");
                   3399:                    free(pattern.replace);
                   3400:                    goto cleanup;
                   3401:                }
1.15      christos 3402:
1.108     sjg      3403:                pattern.nsub = pattern.re.re_nsub + 1;
                   3404:                if (pattern.nsub < 1)
                   3405:                    pattern.nsub = 1;
                   3406:                if (pattern.nsub > 10)
                   3407:                    pattern.nsub = 10;
1.134     joerg    3408:                pattern.matches = bmake_malloc(pattern.nsub *
1.108     sjg      3409:                                          sizeof(regmatch_t));
                   3410:                newStr = VarModify(ctxt, &tmpparsestate, nstr,
                   3411:                                   VarRESubstitute,
1.115     dsl      3412:                                   &pattern);
1.108     sjg      3413:                regfree(&pattern.re);
                   3414:                free(pattern.replace);
                   3415:                free(pattern.matches);
                   3416:                delim = '\0';
                   3417:                break;
                   3418:            }
                   3419: #endif
                   3420:        case 'Q':
                   3421:            if (tstr[1] == endc || tstr[1] == ':') {
                   3422:                newStr = VarQuote(nstr);
                   3423:                cp = tstr + 1;
                   3424:                termc = *cp;
                   3425:                break;
                   3426:            }
                   3427:            goto default_case;
                   3428:        case 'T':
                   3429:            if (tstr[1] == endc || tstr[1] == ':') {
                   3430:                newStr = VarModify(ctxt, &parsestate, nstr, VarTail,
1.115     dsl      3431:                                   NULL);
1.108     sjg      3432:                cp = tstr + 1;
                   3433:                termc = *cp;
                   3434:                break;
                   3435:            }
                   3436:            goto default_case;
                   3437:        case 'H':
                   3438:            if (tstr[1] == endc || tstr[1] == ':') {
                   3439:                newStr = VarModify(ctxt, &parsestate, nstr, VarHead,
1.115     dsl      3440:                                   NULL);
1.108     sjg      3441:                cp = tstr + 1;
                   3442:                termc = *cp;
                   3443:                break;
                   3444:            }
                   3445:            goto default_case;
                   3446:        case 'E':
                   3447:            if (tstr[1] == endc || tstr[1] == ':') {
                   3448:                newStr = VarModify(ctxt, &parsestate, nstr, VarSuffix,
1.115     dsl      3449:                                   NULL);
1.108     sjg      3450:                cp = tstr + 1;
                   3451:                termc = *cp;
                   3452:                break;
                   3453:            }
                   3454:            goto default_case;
                   3455:        case 'R':
                   3456:            if (tstr[1] == endc || tstr[1] == ':') {
                   3457:                newStr = VarModify(ctxt, &parsestate, nstr, VarRoot,
1.115     dsl      3458:                                   NULL);
1.108     sjg      3459:                cp = tstr + 1;
                   3460:                termc = *cp;
                   3461:                break;
                   3462:            }
                   3463:            goto default_case;
                   3464:        case 'O':
                   3465:            {
                   3466:                char otype;
1.1       cgd      3467:
1.108     sjg      3468:                cp = tstr + 1;  /* skip to the rest in any case */
                   3469:                if (tstr[1] == endc || tstr[1] == ':') {
                   3470:                    otype = 's';
1.16      christos 3471:                    termc = *cp;
1.108     sjg      3472:                } else if ( (tstr[1] == 'x') &&
                   3473:                            (tstr[2] == endc || tstr[2] == ':') ) {
                   3474:                    otype = tstr[1];
                   3475:                    cp = tstr + 2;
                   3476:                    termc = *cp;
                   3477:                } else {
                   3478:                    goto bad_modifier;
                   3479:                }
                   3480:                newStr = VarOrder(nstr, otype);
                   3481:                break;
                   3482:            }
                   3483:        case 'u':
                   3484:            if (tstr[1] == endc || tstr[1] == ':') {
                   3485:                newStr = VarUniq(nstr);
                   3486:                cp = tstr + 1;
                   3487:                termc = *cp;
                   3488:                break;
                   3489:            }
                   3490:            goto default_case;
                   3491: #ifdef SUNSHCMD
                   3492:        case 's':
                   3493:            if (tstr[1] == 'h' && (tstr[2] == endc || tstr[2] == ':')) {
                   3494:                const char *emsg;
1.202     christos 3495:                if (flags & VARF_WANTRES) {
1.197     sjg      3496:                    newStr = Cmd_Exec(nstr, &emsg);
                   3497:                    if (emsg)
                   3498:                        Error(emsg, nstr);
                   3499:                } else
                   3500:                    newStr = varNoError;
1.108     sjg      3501:                cp = tstr + 2;
                   3502:                termc = *cp;
                   3503:                break;
                   3504:            }
                   3505:            goto default_case;
                   3506: #endif
                   3507:        default:
1.142     dsl      3508:        default_case:
1.108     sjg      3509:        {
                   3510: #ifdef SYSVVARSUB
                   3511:            /*
                   3512:             * This can either be a bogus modifier or a System-V
                   3513:             * substitution command.
                   3514:             */
                   3515:            VarPattern      pattern;
                   3516:            Boolean         eqFound;
1.15      christos 3517:
1.108     sjg      3518:            pattern.flags = 0;
                   3519:            eqFound = FALSE;
                   3520:            /*
                   3521:             * First we make a pass through the string trying
                   3522:             * to verify it is a SYSV-make-style translation:
                   3523:             * it must be: <string1>=<string2>)
                   3524:             */
                   3525:            cp = tstr;
                   3526:            cnt = 1;
                   3527:            while (*cp != '\0' && cnt) {
                   3528:                if (*cp == '=') {
                   3529:                    eqFound = TRUE;
                   3530:                    /* continue looking for endc */
                   3531:                }
                   3532:                else if (*cp == endc)
                   3533:                    cnt--;
                   3534:                else if (*cp == startc)
                   3535:                    cnt++;
                   3536:                if (cnt)
                   3537:                    cp++;
                   3538:            }
                   3539:            if (*cp == endc && eqFound) {
1.25      christos 3540:
1.108     sjg      3541:                /*
                   3542:                 * Now we break this sucker into the lhs and
                   3543:                 * rhs. We must null terminate them of course.
                   3544:                 */
                   3545:                delim='=';
                   3546:                cp = tstr;
1.142     dsl      3547:                if ((pattern.lhs = VarGetPattern(ctxt, &parsestate,
1.202     christos 3548:                                                 flags, &cp, delim, &pattern.flags,
1.108     sjg      3549:                                                 &pattern.leftLen, NULL)) == NULL)
                   3550:                    goto cleanup;
                   3551:                delim = endc;
                   3552:                if ((pattern.rhs = VarGetPattern(ctxt, &parsestate,
1.202     christos 3553:                                                 flags, &cp, delim, NULL, &pattern.rightLen,
1.108     sjg      3554:                                                 &pattern)) == NULL)
                   3555:                    goto cleanup;
1.25      christos 3556:
1.108     sjg      3557:                /*
                   3558:                 * SYSV modifications happen through the whole
                   3559:                 * string. Note the pattern is anchored at the end.
                   3560:                 */
                   3561:                termc = *--cp;
                   3562:                delim = '\0';
1.162     sjg      3563:                if (pattern.leftLen == 0 && *nstr == '\0') {
                   3564:                    newStr = nstr;      /* special case */
                   3565:                } else {
                   3566:                    newStr = VarModify(ctxt, &parsestate, nstr,
                   3567:                                       VarSYSVMatch,
                   3568:                                       &pattern);
                   3569:                }
1.108     sjg      3570:                free(UNCONST(pattern.lhs));
                   3571:                free(UNCONST(pattern.rhs));
                   3572:            } else
                   3573: #endif
                   3574:                {
                   3575:                    Error("Unknown modifier '%c'", *tstr);
                   3576:                    for (cp = tstr+1;
                   3577:                         *cp != ':' && *cp != endc && *cp != '\0';
                   3578:                         cp++)
                   3579:                        continue;
                   3580:                    termc = *cp;
                   3581:                    newStr = var_Error;
                   3582:                }
                   3583:            }
                   3584:        }
                   3585:        if (DEBUG(VAR)) {
1.172     christos 3586:            fprintf(debug_file, "Result[%s] of :%c is \"%s\"\n",
                   3587:                v->name, modifier, newStr);
1.108     sjg      3588:        }
1.25      christos 3589:
1.108     sjg      3590:        if (newStr != nstr) {
                   3591:            if (*freePtr) {
                   3592:                free(nstr);
                   3593:                *freePtr = NULL;
                   3594:            }
                   3595:            nstr = newStr;
                   3596:            if (nstr != var_Error && nstr != varNoError) {
                   3597:                *freePtr = nstr;
                   3598:            }
                   3599:        }
                   3600:        if (termc == '\0' && endc != '\0') {
1.145     dsl      3601:            Error("Unclosed variable specification (expecting '%c') for \"%s\" (value \"%s\") modifier %c", endc, v->name, nstr, modifier);
1.108     sjg      3602:        } else if (termc == ':') {
                   3603:            cp++;
                   3604:        }
                   3605:        tstr = cp;
                   3606:     }
                   3607:  out:
                   3608:     *lengthPtr = tstr - start;
                   3609:     return (nstr);
1.25      christos 3610:
1.108     sjg      3611:  bad_modifier:
                   3612:     /* "{(" */
                   3613:     Error("Bad modifier `:%.*s' for %s", (int)strcspn(tstr, ":)}"), tstr,
                   3614:          v->name);
1.25      christos 3615:
1.108     sjg      3616:  cleanup:
                   3617:     *lengthPtr = cp - start;
                   3618:     if (delim != '\0')
                   3619:        Error("Unclosed substitution for %s (%c missing)",
                   3620:              v->name, delim);
1.196     christos 3621:     free(*freePtr);
                   3622:     *freePtr = NULL;
1.108     sjg      3623:     return (var_Error);
                   3624: }
1.25      christos 3625:
1.108     sjg      3626: /*-
                   3627:  *-----------------------------------------------------------------------
                   3628:  * Var_Parse --
                   3629:  *     Given the start of a variable invocation, extract the variable
                   3630:  *     name and find its value, then modify it according to the
                   3631:  *     specification.
                   3632:  *
                   3633:  * Input:
                   3634:  *     str             The string to parse
                   3635:  *     ctxt            The context for the variable
1.202     christos 3636:  *     flags           VARF_UNDEFERR   if undefineds are an error
                   3637:  *                     VARF_WANTRES    if we actually want the result
                   3638:  *                     VARF_ASSIGN     if we are in a := assignment
1.108     sjg      3639:  *     lengthPtr       OUT: The length of the specification
1.121     apb      3640:  *     freePtr         OUT: Non-NULL if caller should free *freePtr
1.108     sjg      3641:  *
                   3642:  * Results:
                   3643:  *     The (possibly-modified) value of the variable or var_Error if the
                   3644:  *     specification is invalid. The length of the specification is
                   3645:  *     placed in *lengthPtr (for invalid specifications, this is just
                   3646:  *     2...?).
1.121     apb      3647:  *     If *freePtr is non-NULL then it's a pointer that the caller
                   3648:  *     should pass to free() to free memory used by the result.
1.108     sjg      3649:  *
                   3650:  * Side Effects:
                   3651:  *     None.
                   3652:  *
                   3653:  *-----------------------------------------------------------------------
                   3654:  */
                   3655: /* coverity[+alloc : arg-*4] */
                   3656: char *
1.202     christos 3657: Var_Parse(const char *str, GNode *ctxt, int flags,
                   3658:          int *lengthPtr, void **freePtr)
1.108     sjg      3659: {
                   3660:     const char    *tstr;       /* Pointer into str */
1.138     dsl      3661:     Var                   *v;          /* Variable in invocation */
1.108     sjg      3662:     Boolean        haveModifier;/* TRUE if have modifiers for the variable */
                   3663:     char           endc;       /* Ending character when variable in parens
                   3664:                                 * or braces */
1.141     dsl      3665:     char           startc;     /* Starting character when variable in parens
1.108     sjg      3666:                                 * or braces */
1.127     christos 3667:     int                    vlen;       /* Length of variable name */
1.123     apb      3668:     const char            *start;      /* Points to original start of str */
                   3669:     char          *nstr;       /* New string, used during expansion */
1.108     sjg      3670:     Boolean        dynamic;    /* TRUE if the variable is local and we're
                   3671:                                 * expanding it in a non-local context. This
                   3672:                                 * is done to support dynamic sources. The
                   3673:                                 * result is just the invocation, unaltered */
1.191     dholland 3674:     const char     *extramodifiers; /* extra modifiers to apply first */
1.141     dsl      3675:     char         name[2];
1.1       cgd      3676:
1.108     sjg      3677:     *freePtr = NULL;
1.191     dholland 3678:     extramodifiers = NULL;
1.108     sjg      3679:     dynamic = FALSE;
                   3680:     start = str;
1.1       cgd      3681:
1.141     dsl      3682:     startc = str[1];
                   3683:     if (startc != PROPEN && startc != BROPEN) {
1.108     sjg      3684:        /*
                   3685:         * If it's not bounded by braces of some sort, life is much simpler.
                   3686:         * We just need to check for the first character and return the
                   3687:         * value if it exists.
                   3688:         */
1.15      christos 3689:
1.141     dsl      3690:        /* Error out some really stupid names */
                   3691:        if (startc == '\0' || strchr(")}:$", startc)) {
                   3692:            *lengthPtr = 1;
                   3693:            return var_Error;
                   3694:        }
                   3695:        name[0] = startc;
1.108     sjg      3696:        name[1] = '\0';
1.16      christos 3697:
1.108     sjg      3698:        v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
1.136     dsl      3699:        if (v == NULL) {
1.108     sjg      3700:            *lengthPtr = 2;
1.1       cgd      3701:
1.108     sjg      3702:            if ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) {
                   3703:                /*
                   3704:                 * If substituting a local variable in a non-local context,
                   3705:                 * assume it's for dynamic source stuff. We have to handle
                   3706:                 * this specially and return the longhand for the variable
                   3707:                 * with the dollar sign escaped so it makes it back to the
                   3708:                 * caller. Only four of the local variables are treated
                   3709:                 * specially as they are the only four that will be set
                   3710:                 * when dynamic sources are expanded.
                   3711:                 */
                   3712:                switch (str[1]) {
                   3713:                    case '@':
                   3714:                        return UNCONST("$(.TARGET)");
                   3715:                    case '%':
1.207     matthias 3716:                        return UNCONST("$(.MEMBER)");
1.108     sjg      3717:                    case '*':
                   3718:                        return UNCONST("$(.PREFIX)");
                   3719:                    case '!':
1.207     matthias 3720:                        return UNCONST("$(.ARCHIVE)");
1.108     sjg      3721:                }
                   3722:            }
                   3723:            /*
                   3724:             * Error
                   3725:             */
1.202     christos 3726:            return (flags & VARF_UNDEFERR) ? var_Error : varNoError;
1.108     sjg      3727:        } else {
                   3728:            haveModifier = FALSE;
                   3729:            tstr = &str[1];
                   3730:            endc = str[1];
                   3731:        }
1.123     apb      3732:     } else {
1.108     sjg      3733:        Buffer buf;     /* Holds the variable name */
1.185     sjg      3734:        int depth = 1;
1.1       cgd      3735:
1.108     sjg      3736:        endc = startc == PROPEN ? PRCLOSE : BRCLOSE;
1.146     dsl      3737:        Buf_Init(&buf, 0);
1.1       cgd      3738:
1.108     sjg      3739:        /*
                   3740:         * Skip to the end character or a colon, whichever comes first.
                   3741:         */
1.185     sjg      3742:        for (tstr = str + 2; *tstr != '\0'; tstr++)
1.108     sjg      3743:        {
                   3744:            /*
1.185     sjg      3745:             * Track depth so we can spot parse errors.
                   3746:             */
                   3747:            if (*tstr == startc) {
                   3748:                depth++;
                   3749:            }
                   3750:            if (*tstr == endc) {
                   3751:                if (--depth == 0)
                   3752:                    break;
                   3753:            }
                   3754:            if (depth == 1 && *tstr == ':') {
                   3755:                break;
                   3756:            }
                   3757:            /*
1.108     sjg      3758:             * A variable inside a variable, expand
                   3759:             */
                   3760:            if (*tstr == '$') {
1.127     christos 3761:                int rlen;
1.108     sjg      3762:                void *freeIt;
1.202     christos 3763:                char *rval = Var_Parse(tstr, ctxt, flags,  &rlen, &freeIt);
1.108     sjg      3764:                if (rval != NULL) {
1.146     dsl      3765:                    Buf_AddBytes(&buf, strlen(rval), rval);
1.1       cgd      3766:                }
1.196     christos 3767:                free(freeIt);
1.108     sjg      3768:                tstr += rlen - 1;
                   3769:            }
                   3770:            else
1.146     dsl      3771:                Buf_AddByte(&buf, *tstr);
1.108     sjg      3772:        }
                   3773:        if (*tstr == ':') {
                   3774:            haveModifier = TRUE;
1.185     sjg      3775:        } else if (*tstr == endc) {
1.108     sjg      3776:            haveModifier = FALSE;
                   3777:        } else {
                   3778:            /*
                   3779:             * If we never did find the end character, return NULL
                   3780:             * right now, setting the length to be the distance to
                   3781:             * the end of the string, since that's what make does.
                   3782:             */
                   3783:            *lengthPtr = tstr - str;
1.146     dsl      3784:            Buf_Destroy(&buf, TRUE);
1.108     sjg      3785:            return (var_Error);
                   3786:        }
1.146     dsl      3787:        str = Buf_GetAll(&buf, &vlen);
1.93      sjg      3788:
1.123     apb      3789:        /*
                   3790:         * At this point, str points into newly allocated memory from
                   3791:         * buf, containing only the name of the variable.
                   3792:         *
                   3793:         * start and tstr point into the const string that was pointed
                   3794:         * to by the original value of the str parameter.  start points
                   3795:         * to the '$' at the beginning of the string, while tstr points
                   3796:         * to the char just after the end of the variable name -- this
                   3797:         * will be '\0', ':', PRCLOSE, or BRCLOSE.
                   3798:         */
                   3799:
1.108     sjg      3800:        v = VarFind(str, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
1.141     dsl      3801:        /*
                   3802:         * Check also for bogus D and F forms of local variables since we're
                   3803:         * in a local context and the name is the right length.
                   3804:         */
1.136     dsl      3805:        if ((v == NULL) && (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) &&
1.141     dsl      3806:                (vlen == 2) && (str[1] == 'F' || str[1] == 'D') &&
1.190     dholland 3807:                strchr("@%?*!<>", str[0]) != NULL) {
1.108     sjg      3808:            /*
1.141     dsl      3809:             * Well, it's local -- go look for it.
1.108     sjg      3810:             */
1.141     dsl      3811:            name[0] = *str;
                   3812:            name[1] = '\0';
                   3813:            v = VarFind(name, ctxt, 0);
1.15      christos 3814:
1.141     dsl      3815:            if (v != NULL) {
                   3816:                if (str[1] == 'D') {
1.191     dholland 3817:                        extramodifiers = "H:";
                   3818:                }
                   3819:                else { /* F */
                   3820:                        extramodifiers = "T:";
1.1       cgd      3821:                }
                   3822:            }
1.108     sjg      3823:        }
                   3824:
1.136     dsl      3825:        if (v == NULL) {
1.108     sjg      3826:            if (((vlen == 1) ||
1.141     dsl      3827:                 (((vlen == 2) && (str[1] == 'F' || str[1] == 'D')))) &&
1.108     sjg      3828:                ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)))
                   3829:            {
                   3830:                /*
                   3831:                 * If substituting a local variable in a non-local context,
                   3832:                 * assume it's for dynamic source stuff. We have to handle
                   3833:                 * this specially and return the longhand for the variable
                   3834:                 * with the dollar sign escaped so it makes it back to the
                   3835:                 * caller. Only four of the local variables are treated
                   3836:                 * specially as they are the only four that will be set
                   3837:                 * when dynamic sources are expanded.
                   3838:                 */
                   3839:                switch (*str) {
                   3840:                    case '@':
                   3841:                    case '%':
                   3842:                    case '*':
                   3843:                    case '!':
                   3844:                        dynamic = TRUE;
                   3845:                        break;
                   3846:                }
                   3847:            } else if ((vlen > 2) && (*str == '.') &&
                   3848:                       isupper((unsigned char) str[1]) &&
                   3849:                       ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)))
                   3850:            {
1.127     christos 3851:                int     len;
1.108     sjg      3852:
                   3853:                len = vlen - 1;
                   3854:                if ((strncmp(str, ".TARGET", len) == 0) ||
                   3855:                    (strncmp(str, ".ARCHIVE", len) == 0) ||
                   3856:                    (strncmp(str, ".PREFIX", len) == 0) ||
                   3857:                    (strncmp(str, ".MEMBER", len) == 0))
                   3858:                {
                   3859:                    dynamic = TRUE;
                   3860:                }
1.1       cgd      3861:            }
1.15      christos 3862:
1.108     sjg      3863:            if (!haveModifier) {
                   3864:                /*
                   3865:                 * No modifiers -- have specification length so we can return
                   3866:                 * now.
                   3867:                 */
                   3868:                *lengthPtr = tstr - start + 1;
                   3869:                if (dynamic) {
1.134     joerg    3870:                    char *pstr = bmake_strndup(start, *lengthPtr);
1.108     sjg      3871:                    *freePtr = pstr;
1.146     dsl      3872:                    Buf_Destroy(&buf, TRUE);
1.108     sjg      3873:                    return(pstr);
1.40      sjg      3874:                } else {
1.146     dsl      3875:                    Buf_Destroy(&buf, TRUE);
1.202     christos 3876:                    return (flags & VARF_UNDEFERR) ? var_Error : varNoError;
1.40      sjg      3877:                }
1.1       cgd      3878:            } else {
1.108     sjg      3879:                /*
                   3880:                 * Still need to get to the end of the variable specification,
                   3881:                 * so kludge up a Var structure for the modifications
                   3882:                 */
1.134     joerg    3883:                v = bmake_malloc(sizeof(Var));
1.108     sjg      3884:                v->name = UNCONST(str);
1.146     dsl      3885:                Buf_Init(&v->val, 1);
1.108     sjg      3886:                v->flags = VAR_JUNK;
1.146     dsl      3887:                Buf_Destroy(&buf, FALSE);
1.1       cgd      3888:            }
1.108     sjg      3889:        } else
1.146     dsl      3890:            Buf_Destroy(&buf, TRUE);
1.108     sjg      3891:     }
                   3892:
1.188     joerg    3893:     if (v->flags & VAR_IN_USE) {
                   3894:        Fatal("Variable %s is recursive.", v->name);
                   3895:        /*NOTREACHED*/
                   3896:     } else {
                   3897:        v->flags |= VAR_IN_USE;
                   3898:     }
                   3899:     /*
                   3900:      * Before doing any modification, we have to make sure the value
                   3901:      * has been fully expanded. If it looks like recursion might be
                   3902:      * necessary (there's a dollar sign somewhere in the variable's value)
                   3903:      * we just call Var_Subst to do any other substitutions that are
                   3904:      * necessary. Note that the value returned by Var_Subst will have
                   3905:      * been dynamically-allocated, so it will need freeing when we
                   3906:      * return.
                   3907:      */
                   3908:     nstr = Buf_GetAll(&v->val, NULL);
                   3909:     if (strchr(nstr, '$') != NULL) {
1.202     christos 3910:        nstr = Var_Subst(NULL, nstr, ctxt, flags);
1.188     joerg    3911:        *freePtr = nstr;
                   3912:     }
1.187     christos 3913:
1.188     joerg    3914:     v->flags &= ~VAR_IN_USE;
1.108     sjg      3915:
1.191     dholland 3916:     if ((nstr != NULL) && (haveModifier || extramodifiers != NULL)) {
                   3917:        void *extraFree;
1.127     christos 3918:        int used;
1.108     sjg      3919:
1.191     dholland 3920:        extraFree = NULL;
                   3921:        if (extramodifiers != NULL) {
                   3922:                nstr = ApplyModifiers(nstr, extramodifiers, '(', ')',
1.202     christos 3923:                                      v, ctxt, flags, &used, &extraFree);
1.191     dholland 3924:        }
                   3925:
                   3926:        if (haveModifier) {
                   3927:                /* Skip initial colon. */
                   3928:                tstr++;
                   3929:
                   3930:                nstr = ApplyModifiers(nstr, tstr, startc, endc,
1.202     christos 3931:                                      v, ctxt, flags, &used, freePtr);
1.191     dholland 3932:                tstr += used;
1.196     christos 3933:                free(extraFree);
1.191     dholland 3934:        } else {
                   3935:                *freePtr = extraFree;
                   3936:        }
1.119     sjg      3937:     }
                   3938:     if (*tstr) {
1.1       cgd      3939:        *lengthPtr = tstr - start + 1;
                   3940:     } else {
1.119     sjg      3941:        *lengthPtr = tstr - start;
1.1       cgd      3942:     }
1.15      christos 3943:
1.1       cgd      3944:     if (v->flags & VAR_FROM_ENV) {
                   3945:        Boolean   destroy = FALSE;
1.15      christos 3946:
1.146     dsl      3947:        if (nstr != Buf_GetAll(&v->val, NULL)) {
1.1       cgd      3948:            destroy = TRUE;
                   3949:        } else {
                   3950:            /*
                   3951:             * Returning the value unmodified, so tell the caller to free
                   3952:             * the thing.
                   3953:             */
1.104     christos 3954:            *freePtr = nstr;
1.1       cgd      3955:        }
1.105     christos 3956:        VarFreeEnv(v, destroy);
1.1       cgd      3957:     } else if (v->flags & VAR_JUNK) {
                   3958:        /*
1.121     apb      3959:         * Perform any free'ing needed and set *freePtr to NULL so the caller
1.1       cgd      3960:         * doesn't try to free a static pointer.
1.40      sjg      3961:         * If VAR_KEEP is also set then we want to keep str as is.
1.1       cgd      3962:         */
1.58      itojun   3963:        if (!(v->flags & VAR_KEEP)) {
1.40      sjg      3964:            if (*freePtr) {
1.73      christos 3965:                free(nstr);
1.108     sjg      3966:                *freePtr = NULL;
1.40      sjg      3967:            }
                   3968:            if (dynamic) {
1.134     joerg    3969:                nstr = bmake_strndup(start, *lengthPtr);
1.104     christos 3970:                *freePtr = nstr;
1.40      sjg      3971:            } else {
1.202     christos 3972:                nstr = (flags & VARF_UNDEFERR) ? var_Error : varNoError;
1.40      sjg      3973:            }
1.34      christos 3974:        }
1.146     dsl      3975:        if (nstr != Buf_GetAll(&v->val, NULL))
                   3976:            Buf_Destroy(&v->val, TRUE);
1.98      christos 3977:        free(v->name);
                   3978:        free(v);
1.1       cgd      3979:     }
1.73      christos 3980:     return (nstr);
1.1       cgd      3981: }
                   3982:
                   3983: /*-
                   3984:  *-----------------------------------------------------------------------
                   3985:  * Var_Subst  --
                   3986:  *     Substitute for all variables in the given string in the given context
1.202     christos 3987:  *     If flags & VARF_UNDEFERR, Parse_Error will be called when an undefined
1.1       cgd      3988:  *     variable is encountered.
                   3989:  *
1.70      wiz      3990:  * Input:
                   3991:  *     var             Named variable || NULL for all
                   3992:  *     str             the string which to substitute
                   3993:  *     ctxt            the context wherein to find variables
1.202     christos 3994:  *     flags           VARF_UNDEFERR   if undefineds are an error
                   3995:  *                     VARF_WANTRES    if we actually want the result
                   3996:  *                     VARF_ASSIGN     if we are in a := assignment
1.70      wiz      3997:  *
1.1       cgd      3998:  * Results:
                   3999:  *     The resulting string.
                   4000:  *
                   4001:  * Side Effects:
                   4002:  *     None. The old string must be freed by the caller
                   4003:  *-----------------------------------------------------------------------
                   4004:  */
                   4005: char *
1.202     christos 4006: Var_Subst(const char *var, const char *str, GNode *ctxt, int flags)
1.1       cgd      4007: {
1.138     dsl      4008:     Buffer       buf;              /* Buffer for forming things */
1.1       cgd      4009:     char         *val;             /* Value to substitute for a variable */
1.138     dsl      4010:     int                  length;           /* Length of the variable invocation */
1.96      christos 4011:     Boolean      trailingBslash;   /* variable ends in \ */
1.104     christos 4012:     void         *freeIt = NULL;    /* Set if it should be freed */
1.1       cgd      4013:     static Boolean errorReported;   /* Set true if an error has already
                   4014:                                     * been reported to prevent a plethora
                   4015:                                     * of messages when recursing */
                   4016:
1.146     dsl      4017:     Buf_Init(&buf, 0);
1.1       cgd      4018:     errorReported = FALSE;
1.96      christos 4019:     trailingBslash = FALSE;
1.1       cgd      4020:
                   4021:     while (*str) {
1.96      christos 4022:        if (*str == '\n' && trailingBslash)
1.146     dsl      4023:            Buf_AddByte(&buf, ' ');
1.48      mycroft  4024:        if (var == NULL && (*str == '$') && (str[1] == '$')) {
1.1       cgd      4025:            /*
                   4026:             * A dollar sign may be escaped either with another dollar sign.
                   4027:             * In such a case, we skip over the escape character and store the
                   4028:             * dollar sign into the buffer directly.
                   4029:             */
1.205     sjg      4030:            if (save_dollars && (flags & VARF_ASSIGN))
1.201     christos 4031:                Buf_AddByte(&buf, *str);
1.1       cgd      4032:            str++;
1.146     dsl      4033:            Buf_AddByte(&buf, *str);
1.1       cgd      4034:            str++;
                   4035:        } else if (*str != '$') {
                   4036:            /*
                   4037:             * Skip as many characters as possible -- either to the end of
                   4038:             * the string or to the next dollar sign (variable invocation).
                   4039:             */
1.73      christos 4040:            const char  *cp;
1.1       cgd      4041:
1.5       cgd      4042:            for (cp = str++; *str != '$' && *str != '\0'; str++)
                   4043:                continue;
1.146     dsl      4044:            Buf_AddBytes(&buf, str - cp, cp);
1.1       cgd      4045:        } else {
1.48      mycroft  4046:            if (var != NULL) {
                   4047:                int expand;
                   4048:                for (;;) {
1.100     christos 4049:                    if (str[1] == '\0') {
                   4050:                        /* A trailing $ is kind of a special case */
1.146     dsl      4051:                        Buf_AddByte(&buf, str[0]);
1.100     christos 4052:                        str++;
                   4053:                        expand = FALSE;
                   4054:                    } else if (str[1] != PROPEN && str[1] != BROPEN) {
1.48      mycroft  4055:                        if (str[1] != *var || strlen(var) > 1) {
1.146     dsl      4056:                            Buf_AddBytes(&buf, 2, str);
1.48      mycroft  4057:                            str += 2;
                   4058:                            expand = FALSE;
                   4059:                        }
                   4060:                        else
                   4061:                            expand = TRUE;
                   4062:                        break;
                   4063:                    }
                   4064:                    else {
1.73      christos 4065:                        const char *p;
1.48      mycroft  4066:
                   4067:                        /*
                   4068:                         * Scan up to the end of the variable name.
                   4069:                         */
                   4070:                        for (p = &str[2]; *p &&
1.100     christos 4071:                             *p != ':' && *p != PRCLOSE && *p != BRCLOSE; p++)
1.48      mycroft  4072:                            if (*p == '$')
                   4073:                                break;
                   4074:                        /*
                   4075:                         * A variable inside the variable. We cannot expand
                   4076:                         * the external variable yet, so we try again with
                   4077:                         * the nested one
                   4078:                         */
                   4079:                        if (*p == '$') {
1.146     dsl      4080:                            Buf_AddBytes(&buf, p - str, str);
1.48      mycroft  4081:                            str = p;
                   4082:                            continue;
                   4083:                        }
                   4084:
1.127     christos 4085:                        if (strncmp(var, str + 2, p - str - 2) != 0 ||
1.48      mycroft  4086:                            var[p - str - 2] != '\0') {
                   4087:                            /*
                   4088:                             * Not the variable we want to expand, scan
                   4089:                             * until the next variable
                   4090:                             */
                   4091:                            for (;*p != '$' && *p != '\0'; p++)
                   4092:                                continue;
1.146     dsl      4093:                            Buf_AddBytes(&buf, p - str, str);
1.48      mycroft  4094:                            str = p;
                   4095:                            expand = FALSE;
                   4096:                        }
                   4097:                        else
                   4098:                            expand = TRUE;
                   4099:                        break;
                   4100:                    }
                   4101:                }
                   4102:                if (!expand)
                   4103:                    continue;
                   4104:            }
                   4105:
1.202     christos 4106:            val = Var_Parse(str, ctxt, flags, &length, &freeIt);
1.1       cgd      4107:
                   4108:            /*
                   4109:             * When we come down here, val should either point to the
                   4110:             * value of this variable, suitably modified, or be NULL.
                   4111:             * Length should be the total length of the potential
                   4112:             * variable invocation (from $ to end character...)
                   4113:             */
                   4114:            if (val == var_Error || val == varNoError) {
                   4115:                /*
                   4116:                 * If performing old-time variable substitution, skip over
                   4117:                 * the variable and continue with the substitution. Otherwise,
                   4118:                 * store the dollar sign and advance str so we continue with
                   4119:                 * the string...
                   4120:                 */
                   4121:                if (oldVars) {
                   4122:                    str += length;
1.202     christos 4123:                } else if ((flags & VARF_UNDEFERR) || val == var_Error) {
1.1       cgd      4124:                    /*
                   4125:                     * If variable is undefined, complain and skip the
                   4126:                     * variable. The complaint will stop us from doing anything
                   4127:                     * when the file is parsed.
                   4128:                     */
                   4129:                    if (!errorReported) {
1.92      christos 4130:                        Parse_Error(PARSE_FATAL,
1.127     christos 4131:                                     "Undefined variable \"%.*s\"",length,str);
1.1       cgd      4132:                    }
                   4133:                    str += length;
                   4134:                    errorReported = TRUE;
                   4135:                } else {
1.146     dsl      4136:                    Buf_AddByte(&buf, *str);
1.1       cgd      4137:                    str += 1;
                   4138:                }
                   4139:            } else {
                   4140:                /*
                   4141:                 * We've now got a variable structure to store in. But first,
                   4142:                 * advance the string pointer.
                   4143:                 */
                   4144:                str += length;
1.15      christos 4145:
1.1       cgd      4146:                /*
                   4147:                 * Copy all the characters from the variable value straight
                   4148:                 * into the new string.
                   4149:                 */
1.96      christos 4150:                length = strlen(val);
1.146     dsl      4151:                Buf_AddBytes(&buf, length, val);
1.96      christos 4152:                trailingBslash = length > 0 && val[length - 1] == '\\';
1.104     christos 4153:            }
1.196     christos 4154:            free(freeIt);
                   4155:            freeIt = NULL;
1.1       cgd      4156:        }
                   4157:     }
1.15      christos 4158:
1.168     sjg      4159:     return Buf_DestroyCompact(&buf);
1.1       cgd      4160: }
                   4161:
                   4162: /*-
                   4163:  *-----------------------------------------------------------------------
                   4164:  * Var_GetTail --
                   4165:  *     Return the tail from each of a list of words. Used to set the
                   4166:  *     System V local variables.
                   4167:  *
1.70      wiz      4168:  * Input:
                   4169:  *     file            Filename to modify
                   4170:  *
1.1       cgd      4171:  * Results:
                   4172:  *     The resulting string.
                   4173:  *
                   4174:  * Side Effects:
                   4175:  *     None.
                   4176:  *
                   4177:  *-----------------------------------------------------------------------
                   4178:  */
1.61      explorer 4179: #if 0
1.1       cgd      4180: char *
1.70      wiz      4181: Var_GetTail(char *file)
1.1       cgd      4182: {
1.115     dsl      4183:     return(VarModify(file, VarTail, NULL));
1.1       cgd      4184: }
                   4185:
                   4186: /*-
                   4187:  *-----------------------------------------------------------------------
                   4188:  * Var_GetHead --
                   4189:  *     Find the leading components of a (list of) filename(s).
                   4190:  *     XXX: VarHead does not replace foo by ., as (sun) System V make
                   4191:  *     does.
                   4192:  *
1.70      wiz      4193:  * Input:
                   4194:  *     file            Filename to manipulate
                   4195:  *
1.1       cgd      4196:  * Results:
                   4197:  *     The leading components.
                   4198:  *
                   4199:  * Side Effects:
                   4200:  *     None.
                   4201:  *
                   4202:  *-----------------------------------------------------------------------
                   4203:  */
                   4204: char *
1.70      wiz      4205: Var_GetHead(char *file)
1.1       cgd      4206: {
1.115     dsl      4207:     return(VarModify(file, VarHead, NULL));
1.1       cgd      4208: }
1.61      explorer 4209: #endif
1.1       cgd      4210:
                   4211: /*-
                   4212:  *-----------------------------------------------------------------------
                   4213:  * Var_Init --
                   4214:  *     Initialize the module
                   4215:  *
                   4216:  * Results:
                   4217:  *     None
                   4218:  *
                   4219:  * Side Effects:
1.15      christos 4220:  *     The VAR_CMD and VAR_GLOBAL contexts are created
1.1       cgd      4221:  *-----------------------------------------------------------------------
                   4222:  */
                   4223: void
1.70      wiz      4224: Var_Init(void)
1.1       cgd      4225: {
1.184     sjg      4226:     VAR_INTERNAL = Targ_NewGN("Internal");
1.92      christos 4227:     VAR_GLOBAL = Targ_NewGN("Global");
                   4228:     VAR_CMD = Targ_NewGN("Command");
1.6       jtc      4229:
                   4230: }
                   4231:
1.1       cgd      4232:
1.6       jtc      4233: void
1.70      wiz      4234: Var_End(void)
1.6       jtc      4235: {
1.1       cgd      4236: }
1.15      christos 4237:
1.1       cgd      4238:
                   4239: /****************** PRINT DEBUGGING INFO *****************/
1.36      mycroft  4240: static void
1.148     dsl      4241: VarPrintVar(void *vp)
1.1       cgd      4242: {
1.99      christos 4243:     Var    *v = (Var *)vp;
1.146     dsl      4244:     fprintf(debug_file, "%-16s = %s\n", v->name, Buf_GetAll(&v->val, NULL));
1.1       cgd      4245: }
                   4246:
                   4247: /*-
                   4248:  *-----------------------------------------------------------------------
                   4249:  * Var_Dump --
                   4250:  *     print all variables in a context
                   4251:  *-----------------------------------------------------------------------
                   4252:  */
1.5       cgd      4253: void
1.70      wiz      4254: Var_Dump(GNode *ctxt)
1.1       cgd      4255: {
1.36      mycroft  4256:     Hash_Search search;
                   4257:     Hash_Entry *h;
                   4258:
                   4259:     for (h = Hash_EnumFirst(&ctxt->context, &search);
                   4260:         h != NULL;
                   4261:         h = Hash_EnumNext(&search)) {
                   4262:            VarPrintVar(Hash_GetValue(h));
                   4263:     }
1.1       cgd      4264: }

CVSweb <webmaster@jp.NetBSD.org>