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

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

1.17    ! espie       1: /*     $OpenBSD: var.c,v 1.16 1999/12/06 22:24:32 espie Exp $  */
1.6       millert     2: /*     $NetBSD: var.c,v 1.18 1997/03/18 19:24:46 christos Exp $        */
1.1       deraadt     3:
                      4: /*
1.17    ! espie       5:  * Copyright (c) 1999 Marc Espie.
        !             6:  *
        !             7:  * Extensive code modifications for the OpenBSD project.
        !             8:  *
        !             9:  * Redistribution and use in source and binary forms, with or without
        !            10:  * modification, are permitted provided that the following conditions
        !            11:  * are met:
        !            12:  * 1. Redistributions of source code must retain the above copyright
        !            13:  *    notice, this list of conditions and the following disclaimer.
        !            14:  * 2. Redistributions in binary form must reproduce the above copyright
        !            15:  *    notice, this list of conditions and the following disclaimer in the
        !            16:  *    documentation and/or other materials provided with the distribution.
        !            17:  *
        !            18:  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
        !            19:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
        !            20:  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
        !            21:  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
        !            22:  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
        !            23:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
        !            24:  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
        !            25:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
        !            26:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
        !            27:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
        !            28:  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        !            29:  */
        !            30:
        !            31: /*
1.5       millert    32:  * Copyright (c) 1988, 1989, 1990, 1993
                     33:  *     The Regents of the University of California.  All rights reserved.
1.1       deraadt    34:  * Copyright (c) 1989 by Berkeley Softworks
                     35:  * All rights reserved.
                     36:  *
                     37:  * This code is derived from software contributed to Berkeley by
                     38:  * Adam de Boor.
                     39:  *
                     40:  * Redistribution and use in source and binary forms, with or without
                     41:  * modification, are permitted provided that the following conditions
                     42:  * are met:
                     43:  * 1. Redistributions of source code must retain the above copyright
                     44:  *    notice, this list of conditions and the following disclaimer.
                     45:  * 2. Redistributions in binary form must reproduce the above copyright
                     46:  *    notice, this list of conditions and the following disclaimer in the
                     47:  *    documentation and/or other materials provided with the distribution.
                     48:  * 3. All advertising materials mentioning features or use of this software
                     49:  *    must display the following acknowledgement:
                     50:  *     This product includes software developed by the University of
                     51:  *     California, Berkeley and its contributors.
                     52:  * 4. Neither the name of the University nor the names of its contributors
                     53:  *    may be used to endorse or promote products derived from this software
                     54:  *    without specific prior written permission.
                     55:  *
                     56:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     57:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     58:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     59:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     60:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     61:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     62:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     63:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     64:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     65:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     66:  * SUCH DAMAGE.
                     67:  */
                     68:
                     69: #ifndef lint
                     70: #if 0
1.5       millert    71: static char sccsid[] = "@(#)var.c      8.3 (Berkeley) 3/19/94";
1.1       deraadt    72: #else
1.17    ! espie      73: static char rcsid[] = "$OpenBSD: var.c,v 1.16 1999/12/06 22:24:32 espie Exp $";
1.1       deraadt    74: #endif
                     75: #endif /* not lint */
                     76:
                     77: /*-
                     78:  * var.c --
                     79:  *     Variable-handling functions
                     80:  *
                     81:  * Interface:
                     82:  *     Var_Set             Set the value of a variable in the given
                     83:  *                         context. The variable is created if it doesn't
                     84:  *                         yet exist. The value and variable name need not
                     85:  *                         be preserved.
                     86:  *
                     87:  *     Var_Append          Append more characters to an existing variable
                     88:  *                         in the given context. The variable needn't
                     89:  *                         exist already -- it will be created if it doesn't.
                     90:  *                         A space is placed between the old value and the
                     91:  *                         new one.
                     92:  *
                     93:  *     Var_Exists          See if a variable exists.
                     94:  *
                     95:  *     Var_Value           Return the value of a variable in a context or
                     96:  *                         NULL if the variable is undefined.
                     97:  *
                     98:  *     Var_Subst           Substitute named variable, or all variables if
                     99:  *                         NULL in a string using
                    100:  *                         the given context as the top-most one. If the
                    101:  *                         third argument is non-zero, Parse_Error is
                    102:  *                         called if any variables are undefined.
                    103:  *
                    104:  *     Var_Parse           Parse a variable expansion from a string and
                    105:  *                         return the result and the number of characters
                    106:  *                         consumed.
                    107:  *
                    108:  *     Var_Delete          Delete a variable in a context.
                    109:  *
                    110:  *     Var_Init            Initialize this module.
                    111:  *
                    112:  * Debugging:
                    113:  *     Var_Dump            Print out all variables defined in the given
                    114:  *                         context.
                    115:  *
                    116:  * XXX: There's a lot of duplication in these functions.
                    117:  */
                    118:
                    119: #include    <ctype.h>
1.6       millert   120: #ifndef MAKE_BOOTSTRAP
1.9       deraadt   121: #include    <sys/types.h>
1.6       millert   122: #include    <regex.h>
                    123: #endif
                    124: #include    <stdlib.h>
1.1       deraadt   125: #include    "make.h"
                    126: #include    "buf.h"
                    127:
                    128: /*
                    129:  * This is a harmless return value for Var_Parse that can be used by Var_Subst
                    130:  * to determine if there was an error in parsing -- easier than returning
                    131:  * a flag, as things outside this module don't give a hoot.
                    132:  */
                    133: char   var_Error[] = "";
                    134:
                    135: /*
                    136:  * Similar to var_Error, but returned when the 'err' flag for Var_Parse is
                    137:  * set false. Why not just use a constant? Well, gcc likes to condense
                    138:  * identical string instances...
                    139:  */
                    140: static char    varNoError[] = "";
                    141:
                    142: /*
                    143:  * Internally, variables are contained in four different contexts.
                    144:  *     1) the environment. They may not be changed. If an environment
                    145:  *         variable is appended-to, the result is placed in the global
                    146:  *         context.
                    147:  *     2) the global context. Variables set in the Makefile are located in
                    148:  *         the global context. It is the penultimate context searched when
                    149:  *         substituting.
                    150:  *     3) the command-line context. All variables set on the command line
                    151:  *        are placed in this context. They are UNALTERABLE once placed here.
                    152:  *     4) the local context. Each target has associated with it a context
                    153:  *        list. On this list are located the structures describing such
                    154:  *        local variables as $(@) and $(*)
                    155:  * The four contexts are searched in the reverse order from which they are
                    156:  * listed.
                    157:  */
1.17    ! espie     158: GNode          *VAR_GLOBAL;    /* variables from the makefile */
        !           159: GNode          *VAR_CMD;       /* variables defined on the command-line */
        !           160: static GNode   *VAR_ENV;       /* variables read from env */
1.1       deraadt   161:
                    162: static Lst     allVars;      /* List of all variables */
                    163:
                    164: #define FIND_CMD       0x1   /* look in VAR_CMD when searching */
                    165: #define FIND_GLOBAL    0x2   /* look in VAR_GLOBAL as well */
                    166: #define FIND_ENV       0x4   /* look in the environment also */
                    167:
                    168: typedef struct Var {
                    169:     char          *name;       /* the variable's name */
                    170:     Buffer       val;          /* its value */
                    171:     int                  flags;        /* miscellaneous status flags */
                    172: #define VAR_IN_USE     1           /* Variable's value currently being used.
                    173:                                     * Used to avoid recursion */
                    174: #define VAR_JUNK       4           /* Variable is a junk variable that
                    175:                                     * should be destroyed when done with
                    176:                                     * it. Used by Var_Parse for undefined,
                    177:                                     * modified variables */
                    178: }  Var;
                    179:
1.6       millert   180: /* Var*Pattern flags */
                    181: #define VAR_SUB_GLOBAL 0x01    /* Apply substitution globally */
                    182: #define VAR_SUB_ONE    0x02    /* Apply substitution to one word */
                    183: #define VAR_SUB_MATCHED        0x04    /* There was a match */
                    184: #define VAR_MATCH_START        0x08    /* Match at start of word */
                    185: #define VAR_MATCH_END  0x10    /* Match at end of word */
                    186:
1.1       deraadt   187: typedef struct {
                    188:     char         *lhs;     /* String to match */
1.16      espie     189:     size_t       leftLen;  /* Length of string */
1.1       deraadt   190:     char         *rhs;     /* Replacement string (w/ &'s removed) */
1.16      espie     191:     size_t       rightLen; /* Length of replacement */
1.1       deraadt   192:     int                  flags;
                    193: } VarPattern;
                    194:
1.6       millert   195: #ifndef MAKE_BOOTSTRAP
                    196: typedef struct {
                    197:     regex_t      re;
                    198:     int                  nsub;
                    199:     regmatch_t  *matches;
                    200:     char        *replace;
                    201:     int                  flags;
                    202: } VarREPattern;
                    203: #endif
                    204:
1.1       deraadt   205: static int VarCmp __P((ClientData, ClientData));
                    206: static Var *VarFind __P((char *, GNode *, int));
1.17    ! espie     207: static Var *VarAdd __P((char *, char *, GNode *));
1.1       deraadt   208: static void VarDelete __P((ClientData));
                    209: static Boolean VarHead __P((char *, Boolean, Buffer, ClientData));
                    210: static Boolean VarTail __P((char *, Boolean, Buffer, ClientData));
                    211: static Boolean VarSuffix __P((char *, Boolean, Buffer, ClientData));
                    212: static Boolean VarRoot __P((char *, Boolean, Buffer, ClientData));
                    213: static Boolean VarMatch __P((char *, Boolean, Buffer, ClientData));
1.4       briggs    214: #ifdef SYSVVARSUB
1.1       deraadt   215: static Boolean VarSYSVMatch __P((char *, Boolean, Buffer, ClientData));
1.4       briggs    216: #endif
1.1       deraadt   217: static Boolean VarNoMatch __P((char *, Boolean, Buffer, ClientData));
1.6       millert   218: #ifndef MAKE_BOOTSTRAP
                    219: static void VarREError __P((int, regex_t *, const char *));
                    220: static Boolean VarRESubstitute __P((char *, Boolean, Buffer, ClientData));
                    221: #endif
1.1       deraadt   222: static Boolean VarSubstitute __P((char *, Boolean, Buffer, ClientData));
1.16      espie     223: static char *VarGetPattern __P((GNode *, int, char **, int, int *, size_t *,
1.6       millert   224:                                VarPattern *));
                    225: static char *VarQuote __P((char *));
1.1       deraadt   226: static char *VarModify __P((char *, Boolean (*)(char *, Boolean, Buffer,
                    227:                                                ClientData),
                    228:                            ClientData));
                    229: static int VarPrintVar __P((ClientData, ClientData));
1.14      espie     230: static Boolean VarUppercase __P((char *word, Boolean addSpace, Buffer buf, ClientData dummy));
                    231: static Boolean VarLowercase __P((char *word, Boolean addSpace, Buffer buf, ClientData dummy));
