* Fix problems with double-colon rules.
[make.git] / variable.c
blob8058a74885335f99f7930725dadb2813fcdc23c4
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, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include "make.h"
21 #include "dep.h"
22 #include "filedef.h"
23 #include "job.h"
24 #include "commands.h"
25 #include "variable.h"
26 #ifdef WINDOWS32
27 #include "pathstuff.h"
28 #endif
30 /* Hash table of all global variable definitions. */
32 #ifndef VARIABLE_BUCKETS
33 #define VARIABLE_BUCKETS 523
34 #endif
35 #ifndef PERFILE_VARIABLE_BUCKETS
36 #define PERFILE_VARIABLE_BUCKETS 23
37 #endif
38 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
39 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
40 #endif
41 static struct variable *variable_table[VARIABLE_BUCKETS];
42 static struct variable_set global_variable_set
43 = { variable_table, VARIABLE_BUCKETS };
44 static struct variable_set_list global_setlist
45 = { 0, &global_variable_set };
46 struct variable_set_list *current_variable_set_list = &global_setlist;
48 static struct variable *lookup_variable_in_set PARAMS ((char *name,
49 unsigned int length, struct variable_set *set));
51 /* Implement variables. */
53 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
54 LENGTH is the length of NAME, which does not need to be null-terminated.
55 ORIGIN specifies the origin of the variable (makefile, command line
56 or environment).
57 If RECURSIVE is nonzero a flag is set in the variable saying
58 that it should be recursively re-expanded. */
60 struct variable *
61 define_variable_in_set (name, length, value, origin, recursive, set)
62 char *name;
63 unsigned int length;
64 char *value;
65 enum variable_origin origin;
66 int recursive;
67 struct variable_set *set;
69 register unsigned int i;
70 register unsigned int hashval;
71 register struct variable *v;
73 hashval = 0;
74 for (i = 0; i < length; ++i)
75 HASH (hashval, name[i]);
76 hashval %= set->buckets;
78 for (v = set->table[hashval]; v != 0; v = v->next)
79 if (*v->name == *name
80 && strneq (v->name + 1, name + 1, length - 1)
81 && v->name[length] == '\0')
82 break;
84 if (env_overrides && origin == o_env)
85 origin = o_env_override;
87 if (v != 0)
89 if (env_overrides && v->origin == o_env)
90 /* V came from in the environment. Since it was defined
91 before the switches were parsed, it wasn't affected by -e. */
92 v->origin = o_env_override;
94 /* A variable of this name is already defined.
95 If the old definition is from a stronger source
96 than this one, don't redefine it. */
97 if ((int) origin >= (int) v->origin)
99 if (v->value != 0)
100 free (v->value);
101 v->value = xstrdup (value);
102 v->origin = origin;
103 v->recursive = recursive;
105 return v;
108 /* Create a new variable definition and add it to the hash table. */
110 v = (struct variable *) xmalloc (sizeof (struct variable));
111 v->name = savestring (name, length);
112 v->value = xstrdup (value);
113 v->origin = origin;
114 v->recursive = recursive;
115 v->expanding = 0;
116 v->per_target = 0;
117 v->append = 0;
118 v->export = v_default;
119 v->next = set->table[hashval];
120 set->table[hashval] = v;
121 return v;
124 /* Define a variable in the current variable set. */
126 struct variable *
127 define_variable (name, length, value, origin, recursive)
128 char *name;
129 unsigned int length;
130 char *value;
131 enum variable_origin origin;
132 int recursive;
134 return define_variable_in_set (name, length, value, origin, recursive,
135 current_variable_set_list->set);
138 /* Define a variable in FILE's variable set. */
140 struct variable *
141 define_variable_for_file (name, length, value, origin, recursive, file)
142 char *name;
143 unsigned int length;
144 char *value;
145 enum variable_origin origin;
146 int recursive;
147 struct file *file;
149 return define_variable_in_set (name, length, value, origin, recursive,
150 file->variables->set);
153 /* Lookup a variable whose name is a string starting at NAME
154 and with LENGTH chars. NAME need not be null-terminated.
155 Returns address of the `struct variable' containing all info
156 on the variable, or nil if no such variable is defined. */
158 struct variable *
159 lookup_variable (name, length)
160 char *name;
161 unsigned int length;
163 register struct variable_set_list *setlist;
165 register unsigned int i;
166 register unsigned int rawhash = 0;
168 for (i = 0; i < length; ++i)
169 HASH (rawhash, name[i]);
171 for (setlist = current_variable_set_list;
172 setlist != 0; setlist = setlist->next)
174 register struct variable_set *set = setlist->set;
175 register unsigned int hashval = rawhash % set->buckets;
176 register struct variable *v;
178 for (v = set->table[hashval]; v != 0; v = v->next)
179 if (*v->name == *name
180 && strneq (v->name + 1, name + 1, length - 1)
181 && v->name[length] == 0)
182 return v;
185 return 0;
188 /* Lookup a variable whose name is a string starting at NAME
189 and with LENGTH chars in set SET. NAME need not be null-terminated.
190 Returns address of the `struct variable' containing all info
191 on the variable, or nil if no such variable is defined. */
193 static struct variable *
194 lookup_variable_in_set (name, length, set)
195 char *name;
196 unsigned int length;
197 struct variable_set *set;
199 register unsigned int i;
200 register unsigned int hash = 0;
201 register struct variable *v;
203 for (i = 0; i < length; ++i)
204 HASH (hash, name[i]);
205 hash %= set->buckets;
207 for (v = set->table[hash]; v != 0; v = v->next)
208 if (*v->name == *name
209 && strneq (v->name + 1, name + 1, length - 1)
210 && v->name[length] == 0)
211 return v;
213 return 0;
216 /* Initialize FILE's variable set list. If FILE already has a variable set
217 list, the topmost variable set is left intact, but the the rest of the
218 chain is replaced with FILE->parent's setlist. */
220 void
221 initialize_file_variables (file)
222 struct file *file;
224 register struct variable_set_list *l = file->variables;
225 if (l == 0)
227 l = (struct variable_set_list *)
228 xmalloc (sizeof (struct variable_set_list));
229 l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
230 l->set->buckets = PERFILE_VARIABLE_BUCKETS;
231 l->set->table = (struct variable **)
232 xmalloc (l->set->buckets * sizeof (struct variable *));
233 bzero ((char *) l->set->table,
234 l->set->buckets * sizeof (struct variable *));
235 file->variables = l;
238 if (file->parent == 0)
239 l->next = &global_setlist;
240 else
242 initialize_file_variables (file->parent);
243 l->next = file->parent->variables;
247 /* Pop the top set off the current variable set list,
248 and free all its storage. */
250 void
251 pop_variable_scope ()
253 register struct variable_set_list *setlist = current_variable_set_list;
254 register struct variable_set *set = setlist->set;
255 register unsigned int i;
257 current_variable_set_list = setlist->next;
258 free ((char *) setlist);
260 for (i = 0; i < set->buckets; ++i)
262 register struct variable *next = set->table[i];
263 while (next != 0)
265 register struct variable *v = next;
266 next = v->next;
268 free (v->name);
269 if (v->value)
270 free (v->value);
271 free ((char *) v);
274 free ((char *) set->table);
275 free ((char *) set);
278 struct variable_set_list *
279 create_new_variable_set ()
281 register struct variable_set_list *setlist;
282 register struct variable_set *set;
284 set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
285 set->buckets = SMALL_SCOPE_VARIABLE_BUCKETS;
286 set->table = (struct variable **)
287 xmalloc (set->buckets * sizeof (struct variable *));
288 bzero ((char *) set->table, set->buckets * sizeof (struct variable *));
290 setlist = (struct variable_set_list *)
291 xmalloc (sizeof (struct variable_set_list));
292 setlist->set = set;
293 setlist->next = current_variable_set_list;
295 return setlist;
298 /* Create a new variable set and push it on the current setlist. */
300 struct variable_set_list *
301 push_new_variable_scope ()
303 return (current_variable_set_list = create_new_variable_set());
306 /* Merge SET1 into SET0, freeing unused storage in SET1. */
308 static void
309 merge_variable_sets (set0, set1)
310 struct variable_set *set0, *set1;
312 register unsigned int bucket1;
314 for (bucket1 = 0; bucket1 < set1->buckets; ++bucket1)
316 register struct variable *v1 = set1->table[bucket1];
317 while (v1 != 0)
319 struct variable *next = v1->next;
320 unsigned int bucket0;
321 register struct variable *v0;
323 if (set1->buckets >= set0->buckets)
324 bucket0 = bucket1;
325 else
327 register char *n;
328 bucket0 = 0;
329 for (n = v1->name; *n != '\0'; ++n)
330 HASH (bucket0, *n);
332 bucket0 %= set0->buckets;
334 for (v0 = set0->table[bucket0]; v0 != 0; v0 = v0->next)
335 if (streq (v0->name, v1->name))
336 break;
338 if (v0 == 0)
340 /* There is no variable in SET0 with the same name. */
341 v1->next = set0->table[bucket0];
342 set0->table[bucket0] = v1;
344 else
346 /* The same variable exists in both sets.
347 SET0 takes precedence. */
348 free (v1->value);
349 free ((char *) v1);
352 v1 = next;
357 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
359 void
360 merge_variable_set_lists (setlist0, setlist1)
361 struct variable_set_list **setlist0, *setlist1;
363 register struct variable_set_list *list0 = *setlist0;
364 struct variable_set_list *last0 = 0;
366 while (setlist1 != 0 && list0 != 0)
368 struct variable_set_list *next = setlist1;
369 setlist1 = setlist1->next;
371 merge_variable_sets (list0->set, next->set);
373 last0 = list0;
374 list0 = list0->next;
377 if (setlist1 != 0)
379 if (last0 == 0)
380 *setlist0 = setlist1;
381 else
382 last0->next = setlist1;
386 /* Define the automatic variables, and record the addresses
387 of their structures so we can change their values quickly. */
389 void
390 define_automatic_variables ()
392 #ifdef WINDOWS32
393 extern char* default_shell;
394 #else
395 extern char default_shell[];
396 #endif
397 register struct variable *v;
398 char buf[200];
400 sprintf (buf, "%u", makelevel);
401 (void) define_variable ("MAKELEVEL", 9, buf, o_env, 0);
403 sprintf (buf, "%s%s%s",
404 version_string,
405 (remote_description == 0 || remote_description[0] == '\0')
406 ? "" : "-",
407 (remote_description == 0 || remote_description[0] == '\0')
408 ? "" : remote_description);
409 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
411 #ifdef __MSDOS__
412 /* Allow to specify a special shell just for Make,
413 and use $COMSPEC as the default $SHELL when appropriate. */
415 static char shell_str[] = "SHELL";
416 const int shlen = sizeof (shell_str) - 1;
417 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
418 struct variable *comp = lookup_variable ("COMSPEC", 7);
420 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
421 if (mshp)
422 (void) define_variable (shell_str, shlen,
423 mshp->value, o_env_override, 0);
424 else if (comp)
426 /* $COMSPEC shouldn't override $SHELL. */
427 struct variable *shp = lookup_variable (shell_str, shlen);
429 if (!shp)
430 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
433 #endif
435 /* This won't override any definition, but it
436 will provide one if there isn't one there. */
437 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
438 v->export = v_export; /* Always export SHELL. */
440 /* On MSDOS we do use SHELL from environment, since
441 it isn't a standard environment variable on MSDOS,
442 so whoever sets it, does that on purpose. */
443 #ifndef __MSDOS__
444 /* Don't let SHELL come from the environment. */
445 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
447 free (v->value);
448 v->origin = o_file;
449 v->value = xstrdup (default_shell);
451 #endif
453 /* Make sure MAKEFILES gets exported if it is set. */
454 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
455 v->export = v_ifset;
457 /* Define the magic D and F variables in terms of
458 the automatic variables they are variations of. */
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 ("+D", 2, "$(patsubst %/,%,$(dir $+))", 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);
473 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
476 int export_all_variables;
478 /* Create a new environment for FILE's commands.
479 If FILE is nil, this is for the `shell' function.
480 The child's MAKELEVEL variable is incremented. */
482 char **
483 target_environment (file)
484 struct file *file;
486 struct variable_set_list *set_list;
487 register struct variable_set_list *s;
488 struct variable_bucket
490 struct variable_bucket *next;
491 struct variable *variable;
493 struct variable_bucket **table;
494 unsigned int buckets;
495 register unsigned int i;
496 register unsigned nvariables;
497 char **result;
498 unsigned int mklev_hash;
500 if (file == 0)
501 set_list = current_variable_set_list;
502 else
503 set_list = file->variables;
505 /* Find the lowest number of buckets in any set in the list. */
506 s = set_list;
507 buckets = s->set->buckets;
508 for (s = s->next; s != 0; s = s->next)
509 if (s->set->buckets < buckets)
510 buckets = s->set->buckets;
512 /* Find the hash value of the bucket `MAKELEVEL' will fall into. */
514 char *p = "MAKELEVEL";
515 mklev_hash = 0;
516 while (*p != '\0')
517 HASH (mklev_hash, *p++);
520 /* Temporarily allocate a table with that many buckets. */
521 table = (struct variable_bucket **)
522 alloca (buckets * sizeof (struct variable_bucket *));
523 bzero ((char *) table, buckets * sizeof (struct variable_bucket *));
525 /* Run through all the variable sets in the list,
526 accumulating variables in TABLE. */
527 nvariables = 0;
528 for (s = set_list; s != 0; s = s->next)
530 register struct variable_set *set = s->set;
531 for (i = 0; i < set->buckets; ++i)
533 register struct variable *v;
534 for (v = set->table[i]; v != 0; v = v->next)
536 unsigned int j = i % buckets;
537 register struct variable_bucket *ov;
538 register char *p = v->name;
540 if (i == mklev_hash % set->buckets
541 && streq (v->name, "MAKELEVEL"))
542 /* Don't include MAKELEVEL because it will be
543 added specially at the end. */
544 continue;
546 /* If this is a per-target variable and it hasn't been touched
547 already then look up the global version and take its export
548 value. */
549 if (v->per_target && v->export == v_default)
551 struct variable *gv;
553 gv = lookup_variable_in_set(v->name, strlen(v->name),
554 &global_variable_set);
555 if (gv)
556 v->export = gv->export;
559 switch (v->export)
561 case v_default:
562 if (v->origin == o_default || v->origin == o_automatic)
563 /* Only export default variables by explicit request. */
564 continue;
566 if (! export_all_variables
567 && v->origin != o_command
568 && v->origin != o_env && v->origin != o_env_override)
569 continue;
571 if (*p != '_' && (*p < 'A' || *p > 'Z')
572 && (*p < 'a' || *p > 'z'))
573 continue;
574 for (++p; *p != '\0'; ++p)
575 if (*p != '_' && (*p < 'a' || *p > 'z')
576 && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9'))
577 continue;
578 if (*p != '\0')
579 continue;
580 break;
582 case v_export:
583 break;
585 case v_noexport:
586 continue;
588 case v_ifset:
589 if (v->origin == o_default)
590 continue;
591 break;
594 /* If this was from a different-sized hash table, then
595 recalculate the bucket it goes in. */
596 if (set->buckets != buckets)
598 register char *np;
600 j = 0;
601 for (np = v->name; *np != '\0'; ++np)
602 HASH (j, *np);
603 j %= buckets;
606 for (ov = table[j]; ov != 0; ov = ov->next)
607 if (streq (v->name, ov->variable->name))
608 break;
610 if (ov == 0)
612 register struct variable_bucket *entry;
613 entry = (struct variable_bucket *)
614 alloca (sizeof (struct variable_bucket));
615 entry->next = table[j];
616 entry->variable = v;
617 table[j] = entry;
618 ++nvariables;
624 result = (char **) xmalloc ((nvariables + 2) * sizeof (char *));
625 nvariables = 0;
626 for (i = 0; i < buckets; ++i)
628 register struct variable_bucket *b;
629 for (b = table[i]; b != 0; b = b->next)
631 register struct variable *v = b->variable;
633 /* If V is recursively expanded and didn't come from the environment,
634 expand its value. If it came from the environment, it should
635 go back into the environment unchanged. */
636 if (v->recursive
637 && v->origin != o_env && v->origin != o_env_override)
639 char *value = recursively_expand (v);
640 #ifdef WINDOWS32
641 if (strcmp(v->name, "Path") == 0 ||
642 strcmp(v->name, "PATH") == 0)
643 convert_Path_to_windows32(value, ';');
644 #endif
645 result[nvariables++] = concat (v->name, "=", value);
646 free (value);
648 else
649 #ifdef WINDOWS32
651 if (strcmp(v->name, "Path") == 0 ||
652 strcmp(v->name, "PATH") == 0)
653 convert_Path_to_windows32(v->value, ';');
654 result[nvariables++] = concat (v->name, "=", v->value);
656 #else
657 result[nvariables++] = concat (v->name, "=", v->value);
658 #endif
661 result[nvariables] = (char *) xmalloc (100);
662 (void) sprintf (result[nvariables], "MAKELEVEL=%u", makelevel + 1);
663 result[++nvariables] = 0;
665 return result;
668 /* Try to interpret LINE (a null-terminated string) as a variable definition.
670 ORIGIN may be o_file, o_override, o_env, o_env_override,
671 or o_command specifying that the variable definition comes
672 from a makefile, an override directive, the environment with
673 or without the -e switch, or the command line.
675 See the comments for parse_variable_definition().
677 If LINE was recognized as a variable definition, a pointer to its `struct
678 variable' is returned. If LINE is not a variable definition, NULL is
679 returned. */
681 struct variable *
682 try_variable_definition (flocp, line, origin, target_var)
683 const struct floc *flocp;
684 char *line;
685 enum variable_origin origin;
686 int target_var;
688 register int c;
689 register char *p = line;
690 register char *beg;
691 register char *end;
692 enum { f_bogus,
693 f_simple, f_recursive, f_append, f_conditional } flavor = f_bogus;
694 char *name, *expanded_name, *value, *alloc_value=NULL;
695 struct variable *v;
696 int append = 0;
698 while (1)
700 c = *p++;
701 if (c == '\0' || c == '#')
702 return 0;
703 if (c == '=')
705 end = p - 1;
706 flavor = f_recursive;
707 break;
709 else if (c == ':')
710 if (*p == '=')
712 end = p++ - 1;
713 flavor = f_simple;
714 break;
716 else
717 /* A colon other than := is a rule line, not a variable defn. */
718 return 0;
719 else if (c == '+' && *p == '=')
721 end = p++ - 1;
722 flavor = f_append;
723 break;
725 else if (c == '?' && *p == '=')
727 end = p++ - 1;
728 flavor = f_conditional;
729 break;
731 else if (c == '$')
733 /* This might begin a variable expansion reference. Make sure we
734 don't misrecognize chars inside the reference as =, := or +=. */
735 char closeparen;
736 int count;
737 c = *p++;
738 if (c == '(')
739 closeparen = ')';
740 else if (c == '{')
741 closeparen = '}';
742 else
743 continue; /* Nope. */
745 /* P now points past the opening paren or brace.
746 Count parens or braces until it is matched. */
747 count = 0;
748 for (; *p != '\0'; ++p)
750 if (*p == c)
751 ++count;
752 else if (*p == closeparen && --count < 0)
754 ++p;
755 break;
761 beg = next_token (line);
762 while (end > beg && isblank (end[-1]))
763 --end;
764 p = next_token (p);
766 /* Expand the name, so "$(foo)bar = baz" works. */
767 name = (char *) alloca (end - beg + 1);
768 bcopy (beg, name, end - beg);
769 name[end - beg] = '\0';
770 expanded_name = allocated_variable_expand (name);
772 if (expanded_name[0] == '\0')
773 fatal (flocp, _("empty variable name"));
775 /* Calculate the variable's new value in VALUE. */
777 switch (flavor)
779 case f_bogus:
780 /* Should not be possible. */
781 abort ();
782 case f_simple:
783 /* A simple variable definition "var := value". Expand the value.
784 We have to allocate memory since otherwise it'll clobber the
785 variable buffer, and we may still need that if we're looking at a
786 target-specific variable. */
787 value = alloc_value = allocated_variable_expand (p);
788 break;
789 case f_conditional:
790 /* A conditional variable definition "var ?= value".
791 The value is set IFF the variable is not defined yet. */
792 v = lookup_variable(expanded_name, strlen(expanded_name));
793 if (v)
795 free(expanded_name);
796 return v;
798 flavor = f_recursive;
799 /* FALLTHROUGH */
800 case f_recursive:
801 /* A recursive variable definition "var = value".
802 The value is used verbatim. */
803 value = p;
804 break;
805 case f_append:
806 /* If we have += but we're in a target variable context, defer the
807 append until the context expansion. */
808 if (target_var)
810 append = 1;
811 flavor = f_recursive;
812 value = p;
813 break;
816 /* An appending variable definition "var += value".
817 Extract the old value and append the new one. */
818 v = lookup_variable (expanded_name, strlen (expanded_name));
819 if (v == 0)
821 /* There was no old value.
822 This becomes a normal recursive definition. */
823 value = p;
824 flavor = f_recursive;
826 else
828 /* Paste the old and new values together in VALUE. */
830 unsigned int oldlen, newlen;
832 if (v->recursive)
833 /* The previous definition of the variable was recursive.
834 The new value comes from the unexpanded old and new values. */
835 flavor = f_recursive;
836 else
837 /* The previous definition of the variable was simple.
838 The new value comes from the old value, which was expanded
839 when it was set; and from the expanded new value. Allocate
840 memory for the expansion as we may still need the rest of the
841 buffer if we're looking at a target-specific variable. */
842 p = alloc_value = allocated_variable_expand (p);
844 oldlen = strlen (v->value);
845 newlen = strlen (p);
846 value = (char *) alloca (oldlen + 1 + newlen + 1);
847 bcopy (v->value, value, oldlen);
848 value[oldlen] = ' ';
849 bcopy (p, &value[oldlen + 1], newlen + 1);
853 #ifdef __MSDOS__
854 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
855 non-Unix systems don't conform to this default configuration (in
856 fact, most of them don't even have `/bin'). On the other hand,
857 $SHELL in the environment, if set, points to the real pathname of
858 the shell.
859 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
860 the Makefile override $SHELL from the environment. But first, we
861 look for the basename of the shell in the directory where SHELL=
862 points, and along the $PATH; if it is found in any of these places,
863 we define $SHELL to be the actual pathname of the shell. Thus, if
864 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
865 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
866 defining SHELL to be "d:/unix/bash.exe". */
867 if ((origin == o_file || origin == o_override)
868 && strcmp (expanded_name, "SHELL") == 0)
870 char shellpath[PATH_MAX];
871 extern char * __dosexec_find_on_path (const char *, char *[], char *);
873 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
874 if (__dosexec_find_on_path (value, (char **)0, shellpath))
876 char *p;
878 for (p = shellpath; *p; p++)
880 if (*p == '\\')
881 *p = '/';
883 v = define_variable (expanded_name, strlen (expanded_name),
884 shellpath, origin, flavor == f_recursive);
886 else
888 char *shellbase, *bslash;
889 struct variable *pathv = lookup_variable ("PATH", 4);
890 char *path_string;
891 char *fake_env[2];
892 size_t pathlen = 0;
894 shellbase = strrchr (value, '/');
895 bslash = strrchr (value, '\\');
896 if (!shellbase || bslash > shellbase)
897 shellbase = bslash;
898 if (!shellbase && value[1] == ':')
899 shellbase = value + 1;
900 if (shellbase)
901 shellbase++;
902 else
903 shellbase = value;
905 /* Search for the basename of the shell (with standard
906 executable extensions) along the $PATH. */
907 if (pathv)
908 pathlen = strlen (pathv->value);
909 path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
910 /* On MSDOS, current directory is considered as part of $PATH. */
911 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
912 fake_env[0] = path_string;
913 fake_env[1] = (char *)0;
914 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
916 char *p;
918 for (p = shellpath; *p; p++)
920 if (*p == '\\')
921 *p = '/';
923 v = define_variable (expanded_name, strlen (expanded_name),
924 shellpath, origin, flavor == f_recursive);
926 else
927 v = lookup_variable (expanded_name, strlen (expanded_name));
929 free (path_string);
932 else
933 #endif /* __MSDOS__ */
934 #ifdef WINDOWS32
935 if ((origin == o_file || origin == o_override)
936 && strcmp (expanded_name, "SHELL") == 0) {
937 extern char* default_shell;
940 * Call shell locator function. If it returns TRUE, then
941 * set no_default_sh_exe to indicate sh was found and
942 * set new value for SHELL variable.
944 if (find_and_set_default_shell(value)) {
945 v = define_variable (expanded_name, strlen (expanded_name),
946 default_shell, origin, flavor == f_recursive);
947 no_default_sh_exe = 0;
949 } else
950 #endif
952 v = define_variable (expanded_name, strlen (expanded_name),
953 value, origin, flavor == f_recursive);
955 v->append = append;
957 if (alloc_value)
958 free (alloc_value);
959 free (expanded_name);
961 return v;
964 /* Print information for variable V, prefixing it with PREFIX. */
966 static void
967 print_variable (v, prefix)
968 register struct variable *v;
969 char *prefix;
971 const char *origin;
973 switch (v->origin)
975 case o_default:
976 origin = "default";
977 break;
978 case o_env:
979 origin = "environment";
980 break;
981 case o_file:
982 origin = "makefile";
983 break;
984 case o_env_override:
985 origin = "environment under -e";
986 break;
987 case o_command:
988 origin = "command line";
989 break;
990 case o_override:
991 origin = "`override' directive";
992 break;
993 case o_automatic:
994 origin = "automatic";
995 break;
996 case o_invalid:
997 default:
998 abort ();
1000 printf ("# %s\n", origin);
1002 fputs (prefix, stdout);
1004 /* Is this a `define'? */
1005 if (v->recursive && strchr (v->value, '\n') != 0)
1006 printf ("define %s\n%s\nendef\n", v->name, v->value);
1007 else
1009 register char *p;
1011 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1013 /* Check if the value is just whitespace. */
1014 p = next_token (v->value);
1015 if (p != v->value && *p == '\0')
1016 /* All whitespace. */
1017 printf ("$(subst ,,%s)", v->value);
1018 else if (v->recursive)
1019 fputs (v->value, stdout);
1020 else
1021 /* Double up dollar signs. */
1022 for (p = v->value; *p != '\0'; ++p)
1024 if (*p == '$')
1025 putchar ('$');
1026 putchar (*p);
1028 putchar ('\n');
1033 /* Print all the variables in SET. PREFIX is printed before
1034 the actual variable definitions (everything else is comments). */
1036 void
1037 print_variable_set (set, prefix)
1038 register struct variable_set *set;
1039 char *prefix;
1041 register unsigned int i, nvariables, per_bucket;
1042 register struct variable *v;
1044 per_bucket = nvariables = 0;
1045 for (i = 0; i < set->buckets; ++i)
1047 register unsigned int this_bucket = 0;
1049 for (v = set->table[i]; v != 0; v = v->next)
1051 ++this_bucket;
1052 print_variable (v, prefix);
1055 nvariables += this_bucket;
1056 if (this_bucket > per_bucket)
1057 per_bucket = this_bucket;
1060 if (nvariables == 0)
1061 puts (_("# No variables."));
1062 else
1064 printf (_("# %u variables in %u hash buckets.\n"),
1065 nvariables, set->buckets);
1066 #ifndef NO_FLOAT
1067 printf (_("# average of %.1f variables per bucket, \
1068 max %u in one bucket.\n"),
1069 (double) nvariables / (double) set->buckets,
1070 per_bucket);
1071 #else
1073 int f = (nvariables * 1000 + 5) / set->buckets;
1074 printf (_("# average of %d.%d variables per bucket, \
1075 max %u in one bucket.\n"),
1076 f/10, f%10,
1077 per_bucket);
1079 #endif
1084 /* Print the data base of variables. */
1086 void
1087 print_variable_data_base ()
1089 puts (_("\n# Variables\n"));
1091 print_variable_set (&global_variable_set, "");
1095 /* Print all the local variables of FILE. */
1097 void
1098 print_file_variables (file)
1099 struct file *file;
1101 if (file->variables != 0)
1102 print_variable_set (file->variables->set, "# ");
1105 #ifdef WINDOWS32
1106 void
1107 sync_Path_environment(void)
1109 char* path = allocated_variable_expand("$(Path)");
1110 static char* environ_path = NULL;
1112 if (!path)
1113 return;
1116 * If done this before, don't leak memory unnecessarily.
1117 * Free the previous entry before allocating new one.
1119 if (environ_path)
1120 free(environ_path);
1123 * Create something WINDOWS32 world can grok
1125 convert_Path_to_windows32(path, ';');
1126 environ_path = concat("Path", "=", path);
1127 putenv(environ_path);
1128 free(path);
1130 #endif