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

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.66      espie     101: bool           errorIsOkay;
                    102: static bool    checkEnvFirst;  /* true if environment should be searched for
                    103:                                 * variables before the global context */
1.62      espie     104:
                    105: void
                    106: Var_setCheckEnvFirst(bool yes)
                    107: {
                    108:        checkEnvFirst = yes;
                    109: }
1.1       deraadt   110:
                    111: /*
1.66      espie     112:  * The rules for variable look-up are complicated.
                    113:  *
                    114:  * - Dynamic variables like $@ and $* are special. They always pertain to
                    115:  * a given variable.  In this implementation of make, it is an error to
                    116:  * try to affect them manually. They are stored in a local symtable directly
                    117:  * inside the gnode.
                    118:  *
                    119:  * Global variables can be obtained:
                    120:  * - from the command line
                    121:  * - from the environment
                    122:  * - from the Makefile proper.
                    123:  * All of these are stored in a hash global_variables.
                    124:  *
                    125:  * Variables set on the command line override Makefile contents, are
                    126:  * passed to submakes (see Var_AddCmdLine), and are also exported to the
                    127:  * environment.
                    128:  *
                    129:  * Without -e (!checkEnvFirst), make will see variables set in the
                    130:  * Makefile, and default to the environment otherwise.
                    131:  *
                    132:  * With -e (checkEnvFirst), make will see the environment first, and that
                    133:  * will override anything that's set in the Makefile (but not set on
                    134:  * the command line).
                    135:  *
                    136:  * The SHELL variable is very special: it is never obtained from the
                    137:  * environment, and never passed to the environment.
1.1       deraadt   138:  */
1.53      espie     139:
1.66      espie     140: /* definitions pertaining to dynamic variables */
                    141:
                    142: /* full names of dynamic variables */
1.53      espie     143: static char *varnames[] = {
1.66      espie     144:        TARGET,
                    145:        PREFIX,
                    146:        ARCHIVE,
                    147:        MEMBER,
                    148:        OODATE,
                    149:        ALLSRC,
                    150:        IMPSRC,
                    151:        FTARGET,
                    152:        DTARGET,
                    153:        FPREFIX,
                    154:        DPREFIX,
                    155:        FARCHIVE,
                    156:        DARCHIVE,
                    157:        FMEMBER,
                    158:        DMEMBER
                    159: };
1.53      espie     160:
1.66      espie     161: /* hashed names of dynamic variables */
1.65      espie     162: #include    "varhashconsts.h"
                    163:
                    164: /* extended indices for System V stuff */
                    165: #define FTARGET_INDEX  7
                    166: #define DTARGET_INDEX  8
                    167: #define FPREFIX_INDEX  9
                    168: #define DPREFIX_INDEX  10
                    169: #define FARCHIVE_INDEX 11
                    170: #define DARCHIVE_INDEX 12
                    171: #define FMEMBER_INDEX  13
                    172: #define DMEMBER_INDEX  14
                    173:
1.66      espie     174: #define GLOBAL_INDEX   -1
                    175:
1.65      espie     176: #define EXTENDED2SIMPLE(i)     (((i)-LOCAL_SIZE)/2)
                    177: #define IS_EXTENDED_F(i)       ((i)%2 == 1)
                    178:
1.66      espie     179:
1.65      espie     180: static struct ohash global_variables;
1.1       deraadt   181:
1.66      espie     182:
1.37      espie     183: typedef struct Var_ {
1.66      espie     184:        BUFFER val;             /* the variable value */
                    185:        unsigned int flags;     /* miscellaneous status flags */
1.62      espie     186: #define VAR_IN_USE     1       /* Variable's value currently being used. */
1.66      espie     187:                                /* (Used to avoid recursion) */
                    188: #define VAR_DUMMY      2       /* Variable is currently just a name */
                    189:                                /* In particular: BUFFER is invalid */
                    190: #define VAR_FROM_CMD   4       /* Special source: command line */
                    191: #define VAR_FROM_ENV   8       /* Special source: environment */
                    192: #define VAR_SEEN_ENV   16      /* No need to go look up environment again */
                    193: #define VAR_SHELL      32      /* Magic behavior */
                    194:
1.62      espie     195: #define POISONS (POISON_NORMAL | POISON_EMPTY | POISON_NOT_DEFINED)
1.66      espie     196:                                /* Defined in var.h */
                    197:        char name[1];           /* the variable's name */
1.1       deraadt   198: }  Var;
                    199:
1.53      espie     200:
                    201: static struct ohash_info var_info = {
                    202:        offsetof(Var, name),
1.66      espie     203:        NULL,
                    204:        hash_alloc, hash_free, element_alloc
                    205: };
                    206:
                    207: static int classify_var(const char *, const char **, uint32_t *);
                    208: static Var *find_any_var(const char *, const char *, SymTable *, int, uint32_t);
1.62      espie     209: static Var *find_global_var(const char *, const char *, uint32_t);
1.66      espie     210: static Var *find_global_var_without_env(const char *, const char *, uint32_t);
1.62      espie     211: static void fill_from_env(Var *);
1.53      espie     212: static Var *create_var(const char *, const char *);
1.66      espie     213: static void var_set_initial_value(Var *, const char *);
                    214: static void var_set_value(Var *, const char *);
                    215: #define var_get_value(v)       Buf_Retrieve(&((v)->val))
                    216: static void var_append_value(Var *, const char *);
                    217: static void poison_check(Var *);
1.62      espie     218: static void varq_set_append(int, const char *, GNode *, bool);
                    219: static void var_set_append(const char *, const char *, const char *, int, bool);
                    220: static void set_magic_shell_variable(void);
1.66      espie     221:
                    222: static void delete_var(Var *);
                    223: static void print_var(Var *);
                    224:
                    225:
1.53      espie     226: static const char *find_rparen(const char *);
                    227: static const char *find_ket(const char *);
                    228: typedef const char * (*find_t)(const char *);
                    229: static find_t find_pos(int);
                    230:
1.66      espie     231:
                    232:
                    233: /* Variable lookup function: return idx for dynamic variable, or
                    234:  * GLOBAL_INDEX if name is not dynamic. Set up *pk for further use.
                    235:  */