1.1       deraadt   232:
                    233: /*-
                    234:  *-----------------------------------------------------------------------
                    235:  * VarCmp  --
                    236:  *     See if the given variable matches the named one. Called from
                    237:  *     Lst_Find when searching for a variable of a given name.
                    238:  *
                    239:  * Results:
                    240:  *     0 if they match. non-zero otherwise.
                    241:  *
                    242:  * Side Effects:
                    243:  *     none
                    244:  *-----------------------------------------------------------------------
                    245:  */
                    246: static int
                    247: VarCmp (v, name)
                    248:     ClientData     v;          /* VAR structure to compare */
                    249:     ClientData     name;       /* name to look for */
                    250: {
                    251:     return (strcmp ((char *) name, ((Var *) v)->name));
                    252: }
                    253:
                    254: /*-
                    255:  *-----------------------------------------------------------------------
                    256:  * VarFind --
                    257:  *     Find the given variable in the given context and any other contexts
                    258:  *     indicated.
                    259:  *
                    260:  * Results:
                    261:  *     A pointer to the structure describing the desired variable or
                    262:  *     NIL if the variable does not exist.
                    263:  *
                    264:  * Side Effects:
1.17    ! espie     265:  *     Caches env variables in the VAR_ENV context.
1.1       deraadt   266:  *-----------------------------------------------------------------------
                    267:  */
                    268: static Var *
                    269: VarFind (name, ctxt, flags)
                    270:     char               *name;  /* name to find */
                    271:     GNode              *ctxt;  /* context in which to find it */
                    272:     int                flags;  /* FIND_GLOBAL set means to look in the
                    273:                                 * VAR_GLOBAL context as well.
                    274:                                 * FIND_CMD set means to look in the VAR_CMD
                    275:                                 * context also.
                    276:                                 * FIND_ENV set means to look in the
1.17    ! espie     277:                                 * environment/VAR_ENV context.  */
1.1       deraadt   278: {
                    279:     LstNode            var;
                    280:     Var                        *v;
                    281:
                    282:        /*
                    283:         * If the variable name begins with a '.', it could very well be one of
                    284:         * the local ones.  We check the name against all the local variables
                    285:         * and substitute the short version in for 'name' if it matches one of
                    286:         * them.
                    287:         */
                    288:        if (*name == '.' && isupper((unsigned char) name[1]))
                    289:                switch (name[1]) {
                    290:                case 'A':
                    291:                        if (!strcmp(name, ".ALLSRC"))
                    292:                                name = ALLSRC;
                    293:                        if (!strcmp(name, ".ARCHIVE"))
                    294:                                name = ARCHIVE;
                    295:                        break;
                    296:                case 'I':
                    297:                        if (!strcmp(name, ".IMPSRC"))
                    298:                                name = IMPSRC;
                    299:                        break;
                    300:                case 'M':
                    301:                        if (!strcmp(name, ".MEMBER"))
                    302:                                name = MEMBER;
                    303:                        break;
                    304:                case 'O':
                    305:                        if (!strcmp(name, ".OODATE"))
                    306:                                name = OODATE;
                    307:                        break;
                    308:                case 'P':
                    309:                        if (!strcmp(name, ".PREFIX"))
                    310:                                name = PREFIX;
                    311:                        break;
                    312:                case 'T':
                    313:                        if (!strcmp(name, ".TARGET"))
                    314:                                name = TARGET;
                    315:                        break;
                    316:                }
                    317:     /*
                    318:      * First look for the variable in the given context. If it's not there,
                    319:      * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order,
                    320:      * depending on the FIND_* flags in 'flags'
                    321:      */
1.17    ! espie     322:     var = Lst_Find(ctxt->context, (ClientData)name, VarCmp);
1.1       deraadt   323:
1.17    ! espie     324:     if ((var == NILLNODE) && (flags & FIND_CMD) && (ctxt != VAR_CMD))
1.1       deraadt   325:        var = Lst_Find (VAR_CMD->context, (ClientData)name, VarCmp);
                    326:     if (!checkEnvFirst && (var == NILLNODE) && (flags & FIND_GLOBAL) &&
1.17    ! espie     327:        (ctxt != VAR_GLOBAL)) {
1.1       deraadt   328:        var = Lst_Find (VAR_GLOBAL->context, (ClientData)name, VarCmp);
                    329:     }
                    330:     if ((var == NILLNODE) && (flags & FIND_ENV)) {
1.17    ! espie     331:        var = Lst_Find(VAR_ENV->context, (ClientData)name, VarCmp);
        !           332:        if (var == NILLNODE) {
        !           333:            char *env;
        !           334:
        !           335:            if ((env = getenv(name)) != NULL)
        !           336:                return VarAdd(name, env, VAR_ENV);
1.1       deraadt   337:        }
1.17    ! espie     338:     }
        !           339:     if (var == NILLNODE && checkEnvFirst && (flags & FIND_GLOBAL) &&
        !           340:                   (ctxt != VAR_GLOBAL))
        !           341:            var = Lst_Find(VAR_GLOBAL->context, (ClientData)name, VarCmp);
        !           342:     if (var == NILLNODE)
1.1       deraadt   343:        return ((Var *) NIL);
1.17    ! espie     344:     else
1.1       deraadt   345:        return ((Var *) Lst_Datum (var));
                    346: }
                    347:
                    348: /*-
                    349:  *-----------------------------------------------------------------------
                    350:  * VarAdd  --
                    351:  *     Add a new variable of name name and value val to the given context
                    352:  *
                    353:  * Results:
1.17    ! espie     354:  *     The added variable
1.1       deraadt   355:  *
                    356:  * Side Effects:
                    357:  *     The new variable is placed at the front of the given context
                    358:  *     The name and val arguments are duplicated so they may
                    359:  *     safely be freed.
                    360:  *-----------------------------------------------------------------------
                    361:  */
1.17    ! espie     362: static Var *
        !           363: VarAdd(name, val, ctxt)
