Make sure templates are built.
[make/kirr.git] / variable.c
blob5495fbc231a7e9e60be9d2243031c70abb21ed44
1 /* Internals of variables for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997,
3 2002 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Make is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Make; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "make.h"
22 #include "dep.h"
23 #include "filedef.h"
24 #include "job.h"
25 #include "commands.h"
26 #include "variable.h"
27 #include "rule.h"
28 #ifdef WINDOWS32
29 #include "pathstuff.h"
30 #endif
31 #include "hash.h"
33 /* Hash table of all global variable definitions. */
35 static unsigned long
36 variable_hash_1 (void const *keyv)
38 struct variable const *key = (struct variable const *) keyv;
39 return_STRING_N_HASH_1 (key->name, key->length);
42 static unsigned long
43 variable_hash_2 (void const *keyv)
45 struct variable const *key = (struct variable const *) keyv;
46 return_STRING_N_HASH_2 (key->name, key->length);
49 static int
50 variable_hash_cmp (void const *xv, void const *yv)
52 struct variable const *x = (struct variable const *) xv;
53 struct variable const *y = (struct variable const *) yv;
54 int result = x->length - y->length;
55 if (result)
56 return result;
57 return_STRING_N_COMPARE (x->name, y->name, x->length);
60 #ifndef VARIABLE_BUCKETS
61 #define VARIABLE_BUCKETS 523
62 #endif
63 #ifndef PERFILE_VARIABLE_BUCKETS
64 #define PERFILE_VARIABLE_BUCKETS 23
65 #endif
66 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
67 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
68 #endif
70 static struct variable_set global_variable_set;
71 static struct variable_set_list global_setlist
72 = { 0, &global_variable_set };
73 struct variable_set_list *current_variable_set_list = &global_setlist;
75 /* Implement variables. */
77 void
78 init_hash_global_variable_set ()
80 hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
81 variable_hash_1, variable_hash_2, variable_hash_cmp);
84 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
85 LENGTH is the length of NAME, which does not need to be null-terminated.
86 ORIGIN specifies the origin of the variable (makefile, command line
87 or environment).
88 If RECURSIVE is nonzero a flag is set in the variable saying
89 that it should be recursively re-expanded. */
91 struct variable *
92 define_variable_in_set (name, length, value, origin, recursive, set, flocp)
93 const char *name;
94 unsigned int length;
95 char *value;
96 enum variable_origin origin;
97 int recursive;
98 struct variable_set *set;
99 const struct floc *flocp;
101 struct variable *v;
102 struct variable **var_slot;
103 struct variable var_key;
105 if (set == NULL)
106 set = &global_variable_set;
108 var_key.name = (char *) name;
109 var_key.length = length;
110 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
112 if (env_overrides && origin == o_env)
113 origin = o_env_override;
115 v = *var_slot;
116 if (! HASH_VACANT (v))
118 if (env_overrides && v->origin == o_env)
119 /* V came from in the environment. Since it was defined
120 before the switches were parsed, it wasn't affected by -e. */
121 v->origin = o_env_override;
123 /* A variable of this name is already defined.
124 If the old definition is from a stronger source
125 than this one, don't redefine it. */
126 if ((int) origin >= (int) v->origin)
128 if (v->value != 0)
129 free (v->value);
130 v->value = xstrdup (value);
131 if (flocp != 0)
132 v->fileinfo = *flocp;
133 else
134 v->fileinfo.filenm = 0;
135 v->origin = origin;
136 v->recursive = recursive;
138 return v;
141 /* Create a new variable definition and add it to the hash table. */
143 v = (struct variable *) xmalloc (sizeof (struct variable));
144 v->name = savestring (name, length);
145 v->length = length;
146 hash_insert_at (&set->table, v, var_slot);
147 v->value = xstrdup (value);
148 if (flocp != 0)
149 v->fileinfo = *flocp;
150 else
151 v->fileinfo.filenm = 0;
152 v->origin = origin;
153 v->recursive = recursive;
154 v->expanding = 0;
155 v->exp_count = 0;
156 v->per_target = 0;
157 v->append = 0;
158 v->export = v_default;
160 return v;
163 /* Lookup a variable whose name is a string starting at NAME
164 and with LENGTH chars. NAME need not be null-terminated.
165 Returns address of the `struct variable' containing all info
166 on the variable, or nil if no such variable is defined. */
168 struct variable *
169 lookup_variable (name, length)
170 const char *name;
171 unsigned int length;
173 const struct variable_set_list *setlist;
174 struct variable var_key;
176 var_key.name = (char *) name;
177 var_key.length = length;
179 for (setlist = current_variable_set_list;
180 setlist != 0; setlist = setlist->next)
182 const struct variable_set *set = setlist->set;
183 struct variable *v;
185 v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
186 if (v)
187 return v;
190 #ifdef VMS
191 /* since we don't read envp[] on startup, try to get the
192 variable via getenv() here. */
194 char *vname = alloca (length + 1);
195 char *value;
196 strncpy (vname, name, length);
197 vname[length] = 0;
198 value = getenv (vname);
199 if (value != 0)
201 char *sptr;
202 int scnt;
204 sptr = value;
205 scnt = 0;
207 while ((sptr = strchr (sptr, '$')))
209 scnt++;
210 sptr++;
213 if (scnt > 0)
215 char *nvalue;
216 char *nptr;
218 nvalue = alloca (strlen (value) + scnt + 1);
219 sptr = value;
220 nptr = nvalue;
222 while (*sptr)
224 if (*sptr == '$')
226 *nptr++ = '$';
227 *nptr++ = '$';
229 else
231 *nptr++ = *sptr;
233 sptr++;
236 *nptr = '\0';
237 return define_variable (vname, length, nvalue, o_env, 1);
241 return define_variable (vname, length, value, o_env, 1);
244 #endif /* VMS */
246 return 0;
249 /* Lookup a variable whose name is a string starting at NAME
250 and with LENGTH chars in set SET. NAME need not be null-terminated.
251 Returns address of the `struct variable' containing all info
252 on the variable, or nil if no such variable is defined. */
254 struct variable *
255 lookup_variable_in_set (name, length, set)
256 const char *name;
257 unsigned int length;
258 const struct variable_set *set;
260 struct variable var_key;
262 var_key.name = (char *) name;
263 var_key.length = length;
265 return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
268 /* Initialize FILE's variable set list. If FILE already has a variable set
269 list, the topmost variable set is left intact, but the the rest of the
270 chain is replaced with FILE->parent's setlist. If we're READing a
271 makefile, don't do the pattern variable search now, since the pattern
272 variable might not have been defined yet. */
274 void
275 initialize_file_variables (file, reading)
276 struct file *file;
277 int reading;
279 register struct variable_set_list *l = file->variables;
281 if (l == 0)
283 l = (struct variable_set_list *)
284 xmalloc (sizeof (struct variable_set_list));
285 l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
286 hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
287 variable_hash_1, variable_hash_2, variable_hash_cmp);
288 file->variables = l;
291 if (file->parent == 0)
292 l->next = &global_setlist;
293 else
295 initialize_file_variables (file->parent, reading);
296 l->next = file->parent->variables;
299 /* If we're not reading makefiles and we haven't looked yet, see if
300 we can find a pattern variable. */
302 if (!reading && !file->pat_searched)
304 struct pattern_var *p = lookup_pattern_var (file->name);
306 file->pat_searched = 1;
307 if (p != 0)
309 /* If we found one, insert it between the current target's
310 variables and the next set, whatever it is. */
311 file->pat_variables = (struct variable_set_list *)
312 xmalloc (sizeof (struct variable_set_list));
313 file->pat_variables->set = p->vars->set;
317 /* If we have a pattern variable match, set it up. */
319 if (file->pat_variables != 0)
321 file->pat_variables->next = l->next;
322 l->next = file->pat_variables;
326 /* Pop the top set off the current variable set list,
327 and free all its storage. */
329 static void
330 free_variable_name_and_value (item)
331 void *item;
333 struct variable *v = (struct variable *) item;
334 free (v->name);
335 free (v->value);
338 void
339 pop_variable_scope ()
341 struct variable_set_list *setlist = current_variable_set_list;
342 struct variable_set *set = setlist->set;
344 current_variable_set_list = setlist->next;
345 free ((char *) setlist);
347 hash_map (&set->table, free_variable_name_and_value);
348 hash_free (&set->table, 1);
350 free ((char *) set);
353 struct variable_set_list *
354 create_new_variable_set ()
356 register struct variable_set_list *setlist;
357 register struct variable_set *set;
359 set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
360 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
361 variable_hash_1, variable_hash_2, variable_hash_cmp);
363 setlist = (struct variable_set_list *)
364 xmalloc (sizeof (struct variable_set_list));
365 setlist->set = set;
366 setlist->next = current_variable_set_list;
368 return setlist;
371 /* Create a new variable set and push it on the current setlist. */
373 struct variable_set_list *
374 push_new_variable_scope ()
376 return (current_variable_set_list = create_new_variable_set());
379 /* Merge SET1 into SET0, freeing unused storage in SET1. */
381 static void
382 merge_variable_sets (to_set, from_set)
383 struct variable_set *to_set, *from_set;
385 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
386 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
388 for ( ; from_var_slot < from_var_end; from_var_slot++)
389 if (! HASH_VACANT (*from_var_slot))
391 struct variable *from_var = *from_var_slot;
392 struct variable **to_var_slot
393 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
394 if (HASH_VACANT (*to_var_slot))
395 hash_insert_at (&to_set->table, from_var, to_var_slot);
396 else
398 /* GKM FIXME: delete in from_set->table */
399 free (from_var->value);
400 free (from_var);
405 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
407 void
408 merge_variable_set_lists (setlist0, setlist1)
409 struct variable_set_list **setlist0, *setlist1;
411 register struct variable_set_list *list0 = *setlist0;
412 struct variable_set_list *last0 = 0;
414 while (setlist1 != 0 && list0 != 0)
416 struct variable_set_list *next = setlist1;
417 setlist1 = setlist1->next;
419 merge_variable_sets (list0->set, next->set);
421 last0 = list0;
422 list0 = list0->next;
425 if (setlist1 != 0)
427 if (last0 == 0)
428 *setlist0 = setlist1;
429 else
430 last0->next = setlist1;
434 /* Define the automatic variables, and record the addresses
435 of their structures so we can change their values quickly. */
437 void
438 define_automatic_variables ()
440 #ifdef WINDOWS32
441 extern char* default_shell;
442 #else
443 extern char default_shell[];
444 #endif
445 register struct variable *v;
446 char buf[200];
448 sprintf (buf, "%u", makelevel);
449 (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
451 sprintf (buf, "%s%s%s",
452 version_string,
453 (remote_description == 0 || remote_description[0] == '\0')
454 ? "" : "-",
455 (remote_description == 0 || remote_description[0] == '\0')
456 ? "" : remote_description);
457 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
459 #ifdef __MSDOS__
460 /* Allow to specify a special shell just for Make,
461 and use $COMSPEC as the default $SHELL when appropriate. */
463 static char shell_str[] = "SHELL";
464 const int shlen = sizeof (shell_str) - 1;
465 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
466 struct variable *comp = lookup_variable ("COMSPEC", 7);
468 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
469 if (mshp)
470 (void) define_variable (shell_str, shlen,
471 mshp->value, o_env_override, 0);
472 else if (comp)
474 /* $COMSPEC shouldn't override $SHELL. */
475 struct variable *shp = lookup_variable (shell_str, shlen);
477 if (!shp)
478 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
481 #endif
483 /* This won't override any definition, but it
484 will provide one if there isn't one there. */
485 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
486 v->export = v_export; /* Always export SHELL. */
488 /* On MSDOS we do use SHELL from environment, since
489 it isn't a standard environment variable on MSDOS,
490 so whoever sets it, does that on purpose. */
491 #ifndef __MSDOS__
492 /* Don't let SHELL come from the environment. */
493 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
495 free (v->value);
496 v->origin = o_file;
497 v->value = xstrdup (default_shell);
499 #endif
501 /* Make sure MAKEFILES gets exported if it is set. */
502 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
503 v->export = v_ifset;
505 /* Define the magic D and F variables in terms of
506 the automatic variables they are variations of. */
508 #ifdef VMS
509 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
510 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
511 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
512 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
513 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
514 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
515 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
516 #else
517 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
518 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
519 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
520 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
521 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
522 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
523 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
524 #endif
525 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
526 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
527 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
528 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
529 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
530 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
531 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
534 int export_all_variables;
536 /* Create a new environment for FILE's commands.
537 If FILE is nil, this is for the `shell' function.
538 The child's MAKELEVEL variable is incremented. */
540 char **
541 target_environment (file)
542 struct file *file;
544 struct variable_set_list *set_list;
545 register struct variable_set_list *s;
546 struct hash_table table;
547 struct variable **v_slot;
548 struct variable **v_end;
549 struct variable makelevel_key;
550 char **result_0;
551 char **result;
553 if (file == 0)
554 set_list = current_variable_set_list;
555 else
556 set_list = file->variables;
558 hash_init (&table, VARIABLE_BUCKETS,
559 variable_hash_1, variable_hash_2, variable_hash_cmp);
561 /* Run through all the variable sets in the list,
562 accumulating variables in TABLE. */
563 for (s = set_list; s != 0; s = s->next)
565 struct variable_set *set = s->set;
566 v_slot = (struct variable **) set->table.ht_vec;
567 v_end = v_slot + set->table.ht_size;
568 for ( ; v_slot < v_end; v_slot++)
569 if (! HASH_VACANT (*v_slot))
571 struct variable **new_slot;
572 struct variable *v = *v_slot;
573 char *p = v->name;
575 /* If this is a per-target variable and it hasn't been touched
576 already then look up the global version and take its export
577 value. */
578 if (v->per_target && v->export == v_default)
580 struct variable *gv;
582 gv = lookup_variable_in_set (v->name, strlen(v->name),
583 &global_variable_set);
584 if (gv)
585 v->export = gv->export;
588 switch (v->export)
590 case v_default:
591 if (v->origin == o_default || v->origin == o_automatic)
592 /* Only export default variables by explicit request. */
593 continue;
595 if (! export_all_variables
596 && v->origin != o_command
597 && v->origin != o_env && v->origin != o_env_override)
598 continue;
600 if (*p != '_' && (*p < 'A' || *p > 'Z')
601 && (*p < 'a' || *p > 'z'))
602 continue;
603 for (++p; *p != '\0'; ++p)
604 if (*p != '_' && (*p < 'a' || *p > 'z')
605 && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9'))
606 continue;
607 if (*p != '\0')
608 continue;
609 break;
611 case v_export:
612 break;
614 case v_noexport:
615 continue;
617 case v_ifset:
618 if (v->origin == o_default)
619 continue;
620 break;
623 new_slot = (struct variable **) hash_find_slot (&table, v);
624 if (HASH_VACANT (*new_slot))
625 hash_insert_at (&table, v, new_slot);
629 makelevel_key.name = MAKELEVEL_NAME;
630 makelevel_key.length = MAKELEVEL_LENGTH;
631 hash_delete (&table, &makelevel_key);
633 result = result_0 = (char **) xmalloc ((table.ht_fill + 2) * sizeof (char *));
635 v_slot = (struct variable **) table.ht_vec;
636 v_end = v_slot + table.ht_size;
637 for ( ; v_slot < v_end; v_slot++)
638 if (! HASH_VACANT (*v_slot))
640 struct variable *v = *v_slot;
642 /* If V is recursively expanded and didn't come from the environment,
643 expand its value. If it came from the environment, it should
644 go back into the environment unchanged. */
645 if (v->recursive
646 && v->origin != o_env && v->origin != o_env_override)
648 char *value = recursively_expand_for_file (v, file);
649 #ifdef WINDOWS32
650 if (strcmp(v->name, "Path") == 0 ||
651 strcmp(v->name, "PATH") == 0)
652 convert_Path_to_windows32(value, ';');
653 #endif
654 *result++ = concat (v->name, "=", value);
655 free (value);
657 else
659 #ifdef WINDOWS32
660 if (strcmp(v->name, "Path") == 0 ||
661 strcmp(v->name, "PATH") == 0)
662 convert_Path_to_windows32(v->value, ';');
663 #endif
664 *result++ = concat (v->name, "=", v->value);
668 *result = (char *) xmalloc (100);
669 (void) sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
670 *++result = 0;
672 hash_free (&table, 0);
674 return result_0;
677 /* Given a variable, a value, and a flavor, define the variable.
678 See the try_variable_definition() function for details on the parameters. */
680 struct variable *
681 do_variable_definition (flocp, varname, value, origin, flavor, target_var)
682 const struct floc *flocp;
683 const char *varname;
684 char *value;
685 enum variable_origin origin;
686 enum variable_flavor flavor;
687 int target_var;
689 char *p, *alloc_value = NULL;
690 struct variable *v;
691 int append = 0;
693 /* Calculate the variable's new value in VALUE. */
695 switch (flavor)
697 default:
698 case f_bogus:
699 /* Should not be possible. */
700 abort ();
701 case f_simple:
702 /* A simple variable definition "var := value". Expand the value.
703 We have to allocate memory since otherwise it'll clobber the
704 variable buffer, and we may still need that if we're looking at a
705 target-specific variable. */
706 p = alloc_value = allocated_variable_expand (value);
707 break;
708 case f_conditional:
709 /* A conditional variable definition "var ?= value".
710 The value is set IFF the variable is not defined yet. */
711 v = lookup_variable (varname, strlen (varname));
712 if (v)
713 return v;
715 flavor = f_recursive;
716 /* FALLTHROUGH */
717 case f_recursive:
718 /* A recursive variable definition "var = value".
719 The value is used verbatim. */
720 p = value;
721 break;
722 case f_append:
724 /* If we have += but we're in a target variable context, we want to
725 append only with other variables in the context of this target. */
726 if (target_var)
728 append = 1;
729 v = lookup_variable_in_set (varname, strlen (varname),
730 current_variable_set_list->set);
732 else
733 v = lookup_variable (varname, strlen (varname));
735 if (v == 0)
737 /* There was no old value.
738 This becomes a normal recursive definition. */
739 p = value;
740 flavor = f_recursive;
742 else
744 /* Paste the old and new values together in VALUE. */
746 unsigned int oldlen, newlen;
748 p = value;
749 if (v->recursive)
750 /* The previous definition of the variable was recursive.
751 The new value is the unexpanded old and new values. */
752 flavor = f_recursive;
753 else
754 /* The previous definition of the variable was simple.
755 The new value comes from the old value, which was expanded
756 when it was set; and from the expanded new value. Allocate
757 memory for the expansion as we may still need the rest of the
758 buffer if we're looking at a target-specific variable. */
759 p = alloc_value = allocated_variable_expand (p);
761 oldlen = strlen (v->value);
762 newlen = strlen (p);
763 p = (char *) alloca (oldlen + 1 + newlen + 1);
764 bcopy (v->value, p, oldlen);
765 p[oldlen] = ' ';
766 bcopy (value, &p[oldlen + 1], newlen + 1);
771 #ifdef __MSDOS__
772 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
773 non-Unix systems don't conform to this default configuration (in
774 fact, most of them don't even have `/bin'). On the other hand,
775 $SHELL in the environment, if set, points to the real pathname of
776 the shell.
777 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
778 the Makefile override $SHELL from the environment. But first, we
779 look for the basename of the shell in the directory where SHELL=
780 points, and along the $PATH; if it is found in any of these places,
781 we define $SHELL to be the actual pathname of the shell. Thus, if
782 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
783 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
784 defining SHELL to be "d:/unix/bash.exe". */
785 if ((origin == o_file || origin == o_override)
786 && strcmp (varname, "SHELL") == 0)
788 char shellpath[PATH_MAX];
789 extern char * __dosexec_find_on_path (const char *, char *[], char *);
791 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
792 if (__dosexec_find_on_path (p, (char **)0, shellpath))
794 char *p;
796 for (p = shellpath; *p; p++)
798 if (*p == '\\')
799 *p = '/';
801 v = define_variable_loc (varname, strlen (varname),
802 shellpath, origin, flavor == f_recursive,
803 flocp);
805 else
807 char *shellbase, *bslash;
808 struct variable *pathv = lookup_variable ("PATH", 4);
809 char *path_string;
810 char *fake_env[2];
811 size_t pathlen = 0;
813 shellbase = strrchr (p, '/');
814 bslash = strrchr (p, '\\');
815 if (!shellbase || bslash > shellbase)
816 shellbase = bslash;
817 if (!shellbase && p[1] == ':')
818 shellbase = p + 1;
819 if (shellbase)
820 shellbase++;
821 else
822 shellbase = p;
824 /* Search for the basename of the shell (with standard
825 executable extensions) along the $PATH. */
826 if (pathv)
827 pathlen = strlen (pathv->value);
828 path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
829 /* On MSDOS, current directory is considered as part of $PATH. */
830 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
831 fake_env[0] = path_string;
832 fake_env[1] = (char *)0;
833 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
835 char *p;
837 for (p = shellpath; *p; p++)
839 if (*p == '\\')
840 *p = '/';
842 v = define_variable_loc (varname, strlen (varname),
843 shellpath, origin,
844 flavor == f_recursive, flocp);
846 else
847 v = lookup_variable (varname, strlen (varname));
849 free (path_string);
852 else
853 #endif /* __MSDOS__ */
854 #ifdef WINDOWS32
855 if ((origin == o_file || origin == o_override) && streq (varname, "SHELL"))
857 extern char *default_shell;
859 /* Call shell locator function. If it returns TRUE, then
860 set no_default_sh_exe to indicate sh was found and
861 set new value for SHELL variable. */
863 if (find_and_set_default_shell (p))
865 v = define_variable_in_set (varname, strlen (varname), default_shell,
866 origin, flavor == f_recursive,
867 (target_var
868 ? current_variable_set_list->set
869 : NULL),
870 flocp);
871 no_default_sh_exe = 0;
873 else
874 v = lookup_variable (varname, strlen (varname));
876 else
877 #endif
879 /* If we are defining variables inside an $(eval ...), we might have a
880 different variable context pushed, not the global context (maybe we're
881 inside a $(call ...) or something. Since this function is only ever
882 invoked in places where we want to define globally visible variables,
883 make sure we define this variable in the global set. */
885 v = define_variable_in_set (varname, strlen (varname), p,
886 origin, flavor == f_recursive,
887 (target_var
888 ? current_variable_set_list->set : NULL),
889 flocp);
890 v->append = append;
892 if (alloc_value)
893 free (alloc_value);
895 return v;
898 /* Try to interpret LINE (a null-terminated string) as a variable definition.
900 ORIGIN may be o_file, o_override, o_env, o_env_override,
901 or o_command specifying that the variable definition comes
902 from a makefile, an override directive, the environment with
903 or without the -e switch, or the command line.
905 See the comments for parse_variable_definition().
907 If LINE was recognized as a variable definition, a pointer to its `struct
908 variable' is returned. If LINE is not a variable definition, NULL is
909 returned. */
911 struct variable *
912 try_variable_definition (flocp, line, origin, target_var)
913 const struct floc *flocp;
914 char *line;
915 enum variable_origin origin;
916 int target_var;
918 register int c;
919 register char *p = line;
920 register char *beg;
921 register char *end;
922 enum variable_flavor flavor = f_bogus;
923 char *name, *expanded_name;
924 struct variable *v;
926 while (1)
928 c = *p++;
929 if (c == '\0' || c == '#')
930 return 0;
931 if (c == '=')
933 end = p - 1;
934 flavor = f_recursive;
935 break;
937 else if (c == ':')
938 if (*p == '=')
940 end = p++ - 1;
941 flavor = f_simple;
942 break;
944 else
945 /* A colon other than := is a rule line, not a variable defn. */
946 return 0;
947 else if (c == '+' && *p == '=')
949 end = p++ - 1;
950 flavor = f_append;
951 break;
953 else if (c == '?' && *p == '=')
955 end = p++ - 1;
956 flavor = f_conditional;
957 break;
959 else if (c == '$')
961 /* This might begin a variable expansion reference. Make sure we
962 don't misrecognize chars inside the reference as =, := or +=. */
963 char closeparen;
964 int count;
965 c = *p++;
966 if (c == '(')
967 closeparen = ')';
968 else if (c == '{')
969 closeparen = '}';
970 else
971 continue; /* Nope. */
973 /* P now points past the opening paren or brace.
974 Count parens or braces until it is matched. */
975 count = 0;
976 for (; *p != '\0'; ++p)
978 if (*p == c)
979 ++count;
980 else if (*p == closeparen && --count < 0)
982 ++p;
983 break;
989 beg = next_token (line);
990 while (end > beg && isblank ((unsigned char)end[-1]))
991 --end;
992 p = next_token (p);
994 /* Expand the name, so "$(foo)bar = baz" works. */
995 name = (char *) alloca (end - beg + 1);
996 bcopy (beg, name, end - beg);
997 name[end - beg] = '\0';
998 expanded_name = allocated_variable_expand (name);
1000 if (expanded_name[0] == '\0')
1001 fatal (flocp, _("empty variable name"));
1003 v = do_variable_definition (flocp, expanded_name, p,
1004 origin, flavor, target_var);
1006 free (expanded_name);
1008 return v;
1011 /* Print information for variable V, prefixing it with PREFIX. */
1013 static void
1014 print_variable (v, prefix)
1015 register struct variable *v;
1016 char *prefix;
1018 const char *origin;
1020 switch (v->origin)
1022 case o_default:
1023 origin = _("default");
1024 break;
1025 case o_env:
1026 origin = _("environment");
1027 break;
1028 case o_file:
1029 origin = _("makefile");
1030 break;
1031 case o_env_override:
1032 origin = _("environment under -e");
1033 break;
1034 case o_command:
1035 origin = _("command line");
1036 break;
1037 case o_override:
1038 origin = _("`override' directive");
1039 break;
1040 case o_automatic:
1041 origin = _("automatic");
1042 break;
1043 case o_invalid:
1044 default:
1045 abort ();
1047 fputs ("# ", stdout);
1048 fputs (origin, stdout);
1049 if (v->fileinfo.filenm)
1050 printf (_(" (from `%s', line %lu)"),
1051 v->fileinfo.filenm, v->fileinfo.lineno);
1052 putchar ('\n');
1053 fputs (prefix, stdout);
1055 /* Is this a `define'? */
1056 if (v->recursive && strchr (v->value, '\n') != 0)
1057 printf ("define %s\n%s\nendef\n", v->name, v->value);
1058 else
1060 register char *p;
1062 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1064 /* Check if the value is just whitespace. */
1065 p = next_token (v->value);
1066 if (p != v->value && *p == '\0')
1067 /* All whitespace. */
1068 printf ("$(subst ,,%s)", v->value);
1069 else if (v->recursive)
1070 fputs (v->value, stdout);
1071 else
1072 /* Double up dollar signs. */
1073 for (p = v->value; *p != '\0'; ++p)
1075 if (*p == '$')
1076 putchar ('$');
1077 putchar (*p);
1079 putchar ('\n');
1084 /* Print all the variables in SET. PREFIX is printed before
1085 the actual variable definitions (everything else is comments). */
1087 void
1088 print_variable_set (set, prefix)
1089 register struct variable_set *set;
1090 char *prefix;
1092 hash_map_arg (&set->table, print_variable, prefix);
1094 fputs (_("# variable set hash-table stats:\n"), stdout);
1095 fputs ("# ", stdout);
1096 hash_print_stats (&set->table, stdout);
1097 putc ('\n', stdout);
1100 /* Print the data base of variables. */
1102 void
1103 print_variable_data_base ()
1105 puts (_("\n# Variables\n"));
1107 print_variable_set (&global_variable_set, "");
1111 /* Print all the local variables of FILE. */
1113 void
1114 print_file_variables (file)
1115 struct file *file;
1117 if (file->variables != 0)
1118 print_variable_set (file->variables->set, "# ");
1121 #ifdef WINDOWS32
1122 void
1123 sync_Path_environment(void)
1125 char* path = allocated_variable_expand("$(Path)");
1126 static char* environ_path = NULL;
1128 if (!path)
1129 return;
1132 * If done this before, don't leak memory unnecessarily.
1133 * Free the previous entry before allocating new one.
1135 if (environ_path)
1136 free(environ_path);
1139 * Create something WINDOWS32 world can grok
1141 convert_Path_to_windows32(path, ';');
1142 environ_path = concat("Path", "=", path);
1143 putenv(environ_path);
1144 free(path);
1146 #endif