1.38      espie     236: static int
1.66      espie     237: classify_var(const char *name, const char **enamePtr, uint32_t *pk)
1.38      espie     238: {
1.66      espie     239:        size_t len;
1.38      espie     240:
1.66      espie     241:        *pk = ohash_interval(name, enamePtr);
                    242:        len = *enamePtr - name;
                    243:            /* substitute short version for long local name */
                    244:        switch (*pk % MAGICSLOTS1) {    /* MAGICSLOTS should be the    */
                    245:        case K_LONGALLSRC % MAGICSLOTS1:/* smallest constant yielding  */
                    246:                                        /* distinct case values    */
                    247:                if (*pk == K_LONGALLSRC && len == strlen(LONGALLSRC) &&
                    248:                    strncmp(name, LONGALLSRC, len) == 0)
                    249:                        return ALLSRC_INDEX;
                    250:                break;
                    251:        case K_LONGARCHIVE % MAGICSLOTS1:
                    252:                if (*pk == K_LONGARCHIVE && len == strlen(LONGARCHIVE) &&
                    253:                    strncmp(name, LONGARCHIVE, len) == 0)
                    254:                        return ARCHIVE_INDEX;
                    255:                break;
                    256:        case K_LONGIMPSRC % MAGICSLOTS1:
                    257:                if (*pk == K_LONGIMPSRC && len == strlen(LONGIMPSRC) &&
                    258:                    strncmp(name, LONGIMPSRC, len) == 0)
                    259:                        return IMPSRC_INDEX;
                    260:                break;
                    261:        case K_LONGMEMBER % MAGICSLOTS1:
                    262:                if (*pk == K_LONGMEMBER && len == strlen(LONGMEMBER) &&
                    263:                    strncmp(name, LONGMEMBER, len) == 0)
                    264:                        return MEMBER_INDEX;
                    265:                break;
                    266:        case K_LONGOODATE % MAGICSLOTS1:
                    267:                if (*pk == K_LONGOODATE && len == strlen(LONGOODATE) &&
                    268:                    strncmp(name, LONGOODATE, len) == 0)
                    269:                        return OODATE_INDEX;
                    270:                break;
                    271:        case K_LONGPREFIX % MAGICSLOTS1:
                    272:                if (*pk == K_LONGPREFIX && len == strlen(LONGPREFIX) &&
                    273:                    strncmp(name, LONGPREFIX, len) == 0)
                    274:                        return PREFIX_INDEX;
                    275:                break;
                    276:        case K_LONGTARGET % MAGICSLOTS1:
                    277:                if (*pk == K_LONGTARGET && len == strlen(LONGTARGET) &&
                    278:                    strncmp(name, LONGTARGET, len) == 0)
                    279:                        return TARGET_INDEX;
                    280:                break;
                    281:        case K_TARGET % MAGICSLOTS1:
                    282:                if (name[0] == TARGET[0] && len == 1)
                    283:                        return TARGET_INDEX;
                    284:                break;
                    285:        case K_OODATE % MAGICSLOTS1:
                    286:                if (name[0] == OODATE[0] && len == 1)
                    287:                        return OODATE_INDEX;
                    288:                break;
                    289:        case K_ALLSRC % MAGICSLOTS1:
                    290:                if (name[0] == ALLSRC[0] && len == 1)
                    291:                        return ALLSRC_INDEX;
                    292:                break;
                    293:        case K_IMPSRC % MAGICSLOTS1:
                    294:                if (name[0] == IMPSRC[0] && len == 1)
                    295:                        return IMPSRC_INDEX;
                    296:                break;
                    297:        case K_PREFIX % MAGICSLOTS1:
                    298:                if (name[0] == PREFIX[0] && len == 1)
                    299:                        return PREFIX_INDEX;
                    300:                break;
                    301:        case K_ARCHIVE % MAGICSLOTS1:
                    302:                if (name[0] == ARCHIVE[0] && len == 1)
                    303:                        return ARCHIVE_INDEX;
                    304:                break;
                    305:        case K_MEMBER % MAGICSLOTS1:
                    306:                if (name[0] == MEMBER[0] && len == 1)
                    307:                        return MEMBER_INDEX;
                    308:                break;
                    309:        case K_FTARGET % MAGICSLOTS1:
                    310:                if (name[0] == FTARGET[0] && name[1] == FTARGET[1] && len == 2)
                    311:                        return FTARGET_INDEX;
                    312:                break;
                    313:        case K_DTARGET % MAGICSLOTS1:
                    314:                if (name[0] == DTARGET[0] && name[1] == DTARGET[1] && len == 2)
                    315:                        return DTARGET_INDEX;
                    316:                break;
                    317:        case K_FPREFIX % MAGICSLOTS1:
                    318:                if (name[0] == FPREFIX[0] && name[1] == FPREFIX[1] && len == 2)
                    319:                        return FPREFIX_INDEX;
                    320:                break;
                    321:        case K_DPREFIX % MAGICSLOTS1:
                    322:                if (name[0] == DPREFIX[0] && name[1] == DPREFIX[1] && len == 2)
                    323:                        return DPREFIX_INDEX;
                    324:                break;
                    325:        case K_FARCHIVE % MAGICSLOTS1:
                    326:                if (name[0] == FARCHIVE[0] && name[1] == FARCHIVE[1] &&
                    327:                    len == 2)
                    328:                        return FARCHIVE_INDEX;
                    329:                break;
                    330:        case K_DARCHIVE % MAGICSLOTS1:
                    331:                if (name[0] == DARCHIVE[0] && name[1] == DARCHIVE[1] &&
                    332:                    len == 2)
                    333:                        return DARCHIVE_INDEX;
                    334:                break;
                    335:        case K_FMEMBER % MAGICSLOTS1:
                    336:                if (name[0] == FMEMBER[0] && name[1] == FMEMBER[1] && len == 2)
                    337:                        return FMEMBER_INDEX;
                    338:                break;
                    339:        case K_DMEMBER % MAGICSLOTS1:
                    340:                if (name[0] == DMEMBER[0] && name[1] == DMEMBER[1] && len == 2)
                    341:                    return DMEMBER_INDEX;
                    342:                break;
                    343:        default:
                    344:                break;
                    345:        }
                    346:        return GLOBAL_INDEX;
1.38      espie     347: }
1.37      espie     348:
1.66      espie     349:
                    350: /***
                    351:  ***   Internal handling of variables.
                    352:  ***/
                    353:
                    354:
                    355: /* Create a new variable, does not initialize anything except the name.
                    356:  * in particular, buffer is invalid, and flag value is invalid. Accordingly,
                    357:  * must either:
                    358:  * - set flags to VAR_DUMMY
                    359:  * - set flags to !VAR_DUMMY, and initialize buffer, for instance with
                    360:  * var_set_initial_value().
                    361:  */
1.65      espie     362: static Var *
                    363: create_var(const char *name, const char *ename)
                    364: {
1.66      espie     365:        return ohash_create_entry(&var_info, name, &ename);
1.65      espie     366: }
                    367:
1.66      espie     368: /* Initial version of var_set_value(), to be called after create_var().
                    369:  */
