xref: /netbsd-src/external/gpl2/gmake/dist/variable.c (revision 69606e3f5c9388e52aed8c120ad63c049ca45d8f)
1*69606e3fSchristos /* Internals of variables for GNU Make.
2*69606e3fSchristos Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3*69606e3fSchristos 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4*69606e3fSchristos Foundation, Inc.
5*69606e3fSchristos This file is part of GNU Make.
6*69606e3fSchristos 
7*69606e3fSchristos GNU Make is free software; you can redistribute it and/or modify it under the
8*69606e3fSchristos terms of the GNU General Public License as published by the Free Software
9*69606e3fSchristos Foundation; either version 2, or (at your option) any later version.
10*69606e3fSchristos 
11*69606e3fSchristos GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12*69606e3fSchristos WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13*69606e3fSchristos A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14*69606e3fSchristos 
15*69606e3fSchristos You should have received a copy of the GNU General Public License along with
16*69606e3fSchristos GNU Make; see the file COPYING.  If not, write to the Free Software
17*69606e3fSchristos Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.  */
18*69606e3fSchristos 
19*69606e3fSchristos #include "make.h"
20*69606e3fSchristos 
21*69606e3fSchristos #include <assert.h>
22*69606e3fSchristos 
23*69606e3fSchristos #include "dep.h"
24*69606e3fSchristos #include "filedef.h"
25*69606e3fSchristos #include "job.h"
26*69606e3fSchristos #include "commands.h"
27*69606e3fSchristos #include "variable.h"
28*69606e3fSchristos #include "rule.h"
29*69606e3fSchristos #ifdef WINDOWS32
30*69606e3fSchristos #include "pathstuff.h"
31*69606e3fSchristos #endif
32*69606e3fSchristos #include "hash.h"
33*69606e3fSchristos 
34*69606e3fSchristos /* Chain of all pattern-specific variables.  */
35*69606e3fSchristos 
36*69606e3fSchristos static struct pattern_var *pattern_vars;
37*69606e3fSchristos 
38*69606e3fSchristos /* Pointer to last struct in the chain, so we can add onto the end.  */
39*69606e3fSchristos 
40*69606e3fSchristos static struct pattern_var *last_pattern_var;
41*69606e3fSchristos 
42*69606e3fSchristos /* Create a new pattern-specific variable struct.  */
43*69606e3fSchristos 
44*69606e3fSchristos struct pattern_var *
create_pattern_var(char * target,char * suffix)45*69606e3fSchristos create_pattern_var (char *target, char *suffix)
46*69606e3fSchristos {
47*69606e3fSchristos   register struct pattern_var *p
48*69606e3fSchristos     = (struct pattern_var *) xmalloc (sizeof (struct pattern_var));
49*69606e3fSchristos 
50*69606e3fSchristos   if (last_pattern_var != 0)
51*69606e3fSchristos     last_pattern_var->next = p;
52*69606e3fSchristos   else
53*69606e3fSchristos     pattern_vars = p;
54*69606e3fSchristos   last_pattern_var = p;
55*69606e3fSchristos   p->next = 0;
56*69606e3fSchristos 
57*69606e3fSchristos   p->target = target;
58*69606e3fSchristos   p->len = strlen (target);
59*69606e3fSchristos   p->suffix = suffix + 1;
60*69606e3fSchristos 
61*69606e3fSchristos   return p;
62*69606e3fSchristos }
63*69606e3fSchristos 
64*69606e3fSchristos /* Look up a target in the pattern-specific variable list.  */
65*69606e3fSchristos 
66*69606e3fSchristos static struct pattern_var *
lookup_pattern_var(struct pattern_var * start,char * target)67*69606e3fSchristos lookup_pattern_var (struct pattern_var *start, char *target)
68*69606e3fSchristos {
69*69606e3fSchristos   struct pattern_var *p;
70*69606e3fSchristos   unsigned int targlen = strlen(target);
71*69606e3fSchristos 
72*69606e3fSchristos   for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
73*69606e3fSchristos     {
74*69606e3fSchristos       char *stem;
75*69606e3fSchristos       unsigned int stemlen;
76*69606e3fSchristos 
77*69606e3fSchristos       if (p->len > targlen)
78*69606e3fSchristos         /* It can't possibly match.  */
79*69606e3fSchristos         continue;
80*69606e3fSchristos 
81*69606e3fSchristos       /* From the lengths of the filename and the pattern parts,
82*69606e3fSchristos          find the stem: the part of the filename that matches the %.  */
83*69606e3fSchristos       stem = target + (p->suffix - p->target - 1);
84*69606e3fSchristos       stemlen = targlen - p->len + 1;
85*69606e3fSchristos 
86*69606e3fSchristos       /* Compare the text in the pattern before the stem, if any.  */
87*69606e3fSchristos       if (stem > target && !strneq (p->target, target, stem - target))
88*69606e3fSchristos         continue;
89*69606e3fSchristos 
90*69606e3fSchristos       /* Compare the text in the pattern after the stem, if any.
91*69606e3fSchristos          We could test simply using streq, but this way we compare the
92*69606e3fSchristos          first two characters immediately.  This saves time in the very
93*69606e3fSchristos          common case where the first character matches because it is a
94*69606e3fSchristos          period.  */
95*69606e3fSchristos       if (*p->suffix == stem[stemlen]
96*69606e3fSchristos           && (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
97*69606e3fSchristos         break;
98*69606e3fSchristos     }
99*69606e3fSchristos 
100*69606e3fSchristos   return p;
101*69606e3fSchristos }
102*69606e3fSchristos 
103*69606e3fSchristos /* Hash table of all global variable definitions.  */
104*69606e3fSchristos 
105*69606e3fSchristos static unsigned long
variable_hash_1(const void * keyv)106*69606e3fSchristos variable_hash_1 (const void *keyv)
107*69606e3fSchristos {
108*69606e3fSchristos   struct variable const *key = (struct variable const *) keyv;
109*69606e3fSchristos   return_STRING_N_HASH_1 (key->name, key->length);
110*69606e3fSchristos }
111*69606e3fSchristos 
112*69606e3fSchristos static unsigned long
variable_hash_2(const void * keyv)113*69606e3fSchristos variable_hash_2 (const void *keyv)
114*69606e3fSchristos {
115*69606e3fSchristos   struct variable const *key = (struct variable const *) keyv;
116*69606e3fSchristos   return_STRING_N_HASH_2 (key->name, key->length);
117*69606e3fSchristos }
118*69606e3fSchristos 
119*69606e3fSchristos static int
variable_hash_cmp(const void * xv,const void * yv)120*69606e3fSchristos variable_hash_cmp (const void *xv, const void *yv)
121*69606e3fSchristos {
122*69606e3fSchristos   struct variable const *x = (struct variable const *) xv;
123*69606e3fSchristos   struct variable const *y = (struct variable const *) yv;
124*69606e3fSchristos   int result = x->length - y->length;
125*69606e3fSchristos   if (result)
126*69606e3fSchristos     return result;
127*69606e3fSchristos   return_STRING_N_COMPARE (x->name, y->name, x->length);
128*69606e3fSchristos }
129*69606e3fSchristos 
130*69606e3fSchristos #ifndef	VARIABLE_BUCKETS
131*69606e3fSchristos #define VARIABLE_BUCKETS		523
132*69606e3fSchristos #endif
133*69606e3fSchristos #ifndef	PERFILE_VARIABLE_BUCKETS
134*69606e3fSchristos #define	PERFILE_VARIABLE_BUCKETS	23
135*69606e3fSchristos #endif
136*69606e3fSchristos #ifndef	SMALL_SCOPE_VARIABLE_BUCKETS
137*69606e3fSchristos #define	SMALL_SCOPE_VARIABLE_BUCKETS	13
138*69606e3fSchristos #endif
139*69606e3fSchristos 
140*69606e3fSchristos static struct variable_set global_variable_set;
141*69606e3fSchristos static struct variable_set_list global_setlist
142*69606e3fSchristos   = { 0, &global_variable_set };
143*69606e3fSchristos struct variable_set_list *current_variable_set_list = &global_setlist;
144*69606e3fSchristos 
145*69606e3fSchristos /* Implement variables.  */
146*69606e3fSchristos 
147*69606e3fSchristos void
init_hash_global_variable_set(void)148*69606e3fSchristos init_hash_global_variable_set (void)
149*69606e3fSchristos {
150*69606e3fSchristos   hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
151*69606e3fSchristos 	     variable_hash_1, variable_hash_2, variable_hash_cmp);
152*69606e3fSchristos }
153*69606e3fSchristos 
154*69606e3fSchristos /* Define variable named NAME with value VALUE in SET.  VALUE is copied.
155*69606e3fSchristos    LENGTH is the length of NAME, which does not need to be null-terminated.
156*69606e3fSchristos    ORIGIN specifies the origin of the variable (makefile, command line
157*69606e3fSchristos    or environment).
158*69606e3fSchristos    If RECURSIVE is nonzero a flag is set in the variable saying
159*69606e3fSchristos    that it should be recursively re-expanded.  */
160*69606e3fSchristos 
161*69606e3fSchristos struct variable *
define_variable_in_set(const char * name,unsigned int length,char * value,enum variable_origin origin,int recursive,struct variable_set * set,const struct floc * flocp)162*69606e3fSchristos define_variable_in_set (const char *name, unsigned int length,
163*69606e3fSchristos                         char *value, enum variable_origin origin,
164*69606e3fSchristos                         int recursive, struct variable_set *set,
165*69606e3fSchristos                         const struct floc *flocp)
166*69606e3fSchristos {
167*69606e3fSchristos   struct variable *v;
168*69606e3fSchristos   struct variable **var_slot;
169*69606e3fSchristos   struct variable var_key;
170*69606e3fSchristos 
171*69606e3fSchristos   if (set == NULL)
172*69606e3fSchristos     set = &global_variable_set;
173*69606e3fSchristos 
174*69606e3fSchristos   var_key.name = (char *) name;
175*69606e3fSchristos   var_key.length = length;
176*69606e3fSchristos   var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
177*69606e3fSchristos 
178*69606e3fSchristos   if (env_overrides && origin == o_env)
179*69606e3fSchristos     origin = o_env_override;
180*69606e3fSchristos 
181*69606e3fSchristos   v = *var_slot;
182*69606e3fSchristos   if (! HASH_VACANT (v))
183*69606e3fSchristos     {
184*69606e3fSchristos       if (env_overrides && v->origin == o_env)
185*69606e3fSchristos 	/* V came from in the environment.  Since it was defined
186*69606e3fSchristos 	   before the switches were parsed, it wasn't affected by -e.  */
187*69606e3fSchristos 	v->origin = o_env_override;
188*69606e3fSchristos 
189*69606e3fSchristos       /* A variable of this name is already defined.
190*69606e3fSchristos 	 If the old definition is from a stronger source
191*69606e3fSchristos 	 than this one, don't redefine it.  */
192*69606e3fSchristos       if ((int) origin >= (int) v->origin)
193*69606e3fSchristos 	{
194*69606e3fSchristos 	  if (v->value != 0)
195*69606e3fSchristos 	    free (v->value);
196*69606e3fSchristos 	  v->value = xstrdup (value);
197*69606e3fSchristos           if (flocp != 0)
198*69606e3fSchristos             v->fileinfo = *flocp;
199*69606e3fSchristos           else
200*69606e3fSchristos             v->fileinfo.filenm = 0;
201*69606e3fSchristos 	  v->origin = origin;
202*69606e3fSchristos 	  v->recursive = recursive;
203*69606e3fSchristos 	}
204*69606e3fSchristos       return v;
205*69606e3fSchristos     }
206*69606e3fSchristos 
207*69606e3fSchristos   /* Create a new variable definition and add it to the hash table.  */
208*69606e3fSchristos 
209*69606e3fSchristos   v = (struct variable *) xmalloc (sizeof (struct variable));
210*69606e3fSchristos   v->name = savestring (name, length);
211*69606e3fSchristos   v->length = length;
212*69606e3fSchristos   hash_insert_at (&set->table, v, var_slot);
213*69606e3fSchristos   v->value = xstrdup (value);
214*69606e3fSchristos   if (flocp != 0)
215*69606e3fSchristos     v->fileinfo = *flocp;
216*69606e3fSchristos   else
217*69606e3fSchristos     v->fileinfo.filenm = 0;
218*69606e3fSchristos   v->origin = origin;
219*69606e3fSchristos   v->recursive = recursive;
220*69606e3fSchristos   v->special = 0;
221*69606e3fSchristos   v->expanding = 0;
222*69606e3fSchristos   v->exp_count = 0;
223*69606e3fSchristos   v->per_target = 0;
224*69606e3fSchristos   v->append = 0;
225*69606e3fSchristos   v->export = v_default;
226*69606e3fSchristos 
227*69606e3fSchristos   v->exportable = 1;
228*69606e3fSchristos   if (*name != '_' && (*name < 'A' || *name > 'Z')
229*69606e3fSchristos       && (*name < 'a' || *name > 'z'))
230*69606e3fSchristos     v->exportable = 0;
231*69606e3fSchristos   else
232*69606e3fSchristos     {
233*69606e3fSchristos       for (++name; *name != '\0'; ++name)
234*69606e3fSchristos         if (*name != '_' && (*name < 'a' || *name > 'z')
235*69606e3fSchristos             && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
236*69606e3fSchristos           break;
237*69606e3fSchristos 
238*69606e3fSchristos       if (*name != '\0')
239*69606e3fSchristos         v->exportable = 0;
240*69606e3fSchristos     }
241*69606e3fSchristos 
242*69606e3fSchristos   return v;
243*69606e3fSchristos }
244*69606e3fSchristos 
245*69606e3fSchristos /* If the variable passed in is "special", handle its special nature.
246*69606e3fSchristos    Currently there are two such variables, both used for introspection:
247*69606e3fSchristos    .VARIABLES expands to a list of all the variables defined in this instance
248*69606e3fSchristos    of make.
249*69606e3fSchristos    .TARGETS expands to a list of all the targets defined in this
250*69606e3fSchristos    instance of make.
251*69606e3fSchristos    Returns the variable reference passed in.  */
252*69606e3fSchristos 
253*69606e3fSchristos #define EXPANSION_INCREMENT(_l)  ((((_l) / 500) + 1) * 500)
254*69606e3fSchristos 
255*69606e3fSchristos static struct variable *
handle_special_var(struct variable * var)256*69606e3fSchristos handle_special_var (struct variable *var)
257*69606e3fSchristos {
258*69606e3fSchristos   static unsigned long last_var_count = 0;
259*69606e3fSchristos 
260*69606e3fSchristos 
261*69606e3fSchristos   /* This one actually turns out to be very hard, due to the way the parser
262*69606e3fSchristos      records targets.  The way it works is that target information is collected
263*69606e3fSchristos      internally until make knows the target is completely specified.  It unitl
264*69606e3fSchristos      it sees that some new construct (a new target or variable) is defined that
265*69606e3fSchristos      it knows the previous one is done.  In short, this means that if you do
266*69606e3fSchristos      this:
267*69606e3fSchristos 
268*69606e3fSchristos        all:
269*69606e3fSchristos 
270*69606e3fSchristos        TARGS := $(.TARGETS)
271*69606e3fSchristos 
272*69606e3fSchristos      then $(TARGS) won't contain "all", because it's not until after the
273*69606e3fSchristos      variable is created that the previous target is completed.
274*69606e3fSchristos 
275*69606e3fSchristos      Changing this would be a major pain.  I think a less complex way to do it
276*69606e3fSchristos      would be to pre-define the target files as soon as the first line is
277*69606e3fSchristos      parsed, then come back and do the rest of the definition as now.  That
278*69606e3fSchristos      would allow $(.TARGETS) to be correct without a major change to the way
279*69606e3fSchristos      the parser works.
280*69606e3fSchristos 
281*69606e3fSchristos   if (streq (var->name, ".TARGETS"))
282*69606e3fSchristos     var->value = build_target_list (var->value);
283*69606e3fSchristos   else
284*69606e3fSchristos   */
285*69606e3fSchristos 
286*69606e3fSchristos   if (streq (var->name, ".VARIABLES")
287*69606e3fSchristos       && global_variable_set.table.ht_fill != last_var_count)
288*69606e3fSchristos     {
289*69606e3fSchristos       unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
290*69606e3fSchristos       unsigned long len;
291*69606e3fSchristos       char *p;
292*69606e3fSchristos       struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
293*69606e3fSchristos       struct variable **end = &vp[global_variable_set.table.ht_size];
294*69606e3fSchristos 
295*69606e3fSchristos       /* Make sure we have at least MAX bytes in the allocated buffer.  */
296*69606e3fSchristos       var->value = xrealloc (var->value, max);
297*69606e3fSchristos 
298*69606e3fSchristos       /* Walk through the hash of variables, constructing a list of names.  */
299*69606e3fSchristos       p = var->value;
300*69606e3fSchristos       len = 0;
301*69606e3fSchristos       for (; vp < end; ++vp)
302*69606e3fSchristos         if (!HASH_VACANT (*vp))
303*69606e3fSchristos           {
304*69606e3fSchristos             struct variable *v = *vp;
305*69606e3fSchristos             int l = v->length;
306*69606e3fSchristos 
307*69606e3fSchristos             len += l + 1;
308*69606e3fSchristos             if (len > max)
309*69606e3fSchristos               {
310*69606e3fSchristos                 unsigned long off = p - var->value;
311*69606e3fSchristos 
312*69606e3fSchristos                 max += EXPANSION_INCREMENT (l + 1);
313*69606e3fSchristos                 var->value = xrealloc (var->value, max);
314*69606e3fSchristos                 p = &var->value[off];
315*69606e3fSchristos               }
316*69606e3fSchristos 
317*69606e3fSchristos             bcopy (v->name, p, l);
318*69606e3fSchristos             p += l;
319*69606e3fSchristos             *(p++) = ' ';
320*69606e3fSchristos           }
321*69606e3fSchristos       *(p-1) = '\0';
322*69606e3fSchristos 
323*69606e3fSchristos       /* Remember how many variables are in our current count.  Since we never
324*69606e3fSchristos          remove variables from the list, this is a reliable way to know whether
325*69606e3fSchristos          the list is up to date or needs to be recomputed.  */
326*69606e3fSchristos 
327*69606e3fSchristos       last_var_count = global_variable_set.table.ht_fill;
328*69606e3fSchristos     }
329*69606e3fSchristos 
330*69606e3fSchristos   return var;
331*69606e3fSchristos }
332*69606e3fSchristos 
333*69606e3fSchristos 
334*69606e3fSchristos /* Lookup a variable whose name is a string starting at NAME
335*69606e3fSchristos    and with LENGTH chars.  NAME need not be null-terminated.
336*69606e3fSchristos    Returns address of the `struct variable' containing all info
337*69606e3fSchristos    on the variable, or nil if no such variable is defined.  */
338*69606e3fSchristos 
339*69606e3fSchristos struct variable *
lookup_variable(const char * name,unsigned int length)340*69606e3fSchristos lookup_variable (const char *name, unsigned int length)
341*69606e3fSchristos {
342*69606e3fSchristos   const struct variable_set_list *setlist;
343*69606e3fSchristos   struct variable var_key;
344*69606e3fSchristos 
345*69606e3fSchristos   var_key.name = (char *) name;
346*69606e3fSchristos   var_key.length = length;
347*69606e3fSchristos 
348*69606e3fSchristos   for (setlist = current_variable_set_list;
349*69606e3fSchristos        setlist != 0; setlist = setlist->next)
350*69606e3fSchristos     {
351*69606e3fSchristos       const struct variable_set *set = setlist->set;
352*69606e3fSchristos       struct variable *v;
353*69606e3fSchristos 
354*69606e3fSchristos       v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
355*69606e3fSchristos       if (v)
356*69606e3fSchristos 	return v->special ? handle_special_var (v) : v;
357*69606e3fSchristos     }
358*69606e3fSchristos 
359*69606e3fSchristos #ifdef VMS
360*69606e3fSchristos   /* since we don't read envp[] on startup, try to get the
361*69606e3fSchristos      variable via getenv() here.  */
362*69606e3fSchristos   {
363*69606e3fSchristos     char *vname = alloca (length + 1);
364*69606e3fSchristos     char *value;
365*69606e3fSchristos     strncpy (vname, name, length);
366*69606e3fSchristos     vname[length] = 0;
367*69606e3fSchristos     value = getenv (vname);
368*69606e3fSchristos     if (value != 0)
369*69606e3fSchristos       {
370*69606e3fSchristos         char *sptr;
371*69606e3fSchristos         int scnt;
372*69606e3fSchristos 
373*69606e3fSchristos         sptr = value;
374*69606e3fSchristos         scnt = 0;
375*69606e3fSchristos 
376*69606e3fSchristos         while ((sptr = strchr (sptr, '$')))
377*69606e3fSchristos           {
378*69606e3fSchristos             scnt++;
379*69606e3fSchristos             sptr++;
380*69606e3fSchristos           }
381*69606e3fSchristos 
382*69606e3fSchristos         if (scnt > 0)
383*69606e3fSchristos           {
384*69606e3fSchristos             char *nvalue;
385*69606e3fSchristos             char *nptr;
386*69606e3fSchristos 
387*69606e3fSchristos             nvalue = alloca (strlen (value) + scnt + 1);
388*69606e3fSchristos             sptr = value;
389*69606e3fSchristos             nptr = nvalue;
390*69606e3fSchristos 
391*69606e3fSchristos             while (*sptr)
392*69606e3fSchristos               {
393*69606e3fSchristos                 if (*sptr == '$')
394*69606e3fSchristos                   {
395*69606e3fSchristos                     *nptr++ = '$';
396*69606e3fSchristos                     *nptr++ = '$';
397*69606e3fSchristos                   }
398*69606e3fSchristos                 else
399*69606e3fSchristos                   {
400*69606e3fSchristos                     *nptr++ = *sptr;
401*69606e3fSchristos                   }
402*69606e3fSchristos                 sptr++;
403*69606e3fSchristos               }
404*69606e3fSchristos 
405*69606e3fSchristos             *nptr = '\0';
406*69606e3fSchristos             return define_variable (vname, length, nvalue, o_env, 1);
407*69606e3fSchristos 
408*69606e3fSchristos           }
409*69606e3fSchristos 
410*69606e3fSchristos         return define_variable (vname, length, value, o_env, 1);
411*69606e3fSchristos       }
412*69606e3fSchristos   }
413*69606e3fSchristos #endif /* VMS */
414*69606e3fSchristos 
415*69606e3fSchristos   return 0;
416*69606e3fSchristos }
417*69606e3fSchristos 
418*69606e3fSchristos /* Lookup a variable whose name is a string starting at NAME
419*69606e3fSchristos    and with LENGTH chars in set SET.  NAME need not be null-terminated.
420*69606e3fSchristos    Returns address of the `struct variable' containing all info
421*69606e3fSchristos    on the variable, or nil if no such variable is defined.  */
422*69606e3fSchristos 
423*69606e3fSchristos struct variable *
lookup_variable_in_set(const char * name,unsigned int length,const struct variable_set * set)424*69606e3fSchristos lookup_variable_in_set (const char *name, unsigned int length,
425*69606e3fSchristos                         const struct variable_set *set)
426*69606e3fSchristos {
427*69606e3fSchristos   struct variable var_key;
428*69606e3fSchristos 
429*69606e3fSchristos   var_key.name = (char *) name;
430*69606e3fSchristos   var_key.length = length;
431*69606e3fSchristos 
432*69606e3fSchristos   return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
433*69606e3fSchristos }
434*69606e3fSchristos 
435*69606e3fSchristos /* Initialize FILE's variable set list.  If FILE already has a variable set
436*69606e3fSchristos    list, the topmost variable set is left intact, but the the rest of the
437*69606e3fSchristos    chain is replaced with FILE->parent's setlist.  If FILE is a double-colon
438*69606e3fSchristos    rule, then we will use the "root" double-colon target's variable set as the
439*69606e3fSchristos    parent of FILE's variable set.
440*69606e3fSchristos 
441*69606e3fSchristos    If we're READing a makefile, don't do the pattern variable search now,
442*69606e3fSchristos    since the pattern variable might not have been defined yet.  */
443*69606e3fSchristos 
444*69606e3fSchristos void
initialize_file_variables(struct file * file,int reading)445*69606e3fSchristos initialize_file_variables (struct file *file, int reading)
446*69606e3fSchristos {
447*69606e3fSchristos   struct variable_set_list *l = file->variables;
448*69606e3fSchristos 
449*69606e3fSchristos   if (l == 0)
450*69606e3fSchristos     {
451*69606e3fSchristos       l = (struct variable_set_list *)
452*69606e3fSchristos 	xmalloc (sizeof (struct variable_set_list));
453*69606e3fSchristos       l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
454*69606e3fSchristos       hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
455*69606e3fSchristos                  variable_hash_1, variable_hash_2, variable_hash_cmp);
456*69606e3fSchristos       file->variables = l;
457*69606e3fSchristos     }
458*69606e3fSchristos 
459*69606e3fSchristos   /* If this is a double-colon, then our "parent" is the "root" target for
460*69606e3fSchristos      this double-colon rule.  Since that rule has the same name, parent,
461*69606e3fSchristos      etc. we can just use its variables as the "next" for ours.  */
462*69606e3fSchristos 
463*69606e3fSchristos   if (file->double_colon && file->double_colon != file)
464*69606e3fSchristos     {
465*69606e3fSchristos       initialize_file_variables (file->double_colon, reading);
466*69606e3fSchristos       l->next = file->double_colon->variables;
467*69606e3fSchristos       return;
468*69606e3fSchristos     }
469*69606e3fSchristos 
470*69606e3fSchristos   if (file->parent == 0)
471*69606e3fSchristos     l->next = &global_setlist;
472*69606e3fSchristos   else
473*69606e3fSchristos     {
474*69606e3fSchristos       initialize_file_variables (file->parent, reading);
475*69606e3fSchristos       l->next = file->parent->variables;
476*69606e3fSchristos     }
477*69606e3fSchristos 
478*69606e3fSchristos   /* If we're not reading makefiles and we haven't looked yet, see if
479*69606e3fSchristos      we can find pattern variables for this target.  */
480*69606e3fSchristos 
481*69606e3fSchristos   if (!reading && !file->pat_searched)
482*69606e3fSchristos     {
483*69606e3fSchristos       struct pattern_var *p;
484*69606e3fSchristos 
485*69606e3fSchristos       p = lookup_pattern_var (0, file->name);
486*69606e3fSchristos       if (p != 0)
487*69606e3fSchristos         {
488*69606e3fSchristos           struct variable_set_list *global = current_variable_set_list;
489*69606e3fSchristos 
490*69606e3fSchristos           /* We found at least one.  Set up a new variable set to accumulate
491*69606e3fSchristos              all the pattern variables that match this target.  */
492*69606e3fSchristos 
493*69606e3fSchristos           file->pat_variables = create_new_variable_set ();
494*69606e3fSchristos           current_variable_set_list = file->pat_variables;
495*69606e3fSchristos 
496*69606e3fSchristos           do
497*69606e3fSchristos             {
498*69606e3fSchristos               /* We found one, so insert it into the set.  */
499*69606e3fSchristos 
500*69606e3fSchristos               struct variable *v;
501*69606e3fSchristos 
502*69606e3fSchristos               if (p->variable.flavor == f_simple)
503*69606e3fSchristos                 {
504*69606e3fSchristos                   v = define_variable_loc (
505*69606e3fSchristos                     p->variable.name, strlen (p->variable.name),
506*69606e3fSchristos                     p->variable.value, p->variable.origin,
507*69606e3fSchristos                     0, &p->variable.fileinfo);
508*69606e3fSchristos 
509*69606e3fSchristos                   v->flavor = f_simple;
510*69606e3fSchristos                 }
511*69606e3fSchristos               else
512*69606e3fSchristos                 {
513*69606e3fSchristos                   v = do_variable_definition (
514*69606e3fSchristos                     &p->variable.fileinfo, p->variable.name,
515*69606e3fSchristos                     p->variable.value, p->variable.origin,
516*69606e3fSchristos                     p->variable.flavor, 1);
517*69606e3fSchristos                 }
518*69606e3fSchristos 
519*69606e3fSchristos               /* Also mark it as a per-target and copy export status. */
520*69606e3fSchristos               v->per_target = p->variable.per_target;
521*69606e3fSchristos               v->export = p->variable.export;
522*69606e3fSchristos             }
523*69606e3fSchristos           while ((p = lookup_pattern_var (p, file->name)) != 0);
524*69606e3fSchristos 
525*69606e3fSchristos           current_variable_set_list = global;
526*69606e3fSchristos         }
527*69606e3fSchristos       file->pat_searched = 1;
528*69606e3fSchristos     }
529*69606e3fSchristos 
530*69606e3fSchristos   /* If we have a pattern variable match, set it up.  */
531*69606e3fSchristos 
532*69606e3fSchristos   if (file->pat_variables != 0)
533*69606e3fSchristos     {
534*69606e3fSchristos       file->pat_variables->next = l->next;
535*69606e3fSchristos       l->next = file->pat_variables;
536*69606e3fSchristos     }
537*69606e3fSchristos }
538*69606e3fSchristos 
539*69606e3fSchristos /* Pop the top set off the current variable set list,
540*69606e3fSchristos    and free all its storage.  */
541*69606e3fSchristos 
542*69606e3fSchristos struct variable_set_list *
create_new_variable_set(void)543*69606e3fSchristos create_new_variable_set (void)
544*69606e3fSchristos {
545*69606e3fSchristos   register struct variable_set_list *setlist;
546*69606e3fSchristos   register struct variable_set *set;
547*69606e3fSchristos 
548*69606e3fSchristos   set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
549*69606e3fSchristos   hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
550*69606e3fSchristos 	     variable_hash_1, variable_hash_2, variable_hash_cmp);
551*69606e3fSchristos 
552*69606e3fSchristos   setlist = (struct variable_set_list *)
553*69606e3fSchristos     xmalloc (sizeof (struct variable_set_list));
554*69606e3fSchristos   setlist->set = set;
555*69606e3fSchristos   setlist->next = current_variable_set_list;
556*69606e3fSchristos 
557*69606e3fSchristos   return setlist;
558*69606e3fSchristos }
559*69606e3fSchristos 
560*69606e3fSchristos static void
free_variable_name_and_value(const void * item)561*69606e3fSchristos free_variable_name_and_value (const void *item)
562*69606e3fSchristos {
563*69606e3fSchristos   struct variable *v = (struct variable *) item;
564*69606e3fSchristos   free (v->name);
565*69606e3fSchristos   free (v->value);
566*69606e3fSchristos }
567*69606e3fSchristos 
568*69606e3fSchristos void
free_variable_set(struct variable_set_list * list)569*69606e3fSchristos free_variable_set (struct variable_set_list *list)
570*69606e3fSchristos {
571*69606e3fSchristos   hash_map (&list->set->table, free_variable_name_and_value);
572*69606e3fSchristos   hash_free (&list->set->table, 1);
573*69606e3fSchristos   free ((char *) list->set);
574*69606e3fSchristos   free ((char *) list);
575*69606e3fSchristos }
576*69606e3fSchristos 
577*69606e3fSchristos /* Create a new variable set and push it on the current setlist.
578*69606e3fSchristos    If we're pushing a global scope (that is, the current scope is the global
579*69606e3fSchristos    scope) then we need to "push" it the other way: file variable sets point
580*69606e3fSchristos    directly to the global_setlist so we need to replace that with the new one.
581*69606e3fSchristos  */
582*69606e3fSchristos 
583*69606e3fSchristos struct variable_set_list *
push_new_variable_scope(void)584*69606e3fSchristos push_new_variable_scope (void)
585*69606e3fSchristos {
586*69606e3fSchristos   current_variable_set_list = create_new_variable_set();
587*69606e3fSchristos   if (current_variable_set_list->next == &global_setlist)
588*69606e3fSchristos     {
589*69606e3fSchristos       /* It was the global, so instead of new -> &global we want to replace
590*69606e3fSchristos          &global with the new one and have &global -> new, with current still
591*69606e3fSchristos          pointing to &global  */
592*69606e3fSchristos       struct variable_set *set = current_variable_set_list->set;
593*69606e3fSchristos       current_variable_set_list->set = global_setlist.set;
594*69606e3fSchristos       global_setlist.set = set;
595*69606e3fSchristos       current_variable_set_list->next = global_setlist.next;
596*69606e3fSchristos       global_setlist.next = current_variable_set_list;
597*69606e3fSchristos       current_variable_set_list = &global_setlist;
598*69606e3fSchristos     }
599*69606e3fSchristos   return (current_variable_set_list);
600*69606e3fSchristos }
601*69606e3fSchristos 
602*69606e3fSchristos void
pop_variable_scope(void)603*69606e3fSchristos pop_variable_scope (void)
604*69606e3fSchristos {
605*69606e3fSchristos   struct variable_set_list *setlist;
606*69606e3fSchristos   struct variable_set *set;
607*69606e3fSchristos 
608*69606e3fSchristos   /* Can't call this if there's no scope to pop!  */
609*69606e3fSchristos   assert(current_variable_set_list->next != NULL);
610*69606e3fSchristos 
611*69606e3fSchristos   if (current_variable_set_list != &global_setlist)
612*69606e3fSchristos     {
613*69606e3fSchristos       /* We're not pointing to the global setlist, so pop this one.  */
614*69606e3fSchristos       setlist = current_variable_set_list;
615*69606e3fSchristos       set = setlist->set;
616*69606e3fSchristos       current_variable_set_list = setlist->next;
617*69606e3fSchristos     }
618*69606e3fSchristos   else
619*69606e3fSchristos     {
620*69606e3fSchristos       /* This set is the one in the global_setlist, but there is another global
621*69606e3fSchristos          set beyond that.  We want to copy that set to global_setlist, then
622*69606e3fSchristos          delete what used to be in global_setlist.  */
623*69606e3fSchristos       setlist = global_setlist.next;
624*69606e3fSchristos       set = global_setlist.set;
625*69606e3fSchristos       global_setlist.set = setlist->set;
626*69606e3fSchristos       global_setlist.next = setlist->next;
627*69606e3fSchristos     }
628*69606e3fSchristos 
629*69606e3fSchristos   /* Free the one we no longer need.  */
630*69606e3fSchristos   free ((char *) setlist);
631*69606e3fSchristos   hash_map (&set->table, free_variable_name_and_value);
632*69606e3fSchristos   hash_free (&set->table, 1);
633*69606e3fSchristos   free ((char *) set);
634*69606e3fSchristos }
635*69606e3fSchristos 
636*69606e3fSchristos /* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET.  */
637*69606e3fSchristos 
638*69606e3fSchristos static void
merge_variable_sets(struct variable_set * to_set,struct variable_set * from_set)639*69606e3fSchristos merge_variable_sets (struct variable_set *to_set,
640*69606e3fSchristos                      struct variable_set *from_set)
641*69606e3fSchristos {
642*69606e3fSchristos   struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
643*69606e3fSchristos   struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
644*69606e3fSchristos 
645*69606e3fSchristos   for ( ; from_var_slot < from_var_end; from_var_slot++)
646*69606e3fSchristos     if (! HASH_VACANT (*from_var_slot))
647*69606e3fSchristos       {
648*69606e3fSchristos 	struct variable *from_var = *from_var_slot;
649*69606e3fSchristos 	struct variable **to_var_slot
650*69606e3fSchristos 	  = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
651*69606e3fSchristos 	if (HASH_VACANT (*to_var_slot))
652*69606e3fSchristos 	  hash_insert_at (&to_set->table, from_var, to_var_slot);
653*69606e3fSchristos 	else
654*69606e3fSchristos 	  {
655*69606e3fSchristos 	    /* GKM FIXME: delete in from_set->table */
656*69606e3fSchristos 	    free (from_var->value);
657*69606e3fSchristos 	    free (from_var);
658*69606e3fSchristos 	  }
659*69606e3fSchristos       }
660*69606e3fSchristos }
661*69606e3fSchristos 
662*69606e3fSchristos /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1.  */
663*69606e3fSchristos 
664*69606e3fSchristos void
merge_variable_set_lists(struct variable_set_list ** setlist0,struct variable_set_list * setlist1)665*69606e3fSchristos merge_variable_set_lists (struct variable_set_list **setlist0,
666*69606e3fSchristos                           struct variable_set_list *setlist1)
667*69606e3fSchristos {
668*69606e3fSchristos   struct variable_set_list *to = *setlist0;
669*69606e3fSchristos   struct variable_set_list *last0 = 0;
670*69606e3fSchristos 
671*69606e3fSchristos   /* If there's nothing to merge, stop now.  */
672*69606e3fSchristos   if (!setlist1)
673*69606e3fSchristos     return;
674*69606e3fSchristos 
675*69606e3fSchristos   /* This loop relies on the fact that all setlists terminate with the global
676*69606e3fSchristos      setlist (before NULL).  If that's not true, arguably we SHOULD die.  */
677*69606e3fSchristos   if (to)
678*69606e3fSchristos     while (setlist1 != &global_setlist && to != &global_setlist)
679*69606e3fSchristos       {
680*69606e3fSchristos         struct variable_set_list *from = setlist1;
681*69606e3fSchristos         setlist1 = setlist1->next;
682*69606e3fSchristos 
683*69606e3fSchristos         merge_variable_sets (to->set, from->set);
684*69606e3fSchristos 
685*69606e3fSchristos         last0 = to;
686*69606e3fSchristos         to = to->next;
687*69606e3fSchristos       }
688*69606e3fSchristos 
689*69606e3fSchristos   if (setlist1 != &global_setlist)
690*69606e3fSchristos     {
691*69606e3fSchristos       if (last0 == 0)
692*69606e3fSchristos 	*setlist0 = setlist1;
693*69606e3fSchristos       else
694*69606e3fSchristos 	last0->next = setlist1;
695*69606e3fSchristos     }
696*69606e3fSchristos }
697*69606e3fSchristos 
698*69606e3fSchristos /* Define the automatic variables, and record the addresses
699*69606e3fSchristos    of their structures so we can change their values quickly.  */
700*69606e3fSchristos 
701*69606e3fSchristos void
define_automatic_variables(void)702*69606e3fSchristos define_automatic_variables (void)
703*69606e3fSchristos {
704*69606e3fSchristos #if defined(WINDOWS32) || defined(__EMX__)
705*69606e3fSchristos   extern char* default_shell;
706*69606e3fSchristos #else
707*69606e3fSchristos   extern char default_shell[];
708*69606e3fSchristos #endif
709*69606e3fSchristos   register struct variable *v;
710*69606e3fSchristos   char buf[200];
711*69606e3fSchristos 
712*69606e3fSchristos   sprintf (buf, "%u", makelevel);
713*69606e3fSchristos   (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
714*69606e3fSchristos 
715*69606e3fSchristos   sprintf (buf, "%s%s%s",
716*69606e3fSchristos 	   version_string,
717*69606e3fSchristos 	   (remote_description == 0 || remote_description[0] == '\0')
718*69606e3fSchristos 	   ? "" : "-",
719*69606e3fSchristos 	   (remote_description == 0 || remote_description[0] == '\0')
720*69606e3fSchristos 	   ? "" : remote_description);
721*69606e3fSchristos   (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
722*69606e3fSchristos 
723*69606e3fSchristos #ifdef  __MSDOS__
724*69606e3fSchristos   /* Allow to specify a special shell just for Make,
725*69606e3fSchristos      and use $COMSPEC as the default $SHELL when appropriate.  */
726*69606e3fSchristos   {
727*69606e3fSchristos     static char shell_str[] = "SHELL";
728*69606e3fSchristos     const int shlen = sizeof (shell_str) - 1;
729*69606e3fSchristos     struct variable *mshp = lookup_variable ("MAKESHELL", 9);
730*69606e3fSchristos     struct variable *comp = lookup_variable ("COMSPEC", 7);
731*69606e3fSchristos 
732*69606e3fSchristos     /* Make $MAKESHELL override $SHELL even if -e is in effect.  */
733*69606e3fSchristos     if (mshp)
734*69606e3fSchristos       (void) define_variable (shell_str, shlen,
735*69606e3fSchristos 			      mshp->value, o_env_override, 0);
736*69606e3fSchristos     else if (comp)
737*69606e3fSchristos       {
738*69606e3fSchristos 	/* $COMSPEC shouldn't override $SHELL.  */
739*69606e3fSchristos 	struct variable *shp = lookup_variable (shell_str, shlen);
740*69606e3fSchristos 
741*69606e3fSchristos 	if (!shp)
742*69606e3fSchristos 	  (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
743*69606e3fSchristos       }
744*69606e3fSchristos   }
745*69606e3fSchristos #elif defined(__EMX__)
746*69606e3fSchristos   {
747*69606e3fSchristos     static char shell_str[] = "SHELL";
748*69606e3fSchristos     const int shlen = sizeof (shell_str) - 1;
749*69606e3fSchristos     struct variable *shell = lookup_variable (shell_str, shlen);
750*69606e3fSchristos     struct variable *replace = lookup_variable ("MAKESHELL", 9);
751*69606e3fSchristos 
752*69606e3fSchristos     /* if $MAKESHELL is defined in the environment assume o_env_override */
753*69606e3fSchristos     if (replace && *replace->value && replace->origin == o_env)
754*69606e3fSchristos       replace->origin = o_env_override;
755*69606e3fSchristos 
756*69606e3fSchristos     /* if $MAKESHELL is not defined use $SHELL but only if the variable
757*69606e3fSchristos        did not come from the environment */
758*69606e3fSchristos     if (!replace || !*replace->value)
759*69606e3fSchristos       if (shell && *shell->value && (shell->origin == o_env
760*69606e3fSchristos 	  || shell->origin == o_env_override))
761*69606e3fSchristos 	{
762*69606e3fSchristos 	  /* overwrite whatever we got from the environment */
763*69606e3fSchristos 	  free(shell->value);
764*69606e3fSchristos 	  shell->value = xstrdup (default_shell);
765*69606e3fSchristos 	  shell->origin = o_default;
766*69606e3fSchristos 	}
767*69606e3fSchristos 
768*69606e3fSchristos     /* Some people do not like cmd to be used as the default
769*69606e3fSchristos        if $SHELL is not defined in the Makefile.
770*69606e3fSchristos        With -DNO_CMD_DEFAULT you can turn off this behaviour */
771*69606e3fSchristos # ifndef NO_CMD_DEFAULT
772*69606e3fSchristos     /* otherwise use $COMSPEC */
773*69606e3fSchristos     if (!replace || !*replace->value)
774*69606e3fSchristos       replace = lookup_variable ("COMSPEC", 7);
775*69606e3fSchristos 
776*69606e3fSchristos     /* otherwise use $OS2_SHELL */
777*69606e3fSchristos     if (!replace || !*replace->value)
778*69606e3fSchristos       replace = lookup_variable ("OS2_SHELL", 9);
779*69606e3fSchristos # else
780*69606e3fSchristos #   warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
781*69606e3fSchristos # endif
782*69606e3fSchristos 
783*69606e3fSchristos     if (replace && *replace->value)
784*69606e3fSchristos       /* overwrite $SHELL */
785*69606e3fSchristos       (void) define_variable (shell_str, shlen, replace->value,
786*69606e3fSchristos 			      replace->origin, 0);
787*69606e3fSchristos     else
788*69606e3fSchristos       /* provide a definition if there is none */
789*69606e3fSchristos       (void) define_variable (shell_str, shlen, default_shell,
790*69606e3fSchristos 			      o_default, 0);
791*69606e3fSchristos   }
792*69606e3fSchristos 
793*69606e3fSchristos #endif
794*69606e3fSchristos 
795*69606e3fSchristos   /* This won't override any definition, but it will provide one if there
796*69606e3fSchristos      isn't one there.  */
797*69606e3fSchristos   v = define_variable ("SHELL", 5, default_shell, o_default, 0);
798*69606e3fSchristos 
799*69606e3fSchristos   /* On MSDOS we do use SHELL from environment, since it isn't a standard
800*69606e3fSchristos      environment variable on MSDOS, so whoever sets it, does that on purpose.
801*69606e3fSchristos      On OS/2 we do not use SHELL from environment but we have already handled
802*69606e3fSchristos      that problem above. */
803*69606e3fSchristos #if !defined(__MSDOS__) && !defined(__EMX__)
804*69606e3fSchristos   /* Don't let SHELL come from the environment.  */
805*69606e3fSchristos   if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
806*69606e3fSchristos     {
807*69606e3fSchristos       free (v->value);
808*69606e3fSchristos       v->origin = o_file;
809*69606e3fSchristos       v->value = xstrdup (default_shell);
810*69606e3fSchristos     }
811*69606e3fSchristos #endif
812*69606e3fSchristos 
813*69606e3fSchristos   /* Make sure MAKEFILES gets exported if it is set.  */
814*69606e3fSchristos   v = define_variable ("MAKEFILES", 9, "", o_default, 0);
815*69606e3fSchristos   v->export = v_ifset;
816*69606e3fSchristos 
817*69606e3fSchristos   /* Define the magic D and F variables in terms of
818*69606e3fSchristos      the automatic variables they are variations of.  */
819*69606e3fSchristos 
820*69606e3fSchristos #ifdef VMS
821*69606e3fSchristos   define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
822*69606e3fSchristos   define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
823*69606e3fSchristos   define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
824*69606e3fSchristos   define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
825*69606e3fSchristos   define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
826*69606e3fSchristos   define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
827*69606e3fSchristos   define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
828*69606e3fSchristos #else
829*69606e3fSchristos   define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
830*69606e3fSchristos   define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
831*69606e3fSchristos   define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
832*69606e3fSchristos   define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
833*69606e3fSchristos   define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
834*69606e3fSchristos   define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
835*69606e3fSchristos   define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
836*69606e3fSchristos #endif
837*69606e3fSchristos   define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
838*69606e3fSchristos   define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
839*69606e3fSchristos   define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
840*69606e3fSchristos   define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
841*69606e3fSchristos   define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
842*69606e3fSchristos   define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
843*69606e3fSchristos   define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
844*69606e3fSchristos }
845*69606e3fSchristos 
846*69606e3fSchristos int export_all_variables;
847*69606e3fSchristos 
848*69606e3fSchristos /* Create a new environment for FILE's commands.
849*69606e3fSchristos    If FILE is nil, this is for the `shell' function.
850*69606e3fSchristos    The child's MAKELEVEL variable is incremented.  */
851*69606e3fSchristos 
852*69606e3fSchristos char **
target_environment(struct file * file)853*69606e3fSchristos target_environment (struct file *file)
854*69606e3fSchristos {
855*69606e3fSchristos   struct variable_set_list *set_list;
856*69606e3fSchristos   register struct variable_set_list *s;
857*69606e3fSchristos   struct hash_table table;
858*69606e3fSchristos   struct variable **v_slot;
859*69606e3fSchristos   struct variable **v_end;
860*69606e3fSchristos   struct variable makelevel_key;
861*69606e3fSchristos   char **result_0;
862*69606e3fSchristos   char **result;
863*69606e3fSchristos 
864*69606e3fSchristos   if (file == 0)
865*69606e3fSchristos     set_list = current_variable_set_list;
866*69606e3fSchristos   else
867*69606e3fSchristos     set_list = file->variables;
868*69606e3fSchristos 
869*69606e3fSchristos   hash_init (&table, VARIABLE_BUCKETS,
870*69606e3fSchristos 	     variable_hash_1, variable_hash_2, variable_hash_cmp);
871*69606e3fSchristos 
872*69606e3fSchristos   /* Run through all the variable sets in the list,
873*69606e3fSchristos      accumulating variables in TABLE.  */
874*69606e3fSchristos   for (s = set_list; s != 0; s = s->next)
875*69606e3fSchristos     {
876*69606e3fSchristos       struct variable_set *set = s->set;
877*69606e3fSchristos       v_slot = (struct variable **) set->table.ht_vec;
878*69606e3fSchristos       v_end = v_slot + set->table.ht_size;
879*69606e3fSchristos       for ( ; v_slot < v_end; v_slot++)
880*69606e3fSchristos 	if (! HASH_VACANT (*v_slot))
881*69606e3fSchristos 	  {
882*69606e3fSchristos 	    struct variable **new_slot;
883*69606e3fSchristos 	    struct variable *v = *v_slot;
884*69606e3fSchristos 
885*69606e3fSchristos 	    /* If this is a per-target variable and it hasn't been touched
886*69606e3fSchristos 	       already then look up the global version and take its export
887*69606e3fSchristos 	       value.  */
888*69606e3fSchristos 	    if (v->per_target && v->export == v_default)
889*69606e3fSchristos 	      {
890*69606e3fSchristos 		struct variable *gv;
891*69606e3fSchristos 
892*69606e3fSchristos 		gv = lookup_variable_in_set (v->name, strlen(v->name),
893*69606e3fSchristos                                              &global_variable_set);
894*69606e3fSchristos 		if (gv)
895*69606e3fSchristos 		  v->export = gv->export;
896*69606e3fSchristos 	      }
897*69606e3fSchristos 
898*69606e3fSchristos 	    switch (v->export)
899*69606e3fSchristos 	      {
900*69606e3fSchristos 	      case v_default:
901*69606e3fSchristos 		if (v->origin == o_default || v->origin == o_automatic)
902*69606e3fSchristos 		  /* Only export default variables by explicit request.  */
903*69606e3fSchristos 		  continue;
904*69606e3fSchristos 
905*69606e3fSchristos                 /* The variable doesn't have a name that can be exported.  */
906*69606e3fSchristos                 if (! v->exportable)
907*69606e3fSchristos                   continue;
908*69606e3fSchristos 
909*69606e3fSchristos 		if (! export_all_variables
910*69606e3fSchristos 		    && v->origin != o_command
911*69606e3fSchristos 		    && v->origin != o_env && v->origin != o_env_override)
912*69606e3fSchristos 		  continue;
913*69606e3fSchristos 		break;
914*69606e3fSchristos 
915*69606e3fSchristos 	      case v_export:
916*69606e3fSchristos 		break;
917*69606e3fSchristos 
918*69606e3fSchristos 	      case v_noexport:
919*69606e3fSchristos                 /* If this is the SHELL variable and it's not exported, then
920*69606e3fSchristos                    add the value from our original environment.  */
921*69606e3fSchristos                 if (streq (v->name, "SHELL"))
922*69606e3fSchristos                   {
923*69606e3fSchristos                     extern struct variable shell_var;
924*69606e3fSchristos                     v = &shell_var;
925*69606e3fSchristos                     break;
926*69606e3fSchristos                   }
927*69606e3fSchristos                 continue;
928*69606e3fSchristos 
929*69606e3fSchristos 	      case v_ifset:
930*69606e3fSchristos 		if (v->origin == o_default)
931*69606e3fSchristos 		  continue;
932*69606e3fSchristos 		break;
933*69606e3fSchristos 	      }
934*69606e3fSchristos 
935*69606e3fSchristos 	    new_slot = (struct variable **) hash_find_slot (&table, v);
936*69606e3fSchristos 	    if (HASH_VACANT (*new_slot))
937*69606e3fSchristos 	      hash_insert_at (&table, v, new_slot);
938*69606e3fSchristos 	  }
939*69606e3fSchristos     }
940*69606e3fSchristos 
941*69606e3fSchristos   makelevel_key.name = MAKELEVEL_NAME;
942*69606e3fSchristos   makelevel_key.length = MAKELEVEL_LENGTH;
943*69606e3fSchristos   hash_delete (&table, &makelevel_key);
944*69606e3fSchristos 
945*69606e3fSchristos   result = result_0 = (char **) xmalloc ((table.ht_fill + 2) * sizeof (char *));
946*69606e3fSchristos 
947*69606e3fSchristos   v_slot = (struct variable **) table.ht_vec;
948*69606e3fSchristos   v_end = v_slot + table.ht_size;
949*69606e3fSchristos   for ( ; v_slot < v_end; v_slot++)
950*69606e3fSchristos     if (! HASH_VACANT (*v_slot))
951*69606e3fSchristos       {
952*69606e3fSchristos 	struct variable *v = *v_slot;
953*69606e3fSchristos 
954*69606e3fSchristos 	/* If V is recursively expanded and didn't come from the environment,
955*69606e3fSchristos 	   expand its value.  If it came from the environment, it should
956*69606e3fSchristos 	   go back into the environment unchanged.  */
957*69606e3fSchristos 	if (v->recursive
958*69606e3fSchristos 	    && v->origin != o_env && v->origin != o_env_override)
959*69606e3fSchristos 	  {
960*69606e3fSchristos 	    char *value = recursively_expand_for_file (v, file);
961*69606e3fSchristos #ifdef WINDOWS32
962*69606e3fSchristos 	    if (strcmp(v->name, "Path") == 0 ||
963*69606e3fSchristos 		strcmp(v->name, "PATH") == 0)
964*69606e3fSchristos 	      convert_Path_to_windows32(value, ';');
965*69606e3fSchristos #endif
966*69606e3fSchristos 	    *result++ = concat (v->name, "=", value);
967*69606e3fSchristos 	    free (value);
968*69606e3fSchristos 	  }
969*69606e3fSchristos 	else
970*69606e3fSchristos 	  {
971*69606e3fSchristos #ifdef WINDOWS32
972*69606e3fSchristos             if (strcmp(v->name, "Path") == 0 ||
973*69606e3fSchristos                 strcmp(v->name, "PATH") == 0)
974*69606e3fSchristos               convert_Path_to_windows32(v->value, ';');
975*69606e3fSchristos #endif
976*69606e3fSchristos 	    *result++ = concat (v->name, "=", v->value);
977*69606e3fSchristos 	  }
978*69606e3fSchristos       }
979*69606e3fSchristos 
980*69606e3fSchristos   *result = (char *) xmalloc (100);
981*69606e3fSchristos   (void) sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
982*69606e3fSchristos   *++result = 0;
983*69606e3fSchristos 
984*69606e3fSchristos   hash_free (&table, 0);
985*69606e3fSchristos 
986*69606e3fSchristos   return result_0;
987*69606e3fSchristos }
988*69606e3fSchristos 
989*69606e3fSchristos /* Given a variable, a value, and a flavor, define the variable.
990*69606e3fSchristos    See the try_variable_definition() function for details on the parameters. */
991*69606e3fSchristos 
992*69606e3fSchristos struct variable *
do_variable_definition(const struct floc * flocp,const char * varname,char * value,enum variable_origin origin,enum variable_flavor flavor,int target_var)993*69606e3fSchristos do_variable_definition (const struct floc *flocp, const char *varname,
994*69606e3fSchristos                         char *value, enum variable_origin origin,
995*69606e3fSchristos                         enum variable_flavor flavor, int target_var)
996*69606e3fSchristos {
997*69606e3fSchristos   char *p, *alloc_value = NULL;
998*69606e3fSchristos   struct variable *v;
999*69606e3fSchristos   int append = 0;
1000*69606e3fSchristos   int conditional = 0;
1001*69606e3fSchristos 
1002*69606e3fSchristos   /* Calculate the variable's new value in VALUE.  */
1003*69606e3fSchristos 
1004*69606e3fSchristos   switch (flavor)
1005*69606e3fSchristos     {
1006*69606e3fSchristos     default:
1007*69606e3fSchristos     case f_bogus:
1008*69606e3fSchristos       /* Should not be possible.  */
1009*69606e3fSchristos       abort ();
1010*69606e3fSchristos     case f_simple:
1011*69606e3fSchristos       /* A simple variable definition "var := value".  Expand the value.
1012*69606e3fSchristos          We have to allocate memory since otherwise it'll clobber the
1013*69606e3fSchristos 	 variable buffer, and we may still need that if we're looking at a
1014*69606e3fSchristos          target-specific variable.  */
1015*69606e3fSchristos       p = alloc_value = allocated_variable_expand (value);
1016*69606e3fSchristos       break;
1017*69606e3fSchristos     case f_conditional:
1018*69606e3fSchristos       /* A conditional variable definition "var ?= value".
1019*69606e3fSchristos          The value is set IFF the variable is not defined yet. */
1020*69606e3fSchristos       v = lookup_variable (varname, strlen (varname));
1021*69606e3fSchristos       if (v)
1022*69606e3fSchristos         return v;
1023*69606e3fSchristos 
1024*69606e3fSchristos       conditional = 1;
1025*69606e3fSchristos       flavor = f_recursive;
1026*69606e3fSchristos       /* FALLTHROUGH */
1027*69606e3fSchristos     case f_recursive:
1028*69606e3fSchristos       /* A recursive variable definition "var = value".
1029*69606e3fSchristos 	 The value is used verbatim.  */
1030*69606e3fSchristos       p = value;
1031*69606e3fSchristos       break;
1032*69606e3fSchristos     case f_append:
1033*69606e3fSchristos       {
1034*69606e3fSchristos         /* If we have += but we're in a target variable context, we want to
1035*69606e3fSchristos            append only with other variables in the context of this target.  */
1036*69606e3fSchristos         if (target_var)
1037*69606e3fSchristos           {
1038*69606e3fSchristos             append = 1;
1039*69606e3fSchristos             v = lookup_variable_in_set (varname, strlen (varname),
1040*69606e3fSchristos                                         current_variable_set_list->set);
1041*69606e3fSchristos 
1042*69606e3fSchristos             /* Don't append from the global set if a previous non-appending
1043*69606e3fSchristos                target-specific variable definition exists. */
1044*69606e3fSchristos             if (v && !v->append)
1045*69606e3fSchristos               append = 0;
1046*69606e3fSchristos           }
1047*69606e3fSchristos         else
1048*69606e3fSchristos           v = lookup_variable (varname, strlen (varname));
1049*69606e3fSchristos 
1050*69606e3fSchristos         if (v == 0)
1051*69606e3fSchristos           {
1052*69606e3fSchristos             /* There was no old value.
1053*69606e3fSchristos                This becomes a normal recursive definition.  */
1054*69606e3fSchristos             p = value;
1055*69606e3fSchristos             flavor = f_recursive;
1056*69606e3fSchristos           }
1057*69606e3fSchristos         else
1058*69606e3fSchristos           {
1059*69606e3fSchristos             /* Paste the old and new values together in VALUE.  */
1060*69606e3fSchristos 
1061*69606e3fSchristos             unsigned int oldlen, vallen;
1062*69606e3fSchristos             char *val;
1063*69606e3fSchristos 
1064*69606e3fSchristos             val = value;
1065*69606e3fSchristos             if (v->recursive)
1066*69606e3fSchristos               /* The previous definition of the variable was recursive.
1067*69606e3fSchristos                  The new value is the unexpanded old and new values. */
1068*69606e3fSchristos               flavor = f_recursive;
1069*69606e3fSchristos             else
1070*69606e3fSchristos               /* The previous definition of the variable was simple.
1071*69606e3fSchristos                  The new value comes from the old value, which was expanded
1072*69606e3fSchristos                  when it was set; and from the expanded new value.  Allocate
1073*69606e3fSchristos                  memory for the expansion as we may still need the rest of the
1074*69606e3fSchristos                  buffer if we're looking at a target-specific variable.  */
1075*69606e3fSchristos               val = alloc_value = allocated_variable_expand (val);
1076*69606e3fSchristos 
1077*69606e3fSchristos             oldlen = strlen (v->value);
1078*69606e3fSchristos             vallen = strlen (val);
1079*69606e3fSchristos             p = (char *) alloca (oldlen + 1 + vallen + 1);
1080*69606e3fSchristos             bcopy (v->value, p, oldlen);
1081*69606e3fSchristos             p[oldlen] = ' ';
1082*69606e3fSchristos             bcopy (val, &p[oldlen + 1], vallen + 1);
1083*69606e3fSchristos           }
1084*69606e3fSchristos       }
1085*69606e3fSchristos     }
1086*69606e3fSchristos 
1087*69606e3fSchristos #ifdef __MSDOS__
1088*69606e3fSchristos   /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1089*69606e3fSchristos      non-Unix systems don't conform to this default configuration (in
1090*69606e3fSchristos      fact, most of them don't even have `/bin').  On the other hand,
1091*69606e3fSchristos      $SHELL in the environment, if set, points to the real pathname of
1092*69606e3fSchristos      the shell.
1093*69606e3fSchristos      Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1094*69606e3fSchristos      the Makefile override $SHELL from the environment.  But first, we
1095*69606e3fSchristos      look for the basename of the shell in the directory where SHELL=
1096*69606e3fSchristos      points, and along the $PATH; if it is found in any of these places,
1097*69606e3fSchristos      we define $SHELL to be the actual pathname of the shell.  Thus, if
1098*69606e3fSchristos      you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1099*69606e3fSchristos      your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1100*69606e3fSchristos      defining SHELL to be "d:/unix/bash.exe".  */
1101*69606e3fSchristos   if ((origin == o_file || origin == o_override)
1102*69606e3fSchristos       && strcmp (varname, "SHELL") == 0)
1103*69606e3fSchristos     {
1104*69606e3fSchristos       PATH_VAR (shellpath);
1105*69606e3fSchristos       extern char * __dosexec_find_on_path (const char *, char *[], char *);
1106*69606e3fSchristos 
1107*69606e3fSchristos       /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc.  */
1108*69606e3fSchristos       if (__dosexec_find_on_path (p, (char **)0, shellpath))
1109*69606e3fSchristos 	{
1110*69606e3fSchristos 	  char *p;
1111*69606e3fSchristos 
1112*69606e3fSchristos 	  for (p = shellpath; *p; p++)
1113*69606e3fSchristos 	    {
1114*69606e3fSchristos 	      if (*p == '\\')
1115*69606e3fSchristos 		*p = '/';
1116*69606e3fSchristos 	    }
1117*69606e3fSchristos 	  v = define_variable_loc (varname, strlen (varname),
1118*69606e3fSchristos                                    shellpath, origin, flavor == f_recursive,
1119*69606e3fSchristos                                    flocp);
1120*69606e3fSchristos 	}
1121*69606e3fSchristos       else
1122*69606e3fSchristos 	{
1123*69606e3fSchristos 	  char *shellbase, *bslash;
1124*69606e3fSchristos 	  struct variable *pathv = lookup_variable ("PATH", 4);
1125*69606e3fSchristos 	  char *path_string;
1126*69606e3fSchristos 	  char *fake_env[2];
1127*69606e3fSchristos 	  size_t pathlen = 0;
1128*69606e3fSchristos 
1129*69606e3fSchristos 	  shellbase = strrchr (p, '/');
1130*69606e3fSchristos 	  bslash = strrchr (p, '\\');
1131*69606e3fSchristos 	  if (!shellbase || bslash > shellbase)
1132*69606e3fSchristos 	    shellbase = bslash;
1133*69606e3fSchristos 	  if (!shellbase && p[1] == ':')
1134*69606e3fSchristos 	    shellbase = p + 1;
1135*69606e3fSchristos 	  if (shellbase)
1136*69606e3fSchristos 	    shellbase++;
1137*69606e3fSchristos 	  else
1138*69606e3fSchristos 	    shellbase = p;
1139*69606e3fSchristos 
1140*69606e3fSchristos 	  /* Search for the basename of the shell (with standard
1141*69606e3fSchristos 	     executable extensions) along the $PATH.  */
1142*69606e3fSchristos 	  if (pathv)
1143*69606e3fSchristos 	    pathlen = strlen (pathv->value);
1144*69606e3fSchristos 	  path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
1145*69606e3fSchristos 	  /* On MSDOS, current directory is considered as part of $PATH.  */
1146*69606e3fSchristos 	  sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1147*69606e3fSchristos 	  fake_env[0] = path_string;
1148*69606e3fSchristos 	  fake_env[1] = (char *)0;
1149*69606e3fSchristos 	  if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1150*69606e3fSchristos 	    {
1151*69606e3fSchristos 	      char *p;
1152*69606e3fSchristos 
1153*69606e3fSchristos 	      for (p = shellpath; *p; p++)
1154*69606e3fSchristos 		{
1155*69606e3fSchristos 		  if (*p == '\\')
1156*69606e3fSchristos 		    *p = '/';
1157*69606e3fSchristos 		}
1158*69606e3fSchristos 	      v = define_variable_loc (varname, strlen (varname),
1159*69606e3fSchristos                                        shellpath, origin,
1160*69606e3fSchristos                                        flavor == f_recursive, flocp);
1161*69606e3fSchristos 	    }
1162*69606e3fSchristos 	  else
1163*69606e3fSchristos 	    v = lookup_variable (varname, strlen (varname));
1164*69606e3fSchristos 
1165*69606e3fSchristos 	  free (path_string);
1166*69606e3fSchristos 	}
1167*69606e3fSchristos     }
1168*69606e3fSchristos   else
1169*69606e3fSchristos #endif /* __MSDOS__ */
1170*69606e3fSchristos #ifdef WINDOWS32
1171*69606e3fSchristos   if ((origin == o_file || origin == o_override || origin == o_command)
1172*69606e3fSchristos       && streq (varname, "SHELL"))
1173*69606e3fSchristos     {
1174*69606e3fSchristos       extern char *default_shell;
1175*69606e3fSchristos 
1176*69606e3fSchristos       /* Call shell locator function. If it returns TRUE, then
1177*69606e3fSchristos 	 set no_default_sh_exe to indicate sh was found and
1178*69606e3fSchristos          set new value for SHELL variable.  */
1179*69606e3fSchristos 
1180*69606e3fSchristos       if (find_and_set_default_shell (p))
1181*69606e3fSchristos         {
1182*69606e3fSchristos           v = define_variable_in_set (varname, strlen (varname), default_shell,
1183*69606e3fSchristos                                       origin, flavor == f_recursive,
1184*69606e3fSchristos                                       (target_var
1185*69606e3fSchristos                                        ? current_variable_set_list->set
1186*69606e3fSchristos                                        : NULL),
1187*69606e3fSchristos                                       flocp);
1188*69606e3fSchristos           no_default_sh_exe = 0;
1189*69606e3fSchristos         }
1190*69606e3fSchristos       else
1191*69606e3fSchristos         v = lookup_variable (varname, strlen (varname));
1192*69606e3fSchristos     }
1193*69606e3fSchristos   else
1194*69606e3fSchristos #endif
1195*69606e3fSchristos 
1196*69606e3fSchristos   /* If we are defining variables inside an $(eval ...), we might have a
1197*69606e3fSchristos      different variable context pushed, not the global context (maybe we're
1198*69606e3fSchristos      inside a $(call ...) or something.  Since this function is only ever
1199*69606e3fSchristos      invoked in places where we want to define globally visible variables,
1200*69606e3fSchristos      make sure we define this variable in the global set.  */
1201*69606e3fSchristos 
1202*69606e3fSchristos   v = define_variable_in_set (varname, strlen (varname), p,
1203*69606e3fSchristos                               origin, flavor == f_recursive,
1204*69606e3fSchristos                               (target_var
1205*69606e3fSchristos                                ? current_variable_set_list->set : NULL),
1206*69606e3fSchristos                               flocp);
1207*69606e3fSchristos   v->append = append;
1208*69606e3fSchristos   v->conditional = conditional;
1209*69606e3fSchristos 
1210*69606e3fSchristos   if (alloc_value)
1211*69606e3fSchristos     free (alloc_value);
1212*69606e3fSchristos 
1213*69606e3fSchristos   return v;
1214*69606e3fSchristos }
1215*69606e3fSchristos 
1216*69606e3fSchristos /* Try to interpret LINE (a null-terminated string) as a variable definition.
1217*69606e3fSchristos 
1218*69606e3fSchristos    ORIGIN may be o_file, o_override, o_env, o_env_override,
1219*69606e3fSchristos    or o_command specifying that the variable definition comes
1220*69606e3fSchristos    from a makefile, an override directive, the environment with
1221*69606e3fSchristos    or without the -e switch, or the command line.
1222*69606e3fSchristos 
1223*69606e3fSchristos    See the comments for parse_variable_definition().
1224*69606e3fSchristos 
1225*69606e3fSchristos    If LINE was recognized as a variable definition, a pointer to its `struct
1226*69606e3fSchristos    variable' is returned.  If LINE is not a variable definition, NULL is
1227*69606e3fSchristos    returned.  */
1228*69606e3fSchristos 
1229*69606e3fSchristos struct variable *
parse_variable_definition(struct variable * v,char * line)1230*69606e3fSchristos parse_variable_definition (struct variable *v, char *line)
1231*69606e3fSchristos {
1232*69606e3fSchristos   register int c;
1233*69606e3fSchristos   register char *p = line;
1234*69606e3fSchristos   register char *beg;
1235*69606e3fSchristos   register char *end;
1236*69606e3fSchristos   enum variable_flavor flavor = f_bogus;
1237*69606e3fSchristos   char *name;
1238*69606e3fSchristos 
1239*69606e3fSchristos   while (1)
1240*69606e3fSchristos     {
1241*69606e3fSchristos       c = *p++;
1242*69606e3fSchristos       if (c == '\0' || c == '#')
1243*69606e3fSchristos 	return 0;
1244*69606e3fSchristos       if (c == '=')
1245*69606e3fSchristos 	{
1246*69606e3fSchristos 	  end = p - 1;
1247*69606e3fSchristos 	  flavor = f_recursive;
1248*69606e3fSchristos 	  break;
1249*69606e3fSchristos 	}
1250*69606e3fSchristos       else if (c == ':')
1251*69606e3fSchristos 	if (*p == '=')
1252*69606e3fSchristos 	  {
1253*69606e3fSchristos 	    end = p++ - 1;
1254*69606e3fSchristos 	    flavor = f_simple;
1255*69606e3fSchristos 	    break;
1256*69606e3fSchristos 	  }
1257*69606e3fSchristos 	else
1258*69606e3fSchristos 	  /* A colon other than := is a rule line, not a variable defn.  */
1259*69606e3fSchristos 	  return 0;
1260*69606e3fSchristos       else if (c == '+' && *p == '=')
1261*69606e3fSchristos 	{
1262*69606e3fSchristos 	  end = p++ - 1;
1263*69606e3fSchristos 	  flavor = f_append;
1264*69606e3fSchristos 	  break;
1265*69606e3fSchristos 	}
1266*69606e3fSchristos       else if (c == '?' && *p == '=')
1267*69606e3fSchristos         {
1268*69606e3fSchristos           end = p++ - 1;
1269*69606e3fSchristos           flavor = f_conditional;
1270*69606e3fSchristos           break;
1271*69606e3fSchristos         }
1272*69606e3fSchristos       else if (c == '$')
1273*69606e3fSchristos 	{
1274*69606e3fSchristos 	  /* This might begin a variable expansion reference.  Make sure we
1275*69606e3fSchristos 	     don't misrecognize chars inside the reference as =, := or +=.  */
1276*69606e3fSchristos 	  char closeparen;
1277*69606e3fSchristos 	  int count;
1278*69606e3fSchristos 	  c = *p++;
1279*69606e3fSchristos 	  if (c == '(')
1280*69606e3fSchristos 	    closeparen = ')';
1281*69606e3fSchristos 	  else if (c == '{')
1282*69606e3fSchristos 	    closeparen = '}';
1283*69606e3fSchristos 	  else
1284*69606e3fSchristos 	    continue;		/* Nope.  */
1285*69606e3fSchristos 
1286*69606e3fSchristos 	  /* P now points past the opening paren or brace.
1287*69606e3fSchristos 	     Count parens or braces until it is matched.  */
1288*69606e3fSchristos 	  count = 0;
1289*69606e3fSchristos 	  for (; *p != '\0'; ++p)
1290*69606e3fSchristos 	    {
1291*69606e3fSchristos 	      if (*p == c)
1292*69606e3fSchristos 		++count;
1293*69606e3fSchristos 	      else if (*p == closeparen && --count < 0)
1294*69606e3fSchristos 		{
1295*69606e3fSchristos 		  ++p;
1296*69606e3fSchristos 		  break;
1297*69606e3fSchristos 		}
1298*69606e3fSchristos 	    }
1299*69606e3fSchristos 	}
1300*69606e3fSchristos     }
1301*69606e3fSchristos   v->flavor = flavor;
1302*69606e3fSchristos 
1303*69606e3fSchristos   beg = next_token (line);
1304*69606e3fSchristos   while (end > beg && isblank ((unsigned char)end[-1]))
1305*69606e3fSchristos     --end;
1306*69606e3fSchristos   p = next_token (p);
1307*69606e3fSchristos   v->value = p;
1308*69606e3fSchristos 
1309*69606e3fSchristos   /* Expand the name, so "$(foo)bar = baz" works.  */
1310*69606e3fSchristos   name = (char *) alloca (end - beg + 1);
1311*69606e3fSchristos   bcopy (beg, name, end - beg);
1312*69606e3fSchristos   name[end - beg] = '\0';
1313*69606e3fSchristos   v->name = allocated_variable_expand (name);
1314*69606e3fSchristos 
1315*69606e3fSchristos   if (v->name[0] == '\0')
1316*69606e3fSchristos     fatal (&v->fileinfo, _("empty variable name"));
1317*69606e3fSchristos 
1318*69606e3fSchristos   return v;
1319*69606e3fSchristos }
1320*69606e3fSchristos 
1321*69606e3fSchristos /* Try to interpret LINE (a null-terminated string) as a variable definition.
1322*69606e3fSchristos 
1323*69606e3fSchristos    ORIGIN may be o_file, o_override, o_env, o_env_override,
1324*69606e3fSchristos    or o_command specifying that the variable definition comes
1325*69606e3fSchristos    from a makefile, an override directive, the environment with
1326*69606e3fSchristos    or without the -e switch, or the command line.
1327*69606e3fSchristos 
1328*69606e3fSchristos    See the comments for parse_variable_definition().
1329*69606e3fSchristos 
1330*69606e3fSchristos    If LINE was recognized as a variable definition, a pointer to its `struct
1331*69606e3fSchristos    variable' is returned.  If LINE is not a variable definition, NULL is
1332*69606e3fSchristos    returned.  */
1333*69606e3fSchristos 
1334*69606e3fSchristos struct variable *
try_variable_definition(const struct floc * flocp,char * line,enum variable_origin origin,int target_var)1335*69606e3fSchristos try_variable_definition (const struct floc *flocp, char *line,
1336*69606e3fSchristos                          enum variable_origin origin, int target_var)
1337*69606e3fSchristos {
1338*69606e3fSchristos   struct variable v;
1339*69606e3fSchristos   struct variable *vp;
1340*69606e3fSchristos 
1341*69606e3fSchristos   if (flocp != 0)
1342*69606e3fSchristos     v.fileinfo = *flocp;
1343*69606e3fSchristos   else
1344*69606e3fSchristos     v.fileinfo.filenm = 0;
1345*69606e3fSchristos 
1346*69606e3fSchristos   if (!parse_variable_definition (&v, line))
1347*69606e3fSchristos     return 0;
1348*69606e3fSchristos 
1349*69606e3fSchristos   vp = do_variable_definition (flocp, v.name, v.value,
1350*69606e3fSchristos                                origin, v.flavor, target_var);
1351*69606e3fSchristos 
1352*69606e3fSchristos   free (v.name);
1353*69606e3fSchristos 
1354*69606e3fSchristos   return vp;
1355*69606e3fSchristos }
1356*69606e3fSchristos 
1357*69606e3fSchristos /* Print information for variable V, prefixing it with PREFIX.  */
1358*69606e3fSchristos 
1359*69606e3fSchristos static void
print_variable(const void * item,void * arg)1360*69606e3fSchristos print_variable (const void *item, void *arg)
1361*69606e3fSchristos {
1362*69606e3fSchristos   const struct variable *v = (struct variable *) item;
1363*69606e3fSchristos   const char *prefix = (char *) arg;
1364*69606e3fSchristos   const char *origin;
1365*69606e3fSchristos 
1366*69606e3fSchristos   switch (v->origin)
1367*69606e3fSchristos     {
1368*69606e3fSchristos     case o_default:
1369*69606e3fSchristos       origin = _("default");
1370*69606e3fSchristos       break;
1371*69606e3fSchristos     case o_env:
1372*69606e3fSchristos       origin = _("environment");
1373*69606e3fSchristos       break;
1374*69606e3fSchristos     case o_file:
1375*69606e3fSchristos       origin = _("makefile");
1376*69606e3fSchristos       break;
1377*69606e3fSchristos     case o_env_override:
1378*69606e3fSchristos       origin = _("environment under -e");
1379*69606e3fSchristos       break;
1380*69606e3fSchristos     case o_command:
1381*69606e3fSchristos       origin = _("command line");
1382*69606e3fSchristos       break;
1383*69606e3fSchristos     case o_override:
1384*69606e3fSchristos       origin = _("`override' directive");
1385*69606e3fSchristos       break;
1386*69606e3fSchristos     case o_automatic:
1387*69606e3fSchristos       origin = _("automatic");
1388*69606e3fSchristos       break;
1389*69606e3fSchristos     case o_invalid:
1390*69606e3fSchristos     default:
1391*69606e3fSchristos       abort ();
1392*69606e3fSchristos     }
1393*69606e3fSchristos   fputs ("# ", stdout);
1394*69606e3fSchristos   fputs (origin, stdout);
1395*69606e3fSchristos   if (v->fileinfo.filenm)
1396*69606e3fSchristos     printf (_(" (from `%s', line %lu)"),
1397*69606e3fSchristos             v->fileinfo.filenm, v->fileinfo.lineno);
1398*69606e3fSchristos   putchar ('\n');
1399*69606e3fSchristos   fputs (prefix, stdout);
1400*69606e3fSchristos 
1401*69606e3fSchristos   /* Is this a `define'?  */
1402*69606e3fSchristos   if (v->recursive && strchr (v->value, '\n') != 0)
1403*69606e3fSchristos     printf ("define %s\n%s\nendef\n", v->name, v->value);
1404*69606e3fSchristos   else
1405*69606e3fSchristos     {
1406*69606e3fSchristos       register char *p;
1407*69606e3fSchristos 
1408*69606e3fSchristos       printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1409*69606e3fSchristos 
1410*69606e3fSchristos       /* Check if the value is just whitespace.  */
1411*69606e3fSchristos       p = next_token (v->value);
1412*69606e3fSchristos       if (p != v->value && *p == '\0')
1413*69606e3fSchristos 	/* All whitespace.  */
1414*69606e3fSchristos 	printf ("$(subst ,,%s)", v->value);
1415*69606e3fSchristos       else if (v->recursive)
1416*69606e3fSchristos 	fputs (v->value, stdout);
1417*69606e3fSchristos       else
1418*69606e3fSchristos 	/* Double up dollar signs.  */
1419*69606e3fSchristos 	for (p = v->value; *p != '\0'; ++p)
1420*69606e3fSchristos 	  {
1421*69606e3fSchristos 	    if (*p == '$')
1422*69606e3fSchristos 	      putchar ('$');
1423*69606e3fSchristos 	    putchar (*p);
1424*69606e3fSchristos 	  }
1425*69606e3fSchristos       putchar ('\n');
1426*69606e3fSchristos     }
1427*69606e3fSchristos }
1428*69606e3fSchristos 
1429*69606e3fSchristos 
1430*69606e3fSchristos /* Print all the variables in SET.  PREFIX is printed before
1431*69606e3fSchristos    the actual variable definitions (everything else is comments).  */
1432*69606e3fSchristos 
1433*69606e3fSchristos void
print_variable_set(struct variable_set * set,char * prefix)1434*69606e3fSchristos print_variable_set (struct variable_set *set, char *prefix)
1435*69606e3fSchristos {
1436*69606e3fSchristos   hash_map_arg (&set->table, print_variable, prefix);
1437*69606e3fSchristos 
1438*69606e3fSchristos   fputs (_("# variable set hash-table stats:\n"), stdout);
1439*69606e3fSchristos   fputs ("# ", stdout);
1440*69606e3fSchristos   hash_print_stats (&set->table, stdout);
1441*69606e3fSchristos   putc ('\n', stdout);
1442*69606e3fSchristos }
1443*69606e3fSchristos 
1444*69606e3fSchristos /* Print the data base of variables.  */
1445*69606e3fSchristos 
1446*69606e3fSchristos void
print_variable_data_base(void)1447*69606e3fSchristos print_variable_data_base (void)
1448*69606e3fSchristos {
1449*69606e3fSchristos   puts (_("\n# Variables\n"));
1450*69606e3fSchristos 
1451*69606e3fSchristos   print_variable_set (&global_variable_set, "");
1452*69606e3fSchristos 
1453*69606e3fSchristos   puts (_("\n# Pattern-specific Variable Values"));
1454*69606e3fSchristos 
1455*69606e3fSchristos   {
1456*69606e3fSchristos     struct pattern_var *p;
1457*69606e3fSchristos     int rules = 0;
1458*69606e3fSchristos 
1459*69606e3fSchristos     for (p = pattern_vars; p != 0; p = p->next)
1460*69606e3fSchristos       {
1461*69606e3fSchristos         ++rules;
1462*69606e3fSchristos         printf ("\n%s :\n", p->target);
1463*69606e3fSchristos         print_variable (&p->variable, "# ");
1464*69606e3fSchristos       }
1465*69606e3fSchristos 
1466*69606e3fSchristos     if (rules == 0)
1467*69606e3fSchristos       puts (_("\n# No pattern-specific variable values."));
1468*69606e3fSchristos     else
1469*69606e3fSchristos       printf (_("\n# %u pattern-specific variable values"), rules);
1470*69606e3fSchristos   }
1471*69606e3fSchristos }
1472*69606e3fSchristos 
1473*69606e3fSchristos 
1474*69606e3fSchristos /* Print all the local variables of FILE.  */
1475*69606e3fSchristos 
1476*69606e3fSchristos void
print_file_variables(struct file * file)1477*69606e3fSchristos print_file_variables (struct file *file)
1478*69606e3fSchristos {
1479*69606e3fSchristos   if (file->variables != 0)
1480*69606e3fSchristos     print_variable_set (file->variables->set, "# ");
1481*69606e3fSchristos }
1482*69606e3fSchristos 
1483*69606e3fSchristos #ifdef WINDOWS32
1484*69606e3fSchristos void
sync_Path_environment(void)1485*69606e3fSchristos sync_Path_environment (void)
1486*69606e3fSchristos {
1487*69606e3fSchristos   char *path = allocated_variable_expand ("$(PATH)");
1488*69606e3fSchristos   static char *environ_path = NULL;
1489*69606e3fSchristos 
1490*69606e3fSchristos   if (!path)
1491*69606e3fSchristos     return;
1492*69606e3fSchristos 
1493*69606e3fSchristos   /*
1494*69606e3fSchristos    * If done this before, don't leak memory unnecessarily.
1495*69606e3fSchristos    * Free the previous entry before allocating new one.
1496*69606e3fSchristos    */
1497*69606e3fSchristos   if (environ_path)
1498*69606e3fSchristos     free (environ_path);
1499*69606e3fSchristos 
1500*69606e3fSchristos   /*
1501*69606e3fSchristos    * Create something WINDOWS32 world can grok
1502*69606e3fSchristos    */
1503*69606e3fSchristos   convert_Path_to_windows32 (path, ';');
1504*69606e3fSchristos   environ_path = concat ("PATH", "=", path);
1505*69606e3fSchristos   putenv (environ_path);
1506*69606e3fSchristos   free (path);
1507*69606e3fSchristos }
1508*69606e3fSchristos #endif
1509