GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/make/var.c Lines: 374 530 70.6 %
Date: 2017-11-07 Branches: 230 433 53.1 %

Line Branch Exec Source
1
/*	$OpenBSD: var.c,v 1.101 2016/10/23 14:54:14 espie Exp $	*/
2
/*	$NetBSD: var.c,v 1.18 1997/03/18 19:24:46 christos Exp $	*/
3
4
/*
5
 * Copyright (c) 1999,2000,2007 Marc Espie.
6
 *
7
 * Extensive code modifications for the OpenBSD project.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
19
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
22
 * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 */
30
/*
31
 * Copyright (c) 1988, 1989, 1990, 1993
32
 *	The Regents of the University of California.  All rights reserved.
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.
47
 * 3. Neither the name of the University nor the names of its contributors
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
64
#include <assert.h>
65
#include <stddef.h>
66
#include <stdint.h>
67
#include <stdio.h>
68
#include <stdlib.h>
69
#include <string.h>
70
#include <ohash.h>
71
72
#include "config.h"
73
#include "defines.h"
74
#include "buf.h"
75
#include "cmd_exec.h"
76
#include "stats.h"
77
#include "pathnames.h"
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
#include "dump.h"
88
#include "lowparse.h"
89
90
/*
91
 * This is a harmless return value for Var_Parse that can be used by Var_Subst
92
 * to determine if there was an error in parsing -- easier than returning
93
 * a flag, as things outside this module don't give a hoot.
94
 */
95
char	var_Error[] = "";
96
97
GNode *current_node = NULL;
98
/*
99
 * Similar to var_Error, but returned when the 'err' flag for Var_Parse is
100
 * set false. Why not just use a constant? Well, gcc likes to condense
101
 * identical string instances...
102
 */
103
static char	varNoError[] = "";
104
bool		errorIsOkay;
105
static bool	checkEnvFirst;	/* true if environment should be searched for
106
				 * variables before the global context */
107
108
void
109
Var_setCheckEnvFirst(bool yes)
110
{
111
34124
	checkEnvFirst = yes;
112
34124
}
113
114
/*
115
 * The rules for variable look-up are complicated.
116
 *
117
 * - Dynamic variables like $@ and $* are special. They always pertain to
118
 * a given variable.  In this implementation of make, it is an error to
119
 * try to affect them manually. They are stored in a local symtable directly
120
 * inside the gnode.
121
 *
122
 * Global variables can be obtained:
123
 * - from the command line
124
 * - from the environment
125
 * - from the Makefile proper.
126
 * All of these are stored in a hash global_variables.
127
 *
128
 * Variables set on the command line override Makefile contents, are
129
 * passed to submakes (see Var_AddCmdLine), and are also exported to the
130
 * environment.
131
 *
132
 * Without -e (!checkEnvFirst), make will see variables set in the
133
 * Makefile, and default to the environment otherwise.
134
 *
135
 * With -e (checkEnvFirst), make will see the environment first, and that
136
 * will override anything that's set in the Makefile (but not set on
137
 * the command line).
138
 *
139
 * The SHELL variable is very special: it is never obtained from the
140
 * environment, and never passed to the environment.
141
 */
142
143
/* definitions pertaining to dynamic variables */
144
145
/* full names of dynamic variables */
146
static char *varnames[] = {
147
	TARGET,
148
	PREFIX,
149
	ARCHIVE,
150
	MEMBER,
151
	IMPSRC,
152
	OODATE,
153
	ALLSRC,
154
	FTARGET,
155
	DTARGET,
156
	FPREFIX,
157
	DPREFIX,
158
	FARCHIVE,
159
	DARCHIVE,
160
	FMEMBER,
161
	DMEMBER,
162
	FIMPSRC,
163
	DIMPSRC
164
};
165
166
static bool xtlist[] = {
167
	false,	/* GLOBAL_INDEX */
168
	true,	/* $@ */
169
	false,	/* $* */
170
	false,	/* $! */
171
	true,	/* $% */
172
	true,	/* $< */
173
	false,	/* $? */
174
	false,	/* $> */
175
	true,	/* ${@F} */
176
	true,	/* ${@D} */
177
	false,	/* ${*F} */
178
	false,	/* ${*D} */
179
	false,	/* ${!F} */
180
	false,	/* ${!D} */
181
	true,	/* ${%F} */
182
	true,	/* ${%D} */
183
	true,	/* ${<F} */
184
	true,	/* ${<D} */
185
};
186
187
/* so that we can access tlist[-1] */
188
static bool *tlist = xtlist+1;
189
190
/* hashed names of dynamic variables */
191
#include    "varhashconsts.h"
192
193
/* extended indices for System V stuff */
194
#define FTARGET_INDEX	7
195
#define DTARGET_INDEX	8
196
#define FPREFIX_INDEX	9
197
#define DPREFIX_INDEX	10
198
#define FARCHIVE_INDEX	11
199
#define DARCHIVE_INDEX	12
200
#define FMEMBER_INDEX	13
201
#define DMEMBER_INDEX	14
202
#define FIMPSRC_INDEX	15
203
#define DIMPSRC_INDEX	16
204
205
#define GLOBAL_INDEX	-1
206
207
#define EXTENDED2SIMPLE(i)	(((i)-LOCAL_SIZE)/2)
208
#define IS_EXTENDED_F(i)	((i)%2 == 1)
209
210
211
static struct ohash global_variables;
212
213
214
typedef struct Var_ {
215
	BUFFER val;		/* the variable value */
216
	unsigned int flags;	/* miscellaneous status flags */
217
#define VAR_IN_USE	1	/* Variable's value currently being used. */
218
				/* (Used to avoid recursion) */
219
#define VAR_DUMMY	2	/* Variable is currently just a name */
220
				/* In particular: BUFFER is invalid */
221
#define VAR_FROM_CMD	4	/* Special source: command line */
222
#define VAR_FROM_ENV	8	/* Special source: environment */
223
#define VAR_SEEN_ENV	16	/* No need to go look up environment again */
224
#define VAR_SHELL	32	/* Magic behavior */
225
226
#define POISONS (POISON_NORMAL | POISON_EMPTY | POISON_NOT_DEFINED)
227
				/* Defined in var.h */
228
	char name[1];		/* the variable's name */
229
}  Var;
230
231
232
static struct ohash_info var_info = {
233
	offsetof(Var, name),
234
	NULL,
235
	hash_calloc, hash_free, element_alloc
236
};
237
238
static int classify_var(const char *, const char **, uint32_t *);
239
static Var *find_global_var(const char *, const char *, uint32_t);
240
static Var *find_global_var_without_env(const char *, const char *, uint32_t);
241
static void fill_from_env(Var *);
242
static Var *create_var(const char *, const char *);
243
static void var_set_initial_value(Var *, const char *);
244
static void var_set_value(Var *, const char *);
245
#define var_get_value(v)	((v)->flags & VAR_EXEC_LATER ? \
246
	var_exec_cmd(v) : \
