(\startcontents): Leave ^ catcode as other.
[make.git] / variable.c
blob8f334c76565592ad2889c5b12d1a2dc240447634
1 /* Internals of variables for GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 96 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 *define_variable_in_set PARAMS ((char *name, unsigned int length,
48 char *value, enum variable_origin origin,
49 int recursive, struct variable_set *set));
52 /* Implement variables. */
54 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
55 LENGTH is the length of NAME, which does not need to be null-terminated.
56 ORIGIN specifies the origin of the variable (makefile, command line
57 or environment).
58 If RECURSIVE is nonzero a flag is set in the variable saying
59 that it should be recursively re-expanded. */
61 static struct variable *
62 define_variable_in_set (name, length, value, origin, recursive, set)
63 char *name;
64 unsigned int length;
65 char *value;
66 enum variable_origin origin;
67 int recursive;
68 struct variable_set *set;
70 register unsigned int i;
71 register unsigned int hashval;
72 register struct variable *v;
74 hashval = 0;
75 for (i = 0; i < length; ++i)
76 HASH (hashval, name[i]);
77 hashval %= set->buckets;
79 for (v = set->table[hashval]; v != 0; v = v->next)
80 if (*v->name == *name
81 && !strncmp (v->name + 1, name + 1, length - 1)
82 && v->name[length] == '\0')
83 break;
85 if (env_overrides && origin == o_env)
86 origin = o_env_override;
88 if (v != 0)
90 if (env_overrides && v->origin == o_env)
91 /* V came from in the environment. Since it was defined
92 before the switches were parsed, it wasn't affected by -e. */
93 v->origin = o_env_override;
95 /* A variable of this name is already defined.
96 If the old definition is from a stronger source
97 than this one, don't redefine it. */
98 if ((int) origin >= (int) v->origin)
100 if (v->value != 0)
101 free (v->value);
102 v->value = savestring (value, strlen (value));
103 v->origin = origin;
104 v->recursive = recursive;
106 return v;
109 /* Create a new variable definition and add it to the hash table. */
111 v = (struct variable *) xmalloc (sizeof (struct variable));
112 v->name = savestring (name, length);
113 v->value = savestring (value, strlen (value));
114 v->origin = origin;
115 v->recursive = recursive;
116 v->expanding = 0;
117 v->export = v_default;
118 v->next = set->table[hashval];
119 set->table[hashval] = v;
120 return v;
123 /* Define a variable in the current variable set. */
125 struct variable *
126 define_variable (name, length, value, origin, recursive)
127 char *name;
128 unsigned int length;
129 char *value;
130 enum variable_origin origin;
131 int recursive;
133 return define_variable_in_set (name, length, value, origin, recursive,
134 current_variable_set_list->set);
137 /* Define a variable in FILE's variable set. */
139 struct variable *
140 define_variable_for_file (name, length, value, origin, recursive, file)
141 char *name;
142 unsigned int length;
143 char *value;
144 enum variable_origin origin;
145 int recursive;
146 struct file *file;
148 return define_variable_in_set (name, length, value, origin, recursive,
149 file->variables->set);
152 /* Lookup a variable whose name is a string starting at NAME
153 and with LENGTH chars. NAME need not be null-terminated.
154 Returns address of the `struct variable' containing all info
155 on the variable, or nil if no such variable is defined. */
157 struct variable *
158 lookup_variable (name, length)
159 char *name;
160 unsigned int length;
162 register struct variable_set_list *setlist;
164 register unsigned int i;
165 register unsigned int rawhash = 0;
167 for (i = 0; i < length; ++i)
168 HASH (rawhash, name[i]);
170 for (setlist = current_variable_set_list;
171 setlist != 0; setlist = setlist->next)
173 register struct variable_set *set = setlist->set;
174 register unsigned int hashval = rawhash % set->buckets;
175 register struct variable *v;
177 for (v = set->table[hashval]; v != 0; v = v->next)
178 if (*v->name == *name
179 && !strncmp (v->name + 1, name + 1, length - 1)
180 && v->name[length] == 0)
181 return v;
184 return 0;
187 /* Initialize FILE's variable set list. If FILE already has a variable set
188 list, the topmost variable set is left intact, but the the rest of the
189 chain is replaced with FILE->parent's setlist. */
191 void
192 initialize_file_variables (file)
193 struct file *file;
195 register struct variable_set_list *l = file->variables;
196 if (l == 0)
198 l = (struct variable_set_list *)
199 xmalloc (sizeof (struct variable_set_list));
200 l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
201 l->set->buckets = PERFILE_VARIABLE_BUCKETS;
202 l->set->table = (struct variable **)
203 xmalloc (l->set->buckets * sizeof (struct variable *));
204 bzero ((char *) l->set->table,
205 l->set->buckets * sizeof (struct variable *));
206 file->variables = l;
209 if (file->parent == 0)
210 l->next = &global_setlist;
211 else
213 if (file->parent->variables == 0)
214 initialize_file_variables (file->parent);
215 l->next = file->parent->variables;
219 /* Pop the top set off the current variable set list,
220 and free all its storage. */
222 void
223 pop_variable_scope ()
225 register struct variable_set_list *setlist = current_variable_set_list;
226 register struct variable_set *set = setlist->set;
227 register unsigned int i;
229 current_variable_set_list = setlist->next;
230 free ((char *) setlist);
232 for (i = 0; i < set->buckets; ++i)
234 register struct variable *next = set->table[i];
235 while (next != 0)
237 register struct variable *v = next;
238 next = v->next;
240 free (v->name);
241 free ((char *) v);
244 free ((char *) set->table);
245 free ((char *) set);
248 /* Create a new variable set and push it on the current setlist. */
250 void
251 push_new_variable_scope ()
253 register struct variable_set_list *setlist;
254 register struct variable_set *set;
256 set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
257 set->buckets = SMALL_SCOPE_VARIABLE_BUCKETS;
258 set->table = (struct variable **)
259 xmalloc (set->buckets * sizeof (struct variable *));
260 bzero ((char *) set->table, set->buckets * sizeof (struct variable *));
262 setlist = (struct variable_set_list *)
263 xmalloc (sizeof (struct variable_set_list));
264 setlist->set = set;
265 setlist->next = current_variable_set_list;
266 current_variable_set_list = setlist;
269 /* Merge SET1 into SET0, freeing unused storage in SET1. */
271 static void
272 merge_variable_sets (set0, set1)
273 struct variable_set *set0, *set1;
275 register unsigned int bucket1;
277 for (bucket1 = 0; bucket1 < set1->buckets; ++bucket1)
279 register struct variable *v1 = set1->table[bucket1];
280 while (v1 != 0)
282 struct variable *next = v1->next;
283 unsigned int bucket0;
284 register struct variable *v0;
286 if (set1->buckets >= set0->buckets)
287 bucket0 = bucket1;
288 else
290 register char *n;
291 bucket0 = 0;
292 for (n = v1->name; *n != '\0'; ++n)
293 HASH (bucket0, *n);
295 bucket0 %= set0->buckets;
297 for (v0 = set0->table[bucket0]; v0 != 0; v0 = v0->next)
298 if (streq (v0->name, v1->name))
299 break;
301 if (v0 == 0)
303 /* There is no variable in SET0 with the same name. */
304 v1->next = set0->table[bucket0];
305 set0->table[bucket0] = v1;
307 else
309 /* The same variable exists in both sets.
310 SET0 takes precedence. */
311 free (v1->value);
312 free ((char *) v1);
315 v1 = next;
320 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
322 void
323 merge_variable_set_lists (setlist0, setlist1)
324 struct variable_set_list **setlist0, *setlist1;
326 register struct variable_set_list *list0 = *setlist0;
327 struct variable_set_list *last0 = 0;
329 while (setlist1 != 0 && list0 != 0)
331 struct variable_set_list *next = setlist1;
332 setlist1 = setlist1->next;
334 merge_variable_sets (list0->set, next->set);
336 free ((char *) next);
338 last0 = list0;
339 list0 = list0->next;
342 if (setlist1 != 0)
344 if (last0 == 0)
345 *setlist0 = setlist1;
346 else
347 last0->next = setlist1;
351 /* Define the automatic variables, and record the addresses
352 of their structures so we can change their values quickly. */
354 void
355 define_automatic_variables ()
357 #ifdef WINDOWS32
358 extern char* default_shell;
359 #else
360 extern char default_shell[];
361 #endif
362 register struct variable *v;
363 char buf[200];
365 sprintf (buf, "%u", makelevel);
366 (void) define_variable ("MAKELEVEL", 9, buf, o_env, 0);
368 sprintf (buf, "%s%s%s",
369 version_string,
370 (remote_description == 0 || remote_description[0] == '\0')
371 ? "" : "-",
372 (remote_description == 0 || remote_description[0] == '\0')
373 ? "" : remote_description);
374 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
376 #ifdef __MSDOS__
377 /* Allow to specify a special shell just for Make,
378 and use $COMSPEC as the default $SHELL when appropriate. */
380 static char shell_str[] = "SHELL";
381 const int shlen = sizeof (shell_str) - 1;
382 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
383 struct variable *comp = lookup_variable ("COMSPEC", 7);
385 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
386 if (mshp)
387 (void) define_variable (shell_str, shlen,
388 mshp->value, o_env_override, 0);
389 else if (comp)
391 /* $COMSPEC shouldn't override $SHELL. */
392 struct variable *shp = lookup_variable (shell_str, shlen);
394 if (!shp)
395 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
398 #endif
400 /* This won't override any definition, but it
401 will provide one if there isn't one there. */
402 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
403 v->export = v_export; /* Always export SHELL. */
405 /* On MSDOS we do use SHELL from environment, since
406 it isn't a standard environment variable on MSDOS,
407 so whoever sets it, does that on purpose. */
408 #ifndef __MSDOS__
409 /* Don't let SHELL come from the environment. */
410 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
412 free (v->value);
413 v->origin = o_file;
414 v->value = savestring (default_shell, strlen (default_shell));
416 #endif
418 /* Make sure MAKEFILES gets exported if it is set. */
419 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
420 v->export = v_ifset;
422 /* Define the magic D and F variables in terms of
423 the automatic variables they are variations of. */
425 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
426 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
427 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
428 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
429 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
430 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
431 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
432 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
433 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
434 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
435 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
436 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
437 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
438 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
441 int export_all_variables;
443 /* Create a new environment for FILE's commands.
444 If FILE is nil, this is for the `shell' function.
445 The child's MAKELEVEL variable is incremented. */
447 char **
448 target_environment (file)
449 struct file *file;
451 struct variable_set_list *set_list;
452 register struct variable_set_list *s;
453 struct variable_bucket
455 struct variable_bucket *next;
456 struct variable *variable;
458 struct variable_bucket **table;
459 unsigned int buckets;
460 register unsigned int i;
461 register unsigned nvariables;
462 char **result;
463 unsigned int mklev_hash;
465 if (file == 0)
466 set_list = current_variable_set_list;
467 else
468 set_list = file->variables;
470 /* Find the lowest number of buckets in any set in the list. */
471 s = set_list;
472 buckets = s->set->buckets;
473 for (s = s->next; s != 0; s = s->next)
474 if (s->set->buckets < buckets)
475 buckets = s->set->buckets;
477 /* Find the hash value of the bucket `MAKELEVEL' will fall into. */
479 char *p = "MAKELEVEL";
480 mklev_hash = 0;
481 while (*p != '\0')
482 HASH (mklev_hash, *p++);
485 /* Temporarily allocate a table with that many buckets. */
486 table = (struct variable_bucket **)
487 alloca (buckets * sizeof (struct variable_bucket *));
488 bzero ((char *) table, buckets * sizeof (struct variable_bucket *));
490 /* Run through all the variable sets in the list,
491 accumulating variables in TABLE. */
492 nvariables = 0;
493 for (s = set_list; s != 0; s = s->next)
495 register struct variable_set *set = s->set;
496 for (i = 0; i < set->buckets; ++i)
498 register struct variable *v;
499 for (v = set->table[i]; v != 0; v = v->next)
501 unsigned int j = i % buckets;
502 register struct variable_bucket *ov;
503 register char *p = v->name;
505 if (i == mklev_hash % set->buckets
506 && streq (v->name, "MAKELEVEL"))
507 /* Don't include MAKELEVEL because it will be
508 added specially at the end. */
509 continue;
511 switch (v->export)
513 case v_default:
514 if (v->origin == o_default || v->origin == o_automatic)
515 /* Only export default variables by explicit request. */
516 continue;
518 if (! export_all_variables
519 && v->origin != o_command
520 && v->origin != o_env && v->origin != o_env_override)
521 continue;
523 if (*p != '_' && (*p < 'A' || *p > 'Z')
524 && (*p < 'a' || *p > 'z'))
525 continue;
526 for (++p; *p != '\0'; ++p)
527 if (*p != '_' && (*p < 'a' || *p > 'z')
528 && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9'))
529 break;
530 if (*p != '\0')
531 continue;
533 case v_export:
534 break;
536 case v_noexport:
537 continue;
539 case v_ifset:
540 if (v->origin == o_default)
541 continue;
542 break;
545 for (ov = table[j]; ov != 0; ov = ov->next)
546 if (streq (v->name, ov->variable->name))
547 break;
548 if (ov == 0)
550 register struct variable_bucket *entry;
551 entry = (struct variable_bucket *)
552 alloca (sizeof (struct variable_bucket));
553 entry->next = table[j];
554 entry->variable = v;
555 table[j] = entry;
556 ++nvariables;
562 result = (char **) xmalloc ((nvariables + 2) * sizeof (char *));
563 nvariables = 0;
564 for (i = 0; i < buckets; ++i)
566 register struct variable_bucket *b;
567 for (b = table[i]; b != 0; b = b->next)
569 register struct variable *v = b->variable;
570 /* If V is recursively expanded and didn't come from the environment,
571 expand its value. If it came from the environment, it should
572 go back into the environment unchanged. */
573 if (v->recursive
574 && v->origin != o_env && v->origin != o_env_override)
576 char *value = recursively_expand (v);
577 #ifdef WINDOWS32
578 if (strcmp(v->name, "Path") == 0 ||
579 strcmp(v->name, "PATH") == 0)
580 convert_Path_to_windows32(value, ';');
581 #endif
582 result[nvariables++] = concat (v->name, "=", value);
583 free (value);
585 else
586 #ifdef WINDOWS32
588 if (strcmp(v->name, "Path") == 0 ||
589 strcmp(v->name, "PATH") == 0)
590 convert_Path_to_windows32(v->value, ';');
591 result[nvariables++] = concat (v->name, "=", v->value);
593 #else
594 result[nvariables++] = concat (v->name, "=", v->value);
595 #endif
598 result[nvariables] = (char *) xmalloc (100);
599 (void) sprintf (result[nvariables], "MAKELEVEL=%u", makelevel + 1);
600 result[++nvariables] = 0;
602 return result;
605 /* Try to interpret LINE (a null-terminated string) as a variable definition.
607 ORIGIN may be o_file, o_override, o_env, o_env_override,
608 or o_command specifying that the variable definition comes
609 from a makefile, an override directive, the environment with
610 or without the -e switch, or the command line.
612 A variable definition has the form "name = value" or "name := value".
613 Any whitespace around the "=" or ":=" is removed. The first form
614 defines a variable that is recursively re-evaluated. The second form
615 defines a variable whose value is variable-expanded at the time of
616 definition and then is evaluated only once at the time of expansion.
618 If a variable was defined, a pointer to its `struct variable' is returned.
619 If not, NULL is returned. */
621 struct variable *
622 try_variable_definition (filename, lineno, line, origin)
623 char *filename;
624 unsigned int lineno;
625 char *line;
626 enum variable_origin origin;
628 register int c;
629 register char *p = line;
630 register char *beg;
631 register char *end;
632 enum { bogus, simple, recursive, append } flavor = bogus;
633 char *name, *expanded_name, *value;
634 struct variable *v;
636 while (1)
638 c = *p++;
639 if (c == '\0' || c == '#')
640 return 0;
641 if (c == '=')
643 end = p - 1;
644 flavor = recursive;
645 break;
647 else if (c == ':')
648 if (*p == '=')
650 end = p++ - 1;
651 flavor = simple;
652 break;
654 else
655 /* A colon other than := is a rule line, not a variable defn. */
656 return 0;
657 else if (c == '+' && *p == '=')
659 end = p++ - 1;
660 flavor = append;
661 break;
663 else if (c == '$')
665 /* This might begin a variable expansion reference. Make sure we
666 don't misrecognize chars inside the reference as =, := or +=. */
667 char closeparen;
668 int count;
669 c = *p++;
670 if (c == '(')
671 closeparen = ')';
672 else if (c == '{')
673 closeparen = '}';
674 else
675 continue; /* Nope. */
677 /* P now points past the opening paren or brace.
678 Count parens or braces until it is matched. */
679 count = 0;
680 for (; *p != '\0'; ++p)
682 if (*p == c)
683 ++count;
684 else if (*p == closeparen && --count < 0)
686 ++p;
687 break;
693 beg = next_token (line);
694 while (end > beg && isblank (end[-1]))
695 --end;
696 p = next_token (p);
698 /* Expand the name, so "$(foo)bar = baz" works. */
699 name = (char *) alloca (end - beg + 1);
700 bcopy (beg, name, end - beg);
701 name[end - beg] = '\0';
702 expanded_name = allocated_variable_expand (name);
704 if (expanded_name[0] == '\0')
706 if (filename == 0)
707 fatal ("empty variable name");
708 else
709 makefile_fatal (filename, lineno, "empty variable name");
712 /* Calculate the variable's new value in VALUE. */
714 switch (flavor)
716 case bogus:
717 /* Should not be possible. */
718 abort ();
719 return 0;
720 case simple:
721 /* A simple variable definition "var := value". Expand the value. */
722 value = variable_expand (p);
723 break;
724 case recursive:
725 /* A recursive variable definition "var = value".
726 The value is used verbatim. */
727 value = p;
728 break;
729 case append:
730 /* An appending variable definition "var += value".
731 Extract the old value and append the new one. */
732 v = lookup_variable (expanded_name, strlen (expanded_name));
733 if (v == 0)
735 /* There was no old value.
736 This becomes a normal recursive definition. */
737 value = p;
738 flavor = recursive;
740 else
742 /* Paste the old and new values together in VALUE. */
744 unsigned int oldlen, newlen;
746 if (v->recursive)
747 /* The previous definition of the variable was recursive.
748 The new value comes from the unexpanded old and new values. */
749 flavor = recursive;
750 else
751 /* The previous definition of the variable was simple.
752 The new value comes from the old value, which was expanded
753 when it was set; and from the expanded new value. */
754 p = variable_expand (p);
756 oldlen = strlen (v->value);
757 newlen = strlen (p);
758 value = (char *) alloca (oldlen + 1 + newlen + 1);
759 bcopy (v->value, value, oldlen);
760 value[oldlen] = ' ';
761 bcopy (p, &value[oldlen + 1], newlen + 1);
765 #ifdef __MSDOS__
766 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
767 non-Unix systems don't conform to this default configuration (in
768 fact, most of them don't even have `/bin'). On the other hand,
769 $SHELL in the environment, if set, points to the real pathname of
770 the shell.
771 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
772 the Makefile override $SHELL from the environment. But first, we
773 look for the basename of the shell in the directory where SHELL=
774 points, and along the $PATH; if it is found in any of these places,
775 we define $SHELL to be the actual pathname of the shell. Thus, if
776 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
777 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
778 defining SHELL to be "d:/unix/bash.exe". */
779 if (origin == o_file
780 && strcmp (expanded_name, "SHELL") == 0)
782 char shellpath[PATH_MAX];
783 extern char * __dosexec_find_on_path (const char *, char *[], char *);
785 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
786 if (__dosexec_find_on_path (value, (char **)0, shellpath))
788 char *p;
790 for (p = shellpath; *p; p++)
792 if (*p == '\\')
793 *p = '/';
795 v = define_variable (expanded_name, strlen (expanded_name),
796 shellpath, origin, flavor == recursive);
798 else
800 char *shellbase, *bslash;
801 struct variable *pathv = lookup_variable ("PATH", 4);
802 char *path_string;
803 char *fake_env[2];
804 size_t pathlen = 0;
806 shellbase = rindex (value, '/');
807 bslash = rindex (value, '\\');
808 if (!shellbase || bslash > shellbase)
809 shellbase = bslash;
810 if (!shellbase && value[1] == ':')
811 shellbase = value + 1;
812 if (shellbase)
813 shellbase++;
814 else
815 shellbase = value;
817 /* Search for the basename of the shell (with standard
818 executable extensions) along the $PATH. */
819 if (pathv)
820 pathlen = strlen (pathv->value);
821 path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
822 /* On MSDOS, current directory is considered as part of $PATH. */
823 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
824 fake_env[0] = path_string;
825 fake_env[1] = (char *)0;
826 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
828 char *p;
830 for (p = shellpath; *p; p++)
832 if (*p == '\\')
833 *p = '/';
835 v = define_variable (expanded_name, strlen (expanded_name),
836 shellpath, origin, flavor == recursive);
838 else
839 v = lookup_variable (expanded_name, strlen (expanded_name));
841 free (path_string);
844 else
845 #endif /* __MSDOS__ */
847 v = define_variable (expanded_name, strlen (expanded_name),
848 value, origin, flavor == recursive);
850 free (expanded_name);
852 return v;
855 /* Print information for variable V, prefixing it with PREFIX. */
857 static void
858 print_variable (v, prefix)
859 register struct variable *v;
860 char *prefix;
862 char *origin;
864 switch (v->origin)
866 case o_default:
867 origin = "default";
868 break;
869 case o_env:
870 origin = "environment";
871 break;
872 case o_file:
873 origin = "makefile";
874 break;
875 case o_env_override:
876 origin = "environment under -e";
877 break;
878 case o_command:
879 origin = "command line";
880 break;
881 case o_override:
882 origin = "`override' directive";
883 break;
884 case o_automatic:
885 origin = "automatic";
886 break;
887 case o_invalid:
888 default:
889 abort ();
890 break;
892 printf ("# %s\n", origin);
894 fputs (prefix, stdout);
896 /* Is this a `define'? */
897 if (v->recursive && index (v->value, '\n') != 0)
898 printf ("define %s\n%s\nendef\n", v->name, v->value);
899 else
901 register char *p;
903 printf ("%s %s= ", v->name, v->recursive ? "" : ":");
905 /* Check if the value is just whitespace. */
906 p = next_token (v->value);
907 if (p != v->value && *p == '\0')
908 /* All whitespace. */
909 printf ("$(subst ,,%s)", v->value);
910 else if (v->recursive)
911 fputs (v->value, stdout);
912 else
913 /* Double up dollar signs. */
914 for (p = v->value; *p != '\0'; ++p)
916 if (*p == '$')
917 putchar ('$');
918 putchar (*p);
920 putchar ('\n');
925 /* Print all the variables in SET. PREFIX is printed before
926 the actual variable definitions (everything else is comments). */
928 static void
929 print_variable_set (set, prefix)
930 register struct variable_set *set;
931 char *prefix;
933 register unsigned int i, nvariables, per_bucket;
934 register struct variable *v;
936 per_bucket = nvariables = 0;
937 for (i = 0; i < set->buckets; ++i)
939 register unsigned int this_bucket = 0;
941 for (v = set->table[i]; v != 0; v = v->next)
943 ++this_bucket;
944 print_variable (v, prefix);
947 nvariables += this_bucket;
948 if (this_bucket > per_bucket)
949 per_bucket = this_bucket;
952 if (nvariables == 0)
953 puts ("# No variables.");
954 else
956 printf ("# %u variables in %u hash buckets.\n",
957 nvariables, set->buckets);
958 #ifndef NO_FLOAT
959 printf ("# average of %.1f variables per bucket, \
960 max %u in one bucket.\n",
961 (double) nvariables / (double) set->buckets,
962 per_bucket);
963 #else
965 int f = (nvariables * 1000 + 5) / set->buckets;
966 printf ("# average of %d.%d variables per bucket, \
967 max %u in one bucket.\n",
968 f/10, f%10,
969 per_bucket);
971 #endif
976 /* Print the data base of variables. */
978 void
979 print_variable_data_base ()
981 puts ("\n# Variables\n");
983 print_variable_set (&global_variable_set, "");
987 /* Print all the local variables of FILE. */
989 void
990 print_file_variables (file)
991 struct file *file;
993 if (file->variables != 0)
994 print_variable_set (file->variables->set, "# ");
997 #ifdef WINDOWS32
998 void
999 sync_Path_environment(void)
1001 char* path = allocated_variable_expand("$(Path)");
1002 static char* environ_path = NULL;
1004 if (!path)
1005 return;
1008 * If done this before, don't leak memory unnecessarily.
1009 * Free the previous entry before allocating new one.
1011 if (environ_path)
1012 free(environ_path);
1015 * Create something WINDOWS32 world can grok
1017 convert_Path_to_windows32(path, ';');
1018 environ_path = concat("Path", "=", path);
1019 putenv(environ_path);
1020 free(path);
1022 #endif