Ignore:
Timestamp:
Jun 20, 2012, 12:44:52 AM (13 years ago)
Author:
bird
Message:

gnumake/current -> 3.82-cvs.

Location:
vendor/gnumake/current
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • vendor/gnumake/current

    • Property svn:ignore deleted
  • vendor/gnumake/current/read.c

    r1989 r2596  
    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
     
    5555    FILE *fp;           /* File, or NULL if this is an internal buffer.  */
    5656    struct floc floc;   /* Info on the file in fp (if any).  */
     57  };
     58
     59/* Track the modifiers we can have on variable assignments */
     60
     61struct vmodifiers
     62  {
     63    unsigned int assign_v:1;
     64    unsigned int define_v:1;
     65    unsigned int undefine_v:1;
     66    unsigned int export_v:1;
     67    unsigned int override_v:1;
     68    unsigned int private_v:1;
    5769  };
    5870
     
    123135
    124136static int eval_makefile (const char *filename, int flags);
    125 static int eval (struct ebuffer *buffer, int flags);
     137static void eval (struct ebuffer *buffer, int flags);
    126138
    127139static long readline (struct ebuffer *ebuf);
    128 static void do_define (char *name, unsigned int namelen,
    129                        enum variable_origin origin, struct ebuffer *ebuf);
     140static void do_undefine (char *name, enum variable_origin origin,
     141                         struct ebuffer *ebuf);
     142static struct variable *do_define (char *name, enum variable_origin origin,
     143                                   struct ebuffer *ebuf);
    130144static int conditional_line (char *line, int len, const struct floc *flocp);
    131145static void record_files (struct nameseq *filenames, const char *pattern,
    132                           const char *pattern_percent, struct dep *deps,
     146                          const char *pattern_percent, char *depstr,
    133147                          unsigned int cmds_started, char *commands,
    134148                          unsigned int commands_idx, int two_colon,
    135149                          const struct floc *flocp);
    136150static void record_target_var (struct nameseq *filenames, char *defn,
    137                                enum variable_origin origin, int enabled,
     151                               enum variable_origin origin,
     152                               struct vmodifiers *vmod,
    138153                               const struct floc *flocp);
    139154static enum make_word_type get_next_mword (char *buffer, char *delim,
     
    144159
    145160
     161/* Compare a word, both length and contents.
     162   P must point to the word to be tested, and WLEN must be the length.
     163*/
     164#define word1eq(s)      (wlen == sizeof(s)-1 && strneq (s, p, sizeof(s)-1))
     165
     166
     167
    146168/* Read in all the makefiles and return the chain of their names.  */
    147169
     
    154176     we will be reading. */
    155177
    156   define_variable ("MAKEFILE_LIST", sizeof ("MAKEFILE_LIST")-1, "", o_file, 0);
     178  define_variable_cname ("MAKEFILE_LIST", "", o_file, 0);
    157179
    158180  DB (DB_BASIC, (_("Reading makefiles...\n")));
     
    249271              struct dep *d = alloc_dep ();
    250272              d->file = enter_file (strcache_add (*p));
    251               d->file->dontcare = 1;
     273              d->dontcare = 1;
    252274              /* Tell update_goal_chain to bail out as soon as this file is
    253275                 made, and main not to die if we can't make this file.  */
     
    305327  char *expanded = 0;
    306328  int makefile_errno;
    307   int r;
    308329
    309330  filename = strcache_add (filename);
     
    348369      for (i = 0; include_directories[i] != 0; ++i)
    349370        {
    350           const char *included = concat (include_directories[i], "/", filename);
     371          const char *included = concat (3, include_directories[i],
     372                                         "/", filename);
    351373          ebuf.fp = fopen (included, "r");
    352374          if (ebuf.fp)
     
    368390  deps->changed = flags;
    369391  if (flags & RM_DONTCARE)
    370     deps->file->dontcare = 1;
     392    deps->dontcare = 1;
    371393
    372394  if (expanded)
     
    384406    }
    385407
     408  /* Set close-on-exec to avoid leaking the makefile to children, such as
     409     $(shell ...).  */
     410#ifdef HAVE_FILENO
     411  CLOSE_ON_EXEC (fileno (ebuf.fp));
     412#endif
     413
    386414  /* Add this makefile to the list. */
    387415  do_variable_definition (&ebuf.floc, "MAKEFILE_LIST", filename, o_file,
     
    396424  reading_file = &ebuf.floc;
    397425
    398   r = eval (&ebuf, !(flags & RM_NO_DEFAULT_GOAL));
     426  eval (&ebuf, !(flags & RM_NO_DEFAULT_GOAL));
    399427
    400428  reading_file = curfile;
     
    404432  free (ebuf.bufstart);
    405433  alloca (0);
    406   return r;
     434
     435  return 1;
    407436}
    408437
    409 int
     438void
    410439eval_buffer (char *buffer)
    411440{
     
    414443  struct conditionals new;
    415444  const struct floc *curfile;
    416   int r;
    417445
    418446  /* Evaluate the buffer */
     
    422450  ebuf.fp = NULL;
    423451
    424   ebuf.floc = *reading_file;
     452  if (reading_file)
     453    ebuf.floc = *reading_file;
     454  else
     455    ebuf.floc.filenm = NULL;
    425456
    426457  curfile = reading_file;
     
    429460  saved = install_conditionals (&new);
    430461
    431   r = eval (&ebuf, 1);
     462  eval (&ebuf, 1);
    432463
    433464  restore_conditionals (saved);
     
    436467
    437468  alloca (0);
    438   return r;
    439469}
    440470
    441471
     472/* Check LINE to see if it's a variable assignment or undefine.
     473
     474   It might use one of the modifiers "export", "override", "private", or it
     475   might be one of the conditional tokens like "ifdef", "include", etc.
     476
     477   If it's not a variable assignment or undefine, VMOD.V_ASSIGN is 0.
     478   Returns LINE.
     479
     480   Returns a pointer to the first non-modifier character, and sets VMOD
     481   based on the modifiers found if any, plus V_ASSIGN is 1.
     482 */
     483static char *
     484parse_var_assignment (const char *line, struct vmodifiers *vmod)
     485{
     486  const char *p;
     487  memset (vmod, '\0', sizeof (*vmod));
     488
     489  /* Find the start of the next token.  If there isn't one we're done.  */
     490  line = next_token (line);
     491  if (*line == '\0')
     492    return (char *)line;
     493
     494  p = line;
     495  while (1)
     496    {
     497      int wlen;
     498      const char *p2;
     499      enum variable_flavor flavor;
     500
     501      p2 = parse_variable_definition (p, &flavor);
     502
     503      /* If this is a variable assignment, we're done.  */
     504      if (p2)
     505        break;
     506
     507      /* It's not a variable; see if it's a modifier.  */
     508      p2 = end_of_token (p);
     509      wlen = p2 - p;
     510
     511      if (word1eq ("export"))
     512        vmod->export_v = 1;
     513      else if (word1eq ("override"))
     514        vmod->override_v = 1;
     515      else if (word1eq ("private"))
     516        vmod->private_v = 1;
     517      else if (word1eq ("define"))
     518        {
     519          /* We can't have modifiers after 'define' */
     520          vmod->define_v = 1;
     521          p = next_token (p2);
     522          break;
     523        }
     524      else if (word1eq ("undefine"))
     525        {
     526          /* We can't have modifiers after 'undefine' */
     527          vmod->undefine_v = 1;
     528          p = next_token (p2);
     529          break;
     530        }
     531      else
     532        /* Not a variable or modifier: this is not a variable assignment.  */
     533        return (char *)line;
     534
     535      /* It was a modifier.  Try the next word.  */
     536      p = next_token (p2);
     537      if (*p == '\0')
     538        return (char *)line;
     539    }
     540
     541  /* Found a variable assignment or undefine.  */
     542  vmod->assign_v = 1;
     543  return (char *)p;
     544}
     545
     546
    442547
    443548/* Read file FILENAME as a makefile and add its contents to the data base.
     
    445550   SET_DEFAULT is true if we are allowed to set the default goal.  */
    446551
    447 
    448 static int
     552static void
    449553eval (struct ebuffer *ebuf, int set_default)
    450554{
     
    458562  int no_targets = 0;           /* Set when reading a rule without targets.  */
    459563  struct nameseq *filenames = 0;
    460   struct dep *deps = 0;
     564  char *depstr = 0;
    461565  long nlines = 0;
    462566  int two_colon = 0;
     
    472576        {                                                                     \
    473577          fi.lineno = tgts_started;                                           \
    474           record_files (filenames, pattern, pattern_percent, deps          \
     578          record_files (filenames, pattern, pattern_percent, depstr,          \
    475579                        cmds_started, commands, commands_idx, two_colon,      \
    476580                        &fi);                                                 \
     581          filenames = 0;                                                      \
    477582        }                                                                     \
    478       filenames = 0;                                                          \
    479583      commands_idx = 0;                                                       \
    480584      no_targets = 0;                                                         \
     
    507611      char *p;
    508612      char *p2;
    509 
     613      struct vmodifiers vmod;
     614
     615      /* At the top of this loop, we are starting a brand new line.  */
    510616      /* Grab the next line to be evaluated */
    511617      ebuf->floc.lineno += nlines;
     
    532638
    533639          /* If there is no preceding rule line, don't treat this line
    534              as a command, even though it begins with a tab character.
     640             as a command, even though it begins with a recipe prefix.
    535641             SunOS 4 make appears to behave this way.  */
    536642
     
    571677        }
    572678
    573       /* This line is not a shell command line.  Don't worry about tabs.
     679      /* This line is not a shell command line.  Don't worry about whitespace.
    574680         Get more space if we need it; we don't need to preserve the current
    575681         contents of the buffer.  */
     
    580686          if (collapsed)
    581687            free (collapsed);
     688          /* Don't need xrealloc: we don't need to preserve the content.  */
    582689          collapsed = xmalloc (collapsed_length);
    583690        }
     
    587694      remove_comments (collapsed);
    588695
    589       /* Compare a word, both length and contents. */
    590 #define word1eq(s)      (wlen == sizeof(s)-1 && strneq (s, p, sizeof(s)-1))
     696      /* Get rid if starting space (including formfeed, vtab, etc.)  */
    591697      p = collapsed;
    592698      while (isspace ((unsigned char)*p))
    593         ++p;
    594 
    595       if (*p == '\0')
    596         /* This line is completely empty--ignore it.  */
    597         continue;
    598 
    599       /* Find the end of the first token.  Note we don't need to worry about
    600        * ":" here since we compare tokens by length (so "export" will never
    601        * be equal to "export:").
    602        */
    603       for (p2 = p+1; *p2 != '\0' && !isspace ((unsigned char)*p2); ++p2)
    604         ;
    605       wlen = p2 - p;
    606 
    607       /* Find the start of the second token.  If it looks like a target or
    608          variable definition it can't be a preprocessor token so skip
    609          them--this allows variables/targets named `ifdef', `export', etc. */
    610       while (isspace ((unsigned char)*p2))
    611         ++p2;
    612 
    613       if ((p2[0] == ':' || p2[0] == '+' || p2[0] == '=') && p2[1] == '\0')
     699        ++p;
     700
     701      /* See if this is a variable assignment.  We need to do this early, to
     702         allow variables with names like 'ifdef', 'export', 'private', etc.  */
     703      p = parse_var_assignment(p, &vmod);
     704      if (vmod.assign_v)
    614705        {
    615           /* It can't be a preprocessor token so skip it if we're ignoring */
    616           if (ignoring)
    617             continue;
    618 
    619           goto skip_conditionals;
    620         }
    621 
    622       /* We must first check for conditional and `define' directives before
    623          ignoring anything, since they control what we will do with
    624          following lines.  */
    625 
    626       if (!in_ignored_define)
    627         {
    628           int i = conditional_line (p, wlen, fstart);
    629           if (i != -2)
     706          struct variable *v;
     707          enum variable_origin origin = vmod.override_v ? o_override : o_file;
     708
     709          /* If we're ignoring then we're done now.  */
     710          if (ignoring)
    630711            {
    631               if (i == -1)
    632                 fatal (fstart, _("invalid syntax in conditional"));
    633 
    634               ignoring = i;
     712              if (vmod.define_v)
     713                in_ignored_define = 1;
    635714              continue;
    636715            }
    637         }
    638 
    639       if (word1eq ("endef"))
     716
     717          if (vmod.undefine_v)
     718          {
     719            do_undefine (p, origin, ebuf);
     720
     721            /* This line has been dealt with.  */
     722            goto rule_complete;
     723          }
     724          else if (vmod.define_v)
     725            v = do_define (p, origin, ebuf);
     726          else
     727            v = try_variable_definition (fstart, p, origin, 0);
     728
     729          assert (v != NULL);
     730
     731          if (vmod.export_v)
     732            v->export = v_export;
     733          if (vmod.private_v)
     734            v->private_var = 1;
     735
     736          /* This line has been dealt with.  */
     737          goto rule_complete;
     738        }
     739
     740      /* If this line is completely empty, ignore it.  */
     741      if (*p == '\0')
     742        continue;
     743
     744      p2 = end_of_token (p);
     745      wlen = p2 - p;
     746      p2 = next_token (p2);
     747
     748      /* If we're in an ignored define, skip this line (but maybe get out).  */
     749      if (in_ignored_define)
    640750        {
    641           if (!in_ignored_define)
    642             fatal (fstart, _("extraneous `endef'"));
    643           in_ignored_define = 0;
     751          /* See if this is an endef line (plus optional comment).  */
     752          if (word1eq ("endef") && (*p2 == '\0' || *p2 == '#'))
     753            in_ignored_define = 0;
     754
    644755          continue;
    645756        }
    646757
    647       if (word1eq ("define"))
     758      /* Check for conditional state changes.  */
     759      {
     760        int i = conditional_line (p, wlen, fstart);
     761        if (i != -2)
     762          {
     763            if (i == -1)
     764              fatal (fstart, _("invalid syntax in conditional"));
     765
     766            ignoring = i;
     767            continue;
     768          }
     769      }
     770
     771      /* Nothing to see here... move along.  */
     772      if (ignoring)
     773        continue;
     774
     775      /* Manage the "export" keyword used outside of variable assignment
     776         as well as "unexport".  */
     777      if (word1eq ("export") || word1eq ("unexport"))
    648778        {
    649           if (ignoring)
    650             in_ignored_define = 1;
    651           else
    652             {
    653               if (*p2 == '\0')
    654                 fatal (fstart, _("empty variable name"));
    655 
    656               /* Let the variable name be the whole rest of the line,
    657                  with trailing blanks stripped (comments have already been
    658                  removed), so it could be a complex variable/function
    659                  reference that might contain blanks.  */
    660               p = strchr (p2, '\0');
    661               while (isblank ((unsigned char)p[-1]))
    662                 --p;
    663               do_define (p2, p - p2, o_file, ebuf);
    664             }
    665           continue;
    666         }
    667 
    668       if (word1eq ("override"))
    669         {
     779          int exporting = *p == 'u' ? 0 : 1;
     780
     781          /* (un)export by itself causes everything to be (un)exported. */
    670782          if (*p2 == '\0')
    671             error (fstart, _("empty `override' directive"));
    672 
    673           if (strneq (p2, "define", 6)
    674               && (isblank ((unsigned char)p2[6]) || p2[6] == '\0'))
    675             {
    676               if (ignoring)
    677                 in_ignored_define = 1;
    678               else
    679                 {
    680                   p2 = next_token (p2 + 6);
    681                   if (*p2 == '\0')
    682                     fatal (fstart, _("empty variable name"));
    683 
    684                   /* Let the variable name be the whole rest of the line,
    685                      with trailing blanks stripped (comments have already been
    686                      removed), so it could be a complex variable/function
    687                      reference that might contain blanks.  */
    688                   p = strchr (p2, '\0');
    689                   while (isblank ((unsigned char)p[-1]))
    690                     --p;
    691                   do_define (p2, p - p2, o_override, ebuf);
    692                 }
    693             }
    694           else if (!ignoring
    695                    && !try_variable_definition (fstart, p2, o_override, 0))
    696             error (fstart, _("invalid `override' directive"));
    697 
    698           continue;
    699         }
    700 
    701       if (ignoring)
    702         /* Ignore the line.  We continue here so conditionals
    703            can appear in the middle of a rule.  */
    704         continue;
    705 
    706       if (word1eq ("export"))
    707         {
    708           /* 'export' by itself causes everything to be exported. */
    709           if (*p2 == '\0')
    710             export_all_variables = 1;
    711           else
    712             {
    713               struct variable *v;
    714 
    715               v = try_variable_definition (fstart, p2, o_file, 0);
    716               if (v != 0)
    717                 v->export = v_export;
    718               else
    719                 {
    720                   unsigned int l;
    721                   const char *cp;
    722                   char *ap;
    723 
    724                   /* Expand the line so we can use indirect and constructed
    725                      variable names in an export command.  */
    726                   cp = ap = allocated_variable_expand (p2);
    727 
    728                   for (p = find_next_token (&cp, &l); p != 0;
    729                        p = find_next_token (&cp, &l))
    730                     {
    731                       v = lookup_variable (p, l);
    732                       if (v == 0)
    733                         v = define_variable_loc (p, l, "", o_file, 0, fstart);
    734                       v->export = v_export;
    735                     }
    736 
    737                   free (ap);
    738                 }
    739             }
    740           goto rule_complete;
    741         }
    742 
    743       if (word1eq ("unexport"))
    744         {
    745           if (*p2 == '\0')
    746             export_all_variables = 0;
     783            export_all_variables = exporting;
    747784          else
    748785            {
    749786              unsigned int l;
    750               struct variable *v;
    751787              const char *cp;
    752788              char *ap;
    753789
    754790              /* Expand the line so we can use indirect and constructed
    755                  variable names in an unexport command.  */
     791                 variable names in an (un)export command.  */
    756792              cp = ap = allocated_variable_expand (p2);
    757793
     
    759795                   p = find_next_token (&cp, &l))
    760796                {
    761                   v = lookup_variable (p, l);
     797                  struct variable *v = lookup_variable (p, l);
    762798                  if (v == 0)
    763799                    v = define_variable_loc (p, l, "", o_file, 0, fstart);
    764 
    765                   v->export = v_noexport;
     800                  v->export = exporting ? v_export : v_noexport;
    766801                }
    767802
     
    771806        }
    772807
    773  skip_conditionals:
     808      /* Handle the special syntax for vpath.  */
    774809      if (word1eq ("vpath"))
    775810        {
     
    781816          if (p != 0)
    782817            {
    783               vpat = savestring (p, l);
     818              vpat = xstrndup (p, l);
    784819              p = find_next_token (&cp, &l);
    785820              /* No searchpath means remove all previous
     
    796831        }
    797832
     833      /* Handle include and variants.  */
    798834      if (word1eq ("include") || word1eq ("-include") || word1eq ("sinclude"))
    799835        {
     
    816852            }
    817853
    818           /* Parse the list of file names.  */
     854          /* Parse the list of file names.  Don't expand archive references!  */
    819855          p2 = p;
    820           files = multi_glob (parse_file_seq (&p2, '\0',
    821                                               sizeof (struct nameseq),
    822                                               1),
    823                               sizeof (struct nameseq));
     856          files = PARSE_FILE_SEQ (&p2, struct nameseq, '\0', NULL,
     857                                  PARSEFS_NOAR);
    824858          free (p);
    825859
     
    839873              int r;
    840874
    841               free (files);
     875              free_ns (files);
    842876              files = next;
    843877
    844               r = eval_makefile (name, (RM_INCLUDED | RM_NO_TILDE
    845                                         | (noerror ? RM_DONTCARE : 0)));
     878              r = eval_makefile (name,
     879                                 (RM_INCLUDED | RM_NO_TILDE
     880                                  | (noerror ? RM_DONTCARE : 0)
     881                                  | (set_default ? 0 : RM_NO_DEFAULT_GOAL)));
    846882              if (!r && !noerror)
    847883                error (fstart, "%s: %s", name, strerror (errno));
     
    853889          goto rule_complete;
    854890        }
    855 
    856       if (try_variable_definition (fstart, p, o_file, 0))
    857         /* This line has been dealt with.  */
    858         goto rule_complete;
    859891
    860892      /* This line starts with a tab but was not caught above because there
     
    876908      {
    877909        enum make_word_type wtype;
    878         enum variable_origin v_origin;
    879         int exported;
    880910        char *cmdleft, *semip, *lb_next;
    881911        unsigned int plen = 0;
     
    10091039           looking for targets.  */
    10101040        *colonp = '\0';
    1011         filenames = multi_glob (parse_file_seq (&p2, '\0',
    1012                                                 sizeof (struct nameseq),
    1013                                                 1),
    1014                                 sizeof (struct nameseq));
     1041        filenames = PARSE_FILE_SEQ (&p2, struct nameseq, '\0', NULL, 0);
    10151042        *p2 = ':';
    10161043
     
    10431070          }
    10441071
    1045         /* See if it's an "override" or "export" keyword; if so see if what
    1046            comes after it looks like a variable definition.  */
    1047 
    1048         wtype = get_next_mword (p2, NULL, &p, &wlen);
    1049 
    1050         v_origin = o_file;
    1051         exported = 0;
    1052         if (wtype == w_static)
    1053           {
    1054             if (word1eq ("override"))
    1055               {
    1056                 v_origin = o_override;
    1057                 wtype = get_next_mword (p+wlen, NULL, &p, &wlen);
    1058               }
    1059             else if (word1eq ("export"))
    1060               {
    1061                 exported = 1;
    1062                 wtype = get_next_mword (p+wlen, NULL, &p, &wlen);
    1063               }
    1064           }
    1065 
    1066         if (wtype != w_eol)
    1067           wtype = get_next_mword (p+wlen, NULL, NULL, NULL);
    1068 
    1069         if (wtype == w_varassign)
     1072        p2 = parse_var_assignment (p2, &vmod);
     1073        if (vmod.assign_v)
    10701074          {
    10711075            /* If there was a semicolon found, add it back, plus anything
     
    10751079                unsigned int l = p - variable_buffer;
    10761080                *(--semip) = ';';
     1081                collapse_continuations (semip);
    10771082                variable_buffer_output (p2 + strlen (p2),
    10781083                                        semip, strlen (semip)+1);
    10791084                p = variable_buffer + l;
    10801085              }
    1081             record_target_var (filenames, p, v_origin, exported, fstart);
     1086            record_target_var (filenames, p2,
     1087                               vmod.override_v ? o_override : o_file,
     1088                               &vmod, fstart);
    10821089            filenames = 0;
    10831090            continue;
     
    11111118        while (p != 0 && p[-1] == '\\')
    11121119          {
    1113             register char *q = &p[-1];
    1114             register int backslash = 0;
     1120            char *q = &p[-1];
     1121            int backslash = 0;
    11151122            while (*q-- == '\\')
    11161123              backslash = !backslash;
     
    11551162          {
    11561163            struct nameseq *target;
    1157             target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
     1164            target = PARSE_FILE_SEQ (&p2, struct nameseq, ':', NULL,
     1165                                     PARSEFS_NOGLOB);
    11581166            ++p2;
    11591167            if (target == 0)
     
    11651173            if (pattern_percent == 0)
    11661174              fatal (fstart, _("target pattern contains no `%%'"));
    1167             free (target);
     1175            free_ns (target);
    11681176          }
    11691177        else
     
    11751183        strip_whitespace (&beg, &end);
    11761184
     1185        /* Put all the prerequisites here; they'll be parsed later.  */
    11771186        if (beg <= end && *beg != '\0')
    1178           {
    1179             /* Put all the prerequisites here; they'll be parsed later.  */
    1180             deps = alloc_dep ();
    1181             deps->name = strcache_add_len (beg, end - beg + 1);
    1182           }
     1187          depstr = xstrndup (beg, end - beg + 1);
    11831188        else
    1184           deps = 0;
     1189          depstr = 0;
    11851190
    11861191        commands_idx = 0;
     
    12161221           Because the target is not recorded until after ifeq directive is
    12171222           evaluated the .DEFAULT_GOAL does not contain foo yet as one
    1218            would expect. Because of this we have to move some of the logic
    1219            here.  */
    1220 
    1221         if (**default_goal_name == '\0' && set_default)
     1223           would expect. Because of this we have to move the logic here.  */
     1224
     1225        if (set_default && default_goal_var->value[0] == '\0')
    12221226          {
    12231227            const char *name;
     
    13001304    free (collapsed);
    13011305  free (commands);
    1302 
    1303   return 1;
    13041306}
    13051307
     
    13211323}
    13221324
     1325/* Execute a `undefine' directive.
     1326   The undefine line has already been read, and NAME is the name of
     1327   the variable to be undefined. */
     1328
     1329static void
     1330do_undefine (char *name, enum variable_origin origin, struct ebuffer *ebuf)
     1331{
     1332  char *p, *var;
     1333
     1334  /* Expand the variable name and find the beginning (NAME) and end.  */
     1335  var = allocated_variable_expand (name);
     1336  name = next_token (var);
     1337  if (*name == '\0')
     1338    fatal (&ebuf->floc, _("empty variable name"));
     1339  p = name + strlen (name) - 1;
     1340  while (p > name && isblank ((unsigned char)*p))
     1341    --p;
     1342  p[1] = '\0';
     1343
     1344  undefine_variable_global (name, p - name + 1, origin);
     1345  free (var);
     1346}
     1347
    13231348/* Execute a `define' directive.
    13241349   The first line has already been read, and NAME is the name of
    13251350   the variable to be defined.  The following lines remain to be read.  */
    13261351
    1327 static void
    1328 do_define (char *name, unsigned int namelen,
    1329            enum variable_origin origin, struct ebuffer *ebuf)
     1352static struct variable *
     1353do_define (char *name, enum variable_origin origin, struct ebuffer *ebuf)
    13301354{
     1355  struct variable *v;
     1356  enum variable_flavor flavor;
    13311357  struct floc defstart;
    1332   long nlines = 0;
    13331358  int nlevels = 1;
    13341359  unsigned int length = 100;
    13351360  char *definition = xmalloc (length);
    13361361  unsigned int idx = 0;
    1337   char *p;
    1338 
    1339   /* Expand the variable name.  */
    1340   char *var = alloca (namelen + 1);
    1341   memcpy (var, name, namelen);
    1342   var[namelen] = '\0';
    1343   var = variable_expand (var);
     1362  char *p, *var;
    13441363
    13451364  defstart = ebuf->floc;
    13461365
     1366  p = parse_variable_definition (name, &flavor);
     1367  if (p == NULL)
     1368    /* No assignment token, so assume recursive.  */
     1369    flavor = f_recursive;
     1370  else
     1371    {
     1372      if (*(next_token (p)) != '\0')
     1373        error (&defstart, _("extraneous text after `define' directive"));
     1374
     1375      /* Chop the string before the assignment token to get the name.  */
     1376      p[flavor == f_recursive ? -1 : -2] = '\0';
     1377    }
     1378
     1379  /* Expand the variable name and find the beginning (NAME) and end.  */
     1380  var = allocated_variable_expand (name);
     1381  name = next_token (var);
     1382  if (*name == '\0')
     1383    fatal (&defstart, _("empty variable name"));
     1384  p = name + strlen (name) - 1;
     1385  while (p > name && isblank ((unsigned char)*p))
     1386    --p;
     1387  p[1] = '\0';
     1388
     1389  /* Now read the value of the variable.  */
    13471390  while (1)
    13481391    {
    13491392      unsigned int len;
    13501393      char *line;
    1351 
    1352       nlines = readline (ebuf);
     1394      long nlines = readline (ebuf);
     1395
     1396      /* If there is nothing left to be eval'd, there's no 'endef'!!  */
     1397      if (nlines < 0)
     1398        fatal (&defstart, _("missing `endef', unterminated `define'"));
     1399
    13531400      ebuf->floc.lineno += nlines;
    1354 
    1355       /* If there is nothing left to eval, we're done. */
    1356       if (nlines < 0)
    1357         break;
    1358 
    13591401      line = ebuf->buffer;
    13601402
     
    13621404
    13631405      /* If the line doesn't begin with a tab, test to see if it introduces
    1364          another define, or ends one.  */
    1365 
    1366       /* Stop if we find an 'endef' */
     1406         another define, or ends one.  Stop if we find an 'endef' */
    13671407      if (line[0] != cmd_prefix)
    13681408        {
     
    13821422              p += 5;
    13831423              remove_comments (p);
    1384               if (*next_token (p) != '\0')
     1424              if (*(next_token (p)) != '\0')
    13851425                error (&ebuf->floc,
    1386                        _("Extraneous text after `endef' directive"));
     1426                       _("extraneous text after `endef' directive"));
    13871427
    13881428              if (--nlevels == 0)
    1389                 {
    1390                   /* Define the variable.  */
    1391                   if (idx == 0)
    1392                     definition[0] = '\0';
    1393                   else
    1394                     definition[idx - 1] = '\0';
    1395 
    1396                   /* Always define these variables in the global set.  */
    1397                   define_variable_global (var, strlen (var), definition,
    1398                                           origin, 1, &defstart);
    1399                   free (definition);
    1400                   return;
    1401                 }
     1429                break;
    14021430            }
    14031431        }
    14041432
    1405       /* Otherwise add this line to the variable definition.  */
     1433      /* Add this line to the variable definition.  */
    14061434      len = strlen (line);
    14071435      if (idx + len + 1 > length)
     
    14171445    }
    14181446
    1419   /* No `endef'!!  */
    1420   fatal (&defstart, _("missing `endef', unterminated `define'"));
    1421 
    1422   /* NOTREACHED */
    1423   return;
     1447  /* We've got what we need; define the variable.  */
     1448  if (idx == 0)
     1449    definition[0] = '\0';
     1450  else
     1451    definition[idx - 1] = '\0';
     1452
     1453  v = do_variable_definition (&defstart, name, definition, origin, flavor, 0);
     1454  free (definition);
     1455  free (var);
     1456  return (v);
    14241457}
    14251458
     
    16951728
    16961729
    1697 /* Remove duplicate dependencies in CHAIN.  */
    1698 
    1699 static unsigned long
    1700 dep_hash_1 (const void *key)
    1701 {
    1702   return_STRING_HASH_1 (dep_name ((struct dep const *) key));
    1703 }
    1704 
    1705 static unsigned long
    1706 dep_hash_2 (const void *key)
    1707 {
    1708   return_STRING_HASH_2 (dep_name ((struct dep const *) key));
    1709 }
    1710 
    1711 static int
    1712 dep_hash_cmp (const void *x, const void *y)
    1713 {
    1714   struct dep *dx = (struct dep *) x;
    1715   struct dep *dy = (struct dep *) y;
    1716   int cmp = strcmp (dep_name (dx), dep_name (dy));
    1717 
    1718   /* If the names are the same but ignore_mtimes are not equal, one of these
    1719      is an order-only prerequisite and one isn't.  That means that we should
    1720      remove the one that isn't and keep the one that is.  */
    1721 
    1722   if (!cmp && dx->ignore_mtime != dy->ignore_mtime)
    1723     dx->ignore_mtime = dy->ignore_mtime = 0;
    1724 
    1725   return cmp;
    1726 }
    1727 
    1728 
    1729 void
    1730 uniquize_deps (struct dep *chain)
    1731 {
    1732   struct hash_table deps;
    1733   register struct dep **depp;
    1734 
    1735   hash_init (&deps, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
    1736 
    1737   /* Make sure that no dependencies are repeated.  This does not
    1738      really matter for the purpose of updating targets, but it
    1739      might make some names be listed twice for $^ and $?.  */
    1740 
    1741   depp = &chain;
    1742   while (*depp)
    1743     {
    1744       struct dep *dep = *depp;
    1745       struct dep **dep_slot = (struct dep **) hash_find_slot (&deps, dep);
    1746       if (HASH_VACANT (*dep_slot))
    1747         {
    1748           hash_insert_at (&deps, dep, dep_slot);
    1749           depp = &dep->next;
    1750         }
    1751       else
    1752         {
    1753           /* Don't bother freeing duplicates.
    1754              It's dangerous and little benefit accrues.  */
    1755           *depp = dep->next;
    1756         }
    1757     }
    1758 
    1759   hash_free (&deps, 0);
    1760 }
    1761 
    17621730
    17631731/* Record target-specific variable values for files FILENAMES.
     
    17721740static void
    17731741record_target_var (struct nameseq *filenames, char *defn,
    1774                    enum variable_origin origin, int exported,
     1742                   enum variable_origin origin, struct vmodifiers *vmod,
    17751743                   const struct floc *flocp)
    17761744{
     
    17921760
    17931761      nextf = filenames->next;
    1794       free (filenames);
     1762      free_ns (filenames);
    17951763
    17961764      /* If it's a pattern target, then add it to the pattern-specific
     
    18041772          /* I don't think this can fail since we already determined it was a
    18051773             variable definition.  */
    1806           v = parse_variable_definition (&p->variable, defn);
     1774          v = assign_variable_definition (&p->variable, defn);
    18071775          assert (v != 0);
    18081776
     1777          v->origin = origin;
    18091778          if (v->flavor == f_simple)
    18101779            v->value = allocated_variable_expand (v->value);
     
    18341803          v = try_variable_definition (flocp, defn, origin, 1);
    18351804          if (!v)
    1836             error (flocp, _("Malformed target-specific variable definition"));
     1805            fatal (flocp, _("Malformed target-specific variable definition"));
    18371806          current_variable_set_list = global;
    18381807        }
    18391808
    18401809      /* Set up the variable to be *-specific.  */
    1841       v->origin = origin;
    18421810      v->per_target = 1;
    1843       v->export = exported ? v_export : v_default;
     1811      v->private_var = vmod->private_v;
     1812      v->export = vmod->export_v ? v_export : v_default;
    18441813
    18451814      /* If it's not an override, check to see if there was a command-line
    18461815         setting.  If so, reset the value.  */
    1847       if (origin != o_override)
     1816      if (v->origin != o_override)
    18481817        {
    18491818          struct variable *gv;
     
    18781847static void
    18791848record_files (struct nameseq *filenames, const char *pattern,
    1880               const char *pattern_percent, struct dep *deps,
     1849              const char *pattern_percent, char *depstr,
    18811850              unsigned int cmds_started, char *commands,
    18821851              unsigned int commands_idx, int two_colon,
    18831852              const struct floc *flocp)
    18841853{
    1885   struct nameseq *nextf;
    1886   int implicit = 0;
    1887   unsigned int max_targets = 0, target_idx = 0;
    1888   const char **targets = 0, **target_percents = 0;
    18891854  struct commands *cmds;
     1855  struct dep *deps;
     1856  const char *implicit_percent;
     1857  const char *name;
    18901858
    18911859  /* If we've already snapped deps, that means we're in an eval being
     
    18961864    fatal (flocp, _("prerequisites cannot be defined in recipes"));
    18971865
     1866  /* Determine if this is a pattern rule or not.  */
     1867  name = filenames->name;
     1868  implicit_percent = find_percent_cached (&name);
     1869
     1870  /* If there's a recipe, set up a struct for it.  */
    18981871  if (commands_idx > 0)
    18991872    {
     
    19011874      cmds->fileinfo.filenm = flocp->filenm;
    19021875      cmds->fileinfo.lineno = cmds_started;
    1903       cmds->commands = savestring (commands, commands_idx);
     1876      cmds->commands = xstrndup (commands, commands_idx);
    19041877      cmds->command_lines = 0;
    19051878    }
    19061879  else
    1907     cmds = 0;
    1908 
    1909   for (; filenames != 0; filenames = nextf)
     1880     cmds = 0;
     1881
     1882  /* If there's a prereq string then parse it--unless it's eligible for 2nd
     1883     expansion: if so, snap_deps() will do it.  */
     1884  if (depstr == 0)
     1885    deps = 0;
     1886  else if (second_expansion && strchr (depstr, '$'))
    19101887    {
    1911       const char *name = filenames->name;
     1888      deps = alloc_dep ();
     1889      deps->name = depstr;
     1890      deps->need_2nd_expansion = 1;
     1891      deps->staticpattern = pattern != 0;
     1892    }
     1893  else
     1894    {
     1895      deps = split_prereqs (depstr);
     1896      free (depstr);
     1897
     1898      /* We'll enter static pattern prereqs later when we have the stem.  We
     1899         don't want to enter pattern rules at all so that we don't think that
     1900         they ought to exist (make manual "Implicit Rule Search Algorithm",
     1901         item 5c).  */
     1902      if (! pattern && ! implicit_percent)
     1903        deps = enter_prereqs (deps, NULL);
     1904    }
     1905
     1906  /* For implicit rules, _all_ the targets must have a pattern.  That means we
     1907     can test the first one to see if we're working with an implicit rule; if
     1908     so we handle it specially. */
     1909
     1910  if (implicit_percent)
     1911    {
     1912      struct nameseq *nextf;
     1913      const char **targets, **target_pats;
     1914      unsigned int c;
     1915
     1916      if (pattern != 0)
     1917        fatal (flocp, _("mixed implicit and static pattern rules"));
     1918
     1919      /* Count the targets to create an array of target names.
     1920         We already have the first one.  */
     1921      nextf = filenames->next;
     1922      free_ns (filenames);
     1923      filenames = nextf;
     1924
     1925      for (c = 1; nextf; ++c, nextf = nextf->next)
     1926        ;
     1927      targets = xmalloc (c * sizeof (const char *));
     1928      target_pats = xmalloc (c * sizeof (const char *));
     1929
     1930      targets[0] = name;
     1931      target_pats[0] = implicit_percent;
     1932
     1933      c = 1;
     1934      while (filenames)
     1935        {
     1936          name = filenames->name;
     1937          implicit_percent = find_percent_cached (&name);
     1938
     1939          if (implicit_percent == 0)
     1940            fatal (flocp, _("mixed implicit and normal rules"));
     1941
     1942          targets[c] = name;
     1943          target_pats[c] = implicit_percent;
     1944          ++c;
     1945
     1946          nextf = filenames->next;
     1947          free_ns (filenames);
     1948          filenames = nextf;
     1949        }
     1950
     1951      create_pattern_rule (targets, target_pats, c, two_colon, deps, cmds, 1);
     1952
     1953      return;
     1954    }
     1955
     1956
     1957  /* Walk through each target and create it in the database.
     1958     We already set up the first target, above.  */
     1959  while (1)
     1960    {
     1961      struct nameseq *nextf = filenames->next;
    19121962      struct file *f;
    19131963      struct dep *this = 0;
    1914       const char *implicit_percent;
    1915 
    1916       nextf = filenames->next;
    1917       free (filenames);
     1964
     1965      free_ns (filenames);
    19181966
    19191967      /* Check for special targets.  Do it here instead of, say, snap_deps()
    19201968         so that we can immediately use the value.  */
    1921 
    19221969      if (streq (name, ".POSIX"))
    1923         posix_pedantic = 1;
     1970        {
     1971          posix_pedantic = 1;
     1972          define_variable_cname (".SHELLFLAGS", "-ec", o_default, 0);
     1973        }
    19241974      else if (streq (name, ".SECONDEXPANSION"))
    19251975        second_expansion = 1;
    1926 
    1927       implicit_percent = find_percent_cached (&name);
    1928       implicit |= implicit_percent != 0;
    1929 
    1930       if (implicit)
    1931         {
    1932           if (pattern != 0)
    1933             fatal (flocp, _("mixed implicit and static pattern rules"));
    1934 
    1935           if (implicit_percent == 0)
    1936             fatal (flocp, _("mixed implicit and normal rules"));
    1937 
    1938           if (targets == 0)
    1939             {
    1940               max_targets = 5;
    1941               targets = xmalloc (5 * sizeof (char *));
    1942               target_percents = xmalloc (5 * sizeof (char *));
    1943               target_idx = 0;
    1944             }
    1945           else if (target_idx == max_targets - 1)
    1946             {
    1947               max_targets += 5;
    1948               targets = xrealloc (targets, max_targets * sizeof (char *));
    1949               target_percents = xrealloc (target_percents,
    1950                                           max_targets * sizeof (char *));
    1951             }
    1952           targets[target_idx] = name;
    1953           target_percents[target_idx] = implicit_percent;
    1954           ++target_idx;
    1955           continue;
    1956         }
     1976#if !defined(WINDOWS32) && !defined (__MSDOS__) && !defined (__EMX__)
     1977      else if (streq (name, ".ONESHELL"))
     1978        one_shell = 1;
     1979#endif
    19571980
    19581981      /* If this is a static pattern rule:
    1959          `targets: target%pattern: dep%pattern; cmds',
     1982         `targets: target%pattern: prereq%pattern; recipe',
    19601983         make sure the pattern matches this target name.  */
    19611984      if (pattern && !pattern_matches (pattern, pattern_percent, name))
    19621985        error (flocp, _("target `%s' doesn't match the target pattern"), name);
    19631986      else if (deps)
    1964         {
    1965           /* If there are multiple filenames, copy the chain DEPS for all but
    1966              the last one.  It is not safe for the same deps to go in more
    1967              than one place in the database.  */
    1968           this = nextf != 0 ? copy_dep_chain (deps) : deps;
    1969           this->need_2nd_expansion = (second_expansion
    1970                                       && strchr (this->name, '$'));
    1971         }
    1972 
     1987        /* If there are multiple targets, copy the chain DEPS for all but the
     1988           last one.  It is not safe for the same deps to go in more than one
     1989           place in the database.  */
     1990        this = nextf != 0 ? copy_dep_chain (deps) : deps;
     1991
     1992      /* Find or create an entry in the file database for this target.  */
    19731993      if (!two_colon)
    19741994        {
    1975           /* Single-colon.  Combine these dependencies
    1976              with others in file's existing record, if any.  */
     1995          /* Single-colon.  Combine this rule with the file's existing record,
     1996             if any.  */
    19771997          f = enter_file (strcache_add (name));
    1978 
    19791998          if (f->double_colon)
    19801999            fatal (flocp,
     
    20012020            }
    20022021
    2003           f->is_target = 1;
    2004 
    20052022          /* Defining .DEFAULT with no deps or cmds clears it.  */
    20062023          if (f == default_file && this == 0 && cmds == 0)
     
    20162033              f->deps = 0;
    20172034            }
    2018           else if (this != 0)
    2019             {
    2020               /* Add the file's old deps and the new ones in THIS together.  */
    2021 
    2022               if (f->deps != 0)
    2023                 {
    2024                   struct dep **d_ptr = &f->deps;
    2025 
    2026                   while ((*d_ptr)->next != 0)
    2027                     d_ptr = &(*d_ptr)->next;
    2028 
    2029                   if (cmds != 0)
    2030                     /* This is the rule with commands, so put its deps
    2031                        last. The rationale behind this is that $< expands to
    2032                        the first dep in the chain, and commands use $<
    2033                        expecting to get the dep that rule specifies.  However
    2034                        the second expansion algorithm reverses the order thus
    2035                        we need to make it last here.  */
    2036                     (*d_ptr)->next = this;
    2037                   else
    2038                     {
    2039                       /* This is the rule without commands. Put its
    2040                          dependencies at the end but before dependencies from
    2041                          the rule with commands (if any). This way everything
    2042                          appears in makefile order.  */
    2043 
    2044                       if (f->cmds != 0)
    2045                         {
    2046                           this->next = *d_ptr;
    2047                           *d_ptr = this;
    2048                         }
    2049                       else
    2050                         (*d_ptr)->next = this;
    2051                     }
    2052                 }
    2053               else
    2054                 f->deps = this;
    2055 
    2056               /* This is a hack. I need a way to communicate to snap_deps()
    2057                  that the last dependency line in this file came with commands
    2058                  (so that logic in snap_deps() can put it in front and all
    2059                  this $< -logic works). I cannot simply rely on file->cmds
    2060                  being not 0 because of the cases like the following:
    2061 
    2062                  foo: bar
    2063                  foo:
    2064                      ...
    2065 
    2066                  I am going to temporarily "borrow" UPDATING member in
    2067                  `struct file' for this.   */
    2068 
    2069               if (cmds != 0)
    2070                 f->updating = 1;
    2071             }
    20722035        }
    20732036      else
     
    20762039          f = lookup_file (name);
    20772040
    2078           /* Check for both : and :: rules.  Check is_target so
    2079              we don't lose on default suffix rules or makefiles.  */
     2041          /* Check for both : and :: rules.  Check is_target so we don't lose
     2042             on default suffix rules or makefiles.  */
    20802043          if (f != 0 && f->is_target && !f->double_colon)
    20812044            fatal (flocp,
    20822045                   _("target file `%s' has both : and :: entries"), f->name);
     2046
    20832047          f = enter_file (strcache_add (name));
    20842048          /* If there was an existing entry and it was a double-colon entry,
     
    20902054               double_colon pointer to itself.  */
    20912055            f->double_colon = f;
    2092           f->is_target = 1;
    2093           f->deps = this;
     2056
    20942057          f->cmds = cmds;
    20952058        }
    20962059
     2060      f->is_target = 1;
     2061
    20972062      /* If this is a static pattern rule, set the stem to the part of its
    20982063         name that matched the `%' in the pattern, so you can use $* in the
    2099          commands.  */
     2064         commands.  If we didn't do it before, enter the prereqs now.  */
    21002065      if (pattern)
    21012066        {
     
    21072072          if (this)
    21082073            {
    2109               this->staticpattern = 1;
    2110               this->stem = f->stem;
     2074              if (! this->need_2nd_expansion)
     2075                this = enter_prereqs (this, f->stem);
     2076              else
     2077                this->stem = f->stem;
    21112078            }
    21122079        }
    21132080
     2081      /* Add the dependencies to this file entry.  */
     2082      if (this != 0)
     2083        {
     2084          /* Add the file's old deps and the new ones in THIS together.  */
     2085          if (f->deps == 0)
     2086            f->deps = this;
     2087          else if (cmds != 0)
     2088            {
     2089              struct dep *d = this;
     2090
     2091              /* If this rule has commands, put these deps first.  */
     2092              while (d->next != 0)
     2093                d = d->next;
     2094
     2095              d->next = f->deps;
     2096              f->deps = this;
     2097            }
     2098          else
     2099            {
     2100              struct dep *d = f->deps;
     2101
     2102              /* A rule without commands: put its prereqs at the end.  */
     2103              while (d->next != 0)
     2104                d = d->next;
     2105
     2106              d->next = this;
     2107            }
     2108        }
     2109
    21142110      name = f->name;
    21152111
    2116       /* If this target is a default target, update DEFAULT_GOAL_FILE.  */
    2117       if (streq (*default_goal_name, name)
    2118           && (default_goal_file == 0
    2119               || ! streq (default_goal_file->name, name)))
    2120         default_goal_file = f;
    2121     }
    2122 
    2123   if (implicit)
    2124     {
    2125       if (deps)
    2126         deps->need_2nd_expansion = second_expansion;
    2127       create_pattern_rule (targets, target_percents, target_idx,
    2128                            two_colon, deps, cmds, 1);
     2112      /* All done!  Set up for the next one.  */
     2113      if (nextf == 0)
     2114        break;
     2115
     2116      filenames = nextf;
     2117
     2118      /* Reduce escaped percents.  If there are any unescaped it's an error  */
     2119      name = filenames->name;
     2120      if (find_percent_cached (&name))
     2121        fatal (flocp, _("mixed implicit and normal rules"));
    21292122    }
    21302123}
     
    22502243  const char *p = *string;
    22512244  char *new = 0;
    2252   int slen;
     2245  int slen = 0;
    22532246
    22542247  /* If the first char is a % return now.  This lets us avoid extra tests
     
    23162309
    23172310
    2318 /* Parse a string into a sequence of filenames represented as a
    2319    chain of struct nameseq's in reverse order and return that chain.
     2311/* Find the next line of text in an eval buffer, combining continuation lines
     2312   into one line.
     2313   Return the number of actual lines read (> 1 if continuation lines).
     2314   Returns -1 if there's nothing left in the buffer.
     2315
     2316   After this function, ebuf->buffer points to the first character of the
     2317   line we just found.
     2318 */
     2319
     2320/* Read a line of text from a STRING.
     2321   Since we aren't really reading from a file, don't bother with linenumbers.
     2322 */
     2323
     2324static unsigned long
     2325readstring (struct ebuffer *ebuf)
     2326{
     2327  char *eol;
     2328
     2329  /* If there is nothing left in this buffer, return 0.  */
     2330  if (ebuf->bufnext >= ebuf->bufstart + ebuf->size)
     2331    return -1;
     2332
     2333  /* Set up a new starting point for the buffer, and find the end of the
     2334     next logical line (taking into account backslash/newline pairs).  */
     2335
     2336  eol = ebuf->buffer = ebuf->bufnext;
     2337
     2338  while (1)
     2339    {
     2340      int backslash = 0;
     2341      const char *bol = eol;
     2342      const char *p;
     2343
     2344      /* Find the next newline.  At EOS, stop.  */
     2345      p = eol = strchr (eol , '\n');
     2346      if (!eol)
     2347        {
     2348          ebuf->bufnext = ebuf->bufstart + ebuf->size + 1;
     2349          return 0;
     2350        }
     2351
     2352      /* Found a newline; if it's escaped continue; else we're done.  */
     2353      while (p > bol && *(--p) == '\\')
     2354        backslash = !backslash;
     2355      if (!backslash)
     2356        break;
     2357      ++eol;
     2358    }
     2359
     2360  /* Overwrite the newline char.  */
     2361  *eol = '\0';
     2362  ebuf->bufnext = eol+1;
     2363
     2364  return 0;
     2365}
     2366
     2367static long
     2368readline (struct ebuffer *ebuf)
     2369{
     2370  char *p;
     2371  char *end;
     2372  char *start;
     2373  long nlines = 0;
     2374
     2375  /* The behaviors between string and stream buffers are different enough to
     2376     warrant different functions.  Do the Right Thing.  */
     2377
     2378  if (!ebuf->fp)
     2379    return readstring (ebuf);
     2380
     2381  /* When reading from a file, we always start over at the beginning of the
     2382     buffer for each new line.  */
     2383
     2384  p = start = ebuf->bufstart;
     2385  end = p + ebuf->size;
     2386  *p = '\0';
     2387
     2388  while (fgets (p, end - p, ebuf->fp) != 0)
     2389    {
     2390      char *p2;
     2391      unsigned long len;
     2392      int backslash;
     2393
     2394      len = strlen (p);
     2395      if (len == 0)
     2396        {
     2397          /* This only happens when the first thing on the line is a '\0'.
     2398             It is a pretty hopeless case, but (wonder of wonders) Athena
     2399             lossage strikes again!  (xmkmf puts NULs in its makefiles.)
     2400             There is nothing really to be done; we synthesize a newline so
     2401             the following line doesn't appear to be part of this line.  */
     2402          error (&ebuf->floc,
     2403                 _("warning: NUL character seen; rest of line ignored"));
     2404          p[0] = '\n';
     2405          len = 1;
     2406        }
     2407
     2408      /* Jump past the text we just read.  */
     2409      p += len;
     2410
     2411      /* If the last char isn't a newline, the whole line didn't fit into the
     2412         buffer.  Get some more buffer and try again.  */
     2413      if (p[-1] != '\n')
     2414        goto more_buffer;
     2415
     2416      /* We got a newline, so add one to the count of lines.  */
     2417      ++nlines;
     2418
     2419#if !defined(WINDOWS32) && !defined(__MSDOS__) && !defined(__EMX__)
     2420      /* Check to see if the line was really ended with CRLF; if so ignore
     2421         the CR.  */
     2422      if ((p - start) > 1 && p[-2] == '\r')
     2423        {
     2424          --p;
     2425          p[-1] = '\n';
     2426        }
     2427#endif
     2428
     2429      backslash = 0;
     2430      for (p2 = p - 2; p2 >= start; --p2)
     2431        {
     2432          if (*p2 != '\\')
     2433            break;
     2434          backslash = !backslash;
     2435        }
     2436
     2437      if (!backslash)
     2438        {
     2439          p[-1] = '\0';
     2440          break;
     2441        }
     2442
     2443      /* It was a backslash/newline combo.  If we have more space, read
     2444         another line.  */
     2445      if (end - p >= 80)
     2446        continue;
     2447
     2448      /* We need more space at the end of our buffer, so realloc it.
     2449         Make sure to preserve the current offset of p.  */
     2450    more_buffer:
     2451      {
     2452        unsigned long off = p - start;
     2453        ebuf->size *= 2;
     2454        start = ebuf->buffer = ebuf->bufstart = xrealloc (start, ebuf->size);
     2455        p = start + off;
     2456        end = start + ebuf->size;
     2457        *p = '\0';
     2458      }
     2459    }
     2460
     2461  if (ferror (ebuf->fp))
     2462    pfatal_with_name (ebuf->floc.filenm);
     2463
     2464  /* If we found some lines, return how many.
     2465     If we didn't, but we did find _something_, that indicates we read the last
     2466     line of a file with no final newline; return 1.
     2467     If we read nothing, we're at EOF; return -1.  */
     2468
     2469  return nlines ? nlines : p == ebuf->bufstart ? -1 : 1;
     2470}
     2471
     2472
     2473/* Parse the next "makefile word" from the input buffer, and return info
     2474   about it.
     2475
     2476   A "makefile word" is one of:
     2477
     2478     w_bogus        Should never happen
     2479     w_eol          End of input
     2480     w_static       A static word; cannot be expanded
     2481     w_variable     A word containing one or more variables/functions
     2482     w_colon        A colon
     2483     w_dcolon       A double-colon
     2484     w_semicolon    A semicolon
     2485     w_varassign    A variable assignment operator (=, :=, +=, or ?=)
     2486
     2487   Note that this function is only used when reading certain parts of the
     2488   makefile.  Don't use it where special rules hold sway (RHS of a variable,
     2489   in a command list, etc.)  */
     2490
     2491static enum make_word_type
     2492get_next_mword (char *buffer, char *delim, char **startp, unsigned int *length)
     2493{
     2494  enum make_word_type wtype = w_bogus;
     2495  char *p = buffer, *beg;
     2496  char c;
     2497
     2498  /* Skip any leading whitespace.  */
     2499  while (isblank ((unsigned char)*p))
     2500    ++p;
     2501
     2502  beg = p;
     2503  c = *(p++);
     2504  switch (c)
     2505    {
     2506    case '\0':
     2507      wtype = w_eol;
     2508      break;
     2509
     2510    case ';':
     2511      wtype = w_semicolon;
     2512      break;
     2513
     2514    case '=':
     2515      wtype = w_varassign;
     2516      break;
     2517
     2518    case ':':
     2519      wtype = w_colon;
     2520      switch (*p)
     2521        {
     2522        case ':':
     2523          ++p;
     2524          wtype = w_dcolon;
     2525          break;
     2526
     2527        case '=':
     2528          ++p;
     2529          wtype = w_varassign;
     2530          break;
     2531        }
     2532      break;
     2533
     2534    case '+':
     2535    case '?':
     2536      if (*p == '=')
     2537        {
     2538          ++p;
     2539          wtype = w_varassign;
     2540          break;
     2541        }
     2542
     2543    default:
     2544      if (delim && strchr (delim, c))
     2545        wtype = w_static;
     2546      break;
     2547    }
     2548
     2549  /* Did we find something?  If so, return now.  */
     2550  if (wtype != w_bogus)
     2551    goto done;
     2552
     2553  /* This is some non-operator word.  A word consists of the longest
     2554     string of characters that doesn't contain whitespace, one of [:=#],
     2555     or [?+]=, or one of the chars in the DELIM string.  */
     2556
     2557  /* We start out assuming a static word; if we see a variable we'll
     2558     adjust our assumptions then.  */
     2559  wtype = w_static;
     2560
     2561  /* We already found the first value of "c", above.  */
     2562  while (1)
     2563    {
     2564      char closeparen;
     2565      int count;
     2566
     2567      switch (c)
     2568        {
     2569        case '\0':
     2570        case ' ':
     2571        case '\t':
     2572        case '=':
     2573          goto done_word;
     2574
     2575        case ':':
     2576#ifdef HAVE_DOS_PATHS
     2577          /* A word CAN include a colon in its drive spec.  The drive
     2578             spec is allowed either at the beginning of a word, or as part
     2579             of the archive member name, like in "libfoo.a(d:/foo/bar.o)".  */
     2580          if (!(p - beg >= 2
     2581                && (*p == '/' || *p == '\\') && isalpha ((unsigned char)p[-2])
     2582                && (p - beg == 2 || p[-3] == '(')))
     2583#endif
     2584          goto done_word;
     2585
     2586        case '$':
     2587          c = *(p++);
     2588          if (c == '$')
     2589            break;
     2590
     2591          /* This is a variable reference, so note that it's expandable.
     2592             Then read it to the matching close paren.  */
     2593          wtype = w_variable;
     2594
     2595          if (c == '(')
     2596            closeparen = ')';
     2597          else if (c == '{')
     2598            closeparen = '}';
     2599          else
     2600            /* This is a single-letter variable reference.  */
     2601            break;
     2602
     2603          for (count=0; *p != '\0'; ++p)
     2604            {
     2605              if (*p == c)
     2606                ++count;
     2607              else if (*p == closeparen && --count < 0)
     2608                {
     2609                  ++p;
     2610                  break;
     2611                }
     2612            }
     2613          break;
     2614
     2615        case '?':
     2616        case '+':
     2617          if (*p == '=')
     2618            goto done_word;
     2619          break;
     2620
     2621        case '\\':
     2622          switch (*p)
     2623            {
     2624            case ':':
     2625            case ';':
     2626            case '=':
     2627            case '\\':
     2628              ++p;
     2629              break;
     2630            }
     2631          break;
     2632
     2633        default:
     2634          if (delim && strchr (delim, c))
     2635            goto done_word;
     2636          break;
     2637        }
     2638
     2639      c = *(p++);
     2640    }
     2641 done_word:
     2642  --p;
     2643
     2644 done:
     2645  if (startp)
     2646    *startp = beg;
     2647  if (length)
     2648    *length = p - beg;
     2649  return wtype;
     2650}
     2651
     2652
     2653/* Construct the list of include directories
     2654   from the arguments and the default list.  */
     2655
     2656void
     2657construct_include_path (const char **arg_dirs)
     2658{
     2659#ifdef VAXC             /* just don't ask ... */
     2660  stat_t stbuf;
     2661#else
     2662  struct stat stbuf;
     2663#endif
     2664  const char **dirs;
     2665  const char **cpp;
     2666  unsigned int idx;
     2667
     2668  /* Compute the number of pointers we need in the table.  */
     2669  idx = sizeof (default_include_directories) / sizeof (const char *);
     2670  if (arg_dirs)
     2671    for (cpp = arg_dirs; *cpp != 0; ++cpp)
     2672      ++idx;
     2673
     2674#ifdef  __MSDOS__
     2675  /* Add one for $DJDIR.  */
     2676  ++idx;
     2677#endif
     2678
     2679  dirs = xmalloc (idx * sizeof (const char *));
     2680
     2681  idx = 0;
     2682  max_incl_len = 0;
     2683
     2684  /* First consider any dirs specified with -I switches.
     2685     Ignore any that don't exist.  Remember the maximum string length.  */
     2686
     2687  if (arg_dirs)
     2688    while (*arg_dirs != 0)
     2689      {
     2690        const char *dir = *(arg_dirs++);
     2691        char *expanded = 0;
     2692        int e;
     2693
     2694        if (dir[0] == '~')
     2695          {
     2696            expanded = tilde_expand (dir);
     2697            if (expanded != 0)
     2698              dir = expanded;
     2699          }
     2700
     2701        EINTRLOOP (e, stat (dir, &stbuf));
     2702        if (e == 0 && S_ISDIR (stbuf.st_mode))
     2703          {
     2704            unsigned int len = strlen (dir);
     2705            /* If dir name is written with trailing slashes, discard them.  */
     2706            while (len > 1 && dir[len - 1] == '/')
     2707              --len;
     2708            if (len > max_incl_len)
     2709              max_incl_len = len;
     2710            dirs[idx++] = strcache_add_len (dir, len);
     2711          }
     2712
     2713        if (expanded)
     2714          free (expanded);
     2715      }
     2716
     2717  /* Now add the standard default dirs at the end.  */
     2718
     2719#ifdef  __MSDOS__
     2720  {
     2721    /* The environment variable $DJDIR holds the root of the DJGPP directory
     2722       tree; add ${DJDIR}/include.  */
     2723    struct variable *djdir = lookup_variable ("DJDIR", 5);
     2724
     2725    if (djdir)
     2726      {
     2727        unsigned int len = strlen (djdir->value) + 8;
     2728        char *defdir = alloca (len + 1);
     2729
     2730        strcat (strcpy (defdir, djdir->value), "/include");
     2731        dirs[idx++] = strcache_add (defdir);
     2732
     2733        if (len > max_incl_len)
     2734          max_incl_len = len;
     2735      }
     2736  }
     2737#endif
     2738
     2739  for (cpp = default_include_directories; *cpp != 0; ++cpp)
     2740    {
     2741      int e;
     2742
     2743      EINTRLOOP (e, stat (*cpp, &stbuf));
     2744      if (e == 0 && S_ISDIR (stbuf.st_mode))
     2745        {
     2746          unsigned int len = strlen (*cpp);
     2747          /* If dir name is written with trailing slashes, discard them.  */
     2748          while (len > 1 && (*cpp)[len - 1] == '/')
     2749            --len;
     2750          if (len > max_incl_len)
     2751            max_incl_len = len;
     2752          dirs[idx++] = strcache_add_len (*cpp, len);
     2753        }
     2754    }
     2755
     2756  dirs[idx] = 0;
     2757
     2758  /* Now add each dir to the .INCLUDE_DIRS variable.  */
     2759
     2760  for (cpp = dirs; *cpp != 0; ++cpp)
     2761    do_variable_definition (NILF, ".INCLUDE_DIRS", *cpp,
     2762                            o_default, f_append, 0);
     2763
     2764  include_directories = dirs;
     2765}
     2766
     2767
     2768/* Expand ~ or ~USER at the beginning of NAME.
     2769   Return a newly malloc'd string or 0.  */
     2770
     2771char *
     2772tilde_expand (const char *name)
     2773{
     2774#ifndef VMS
     2775  if (name[1] == '/' || name[1] == '\0')
     2776    {
     2777      extern char *getenv ();
     2778      char *home_dir;
     2779      int is_variable;
     2780
     2781      {
     2782        /* Turn off --warn-undefined-variables while we expand HOME.  */
     2783        int save = warn_undefined_variables_flag;
     2784        warn_undefined_variables_flag = 0;
     2785
     2786        home_dir = allocated_variable_expand ("$(HOME)");
     2787
     2788        warn_undefined_variables_flag = save;
     2789      }
     2790
     2791      is_variable = home_dir[0] != '\0';
     2792      if (!is_variable)
     2793        {
     2794          free (home_dir);
     2795          home_dir = getenv ("HOME");
     2796        }
     2797# if !defined(_AMIGA) && !defined(WINDOWS32)
     2798      if (home_dir == 0 || home_dir[0] == '\0')
     2799        {
     2800          extern char *getlogin ();
     2801          char *logname = getlogin ();
     2802          home_dir = 0;
     2803          if (logname != 0)
     2804            {
     2805              struct passwd *p = getpwnam (logname);
     2806              if (p != 0)
     2807                home_dir = p->pw_dir;
     2808            }
     2809        }
     2810# endif /* !AMIGA && !WINDOWS32 */
     2811      if (home_dir != 0)
     2812        {
     2813          char *new = xstrdup (concat (2, home_dir, name + 1));
     2814          if (is_variable)
     2815            free (home_dir);
     2816          return new;
     2817        }
     2818    }
     2819# if !defined(_AMIGA) && !defined(WINDOWS32)
     2820  else
     2821    {
     2822      struct passwd *pwent;
     2823      char *userend = strchr (name + 1, '/');
     2824      if (userend != 0)
     2825        *userend = '\0';
     2826      pwent = getpwnam (name + 1);
     2827      if (pwent != 0)
     2828        {
     2829          if (userend == 0)
     2830            return xstrdup (pwent->pw_dir);
     2831          else
     2832            return xstrdup (concat (3, pwent->pw_dir, "/", userend + 1));
     2833        }
     2834      else if (userend != 0)
     2835        *userend = '/';
     2836    }
     2837# endif /* !AMIGA && !WINDOWS32 */
     2838#endif /* !VMS */
     2839  return 0;
     2840}
     2841
     2842
     2843/* Parse a string into a sequence of filenames represented as a chain of
     2844   struct nameseq's and return that chain.  Optionally expand the strings via
     2845   glob().
    23202846
    23212847   The string is passed as STRINGP, the address of a string pointer.
     
    23272853   that have room for additional info.
    23282854
    2329    If STRIP is nonzero, strip `./'s off the beginning.  */
    2330 
    2331 struct nameseq *
    2332 parse_file_seq (char **stringp, int stopchar, unsigned int size, int strip)
     2855   PREFIX, if non-null, is added to the beginning of each filename.
     2856
     2857   FLAGS allows one or more of the following bitflags to be set:
     2858        PARSEFS_NOSTRIP - Do no strip './'s off the beginning
     2859        PARSEFS_NOAR    - Do not check filenames for archive references
     2860        PARSEFS_NOGLOB  - Do not expand globbing characters
     2861        PARSEFS_EXISTS  - Only return globbed files that actually exist
     2862                          (cannot also set NOGLOB)
     2863        PARSEFS_NOCACHE - Do not add filenames to the strcache (caller frees)
     2864  */
     2865
     2866void *
     2867parse_file_seq (char **stringp, unsigned int size, int stopchar,
     2868                const char *prefix, int flags)
    23332869{
     2870  extern void dir_setup_glob (glob_t *glob);
     2871
     2872  /* tmp points to tmpbuf after the prefix, if any.
     2873     tp is the end of the buffer. */
     2874  static char *tmpbuf = NULL;
     2875  static int tmpbuf_len = 0;
     2876
     2877  int cachep = (! (flags & PARSEFS_NOCACHE));
     2878
    23342879  struct nameseq *new = 0;
    2335   struct nameseq *new1, *lastnew1;
    2336   char *p = *stringp;
     2880  struct nameseq **newp = &new;
     2881#define NEWELT(_n)  do { \
     2882                        const char *__n = (_n); \
     2883                        *newp = xcalloc (size); \
     2884                        (*newp)->name = (cachep ? strcache_add (__n) : xstrdup (__n)); \
     2885                        newp = &(*newp)->next; \
     2886                    } while(0)
     2887
     2888  char *p;
     2889  glob_t gl;
     2890  char *tp;
    23372891
    23382892#ifdef VMS
     
    23422896#endif
    23432897
     2898  if (size < sizeof (struct nameseq))
     2899    size = sizeof (struct nameseq);
     2900
     2901  if (! (flags & PARSEFS_NOGLOB))
     2902    dir_setup_glob (&gl);
     2903
     2904  /* Get enough temporary space to construct the largest possible target.  */
     2905  {
     2906    int l = strlen (*stringp) + 1;
     2907    if (l > tmpbuf_len)
     2908      {
     2909        tmpbuf = xrealloc (tmpbuf, l);
     2910        tmpbuf_len = l;
     2911      }
     2912  }
     2913  tp = tmpbuf;
     2914
     2915  /* Parse STRING.  P will always point to the end of the parsed content.  */
     2916  p = *stringp;
    23442917  while (1)
    23452918    {
    23462919      const char *name;
    2347       char *q;
    2348 
    2349       /* Skip whitespace; see if any more names are left.  */
     2920      const char **nlist = 0;
     2921      char *tildep = 0;
     2922#ifndef NO_ARCHIVES
     2923      char *arname = 0;
     2924      char *memname = 0;
     2925#endif
     2926      char *s;
     2927      int nlen;
     2928      int i;
     2929
     2930      /* Skip whitespace; at the end of the string or STOPCHAR we're done.  */
    23502931      p = next_token (p);
    2351       if (*p == '\0')
     2932      if (*p == '\0' || *p == stopchar)
    23522933        break;
    2353       if (*p == stopchar)
    2354         break;
    2355 
    2356       /* There are, so find the end of the next name.  */
    2357       q = p;
    2358       p = find_char_unquote (q, stopchar, VMS_COMMA, 1, 0);
     2934
     2935      /* There are names left, so find the end of the next name.
     2936         Throughout this iteration S points to the start.  */
     2937      s = p;
     2938      p = find_char_unquote (p, stopchar, VMS_COMMA, 1, 0);
    23592939#ifdef VMS
    23602940        /* convert comma separated list to space separated */
     
    23792959#endif
    23802960      if (p == 0)
    2381         p = q + strlen (q);
    2382 
    2383       if (strip)
     2961        p = s + strlen (s);
     2962
     2963      /* Strip leading "this directory" references.  */
     2964      if (! (flags & PARSEFS_NOSTRIP))
    23842965#ifdef VMS
    23852966        /* Skip leading `[]'s.  */
    2386         while (p - q > 2 && q[0] == '[' && q[1] == ']')
     2967        while (p - s > 2 && s[0] == '[' && s[1] == ']')
    23872968#else
    23882969        /* Skip leading `./'s.  */
    2389         while (p - q > 2 && q[0] == '.' && q[1] == '/')
     2970        while (p - s > 2 && s[0] == '.' && s[1] == '/')
    23902971#endif
    23912972          {
    2392             q += 2;             /* Skip "./".  */
    2393             while (q < p && *q == '/')
    2394               /* Skip following slashes: ".//foo" is "foo", not "/foo".  */
    2395               ++q;
     2973            /* Skip "./" and all following slashes.  */
     2974            s += 2;
     2975            while (*s == '/')
     2976              ++s;
    23962977          }
    23972978
    2398       /* Extract the filename just found, and skip it.  */
    2399 
    2400       if (q == p)
    2401         /* ".///" was stripped to "". */
     2979      /* Extract the filename just found, and skip it.
     2980         Set NAME to the string, and NLEN to its length.  */
     2981
     2982      if (s == p)
     2983        {
     2984        /* The name was stripped to empty ("./"). */
    24022985#if defined(VMS)
    2403         continue;
     2986          continue;
    24042987#elif defined(_AMIGA)
    2405         name = "";
     2988          /* PDS-- This cannot be right!! */
     2989          tp[0] = '\0';
     2990          nlen = 0;
    24062991#else
    2407         name = "./";
     2992          tp[0] = '.';
     2993          tp[1] = '/';
     2994          tp[2] = '\0';
     2995          nlen = 2;
    24082996#endif
     2997        }
    24092998      else
     2999        {
    24103000#ifdef VMS
    24113001/* VMS filenames can have a ':' in them but they have to be '\'ed but we need
    24123002 *  to remove this '\' before we can use the filename.
    2413  * Savestring called because q may be read-only string constant.
     3003 * xstrdup called because S may be read-only string constant.
    24143004 */
    2415         {
    2416           char *qbase = xstrdup (q);
    2417           char *pbase = qbase + (p-q);
    2418           char *q1 = qbase;
    2419           char *q2 = q1;
    2420           char *p1 = pbase;
    2421 
    2422           while (q1 != pbase)
     3005          char *n = tp;
     3006          while (s < p)
    24233007            {
    2424               if (*q1 == '\\' && *(q1+1) == ':')
    2425                 {
    2426                   q1++;
    2427                   p1--;
    2428                 }
    2429               *q2++ = *q1++;
     3008              if (s[0] == '\\' && s[1] == ':')
     3009                ++s;
     3010              *(n++) = *(s++);
    24303011            }
    2431           name = strcache_add_len (qbase, p1 - qbase);
    2432           free (qbase);
    2433         }
     3012          n[0] = '\0';
     3013          nlen = strlen (tp);
    24343014#else
    2435         name = strcache_add_len (q, p - q);
     3015          nlen = p - s;
     3016          memcpy (tp, s, nlen);
     3017          tp[nlen] = '\0';
    24363018#endif
    2437 
    2438       /* Add it to the front of the chain.  */
    2439       new1 = xmalloc (size);
    2440       memset (new1, '\0', size);
    2441       new1->name = name;
    2442       new1->next = new;
    2443       new = new1;
    2444     }
     3019        }
     3020
     3021      /* At this point, TP points to the element and NLEN is its length.  */
    24453022
    24463023#ifndef NO_ARCHIVES
    2447 
    2448   /* Look for multi-word archive references.
    2449      They are indicated by a elt ending with an unmatched `)' and
    2450      an elt further down the chain (i.e., previous in the file list)
    2451      with an unmatched `(' (e.g., "lib(mem").  */
    2452 
    2453   new1 = new;
    2454   lastnew1 = 0;
    2455   while (new1 != 0)
    2456     if (new1->name[0] != '('    /* Don't catch "(%)" and suchlike.  */
    2457         && new1->name[strlen (new1->name) - 1] == ')'
    2458         && strchr (new1->name, '(') == 0)
    2459       {
    2460         /* NEW1 ends with a `)' but does not contain a `('.
    2461            Look back for an elt with an opening `(' but no closing `)'.  */
    2462 
    2463         struct nameseq *n = new1->next, *lastn = new1;
    2464         char *paren = 0;
    2465         while (n != 0 && (paren = strchr (n->name, '(')) == 0)
    2466           {
    2467             lastn = n;
    2468             n = n->next;
    2469           }
    2470         if (n != 0
    2471             /* Ignore something starting with `(', as that cannot actually
    2472                be an archive-member reference (and treating it as such
    2473                results in an empty file name, which causes much lossage).  */
    2474             && n->name[0] != '(')
    2475           {
    2476             /* N is the first element in the archive group.
    2477                Its name looks like "lib(mem" (with no closing `)').  */
    2478 
    2479             char *libname;
    2480 
    2481             /* Copy "lib(" into LIBNAME.  */
    2482             ++paren;
    2483             libname = alloca (paren - n->name + 1);
    2484             memcpy (libname, n->name, paren - n->name);
    2485             libname[paren - n->name] = '\0';
    2486 
    2487             if (*paren == '\0')
    2488               {
    2489                 /* N was just "lib(", part of something like "lib( a b)".
    2490                    Edit it out of the chain and free its storage.  */
    2491                 lastn->next = n->next;
    2492                 free (n);
    2493                 /* LASTN->next is the new stopping elt for the loop below.  */
    2494                 n = lastn->next;
    2495               }
    2496             else
    2497               {
    2498                 /* Replace N's name with the full archive reference.  */
    2499                 n->name = strcache_add (concat (libname, paren, ")"));
    2500               }
    2501 
    2502             if (new1->name[1] == '\0')
    2503               {
    2504                 /* NEW1 is just ")", part of something like "lib(a b )".
    2505                    Omit it from the chain and free its storage.  */
    2506                 if (lastnew1 == 0)
    2507                   new = new1->next;
    2508                 else
    2509                   lastnew1->next = new1->next;
    2510                 lastn = new1;
    2511                 new1 = new1->next;
    2512                 free (lastn);
    2513               }
    2514             else
    2515               {
    2516                 /* Replace also NEW1->name, which already has closing `)'.  */
    2517                 new1->name = strcache_add (concat (libname, new1->name, ""));
    2518                 new1 = new1->next;
    2519               }
    2520 
    2521             /* Trace back from NEW1 (the end of the list) until N
    2522                (the beginning of the list), rewriting each name
    2523                with the full archive reference.  */
    2524 
    2525             while (new1 != n)
    2526               {
    2527                 new1->name = strcache_add (concat (libname, new1->name, ")"));
    2528                 lastnew1 = new1;
    2529                 new1 = new1->next;
    2530               }
    2531           }
    2532         else
    2533           {
    2534             /* No frobnication happening.  Just step down the list.  */
    2535             lastnew1 = new1;
    2536             new1 = new1->next;
    2537           }
    2538       }
    2539     else
    2540       {
    2541         lastnew1 = new1;
    2542         new1 = new1->next;
    2543       }
    2544 
    2545 #endif
    2546 
    2547   *stringp = p;
    2548   return new;
    2549 }
    2550 
    2551 
    2552 /* Find the next line of text in an eval buffer, combining continuation lines
    2553    into one line.
    2554    Return the number of actual lines read (> 1 if continuation lines).
    2555    Returns -1 if there's nothing left in the buffer.
    2556 
    2557    After this function, ebuf->buffer points to the first character of the
    2558    line we just found.
    2559  */
    2560 
    2561 /* Read a line of text from a STRING.
    2562    Since we aren't really reading from a file, don't bother with linenumbers.
    2563  */
    2564 
    2565 static unsigned long
    2566 readstring (struct ebuffer *ebuf)
    2567 {
    2568   char *eol;
    2569 
    2570   /* If there is nothing left in this buffer, return 0.  */
    2571   if (ebuf->bufnext >= ebuf->bufstart + ebuf->size)
    2572     return -1;
    2573 
    2574   /* Set up a new starting point for the buffer, and find the end of the
    2575      next logical line (taking into account backslash/newline pairs).  */
    2576 
    2577   eol = ebuf->buffer = ebuf->bufnext;
    2578 
    2579   while (1)
    2580     {
    2581       int backslash = 0;
    2582       char *bol = eol;
    2583       char *p;
    2584 
    2585       /* Find the next newline.  At EOS, stop.  */
    2586       eol = p = strchr (eol , '\n');
    2587       if (!eol)
     3024      /* If this is the start of an archive group that isn't complete, set up
     3025         to add the archive prefix for future files.  A file list like:
     3026         "libf.a(x.o y.o z.o)" needs to be expanded as:
     3027         "libf.a(x.o) libf.a(y.o) libf.a(z.o)"
     3028
     3029         TP == TMP means we're not already in an archive group.  Ignore
     3030         something starting with `(', as that cannot actually be an
     3031         archive-member reference (and treating it as such results in an empty
     3032         file name, which causes much lossage).  Also if it ends in ")" then
     3033         it's a complete reference so we don't need to treat it specially.
     3034
     3035         Finally, note that archive groups must end with ')' as the last
     3036         character, so ensure there's some word ending like that before
     3037         considering this an archive group.  */
     3038      if (! (flags & PARSEFS_NOAR)
     3039          && tp == tmpbuf && tp[0] != '(' && tp[nlen-1] != ')')
    25883040        {
    2589           ebuf->bufnext = ebuf->bufstart + ebuf->size + 1;
    2590           return 0;
     3041          char *n = strchr (tp, '(');
     3042          if (n)
     3043            {
     3044              /* This looks like the first element in an open archive group.
     3045                 A valid group MUST have ')' as the last character.  */
     3046              const char *e = p + nlen;
     3047              do
     3048                {
     3049                  e = next_token (e);
     3050                  /* Find the end of this word.  We don't want to unquote and
     3051                     we don't care about quoting since we're looking for the
     3052                     last char in the word. */
     3053                  while (*e != '\0' && *e != stopchar && *e != VMS_COMMA
     3054                         && ! isblank ((unsigned char) *e))
     3055                    ++e;
     3056                  if (e[-1] == ')')
     3057                    {
     3058                      /* Found the end, so this is the first element in an
     3059                         open archive group.  It looks like "lib(mem".
     3060                         Reset TP past the open paren.  */
     3061                      nlen -= (n + 1) - tp;
     3062                      tp = n + 1;
     3063
     3064                      /* If we have just "lib(", part of something like
     3065                         "lib( a b)", go to the next item.  */
     3066                      if (! nlen)
     3067                        continue;
     3068
     3069                      /* We can stop looking now.  */
     3070                      break;
     3071                    }
     3072                }
     3073              while (*e != '\0');
     3074            }
    25913075        }
    25923076
    2593       /* Found a newline; if it's escaped continue; else we're done.  */
    2594       while (p > bol && *(--p) == '\\')
    2595         backslash = !backslash;
    2596       if (!backslash)
    2597         break;
    2598       ++eol;
    2599     }
    2600 
    2601   /* Overwrite the newline char.  */
    2602   *eol = '\0';
    2603   ebuf->bufnext = eol+1;
    2604 
    2605   return 0;
    2606 }
    2607 
    2608 static long
    2609 readline (struct ebuffer *ebuf)
    2610 {
    2611   char *p;
    2612   char *end;
    2613   char *start;
    2614   long nlines = 0;
    2615 
    2616   /* The behaviors between string and stream buffers are different enough to
    2617      warrant different functions.  Do the Right Thing.  */
    2618 
    2619   if (!ebuf->fp)
    2620     return readstring (ebuf);
    2621 
    2622   /* When reading from a file, we always start over at the beginning of the
    2623      buffer for each new line.  */
    2624 
    2625   p = start = ebuf->bufstart;
    2626   end = p + ebuf->size;
    2627   *p = '\0';
    2628 
    2629   while (fgets (p, end - p, ebuf->fp) != 0)
    2630     {
    2631       char *p2;
    2632       unsigned long len;
    2633       int backslash;
    2634 
    2635       len = strlen (p);
    2636       if (len == 0)
    2637         {
    2638           /* This only happens when the first thing on the line is a '\0'.
    2639              It is a pretty hopeless case, but (wonder of wonders) Athena
    2640              lossage strikes again!  (xmkmf puts NULs in its makefiles.)
    2641              There is nothing really to be done; we synthesize a newline so
    2642              the following line doesn't appear to be part of this line.  */
    2643           error (&ebuf->floc,
    2644                  _("warning: NUL character seen; rest of line ignored"));
    2645           p[0] = '\n';
    2646           len = 1;
    2647         }
    2648 
    2649       /* Jump past the text we just read.  */
    2650       p += len;
    2651 
    2652       /* If the last char isn't a newline, the whole line didn't fit into the
    2653          buffer.  Get some more buffer and try again.  */
    2654       if (p[-1] != '\n')
    2655         goto more_buffer;
    2656 
    2657       /* We got a newline, so add one to the count of lines.  */
    2658       ++nlines;
    2659 
    2660 #if !defined(WINDOWS32) && !defined(__MSDOS__) && !defined(__EMX__)
    2661       /* Check to see if the line was really ended with CRLF; if so ignore
    2662          the CR.  */
    2663       if ((p - start) > 1 && p[-2] == '\r')
     3077      /* If we are inside an archive group, make sure it has an end.  */
     3078      if (tp > tmpbuf)
    26643079        {
    2665           --p;
    2666           p[-1] = '\n';
     3080          if (tp[nlen-1] == ')')
     3081            {
     3082              /* This is the natural end; reset TP.  */
     3083              tp = tmpbuf;
     3084
     3085              /* This is just ")", something like "lib(a b )": skip it.  */
     3086              if (nlen == 1)
     3087                continue;
     3088            }
     3089          else
     3090            {
     3091              /* Not the end, so add a "fake" end.  */
     3092              tp[nlen++] = ')';
     3093              tp[nlen] = '\0';
     3094            }
    26673095        }
    26683096#endif
    26693097
    2670       backslash = 0;
    2671       for (p2 = p - 2; p2 >= start; --p2)
     3098      /* If we're not globbing we're done: add it to the end of the chain.
     3099         Go to the next item in the string.  */
     3100      if (flags & PARSEFS_NOGLOB)
     3101        {
     3102          NEWELT (concat (2, prefix, tp));
     3103          continue;
     3104        }
     3105
     3106      /* If we get here we know we're doing glob expansion.
     3107         TP is a string in tmpbuf.  NLEN is no longer used.
     3108         We may need to do more work: after this NAME will be set.  */
     3109      name = tp;
     3110
     3111      /* Expand tilde if applicable.  */
     3112      if (tp[0] == '~')
    26723113        {
    2673           if (*p2 != '\\')
    2674             break;
    2675           backslash = !backslash;
     3114          tildep = tilde_expand (tp);
     3115          if (tildep != 0)
     3116            name = tildep;
    26763117        }
    26773118
    2678       if (!backslash)
     3119#ifndef NO_ARCHIVES
     3120      /* If NAME is an archive member reference replace it with the archive
     3121         file name, and save the member name in MEMNAME.  We will glob on the
     3122         archive name and then reattach MEMNAME later.  */
     3123      if (! (flags & PARSEFS_NOAR) && ar_name (name))
    26793124        {
    2680           p[-1] = '\0';
    2681           break;
     3125          ar_parse_name (name, &arname, &memname);
     3126          name = arname;
    26823127        }
    2683 
    2684       /* It was a backslash/newline combo.  If we have more space, read
    2685          another line.  */
    2686       if (end - p >= 80)
    2687         continue;
    2688 
    2689       /* We need more space at the end of our buffer, so realloc it.
    2690          Make sure to preserve the current offset of p.  */
    2691     more_buffer:
    2692       {
    2693         unsigned long off = p - start;
    2694         ebuf->size *= 2;
    2695         start = ebuf->buffer = ebuf->bufstart = xrealloc (start, ebuf->size);
    2696         p = start + off;
    2697         end = start + ebuf->size;
    2698         *p = '\0';
    2699       }
    2700     }
    2701 
    2702   if (ferror (ebuf->fp))
    2703     pfatal_with_name (ebuf->floc.filenm);
    2704 
    2705   /* If we found some lines, return how many.
    2706      If we didn't, but we did find _something_, that indicates we read the last
    2707      line of a file with no final newline; return 1.
    2708      If we read nothing, we're at EOF; return -1.  */
    2709 
    2710   return nlines ? nlines : p == ebuf->bufstart ? -1 : 1;
    2711 }
    2712 
    2713 
    2714 /* Parse the next "makefile word" from the input buffer, and return info
    2715    about it.
    2716 
    2717    A "makefile word" is one of:
    2718 
    2719      w_bogus        Should never happen
    2720      w_eol          End of input
    2721      w_static       A static word; cannot be expanded
    2722      w_variable     A word containing one or more variables/functions
    2723      w_colon        A colon
    2724      w_dcolon       A double-colon
    2725      w_semicolon    A semicolon
    2726      w_varassign    A variable assignment operator (=, :=, +=, or ?=)
    2727 
    2728    Note that this function is only used when reading certain parts of the
    2729    makefile.  Don't use it where special rules hold sway (RHS of a variable,
    2730    in a command list, etc.)  */
    2731 
    2732 static enum make_word_type
    2733 get_next_mword (char *buffer, char *delim, char **startp, unsigned int *length)
    2734 {
    2735   enum make_word_type wtype = w_bogus;
    2736   char *p = buffer, *beg;
    2737   char c;
    2738 
    2739   /* Skip any leading whitespace.  */
    2740   while (isblank ((unsigned char)*p))
    2741     ++p;
    2742 
    2743   beg = p;
    2744   c = *(p++);
    2745   switch (c)
    2746     {
    2747     case '\0':
    2748       wtype = w_eol;
    2749       break;
    2750 
    2751     case ';':
    2752       wtype = w_semicolon;
    2753       break;
    2754 
    2755     case '=':
    2756       wtype = w_varassign;
    2757       break;
    2758 
    2759     case ':':
    2760       wtype = w_colon;
    2761       switch (*p)
    2762         {
    2763         case ':':
    2764           ++p;
    2765           wtype = w_dcolon;
     3128#endif /* !NO_ARCHIVES */
     3129
     3130      switch (glob (name, GLOB_NOSORT|GLOB_ALTDIRFUNC, NULL, &gl))
     3131        {
     3132        case GLOB_NOSPACE:
     3133          fatal (NILF, _("virtual memory exhausted"));
     3134
     3135        case 0:
     3136          /* Success.  */
     3137          i = gl.gl_pathc;
     3138          nlist = (const char **)gl.gl_pathv;
    27663139          break;
    27673140
    2768         case '=':
    2769           ++p;
    2770           wtype = w_varassign;
    2771           break;
    2772         }
    2773       break;
    2774 
    2775     case '+':
    2776     case '?':
    2777       if (*p == '=')
    2778         {
    2779           ++p;
    2780           wtype = w_varassign;
    2781           break;
    2782         }
    2783 
    2784     default:
    2785       if (delim && strchr (delim, c))
    2786         wtype = w_static;
    2787       break;
    2788     }
    2789 
    2790   /* Did we find something?  If so, return now.  */
    2791   if (wtype != w_bogus)
    2792     goto done;
    2793 
    2794   /* This is some non-operator word.  A word consists of the longest
    2795      string of characters that doesn't contain whitespace, one of [:=#],
    2796      or [?+]=, or one of the chars in the DELIM string.  */
    2797 
    2798   /* We start out assuming a static word; if we see a variable we'll
    2799      adjust our assumptions then.  */
    2800   wtype = w_static;
    2801 
    2802   /* We already found the first value of "c", above.  */
    2803   while (1)
    2804     {
    2805       char closeparen;
    2806       int count;
    2807 
    2808       switch (c)
    2809         {
    2810         case '\0':
    2811         case ' ':
    2812         case '\t':
    2813         case '=':
    2814           goto done_word;
    2815 
    2816         case ':':
    2817 #ifdef HAVE_DOS_PATHS
    2818           /* A word CAN include a colon in its drive spec.  The drive
    2819              spec is allowed either at the beginning of a word, or as part
    2820              of the archive member name, like in "libfoo.a(d:/foo/bar.o)".  */
    2821           if (!(p - beg >= 2
    2822                 && (*p == '/' || *p == '\\') && isalpha ((unsigned char)p[-2])
    2823                 && (p - beg == 2 || p[-3] == '(')))
    2824 #endif
    2825           goto done_word;
    2826 
    2827         case '$':
    2828           c = *(p++);
    2829           if (c == '$')
    2830             break;
    2831 
    2832           /* This is a variable reference, so note that it's expandable.
    2833              Then read it to the matching close paren.  */
    2834           wtype = w_variable;
    2835 
    2836           if (c == '(')
    2837             closeparen = ')';
    2838           else if (c == '{')
    2839             closeparen = '}';
    2840           else
    2841             /* This is a single-letter variable reference.  */
    2842             break;
    2843 
    2844           for (count=0; *p != '\0'; ++p)
     3141        case GLOB_NOMATCH:
     3142          /* If we want only existing items, skip this one.  */
     3143          if (flags & PARSEFS_EXISTS)
    28453144            {
    2846               if (*p == c)
    2847                 ++count;
    2848               else if (*p == closeparen && --count < 0)
    2849                 {
    2850                   ++p;
    2851                   break;
    2852                 }
    2853             }
    2854           break;
    2855 
    2856         case '?':
    2857         case '+':
    2858           if (*p == '=')
    2859             goto done_word;
    2860           break;
    2861 
    2862         case '\\':
    2863           switch (*p)
    2864             {
    2865             case ':':
    2866             case ';':
    2867             case '=':
    2868             case '\\':
    2869               ++p;
     3145              i = 0;
    28703146              break;
    28713147            }
     3148          /* FALLTHROUGH */
     3149
     3150        default:
     3151          /* By default keep this name.  */
     3152          i = 1;
     3153          nlist = &name;
    28723154          break;
    2873 
    2874         default:
    2875           if (delim && strchr (delim, c))
    2876             goto done_word;
    2877           break;
    2878         }
    2879 
    2880       c = *(p++);
    2881     }
    2882  done_word:
    2883   --p;
    2884 
    2885  done:
    2886   if (startp)
    2887     *startp = beg;
    2888   if (length)
    2889     *length = p - beg;
    2890   return wtype;
    2891 }
    2892 
    2893 
    2894 /* Construct the list of include directories
    2895    from the arguments and the default list.  */
    2896 
    2897 void
    2898 construct_include_path (const char **arg_dirs)
    2899 {
    2900 #ifdef VAXC             /* just don't ask ... */
    2901   stat_t stbuf;
    2902 #else
    2903   struct stat stbuf;
    2904 #endif
    2905   const char **dirs;
    2906   const char **cpp;
    2907   unsigned int idx;
    2908 
    2909   /* Compute the number of pointers we need in the table.  */
    2910   idx = sizeof (default_include_directories) / sizeof (const char *);
    2911   if (arg_dirs)
    2912     for (cpp = arg_dirs; *cpp != 0; ++cpp)
    2913       ++idx;
    2914 
    2915 #ifdef  __MSDOS__
    2916   /* Add one for $DJDIR.  */
    2917   ++idx;
    2918 #endif
    2919 
    2920   dirs = xmalloc (idx * sizeof (const char *));
    2921 
    2922   idx = 0;
    2923   max_incl_len = 0;
    2924 
    2925   /* First consider any dirs specified with -I switches.
    2926      Ignore any that don't exist.  Remember the maximum string length.  */
    2927 
    2928   if (arg_dirs)
    2929     while (*arg_dirs != 0)
    2930       {
    2931         const char *dir = *(arg_dirs++);
    2932         char *expanded = 0;
    2933         int e;
    2934 
    2935         if (dir[0] == '~')
    2936           {
    2937             expanded = tilde_expand (dir);
    2938             if (expanded != 0)
    2939               dir = expanded;
    2940           }
    2941 
    2942         EINTRLOOP (e, stat (dir, &stbuf));
    2943         if (e == 0 && S_ISDIR (stbuf.st_mode))
     3155        }
     3156
     3157      /* For each matched element, add it to the list.  */
     3158      while (i-- > 0)
     3159#ifndef NO_ARCHIVES
     3160        if (memname != 0)
    29443161          {
    2945             unsigned int len = strlen (dir);
    2946             /* If dir name is written with trailing slashes, discard them.  */
    2947             while (len > 1 && dir[len - 1] == '/')
    2948               --len;
    2949             if (len > max_incl_len)
    2950               max_incl_len = len;
    2951             dirs[idx++] = strcache_add_len (dir, len);
     3162            /* Try to glob on MEMNAME within the archive.  */
     3163            struct nameseq *found = ar_glob (nlist[i], memname, size);
     3164            if (! found)
     3165              /* No matches.  Use MEMNAME as-is.  */
     3166              NEWELT (concat (5, prefix, nlist[i], "(", memname, ")"));
     3167            else
     3168              {
     3169                /* We got a chain of items.  Attach them.  */
     3170                (*newp)->next = found;
     3171
     3172                /* Find and set the new end.  Massage names if necessary.  */
     3173                while (1)
     3174                  {
     3175                    if (! cachep)
     3176                      found->name = xstrdup (concat (2, prefix, name));
     3177                    else if (prefix)
     3178                      found->name = strcache_add (concat (2, prefix, name));
     3179
     3180                    if (found->next == 0)
     3181                      break;
     3182
     3183                    found = found->next;
     3184                  }
     3185                newp = &found->next;
     3186              }
    29523187          }
    2953 
    2954         if (expanded)
    2955           free (expanded);
    2956       }
    2957 
    2958   /* Now add the standard default dirs at the end.  */
    2959 
    2960 #ifdef  __MSDOS__
    2961   {
    2962     /* The environment variable $DJDIR holds the root of the DJGPP directory
    2963        tree; add ${DJDIR}/include.  */
    2964     struct variable *djdir = lookup_variable ("DJDIR", 5);
    2965 
    2966     if (djdir)
    2967       {
    2968         unsigned int len = strlen (djdir->value) + 8;
    2969         char *defdir = alloca (len + 1);
    2970 
    2971         strcat (strcpy (defdir, djdir->value), "/include");
    2972         dirs[idx++] = strcache_add (defdir);
    2973 
    2974         if (len > max_incl_len)
    2975           max_incl_len = len;
    2976       }
    2977   }
    2978 #endif
    2979 
    2980   for (cpp = default_include_directories; *cpp != 0; ++cpp)
    2981     {
    2982       int e;
    2983 
    2984       EINTRLOOP (e, stat (*cpp, &stbuf));
    2985       if (e == 0 && S_ISDIR (stbuf.st_mode))
    2986         {
    2987           unsigned int len = strlen (*cpp);
    2988           /* If dir name is written with trailing slashes, discard them.  */
    2989           while (len > 1 && (*cpp)[len - 1] == '/')
    2990             --len;
    2991           if (len > max_incl_len)
    2992             max_incl_len = len;
    2993           dirs[idx++] = strcache_add_len (*cpp, len);
    2994         }
    2995     }
    2996 
    2997   dirs[idx] = 0;
    2998 
    2999   /* Now add each dir to the .INCLUDE_DIRS variable.  */
    3000 
    3001   for (cpp = dirs; *cpp != 0; ++cpp)
    3002     do_variable_definition (NILF, ".INCLUDE_DIRS", *cpp,
    3003                             o_default, f_append, 0);
    3004 
    3005   include_directories = dirs;
    3006 }
    3007 
    3008 
    3009 /* Expand ~ or ~USER at the beginning of NAME.
    3010    Return a newly malloc'd string or 0.  */
    3011 
    3012 char *
    3013 tilde_expand (const char *name)
    3014 {
    3015 #ifndef VMS
    3016   if (name[1] == '/' || name[1] == '\0')
    3017     {
    3018       extern char *getenv ();
    3019       char *home_dir;
    3020       int is_variable;
    3021 
    3022       {
    3023         /* Turn off --warn-undefined-variables while we expand HOME.  */
    3024         int save = warn_undefined_variables_flag;
    3025         warn_undefined_variables_flag = 0;
    3026 
    3027         home_dir = allocated_variable_expand ("$(HOME)");
    3028 
    3029         warn_undefined_variables_flag = save;
    3030       }
    3031 
    3032       is_variable = home_dir[0] != '\0';
    3033       if (!is_variable)
    3034         {
    3035           free (home_dir);
    3036           home_dir = getenv ("HOME");
    3037         }
    3038 # if !defined(_AMIGA) && !defined(WINDOWS32)
    3039       if (home_dir == 0 || home_dir[0] == '\0')
    3040         {
    3041           extern char *getlogin ();
    3042           char *logname = getlogin ();
    3043           home_dir = 0;
    3044           if (logname != 0)
    3045             {
    3046               struct passwd *p = getpwnam (logname);
    3047               if (p != 0)
    3048                 home_dir = p->pw_dir;
    3049             }
    3050         }
    3051 # endif /* !AMIGA && !WINDOWS32 */
    3052       if (home_dir != 0)
    3053         {
    3054           char *new = xstrdup (concat (home_dir, "", name + 1));
    3055           if (is_variable)
    3056             free (home_dir);
    3057           return new;
    3058         }
    3059     }
    3060 # if !defined(_AMIGA) && !defined(WINDOWS32)
    3061   else
    3062     {
    3063       struct passwd *pwent;
    3064       char *userend = strchr (name + 1, '/');
    3065       if (userend != 0)
    3066         *userend = '\0';
    3067       pwent = getpwnam (name + 1);
    3068       if (pwent != 0)
    3069         {
    3070           if (userend == 0)
    3071             return xstrdup (pwent->pw_dir);
    3072           else
    3073             return xstrdup (concat (pwent->pw_dir, "/", userend + 1));
    3074         }
    3075       else if (userend != 0)
    3076         *userend = '/';
    3077     }
    3078 # endif /* !AMIGA && !WINDOWS32 */
    3079 #endif /* !VMS */
    3080   return 0;
    3081 }
    3082 
    3083 /* Given a chain of struct nameseq's describing a sequence of filenames,
    3084    in reverse of the intended order, return a new chain describing the
    3085    result of globbing the filenames.  The new chain is in forward order.
    3086    The links of the old chain are freed or used in the new chain.
    3087    Likewise for the names in the old chain.
    3088 
    3089    SIZE is how big to construct chain elements.
    3090    This is useful if we want them actually to be other structures
    3091    that have room for additional info.  */
    3092 
    3093 struct nameseq *
    3094 multi_glob (struct nameseq *chain, unsigned int size)
    3095 {
    3096   void dir_setup_glob (glob_t *);
    3097   struct nameseq *new = 0;
    3098   struct nameseq *old;
    3099   struct nameseq *nexto;
    3100   glob_t gl;
    3101 
    3102   dir_setup_glob (&gl);
    3103 
    3104   for (old = chain; old != 0; old = nexto)
    3105     {
    3106       const char *gname;
    3107 #ifndef NO_ARCHIVES
    3108       char *arname = 0;
    3109       char *memname = 0;
    3110 #endif
    3111       nexto = old->next;
    3112       gname = old->name;
    3113 
    3114       if (gname[0] == '~')
    3115         {
    3116           char *newname = tilde_expand (old->name);
    3117           if (newname != 0)
    3118             gname = newname;
    3119         }
    3120 
    3121 #ifndef NO_ARCHIVES
    3122       if (ar_name (gname))
    3123         {
    3124           /* OLD->name is an archive member reference.  Replace it with the
    3125              archive file name, and save the member name in MEMNAME.  We will
    3126              glob on the archive name and then reattach MEMNAME later.  */
    3127           ar_parse_name (gname, &arname, &memname);
    3128           gname = arname;
    3129         }
     3188        else
    31303189#endif /* !NO_ARCHIVES */
    3131 
    3132       switch (glob (gname, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
    3133         {
    3134         case 0:                 /* Success.  */
    3135           {
    3136             int i = gl.gl_pathc;
    3137             while (i-- > 0)
    3138               {
    3139 #ifndef NO_ARCHIVES
    3140                 if (memname != 0)
    3141                   {
    3142                     /* Try to glob on MEMNAME within the archive.  */
    3143                     struct nameseq *found
    3144                       = ar_glob (gl.gl_pathv[i], memname, size);
    3145                     if (! found)
    3146                       {
    3147                         /* No matches.  Use MEMNAME as-is.  */
    3148                         unsigned int alen = strlen (gl.gl_pathv[i]);
    3149                         unsigned int mlen = strlen (memname);
    3150                         char *name;
    3151                         struct nameseq *elt = xmalloc (size);
    3152                         memset (elt, '\0', size);
    3153 
    3154                         name = alloca (alen + 1 + mlen + 2);
    3155                         memcpy (name, gl.gl_pathv[i], alen);
    3156                         name[alen] = '(';
    3157                         memcpy (name+alen+1, memname, mlen);
    3158                         name[alen + 1 + mlen] = ')';
    3159                         name[alen + 1 + mlen + 1] = '\0';
    3160                         elt->name = strcache_add (name);
    3161                         elt->next = new;
    3162                         new = elt;
    3163                       }
    3164                     else
    3165                       {
    3166                         /* Find the end of the FOUND chain.  */
    3167                         struct nameseq *f = found;
    3168                         while (f->next != 0)
    3169                           f = f->next;
    3170 
    3171                         /* Attach the chain being built to the end of the FOUND
    3172                            chain, and make FOUND the new NEW chain.  */
    3173                         f->next = new;
    3174                         new = found;
    3175                       }
    3176                   }
    3177                 else
    3178 #endif /* !NO_ARCHIVES */
    3179                   {
    3180                     struct nameseq *elt = xmalloc (size);
    3181                     memset (elt, '\0', size);
    3182                     elt->name = strcache_add (gl.gl_pathv[i]);
    3183                     elt->next = new;
    3184                     new = elt;
    3185                   }
    3186               }
    3187             globfree (&gl);
    3188             free (old);
    3189             break;
    3190           }
    3191 
    3192         case GLOB_NOSPACE:
    3193           fatal (NILF, _("virtual memory exhausted"));
    3194           break;
    3195 
    3196         default:
    3197           old->next = new;
    3198           new = old;
    3199           break;
    3200         }
     3190          NEWELT (concat (2, prefix, nlist[i]));
     3191
     3192      globfree (&gl);
    32013193
    32023194#ifndef NO_ARCHIVES
     
    32043196        free (arname);
    32053197#endif
     3198
     3199      if (tildep)
     3200        free (tildep);
    32063201    }
    32073202
     3203  *stringp = p;
    32083204  return new;
    32093205}
Note: See TracChangeset for help on using the changeset viewer.