247
	Buf_Retrieve(&((v)->val)))
248
static char *var_exec_cmd(Var *);
249
static void var_append_value(Var *, const char *);
250
static void poison_check(Var *);
251
static void var_set_append(const char *, const char *, const char *, int, bool);
252
static void set_magic_shell_variable(void);
253
254
static void delete_var(Var *);
255
static void print_var(Var *);
256
257
258
static const char *find_rparen(const char *);
259
static const char *find_ket(const char *);
260
typedef const char * (*find_t)(const char *);
261
static find_t find_pos(int);
262
static void push_used(Var *);
263
static void pop_used(Var *);
264
static char *get_expanded_value(const char *, const char *, int, uint32_t,
265
    SymTable *, bool, bool *);
266
static bool parse_base_variable_name(const char **, struct Name *, SymTable *);
267
268
269
270
/* Variable lookup function: return idx for dynamic variable, or
271
 * GLOBAL_INDEX if name is not dynamic. Set up *pk for further use.
272
 */
273
static int
274
classify_var(const char *name, const char **enamePtr, uint32_t *pk)
275
{
276
	size_t len;
277
278
86137159
	*pk = ohash_interval(name, enamePtr);
279
46413154
	len = *enamePtr - name;
280
	    /* substitute short version for long local name */
281






46413154
	switch (*pk % MAGICSLOTS1) {	/* MAGICSLOTS should be the    */
282
	case K_LONGALLSRC % MAGICSLOTS1:/* smallest constant yielding  */
283
					/* distinct case values	   */
284

272844
		if (*pk == K_LONGALLSRC && len == strlen(LONGALLSRC) &&
285
10623
		    strncmp(name, LONGALLSRC, len) == 0)
286
10623
			return ALLSRC_INDEX;
287
		break;
288
	case K_LONGARCHIVE % MAGICSLOTS1:
289

171239
		if (*pk == K_LONGARCHIVE && len == strlen(LONGARCHIVE) &&
290
		    strncmp(name, LONGARCHIVE, len) == 0)
291
			return ARCHIVE_INDEX;
292
		break;
293
	case K_LONGIMPSRC % MAGICSLOTS1:
294

321044
		if (*pk == K_LONGIMPSRC && len == strlen(LONGIMPSRC) &&
295
70172
		    strncmp(name, LONGIMPSRC, len) == 0)
296
70172
			return IMPSRC_INDEX;
297
		break;
298
	case K_LONGMEMBER % MAGICSLOTS1:
299

458128
		if (*pk == K_LONGMEMBER && len == strlen(LONGMEMBER) &&
300
		    strncmp(name, LONGMEMBER, len) == 0)
301
			return MEMBER_INDEX;
302
		break;
303
	case K_LONGOODATE % MAGICSLOTS1:
304

225546
		if (*pk == K_LONGOODATE && len == strlen(LONGOODATE) &&
305
		    strncmp(name, LONGOODATE, len) == 0)
306
			return OODATE_INDEX;
307
		break;
308
	case K_LONGPREFIX % MAGICSLOTS1:
309

662061
		if (*pk == K_LONGPREFIX && len == strlen(LONGPREFIX) &&
310
1290
		    strncmp(name, LONGPREFIX, len) == 0)
311
1290
			return PREFIX_INDEX;
312
		break;
313
	case K_LONGTARGET % MAGICSLOTS1:
314

537052
		if (*pk == K_LONGTARGET && len == strlen(LONGTARGET) &&
315
130170
		    strncmp(name, LONGTARGET, len) == 0)
316
130170
			return TARGET_INDEX;
317
		break;
318
	case K_TARGET % MAGICSLOTS1:
319
59195
		if (name[0] == TARGET[0] && len == 1)
320
42661
			return TARGET_INDEX;
321
		break;
322
	case K_OODATE % MAGICSLOTS1:
323
40999
		if (name[0] == OODATE[0] && len == 1)
324
63
			return OODATE_INDEX;
325
		break;
326
	case K_ALLSRC % MAGICSLOTS1:
327
158464
		if (name[0] == ALLSRC[0] && len == 1)
328
23
			return ALLSRC_INDEX;
329
		break;
330
	case K_IMPSRC % MAGICSLOTS1:
331
103260
		if (name[0] == IMPSRC[0] && len == 1)
332
146
			return IMPSRC_INDEX;
333
		break;
334
	case K_PREFIX % MAGICSLOTS1:
335
1036847
		if (name[0] == PREFIX[0] && len == 1)
336
144856
			return PREFIX_INDEX;
337
		break;
338
	case K_ARCHIVE % MAGICSLOTS1:
339
48267
		if (name[0] == ARCHIVE[0] && len == 1)
340
			return ARCHIVE_INDEX;
341
		break;
342
	case K_MEMBER % MAGICSLOTS1:
343
119672
		if (name[0] == MEMBER[0] && len == 1)
344
			return MEMBER_INDEX;
345
		break;
346
	case K_FTARGET % MAGICSLOTS1:
347

418791
		if (name[0] == FTARGET[0] && name[1] == FTARGET[1] && len == 2)
348
			return FTARGET_INDEX;
349
		break;
350
	case K_DTARGET % MAGICSLOTS1:
351

224726
		if (name[0] == DTARGET[0] && name[1] == DTARGET[1] && len == 2)
352
			return DTARGET_INDEX;
353
		break;
354
	case K_FPREFIX % MAGICSLOTS1:
355

73520
		if (name[0] == FPREFIX[0] && name[1] == FPREFIX[1] && len == 2)
356
			return FPREFIX_INDEX;
357
		break;
358
	case K_DPREFIX % MAGICSLOTS1:
359

369522
		if (name[0] == DPREFIX[0] && name[1] == DPREFIX[1] && len == 2)
360
			return DPREFIX_INDEX;
361
		break;
362
	case K_FARCHIVE % MAGICSLOTS1:
363

635866
		if (name[0] == FARCHIVE[0] && name[1] == FARCHIVE[1] &&
364
		    len == 2)
365
			return FARCHIVE_INDEX;
366
		break;
367
	case K_DARCHIVE % MAGICSLOTS1:
368

42335
		if (name[0] == DARCHIVE[0] && name[1] == DARCHIVE[1] &&
369
		    len == 2)
370
			return DARCHIVE_INDEX;
371
		break;
372
	case K_FMEMBER % MAGICSLOTS1:
373

300655
		if (name[0] == FMEMBER[0] && name[1] == FMEMBER[1] && len == 2)
374
			return FMEMBER_INDEX;
375
		break;
376
	case K_DMEMBER % MAGICSLOTS1:
377

246218
		if (name[0] == DMEMBER[0] && name[1] == DMEMBER[1] && len == 2)
378
			return DMEMBER_INDEX;
379
		break;
380
	case K_FIMPSRC % MAGICSLOTS1:
381

702686
		if (name[0] == FIMPSRC[0] && name[1] == FIMPSRC[1] && len == 2)
382
			return FIMPSRC_INDEX;
383
		break;
384
	case K_DIMPSRC % MAGICSLOTS1:
385

72471
		if (name[0] == DIMPSRC[0] && name[1] == DIMPSRC[1] && len == 2)
386
			return DIMPSRC_INDEX;
387
		break;
388
	default:
389
		break;
390
	}
391
39324001
	return GLOBAL_INDEX;
392
39724005
}
393
394
395
/***
396
 ***	Internal handling of variables.
397
 ***/
