VirtualBox

Changeset 3138 in kBuild for vendor/gnumake/current/expand.c


Ignore:
Timestamp:
Mar 12, 2018 7: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/expand.c

    r2596 r3138  
    11/* Variable expansion functions 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>
     
    2927/* Initially, any errors reported when expanding strings will be reported
    3028   against the file where the error appears.  */
    31 const struct floc **expanding_var = &reading_file;
     29const floc **expanding_var = &reading_file;
    3230
    3331/* The next two describe the variable output buffer.
     
    6462      unsigned int offset = ptr - variable_buffer;
    6563      variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
    66                                 ? newlen + 100
    67                                 : 2 * variable_buffer_length);
     64                                ? newlen + 100
     65                                : 2 * variable_buffer_length);
    6866      variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
    6967      ptr = variable_buffer + offset;
     
    10098{
    10199  char *value;
    102   const struct floc *this_var;
    103   const struct floc **saved_varp;
     100  const floc *this_var;
     101  const floc **saved_varp;
    104102  struct variable_set_list *save = 0;
    105103  int set_reading = 0;
     
    125123      if (!v->exp_count)
    126124        /* Expanding V causes infinite recursion.  Lose.  */
    127         fatal (*expanding_var,
    128                _("Recursive variable `%s' references itself (eventually)"),
    129                v->name);
     125        OS (fatal, *expanding_var,
     126            _("Recursive variable '%s' references itself (eventually)"),
     127            v->name);
    130128      --v->exp_count;
    131129    }
     
    190188   a null byte is found.
    191189
    192    Write the results to LINE, which must point into `variable_buffer'.  If
     190   Write the results to LINE, which must point into 'variable_buffer'.  If
    193191   LINE is NULL, start at the beginning of the buffer.
    194192   Return a pointer to LINE, or to the beginning of the buffer if LINE is
     
    200198  struct variable *v;
    201199  const char *p, *p1;
    202   char *abuf = NULL;
     200  char *save;
    203201  char *o;
    204202  unsigned int line_offset;
    205203
    206204  if (!line)
    207     line = initialize_variable_output();
     205    line = initialize_variable_output ();
    208206  o = line;
    209207  line_offset = line - variable_buffer;
     
    215213    }
    216214
    217   /* If we want a subset of the string, allocate a temporary buffer for it.
    218      Most of the functions we use here don't work with length limits.  */
    219   if (length > 0 && string[length] != '\0')
    220     {
    221       abuf = xmalloc(length+1);
    222       memcpy(abuf, string, length);
    223       abuf[length] = '\0';
    224       string = abuf;
    225     }
    226   p = string;
     215  /* We need a copy of STRING: due to eval, it's possible that it will get
     216     freed as we process it (it might be the value of a variable that's reset
     217     for example).  Also having a nil-terminated string is handy.  */
     218  save = length < 0 ? xstrdup (string) : xstrndup (string, length);
     219  p = save;
    227220
    228221  while (1)
     
    230223      /* Copy all following uninteresting chars all at once to the
    231224         variable output buffer, and skip them.  Uninteresting chars end
    232         at the next $ or the end of the input.  */
     225        at the next $ or the end of the input.  */
    233226
    234227      p1 = strchr (p, '$');
     
    237230
    238231      if (p1 == 0)
    239         break;
     232        break;
    240233      p = p1 + 1;
    241234
     
    243236
    244237      switch (*p)
    245         {
    246         case '$':
    247           /* $$ seen means output one $ to the variable output buffer.  */
    248           o = variable_buffer_output (o, p, 1);
    249           break;
    250 
    251         case '(':
    252         case '{':
    253           /* $(...) or ${...} is the general case of substitution.  */
    254           {
    255             char openparen = *p;
    256             char closeparen = (openparen == '(') ? ')' : '}';
     238        {
     239        case '$':
     240        case '\0':
     241          /* $$ or $ at the end of the string means output one $ to the
     242             variable output buffer.  */
     243          o = variable_buffer_output (o, p1, 1);
     244          break;
     245
     246        case '(':
     247        case '{':
     248          /* $(...) or ${...} is the general case of substitution.  */
     249          {
     250            char openparen = *p;
     251            char closeparen = (openparen == '(') ? ')' : '}';
    257252            const char *begp;
    258             const char *beg = p + 1;
    259             char *op;
     253            const char *beg = p + 1;
     254            char *op;
    260255            char *abeg = NULL;
    261             const char *end, *colon;
    262 
    263             op = o;
    264             begp = p;
    265             if (handle_function (&op, &begp))
    266               {
    267                 o = op;
    268                 p = begp;
    269                 break;
    270               }
    271 
    272             /* Is there a variable reference inside the parens or braces?
    273                If so, expand it before expanding the entire reference.  */
    274 
    275             end = strchr (beg, closeparen);
    276             if (end == 0)
     256            const char *end, *colon;
     257
     258            op = o;
     259            begp = p;
     260            if (handle_function (&op, &begp))
     261              {
     262                o = op;
     263                p = begp;
     264                break;
     265              }
     266
     267            /* Is there a variable reference inside the parens or braces?
     268               If so, expand it before expanding the entire reference.  */
     269
     270            end = strchr (beg, closeparen);
     271            if (end == 0)
    277272              /* Unterminated variable reference.  */
    278               fatal (*expanding_var, _("unterminated variable reference"));
    279             p1 = lindex (beg, end, '$');
    280             if (p1 != 0)
    281               {
    282                 /* BEG now points past the opening paren or brace.
    283                    Count parens or braces until it is matched.  */
    284                 int count = 0;
    285                 for (p = beg; *p != '\0'; ++p)
    286                   {
    287                     if (*p == openparen)
    288                       ++count;
    289                     else if (*p == closeparen && --count < 0)
    290                       break;
    291                   }
    292                 /* If COUNT is >= 0, there were unmatched opening parens
    293                    or braces, so we go to the simple case of a variable name
    294                    such as `$($(a)'.  */
    295                 if (count < 0)
    296                   {
    297                     abeg = expand_argument (beg, p); /* Expand the name.  */
    298                     beg = abeg;
    299                     end = strchr (beg, '\0');
    300                   }
    301               }
    302             else
    303               /* Advance P to the end of this reference.  After we are
     273              O (fatal, *expanding_var, _("unterminated variable reference"));
     274            p1 = lindex (beg, end, '$');
     275            if (p1 != 0)
     276              {
     277                /* BEG now points past the opening paren or brace.
     278                   Count parens or braces until it is matched.  */
     279                int count = 0;
     280                for (p = beg; *p != '\0'; ++p)
     281                  {
     282                    if (*p == openparen)
     283                      ++count;
     284                    else if (*p == closeparen && --count < 0)
     285                      break;
     286                  }
     287                /* If COUNT is >= 0, there were unmatched opening parens
     288                   or braces, so we go to the simple case of a variable name
     289                   such as '$($(a)'.  */
     290                if (count < 0)
     291                  {
     292                    abeg = expand_argument (beg, p); /* Expand the name.  */
     293                    beg = abeg;
     294                    end = strchr (beg, '\0');
     295                  }
     296              }
     297            else
     298              /* Advance P to the end of this reference.  After we are
    304299                 finished expanding this one, P will be incremented to
    305300                 continue the scan.  */
    306               p = end;
    307 
    308             /* This is not a reference to a built-in function and
    309                any variable references inside are now expanded.
    310                Is the resultant text a substitution reference?  */
    311 
    312             colon = lindex (beg, end, ':');
    313             if (colon)
    314               {
    315                 /* This looks like a substitution reference: $(FOO:A=B).  */
    316                 const char *subst_beg, *subst_end, *replace_beg, *replace_end;
    317 
    318                 subst_beg = colon + 1;
    319                 subst_end = lindex (subst_beg, end, '=');
    320                 if (subst_end == 0)
    321                   /* There is no = in sight.  Punt on the substitution
    322                      reference and treat this as a variable name containing
    323                      a colon, in the code below.  */
    324                   colon = 0;
    325                 else
    326                   {
    327                     replace_beg = subst_end + 1;
    328                     replace_end = end;
    329 
    330                     /* Extract the variable name before the colon
    331                        and look up that variable.  */
    332                     v = lookup_variable (beg, colon - beg);
    333                     if (v == 0)
    334                       warn_undefined (beg, colon - beg);
     301              p = end;
     302
     303            /* This is not a reference to a built-in function and
     304               any variable references inside are now expanded.
     305               Is the resultant text a substitution reference?  */
     306
     307            colon = lindex (beg, end, ':');
     308            if (colon)
     309              {
     310                /* This looks like a substitution reference: $(FOO:A=B).  */
     311                const char *subst_beg = colon + 1;
     312                const char *subst_end = lindex (subst_beg, end, '=');
     313                if (subst_end == 0)
     314                  /* There is no = in sight.  Punt on the substitution
     315                     reference and treat this as a variable name containing
     316                     a colon, in the code below.  */
     317                  colon = 0;
     318                else
     319                  {
     320                    const char *replace_beg = subst_end + 1;
     321                    const char *replace_end = end;
     322
     323                    /* Extract the variable name before the colon
     324                       and look up that variable.  */
     325                    v = lookup_variable (beg, colon - beg);
     326                    if (v == 0)
     327                      warn_undefined (beg, colon - beg);
    335328
    336329                    /* If the variable is not empty, perform the
    337330                       substitution.  */
    338                     if (v != 0 && *v->value != '\0')
    339                       {
    340                         char *pattern, *replace, *ppercent, *rpercent;
    341                         char *value = (v->recursive
     331                    if (v != 0 && *v->value != '\0')
     332                      {
     333                        char *pattern, *replace, *ppercent, *rpercent;
     334                        char *value = (v->recursive
    342335                                       ? recursively_expand (v)
    343                                        : v->value);
     336                                       : v->value);
    344337
    345338                        /* Copy the pattern and the replacement.  Add in an
     
    359352                        /* Look for %.  Set the percent pointers properly
    360353                           based on whether we find one or not.  */
    361                         ppercent = find_percent (pattern);
    362                         if (ppercent)
     354                        ppercent = find_percent (pattern);
     355                        if (ppercent)
    363356                          {
    364357                            ++ppercent;
     
    367360                              ++rpercent;
    368361                          }
    369                         else
     362                        else
    370363                          {
    371364                            ppercent = pattern;
     
    378371                                                 ppercent, rpercent);
    379372
    380                         if (v->recursive)
    381                           free (value);
    382                       }
    383                   }
    384               }
    385 
    386             if (colon == 0)
    387               /* This is an ordinary variable reference.
    388                  Look up the value of the variable.  */
    389                 o = reference_variable (o, beg, end - beg);
    390 
    391           if (abeg)
    392             free (abeg);
    393           }
    394           break;
    395 
    396         case '\0':
    397           break;
    398 
    399         default:
    400           if (isblank ((unsigned char)p[-1]))
    401             break;
    402 
    403           /* A $ followed by a random char is a variable reference:
    404              $a is equivalent to $(a).  */
     373                        if (v->recursive)
     374                          free (value);
     375                      }
     376                  }
     377              }
     378
     379            if (colon == 0)
     380              /* This is an ordinary variable reference.
     381                 Look up the value of the variable.  */
     382                o = reference_variable (o, beg, end - beg);
     383
     384            free (abeg);
     385          }
     386          break;
     387
     388        default:
     389          if (ISSPACE (p[-1]))
     390            break;
     391
     392          /* A $ followed by a random char is a variable reference:
     393             $a is equivalent to $(a).  */
    405394          o = reference_variable (o, p, 1);
    406395
    407           break;
    408         }
     396          break;
     397        }
    409398
    410399      if (*p == '\0')
    411         break;
     400        break;
    412401
    413402      ++p;
    414403    }
    415404
    416   if (abuf)
    417     free (abuf);
     405  free (save);
    418406
    419407  variable_buffer_output (o, "", 1);
     
    423411
    424412/* Scan LINE for variable references and expansion-function calls.
    425    Build in `variable_buffer' the result of expanding the references and calls.
     413   Build in 'variable_buffer' the result of expanding the references and calls.
    426414   Return the address of the resulting string, which is null-terminated
    427415   and is valid only until the next time this function is called.  */
     
    430418variable_expand (const char *line)
    431419{
    432   return variable_expand_string(NULL, line, (long)-1);
     420  return variable_expand_string (NULL, line, (long)-1);
    433421}
    434422
     
    437425   The text starting at STR and ending at END is variable-expanded
    438426   into a null-terminated string that is returned as the value.
    439    This is done without clobbering `variable_buffer' or the current
     427   This is done without clobbering 'variable_buffer' or the current
    440428   variable-expansion that is in progress.  */
    441429
     
    447435
    448436  if (str == end)
    449     return xstrdup("");
     437    return xstrdup ("");
    450438
    451439  if (!end || *end == '\0')
     
    462450  r = allocated_variable_expand (tmp);
    463451
    464   if (alloc)
    465     free (alloc);
     452  free (alloc);
    466453
    467454  return r;
     
    477464  char *result;
    478465  struct variable_set_list *savev;
    479   const struct floc *savef;
     466  const floc *savef;
    480467
    481468  if (file == 0)
     
    506493static char *
    507494variable_append (const char *name, unsigned int length,
    508                  const struct variable_set_list *set)
     495                 const struct variable_set_list *set, int local)
    509496{
    510497  const struct variable *v;
    511498  char *buf = 0;
     499  /* If this set is local and the next is not a parent, then next is local.  */
     500  int nextlocal = local && set->next_is_parent == 0;
    512501
    513502  /* If there's nothing left to check, return the empty buffer.  */
     
    518507  v = lookup_variable_in_set (name, length, set->set);
    519508
    520   /* If there isn't one, look to see if there's one in a set above us.  */
    521   if (!v)
    522     return variable_append (name, length, set->next);
     509  /* If there isn't one, or this one is private, try the set above us.  */
     510  if (!v || (!local && v->private_var))
     511    return variable_append (name, length, set->next, nextlocal);
    523512
    524513  /* If this variable type is append, first get any upper values.
    525514     If not, initialize the buffer.  */
    526515  if (v->append)
    527     buf = variable_append (name, length, set->next);
     516    buf = variable_append (name, length, set->next, nextlocal);
    528517  else
    529518    buf = initialize_variable_output ();
     
    555544  variable_buffer = 0;
    556545
    557   val = variable_append (v->name, strlen (v->name), current_variable_set_list);
     546  val = variable_append (v->name, strlen (v->name),
     547                         current_variable_set_list, 1);
    558548  variable_buffer_output (val, "", 1);
    559549  val = variable_buffer;
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette