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