398
399
400
/* Create a new variable, does not initialize anything except the name.
401
 * in particular, buffer is invalid, and flag value is invalid. Accordingly,
402
 * must either:
403
 * - set flags to VAR_DUMMY
404
 * - set flags to !VAR_DUMMY, and initialize buffer, for instance with
405
 * var_set_initial_value().
406
 */
407
static Var *
408
create_var(const char *name, const char *ename)
409
{
410
7347390
	return ohash_create_entry(&var_info, name, &ename);
411
}
412
413
/* Initial version of var_set_value(), to be called after create_var().
414
 */
415
static void
416
var_set_initial_value(Var *v, const char *val)
417
{
418
	size_t len;
419
420
13085250
	len = strlen(val);
421
6542625
	Buf_Init(&(v->val), len+1);
422
6542625
	Buf_AddChars(&(v->val), len, val);
423
6542625
}
424
425
/* Normal version of var_set_value(), to be called after variable is fully
426
 * initialized.
427
 */
428
static void
429
var_set_value(Var *v, const char *val)
430
{
431
47077636
	if ((v->flags & VAR_DUMMY) == 0) {
432
17306017
		Buf_Reset(&(v->val));
433
17306017
		Buf_AddString(&(v->val), val);
434
17306017
	} else {
435
6232801
		var_set_initial_value(v, val);
436
6232801
		v->flags &= ~VAR_DUMMY;
437
	}
438
23538818
}
439
440
/* Add to a variable, insert a separating space if the variable was already
441
 * defined.
442
 */
443
static void
444
var_append_value(Var *v, const char *val)
445
{
446
13231490
	if ((v->flags & VAR_DUMMY) == 0) {
447
13025756
		Buf_AddSpace(&(v->val));
448
6305921
		Buf_AddString(&(v->val), val);
449
6305921
	} else {
450
309824
		var_set_initial_value(v, val);
451
309824
		v->flags &= ~VAR_DUMMY;
452
	}
453
6615745
}
454
455
456
/* Delete a variable and all the space associated with it.
457
 */
458
static void
459
delete_var(Var *v)
460
{
461
	if ((v->flags & VAR_DUMMY) == 0)
462
		Buf_Destroy(&(v->val));
463
	free(v);
464
}
465
466
467
468
469
/***
470
 ***	Dynamic variable handling.
471
 ***/
472
473
474
475
/* create empty symtable.
476
 * XXX: to save space, dynamic variables may be NULL pointers.
477
 */
478
void
479
SymTable_Init(SymTable *ctxt)
480
{
481
	static SymTable sym_template;
482
21569434
	memcpy(ctxt, &sym_template, sizeof(*ctxt));
483
10784717
}
484
485
/***
486
 ***	Global variable handling.
487
 ***/
488
489
/* Create a new global var if necessary, and set it up correctly.
490
 * Do not take environment into account.
491
 */
492
static Var *
493
find_global_var_without_env(const char *name, const char *ename, uint32_t k)
494
{
495
	unsigned int slot;
496
	Var *v;
497
498
79521508
	slot = ohash_lookup_interval(&global_variables, name, ename, k);
499
39760754
	v = ohash_find(&global_variables, slot);
500
39760754
	if (v == NULL) {
501
7347390
		v = create_var(name, ename);
502
7347390
		v->flags = VAR_DUMMY;
503
7347390
		ohash_insert(&global_variables, slot, v);
504
7347390
	}
505
39760754
	return v;
506
}
507
508
/* Helper for find_global_var(): grab environment value if needed.
509
 */
510
static void
511
fill_from_env(Var *v)
512
{
513
	char	*env;
514
515
14194996
	env = getenv(v->name);
516
7097498
	if (env == NULL)
517
		v->flags |= VAR_SEEN_ENV;
518
	else {
519
139449
		var_set_value(v, env);
520
		v->flags |= VAR_FROM_ENV | VAR_SEEN_ENV;
521
	}
522
523
#ifdef STATS_VAR_LOOKUP
524
	STAT_VAR_FROM_ENV++;
525
#endif
526
7097498
}
527
528
/* Find global var, and obtain its value from the environment if needed.
529
 */
530
static Var *
531
find_global_var(const char *name, const char *ename, uint32_t k)
532
{
533
	Var *v;
534
535
78608074
	v = find_global_var_without_env(name, ename, k);
536
537
39304037
	if ((v->flags & VAR_SEEN_ENV) == 0)
538

14194996
		if ((checkEnvFirst && (v->flags & VAR_FROM_CMD) == 0) ||
539
7097498
		    (v->flags & VAR_DUMMY) != 0)
540
7097498
			fill_from_env(v);
541
542
39304037
	return v;
543
}
544
545
/* mark variable with special flags, in a given setup.
546
 */
547
void
548
Var_Mark(const char *name, const char *ename, unsigned int type)
549
{
550
	Var   *v;
551
227
	uint32_t	k;
552
	int		idx;
553
227
	idx = classify_var(name, &ename, &k);
554
555
227
	if (idx != GLOBAL_INDEX) {
556
		Parse_Error(PARSE_FATAL,
557
		    "Trying to poison dynamic variable $%s",
558
		    varnames[idx]);
559
		return;
560
	}
561
562
227
	v = find_global_var(name, ename, k);
563
227
	v->flags |= type;
564
	/* POISON_NORMAL is not lazy: if the variable already exists in
565
	 * the Makefile, then it's a mistake.
566
	 */
567
227
	if (v->flags & POISON_NORMAL) {
568
		if (v->flags & VAR_DUMMY)
569
			return;
570
		if (v->flags & VAR_FROM_ENV)
571
			return;
572
		Parse_Error(PARSE_FATAL,
573
		    "Poisoned variable %s is already set\n", v->name);
574
	}
575
454
}
576
577
/* Check if there's any reason not to use the variable in this context.
578
 */
579
static void
580
poison_check(Var *v)
581
{
582
	if (v->flags & POISON_NORMAL) {
583
		Parse_Error(PARSE_FATAL,
584
		    "Poisoned variable %s has been referenced\n", v->name);
585
		return;
586
	}
587
	if (v->flags & VAR_DUMMY) {
588
		Parse_Error(PARSE_FATAL,
589
		    "Poisoned variable %s is not defined\n", v->name);
590
		return;
591
	}
592
	if (v->flags & POISON_EMPTY)
593
		if (strcmp(var_get_value(v), "") == 0)
594
			Parse_Error(PARSE_FATAL,
595
			    "Poisoned variable %s is empty\n", v->name);
596
}
597
598
/* Delete global variable.
599
 */
600
void
601
Var_Deletei(const char *name, const char *ename)
602
{
603
	Var *v;
604
	uint32_t k;
605
	unsigned int slot;
606
	int idx;
607
608
	idx = classify_var(name, &ename, &k);
609
	if (idx != GLOBAL_INDEX) {
610
		Parse_Error(PARSE_FATAL,
611
		    "Trying to delete dynamic variable $%s", varnames[idx]);
612
		return;
613
	}
614
	slot = ohash_lookup_interval(&global_variables, name, ename, k);
615
	v = ohash_find(&global_variables, slot);
616
617
	if (v == NULL)
618
		return;
619
620
	if (checkEnvFirst && (v->flags & VAR_FROM_ENV))
621
		return;
622
623
	if (v->flags & VAR_FROM_CMD)
624
		return;
625
626
	ohash_remove(&global_variables, slot);
627
	delete_var(v);
628
}
629
630
/* Set or add a global variable, in VAR_CMD or VAR_GLOBAL context.
631
 */
632
static void
633
var_set_append(const char *name, const char *ename, const char *val, int ctxt,
634
    bool append)
635
{
636
	Var *v;
637
13590333
	uint32_t k;
638
	int idx;
639
640
13590333
	idx = classify_var(name, &ename, &k);
641
13590333
	if (idx != GLOBAL_INDEX) {
642
		Parse_Error(PARSE_FATAL, "Trying to %s dynamic variable $%s",
643
		    append ? "append to" : "set", varnames[idx]);
644
		return;
645
	}
646
647
13590333
	v = find_global_var(name, ename, k);
648
13590333
	if (v->flags & POISON_NORMAL)
649
		Parse_Error(PARSE_FATAL, "Trying to %s poisoned variable %s\n",
650
		    append ? "append to" : "set", v->name);
651
	/* so can we write to it ? */
652
13590333
	if (ctxt == VAR_CMD) {	/* always for command line */
653
114395
		(append ? var_append_value : var_set_value)(v, val);
654
114395
		v->flags |= VAR_FROM_CMD;
655
114395
		if ((v->flags & VAR_SHELL) == 0) {
656
			/* Any variables given on the command line are
657
			 * automatically exported to the environment,
658
			 * except for SHELL (as per POSIX standard).
659
			 */
660
114395
			esetenv(v->name, val);
661
114395
		}
662
114395
		if (DEBUG(VAR))
663
			printf("command:%s = %s\n", v->name, var_get_value(v));
664

13475938
	} else if ((v->flags & VAR_FROM_CMD) == 0 &&
665
13004459
	     (!checkEnvFirst || (v->flags & VAR_FROM_ENV) == 0)) {
666
13004459
		(append ? var_append_value : var_set_value)(v, val);
667
13004459
		if (DEBUG(VAR))
668
			printf("global:%s = %s\n", v->name, var_get_value(v));
669
471479
	} else if (DEBUG(VAR))
670
		printf("overridden:%s = %s\n", v->name, var_get_value(v));
671
27180666
}
672
673
void
674
Var_Seti_with_ctxt(const char *name, const char *ename, const char *val,
675
    int ctxt)
676
{
677
13006372
	var_set_append(name, ename, val, ctxt, false);
678
6503186
}
679
680
void
681
Var_Appendi_with_ctxt(const char *name, const char *ename, const char *val,
682
    int ctxt)
683
{
684
14174294
	var_set_append(name, ename, val, ctxt, true);
685
7087147
}
686
687
static char *
688
var_exec_cmd(Var *v)
689
{
690
102
	char *arg = Buf_Retrieve(&(v->val));
691
51
	char *err;
692
	char *res1;
693
51
	res1 = Cmd_Exec(arg, &err);
694
51
	if (err)
695
		Parse_Error(PARSE_WARNING, err, arg);
696
51
	var_set_value(v, res1);
697
51
	free(res1);
698
51
	v->flags &= ~VAR_EXEC_LATER;
699
102
	return Buf_Retrieve(&(v->val));
700
51
}
701
702
/* XXX different semantics for Var_Valuei() and Var_Definedi():
703
 * references to poisoned value variables will error out in Var_Valuei(),
704
 * but not in Var_Definedi(), so the following construct works:
705
 *	.poison BINDIR
706
 *	BINDIR ?= /usr/bin
707
 */
708
char *
709
Var_Valuei(const char *name, const char *ename)
710
{
711
	Var *v;
712
135136
	uint32_t k;
713
	int idx;
714
715
135136
	idx = classify_var(name, &ename, &k);
716
135136
	if (idx != GLOBAL_INDEX) {
717
		Parse_Error(PARSE_FATAL,
718
		    "Trying to get value of dynamic variable $%s",
719
			varnames[idx]);
720
		return NULL;
721
	}
722
135136
	v = find_global_var(name, ename, k);
723
135136
	if (v->flags & POISONS)
724
		poison_check(v);
725
135136
	if ((v->flags & VAR_DUMMY) == 0)
726
303093
		return var_get_value(v);
727
	else
728
34105
		return NULL;
729
135136
}
730
731
bool
732
Var_Definedi(const char *name, const char *ename)
733
{
734
	Var *v;
735
13014959
	uint32_t k;
736
	int idx;
737
738
13014959
	idx = classify_var(name, &ename, &k);
739
	/* We don't bother writing an error message for dynamic variables,
740
	 * these will be caught when getting set later, usually.
741
	 */
742
13014959
	if (idx == GLOBAL_INDEX) {
743
13014959
		v = find_global_var(name, ename, k);
744
13014959
		if (v->flags & POISON_NORMAL)
745
			poison_check(v);
746
13014959
		if ((v->flags & VAR_DUMMY) == 0)
747
2149511
			return true;
748
	}
749
10865448
	return false;
750
13014959
}
751
752
753
/***
754
 ***	Substitution functions, handling both global and dynamic variables.
755
 ***/
756
757
758
/* All the scanning functions needed to account for all the forms of
759
 * variable names that exist:
760
 *	$A, ${AB}, $(ABC), ${A:mod}, $(A:mod)
761
 */
762
763
static const char *
764
find_rparen(const char *p)
765
{
766


126762269
	while (*p != '$' && *p != '\0' && *p != ')' && *p != ':')
767
18999487
		p++;
768
1857214
	return p;
769
}
770
771
static const char *
772
find_ket(const char *p)
773
{
774


8316111856
	while (*p != '$' && *p != '\0' && *p != '}' && *p != ':')
775
1221159415
		p++;
776
159613593
	return p;
777
}
778
779
/* Figure out what kind of name we're looking for from a start character.
780
 */
781
static find_t
782
find_pos(int c)
783
{
784
322940962
	switch(c) {
785
	case '(':
786
1857214
		return find_rparen;
787
	case '{':
788
159613267
		return find_ket;
789
	default:
790
		Parse_Error(PARSE_FATAL,
791
		    "Wrong character in variable spec %c (can't happen)", c);
792
		return find_rparen;
793
	}
794
161470481
}
795
796
static bool
797
parse_base_variable_name(const char **pstr, struct Name *name, SymTable *ctxt)
798
{
799
25966700
	const char *str = *pstr;
800
	const char *tstr;
801
	bool has_modifier = false;
802
803
12983350
	switch(str[1]) {
804
	case '(':
805
	case '{':
806
		/* Find eventual modifiers in the variable */
807
12827816
		tstr = VarName_Get(str+2, name, ctxt, false, find_pos(str[1]));
808
12827816
		if (*tstr == '\0')
809
			 Parse_Error(PARSE_FATAL, "Unterminated variable spec in %s", *pstr);
810
12827816
		else if (*tstr == ':')
811
7487084
			has_modifier = true;
812
		else
813
5340732
			tstr++;
814
		break;
815
	default:
816
155534
		name->s = str+1;
817
155534
		name->e = str+2;
818
155534
		name->tofree = false;
819
		tstr = str + 2;
820
155534
		break;
821
	}
822
12983350
	*pstr = tstr;
823
12983350
	return has_modifier;
824
}
825
826
bool
827
Var_ParseSkip(const char **pstr, SymTable *ctxt)
828
{
829
	const char *str = *pstr;
830
	struct Name name;
831
	bool result;
832
	bool has_modifier;
833
	const char *tstr = str;
834
835
	if (str[1] == 0) {
836
		*pstr = str+1;
837
		return false;
838
	}
839
	has_modifier = parse_base_variable_name(&tstr, &name, ctxt);
840
	VarName_Free(&name);
841
	result = true;
842
	if (has_modifier) {
843
		bool freePtr = false;
844
		char *s = VarModifiers_Apply(NULL, NULL, ctxt, true, &freePtr,
845
		    &tstr, str[1]);
846
		if (s == var_Error)
847
			result = false;
848
		if (freePtr)
849
			free(s);
850
	}
851
	*pstr = tstr;
852
	return result;
853
}
854
855
/* As of now, Var_ParseBuffer is just a wrapper around Var_Parse. For
856
 * speed, it may be better to revisit the implementation to do things
857
 * directly. */
858
bool
859
Var_ParseBuffer(Buffer buf, const char *str, SymTable *ctxt, bool err,
860
    size_t *lengthPtr)
861
{
862
	char *result;
863
572631
	bool freeIt;
864
865
572631
	result = Var_Parse(str, ctxt, err, lengthPtr, &freeIt);
866
572631
	if (result == var_Error)
867
68820
		return false;
868
869
503811
	Buf_AddString(buf, result);
870
503811
	if (freeIt)
871
45446
		free(result);
872
503811
	return true;
873
572631
}
874
875
/* Helper function for Var_Parse: still recursive, but we tag what variables
876
 * we expand for better error messages.
877
 */
878
#define MAX_DEPTH 350
879
static Var *call_trace[MAX_DEPTH];
880
static int current_depth = 0;
881
882
static void
883
push_used(Var *v)
884
{
885
2249536
	if (v->flags & VAR_IN_USE) {
886
		int i;
887
		fprintf(stderr, "Problem with variable expansion chain: ");
888
		for (i = 0;
889
		    i < (current_depth > MAX_DEPTH ? MAX_DEPTH : current_depth);
890
		    i++)
891
			fprintf(stderr, "%s -> ", call_trace[i]->name);
892
		fprintf(stderr, "%s\n", v->name);
893
		Fatal("\tVariable %s is recursive.", v->name);
894
		/*NOTREACHED*/
895
	}
896
897
1124768
	v->flags |= VAR_IN_USE;
898
1124768
	if (current_depth < MAX_DEPTH)
899
1124768
		call_trace[current_depth] = v;
900
1124768
	current_depth++;
901
1124768
}
902
903
static void
904
pop_used(Var *v)
905
{
906
2249536
	v->flags &= ~VAR_IN_USE;
907
1124768
	current_depth--;
908
1124768
}
909
910
static char *
911
get_expanded_value(const char *name, const char *ename, int idx, uint32_t k,
912
    SymTable *ctxt, bool err, bool *freePtr)
