[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.65

1.53      espie       1: /*     $OpenPackages$ */
1.65    ! espie       2: /*     $OpenBSD: var.c,v 1.64 2007/07/08 17:53:15 espie Exp $  */
1.6       millert     3: /*     $NetBSD: var.c,v 1.18 1997/03/18 19:24:46 christos Exp $        */
1.1       deraadt     4:
                      5: /*
1.62      espie       6:  * Copyright (c) 1999,2000,2007 Marc Espie.
1.17      espie       7:  *
                      8:  * Extensive code modifications for the OpenBSD project.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
                     20:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
                     21:  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
                     22:  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
                     23:  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
                     24:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
                     25:  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     26:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     27:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     28:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
                     29:  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     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.
1.57      millert    48:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    49:  *    may be used to endorse or promote products derived from this software
                     50:  *    without specific prior written permission.
                     51:  *
                     52:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     53:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     54:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     55:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     56:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     57:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     58:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     59:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     60:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     61:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     62:  * SUCH DAMAGE.
                     63:  */
                     64:
1.55      espie      65: #include <assert.h>
                     66: #include <stddef.h>
                     67: #include <stdio.h>
1.60      espie      68: #include <stdint.h>
1.55      espie      69: #include <stdlib.h>
                     70: #include <string.h>
                     71:
                     72: #include "config.h"
                     73: #include "defines.h"
                     74: #include "buf.h"
                     75: #include "stats.h"
                     76: #include "ohash.h"
1.62      espie      77: #include "pathnames.h"
1.55      espie      78: #include "varmodifiers.h"
                     79: #include "var.h"
                     80: #include "varname.h"
                     81: #include "error.h"
                     82: #include "str.h"
                     83: #include "var_int.h"
                     84: #include "memory.h"
                     85: #include "symtable.h"
                     86: #include "gnode.h"
                     87:
1.1       deraadt    88: /*
                     89:  * This is a harmless return value for Var_Parse that can be used by Var_Subst
                     90:  * to determine if there was an error in parsing -- easier than returning
                     91:  * a flag, as things outside this module don't give a hoot.
                     92:  */
1.53      espie      93: char   var_Error[] = "";
1.1       deraadt    94:
                     95: /*
                     96:  * Similar to var_Error, but returned when the 'err' flag for Var_Parse is
                     97:  * set false. Why not just use a constant? Well, gcc likes to condense
                     98:  * identical string instances...
                     99:  */
                    100: static char    varNoError[] = "";
1.64      espie     101: bool           errorIsOkay;
1.62      espie     102: static bool    checkEnvFirst;  /* true if environment should be searched for
                    103:                                 * variables before the global context */
                    104:
                    105: void
                    106: Var_setCheckEnvFirst(bool yes)
                    107: {
                    108:        checkEnvFirst = yes;
                    109: }
1.1       deraadt   110:
                    111: /*
1.53      espie     112:  * Variable values are obtained from four different contexts:
                    113:  *     1) the process environment. The process environment itself
                    114:  *        may not be changed, but these variables may be modified,
                    115:  *        unless make is invoked with -e, in which case those variables
                    116:  *        are unmodifiable and supersede the global context.
1.1       deraadt   117:  *     2) the global context. Variables set in the Makefile are located in
                    118:  *         the global context. It is the penultimate context searched when
                    119:  *         substituting.
                    120:  *     3) the command-line context. All variables set on the command line
                    121:  *        are placed in this context. They are UNALTERABLE once placed here.
                    122:  *     4) the local context. Each target has associated with it a context
                    123:  *        list. On this list are located the structures describing such
                    124:  *        local variables as $(@) and $(*)
                    125:  * The four contexts are searched in the reverse order from which they are
                    126:  * listed.
                    127:  */
1.53      espie     128:
                    129: static char *varnames[] = {
                    130:     TARGET,
                    131:     PREFIX,
                    132:     ARCHIVE,
                    133:     MEMBER,
                    134:     OODATE,
                    135:     ALLSRC,
                    136:     IMPSRC,
                    137:     FTARGET,
                    138:     DTARGET,
                    139:     FPREFIX,
                    140:     DPREFIX,
                    141:     FARCHIVE,
                    142:     DARCHIVE,
                    143:     FMEMBER,
                    144:     DMEMBER
                    145:     };
                    146:
1.65    ! espie     147: /* retrieve the hashed values  for well-known variables.  */
        !           148: #include    "varhashconsts.h"
        !           149:
        !           150: /* extended indices for System V stuff */
        !           151: #define FTARGET_INDEX  7
        !           152: #define DTARGET_INDEX  8
        !           153: #define FPREFIX_INDEX  9
        !           154: #define DPREFIX_INDEX  10
        !           155: #define FARCHIVE_INDEX 11
        !           156: #define DARCHIVE_INDEX 12
        !           157: #define FMEMBER_INDEX  13
        !           158: #define DMEMBER_INDEX  14
        !           159:
        !           160: #define EXTENDED2SIMPLE(i)     (((i)-LOCAL_SIZE)/2)
        !           161: #define IS_EXTENDED_F(i)       ((i)%2 == 1)
        !           162:
        !           163: static struct ohash global_variables;
1.1       deraadt   164:
1.37      espie     165: typedef struct Var_ {
1.53      espie     166:     BUFFER       val;          /* its value */
                    167:     unsigned int  flags;       /* miscellaneous status flags */
1.62      espie     168: #define VAR_IN_USE     1       /* Variable's value currently being used. */
                    169:                                /* Used to avoid recursion */
                    170: #define VAR_DUMMY      2       /* Placeholder: already looked up */
                    171: #define VAR_FROM_CMD   4       /* From the command line */
                    172: #define VAR_FROM_ENV   8       /* Read from environment */
                    173: #define VAR_SEEN_ENV   16      /* Already seen environment */
                    174: #define VAR_SHELL      32      /* magic, see posix */
                    175: #define POISONS (POISON_NORMAL | POISON_EMPTY | POISON_NOT_DEFINED)
1.53      espie     176:     char         name[1];      /* the variable's name */
1.1       deraadt   177: }  Var;
                    178:
1.53      espie     179:
                    180: static struct ohash_info var_info = {
                    181:        offsetof(Var, name),
1.38      espie     182:     NULL, hash_alloc, hash_free, element_alloc };
1.60      espie     183: static int quick_lookup(const char *, const char **, uint32_t *);
1.23      espie     184: #define VarValue(v)    Buf_Retrieve(&((v)->val))
1.62      espie     185: static Var *varfind(const char *, const char *, SymTable *, int, uint32_t);
                    186: static Var *find_global_var(const char *, const char *, uint32_t);
1.61      espie     187: static void VarDelete(Var *);
1.53      espie     188: static void VarPrintVar(Var *);
1.62      espie     189:
                    190: static Var *obtain_global_var(const char *, const char *, uint32_t);
                    191: static void fill_from_env(Var *);
1.53      espie     192: static Var *create_var(const char *, const char *);
1.62      espie     193: static void varq_set_append(int, const char *, GNode *, bool);
1.53      espie     194: static void var_init_string(Var *, const char *);
1.62      espie     195: static void var_set_string(Var *, const char *);
                    196: static void var_append_string(Var *, const char *);
                    197: static void var_set_append(const char *, const char *, const char *, int, bool);
                    198: static void set_magic_shell_variable(void);
                    199: static void poison_check(Var *);
1.53      espie     200: static const char *find_0(const char *);
                    201: static const char *find_rparen(const char *);
                    202: static const char *find_ket(const char *);
                    203: typedef const char * (*find_t)(const char *);
                    204: static find_t find_pos(int);
                    205:
1.38      espie     206: static int
1.60      espie     207: quick_lookup(const char *name, const char **enamePtr, uint32_t *pk)
1.38      espie     208: {
                    209:     size_t len;
                    210:
1.59      espie     211:     *pk = ohash_interval(name, enamePtr);
                    212:     len = *enamePtr - name;
1.38      espie     213:        /* substitute short version for long local name */
1.53      espie     214:     switch (*pk % MAGICSLOTS1) {           /* MAGICSLOTS should be the    */
                    215:     case K_LONGALLSRC % MAGICSLOTS1:       /* smallest constant yielding  */
                    216:                                            /* distinct case values        */
1.56      espie     217:        if (*pk == K_LONGALLSRC && len == strlen(LONGALLSRC) &&
                    218:            strncmp(name, LONGALLSRC, len) == 0)
1.38      espie     219:            return ALLSRC_INDEX;
                    220:        break;
1.53      espie     221:     case K_LONGARCHIVE % MAGICSLOTS1:
1.56      espie     222:        if (*pk == K_LONGARCHIVE && len == strlen(LONGARCHIVE) &&
                    223:            strncmp(name, LONGARCHIVE, len) == 0)
1.38      espie     224:            return ARCHIVE_INDEX;
                    225:        break;
1.53      espie     226:     case K_LONGIMPSRC % MAGICSLOTS1:
1.56      espie     227:        if (*pk == K_LONGIMPSRC && len == strlen(LONGIMPSRC) &&
                    228:            strncmp(name, LONGIMPSRC, len) == 0)
1.38      espie     229:            return IMPSRC_INDEX;
                    230:        break;
1.53      espie     231:     case K_LONGMEMBER % MAGICSLOTS1:
1.56      espie     232:        if (*pk == K_LONGMEMBER && len == strlen(LONGMEMBER) &&
                    233:            strncmp(name, LONGMEMBER, len) == 0)
1.38      espie     234:            return MEMBER_INDEX;
                    235:        break;
1.53      espie     236:     case K_LONGOODATE % MAGICSLOTS1:
1.56      espie     237:        if (*pk == K_LONGOODATE && len == strlen(LONGOODATE) &&
                    238:            strncmp(name, LONGOODATE, len) == 0)
1.38      espie     239:            return OODATE_INDEX;
                    240:        break;
1.53      espie     241:     case K_LONGPREFIX % MAGICSLOTS1:
1.56      espie     242:        if (*pk == K_LONGPREFIX && len == strlen(LONGPREFIX) &&
                    243:            strncmp(name, LONGPREFIX, len) == 0)
1.38      espie     244:            return PREFIX_INDEX;
                    245:        break;
1.53      espie     246:     case K_LONGTARGET % MAGICSLOTS1:
1.56      espie     247:        if (*pk == K_LONGTARGET && len == strlen(LONGTARGET) &&
                    248:            strncmp(name, LONGTARGET, len) == 0)
1.38      espie     249:            return TARGET_INDEX;
                    250:        break;
1.53      espie     251:     case K_TARGET % MAGICSLOTS1:
1.38      espie     252:        if (name[0] == TARGET[0] && len == 1)
                    253:            return TARGET_INDEX;
                    254:        break;
1.53      espie     255:     case K_OODATE % MAGICSLOTS1:
1.38      espie     256:        if (name[0] == OODATE[0] && len == 1)
                    257:            return OODATE_INDEX;
                    258:        break;
1.53      espie     259:     case K_ALLSRC % MAGICSLOTS1:
1.38      espie     260:        if (name[0] == ALLSRC[0] && len == 1)
                    261:            return ALLSRC_INDEX;
                    262:        break;
1.53      espie     263:     case K_IMPSRC % MAGICSLOTS1:
1.38      espie     264:        if (name[0] == IMPSRC[0] && len == 1)
                    265:            return IMPSRC_INDEX;
                    266:        break;
1.53      espie     267:     case K_PREFIX % MAGICSLOTS1:
1.38      espie     268:        if (name[0] == PREFIX[0] && len == 1)
                    269:            return PREFIX_INDEX;
                    270:        break;
1.53      espie     271:     case K_ARCHIVE % MAGICSLOTS1:
1.38      espie     272:        if (name[0] == ARCHIVE[0] && len == 1)
                    273:            return ARCHIVE_INDEX;
                    274:        break;
1.53      espie     275:     case K_MEMBER % MAGICSLOTS1:
1.38      espie     276:        if (name[0] == MEMBER[0] && len == 1)
                    277:            return MEMBER_INDEX;
                    278:        break;
1.53      espie     279:     case K_FTARGET % MAGICSLOTS1:
                    280:        if (name[0] == FTARGET[0] && name[1] == FTARGET[1] && len == 2)
                    281:            return FTARGET_INDEX;
                    282:        break;
                    283:     case K_DTARGET % MAGICSLOTS1:
                    284:        if (name[0] == DTARGET[0] && name[1] == DTARGET[1] && len == 2)
                    285:            return DTARGET_INDEX;
                    286:        break;
                    287:     case K_FPREFIX % MAGICSLOTS1:
                    288:        if (name[0] == FPREFIX[0] && name[1] == FPREFIX[1] && len == 2)
                    289:            return FPREFIX_INDEX;
                    290:        break;
                    291:     case K_DPREFIX % MAGICSLOTS1:
                    292:        if (name[0] == DPREFIX[0] && name[1] == DPREFIX[1] && len == 2)
                    293:            return DPREFIX_INDEX;
                    294:        break;
                    295:     case K_FARCHIVE % MAGICSLOTS1:
                    296:        if (name[0] == FARCHIVE[0] && name[1] == FARCHIVE[1] && len == 2)
                    297:            return FARCHIVE_INDEX;
                    298:        break;
                    299:     case K_DARCHIVE % MAGICSLOTS1:
                    300:        if (name[0] == DARCHIVE[0] && name[1] == DARCHIVE[1] && len == 2)
                    301:            return DARCHIVE_INDEX;
                    302:        break;
                    303:     case K_FMEMBER % MAGICSLOTS1:
                    304:        if (name[0] == FMEMBER[0] && name[1] == FMEMBER[1] && len == 2)
                    305:            return FMEMBER_INDEX;
                    306:        break;
                    307:     case K_DMEMBER % MAGICSLOTS1:
                    308:        if (name[0] == DMEMBER[0] && name[1] == DMEMBER[1] && len == 2)
                    309:            return DMEMBER_INDEX;
                    310:        break;
1.38      espie     311:     default:
                    312:        break;
                    313:     }
                    314:     return -1;
                    315: }
1.37      espie     316:
1.65    ! espie     317: static Var *
        !           318: create_var(const char *name, const char *ename)
        !           319: {
        !           320:     return ohash_create_entry(&var_info, name, &ename);
        !           321: }
        !           322:
        !           323: /* Set the initial value a var should have */
        !           324: static void
        !           325: var_init_string(Var *v, const char *val)
        !           326: {
        !           327:     size_t len;
        !           328:
        !           329:     len = strlen(val);
        !           330:     Buf_Init(&(v->val), len+1);
        !           331:     Buf_AddChars(&(v->val), len, val);
        !           332: }
        !           333:
        !           334: static void
        !           335: var_set_string(Var *v, const char *val)
        !           336: {
        !           337:     if ((v->flags & VAR_DUMMY) == 0) {
        !           338:        Buf_Reset(&(v->val));
        !           339:        Buf_AddString(&(v->val), val);
        !           340:     } else {
        !           341:        var_init_string(v, val);
        !           342:        v->flags &= ~VAR_DUMMY;
        !           343:     }
        !           344: }
        !           345:
        !           346: static void
        !           347: var_append_string(Var *v, const char *val)
        !           348: {
        !           349:     if ((v->flags & VAR_DUMMY) == 0) {
        !           350:        Buf_AddSpace(&(v->val));
        !           351:        Buf_AddString(&(v->val), val);
        !           352:     } else {
        !           353:        var_init_string(v, val);
        !           354:        v->flags &= ~VAR_DUMMY;
        !           355:     }
        !           356: }
        !           357:
        !           358: /*-
        !           359:  *-----------------------------------------------------------------------
        !           360:  * VarDelete  --
        !           361:  *     Delete a variable and all the space associated with it.
        !           362:  *-----------------------------------------------------------------------
        !           363:  */
        !           364: static void
        !           365: VarDelete(Var *v)
        !           366: {
        !           367:     if ((v->flags & VAR_DUMMY) == 0)
        !           368:        Buf_Destroy(&(v->val));
        !           369:     free(v);
        !           370: }
        !           371:
        !           372:
        !           373:
        !           374: void
        !           375: SymTable_Init(SymTable *ctxt)
        !           376: {
        !           377:     static SymTable sym_template;
        !           378:     memcpy(ctxt, &sym_template, sizeof(*ctxt));
        !           379: }
        !           380:
        !           381: #ifdef CLEANUP
        !           382: void
        !           383: SymTable_Destroy(SymTable *ctxt)
        !           384: {
        !           385:     int i;
        !           386:
        !           387:     for (i = 0; i < LOCAL_SIZE; i++)
        !           388:        if (ctxt->locals[i] != NULL)
        !           389:            VarDelete(ctxt->locals[i]);
        !           390: }
        !           391: #endif
        !           392:
1.62      espie     393: static void
                    394: varq_set_append(int idx, const char *val, GNode *gn, bool append)
1.37      espie     395: {
                    396:     Var *v = gn->context.locals[idx];
                    397:
                    398:     if (v == NULL) {
1.62      espie     399:        v = create_var(varnames[idx], NULL);
                    400: #ifdef STATS_VAR_LOOKUP
                    401:        STAT_VAR_CREATION++;
                    402: #endif
                    403:        if (val != NULL)
                    404:            var_init_string(v, val);
                    405:        else
                    406:            Buf_Init(&(v->val), 1);
1.37      espie     407:        v->flags = 0;
                    408:        gn->context.locals[idx] = v;
                    409:     } else {
1.62      espie     410:        if (append)
                    411:                Buf_AddSpace(&(v->val));
                    412:        else
                    413:                Buf_Reset(&(v->val));
1.37      espie     414:        Buf_AddString(&(v->val), val);
                    415:     }
                    416:     if (DEBUG(VAR))
1.62      espie     417:        printf("%s:%s = %s\n", gn->name, varnames[idx], VarValue(v));
                    418: }
                    419:
                    420: void
                    421: Varq_Set(int idx, const char *val, GNode *gn)
                    422: {
                    423:     varq_set_append(idx, val, gn, false);
1.37      espie     424: }
                    425:
1.53      espie     426: void
1.59      espie     427: Varq_Append(int idx, const char *val, GNode *gn)
1.37      espie     428: {
1.62      espie     429:     varq_set_append(idx, val, gn, true);
1.37      espie     430: }
                    431:
                    432: char *
1.59      espie     433: Varq_Value(int idx, GNode *gn)
1.37      espie     434: {
                    435:     Var *v = gn->context.locals[idx];
                    436:
1.53      espie     437:     if (v == NULL)
                    438:        return NULL;
1.37      espie     439:     else
1.53      espie     440:        return VarValue(v);
                    441: }
                    442:
                    443: static Var *
1.65    ! espie     444: obtain_global_var(const char *name, const char *ename, uint32_t k)
1.37      espie     445: {
1.65    ! espie     446:        unsigned int slot;
        !           447:        Var *v;
1.1       deraadt   448:
1.65    ! espie     449:        slot = ohash_lookup_interval(&global_variables, name, ename, k);
        !           450:        v = ohash_find(&global_variables, slot);
        !           451:        if (v == NULL) {
        !           452:                v = create_var(name, ename);
        !           453:                v->flags = VAR_DUMMY;
        !           454:                ohash_insert(&global_variables, slot, v);
        !           455:        }
        !           456:        return v;
1.35      espie     457: }
1.53      espie     458:
1.62      espie     459: static void
                    460: fill_from_env(Var *v)
1.37      espie     461: {
1.53      espie     462:     char       *env;
1.37      espie     463:
1.53      espie     464:     env = getenv(v->name);
                    465:     if (env == NULL)
1.62      espie     466:        v->flags |= VAR_SEEN_ENV;
1.53      espie     467:     else {
1.62      espie     468:        var_set_string(v, env);
                    469:        v->flags |= VAR_FROM_ENV | VAR_SEEN_ENV;
1.53      espie     470:     }
1.37      espie     471:
1.53      espie     472: #ifdef STATS_VAR_LOOKUP
                    473:     STAT_VAR_FROM_ENV++;
                    474: #endif
1.62      espie     475: }
1.37      espie     476:
1.62      espie     477: static Var *
1.65    ! espie     478: find_global_var(const char *name, const char *ename, uint32_t k)
        !           479: {
        !           480:     Var                *v;
        !           481:
        !           482:     v = obtain_global_var(name, ename, k);
        !           483:
        !           484:     if ((v->flags & VAR_SEEN_ENV) == 0 &&
        !           485:        (checkEnvFirst  && (v->flags & VAR_FROM_CMD) == 0 ||
        !           486:            (v->flags & VAR_DUMMY) != 0))
        !           487:                fill_from_env(v);
        !           488:
        !           489:     return v;
        !           490: }
        !           491:
        !           492:
        !           493: void
        !           494: Var_MarkPoisoned(const char *name, const char *ename, unsigned int type)
1.62      espie     495: {
1.65    ! espie     496:        Var   *v;
        !           497:        uint32_t        k;
        !           498:        int             idx;
        !           499:        idx = quick_lookup(name, &ename, &k);
        !           500:
        !           501:        if (idx != -1) {
        !           502:                Parse_Error(PARSE_FATAL,
        !           503:                    "Trying to poison dynamic variable $%s",
        !           504:                    varnames[idx]);
        !           505:                return;
        !           506:        }
1.62      espie     507:
1.65    ! espie     508:        v = find_global_var(name, ename, k);
        !           509:        v->flags |= type;
        !           510:        if (v->flags & POISON_NORMAL) {
        !           511:                if (v->flags & VAR_DUMMY)
        !           512:                        return;
        !           513:                if (v->flags & VAR_FROM_ENV)
        !           514:                        return;
        !           515:                Parse_Error(PARSE_FATAL,
        !           516:                    "Poisoned variable %s is already set\n", v->name);
1.62      espie     517:        }
1.37      espie     518: }
                    519:
1.62      espie     520: static void
                    521: poison_check(Var *v)
1.1       deraadt   522: {
1.62      espie     523:        if (v->flags & POISON_NORMAL) {
                    524:                Parse_Error(PARSE_FATAL,
                    525:                    "Poisoned variable %s has been referenced\n", v->name);
                    526:                return;
                    527:        }
                    528:        if (v->flags & VAR_DUMMY) {
                    529:                Parse_Error(PARSE_FATAL,
                    530:                    "Poisoned variable %s is not defined\n", v->name);
                    531:                return;
                    532:        }
                    533:        if (v->flags & POISON_EMPTY)
                    534:                if (strcmp(VarValue(v), "") == 0)
                    535:                        Parse_Error(PARSE_FATAL,
                    536:                            "Poisoned variable %s is empty\n", v->name);
1.37      espie     537: }
                    538:
1.1       deraadt   539: void
1.59      espie     540: Var_Delete(const char *name)
1.1       deraadt   541: {
1.62      espie     542:        Var     *v;
                    543:        uint32_t        k;
                    544:        unsigned int slot;
                    545:        const char      *ename = NULL;
                    546:        int             idx;
                    547:
                    548:
                    549:        if (DEBUG(VAR))
                    550:                printf("delete %s\n", name);
                    551:
                    552:        idx = quick_lookup(name, &ename, &k);
                    553:        if (idx != -1)
                    554:                Parse_Error(PARSE_FATAL, "Trying to delete dynamic variable");
                    555:        slot = ohash_lookup_interval(&global_variables, name, ename, k);
                    556:        v = ohash_find(&global_variables, slot);
                    557:
                    558:        if (v == NULL)
                    559:                return;
                    560:        if (checkEnvFirst && (v->flags & VAR_FROM_ENV))
                    561:                return;
1.1       deraadt   562:
1.62      espie     563:        if (v->flags & VAR_FROM_CMD)
                    564:                return;
1.37      espie     565:
1.62      espie     566:        ohash_remove(&global_variables, slot);
1.30      espie     567:        VarDelete(v);
1.1       deraadt   568: }
                    569:
1.62      espie     570: static void
                    571: var_set_append(const char *name, const char *ename, const char *val, int ctxt,
                    572:     bool append)
1.1       deraadt   573: {
1.62      espie     574:        Var   *v;
                    575:        uint32_t        k;
                    576:        int             idx;
1.53      espie     577:
1.62      espie     578:        idx = quick_lookup(name, &ename, &k);
                    579:        if (idx != -1) {
                    580:                Parse_Error(PARSE_FATAL, "Trying to %s dynamic variable $%s",
                    581:                    append ? "append to" : "set", varnames[idx]);
                    582:                return;
                    583:        }
1.53      espie     584:
1.62      espie     585:        v = find_global_var(name, ename, k);
                    586:        if (v->flags & POISON_NORMAL)
                    587:                Parse_Error(PARSE_FATAL, "Trying to %s poisoned variable %s\n",
                    588:                    append ? "append to" : "set", v->name);
                    589:        /* so can we write to it ? */
                    590:        if (ctxt == VAR_CMD) {  /* always for command line */
                    591:                (append ? var_append_string : var_set_string)(v, val);
                    592:                v->flags |= VAR_FROM_CMD;
                    593:                if ((v->flags & VAR_SHELL) == 0) {
                    594:                        /* Any variables given on the command line are
                    595:                         * automatically exported to the environment,
                    596:                         * except for SHELL (as per POSIX standard).
                    597:                         */
                    598:                        esetenv(v->name, val);
                    599:                }
                    600:                if (DEBUG(VAR))
                    601:                        printf("command:%s = %s\n", v->name, VarValue(v));
                    602:        } else if ((v->flags & VAR_FROM_CMD) == 0 &&
                    603:             (!checkEnvFirst || (v->flags & VAR_FROM_ENV) == 0)) {
                    604:                (append ? var_append_string : var_set_string)(v, val);
                    605:                if (DEBUG(VAR))
                    606:                        printf("global:%s = %s\n", v->name, VarValue(v));
                    607:        } else if (DEBUG(VAR))
                    608:                        printf("overriden:%s = %s\n", v->name, VarValue(v));
1.1       deraadt   609: }
                    610:
                    611: void
1.62      espie     612: Var_Seti(const char *name, const char *ename, const char *val, int ctxt)
1.1       deraadt   613: {
1.62      espie     614:        var_set_append(name, ename, val, ctxt, false);
                    615: }
1.53      espie     616:
1.62      espie     617: void
                    618: Var_Appendi(const char *name, const char *ename, const char *val, int ctxt)
                    619: {
                    620:        var_set_append(name, ename, val, ctxt, true);
                    621: }
1.53      espie     622:
                    623: char *
1.59      espie     624: Var_Valuei(const char *name, const char *ename)
1.53      espie     625: {
1.62      espie     626:        Var        *v;
                    627:        uint32_t                k;
                    628:        int             idx;
                    629:
                    630:        idx = quick_lookup(name, &ename, &k);
                    631:        if (idx == -1) {
                    632:                v = find_global_var(name, ename, k);
                    633:                if (v->flags & POISONS)
                    634:                    poison_check(v);
                    635:                if ((v->flags & VAR_DUMMY) == 0)
                    636:                        return VarValue(v);
                    637:        }
1.53      espie     638:
                    639:        return NULL;
                    640: }
                    641:
1.62      espie     642: bool
                    643: Var_Definedi(const char *name, const char *ename)
                    644: {
                    645:        Var             *v;
                    646:        uint32_t        k;
                    647:        int             idx;
                    648:
                    649:        idx = quick_lookup(name, &ename, &k);
                    650:        if (idx == -1) {
                    651:                v = find_global_var(name, ename, k);
                    652:                if (v->flags & POISON_NORMAL)
                    653:                    poison_check(v);
                    654:                if ((v->flags & VAR_DUMMY) == 0)
                    655:                        return true;
                    656:        }
                    657:
                    658:        return false;
1.65    ! espie     659: }
        !           660:
        !           661: static Var *
        !           662: varfind(const char *name, const char *ename, SymTable *ctxt,
        !           663:     int idx, uint32_t k)
        !           664: {
        !           665:     /* Handle local variables first */
        !           666:     if (idx != -1) {
        !           667:        if (ctxt != NULL) {
        !           668:                if (idx < LOCAL_SIZE)
        !           669:                    return ctxt->locals[idx];
        !           670:                else
        !           671:                    return ctxt->locals[EXTENDED2SIMPLE(idx)];
        !           672:        } else
        !           673:                return NULL;
        !           674:     } else {
        !           675:        return find_global_var(name, ename, k);
        !           676:     }
1.62      espie     677: }
                    678:
1.53      espie     679: static const char *
1.59      espie     680: find_0(const char *p)
1.1       deraadt   681: {
1.53      espie     682:        while (*p != '$' && *p != '\0' && *p != ':')
                    683:                p++;
                    684:        return p;
                    685: }
                    686:
                    687: static const char *
1.59      espie     688: find_rparen(const char *p)
1.53      espie     689: {
                    690:        while (*p != '$' && *p != '\0' && *p != ')' && *p != ':')
                    691:                p++;
                    692:        return p;
                    693: }
1.1       deraadt   694:
1.53      espie     695: static const char *
1.59      espie     696: find_ket(const char *p)
1.53      espie     697: {
                    698:        while (*p != '$' && *p != '\0' && *p != '}' && *p != ':')
                    699:                p++;
                    700:        return p;
                    701: }
1.1       deraadt   702:
1.53      espie     703: static find_t
1.59      espie     704: find_pos(int c)
1.53      espie     705: {
                    706:        switch(c) {
                    707:        case '\0':
                    708:                return find_0;
                    709:        case ')':
                    710:                return find_rparen;
                    711:        case '}':
                    712:                return find_ket;
                    713:        default:
                    714:                return 0;
                    715:        }
1.1       deraadt   716: }
                    717:
1.53      espie     718: size_t
1.59      espie     719: Var_ParseSkip(const char *str, SymTable *ctxt, bool *result)
1.1       deraadt   720: {
1.53      espie     721:     const char *tstr;          /* Pointer into str */
                    722:     Var        *v;             /* Variable in invocation */
                    723:     char       endc;           /* Ending character when variable in parens
                    724:                                 * or braces */
                    725:     const char *start;
                    726:     size_t     length;
                    727:     struct Name name;
1.1       deraadt   728:
1.53      espie     729:     v = NULL;
                    730:     start = str;
                    731:     str++;
                    732:
                    733:     if (*str != '(' && *str != '{') {
1.55      espie     734:        name.tofree = false;
1.53      espie     735:        tstr = str + 1;
                    736:        length = 2;
                    737:        endc = '\0';
                    738:     } else {
                    739:        endc = *str == '(' ? ')' : '}';
                    740:        str++;
                    741:
                    742:        /* Find eventual modifiers in the variable */
1.55      espie     743:        tstr = VarName_Get(str, &name, ctxt, false, find_pos(endc));
                    744:        VarName_Free(&name);
1.54      espie     745:        length = tstr - start;
1.55      espie     746:        if (*tstr != 0)
                    747:            length++;
1.53      espie     748:     }
                    749:
                    750:     if (result != NULL)
1.55      espie     751:        *result = true;
1.53      espie     752:     if (*tstr == ':' && endc != '\0')
1.55      espie     753:         if (VarModifiers_Apply(NULL, NULL, ctxt, true, NULL, tstr, endc,
1.53      espie     754:            &length) == var_Error)
1.58      fgsch     755:                if (result != NULL)
                    756:                    *result = false;
1.53      espie     757:     return length;
1.1       deraadt   758: }
                    759:
1.55      espie     760: /* As of now, Var_ParseBuffer is just a wrapper around Var_Parse. For
                    761:  * speed, it may be better to revisit the implementation to do things
                    762:  * directly. */
                    763: bool
1.59      espie     764: Var_ParseBuffer(Buffer buf, const char *str, SymTable *ctxt, bool err,
                    765:     size_t *lengthPtr)
1.53      espie     766: {
                    767:     char       *result;
1.55      espie     768:     bool       freeIt;
1.45      espie     769:
1.53      espie     770:     result = Var_Parse(str, ctxt, err, lengthPtr, &freeIt);
                    771:     if (result == var_Error)
1.55      espie     772:        return false;
1.53      espie     773:
                    774:     Buf_AddString(buf, result);
                    775:     if (freeIt)
                    776:        free(result);
1.55      espie     777:     return true;
1.45      espie     778: }
                    779:
1.1       deraadt   780: char *
1.59      espie     781: Var_Parse(const char *str,     /* The string to parse */
                    782:     SymTable *ctxt,            /* The context for the variable */
                    783:     bool err,                  /* true if undefined variables are an error */
                    784:     size_t *lengthPtr,                 /* OUT: The length of the specification */
                    785:     bool *freePtr)             /* OUT: true if caller should free result */
1.44      espie     786: {
1.53      espie     787:     const char *tstr;          /* Pointer into str */
                    788:     Var        *v;             /* Variable in invocation */
                    789:     char       endc;           /* Ending character when variable in parens
1.1       deraadt   790:                                 * or braces */
1.53      espie     791:     struct Name        name;
                    792:     const char *start;
1.44      espie     793:     char       *val;           /* Variable value  */
1.60      espie     794:     uint32_t   k;
1.53      espie     795:     int        idx;
1.5       millert   796:
1.55      espie     797:     *freePtr = false;
1.44      espie     798:     start = str++;
1.5       millert   799:
1.44      espie     800:     val = NULL;
1.45      espie     801:     v = NULL;
1.53      espie     802:     idx = -1;
1.44      espie     803:
                    804:     if (*str != '(' && *str != '{') {
1.53      espie     805:        name.s = str;
                    806:        name.e = str+1;
1.55      espie     807:        name.tofree = false;
1.53      espie     808:        tstr = str + 1;
1.44      espie     809:        *lengthPtr = 2;
                    810:        endc = '\0';
1.1       deraadt   811:     } else {
1.44      espie     812:        endc = *str == '(' ? ')' : '}';
1.53      espie     813:        str++;
1.5       millert   814:
1.44      espie     815:        /* Find eventual modifiers in the variable */
1.55      espie     816:        tstr = VarName_Get(str, &name, ctxt, false, find_pos(endc));
1.54      espie     817:        *lengthPtr = tstr - start;
                    818:        if (*tstr != '\0')
                    819:                (*lengthPtr)++;
1.44      espie     820:     }
1.5       millert   821:
1.53      espie     822:     idx = quick_lookup(name.s, &name.e, &k);
1.62      espie     823:     v = varfind(name.s, name.e, ctxt, idx, k);
1.63      espie     824:     if (v != NULL && (v->flags & POISONS) != 0)
1.62      espie     825:        poison_check(v);
1.53      espie     826:     if (v != NULL && (v->flags & VAR_DUMMY) == 0) {
1.44      espie     827:        if (v->flags & VAR_IN_USE)
                    828:            Fatal("Variable %s is recursive.", v->name);
                    829:            /*NOTREACHED*/
                    830:        else
                    831:            v->flags |= VAR_IN_USE;
1.53      espie     832:
1.44      espie     833:        /* Before doing any modification, we have to make sure the value
                    834:         * has been fully expanded. If it looks like recursion might be
                    835:         * necessary (there's a dollar sign somewhere in the variable's value)
                    836:         * we just call Var_Subst to do any other substitutions that are
                    837:         * necessary. Note that the value returned by Var_Subst will have
                    838:         * been dynamically-allocated, so it will need freeing when we
                    839:         * return.  */
                    840:        val = VarValue(v);
1.53      espie     841:        if (idx == -1) {
                    842:            if (strchr(val, '$') != NULL) {
                    843:                val = Var_Subst(val, ctxt, err);
1.55      espie     844:                *freePtr = true;
1.53      espie     845:            }
                    846:        } else if (idx >= LOCAL_SIZE) {
                    847:            if (IS_EXTENDED_F(idx))
                    848:                val = Var_GetTail(val);
                    849:            else
                    850:                val = Var_GetHead(val);
1.55      espie     851:            *freePtr = true;
1.44      espie     852:        }
                    853:        v->flags &= ~VAR_IN_USE;
1.1       deraadt   854:     }
1.53      espie     855:     if (*tstr == ':' && endc != '\0')
                    856:        val = VarModifiers_Apply(val, &name, ctxt, err, freePtr, tstr, endc,
1.42      espie     857:            lengthPtr);
1.44      espie     858:     if (val == NULL) {
1.53      espie     859:        val = err ? var_Error : varNoError;
                    860:        /* Dynamic source */
                    861:        if (idx != -1) {
                    862:            /* can't be expanded for now: copy the var spec instead. */
1.62      espie     863:            if (ctxt == NULL) {
1.55      espie     864:                *freePtr = true;
                    865:                val = Str_dupi(start, start+ *lengthPtr);
1.53      espie     866:            } else {
                    867:            /* somehow, this should have been expanded already. */
                    868:                GNode *n;
                    869:
                    870:                n = (GNode *)(((char *)ctxt) - offsetof(GNode, context));
                    871:                if (idx >= LOCAL_SIZE)
                    872:                        idx = EXTENDED2SIMPLE(idx);
                    873:                switch(idx) {
                    874:                case IMPSRC_INDEX:
                    875:                    Fatal("Using $< in a non-suffix rule context is a GNUmake idiom (line %lu of %s)",
                    876:                        n->lineno, n->fname);
                    877:                default:
                    878:                    Error("Using undefined dynamic variable $%s (line %lu of %s)",
                    879:                        varnames[idx], n->lineno, n->fname);
                    880:                    break;
                    881:                }
                    882:            }
                    883:        }
1.42      espie     884:     }
1.55      espie     885:     VarName_Free(&name);
1.44      espie     886:     return val;
1.42      espie     887: }
                    888:
1.1       deraadt   889: char *
1.59      espie     890: Var_Subst(const char *str,     /* the string in which to substitute */
                    891:     SymTable *ctxt,            /* the context wherein to find variables */
                    892:     bool undefErr)             /* true if undefineds are an error */
                    893: {
                    894:     BUFFER       buf;          /* Buffer for forming things */
                    895:     static bool errorReported;  /* Set true if an error has already
                    896:                                 * been reported to prevent a plethora
                    897:                                 * of messages when recursing */
1.1       deraadt   898:
1.23      espie     899:     Buf_Init(&buf, MAKE_BSIZE);
1.55      espie     900:     errorReported = false;
1.1       deraadt   901:
1.24      espie     902:     for (;;) {
1.59      espie     903:        char            *val;   /* Value to substitute for a variable */
                    904:        size_t          length; /* Length of the variable invocation */
1.55      espie     905:        bool    doFree;         /* Set true if val should be freed */
1.24      espie     906:        const char *cp;
                    907:
                    908:        /* copy uninteresting stuff */
                    909:        for (cp = str; *str != '\0' && *str != '$'; str++)
                    910:            ;
1.55      espie     911:        Buf_Addi(&buf, cp, str);
1.24      espie     912:        if (*str == '\0')
                    913:            break;
                    914:        if (str[1] == '$') {
                    915:            /* A dollar sign may be escaped with another dollar sign.  */
                    916:            Buf_AddChar(&buf, '$');
                    917:            str += 2;
                    918:            continue;
                    919:        }
                    920:        val = Var_Parse(str, ctxt, undefErr, &length, &doFree);
                    921:        /* When we come down here, val should either point to the
                    922:         * value of this variable, suitably modified, or be NULL.
                    923:         * Length should be the total length of the potential
                    924:         * variable invocation (from $ to end character...) */
                    925:        if (val == var_Error || val == varNoError) {
                    926:            /* If performing old-time variable substitution, skip over
                    927:             * the variable and continue with the substitution. Otherwise,
                    928:             * store the dollar sign and advance str so we continue with
                    929:             * the string...  */
1.64      espie     930:            if (errorIsOkay)
1.24      espie     931:                str += length;
1.53      espie     932:            else if (undefErr) {
1.24      espie     933:                /* If variable is undefined, complain and skip the
                    934:                 * variable. The complaint will stop us from doing anything
                    935:                 * when the file is parsed.  */
1.53      espie     936:                if (!errorReported)
1.24      espie     937:                    Parse_Error(PARSE_FATAL,
                    938:                                 "Undefined variable \"%.*s\"",length,str);
                    939:                str += length;
1.55      espie     940:                errorReported = true;
1.24      espie     941:            } else {
                    942:                Buf_AddChar(&buf, *str);
1.53      espie     943:                str++;
1.24      espie     944:            }
                    945:        } else {
                    946:            /* We've now got a variable structure to store in. But first,
                    947:             * advance the string pointer.  */
                    948:            str += length;
                    949:
                    950:            /* Copy all the characters from the variable value straight
                    951:             * into the new string.  */
                    952:            Buf_AddString(&buf, val);
                    953:            if (doFree)
                    954:                free(val);
                    955:        }
                    956:     }
                    957:     return  Buf_Retrieve(&buf);
                    958: }
1.1       deraadt   959:
1.53      espie     960: void
1.59      espie     961: Var_SubstVar(Buffer buf,       /* To store result */
                    962:     const char *str,           /* The string in which to substitute */
                    963:     const char *var,           /* Named variable */
                    964:     const char *val)           /* Its value */
1.24      espie     965: {
1.63      espie     966:     /* we save the old value and affect the new value temporarily */
                    967:     Var old;
                    968:     const char *ename = NULL;
                    969:     uint32_t k;
                    970:     Var *me;
                    971:     k = ohash_interval(var, &ename);
                    972:     me = obtain_global_var(var, ename, k);
                    973:     old = *me;
                    974:     var_init_string(me, val);
                    975:     me->flags = VAR_SEEN_ENV;
1.24      espie     976:
1.53      espie     977:     assert(*var != '\0');
                    978:
1.24      espie     979:     for (;;) {
1.50      espie     980:        const char *start;
                    981:        /* Copy uninteresting stuff */
                    982:        for (start = str; *str != '\0' && *str != '$'; str++)
1.24      espie     983:            ;
1.55      espie     984:        Buf_Addi(buf, start, str);
1.50      espie     985:
                    986:        start = str;
                    987:        if (*str++ == '\0')
1.24      espie     988:            break;
1.50      espie     989:        str++;
                    990:        /* and escaped dollars */
                    991:        if (start[1] == '$') {
1.55      espie     992:            Buf_Addi(buf, start, start+2);
1.24      espie     993:            continue;
                    994:        }
1.50      espie     995:        /* Simple variable, if it's not us, copy.  */
                    996:        if (start[1] != '(' && start[1] != '{') {
                    997:            if (start[1] != *var || var[1] != '\0') {
1.53      espie     998:                Buf_AddChars(buf, 2, start);
1.1       deraadt   999:                continue;
1.24      espie    1000:            }
1.1       deraadt  1001:        } else {
1.50      espie    1002:            const char *p;
1.24      espie    1003:            char endc;
1.1       deraadt  1004:
1.50      espie    1005:            if (start[1] == '(')
1.24      espie    1006:                endc = ')';
1.53      espie    1007:            else
1.24      espie    1008:                endc = '}';
                   1009:
                   1010:            /* Find the end of the variable specification.  */
1.50      espie    1011:            p = str;
1.24      espie    1012:            while (*p != '\0' && *p != ':' && *p != endc && *p != '$')
                   1013:                p++;
1.53      espie    1014:            /* A variable inside the variable.  We don't know how to
                   1015:             * expand the external variable at this point, so we try
                   1016:             * again with the nested variable.  */
1.24      espie    1017:            if (*p == '$') {
1.55      espie    1018:                Buf_Addi(buf, start, p);
1.24      espie    1019:                str = p;
                   1020:                continue;
1.1       deraadt  1021:            }
1.5       millert  1022:
1.50      espie    1023:            if (strncmp(var, str, p - str) != 0 ||
                   1024:                var[p - str] != '\0') {
1.53      espie    1025:                /* Not the variable we want to expand.  */
1.55      espie    1026:                Buf_Addi(buf, start, p);
1.24      espie    1027:                str = p;
                   1028:                continue;
1.53      espie    1029:            }
1.50      espie    1030:            if (*p == ':') {
1.53      espie    1031:                size_t  length;         /* Length of the variable invocation */
1.55      espie    1032:                bool doFree;    /* Set true if val should be freed */
1.50      espie    1033:                char    *newval;        /* Value substituted for a variable */
1.53      espie    1034:                struct Name name;
1.50      espie    1035:
1.53      espie    1036:                length = p - str + 1;
1.55      espie    1037:                doFree = false;
1.53      espie    1038:                name.s = var;
                   1039:                name.e = var + (p-str);
1.50      espie    1040:
1.55      espie    1041:                /* val won't be freed since doFree == false, but
1.50      espie    1042:                 * VarModifiers_Apply doesn't know that, hence the cast. */
1.55      espie    1043:                newval = VarModifiers_Apply((char *)val, &name, NULL, false,
1.53      espie    1044:                    &doFree, p, endc, &length);
                   1045:                Buf_AddString(buf, newval);
1.50      espie    1046:                if (doFree)
                   1047:                    free(newval);
                   1048:                str += length;
                   1049:                continue;
                   1050:            } else
1.53      espie    1051:                str = p+1;
1.1       deraadt  1052:        }
1.53      espie    1053:        Buf_AddString(buf, val);
1.1       deraadt  1054:     }
1.63      espie    1055:     *me = old;
1.1       deraadt  1056: }
                   1057:
1.62      espie    1058: static void
                   1059: set_magic_shell_variable()
                   1060: {
                   1061:     const char *name = "SHELL";
                   1062:     const char *ename = NULL;
                   1063:     uint32_t k;
                   1064:     Var *v;
                   1065:     k = ohash_interval(name, &ename);
                   1066:     v = create_var(name, ename);
                   1067:     ohash_insert(&global_variables,
                   1068:        ohash_lookup_interval(&global_variables, name, ename, k), v);
                   1069:        /* the environment shall not affect it */
                   1070:     v->flags = VAR_SHELL | VAR_SEEN_ENV;
                   1071:     var_init_string(v, _PATH_BSHELL);
                   1072: }
                   1073:
1.1       deraadt  1074: /*-
                   1075:  *-----------------------------------------------------------------------
                   1076:  * Var_Init --
                   1077:  *     Initialize the module
                   1078:  *
                   1079:  * Side Effects:
1.53      espie    1080:  *     The CTXT_CMD and CTXT_GLOBAL contexts are initialized
1.1       deraadt  1081:  *-----------------------------------------------------------------------
                   1082:  */
                   1083: void
1.59      espie    1084: Var_Init(void)
1.1       deraadt  1085: {
1.62      espie    1086:     ohash_init(&global_variables, 10, &var_info);
                   1087:     set_magic_shell_variable();
1.35      espie    1088:
1.62      espie    1089:
1.64      espie    1090:     errorIsOkay = true;
1.62      espie    1091:     Var_setCheckEnvFirst(false);
1.53      espie    1092:
                   1093:     VarModifiers_Init();
1.1       deraadt  1094: }
                   1095:
                   1096:
1.55      espie    1097: #ifdef CLEANUP
1.1       deraadt  1098: void
1.59      espie    1099: Var_End(void)
1.1       deraadt  1100: {
1.38      espie    1101:     Var *v;
                   1102:     unsigned int i;
                   1103:
1.62      espie    1104:     for (v = ohash_first(&global_variables, &i); v != NULL;
                   1105:        v = ohash_next(&global_variables, &i))
1.38      espie    1106:            VarDelete(v);
1.55      espie    1107: }
1.37      espie    1108: #endif
1.5       millert  1109:
1.53      espie    1110: static const char *interpret(int);
                   1111:
                   1112: static const char *
1.59      espie    1113: interpret(int f)
1.53      espie    1114: {
                   1115:     if (f & VAR_DUMMY)
                   1116:        return "(D)";
                   1117:     return "";
                   1118: }
                   1119:
1.1       deraadt  1120:
                   1121: /****************** PRINT DEBUGGING INFO *****************/
1.31      espie    1122: static void
1.59      espie    1123: VarPrintVar(Var *v)
1.1       deraadt  1124: {
1.53      espie    1125:     printf("%-16s%s = %s\n", v->name, interpret(v->flags),
                   1126:        (v->flags & VAR_DUMMY) == 0 ? VarValue(v) : "(none)");
1.1       deraadt  1127: }
                   1128:
                   1129: void
1.59      espie    1130: Var_Dump(void)
1.1       deraadt  1131: {
1.53      espie    1132:     Var *v;
                   1133:     unsigned int i;
                   1134:
                   1135:     printf("#*** Global Variables:\n");
1.38      espie    1136:
1.62      espie    1137:     for (v = ohash_first(&global_variables, &i); v != NULL;
                   1138:        v = ohash_next(&global_variables, &i))
1.53      espie    1139:        VarPrintVar(v);
1.37      espie    1140:
1.53      espie    1141: }
1.46      espie    1142:
                   1143: static const char *quotable = " \t\n\\'\"";
                   1144:
                   1145: /* In POSIX mode, variable assignments passed on the command line are
                   1146:  * propagated to sub makes through MAKEFLAGS.
                   1147:  */
                   1148: void
1.59      espie    1149: Var_AddCmdline(const char *name)
1.46      espie    1150: {
                   1151:     Var *v;
                   1152:     unsigned int i;
                   1153:     BUFFER buf;
                   1154:     char *s;
                   1155:
                   1156:     Buf_Init(&buf, MAKE_BSIZE);
                   1157:
1.62      espie    1158:     for (v = ohash_first(&global_variables, &i); v != NULL;
                   1159:        v = ohash_next(&global_variables, &i)) {
                   1160:                if (!(v->flags & VAR_FROM_CMD)) {
                   1161:                        continue;
                   1162:                }
1.46      espie    1163:                /* We assume variable names don't need quoting */
                   1164:                Buf_AddString(&buf, v->name);
                   1165:                Buf_AddChar(&buf, '=');
                   1166:                for (s = VarValue(v); *s != '\0'; s++) {
                   1167:                        if (strchr(quotable, *s))
                   1168:                                Buf_AddChar(&buf, '\\');
                   1169:                        Buf_AddChar(&buf, *s);
                   1170:                }
                   1171:                Buf_AddSpace(&buf);
                   1172:     }
                   1173:     Var_Append(name, Buf_Retrieve(&buf), VAR_GLOBAL);
                   1174:     Buf_Destroy(&buf);
                   1175: }