1.1       deraadt   364:     char           *name;      /* name of variable to add */
                    365:     char           *val;       /* value to set it to */
                    366:     GNode          *ctxt;      /* context in which to set it */
                    367: {
                    368:     register Var   *v;
                    369:     int                  len;
                    370:
                    371:     v = (Var *) emalloc (sizeof (Var));
                    372:
1.4       briggs    373:     v->name = estrdup (name);
1.1       deraadt   374:
                    375:     len = val ? strlen(val) : 0;
                    376:     v->val = Buf_Init(len+1);
1.16      espie     377:     Buf_AddChars(v->val, len, val);
1.1       deraadt   378:
                    379:     v->flags = 0;
                    380:
                    381:     (void) Lst_AtFront (ctxt->context, (ClientData)v);
                    382:     (void) Lst_AtEnd (allVars, (ClientData) v);
                    383:     if (DEBUG(VAR)) {
                    384:        printf("%s:%s = %s\n", ctxt->name, name, val);
                    385:     }
1.17    ! espie     386:     return v;
1.1       deraadt   387: }
                    388:
                    389:
                    390: /*-
                    391:  *-----------------------------------------------------------------------
                    392:  * VarDelete  --
                    393:  *     Delete a variable and all the space associated with it.
                    394:  *
                    395:  * Results:
                    396:  *     None
                    397:  *
                    398:  * Side Effects:
                    399:  *     None
                    400:  *-----------------------------------------------------------------------
                    401:  */
                    402: static void
                    403: VarDelete(vp)
                    404:     ClientData vp;
                    405: {
                    406:     Var *v = (Var *) vp;
                    407:     free(v->name);
                    408:     Buf_Destroy(v->val, TRUE);
                    409:     free((Address) v);
                    410: }
                    411:
                    412:
                    413:
                    414: /*-
                    415:  *-----------------------------------------------------------------------
                    416:  * Var_Delete --
                    417:  *     Remove a variable from a context.
                    418:  *
                    419:  * Results:
                    420:  *     None.
                    421:  *
                    422:  * Side Effects:
                    423:  *     The Var structure is removed and freed.
                    424:  *
                    425:  *-----------------------------------------------------------------------
                    426:  */
                    427: void
                    428: Var_Delete(name, ctxt)
                    429:     char         *name;
                    430:     GNode        *ctxt;
                    431: {
                    432:     LstNode      ln;
                    433:
                    434:     if (DEBUG(VAR)) {
                    435:        printf("%s:delete %s\n", ctxt->name, name);
                    436:     }
                    437:     ln = Lst_Find(ctxt->context, (ClientData)name, VarCmp);
                    438:     if (ln != NILLNODE) {
                    439:        register Var      *v;
                    440:
                    441:        v = (Var *)Lst_Datum(ln);
                    442:        Lst_Remove(ctxt->context, ln);
                    443:        ln = Lst_Member(allVars, v);
                    444:        Lst_Remove(allVars, ln);
                    445:        VarDelete((ClientData) v);
                    446:     }
                    447: }
                    448:
                    449: /*-
                    450:  *-----------------------------------------------------------------------
                    451:  * Var_Set --
                    452:  *     Set the variable name to the value val in the given context.
                    453:  *
                    454:  * Results:
                    455:  *     None.
                    456:  *
                    457:  * Side Effects:
                    458:  *     If the variable doesn't yet exist, a new record is created for it.
                    459:  *     Else the old value is freed and the new one stuck in its place
                    460:  *
                    461:  * Notes:
                    462:  *     The variable is searched for only in its context before being
                    463:  *     created in that context. I.e. if the context is VAR_GLOBAL,
                    464:  *     only VAR_GLOBAL->context is searched. Likewise if it is VAR_CMD, only
                    465:  *     VAR_CMD->context is searched. This is done to avoid the literally
                    466:  *     thousands of unnecessary strcmp's that used to be done to
                    467:  *     set, say, $(@) or $(<).
                    468:  *-----------------------------------------------------------------------
                    469:  */
                    470: void
                    471: Var_Set (name, val, ctxt)
                    472:     char           *name;      /* name of variable to set */
                    473:     char           *val;       /* value to give to the variable */
                    474:     GNode          *ctxt;      /* context in which to set it */
                    475: {
                    476:     register Var   *v;
                    477:
                    478:     /*
                    479:      * We only look for a variable in the given context since anything set
                    480:      * here will override anything in a lower context, so there's not much
                    481:      * point in searching them all just to save a bit of memory...
                    482:      */
                    483:     v = VarFind (name, ctxt, 0);
                    484:     if (v == (Var *) NIL) {
1.17    ! espie     485:        (void)VarAdd(name, val, ctxt);
1.1       deraadt   486:     } else {
                    487:        Buf_Discard(v->val, Buf_Size(v->val));
1.16      espie     488:        Buf_AddChars(v->val, strlen(val), val);
1.1       deraadt   489:
                    490:        if (DEBUG(VAR)) {
                    491:            printf("%s:%s = %s\n", ctxt->name, name, val);
                    492:        }
                    493:     }
                    494:     /*
                    495:      * Any variables given on the command line are automatically exported
1.17    ! espie     496:      * to the environment (as per POSIX standard).
        !           497:      * We put them into the env cache directly.
        !           498:      * (Note that additions to VAR_CMD occur very early, so VAR_ENV is
        !           499:      * actually empty at this point).
1.1       deraadt   500:      */
                    501:     if (ctxt == VAR_CMD) {
                    502:        setenv(name, val, 1);
1.17    ! espie     503:        (void)VarAdd(name, val, VAR_ENV);
1.1       deraadt   504:     }
                    505: }
                    506:
                    507: /*-
                    508:  *-----------------------------------------------------------------------
                    509:  * Var_Append --
                    510:  *     The variable of the given name has the given value appended to it in
                    511:  *     the given context.
                    512:  *
                    513:  * Results:
                    514:  *     None
                    515:  *
                    516:  * Side Effects:
                    517:  *     If the variable doesn't exist, it is created. Else the strings
                    518:  *     are concatenated (with a space in between).
                    519:  *
                    520:  * Notes:
                    521:  *     Only if the variable is being sought in the global context is the
                    522:  *     environment searched.
                    523:  *     XXX: Knows its calling circumstances in that if called with ctxt
                    524:  *     an actual target, it will only search that context since only
                    525:  *     a local variable could be being appended to. This is actually
                    526:  *     a big win and must be tolerated.
                    527:  *-----------------------------------------------------------------------
                    528:  */
                    529: void
                    530: Var_Append (name, val, ctxt)
                    531:     char           *name;      /* Name of variable to modify */
                    532:     char           *val;       /* String to append to it */
                    533:     GNode          *ctxt;      /* Context in which this should occur */
                    534: {
                    535:     register Var   *v;
                    536:
                    537:     v = VarFind (name, ctxt, (ctxt == VAR_GLOBAL) ? FIND_ENV : 0);
                    538:
                    539:     if (v == (Var *) NIL) {
1.17    ! espie     540:        (void)VarAdd(name, val, ctxt);
1.1       deraadt   541:     } else {
1.16      espie     542:        Buf_AddChar(v->val, ' ');
                    543:        Buf_AddChars(v->val, strlen(val), val);
1.1       deraadt   544:
                    545:        if (DEBUG(VAR)) {
                    546:            printf("%s:%s = %s\n", ctxt->name, name,
1.16      espie     547:                   Buf_GetAll(v->val, NULL));
1.1       deraadt   548:        }
                    549:
                    550:     }
                    551: }
                    552:
                    553: /*-
                    554:  *-----------------------------------------------------------------------
                    555:  * Var_Exists --
                    556:  *     See if the given variable exists.
                    557:  *
                    558:  * Results:
                    559:  *     TRUE if it does, FALSE if it doesn't
                    560:  *
                    561:  * Side Effects:
                    562:  *     None.
                    563:  *
                    564:  *-----------------------------------------------------------------------
                    565:  */
                    566: Boolean
                    567: Var_Exists(name, ctxt)
                    568:     char         *name;        /* Variable to find */
                    569:     GNode        *ctxt;        /* Context in which to start search */
                    570: {
                    571:     Var                  *v;
                    572:
                    573:     v = VarFind(name, ctxt, FIND_CMD|FIND_GLOBAL|FIND_ENV);
                    574:
1.17    ! espie     575:     if (v == (Var *)NIL)
        !           576:        return FALSE;
        !           577:     else
        !           578:        return TRUE;
1.1       deraadt   579: }
                    580:
                    581: /*-
                    582:  *-----------------------------------------------------------------------
                    583:  * Var_Value --
                    584:  *     Return the value of the named variable in the given context
                    585:  *
                    586:  * Results:
                    587:  *     The value if the variable exists, NULL if it doesn't
                    588:  *
                    589:  * Side Effects:
                    590:  *     None
                    591:  *-----------------------------------------------------------------------
                    592:  */
                    593: char *
                    594: Var_Value (name, ctxt, frp)
                    595:     char           *name;      /* name to find */
                    596:     GNode          *ctxt;      /* context in which to search for it */
                    597:     char          **frp;
                    598: {
                    599:     Var            *v;
                    600:
                    601:     v = VarFind (name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
                    602:     *frp = NULL;
                    603:     if (v != (Var *) NIL) {
1.16      espie     604:        char *p = Buf_GetAll(v->val, NULL);
1.1       deraadt   605:        return p;
1.17    ! espie     606:     } else
        !           607:        return NULL;
1.1       deraadt   608: }
                    609:
                    610: /*-
                    611:  *-----------------------------------------------------------------------
1.11      espie     612:  * VarUppercase --
                    613:  *     Place the Upper cased word in the given buffer.
                    614:  *
                    615:  * Results:
                    616:  *     TRUE if characters were added to the buffer (a space needs to be
                    617:  *     added to the buffer before the next word).
                    618:  *
                    619:  * Side Effects:
                    620:  *     The word is added to the buffer.
                    621:  *
                    622:  *-----------------------------------------------------------------------
                    623:  */
                    624: static Boolean
                    625: VarUppercase (word, addSpace, buf, dummy)
                    626:     char         *word;        /* Word to Upper Case */
                    627:     Boolean      addSpace;     /* True if need to add a space to the buffer
                    628:                                 * before sticking in the head */
                    629:     Buffer       buf;          /* Buffer in which to store it */
                    630:     ClientData   dummy;
                    631: {
                    632:     size_t len = strlen(word);
                    633:
                    634:     if (addSpace) {
1.16      espie     635:        Buf_AddChar(buf, ' ');
1.11      espie     636:     }
                    637:
                    638:     while (len--) {
1.16      espie     639:        Buf_AddChar(buf, toupper(*word++));
1.11      espie     640:     }
                    641:     return (TRUE);
                    642: }
                    643:
                    644: /*-
                    645:  *-----------------------------------------------------------------------
                    646:  * VarLowercase --
                    647:  *     Place the Lower cased word in the given buffer.
                    648:  *
                    649:  * Results:
                    650:  *     TRUE if characters were added to the buffer (a space needs to be
                    651:  *     added to the buffer before the next word).
                    652:  *
                    653:  * Side Effects:
                    654:  *     The word is added to the buffer.
                    655:  *
                    656:  *-----------------------------------------------------------------------
                    657:  */
                    658: static Boolean
                    659: VarLowercase (word, addSpace, buf, dummy)
                    660:     char         *word;        /* Word to Lower Case */
                    661:     Boolean      addSpace;     /* True if need to add a space to the buffer
                    662:                                 * before sticking in the head */
                    663:     Buffer       buf;          /* Buffer in which to store it */
                    664:     ClientData   dummy;
                    665: {
                    666:     size_t len = strlen(word);
                    667:
                    668:     if (addSpace) {
1.16      espie     669:        Buf_AddChar(buf, ' ');
1.11      espie     670:     }
                    671:
                    672:     while (len--) {
1.16      espie     673:        Buf_AddChar(buf, tolower(*word++));
1.11      espie     674:     }
                    675:     return (TRUE);
                    676: }
                    677:
                    678: /*-
                    679:  *-----------------------------------------------------------------------
1.1       deraadt   680:  * VarHead --
                    681:  *     Remove the tail of the given word and place the result in the given
                    682:  *     buffer.
                    683:  *
                    684:  * Results:
                    685:  *     TRUE if characters were added to the buffer (a space needs to be
                    686:  *     added to the buffer before the next word).
                    687:  *
                    688:  * Side Effects:
                    689:  *     The trimmed word is added to the buffer.
                    690:  *
                    691:  *-----------------------------------------------------------------------
                    692:  */
                    693: static Boolean
                    694: VarHead (word, addSpace, buf, dummy)
                    695:     char         *word;        /* Word to trim */
                    696:     Boolean      addSpace;     /* True if need to add a space to the buffer
                    697:                                 * before sticking in the head */
                    698:     Buffer       buf;          /* Buffer in which to store it */
                    699:     ClientData   dummy;
                    700: {
                    701:     register char *slash;
                    702:
                    703:     slash = strrchr (word, '/');
                    704:     if (slash != (char *)NULL) {
                    705:        if (addSpace) {
1.16      espie     706:            Buf_AddChar(buf, ' ');
1.1       deraadt   707:        }
                    708:        *slash = '\0';
1.16      espie     709:        Buf_AddChars(buf, strlen(word), word);
1.1       deraadt   710:        *slash = '/';
                    711:        return (TRUE);
                    712:     } else {
                    713:        /*
                    714:         * If no directory part, give . (q.v. the POSIX standard)
                    715:         */
                    716:        if (addSpace) {
1.16      espie     717:            Buf_AddChars(buf, 2, " .");
1.1       deraadt   718:        } else {
1.16      espie     719:            Buf_AddChar(buf, '.');
1.1       deraadt   720:        }
                    721:     }
                    722:     return(dummy ? TRUE : TRUE);
                    723: }
                    724:
                    725: /*-
                    726:  *-----------------------------------------------------------------------
                    727:  * VarTail --
                    728:  *     Remove the head of the given word and place the result in the given
                    729:  *     buffer.
                    730:  *
                    731:  * Results:
                    732:  *     TRUE if characters were added to the buffer (a space needs to be
                    733:  *     added to the buffer before the next word).
                    734:  *
                    735:  * Side Effects:
                    736:  *     The trimmed word is added to the buffer.
                    737:  *
                    738:  *-----------------------------------------------------------------------
                    739:  */
                    740: static Boolean
                    741: VarTail (word, addSpace, buf, dummy)
                    742:     char         *word;        /* Word to trim */
                    743:     Boolean      addSpace;     /* TRUE if need to stick a space in the
                    744:                                 * buffer before adding the tail */
                    745:     Buffer       buf;          /* Buffer in which to store it */
                    746:     ClientData   dummy;
                    747: {
                    748:     register char *slash;
                    749:
                    750:     if (addSpace) {
1.16      espie     751:        Buf_AddChar(buf, ' ');
1.1       deraadt   752:     }
                    753:
                    754:     slash = strrchr (word, '/');
                    755:     if (slash != (char *)NULL) {
                    756:        *slash++ = '\0';
1.16      espie     757:        Buf_AddChars(buf, strlen(slash), slash);
1.1       deraadt   758:        slash[-1] = '/';
                    759:     } else {
1.16      espie     760:        Buf_AddChars(buf, strlen(word), word);
1.1       deraadt   761:     }
                    762:     return (dummy ? TRUE : TRUE);
                    763: }
                    764:
                    765: /*-
                    766:  *-----------------------------------------------------------------------
                    767:  * VarSuffix --
                    768:  *     Place the suffix of the given word in the given buffer.
                    769:  *
                    770:  * Results:
                    771:  *     TRUE if characters were added to the buffer (a space needs to be
                    772:  *     added to the buffer before the next word).
                    773:  *
                    774:  * Side Effects:
                    775:  *     The suffix from the word is placed in the buffer.
                    776:  *
                    777:  *-----------------------------------------------------------------------
                    778:  */
                    779: static Boolean
                    780: VarSuffix (word, addSpace, buf, dummy)
                    781:     char         *word;        /* Word to trim */
                    782:     Boolean      addSpace;     /* TRUE if need to add a space before placing
                    783:                                 * the suffix in the buffer */
                    784:     Buffer       buf;          /* Buffer in which to store it */
                    785:     ClientData   dummy;
                    786: {
                    787:     register char *dot;
                    788:
                    789:     dot = strrchr (word, '.');
                    790:     if (dot != (char *)NULL) {
                    791:        if (addSpace) {
1.16      espie     792:            Buf_AddChar(buf, ' ');
1.1       deraadt   793:        }
                    794:        *dot++ = '\0';
1.16      espie     795:        Buf_AddChars(buf, strlen(dot), dot);
1.1       deraadt   796:        dot[-1] = '.';
                    797:        addSpace = TRUE;
                    798:     }
                    799:     return (dummy ? addSpace : addSpace);
                    800: }
                    801:
                    802: /*-
                    803:  *-----------------------------------------------------------------------
                    804:  * VarRoot --
                    805:  *     Remove the suffix of the given word and place the result in the
                    806:  *     buffer.
                    807:  *
                    808:  * Results:
                    809:  *     TRUE if characters were added to the buffer (a space needs to be
                    810:  *     added to the buffer before the next word).
                    811:  *
                    812:  * Side Effects:
                    813:  *     The trimmed word is added to the buffer.
                    814:  *
                    815:  *-----------------------------------------------------------------------
                    816:  */
                    817: static Boolean
                    818: VarRoot (word, addSpace, buf, dummy)
                    819:     char         *word;        /* Word to trim */
                    820:     Boolean      addSpace;     /* TRUE if need to add a space to the buffer
                    821:                                 * before placing the root in it */
                    822:     Buffer       buf;          /* Buffer in which to store it */
                    823:     ClientData   dummy;
                    824: {
                    825:     register char *dot;
                    826:
                    827:     if (addSpace) {
1.16      espie     828:        Buf_AddChar(buf, ' ');
1.1       deraadt   829:     }
                    830:
                    831:     dot = strrchr (word, '.');
                    832:     if (dot != (char *)NULL) {
                    833:        *dot = '\0';
1.16      espie     834:        Buf_AddChars(buf, strlen(word), word);
1.1       deraadt   835:        *dot = '.';
                    836:     } else {
1.16      espie     837:        Buf_AddChars(buf, strlen(word), word);
1.1       deraadt   838:     }
                    839:     return (dummy ? TRUE : TRUE);
                    840: }
                    841:
                    842: /*-
                    843:  *-----------------------------------------------------------------------
                    844:  * VarMatch --
                    845:  *     Place the word in the buffer if it matches the given pattern.
                    846:  *     Callback function for VarModify to implement the :M modifier.
1.5       millert   847:  *
1.1       deraadt   848:  * Results:
                    849:  *     TRUE if a space should be placed in the buffer before the next
                    850:  *     word.
                    851:  *
                    852:  * Side Effects:
                    853:  *     The word may be copied to the buffer.
                    854:  *
                    855:  *-----------------------------------------------------------------------
                    856:  */
                    857: static Boolean
                    858: VarMatch (word, addSpace, buf, pattern)
                    859:     char         *word;        /* Word to examine */
                    860:     Boolean      addSpace;     /* TRUE if need to add a space to the
                    861:                                 * buffer before adding the word, if it
                    862:                                 * matches */
                    863:     Buffer       buf;          /* Buffer in which to store it */
                    864:     ClientData    pattern;     /* Pattern the word must match */
                    865: {
                    866:     if (Str_Match(word, (char *) pattern)) {
                    867:        if (addSpace) {
1.16      espie     868:            Buf_AddChar(buf, ' ');
1.1       deraadt   869:        }
                    870:        addSpace = TRUE;
1.16      espie     871:        Buf_AddChars(buf, strlen(word), word);
1.1       deraadt   872:     }
                    873:     return(addSpace);
                    874: }
                    875:
1.4       briggs    876: #ifdef SYSVVARSUB
1.1       deraadt   877: /*-
                    878:  *-----------------------------------------------------------------------
                    879:  * VarSYSVMatch --
                    880:  *     Place the word in the buffer if it matches the given pattern.
                    881:  *     Callback function for VarModify to implement the System V %
                    882:  *     modifiers.
1.5       millert   883:  *
1.1       deraadt   884:  * Results:
                    885:  *     TRUE if a space should be placed in the buffer before the next
                    886:  *     word.
                    887:  *
                    888:  * Side Effects:
                    889:  *     The word may be copied to the buffer.
                    890:  *
                    891:  *-----------------------------------------------------------------------
                    892:  */
                    893: static Boolean
                    894: VarSYSVMatch (word, addSpace, buf, patp)
                    895:     char         *word;        /* Word to examine */
                    896:     Boolean      addSpace;     /* TRUE if need to add a space to the
                    897:                                 * buffer before adding the word, if it
                    898:                                 * matches */
                    899:     Buffer       buf;          /* Buffer in which to store it */
                    900:     ClientData           patp;         /* Pattern the word must match */
                    901: {
                    902:     int len;
                    903:     char *ptr;
                    904:     VarPattern           *pat = (VarPattern *) patp;
                    905:
1.7       deraadt   906:     if (*word) {
                    907:            if (addSpace)
1.16      espie     908:                Buf_AddChar(buf, ' ');
1.7       deraadt   909:
                    910:            addSpace = TRUE;
                    911:
                    912:            if ((ptr = Str_SYSVMatch(word, pat->lhs, &len)) != NULL)
                    913:                Str_SYSVSubst(buf, pat->rhs, ptr, len);
                    914:            else
1.16      espie     915:                Buf_AddChars(buf, strlen(word), word);
1.7       deraadt   916:     }
1.1       deraadt   917:     return(addSpace);
                    918: }
1.4       briggs    919: #endif
1.1       deraadt   920:
                    921:
                    922: /*-
                    923:  *-----------------------------------------------------------------------
                    924:  * VarNoMatch --
                    925:  *     Place the word in the buffer if it doesn't match the given pattern.
                    926:  *     Callback function for VarModify to implement the :N modifier.
1.5       millert   927:  *
1.1       deraadt   928:  * Results:
                    929:  *     TRUE if a space should be placed in the buffer before the next
                    930:  *     word.
                    931:  *
                    932:  * Side Effects:
                    933:  *     The word may be copied to the buffer.
                    934:  *
                    935:  *-----------------------------------------------------------------------
                    936:  */
                    937: static Boolean
                    938: VarNoMatch (word, addSpace, buf, pattern)
                    939:     char         *word;        /* Word to examine */
                    940:     Boolean      addSpace;     /* TRUE if need to add a space to the
                    941:                                 * buffer before adding the word, if it
                    942:                                 * matches */
                    943:     Buffer       buf;          /* Buffer in which to store it */
                    944:     ClientData    pattern;     /* Pattern the word must match */
                    945: {
                    946:     if (!Str_Match(word, (char *) pattern)) {
                    947:        if (addSpace) {
1.16      espie     948:            Buf_AddChar(buf, ' ');
1.1       deraadt   949:        }
                    950:        addSpace = TRUE;
1.16      espie     951:        Buf_AddChars(buf, strlen(word), word);
1.1       deraadt   952:     }
                    953:     return(addSpace);
                    954: }
                    955:
                    956:
                    957: /*-
                    958:  *-----------------------------------------------------------------------
                    959:  * VarSubstitute --
                    960:  *     Perform a string-substitution on the given word, placing the
                    961:  *     result in the passed buffer.
                    962:  *
                    963:  * Results:
                    964:  *     TRUE if a space is needed before more characters are added.
                    965:  *
                    966:  * Side Effects:
                    967:  *     None.
                    968:  *
                    969:  *-----------------------------------------------------------------------
                    970:  */
                    971: static Boolean
                    972: VarSubstitute (word, addSpace, buf, patternp)
                    973:     char               *word;      /* Word to modify */
                    974:     Boolean            addSpace;   /* True if space should be added before
                    975:                                     * other characters */
                    976:     Buffer             buf;        /* Buffer for result */
                    977:     ClientData         patternp;   /* Pattern for substitution */
                    978: {
1.16      espie     979:     register size_t    wordLen;    /* Length of word */
1.1       deraadt   980:     register char      *cp;        /* General pointer */
                    981:     VarPattern *pattern = (VarPattern *) patternp;
                    982:
                    983:     wordLen = strlen(word);
1.6       millert   984:     if ((pattern->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) !=
                    985:        (VAR_SUB_ONE|VAR_SUB_MATCHED)) {
1.1       deraadt   986:        /*
1.6       millert   987:         * Still substituting -- break it down into simple anchored cases
1.1       deraadt   988:         * and if none of them fits, perform the general substitution case.
                    989:         */
                    990:        if ((pattern->flags & VAR_MATCH_START) &&
                    991:            (strncmp(word, pattern->lhs, pattern->leftLen) == 0)) {
                    992:                /*
                    993:                 * Anchored at start and beginning of word matches pattern
                    994:                 */
                    995:                if ((pattern->flags & VAR_MATCH_END) &&
                    996:                    (wordLen == pattern->leftLen)) {
                    997:                        /*
                    998:                         * Also anchored at end and matches to the end (word
                    999:                         * is same length as pattern) add space and rhs only
                   1000:                         * if rhs is non-null.
                   1001:                         */
                   1002:                        if (pattern->rightLen != 0) {
                   1003:                            if (addSpace) {
1.16      espie    1004:                                Buf_AddChar(buf, ' ');
1.1       deraadt  1005:                            }
                   1006:                            addSpace = TRUE;
1.16      espie    1007:                            Buf_AddChars(buf, pattern->rightLen, pattern->rhs);
1.1       deraadt  1008:                        }
1.6       millert  1009:                        pattern->flags |= VAR_SUB_MATCHED;
1.1       deraadt  1010:                } else if (pattern->flags & VAR_MATCH_END) {
                   1011:                    /*
                   1012:                     * Doesn't match to end -- copy word wholesale
                   1013:                     */
                   1014:                    goto nosub;
                   1015:                } else {
                   1016:                    /*
                   1017:                     * Matches at start but need to copy in trailing characters
                   1018:                     */
                   1019:                    if ((pattern->rightLen + wordLen - pattern->leftLen) != 0){
                   1020:                        if (addSpace) {
1.16      espie    1021:                            Buf_AddChar(buf, ' ');
1.1       deraadt  1022:                        }
                   1023:                        addSpace = TRUE;
                   1024:                    }
1.16      espie    1025:                    Buf_AddChars(buf, pattern->rightLen, pattern->rhs);
                   1026:                    Buf_AddChars(buf, wordLen - pattern->leftLen,
                   1027:                                 word + pattern->leftLen);
1.6       millert  1028:                    pattern->flags |= VAR_SUB_MATCHED;
1.1       deraadt  1029:                }
                   1030:        } else if (pattern->flags & VAR_MATCH_START) {
                   1031:            /*
                   1032:             * Had to match at start of word and didn't -- copy whole word.
                   1033:             */
                   1034:            goto nosub;
                   1035:        } else if (pattern->flags & VAR_MATCH_END) {
                   1036:            /*
                   1037:             * Anchored at end, Find only place match could occur (leftLen
                   1038:             * characters from the end of the word) and see if it does. Note
                   1039:             * that because the $ will be left at the end of the lhs, we have
                   1040:             * to use strncmp.
                   1041:             */
                   1042:            cp = word + (wordLen - pattern->leftLen);
                   1043:            if ((cp >= word) &&
                   1044:                (strncmp(cp, pattern->lhs, pattern->leftLen) == 0)) {
                   1045:                /*
                   1046:                 * Match found. If we will place characters in the buffer,
                   1047:                 * add a space before hand as indicated by addSpace, then
                   1048:                 * stuff in the initial, unmatched part of the word followed
                   1049:                 * by the right-hand-side.
                   1050:                 */
                   1051:                if (((cp - word) + pattern->rightLen) != 0) {
                   1052:                    if (addSpace) {
1.16      espie    1053:                        Buf_AddChar(buf, ' ');
1.1       deraadt  1054:                    }
                   1055:                    addSpace = TRUE;
                   1056:                }
1.16      espie    1057:                Buf_AddChars(buf, cp - word, word);
                   1058:                Buf_AddChars(buf, pattern->rightLen, pattern->rhs);
1.6       millert  1059:                pattern->flags |= VAR_SUB_MATCHED;
1.1       deraadt  1060:            } else {
                   1061:                /*
                   1062:                 * Had to match at end and didn't. Copy entire word.
                   1063:                 */
                   1064:                goto nosub;
                   1065:            }
                   1066:        } else {
                   1067:            /*
                   1068:             * Pattern is unanchored: search for the pattern in the word using
                   1069:             * String_FindSubstring, copying unmatched portions and the
                   1070:             * right-hand-side for each match found, handling non-global
1.5       millert  1071:             * substitutions correctly, etc. When the loop is done, any
1.1       deraadt  1072:             * remaining part of the word (word and wordLen are adjusted
                   1073:             * accordingly through the loop) is copied straight into the
                   1074:             * buffer.
                   1075:             * addSpace is set FALSE as soon as a space is added to the
                   1076:             * buffer.
                   1077:             */
                   1078:            register Boolean done;
1.16      espie    1079:            size_t origSize;
1.1       deraadt  1080:
                   1081:            done = FALSE;
                   1082:            origSize = Buf_Size(buf);
                   1083:            while (!done) {
1.15      espie    1084:                cp = strstr(word, pattern->lhs);
1.1       deraadt  1085:                if (cp != (char *)NULL) {
                   1086:                    if (addSpace && (((cp - word) + pattern->rightLen) != 0)){
1.16      espie    1087:                        Buf_AddChar(buf, ' ');
1.1       deraadt  1088:                        addSpace = FALSE;
                   1089:                    }
1.16      espie    1090:                    Buf_AddChars(buf, cp-word, word);
                   1091:                    Buf_AddChars(buf, pattern->rightLen, pattern->rhs);
1.1       deraadt  1092:                    wordLen -= (cp - word) + pattern->leftLen;
                   1093:                    word = cp + pattern->leftLen;
1.5       millert  1094:                    if (wordLen == 0 || (pattern->flags & VAR_SUB_GLOBAL) == 0){
1.1       deraadt  1095:                        done = TRUE;
                   1096:                    }
1.6       millert  1097:                    pattern->flags |= VAR_SUB_MATCHED;
1.1       deraadt  1098:                } else {
                   1099:                    done = TRUE;
                   1100:                }
                   1101:            }
                   1102:            if (wordLen != 0) {
                   1103:                if (addSpace) {
1.16      espie    1104:                    Buf_AddChar(buf, ' ');
1.1       deraadt  1105:                }
1.16      espie    1106:                Buf_AddChars(buf, wordLen, word);
1.1       deraadt  1107:            }
                   1108:            /*
                   1109:             * If added characters to the buffer, need to add a space
                   1110:             * before we add any more. If we didn't add any, just return
                   1111:             * the previous value of addSpace.
                   1112:             */
1.16      espie    1113:            return (Buf_Size(buf) != origSize || addSpace);
1.1       deraadt  1114:        }
                   1115:        return (addSpace);
                   1116:     }
                   1117:  nosub:
                   1118:     if (addSpace) {
1.16      espie    1119:        Buf_AddChar(buf, ' ');
1.1       deraadt  1120:     }
1.16      espie    1121:     Buf_AddChars(buf, wordLen, word);
1.1       deraadt  1122:     return(TRUE);
                   1123: }
                   1124:
1.6       millert  1125: #ifndef MAKE_BOOTSTRAP
                   1126: /*-
                   1127:  *-----------------------------------------------------------------------
                   1128:  * VarREError --
                   1129:  *     Print the error caused by a regcomp or regexec call.
                   1130:  *
                   1131:  * Results:
                   1132:  *     None.
                   1133:  *
                   1134:  * Side Effects:
                   1135:  *     An error gets printed.
                   1136:  *
                   1137:  *-----------------------------------------------------------------------
                   1138:  */
                   1139: static void
                   1140: VarREError(err, pat, str)
                   1141:     int err;
                   1142:     regex_t *pat;
                   1143:     const char *str;
                   1144: {
                   1145:     char *errbuf;
                   1146:     int errlen;
                   1147:
                   1148:     errlen = regerror(err, pat, 0, 0);
                   1149:     errbuf = emalloc(errlen);
                   1150:     regerror(err, pat, errbuf, errlen);
                   1151:     Error("%s: %s", str, errbuf);
                   1152:     free(errbuf);
                   1153: }
                   1154:
                   1155: /*-
                   1156:  *-----------------------------------------------------------------------
                   1157:  * VarRESubstitute --
                   1158:  *     Perform a regex substitution on the given word, placing the
                   1159:  *     result in the passed buffer.
                   1160:  *
                   1161:  * Results:
                   1162:  *     TRUE if a space is needed before more characters are added.
                   1163:  *
                   1164:  * Side Effects:
                   1165:  *     None.
                   1166:  *
                   1167:  *-----------------------------------------------------------------------
                   1168:  */
                   1169: static Boolean
                   1170: VarRESubstitute(word, addSpace, buf, patternp)
                   1171:     char *word;
                   1172:     Boolean addSpace;
                   1173:     Buffer buf;
                   1174:     ClientData patternp;
                   1175: {
                   1176:     VarREPattern *pat;
                   1177:     int xrv;
                   1178:     char *wp;
                   1179:     char *rp;
                   1180:     int added;
                   1181:
                   1182: #define MAYBE_ADD_SPACE()              \
                   1183:        if (addSpace && !added)         \
1.16      espie    1184:            Buf_AddChar(buf, ' ');      \
1.6       millert  1185:        added = 1
                   1186:
                   1187:     added = 0;
                   1188:     wp = word;
                   1189:     pat = patternp;
                   1190:
                   1191:     if ((pat->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) ==
                   1192:        (VAR_SUB_ONE|VAR_SUB_MATCHED))
                   1193:        xrv = REG_NOMATCH;
                   1194:     else {
                   1195:     tryagain:
                   1196:        xrv = regexec(&pat->re, wp, pat->nsub, pat->matches, 0);
                   1197:     }
                   1198:
                   1199:     switch (xrv) {
                   1200:     case 0:
                   1201:        pat->flags |= VAR_SUB_MATCHED;
                   1202:        if (pat->matches[0].rm_so > 0) {
                   1203:            MAYBE_ADD_SPACE();
1.16      espie    1204:            Buf_AddChars(buf, pat->matches[0].rm_so, wp);
1.6       millert  1205:        }
                   1206:
                   1207:        for (rp = pat->replace; *rp; rp++) {
                   1208:            if ((*rp == '\\') && ((rp[1] == '&') || (rp[1] == '\\'))) {
                   1209:                MAYBE_ADD_SPACE();
1.16      espie    1210:                Buf_AddChar(buf, rp[1]);
1.6       millert  1211:                rp++;
                   1212:            }
                   1213:            else if ((*rp == '&') || ((*rp == '\\') && isdigit(rp[1]))) {
                   1214:                int n;
                   1215:                char *subbuf;
                   1216:                int sublen;
                   1217:                char errstr[3];
                   1218:
                   1219:                if (*rp == '&') {
                   1220:                    n = 0;
                   1221:                    errstr[0] = '&';
                   1222:                    errstr[1] = '\0';
                   1223:                } else {
                   1224:                    n = rp[1] - '0';
                   1225:                    errstr[0] = '\\';
                   1226:                    errstr[1] = rp[1];
                   1227:                    errstr[2] = '\0';
                   1228:                    rp++;
                   1229:                }
                   1230:
                   1231:                if (n > pat->nsub) {
                   1232:                    Error("No subexpression %s", &errstr[0]);
                   1233:                    subbuf = "";
                   1234:                    sublen = 0;
                   1235:                } else if ((pat->matches[n].rm_so == -1) &&
                   1236:                           (pat->matches[n].rm_eo == -1)) {
                   1237:                    Error("No match for subexpression %s", &errstr[0]);
                   1238:                    subbuf = "";
                   1239:                    sublen = 0;
                   1240:                } else {
                   1241:                    subbuf = wp + pat->matches[n].rm_so;
                   1242:                    sublen = pat->matches[n].rm_eo - pat->matches[n].rm_so;
                   1243:                }
                   1244:
                   1245:                if (sublen > 0) {
                   1246:                    MAYBE_ADD_SPACE();
1.16      espie    1247:                    Buf_AddChars(buf, sublen, subbuf);
1.6       millert  1248:                }
                   1249:            } else {
                   1250:                MAYBE_ADD_SPACE();
1.16      espie    1251:                Buf_AddChar(buf, *rp);
1.6       millert  1252:            }
                   1253:        }
                   1254:        wp += pat->matches[0].rm_eo;
                   1255:        if (pat->flags & VAR_SUB_GLOBAL)
                   1256:            goto tryagain;
                   1257:        if (*wp) {
                   1258:            MAYBE_ADD_SPACE();
1.16      espie    1259:            Buf_AddChars(buf, strlen(wp), wp);
1.6       millert  1260:        }
                   1261:        break;
                   1262:     default:
                   1263:        VarREError(xrv, &pat->re, "Unexpected regex error");
                   1264:        /* fall through */
                   1265:     case REG_NOMATCH:
                   1266:        if (*wp) {
                   1267:            MAYBE_ADD_SPACE();
1.16      espie    1268:            Buf_AddChars(buf, strlen(wp), wp);
1.6       millert  1269:        }
                   1270:        break;
                   1271:     }
                   1272:     return(addSpace||added);
                   1273: }
                   1274: #endif
                   1275:
1.1       deraadt  1276: /*-
                   1277:  *-----------------------------------------------------------------------
                   1278:  * VarModify --
                   1279:  *     Modify each of the words of the passed string using the given
                   1280:  *     function. Used to implement all modifiers.
                   1281:  *
                   1282:  * Results:
                   1283:  *     A string of all the words modified appropriately.
                   1284:  *
                   1285:  * Side Effects:
                   1286:  *     None.
                   1287:  *
                   1288:  *-----------------------------------------------------------------------
                   1289:  */
                   1290: static char *
                   1291: VarModify (str, modProc, datum)
                   1292:     char         *str;             /* String whose words should be trimmed */
                   1293:                                    /* Function to use to modify them */
                   1294:     Boolean              (*modProc) __P((char *, Boolean, Buffer, ClientData));
                   1295:     ClientData   datum;            /* Datum to pass it */
                   1296: {
                   1297:     Buffer       buf;              /* Buffer for the new string */
                   1298:     Boolean      addSpace;         /* TRUE if need to add a space to the
                   1299:                                     * buffer before adding the trimmed
                   1300:                                     * word */
1.10      espie    1301:     char **av;                     /* word list */
                   1302:     char *as;                      /* word list memory */
1.1       deraadt  1303:     int ac, i;
                   1304:
1.16      espie    1305:     buf = Buf_Init(0);
1.1       deraadt  1306:     addSpace = FALSE;
                   1307:
1.10      espie    1308:     av = brk_string(str, &ac, FALSE, &as);
1.1       deraadt  1309:
1.10      espie    1310:     for (i = 0; i < ac; i++)
1.1       deraadt  1311:        addSpace = (*modProc)(av[i], addSpace, buf, datum);
1.5       millert  1312:
1.10      espie    1313:     free(as);
                   1314:     free(av);
1.16      espie    1315:     Buf_AddChar(buf, '\0');
                   1316:     str = Buf_GetAll(buf, NULL);
                   1317:     Buf_Destroy(buf, FALSE);
1.1       deraadt  1318:     return (str);
                   1319: }
                   1320:
                   1321: /*-
                   1322:  *-----------------------------------------------------------------------
1.6       millert  1323:  * VarGetPattern --
                   1324:  *     Pass through the tstr looking for 1) escaped delimiters,
                   1325:  *     '$'s and backslashes (place the escaped character in
                   1326:  *     uninterpreted) and 2) unescaped $'s that aren't before
                   1327:  *     the delimiter (expand the variable substitution).
                   1328:  *     Return the expanded string or NULL if the delimiter was missing
1.10      espie    1329:  *     If pattern is specified, handle escaped ampersands, and replace
1.6       millert  1330:  *     unescaped ampersands with the lhs of the pattern.
                   1331:  *
                   1332:  * Results:
                   1333:  *     A string of all the words modified appropriately.
                   1334:  *     If length is specified, return the string length of the buffer
                   1335:  *     If flags is specified and the last character of the pattern is a
                   1336:  *     $ set the VAR_MATCH_END bit of flags.
                   1337:  *
                   1338:  * Side Effects:
                   1339:  *     None.
                   1340:  *-----------------------------------------------------------------------
                   1341:  */
                   1342: static char *
                   1343: VarGetPattern(ctxt, err, tstr, delim, flags, length, pattern)
                   1344:     GNode *ctxt;
                   1345:     int err;
                   1346:     char **tstr;
                   1347:     int delim;
                   1348:     int *flags;
1.16      espie    1349:     size_t *length;
1.6       millert  1350:     VarPattern *pattern;
                   1351: {
                   1352:     char *cp;
                   1353:     Buffer buf = Buf_Init(0);
                   1354:     int junk;
                   1355:     if (length == NULL)
                   1356:        length = &junk;
                   1357:
                   1358: #define IS_A_MATCH(cp, delim) \
                   1359:     ((cp[0] == '\\') && ((cp[1] == delim) ||  \
                   1360:      (cp[1] == '\\') || (cp[1] == '$') || (pattern && (cp[1] == '&'))))
                   1361:
                   1362:     /*
                   1363:      * Skim through until the matching delimiter is found;
                   1364:      * pick up variable substitutions on the way. Also allow
                   1365:      * backslashes to quote the delimiter, $, and \, but don't
                   1366:      * touch other backslashes.
                   1367:      */
                   1368:     for (cp = *tstr; *cp && (*cp != delim); cp++) {
                   1369:        if (IS_A_MATCH(cp, delim)) {
1.16      espie    1370:            Buf_AddChar(buf, cp[1]);
1.6       millert  1371:            cp++;
                   1372:        } else if (*cp == '$') {
                   1373:            if (cp[1] == delim) {
                   1374:                if (flags == NULL)
1.16      espie    1375:                    Buf_AddChar(buf, *cp);
1.6       millert  1376:                else
                   1377:                    /*
                   1378:                     * Unescaped $ at end of pattern => anchor
                   1379:                     * pattern at end.
                   1380:                     */
                   1381:                    *flags |= VAR_MATCH_END;
                   1382:            }
                   1383:            else {
                   1384:                char   *cp2;
                   1385:                int     len;
                   1386:                Boolean freeIt;
                   1387:
                   1388:                /*
                   1389:                 * If unescaped dollar sign not before the
                   1390:                 * delimiter, assume it's a variable
                   1391:                 * substitution and recurse.
                   1392:                 */
                   1393:                cp2 = Var_Parse(cp, ctxt, err, &len, &freeIt);
1.16      espie    1394:                Buf_AddChars(buf, strlen(cp2), cp2);
1.6       millert  1395:                if (freeIt)
                   1396:                    free(cp2);
                   1397:                cp += len - 1;
                   1398:            }
                   1399:        }
                   1400:        else if (pattern && *cp == '&')
1.16      espie    1401:            Buf_AddChars(buf, pattern->leftLen, pattern->lhs);
1.6       millert  1402:        else
1.16      espie    1403:            Buf_AddChar(buf, *cp);
1.6       millert  1404:     }
                   1405:
1.16      espie    1406:     Buf_AddChar(buf, '\0');
1.6       millert  1407:
                   1408:     if (*cp != delim) {
                   1409:        *tstr = cp;
                   1410:        *length = 0;
                   1411:        return NULL;
                   1412:     }
                   1413:     else {
                   1414:        *tstr = ++cp;
1.16      espie    1415:        cp = Buf_GetAll(buf, length);
1.6       millert  1416:        *length -= 1;   /* Don't count the NULL */
                   1417:        Buf_Destroy(buf, FALSE);
                   1418:        return cp;
                   1419:     }
                   1420: }
                   1421:
                   1422: /*-
                   1423:  *-----------------------------------------------------------------------
                   1424:  * VarQuote --
                   1425:  *     Quote shell meta-characters in the string
                   1426:  *
                   1427:  * Results:
                   1428:  *     The quoted string
                   1429:  *
                   1430:  * Side Effects:
                   1431:  *     None.
                   1432:  *
                   1433:  *-----------------------------------------------------------------------
                   1434:  */
                   1435: static char *
                   1436: VarQuote(str)
                   1437:        char *str;
                   1438: {
                   1439:
                   1440:     Buffer       buf;
                   1441:     /* This should cover most shells :-( */
                   1442:     static char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~";
                   1443:
1.16      espie    1444:     buf = Buf_Init(MAKE_BSIZE);
1.6       millert  1445:     for (; *str; str++) {
                   1446:        if (strchr(meta, *str) != NULL)
1.16      espie    1447:            Buf_AddChar(buf, '\\');
                   1448:        Buf_AddChar(buf, *str);
1.6       millert  1449:     }
1.16      espie    1450:     Buf_AddChar(buf, '\0');
                   1451:     str = Buf_GetAll(buf, NULL);
                   1452:     Buf_Destroy(buf, FALSE);
1.6       millert  1453:     return str;
                   1454: }
                   1455:
                   1456: /*-
                   1457:  *-----------------------------------------------------------------------
1.1       deraadt  1458:  * Var_Parse --
                   1459:  *     Given the start of a variable invocation, extract the variable
                   1460:  *     name and find its value, then modify it according to the
                   1461:  *     specification.
                   1462:  *
                   1463:  * Results:
                   1464:  *     The (possibly-modified) value of the variable or var_Error if the
                   1465:  *     specification is invalid. The length of the specification is
                   1466:  *     placed in *lengthPtr (for invalid specifications, this is just
                   1467:  *     2...?).
                   1468:  *     A Boolean in *freePtr telling whether the returned string should
                   1469:  *     be freed by the caller.
                   1470:  *
                   1471:  * Side Effects:
                   1472:  *     None.
                   1473:  *
                   1474:  *-----------------------------------------------------------------------
                   1475:  */
                   1476: char *
                   1477: Var_Parse (str, ctxt, err, lengthPtr, freePtr)
                   1478:     char         *str;         /* The string to parse */
                   1479:     GNode        *ctxt;        /* The context for the variable */
                   1480:     Boolean        err;        /* TRUE if undefined variables are an error */
                   1481:     int                    *lengthPtr; /* OUT: The length of the specification */
                   1482:     Boolean        *freePtr;   /* OUT: TRUE if caller should free result */
                   1483: {
                   1484:     register char   *tstr;     /* Pointer into str */
                   1485:     Var                    *v;         /* Variable in invocation */
1.6       millert  1486:     char           *cp;        /* Secondary pointer into str (place marker
1.1       deraadt  1487:                                 * for tstr) */
                   1488:     Boolean        haveModifier;/* TRUE if have modifiers for the variable */
                   1489:     register char   endc;      /* Ending character when variable in parens
                   1490:                                 * or braces */
1.2       deraadt  1491:     register char   startc=0;  /* Starting character when variable in parens
1.1       deraadt  1492:                                 * or braces */
                   1493:     int             cnt;       /* Used to count brace pairs when variable in
                   1494:                                 * in parens or braces */
                   1495:     char           *start;
1.6       millert  1496:     char            delim;
1.1       deraadt  1497:     Boolean        dynamic;    /* TRUE if the variable is local and we're
                   1498:                                 * expanding it in a non-local context. This
                   1499:                                 * is done to support dynamic sources. The
                   1500:                                 * result is just the invocation, unaltered */
1.5       millert  1501:
1.1       deraadt  1502:     *freePtr = FALSE;
                   1503:     dynamic = FALSE;
                   1504:     start = str;
1.5       millert  1505:
1.1       deraadt  1506:     if (str[1] != '(' && str[1] != '{') {
                   1507:        /*
                   1508:         * If it's not bounded by braces of some sort, life is much simpler.
                   1509:         * We just need to check for the first character and return the
                   1510:         * value if it exists.
                   1511:         */
                   1512:        char      name[2];
                   1513:
                   1514:        name[0] = str[1];
                   1515:        name[1] = '\0';
                   1516:
                   1517:        v = VarFind (name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
                   1518:        if (v == (Var *)NIL) {
                   1519:            *lengthPtr = 2;
1.5       millert  1520:
1.1       deraadt  1521:            if ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) {
                   1522:                /*
                   1523:                 * If substituting a local variable in a non-local context,
                   1524:                 * assume it's for dynamic source stuff. We have to handle
                   1525:                 * this specially and return the longhand for the variable
                   1526:                 * with the dollar sign escaped so it makes it back to the
                   1527:                 * caller. Only four of the local variables are treated
                   1528:                 * specially as they are the only four that will be set
                   1529:                 * when dynamic sources are expanded.
                   1530:                 */
                   1531:                switch (str[1]) {
                   1532:                    case '@':
                   1533:                        return("$(.TARGET)");
                   1534:                    case '%':
                   1535:                        return("$(.ARCHIVE)");
                   1536:                    case '*':
                   1537:                        return("$(.PREFIX)");
                   1538:                    case '!':
                   1539:                        return("$(.MEMBER)");
                   1540:                }
                   1541:            }
                   1542:            /*
                   1543:             * Error
                   1544:             */
                   1545:            return (err ? var_Error : varNoError);
                   1546:        } else {
                   1547:            haveModifier = FALSE;
                   1548:            tstr = &str[1];
                   1549:            endc = str[1];
                   1550:        }
                   1551:     } else {
                   1552:        startc = str[1];
                   1553:        endc = startc == '(' ? ')' : '}';
                   1554:
                   1555:        /*
                   1556:         * Skip to the end character or a colon, whichever comes first.
                   1557:         */
                   1558:        for (tstr = str + 2;
                   1559:             *tstr != '\0' && *tstr != endc && *tstr != ':';
                   1560:             tstr++)
                   1561:        {
                   1562:            continue;
                   1563:        }
                   1564:        if (*tstr == ':') {
                   1565:            haveModifier = TRUE;
                   1566:        } else if (*tstr != '\0') {
                   1567:            haveModifier = FALSE;
                   1568:        } else {
                   1569:            /*
                   1570:             * If we never did find the end character, return NULL
                   1571:             * right now, setting the length to be the distance to
                   1572:             * the end of the string, since that's what make does.
                   1573:             */
                   1574:            *lengthPtr = tstr - str;
                   1575:            return (var_Error);
                   1576:        }
                   1577:        *tstr = '\0';
1.5       millert  1578:
1.1       deraadt  1579:        v = VarFind (str + 2, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
                   1580:        if ((v == (Var *)NIL) && (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) &&
                   1581:            ((tstr-str) == 4) && (str[3] == 'F' || str[3] == 'D'))
                   1582:        {
                   1583:            /*
                   1584:             * Check for bogus D and F forms of local variables since we're
                   1585:             * in a local context and the name is the right length.
                   1586:             */
                   1587:            switch(str[2]) {
                   1588:                case '@':
                   1589:                case '%':
                   1590:                case '*':
                   1591:                case '!':
                   1592:                case '>':
                   1593:                case '<':
                   1594:                {
                   1595:                    char    vname[2];
                   1596:                    char    *val;
                   1597:
                   1598:                    /*
                   1599:                     * Well, it's local -- go look for it.
                   1600:                     */
                   1601:                    vname[0] = str[2];
                   1602:                    vname[1] = '\0';
                   1603:                    v = VarFind(vname, ctxt, 0);
1.5       millert  1604:
1.1       deraadt  1605:                    if (v != (Var *)NIL) {
                   1606:                        /*
                   1607:                         * No need for nested expansion or anything, as we're
                   1608:                         * the only one who sets these things and we sure don't
                   1609:                         * but nested invocations in them...
                   1610:                         */
1.16      espie    1611:                        val = Buf_GetAll(v->val, NULL);
1.5       millert  1612:
1.1       deraadt  1613:                        if (str[3] == 'D') {
                   1614:                            val = VarModify(val, VarHead, (ClientData)0);
                   1615:                        } else {
                   1616:                            val = VarModify(val, VarTail, (ClientData)0);
                   1617:                        }
                   1618:                        /*
                   1619:                         * Resulting string is dynamically allocated, so
                   1620:                         * tell caller to free it.
                   1621:                         */
                   1622:                        *freePtr = TRUE;
                   1623:                        *lengthPtr = tstr-start+1;
                   1624:                        *tstr = endc;
                   1625:                        return(val);
                   1626:                    }
                   1627:                    break;
                   1628:                }
                   1629:            }
                   1630:        }
1.5       millert  1631:
1.1       deraadt  1632:        if (v == (Var *)NIL) {
                   1633:            if ((((tstr-str) == 3) ||
                   1634:                 ((((tstr-str) == 4) && (str[3] == 'F' ||
                   1635:                                         str[3] == 'D')))) &&
                   1636:                ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)))
                   1637:            {
                   1638:                /*
                   1639:                 * If substituting a local variable in a non-local context,
                   1640:                 * assume it's for dynamic source stuff. We have to handle
                   1641:                 * this specially and return the longhand for the variable
                   1642:                 * with the dollar sign escaped so it makes it back to the
                   1643:                 * caller. Only four of the local variables are treated
                   1644:                 * specially as they are the only four that will be set
                   1645:                 * when dynamic sources are expanded.
                   1646:                 */
                   1647:                switch (str[2]) {
                   1648:                    case '@':
                   1649:                    case '%':
                   1650:                    case '*':
                   1651:                    case '!':
                   1652:                        dynamic = TRUE;
                   1653:                        break;
                   1654:                }
                   1655:            } else if (((tstr-str) > 4) && (str[2] == '.') &&
                   1656:                       isupper((unsigned char) str[3]) &&
                   1657:                       ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)))
                   1658:            {
                   1659:                int     len;
1.5       millert  1660:
1.1       deraadt  1661:                len = (tstr-str) - 3;
                   1662:                if ((strncmp(str+2, ".TARGET", len) == 0) ||
                   1663:                    (strncmp(str+2, ".ARCHIVE", len) == 0) ||
                   1664:                    (strncmp(str+2, ".PREFIX", len) == 0) ||
                   1665:                    (strncmp(str+2, ".MEMBER", len) == 0))
                   1666:                {
                   1667:                    dynamic = TRUE;
                   1668:                }
                   1669:            }
1.5       millert  1670:
1.1       deraadt  1671:            if (!haveModifier) {
                   1672:                /*
                   1673:                 * No modifiers -- have specification length so we can return
                   1674:                 * now.
                   1675:                 */
                   1676:                *lengthPtr = tstr - start + 1;
                   1677:                *tstr = endc;
                   1678:                if (dynamic) {
                   1679:                    str = emalloc(*lengthPtr + 1);
                   1680:                    strncpy(str, start, *lengthPtr);
                   1681:                    str[*lengthPtr] = '\0';
                   1682:                    *freePtr = TRUE;
                   1683:                    return(str);
                   1684:                } else {
                   1685:                    return (err ? var_Error : varNoError);
                   1686:                }
                   1687:            } else {
                   1688:                /*
                   1689:                 * Still need to get to the end of the variable specification,
                   1690:                 * so kludge up a Var structure for the modifications
                   1691:                 */
                   1692:                v = (Var *) emalloc(sizeof(Var));
                   1693:                v->name = &str[1];
                   1694:                v->val = Buf_Init(1);
                   1695:                v->flags = VAR_JUNK;
                   1696:            }
                   1697:        }
                   1698:     }
                   1699:
                   1700:     if (v->flags & VAR_IN_USE) {
                   1701:        Fatal("Variable %s is recursive.", v->name);
                   1702:        /*NOTREACHED*/
                   1703:     } else {
                   1704:        v->flags |= VAR_IN_USE;
                   1705:     }
                   1706:     /*
                   1707:      * Before doing any modification, we have to make sure the value
                   1708:      * has been fully expanded. If it looks like recursion might be
                   1709:      * necessary (there's a dollar sign somewhere in the variable's value)
                   1710:      * we just call Var_Subst to do any other substitutions that are
                   1711:      * necessary. Note that the value returned by Var_Subst will have
                   1712:      * been dynamically-allocated, so it will need freeing when we
                   1713:      * return.
                   1714:      */