913
{
914
	char *val;
915
916
	/* Before doing any modification, we have to make sure the
917
	 * value has been fully expanded. If it looks like recursion
918
	 * might be necessary (there's a dollar sign somewhere in
919
	 * the variable's value) we just call Var_Subst to do any
920
	 * other substitutions that are necessary. Note that the
921
	 * value returned by Var_Subst will have been dynamically
922
	 * allocated, so it will need freeing when we return.
923
	 */
924
12948882
	if (idx == GLOBAL_INDEX) {
925
12563382
		Var *v = find_global_var(name, ename, k);
926
927
12563382
		if (v == NULL)
928
			return NULL;
929
930
12563382
		if ((v->flags & POISONS) != 0)
931
			poison_check(v);
932
12563382
		if ((v->flags & VAR_DUMMY) != 0)
933
728491
			return NULL;
934
935
35504673
		val = var_get_value(v);
936
11834891
		if (strchr(val, '$') != NULL) {
937
1124768
			push_used(v);
938
1124768
			val = Var_Subst(val, ctxt, err);
939
1124768
			pop_used(v);
940
1124768
			*freePtr = true;
941
1124768
		}
942
11834891
	} else {
943
385500
		if (ctxt != NULL) {
944
376392
			if (idx < LOCAL_SIZE)
945
376392
				val = ctxt->locals[idx];
946
			else
947
				val = ctxt->locals[EXTENDED2SIMPLE(idx)];
948
		} else
949
			val = NULL;
950
385500
		if (val == NULL)
951
9114
			return NULL;
952
953
376386
		if (idx >= LOCAL_SIZE) {
954
			if (IS_EXTENDED_F(idx))
955
				val = Var_GetTail(val);
956
			else
957
				val = Var_GetHead(val);
958
			*freePtr = true;
959
		}
960
	}
961
12211277
	return val;
962
12948882
}
963
964
#define ERRMSG1 "Using $< in a non-suffix rule context is a GNUmake idiom "
965
#define ERRMSG2  "Using undefined dynamic variable $%s "
966
static void
967
bad_dynamic_variable(int idx)
968
{
969
12
	Location origin;
970
971
6
	Parse_FillLocation(&origin);
972
6
	if (idx >= LOCAL_SIZE)
973
		idx = EXTENDED2SIMPLE(idx);
974
6
	switch(idx) {
975
	case IMPSRC_INDEX:
976
6
		if (origin.fname)
977
6
			Fatal(ERRMSG1  "(%s:%lu)",
978
6
			    origin.fname, origin.lineno);
979
		else if (current_node)
980
			Fatal(ERRMSG1 "(prereq of %s)", current_node->name);
981
		else
982
			Fatal(ERRMSG1 "(?)");
983
		break;
984
	default:
985
		if (origin.fname)
986
			Error(ERRMSG2 "(%s:%lu)", varnames[idx],
987
			    origin.fname, origin.lineno);
988
		else if (current_node)
989
			Error(ERRMSG2 "(prereq of %s)", varnames[idx],
990
			    current_node->name);
991
		else
992
			Error(ERRMSG2 "(?)", varnames[idx]);
993
		break;
994
    	}
995
}
996
997
char *
998
Var_Parse(const char *str,	/* The string to parse */
999
    SymTable *ctxt,		/* The context for the variable */
1000
    bool err,			/* true if undefined variables are an error */
1001
    size_t *lengthPtr,		/* OUT: The length of the specification */
1002
    bool *freePtr)		/* OUT: true if caller should free result */
1003
{
1004
12948882
	const char *tstr;
1005
12948882
	struct Name name;
1006
	char *val;
1007
12948882
	uint32_t k;
1008
	int idx;
1009
	bool has_modifier;
1010
1011
12948882
	*freePtr = false;
1012
1013
12948882
	tstr = str;
1014
1015
12948882
	if (str[1] == 0) {
1016
		*lengthPtr = 1;
1017
		*freePtr = false;
1018
		return err ? var_Error : varNoError;
1019
	}
1020
1021
12948882
	has_modifier = parse_base_variable_name(&tstr, &name, ctxt);
1022
1023
12948882
	idx = classify_var(name.s, &name.e, &k);
1024
12948882
	val = get_expanded_value(name.s, name.e, idx, k, ctxt, err, freePtr);
1025
12948882
	if (has_modifier) {
1026
7482146
		val = VarModifiers_Apply(val, &name, ctxt, err, freePtr,
1027
7482146
		    &tstr, str[1]);
1028
7482146
	}
1029
12948882
	if (val == NULL) {
1030
737605
		val = err ? var_Error : varNoError;
1031
		/* If it comes from a dynamic source, and it doesn't have
1032
		 * a context, copy the spec instead.
1033
		 * Specifically, this make allows constructs like:
1034
		 * 	target.o: $*.c
1035
		 * Absence of a context means "parsing". But these can't
1036
		 * be expanded during parsing, to be consistent with the
1037
		 * way .SUFFIXES work.
1038
		 * .SUFFIXES may be added/reset/removed during parsing,
1039
		 * but in the end, the final list is what's considered for
1040
		 * handling targets.  So those dynamic variables must be
1041
		 * handled lazily too.
1042
		 */
1043
737605
		if (idx != GLOBAL_INDEX) {
1044
9114
			if (ctxt == NULL) {
1045
9108
				*freePtr = true;
1046
9108
				val = Str_dupi(str, tstr);
1047
9108
			} else {
1048
6
				bad_dynamic_variable(idx);
1049
			}
1050
		}
1051
	}
1052
12948876
	VarName_Free(&name);
1053
12948876
	*lengthPtr = tstr - str;
1054
12948876
	return val;
1055
12948876
}
1056
1057
1058
char *
1059
Var_Subst(const char *str,	/* the string in which to substitute */
1060
    SymTable *ctxt,		/* the context wherein to find variables */
1061
    bool undefErr)		/* true if undefineds are an error */
1062
{
1063
13284050
	BUFFER buf;		/* Buffer for forming things */
1064
	static bool errorReported;
1065
1066
13284050
	Buf_Init(&buf, MAKE_BSIZE);
1067
13284050
	errorReported = false;
1068
1069
13284050
	for (;;) {
1070
		char *val;	/* Value to substitute for a variable */
1071
17501847
		size_t length;	/* Length of the variable invocation */
1072
17501847
		bool doFree;	/* Set true if val should be freed */
1073
		const char *cp;
1074
1075
		/* copy uninteresting stuff */
1076

973182379
		for (cp = str; *str != '\0' && *str != '$'; str++)
1077
			;
1078
17501847
		Buf_Addi(&buf, cp, str);
1079
17501847
		if (*str == '\0')
1080
13284044
			break;
1081
4217803
		if (str[1] == '$') {
1082
			/* A $ may be escaped with another $. */
1083
72177
			Buf_AddChar(&buf, '$');
1084
36044
			str += 2;
1085
36044
			continue;
1086
		}
1087
4181753
		val = Var_Parse(str, ctxt, undefErr, &length, &doFree);
1088
		/* When we come down here, val should either point to the
1089
		 * value of this variable, suitably modified, or be NULL.
1090
		 * Length should be the total length of the potential
1091
		 * variable invocation (from $ to end character...) */
1092
4181753
		if (val == var_Error || val == varNoError) {
1093
			/* If errors are not an issue, skip over the variable
1094
			 * and continue with the substitution. Otherwise, store
1095
			 * the dollar sign and advance str so we continue with
1096
			 * the string...  */
1097
597673
			if (errorIsOkay)
1098
597673
				str += length;
1099
			else if (undefErr) {
1100
				/* If variable is undefined, complain and
1101
				 * skip the variable name. The complaint
1102
				 * will stop us from doing anything when
1103
				 * the file is parsed.  */
1104
				if (!errorReported)
1105
					Parse_Error(PARSE_FATAL,
1106
					     "Undefined variable \"%.*s\"",
1107
					     (int)length, str);
1108
				str += length;
1109
				errorReported = true;
1110
			} else {
1111
				Buf_AddChar(&buf, *str);
1112
				str++;
1113
			}
1114
		} else {
1115
			/* We've now got a variable structure to store in.
1116
			 * But first, advance the string pointer.  */
1117
3584080
			str += length;
1118
1119
			/* Copy all the characters from the variable value
1120
			 * straight into the new string.  */
1121
3584080
			Buf_AddString(&buf, val);
1122
3584080
			if (doFree)
1123
1028998
				free(val);
1124
		}
1125
21683594
	}
1126
26568088
	return  Buf_Retrieve(&buf);
1127
13284044
}
1128
1129
/* Very quick version of the variable scanner that just looks for target
1130
 * variables, and never ever errors out
1131
 */
