Install newest German translation.
[make.git] / variable.c
blob148e081a3356bfa3694d8e691b937ffe86f4b658
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 /* Implement variables. */
51 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
52 LENGTH is the length of NAME, which does not need to be null-terminated.
53 ORIGIN specifies the origin of the variable (makefile, command line
54 or environment).
55 If RECURSIVE is nonzero a flag is set in the variable saying
56 that it should be recursively re-expanded. */
58 struct variable *
59 define_variable_in_set (name, length, value, origin, recursive, set, flocp)
60 char *name;
61 unsigned int length;
62 char *value;
63 enum variable_origin origin;
64 int recursive;
65 struct variable_set *set;
66 const struct floc *flocp;
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 && strneq (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 = xstrdup (value);
101 if (flocp != 0)
102 v->fileinfo = *flocp;
103 else
104 v->fileinfo.filenm = 0;
105 v->origin = origin;
106 v->recursive = recursive;
108 return v;
111 /* Create a new variable definition and add it to the hash table. */
113 v = (struct variable *) xmalloc (sizeof (struct variable));
114 v->name = savestring (name, length);
115 v->value = xstrdup (value);
116 if (flocp != 0)
117 v->fileinfo = *flocp;
118 else
119 v->fileinfo.filenm = 0;
120 v->origin = origin;
121 v->recursive = recursive;
122 v->expanding = 0;
123 v->exp_count = 0;
124 v->per_target = 0;
125 v->append = 0;
126 v->export = v_default;
127 v->next = set->table[hashval];
128 set->table[hashval] = v;
129 return v;
132 /* Lookup a variable whose name is a string starting at NAME
133 and with LENGTH chars. NAME need not be null-terminated.
134 Returns address of the `struct variable' containing all info
135 on the variable, or nil if no such variable is defined. */
137 struct variable *
138 lookup_variable (name, length)
139 const char *name;
140 unsigned int length;
142 const struct variable_set_list *setlist;
144 unsigned int i;
145 unsigned int rawhash = 0;
147 for (i = 0; i < length; ++i)
148 HASH (rawhash, name[i]);
150 for (setlist = current_variable_set_list;
151 setlist != 0; setlist = setlist->next)
153 const struct variable_set *set = setlist->set;
154 unsigned int hashval = rawhash % set->buckets;
155 struct variable *v;
157 /* Look through this set list; return it if found. */
158 for (v = set->table[hashval]; v != 0; v = v->next)
159 if (*v->name == *name
160 && strneq (v->name + 1, name + 1, length - 1)
161 && v->name[length] == '\0')
162 return v;
165 #ifdef VMS
166 /* since we don't read envp[] on startup, try to get the
167 variable via getenv() here. */
169 char *vname = alloca (length + 1);
170 char *value;
171 strncpy (vname, name, length);
172 vname[length] = 0;
173 value = getenv (vname);
174 if (value != 0)
176 char *sptr;
177 int scnt;
179 sptr = value;
180 scnt = 0;
182 while ((sptr = strchr (sptr, '$')))
184 scnt++;
185 sptr++;
188 if (scnt > 0)
190 char *nvalue;
191 char *nptr;
193 nvalue = alloca (strlen (value) + scnt + 1);
194 sptr = value;
195 nptr = nvalue;
197 while (*sptr)
199 if (*sptr == '$')
201 *nptr++ = '$';
202 *nptr++ = '$';
204 else
206 *nptr++ = *sptr;
208 sptr++;
211 *nptr = '\0';
212 return define_variable (vname, length, nvalue, o_env, 1);
216 return define_variable (vname, length, value, o_env, 1);
219 #endif /* VMS */
221 return 0;
224 /* Lookup a variable whose name is a string starting at NAME
225 and with LENGTH chars in set SET. NAME need not be null-terminated.
226 Returns address of the `struct variable' containing all info
227 on the variable, or nil if no such variable is defined. */
229 struct variable *
230 lookup_variable_in_set (name, length, set)
231 const char *name;
232 unsigned int length;
233 const struct variable_set *set;
235 unsigned int i;
236 unsigned int hash = 0;
237 struct variable *v;
239 for (i = 0; i < length; ++i)
240 HASH (hash, name[i]);
241 hash %= set->buckets;
243 for (v = set->table[hash]; v != 0; v = v->next)
244 if (*v->name == *name
245 && strneq (v->name + 1, name + 1, length - 1)
246 && v->name[length] == 0)
247 return v;
249 return 0;
252 /* Initialize FILE's variable set list. If FILE already has a variable set
253 list, the topmost variable set is left intact, but the the rest of the
254 chain is replaced with FILE->parent's setlist. If we're READing a
255 makefile, don't do the pattern variable search now, since the pattern
256 variable might not have been defined yet. */
258 void
259 initialize_file_variables (file, reading)
260 struct file *file;
261 int reading;
263 register struct variable_set_list *l = file->variables;
265 if (l == 0)
267 l = (struct variable_set_list *)
268 xmalloc (sizeof (struct variable_set_list));
269 l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
270 l->set->buckets = PERFILE_VARIABLE_BUCKETS;
271 l->set->table = (struct variable **)
272 xmalloc (l->set->buckets * sizeof (struct variable *));
273 bzero ((char *) l->set->table,
274 l->set->buckets * sizeof (struct variable *));
275 file->variables = l;
278 if (file->parent == 0)
279 l->next = &global_setlist;
280 else
282 initialize_file_variables (file->parent, reading);
283 l->next = file->parent->variables;
286 /* If we're not reading makefiles and we haven't looked yet, see if
287 we can find a pattern variable. */
289 if (!reading && !file->pat_searched)
291 struct pattern_var *p = lookup_pattern_var (file->name);
293 file->pat_searched = 1;
294 if (p != 0)
296 /* If we found one, insert it between the current target's
297 variables and the next set, whatever it is. */
298 file->pat_variables = (struct variable_set_list *)
299 xmalloc (sizeof (struct variable_set_list));
300 file->pat_variables->set = p->vars->set;
304 /* If we have a pattern variable match, set it up. */
306 if (file->pat_variables != 0)
308 file->pat_variables->next = l->next;
309 l->next = file->pat_variables;
313 /* Pop the top set off the current variable set list,
314 and free all its storage. */
316 void
317 pop_variable_scope ()
319 register struct variable_set_list *setlist = current_variable_set_list;
320 register struct variable_set *set = setlist->set;
321 register unsigned int i;
323 current_variable_set_list = setlist->next;
324 free ((char *) setlist);
326 for (i = 0; i < set->buckets; ++i)
328 register struct variable *next = set->table[i];
329 while (next != 0)
331 register struct variable *v = next;
332 next = v->next;
334 free (v->name);
335 if (v->value)
336 free (v->value);
337 free ((char *) v);
340 free ((char *) set->table);
341 free ((char *) set);
344 struct variable_set_list *
345 create_new_variable_set ()
347 register struct variable_set_list *setlist;
348 register struct variable_set *set;
350 set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
351 set->buckets = SMALL_SCOPE_VARIABLE_BUCKETS;
352 set->table = (struct variable **)
353 xmalloc (set->buckets * sizeof (struct variable *));
354 bzero ((char *) set->table, set->buckets * sizeof (struct variable *));
356 setlist = (struct variable_set_list *)
357 xmalloc (sizeof (struct variable_set_list));
358 setlist->set = set;
359 setlist->next = current_variable_set_list;
361 return setlist;
364 /* Create a new variable set and push it on the current setlist. */
366 struct variable_set_list *
367 push_new_variable_scope ()
369 return (current_variable_set_list = create_new_variable_set());
372 /* Merge SET1 into SET0, freeing unused storage in SET1. */
374 static void
375 merge_variable_sets (set0, set1)
376 struct variable_set *set0, *set1;
378 register unsigned int bucket1;
380 for (bucket1 = 0; bucket1 < set1->buckets; ++bucket1)
382 register struct variable *v1 = set1->table[bucket1];
383 while (v1 != 0)
385 struct variable *next = v1->next;
386 unsigned int bucket0;
387 register struct variable *v0;
389 if (set1->buckets >= set0->buckets)
390 bucket0 = bucket1;
391 else
393 register char *n;
394 bucket0 = 0;
395 for (n = v1->name; *n != '\0'; ++n)
396 HASH (bucket0, *n);
398 bucket0 %= set0->buckets;
400 for (v0 = set0->table[bucket0]; v0 != 0; v0 = v0->next)
401 if (streq (v0->name, v1->name))
402 break;
404 if (v0 == 0)
406 /* There is no variable in SET0 with the same name. */
407 v1->next = set0->table[bucket0];
408 set0->table[bucket0] = v1;
410 else
412 /* The same variable exists in both sets.
413 SET0 takes precedence. */
414 free (v1->value);
415 free ((char *) v1);
418 v1 = next;
423 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
425 void
426 merge_variable_set_lists (setlist0, setlist1)
427 struct variable_set_list **setlist0, *setlist1;
429 register struct variable_set_list *list0 = *setlist0;
430 struct variable_set_list *last0 = 0;
432 while (setlist1 != 0 && list0 != 0)
434 struct variable_set_list *next = setlist1;
435 setlist1 = setlist1->next;
437 merge_variable_sets (list0->set, next->set);
439 last0 = list0;
440 list0 = list0->next;
443 if (setlist1 != 0)
445 if (last0 == 0)
446 *setlist0 = setlist1;
447 else
448 last0->next = setlist1;
452 /* Define the automatic variables, and record the addresses
453 of their structures so we can change their values quickly. */
455 void
456 define_automatic_variables ()
458 #ifdef WINDOWS32
459 extern char* default_shell;
460 #else
461 extern char default_shell[];
462 #endif
463 register struct variable *v;
464 char buf[200];
466 sprintf (buf, "%u", makelevel);
467 (void) define_variable ("MAKELEVEL", 9, buf, o_env, 0);
469 sprintf (buf, "%s%s%s",
470 version_string,
471 (remote_description == 0 || remote_description[0] == '\0')
472 ? "" : "-",
473 (remote_description == 0 || remote_description[0] == '\0')
474 ? "" : remote_description);
475 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
477 #ifdef __MSDOS__
478 /* Allow to specify a special shell just for Make,
479 and use $COMSPEC as the default $SHELL when appropriate. */
481 static char shell_str[] = "SHELL";
482 const int shlen = sizeof (shell_str) - 1;
483 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
484 struct variable *comp = lookup_variable ("COMSPEC", 7);
486 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
487 if (mshp)
488 (void) define_variable (shell_str, shlen,
489 mshp->value, o_env_override, 0);
490 else if (comp)
492 /* $COMSPEC shouldn't override $SHELL. */
493 struct variable *shp = lookup_variable (shell_str, shlen);
495 if (!shp)
496 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
499 #endif
501 /* This won't override any definition, but it
502 will provide one if there isn't one there. */
503 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
504 v->export = v_export; /* Always export SHELL. */
506 /* On MSDOS we do use SHELL from environment, since
507 it isn't a standard environment variable on MSDOS,
508 so whoever sets it, does that on purpose. */
509 #ifndef __MSDOS__
510 /* Don't let SHELL come from the environment. */
511 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
513 free (v->value);
514 v->origin = o_file;
515 v->value = xstrdup (default_shell);
517 #endif
519 /* Make sure MAKEFILES gets exported if it is set. */
520 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
521 v->export = v_ifset;
523 /* Define the magic D and F variables in terms of
524 the automatic variables they are variations of. */
526 #ifdef VMS
527 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
528 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
529 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
530 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
531 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
532 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
533 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
534 #else
535 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
536 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
537 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
538 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
539 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
540 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
541 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
542 #endif
543 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
544 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
545 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
546 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
547 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
548 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
549 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
552 int export_all_variables;
554 /* Create a new environment for FILE's commands.
555 If FILE is nil, this is for the `shell' function.
556 The child's MAKELEVEL variable is incremented. */
558 char **
559 target_environment (file)
560 struct file *file;
562 struct variable_set_list *set_list;
563 register struct variable_set_list *s;
564 struct variable_bucket
566 struct variable_bucket *next;
567 struct variable *variable;
569 struct variable_bucket **table;
570 unsigned int buckets;
571 register unsigned int i;
572 register unsigned nvariables;
573 char **result;
574 unsigned int mklev_hash;
576 if (file == 0)
577 set_list = current_variable_set_list;
578 else
579 set_list = file->variables;
581 /* Find the lowest number of buckets in any set in the list. */
582 s = set_list;
583 buckets = s->set->buckets;
584 for (s = s->next; s != 0; s = s->next)
585 if (s->set->buckets < buckets)
586 buckets = s->set->buckets;
588 /* Find the hash value of the bucket `MAKELEVEL' will fall into. */
590 char *p = "MAKELEVEL";
591 mklev_hash = 0;
592 while (*p != '\0')
593 HASH (mklev_hash, *p++);
596 /* Temporarily allocate a table with that many buckets. */
597 table = (struct variable_bucket **)
598 alloca (buckets * sizeof (struct variable_bucket *));
599 bzero ((char *) table, buckets * sizeof (struct variable_bucket *));
601 /* Run through all the variable sets in the list,
602 accumulating variables in TABLE. */
603 nvariables = 0;
604 for (s = set_list; s != 0; s = s->next)
606 register struct variable_set *set = s->set;
607 for (i = 0; i < set->buckets; ++i)
609 register struct variable *v;
610 for (v = set->table[i]; v != 0; v = v->next)
612 unsigned int j = i % buckets;
613 register struct variable_bucket *ov;
614 register char *p = v->name;
616 if (i == mklev_hash % set->buckets
617 && streq (v->name, "MAKELEVEL"))
618 /* Don't include MAKELEVEL because it will be
619 added specially at the end. */
620 continue;
622 /* If this is a per-target variable and it hasn't been touched
623 already then look up the global version and take its export
624 value. */
625 if (v->per_target && v->export == v_default)
627 struct variable *gv;
629 gv = lookup_variable_in_set(v->name, strlen(v->name),
630 &global_variable_set);
631 if (gv)
632 v->export = gv->export;
635 switch (v->export)
637 case v_default:
638 if (v->origin == o_default || v->origin == o_automatic)
639 /* Only export default variables by explicit request. */
640 continue;
642 if (! export_all_variables
643 && v->origin != o_command
644 && v->origin != o_env && v->origin != o_env_override)
645 continue;
647 if (*p != '_' && (*p < 'A' || *p > 'Z')
648 && (*p < 'a' || *p > 'z'))
649 continue;
650 for (++p; *p != '\0'; ++p)
651 if (*p != '_' && (*p < 'a' || *p > 'z')
652 && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9'))
653 continue;
654 if (*p != '\0')
655 continue;
656 break;
658 case v_export:
659 break;
661 case v_noexport:
662 continue;
664 case v_ifset:
665 if (v->origin == o_default)
666 continue;
667 break;
670 /* If this was from a different-sized hash table, then
671 recalculate the bucket it goes in. */
672 if (set->buckets != buckets)
674 register char *np;
676 j = 0;
677 for (np = v->name; *np != '\0'; ++np)
678 HASH (j, *np);
679 j %= buckets;
682 for (ov = table[j]; ov != 0; ov = ov->next)
683 if (streq (v->name, ov->variable->name))
684 break;
686 if (ov == 0)
688 register struct variable_bucket *entry;
689 entry = (struct variable_bucket *)
690 alloca (sizeof (struct variable_bucket));
691 entry->next = table[j];
692 entry->variable = v;
693 table[j] = entry;
694 ++nvariables;
700 result = (char **) xmalloc ((nvariables + 2) * sizeof (char *));
701 nvariables = 0;
702 for (i = 0; i < buckets; ++i)
704 register struct variable_bucket *b;
705 for (b = table[i]; b != 0; b = b->next)
707 register struct variable *v = b->variable;
709 /* If V is recursively expanded and didn't come from the environment,
710 expand its value. If it came from the environment, it should
711 go back into the environment unchanged. */
712 if (v->recursive
713 && v->origin != o_env && v->origin != o_env_override)
715 char *value = recursively_expand_for_file (v, file);
716 #ifdef WINDOWS32
717 if (strcmp(v->name, "Path") == 0 ||
718 strcmp(v->name, "PATH") == 0)
719 convert_Path_to_windows32(value, ';');
720 #endif
721 result[nvariables++] = concat (v->name, "=", value);
722 free (value);
724 else
725 #ifdef WINDOWS32
727 if (strcmp(v->name, "Path") == 0 ||
728 strcmp(v->name, "PATH") == 0)
729 convert_Path_to_windows32(v->value, ';');
730 result[nvariables++] = concat (v->name, "=", v->value);
732 #else
733 result[nvariables++] = concat (v->name, "=", v->value);
734 #endif
737 result[nvariables] = (char *) xmalloc (100);
738 (void) sprintf (result[nvariables], "MAKELEVEL=%u", makelevel + 1);
739 result[++nvariables] = 0;
741 return result;
744 /* Try to interpret LINE (a null-terminated string) as a variable definition.
746 ORIGIN may be o_file, o_override, o_env, o_env_override,
747 or o_command specifying that the variable definition comes
748 from a makefile, an override directive, the environment with
749 or without the -e switch, or the command line.
751 See the comments for parse_variable_definition().
753 If LINE was recognized as a variable definition, a pointer to its `struct
754 variable' is returned. If LINE is not a variable definition, NULL is
755 returned. */
757 struct variable *
758 try_variable_definition (flocp, line, origin, target_var)
759 const struct floc *flocp;
760 char *line;
761 enum variable_origin origin;
762 int target_var;
764 register int c;
765 register char *p = line;
766 register char *beg;
767 register char *end;
768 enum { f_bogus,
769 f_simple, f_recursive, f_append, f_conditional } flavor = f_bogus;
770 char *name, *expanded_name, *value=0, *alloc_value=NULL;
771 struct variable *v;
772 int append = 0;
774 while (1)
776 c = *p++;
777 if (c == '\0' || c == '#')
778 return 0;
779 if (c == '=')
781 end = p - 1;
782 flavor = f_recursive;
783 break;
785 else if (c == ':')
786 if (*p == '=')
788 end = p++ - 1;
789 flavor = f_simple;
790 break;
792 else
793 /* A colon other than := is a rule line, not a variable defn. */
794 return 0;
795 else if (c == '+' && *p == '=')
797 end = p++ - 1;
798 flavor = f_append;
799 break;
801 else if (c == '?' && *p == '=')
803 end = p++ - 1;
804 flavor = f_conditional;
805 break;
807 else if (c == '$')
809 /* This might begin a variable expansion reference. Make sure we
810 don't misrecognize chars inside the reference as =, := or +=. */
811 char closeparen;
812 int count;
813 c = *p++;
814 if (c == '(')
815 closeparen = ')';
816 else if (c == '{')
817 closeparen = '}';
818 else
819 continue; /* Nope. */
821 /* P now points past the opening paren or brace.
822 Count parens or braces until it is matched. */
823 count = 0;
824 for (; *p != '\0'; ++p)
826 if (*p == c)
827 ++count;
828 else if (*p == closeparen && --count < 0)
830 ++p;
831 break;
837 beg = next_token (line);
838 while (end > beg && isblank ((unsigned char)end[-1]))
839 --end;
840 p = next_token (p);
842 /* Expand the name, so "$(foo)bar = baz" works. */
843 name = (char *) alloca (end - beg + 1);
844 bcopy (beg, name, end - beg);
845 name[end - beg] = '\0';
846 expanded_name = allocated_variable_expand (name);
848 if (expanded_name[0] == '\0')
849 fatal (flocp, _("empty variable name"));
851 /* Calculate the variable's new value in VALUE. */
853 switch (flavor)
855 case f_bogus:
856 /* Should not be possible. */
857 abort ();
858 case f_simple:
859 /* A simple variable definition "var := value". Expand the value.
860 We have to allocate memory since otherwise it'll clobber the
861 variable buffer, and we may still need that if we're looking at a
862 target-specific variable. */
863 value = alloc_value = allocated_variable_expand (p);
864 break;
865 case f_conditional:
866 /* A conditional variable definition "var ?= value".
867 The value is set IFF the variable is not defined yet. */
868 v = lookup_variable(expanded_name, strlen(expanded_name));
869 if (v)
871 free(expanded_name);
872 return v;
874 flavor = f_recursive;
875 /* FALLTHROUGH */
876 case f_recursive:
877 /* A recursive variable definition "var = value".
878 The value is used verbatim. */
879 value = p;
880 break;
881 case f_append:
883 /* If we have += but we're in a target variable context, we want to
884 append only with other variables in the context of this target. */
885 if (target_var)
887 append = 1;
888 v = lookup_variable_in_set (expanded_name, strlen (expanded_name),
889 current_variable_set_list->set);
891 else
892 v = lookup_variable (expanded_name, strlen (expanded_name));
894 if (v == 0)
896 /* There was no old value.
897 This becomes a normal recursive definition. */
898 value = p;
899 flavor = f_recursive;
901 else
903 /* Paste the old and new values together in VALUE. */
905 unsigned int oldlen, newlen;
907 if (v->recursive)
908 /* The previous definition of the variable was recursive.
909 The new value is the unexpanded old and new values. */
910 flavor = f_recursive;
911 else
912 /* The previous definition of the variable was simple.
913 The new value comes from the old value, which was expanded
914 when it was set; and from the expanded new value. Allocate
915 memory for the expansion as we may still need the rest of the
916 buffer if we're looking at a target-specific variable. */
917 p = alloc_value = allocated_variable_expand (p);
919 oldlen = strlen (v->value);
920 newlen = strlen (p);
921 value = (char *) alloca (oldlen + 1 + newlen + 1);
922 bcopy (v->value, value, oldlen);
923 value[oldlen] = ' ';
924 bcopy (p, &value[oldlen + 1], newlen + 1);
929 #ifdef __MSDOS__
930 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
931 non-Unix systems don't conform to this default configuration (in
932 fact, most of them don't even have `/bin'). On the other hand,
933 $SHELL in the environment, if set, points to the real pathname of
934 the shell.
935 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
936 the Makefile override $SHELL from the environment. But first, we
937 look for the basename of the shell in the directory where SHELL=
938 points, and along the $PATH; if it is found in any of these places,
939 we define $SHELL to be the actual pathname of the shell. Thus, if
940 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
941 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
942 defining SHELL to be "d:/unix/bash.exe". */
943 if ((origin == o_file || origin == o_override)
944 && strcmp (expanded_name, "SHELL") == 0)
946 char shellpath[PATH_MAX];
947 extern char * __dosexec_find_on_path (const char *, char *[], char *);
949 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
950 if (__dosexec_find_on_path (value, (char **)0, shellpath))
952 char *p;
954 for (p = shellpath; *p; p++)
956 if (*p == '\\')
957 *p = '/';
959 v = define_variable_loc (expanded_name, strlen (expanded_name),
960 shellpath, origin, flavor == f_recursive,
961 flocp);
963 else
965 char *shellbase, *bslash;
966 struct variable *pathv = lookup_variable ("PATH", 4);
967 char *path_string;
968 char *fake_env[2];
969 size_t pathlen = 0;
971 shellbase = strrchr (value, '/');
972 bslash = strrchr (value, '\\');
973 if (!shellbase || bslash > shellbase)
974 shellbase = bslash;
975 if (!shellbase && value[1] == ':')
976 shellbase = value + 1;
977 if (shellbase)
978 shellbase++;
979 else
980 shellbase = value;
982 /* Search for the basename of the shell (with standard
983 executable extensions) along the $PATH. */
984 if (pathv)
985 pathlen = strlen (pathv->value);
986 path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
987 /* On MSDOS, current directory is considered as part of $PATH. */
988 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
989 fake_env[0] = path_string;
990 fake_env[1] = (char *)0;
991 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
993 char *p;
995 for (p = shellpath; *p; p++)
997 if (*p == '\\')
998 *p = '/';
1000 v = define_variable_loc (expanded_name, strlen (expanded_name),
1001 shellpath, origin,
1002 flavor == f_recursive, flocp);
1004 else
1005 v = lookup_variable (expanded_name, strlen (expanded_name));
1007 free (path_string);
1010 else
1011 #endif /* __MSDOS__ */
1012 #ifdef WINDOWS32
1013 if ((origin == o_file || origin == o_override)
1014 && strcmp (expanded_name, "SHELL") == 0)
1016 extern char* default_shell;
1019 * Call shell locator function. If it returns TRUE, then
1020 * set no_default_sh_exe to indicate sh was found and
1021 * set new value for SHELL variable.
1023 if (find_and_set_default_shell(value)) {
1024 v = define_variable_loc (expanded_name, strlen (expanded_name),
1025 default_shell, origin, flavor == f_recursive,
1026 flocp);
1027 no_default_sh_exe = 0;
1030 else
1031 #endif
1033 v = define_variable_loc (expanded_name, strlen (expanded_name), value,
1034 origin, flavor == f_recursive, flocp);
1036 v->append = append;
1038 if (alloc_value)
1039 free (alloc_value);
1040 free (expanded_name);
1042 return v;
1045 /* Print information for variable V, prefixing it with PREFIX. */
1047 static void
1048 print_variable (v, prefix)
1049 register struct variable *v;
1050 char *prefix;
1052 const char *origin;
1054 switch (v->origin)
1056 case o_default:
1057 origin = _("default");
1058 break;
1059 case o_env:
1060 origin = _("environment");
1061 break;
1062 case o_file:
1063 origin = _("makefile");
1064 break;
1065 case o_env_override:
1066 origin = _("environment under -e");
1067 break;
1068 case o_command:
1069 origin = _("command line");
1070 break;
1071 case o_override:
1072 origin = _("`override' directive");
1073 break;
1074 case o_automatic:
1075 origin = _("automatic");
1076 break;
1077 case o_invalid:
1078 default:
1079 abort ();
1081 fputs ("# ", stdout);
1082 fputs (origin, stdout);
1083 if (v->fileinfo.filenm)
1084 printf (_(" (from `%s', line %lu)"),
1085 v->fileinfo.filenm, v->fileinfo.lineno);
1086 putchar ('\n');
1087 fputs (prefix, stdout);
1089 /* Is this a `define'? */
1090 if (v->recursive && strchr (v->value, '\n') != 0)
1091 printf ("define %s\n%s\nendef\n", v->name, v->value);
1092 else
1094 register char *p;
1096 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1098 /* Check if the value is just whitespace. */
1099 p = next_token (v->value);
1100 if (p != v->value && *p == '\0')
1101 /* All whitespace. */
1102 printf ("$(subst ,,%s)", v->value);
1103 else if (v->recursive)
1104 fputs (v->value, stdout);
1105 else
1106 /* Double up dollar signs. */
1107 for (p = v->value; *p != '\0'; ++p)
1109 if (*p == '$')
1110 putchar ('$');
1111 putchar (*p);
1113 putchar ('\n');
1118 /* Print all the variables in SET. PREFIX is printed before
1119 the actual variable definitions (everything else is comments). */
1121 void
1122 print_variable_set (set, prefix)
1123 register struct variable_set *set;
1124 char *prefix;
1126 register unsigned int i, nvariables, per_bucket;
1127 register struct variable *v;
1129 per_bucket = nvariables = 0;
1130 for (i = 0; i < set->buckets; ++i)
1132 register unsigned int this_bucket = 0;
1134 for (v = set->table[i]; v != 0; v = v->next)
1136 ++this_bucket;
1137 print_variable (v, prefix);
1140 nvariables += this_bucket;
1141 if (this_bucket > per_bucket)
1142 per_bucket = this_bucket;
1145 if (nvariables == 0)
1146 puts (_("# No variables."));
1147 else
1149 printf (_("# %u variables in %u hash buckets.\n"),
1150 nvariables, set->buckets);
1151 #ifndef NO_FLOAT
1152 printf (_("# average of %.1f variables per bucket, \
1153 max %u in one bucket.\n"),
1154 (double) nvariables / (double) set->buckets,
1155 per_bucket);
1156 #else
1158 int f = (nvariables * 1000 + 5) / set->buckets;
1159 printf (_("# average of %d.%d variables per bucket, \
1160 max %u in one bucket.\n"),
1161 f/10, f%10,
1162 per_bucket);
1164 #endif
1169 /* Print the data base of variables. */
1171 void
1172 print_variable_data_base ()
1174 puts (_("\n# Variables\n"));
1176 print_variable_set (&global_variable_set, "");
1180 /* Print all the local variables of FILE. */
1182 void
1183 print_file_variables (file)
1184 struct file *file;
1186 if (file->variables != 0)
1187 print_variable_set (file->variables->set, "# ");
1190 #ifdef WINDOWS32
1191 void
1192 sync_Path_environment(void)
1194 char* path = allocated_variable_expand("$(Path)");
1195 static char* environ_path = NULL;
1197 if (!path)
1198 return;
1201 * If done this before, don't leak memory unnecessarily.
1202 * Free the previous entry before allocating new one.
1204 if (environ_path)
1205 free(environ_path);
1208 * Create something WINDOWS32 world can grok
1210 convert_Path_to_windows32(path, ';');
1211 environ_path = concat("Path", "=", path);
1212 putenv(environ_path);
1213 free(path);
1215 #endif