1.16      espie    1715:     str = Buf_GetAll(v->val, NULL);
1.1       deraadt  1716:     if (strchr (str, '$') != (char *)NULL) {
                   1717:        str = Var_Subst(NULL, str, ctxt, err);
                   1718:        *freePtr = TRUE;
                   1719:     }
1.5       millert  1720:
1.1       deraadt  1721:     v->flags &= ~VAR_IN_USE;
1.5       millert  1722:
1.1       deraadt  1723:     /*
                   1724:      * Now we need to apply any modifiers the user wants applied.
                   1725:      * These are:
                   1726:      *           :M<pattern>   words which match the given <pattern>.
                   1727:      *                         <pattern> is of the standard file
                   1728:      *                         wildcarding form.
                   1729:      *           :S<d><pat1><d><pat2><d>[g]
                   1730:      *                         Substitute <pat2> for <pat1> in the value
1.6       millert  1731:      *           :C<d><pat1><d><pat2><d>[g]
                   1732:      *                         Substitute <pat2> for regex <pat1> in the value
1.1       deraadt  1733:      *           :H            Substitute the head of each word
                   1734:      *           :T            Substitute the tail of each word
                   1735:      *           :E            Substitute the extension (minus '.') of
                   1736:      *                         each word
                   1737:      *           :R            Substitute the root of each word
                   1738:      *                         (pathname minus the suffix).
                   1739:      *           :lhs=rhs      Like :S, but the rhs goes to the end of
                   1740:      *                         the invocation.
                   1741:      */
                   1742:     if ((str != (char *)NULL) && haveModifier) {
                   1743:        /*
                   1744:         * Skip initial colon while putting it back.
                   1745:         */
                   1746:        *tstr++ = ':';
                   1747:        while (*tstr != endc) {
                   1748:            char        *newStr;    /* New value to return */
                   1749:            char        termc;      /* Character which terminated scan */
1.5       millert  1750:
1.1       deraadt  1751:            if (DEBUG(VAR)) {
                   1752:                printf("Applying :%c to \"%s\"\n", *tstr, str);
                   1753:            }
                   1754:            switch (*tstr) {
                   1755:                case 'N':
                   1756:                case 'M':
                   1757:                {
                   1758:                    char    *pattern;
                   1759:                    char    *cp2;
                   1760:                    Boolean copy;
                   1761:
                   1762:                    copy = FALSE;
                   1763:                    for (cp = tstr + 1;
                   1764:                         *cp != '\0' && *cp != ':' && *cp != endc;
                   1765:                         cp++)
                   1766:                    {
                   1767:                        if (*cp == '\\' && (cp[1] == ':' || cp[1] == endc)){
                   1768:                            copy = TRUE;
                   1769:                            cp++;
                   1770:                        }
                   1771:                    }
                   1772:                    termc = *cp;
                   1773:                    *cp = '\0';
                   1774:                    if (copy) {
                   1775:                        /*
                   1776:                         * Need to compress the \:'s out of the pattern, so
                   1777:                         * allocate enough room to hold the uncompressed
                   1778:                         * pattern (note that cp started at tstr+1, so
                   1779:                         * cp - tstr takes the null byte into account) and
                   1780:                         * compress the pattern into the space.
                   1781:                         */
                   1782:                        pattern = emalloc(cp - tstr);
                   1783:                        for (cp2 = pattern, cp = tstr + 1;
                   1784:                             *cp != '\0';
                   1785:                             cp++, cp2++)
                   1786:                        {
                   1787:                            if ((*cp == '\\') &&
                   1788:                                (cp[1] == ':' || cp[1] == endc)) {
                   1789:                                    cp++;
                   1790:                            }
                   1791:                            *cp2 = *cp;
                   1792:                        }
                   1793:                        *cp2 = '\0';
                   1794:                    } else {
                   1795:                        pattern = &tstr[1];
                   1796:                    }
                   1797:                    if (*tstr == 'M' || *tstr == 'm') {
                   1798:                        newStr = VarModify(str, VarMatch, (ClientData)pattern);
                   1799:                    } else {
                   1800:                        newStr = VarModify(str, VarNoMatch,
                   1801:                                           (ClientData)pattern);
                   1802:                    }
                   1803:                    if (copy) {
                   1804:                        free(pattern);
                   1805:                    }
                   1806:                    break;
                   1807:                }
                   1808:                case 'S':
                   1809:                {
                   1810:                    VarPattern      pattern;
                   1811:
                   1812:                    pattern.flags = 0;
                   1813:                    delim = tstr[1];
                   1814:                    tstr += 2;
1.6       millert  1815:
1.1       deraadt  1816:                    /*
                   1817:                     * If pattern begins with '^', it is anchored to the
                   1818:                     * start of the word -- skip over it and flag pattern.
                   1819:                     */
                   1820:                    if (*tstr == '^') {
                   1821:                        pattern.flags |= VAR_MATCH_START;
                   1822:                        tstr += 1;
                   1823:                    }
                   1824:
1.6       millert  1825:                    cp = tstr;
                   1826:                    if ((pattern.lhs = VarGetPattern(ctxt, err, &cp, delim,
                   1827:                        &pattern.flags, &pattern.leftLen, NULL)) == NULL)
                   1828:                        goto cleanup;
                   1829:
                   1830:                    if ((pattern.rhs = VarGetPattern(ctxt, err, &cp, delim,
                   1831:                        NULL, &pattern.rightLen, &pattern)) == NULL)
                   1832:                        goto cleanup;
1.5       millert  1833:
1.1       deraadt  1834:                    /*
1.6       millert  1835:                     * Check for global substitution. If 'g' after the final
                   1836:                     * delimiter, substitution is global and is marked that
                   1837:                     * way.
1.1       deraadt  1838:                     */
1.6       millert  1839:                    for (;; cp++) {
                   1840:                        switch (*cp) {
                   1841:                        case 'g':
                   1842:                            pattern.flags |= VAR_SUB_GLOBAL;
                   1843:                            continue;
                   1844:                        case '1':
                   1845:                            pattern.flags |= VAR_SUB_ONE;
                   1846:                            continue;
1.1       deraadt  1847:                        }
1.6       millert  1848:                        break;
1.1       deraadt  1849:                    }
                   1850:
1.6       millert  1851:                    termc = *cp;
                   1852:                    newStr = VarModify(str, VarSubstitute,
                   1853:                                       (ClientData)&pattern);
1.5       millert  1854:
1.1       deraadt  1855:                    /*
1.6       millert  1856:                     * Free the two strings.
1.1       deraadt  1857:                     */
1.6       millert  1858:                    free(pattern.lhs);
                   1859:                    free(pattern.rhs);
                   1860:                    break;
                   1861:                }
                   1862: #ifndef MAKE_BOOTSTRAP
                   1863:                case 'C':
                   1864:                {
                   1865:                    VarREPattern    pattern;
                   1866:                    char           *re;
                   1867:                    int             error;
                   1868:
                   1869:                    pattern.flags = 0;
                   1870:                    delim = tstr[1];
                   1871:                    tstr += 2;
1.1       deraadt  1872:
1.6       millert  1873:                    cp = tstr;
1.1       deraadt  1874:
1.6       millert  1875:                    if ((re = VarGetPattern(ctxt, err, &cp, delim, NULL,
                   1876:                        NULL, NULL)) == NULL)
                   1877:                        goto cleanup;
                   1878:
                   1879:                    if ((pattern.replace = VarGetPattern(ctxt, err, &cp,
                   1880:                        delim, NULL, NULL, NULL)) == NULL) {
                   1881:                        free(re);
                   1882:                        goto cleanup;
                   1883:                    }
1.5       millert  1884:
1.6       millert  1885:                    for (;; cp++) {
                   1886:                        switch (*cp) {
                   1887:                        case 'g':
                   1888:                            pattern.flags |= VAR_SUB_GLOBAL;
                   1889:                            continue;
                   1890:                        case '1':
                   1891:                            pattern.flags |= VAR_SUB_ONE;
                   1892:                            continue;
1.1       deraadt  1893:                        }
1.6       millert  1894:                        break;
1.1       deraadt  1895:                    }
                   1896:
1.6       millert  1897:                    termc = *cp;
1.5       millert  1898:
1.6       millert  1899:                    error = regcomp(&pattern.re, re, REG_EXTENDED);
                   1900:                    free(re);
                   1901:                    if (error) {
1.1       deraadt  1902:                        *lengthPtr = cp - start + 1;
1.6       millert  1903:                        VarREError(error, &pattern.re, "RE substitution error");
                   1904:                        free(pattern.replace);
1.1       deraadt  1905:                        return (var_Error);
                   1906:                    }
                   1907:
1.6       millert  1908:                    pattern.nsub = pattern.re.re_nsub + 1;
                   1909:                    if (pattern.nsub < 1)
                   1910:                        pattern.nsub = 1;
                   1911:                    if (pattern.nsub > 10)
                   1912:                        pattern.nsub = 10;
                   1913:                    pattern.matches = emalloc(pattern.nsub *
                   1914:                                              sizeof(regmatch_t));
                   1915:                    newStr = VarModify(str, VarRESubstitute,
                   1916:                                       (ClientData) &pattern);
                   1917:                    regfree(&pattern.re);
                   1918:                    free(pattern.replace);
                   1919:                    free(pattern.matches);
1.1       deraadt  1920:                    break;
                   1921:                }
1.6       millert  1922: #endif
                   1923:                case 'Q':
                   1924:                    if (tstr[1] == endc || tstr[1] == ':') {
                   1925:                        newStr = VarQuote (str);
                   1926:                        cp = tstr + 1;
                   1927:                        termc = *cp;
                   1928:                        break;
                   1929:                    }
                   1930:                    /*FALLTHRU*/
1.1       deraadt  1931:                case 'T':
                   1932:                    if (tstr[1] == endc || tstr[1] == ':') {
                   1933:                        newStr = VarModify (str, VarTail, (ClientData)0);
                   1934:                        cp = tstr + 1;
                   1935:                        termc = *cp;
                   1936:                        break;
                   1937:                    }
                   1938:                    /*FALLTHRU*/
                   1939:                case 'H':
                   1940:                    if (tstr[1] == endc || tstr[1] == ':') {
                   1941:                        newStr = VarModify (str, VarHead, (ClientData)0);
                   1942:                        cp = tstr + 1;
                   1943:                        termc = *cp;
                   1944:                        break;
                   1945:                    }
                   1946:                    /*FALLTHRU*/
                   1947:                case 'E':
                   1948:                    if (tstr[1] == endc || tstr[1] == ':') {
                   1949:                        newStr = VarModify (str, VarSuffix, (ClientData)0);
                   1950:                        cp = tstr + 1;
                   1951:                        termc = *cp;
                   1952:                        break;
                   1953:                    }
                   1954:                    /*FALLTHRU*/
                   1955:                case 'R':
                   1956:                    if (tstr[1] == endc || tstr[1] == ':') {
                   1957:                        newStr = VarModify (str, VarRoot, (ClientData)0);
1.11      espie    1958:                        cp = tstr + 1;
                   1959:                        termc = *cp;
                   1960:                        break;
                   1961:                    }
                   1962:                    /*FALLTHRU*/
                   1963:                case 'U':
                   1964:                    if (tstr[1] == endc || tstr[1] == ':') {
                   1965:                        newStr = VarModify (str, VarUppercase, (ClientData)0);
                   1966:                        cp = tstr + 1;
                   1967:                        termc = *cp;
                   1968:                        break;
                   1969:                    }
                   1970:                    /*FALLTHRU*/
                   1971:                case 'L':
                   1972:                    if (tstr[1] == endc || tstr[1] == ':') {
                   1973:                        newStr = VarModify (str, VarLowercase, (ClientData)0);
1.1       deraadt  1974:                        cp = tstr + 1;
                   1975:                        termc = *cp;
                   1976:                        break;
                   1977:                    }
                   1978:                    /*FALLTHRU*/
1.4       briggs   1979: #ifdef SUNSHCMD
                   1980:                case 's':
                   1981:                    if (tstr[1] == 'h' && (tstr[2] == endc || tstr[2] == ':')) {
                   1982:                        char *err;
                   1983:                        newStr = Cmd_Exec (str, &err);
                   1984:                        if (err)
                   1985:                            Error (err, str);
                   1986:                        cp = tstr + 2;
                   1987:                        termc = *cp;
                   1988:                        break;
                   1989:                    }
                   1990:                    /*FALLTHRU*/
                   1991: #endif
                   1992:                default:
                   1993:                {
                   1994: #ifdef SYSVVARSUB
1.1       deraadt  1995:                    /*
                   1996:                     * This can either be a bogus modifier or a System-V
                   1997:                     * substitution command.
                   1998:                     */
                   1999:                    VarPattern      pattern;
                   2000:                    Boolean         eqFound;
1.5       millert  2001:
1.1       deraadt  2002:                    pattern.flags = 0;
                   2003:                    eqFound = FALSE;
                   2004:                    /*
                   2005:                     * First we make a pass through the string trying
                   2006:                     * to verify it is a SYSV-make-style translation:
                   2007:                     * it must be: <string1>=<string2>)
                   2008:                     */
                   2009:                    cp = tstr;
                   2010:                    cnt = 1;
                   2011:                    while (*cp != '\0' && cnt) {
                   2012:                        if (*cp == '=') {
                   2013:                            eqFound = TRUE;
                   2014:                            /* continue looking for endc */
                   2015:                        }
                   2016:                        else if (*cp == endc)
                   2017:                            cnt--;
                   2018:                        else if (*cp == startc)
                   2019:                            cnt++;
                   2020:                        if (cnt)
                   2021:                            cp++;
                   2022:                    }
                   2023:                    if (*cp == endc && eqFound) {
1.5       millert  2024:
1.1       deraadt  2025:                        /*
                   2026:                         * Now we break this sucker into the lhs and
                   2027:                         * rhs. We must null terminate them of course.
                   2028:                         */
                   2029:                        for (cp = tstr; *cp != '='; cp++)
                   2030:                            continue;
                   2031:                        pattern.lhs = tstr;
                   2032:                        pattern.leftLen = cp - tstr;
                   2033:                        *cp++ = '\0';
1.5       millert  2034:
1.1       deraadt  2035:                        pattern.rhs = cp;
                   2036:                        cnt = 1;
                   2037:                        while (cnt) {
                   2038:                            if (*cp == endc)
                   2039:                                cnt--;
                   2040:                            else if (*cp == startc)
                   2041:                                cnt++;
                   2042:                            if (cnt)
                   2043:                                cp++;
                   2044:                        }
                   2045:                        pattern.rightLen = cp - pattern.rhs;
                   2046:                        *cp = '\0';
1.5       millert  2047:
1.1       deraadt  2048:                        /*
                   2049:                         * SYSV modifications happen through the whole
                   2050:                         * string. Note the pattern is anchored at the end.
                   2051:                         */
                   2052:                        newStr = VarModify(str, VarSYSVMatch,
                   2053:                                           (ClientData)&pattern);
                   2054:
                   2055:                        /*
                   2056:                         * Restore the nulled characters
                   2057:                         */
                   2058:                        pattern.lhs[pattern.leftLen] = '=';
                   2059:                        pattern.rhs[pattern.rightLen] = endc;
                   2060:                        termc = endc;
1.4       briggs   2061:                    } else
                   2062: #endif
                   2063:                    {
1.1       deraadt  2064:                        Error ("Unknown modifier '%c'\n", *tstr);
                   2065:                        for (cp = tstr+1;
                   2066:                             *cp != ':' && *cp != endc && *cp != '\0';
1.5       millert  2067:                             cp++)
1.1       deraadt  2068:                                 continue;
                   2069:                        termc = *cp;
                   2070:                        newStr = var_Error;
                   2071:                    }
                   2072:                }
                   2073:            }
                   2074:            if (DEBUG(VAR)) {
                   2075:                printf("Result is \"%s\"\n", newStr);
                   2076:            }
1.5       millert  2077:
1.1       deraadt  2078:            if (*freePtr) {
                   2079:                free (str);
                   2080:            }
                   2081:            str = newStr;
                   2082:            if (str != var_Error) {
                   2083:                *freePtr = TRUE;
                   2084:            } else {
                   2085:                *freePtr = FALSE;
                   2086:            }
                   2087:            if (termc == '\0') {
                   2088:                Error("Unclosed variable specification for %s", v->name);
                   2089:            } else if (termc == ':') {
                   2090:                *cp++ = termc;
                   2091:            } else {
                   2092:                *cp = termc;
                   2093:            }
                   2094:            tstr = cp;
                   2095:        }
                   2096:        *lengthPtr = tstr - start + 1;
                   2097:     } else {
                   2098:        *lengthPtr = tstr - start + 1;
                   2099:        *tstr = endc;
                   2100:     }