1.65      espie     370: static void
1.66      espie     371: var_set_initial_value(Var *v, const char *val)
1.65      espie     372: {
1.66      espie     373:        size_t len;
1.65      espie     374:
1.66      espie     375:        len = strlen(val);
                    376:        Buf_Init(&(v->val), len+1);
                    377:        Buf_AddChars(&(v->val), len, val);
1.65      espie     378: }
                    379:
1.66      espie     380: /* Normal version of var_set_value(), to be called after variable is fully
                    381:  * initialized.
                    382:  */
1.65      espie     383: static void
1.66      espie     384: var_set_value(Var *v, const char *val)
1.65      espie     385: {
1.66      espie     386:        if ((v->flags & VAR_DUMMY) == 0) {
                    387:                Buf_Reset(&(v->val));
                    388:                Buf_AddString(&(v->val), val);
                    389:        } else {
                    390:                var_set_initial_value(v, val);
                    391:                v->flags &= ~VAR_DUMMY;
                    392:        }
1.65      espie     393: }
                    394:
1.66      espie     395: /* Add to a variable, insert a separating space if the variable was already
                    396:  * defined.
                    397:  */
1.65      espie     398: static void
1.66      espie     399: var_append_value(Var *v, const char *val)
1.65      espie     400: {
1.66      espie     401:        if ((v->flags & VAR_DUMMY) == 0) {
                    402:                Buf_AddSpace(&(v->val));
                    403:                Buf_AddString(&(v->val), val);
                    404:        } else {
                    405:                var_set_initial_value(v, val);
                    406:                v->flags &= ~VAR_DUMMY;
                    407:        }
                    408: }
                    409:
                    410:
                    411: /* Delete a variable and all the space associated with it.
1.65      espie     412:  */
                    413: static void
1.66      espie     414: delete_var(Var *v)
1.65      espie     415: {
1.66      espie     416:        if ((v->flags & VAR_DUMMY) == 0)
                    417:                Buf_Destroy(&(v->val));
                    418:        free(v);
1.65      espie     419: }
                    420:
                    421:
                    422:
1.66      espie     423:
                    424: /***
                    425:  ***   Dynamic variable handling.
                    426:  ***/
                    427:
                    428:
                    429:
                    430: /* create empty symtable.
                    431:  * XXX: to save space, dynamic variables may be NULL pointers.
                    432:  */
1.65      espie     433: void
                    434: SymTable_Init(SymTable *ctxt)
                    435: {
1.66      espie     436:        static SymTable sym_template;
                    437:        memcpy(ctxt, &sym_template, sizeof(*ctxt));
1.65      espie     438: }
                    439:
1.66      espie     440: /* free symtable.
                    441:  */
1.65      espie     442: #ifdef CLEANUP
                    443: void
                    444: SymTable_Destroy(SymTable *ctxt)
                    445: {
1.66      espie     446:        int i;
1.65      espie     447:
1.66      espie     448:        for (i = 0; i < LOCAL_SIZE; i++)
                    449:                if (ctxt->locals[i] != NULL)
                    450:                        delete_var(ctxt->locals[i]);
1.65      espie     451: }
                    452: #endif
                    453:
1.66      espie     454: /* set or append to dynamic variable.
                    455:  */
1.62      espie     456: static void
                    457: varq_set_append(int idx, const char *val, GNode *gn, bool append)
1.37      espie     458: {
1.66      espie     459:        Var *v = gn->context.locals[idx];
1.37      espie     460:
1.66      espie     461:        if (v == NULL) {
                    462:                v = create_var(varnames[idx], NULL);
1.62      espie     463: #ifdef STATS_VAR_LOOKUP
1.66      espie     464:                STAT_VAR_CREATION++;
1.62      espie     465: #endif
1.66      espie     466:                if (val != NULL)
                    467:                        var_set_initial_value(v, val);
                    468:                else
                    469:                        Buf_Init(&(v->val), 1);
                    470:                v->flags = 0;
                    471:                gn->context.locals[idx] = v;
                    472:        } else {
                    473:                if (append)
                    474:                        Buf_AddSpace(&(v->val));
                    475:                else
                    476:                        Buf_Reset(&(v->val));
                    477:                Buf_AddString(&(v->val), val);
                    478:        }
                    479:        if (DEBUG(VAR))
                    480:                printf("%s:%s = %s\n", gn->name, varnames[idx],
                    481:                    var_get_value(v));
1.62      espie     482: }
                    483:
                    484: void
                    485: Varq_Set(int idx, const char *val, GNode *gn)
                    486: {
1.66      espie     487:        varq_set_append(idx, val, gn, false);
1.37      espie     488: }
                    489:
1.53      espie     490: void
1.59      espie     491: Varq_Append(int idx, const char *val, GNode *gn)
1.37      espie     492: {
1.66      espie     493:        varq_set_append(idx, val, gn, true);
1.37      espie     494: }
                    495:
                    496: char *
1.59      espie     497: Varq_Value(int idx, GNode *gn)
1.37      espie     498: {
1.66      espie     499:        Var *v = gn->context.locals[idx];
1.37      espie     500:
1.66      espie     501:        if (v == NULL)
                    502:                return NULL;
                    503:        else
                    504:                return var_get_value(v);
1.53      espie     505: }
                    506:
1.66      espie     507: /***
                    508:  ***   Global variable handling.
                    509:  ***/
                    510:
                    511: /* Create a new global var if necessary, and set it up correctly.
                    512:  * Do not take environment into account.
                    513:  */
1.53      espie     514: static Var *
1.66      espie     515: find_global_var_without_env(const char *name, const char *ename, uint32_t k)
1.37      espie     516: {
1.65      espie     517:        unsigned int slot;
                    518:        Var *v;
1.1       deraadt   519:
1.65      espie     520:        slot = ohash_lookup_interval(&global_variables, name, ename, k);
                    521:        v = ohash_find(&global_variables, slot);
                    522:        if (v == NULL) {
                    523:                v = create_var(name, ename);
                    524:                v->flags = VAR_DUMMY;
                    525:                ohash_insert(&global_variables, slot, v);
                    526:        }
                    527:        return v;
1.35      espie     528: }
1.53      espie     529:
1.66      espie     530: /* Helper for find_global_var(): grab environment value if needed.
                    531:  */
1.62      espie     532: static void
                    533: fill_from_env(Var *v)
1.37      espie     534: {
1.66      espie     535:        char    *env;
1.37      espie     536:
1.66      espie     537:        env = getenv(v->name);
                    538:        if (env == NULL)
                    539:                v->flags |= VAR_SEEN_ENV;
                    540:        else {
                    541:                var_set_value(v, env);
                    542:                v->flags |= VAR_FROM_ENV | VAR_SEEN_ENV;
                    543:        }
1.37      espie     544:
1.53      espie     545: #ifdef STATS_VAR_LOOKUP
1.66      espie     546:        STAT_VAR_FROM_ENV++;
1.53      espie     547: #endif
1.62      espie     548: }
1.37      espie     549:
1.66      espie     550: /* Find global var, and obtain its value from the environment if needed.
                    551:  */
