Changeset 2591 for trunk/src/kmk/read.c


Ignore:
Timestamp:
Jun 17, 2012, 10:45:31 PM (13 years ago)
Author:
bird
Message:

kmk: Merged in changes from GNU make 3.82. Previous GNU make base version was gnumake-2008-10-28-CVS.

Location:
trunk/src/kmk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk

    • Property svn:ignore
      •  

        old new  
        1313stamp-*
        1414makebook*
         15
        1516.*gdbinit
         17.gdb_history
         18
        1619*.dep
        1720*.dvi
         
        3134*.pg
        3235*.pgs
         36
        3337README
        3438README.DOS
        3539README.W32
         40README.OS2
        3641aclocal.m4
        3742autom4te.cache
         
        5257config.h.W32
        5358config.h-vms
         59
        5460loadavg
        5561loadavg.c
        5662make
         63
        5764.deps
        5865.dep_segment
         66ID
         67TAGS
         68
        5969_*
        6070sun4
         
        7282sol2
        7383i486-linux
         84
        7485customs
         86
        7587install-sh
        7688mkinstalldirs
         89
         90.directive.asc
  • trunk/src/kmk/read.c

    r2548 r2591  
    11/* Reading and parsing of makefiles for GNU Make.
    22Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
    3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
    4 Foundation, Inc.
     31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
     42010 Free Software Foundation, Inc.
    55This file is part of GNU Make.
    66
     
    6060    FILE *fp;           /* File, or NULL if this is an internal buffer.  */
    6161    struct floc floc;   /* Info on the file in fp (if any).  */
     62  };
     63
     64/* Track the modifiers we can have on variable assignments */
     65
     66struct vmodifiers
     67  {
     68    unsigned int assign_v:1;
     69    unsigned int define_v:1;
     70    unsigned int undefine_v:1;
     71    unsigned int export_v:1;
     72    unsigned int override_v:1;
     73    unsigned int private_v:1;
    6274  };
    6375
     
    149161
    150162static int eval_makefile (const char *filename, int flags);
    151 static int eval (struct ebuffer *buffer, int flags);
     163static void eval (struct ebuffer *buffer, int flags);
    152164
    153165static long readline (struct ebuffer *ebuf);
    154 static void do_define (char *name, unsigned int namelen,
    155                        enum variable_origin origin, struct ebuffer *ebuf);
     166static void do_undefine (char *name, enum variable_origin origin,
     167                         struct ebuffer *ebuf);
     168static struct variable *do_define (char *name IF_WITH_VALUE_LENGTH_PARAM(char *eos),
     169                                   enum variable_origin origin, struct ebuffer *ebuf);
    156170#ifndef CONFIG_WITH_VALUE_LENGTH
    157171static int conditional_line (char *line, int len, const struct floc *flocp);
     
    159173static int conditional_line (char *line, char *eol, int len, const struct floc *flocp);
    160174#endif
    161 #ifndef CONFIG_WITH_INCLUDEDEP
    162175static void record_files (struct nameseq *filenames, const char *pattern,
    163                           const char *pattern_percent, struct dep *deps,
     176                          const char *pattern_percent, char *depstr,
    164177                          unsigned int cmds_started, char *commands,
    165178                          unsigned int commands_idx, int two_colon,
    166179                          const struct floc *flocp);
    167 #endif /* !KMK */
    168180static void record_target_var (struct nameseq *filenames, char *defn,
    169                                enum variable_origin origin, int enabled,
     181                               enum variable_origin origin,
     182                               struct vmodifiers *vmod,
    170183                               const struct floc *flocp);
    171184static enum make_word_type get_next_mword (char *buffer, char *delim,
     
    198211
    199212
     213/* Compare a word, both length and contents.
     214   P must point to the word to be tested, and WLEN must be the length.
     215*/
     216#define word1eq(s)      (wlen == sizeof(s)-1 && strneq (s, p, sizeof(s)-1))
     217
     218
     219
    200220/* Read in all the makefiles and return the chain of their names.  */
    201221
     
    208228     we will be reading. */
    209229
    210   define_variable ("MAKEFILE_LIST", sizeof ("MAKEFILE_LIST")-1, "", o_file, 0);
     230  define_variable_cname ("MAKEFILE_LIST", "", o_file, 0);
    211231
    212232  DB (DB_BASIC, (_("Reading makefiles...\n")));
     
    320340              struct dep *d = alloc_dep ();
    321341              d->file = enter_file (strcache_add (*p));
    322               d->file->dontcare = 1;
     342              d->dontcare = 1;
    323343              /* Tell update_goal_chain to bail out as soon as this file is
    324344                 made, and main not to die if we can't make this file.  */
     
    388408  char *expanded = 0;
    389409  int makefile_errno;
    390   int r;
    391410
    392411  filename = strcache_add (filename);
     
    431450      for (i = 0; include_directories[i] != 0; ++i)
    432451        {
    433           const char *included = concat (include_directories[i], "/", filename);
     452          const char *included = concat (3, include_directories[i],
     453                                         "/", filename);
    434454          ebuf.fp = fopen (included, "r");
    435455          if (ebuf.fp)
     
    455475  deps->changed = flags;
    456476  if (flags & RM_DONTCARE)
    457     deps->file->dontcare = 1;
     477    deps->dontcare = 1;
    458478
    459479  if (expanded)
     
    470490      return 0;
    471491    }
     492
     493  /* Set close-on-exec to avoid leaking the makefile to children, such as
     494     $(shell ...).  */
     495#ifdef HAVE_FILENO
     496  CLOSE_ON_EXEC (fileno (ebuf.fp));
     497#endif
    472498
    473499  /* Add this makefile to the list. */
     
    506532  reading_file = &ebuf.floc;
    507533
    508   r = eval (&ebuf, !(flags & RM_NO_DEFAULT_GOAL));
     534  eval (&ebuf, !(flags & RM_NO_DEFAULT_GOAL));
    509535
    510536  reading_file = curfile;
     
    519545  free (ebuf.bufstart);
    520546  alloca (0);
    521   return r;
     547
     548  return 1;
    522549}
    523550
    524 int
     551void
    525552#ifndef CONFIG_WITH_VALUE_LENGTH
    526553eval_buffer (char *buffer)
     
    533560  struct conditionals new;
    534561  const struct floc *curfile;
    535   int r;
    536562
    537563  /* Evaluate the buffer */
     
    547573  ebuf.fp = NULL;
    548574
    549   ebuf.floc = *reading_file;
     575  if (reading_file)
     576    ebuf.floc = *reading_file;
     577  else
     578    ebuf.floc.filenm = NULL;
    550579
    551580  curfile = reading_file;
     
    554583  saved = install_conditionals (&new);
    555584
    556   r = eval (&ebuf, 1);
     585  eval (&ebuf, 1);
    557586
    558587  restore_conditionals (saved);
     
    561590
    562591  alloca (0);
    563   return r;
    564592}
    565593
    566594
     595/* Check LINE to see if it's a variable assignment or undefine.
     596
     597   It might use one of the modifiers "export", "override", "private", or it
     598   might be one of the conditional tokens like "ifdef", "include", etc.
     599
     600   If it's not a variable assignment or undefine, VMOD.V_ASSIGN is 0.
     601   Returns LINE.
     602
     603   Returns a pointer to the first non-modifier character, and sets VMOD
     604   based on the modifiers found if any, plus V_ASSIGN is 1.
     605 */
     606static char *
     607parse_var_assignment (const char *line, struct vmodifiers *vmod)
     608{
     609  const char *p;
     610  memset (vmod, '\0', sizeof (*vmod));
     611
     612  /* Find the start of the next token.  If there isn't one we're done.  */
     613  line = next_token (line);
     614  if (*line == '\0')
     615    return (char *)line;
     616
     617  p = line;
     618  while (1)
     619    {
     620      int wlen;
     621      const char *p2;
     622      enum variable_flavor flavor;
     623
     624      p2 = parse_variable_definition (p, &flavor);
     625
     626      /* If this is a variable assignment, we're done.  */
     627      if (p2)
     628        break;
     629
     630      /* It's not a variable; see if it's a modifier.  */
     631      p2 = end_of_token (p);
     632      wlen = p2 - p;
     633
     634      if (word1eq ("export"))
     635        vmod->export_v = 1;
     636      else if (word1eq ("override"))
     637        vmod->override_v = 1;
     638      else if (word1eq ("private"))
     639        vmod->private_v = 1;
     640      else if (word1eq ("define"))
     641        {
     642          /* We can't have modifiers after 'define' */
     643          vmod->define_v = 1;
     644          p = next_token (p2);
     645          break;
     646        }
     647      else if (word1eq ("undefine"))
     648        {
     649          /* We can't have modifiers after 'undefine' */
     650          vmod->undefine_v = 1;
     651          p = next_token (p2);
     652          break;
     653        }
     654      else
     655        /* Not a variable or modifier: this is not a variable assignment.  */
     656        return (char *)line;
     657
     658      /* It was a modifier.  Try the next word.  */
     659      p = next_token (p2);
     660      if (*p == '\0')
     661        return (char *)line;
     662    }
     663
     664  /* Found a variable assignment or undefine.  */
     665  vmod->assign_v = 1;
     666  return (char *)p;
     667}
     668
     669
    567670
    568671/* Read file FILENAME as a makefile and add its contents to the data base.
     
    570673   SET_DEFAULT is true if we are allowed to set the default goal.  */
    571674
    572 
    573 static int
     675static void
    574676eval (struct ebuffer *ebuf, int set_default)
    575677{
     
    583685  int no_targets = 0;           /* Set when reading a rule without targets.  */
    584686  struct nameseq *filenames = 0;
    585   struct dep *deps = 0;
     687  char *depstr = 0;
    586688  long nlines = 0;
    587689  int two_colon = 0;
     
    604706        {                                                                     \
    605707          fi.lineno = tgts_started;                                           \
    606           record_files (filenames, pattern, pattern_percent, deps          \
     708          record_files (filenames, pattern, pattern_percent, depstr,          \
    607709                        cmds_started, commands, commands_idx, two_colon,      \
    608710                        &fi);                                                 \
     711          filenames = 0;                                                      \
    609712        }                                                                     \
    610       filenames = 0;                                                          \
    611713      commands_idx = 0;                                                       \
    612714      no_targets = 0;                                                         \
     
    642744      char *p;
    643745      char *p2;
    644 
     746      struct vmodifiers vmod;
     747
     748      /* At the top of this loop, we are starting a brand new line.  */
    645749      /* Grab the next line to be evaluated */
    646750      ebuf->floc.lineno += nlines;
     
    672776
    673777          /* If there is no preceding rule line, don't treat this line
    674              as a command, even though it begins with a tab character.
     778             as a command, even though it begins with a recipe prefix.
    675779             SunOS 4 make appears to behave this way.  */
    676780
     
    711815        }
    712816
    713       /* This line is not a shell command line.  Don't worry about tabs.
     817      /* This line is not a shell command line.  Don't worry about whitespace.
    714818         Get more space if we need it; we don't need to preserve the current
    715819         contents of the buffer.  */
     
    720824          if (collapsed)
    721825            free (collapsed);
     826          /* Don't need xrealloc: we don't need to preserve the content.  */
    722827          collapsed = xmalloc (collapsed_length);
    723828        }
     
    736841#endif
    737842
    738       /* Compare a word, both length and contents. */
    739 #define word1eq(s)      (wlen == sizeof(s)-1 && strneq (s, p, sizeof(s)-1))
     843      /* Get rid if starting space (including formfeed, vtab, etc.)  */
    740844      p = collapsed;
    741845      while (isspace ((unsigned char)*p))
    742         ++p;
    743 
    744       if (*p == '\0')
    745         /* This line is completely empty--ignore it.  */
    746         continue;
    747 
    748       /* Find the end of the first token.  Note we don't need to worry about
    749        * ":" here since we compare tokens by length (so "export" will never
    750        * be equal to "export:").
    751        */
    752       for (p2 = p+1; *p2 != '\0' && !isspace ((unsigned char)*p2); ++p2)
    753         ;
    754       wlen = p2 - p;
    755 
    756       /* Find the start of the second token.  If it looks like a target or
    757          variable definition it can't be a preprocessor token so skip
    758          them--this allows variables/targets named `ifdef', `export', etc. */
    759       while (isspace ((unsigned char)*p2))
    760         ++p2;
    761 
    762       if ((p2[0] == ':' || p2[0] == '+' || p2[0] == '=') && p2[1] == '\0')
     846        ++p;
     847
     848      /* See if this is a variable assignment.  We need to do this early, to
     849         allow variables with names like 'ifdef', 'export', 'private', etc.  */
     850      p = parse_var_assignment(p, &vmod);
     851      if (vmod.assign_v)
    763852        {
    764           /* It can't be a preprocessor token so skip it if we're ignoring */
    765           if (ignoring)
    766             continue;
    767 
    768           goto skip_conditionals;
    769         }
    770 
    771       /* We must first check for conditional and `define' directives before
    772          ignoring anything, since they control what we will do with
    773          following lines.  */
    774 
    775       if (!in_ignored_define)
    776         {
    777 #ifndef CONFIG_WITH_VALUE_LENGTH
    778           int i = conditional_line (p, wlen, fstart);
    779 #else
    780           int i = conditional_line (p, eol, wlen, fstart);
    781 #endif
    782           if (i != -2)
     853          struct variable *v;
     854          enum variable_origin origin = vmod.override_v ? o_override : o_file;
     855
     856          /* If we're ignoring then we're done now.  */
     857          if (ignoring)
    783858            {
    784               if (i == -1)
    785                 fatal (fstart, _("invalid syntax in conditional"));
    786 
    787               ignoring = i;
     859              if (vmod.define_v)
     860                in_ignored_define = 1;
    788861              continue;
    789862            }
    790         }
    791 
    792       if (word1eq ("endef"))
     863
     864          if (vmod.undefine_v)
     865          {
     866            do_undefine (p, origin, ebuf);
     867
     868            /* This line has been dealt with.  */
     869            goto rule_complete;
     870          }
     871          else if (vmod.define_v)
     872            v = do_define (p IF_WITH_VALUE_LENGTH_PARAM(NULL), origin, ebuf);
     873          else
     874            v = try_variable_definition (fstart, p IF_WITH_VALUE_LENGTH_PARAM(NULL), origin, 0);
     875
     876          assert (v != NULL);
     877
     878          if (vmod.export_v)
     879            v->export = v_export;
     880          if (vmod.private_v)
     881            v->private_var = 1;
     882
     883          /* This line has been dealt with.  */
     884          goto rule_complete;
     885        }
     886
     887      /* If this line is completely empty, ignore it.  */
     888      if (*p == '\0')
     889        continue;
     890
     891      p2 = end_of_token (p);
     892      wlen = p2 - p;
     893      p2 = next_token (p2);
     894
     895      /* If we're in an ignored define, skip this line (but maybe get out).  */
     896      if (in_ignored_define)
    793897        {
    794           if (!in_ignored_define)
    795             fatal (fstart, _("extraneous `endef'"));
    796           in_ignored_define = 0;
     898          /* See if this is an endef line (plus optional comment).  */
     899          if (word1eq ("endef") && (*p2 == '\0' || *p2 == '#'))
     900            in_ignored_define = 0;
     901
    797902          continue;
    798903        }
    799904
    800       if (word1eq ("define"))
    801         {
    802           if (ignoring)
    803             in_ignored_define = 1;
    804           else
    805             {
    806               if (*p2 == '\0')
    807                 fatal (fstart, _("empty variable name"));
    808 
    809               /* Let the variable name be the whole rest of the line,
    810                  with trailing blanks stripped (comments have already been
    811                  removed), so it could be a complex variable/function
    812                  reference that might contain blanks.  */
    813               p = strchr (p2, '\0');
    814               while (isblank ((unsigned char)p[-1]))
    815                 --p;
    816               do_define (p2, p - p2, o_file, ebuf);
    817             }
    818           continue;
    819         }
    820 
    821       if (word1eq ("override"))
    822         {
    823           if (*p2 == '\0')
    824             error (fstart, _("empty `override' directive"));
    825 
    826           if (strneq (p2, "define", 6)
    827               && (isblank ((unsigned char)p2[6]) || p2[6] == '\0'))
    828             {
    829               if (ignoring)
    830                 in_ignored_define = 1;
    831               else
    832                 {
    833                   p2 = next_token (p2 + 6);
    834                   if (*p2 == '\0')
    835                     fatal (fstart, _("empty variable name"));
    836 
    837                   /* Let the variable name be the whole rest of the line,
    838                      with trailing blanks stripped (comments have already been
    839                      removed), so it could be a complex variable/function
    840                      reference that might contain blanks.  */
    841                   p = strchr (p2, '\0');
    842                   while (isblank ((unsigned char)p[-1]))
    843                     --p;
    844                   do_define (p2, p - p2, o_override, ebuf);
    845                 }
    846             }
    847           else if (!ignoring
     905      /* Check for conditional state changes.  */
     906      {
    848907#ifndef CONFIG_WITH_VALUE_LENGTH
    849                    && !try_variable_definition (fstart, p2, o_override, 0))
    850 #else
    851                    && !try_variable_definition (fstart, p2, eol, o_override, 0))
    852 #endif
    853             error (fstart, _("invalid `override' directive"));
    854 
    855           continue;
    856         }
     908        int i = conditional_line (p, wlen, fstart);
     909#else
     910        int i = conditional_line (p, eol, wlen, fstart);
     911#endif
     912        if (i != -2)
     913          {
     914            if (i == -1)
     915              fatal (fstart, _("invalid syntax in conditional"));
     916
     917            ignoring = i;
     918            continue;
     919          }
     920      }
     921
     922      /* Nothing to see here... move along.  */
     923      if (ignoring)
     924        continue;
     925
    857926#ifdef CONFIG_WITH_LOCAL_VARIABLES
    858 
    859927      if (word1eq ("local"))
    860928        {
     
    880948                  while (isblank ((unsigned char)p[-1]))
    881949                    --p;
    882                   do_define (p2, p - p2, o_local, ebuf);
     950                  do_define (p2 IF_WITH_VALUE_LENGTH_PARAM(p), o_local, ebuf);
    883951                }
    884952            }
    885953          else if (!ignoring
    886 # ifndef CONFIG_WITH_VALUE_LENGTH
    887                    && !try_variable_definition (fstart, p2, o_local, 0))
    888 # else
    889                    && !try_variable_definition (fstart, p2, eol, o_local, 0))
    890 # endif
     954                   && !try_variable_definition (fstart, p2 IF_WITH_VALUE_LENGTH_PARAM(eol), o_local, 0))
    891955            error (fstart, _("invalid `local' directive"));
    892956
     
    894958        }
    895959#endif /* CONFIG_WITH_LOCAL_VARIABLES */
    896 
    897       if (ignoring)
    898         /* Ignore the line.  We continue here so conditionals
    899            can appear in the middle of a rule.  */
    900         continue;
    901960
    902961#ifdef KMK
     
    918977
    919978#endif /* KMK */
    920       if (word1eq ("export"))
     979
     980      /* Manage the "export" keyword used outside of variable assignment
     981         as well as "unexport".  */
     982      if (word1eq ("export") || word1eq ("unexport"))
    921983        {
    922           /* 'export' by itself causes everything to be exported. */
     984          int exporting = *p == 'u' ? 0 : 1;
     985
     986          /* (un)export by itself causes everything to be (un)exported. */
    923987          if (*p2 == '\0')
    924             export_all_variables = 1;
     988            export_all_variables = exporting;
    925989          else
    926990            {
    927               struct variable *v;
    928 
     991              unsigned int l;
     992              const char *cp;
     993              char *ap;
     994
     995              /* Expand the line so we can use indirect and constructed
     996                 variable names in an (un)export command.  */
    929997#ifndef CONFIG_WITH_VALUE_LENGTH
    930               v = try_variable_definition (fstart, p2, o_file, 0);
    931 #else
    932               v = try_variable_definition (fstart, p2, eol, o_file, 0);
    933 #endif
    934               if (v != 0)
    935                 v->export = v_export;
    936               else
     998              cp = ap = allocated_variable_expand (p2);
     999#else
     1000              unsigned int buf_len;
     1001              cp = ap = allocated_variable_expand_3 (p2, eol - p2, NULL, &buf_len);
     1002#endif
     1003
     1004              for (p = find_next_token (&cp, &l); p != 0;
     1005                   p = find_next_token (&cp, &l))
    9371006                {
    938                   unsigned int l;
    939                   const char *cp;
    940                   char *ap;
    941 
    942                   /* Expand the line so we can use indirect and constructed
    943                      variable names in an export command.  */
     1007                  struct variable *v = lookup_variable (p, l);
     1008                  if (v == 0)
     1009                    v = define_variable_loc (p, l, "", o_file, 0, fstart);
     1010                  v->export = exporting ? v_export : v_noexport;
     1011                }
     1012
    9441013#ifndef CONFIG_WITH_VALUE_LENGTH
    945                   cp = ap = allocated_variable_expand (p2);
    946 #else
    947                   unsigned int buf_len;
    948                   cp = ap = allocated_variable_expand_3 (p2, eol - p2, NULL, &buf_len);
    949 #endif
    950 
    951                   for (p = find_next_token (&cp, &l); p != 0;
    952                        p = find_next_token (&cp, &l))
    953                     {
    954                       v = lookup_variable (p, l);
    955                       if (v == 0)
    956                         v = define_variable_loc (p, l, "", o_file, 0, fstart);
    957                       v->export = v_export;
    958                     }
    959 
    960 #ifndef CONFIG_WITH_VALUE_LENGTH
    961                   free (ap);
    962 #else
    963                   recycle_variable_buffer (ap, buf_len);
    964 #endif
    965                 }
     1014              free (ap);
     1015#else
     1016              recycle_variable_buffer (ap, buf_len);
     1017#endif
    9661018            }
    9671019          goto rule_complete;
    9681020        }
    9691021
    970       if (word1eq ("unexport"))
    971         {
    972           if (*p2 == '\0')
    973             export_all_variables = 0;
    974           else
    975             {
    976               unsigned int l;
    977               struct variable *v;
    978               const char *cp;
    979               char *ap;
    980 
    981               /* Expand the line so we can use indirect and constructed
    982                  variable names in an unexport command.  */
    983 #ifndef CONFIG_WITH_VALUE_LENGTH
    984               cp = ap = allocated_variable_expand (p2);
    985 #else
    986               unsigned int buf_len;
    987               cp = ap = allocated_variable_expand_3 (p2, eol - p2, NULL, &buf_len);
    988 #endif
    989 
    990               for (p = find_next_token (&cp, &l); p != 0;
    991                    p = find_next_token (&cp, &l))
    992                 {
    993                   v = lookup_variable (p, l);
    994                   if (v == 0)
    995                     v = define_variable_loc (p, l, "", o_file, 0, fstart);
    996 
    997                   v->export = v_noexport;
    998                 }
    999 
    1000 #ifndef CONFIG_WITH_VALUE_LENGTH
    1001               free (ap);
    1002 #else
    1003               recycle_variable_buffer (ap, buf_len);
    1004 #endif
    1005             }
    1006           goto rule_complete;
    1007         }
    1008 
    1009  skip_conditionals:
     1022      /* Handle the special syntax for vpath.  */
    10101023      if (word1eq ("vpath"))
    10111024        {
     
    10171030          if (p != 0)
    10181031            {
    1019               vpat = savestring (p, l);
     1032              vpat = xstrndup (p, l);
    10201033              p = find_next_token (&cp, &l);
    10211034              /* No searchpath means remove all previous
     
    10691082#endif /* CONFIG_WITH_INCLUDEDEP */
    10701083
     1084      /* Handle include and variants.  */
    10711085      if (word1eq ("include") || word1eq ("-include") || word1eq ("sinclude"))
    10721086        {
     
    10981112            }
    10991113
    1100           /* Parse the list of file names.  */
     1114          /* Parse the list of file names.  Don't expand archive references!  */
    11011115          p2 = p;
    1102 #ifndef CONFIG_WITH_ALLOC_CACHES
    1103           files = multi_glob (parse_file_seq (&p2, '\0',
    1104                                               sizeof (struct nameseq),
    1105                                               1),
    1106                               sizeof (struct nameseq));
    1107 #else
    1108           files = multi_glob (parse_file_seq (&p2, '\0', &nameseq_cache, 1),
    1109                               &nameseq_cache);
    1110 #endif
     1116          files = PARSE_FILE_SEQ (&p2, struct nameseq, '\0', NULL,
     1117                                  PARSEFS_NOAR);
    11111118#ifndef CONFIG_WITH_VALUE_LENGTH
    11121119          free (p);
     
    11301137              int r;
    11311138
    1132 #ifndef CONFIG_WITH_ALLOC_CACHES
    1133               free (files);
    1134 #else
    1135               alloccache_free (&nameseq_cache, files);
    1136 #endif
     1139              free_ns (files);
    11371140              files = next;
    11381141
    1139               r = eval_makefile (name, (RM_INCLUDED | RM_NO_TILDE
    1140                                         | (noerror ? RM_DONTCARE : 0)));
     1142              r = eval_makefile (name,
     1143                                 (RM_INCLUDED | RM_NO_TILDE
     1144                                  | (noerror ? RM_DONTCARE : 0)
     1145                                  | (set_default ? 0 : RM_NO_DEFAULT_GOAL)));
    11411146              if (!r && !noerror)
    11421147                error (fstart, "%s: %s", name, strerror (errno));
     
    11481153          goto rule_complete;
    11491154        }
    1150 
    1151 #ifndef CONFIG_WITH_VALUE_LENGTH
    1152       if (try_variable_definition (fstart, p, o_file, 0))
    1153 #else
    1154       if (try_variable_definition (fstart, p, eol, o_file, 0))
    1155 #endif
    1156         /* This line has been dealt with.  */
    1157         goto rule_complete;
    11581155
    11591156      /* This line starts with a tab but was not caught above because there
     
    11751172      {
    11761173        enum make_word_type wtype;
    1177         enum variable_origin v_origin;
    1178         int exported;
    11791174        char *cmdleft, *semip, *lb_next;
    11801175        unsigned int plen = 0;
     
    13471342           looking for targets.  */
    13481343        *colonp = '\0';
    1349 #ifndef CONFIG_WITH_ALLOC_CACHES
    1350         filenames = multi_glob (parse_file_seq (&p2, '\0',
    1351                                                 sizeof (struct nameseq),
    1352                                                 1),
    1353                                 sizeof (struct nameseq));
    1354 #else
    1355         filenames = multi_glob (parse_file_seq (&p2, '\0', &nameseq_cache, 1),
    1356                                 &nameseq_cache);
    1357 #endif
     1344        filenames = PARSE_FILE_SEQ (&p2, struct nameseq, '\0', NULL, 0);
    13581345        *p2 = ':';
    13591346
     
    13861373          }
    13871374
    1388         /* See if it's an "override" or "export" keyword; if so see if what
    1389            comes after it looks like a variable definition.  */
    1390 
    1391         wtype = get_next_mword (p2, NULL, &p, &wlen);
    1392 
    1393         v_origin = o_file;
    1394         exported = 0;
    1395         if (wtype == w_static)
    1396           {
    1397             if (word1eq ("override"))
    1398               {
    1399                 v_origin = o_override;
    1400                 wtype = get_next_mword (p+wlen, NULL, &p, &wlen);
    1401               }
    1402             else if (word1eq ("export"))
    1403               {
    1404                 exported = 1;
    1405                 wtype = get_next_mword (p+wlen, NULL, &p, &wlen);
    1406               }
    1407           }
    1408 
    1409         if (wtype != w_eol)
    1410           wtype = get_next_mword (p+wlen, NULL, NULL, NULL);
    1411 
    1412         if (wtype == w_varassign)
     1375        p2 = parse_var_assignment (p2, &vmod);
     1376        if (vmod.assign_v)
    14131377          {
    14141378            /* If there was a semicolon found, add it back, plus anything
     
    14181382                unsigned int l = p - variable_buffer;
    14191383                *(--semip) = ';';
     1384#ifndef CONFIG_WITH_VALUE_LENGTH
     1385                collapse_continuations (semip);
     1386#else
     1387                collapse_continuations (semip, strlen(semip)); /** @todo fix this */
     1388#endif
    14201389                variable_buffer_output (p2 + strlen (p2),
    14211390                                        semip, strlen (semip)+1);
    14221391                p = variable_buffer + l;
    14231392              }
    1424             record_target_var (filenames, p, v_origin, exported, fstart);
     1393            record_target_var (filenames, p2,
     1394                               vmod.override_v ? o_override : o_file,
     1395                               &vmod, fstart);
    14251396            filenames = 0;
    14261397            continue;
     
    14631434        while (p != 0 && p[-1] == '\\')
    14641435          {
    1465             register char *q = &p[-1];
    1466             register int backslash = 0;
     1436            char *q = &p[-1];
     1437            int backslash = 0;
    14671438            while (*q-- == '\\')
    14681439              backslash = !backslash;
     
    15071478          {
    15081479            struct nameseq *target;
    1509 #ifndef CONFIG_WITH_ALLOC_CACHES
    1510             target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
    1511 #else
    1512             target = parse_file_seq (&p2, ':', &nameseq_cache, 1);
    1513 #endif
     1480            target = PARSE_FILE_SEQ (&p2, struct nameseq, ':', NULL,
     1481                                     PARSEFS_NOGLOB);
    15141482            ++p2;
    15151483            if (target == 0)
     
    15211489            if (pattern_percent == 0)
    15221490              fatal (fstart, _("target pattern contains no `%%' (target `%s')"), target->name); /* bird */
    1523 #ifndef CONFIG_WITH_ALLOC_CACHES
    1524             free (target);
    1525 #else
    1526             alloccache_free (&nameseq_cache, target);
    1527 #endif
     1491            free_ns (target);
    15281492          }
    15291493        else
     
    15351499        strip_whitespace (&beg, &end);
    15361500
     1501        /* Put all the prerequisites here; they'll be parsed later.  */
    15371502        if (beg <= end && *beg != '\0')
    1538           {
    1539             /* Put all the prerequisites here; they'll be parsed later.  */
    1540             deps = alloc_dep ();
    1541 #ifndef CONFIG_WITH_VALUE_LENGTH
    1542             deps->name = strcache_add_len (beg, end - beg + 1);
    1543 #else  /* CONFIG_WITH_VALUE_LENGTH */
    1544             {
    1545               /* Make sure the strcache_add_len input is terminated so it
    1546                  doesn't have to make a temporary copy on the stack. */
    1547               char saved = end[1];
    1548               ((char *)end)[1] = '\0';
    1549               deps->name = strcache_add_len (beg, end - beg + 1);
    1550               ((char *)end)[1] = saved;
    1551             }
    1552 #endif /* CONFIG_WITH_VALUE_LENGTH */
    1553           }
     1503          depstr = xstrndup (beg, end - beg + 1);
    15541504        else
    1555           deps = 0;
     1505          depstr = 0;
    15561506
    15571507        commands_idx = 0;
     
    15871537           Because the target is not recorded until after ifeq directive is
    15881538           evaluated the .DEFAULT_GOAL does not contain foo yet as one
    1589            would expect. Because of this we have to move some of the logic
    1590            here.  */
    1591 
    1592         if (**default_goal_name == '\0' && set_default)
     1539           would expect. Because of this we have to move the logic here.  */
     1540
     1541        if (set_default && default_goal_var->value[0] == '\0')
    15931542          {
    15941543            const char *name;
     
    16801629    free (collapsed);
    16811630  free (commands);
    1682 
    1683   return 1;
    16841631}
    16851632
     
    17581705#endif /* CONFIG_WITH_VALUE_LENGTH */
    17591706
     1707/* Execute a `undefine' directive.
     1708   The undefine line has already been read, and NAME is the name of
     1709   the variable to be undefined. */
     1710
     1711static void
     1712do_undefine (char *name, enum variable_origin origin, struct ebuffer *ebuf)
     1713{
     1714  char *p, *var;
     1715
     1716  /* Expand the variable name and find the beginning (NAME) and end.  */
     1717  var = allocated_variable_expand (name);
     1718  name = next_token (var);
     1719  if (*name == '\0')
     1720    fatal (&ebuf->floc, _("empty variable name"));
     1721  p = name + strlen (name) - 1;
     1722  while (p > name && isblank ((unsigned char)*p))
     1723    --p;
     1724  p[1] = '\0';
     1725
     1726  undefine_variable_global (name, p - name + 1, origin);
     1727  free (var);
     1728}
     1729
    17601730/* Execute a `define' directive.
    17611731   The first line has already been read, and NAME is the name of
    17621732   the variable to be defined.  The following lines remain to be read.  */
    17631733
    1764 static void
    1765 do_define (char *name, unsigned int namelen,
     1734static struct variable *
     1735do_define (char *name IF_WITH_VALUE_LENGTH_PARAM(char *eos),
    17661736           enum variable_origin origin, struct ebuffer *ebuf)
    17671737{
     1738  struct variable *v;
     1739  enum variable_flavor flavor;
    17681740  struct floc defstart;
    1769   long nlines = 0;
    17701741  int nlevels = 1;
    17711742  unsigned int length = 100;
    17721743  char *definition = xmalloc (length);
    17731744  unsigned int idx = 0;
    1774   char *p;
    1775 
    1776   /* Expand the variable name.  */
    1777   char *var = alloca (namelen + 1);
    1778   memcpy (var, name, namelen);
    1779   var[namelen] = '\0';
    1780   var = variable_expand (var);
     1745  char *p, *var;
    17811746
    17821747  defstart = ebuf->floc;
    17831748
     1749  p = parse_variable_definition (name, &flavor);
     1750  if (p == NULL)
     1751    /* No assignment token, so assume recursive.  */
     1752    flavor = f_recursive;
     1753  else
     1754    {
     1755      if (*(next_token (p)) != '\0')
     1756        error (&defstart, _("extraneous text after `define' directive"));
     1757
     1758      /* Chop the string before the assignment token to get the name.  */
     1759      p[flavor == f_recursive ? -1 : -2] = '\0';
     1760    }
     1761
     1762  /* Expand the variable name and find the beginning (NAME) and end.  */
     1763  var = allocated_variable_expand (name);
     1764  name = next_token (var);
     1765  if (*name == '\0')
     1766    fatal (&defstart, _("empty variable name"));
     1767  p = name + strlen (name) - 1;
     1768  while (p > name && isblank ((unsigned char)*p))
     1769    --p;
     1770  p[1] = '\0';
     1771
     1772  /* Now read the value of the variable.  */
    17841773  while (1)
    17851774    {
    17861775      unsigned int len;
    17871776      char *line;
    1788 
    1789       nlines = readline (ebuf);
     1777      long nlines = readline (ebuf);
     1778
     1779      /* If there is nothing left to be eval'd, there's no 'endef'!!  */
     1780      if (nlines < 0)
     1781        fatal (&defstart, _("missing `endef', unterminated `define'"));
     1782
    17901783      ebuf->floc.lineno += nlines;
    1791 
    1792       /* If there is nothing left to eval, we're done. */
    1793       if (nlines < 0)
    1794         break;
    1795 
    17961784      line = ebuf->buffer;
    17971785
     
    18031791
    18041792      /* If the line doesn't begin with a tab, test to see if it introduces
    1805          another define, or ends one.  */
    1806 
    1807       /* Stop if we find an 'endef' */
     1793         another define, or ends one.  Stop if we find an 'endef' */
    18081794      if (line[0] != cmd_prefix)
    18091795        {
     
    18321818              ebuf->eol = remove_comments (p, ebuf->eol);
    18331819#endif
    1834               if (*next_token (p) != '\0')
     1820              if (*(next_token (p)) != '\0')
    18351821                error (&ebuf->floc,
    1836                        _("Extraneous text after `endef' directive"));
     1822                       _("extraneous text after `endef' directive"));
    18371823
    18381824              if (--nlevels == 0)
    1839                 {
    1840                   /* Define the variable.  */
    1841                   if (idx == 0)
    1842                     definition[0] = '\0';
    1843                   else
    1844                     definition[idx - 1] = '\0';
    1845 
    1846                   /* Always define these variables in the global set.  */
    1847                   define_variable_global (var, strlen (var), definition,
    1848                                           origin, 1, &defstart);
    1849                   free (definition);
    1850                   return;
    1851                 }
     1825                break;
    18521826            }
    18531827        }
    18541828
    1855       /* Otherwise add this line to the variable definition.  */
     1829      /* Add this line to the variable definition.  */
    18561830#ifndef CONFIG_WITH_VALUE_LENGTH
    18571831      len = strlen (line);
     
    18721846    }
    18731847
    1874   /* No `endef'!!  */
    1875   fatal (&defstart, _("missing `endef', unterminated `define'"));
    1876 
    1877   /* NOTREACHED */
    1878   return;
     1848  /* We've got what we need; define the variable.  */
     1849  if (idx == 0)
     1850    definition[0] = '\0';
     1851  else
     1852    definition[idx - 1] = '\0';
     1853
     1854  v = do_variable_definition (&defstart, name, definition, origin, flavor, 0);
     1855  free (definition);
     1856  free (var);
     1857  return (v);
    18791858}
    18801859
     
    22672246
    22682247
    2269 /* Remove duplicate dependencies in CHAIN.  */
    2270 #ifndef CONFIG_WITH_STRCACHE2
    2271 
    2272 static unsigned long
    2273 dep_hash_1 (const void *key)
    2274 {
    2275   return_STRING_HASH_1 (dep_name ((struct dep const *) key));
    2276 }
    2277 
    2278 static unsigned long
    2279 dep_hash_2 (const void *key)
    2280 {
    2281   return_STRING_HASH_2 (dep_name ((struct dep const *) key));
    2282 }
    2283 
    2284 static int
    2285 dep_hash_cmp (const void *x, const void *y)
    2286 {
    2287   struct dep *dx = (struct dep *) x;
    2288   struct dep *dy = (struct dep *) y;
    2289   int cmp = strcmp (dep_name (dx), dep_name (dy));
    2290 
    2291   /* If the names are the same but ignore_mtimes are not equal, one of these
    2292      is an order-only prerequisite and one isn't.  That means that we should
    2293      remove the one that isn't and keep the one that is.  */
    2294 
    2295   if (!cmp && dx->ignore_mtime != dy->ignore_mtime)
    2296     dx->ignore_mtime = dy->ignore_mtime = 0;
    2297 
    2298   return cmp;
    2299 }
    2300 
    2301 #else  /* CONFIG_WITH_STRCACHE2 */
    2302 
    2303 /* Exploit the fact that all names are in the string cache. This means equal
    2304    names shall have the same storage and there is no need for hashing or
    2305    comparing. Use the address as the first hash, avoiding any touching of
    2306    the name, and the length as the second. */
    2307 
    2308 static unsigned long
    2309 dep_hash_1 (const void *key)
    2310 {
    2311   const char *name = dep_name ((struct dep const *) key);
    2312   assert (strcache2_is_cached (&file_strcache, name));
    2313   return (size_t) name / sizeof(void *);
    2314 }
    2315 
    2316 static unsigned long
    2317 dep_hash_2 (const void *key)
    2318 {
    2319   const char *name = dep_name ((struct dep const *) key);
    2320   return strcache2_get_len (&file_strcache, name);
    2321 }
    2322 
    2323 static int
    2324 dep_hash_cmp (const void *x, const void *y)
    2325 {
    2326   struct dep *dx = (struct dep *) x;
    2327   struct dep *dy = (struct dep *) y;
    2328   const char *dxname = dep_name (dx);
    2329   const char *dyname = dep_name (dy);
    2330   int cmp = dxname == dyname ? 0 : 1;
    2331 
    2332   /* check preconds: both cached and the cache contains no duplicates. */
    2333   assert (strcache2_is_cached (&file_strcache, dxname));
    2334   assert (strcache2_is_cached (&file_strcache, dyname));
    2335   assert (cmp == 0 || strcmp (dxname, dyname) != 0);
    2336 
    2337   /* If the names are the same but ignore_mtimes are not equal, one of these
    2338      is an order-only prerequisite and one isn't.  That means that we should
    2339      remove the one that isn't and keep the one that is.  */
    2340 
    2341   if (!cmp && dx->ignore_mtime != dy->ignore_mtime)
    2342     dx->ignore_mtime = dy->ignore_mtime = 0;
    2343 
    2344   return cmp;
    2345 }
    2346 
    2347 #endif /* CONFIG_WITH_STRCACHE2 */
    2348 
    2349 void
    2350 uniquize_deps (struct dep *chain)
    2351 {
    2352   struct hash_table deps;
    2353   register struct dep **depp;
    2354 
    2355   hash_init (&deps, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
    2356 
    2357   /* Make sure that no dependencies are repeated.  This does not
    2358      really matter for the purpose of updating targets, but it
    2359      might make some names be listed twice for $^ and $?.  */
    2360 
    2361   depp = &chain;
    2362   while (*depp)
    2363     {
    2364       struct dep *dep = *depp;
    2365       struct dep **dep_slot = (struct dep **) hash_find_slot (&deps, dep);
    2366       if (HASH_VACANT (*dep_slot))
    2367         {
    2368           hash_insert_at (&deps, dep, dep_slot);
    2369           depp = &dep->next;
    2370         }
    2371       else
    2372         {
    2373           /* Don't bother freeing duplicates.
    2374              It's dangerous and little benefit accrues.  */
    2375           *depp = dep->next;
    2376         }
    2377     }
    2378 
    2379   hash_free (&deps, 0);
    2380 }
    2381 
    2382 
    23832248/* Record target-specific variable values for files FILENAMES.
    23842249   TWO_COLON is nonzero if a double colon was used.
     
    23922257static void
    23932258record_target_var (struct nameseq *filenames, char *defn,
    2394                    enum variable_origin origin, int exported,
     2259                   enum variable_origin origin, struct vmodifiers *vmod,
    23952260                   const struct floc *flocp)
    23962261{
     
    24122277
    24132278      nextf = filenames->next;
    2414 #ifndef CONFIG_WITH_ALLOC_CACHES
    2415       free (filenames);
    2416 #else
    2417       alloccache_free (&nameseq_cache, filenames);
    2418 #endif
     2279      free_ns (filenames);
    24192280
    24202281      /* If it's a pattern target, then add it to the pattern-specific
     
    24282289          /* I don't think this can fail since we already determined it was a
    24292290             variable definition.  */
    2430 #ifndef CONFIG_WITH_VALUE_LENGTH
    2431           v = parse_variable_definition (&p->variable, defn);
    2432 #else
    2433           v = parse_variable_definition (&p->variable, defn, NULL);
    2434 #endif
     2291          v = assign_variable_definition (&p->variable, defn IF_WITH_VALUE_LENGTH_PARAM(NULL));
    24352292          assert (v != 0);
    24362293
     2294          v->origin = origin;
    24372295          if (v->flavor == f_simple)
    24382296            v->value = allocated_variable_expand (v->value);
     
    24682326
    24692327          current_variable_set_list = f->variables;
    2470 #ifndef CONFIG_WITH_VALUE_LENGTH
    2471           v = try_variable_definition (flocp, defn, origin, 1);
    2472 #else
    2473           v = try_variable_definition (flocp, defn, NULL, origin, 1);
    2474 #endif
     2328          v = try_variable_definition (flocp, defn IF_WITH_VALUE_LENGTH_PARAM(NULL), origin, 1);
    24752329          if (!v)
    2476             error (flocp, _("Malformed target-specific variable definition"));
     2330            fatal (flocp, _("Malformed target-specific variable definition"));
    24772331          current_variable_set_list = global;
    24782332        }
    24792333
    24802334      /* Set up the variable to be *-specific.  */
    2481       v->origin = origin;
    24822335      v->per_target = 1;
    2483       v->export = exported ? v_export : v_default;
     2336      v->private_var = vmod->private_v;
     2337      v->export = vmod->export_v ? v_export : v_default;
    24842338
    24852339      /* If it's not an override, check to see if there was a command-line
    24862340         setting.  If so, reset the value.  */
    2487       if (origin != o_override)
     2341      if (v->origin != o_override)
    24882342        {
    24892343          struct variable *gv;
     
    25072361              v->value = xstrdup (gv->value);
    25082362#else
    2509               v->value = savestring (gv->value, gv->value_length);
     2363              v->value = xstrndup (gv->value, gv->value_length);
    25102364              v->value_length = gv->value_length;
    25112365#endif
     
    25302384   that are not incorporated into other data structures.  */
    25312385
    2532 #ifndef CONFIG_WITH_INCLUDEDEP
    25332386static void
    2534 #else
    2535 void
    2536 #endif
    25372387record_files (struct nameseq *filenames, const char *pattern,
    2538               const char *pattern_percent, struct dep *deps,
     2388              const char *pattern_percent, char *depstr,
    25392389              unsigned int cmds_started, char *commands,
    25402390              unsigned int commands_idx, int two_colon,
    25412391              const struct floc *flocp)
    25422392{
    2543   struct nameseq *nextf;
    2544   int implicit = 0;
    2545   unsigned int max_targets = 0, target_idx = 0;
    2546   const char **targets = 0, **target_percents = 0;
    2547   struct commands *cmds;
    25482393#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
    25492394  struct file *prev_file = 0;
     
    25512396    multi_mode = !two_colon && !pattern ? m_unsettled : m_no;
    25522397#endif
     2398  struct commands *cmds;
     2399  struct dep *deps;
     2400  const char *implicit_percent;
     2401  const char *name;
    25532402
    25542403  /* If we've already snapped deps, that means we're in an eval being
     
    25592408    fatal (flocp, _("prerequisites cannot be defined in recipes"));
    25602409
     2410  /* Determine if this is a pattern rule or not.  */
     2411  name = filenames->name;
     2412  implicit_percent = find_percent_cached (&name);
     2413
     2414  /* If there's a recipe, set up a struct for it.  */
    25612415  if (commands_idx > 0)
    25622416    {
     
    25682422      cmds->fileinfo.filenm = flocp->filenm;
    25692423      cmds->fileinfo.lineno = cmds_started;
    2570       cmds->commands = savestring (commands, commands_idx);
     2424      cmds->commands = xstrndup (commands, commands_idx);
    25712425      cmds->command_lines = 0;
    25722426#ifdef CONFIG_WITH_MEMORY_OPTIMIZATIONS
     
    25752429    }
    25762430  else
    2577     cmds = 0;
    2578 
    2579   for (; filenames != 0; filenames = nextf)
     2431     cmds = 0;
     2432
     2433  /* If there's a prereq string then parse it--unless it's eligible for 2nd
     2434     expansion: if so, snap_deps() will do it.  */
     2435  if (depstr == 0)
     2436    deps = 0;
     2437  else if (second_expansion && strchr (depstr, '$'))
    25802438    {
    2581       const char *name = filenames->name;
     2439      deps = alloc_dep ();
     2440      deps->name = depstr;
     2441      deps->need_2nd_expansion = 1;
     2442      deps->staticpattern = pattern != 0;
     2443    }
     2444  else
     2445    {
     2446      deps = split_prereqs (depstr);
     2447      free (depstr);
     2448
     2449      /* We'll enter static pattern prereqs later when we have the stem.  We
     2450         don't want to enter pattern rules at all so that we don't think that
     2451         they ought to exist (make manual "Implicit Rule Search Algorithm",
     2452         item 5c).  */
     2453      if (! pattern && ! implicit_percent)
     2454        deps = enter_prereqs (deps, NULL);
     2455    }
     2456
     2457  /* For implicit rules, _all_ the targets must have a pattern.  That means we
     2458     can test the first one to see if we're working with an implicit rule; if
     2459     so we handle it specially. */
     2460
     2461  if (implicit_percent)
     2462    {
     2463      struct nameseq *nextf;
     2464      const char **targets, **target_pats;
     2465      unsigned int c;
     2466
     2467      if (pattern != 0)
     2468        fatal (flocp, _("mixed implicit and static pattern rules"));
     2469
     2470      /* Count the targets to create an array of target names.
     2471         We already have the first one.  */
     2472      nextf = filenames->next;
     2473      free_ns (filenames);
     2474      filenames = nextf;
     2475
     2476      for (c = 1; nextf; ++c, nextf = nextf->next)
     2477        ;
     2478      targets = xmalloc (c * sizeof (const char *));
     2479      target_pats = xmalloc (c * sizeof (const char *));
     2480
     2481      targets[0] = name;
     2482      target_pats[0] = implicit_percent;
     2483
     2484      c = 1;
     2485      while (filenames)
     2486        {
     2487          name = filenames->name;
     2488          implicit_percent = find_percent_cached (&name);
     2489
     2490          if (implicit_percent == 0)
     2491            fatal (flocp, _("mixed implicit and normal rules"));
     2492
     2493          targets[c] = name;
     2494          target_pats[c] = implicit_percent;
     2495          ++c;
     2496
     2497          nextf = filenames->next;
     2498          free_ns (filenames);
     2499          filenames = nextf;
     2500        }
     2501
     2502      create_pattern_rule (targets, target_pats, c, two_colon, deps, cmds, 1);
     2503
     2504      return;
     2505    }
     2506
     2507
     2508  /* Walk through each target and create it in the database.
     2509     We already set up the first target, above.  */
     2510  while (1)
     2511    {
     2512      struct nameseq *nextf = filenames->next;
    25822513      struct file *f;
    25832514      struct dep *this = 0;
    2584       const char *implicit_percent;
    2585 
    2586       nextf = filenames->next;
    2587 #ifndef CONFIG_WITH_ALLOC_CACHES
    2588       free (filenames);
    2589 #else
    2590       alloccache_free (&nameseq_cache, filenames);
    2591 #endif
     2515
     2516      free_ns (filenames);
    25922517
    25932518      /* Check for special targets.  Do it here instead of, say, snap_deps()
    25942519         so that we can immediately use the value.  */
    2595 
    25962520      if (streq (name, ".POSIX"))
    2597         posix_pedantic = 1;
     2521        {
     2522          posix_pedantic = 1;
     2523          define_variable_cname (".SHELLFLAGS", "-ec", o_default, 0);
     2524        }
    25982525      else if (streq (name, ".SECONDEXPANSION"))
    25992526        second_expansion = 1;
     
    26022529        second_target_expansion = 1;
    26032530#endif
    2604 
    2605       implicit_percent = find_percent_cached (&name);
    2606       implicit |= implicit_percent != 0;
    2607 
    2608       if (implicit)
    2609         {
    2610           if (pattern != 0)
    2611             fatal (flocp, _("mixed implicit and static pattern rules"));
    2612 
    2613           if (implicit_percent == 0)
    2614             fatal (flocp, _("mixed implicit and normal rules"));
    2615 
    2616           if (targets == 0)
    2617             {
    2618               max_targets = 5;
    2619               targets = xmalloc (5 * sizeof (char *));
    2620               target_percents = xmalloc (5 * sizeof (char *));
    2621               target_idx = 0;
    2622             }
    2623           else if (target_idx == max_targets - 1)
    2624             {
    2625               max_targets += 5;
    2626               targets = xrealloc ((void *)targets, max_targets * sizeof (char *));
    2627               target_percents = xrealloc ((void *)target_percents,
    2628                                           max_targets * sizeof (char *));
    2629             }
    2630           targets[target_idx] = name;
    2631           target_percents[target_idx] = implicit_percent;
    2632           ++target_idx;
    2633           continue;
    2634         }
     2531#if !defined(WINDOWS32) && !defined (__MSDOS__) && !defined (__EMX__)
     2532      else if (streq (name, ".ONESHELL"))
     2533        one_shell = 1;
     2534#endif
    26352535
    26362536#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
     
    26702570                }
    26712571              multi_mode = name[1] == '\0' ? m_yes : m_yes_maybe;
    2672               continue;
     2572              goto l_next;
    26732573            }
    26742574        }
     
    26782578
    26792579      /* If this is a static pattern rule:
    2680          `targets: target%pattern: dep%pattern; cmds',
     2580         `targets: target%pattern: prereq%pattern; recipe',
    26812581         make sure the pattern matches this target name.  */
    26822582      if (pattern && !pattern_matches (pattern, pattern_percent, name))
    26832583        error (flocp, _("target `%s' doesn't match the target pattern"), name);
    26842584      else if (deps)
    2685         {
    2686           /* If there are multiple filenames, copy the chain DEPS for all but
    2687              the last one.  It is not safe for the same deps to go in more
    2688              than one place in the database.  */
    2689           this = nextf != 0 ? copy_dep_chain (deps) : deps;
    2690           this->need_2nd_expansion = (second_expansion
    2691                                       && strchr (this->name, '$'));
    2692         }
    2693 
     2585        /* If there are multiple targets, copy the chain DEPS for all but the
     2586           last one.  It is not safe for the same deps to go in more than one
     2587           place in the database.  */
     2588        this = nextf != 0 ? copy_dep_chain (deps) : deps;
     2589
     2590      /* Find or create an entry in the file database for this target.  */
    26942591      if (!two_colon)
    26952592        {
    2696           /* Single-colon.  Combine these dependencies
    2697              with others in file's existing record, if any.  */
     2593          /* Single-colon.  Combine this rule with the file's existing record,
     2594             if any.  */
    26982595#ifndef KMK
    26992596          f = enter_file (strcache_add (name));
     
    27012598          f = enter_file (name);
    27022599#endif
    2703 
    27042600          if (f->double_colon)
    27052601            fatal (flocp,
     
    27262622            }
    27272623
    2728           f->is_target = 1;
    2729 
    27302624          /* Defining .DEFAULT with no deps or cmds clears it.  */
    27312625          if (f == default_file && this == 0 && cmds == 0)
     
    27592653              free_dep_chain (f->deps);
    27602654              f->deps = 0;
    2761             }
    2762           else if (this != 0)
    2763             {
    2764               /* Add the file's old deps and the new ones in THIS together.  */
    2765 
    2766               if (f->deps != 0)
    2767                 {
    2768                   struct dep **d_ptr = &f->deps;
    2769 
    2770                   while ((*d_ptr)->next != 0)
    2771                     d_ptr = &(*d_ptr)->next;
    2772 
    2773                   if (cmds != 0)
    2774                     /* This is the rule with commands, so put its deps
    2775                        last. The rationale behind this is that $< expands to
    2776                        the first dep in the chain, and commands use $<
    2777                        expecting to get the dep that rule specifies.  However
    2778                        the second expansion algorithm reverses the order thus
    2779                        we need to make it last here.  */
    2780                     (*d_ptr)->next = this;
    2781                   else
    2782                     {
    2783                       /* This is the rule without commands. Put its
    2784                          dependencies at the end but before dependencies from
    2785                          the rule with commands (if any). This way everything
    2786                          appears in makefile order.  */
    2787 
    2788                       if (f->cmds != 0)
    2789                         {
    2790 #ifndef KMK /* bugfix: Don't chop the chain! */
    2791                           this->next = *d_ptr;
    2792                           *d_ptr = this;
    2793 #else   /* KMK */
    2794                           struct dep *this_last = this;
    2795                           while (this_last->next)
    2796                             this_last = this_last->next;
    2797                           this_last->next = *d_ptr;
    2798                           *d_ptr = this;
    2799 #endif  /* KMK */
    2800                         }
    2801                       else
    2802                         (*d_ptr)->next = this;
    2803                     }
    2804                 }
    2805               else
    2806                 f->deps = this;
    2807 
    2808               /* This is a hack. I need a way to communicate to snap_deps()
    2809                  that the last dependency line in this file came with commands
    2810                  (so that logic in snap_deps() can put it in front and all
    2811                  this $< -logic works). I cannot simply rely on file->cmds
    2812                  being not 0 because of the cases like the following:
    2813 
    2814                  foo: bar
    2815                  foo:
    2816                      ...
    2817 
    2818                  I am going to temporarily "borrow" UPDATING member in
    2819                  `struct file' for this.   */
    2820 
    2821               if (cmds != 0)
    2822                 f->updating = 1;
    28232655            }
    28242656        }
     
    28322664#endif /* CONFIG_WITH_STRCACHE2 */
    28332665
    2834           /* Check for both : and :: rules.  Check is_target so
    2835              we don't lose on default suffix rules or makefiles.  */
     2666          /* Check for both : and :: rules.  Check is_target so we don't lose
     2667             on default suffix rules or makefiles.  */
    28362668          if (f != 0 && f->is_target && !f->double_colon)
    28372669            fatal (flocp,
    28382670                   _("target file `%s' has both : and :: entries"), f->name);
     2671
    28392672#ifndef KMK
    28402673          f = enter_file (strcache_add (name));
     
    28502683               double_colon pointer to itself.  */
    28512684            f->double_colon = f;
    2852           f->is_target = 1;
    2853           f->deps = this;
     2685
    28542686          f->cmds = cmds;
    28552687        }
    28562688
     2689      f->is_target = 1;
     2690
    28572691      /* If this is a static pattern rule, set the stem to the part of its
    28582692         name that matched the `%' in the pattern, so you can use $* in the
    2859          commands.  */
     2693         commands.  If we didn't do it before, enter the prereqs now.  */
    28602694      if (pattern)
    28612695        {
     
    28692703          if (this)
    28702704            {
    2871               this->staticpattern = 1;
    2872               this->stem = f->stem;
     2705              if (! this->need_2nd_expansion)
     2706                this = enter_prereqs (this, f->stem);
     2707              else
     2708                this->stem = f->stem;
    28732709            }
    28742710        }
    28752711
     2712      /* Add the dependencies to this file entry.  */
     2713      if (this != 0)
     2714        {
     2715          /* Add the file's old deps and the new ones in THIS together.  */
     2716          if (f->deps == 0)
     2717            f->deps = this;
     2718          else if (cmds != 0)
     2719            {
     2720              struct dep *d = this;
     2721
     2722              /* If this rule has commands, put these deps first.  */
     2723              while (d->next != 0)
     2724                d = d->next;
     2725
     2726              d->next = f->deps;
     2727              f->deps = this;
     2728            }
     2729          else
     2730            {
     2731              struct dep *d = f->deps;
     2732
     2733              /* A rule without commands: put its prereqs at the end.  */
     2734              while (d->next != 0)
     2735                d = d->next;
     2736
     2737              d->next = this;
     2738            }
     2739        }
     2740
    28762741      name = f->name;
    28772742
    2878       /* If this target is a default target, update DEFAULT_GOAL_FILE.  */
    2879       if (streq (*default_goal_name, name)
    2880           && (default_goal_file == 0
    2881               || ! streq (default_goal_file->name, name)))
    2882         default_goal_file = f;
    2883     }
    2884 
    2885   if (implicit)
    2886     {
    2887       if (deps)
    2888         deps->need_2nd_expansion = second_expansion;
    2889       create_pattern_rule (targets, target_percents, target_idx,
    2890                            two_colon, deps, cmds, 1);
     2743#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
     2744l_next:
     2745#endif
     2746      /* All done!  Set up for the next one.  */
     2747      if (nextf == 0)
     2748        break;
     2749
     2750      filenames = nextf;
     2751
     2752      /* Reduce escaped percents.  If there are any unescaped it's an error  */
     2753      name = filenames->name;
     2754      if (find_percent_cached (&name))
     2755        fatal (flocp, _("mixed implicit and normal rules"));
    28912756    }
    28922757}
     
    31112976
    31122977
    3113 /* Parse a string into a sequence of filenames represented as a
    3114    chain of struct nameseq's in reverse order and return that chain.
    3115 
    3116    The string is passed as STRINGP, the address of a string pointer.
    3117    The string pointer is updated to point at the first character
    3118    not parsed, which either is a null char or equals STOPCHAR.
    3119 
    3120    SIZE is how big to construct chain elements.
    3121    This is useful if we want them actually to be other structures
    3122    that have room for additional info.
    3123 
    3124    If STRIP is nonzero, strip `./'s off the beginning.  */
    3125 
    3126 #ifndef CONFIG_WITH_ALLOC_CACHES
    3127 struct nameseq *
    3128 parse_file_seq (char **stringp, int stopchar, unsigned int size, int strip)
    3129 #else
    3130 struct nameseq *
    3131 parse_file_seq (char **stringp, int stopchar, struct alloccache *cache, int strip)
    3132 #endif
    3133 {
    3134   struct nameseq *new = 0;
    3135   struct nameseq *new1;
    3136 #ifndef NO_ARCHIVES /* bird: MSC warning */
    3137   struct nameseq *lastnew1;
    3138 #endif
    3139   char *p = *stringp;
    3140 
    3141 #ifdef VMS
    3142 # define VMS_COMMA ','
    3143 #else
    3144 # define VMS_COMMA 0
    3145 #endif
    3146 
    3147   while (1)
    3148     {
    3149       const char *name;
    3150       char *q;
    3151 
    3152       /* Skip whitespace; see if any more names are left.  */
    3153       p = next_token (p);
    3154       if (*p == '\0')
    3155         break;
    3156       if (*p == stopchar)
    3157         break;
    3158 
    3159       /* There are, so find the end of the next name.  */
    3160       q = p;
    3161       p = find_char_unquote (q, stopchar, VMS_COMMA, 1, 0);
    3162 #ifdef VMS
    3163         /* convert comma separated list to space separated */
    3164       if (p && *p == ',')
    3165         *p =' ';
    3166 #endif
    3167 #ifdef _AMIGA
    3168       if (stopchar == ':' && p && *p == ':'
    3169           && !(isspace ((unsigned char)p[1]) || !p[1]
    3170                || isspace ((unsigned char)p[-1])))
    3171         p = find_char_unquote (p+1, stopchar, VMS_COMMA, 1, 0);
    3172 #endif
    3173 #ifdef HAVE_DOS_PATHS
    3174     /* For DOS paths, skip a "C:\..." or a "C:/..." until we find the
    3175        first colon which isn't followed by a slash or a backslash.
    3176        Note that tokens separated by spaces should be treated as separate
    3177        tokens since make doesn't allow path names with spaces */
    3178     if (stopchar == ':')
    3179       while (p != 0 && !isspace ((unsigned char)*p) &&
    3180              (p[1] == '\\' || p[1] == '/') && isalpha ((unsigned char)p[-1]))
    3181         p = find_char_unquote (p + 1, stopchar, VMS_COMMA, 1, 0);
    3182 #endif
    3183       if (p == 0)
    3184         p = q + strlen (q);
    3185 
    3186       if (strip)
    3187 #ifdef VMS
    3188         /* Skip leading `[]'s.  */
    3189         while (p - q > 2 && q[0] == '[' && q[1] == ']')
    3190 #else
    3191         /* Skip leading `./'s.  */
    3192         while (p - q > 2 && q[0] == '.' && q[1] == '/')
    3193 #endif
    3194           {
    3195             q += 2;             /* Skip "./".  */
    3196             while (q < p && *q == '/')
    3197               /* Skip following slashes: ".//foo" is "foo", not "/foo".  */
    3198               ++q;
    3199           }
    3200 
    3201       /* Extract the filename just found, and skip it.  */
    3202 
    3203       if (q == p)
    3204         /* ".///" was stripped to "". */
    3205 #if defined(VMS)
    3206         continue;
    3207 #elif defined(_AMIGA)
    3208         name = "";
    3209 #else
    3210         name = "./";
    3211 #endif
    3212       else
    3213 #ifdef VMS
    3214 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
    3215  *  to remove this '\' before we can use the filename.
    3216  * Savestring called because q may be read-only string constant.
    3217  */
    3218         {
    3219           char *qbase = xstrdup (q);
    3220           char *pbase = qbase + (p-q);
    3221           char *q1 = qbase;
    3222           char *q2 = q1;
    3223           char *p1 = pbase;
    3224 
    3225           while (q1 != pbase)
    3226             {
    3227               if (*q1 == '\\' && *(q1+1) == ':')
    3228                 {
    3229                   q1++;
    3230                   p1--;
    3231                 }
    3232               *q2++ = *q1++;
    3233             }
    3234           name = strcache_add_len (qbase, p1 - qbase);
    3235           free (qbase);
    3236         }
    3237 #elif !defined (CONFIG_WITH_VALUE_LENGTH) || defined (CONFIG_WITH_STRCACHE2)
    3238         name = strcache_add_len (q, p - q);
    3239 #else  /* CONFIG_WITH_VALUE_LENGTH */
    3240        {
    3241          /* Make sure it's terminated, strcache_add_len has to make a
    3242             temp copy on the stack otherwise. */
    3243          char saved = *p;
    3244          if (!saved)
    3245            *p = '\0';
    3246          name = strcache_add_len (q, p - q);
    3247          if (saved)
    3248            *p = saved;
    3249        }
    3250 #endif /* CONFIG_WITH_VALUE_LENGTH */
    3251 
    3252       /* Add it to the front of the chain.  */
    3253 #ifndef CONFIG_WITH_ALLOC_CACHES
    3254       new1 = xmalloc (size);
    3255       memset (new1, '\0', size);
    3256 #else
    3257       new1 = (struct nameseq *) alloccache_calloc (cache);
    3258 #endif
    3259       new1->name = name;
    3260       new1->next = new;
    3261       new = new1;
    3262     }
    3263 
    3264 #ifndef NO_ARCHIVES
    3265 
    3266   /* Look for multi-word archive references.
    3267      They are indicated by a elt ending with an unmatched `)' and
    3268      an elt further down the chain (i.e., previous in the file list)
    3269      with an unmatched `(' (e.g., "lib(mem").  */
    3270 
    3271   new1 = new;
    3272   lastnew1 = 0;
    3273   while (new1 != 0)
    3274     if (new1->name[0] != '('    /* Don't catch "(%)" and suchlike.  */
    3275         && new1->name[strlen (new1->name) - 1] == ')'
    3276         && strchr (new1->name, '(') == 0)
    3277       {
    3278         /* NEW1 ends with a `)' but does not contain a `('.
    3279            Look back for an elt with an opening `(' but no closing `)'.  */
    3280 
    3281         struct nameseq *n = new1->next, *lastn = new1;
    3282         char *paren = 0;
    3283         while (n != 0 && (paren = strchr (n->name, '(')) == 0)
    3284           {
    3285             lastn = n;
    3286             n = n->next;
    3287           }
    3288         if (n != 0
    3289             /* Ignore something starting with `(', as that cannot actually
    3290                be an archive-member reference (and treating it as such
    3291                results in an empty file name, which causes much lossage).  */
    3292             && n->name[0] != '(')
    3293           {
    3294             /* N is the first element in the archive group.
    3295                Its name looks like "lib(mem" (with no closing `)').  */
    3296 
    3297             char *libname;
    3298 
    3299             /* Copy "lib(" into LIBNAME.  */
    3300             ++paren;
    3301             libname = alloca (paren - n->name + 1);
    3302             memcpy (libname, n->name, paren - n->name);
    3303             libname[paren - n->name] = '\0';
    3304 
    3305             if (*paren == '\0')
    3306               {
    3307                 /* N was just "lib(", part of something like "lib( a b)".
    3308                    Edit it out of the chain and free its storage.  */
    3309                 lastn->next = n->next;
    3310 #ifndef CONFIG_WITH_ALLOC_CACHES
    3311                 free (n);
    3312 #else
    3313                 alloccache_free (cache, n);
    3314 #endif
    3315                 /* LASTN->next is the new stopping elt for the loop below.  */
    3316                 n = lastn->next;
    3317               }
    3318             else
    3319               {
    3320                 /* Replace N's name with the full archive reference.  */
    3321                 n->name = strcache_add (concat (libname, paren, ")"));
    3322               }
    3323 
    3324             if (new1->name[1] == '\0')
    3325               {
    3326                 /* NEW1 is just ")", part of something like "lib(a b )".
    3327                    Omit it from the chain and free its storage.  */
    3328                 if (lastnew1 == 0)
    3329                   new = new1->next;
    3330                 else
    3331                   lastnew1->next = new1->next;
    3332                 lastn = new1;
    3333                 new1 = new1->next;
    3334 #ifndef CONFIG_WITH_ALLOC_CACHES
    3335                 free (lastn);
    3336 #else
    3337                 alloccache_free (cache, lastn);
    3338 #endif
    3339               }
    3340             else
    3341               {
    3342                 /* Replace also NEW1->name, which already has closing `)'.  */
    3343                 new1->name = strcache_add (concat (libname, new1->name, ""));
    3344                 new1 = new1->next;
    3345               }
    3346 
    3347             /* Trace back from NEW1 (the end of the list) until N
    3348                (the beginning of the list), rewriting each name
    3349                with the full archive reference.  */
    3350 
    3351             while (new1 != n)
    3352               {
    3353                 new1->name = strcache_add (concat (libname, new1->name, ")"));
    3354                 lastnew1 = new1;
    3355                 new1 = new1->next;
    3356               }
    3357           }
    3358         else
    3359           {
    3360             /* No frobnication happening.  Just step down the list.  */
    3361             lastnew1 = new1;
    3362             new1 = new1->next;
    3363           }
    3364       }
    3365     else
    3366       {
    3367         lastnew1 = new1;
    3368         new1 = new1->next;
    3369       }
    3370 
    3371 #endif
    3372 
    3373   *stringp = p;
    3374   return new;
    3375 }
    3376 
    3377 
    33782978/* Find the next line of text in an eval buffer, combining continuation lines
    33792979   into one line.
     
    34123012    {
    34133013      int backslash = 0;
    3414       char *bol = eol;
    3415       char *p;
     3014      const char *bol = eol;
     3015      const char *p;
    34163016
    34173017      /* Find the next newline.  At EOS, stop.  */
    34183018#ifndef CONFIG_WITH_VALUE_LENGTH
    3419       eol = p = strchr (eol , '\n');
     3019      p = eol = strchr (eol , '\n');
    34203020#else
    34213021      p = (char *)memchr (eol, '\n', end - eol);
    34223022      assert (!memchr (eol, '\0', p != 0 ? p - eol : end - eol));
    3423       eol = p;
     3023      eol = (char *)p;
    34243024#endif
    34253025      if (!eol)
     
    39333533      if (home_dir != 0)
    39343534        {
    3935           char *new = xstrdup (concat (home_dir, "", name + 1));
     3535          char *new = xstrdup (concat (2, home_dir, name + 1));
    39363536          if (is_variable)
    39373537            free (home_dir);
     
    39523552            return xstrdup (pwent->pw_dir);
    39533553          else
    3954             return xstrdup (concat (pwent->pw_dir, "/", userend + 1));
     3554            return xstrdup (concat (3, pwent->pw_dir, "/", userend + 1));
    39553555        }
    39563556      else if (userend != 0)
     
    39623562}
    39633563
    3964 /* Given a chain of struct nameseq's describing a sequence of filenames,
    3965    in reverse of the intended order, return a new chain describing the
    3966    result of globbing the filenames.  The new chain is in forward order.
    3967    The links of the old chain are freed or used in the new chain.
    3968    Likewise for the names in the old chain.
     3564
     3565/* Parse a string into a sequence of filenames represented as a chain of
     3566   struct nameseq's and return that chain.  Optionally expand the strings via
     3567   glob().
     3568
     3569   The string is passed as STRINGP, the address of a string pointer.
     3570   The string pointer is updated to point at the first character
     3571   not parsed, which either is a null char or equals STOPCHAR.
    39693572
    39703573   SIZE is how big to construct chain elements.
    39713574   This is useful if we want them actually to be other structures
    3972    that have room for additional info.  */
    3973 
     3575   that have room for additional info.
     3576
     3577   PREFIX, if non-null, is added to the beginning of each filename.
     3578
     3579   FLAGS allows one or more of the following bitflags to be set:
     3580        PARSEFS_NOSTRIP - Do no strip './'s off the beginning
     3581        PARSEFS_NOAR    - Do not check filenames for archive references
     3582        PARSEFS_NOGLOB  - Do not expand globbing characters
     3583        PARSEFS_EXISTS  - Only return globbed files that actually exist
     3584                          (cannot also set NOGLOB)
     3585        PARSEFS_NOCACHE - Do not add filenames to the strcache (caller frees)
     3586  */
     3587
     3588void *
     3589parse_file_seq (char **stringp, unsigned int size, int stopchar,
     3590                const char *prefix, int flags
     3591                IF_WITH_ALLOC_CACHES_PARAM(struct alloccache *alloc_cache) )
     3592{
     3593  extern void dir_setup_glob (glob_t *glob);
     3594
     3595  /* tmp points to tmpbuf after the prefix, if any.
     3596     tp is the end of the buffer. */
     3597  static char *tmpbuf = NULL;
     3598  static int tmpbuf_len = 0;
     3599
     3600  int cachep = (! (flags & PARSEFS_NOCACHE));
     3601
     3602  struct nameseq *new = 0;
     3603  struct nameseq **newp = &new;
    39743604#ifndef CONFIG_WITH_ALLOC_CACHES
    3975 struct nameseq *
    3976 multi_glob (struct nameseq *chain, unsigned int size)
    3977 #else
    3978 struct nameseq *
    3979 multi_glob (struct nameseq *chain, struct alloccache *cache)
    3980 #endif
    3981 {
    3982   void dir_setup_glob (glob_t *);
    3983   struct nameseq *new = 0;
    3984   struct nameseq *old;
    3985   struct nameseq *nexto;
     3605#define NEWELT(_n)  do { \
     3606                        const char *__n = (_n); \
     3607                        *newp = xcalloc (size); \
     3608                        (*newp)->name = (cachep ? strcache_add (__n) : xstrdup (__n)); \
     3609                        newp = &(*newp)->next; \
     3610                    } while(0)
     3611#else
     3612# define NEWELT(_n)  do { \
     3613                        const char *__n = (_n); \
     3614                        *newp = alloccache_calloc (alloc_cache); \
     3615                        (*newp)->name = (cachep ? strcache_add (__n) : xstrdup (__n)); \
     3616                        newp = &(*newp)->next; \
     3617                    } while(0)
     3618#endif
     3619
     3620  char *p;
    39863621  glob_t gl;
    3987 #if defined(KMK) || defined(__EMX__) /* speed optimization */
    3988   int rc;
    3989 #endif
    3990 
    3991   dir_setup_glob (&gl);
    3992 
    3993   for (old = chain; old != 0; old = nexto)
     3622  char *tp;
     3623
     3624#ifdef VMS
     3625# define VMS_COMMA ','
     3626#else
     3627# define VMS_COMMA 0
     3628#endif
     3629
     3630  if (size < sizeof (struct nameseq))
     3631    size = sizeof (struct nameseq);
     3632
     3633  if (! (flags & PARSEFS_NOGLOB))
     3634    dir_setup_glob (&gl);
     3635
     3636  /* Get enough temporary space to construct the largest possible target.  */
     3637  {
     3638    int l = strlen (*stringp) + 1;
     3639    if (l > tmpbuf_len)
     3640      {
     3641        tmpbuf = xrealloc (tmpbuf, l);
     3642        tmpbuf_len = l;
     3643      }
     3644  }
     3645  tp = tmpbuf;
     3646
     3647  /* Parse STRING.  P will always point to the end of the parsed content.  */
     3648  p = *stringp;
     3649  while (1)
    39943650    {
    3995       const char *gname;
     3651      const char *name;
     3652      const char **nlist = 0;
     3653      char *tildep = 0;
    39963654#ifndef NO_ARCHIVES
    39973655      char *arname = 0;
    39983656      char *memname = 0;
    39993657#endif
    4000       nexto = old->next;
    4001       gname = old->name;
    4002 
    4003       if (gname[0] == '~')
     3658      char *s;
     3659      int nlen;
     3660      int i;
     3661
     3662      /* Skip whitespace; at the end of the string or STOPCHAR we're done.  */
     3663      p = next_token (p);
     3664      if (*p == '\0' || *p == stopchar)
     3665        break;
     3666
     3667      /* There are names left, so find the end of the next name.
     3668         Throughout this iteration S points to the start.  */
     3669      s = p;
     3670      p = find_char_unquote (p, stopchar, VMS_COMMA, 1, 0);
     3671#ifdef VMS
     3672        /* convert comma separated list to space separated */
     3673      if (p && *p == ',')
     3674        *p =' ';
     3675#endif
     3676#ifdef _AMIGA
     3677      if (stopchar == ':' && p && *p == ':'
     3678          && !(isspace ((unsigned char)p[1]) || !p[1]
     3679               || isspace ((unsigned char)p[-1])))
     3680        p = find_char_unquote (p+1, stopchar, VMS_COMMA, 1, 0);
     3681#endif
     3682#ifdef HAVE_DOS_PATHS
     3683    /* For DOS paths, skip a "C:\..." or a "C:/..." until we find the
     3684       first colon which isn't followed by a slash or a backslash.
     3685       Note that tokens separated by spaces should be treated as separate
     3686       tokens since make doesn't allow path names with spaces */
     3687    if (stopchar == ':')
     3688      while (p != 0 && !isspace ((unsigned char)*p) &&
     3689             (p[1] == '\\' || p[1] == '/') && isalpha ((unsigned char)p[-1]))
     3690        p = find_char_unquote (p + 1, stopchar, VMS_COMMA, 1, 0);
     3691#endif
     3692      if (p == 0)
     3693        p = s + strlen (s);
     3694
     3695      /* Strip leading "this directory" references.  */
     3696      if (! (flags & PARSEFS_NOSTRIP))
     3697#ifdef VMS
     3698        /* Skip leading `[]'s.  */
     3699        while (p - s > 2 && s[0] == '[' && s[1] == ']')
     3700#else
     3701        /* Skip leading `./'s.  */
     3702        while (p - s > 2 && s[0] == '.' && s[1] == '/')
     3703#endif
     3704          {
     3705            /* Skip "./" and all following slashes.  */
     3706            s += 2;
     3707            while (*s == '/')
     3708              ++s;
     3709          }
     3710
     3711      /* Extract the filename just found, and skip it.
     3712         Set NAME to the string, and NLEN to its length.  */
     3713
     3714      if (s == p)
     3715        {
     3716        /* The name was stripped to empty ("./"). */
     3717#if defined(VMS)
     3718          continue;
     3719#elif defined(_AMIGA)
     3720          /* PDS-- This cannot be right!! */
     3721          tp[0] = '\0';
     3722          nlen = 0;
     3723#else
     3724          tp[0] = '.';
     3725          tp[1] = '/';
     3726          tp[2] = '\0';
     3727          nlen = 2;
     3728#endif
     3729        }
     3730      else
    40043731        {
    4005           char *newname = tilde_expand (old->name);
    4006           if (newname != 0)
    4007             gname = newname;
     3732#ifdef VMS
     3733/* VMS filenames can have a ':' in them but they have to be '\'ed but we need
     3734 *  to remove this '\' before we can use the filename.
     3735 * xstrdup called because S may be read-only string constant.
     3736 */
     3737          char *n = tp;
     3738          while (s < p)
     3739            {
     3740              if (s[0] == '\\' && s[1] == ':')
     3741                ++s;
     3742              *(n++) = *(s++);
     3743            }
     3744          n[0] = '\0';
     3745          nlen = strlen (tp);
     3746#else
     3747          nlen = p - s;
     3748          memcpy (tp, s, nlen);
     3749          tp[nlen] = '\0';
     3750#endif
     3751        }
     3752
     3753      /* At this point, TP points to the element and NLEN is its length.  */
     3754
     3755#ifndef NO_ARCHIVES
     3756      /* If this is the start of an archive group that isn't complete, set up
     3757         to add the archive prefix for future files.  A file list like:
     3758         "libf.a(x.o y.o z.o)" needs to be expanded as:
     3759         "libf.a(x.o) libf.a(y.o) libf.a(z.o)"
     3760
     3761         TP == TMP means we're not already in an archive group.  Ignore
     3762         something starting with `(', as that cannot actually be an
     3763         archive-member reference (and treating it as such results in an empty
     3764         file name, which causes much lossage).  Also if it ends in ")" then
     3765         it's a complete reference so we don't need to treat it specially.
     3766
     3767         Finally, note that archive groups must end with ')' as the last
     3768         character, so ensure there's some word ending like that before
     3769         considering this an archive group.  */
     3770      if (! (flags & PARSEFS_NOAR)
     3771          && tp == tmpbuf && tp[0] != '(' && tp[nlen-1] != ')')
     3772        {
     3773          char *n = strchr (tp, '(');
     3774          if (n)
     3775            {
     3776              /* This looks like the first element in an open archive group.
     3777                 A valid group MUST have ')' as the last character.  */
     3778              const char *e = p + nlen;
     3779              do
     3780                {
     3781                  e = next_token (e);
     3782                  /* Find the end of this word.  We don't want to unquote and
     3783                     we don't care about quoting since we're looking for the
     3784                     last char in the word. */
     3785                  while (*e != '\0' && *e != stopchar && *e != VMS_COMMA
     3786                         && ! isblank ((unsigned char) *e))
     3787                    ++e;
     3788                  if (e[-1] == ')')
     3789                    {
     3790                      /* Found the end, so this is the first element in an
     3791                         open archive group.  It looks like "lib(mem".
     3792                         Reset TP past the open paren.  */
     3793                      nlen -= (n + 1) - tp;
     3794                      tp = n + 1;
     3795
     3796                      /* If we have just "lib(", part of something like
     3797                         "lib( a b)", go to the next item.  */
     3798                      if (! nlen)
     3799                        continue;
     3800
     3801                      /* We can stop looking now.  */
     3802                      break;
     3803                    }
     3804                }
     3805              while (*e != '\0');
     3806            }
     3807        }
     3808
     3809      /* If we are inside an archive group, make sure it has an end.  */
     3810      if (tp > tmpbuf)
     3811        {
     3812          if (tp[nlen-1] == ')')
     3813            {
     3814              /* This is the natural end; reset TP.  */
     3815              tp = tmpbuf;
     3816
     3817              /* This is just ")", something like "lib(a b )": skip it.  */
     3818              if (nlen == 1)
     3819                continue;
     3820            }
     3821          else
     3822            {
     3823              /* Not the end, so add a "fake" end.  */
     3824              tp[nlen++] = ')';
     3825              tp[nlen] = '\0';
     3826            }
     3827        }
     3828#endif
     3829
     3830      /* If we're not globbing we're done: add it to the end of the chain.
     3831         Go to the next item in the string.  */
     3832      if (flags & PARSEFS_NOGLOB)
     3833        {
     3834          NEWELT (concat (2, prefix, tp));
     3835          continue;
     3836        }
     3837
     3838      /* If we get here we know we're doing glob expansion.
     3839         TP is a string in tmpbuf.  NLEN is no longer used.
     3840         We may need to do more work: after this NAME will be set.  */
     3841      name = tp;
     3842
     3843      /* Expand tilde if applicable.  */
     3844      if (tp[0] == '~')
     3845        {
     3846          tildep = tilde_expand (tp);
     3847          if (tildep != 0)
     3848            name = tildep;
    40083849        }
    40093850
    40103851#ifndef NO_ARCHIVES
    4011       if (ar_name (gname))
     3852      /* If NAME is an archive member reference replace it with the archive
     3853         file name, and save the member name in MEMNAME.  We will glob on the
     3854         archive name and then reattach MEMNAME later.  */
     3855      if (! (flags & PARSEFS_NOAR) && ar_name (name))
    40123856        {
    4013           /* OLD->name is an archive member reference.  Replace it with the
    4014              archive file name, and save the member name in MEMNAME.  We will
    4015              glob on the archive name and then reattach MEMNAME later.  */
    4016           ar_parse_name (gname, &arname, &memname);
    4017           gname = arname;
     3857          ar_parse_name (name, &arname, &memname);
     3858          name = arname;
    40183859        }
    40193860#endif /* !NO_ARCHIVES */
    40203861
    4021 #if defined(KMK) || defined(__EMX__) /* speed optimization */
    4022       if (!strpbrk(gname, "*?["))
    4023         {
    4024           gl.gl_pathc = 1;
    4025           gl.gl_pathv = (char **)&gname;
    4026           rc = 0;
    4027         }
    4028       else
    4029         rc = glob (gname, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl);
    4030       switch (rc)
    4031 #else
    4032       switch (glob (gname, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
    4033 #endif
     3862      switch (glob (name, GLOB_NOSORT|GLOB_ALTDIRFUNC, NULL, &gl))
    40343863        {
    4035         case 0:                 /* Success.  */
    4036           {
    4037             int i = gl.gl_pathc;
    4038             while (i-- > 0)
    4039               {
    4040 #ifndef NO_ARCHIVES
    4041                 if (memname != 0)
    4042                   {
    4043                     /* Try to glob on MEMNAME within the archive.  */
    4044                     struct nameseq *found
    4045                       = ar_glob (gl.gl_pathv[i], memname, size);
    4046                     if (! found)
    4047                       {
    4048                         /* No matches.  Use MEMNAME as-is.  */
    4049                         unsigned int alen = strlen (gl.gl_pathv[i]);
    4050                         unsigned int mlen = strlen (memname);
    4051                         char *name;
    4052                         struct nameseq *elt = xmalloc (size);
    4053                         memset (elt, '\0', size);
    4054 
    4055                         name = alloca (alen + 1 + mlen + 2);
    4056                         memcpy (name, gl.gl_pathv[i], alen);
    4057                         name[alen] = '(';
    4058                         memcpy (name+alen+1, memname, mlen);
    4059                         name[alen + 1 + mlen] = ')';
    4060                         name[alen + 1 + mlen + 1] = '\0';
    4061                         elt->name = strcache_add (name);
    4062                         elt->next = new;
    4063                         new = elt;
    4064                       }
    4065                     else
    4066                       {
    4067                         /* Find the end of the FOUND chain.  */
    4068                         struct nameseq *f = found;
    4069                         while (f->next != 0)
    4070                           f = f->next;
    4071 
    4072                         /* Attach the chain being built to the end of the FOUND
    4073                            chain, and make FOUND the new NEW chain.  */
    4074                         f->next = new;
    4075                         new = found;
    4076                       }
    4077                   }
    4078                 else
    4079 #endif /* !NO_ARCHIVES */
    4080                   {
    4081 #ifndef CONFIG_WITH_ALLOC_CACHES
    4082                     struct nameseq *elt = xmalloc (size);
    4083                     memset (elt, '\0', size);
    4084 #else
    4085                     struct nameseq *elt = alloccache_calloc (cache);
    4086 #endif
    4087                     elt->name = strcache_add (gl.gl_pathv[i]);
    4088                     elt->next = new;
    4089                     new = elt;
    4090                   }
    4091               }
    4092 #if defined(KMK) || defined(__EMX__) /* speed optimization */
    4093             if (gl.gl_pathv != (char **)&gname)
    4094 #endif
    4095             globfree (&gl);
    4096 #ifndef CONFIG_WITH_ALLOC_CACHES
    4097             free (old);
    4098 #else
    4099             alloccache_free (cache, old);
    4100 #endif
    4101             break;
    4102           }
    4103 
    41043864        case GLOB_NOSPACE:
    41053865          fatal (NILF, _("virtual memory exhausted"));
    4106           break;
     3866
     3867        case 0:
     3868          /* Success.  */
     3869          i = gl.gl_pathc;
     3870          nlist = (const char **)gl.gl_pathv;
     3871          break;
     3872
     3873        case GLOB_NOMATCH:
     3874          /* If we want only existing items, skip this one.  */
     3875          if (flags & PARSEFS_EXISTS)
     3876            {
     3877              i = 0;
     3878              break;
     3879            }
     3880          /* FALLTHROUGH */
    41073881
    41083882        default:
    4109           old->next = new;
    4110           new = old;
    4111           break;
     3883          /* By default keep this name.  */
     3884          i = 1;
     3885          nlist = &name;
     3886          break;
    41123887        }
     3888
     3889      /* For each matched element, add it to the list.  */
     3890      while (i-- > 0)
     3891#ifndef NO_ARCHIVES
     3892        if (memname != 0)
     3893          {
     3894            /* Try to glob on MEMNAME within the archive.  */
     3895            struct nameseq *found = ar_glob (nlist[i], memname, size);
     3896            if (! found)
     3897              /* No matches.  Use MEMNAME as-is.  */
     3898              NEWELT (concat (5, prefix, nlist[i], "(", memname, ")"));
     3899            else
     3900              {
     3901                /* We got a chain of items.  Attach them.  */
     3902                (*newp)->next = found;
     3903
     3904                /* Find and set the new end.  Massage names if necessary.  */
     3905                while (1)
     3906                  {
     3907                    if (! cachep)
     3908                      found->name = xstrdup (concat (2, prefix, name));
     3909                    else if (prefix)
     3910                      found->name = strcache_add (concat (2, prefix, name));
     3911
     3912                    if (found->next == 0)
     3913                      break;
     3914
     3915                    found = found->next;
     3916                  }
     3917                newp = &found->next;
     3918              }
     3919          }
     3920        else
     3921#endif /* !NO_ARCHIVES */
     3922          NEWELT (concat (2, prefix, nlist[i]));
     3923
     3924        globfree (&gl);
    41133925
    41143926#ifndef NO_ARCHIVES
     
    41163928        free (arname);
    41173929#endif
     3930
     3931      if (tildep)
     3932        free (tildep);
    41183933    }
    41193934
     3935  *stringp = p;
    41203936  return new;
    41213937}
Note: See TracChangeset for help on using the changeset viewer.