1132
bool
1133
Var_Check_for_target(const char *str)
1134
{
1135
	bool seen_target = false;
1136
1137
198020
	for (;;) {
1138
139916
		const char *tstr;
1139
139916
		uint32_t k;
1140
		int idx;
1141
		bool has_modifier;
1142
139916
		struct Name name;
1143
1144
		/* skip over uninteresting stuff */
1145

12749033
		for (; *str != '\0' && *str != '$'; str++)
1146
			;
1147
139916
		if (*str == '\0')
1148
99010
			break;
1149
40906
		if (str[1] == '$') {
1150
			/* A $ may be escaped with another $. */
1151
6438
			str += 2;
1152
6438
			continue;
1153
		}
1154
1155
34468
		tstr = str;
1156
1157
34468
		has_modifier = parse_base_variable_name(&tstr, &name, NULL);
1158
34468
		idx = classify_var(name.s, &name.e, &k);
1159
34468
		if (has_modifier) {
1160
4938
			bool doFree = false;
1161
4938
			char *val = VarModifiers_Apply(NULL, NULL, NULL, false,
1162
4938
			    &doFree, &tstr, str[1]);
1163
4938
			if (doFree)
1164
				free(val);
1165
4938
		}
1166
34468
		if (tlist[idx])
1167
12936
			seen_target = true;
1168
34468
		VarName_Free(&name);
1169
34468
		str = tstr;
1170
174384
	}
1171
99010
	return seen_target;
1172
}
1173
1174
static BUFFER subst_buffer;
1175
1176
/* we would like to subst on intervals, but it's complicated, so we cheat
1177
 * by storing the interval in a static buffer.
1178
 */
1179
char *
1180
Var_Substi(const char *str, const char *estr, SymTable *ctxt, bool undefErr)
1181
{
1182
	/* delimited string: no need to copy */
1183

728898
	if (estr == NULL || *estr == '\0')
1184
		return Var_Subst(str, ctxt, undefErr);
1185
1186
364449
	Buf_Reset(&subst_buffer);
1187
364449
	Buf_Addi(&subst_buffer, str, estr);
1188
364449
	return Var_Subst(Buf_Retrieve(&subst_buffer), ctxt, undefErr);
1189
364449
}
1190
1191
/***
1192
 ***	Supplementary support for .for loops.
1193
 ***/
1194
1195
1196
1197
struct LoopVar
1198
{
1199
	Var old;	/* keep old variable value (before the loop) */
1200
	Var *me;	/* the variable we're dealing with */
1201
};
1202
1203
1204
struct LoopVar *
1205
Var_NewLoopVar(const char *name, const char *ename)
1206
{
1207
	struct LoopVar *l;
1208
	uint32_t k;
1209
1210
422593
	l = emalloc(sizeof(struct LoopVar));
1211
1212
	/* we obtain a new variable quickly, make a snapshot of its old
1213
	 * value, and make sure the environment cannot touch us.
1214
	 */
1215
	/* XXX: should we avoid dynamic variables ? */
1216
422593
	k = ohash_interval(name, &ename);
1217
1218
422593
	l->me = find_global_var_without_env(name, ename, k);
1219
422593
	l->old = *(l->me);
1220
422593
	l->me->flags = VAR_SEEN_ENV | VAR_DUMMY;
1221
422593
	return l;
1222
}
1223
1224
char *
1225
Var_LoopVarName(struct LoopVar *v)
1226
{
1227
	return v->me->name;
1228
}
1229
1230
void
1231
Var_DeleteLoopVar(struct LoopVar *l)
1232
{
1233
845186
	if ((l->me->flags & VAR_DUMMY) == 0)
1234
318029
		Buf_Destroy(&(l->me->val));
1235
422593
	*(l->me) = l->old;
1236
422593
	free(l);
1237
422593
}
1238
1239
void
1240
Var_SubstVar(Buffer buf,	/* To store result */
1241
    const char *str,		/* The string in which to substitute */
1242
    struct LoopVar *l,		/* Handle */
1243
    const char *val)		/* Its value */
1244
{
1245
33724170
	const char *var = l->me->name;
1246
1247
16862085
	var_set_value(l->me, val);
1248
1249
16862085
	for (;;) {
1250
		const char *start;
1251
		/* Copy uninteresting stuff */
1252

9217302762
		for (start = str; *str != '\0' && *str != '$'; str++)
1253
			;
1254
167763051
		Buf_Addi(buf, start, str);
1255
1256
		start = str;
1257
167763051
		if (*str++ == '\0')
1258
16862085
			break;
1259
150900966
		str++;
1260
		/* and escaped dollars */
1261
150900966
		if (start[1] == '$') {
1262
123936
			Buf_Addi(buf, start, start+2);
1263
123936
			continue;
1264
		}
1265
		/* Simple variable, if it's not us, copy.  */
1266

301554060
		if (start[1] != '(' && start[1] != '{') {
1267

4005147
			if (start[1] != *var || var[1] != '\0') {
1268
263583
				Buf_AddChars(buf, 2, start);
1269
263583
				continue;
1270
		    }
1271
		} else {
1272
148642665
			const char *p;
1273
148642665
			char paren = start[1];
1274
1275
1276
			/* Find the end of the variable specification.  */
1277
148642665
			p = find_pos(paren)(str);
1278
			/* A variable inside the variable. We don't know how to
1279
			 * expand the external variable at this point, so we
1280
			 * try  again with the nested variable.	*/
1281
148642665
			if (*p == '$') {
1282
213063
				Buf_Addi(buf, start, p);
1283
213063
				str = p;
1284
213063
				continue;
1285
			}
1286
1287

189292765
			if (strncmp(var, str, p - str) != 0 ||
1288
40863163
				var[p - str] != '\0') {
1289
				/* Not the variable we want to expand.	*/
1290
107566439
				Buf_Addi(buf, start, p);
1291
107566439
				str = p;
1292
107566439
				continue;
1293
			}
1294
40863163
			if (*p == ':') {
1295
20640916
				bool doFree;	/* should val be freed ? */
1296
				char *newval;
1297
20640916
				struct Name name;
1298
1299
20640916
				doFree = false;
1300
20640916
				name.s = var;
1301
20640916
				name.e = var + (p-str);
1302
1303
				/* val won't be freed since !doFree, but
1304
				 * VarModifiers_Apply doesn't know that,
1305
				 * hence the cast. */
1306
20640916
				newval = VarModifiers_Apply((char *)val,
1307
				    &name, NULL, false, &doFree, &p, paren);
1308
20640916
				Buf_AddString(buf, newval);
1309
20640916
				if (doFree)
1310
20640916
					free(newval);
1311
20640916
				str = p;
1312
				continue;
1313
20640916
			} else
1314
20222247
				str = p+1;
1315
168864912
		}
1316
22093029
		Buf_AddString(buf, val);
1317
22093029
	}
1318
16862085
}
1319
1320
/***
1321
 ***	Odds and ends
1322
 ***/
