Fix a bug where conditional variables weren't being expanded correctly.
[make.git] / variable.c
blobc0a2199fb29388b446809d3d533629f5e3c56922
1 /* Internals of variables for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,96,97 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #include "make.h"
20 #include "dep.h"
21 #include "filedef.h"
22 #include "job.h"
23 #include "commands.h"
24 #include "variable.h"
25 #ifdef WINDOWS32
26 #include "pathstuff.h"
27 #endif
29 /* Hash table of all global variable definitions. */
31 #ifndef VARIABLE_BUCKETS
32 #define VARIABLE_BUCKETS 523
33 #endif
34 #ifndef PERFILE_VARIABLE_BUCKETS
35 #define PERFILE_VARIABLE_BUCKETS 23
36 #endif
37 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
38 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
39 #endif
40 static struct variable *variable_table[VARIABLE_BUCKETS];
41 static struct variable_set global_variable_set
42 = { variable_table, VARIABLE_BUCKETS };
43 static struct variable_set_list global_setlist
44 = { 0, &global_variable_set };
45 struct variable_set_list *current_variable_set_list = &global_setlist;
47 static struct variable *lookup_variable_in_set PARAMS ((char *name,
48 unsigned int length, struct variable_set *set));
50 /* Implement variables. */
52 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
53 LENGTH is the length of NAME, which does not need to be null-terminated.
54 ORIGIN specifies the origin of the variable (makefile, command line
55 or environment).
56 If RECURSIVE is nonzero a flag is set in the variable saying
57 that it should be recursively re-expanded. */
59 struct variable *
60 define_variable_in_set (name, length, value, origin, recursive, set)
61 char *name;
62 unsigned int length;
63 char *value;
64 enum variable_origin origin;
65 int recursive;
66 struct variable_set *set;
68 register unsigned int i;
69 register unsigned int hashval;
70 register struct variable *v;
72 hashval = 0;
73 for (i = 0; i < length; ++i)
74 HASH (hashval, name[i]);
75 hashval %= set->buckets;
77 for (v = set->table[hashval]; v != 0; v = v->next)
78 if (*v->name == *name
79 && !strncmp (v->name + 1, name + 1, length - 1)
80 && v->name[length] == '\0')
81 break;
83 if (env_overrides && origin == o_env)
84 origin = o_env_override;
86 if (v != 0)
88 if (env_overrides && v->origin == o_env)
89 /* V came from in the environment. Since it was defined
90 before the switches were parsed, it wasn't affected by -e. */
91 v->origin = o_env_override;
93 /* A variable of this name is already defined.
94 If the old definition is from a stronger source
95 than this one, don't redefine it. */
96 if ((int) origin >= (int) v->origin)
98 if (v->value != 0)
99 free (v->value);
100 v->value = savestring (value, strlen (value));
101 v->origin = origin;
102 v->recursive = recursive;
104 return v;
107 /* Create a new variable definition and add it to the hash table. */
109 v = (struct variable *) xmalloc (sizeof (struct variable));
110 v->name = savestring (name, length);
111 v->value = savestring (value, strlen (value));
112 v->origin = origin;
113 v->recursive = recursive;
114 v->expanding = 0;
115 v->per_target = 0;
116 v->export = v_default;
117 v->next = set->table[hashval];
118 set->table[hashval] = v;
119 return v;
122 /* Define a variable in the current variable set. */
124 struct variable *
125 define_variable (name, length, value, origin, recursive)
126 char *name;
127 unsigned int length;
128 char *value;
129 enum variable_origin origin;
130 int recursive;
132 return define_variable_in_set (name, length, value, origin, recursive,
133 current_variable_set_list->set);
136 /* Define a variable in FILE's variable set. */
138 struct variable *
139 define_variable_for_file (name, length, value, origin, recursive, file)
140 char *name;
141 unsigned int length;
142 char *value;
143 enum variable_origin origin;
144 int recursive;
145 struct file *file;
147 return define_variable_in_set (name, length, value, origin, recursive,
148 file->variables->set);
151 /* Lookup a variable whose name is a string starting at NAME
152 and with LENGTH chars. NAME need not be null-terminated.
153 Returns address of the `struct variable' containing all info
154 on the variable, or nil if no such variable is defined. */
156 struct variable *
157 lookup_variable (name, length)
158 char *name;
159 unsigned int length;
161 register struct variable_set_list *setlist;
163 register unsigned int i;
164 register unsigned int rawhash = 0;
166 for (i = 0; i < length; ++i)
167 HASH (rawhash, name[i]);
169 for (setlist = current_variable_set_list;
170 setlist != 0; setlist = setlist->next)
172 register struct variable_set *set = setlist->set;
173 register unsigned int hashval = rawhash % set->buckets;
174 register struct variable *v;
176 for (v = set->table[hashval]; v != 0; v = v->next)
177 if (*v->name == *name
178 && !strncmp (v->name + 1, name + 1, length - 1)
179 && v->name[length] == 0)
180 return v;
183 return 0;
186 /* Lookup a variable whose name is a string starting at NAME
187 and with LENGTH chars in set SET. NAME need not be null-terminated.
188 Returns address of the `struct variable' containing all info
189 on the variable, or nil if no such variable is defined. */
191 static struct variable *
192 lookup_variable_in_set (name, length, set)
193 char *name;
194 unsigned int length;
195 struct variable_set *set;
197 register unsigned int i;
198 register unsigned int hash = 0;
199 register struct variable *v;
201 for (i = 0; i < length; ++i)
202 HASH (hash, name[i]);
203 hash %= set->buckets;
205 for (v = set->table[hash]; v != 0; v = v->next)
206 if (*v->name == *name
207 && !strncmp (v->name + 1, name + 1, length - 1)
208 && v->name[length] == 0)
209 return v;
211 return 0;
214 /* Initialize FILE's variable set list. If FILE already has a variable set
215 list, the topmost variable set is left intact, but the the rest of the
216 chain is replaced with FILE->parent's setlist. */
218 void
219 initialize_file_variables (file)
220 struct file *file;
222 register struct variable_set_list *l = file->variables;
223 if (l == 0)
225 l = (struct variable_set_list *)
226 xmalloc (sizeof (struct variable_set_list));
227 l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
228 l->set->buckets = PERFILE_VARIABLE_BUCKETS;
229 l->set->table = (struct variable **)
230 xmalloc (l->set->buckets * sizeof (struct variable *));
231 bzero ((char *) l->set->table,
232 l->set->buckets * sizeof (struct variable *));
233 file->variables = l;
236 if (file->parent == 0)
237 l->next = &global_setlist;
238 else
240 if (file->parent->variables == 0)
241 initialize_file_variables (file->parent);
242 l->next = file->parent->variables;
246 /* Pop the top set off the current variable set list,
247 and free all its storage. */
249 void
250 pop_variable_scope ()
252 register struct variable_set_list *setlist = current_variable_set_list;
253 register struct variable_set *set = setlist->set;
254 register unsigned int i;
256 current_variable_set_list = setlist->next;
257 free ((char *) setlist);
259 for (i = 0; i < set->buckets; ++i)
261 register struct variable *next = set->table[i];
262 while (next != 0)
264 register struct variable *v = next;
265 next = v->next;
267 free (v->name);
268 if (v->value)
269 free (v->value);
270 free ((char *) v);
273 free ((char *) set->table);
274 free ((char *) set);
277 struct variable_set_list *
278 create_new_variable_set ()
280 register struct variable_set_list *setlist;
281 register struct variable_set *set;
283 set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
284 set->buckets = SMALL_SCOPE_VARIABLE_BUCKETS;
285 set->table = (struct variable **)
286 xmalloc (set->buckets * sizeof (struct variable *));
287 bzero ((char *) set->table, set->buckets * sizeof (struct variable *));
289 setlist = (struct variable_set_list *)
290 xmalloc (sizeof (struct variable_set_list));
291 setlist->set = set;
292 setlist->next = current_variable_set_list;
294 return setlist;
297 /* Create a new variable set and push it on the current setlist. */
299 struct variable_set_list *
300 push_new_variable_scope ()
302 return (current_variable_set_list = create_new_variable_set());
305 /* Merge SET1 into SET0, freeing unused storage in SET1. */
307 static void
308 merge_variable_sets (set0, set1)
309 struct variable_set *set0, *set1;
311 register unsigned int bucket1;
313 for (bucket1 = 0; bucket1 < set1->buckets; ++bucket1)
315 register struct variable *v1 = set1->table[bucket1];
316 while (v1 != 0)
318 struct variable *next = v1->next;
319 unsigned int bucket0;
320 register struct variable *v0;
322 if (set1->buckets >= set0->buckets)
323 bucket0 = bucket1;
324 else
326 register char *n;
327 bucket0 = 0;
328 for (n = v1->name; *n != '\0'; ++n)
329 HASH (bucket0, *n);
331 bucket0 %= set0->buckets;
333 for (v0 = set0->table[bucket0]; v0 != 0; v0 = v0->next)
334 if (streq (v0->name, v1->name))
335 break;
337 if (v0 == 0)
339 /* There is no variable in SET0 with the same name. */
340 v1->next = set0->table[bucket0];
341 set0->table[bucket0] = v1;
343 else
345 /* The same variable exists in both sets.
346 SET0 takes precedence. */
347 free (v1->value);
348 free ((char *) v1);
351 v1 = next;
356 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
358 void
359 merge_variable_set_lists (setlist0, setlist1)
360 struct variable_set_list **setlist0, *setlist1;
362 register struct variable_set_list *list0 = *setlist0;
363 struct variable_set_list *last0 = 0;
365 while (setlist1 != 0 && list0 != 0)
367 struct variable_set_list *next = setlist1;
368 setlist1 = setlist1->next;
370 merge_variable_sets (list0->set, next->set);
372 last0 = list0;
373 list0 = list0->next;
376 if (setlist1 != 0)
378 if (last0 == 0)
379 *setlist0 = setlist1;
380 else
381 last0->next = setlist1;
385 /* Define the automatic variables, and record the addresses
386 of their structures so we can change their values quickly. */
388 void
389 define_automatic_variables ()
391 #ifdef WINDOWS32
392 extern char* default_shell;
393 #else
394 extern char default_shell[];
395 #endif
396 register struct variable *v;
397 char buf[200];
399 sprintf (buf, "%u", makelevel);
400 (void) define_variable ("MAKELEVEL", 9, buf, o_env, 0);
402 sprintf (buf, "%s%s%s",
403 version_string,
404 (remote_description == 0 || remote_description[0] == '\0')
405 ? "" : "-",
406 (remote_description == 0 || remote_description[0] == '\0')
407 ? "" : remote_description);
408 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
410 #ifdef __MSDOS__
411 /* Allow to specify a special shell just for Make,
412 and use $COMSPEC as the default $SHELL when appropriate. */
414 static char shell_str[] = "SHELL";
415 const int shlen = sizeof (shell_str) - 1;
416 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
417 struct variable *comp = lookup_variable ("COMSPEC", 7);
419 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
420 if (mshp)
421 (void) define_variable (shell_str, shlen,
422 mshp->value, o_env_override, 0);
423 else if (comp)
425 /* $COMSPEC shouldn't override $SHELL. */
426 struct variable *shp = lookup_variable (shell_str, shlen);
428 if (!shp)
429 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
432 #endif
434 /* This won't override any definition, but it
435 will provide one if there isn't one there. */
436 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
437 v->export = v_export; /* Always export SHELL. */
439 /* On MSDOS we do use SHELL from environment, since
440 it isn't a standard environment variable on MSDOS,
441 so whoever sets it, does that on purpose. */
442 #ifndef __MSDOS__
443 /* Don't let SHELL come from the environment. */
444 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
446 free (v->value);
447 v->origin = o_file;
448 v->value = savestring (default_shell, strlen (default_shell));
450 #endif
452 /* Make sure MAKEFILES gets exported if it is set. */
453 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
454 v->export = v_ifset;
456 /* Define the magic D and F variables in terms of
457 the automatic variables they are variations of. */
459 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
460 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
461 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
462 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
463 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
464 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
465 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
466 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
467 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
468 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
469 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
470 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
471 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
472 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
475 int export_all_variables;
477 /* Create a new environment for FILE's commands.
478 If FILE is nil, this is for the `shell' function.
479 The child's MAKELEVEL variable is incremented. */
481 char **
482 target_environment (file)
483 struct file *file;
485 struct variable_set_list *set_list;
486 register struct variable_set_list *s;
487 struct variable_bucket
489 struct variable_bucket *next;
490 struct variable *variable;
492 struct variable_bucket **table;
493 unsigned int buckets;
494 register unsigned int i;
495 register unsigned nvariables;
496 char **result;
497 unsigned int mklev_hash;
499 if (file == 0)
500 set_list = current_variable_set_list;
501 else
502 set_list = file->variables;
504 /* Find the lowest number of buckets in any set in the list. */
505 s = set_list;
506 buckets = s->set->buckets;
507 for (s = s->next; s != 0; s = s->next)
508 if (s->set->buckets < buckets)
509 buckets = s->set->buckets;
511 /* Find the hash value of the bucket `MAKELEVEL' will fall into. */
513 char *p = "MAKELEVEL";
514 mklev_hash = 0;
515 while (*p != '\0')
516 HASH (mklev_hash, *p++);
519 /* Temporarily allocate a table with that many buckets. */
520 table = (struct variable_bucket **)
521 alloca (buckets * sizeof (struct variable_bucket *));
522 bzero ((char *) table, buckets * sizeof (struct variable_bucket *));
524 /* Run through all the variable sets in the list,
525 accumulating variables in TABLE. */
526 nvariables = 0;
527 for (s = set_list; s != 0; s = s->next)
529 register struct variable_set *set = s->set;
530 for (i = 0; i < set->buckets; ++i)
532 register struct variable *v;
533 for (v = set->table[i]; v != 0; v = v->next)
535 unsigned int j = i % buckets;
536 register struct variable_bucket *ov;
537 register char *p = v->name;
539 if (i == mklev_hash % set->buckets
540 && streq (v->name, "MAKELEVEL"))
541 /* Don't include MAKELEVEL because it will be
542 added specially at the end. */
543 continue;
545 /* If this is a per-target variable and it hasn't been touched
546 already then look up the global version and take its export
547 value. */
548 if (v->per_target && v->export == v_default)
550 struct variable *gv;
552 gv = lookup_variable_in_set(v->name, strlen(v->name),
553 &global_variable_set);
554 if (gv)
555 v->export = gv->export;
558 switch (v->export)
560 case v_default:
561 if (v->origin == o_default || v->origin == o_automatic)
562 /* Only export default variables by explicit request. */
563 continue;
565 if (! export_all_variables
566 && v->origin != o_command
567 && v->origin != o_env && v->origin != o_env_override)
568 continue;
570 if (*p != '_' && (*p < 'A' || *p > 'Z')
571 && (*p < 'a' || *p > 'z'))
572 continue;
573 for (++p; *p != '\0'; ++p)
574 if (*p != '_' && (*p < 'a' || *p > 'z')
575 && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9'))
576 continue;
577 if (*p != '\0')
578 continue;
579 break;
581 case v_export:
582 break;
584 case v_noexport:
585 continue;
587 case v_ifset:
588 if (v->origin == o_default)
589 continue;
590 break;
593 /* If this was from a different-sized hash table, then
594 recalculate the bucket it goes in. */
595 if (set->buckets != buckets)
597 register char *np;
599 j = 0;
600 for (np = v->name; *np != '\0'; ++np)
601 HASH (j, *np);
602 j %= buckets;
605 for (ov = table[j]; ov != 0; ov = ov->next)
606 if (streq (v->name, ov->variable->name))
607 break;
609 if (ov == 0)
611 register struct variable_bucket *entry;
612 entry = (struct variable_bucket *)
613 alloca (sizeof (struct variable_bucket));
614 entry->next = table[j];
615 entry->variable = v;
616 table[j] = entry;
617 ++nvariables;
623 result = (char **) xmalloc ((nvariables + 2) * sizeof (char *));
624 nvariables = 0;
625 for (i = 0; i < buckets; ++i)
627 register struct variable_bucket *b;
628 for (b = table[i]; b != 0; b = b->next)
630 register struct variable *v = b->variable;
632 /* If V is recursively expanded and didn't come from the environment,
633 expand its value. If it came from the environment, it should
634 go back into the environment unchanged. */
635 if (v->recursive
636 && v->origin != o_env && v->origin != o_env_override)
638 char *value = recursively_expand (v);
639 #ifdef WINDOWS32
640 if (strcmp(v->name, "Path") == 0 ||
641 strcmp(v->name, "PATH") == 0)
642 convert_Path_to_windows32(value, ';');
643 #endif
644 result[nvariables++] = concat (v->name, "=", value);
645 free (value);
647 else
648 #ifdef WINDOWS32
650 if (strcmp(v->name, "Path") == 0 ||
651 strcmp(v->name, "PATH") == 0)
652 convert_Path_to_windows32(v->value, ';');
653 result[nvariables++] = concat (v->name, "=", v->value);
655 #else
656 result[nvariables++] = concat (v->name, "=", v->value);
657 #endif
660 result[nvariables] = (char *) xmalloc (100);
661 (void) sprintf (result[nvariables], "MAKELEVEL=%u", makelevel + 1);
662 result[++nvariables] = 0;
664 return result;
667 /* Try to interpret LINE (a null-terminated string) as a variable definition.
669 ORIGIN may be o_file, o_override, o_env, o_env_override,
670 or o_command specifying that the variable definition comes
671 from a makefile, an override directive, the environment with
672 or without the -e switch, or the command line.
674 See the comments for parse_variable_definition().
676 If LINE was recognized as a variable definition, a pointer to its `struct
677 variable' is returned. If LINE is not a variable definition, NULL is
678 returned. */
680 struct variable *
681 try_variable_definition (flocp, line, origin)
682 const struct floc *flocp;
683 char *line;
684 enum variable_origin origin;
686 register int c;
687 register char *p = line;
688 register char *beg;
689 register char *end;
690 enum { f_bogus,
691 f_simple, f_recursive, f_append, f_conditional } flavor = f_bogus;
692 char *name, *expanded_name, *value, *alloc_value=NULL;
693 struct variable *v;
695 while (1)
697 c = *p++;
698 if (c == '\0' || c == '#')
699 return 0;
700 if (c == '=')
702 end = p - 1;
703 flavor = f_recursive;
704 break;
706 else if (c == ':')
707 if (*p == '=')
709 end = p++ - 1;
710 flavor = f_simple;
711 break;
713 else
714 /* A colon other than := is a rule line, not a variable defn. */
715 return 0;
716 else if (c == '+' && *p == '=')
718 end = p++ - 1;
719 flavor = f_append;
720 break;
722 else if (c == '?' && *p == '=')
724 end = p++ - 1;
725 flavor = f_conditional;
726 break;
728 else if (c == '$')
730 /* This might begin a variable expansion reference. Make sure we
731 don't misrecognize chars inside the reference as =, := or +=. */
732 char closeparen;
733 int count;
734 c = *p++;
735 if (c == '(')
736 closeparen = ')';
737 else if (c == '{')
738 closeparen = '}';
739 else
740 continue; /* Nope. */
742 /* P now points past the opening paren or brace.
743 Count parens or braces until it is matched. */
744 count = 0;
745 for (; *p != '\0'; ++p)
747 if (*p == c)
748 ++count;
749 else if (*p == closeparen && --count < 0)
751 ++p;
752 break;
758 beg = next_token (line);
759 while (end > beg && isblank (end[-1]))
760 --end;
761 p = next_token (p);
763 /* Expand the name, so "$(foo)bar = baz" works. */
764 name = (char *) alloca (end - beg + 1);
765 bcopy (beg, name, end - beg);
766 name[end - beg] = '\0';
767 expanded_name = allocated_variable_expand (name);
769 if (expanded_name[0] == '\0')
770 fatal (flocp, "empty variable name");
772 /* Calculate the variable's new value in VALUE. */
774 switch (flavor)
776 case f_bogus:
777 /* Should not be possible. */
778 abort ();
779 case f_simple:
780 /* A simple variable definition "var := value". Expand the value.
781 We have to allocate memory since otherwise it'll clobber the
782 variable buffer, and we still need that. */
783 alloc_value = allocated_variable_expand (p);
784 value = alloc_value;
785 break;
786 case f_conditional:
787 /* A conditional variable definition "var ?= value".
788 The value is set IFF the variable is not defined yet. */
789 v = lookup_variable(expanded_name, strlen(expanded_name));
790 if (v)
792 free(expanded_name);
793 return v;
795 flavor = f_recursive;
796 /* FALLTHROUGH */
797 case f_recursive:
798 /* A recursive variable definition "var = value".
799 The value is used verbatim. */
800 value = p;
801 break;
802 case f_append:
803 /* An appending variable definition "var += value".
804 Extract the old value and append the new one. */
805 v = lookup_variable (expanded_name, strlen (expanded_name));
806 if (v == 0)
808 /* There was no old value.
809 This becomes a normal recursive definition. */
810 value = p;
811 flavor = f_recursive;
813 else
815 /* Paste the old and new values together in VALUE. */
817 unsigned int oldlen, newlen;
819 if (v->recursive)
820 /* The previous definition of the variable was recursive.
821 The new value comes from the unexpanded old and new values. */
822 flavor = f_recursive;
823 else
824 /* The previous definition of the variable was simple.
825 The new value comes from the old value, which was expanded
826 when it was set; and from the expanded new value. */
827 p = variable_expand (p);
829 oldlen = strlen (v->value);
830 newlen = strlen (p);
831 value = (char *) alloca (oldlen + 1 + newlen + 1);
832 bcopy (v->value, value, oldlen);
833 value[oldlen] = ' ';
834 bcopy (p, &value[oldlen + 1], newlen + 1);
838 #ifdef __MSDOS__
839 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
840 non-Unix systems don't conform to this default configuration (in
841 fact, most of them don't even have `/bin'). On the other hand,
842 $SHELL in the environment, if set, points to the real pathname of
843 the shell.
844 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
845 the Makefile override $SHELL from the environment. But first, we
846 look for the basename of the shell in the directory where SHELL=
847 points, and along the $PATH; if it is found in any of these places,
848 we define $SHELL to be the actual pathname of the shell. Thus, if
849 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
850 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
851 defining SHELL to be "d:/unix/bash.exe". */
852 if (origin == o_file
853 && strcmp (expanded_name, "SHELL") == 0)
855 char shellpath[PATH_MAX];
856 extern char * __dosexec_find_on_path (const char *, char *[], char *);
858 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
859 if (__dosexec_find_on_path (value, (char **)0, shellpath))
861 char *p;
863 for (p = shellpath; *p; p++)
865 if (*p == '\\')
866 *p = '/';
868 v = define_variable (expanded_name, strlen (expanded_name),
869 shellpath, origin, flavor == f_recursive);
871 else
873 char *shellbase, *bslash;
874 struct variable *pathv = lookup_variable ("PATH", 4);
875 char *path_string;
876 char *fake_env[2];
877 size_t pathlen = 0;
879 shellbase = rindex (value, '/');
880 bslash = rindex (value, '\\');
881 if (!shellbase || bslash > shellbase)
882 shellbase = bslash;
883 if (!shellbase && value[1] == ':')
884 shellbase = value + 1;
885 if (shellbase)
886 shellbase++;
887 else
888 shellbase = value;
890 /* Search for the basename of the shell (with standard
891 executable extensions) along the $PATH. */
892 if (pathv)
893 pathlen = strlen (pathv->value);
894 path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
895 /* On MSDOS, current directory is considered as part of $PATH. */
896 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
897 fake_env[0] = path_string;
898 fake_env[1] = (char *)0;
899 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
901 char *p;
903 for (p = shellpath; *p; p++)
905 if (*p == '\\')
906 *p = '/';
908 v = define_variable (expanded_name, strlen (expanded_name),
909 shellpath, origin, flavor == f_recursive);
911 else
912 v = lookup_variable (expanded_name, strlen (expanded_name));
914 free (path_string);
917 else
918 #endif /* __MSDOS__ */
919 #ifdef WINDOWS32
920 if (origin == o_file
921 && strcmp (expanded_name, "SHELL") == 0) {
922 extern char* default_shell;
925 * Call shell locator function. If it returns TRUE, then
926 * set no_default_sh_exe to indicate sh was found and
927 * set new value for SHELL variable.
929 if (find_and_set_default_shell(value)) {
930 v = define_variable (expanded_name, strlen (expanded_name),
931 default_shell, origin, flavor == f_recursive);
932 no_default_sh_exe = 0;
934 } else
935 #endif
937 v = define_variable (expanded_name, strlen (expanded_name),
938 value, origin, flavor == f_recursive);
940 if (alloc_value)
941 free (alloc_value);
942 free (expanded_name);
944 return v;
947 /* Print information for variable V, prefixing it with PREFIX. */
949 static void
950 print_variable (v, prefix)
951 register struct variable *v;
952 char *prefix;
954 char *origin;
956 switch (v->origin)
958 case o_default:
959 origin = "default";
960 break;
961 case o_env:
962 origin = "environment";
963 break;
964 case o_file:
965 origin = "makefile";
966 break;
967 case o_env_override:
968 origin = "environment under -e";
969 break;
970 case o_command:
971 origin = "command line";
972 break;
973 case o_override:
974 origin = "`override' directive";
975 break;
976 case o_automatic:
977 origin = "automatic";
978 break;
979 case o_invalid:
980 default:
981 abort ();
982 break;
984 printf ("# %s\n", origin);
986 fputs (prefix, stdout);
988 /* Is this a `define'? */
989 if (v->recursive && index (v->value, '\n') != 0)
990 printf ("define %s\n%s\nendef\n", v->name, v->value);
991 else
993 register char *p;
995 printf ("%s %s= ", v->name, v->recursive ? "" : ":");
997 /* Check if the value is just whitespace. */
998 p = next_token (v->value);
999 if (p != v->value && *p == '\0')
1000 /* All whitespace. */
1001 printf ("$(subst ,,%s)", v->value);
1002 else if (v->recursive)
1003 fputs (v->value, stdout);
1004 else
1005 /* Double up dollar signs. */
1006 for (p = v->value; *p != '\0'; ++p)
1008 if (*p == '$')
1009 putchar ('$');
1010 putchar (*p);
1012 putchar ('\n');
1017 /* Print all the variables in SET. PREFIX is printed before
1018 the actual variable definitions (everything else is comments). */
1020 void
1021 print_variable_set (set, prefix)
1022 register struct variable_set *set;
1023 char *prefix;
1025 register unsigned int i, nvariables, per_bucket;
1026 register struct variable *v;
1028 per_bucket = nvariables = 0;
1029 for (i = 0; i < set->buckets; ++i)
1031 register unsigned int this_bucket = 0;
1033 for (v = set->table[i]; v != 0; v = v->next)
1035 ++this_bucket;
1036 print_variable (v, prefix);
1039 nvariables += this_bucket;
1040 if (this_bucket > per_bucket)
1041 per_bucket = this_bucket;
1044 if (nvariables == 0)
1045 puts ("# No variables.");
1046 else
1048 printf ("# %u variables in %u hash buckets.\n",
1049 nvariables, set->buckets);
1050 #ifndef NO_FLOAT
1051 printf ("# average of %.1f variables per bucket, \
1052 max %u in one bucket.\n",
1053 (double) nvariables / (double) set->buckets,
1054 per_bucket);
1055 #else
1057 int f = (nvariables * 1000 + 5) / set->buckets;
1058 printf ("# average of %d.%d variables per bucket, \
1059 max %u in one bucket.\n",
1060 f/10, f%10,
1061 per_bucket);
1063 #endif
1068 /* Print the data base of variables. */
1070 void
1071 print_variable_data_base ()
1073 puts ("\n# Variables\n");
1075 print_variable_set (&global_variable_set, "");
1079 /* Print all the local variables of FILE. */
1081 void
1082 print_file_variables (file)
1083 struct file *file;
1085 if (file->variables != 0)
1086 print_variable_set (file->variables->set, "# ");
1089 #ifdef WINDOWS32
1090 void
1091 sync_Path_environment(void)
1093 char* path = allocated_variable_expand("$(Path)");
1094 static char* environ_path = NULL;
1096 if (!path)
1097 return;
1100 * If done this before, don't leak memory unnecessarily.
1101 * Free the previous entry before allocating new one.
1103 if (environ_path)
1104 free(environ_path);
1107 * Create something WINDOWS32 world can grok
1109 convert_Path_to_windows32(path, ';');
1110 environ_path = concat("Path", "=", path);
1111 putenv(environ_path);
1112 free(path);
1114 #endif