1.62      espie     552: static Var *
1.65      espie     553: find_global_var(const char *name, const char *ename, uint32_t k)
                    554: {
1.66      espie     555:        Var *v;
1.65      espie     556:
1.66      espie     557:        v = find_global_var_without_env(name, ename, k);
1.65      espie     558:
1.66      espie     559:        if ((v->flags & VAR_SEEN_ENV) == 0 &&
                    560:            (checkEnvFirst  && (v->flags & VAR_FROM_CMD) == 0 ||
                    561:                (v->flags & VAR_DUMMY) != 0))
                    562:                    fill_from_env(v);
1.65      espie     563:
1.66      espie     564:        return v;
1.65      espie     565: }
                    566:
1.66      espie     567: /* mark variable as poisoned, in a given setup.
                    568:  */
1.65      espie     569: void
                    570: Var_MarkPoisoned(const char *name, const char *ename, unsigned int type)
1.62      espie     571: {
1.65      espie     572:        Var   *v;
                    573:        uint32_t        k;
                    574:        int             idx;
1.66      espie     575:        idx = classify_var(name, &ename, &k);
1.65      espie     576:
1.66      espie     577:        if (idx != GLOBAL_INDEX) {
                    578:                Parse_Error(PARSE_FATAL,
1.65      espie     579:                    "Trying to poison dynamic variable $%s",
                    580:                    varnames[idx]);
                    581:                return;
                    582:        }
1.62      espie     583:
1.65      espie     584:        v = find_global_var(name, ename, k);
                    585:        v->flags |= type;
1.66      espie     586:        /* POISON_NORMAL is not lazy: if the variable already exists in
                    587:         * the Makefile, then it's a mistake.
                    588:         */
1.65      espie     589:        if (v->flags & POISON_NORMAL) {
                    590:                if (v->flags & VAR_DUMMY)
                    591:                        return;
                    592:                if (v->flags & VAR_FROM_ENV)
                    593:                        return;
                    594:                Parse_Error(PARSE_FATAL,
                    595:                    "Poisoned variable %s is already set\n", v->name);
1.62      espie     596:        }
1.37      espie     597: }
                    598:
1.66      espie     599: /* Check if there's any reason not to use the variable in this context.
                    600:  */
1.62      espie     601: static void
                    602: poison_check(Var *v)
1.1       deraadt   603: {
1.62      espie     604:        if (v->flags & POISON_NORMAL) {
1.66      espie     605:                Parse_Error(PARSE_FATAL,
1.62      espie     606:                    "Poisoned variable %s has been referenced\n", v->name);
                    607:                return;
                    608:        }
                    609:        if (v->flags & VAR_DUMMY) {
                    610:                Parse_Error(PARSE_FATAL,
                    611:                    "Poisoned variable %s is not defined\n", v->name);
                    612:                return;
                    613:        }
                    614:        if (v->flags & POISON_EMPTY)
1.66      espie     615:                if (strcmp(var_get_value(v), "") == 0)
                    616:                        Parse_Error(PARSE_FATAL,
1.62      espie     617:                            "Poisoned variable %s is empty\n", v->name);
1.37      espie     618: }
                    619:
1.66      espie     620: /* Delete global variable.
                    621:  */
1.1       deraadt   622: void
1.66      espie     623: Var_Deletei(const char *name, const char *ename)
1.1       deraadt   624: {
1.66      espie     625:        Var *v;
                    626:        uint32_t k;
1.62      espie     627:        unsigned int slot;
1.66      espie     628:        int idx;
1.62      espie     629:
1.66      espie     630:        idx = classify_var(name, &ename, &k);
                    631:        if (idx != GLOBAL_INDEX) {
                    632:                Parse_Error(PARSE_FATAL,
                    633:                    "Trying to delete dynamic variable $%s", varnames[idx]);
                    634:                return;
                    635:        }
1.62      espie     636:        slot = ohash_lookup_interval(&global_variables, name, ename, k);
                    637:        v = ohash_find(&global_variables, slot);
                    638:
                    639:        if (v == NULL)
                    640:                return;
1.66      espie     641:
1.62      espie     642:        if (checkEnvFirst && (v->flags & VAR_FROM_ENV))
                    643:                return;
1.1       deraadt   644:
1.62      espie     645:        if (v->flags & VAR_FROM_CMD)
                    646:                return;
1.37      espie     647:
1.62      espie     648:        ohash_remove(&global_variables, slot);
1.66      espie     649:        delete_var(v);
1.1       deraadt   650: }
                    651:
1.66      espie     652: /* Set or add a global variable, in VAR_CMD or VAR_GLOBAL context.
                    653:  */
1.62      espie     654: static void
                    655: var_set_append(const char *name, const char *ename, const char *val, int ctxt,
                    656:     bool append)
1.1       deraadt   657: {
1.66      espie     658:        Var *v;
                    659:        uint32_t k;
                    660:        int idx;
1.53      espie     661:
1.66      espie     662:        idx = classify_var(name, &ename, &k);
                    663:        if (idx != GLOBAL_INDEX) {
1.62      espie     664:                Parse_Error(PARSE_FATAL, "Trying to %s dynamic variable $%s",
                    665:                    append ? "append to" : "set", varnames[idx]);
                    666:                return;
                    667:        }
1.53      espie     668:
1.62      espie     669:        v = find_global_var(name, ename, k);
                    670:        if (v->flags & POISON_NORMAL)
                    671:                Parse_Error(PARSE_FATAL, "Trying to %s poisoned variable %s\n",
                    672:                    append ? "append to" : "set", v->name);
                    673:        /* so can we write to it ? */
1.66      espie     674:        if (ctxt == VAR_CMD) {  /* always for command line */
                    675:                (append ? var_append_value : var_set_value)(v, val);
1.62      espie     676:                v->flags |= VAR_FROM_CMD;
                    677:                if ((v->flags & VAR_SHELL) == 0) {
1.66      espie     678:                        /* Any variables given on the command line are
1.62      espie     679:                         * automatically exported to the environment,
1.66      espie     680:                         * except for SHELL (as per POSIX standard).
1.62      espie     681:                         */
                    682:                        esetenv(v->name, val);
1.66      espie     683:                }
1.62      espie     684:                if (DEBUG(VAR))
1.66      espie     685:                        printf("command:%s = %s\n", v->name, var_get_value(v));
1.62      espie     686:        } else if ((v->flags & VAR_FROM_CMD) == 0 &&
                    687:             (!checkEnvFirst || (v->flags & VAR_FROM_ENV) == 0)) {
1.66      espie     688:                (append ? var_append_value : var_set_value)(v, val);
1.62      espie     689:                if (DEBUG(VAR))
1.66      espie     690:                        printf("global:%s = %s\n", v->name, var_get_value(v));
1.62      espie     691:        } else if (DEBUG(VAR))
1.66      espie     692:                printf("overriden:%s = %s\n", v->name, var_get_value(v));
1.1       deraadt   693: }
                    694:
                    695: void
