Ignore:
Timestamp:
Mar 12, 2018, 8:32:29 PM (7 years ago)
Author:
bird
Message:

Imported make 4.2.1 (2e55f5e4abdc0e38c1d64be703b446695e70b3b6) from https://git.savannah.gnu.org/git/make.git.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vendor/gnumake/current/variable.c

    r2596 r3138  
    11/* Internals of variables for GNU Make.
    2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
    3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
    4 2010 Free Software Foundation, Inc.
     2Copyright (C) 1988-2016 Free Software Foundation, Inc.
    53This file is part of GNU Make.
    64
     
    1715this program.  If not, see <http://www.gnu.org/licenses/>.  */
    1816
    19 #include "make.h"
     17#include "makeint.h"
    2018
    2119#include <assert.h>
    2220
     21#include "filedef.h"
    2322#include "dep.h"
    24 #include "filedef.h"
    2523#include "job.h"
    2624#include "commands.h"
     
    3129#endif
    3230#include "hash.h"
     31
     32/* Incremented every time we add or remove a global variable.  */
     33static unsigned long variable_changenum;
    3334
    3435/* Chain of all pattern-specific variables.  */
     
    101102{
    102103  struct pattern_var *p;
    103   unsigned int targlen = strlen(target);
     104  unsigned int targlen = strlen (target);
    104105
    105106  for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
     
    162163}
    163164
    164 #ifndef VARIABLE_BUCKETS
    165 #define VARIABLE_BUCKETS                523
     165#ifndef VARIABLE_BUCKETS
     166#define VARIABLE_BUCKETS                523
    166167#endif
    167 #ifndef PERFILE_VARIABLE_BUCKETS
    168 #define PERFILE_VARIABLE_BUCKETS        23
     168#ifndef PERFILE_VARIABLE_BUCKETS
     169#define PERFILE_VARIABLE_BUCKETS        23
    169170#endif
    170 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
    171 #define SMALL_SCOPE_VARIABLE_BUCKETS    13
     171#ifndef SMALL_SCOPE_VARIABLE_BUCKETS
     172#define SMALL_SCOPE_VARIABLE_BUCKETS    13
    172173#endif
    173174
     
    184185{
    185186  hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
    186              variable_hash_1, variable_hash_2, variable_hash_cmp);
     187             variable_hash_1, variable_hash_2, variable_hash_cmp);
    187188}
    188189
     
    198199                        const char *value, enum variable_origin origin,
    199200                        int recursive, struct variable_set *set,
    200                         const struct floc *flocp)
     201                        const floc *flocp)
    201202{
    202203  struct variable *v;
     
    210211  var_key.length = length;
    211212  var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
     213  v = *var_slot;
     214
     215#ifdef VMS
     216  /* VMS does not populate envp[] with DCL symbols and logical names which
     217     historically are mapped to environent variables.
     218     If the variable is not yet defined, then we need to check if getenv()
     219     can find it.  Do not do this for origin == o_env to avoid infinte
     220     recursion */
     221  if (HASH_VACANT (v) && (origin != o_env))
     222    {
     223      struct variable * vms_variable;
     224      char * vname = alloca (length + 1);
     225      char * vvalue;
     226
     227      strncpy (vname, name, length);
     228      vvalue = getenv(vname);
     229
     230      /* Values starting with '$' are probably foreign commands.
     231         We want to treat them as Shell aliases and not look them up here */
     232      if ((vvalue != NULL) && (vvalue[0] != '$'))
     233        {
     234          vms_variable =  lookup_variable(name, length);
     235          /* Refresh the slot */
     236          var_slot = (struct variable **) hash_find_slot (&set->table,
     237                                                          &var_key);
     238          v = *var_slot;
     239        }
     240    }
     241#endif
    212242
    213243  if (env_overrides && origin == o_env)
    214244    origin = o_env_override;
    215245
    216   v = *var_slot;
    217246  if (! HASH_VACANT (v))
    218247    {
    219248      if (env_overrides && v->origin == o_env)
    220         /* V came from in the environment.  Since it was defined
    221            before the switches were parsed, it wasn't affected by -e.  */
    222         v->origin = o_env_override;
     249        /* V came from in the environment.  Since it was defined
     250           before the switches were parsed, it wasn't affected by -e.  */
     251        v->origin = o_env_override;
    223252
    224253      /* A variable of this name is already defined.
    225         If the old definition is from a stronger source
    226         than this one, don't redefine it.  */
     254        If the old definition is from a stronger source
     255        than this one, don't redefine it.  */
    227256      if ((int) origin >= (int) v->origin)
    228         {
    229           if (v->value != 0)
    230             free (v->value);
    231           v->value = xstrdup (value);
     257        {
     258          free (v->value);
     259          v->value = xstrdup (value);
    232260          if (flocp != 0)
    233261            v->fileinfo = *flocp;
    234262          else
    235263            v->fileinfo.filenm = 0;
    236           v->origin = origin;
    237           v->recursive = recursive;
    238         }
     264          v->origin = origin;
     265          v->recursive = recursive;
     266        }
    239267      return v;
    240268    }
     
    246274  v->length = length;
    247275  hash_insert_at (&set->table, v, var_slot);
     276  if (set == &global_variable_set)
     277    ++variable_changenum;
     278
    248279  v->value = xstrdup (value);
    249280  if (flocp != 0)
     
    286317
    287318static void
    288 free_variable_name_and_value (const void *item);
     319free_variable_name_and_value (const void *item)
     320{
     321  struct variable *v = (struct variable *) item;
     322  free (v->name);
     323  free (v->value);
     324}
     325
     326void
     327free_variable_set (struct variable_set_list *list)
     328{
     329  hash_map (&list->set->table, free_variable_name_and_value);
     330  hash_free (&list->set->table, 1);
     331  free (list->set);
     332  free (list);
     333}
    289334
    290335void
     
    311356    {
    312357      if (env_overrides && v->origin == o_env)
    313         /* V came from in the environment.  Since it was defined
    314            before the switches were parsed, it wasn't affected by -e.  */
    315         v->origin = o_env_override;
    316 
    317       /* If the definition is from a stronger source than this one, don't
    318          undefine it.  */
     358        /* V came from in the environment.  Since it was defined
     359           before the switches were parsed, it wasn't affected by -e.  */
     360        v->origin = o_env_override;
     361
     362      /* Undefine only if this undefinition is from an equal or stronger
     363         source than the variable definition.  */
    319364      if ((int) origin >= (int) v->origin)
    320         {
     365        {
    321366          hash_delete_at (&set->table, var_slot);
    322367          free_variable_name_and_value (v);
    323         }
     368          free (v);
     369          if (set == &global_variable_set)
     370            ++variable_changenum;
     371        }
    324372    }
    325373}
     
    338386lookup_special_var (struct variable *var)
    339387{
    340   static unsigned long last_var_count = 0;
     388  static unsigned long last_changenum = 0;
    341389
    342390
     
    366414  */
    367415
    368   if (streq (var->name, ".VARIABLES")
    369       && global_variable_set.table.ht_fill != last_var_count)
     416  if (variable_changenum != last_changenum && streq (var->name, ".VARIABLES"))
    370417    {
    371418      unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
     
    403450      *(p-1) = '\0';
    404451
    405       /* Remember how many variables are in our current count.  Since we never
    406          remove variables from the list, this is a reliable way to know whether
    407          the list is up to date or needs to be recomputed.  */
    408 
    409       last_var_count = global_variable_set.table.ht_fill;
     452      /* Remember the current variable change number.  */
     453      last_changenum = variable_changenum;
    410454    }
    411455
     
    417461/* Lookup a variable whose name is a string starting at NAME
    418462   and with LENGTH chars.  NAME need not be null-terminated.
    419    Returns address of the `struct variable' containing all info
     463   Returns address of the 'struct variable' containing all info
    420464   on the variable, or nil if no such variable is defined.  */
    421465
     
    438482      v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
    439483      if (v && (!is_parent || !v->private_var))
    440         return v->special ? lookup_special_var (v) : v;
     484        return v->special ? lookup_special_var (v) : v;
    441485
    442486      is_parent |= setlist->next_is_parent;
     
    444488
    445489#ifdef VMS
    446   /* since we don't read envp[] on startup, try to get the
    447      variable via getenv() here. */
     490  /* VMS does not populate envp[] with DCL symbols and logical names which
     491     historically are mapped to enviroment varables and returned by getenv() */
    448492  {
    449493    char *vname = alloca (length + 1);
     
    505549/* Lookup a variable whose name is a string starting at NAME
    506550   and with LENGTH chars in set SET.  NAME need not be null-terminated.
    507    Returns address of the `struct variable' containing all info
     551   Returns address of the 'struct variable' containing all info
    508552   on the variable, or nil if no such variable is defined.  */
    509553
     
    538582    {
    539583      l = (struct variable_set_list *)
    540         xmalloc (sizeof (struct variable_set_list));
     584        xmalloc (sizeof (struct variable_set_list));
    541585      l->set = xmalloc (sizeof (struct variable_set));
    542586      hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
     
    642686  set = xmalloc (sizeof (struct variable_set));
    643687  hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
    644              variable_hash_1, variable_hash_2, variable_hash_cmp);
     688             variable_hash_1, variable_hash_2, variable_hash_cmp);
    645689
    646690  setlist = (struct variable_set_list *)
     
    653697}
    654698
    655 static void
    656 free_variable_name_and_value (const void *item)
    657 {
    658   struct variable *v = (struct variable *) item;
    659   free (v->name);
    660   free (v->value);
    661 }
    662 
    663 void
    664 free_variable_set (struct variable_set_list *list)
    665 {
    666   hash_map (&list->set->table, free_variable_name_and_value);
    667   hash_free (&list->set->table, 1);
    668   free (list->set);
    669   free (list);
    670 }
    671 
    672699/* Create a new variable set and push it on the current setlist.
    673700   If we're pushing a global scope (that is, the current scope is the global
     
    679706push_new_variable_scope (void)
    680707{
    681   current_variable_set_list = create_new_variable_set();
     708  current_variable_set_list = create_new_variable_set ();
    682709  if (current_variable_set_list->next == &global_setlist)
    683710    {
     
    702729
    703730  /* Can't call this if there's no scope to pop!  */
    704   assert(current_variable_set_list->next != NULL);
     731  assert (current_variable_set_list->next != NULL);
    705732
    706733  if (current_variable_set_list != &global_setlist)
     
    740767  struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
    741768
     769  int inc = to_set == &global_variable_set ? 1 : 0;
     770
    742771  for ( ; from_var_slot < from_var_end; from_var_slot++)
    743772    if (! HASH_VACANT (*from_var_slot))
    744773      {
    745         struct variable *from_var = *from_var_slot;
    746         struct variable **to_var_slot
    747           = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
    748         if (HASH_VACANT (*to_var_slot))
    749           hash_insert_at (&to_set->table, from_var, to_var_slot);
    750         else
    751           {
    752             /* GKM FIXME: delete in from_set->table */
    753             free (from_var->value);
    754             free (from_var);
    755           }
     774        struct variable *from_var = *from_var_slot;
     775        struct variable **to_var_slot
     776          = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
     777        if (HASH_VACANT (*to_var_slot))
     778          {
     779            hash_insert_at (&to_set->table, from_var, to_var_slot);
     780            variable_changenum += inc;
     781          }
     782        else
     783          {
     784            /* GKM FIXME: delete in from_set->table */
     785            free (from_var->value);
     786            free (from_var);
     787          }
    756788      }
    757789}
     
    787819    {
    788820      if (last0 == 0)
    789         *setlist0 = setlist1;
     821        *setlist0 = setlist1;
    790822      else
    791         last0->next = setlist1;
     823        last0->next = setlist1;
    792824    }
    793825}
     
    800832define_automatic_variables (void)
    801833{
    802 #if defined(WINDOWS32) || defined(__EMX__)
    803   extern char* default_shell;
    804 #else
    805   extern char default_shell[];
    806 #endif
    807   register struct variable *v;
     834  struct variable *v;
    808835  char buf[200];
    809836
     
    812839
    813840  sprintf (buf, "%s%s%s",
    814            version_string,
    815            (remote_description == 0 || remote_description[0] == '\0')
    816            ? "" : "-",
    817            (remote_description == 0 || remote_description[0] == '\0')
    818            ? "" : remote_description);
     841           version_string,
     842           (remote_description == 0 || remote_description[0] == '\0')
     843           ? "" : "-",
     844           (remote_description == 0 || remote_description[0] == '\0')
     845           ? "" : remote_description);
    819846  define_variable_cname ("MAKE_VERSION", buf, o_default, 0);
     847  define_variable_cname ("MAKE_HOST", make_host, o_default, 0);
    820848
    821849#ifdef  __MSDOS__
     
    831859    if (mshp)
    832860      (void) define_variable (shell_str, shlen,
    833                               mshp->value, o_env_override, 0);
     861                              mshp->value, o_env_override, 0);
    834862    else if (comp)
    835863      {
    836         /* $(COMSPEC) shouldn't override $(SHELL).  */
    837         struct variable *shp = lookup_variable (shell_str, shlen);
    838 
    839         if (!shp)
    840           (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
     864        /* $(COMSPEC) shouldn't override $(SHELL).  */
     865        struct variable *shp = lookup_variable (shell_str, shlen);
     866
     867        if (!shp)
     868          (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
    841869      }
    842870  }
     
    856884    if (!replace || !*replace->value)
    857885      if (shell && *shell->value && (shell->origin == o_env
    858           || shell->origin == o_env_override))
    859         {
    860           /* overwrite whatever we got from the environment */
    861           free(shell->value);
    862           shell->value = xstrdup (default_shell);
    863           shell->origin = o_default;
    864         }
     886          || shell->origin == o_env_override))
     887        {
     888          /* overwrite whatever we got from the environment */
     889          free (shell->value);
     890          shell->value = xstrdup (default_shell);
     891          shell->origin = o_default;
     892        }
    865893
    866894    /* Some people do not like cmd to be used as the default
     
    882910      /* overwrite $SHELL */
    883911      (void) define_variable (shell_str, shlen, replace->value,
    884                               replace->origin, 0);
     912                              replace->origin, 0);
    885913    else
    886914      /* provide a definition if there is none */
    887915      (void) define_variable (shell_str, shlen, default_shell,
    888                               o_default, 0);
     916                              o_default, 0);
    889917  }
    890918
     
    919947     the automatic variables they are variations of.  */
    920948
    921 #ifdef VMS
    922   define_variable_cname ("@D", "$(dir $@)", o_automatic, 1);
    923   define_variable_cname ("%D", "$(dir $%)", o_automatic, 1);
    924   define_variable_cname ("*D", "$(dir $*)", o_automatic, 1);
    925   define_variable_cname ("<D", "$(dir $<)", o_automatic, 1);
    926   define_variable_cname ("?D", "$(dir $?)", o_automatic, 1);
    927   define_variable_cname ("^D", "$(dir $^)", o_automatic, 1);
    928   define_variable_cname ("+D", "$(dir $+)", o_automatic, 1);
    929 #else
     949#if defined(__MSDOS__) || defined(WINDOWS32)
     950  /* For consistency, remove the trailing backslash as well as slash.  */
     951  define_variable_cname ("@D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $@)))",
     952                         o_automatic, 1);
     953  define_variable_cname ("%D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $%)))",
     954                         o_automatic, 1);
     955  define_variable_cname ("*D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $*)))",
     956                         o_automatic, 1);
     957  define_variable_cname ("<D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $<)))",
     958                         o_automatic, 1);
     959  define_variable_cname ("?D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $?)))",
     960                         o_automatic, 1);
     961  define_variable_cname ("^D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $^)))",
     962                         o_automatic, 1);
     963  define_variable_cname ("+D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $+)))",
     964                         o_automatic, 1);
     965#else  /* not __MSDOS__, not WINDOWS32 */
    930966  define_variable_cname ("@D", "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
    931967  define_variable_cname ("%D", "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
     
    949985
    950986/* Create a new environment for FILE's commands.
    951    If FILE is nil, this is for the `shell' function.
     987   If FILE is nil, this is for the 'shell' function.
    952988   The child's MAKELEVEL variable is incremented.  */
    953989
     
    9701006
    9711007  hash_init (&table, VARIABLE_BUCKETS,
    972              variable_hash_1, variable_hash_2, variable_hash_cmp);
     1008             variable_hash_1, variable_hash_2, variable_hash_cmp);
    9731009
    9741010  /* Run through all the variable sets in the list,
     
    9801016      v_end = v_slot + set->table.ht_size;
    9811017      for ( ; v_slot < v_end; v_slot++)
    982         if (! HASH_VACANT (*v_slot))
    983           {
    984             struct variable **new_slot;
    985             struct variable *v = *v_slot;
    986 
    987             /* If this is a per-target variable and it hasn't been touched
    988                already then look up the global version and take its export
    989                value.  */
    990             if (v->per_target && v->export == v_default)
    991               {
    992                 struct variable *gv;
    993 
    994                 gv = lookup_variable_in_set (v->name, strlen(v->name),
     1018        if (! HASH_VACANT (*v_slot))
     1019          {
     1020            struct variable **new_slot;
     1021            struct variable *v = *v_slot;
     1022
     1023            /* If this is a per-target variable and it hasn't been touched
     1024               already then look up the global version and take its export
     1025               value.  */
     1026            if (v->per_target && v->export == v_default)
     1027              {
     1028                struct variable *gv;
     1029
     1030                gv = lookup_variable_in_set (v->name, strlen (v->name),
    9951031                                             &global_variable_set);
    996                 if (gv)
    997                   v->export = gv->export;
    998               }
    999 
    1000             switch (v->export)
    1001               {
    1002               case v_default:
    1003                 if (v->origin == o_default || v->origin == o_automatic)
    1004                   /* Only export default variables by explicit request.  */
    1005                   continue;
     1032                if (gv)
     1033                  v->export = gv->export;
     1034              }
     1035
     1036            switch (v->export)
     1037              {
     1038              case v_default:
     1039                if (v->origin == o_default || v->origin == o_automatic)
     1040                  /* Only export default variables by explicit request.  */
     1041                  continue;
    10061042
    10071043                /* The variable doesn't have a name that can be exported.  */
     
    10091045                  continue;
    10101046
    1011                 if (! export_all_variables
    1012                     && v->origin != o_command
    1013                     && v->origin != o_env && v->origin != o_env_override)
    1014                   continue;
    1015                 break;
    1016 
    1017               case v_export:
    1018                 break;
    1019 
    1020               case v_noexport:
    1021                 {
    1022                   /* If this is the SHELL variable and it's not exported,
    1023                      then add the value from our original environment, if
    1024                      the original environment defined a value for SHELL.  */
    1025                   extern struct variable shell_var;
    1026                   if (streq (v->name, "SHELL") && shell_var.value)
    1027                     {
    1028                       v = &shell_var;
    1029                       break;
    1030                     }
    1031                   continue;
    1032                 }
    1033 
    1034               case v_ifset:
    1035                 if (v->origin == o_default)
    1036                   continue;
    1037                 break;
    1038               }
    1039 
    1040             new_slot = (struct variable **) hash_find_slot (&table, v);
    1041             if (HASH_VACANT (*new_slot))
    1042               hash_insert_at (&table, v, new_slot);
    1043           }
    1044     }
    1045 
    1046   makelevel_key.name = MAKELEVEL_NAME;
     1047                if (! export_all_variables
     1048                    && v->origin != o_command
     1049                    && v->origin != o_env && v->origin != o_env_override)
     1050                  continue;
     1051                break;
     1052
     1053              case v_export:
     1054                break;
     1055
     1056              case v_noexport:
     1057                {
     1058                  /* If this is the SHELL variable and it's not exported,
     1059                     then add the value from our original environment, if
     1060                     the original environment defined a value for SHELL.  */
     1061                  if (streq (v->name, "SHELL") && shell_var.value)
     1062                    {
     1063                      v = &shell_var;
     1064                      break;
     1065                    }
     1066                  continue;
     1067                }
     1068
     1069              case v_ifset:
     1070                if (v->origin == o_default)
     1071                  continue;
     1072                break;
     1073              }
     1074
     1075            new_slot = (struct variable **) hash_find_slot (&table, v);
     1076            if (HASH_VACANT (*new_slot))
     1077              hash_insert_at (&table, v, new_slot);
     1078          }
     1079    }
     1080
     1081  makelevel_key.name = (char *)MAKELEVEL_NAME;
    10471082  makelevel_key.length = MAKELEVEL_LENGTH;
    10481083  hash_delete (&table, &makelevel_key);
     
    10551090    if (! HASH_VACANT (*v_slot))
    10561091      {
    1057         struct variable *v = *v_slot;
    1058 
    1059         /* If V is recursively expanded and didn't come from the environment,
    1060            expand its value.  If it came from the environment, it should
    1061            go back into the environment unchanged.  */
    1062         if (v->recursive
    1063             && v->origin != o_env && v->origin != o_env_override)
    1064           {
    1065             char *value = recursively_expand_for_file (v, file);
     1092        struct variable *v = *v_slot;
     1093
     1094        /* If V is recursively expanded and didn't come from the environment,
     1095           expand its value.  If it came from the environment, it should
     1096           go back into the environment unchanged.  */
     1097        if (v->recursive
     1098            && v->origin != o_env && v->origin != o_env_override)
     1099          {
     1100            char *value = recursively_expand_for_file (v, file);
    10661101#ifdef WINDOWS32
    1067             if (strcmp(v->name, "Path") == 0 ||
    1068                 strcmp(v->name, "PATH") == 0)
    1069               convert_Path_to_windows32(value, ';');
     1102            if (strcmp (v->name, "Path") == 0 ||
     1103                strcmp (v->name, "PATH") == 0)
     1104              convert_Path_to_windows32 (value, ';');
    10701105#endif
    1071             *result++ = xstrdup (concat (3, v->name, "=", value));
    1072             free (value);
    1073           }
    1074         else
    1075           {
     1106            *result++ = xstrdup (concat (3, v->name, "=", value));
     1107            free (value);
     1108          }
     1109        else
     1110          {
    10761111#ifdef WINDOWS32
    1077             if (strcmp(v->name, "Path") == 0 ||
    1078                 strcmp(v->name, "PATH") == 0)
    1079               convert_Path_to_windows32(v->value, ';');
     1112            if (strcmp (v->name, "Path") == 0 ||
     1113                strcmp (v->name, "PATH") == 0)
     1114              convert_Path_to_windows32 (v->value, ';');
    10801115#endif
    1081             *result++ = xstrdup (concat (3, v->name, "=", v->value));
    1082           }
     1116            *result++ = xstrdup (concat (3, v->name, "=", v->value));
     1117          }
    10831118      }
    10841119
     
    11081143
    11091144
     1145/* Given a string, shell-execute it and return a malloc'ed string of the
     1146 * result. This removes only ONE newline (if any) at the end, for maximum
     1147 * compatibility with the *BSD makes.  If it fails, returns NULL. */
     1148
     1149static char *
     1150shell_result (const char *p)
     1151{
     1152  char *buf;
     1153  unsigned int len;
     1154  char *args[2];
     1155  char *result;
     1156
     1157  install_variable_buffer (&buf, &len);
     1158
     1159  args[0] = (char *) p;
     1160  args[1] = NULL;
     1161  variable_buffer_output (func_shell_base (variable_buffer, args, 0), "\0", 1);
     1162  result = strdup (variable_buffer);
     1163
     1164  restore_variable_buffer (buf, len);
     1165  return result;
     1166}
     1167
     1168
    11101169/* Given a variable, a value, and a flavor, define the variable.
    11111170   See the try_variable_definition() function for details on the parameters. */
    11121171
    11131172struct variable *
    1114 do_variable_definition (const struct floc *flocp, const char *varname,
     1173do_variable_definition (const floc *flocp, const char *varname,
    11151174                        const char *value, enum variable_origin origin,
    11161175                        enum variable_flavor flavor, int target_var)
     
    11331192      /* A simple variable definition "var := value".  Expand the value.
    11341193         We have to allocate memory since otherwise it'll clobber the
    1135         variable buffer, and we may still need that if we're looking at a
     1194        variable buffer, and we may still need that if we're looking at a
    11361195         target-specific variable.  */
    11371196      p = alloc_value = allocated_variable_expand (value);
    11381197      break;
     1198    case f_shell:
     1199      {
     1200        /* A shell definition "var != value".  Expand value, pass it to
     1201           the shell, and store the result in recursively-expanded var. */
     1202        char *q = allocated_variable_expand (value);
     1203        p = alloc_value = shell_result (q);
     1204        free (q);
     1205        flavor = f_recursive;
     1206        break;
     1207      }
    11391208    case f_conditional:
    11401209      /* A conditional variable definition "var ?= value".
     
    11491218    case f_recursive:
    11501219      /* A recursive variable definition "var = value".
    1151         The value is used verbatim.  */
     1220        The value is used verbatim.  */
    11521221      p = value;
    11531222      break;
     
    12051274            memcpy (&alloc_value[oldlen + 1], val, vallen + 1);
    12061275
    1207             if (tp)
    1208               free (tp);
     1276            free (tp);
    12091277          }
    12101278      }
     
    12141282  /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
    12151283     non-Unix systems don't conform to this default configuration (in
    1216      fact, most of them don't even have `/bin').  On the other hand,
     1284     fact, most of them don't even have '/bin').  On the other hand,
    12171285     $SHELL in the environment, if set, points to the real pathname of
    12181286     the shell.
     
    12331301      /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc.  */
    12341302      if (__dosexec_find_on_path (p, NULL, shellpath))
    1235         {
    1236           char *tp;
    1237 
    1238           for (tp = shellpath; *tp; tp++)
     1303        {
     1304          char *tp;
     1305
     1306          for (tp = shellpath; *tp; tp++)
    12391307            if (*tp == '\\')
    12401308              *tp = '/';
    12411309
    1242           v = define_variable_loc (varname, strlen (varname),
     1310          v = define_variable_loc (varname, strlen (varname),
    12431311                                   shellpath, origin, flavor == f_recursive,
    12441312                                   flocp);
    1245         }
     1313        }
    12461314      else
    1247         {
    1248           const char *shellbase, *bslash;
    1249           struct variable *pathv = lookup_variable ("PATH", 4);
    1250           char *path_string;
    1251           char *fake_env[2];
    1252           size_t pathlen = 0;
    1253 
    1254           shellbase = strrchr (p, '/');
    1255           bslash = strrchr (p, '\\');
    1256           if (!shellbase || bslash > shellbase)
    1257             shellbase = bslash;
    1258           if (!shellbase && p[1] == ':')
    1259             shellbase = p + 1;
    1260           if (shellbase)
    1261             shellbase++;
    1262           else
    1263             shellbase = p;
    1264 
    1265           /* Search for the basename of the shell (with standard
    1266              executable extensions) along the $PATH.  */
    1267           if (pathv)
    1268             pathlen = strlen (pathv->value);
    1269           path_string = xmalloc (5 + pathlen + 2 + 1);
    1270           /* On MSDOS, current directory is considered as part of $PATH.  */
    1271           sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
    1272           fake_env[0] = path_string;
    1273           fake_env[1] = 0;
    1274           if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
    1275             {
    1276               char *tp;
    1277 
    1278               for (tp = shellpath; *tp; tp++)
     1315        {
     1316          const char *shellbase, *bslash;
     1317          struct variable *pathv = lookup_variable ("PATH", 4);
     1318          char *path_string;
     1319          char *fake_env[2];
     1320          size_t pathlen = 0;
     1321
     1322          shellbase = strrchr (p, '/');
     1323          bslash = strrchr (p, '\\');
     1324          if (!shellbase || bslash > shellbase)
     1325            shellbase = bslash;
     1326          if (!shellbase && p[1] == ':')
     1327            shellbase = p + 1;
     1328          if (shellbase)
     1329            shellbase++;
     1330          else
     1331            shellbase = p;
     1332
     1333          /* Search for the basename of the shell (with standard
     1334             executable extensions) along the $PATH.  */
     1335          if (pathv)
     1336            pathlen = strlen (pathv->value);
     1337          path_string = xmalloc (5 + pathlen + 2 + 1);
     1338          /* On MSDOS, current directory is considered as part of $PATH.  */
     1339          sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
     1340          fake_env[0] = path_string;
     1341          fake_env[1] = 0;
     1342          if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
     1343            {
     1344              char *tp;
     1345
     1346              for (tp = shellpath; *tp; tp++)
    12791347                if (*tp == '\\')
    12801348                  *tp = '/';
    12811349
    1282               v = define_variable_loc (varname, strlen (varname),
     1350              v = define_variable_loc (varname, strlen (varname),
    12831351                                       shellpath, origin,
    12841352                                       flavor == f_recursive, flocp);
    1285             }
    1286           else
    1287             v = lookup_variable (varname, strlen (varname));
    1288 
    1289           free (path_string);
    1290         }
     1353            }
     1354          else
     1355            v = lookup_variable (varname, strlen (varname));
     1356
     1357          free (path_string);
     1358        }
    12911359    }
    12921360  else
     
    12961364      && streq (varname, "SHELL"))
    12971365    {
    1298       extern char *default_shell;
     1366      extern const char *default_shell;
    12991367
    13001368      /* Call shell locator function. If it returns TRUE, then
    1301         set no_default_sh_exe to indicate sh was found and
     1369        set no_default_sh_exe to indicate sh was found and
    13021370         set new value for SHELL variable.  */
    13031371
     
    13311399            v = lookup_variable (varname, strlen (varname));
    13321400
    1333           if (tp)
    1334             free (tp);
     1401          free (tp);
    13351402        }
    13361403    }
     
    13521419  v->conditional = conditional;
    13531420
    1354   if (alloc_value)
    1355     free (alloc_value);
     1421  free (alloc_value);
    13561422
    13571423  return v->special ? set_special_var (v) : v;
     
    13611427/* Parse P (a null-terminated string) as a variable definition.
    13621428
    1363    If it is not a variable definition, return NULL.
     1429   If it is not a variable definition, return NULL and the contents of *VAR
     1430   are undefined, except NAME is set to the first non-space character or NIL.
    13641431
    13651432   If it is a variable definition, return a pointer to the char after the
    1366    assignment token and set *FLAVOR to the type of variable assignment.  */
     1433   assignment token and set the following fields (only) of *VAR:
     1434    name   : name of the variable (ALWAYS SET) (NOT NUL-TERMINATED!)
     1435    length : length of the variable name
     1436    value  : value of the variable (nul-terminated)
     1437    flavor : flavor of the variable
     1438   Other values in *VAR are unchanged.
     1439  */
    13671440
    13681441char *
    1369 parse_variable_definition (const char *p, enum variable_flavor *flavor)
     1442parse_variable_definition (const char *p, struct variable *var)
    13701443{
    13711444  int wspace = 0;
    1372 
    1373   p = next_token (p);
     1445  const char *e = NULL;
     1446
     1447  NEXT_TOKEN (p);
     1448  var->name = (char *)p;
     1449  var->length = 0;
    13741450
    13751451  while (1)
     
    13781454
    13791455      /* If we find a comment or EOS, it's not a variable definition.  */
    1380       if (c == '\0' || c == '#')
    1381         return NULL;
     1456      if (STOP_SET (c, MAP_COMMENT|MAP_NUL))
     1457        return NULL;
    13821458
    13831459      if (c == '$')
    1384         {
    1385           /* This begins a variable expansion reference.  Make sure we don't
    1386              treat chars inside the reference as assignment tokens.  */
    1387           char closeparen;
    1388           int count;
    1389           c = *p++;
    1390           if (c == '(')
    1391             closeparen = ')';
    1392           else if (c == '{')
    1393             closeparen = '}';
    1394           else
     1460        {
     1461          /* This begins a variable expansion reference.  Make sure we don't
     1462             treat chars inside the reference as assignment tokens.  */
     1463          char closeparen;
     1464          unsigned int count;
     1465
     1466          c = *p++;
     1467          if (c == '(')
     1468            closeparen = ')';
     1469          else if (c == '{')
     1470            closeparen = '}';
     1471          else if (c == '\0')
     1472            return NULL;
     1473          else
    13951474            /* '$$' or '$X'.  Either way, nothing special to do here.  */
    1396             continue;
    1397 
    1398           /* P now points past the opening paren or brace.
    1399              Count parens or braces until it is matched.  */
    1400           count = 0;
    1401           for (; *p != '\0'; ++p)
    1402             {
    1403               if (*p == c)
    1404                 ++count;
    1405               else if (*p == closeparen && --count < 0)
    1406                 {
    1407                   ++p;
    1408                   break;
    1409                 }
    1410             }
     1475            continue;
     1476
     1477          /* P now points past the opening paren or brace.
     1478             Count parens or braces until it is matched.  */
     1479          for (count = 1; *p != '\0'; ++p)
     1480            {
     1481              if (*p == closeparen && --count == 0)
     1482                {
     1483                  ++p;
     1484                  break;
     1485                }
     1486              if (*p == c)
     1487                ++count;
     1488            }
    14111489          continue;
    1412         }
     1490        }
    14131491
    14141492      /* If we find whitespace skip it, and remember we found it.  */
    1415       if (isblank ((unsigned char)c))
     1493      if (ISBLANK (c))
    14161494        {
    14171495          wspace = 1;
    1418           p = next_token (p);
     1496          e = p - 1;
     1497          NEXT_TOKEN (p);
    14191498          c = *p;
    14201499          if (c == '\0')
     
    14251504
    14261505      if (c == '=')
    1427         {
    1428           *flavor = f_recursive;
    1429           return (char *)p;
    1430         }
    1431 
    1432       /* Match assignment variants (:=, +=, ?=)  */
     1506        {
     1507          var->flavor = f_recursive;
     1508          if (! e)
     1509            e = p - 1;
     1510          break;
     1511        }
     1512
     1513      /* Match assignment variants (:=, +=, ?=, !=)  */
    14331514      if (*p == '=')
    14341515        {
     
    14361517            {
    14371518              case ':':
    1438                 *flavor = f_simple;
     1519                var->flavor = f_simple;
    14391520                break;
    14401521              case '+':
    1441                 *flavor = f_append;
     1522                var->flavor = f_append;
    14421523                break;
    14431524              case '?':
    1444                 *flavor = f_conditional;
     1525                var->flavor = f_conditional;
     1526                break;
     1527              case '!':
     1528                var->flavor = f_shell;
    14451529                break;
    14461530              default:
     
    14521536                continue;
    14531537            }
    1454           return (char *)++p;
     1538          if (! e)
     1539            e = p - 1;
     1540          ++p;
     1541          break;
    14551542        }
    1456       else if (c == ':')
    1457         /* A colon other than := is a rule line, not a variable defn.  */
    1458         return NULL;
     1543
     1544      /* Check for POSIX ::= syntax  */
     1545      if (c == ':')
     1546        {
     1547          /* A colon other than :=/::= is not a variable defn.  */
     1548          if (*p != ':' || p[1] != '=')
     1549            return NULL;
     1550
     1551          /* POSIX allows ::= to be the same as GNU make's := */
     1552          var->flavor = f_simple;
     1553          if (! e)
     1554            e = p - 1;
     1555          p += 2;
     1556          break;
     1557        }
    14591558
    14601559      /* If we skipped whitespace, non-assignments means no var.  */
     
    14631562    }
    14641563
     1564  var->length = e - var->name;
     1565  var->value = next_token (p);
    14651566  return (char *)p;
    14661567}
     
    14691570/* Try to interpret LINE (a null-terminated string) as a variable definition.
    14701571
    1471    If LINE was recognized as a variable definition, a pointer to its `struct
     1572   If LINE was recognized as a variable definition, a pointer to its 'struct
    14721573   variable' is returned.  If LINE is not a variable definition, NULL is
    14731574   returned.  */
    14741575
    14751576struct variable *
    1476 assign_variable_definition (struct variable *v, char *line)
    1477 {
    1478   char *beg;
    1479   char *end;
    1480   enum variable_flavor flavor;
     1577assign_variable_definition (struct variable *v, const char *line)
     1578{
    14811579  char *name;
    14821580
    1483   beg = next_token (line);
    1484   line = parse_variable_definition (beg, &flavor);
    1485   if (!line)
     1581  if (!parse_variable_definition (line, v))
    14861582    return NULL;
    14871583
    1488   end = line - (flavor == f_recursive ? 1 : 2);
    1489   while (end > beg && isblank ((unsigned char)end[-1]))
    1490     --end;
    1491   line = next_token (line);
    1492   v->value = line;
    1493   v->flavor = flavor;
    1494 
    14951584  /* Expand the name, so "$(foo)bar = baz" works.  */
    1496   name = alloca (end - beg + 1);
    1497   memcpy (name, beg, end - beg);
    1498   name[end - beg] = '\0';
     1585  name = alloca (v->length + 1);
     1586  memcpy (name, v->name, v->length);
     1587  name[v->length] = '\0';
    14991588  v->name = allocated_variable_expand (name);
    15001589
    15011590  if (v->name[0] == '\0')
    1502     fatal (&v->fileinfo, _("empty variable name"));
     1591    O (fatal, &v->fileinfo, _("empty variable name"));
    15031592
    15041593  return v;
     
    15151604   See the comments for assign_variable_definition().
    15161605
    1517    If LINE was recognized as a variable definition, a pointer to its `struct
     1606   If LINE was recognized as a variable definition, a pointer to its 'struct
    15181607   variable' is returned.  If LINE is not a variable definition, NULL is
    15191608   returned.  */
    15201609
    15211610struct variable *
    1522 try_variable_definition (const struct floc *flocp, char *line,
     1611try_variable_definition (const floc *flocp, const char *line,
    15231612                         enum variable_origin origin, int target_var)
    15241613{
     
    15541643  switch (v->origin)
    15551644    {
     1645    case o_automatic:
     1646      origin = _("automatic");
     1647      break;
    15561648    case o_default:
    15571649      origin = _("default");
     
    15701662      break;
    15711663    case o_override:
    1572       origin = _("`override' directive");
    1573       break;
    1574     case o_automatic:
    1575       origin = _("automatic");
     1664      origin = _("'override' directive");
    15761665      break;
    15771666    case o_invalid:
     
    15841673    fputs (" private", stdout);
    15851674  if (v->fileinfo.filenm)
    1586     printf (_(" (from `%s', line %lu)"),
    1587             v->fileinfo.filenm, v->fileinfo.lineno);
     1675    printf (_(" (from '%s', line %lu)"),
     1676            v->fileinfo.filenm, v->fileinfo.lineno + v->fileinfo.offset);
    15881677  putchar ('\n');
    15891678  fputs (prefix, stdout);
    15901679
    1591   /* Is this a `define'?  */
     1680  /* Is this a 'define'?  */
    15921681  if (v->recursive && strchr (v->value, '\n') != 0)
    15931682    printf ("define %s\n%s\nendef\n", v->name, v->value);
     
    16011690      p = next_token (v->value);
    16021691      if (p != v->value && *p == '\0')
    1603         /* All whitespace.  */
    1604         printf ("$(subst ,,%s)", v->value);
     1692        /* All whitespace.  */
     1693        printf ("$(subst ,,%s)", v->value);
    16051694      else if (v->recursive)
    1606         fputs (v->value, stdout);
     1695        fputs (v->value, stdout);
    16071696      else
    1608         /* Double up dollar signs.  */
    1609         for (p = v->value; *p != '\0'; ++p)
    1610           {
    1611             if (*p == '$')
    1612               putchar ('$');
    1613             putchar (*p);
    1614           }
     1697        /* Double up dollar signs.  */
     1698        for (p = v->value; *p != '\0'; ++p)
     1699          {
     1700            if (*p == '$')
     1701              putchar ('$');
     1702            putchar (*p);
     1703          }
    16151704      putchar ('\n');
    16161705    }
     1706}
     1707
     1708
     1709static void
     1710print_auto_variable (const void *item, void *arg)
     1711{
     1712  const struct variable *v = item;
     1713
     1714  if (v->origin == o_automatic)
     1715    print_variable (item, arg);
     1716}
     1717
     1718
     1719static void
     1720print_noauto_variable (const void *item, void *arg)
     1721{
     1722  const struct variable *v = item;
     1723
     1724  if (v->origin != o_automatic)
     1725    print_variable (item, arg);
    16171726}
    16181727
     
    16211730   the actual variable definitions (everything else is comments).  */
    16221731
    1623 void
    1624 print_variable_set (struct variable_set *set, char *prefix)
    1625 {
    1626   hash_map_arg (&set->table, print_variable, prefix);
     1732static void
     1733print_variable_set (struct variable_set *set, const char *prefix, int pauto)
     1734{
     1735  hash_map_arg (&set->table, (pauto ? print_auto_variable : print_variable),
     1736                (void *)prefix);
    16271737
    16281738  fputs (_("# variable set hash-table stats:\n"), stdout);
     
    16391749  puts (_("\n# Variables\n"));
    16401750
    1641   print_variable_set (&global_variable_set, "");
     1751  print_variable_set (&global_variable_set, "", 0);
    16421752
    16431753  puts (_("\n# Pattern-specific Variable Values"));
     
    16451755  {
    16461756    struct pattern_var *p;
    1647     int rules = 0;
     1757    unsigned int rules = 0;
    16481758
    16491759    for (p = pattern_vars; p != 0; p = p->next)
     
    16511761        ++rules;
    16521762        printf ("\n%s :\n", p->target);
    1653         print_variable (&p->variable, "# ");
     1763        print_variable (&p->variable, (void *)"# ");
    16541764      }
    16551765
     
    16681778{
    16691779  if (file->variables != 0)
    1670     print_variable_set (file->variables->set, "# ");
     1780    print_variable_set (file->variables->set, "# ", 1);
     1781}
     1782
     1783void
     1784print_target_variables (const struct file *file)
     1785{
     1786  if (file->variables != 0)
     1787    {
     1788      int l = strlen (file->name);
     1789      char *t = alloca (l + 3);
     1790
     1791      strcpy (t, file->name);
     1792      t[l] = ':';
     1793      t[l+1] = ' ';
     1794      t[l+2] = '\0';
     1795
     1796      hash_map_arg (&file->variables->set->table, print_noauto_variable, t);
     1797    }
    16711798}
    16721799
     
    16811808    return;
    16821809
    1683   /*
    1684    * If done this before, don't leak memory unnecessarily.
    1685    * Free the previous entry before allocating new one.
    1686    */
    1687   if (environ_path)
    1688     free (environ_path);
    1689 
    1690   /*
    1691    * Create something WINDOWS32 world can grok
    1692    */
     1810  /* If done this before, free the previous entry before allocating new one.  */
     1811  free (environ_path);
     1812
     1813  /* Create something WINDOWS32 world can grok.  */
    16931814  convert_Path_to_windows32 (path, ';');
    16941815  environ_path = xstrdup (concat (3, "PATH", "=", path));
Note: See TracChangeset for help on using the changeset viewer.