1.5       millert  2101:
1.17    ! espie    2102:     if (v->flags & VAR_JUNK) {
1.1       deraadt  2103:        /*
                   2104:         * Perform any free'ing needed and set *freePtr to FALSE so the caller
                   2105:         * doesn't try to free a static pointer.
                   2106:         */
                   2107:        if (*freePtr) {
                   2108:            free(str);
                   2109:        }
                   2110:        *freePtr = FALSE;
                   2111:        Buf_Destroy(v->val, TRUE);
                   2112:        free((Address)v);
                   2113:        if (dynamic) {
                   2114:            str = emalloc(*lengthPtr + 1);
                   2115:            strncpy(str, start, *lengthPtr);
                   2116:            str[*lengthPtr] = '\0';
                   2117:            *freePtr = TRUE;
                   2118:        } else {
1.12      espie    2119:            str = err ? var_Error : varNoError;
1.1       deraadt  2120:        }
                   2121:     }
                   2122:     return (str);
1.6       millert  2123:
                   2124: cleanup:
                   2125:     *lengthPtr = cp - start + 1;
                   2126:     if (*freePtr)
                   2127:        free(str);
                   2128:     Error("Unclosed substitution for %s (%c missing)",
                   2129:          v->name, delim);
                   2130:     return (var_Error);