1.62      espie     696: Var_Seti(const char *name, const char *ename, const char *val, int ctxt)
1.1       deraadt   697: {
1.62      espie     698:        var_set_append(name, ename, val, ctxt, false);
                    699: }
1.53      espie     700:
1.62      espie     701: void
                    702: Var_Appendi(const char *name, const char *ename, const char *val, int ctxt)
                    703: {
                    704:        var_set_append(name, ename, val, ctxt, true);
                    705: }
1.53      espie     706:
1.66      espie     707: /* XXX different semantics for Var_Valuei() and Var_Definedi():
                    708:  * references to poisoned value variables will error out in Var_Valuei(),
                    709:  * but not in Var_Definedi(), so the following construct works:
                    710:  *     .poison BINDIR
                    711:  *     BINDIR ?= /usr/bin
                    712:  */
1.53      espie     713: char *
1.59      espie     714: Var_Valuei(const char *name, const char *ename)
1.53      espie     715: {
1.66      espie     716:        Var *v;
                    717:        uint32_t k;
                    718:        int idx;
1.62      espie     719:
1.66      espie     720:        idx = classify_var(name, &ename, &k);
                    721:        if (idx != GLOBAL_INDEX) {
                    722:                Parse_Error(PARSE_FATAL,
                    723:                    "Trying to get value of dynamic variable $%s",
                    724:                        varnames[idx]);
                    725:                return NULL;
1.62      espie     726:        }
1.66      espie     727:        v = find_global_var(name, ename, k);
                    728:        if (v->flags & POISONS)
                    729:                poison_check(v);
                    730:        if ((v->flags & VAR_DUMMY) == 0)
                    731:                return var_get_value(v);
                    732:        else
                    733:                return NULL;
1.53      espie     734: }
                    735:
1.62      espie     736: bool
                    737: Var_Definedi(const char *name, const char *ename)
                    738: {
1.66      espie     739:        Var *v;
                    740:        uint32_t k;
                    741:        int idx;
1.62      espie     742:
1.66      espie     743:        idx = classify_var(name, &ename, &k);
                    744:        /* We don't bother writing an error message for dynamic variables,
                    745:         * these will be caught when getting set later, usually.
                    746:         */
                    747:        if (idx == GLOBAL_INDEX) {
1.62      espie     748:                v = find_global_var(name, ename, k);
                    749:                if (v->flags & POISON_NORMAL)
1.66      espie     750:                        poison_check(v);
1.62      espie     751:                if ((v->flags & VAR_DUMMY) == 0)
                    752:                        return true;
                    753:        }
                    754:        return false;
1.65      espie     755: }
                    756:
1.66      espie     757:
                    758: /***
                    759:  ***   Substitution functions, handling both global and dynamic variables.
                    760:  ***/
                    761:
                    762:
                    763: /* XXX contrary to find_global_var(), find_any_var() can return NULL pointers.
                    764:  */
1.65      espie     765: static Var *
1.66      espie     766: find_any_var(const char *name, const char *ename, SymTable *ctxt,
1.65      espie     767:     int idx, uint32_t k)
                    768: {
1.66      espie     769:        /* Handle local variables first */
                    770:        if (idx != GLOBAL_INDEX) {
                    771:                if (ctxt != NULL) {
                    772:                        if (idx < LOCAL_SIZE)
                    773:                                return ctxt->locals[idx];
                    774:                        else
                    775:                                return ctxt->locals[EXTENDED2SIMPLE(idx)];
                    776:                } else
                    777:                        return NULL;
                    778:        } else {
                    779:                return find_global_var(name, ename, k);
                    780:        }
1.62      espie     781: }
                    782:
1.66      espie     783: /* All the scanning functions needed to account for all the forms of
                    784:  * variable names that exist:
                    785:  *     $A, ${AB}, $(ABC), ${A:mod}, $(A:mod)
                    786:  */
1.53      espie     787:
                    788: static const char *
1.59      espie     789: find_rparen(const char *p)
1.53      espie     790: {
                    791:        while (*p != '$' && *p != '\0' && *p != ')' && *p != ':')
                    792:                p++;
                    793:        return p;
                    794: }
1.1       deraadt   795:
1.53      espie     796: static const char *
1.59      espie     797: find_ket(const char *p)
1.53      espie     798: {
                    799:        while (*p != '$' && *p != '\0' && *p != '}' && *p != ':')
                    800:                p++;
                    801:        return p;
                    802: }
1.1       deraadt   803:
1.66      espie     804: /* Figure out what kind of name we're looking for from a start character.
                    805:  */
1.53      espie     806: static find_t
1.59      espie     807: find_pos(int c)
1.53      espie     808: {
                    809:        switch(c) {
1.66      espie     810:        case '(':
1.53      espie     811:                return find_rparen;
1.66      espie     812:        case '{':
1.53      espie     813:                return find_ket;
                    814:        default:
1.66      espie     815:                Parse_Error(PARSE_FATAL,
                    816:                    "Wrong character in variable spec %c (can't happen)");
                    817:                return find_rparen;
1.53      espie     818:        }
1.1       deraadt   819: }
                    820:
1.68    ! espie     821: bool
        !           822: Var_ParseSkip(const char **pstr, SymTable *ctxt)
1.1       deraadt   823: {
1.68    ! espie     824:        const char *tstr;
        !           825:        Var *v;
        !           826:        char paren;
        !           827:        const char *str = *pstr;
1.66      espie     828:        const char *start;
                    829:        size_t length;
                    830:        struct Name name;
1.68    ! espie     831:        bool result;
1.66      espie     832:
                    833:        v = NULL;
                    834:        start = str;
1.53      espie     835:        str++;
                    836:
1.66      espie     837:        if (*str != '(' && *str != '{') {
                    838:                name.tofree = false;
                    839:                tstr = str + 1;
                    840:                length = 2;
                    841:                paren = '\0';
                    842:        } else {
                    843:                paren = *str;
                    844:                str++;
                    845:
                    846:                /* Find eventual modifiers in the variable */
                    847:                tstr = VarName_Get(str, &name, ctxt, false, find_pos(paren));
                    848:                VarName_Free(&name);
                    849:                length = tstr - start;
                    850:                if (*tstr != 0)
                    851:                        length++;
                    852:        }
                    853:
1.68    ! espie     854:        result = true;
1.66      espie     855:        if (*tstr == ':' && paren != '\0')
                    856:                 if (VarModifiers_Apply(NULL, NULL, ctxt, true, NULL, tstr,
                    857:                    paren, &length) == var_Error)
1.68    ! espie     858:                        result = false;
        !           859:        *pstr += length;
        !           860:        return result;
1.1       deraadt   861: }
                    862:
