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