* Minor code cleanups
[make.git] / variable.c
blob300368dfd11d086c463f54adaa0718c4f7145395
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 #include "rule.h"
27 #ifdef WINDOWS32
28 #include "pathstuff.h"
29 #endif
31 /* Hash table of all global variable definitions. */
33 #ifndef VARIABLE_BUCKETS
34 #define VARIABLE_BUCKETS 523
35 #endif
36 #ifndef PERFILE_VARIABLE_BUCKETS
37 #define PERFILE_VARIABLE_BUCKETS 23
38 #endif
39 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
40 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
41 #endif
42 static struct variable *variable_table[VARIABLE_BUCKETS];
43 static struct variable_set global_variable_set
44 = { variable_table, VARIABLE_BUCKETS };
45 static struct variable_set_list global_setlist
46 = { 0, &global_variable_set };
47 struct variable_set_list *current_variable_set_list = &global_setlist;
49 static struct variable *lookup_variable_in_set PARAMS ((char *name,
50 unsigned int length, 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 struct variable *
62 define_variable_in_set (name, length, value, origin, recursive, set, flocp)
63 char *name;
64 unsigned int length;
65 char *value;
66 enum variable_origin origin;
67 int recursive;
68 struct variable_set *set;
69 const struct floc *flocp;
71 register unsigned int i;
72 register unsigned int hashval;
73 register struct variable *v;
75 hashval = 0;
76 for (i = 0; i < length; ++i)
77 HASH (hashval, name[i]);
78 hashval %= set->buckets;
80 for (v = set->table[hashval]; v != 0; v = v->next)
81 if (*v->name == *name
82 && strneq (v->name + 1, name + 1, length - 1)
83 && v->name[length] == '\0')
84 break;
86 if (env_overrides && origin == o_env)
87 origin = o_env_override;
89 if (v != 0)
91 if (env_overrides && v->origin == o_env)
92 /* V came from in the environment. Since it was defined
93 before the switches were parsed, it wasn't affected by -e. */
94 v->origin = o_env_override;
96 /* A variable of this name is already defined.
97 If the old definition is from a stronger source
98 than this one, don't redefine it. */
99 if ((int) origin >= (int) v->origin)
101 if (v->value != 0)
102 free (v->value);
103 v->value = xstrdup (value);
104 if (flocp != 0)
105 v->fileinfo = *flocp;
106 else
107 v->fileinfo.filenm = 0;
108 v->origin = origin;
109 v->recursive = recursive;
111 return v;
114 /* Create a new variable definition and add it to the hash table. */
116 v = (struct variable *) xmalloc (sizeof (struct variable));
117 v->name = savestring (name, length);
118 v->value = xstrdup (value);
119 if (flocp != 0)
120 v->fileinfo = *flocp;
121 else
122 v->fileinfo.filenm = 0;
123 v->origin = origin;
124 v->recursive = recursive;
125 v->expanding = 0;
126 v->per_target = 0;
127 v->append = 0;
128 v->export = v_default;
129 v->next = set->table[hashval];
130 set->table[hashval] = v;
131 return v;
134 /* Lookup a variable whose name is a string starting at NAME
135 and with LENGTH chars. NAME need not be null-terminated.
136 Returns address of the `struct variable' containing all info
137 on the variable, or nil if no such variable is defined.
139 If we find a variable which is in the process of being expanded,
140 try to find one further up the set_list chain. If we don't find
141 one that isn't being expanded, return a pointer to whatever we
142 _did_ find. */
144 struct variable *
145 lookup_variable (name, length)
146 char *name;
147 unsigned int length;
149 register struct variable_set_list *setlist;
150 struct variable *firstv = 0;
152 register unsigned int i;
153 register unsigned int rawhash = 0;
155 for (i = 0; i < length; ++i)
156 HASH (rawhash, name[i]);
158 for (setlist = current_variable_set_list;
159 setlist != 0; setlist = setlist->next)
161 register struct variable_set *set = setlist->set;
162 register unsigned int hashval = rawhash % set->buckets;
163 register struct variable *v;
165 /* Look through this set list. */
166 for (v = set->table[hashval]; v != 0; v = v->next)
167 if (*v->name == *name
168 && strneq (v->name + 1, name + 1, length - 1)
169 && v->name[length] == '\0')
170 break;
172 /* If we didn't find anything, go to the next set list. */
173 if (!v)
174 continue;
176 /* If it's not being expanded already, we're done. */
177 if (!v->expanding)
178 return v;
180 /* It is, so try to find another one. If this is the first one we've
181 seen, keep a pointer in case we don't find anything else. */
182 if (!firstv)
183 firstv = v;
186 #ifdef VMS
187 /* since we don't read envp[] on startup, try to get the
188 variable via getenv() here. */
189 if (!firstv)
191 char *vname = alloca (length + 1);
192 char *value;
193 strncpy (vname, name, length);
194 vname[length] = 0;
195 value = getenv (vname);
196 if (value != 0)
198 char *sptr;
199 int scnt;
201 sptr = value;
202 scnt = 0;
204 while ((sptr = strchr (sptr, '$')))
206 scnt++;
207 sptr++;
210 if (scnt > 0)
212 char *nvalue;
213 char *nptr;
215 nvalue = alloca (length + scnt + 1);
216 sptr = value;
217 nptr = nvalue;
219 while (*sptr)
221 if (*sptr == '$')
223 *nptr++ = '$';
224 *nptr++ = '$';
226 else
228 *nptr++ = *sptr;
230 sptr++;
233 return define_variable (vname, length, nvalue, o_env, 1);
237 return define_variable (vname, length, value, o_env, 1);
240 #endif /* VMS */
242 return firstv;
245 /* Lookup a variable whose name is a string starting at NAME
246 and with LENGTH chars in set SET. NAME need not be null-terminated.
247 Returns address of the `struct variable' containing all info
248 on the variable, or nil if no such variable is defined. */
250 static struct variable *
251 lookup_variable_in_set (name, length, set)
252 char *name;
253 unsigned int length;
254 struct variable_set *set;
256 register unsigned int i;
257 register unsigned int hash = 0;
258 register struct variable *v;
260 for (i = 0; i < length; ++i)
261 HASH (hash, name[i]);
262 hash %= set->buckets;
264 for (v = set->table[hash]; v != 0; v = v->next)
265 if (*v->name == *name
266 && strneq (v->name + 1, name + 1, length - 1)
267 && v->name[length] == 0)
268 return v;
270 return 0;
273 /* Initialize FILE's variable set list. If FILE already has a variable set
274 list, the topmost variable set is left intact, but the the rest of the
275 chain is replaced with FILE->parent's setlist. If we're READing a
276 makefile, don't do the pattern variable search now, since the pattern
277 variable might not have been defined yet. */
279 void
280 initialize_file_variables (file, reading)
281 struct file *file;
282 int reading;
284 register struct variable_set_list *l = file->variables;
286 if (l == 0)
288 l = (struct variable_set_list *)
289 xmalloc (sizeof (struct variable_set_list));
290 l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
291 l->set->buckets = PERFILE_VARIABLE_BUCKETS;
292 l->set->table = (struct variable **)
293 xmalloc (l->set->buckets * sizeof (struct variable *));
294 bzero ((char *) l->set->table,
295 l->set->buckets * sizeof (struct variable *));
296 file->variables = l;
299 if (file->parent == 0)
300 l->next = &global_setlist;
301 else
303 initialize_file_variables (file->parent, reading);
304 l->next = file->parent->variables;
307 /* If we're not reading makefiles and we haven't looked yet, see if
308 we can find a pattern variable. */
310 if (!reading && !file->pat_searched)
312 struct pattern_var *p = lookup_pattern_var (file->name);
314 file->pat_searched = 1;
315 if (p != 0)
317 /* If we found one, insert it between the current target's
318 variables and the next set, whatever it is. */
319 file->pat_variables = (struct variable_set_list *)
320 xmalloc (sizeof (struct variable_set_list));
321 file->pat_variables->set = p->vars->set;
325 /* If we have a pattern variable match, set it up. */
327 if (file->pat_variables != 0)
329 file->pat_variables->next = l->next;
330 l->next = file->pat_variables;
334 /* Pop the top set off the current variable set list,
335 and free all its storage. */
337 void
338 pop_variable_scope ()
340 register struct variable_set_list *setlist = current_variable_set_list;
341 register struct variable_set *set = setlist->set;
342 register unsigned int i;
344 current_variable_set_list = setlist->next;
345 free ((char *) setlist);
347 for (i = 0; i < set->buckets; ++i)
349 register struct variable *next = set->table[i];
350 while (next != 0)
352 register struct variable *v = next;
353 next = v->next;
355 free (v->name);
356 if (v->value)
357 free (v->value);
358 free ((char *) v);
361 free ((char *) set->table);
362 free ((char *) set);
365 struct variable_set_list *
366 create_new_variable_set ()
368 register struct variable_set_list *setlist;
369 register struct variable_set *set;
371 set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
372 set->buckets = SMALL_SCOPE_VARIABLE_BUCKETS;
373 set->table = (struct variable **)
374 xmalloc (set->buckets * sizeof (struct variable *));
375 bzero ((char *) set->table, set->buckets * sizeof (struct variable *));
377 setlist = (struct variable_set_list *)
378 xmalloc (sizeof (struct variable_set_list));
379 setlist->set = set;
380 setlist->next = current_variable_set_list;
382 return setlist;
385 /* Create a new variable set and push it on the current setlist. */
387 struct variable_set_list *
388 push_new_variable_scope ()
390 return (current_variable_set_list = create_new_variable_set());
393 /* Merge SET1 into SET0, freeing unused storage in SET1. */
395 static void
396 merge_variable_sets (set0, set1)
397 struct variable_set *set0, *set1;
399 register unsigned int bucket1;
401 for (bucket1 = 0; bucket1 < set1->buckets; ++bucket1)
403 register struct variable *v1 = set1->table[bucket1];
404 while (v1 != 0)
406 struct variable *next = v1->next;
407 unsigned int bucket0;
408 register struct variable *v0;
410 if (set1->buckets >= set0->buckets)
411 bucket0 = bucket1;
412 else
414 register char *n;
415 bucket0 = 0;
416 for (n = v1->name; *n != '\0'; ++n)
417 HASH (bucket0, *n);
419 bucket0 %= set0->buckets;
421 for (v0 = set0->table[bucket0]; v0 != 0; v0 = v0->next)
422 if (streq (v0->name, v1->name))
423 break;
425 if (v0 == 0)
427 /* There is no variable in SET0 with the same name. */
428 v1->next = set0->table[bucket0];
429 set0->table[bucket0] = v1;
431 else
433 /* The same variable exists in both sets.
434 SET0 takes precedence. */
435 free (v1->value);
436 free ((char *) v1);
439 v1 = next;
444 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
446 void
447 merge_variable_set_lists (setlist0, setlist1)
448 struct variable_set_list **setlist0, *setlist1;
450 register struct variable_set_list *list0 = *setlist0;
451 struct variable_set_list *last0 = 0;
453 while (setlist1 != 0 && list0 != 0)
455 struct variable_set_list *next = setlist1;
456 setlist1 = setlist1->next;
458 merge_variable_sets (list0->set, next->set);
460 last0 = list0;
461 list0 = list0->next;
464 if (setlist1 != 0)
466 if (last0 == 0)
467 *setlist0 = setlist1;
468 else
469 last0->next = setlist1;
473 /* Define the automatic variables, and record the addresses
474 of their structures so we can change their values quickly. */
476 void
477 define_automatic_variables ()
479 #ifdef WINDOWS32
480 extern char* default_shell;
481 #else
482 extern char default_shell[];
483 #endif
484 register struct variable *v;
485 char buf[200];
487 sprintf (buf, "%u", makelevel);
488 (void) define_variable ("MAKELEVEL", 9, buf, o_env, 0);
490 sprintf (buf, "%s%s%s",
491 version_string,
492 (remote_description == 0 || remote_description[0] == '\0')
493 ? "" : "-",
494 (remote_description == 0 || remote_description[0] == '\0')
495 ? "" : remote_description);
496 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
498 #ifdef __MSDOS__
499 /* Allow to specify a special shell just for Make,
500 and use $COMSPEC as the default $SHELL when appropriate. */
502 static char shell_str[] = "SHELL";
503 const int shlen = sizeof (shell_str) - 1;
504 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
505 struct variable *comp = lookup_variable ("COMSPEC", 7);
507 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
508 if (mshp)
509 (void) define_variable (shell_str, shlen,
510 mshp->value, o_env_override, 0);
511 else if (comp)
513 /* $COMSPEC shouldn't override $SHELL. */
514 struct variable *shp = lookup_variable (shell_str, shlen);
516 if (!shp)
517 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
520 #endif
522 /* This won't override any definition, but it
523 will provide one if there isn't one there. */
524 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
525 v->export = v_export; /* Always export SHELL. */
527 /* On MSDOS we do use SHELL from environment, since
528 it isn't a standard environment variable on MSDOS,
529 so whoever sets it, does that on purpose. */
530 #ifndef __MSDOS__
531 /* Don't let SHELL come from the environment. */
532 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
534 free (v->value);
535 v->origin = o_file;
536 v->value = xstrdup (default_shell);
538 #endif
540 /* Make sure MAKEFILES gets exported if it is set. */
541 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
542 v->export = v_ifset;
544 /* Define the magic D and F variables in terms of
545 the automatic variables they are variations of. */
547 #ifdef VMS
548 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
549 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
550 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
551 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
552 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
553 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
554 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
555 #else
556 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
557 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
558 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
559 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
560 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
561 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
562 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
563 #endif
564 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
565 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
566 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
567 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
568 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
569 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
570 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
573 int export_all_variables;
575 /* Create a new environment for FILE's commands.
576 If FILE is nil, this is for the `shell' function.
577 The child's MAKELEVEL variable is incremented. */
579 char **
580 target_environment (file)
581 struct file *file;
583 struct variable_set_list *set_list;
584 register struct variable_set_list *s;
585 struct variable_bucket
587 struct variable_bucket *next;
588 struct variable *variable;
590 struct variable_bucket **table;
591 unsigned int buckets;
592 register unsigned int i;
593 register unsigned nvariables;
594 char **result;
595 unsigned int mklev_hash;
597 if (file == 0)
598 set_list = current_variable_set_list;
599 else
600 set_list = file->variables;
602 /* Find the lowest number of buckets in any set in the list. */
603 s = set_list;
604 buckets = s->set->buckets;
605 for (s = s->next; s != 0; s = s->next)
606 if (s->set->buckets < buckets)
607 buckets = s->set->buckets;
609 /* Find the hash value of the bucket `MAKELEVEL' will fall into. */
611 char *p = "MAKELEVEL";
612 mklev_hash = 0;
613 while (*p != '\0')
614 HASH (mklev_hash, *p++);
617 /* Temporarily allocate a table with that many buckets. */
618 table = (struct variable_bucket **)
619 alloca (buckets * sizeof (struct variable_bucket *));
620 bzero ((char *) table, buckets * sizeof (struct variable_bucket *));
622 /* Run through all the variable sets in the list,
623 accumulating variables in TABLE. */
624 nvariables = 0;
625 for (s = set_list; s != 0; s = s->next)
627 register struct variable_set *set = s->set;
628 for (i = 0; i < set->buckets; ++i)
630 register struct variable *v;
631 for (v = set->table[i]; v != 0; v = v->next)
633 unsigned int j = i % buckets;
634 register struct variable_bucket *ov;
635 register char *p = v->name;
637 if (i == mklev_hash % set->buckets
638 && streq (v->name, "MAKELEVEL"))
639 /* Don't include MAKELEVEL because it will be
640 added specially at the end. */
641 continue;
643 /* If this is a per-target variable and it hasn't been touched
644 already then look up the global version and take its export
645 value. */
646 if (v->per_target && v->export == v_default)
648 struct variable *gv;
650 gv = lookup_variable_in_set(v->name, strlen(v->name),
651 &global_variable_set);
652 if (gv)
653 v->export = gv->export;
656 switch (v->export)
658 case v_default:
659 if (v->origin == o_default || v->origin == o_automatic)
660 /* Only export default variables by explicit request. */
661 continue;
663 if (! export_all_variables
664 && v->origin != o_command
665 && v->origin != o_env && v->origin != o_env_override)
666 continue;
668 if (*p != '_' && (*p < 'A' || *p > 'Z')
669 && (*p < 'a' || *p > 'z'))
670 continue;
671 for (++p; *p != '\0'; ++p)
672 if (*p != '_' && (*p < 'a' || *p > 'z')
673 && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9'))
674 continue;
675 if (*p != '\0')
676 continue;
677 break;
679 case v_export:
680 break;
682 case v_noexport:
683 continue;
685 case v_ifset:
686 if (v->origin == o_default)
687 continue;
688 break;
691 /* If this was from a different-sized hash table, then
692 recalculate the bucket it goes in. */
693 if (set->buckets != buckets)
695 register char *np;
697 j = 0;
698 for (np = v->name; *np != '\0'; ++np)
699 HASH (j, *np);
700 j %= buckets;
703 for (ov = table[j]; ov != 0; ov = ov->next)
704 if (streq (v->name, ov->variable->name))
705 break;
707 if (ov == 0)
709 register struct variable_bucket *entry;
710 entry = (struct variable_bucket *)
711 alloca (sizeof (struct variable_bucket));
712 entry->next = table[j];
713 entry->variable = v;
714 table[j] = entry;
715 ++nvariables;
721 result = (char **) xmalloc ((nvariables + 2) * sizeof (char *));
722 nvariables = 0;
723 for (i = 0; i < buckets; ++i)
725 register struct variable_bucket *b;
726 for (b = table[i]; b != 0; b = b->next)
728 register struct variable *v = b->variable;
730 /* If V is recursively expanded and didn't come from the environment,
731 expand its value. If it came from the environment, it should
732 go back into the environment unchanged. */
733 if (v->recursive
734 && v->origin != o_env && v->origin != o_env_override)
736 char *value = recursively_expand (v);
737 #ifdef WINDOWS32
738 if (strcmp(v->name, "Path") == 0 ||
739 strcmp(v->name, "PATH") == 0)
740 convert_Path_to_windows32(value, ';');
741 #endif
742 result[nvariables++] = concat (v->name, "=", value);
743 free (value);
745 else
746 #ifdef WINDOWS32
748 if (strcmp(v->name, "Path") == 0 ||
749 strcmp(v->name, "PATH") == 0)
750 convert_Path_to_windows32(v->value, ';');
751 result[nvariables++] = concat (v->name, "=", v->value);
753 #else
754 result[nvariables++] = concat (v->name, "=", v->value);
755 #endif
758 result[nvariables] = (char *) xmalloc (100);
759 (void) sprintf (result[nvariables], "MAKELEVEL=%u", makelevel + 1);
760 result[++nvariables] = 0;
762 return result;
765 /* Try to interpret LINE (a null-terminated string) as a variable definition.
767 ORIGIN may be o_file, o_override, o_env, o_env_override,
768 or o_command specifying that the variable definition comes
769 from a makefile, an override directive, the environment with
770 or without the -e switch, or the command line.
772 See the comments for parse_variable_definition().
774 If LINE was recognized as a variable definition, a pointer to its `struct
775 variable' is returned. If LINE is not a variable definition, NULL is
776 returned. */
778 struct variable *
779 try_variable_definition (flocp, line, origin, target_var)
780 const struct floc *flocp;
781 char *line;
782 enum variable_origin origin;
783 int target_var;
785 register int c;
786 register char *p = line;
787 register char *beg;
788 register char *end;
789 enum { f_bogus,
790 f_simple, f_recursive, f_append, f_conditional } flavor = f_bogus;
791 char *name, *expanded_name, *value, *alloc_value=NULL;
792 struct variable *v;
793 int append = 0;
795 while (1)
797 c = *p++;
798 if (c == '\0' || c == '#')
799 return 0;
800 if (c == '=')
802 end = p - 1;
803 flavor = f_recursive;
804 break;
806 else if (c == ':')
807 if (*p == '=')
809 end = p++ - 1;
810 flavor = f_simple;
811 break;
813 else
814 /* A colon other than := is a rule line, not a variable defn. */
815 return 0;
816 else if (c == '+' && *p == '=')
818 end = p++ - 1;
819 flavor = f_append;
820 break;
822 else if (c == '?' && *p == '=')
824 end = p++ - 1;
825 flavor = f_conditional;
826 break;
828 else if (c == '$')
830 /* This might begin a variable expansion reference. Make sure we
831 don't misrecognize chars inside the reference as =, := or +=. */
832 char closeparen;
833 int count;
834 c = *p++;
835 if (c == '(')
836 closeparen = ')';
837 else if (c == '{')
838 closeparen = '}';
839 else
840 continue; /* Nope. */
842 /* P now points past the opening paren or brace.
843 Count parens or braces until it is matched. */
844 count = 0;
845 for (; *p != '\0'; ++p)
847 if (*p == c)
848 ++count;
849 else if (*p == closeparen && --count < 0)
851 ++p;
852 break;
858 beg = next_token (line);
859 while (end > beg && isblank ((unsigned char)end[-1]))
860 --end;
861 p = next_token (p);
863 /* Expand the name, so "$(foo)bar = baz" works. */
864 name = (char *) alloca (end - beg + 1);
865 bcopy (beg, name, end - beg);
866 name[end - beg] = '\0';
867 expanded_name = allocated_variable_expand (name);
869 if (expanded_name[0] == '\0')
870 fatal (flocp, _("empty variable name"));
872 /* Calculate the variable's new value in VALUE. */
874 switch (flavor)
876 case f_bogus:
877 /* Should not be possible. */
878 abort ();
879 case f_simple:
880 /* A simple variable definition "var := value". Expand the value.
881 We have to allocate memory since otherwise it'll clobber the
882 variable buffer, and we may still need that if we're looking at a
883 target-specific variable. */
884 value = alloc_value = allocated_variable_expand (p);
885 break;
886 case f_conditional:
887 /* A conditional variable definition "var ?= value".
888 The value is set IFF the variable is not defined yet. */
889 v = lookup_variable(expanded_name, strlen(expanded_name));
890 if (v)
892 free(expanded_name);
893 return v;
895 flavor = f_recursive;
896 /* FALLTHROUGH */
897 case f_recursive:
898 /* A recursive variable definition "var = value".
899 The value is used verbatim. */
900 value = p;
901 break;
902 case f_append:
903 /* If we have += but we're in a target variable context, defer the
904 append until the context expansion. */
905 if (target_var)
907 append = 1;
908 flavor = f_recursive;
909 value = p;
910 break;
913 /* An appending variable definition "var += value".
914 Extract the old value and append the new one. */
915 v = lookup_variable (expanded_name, strlen (expanded_name));
916 if (v == 0)
918 /* There was no old value.
919 This becomes a normal recursive definition. */
920 value = p;
921 flavor = f_recursive;
923 else
925 /* Paste the old and new values together in VALUE. */
927 unsigned int oldlen, newlen;
929 if (v->recursive)
930 /* The previous definition of the variable was recursive.
931 The new value comes from the unexpanded old and new values. */
932 flavor = f_recursive;
933 else
934 /* The previous definition of the variable was simple.
935 The new value comes from the old value, which was expanded
936 when it was set; and from the expanded new value. Allocate
937 memory for the expansion as we may still need the rest of the
938 buffer if we're looking at a target-specific variable. */
939 p = alloc_value = allocated_variable_expand (p);
941 oldlen = strlen (v->value);
942 newlen = strlen (p);
943 value = (char *) alloca (oldlen + 1 + newlen + 1);
944 bcopy (v->value, value, oldlen);
945 value[oldlen] = ' ';
946 bcopy (p, &value[oldlen + 1], newlen + 1);
950 #ifdef __MSDOS__
951 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
952 non-Unix systems don't conform to this default configuration (in
953 fact, most of them don't even have `/bin'). On the other hand,
954 $SHELL in the environment, if set, points to the real pathname of
955 the shell.
956 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
957 the Makefile override $SHELL from the environment. But first, we
958 look for the basename of the shell in the directory where SHELL=
959 points, and along the $PATH; if it is found in any of these places,
960 we define $SHELL to be the actual pathname of the shell. Thus, if
961 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
962 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
963 defining SHELL to be "d:/unix/bash.exe". */
964 if ((origin == o_file || origin == o_override)
965 && strcmp (expanded_name, "SHELL") == 0)
967 char shellpath[PATH_MAX];
968 extern char * __dosexec_find_on_path (const char *, char *[], char *);
970 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
971 if (__dosexec_find_on_path (value, (char **)0, shellpath))
973 char *p;
975 for (p = shellpath; *p; p++)
977 if (*p == '\\')
978 *p = '/';
980 v = define_variable_loc (expanded_name, strlen (expanded_name),
981 shellpath, origin, flavor == f_recursive,
982 flocp);
984 else
986 char *shellbase, *bslash;
987 struct variable *pathv = lookup_variable ("PATH", 4);
988 char *path_string;
989 char *fake_env[2];
990 size_t pathlen = 0;
992 shellbase = strrchr (value, '/');
993 bslash = strrchr (value, '\\');
994 if (!shellbase || bslash > shellbase)
995 shellbase = bslash;
996 if (!shellbase && value[1] == ':')
997 shellbase = value + 1;
998 if (shellbase)
999 shellbase++;
1000 else
1001 shellbase = value;
1003 /* Search for the basename of the shell (with standard
1004 executable extensions) along the $PATH. */
1005 if (pathv)
1006 pathlen = strlen (pathv->value);
1007 path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
1008 /* On MSDOS, current directory is considered as part of $PATH. */
1009 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1010 fake_env[0] = path_string;
1011 fake_env[1] = (char *)0;
1012 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1014 char *p;
1016 for (p = shellpath; *p; p++)
1018 if (*p == '\\')
1019 *p = '/';
1021 v = define_variable_loc (expanded_name, strlen (expanded_name),
1022 shellpath, origin,
1023 flavor == f_recursive, flocp);
1025 else
1026 v = lookup_variable (expanded_name, strlen (expanded_name));
1028 free (path_string);
1031 else
1032 #endif /* __MSDOS__ */
1033 #ifdef WINDOWS32
1034 if ((origin == o_file || origin == o_override)
1035 && strcmp (expanded_name, "SHELL") == 0) {
1036 extern char* default_shell;
1039 * Call shell locator function. If it returns TRUE, then
1040 * set no_default_sh_exe to indicate sh was found and
1041 * set new value for SHELL variable.
1043 if (find_and_set_default_shell(value)) {
1044 v = define_variable_loc (expanded_name, strlen (expanded_name),
1045 default_shell, origin, flavor == f_recursive,
1046 flocp);
1047 no_default_sh_exe = 0;
1049 } else
1050 #endif
1052 v = define_variable_loc (expanded_name, strlen (expanded_name), value,
1053 origin, flavor == f_recursive, flocp);
1055 v->append = append;
1057 if (alloc_value)
1058 free (alloc_value);
1059 free (expanded_name);
1061 return v;
1064 /* Print information for variable V, prefixing it with PREFIX. */
1066 static void
1067 print_variable (v, prefix)
1068 register struct variable *v;
1069 char *prefix;
1071 const char *origin;
1073 switch (v->origin)
1075 case o_default:
1076 origin = _("default");
1077 break;
1078 case o_env:
1079 origin = _("environment");
1080 break;
1081 case o_file:
1082 origin = _("makefile");
1083 break;
1084 case o_env_override:
1085 origin = _("environment under -e");
1086 break;
1087 case o_command:
1088 origin = _("command line");
1089 break;
1090 case o_override:
1091 origin = _("`override' directive");
1092 break;
1093 case o_automatic:
1094 origin = _("automatic");
1095 break;
1096 case o_invalid:
1097 default:
1098 abort ();
1100 fputs ("# ", stdout);
1101 fputs (origin, stdout);
1102 if (v->fileinfo.filenm)
1103 printf (" (from `%s', line %lu)", v->fileinfo.filenm, v->fileinfo.lineno);
1104 putchar ('\n');
1105 fputs (prefix, stdout);
1107 /* Is this a `define'? */
1108 if (v->recursive && strchr (v->value, '\n') != 0)
1109 printf ("define %s\n%s\nendef\n", v->name, v->value);
1110 else
1112 register char *p;
1114 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1116 /* Check if the value is just whitespace. */
1117 p = next_token (v->value);
1118 if (p != v->value && *p == '\0')
1119 /* All whitespace. */
1120 printf ("$(subst ,,%s)", v->value);
1121 else if (v->recursive)
1122 fputs (v->value, stdout);
1123 else
1124 /* Double up dollar signs. */
1125 for (p = v->value; *p != '\0'; ++p)
1127 if (*p == '$')
1128 putchar ('$');
1129 putchar (*p);
1131 putchar ('\n');
1136 /* Print all the variables in SET. PREFIX is printed before
1137 the actual variable definitions (everything else is comments). */
1139 void
1140 print_variable_set (set, prefix)
1141 register struct variable_set *set;
1142 char *prefix;
1144 register unsigned int i, nvariables, per_bucket;
1145 register struct variable *v;
1147 per_bucket = nvariables = 0;
1148 for (i = 0; i < set->buckets; ++i)
1150 register unsigned int this_bucket = 0;
1152 for (v = set->table[i]; v != 0; v = v->next)
1154 ++this_bucket;
1155 print_variable (v, prefix);
1158 nvariables += this_bucket;
1159 if (this_bucket > per_bucket)
1160 per_bucket = this_bucket;
1163 if (nvariables == 0)
1164 puts (_("# No variables."));
1165 else
1167 printf (_("# %u variables in %u hash buckets.\n"),
1168 nvariables, set->buckets);
1169 #ifndef NO_FLOAT
1170 printf (_("# average of %.1f variables per bucket, \
1171 max %u in one bucket.\n"),
1172 (double) nvariables / (double) set->buckets,
1173 per_bucket);
1174 #else
1176 int f = (nvariables * 1000 + 5) / set->buckets;
1177 printf (_("# average of %d.%d variables per bucket, \
1178 max %u in one bucket.\n"),
1179 f/10, f%10,
1180 per_bucket);
1182 #endif
1187 /* Print the data base of variables. */
1189 void
1190 print_variable_data_base ()
1192 puts (_("\n# Variables\n"));
1194 print_variable_set (&global_variable_set, "");
1198 /* Print all the local variables of FILE. */
1200 void
1201 print_file_variables (file)
1202 struct file *file;
1204 if (file->variables != 0)
1205 print_variable_set (file->variables->set, "# ");
1208 #ifdef WINDOWS32
1209 void
1210 sync_Path_environment(void)
1212 char* path = allocated_variable_expand("$(Path)");
1213 static char* environ_path = NULL;
1215 if (!path)
1216 return;
1219 * If done this before, don't leak memory unnecessarily.
1220 * Free the previous entry before allocating new one.
1222 if (environ_path)
1223 free(environ_path);
1226 * Create something WINDOWS32 world can grok
1228 convert_Path_to_windows32(path, ';');
1229 environ_path = concat("Path", "=", path);
1230 putenv(environ_path);
1231 free(path);
1233 #endif