1.55      espie     863: /* As of now, Var_ParseBuffer is just a wrapper around Var_Parse. For
                    864:  * speed, it may be better to revisit the implementation to do things
                    865:  * directly. */
                    866: bool
1.66      espie     867: Var_ParseBuffer(Buffer buf, const char *str, SymTable *ctxt, bool err,
1.59      espie     868:     size_t *lengthPtr)
1.53      espie     869: {
1.66      espie     870:        char *result;
                    871:        bool freeIt;
1.45      espie     872:
1.66      espie     873:        result = Var_Parse(str, ctxt, err, lengthPtr, &freeIt);
                    874:        if (result == var_Error)
                    875:                return false;
                    876:
                    877:        Buf_AddString(buf, result);
                    878:        if (freeIt)
                    879:                free(result);
                    880:        return true;
1.45      espie     881: }
                    882:
1.1       deraadt   883: char *
1.66      espie     884: Var_Parse(const char *str,     /* The string to parse */
                    885:     SymTable *ctxt,            /* The context for the variable */
                    886:     bool err,                  /* true if undefined variables are an error */
                    887:     size_t *lengthPtr,         /* OUT: The length of the specification */
1.59      espie     888:     bool *freePtr)             /* OUT: true if caller should free result */
1.44      espie     889: {
1.66      espie     890:        const char *tstr;       /* Pointer into str */
                    891:        Var *v;                 /* Variable in invocation */
                    892:        char paren;             /* Parenthesis or brace or nothing */
                    893:        struct Name name;
                    894:        const char *start;
                    895:        char *val;              /* Variable value  */
                    896:        uint32_t k;
                    897:        int idx;
                    898:
                    899:        *freePtr = false;
                    900:        start = str++;
                    901:
                    902:        val = NULL;
                    903:        v = NULL;
                    904:        idx = GLOBAL_INDEX;
                    905:
                    906:        if (*str != '(' && *str != '{') {
                    907:                name.s = str;
                    908:                name.e = str+1;
                    909:                name.tofree = false;
                    910:                tstr = str + 1;
                    911:                *lengthPtr = 2;
                    912:                paren = '\0';
                    913:        } else {
                    914:                paren = *str;
                    915:                str++;
                    916:
                    917:                /* Find eventual modifiers in the variable */
                    918:                tstr = VarName_Get(str, &name, ctxt, false, find_pos(paren));
                    919:                *lengthPtr = tstr - start;
                    920:                if (*tstr != '\0')
                    921:                        (*lengthPtr)++;
                    922:        }
1.5       millert   923:
1.66      espie     924:        idx = classify_var(name.s, &name.e, &k);
                    925:        v = find_any_var(name.s, name.e, ctxt, idx, k);
                    926:        if (v != NULL && (v->flags & POISONS) != 0)
                    927:                poison_check(v);
                    928:        if (v != NULL && (v->flags & VAR_DUMMY) == 0) {
                    929:                if (v->flags & VAR_IN_USE)
                    930:                        Fatal("Variable %s is recursive.", v->name);
                    931:                        /*NOTREACHED*/
                    932:                else
                    933:                        v->flags |= VAR_IN_USE;
1.53      espie     934:
1.66      espie     935:                /* Before doing any modification, we have to make sure the
                    936:                 * value has been fully expanded. If it looks like recursion
                    937:                 * might be necessary (there's a dollar sign somewhere in
                    938:                 * the variable's value) we just call Var_Subst to do any
                    939:                 * other substitutions that are necessary. Note that the
                    940:                 * value returned by Var_Subst will have been dynamically
                    941:                 * allocated, so it will need freeing when we return.
                    942:                 */
                    943:                val = var_get_value(v);
                    944:                if (idx == GLOBAL_INDEX) {
                    945:                        if (strchr(val, '$') != NULL) {
                    946:                                val = Var_Subst(val, ctxt, err);
                    947:                                *freePtr = true;
                    948:                        }
                    949:                } else if (idx >= LOCAL_SIZE) {
                    950:                        if (IS_EXTENDED_F(idx))
                    951:                                val = Var_GetTail(val);
                    952:                        else
                    953:                                val = Var_GetHead(val);
                    954:                        *freePtr = true;
                    955:                }
                    956:                v->flags &= ~VAR_IN_USE;
                    957:        }
                    958:        if (*tstr == ':' && paren != '\0')
                    959:                val = VarModifiers_Apply(val, &name, ctxt, err, freePtr,
                    960:                    tstr, paren, lengthPtr);
                    961:        if (val == NULL) {
                    962:                val = err ? var_Error : varNoError;
                    963:                /* Dynamic source */
                    964:                if (idx != GLOBAL_INDEX) {
                    965:                        /* can't be expanded for now: copy the spec instead. */
                    966:                        if (ctxt == NULL) {
                    967:                                *freePtr = true;
                    968:                                val = Str_dupi(start, start+ *lengthPtr);
                    969:                        } else {
                    970:                        /* somehow, this should have been expanded already. */
                    971:                                GNode *n;
                    972:
                    973:                                /* XXX */
                    974:                                n = (GNode *)(((char *)ctxt) -
                    975:                                    offsetof(GNode, context));
                    976:                                if (idx >= LOCAL_SIZE)
                    977:                                        idx = EXTENDED2SIMPLE(idx);
                    978:                                switch(idx) {
                    979:                                case IMPSRC_INDEX:
                    980:                                        Fatal(
                    981: "Using $< in a non-suffix rule context is a GNUmake idiom (line %lu of %s)",
                    982:                                            n->lineno, n->fname);
                    983:                                default:
                    984:                                        Error(
                    985: "Using undefined dynamic variable $%s (line %lu of %s)",
                    986:                                            varnames[idx], n->lineno, n->fname);
                    987:                                        break;
                    988:                                }
                    989:                        }
1.53      espie     990:                }
                    991:        }
1.66      espie     992:        VarName_Free(&name);
                    993:        return val;
1.42      espie     994: }
                    995:
1.66      espie     996:
1.1       deraadt   997: char *
1.66      espie     998: Var_Subst(const char *str,     /* the string in which to substitute */
                    999:     SymTable *ctxt,            /* the context wherein to find variables */
1.59      espie    1000:     bool undefErr)             /* true if undefineds are an error */
                   1001: {
1.66      espie    1002:        BUFFER buf;             /* Buffer for forming things */
                   1003:        static bool errorReported;
                   1004:
                   1005:        Buf_Init(&buf, MAKE_BSIZE);
                   1006:        errorReported = false;
                   1007:
                   1008:        for (;;) {
                   1009:                char *val;      /* Value to substitute for a variable */
                   1010:                size_t length;  /* Length of the variable invocation */
                   1011:                bool doFree;    /* Set true if val should be freed */
                   1012:                const char *cp;
                   1013:
                   1014:                /* copy uninteresting stuff */
                   1015:                for (cp = str; *str != '\0' && *str != '$'; str++)
                   1016:                        ;
                   1017:                Buf_Addi(&buf, cp, str);
                   1018:                if (*str == '\0')
                   1019:                        break;
                   1020:                if (str[1] == '$') {
                   1021:                        /* A $ may be escaped with another $. */
                   1022:                        Buf_AddChar(&buf, '$');
                   1023:                        str += 2;
                   1024:                        continue;
                   1025:                }
                   1026:                val = Var_Parse(str, ctxt, undefErr, &length, &doFree);
                   1027:                /* When we come down here, val should either point to the
                   1028:                 * value of this variable, suitably modified, or be NULL.
                   1029:                 * Length should be the total length of the potential
                   1030:                 * variable invocation (from $ to end character...) */
                   1031:                if (val == var_Error || val == varNoError) {
                   1032:                        /* If errors are not an issue, skip over the variable
                   1033:                         * and continue with the substitution. Otherwise, store
                   1034:                         * the dollar sign and advance str so we continue with
                   1035:                         * the string...  */
                   1036:                        if (errorIsOkay)
                   1037:                                str += length;
                   1038:                        else if (undefErr) {
                   1039:                                /* If variable is undefined, complain and
                   1040:                                 * skip the variable name. The complaint
                   1041:                                 * will stop us from doing anything when
                   1042:                                 * the file is parsed.  */
                   1043:                                if (!errorReported)
                   1044:                                        Parse_Error(PARSE_FATAL,
                   1045:                                             "Undefined variable \"%.*s\"",
                   1046:                                             length, str);
                   1047:                                str += length;
                   1048:                                errorReported = true;
                   1049:                        } else {
                   1050:                                Buf_AddChar(&buf, *str);
                   1051:                                str++;
                   1052:                        }
                   1053:                } else {
                   1054:                        /* We've now got a variable structure to store in.
                   1055:                         * But first, advance the string pointer.  */
                   1056:                        str += length;
                   1057:
                   1058:                        /* Copy all the characters from the variable value
                   1059:                         * straight into the new string.  */
                   1060:                        Buf_AddString(&buf, val);
                   1061:                        if (doFree)
                   1062:                                free(val);
                   1063:                }
1.24      espie    1064:        }
1.66      espie    1065:        return  Buf_Retrieve(&buf);
                   1066: }
                   1067:
                   1068:
                   1069: /***
                   1070:  ***   Supplementary support for .for loops.
                   1071:  ***/
                   1072:
                   1073:
                   1074:
                   1075: struct LoopVar
                   1076: {
                   1077:        Var old;        /* keep old variable value (before the loop) */
                   1078:        Var *me;        /* the variable we're dealing with */
                   1079: };
                   1080:
                   1081:
                   1082: struct LoopVar *
                   1083: Var_NewLoopVar(const char *name, const char *ename)
                   1084: {
                   1085:        struct LoopVar *l;
                   1086:        uint32_t k;
                   1087:
                   1088:        l = emalloc(sizeof(struct LoopVar));
                   1089:
                   1090:        /* we obtain a new variable quickly, make a snapshot of its old
                   1091:         * value, and make sure the environment cannot touch us.
                   1092:         */
                   1093:        /* XXX: should we avoid dynamic variables ? */
                   1094:        k = ohash_interval(name, &ename);
                   1095:
                   1096:        l->me = find_global_var_without_env(name, ename, k);
                   1097:        l->old = *(l->me);
1.67      espie    1098:        l->me->flags = VAR_SEEN_ENV | VAR_DUMMY;
1.66      espie    1099:        return l;
                   1100: }
                   1101:
                   1102: void
                   1103: Var_DeleteLoopVar(struct LoopVar *l)
                   1104: {
1.67      espie    1105:        if ((l->me->flags & VAR_DUMMY) == 0)
                   1106:                Buf_Destroy(&(l->me->val));
1.66      espie    1107:        *(l->me) = l->old;
                   1108:        free(l);
1.24      espie    1109: }
1.1       deraadt  1110:
1.53      espie    1111: void
1.66      espie    1112: Var_SubstVar(Buffer buf,       /* To store result */
                   1113:     const char *str,           /* The string in which to substitute */
                   1114:     struct LoopVar *l,         /* Handle */
1.59      espie    1115:     const char *val)           /* Its value */
1.24      espie    1116: {
1.66      espie    1117:        const char *var = l->me->name;
1.24      espie    1118:
1.66      espie    1119:        var_set_value(l->me, val);
1.53      espie    1120:
1.66      espie    1121:        for (;;) {
                   1122:                const char *start;
                   1123:                /* Copy uninteresting stuff */
                   1124:                for (start = str; *str != '\0' && *str != '$'; str++)
                   1125:                        ;
                   1126:                Buf_Addi(buf, start, str);
                   1127:
                   1128:                start = str;
                   1129:                if (*str++ == '\0')
                   1130:                        break;
                   1131:                str++;
                   1132:                /* and escaped dollars */
                   1133:                if (start[1] == '$') {
                   1134:                        Buf_Addi(buf, start, start+2);
                   1135:                        continue;
                   1136:                }
                   1137:                /* Simple variable, if it's not us, copy.  */
                   1138:                if (start[1] != '(' && start[1] != '{') {
                   1139:                        if (start[1] != *var || var[1] != '\0') {
                   1140:                                Buf_AddChars(buf, 2, start);
                   1141:                                continue;
                   1142:                    }
                   1143:                } else {
                   1144:                        const char *p;
                   1145:                        char paren = start[1];
                   1146:
                   1147:
                   1148:                        /* Find the end of the variable specification.  */
                   1149:                        p = find_pos(paren)(str);
                   1150:                        /* A variable inside the variable. We don't know how to
                   1151:                         * expand the external variable at this point, so we
                   1152:                         * try  again with the nested variable. */
                   1153:                        if (*p == '$') {
                   1154:                                Buf_Addi(buf, start, p);
                   1155:                                str = p;
                   1156:                                continue;
                   1157:                        }
                   1158:
                   1159:                        if (strncmp(var, str, p - str) != 0 ||
                   1160:                                var[p - str] != '\0') {
                   1161:                                /* Not the variable we want to expand.  */
                   1162:                                Buf_Addi(buf, start, p);
                   1163:                                str = p;
                   1164:                                continue;
                   1165:                        }
                   1166:                        if (*p == ':') {
                   1167:                                size_t length;  /* Length of variable name */
                   1168:                                bool doFree;    /* should val be freed ? */
                   1169:                                char *newval;
                   1170:                                struct Name name;
                   1171:
                   1172:                                length = p - str + 1;
                   1173:                                doFree = false;
                   1174:                                name.s = var;
                   1175:                                name.e = var + (p-str);
                   1176:
                   1177:                                /* val won't be freed since !doFree, but
                   1178:                                 * VarModifiers_Apply doesn't know that,
                   1179:                                 * hence the cast. */
                   1180:                                newval = VarModifiers_Apply((char *)val, &name,
                   1181:                                    NULL, false, &doFree, p, paren, &length);
                   1182:                                Buf_AddString(buf, newval);
                   1183:                                if (doFree)
                   1184:                                        free(newval);
                   1185:                                str += length;
                   1186:                                continue;
                   1187:                        } else
                   1188:                                str = p+1;
                   1189:                }
                   1190:                Buf_AddString(buf, val);
                   1191:        }
                   1192: }