1323
1324
static void
1325
set_magic_shell_variable()
1326
{
1327
	const char *name = "SHELL";
1328
68248
	const char *ename = NULL;
1329
	uint32_t k;
1330
	Var *v;
1331
1332
34124
	k = ohash_interval(name, &ename);
1333
34124
	v = find_global_var_without_env(name, ename, k);
1334
34124
	var_set_value(v, _PATH_BSHELL);
1335
	/* XXX the environment shall never affect it */
1336
34124
	v->flags = VAR_SHELL | VAR_SEEN_ENV;
1337
34124
}
1338
1339
/*
1340
 * Var_Init
1341
 *	Initialize the module
1342
 */
1343
void
1344
Var_Init(void)
1345
{
1346
68248
	ohash_init(&global_variables, 10, &var_info);
1347
34124
	set_magic_shell_variable();
1348
1349
1350
34124
	errorIsOkay = true;
1351
34124
	Var_setCheckEnvFirst(false);
1352
1353
34124
	VarModifiers_Init();
1354
34124
	Buf_Init(&subst_buffer, MAKE_BSIZE);
1355
34124
}
1356
1357
1358
static const char *interpret(int);
1359
1360
static const char *
1361
interpret(int f)
1362
{
1363
	if (f & VAR_DUMMY)
1364
		return "(D)";
1365
	return "";
1366
}
1367
1368
1369
static void
1370
print_var(Var *v)
1371
{
1372
	printf("%-16s%s = %s\n", v->name, interpret(v->flags),
1373
	    (v->flags & VAR_DUMMY) == 0 ? var_get_value(v) : "(none)");
1374
}
1375
1376
1377
void
1378
Var_Dump(void)
1379
{
1380
	Var **t;
1381
1382
	unsigned int i;
1383
	const char *banner;
1384
	bool first = true;
1385
1386
	t = sort_ohash_by_name(&global_variables);
1387
/* somewhat dirty, but does the trick */
1388
1389
#define LOOP(mask, value, do_stuff) \
1390
	for (i = 0; t[i] != NULL; i++) \
1391
		if ((t[i]->flags & (mask)) == (value)) { \
1392
			if (banner) { \
1393
				if (first) \
1394
					first = false; \
1395
				else \
1396
					putchar('\n'); \
1397
				fputs(banner, stdout); \
1398
				banner = NULL; \
1399
			} \
1400
		    do_stuff; \
1401
		}
1402
1403
	banner = "#variables from command line:\n";
1404
	LOOP(VAR_FROM_CMD | VAR_DUMMY, VAR_FROM_CMD, print_var(t[i]));
1405
1406
	banner = "#global variables:\n";
1407
	LOOP(VAR_FROM_ENV| VAR_FROM_CMD | VAR_DUMMY, 0, print_var(t[i]));
1408
1409
	banner = "#variables from env:\n";
1410
	LOOP(VAR_FROM_ENV|VAR_DUMMY, VAR_FROM_ENV, print_var(t[i]));
1411
1412
	banner = "#variable name seen, but not defined:";
1413
	LOOP(VAR_DUMMY|POISONS, VAR_DUMMY, printf(" %s", t[i]->name));
1414
1415
#undef LOOP
1416
1417
	printf("\n\n");
1418
1419
	for (i = 0; t[i] != NULL; i++)
1420
		switch(t[i]->flags & POISONS) {
1421
		case POISON_NORMAL:
1422
			printf(".poison %s\n", t[i]->name);
1423
			break;
1424
		case POISON_EMPTY:
1425
			printf(".poison empty(%s)\n", t[i]->name);
1426
			break;
1427
		case POISON_NOT_DEFINED:
1428
			printf(".poison !defined(%s)\n", t[i]->name);
1429
			break;
1430
		default:
1431
			break;
1432
		}
1433
	free(t);
1434
	printf("\n");
1435
}
1436
1437
static const char *quotable = " \t\n\\'\"";
1438
1439
/* POSIX says that variable assignments passed on the command line should be
1440
 * propagated to sub makes through MAKEFLAGS.
1441
 */
1442
void
1443
Var_AddCmdline(const char *name)
1444
{
1445
	Var *v;
1446
68246
	unsigned int i;
1447
34123
	BUFFER buf;
1448
	char *s;
1449
1450
34123
	Buf_Init(&buf, MAKE_BSIZE);
1451
1452
934386
	for (v = ohash_first(&global_variables, &i); v != NULL;
1453
433070
	    v = ohash_next(&global_variables, &i)) {
1454
		/* This is not as expensive as it looks: this function is
1455
		 * called before parsing Makefiles, so there are just a
1456
		 * few non cmdling variables in there.
1457
		 */
1458
433070
		if (!(v->flags & VAR_FROM_CMD)) {
1459
			continue;
1460
		}
1461
		/* We assume variable names don't need quoting */
1462
91840
		Buf_AddString(&buf, v->name);
1463
183680
		Buf_AddChar(&buf, '=');
1464

1453614
		for (s = var_get_value(v); *s != '\0'; s++) {
1465
543127
			if (strchr(quotable, *s))
1466
242
				Buf_AddChar(&buf, '\\');
1467
1086254
			Buf_AddChar(&buf, *s);
1468
		}
1469
183680
		Buf_AddSpace(&buf);
1470
91840
	}
1471
34123
	Var_Append(name, Buf_Retrieve(&buf));
1472
34123
	Buf_Destroy(&buf);
1473
34123
}