1.1       deraadt  2131: }
                   2132:
                   2133: /*-
                   2134:  *-----------------------------------------------------------------------
                   2135:  * Var_Subst  --
                   2136:  *     Substitute for all variables in the given string in the given context
                   2137:  *     If undefErr is TRUE, Parse_Error will be called when an undefined
                   2138:  *     variable is encountered.
                   2139:  *
                   2140:  * Results:
                   2141:  *     The resulting string.
                   2142:  *
                   2143:  * Side Effects:
                   2144:  *     None. The old string must be freed by the caller
                   2145:  *-----------------------------------------------------------------------
                   2146:  */
                   2147: char *
                   2148: Var_Subst (var, str, ctxt, undefErr)
                   2149:     char         *var;             /* Named variable || NULL for all */
                   2150:     char         *str;             /* the string in which to substitute */
                   2151:     GNode         *ctxt;           /* the context wherein to find variables */
                   2152:     Boolean      undefErr;         /* TRUE if undefineds are an error */
                   2153: {
                   2154:     Buffer       buf;              /* Buffer for forming things */
                   2155:     char         *val;             /* Value to substitute for a variable */
                   2156:     int                  length;           /* Length of the variable invocation */
                   2157:     Boolean      doFree;           /* Set true if val should be freed */
                   2158:     static Boolean errorReported;   /* Set true if an error has already
                   2159:                                     * been reported to prevent a plethora
                   2160:                                     * of messages when recursing */
                   2161:
1.16      espie    2162:     buf = Buf_Init(MAKE_BSIZE);
1.1       deraadt  2163:     errorReported = FALSE;
                   2164:
                   2165:     while (*str) {
                   2166:        if (var == NULL && (*str == '$') && (str[1] == '$')) {
                   2167:            /*
                   2168:             * A dollar sign may be escaped either with another dollar sign.
                   2169:             * In such a case, we skip over the escape character and store the
                   2170:             * dollar sign into the buffer directly.
                   2171:             */
                   2172:            str++;
1.16      espie    2173:            Buf_AddChar(buf, *str);
1.1       deraadt  2174:            str++;
                   2175:        } else if (*str != '$') {
                   2176:            /*
                   2177:             * Skip as many characters as possible -- either to the end of
                   2178:             * the string or to the next dollar sign (variable invocation).
                   2179:             */
                   2180:            char  *cp;
                   2181:
                   2182:            for (cp = str++; *str != '$' && *str != '\0'; str++)
                   2183:                continue;
1.16      espie    2184:            Buf_AddChars(buf, str - cp, cp);
1.1       deraadt  2185:        } else {
                   2186:            if (var != NULL) {
                   2187:                int expand;
                   2188:                for (;;) {
                   2189:                    if (str[1] != '(' && str[1] != '{') {
1.13      espie    2190:                        if (str[1] != *var || var[1] != '\0') {
1.16      espie    2191:                            Buf_AddChars(buf, 2, str);
1.1       deraadt  2192:                            str += 2;
                   2193:                            expand = FALSE;
                   2194:                        }
                   2195:                        else
                   2196:                            expand = TRUE;
                   2197:                        break;
                   2198:                    }
                   2199:                    else {
                   2200:                        char *p;
                   2201:
                   2202:                        /*
                   2203:                         * Scan up to the end of the variable name.
                   2204:                         */
1.5       millert  2205:                        for (p = &str[2]; *p &&
1.1       deraadt  2206:                             *p != ':' && *p != ')' && *p != '}'; p++)
1.5       millert  2207:                            if (*p == '$')
1.1       deraadt  2208:                                break;
                   2209:                        /*
                   2210:                         * A variable inside the variable. We cannot expand
                   2211:                         * the external variable yet, so we try again with
                   2212:                         * the nested one
                   2213:                         */
                   2214:                        if (*p == '$') {
1.16      espie    2215:                            Buf_AddChars(buf, p - str, str);
1.1       deraadt  2216:                            str = p;
                   2217:                            continue;
                   2218:                        }
1.5       millert  2219:
                   2220:                        if (strncmp(var, str + 2, p - str - 2) != 0 ||
1.1       deraadt  2221:                            var[p - str - 2] != '\0') {
                   2222:                            /*
                   2223:                             * Not the variable we want to expand, scan
                   2224:                             * until the next variable
                   2225:                             */
1.5       millert  2226:                            for (;*p != '$' && *p != '\0'; p++)
1.1       deraadt  2227:                                continue;
1.16      espie    2228:                            Buf_AddChars(buf, p - str, str);
1.1       deraadt  2229:                            str = p;
                   2230:                            expand = FALSE;
                   2231:                        }
                   2232:                        else
                   2233:                            expand = TRUE;
                   2234:                        break;
                   2235:                    }
                   2236:                }
                   2237:                if (!expand)
                   2238:                    continue;
                   2239:            }
1.5       millert  2240:
1.1       deraadt  2241:            val = Var_Parse (str, ctxt, undefErr, &length, &doFree);
                   2242:
                   2243:            /*
                   2244:             * When we come down here, val should either point to the
                   2245:             * value of this variable, suitably modified, or be NULL.
                   2246:             * Length should be the total length of the potential
                   2247:             * variable invocation (from $ to end character...)
                   2248:             */
                   2249:            if (val == var_Error || val == varNoError) {
                   2250:                /*
                   2251:                 * If performing old-time variable substitution, skip over
                   2252:                 * the variable and continue with the substitution. Otherwise,
                   2253:                 * store the dollar sign and advance str so we continue with
                   2254:                 * the string...
                   2255:                 */
                   2256:                if (oldVars) {
                   2257:                    str += length;
                   2258:                } else if (undefErr) {
                   2259:                    /*
                   2260:                     * If variable is undefined, complain and skip the
                   2261:                     * variable. The complaint will stop us from doing anything
                   2262:                     * when the file is parsed.
                   2263:                     */
                   2264:                    if (!errorReported) {
                   2265:                        Parse_Error (PARSE_FATAL,
                   2266:                                     "Undefined variable \"%.*s\"",length,str);
                   2267:                    }
                   2268:                    str += length;
                   2269:                    errorReported = TRUE;
                   2270:                } else {
1.16      espie    2271:                    Buf_AddChar(buf, *str);
1.1       deraadt  2272:                    str += 1;
                   2273:                }
                   2274:            } else {
                   2275:                /*
                   2276:                 * We've now got a variable structure to store in. But first,
                   2277:                 * advance the string pointer.
                   2278:                 */
                   2279:                str += length;
1.5       millert  2280:
1.1       deraadt  2281:                /*
                   2282:                 * Copy all the characters from the variable value straight
                   2283:                 * into the new string.
                   2284:                 */
1.16      espie    2285:                Buf_AddChars(buf, strlen(val), val);
1.1       deraadt  2286:                if (doFree) {
                   2287:                    free ((Address)val);
                   2288:                }
                   2289:            }
                   2290:        }
                   2291:     }
1.5       millert  2292:
1.16      espie    2293:     Buf_AddChar(buf, '\0');
                   2294:     str = Buf_GetAll(buf, NULL);
                   2295:     Buf_Destroy(buf, FALSE);
1.1       deraadt  2296:     return (str);
                   2297: }
                   2298:
                   2299: /*-
                   2300:  *-----------------------------------------------------------------------
                   2301:  * Var_GetTail --
                   2302:  *     Return the tail from each of a list of words. Used to set the
                   2303:  *     System V local variables.
                   2304:  *
                   2305:  * Results:
                   2306:  *     The resulting string.
                   2307:  *
                   2308:  * Side Effects:
                   2309:  *     None.
                   2310:  *
                   2311:  *-----------------------------------------------------------------------
                   2312:  */
                   2313: char *
                   2314: Var_GetTail(file)
                   2315:     char       *file;      /* Filename to modify */
                   2316: {
                   2317:     return(VarModify(file, VarTail, (ClientData)0));
                   2318: }
                   2319:
                   2320: /*-
                   2321:  *-----------------------------------------------------------------------
                   2322:  * Var_GetHead --
                   2323:  *     Find the leading components of a (list of) filename(s).
                   2324:  *     XXX: VarHead does not replace foo by ., as (sun) System V make
                   2325:  *     does.
                   2326:  *
                   2327:  * Results:
                   2328:  *     The leading components.
                   2329:  *
                   2330:  * Side Effects:
                   2331:  *     None.
                   2332:  *
                   2333:  *-----------------------------------------------------------------------
                   2334:  */
                   2335: char *
                   2336: Var_GetHead(file)
                   2337:     char       *file;      /* Filename to manipulate */
                   2338: {
                   2339:     return(VarModify(file, VarHead, (ClientData)0));
                   2340: }
                   2341:
                   2342: /*-
                   2343:  *-----------------------------------------------------------------------
                   2344:  * Var_Init --
                   2345:  *     Initialize the module
                   2346:  *
                   2347:  * Results:
                   2348:  *     None
                   2349:  *
                   2350:  * Side Effects:
1.5       millert  2351:  *     The VAR_CMD and VAR_GLOBAL contexts are created
1.1       deraadt  2352:  *-----------------------------------------------------------------------
                   2353:  */
                   2354: void
                   2355: Var_Init ()
                   2356: {
1.17    ! espie    2357:     VAR_GLOBAL = Targ_NewGN("Global");
        !          2358:     VAR_CMD = Targ_NewGN("Command");
        !          2359:     VAR_ENV = Targ_NewGN("Environment");
1.1       deraadt  2360:     allVars = Lst_Init(FALSE);
                   2361:
                   2362: }
                   2363:
                   2364:
                   2365: void
                   2366: Var_End ()
                   2367: {
                   2368:     Lst_Destroy(allVars, VarDelete);
                   2369: }
1.5       millert  2370:
1.1       deraadt  2371:
                   2372: /****************** PRINT DEBUGGING INFO *****************/
                   2373: static int
                   2374: VarPrintVar (vp, dummy)
                   2375:     ClientData vp;
                   2376:     ClientData dummy;
                   2377: {
                   2378:     Var    *v = (Var *) vp;
1.16      espie    2379:     printf ("%-16s = %s\n", v->name, Buf_GetAll(v->val, NULL));
1.1       deraadt  2380:     return (dummy ? 0 : 0);
                   2381: }
                   2382:
                   2383: /*-
                   2384:  *-----------------------------------------------------------------------
                   2385:  * Var_Dump --
                   2386:  *     print all variables in a context
                   2387:  *-----------------------------------------------------------------------
                   2388:  */
                   2389: void
                   2390: Var_Dump (ctxt)
                   2391:     GNode          *ctxt;
                   2392: {
                   2393:     Lst_ForEach (ctxt->context, VarPrintVar, (ClientData) 0);
                   2394: }