1.1       deraadt  1193:
1.66      espie    1194: /***
                   1195:  ***   Odds and ends
                   1196:  ***/
1.1       deraadt  1197:
1.62      espie    1198: static void
                   1199: set_magic_shell_variable()
                   1200: {
1.66      espie    1201:        const char *name = "SHELL";
                   1202:        const char *ename = NULL;
                   1203:        uint32_t k;
                   1204:        Var *v;
                   1205:
                   1206:        k = ohash_interval(name, &ename);
                   1207:        v = find_global_var_without_env(name, ename, k);
                   1208:        var_set_value(v, _PATH_BSHELL);
                   1209:        /* XXX the environment shall never affect it */
                   1210:        v->flags = VAR_SHELL | VAR_SEEN_ENV;
                   1211: }
                   1212:
                   1213: /*
                   1214:  * Var_Init
1.1       deraadt  1215:  *     Initialize the module
                   1216:  */
                   1217: void
1.59      espie    1218: Var_Init(void)
1.1       deraadt  1219: {
1.66      espie    1220:        ohash_init(&global_variables, 10, &var_info);
                   1221:        set_magic_shell_variable();
1.35      espie    1222:
1.62      espie    1223:
1.66      espie    1224:        errorIsOkay = true;
                   1225:        Var_setCheckEnvFirst(false);
1.53      espie    1226:
1.66      espie    1227:        VarModifiers_Init();
1.1       deraadt  1228: }
                   1229:
                   1230:
1.55      espie    1231: #ifdef CLEANUP
1.1       deraadt  1232: void
1.59      espie    1233: Var_End(void)
1.1       deraadt  1234: {
1.66      espie    1235:        Var *v;
                   1236:        unsigned int i;
1.38      espie    1237:
1.66      espie    1238:        for (v = ohash_first(&global_variables, &i); v != NULL;
                   1239:            v = ohash_next(&global_variables, &i))
                   1240:                delete_var(v);
1.55      espie    1241: }
1.37      espie    1242: #endif
1.5       millert  1243:
1.53      espie    1244: static const char *interpret(int);
                   1245:
                   1246: static const char *
1.59      espie    1247: interpret(int f)
1.53      espie    1248: {
1.66      espie    1249:        if (f & VAR_DUMMY)
                   1250:                return "(D)";
                   1251:        return "";
1.53      espie    1252: }
                   1253:
1.1       deraadt  1254:
1.31      espie    1255: static void
1.66      espie    1256: print_var(Var *v)
1.1       deraadt  1257: {
1.66      espie    1258:        printf("%-16s%s = %s\n", v->name, interpret(v->flags),
                   1259:            (v->flags & VAR_DUMMY) == 0 ? var_get_value(v) : "(none)");
1.1       deraadt  1260: }
                   1261:
                   1262: void
1.59      espie    1263: Var_Dump(void)
1.1       deraadt  1264: {
1.66      espie    1265:        Var *v;
                   1266:        unsigned int i;
1.53      espie    1267:
1.66      espie    1268:        printf("#*** Global Variables:\n");
1.37      espie    1269:
1.66      espie    1270:        for (v = ohash_first(&global_variables, &i); v != NULL;
                   1271:            v = ohash_next(&global_variables, &i))
                   1272:                print_var(v);
1.53      espie    1273: }
1.46      espie    1274:
                   1275: static const char *quotable = " \t\n\\'\"";
                   1276:
1.66      espie    1277: /* POSIX says that variable assignments passed on the command line should be
1.46      espie    1278:  * propagated to sub makes through MAKEFLAGS.
                   1279:  */
                   1280: void
1.59      espie    1281: Var_AddCmdline(const char *name)
1.46      espie    1282: {
1.66      espie    1283:        Var *v;
                   1284:        unsigned int i;
                   1285:        BUFFER buf;
                   1286:        char *s;
                   1287:
                   1288:        Buf_Init(&buf, MAKE_BSIZE);
                   1289:
                   1290:        for (v = ohash_first(&global_variables, &i); v != NULL;
                   1291:            v = ohash_next(&global_variables, &i)) {
                   1292:                /* This is not as expensive as it looks: this function is
                   1293:                 * called before parsing Makefiles, so there are just a
                   1294:                 * few non cmdling variables in there.
                   1295:                 */
1.62      espie    1296:                if (!(v->flags & VAR_FROM_CMD)) {
                   1297:                        continue;
                   1298:                }
1.46      espie    1299:                /* We assume variable names don't need quoting */
                   1300:                Buf_AddString(&buf, v->name);
                   1301:                Buf_AddChar(&buf, '=');
1.66      espie    1302:                for (s = var_get_value(v); *s != '\0'; s++) {
1.46      espie    1303:                        if (strchr(quotable, *s))
                   1304:                                Buf_AddChar(&buf, '\\');
                   1305:                        Buf_AddChar(&buf, *s);
                   1306:                }
                   1307:                Buf_AddSpace(&buf);
1.66      espie    1308:        }
                   1309:        Var_Append(name, Buf_Retrieve(&buf), VAR_GLOBAL);
                   1310:        Buf_Destroy(&buf);
1.46      espie    1311: }