source: trunk/third/gmake/variable.c @ 15972

Revision 15972, 33.4 KB checked in by ghudson, 24 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r15971, which included commits to RCS files with non-trunk default branches.
Line 
1/* Internals of variables for GNU Make.
2Copyright (C) 1988,89,90,91,92,93,94,96,97 Free Software Foundation, Inc.
3This file is part of GNU Make.
4
5GNU Make is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 2, or (at your option)
8any later version.
9
10GNU Make is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with GNU Make; see the file COPYING.  If not, write to
17the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18Boston, MA 02111-1307, USA.  */
19
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
30
31/* Hash table of all global variable definitions.  */
32
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
42static struct variable *variable_table[VARIABLE_BUCKETS];
43static struct variable_set global_variable_set
44  = { variable_table, VARIABLE_BUCKETS };
45static struct variable_set_list global_setlist
46  = { 0, &global_variable_set };
47struct variable_set_list *current_variable_set_list = &global_setlist;
48
49static struct variable *lookup_variable_in_set PARAMS ((char *name,
50                          unsigned int length, struct variable_set *set));
51
52/* Implement variables.  */
53
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.  */
60
61struct variable *
62define_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;
70{
71  register unsigned int i;
72  register unsigned int hashval;
73  register struct variable *v;
74
75  hashval = 0;
76  for (i = 0; i < length; ++i)
77    HASH (hashval, name[i]);
78  hashval %= set->buckets;
79
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;
85
86  if (env_overrides && origin == o_env)
87    origin = o_env_override;
88
89  if (v != 0)
90    {
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;
95
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)
100        {
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;
110        }
111      return v;
112    }
113
114  /* Create a new variable definition and add it to the hash table.  */
115
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;
132}
133
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.
138
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.  */
143
144struct variable *
145lookup_variable (name, length)
146     char *name;
147     unsigned int length;
148{
149  register struct variable_set_list *setlist;
150  struct variable *firstv = 0;
151
152  register unsigned int i;
153  register unsigned int rawhash = 0;
154
155  for (i = 0; i < length; ++i)
156    HASH (rawhash, name[i]);
157
158  for (setlist = current_variable_set_list;
159       setlist != 0; setlist = setlist->next)
160    {
161      register struct variable_set *set = setlist->set;
162      register unsigned int hashval = rawhash % set->buckets;
163      register struct variable *v;
164
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;
171
172      /* If we didn't find anything, go to the next set list.  */
173      if (!v)
174        continue;
175
176      /* If it's not being expanded already, we're done.  */
177      if (!v->expanding)
178        return v;
179
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;
184    }
185
186#ifdef VMS
187  /* since we don't read envp[] on startup, try to get the
188     variable via getenv() here.  */
189  if (!firstv)
190    {
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)
197        {
198          char *sptr;
199          int scnt;
200
201          sptr = value;
202          scnt = 0;
203
204          if (listp)
205            *listp = current_variable_set_list;
206
207          while ((sptr = strchr (sptr, '$')))
208            {
209              scnt++;
210              sptr++;
211            }
212
213          if (scnt > 0)
214            {
215              char *nvalue;
216              char *nptr;
217
218              nvalue = alloca (length + scnt + 1);
219              sptr = value;
220              nptr = nvalue;
221
222              while (*sptr)
223                {
224                  if (*sptr == '$')
225                    {
226                      *nptr++ = '$';
227                      *nptr++ = '$';
228                    }
229                  else
230                    {
231                      *nptr++ = *sptr;
232                    }
233                  sptr++;
234                }
235
236              return define_variable (vname, length, nvalue, o_env, 1);
237
238            }
239
240          return define_variable (vname, length, value, o_env, 1);
241        }
242    }
243#endif /* VMS */
244
245  return firstv;
246}
247
248/* Lookup a variable whose name is a string starting at NAME
249   and with LENGTH chars in set SET.  NAME need not be null-terminated.
250   Returns address of the `struct variable' containing all info
251   on the variable, or nil if no such variable is defined.  */
252
253static struct variable *
254lookup_variable_in_set (name, length, set)
255     char *name;
256     unsigned int length;
257     struct variable_set *set;
258{
259  register unsigned int i;
260  register unsigned int hash = 0;
261  register struct variable *v;
262
263  for (i = 0; i < length; ++i)
264    HASH (hash, name[i]);
265  hash %= set->buckets;
266
267  for (v = set->table[hash]; v != 0; v = v->next)
268    if (*v->name == *name
269        && strneq (v->name + 1, name + 1, length - 1)
270        && v->name[length] == 0)
271      return v;
272
273  return 0;
274}
275
276/* Initialize FILE's variable set list.  If FILE already has a variable set
277   list, the topmost variable set is left intact, but the the rest of the
278   chain is replaced with FILE->parent's setlist.  If we're READing a
279   makefile, don't do the pattern variable search now, since the pattern
280   variable might not have been defined yet.  */
281
282void
283initialize_file_variables (file, reading)
284     struct file *file;
285     int reading;
286{
287  register struct variable_set_list *l = file->variables;
288
289  if (l == 0)
290    {
291      l = (struct variable_set_list *)
292        xmalloc (sizeof (struct variable_set_list));
293      l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
294      l->set->buckets = PERFILE_VARIABLE_BUCKETS;
295      l->set->table = (struct variable **)
296        xmalloc (l->set->buckets * sizeof (struct variable *));
297      bzero ((char *) l->set->table,
298             l->set->buckets * sizeof (struct variable *));
299      file->variables = l;
300    }
301
302  if (file->parent == 0)
303    l->next = &global_setlist;
304  else
305    {
306      initialize_file_variables (file->parent, reading);
307      l->next = file->parent->variables;
308    }
309
310  /* If we're not reading makefiles and we haven't looked yet, see if
311     we can find a pattern variable.  */
312
313  if (!reading && !file->pat_searched)
314    {
315      struct pattern_var *p = lookup_pattern_var (file->name);
316
317      file->pat_searched = 1;
318      if (p != 0)
319        {
320          /* If we found one, insert it between the current target's
321             variables and the next set, whatever it is.  */
322          file->pat_variables = (struct variable_set_list *)
323            xmalloc (sizeof (struct variable_set_list));
324          file->pat_variables->set = p->vars->set;
325        }
326    }
327
328  /* If we have a pattern variable match, set it up.  */
329
330  if (file->pat_variables != 0)
331    {
332      file->pat_variables->next = l->next;
333      l->next = file->pat_variables;
334    }
335}
336
337/* Pop the top set off the current variable set list,
338   and free all its storage.  */
339
340void
341pop_variable_scope ()
342{
343  register struct variable_set_list *setlist = current_variable_set_list;
344  register struct variable_set *set = setlist->set;
345  register unsigned int i;
346
347  current_variable_set_list = setlist->next;
348  free ((char *) setlist);
349
350  for (i = 0; i < set->buckets; ++i)
351    {
352      register struct variable *next = set->table[i];
353      while (next != 0)
354        {
355          register struct variable *v = next;
356          next = v->next;
357
358          free (v->name);
359          if (v->value)
360            free (v->value);
361          free ((char *) v);
362        }
363    }
364  free ((char *) set->table);
365  free ((char *) set);
366}
367
368struct variable_set_list *
369create_new_variable_set ()
370{
371  register struct variable_set_list *setlist;
372  register struct variable_set *set;
373
374  set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
375  set->buckets = SMALL_SCOPE_VARIABLE_BUCKETS;
376  set->table = (struct variable **)
377    xmalloc (set->buckets * sizeof (struct variable *));
378  bzero ((char *) set->table, set->buckets * sizeof (struct variable *));
379
380  setlist = (struct variable_set_list *)
381    xmalloc (sizeof (struct variable_set_list));
382  setlist->set = set;
383  setlist->next = current_variable_set_list;
384
385  return setlist;
386}
387
388/* Create a new variable set and push it on the current setlist.  */
389
390struct variable_set_list *
391push_new_variable_scope ()
392{
393  return (current_variable_set_list = create_new_variable_set());
394}
395
396/* Merge SET1 into SET0, freeing unused storage in SET1.  */
397
398static void
399merge_variable_sets (set0, set1)
400     struct variable_set *set0, *set1;
401{
402  register unsigned int bucket1;
403
404  for (bucket1 = 0; bucket1 < set1->buckets; ++bucket1)
405    {
406      register struct variable *v1 = set1->table[bucket1];
407      while (v1 != 0)
408        {
409          struct variable *next = v1->next;
410          unsigned int bucket0;
411          register struct variable *v0;
412
413          if (set1->buckets >= set0->buckets)
414            bucket0 = bucket1;
415          else
416            {
417              register char *n;
418              bucket0 = 0;
419              for (n = v1->name; *n != '\0'; ++n)
420                HASH (bucket0, *n);
421            }
422          bucket0 %= set0->buckets;
423
424          for (v0 = set0->table[bucket0]; v0 != 0; v0 = v0->next)
425            if (streq (v0->name, v1->name))
426              break;
427
428          if (v0 == 0)
429            {
430              /* There is no variable in SET0 with the same name.  */
431              v1->next = set0->table[bucket0];
432              set0->table[bucket0] = v1;
433            }
434          else
435            {
436              /* The same variable exists in both sets.
437                 SET0 takes precedence.  */
438              free (v1->value);
439              free ((char *) v1);
440            }
441
442          v1 = next;
443        }
444    }
445}
446
447/* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1.  */
448
449void
450merge_variable_set_lists (setlist0, setlist1)
451     struct variable_set_list **setlist0, *setlist1;
452{
453  register struct variable_set_list *list0 = *setlist0;
454  struct variable_set_list *last0 = 0;
455
456  while (setlist1 != 0 && list0 != 0)
457    {
458      struct variable_set_list *next = setlist1;
459      setlist1 = setlist1->next;
460
461      merge_variable_sets (list0->set, next->set);
462
463      last0 = list0;
464      list0 = list0->next;
465    }
466
467  if (setlist1 != 0)
468    {
469      if (last0 == 0)
470        *setlist0 = setlist1;
471      else
472        last0->next = setlist1;
473    }
474}
475
476/* Define the automatic variables, and record the addresses
477   of their structures so we can change their values quickly.  */
478
479void
480define_automatic_variables ()
481{
482#ifdef WINDOWS32
483  extern char* default_shell;
484#else
485  extern char default_shell[];
486#endif
487  register struct variable *v;
488  char buf[200];
489
490  sprintf (buf, "%u", makelevel);
491  (void) define_variable ("MAKELEVEL", 9, buf, o_env, 0);
492
493  sprintf (buf, "%s%s%s",
494           version_string,
495           (remote_description == 0 || remote_description[0] == '\0')
496           ? "" : "-",
497           (remote_description == 0 || remote_description[0] == '\0')
498           ? "" : remote_description);
499  (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
500
501#ifdef  __MSDOS__
502  /* Allow to specify a special shell just for Make,
503     and use $COMSPEC as the default $SHELL when appropriate.  */
504  {
505    static char shell_str[] = "SHELL";
506    const int shlen = sizeof (shell_str) - 1;
507    struct variable *mshp = lookup_variable ("MAKESHELL", 9);
508    struct variable *comp = lookup_variable ("COMSPEC", 7);
509
510    /* Make $MAKESHELL override $SHELL even if -e is in effect.  */
511    if (mshp)
512      (void) define_variable (shell_str, shlen,
513                              mshp->value, o_env_override, 0);
514    else if (comp)
515      {
516        /* $COMSPEC shouldn't override $SHELL.  */
517        struct variable *shp = lookup_variable (shell_str, shlen);
518
519        if (!shp)
520          (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
521      }
522  }
523#endif
524
525  /* This won't override any definition, but it
526     will provide one if there isn't one there.  */
527  v = define_variable ("SHELL", 5, default_shell, o_default, 0);
528  v->export = v_export;         /* Always export SHELL.  */
529
530  /* On MSDOS we do use SHELL from environment, since
531     it isn't a standard environment variable on MSDOS,
532     so whoever sets it, does that on purpose.  */
533#ifndef __MSDOS__
534  /* Don't let SHELL come from the environment.  */
535  if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
536    {
537      free (v->value);
538      v->origin = o_file;
539      v->value = xstrdup (default_shell);
540    }
541#endif
542
543  /* Make sure MAKEFILES gets exported if it is set.  */
544  v = define_variable ("MAKEFILES", 9, "", o_default, 0);
545  v->export = v_ifset;
546
547  /* Define the magic D and F variables in terms of
548     the automatic variables they are variations of.  */
549
550#ifdef VMS
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  define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
556  define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
557  define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
558#else
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  define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
564  define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
565  define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
566#endif
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);
571  define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
572  define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
573  define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
574}
575
576int export_all_variables;
577
578/* Create a new environment for FILE's commands.
579   If FILE is nil, this is for the `shell' function.
580   The child's MAKELEVEL variable is incremented.  */
581
582char **
583target_environment (file)
584     struct file *file;
585{
586  struct variable_set_list *set_list;
587  register struct variable_set_list *s;
588  struct variable_bucket
589    {
590      struct variable_bucket *next;
591      struct variable *variable;
592    };
593  struct variable_bucket **table;
594  unsigned int buckets;
595  register unsigned int i;
596  register unsigned nvariables;
597  char **result;
598  unsigned int mklev_hash;
599
600  if (file == 0)
601    set_list = current_variable_set_list;
602  else
603    set_list = file->variables;
604
605  /* Find the lowest number of buckets in any set in the list.  */
606  s = set_list;
607  buckets = s->set->buckets;
608  for (s = s->next; s != 0; s = s->next)
609    if (s->set->buckets < buckets)
610      buckets = s->set->buckets;
611
612  /* Find the hash value of the bucket `MAKELEVEL' will fall into.  */
613  {
614    char *p = "MAKELEVEL";
615    mklev_hash = 0;
616    while (*p != '\0')
617      HASH (mklev_hash, *p++);
618  }
619
620  /* Temporarily allocate a table with that many buckets.  */
621  table = (struct variable_bucket **)
622    alloca (buckets * sizeof (struct variable_bucket *));
623  bzero ((char *) table, buckets * sizeof (struct variable_bucket *));
624
625  /* Run through all the variable sets in the list,
626     accumulating variables in TABLE.  */
627  nvariables = 0;
628  for (s = set_list; s != 0; s = s->next)
629    {
630      register struct variable_set *set = s->set;
631      for (i = 0; i < set->buckets; ++i)
632        {
633          register struct variable *v;
634          for (v = set->table[i]; v != 0; v = v->next)
635            {
636              unsigned int j = i % buckets;
637              register struct variable_bucket *ov;
638              register char *p = v->name;
639
640              if (i == mklev_hash % set->buckets
641                  && streq (v->name, "MAKELEVEL"))
642                /* Don't include MAKELEVEL because it will be
643                   added specially at the end.  */
644                continue;
645
646              /* If this is a per-target variable and it hasn't been touched
647                 already then look up the global version and take its export
648                 value.  */
649              if (v->per_target && v->export == v_default)
650                {
651                  struct variable *gv;
652
653                  gv = lookup_variable_in_set(v->name, strlen(v->name),
654                                              &global_variable_set);
655                  if (gv)
656                    v->export = gv->export;
657                }
658
659              switch (v->export)
660                {
661                case v_default:
662                  if (v->origin == o_default || v->origin == o_automatic)
663                    /* Only export default variables by explicit request.  */
664                    continue;
665
666                  if (! export_all_variables
667                      && v->origin != o_command
668                      && v->origin != o_env && v->origin != o_env_override)
669                    continue;
670
671                  if (*p != '_' && (*p < 'A' || *p > 'Z')
672                      && (*p < 'a' || *p > 'z'))
673                    continue;
674                  for (++p; *p != '\0'; ++p)
675                    if (*p != '_' && (*p < 'a' || *p > 'z')
676                        && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9'))
677                      continue;
678                  if (*p != '\0')
679                    continue;
680                  break;
681
682                case v_export:
683                  break;
684
685                case v_noexport:
686                  continue;
687
688                case v_ifset:
689                  if (v->origin == o_default)
690                    continue;
691                  break;
692                }
693
694              /* If this was from a different-sized hash table, then
695                 recalculate the bucket it goes in.  */
696              if (set->buckets != buckets)
697                {
698                  register char *np;
699
700                  j = 0;
701                  for (np = v->name; *np != '\0'; ++np)
702                    HASH (j, *np);
703                  j %= buckets;
704                }
705
706              for (ov = table[j]; ov != 0; ov = ov->next)
707                if (streq (v->name, ov->variable->name))
708                  break;
709
710              if (ov == 0)
711                {
712                  register struct variable_bucket *entry;
713                  entry = (struct variable_bucket *)
714                    alloca (sizeof (struct variable_bucket));
715                  entry->next = table[j];
716                  entry->variable = v;
717                  table[j] = entry;
718                  ++nvariables;
719                }
720            }
721        }
722    }
723
724  result = (char **) xmalloc ((nvariables + 2) * sizeof (char *));
725  nvariables = 0;
726  for (i = 0; i < buckets; ++i)
727    {
728      register struct variable_bucket *b;
729      for (b = table[i]; b != 0; b = b->next)
730        {
731          register struct variable *v = b->variable;
732
733          /* If V is recursively expanded and didn't come from the environment,
734             expand its value.  If it came from the environment, it should
735             go back into the environment unchanged.  */
736          if (v->recursive
737              && v->origin != o_env && v->origin != o_env_override)
738            {
739              char *value = recursively_expand (v);
740#ifdef WINDOWS32
741              if (strcmp(v->name, "Path") == 0 ||
742                  strcmp(v->name, "PATH") == 0)
743                convert_Path_to_windows32(value, ';');
744#endif
745              result[nvariables++] = concat (v->name, "=", value);
746              free (value);
747            }
748          else
749#ifdef WINDOWS32
750          {
751            if (strcmp(v->name, "Path") == 0 ||
752                strcmp(v->name, "PATH") == 0)
753              convert_Path_to_windows32(v->value, ';');
754            result[nvariables++] = concat (v->name, "=", v->value);
755          }
756#else
757            result[nvariables++] = concat (v->name, "=", v->value);
758#endif
759        }
760    }
761  result[nvariables] = (char *) xmalloc (100);
762  (void) sprintf (result[nvariables], "MAKELEVEL=%u", makelevel + 1);
763  result[++nvariables] = 0;
764
765  return result;
766}
767
768/* Try to interpret LINE (a null-terminated string) as a variable definition.
769
770   ORIGIN may be o_file, o_override, o_env, o_env_override,
771   or o_command specifying that the variable definition comes
772   from a makefile, an override directive, the environment with
773   or without the -e switch, or the command line.
774
775   See the comments for parse_variable_definition().
776
777   If LINE was recognized as a variable definition, a pointer to its `struct
778   variable' is returned.  If LINE is not a variable definition, NULL is
779   returned.  */
780
781struct variable *
782try_variable_definition (flocp, line, origin, target_var)
783     const struct floc *flocp;
784     char *line;
785     enum variable_origin origin;
786     int target_var;
787{
788  register int c;
789  register char *p = line;
790  register char *beg;
791  register char *end;
792  enum { f_bogus,
793         f_simple, f_recursive, f_append, f_conditional } flavor = f_bogus;
794  char *name, *expanded_name, *value, *alloc_value=NULL;
795  struct variable *v;
796  int append = 0;
797
798  while (1)
799    {
800      c = *p++;
801      if (c == '\0' || c == '#')
802        return 0;
803      if (c == '=')
804        {
805          end = p - 1;
806          flavor = f_recursive;
807          break;
808        }
809      else if (c == ':')
810        if (*p == '=')
811          {
812            end = p++ - 1;
813            flavor = f_simple;
814            break;
815          }
816        else
817          /* A colon other than := is a rule line, not a variable defn.  */
818          return 0;
819      else if (c == '+' && *p == '=')
820        {
821          end = p++ - 1;
822          flavor = f_append;
823          break;
824        }
825      else if (c == '?' && *p == '=')
826        {
827          end = p++ - 1;
828          flavor = f_conditional;
829          break;
830        }
831      else if (c == '$')
832        {
833          /* This might begin a variable expansion reference.  Make sure we
834             don't misrecognize chars inside the reference as =, := or +=.  */
835          char closeparen;
836          int count;
837          c = *p++;
838          if (c == '(')
839            closeparen = ')';
840          else if (c == '{')
841            closeparen = '}';
842          else
843            continue;           /* Nope.  */
844
845          /* P now points past the opening paren or brace.
846             Count parens or braces until it is matched.  */
847          count = 0;
848          for (; *p != '\0'; ++p)
849            {
850              if (*p == c)
851                ++count;
852              else if (*p == closeparen && --count < 0)
853                {
854                  ++p;
855                  break;
856                }
857            }
858        }
859    }
860
861  beg = next_token (line);
862  while (end > beg && isblank ((unsigned char)end[-1]))
863    --end;
864  p = next_token (p);
865
866  /* Expand the name, so "$(foo)bar = baz" works.  */
867  name = (char *) alloca (end - beg + 1);
868  bcopy (beg, name, end - beg);
869  name[end - beg] = '\0';
870  expanded_name = allocated_variable_expand (name);
871
872  if (expanded_name[0] == '\0')
873    fatal (flocp, _("empty variable name"));
874
875  /* Calculate the variable's new value in VALUE.  */
876
877  switch (flavor)
878    {
879    case f_bogus:
880      /* Should not be possible.  */
881      abort ();
882    case f_simple:
883      /* A simple variable definition "var := value".  Expand the value.
884         We have to allocate memory since otherwise it'll clobber the
885         variable buffer, and we may still need that if we're looking at a
886         target-specific variable.  */
887      value = alloc_value = allocated_variable_expand (p);
888      break;
889    case f_conditional:
890      /* A conditional variable definition "var ?= value".
891         The value is set IFF the variable is not defined yet. */
892      v = lookup_variable(expanded_name, strlen(expanded_name));
893      if (v)
894        {
895          free(expanded_name);
896          return v;
897        }
898      flavor = f_recursive;
899      /* FALLTHROUGH */
900    case f_recursive:
901      /* A recursive variable definition "var = value".
902         The value is used verbatim.  */
903      value = p;
904      break;
905    case f_append:
906      /* If we have += but we're in a target variable context, defer the
907         append until the context expansion.  */
908      if (target_var)
909        {
910          append = 1;
911          flavor = f_recursive;
912          value = p;
913          break;
914        }
915
916      /* An appending variable definition "var += value".
917         Extract the old value and append the new one.  */
918      v = lookup_variable (expanded_name, strlen (expanded_name));
919      if (v == 0)
920        {
921          /* There was no old value.
922             This becomes a normal recursive definition.  */
923          value = p;
924          flavor = f_recursive;
925        }
926      else
927        {
928          /* Paste the old and new values together in VALUE.  */
929
930          unsigned int oldlen, newlen;
931
932          if (v->recursive)
933            /* The previous definition of the variable was recursive.
934               The new value comes from the unexpanded old and new values.  */
935            flavor = f_recursive;
936          else
937            /* The previous definition of the variable was simple.
938               The new value comes from the old value, which was expanded
939               when it was set; and from the expanded new value.  Allocate
940               memory for the expansion as we may still need the rest of the
941               buffer if we're looking at a target-specific variable.  */
942            p = alloc_value = allocated_variable_expand (p);
943
944          oldlen = strlen (v->value);
945          newlen = strlen (p);
946          value = (char *) alloca (oldlen + 1 + newlen + 1);
947          bcopy (v->value, value, oldlen);
948          value[oldlen] = ' ';
949          bcopy (p, &value[oldlen + 1], newlen + 1);
950        }
951    }
952
953#ifdef __MSDOS__
954  /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
955     non-Unix systems don't conform to this default configuration (in
956     fact, most of them don't even have `/bin').  On the other hand,
957     $SHELL in the environment, if set, points to the real pathname of
958     the shell.
959     Therefore, we generally won't let lines like "SHELL=/bin/sh" from
960     the Makefile override $SHELL from the environment.  But first, we
961     look for the basename of the shell in the directory where SHELL=
962     points, and along the $PATH; if it is found in any of these places,
963     we define $SHELL to be the actual pathname of the shell.  Thus, if
964     you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
965     your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
966     defining SHELL to be "d:/unix/bash.exe".  */
967  if ((origin == o_file || origin == o_override)
968      && strcmp (expanded_name, "SHELL") == 0)
969    {
970      char shellpath[PATH_MAX];
971      extern char * __dosexec_find_on_path (const char *, char *[], char *);
972
973      /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc.  */
974      if (__dosexec_find_on_path (value, (char **)0, shellpath))
975        {
976          char *p;
977
978          for (p = shellpath; *p; p++)
979            {
980              if (*p == '\\')
981                *p = '/';
982            }
983          v = define_variable_loc (expanded_name, strlen (expanded_name),
984                                   shellpath, origin, flavor == f_recursive,
985                                   flocp);
986        }
987      else
988        {
989          char *shellbase, *bslash;
990          struct variable *pathv = lookup_variable ("PATH", 4);
991          char *path_string;
992          char *fake_env[2];
993          size_t pathlen = 0;
994
995          shellbase = strrchr (value, '/');
996          bslash = strrchr (value, '\\');
997          if (!shellbase || bslash > shellbase)
998            shellbase = bslash;
999          if (!shellbase && value[1] == ':')
1000            shellbase = value + 1;
1001          if (shellbase)
1002            shellbase++;
1003          else
1004            shellbase = value;
1005
1006          /* Search for the basename of the shell (with standard
1007             executable extensions) along the $PATH.  */
1008          if (pathv)
1009            pathlen = strlen (pathv->value);
1010          path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
1011          /* On MSDOS, current directory is considered as part of $PATH.  */
1012          sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1013          fake_env[0] = path_string;
1014          fake_env[1] = (char *)0;
1015          if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1016            {
1017              char *p;
1018
1019              for (p = shellpath; *p; p++)
1020                {
1021                  if (*p == '\\')
1022                    *p = '/';
1023                }
1024              v = define_variable_loc (expanded_name, strlen (expanded_name),
1025                                       shellpath, origin,
1026                                       flavor == f_recursive, flocp);
1027            }
1028          else
1029            v = lookup_variable (expanded_name, strlen (expanded_name));
1030
1031          free (path_string);
1032        }
1033    }
1034  else
1035#endif /* __MSDOS__ */
1036#ifdef WINDOWS32
1037  if ((origin == o_file || origin == o_override)
1038      && strcmp (expanded_name, "SHELL") == 0) {
1039    extern char* default_shell;
1040
1041    /*
1042     * Call shell locator function. If it returns TRUE, then
1043         * set no_default_sh_exe to indicate sh was found and
1044     * set new value for SHELL variable.
1045         */
1046    if (find_and_set_default_shell(value)) {
1047       v = define_variable_loc (expanded_name, strlen (expanded_name),
1048                                default_shell, origin, flavor == f_recursive,
1049                                flocp);
1050       no_default_sh_exe = 0;
1051    }
1052  } else
1053#endif
1054
1055  v = define_variable_loc (expanded_name, strlen (expanded_name), value,
1056                           origin, flavor == f_recursive, flocp);
1057
1058  v->append = append;
1059
1060  if (alloc_value)
1061    free (alloc_value);
1062  free (expanded_name);
1063
1064  return v;
1065}
1066
1067/* Print information for variable V, prefixing it with PREFIX.  */
1068
1069static void
1070print_variable (v, prefix)
1071     register struct variable *v;
1072     char *prefix;
1073{
1074  const char *origin;
1075
1076  switch (v->origin)
1077    {
1078    case o_default:
1079      origin = _("default");
1080      break;
1081    case o_env:
1082      origin = _("environment");
1083      break;
1084    case o_file:
1085      origin = _("makefile");
1086      break;
1087    case o_env_override:
1088      origin = _("environment under -e");
1089      break;
1090    case o_command:
1091      origin = _("command line");
1092      break;
1093    case o_override:
1094      origin = _("`override' directive");
1095      break;
1096    case o_automatic:
1097      origin = _("automatic");
1098      break;
1099    case o_invalid:
1100    default:
1101      abort ();
1102    }
1103  fputs ("# ", stdout);
1104  fputs (origin, stdout);
1105  if (v->fileinfo.filenm)
1106    printf (" (from `%s', line %lu)", v->fileinfo.filenm, v->fileinfo.lineno);
1107  putchar ('\n');
1108  fputs (prefix, stdout);
1109
1110  /* Is this a `define'?  */
1111  if (v->recursive && strchr (v->value, '\n') != 0)
1112    printf ("define %s\n%s\nendef\n", v->name, v->value);
1113  else
1114    {
1115      register char *p;
1116
1117      printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1118
1119      /* Check if the value is just whitespace.  */
1120      p = next_token (v->value);
1121      if (p != v->value && *p == '\0')
1122        /* All whitespace.  */
1123        printf ("$(subst ,,%s)", v->value);
1124      else if (v->recursive)
1125        fputs (v->value, stdout);
1126      else
1127        /* Double up dollar signs.  */
1128        for (p = v->value; *p != '\0'; ++p)
1129          {
1130            if (*p == '$')
1131              putchar ('$');
1132            putchar (*p);
1133          }
1134      putchar ('\n');
1135    }
1136}
1137
1138
1139/* Print all the variables in SET.  PREFIX is printed before
1140   the actual variable definitions (everything else is comments).  */
1141
1142void
1143print_variable_set (set, prefix)
1144     register struct variable_set *set;
1145     char *prefix;
1146{
1147  register unsigned int i, nvariables, per_bucket;
1148  register struct variable *v;
1149
1150  per_bucket = nvariables = 0;
1151  for (i = 0; i < set->buckets; ++i)
1152    {
1153      register unsigned int this_bucket = 0;
1154
1155      for (v = set->table[i]; v != 0; v = v->next)
1156        {
1157          ++this_bucket;
1158          print_variable (v, prefix);
1159        }
1160
1161      nvariables += this_bucket;
1162      if (this_bucket > per_bucket)
1163        per_bucket = this_bucket;
1164    }
1165
1166  if (nvariables == 0)
1167    puts (_("# No variables."));
1168  else
1169    {
1170      printf (_("# %u variables in %u hash buckets.\n"),
1171              nvariables, set->buckets);
1172#ifndef NO_FLOAT
1173      printf (_("# average of %.1f variables per bucket, \
1174max %u in one bucket.\n"),
1175              (double) nvariables / (double) set->buckets,
1176              per_bucket);
1177#else
1178      {
1179        int f = (nvariables * 1000 + 5) / set->buckets;
1180        printf (_("# average of %d.%d variables per bucket, \
1181max %u in one bucket.\n"),
1182              f/10, f%10,
1183              per_bucket);
1184      }
1185#endif
1186    }
1187}
1188
1189
1190/* Print the data base of variables.  */
1191
1192void
1193print_variable_data_base ()
1194{
1195  puts (_("\n# Variables\n"));
1196
1197  print_variable_set (&global_variable_set, "");
1198}
1199
1200
1201/* Print all the local variables of FILE.  */
1202
1203void
1204print_file_variables (file)
1205     struct file *file;
1206{
1207  if (file->variables != 0)
1208    print_variable_set (file->variables->set, "# ");
1209}
1210
1211#ifdef WINDOWS32
1212void
1213sync_Path_environment(void)
1214{
1215    char* path = allocated_variable_expand("$(Path)");
1216    static char* environ_path = NULL;
1217
1218    if (!path)
1219        return;
1220
1221    /*
1222     * If done this before, don't leak memory unnecessarily.
1223     * Free the previous entry before allocating new one.
1224     */
1225    if (environ_path)
1226        free(environ_path);
1227
1228    /*
1229     * Create something WINDOWS32 world can grok
1230     */
1231    convert_Path_to_windows32(path, ';');
1232    environ_path = concat("Path", "=", path);
1233    putenv(environ_path);
1234    free(path);
1235}
1236#endif
Note: See TracBrowser for help on using the repository browser.