home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / GNUSRC.Z / read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-01  |  57.0 KB  |  2,172 lines

  1. /* Reading and parsing of makefiles for GNU Make.
  2. Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "commands.h"
  21. #include "dep.h"
  22. #include "file.h"
  23. #include "variable.h"
  24.  
  25. /* This is POSIX.2, but most systems using -DPOSIX probably don't have it.  */
  26. #ifdef    HAVE_GLOB_H
  27. #include <glob.h>
  28. #else
  29. #include "glob/glob.h"
  30. #endif
  31.  
  32. #include <pwd.h>
  33. struct passwd *getpwnam ();
  34.  
  35.  
  36. static int read_makefile ();
  37. static unsigned int readline (), do_define ();
  38. static int conditional_line ();
  39. static void record_files ();
  40. static char *find_semicolon ();
  41.  
  42.  
  43. /* A `struct linebuffer' is a structure which holds a line of text.
  44.    `readline' reads a line from a stream into a linebuffer
  45.    and works regardless of the length of the line.  */
  46.  
  47. struct linebuffer
  48.   {
  49.     /* Note:  This is the number of bytes malloc'ed for `buffer'
  50.        It does not indicate `buffer's real length.
  51.        Instead, a null char indicates end-of-string.  */
  52.     unsigned int size;
  53.     char *buffer;
  54.   };
  55.  
  56. #define initbuffer(lb) (lb)->buffer = (char *) xmalloc ((lb)->size = 200)
  57. #define freebuffer(lb) free ((lb)->buffer)
  58.  
  59.  
  60. /* A `struct conditionals' contains the information describing
  61.    all the active conditionals in a makefile.
  62.  
  63.    The global variable `conditionals' contains the conditionals
  64.    information for the current makefile.  It is initialized from
  65.    the static structure `toplevel_conditionals' and is later changed
  66.    to new structures for included makefiles.  */
  67.  
  68. struct conditionals
  69.   {
  70.     unsigned int if_cmds;    /* Depth of conditional nesting.  */
  71.     unsigned int allocated;    /* Elts allocated in following arrays.  */
  72.     char *ignoring;        /* Are we ignoring or interepreting?  */
  73.     char *seen_else;        /* Have we already seen an `else'?  */
  74.   };
  75.  
  76. static struct conditionals toplevel_conditionals;
  77. static struct conditionals *conditionals = &toplevel_conditionals;
  78.   
  79.  
  80. /* Default directories to search for include files in  */
  81.  
  82. static char *default_include_directories[] =
  83.   {
  84.     INCLUDEDIR,
  85.     "/usr/gnu/include",
  86.     "/usr/local/include",
  87.     "/usr/include",
  88.     0
  89.   };
  90.  
  91. /* List of directories to search for include files in  */
  92.  
  93. static char **include_directories;
  94.  
  95. /* Maximum length of an element of the above.  */
  96.  
  97. static unsigned int max_incl_len;
  98.  
  99. /* The filename and pointer to line number of the
  100.    makefile currently being read in.  */
  101.  
  102. char *reading_filename;
  103. unsigned int *reading_lineno_ptr;
  104.  
  105. /* The chain of makefiles read by read_makefile.  */
  106.  
  107. static struct dep *read_makefiles = 0;
  108.  
  109. /* Read in all the makefiles and return the chain of their names.  */
  110.  
  111. struct dep *
  112. read_all_makefiles (makefiles)
  113.      char **makefiles;
  114. {
  115.   unsigned int num_makefiles = 0;
  116.  
  117.   if (debug_flag)
  118.     puts ("Reading makefiles...");
  119.  
  120.   /* If there's a non-null variable MAKEFILES, its value is a list of
  121.      files to read first thing.  But don't let it prevent reading the
  122.      default makefiles and don't let the default goal come from there.  */
  123.  
  124.   {
  125.     char *value;
  126.     char *name, *p;
  127.     unsigned int length;
  128.  
  129.     {
  130.       /* Turn off --warn-undefined-variables while we expand MAKEFILES.  */
  131.       int save = warn_undefined_variables_flag;
  132.       warn_undefined_variables_flag = 0;
  133.  
  134.       value = allocated_variable_expand ("$(MAKEFILES)");
  135.  
  136.       warn_undefined_variables_flag = save;
  137.     }
  138.  
  139.     /* Set NAME to the start of next token and LENGTH to its length.
  140.        MAKEFILES is updated for finding remaining tokens.  */
  141.     p = value;
  142.     while ((name = find_next_token (&p, &length)) != 0)
  143.       {
  144.     if (*p != '\0')
  145.       *p++ = '\0';
  146.     (void) read_makefile (name,
  147.                   RM_NO_DEFAULT_GOAL | RM_INCLUDED | RM_DONTCARE);
  148.       }
  149.  
  150.     free (value);
  151.   }
  152.  
  153.   /* Read makefiles specified with -f switches.  */
  154.  
  155.   if (makefiles != 0)
  156.     while (*makefiles != 0)
  157.       {
  158.     struct dep *tail = read_makefiles;
  159.     register struct dep *d;
  160.  
  161.     if (! read_makefile (*makefiles, 0))
  162.       perror_with_name ("", *makefiles);
  163.  
  164.     /* Find the right element of read_makefiles.  */
  165.     d = read_makefiles;
  166.     while (d->next != tail)
  167.       d = d->next;
  168.  
  169.     /* Use the storage read_makefile allocates.  */
  170.     *makefiles = dep_name (d);
  171.     ++num_makefiles;
  172.     ++makefiles;
  173.       }
  174.  
  175.   /* If there were no -f switches, try the default names.  */
  176.  
  177.   if (num_makefiles == 0)
  178.     {
  179.       static char *default_makefiles[] =
  180.     { "GNUmakefile", "makefile", "Makefile", 0 };
  181.       register char **p = default_makefiles;
  182.       while (*p != 0 && !file_exists_p (*p))
  183.     ++p;
  184.  
  185.       if (*p != 0)
  186.     {
  187.       if (! read_makefile (*p, 0))
  188.         perror_with_name ("", *p);
  189.     }
  190.       else
  191.     {
  192.       /* No default makefile was found.  Add the default makefiles to the
  193.          `read_makefiles' chain so they will be updated if possible.  */
  194.       struct dep *tail = read_makefiles;
  195.       for (p = default_makefiles; *p != 0; ++p)
  196.         {
  197.           struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
  198.           d->name = 0;
  199.           d->file = enter_file (*p);
  200.           d->file->dontcare = 1;
  201.           /* Tell update_goal_chain to bail out as soon as this file is
  202.          made, and main not to die if we can't make this file.  */
  203.           d->changed = RM_DONTCARE;
  204.           if (tail == 0)
  205.         read_makefiles = d;
  206.           else
  207.         tail->next = d;
  208.           tail = d;
  209.         }
  210.       if (tail != 0)
  211.         tail->next = 0;
  212.     }
  213.     }
  214.  
  215.   return read_makefiles;
  216. }
  217.  
  218. /* Read file FILENAME as a makefile and add its contents to the data base.
  219.  
  220.    FLAGS contains bits as above.
  221.  
  222.    FILENAME is added to the `read_makefiles' chain.
  223.  
  224.    Returns 1 if a file was found and read, 0 if not.  */
  225.  
  226. static int
  227. read_makefile (filename, flags)
  228.      char *filename;
  229.      int flags;
  230. {
  231.   static char *collapsed = 0;
  232.   static unsigned int collapsed_length = 0;
  233.   register FILE *infile;
  234.   struct linebuffer lb;
  235.   unsigned int commands_len = 200;
  236.   char *commands = (char *) xmalloc (200);
  237.   unsigned int commands_idx = 0;
  238.   unsigned int commands_started;
  239.   register char *p;
  240.   char *p2;
  241.   int ignoring = 0, in_ignored_define = 0;
  242.   int no_targets = 0;        /* Set when reading a rule without targets.  */
  243.   char *passed_filename = filename;
  244.  
  245.   struct nameseq *filenames = 0;
  246.   struct dep *deps;
  247.   unsigned int lineno = 1;
  248.   unsigned int nlines = 0;
  249.   int two_colon;
  250.   char *pattern = 0, *pattern_percent;
  251.  
  252.   int makefile_errno;
  253.  
  254. #define record_waiting_files()                              \
  255.   do                                          \
  256.     {                                           \
  257.       if (filenames != 0)                              \
  258.     record_files (filenames, pattern, pattern_percent, deps,          \
  259.               commands_started, commands, commands_idx,              \
  260.               two_colon, filename, lineno,                  \
  261.               !(flags & RM_NO_DEFAULT_GOAL));                       \
  262.       filenames = 0;                                  \
  263.       commands_idx = 0;                                  \
  264.       pattern = 0;                                  \
  265.     } while (0)
  266.  
  267. #ifdef    lint    /* Suppress `used before set' messages.  */
  268.   two_colon = 0;
  269. #endif
  270.  
  271.   if (debug_flag)
  272.     {
  273.       printf ("Reading makefile `%s'", filename);
  274.       if (flags & RM_NO_DEFAULT_GOAL)
  275.     printf (" (no default goal)");
  276.       if (flags & RM_INCLUDED)
  277.     printf (" (search path)");
  278.       if (flags & RM_DONTCARE)
  279.     printf (" (don't care)");
  280.       if (flags & RM_NO_TILDE)
  281.     printf (" (no ~ expansion)");
  282.       puts ("...");
  283.     }
  284.  
  285.   /* First, get a stream to read.  */
  286.  
  287.   /* Expand ~ in FILENAME unless it came from `include',
  288.      in which case it was already done.  */
  289.   if (!(flags & RM_NO_TILDE) && filename[0] == '~')
  290.     {
  291.       char *expanded = tilde_expand (filename);
  292.       if (expanded != 0)
  293.     filename = expanded;
  294.     }
  295.  
  296.   infile = fopen (filename, "r");
  297.   /* Save the error code so we print the right message later.  */
  298.   makefile_errno = errno;
  299.  
  300.   /* If the makefile wasn't found and it's either a makefile from
  301.      the `MAKEFILES' variable or an included makefile,
  302.      search the included makefile search path for this makefile.  */
  303.  
  304.   if (infile == 0 && (flags & RM_INCLUDED) && *filename != '/')
  305.     {
  306.       register unsigned int i;
  307.       for (i = 0; include_directories[i] != 0; ++i)
  308.     {
  309.       char *name = concat (include_directories[i], "/", filename);
  310.       infile = fopen (name, "r");
  311.       if (infile == 0)
  312.         free (name);
  313.       else
  314.         {
  315.           filename = name;
  316.           break;
  317.         }
  318.     }
  319.     }
  320.  
  321.   /* Add FILENAME to the chain of read makefiles.  */
  322.   deps = (struct dep *) xmalloc (sizeof (struct dep));
  323.   deps->next = read_makefiles;
  324.   read_makefiles = deps;
  325.   deps->name = 0;
  326.   deps->file = lookup_file (filename);
  327.   if (deps->file == 0)
  328.     {
  329.       deps->file = enter_file (savestring (filename, strlen (filename)));
  330.       if (flags & RM_DONTCARE)
  331.     deps->file->dontcare = 1;
  332.     }
  333.   if (filename != passed_filename)
  334.     free (filename);
  335.   filename = deps->file->name;
  336.   deps->changed = flags;
  337.   deps = 0;
  338.  
  339.   /* If the makefile can't be found at all, give up entirely.  */
  340.  
  341.   if (infile == 0)
  342.     {
  343.       /* If we did some searching, errno has the error from the last
  344.      attempt, rather from FILENAME itself.  Restore it in case the
  345.      caller wants to use it in a message.  */
  346.       errno = makefile_errno;
  347.       return 0;
  348.     }
  349.  
  350.   reading_filename = filename;
  351.   reading_lineno_ptr = &lineno;
  352.  
  353.   /* Loop over lines in the file.
  354.      The strategy is to accumulate target names in FILENAMES, dependencies
  355.      in DEPS and commands in COMMANDS.  These are used to define a rule
  356.      when the start of the next rule (or eof) is encountered.  */
  357.  
  358.   initbuffer (&lb);
  359.  
  360.   while (!feof (infile))
  361.     {
  362.       lineno += nlines;
  363.       nlines = readline (&lb, infile, filename, lineno);
  364.  
  365.       /* Check for a shell command line first.
  366.      If it is not one, we can stop treating tab specially.  */
  367.       if (lb.buffer[0] == '\t')
  368.     {
  369.       /* This line is a probably shell command.  */
  370.       unsigned int len;
  371.  
  372.       if (no_targets)
  373.         /* Ignore the commands in a rule with no targets.  */
  374.         continue;
  375.  
  376.       /* If there is no preceding rule line, don't treat this line
  377.          as a command, even though it begins with a tab character.
  378.          SunOS 4 make appears to behave this way.  */
  379.  
  380.       if (filenames != 0)
  381.         {
  382.           if (ignoring)
  383.         /* Yep, this is a shell command, and we don't care.  */
  384.         continue;
  385.  
  386.           /* Append this command line to the line being accumulated.  */
  387.           p = lb.buffer;
  388.           if (commands_idx == 0)
  389.         commands_started = lineno;
  390.           len = strlen (p);
  391.           if (len + 1 + commands_idx > commands_len)
  392.         {
  393.           commands_len = (len + 1 + commands_idx) * 2;
  394.           commands = (char *) xrealloc (commands, commands_len);
  395.         }
  396.           bcopy (p, &commands[commands_idx], len);
  397.           commands_idx += len;
  398.           commands[commands_idx++] = '\n';
  399.  
  400.           continue;
  401.         }
  402.     }
  403.  
  404.       /* This line is not a shell command line.  Don't worry about tabs.  */
  405.  
  406.       if (collapsed_length < lb.size)
  407.     {
  408.       collapsed_length = lb.size;
  409.       if (collapsed != 0)
  410.         free (collapsed);
  411.       collapsed = (char *) xmalloc (collapsed_length);
  412.     }
  413.       strcpy (collapsed, lb.buffer);
  414.       /* Collapse continuation lines.  */
  415.       collapse_continuations (collapsed);
  416.       remove_comments (collapsed);
  417.  
  418.       /* strncmp is first to avoid dereferencing out into space.  */
  419. #define    word1eq(s, l)     (!strncmp (s, p, l) \
  420.              && (p[l] == '\0' || isblank (p[l])))
  421.       p = collapsed;
  422.       while (isspace (*p))
  423.     ++p;
  424.       if (*p == '\0')
  425.     /* This line is completely empty.  */
  426.     continue;
  427.  
  428.       /* We must first check for conditional and `define' directives before
  429.      ignoring anything, since they control what we will do with
  430.      following lines.  */
  431.  
  432.       if (!in_ignored_define
  433.       && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
  434.           || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
  435.           || word1eq ("else", 4) || word1eq ("endif", 5)))
  436.     {
  437.       int i = conditional_line (p, filename, lineno);
  438.       if (i >= 0)
  439.         ignoring = i;
  440.       else
  441.         makefile_fatal (filename, lineno,
  442.                 "invalid syntax in conditional");
  443.       continue;
  444.     }
  445.       else if (word1eq ("endef", 5))
  446.     {
  447.       if (in_ignored_define)
  448.         in_ignored_define = 0;
  449.       else
  450.         makefile_fatal (filename, lineno, "extraneous `endef'");
  451.       continue;
  452.     }
  453.       else if (word1eq ("define", 6))
  454.     {
  455.       if (ignoring)
  456.         in_ignored_define = 1;
  457.       else
  458.         {
  459.           p2 = next_token (p + 6);
  460.           /* Let the variable name be the whole rest of the line,
  461.          with trailing blanks stripped (comments have already been
  462.          removed), so it could be a complex variable/function
  463.          reference that might contain blanks.  */
  464.           p = index (p2, '\0');
  465.           while (isblank (p[-1]))
  466.         --p;
  467.           lineno = do_define (p2, p - p2, o_file,
  468.                   lineno, infile, filename);
  469.         }
  470.       continue;
  471.     }
  472.       else if (word1eq ("override", 8))
  473.     {
  474.       p2 = next_token (p + 8);
  475.       if (p2 == 0)
  476.         makefile_error (filename, lineno, "empty `override' directive");
  477.       if (!strncmp (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
  478.         {
  479.           if (ignoring)
  480.         in_ignored_define = 1;
  481.           else
  482.         {
  483.           p2 = next_token (p2 + 6);
  484.           /* Let the variable name be the whole rest of the line,
  485.              with trailing blanks stripped (comments have already been
  486.              removed), so it could be a complex variable/function
  487.              reference that might contain blanks.  */
  488.           p = index (p2, '\0');
  489.           while (isblank (p[-1]))
  490.             --p;
  491.           lineno = do_define (p2, p - p2, o_override,
  492.                       lineno, infile, filename);
  493.         }
  494.         }
  495.       else if (!ignoring
  496.            && !try_variable_definition (filename, lineno,
  497.                         p2, o_override))
  498.         makefile_error (filename, lineno, "empty `override' directive");
  499.  
  500.       continue;
  501.     }
  502.  
  503.       if (ignoring)
  504.     /* Ignore the line.  We continue here so conditionals
  505.        can appear in the middle of a rule.  */
  506.     continue;
  507.       else if (word1eq ("export", 6))
  508.     {
  509.       struct variable *v;
  510.       p2 = next_token (p + 6);
  511.       if (*p2 == '\0')
  512.         export_all_variables = 1;
  513.       v = try_variable_definition (filename, lineno, p2, o_file);
  514.       if (v != 0)
  515.         v->export = v_export;
  516.       else
  517.         {
  518.           unsigned int len;
  519.           for (p = find_next_token (&p2, &len); p != 0;
  520.            p = find_next_token (&p2, &len))
  521.         {
  522.           v = lookup_variable (p, len);
  523.           if (v == 0)
  524.             v = define_variable (p, len, "", o_file, 0);
  525.           v->export = v_export;
  526.         }
  527.         }
  528.     }
  529.       else if (word1eq ("unexport", 8))
  530.     {
  531.       unsigned int len;
  532.       struct variable *v;
  533.       p2 = next_token (p + 8);
  534.       if (*p2 == '\0')
  535.         export_all_variables = 0;
  536.       for (p = find_next_token (&p2, &len); p != 0;
  537.            p = find_next_token (&p2, &len))
  538.         {
  539.           v = lookup_variable (p, len);
  540.           if (v == 0)
  541.         v = define_variable (p, len, "", o_file, 0);
  542.           v->export = v_noexport;
  543.         }
  544.     }
  545.       else if (word1eq ("include", 7) || word1eq ("-include", 8))
  546.     {
  547.       /* We have found an `include' line specifying a nested
  548.          makefile to be read at this point.  */
  549.       struct conditionals *save, new_conditionals;
  550.       struct nameseq *files;
  551.       /* "-include" (vs "include") says no
  552.          error if the file does not exist.  */
  553.       int noerror = p[0] == '-';
  554.  
  555.       p = allocated_variable_expand (next_token (p + (noerror ? 9 : 8)));
  556.       if (*p == '\0')
  557.         {
  558.           makefile_error (filename, lineno,
  559.                   "no file name for `%sinclude'",
  560.                   noerror ? "-" : "");
  561.           continue;
  562.         }
  563.  
  564.       /* Parse the list of file names.  */
  565.       p2 = p;
  566.       files = multi_glob (parse_file_seq (&p2, '\0',
  567.                           sizeof (struct nameseq),
  568.                           1),
  569.                   sizeof (struct nameseq));
  570.       free (p);
  571.  
  572.       /* Save the state of conditionals and start
  573.          the included makefile with a clean slate.  */
  574.       save = conditionals;
  575.       bzero ((char *) &new_conditionals, sizeof new_conditionals);
  576.       conditionals = &new_conditionals;
  577.  
  578.       /* Record the rules that are waiting so they will determine
  579.          the default goal before those in the included makefile.  */
  580.       record_waiting_files ();
  581.  
  582.       /* Read each included makefile.  */
  583.       while (files != 0)
  584.         {
  585.           struct nameseq *next = files->next;
  586.           char *name = files->name;
  587.           free (files);
  588.           files = next;
  589.  
  590.           if (! read_makefile (name, (RM_INCLUDED | RM_NO_TILDE
  591.                       | (noerror ? RM_DONTCARE : 0)))
  592.           && ! noerror)
  593.         makefile_error (filename, lineno,
  594.                 "%s: %s", name, strerror (errno));
  595.         }
  596.  
  597.       /* Free any space allocated by conditional_line.  */
  598.       if (conditionals->ignoring)
  599.         free (conditionals->ignoring);
  600.       if (conditionals->seen_else)
  601.         free (conditionals->seen_else);
  602.  
  603.       /* Restore state.  */
  604.       conditionals = save;
  605.       reading_filename = filename;
  606.       reading_lineno_ptr = &lineno;
  607.     }
  608. #if NeXT || NeXT_PDO
  609.       else if ((next_flag & NEXT_FINDFILE_FLAG) && word1eq ("findfile", 8))
  610.         {
  611.           /* A `findfile' has been found.
  612.              Handle it as above, but don't choke if the file
  613.              doesn't really exist.  My quick hack is to treat
  614.              it just like a Makefile from the MAKEFILES
  615.              environment variable. */
  616.           struct conditionals *save = conditionals;
  617.           struct conditionals new_conditionals;
  618.           p = allocated_variable_expand (next_token (p + 8));
  619.           if (*p == '\0') {
  620.               makefile_error (filename, lineno, "no filename for `findfile'");
  621.               continue;
  622.       }
  623.           p2 = end_of_token (p);
  624.           if (*p2 != '\0') {
  625.               *p2++ = '\0';
  626.               if (*next_token (p2) != '\0')
  627.                 makefile_error(filename, lineno,
  628.                                "extraneous text after `findfile'");
  629.       }
  630.       if (*p == '/') {
  631.           makefile_error (filename, lineno, 
  632.                   "filename for `findfile' begins with `/'");
  633.           free(p);
  634.           continue;
  635.       }
  636.       {
  637.           char *prefix = concat("./", "", "");
  638.           char *name;
  639.           struct stat s;
  640.  
  641.           while (1) {
  642.           name = concat (prefix, p, "");
  643.           if (stat(name, &s) >= 0 && (s.st_mode & S_IFMT) == S_IFREG)
  644.               break;
  645.           free(name);
  646.           name = concat("../", prefix, "");
  647.           free(prefix);
  648.           prefix = name;
  649.           name = 0;
  650.           if (stat(prefix, &s) < 0)
  651.               break;
  652.           }
  653.           free(prefix);
  654.           if (name == 0) {
  655.           makefile_error(filename, lineno, "can't find `%s' ", p);
  656.           free(p);
  657.           continue;
  658.           }
  659.           free(p);
  660.           p = name;
  661.       }
  662.  
  663.           bzero ((char *) &new_conditionals, sizeof new_conditionals);
  664.           conditionals = &new_conditionals;
  665.           /* Record the rules that are waiting so they will determine
  666.              the default goal before those in the included makefile.  */
  667.           record_waiting_files ();
  668.           read_makefile (p, 1);
  669.           free (p);
  670.           conditionals = save;
  671.           reading_filename = filename;
  672.           reading_lineno_ptr = &lineno;
  673.           continue;
  674.         }
  675. #endif    /* NeXT */
  676.       else if (word1eq ("vpath", 5))
  677.     {
  678.       char *pattern;
  679.       unsigned int len;
  680.       p2 = variable_expand (p + 5);
  681.       p = find_next_token (&p2, &len);
  682.       if (p != 0)
  683.         {
  684.           pattern = savestring (p, len);
  685.           p = find_next_token (&p2, &len);
  686.           /* No searchpath means remove all previous
  687.          selective VPATH's with the same pattern.  */
  688.         }
  689.       else
  690.         /* No pattern means remove all previous selective VPATH's.  */
  691.         pattern = 0;
  692.       construct_vpath_list (pattern, p);
  693.       if (pattern != 0)
  694.         free (pattern);
  695.     }
  696. #undef    word1eq
  697.       else if (try_variable_definition (filename, lineno, p, o_file))
  698.     /* This line has been dealt with.  */
  699.     ;
  700.       else if (lb.buffer[0] == '\t')
  701.     {
  702.       p = collapsed;    /* Ignore comments.  */
  703.       while (isblank (*p))
  704.         ++p;
  705.       if (*p == '\0')
  706.         /* The line is completely blank; that is harmless.  */
  707.         continue;
  708.       /* This line starts with a tab but was not caught above
  709.          because there was no preceding target, and the line
  710.          might have been usable as a variable definition.
  711.          But now it is definitely lossage.  */
  712.       makefile_fatal (filename, lineno,
  713.               "commands commence before first target");
  714.     }
  715.       else
  716.     {
  717.       /* This line describes some target files.  */
  718.  
  719.       char *cmdleft;
  720.  
  721.       /* Record the previous rule.  */
  722.  
  723.       record_waiting_files ();
  724.  
  725.       /* Look for a semicolon in the unexpanded line.  */
  726.       cmdleft = find_semicolon (lb.buffer);
  727.       if (cmdleft != 0)
  728.         /* Found one.  Cut the line short there before expanding it.  */
  729.         *cmdleft = '\0';
  730.  
  731.       collapse_continuations (lb.buffer);
  732.  
  733.       /* Expand variable and function references before doing anything
  734.          else so that special characters can be inside variables.  */
  735.       p = variable_expand (lb.buffer);
  736.  
  737.       if (cmdleft == 0)
  738.         /* Look for a semicolon in the expanded line.  */
  739.         cmdleft = find_semicolon (p);
  740.  
  741.       if (cmdleft != 0)
  742.         /* Cut the line short at the semicolon.  */
  743.         *cmdleft = '\0';
  744.  
  745.       /* Remove comments from the line.  */
  746.       remove_comments (p);
  747.  
  748.       p2 = next_token (p);
  749.       if (*p2 == '\0')
  750.         {
  751.           if (cmdleft != 0)
  752.         makefile_fatal (filename, lineno,
  753.                 "missing rule before commands");
  754.           else
  755.         /* This line contained a variable reference that
  756.            expanded to nothing but whitespace.  */
  757.         continue;
  758.         }
  759.       else if (*p2 == ':')
  760.         {
  761.           /* We accept and ignore rules without targets for
  762.          compatibility with SunOS 4 make.  */
  763.           no_targets = 1;
  764.           continue;
  765.         }
  766.  
  767.       filenames = multi_glob (parse_file_seq (&p2, ':',
  768.                           sizeof (struct nameseq),
  769.                           1),
  770.                   sizeof (struct nameseq));
  771.       if (*p2++ == '\0')
  772.         makefile_fatal (filename, lineno, "missing separator");
  773.       /* Is this a one-colon or two-colon entry?  */
  774.       two_colon = *p2 == ':';
  775.       if (two_colon)
  776.         p2++;
  777.  
  778.       /* We have some targets, so don't ignore the following commands.  */
  779.       no_targets = 0;
  780.  
  781.       /* Is this a static pattern rule: `target: %targ: %dep; ...'?  */
  782.       p = index (p2, ':');
  783.           while (p != 0)
  784.             {
  785.             /* Skip over a ':' escaped by a backslash */
  786.             if (p[-1] == '\\')
  787.               {
  788.               register char *q = &p[-1];
  789.               register int backslash = 0;
  790.               while (*q-- == '\\')
  791.                 backslash = !backslash;
  792.               if (backslash)
  793.                 p = index (p+1, ':');
  794.               else
  795.                 break;
  796.               }
  797. #if defined (__MSDOS__) || defined (WIN32)
  798.             /* Skip over a ':' drive specifier */
  799.             /* I should probably ensure that p[-2] is blank, but */
  800.             /* I don't want to risk a seg fault.                 */
  801.             else if (p != 0 && (p[1] == '/' || p[1] == '\\') && isalpha (p[-1]))
  802.               {
  803.               p = index (p+1, ':');
  804.               }
  805. #endif
  806.             /* If neither of the above, stop skipping */
  807.             else
  808.               {
  809.               break;
  810.               }
  811.             }
  812.       if (p != 0)
  813.         {
  814.           struct nameseq *target;
  815.           target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
  816.           ++p2;
  817.           if (target == 0)
  818.         makefile_fatal (filename, lineno, "missing target pattern");
  819.           else if (target->next != 0)
  820.         makefile_fatal (filename, lineno, "multiple target patterns");
  821.           pattern = target->name;
  822.           pattern_percent = find_percent (pattern);
  823.           if (pattern_percent == 0)
  824.         makefile_fatal (filename, lineno,
  825.                 "target pattern contains no `%%'");
  826.         }
  827.       else
  828.         pattern = 0;
  829.  
  830.       /* Parse the dependencies.  */
  831.       deps = (struct dep *)
  832.         multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
  833.             sizeof (struct dep));
  834.  
  835.       commands_idx = 0;
  836.       if (cmdleft != 0)
  837.         {
  838.           /* Semicolon means rest of line is a command.  */
  839.           unsigned int len = strlen (cmdleft + 1);
  840.  
  841.           commands_started = lineno;
  842.  
  843.           /* Add this command line to the buffer.  */
  844.           if (len + 2 > commands_len)
  845.         {
  846.           commands_len = (len + 2) * 2;
  847.           commands = (char *) xrealloc (commands, commands_len);
  848.         }
  849.           bcopy (cmdleft + 1, commands, len);
  850.           commands_idx += len;
  851.           commands[commands_idx++] = '\n';
  852.         }
  853.  
  854.       continue;
  855.     }
  856.  
  857.       /* We get here except in the case that we just read a rule line.
  858.      Record now the last rule we read, so following spurious
  859.      commands are properly diagnosed.  */
  860.       record_waiting_files ();
  861.       no_targets = 0;
  862.     }
  863.  
  864.   if (conditionals->if_cmds)
  865.     makefile_fatal (filename, lineno, "missing `endif'");
  866.  
  867.   /* At eof, record the last rule.  */
  868.   record_waiting_files ();
  869.  
  870.   freebuffer (&lb);
  871.   free ((char *) commands);
  872.   fclose (infile);
  873.  
  874.   reading_filename = 0;
  875.   reading_lineno_ptr = 0;
  876.  
  877.   return 1;
  878. }
  879.  
  880. /* Execute a `define' directive.
  881.    The first line has already been read, and NAME is the name of
  882.    the variable to be defined.  The following lines remain to be read.
  883.    LINENO, INFILE and FILENAME refer to the makefile being read.
  884.    The value returned is LINENO, updated for lines read here.  */
  885.  
  886. static unsigned int
  887. do_define (name, namelen, origin, lineno, infile, filename)
  888.      char *name;
  889.      unsigned int namelen;
  890.      enum variable_origin origin;
  891.      unsigned int lineno;
  892.      FILE *infile;
  893.      char *filename;
  894. {
  895.   struct linebuffer lb;
  896.   unsigned int nlines = 0;
  897.   unsigned int length = 100;
  898.   char *definition = (char *) xmalloc (100);
  899.   register unsigned int idx = 0;
  900.   register char *p;
  901.  
  902.   /* Expand the variable name.  */
  903.   char *var = (char *) alloca (namelen + 1);
  904.   bcopy (name, var, namelen);
  905.   var[namelen] = '\0';
  906.   var = variable_expand (var);
  907.  
  908.   initbuffer (&lb);
  909.   while (!feof (infile))
  910.     {
  911.       lineno += nlines;
  912.       nlines = readline (&lb, infile, filename, lineno);
  913.  
  914.       collapse_continuations (lb.buffer);
  915.  
  916.       p = next_token (lb.buffer);
  917.       if ((p[5] == '\0' || isblank (p[5])) && !strncmp (p, "endef", 5))
  918.     {
  919.       p += 5;
  920.       remove_comments (p);
  921.       if (*next_token (p) != '\0')
  922.         makefile_error (filename, lineno,
  923.                 "Extraneous text after `endef' directive");
  924.       /* Define the variable.  */
  925.       if (idx == 0)
  926.         definition[0] = '\0';
  927.       else
  928.         definition[idx - 1] = '\0';
  929.       (void) define_variable (var, strlen (var), definition, origin, 1);
  930.       free (definition);
  931.       freebuffer (&lb);
  932.       return lineno;
  933.     }
  934.       else
  935.     {
  936.       unsigned int len = strlen (lb.buffer);
  937.  
  938.       /* Increase the buffer size if necessary.  */
  939.       if (idx + len + 1 > length)
  940.         {
  941.           length = (idx + len) * 2;
  942.           definition = (char *) xrealloc (definition, length + 1);
  943.         }
  944.  
  945.       bcopy (lb.buffer, &definition[idx], len);
  946.       idx += len;
  947.       /* Separate lines with a newline.  */
  948.       definition[idx++] = '\n';
  949.     }
  950.     }
  951.  
  952.   /* No `endef'!!  */
  953.   makefile_fatal (filename, lineno, "missing `endef', unterminated `define'");
  954.  
  955.   /* NOTREACHED */
  956.   return 0;
  957. }
  958.  
  959. /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
  960.    "ifneq", "else" and "endif".
  961.    LINE is the input line, with the command as its first word.
  962.  
  963.    FILENAME and LINENO are the filename and line number in the
  964.    current makefile.  They are used for error messages.
  965.  
  966.    Value is -1 if the line is invalid,
  967.    0 if following text should be interpreted,
  968.    1 if following text should be ignored.  */
  969.  
  970. static int
  971. conditional_line (line, filename, lineno)
  972.      char *line;
  973.      char *filename;
  974.      unsigned int lineno;
  975. {
  976.   int notdef;
  977.   char *cmdname;
  978.   register unsigned int i;
  979.  
  980.   if (*line == 'i')
  981.     {
  982.       /* It's an "if..." command.  */
  983.       notdef = line[2] == 'n';
  984.       if (notdef)
  985.     {
  986.       cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
  987.       line += cmdname[3] == 'd' ? 7 : 6;
  988.     }
  989.       else
  990.     {
  991.       cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
  992.       line += cmdname[2] == 'd' ? 6 : 5;
  993.     }
  994.     }
  995.   else
  996.     {
  997.       /* It's an "else" or "endif" command.  */
  998.       notdef = line[1] == 'n';
  999.       cmdname = notdef ? "endif" : "else";
  1000.       line += notdef ? 5 : 4;
  1001.     }
  1002.  
  1003.   line = next_token (line);
  1004.  
  1005.   if (*cmdname == 'e')
  1006.     {
  1007.       if (*line != '\0')
  1008.     makefile_error (filename, lineno,
  1009.             "Extraneous text after `%s' directive",
  1010.             cmdname);
  1011.       /* "Else" or "endif".  */
  1012.       if (conditionals->if_cmds == 0)
  1013.     makefile_fatal (filename, lineno, "extraneous `%s'", cmdname);
  1014.       /* NOTDEF indicates an `endif' command.  */
  1015.       if (notdef)
  1016.     --conditionals->if_cmds;
  1017.       else if (conditionals->seen_else[conditionals->if_cmds - 1])
  1018.     makefile_fatal (filename, lineno, "only one `else' per conditional");
  1019.       else
  1020.     {
  1021.       /* Toggle the state of ignorance.  */
  1022.       conditionals->ignoring[conditionals->if_cmds - 1]
  1023.         = !conditionals->ignoring[conditionals->if_cmds - 1];
  1024.       /* Record that we have seen an `else' in this conditional.
  1025.          A second `else' will be erroneous.  */
  1026.       conditionals->seen_else[conditionals->if_cmds - 1] = 1;
  1027.     }
  1028.       for (i = 0; i < conditionals->if_cmds; ++i)
  1029.     if (conditionals->ignoring[i])
  1030.       return 1;
  1031.       return 0;
  1032.     }
  1033.  
  1034.   if (conditionals->allocated == 0)
  1035.     {
  1036.       conditionals->allocated = 5;
  1037.       conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
  1038.       conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
  1039.     }
  1040.  
  1041.   ++conditionals->if_cmds;
  1042.   if (conditionals->if_cmds > conditionals->allocated)
  1043.     {
  1044.       conditionals->allocated += 5;
  1045.       conditionals->ignoring = (char *)
  1046.     xrealloc (conditionals->ignoring, conditionals->allocated);
  1047.       conditionals->seen_else = (char *)
  1048.     xrealloc (conditionals->seen_else, conditionals->allocated);
  1049.     }
  1050.  
  1051.   /* Record that we have seen an `if...' but no `else' so far.  */
  1052.   conditionals->seen_else[conditionals->if_cmds - 1] = 0;
  1053.  
  1054.   /* Search through the stack to see if we're already ignoring.  */
  1055.   for (i = 0; i < conditionals->if_cmds - 1; ++i)
  1056.     if (conditionals->ignoring[i])
  1057.       {
  1058.     /* We are already ignoring, so just push a level
  1059.        to match the next "else" or "endif", and keep ignoring.
  1060.        We don't want to expand variables in the condition.  */
  1061.     conditionals->ignoring[conditionals->if_cmds - 1] = 1;
  1062.     return 1;
  1063.       }
  1064.  
  1065.   if (cmdname[notdef ? 3 : 2] == 'd')
  1066.     {
  1067.       /* "Ifdef" or "ifndef".  */
  1068.       struct variable *v;
  1069.       register char *p = end_of_token (line);
  1070.       i = p - line;
  1071.       p = next_token (p);
  1072.       if (*p != '\0')
  1073.     return -1;
  1074.       v = lookup_variable (line, i);
  1075.       conditionals->ignoring[conditionals->if_cmds - 1]
  1076.     = (v != 0 && *v->value != '\0') == notdef;
  1077.     }
  1078.   else
  1079.     {
  1080.       /* "Ifeq" or "ifneq".  */
  1081.       char *s1, *s2;
  1082.       unsigned int len;
  1083.       char termin = *line == '(' ? ',' : *line;
  1084.  
  1085.       if (termin != ',' && termin != '"' && termin != '\'')
  1086.     return -1;
  1087.  
  1088.       s1 = ++line;
  1089.       /* Find the end of the first string.  */
  1090.       if (termin == ',')
  1091.     {
  1092.       register int count = 0;
  1093.       for (; *line != '\0'; ++line)
  1094.         if (*line == '(')
  1095.           ++count;
  1096.         else if (*line == ')')
  1097.           --count;
  1098.         else if (*line == ',' && count <= 0)
  1099.           break;
  1100.     }
  1101.       else
  1102.     while (*line != '\0' && *line != termin)
  1103.       ++line;
  1104.  
  1105.       if (*line == '\0')
  1106.     return -1;
  1107.  
  1108.       *line++ = '\0';
  1109.  
  1110.       s2 = variable_expand (s1);
  1111.       /* We must allocate a new copy of the expanded string because
  1112.      variable_expand re-uses the same buffer.  */
  1113.       len = strlen (s2);
  1114.       s1 = (char *) alloca (len + 1);
  1115.       bcopy (s2, s1, len + 1);
  1116.  
  1117.       if (termin != ',')
  1118.     /* Find the start of the second string.  */
  1119.     line = next_token (line);
  1120.  
  1121.       termin = termin == ',' ? ')' : *line;
  1122.       if (termin != ')' && termin != '"' && termin != '\'')
  1123.     return -1;
  1124.  
  1125.       /* Find the end of the second string.  */
  1126.       if (termin == ')')
  1127.     {
  1128.       register int count = 0;
  1129.       s2 = next_token (line);
  1130.       for (line = s2; *line != '\0'; ++line)
  1131.         {
  1132.           if (*line == '(')
  1133.         ++count;
  1134.           else if (*line == ')')
  1135.         if (count <= 0)
  1136.           break;
  1137.         else
  1138.           --count;
  1139.         }
  1140.     }
  1141.       else
  1142.     {
  1143.       ++line;
  1144.       s2 = line;
  1145.       while (*line != '\0' && *line != termin)
  1146.         ++line;
  1147.     }
  1148.  
  1149.       if (*line == '\0')
  1150.     return -1;
  1151.  
  1152.       *line = '\0';
  1153.       line = next_token (++line);
  1154.       if (*line != '\0')
  1155.     makefile_error (filename, lineno,
  1156.             "Extraneous text after `%s' directive",
  1157.             cmdname);
  1158.  
  1159.       s2 = variable_expand (s2);
  1160.       conditionals->ignoring[conditionals->if_cmds - 1]
  1161.     = streq (s1, s2) == notdef;
  1162.     }
  1163.  
  1164.   /* Search through the stack to see if we're ignoring.  */
  1165.   for (i = 0; i < conditionals->if_cmds; ++i)
  1166.     if (conditionals->ignoring[i])
  1167.       return 1;
  1168.   return 0;
  1169. }
  1170.  
  1171. /* Remove duplicate dependencies in CHAIN.  */
  1172.  
  1173. void
  1174. uniquize_deps (chain)
  1175.      struct dep *chain;
  1176. {
  1177.   register struct dep *d;
  1178.  
  1179.   /* Make sure that no dependencies are repeated.  This does not
  1180.      really matter for the purpose of updating targets, but it
  1181.      might make some names be listed twice for $^ and $?.  */
  1182.  
  1183.   for (d = chain; d != 0; d = d->next)
  1184.     {
  1185.       struct dep *last, *next;
  1186.  
  1187.       last = d;
  1188.       next = d->next;
  1189.       while (next != 0)
  1190.     if (streq (dep_name (d), dep_name (next)))
  1191.       {
  1192.         struct dep *n = next->next;
  1193.         last->next = n;
  1194.         if (next->name != 0 && next->name != d->name)
  1195.           free (next->name);
  1196.         if (next != d)
  1197.           free ((char *) next);
  1198.         next = n;
  1199.       }
  1200.     else
  1201.       {
  1202.         last = next;
  1203.         next = next->next;
  1204.       }
  1205.     }
  1206. }
  1207.  
  1208. /* Record a description line for files FILENAMES,
  1209.    with dependencies DEPS, commands to execute described
  1210.    by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
  1211.    TWO_COLON is nonzero if a double colon was used.
  1212.    If not nil, PATTERN is the `%' pattern to make this
  1213.    a static pattern rule, and PATTERN_PERCENT is a pointer
  1214.    to the `%' within it.
  1215.  
  1216.    The links of FILENAMES are freed, and so are any names in it
  1217.    that are not incorporated into other data structures.  */
  1218.  
  1219. static void
  1220. record_files (filenames, pattern, pattern_percent, deps, commands_started,
  1221.           commands, commands_idx, two_colon, filename, lineno, set_default)
  1222.      struct nameseq *filenames;
  1223.      char *pattern, *pattern_percent;
  1224.      struct dep *deps;
  1225.      unsigned int commands_started;
  1226.      char *commands;
  1227.      unsigned int commands_idx;
  1228.      int two_colon;
  1229.      char *filename;
  1230.      unsigned int lineno;
  1231.      int set_default;
  1232. {
  1233.   struct nameseq *nextf;
  1234.   int implicit = 0;
  1235.   unsigned int max_targets, target_idx;
  1236.   char **targets = 0, **target_percents = 0;
  1237.   struct commands *cmds;
  1238.  
  1239.   if (commands_idx > 0)
  1240.     {
  1241.       cmds = (struct commands *) xmalloc (sizeof (struct commands));
  1242.       cmds->filename = filename;
  1243.       cmds->lineno = commands_started;
  1244.       cmds->commands = savestring (commands, commands_idx);
  1245.       cmds->command_lines = 0;
  1246.     }
  1247.   else
  1248.     cmds = 0;
  1249.  
  1250.   for (; filenames != 0; filenames = nextf)
  1251.     {
  1252.       register char *name = filenames->name;
  1253.       register struct file *f;
  1254.       register struct dep *d;
  1255.       struct dep *this;
  1256.       char *implicit_percent;
  1257.  
  1258.       nextf = filenames->next;
  1259.       free ((char *) filenames);
  1260.  
  1261.       implicit_percent = find_percent (name);
  1262.       implicit |= implicit_percent != 0;
  1263.  
  1264.       if (implicit && pattern != 0)
  1265.     makefile_fatal (filename, lineno,
  1266.             "mixed implicit and static pattern rules");
  1267.  
  1268.       if (implicit && implicit_percent == 0)
  1269.     makefile_fatal (filename, lineno, "mixed implicit and normal rules");
  1270.  
  1271.       if (implicit)
  1272.     {
  1273.       if (targets == 0)
  1274.         {
  1275.           max_targets = 5;
  1276.           targets = (char **) xmalloc (5 * sizeof (char *));
  1277.           target_percents = (char **) xmalloc (5 * sizeof (char *));
  1278.           target_idx = 0;
  1279.         }
  1280.       else if (target_idx == max_targets - 1)
  1281.         {
  1282.           max_targets += 5;
  1283.           targets = (char **) xrealloc ((char *) targets,
  1284.                         max_targets * sizeof (char *));
  1285.           target_percents
  1286.         = (char **) xrealloc ((char *) target_percents,
  1287.                       max_targets * sizeof (char *));
  1288.         }
  1289.       targets[target_idx] = name;
  1290.       target_percents[target_idx] = implicit_percent;
  1291.       ++target_idx;
  1292.       continue;
  1293.     }
  1294.  
  1295.       /* If there are multiple filenames, copy the chain DEPS
  1296.      for all but the last one.  It is not safe for the same deps
  1297.      to go in more than one place in the data base.  */
  1298.       this = nextf != 0 ? copy_dep_chain (deps) : deps;
  1299.  
  1300.       if (pattern != 0)
  1301.     /* If this is an extended static rule:
  1302.        `targets: target%pattern: dep%pattern; cmds',
  1303.        translate each dependency pattern into a plain filename
  1304.        using the target pattern and this target's name.  */
  1305.     if (!pattern_matches (pattern, pattern_percent, name))
  1306.       {
  1307.         /* Give a warning if the rule is meaningless.  */
  1308.         makefile_error (filename, lineno,
  1309.                 "target `%s' doesn't match the target pattern",
  1310.                 name);
  1311.         this = 0;
  1312.       }
  1313.     else
  1314.       {
  1315.         /* We use patsubst_expand to do the work of translating
  1316.            the target pattern, the target's name and the dependencies'
  1317.            patterns into plain dependency names.  */
  1318.         char *buffer = variable_expand ("");
  1319.  
  1320.         for (d = this; d != 0; d = d->next)
  1321.           {
  1322.         char *o;
  1323.         char *percent = find_percent (d->name);
  1324.         if (percent == 0)
  1325.           continue;
  1326.         o = patsubst_expand (buffer, name, pattern, d->name,
  1327.                      pattern_percent, percent);
  1328.         free (d->name);
  1329.         d->name = savestring (buffer, o - buffer);
  1330.           }
  1331.       }
  1332.       
  1333.       if (!two_colon)
  1334.     {
  1335.       /* Single-colon.  Combine these dependencies
  1336.          with others in file's existing record, if any.  */
  1337.       f = enter_file (name);
  1338.  
  1339.       if (f->double_colon)
  1340.         makefile_fatal (filename, lineno,
  1341.                 "target file `%s' has both : and :: entries",
  1342.                 f->name);
  1343.  
  1344. #if NeXT ||  NeXT_PDO
  1345.           if (!(next_flag & NEXT_QUIET_FLAG))
  1346. #endif
  1347.              /* If CMDS == F->CMDS, this target was listed in this rule
  1348.                 more than once.  Just give a warning since this is harmless.  */
  1349.              if (cmds != 0 && cmds == f->cmds)
  1350.                 makefile_error
  1351.                    (filename, lineno,
  1352.                     "target `%s' given more than once in the same rule.",
  1353.                     f->name);
  1354.  
  1355.  
  1356.       /* Check for two single-colon entries both with commands.
  1357.          Check is_target so that we don't lose on files such as .c.o
  1358.          whose commands were preinitialized.  */
  1359.       else if (cmds != 0 && f->cmds != 0 && f->is_target)
  1360.         {
  1361. #if NeXT ||  NeXT_PDO
  1362.           if (!(next_flag & NEXT_QUIET_FLAG)) {
  1363.         makefile_error (cmds->filename, cmds->lineno,
  1364.                 "warning: overriding commands for target `%s'",
  1365.                 f->name);
  1366.         makefile_error (f->cmds->filename, f->cmds->lineno,
  1367.                 "warning: ignoring old commands for target `%s'",
  1368.                 f->name);
  1369.         }
  1370. #else /* NeXT || NeXT_PDO */
  1371.           makefile_error (cmds->filename, cmds->lineno,
  1372.                   "warning: overriding commands for target `%s'",
  1373.                   f->name);
  1374.           makefile_error (f->cmds->filename, f->cmds->lineno,
  1375.                   "warning: ignoring old commands for target `%s'",
  1376.                   f->name);
  1377. #endif /* NeXT || NeXT_PDO */
  1378.         }
  1379.  
  1380.       f->is_target = 1;
  1381.  
  1382.       /* Defining .DEFAULT with no deps or cmds clears it.  */
  1383.       if (f == default_file && this == 0 && cmds == 0)
  1384.         f->cmds = 0;
  1385.       if (cmds != 0)
  1386.         f->cmds = cmds;
  1387.       /* Defining .SUFFIXES with no dependencies
  1388.          clears out the list of suffixes.  */
  1389.       if (f == suffix_file && this == 0)
  1390.         {
  1391.           d = f->deps;
  1392.           while (d != 0)
  1393.         {
  1394.           struct dep *nextd = d->next;
  1395.            free (d->name);
  1396.            free (d);
  1397.           d = nextd;
  1398.         }
  1399.           f->deps = 0;
  1400.         }
  1401.       else if (f->deps != 0)
  1402.         {
  1403.           /* Add the file's old deps and the new ones in THIS together.  */
  1404.  
  1405.           struct dep *firstdeps, *moredeps;
  1406.           if (cmds != 0)
  1407.         {
  1408.           /* This is the rule with commands, so put its deps first.
  1409.              The rationale behind this is that $< expands to the
  1410.              first dep in the chain, and commands use $< expecting
  1411.              to get the dep that rule specifies.  */
  1412.           firstdeps = this;
  1413.           moredeps = f->deps;
  1414.         }
  1415.           else
  1416.         {
  1417.           /* Append the new deps to the old ones.  */
  1418.           firstdeps = f->deps;
  1419.           moredeps = this;
  1420.         }
  1421.  
  1422.           if (firstdeps == 0)
  1423.         firstdeps = moredeps;
  1424.           else
  1425.         {
  1426.           d = firstdeps;
  1427.           while (d->next != 0)
  1428.             d = d->next;
  1429.           d->next = moredeps;
  1430.         }
  1431.  
  1432.           f->deps = firstdeps;
  1433.         }
  1434.       else
  1435.         f->deps = this;
  1436.  
  1437.       /* If this is a static pattern rule, set the file's stem to
  1438.          the part of its name that matched the `%' in the pattern,
  1439.          so you can use $* in the commands.  */
  1440.       if (pattern != 0)
  1441.         {
  1442.           static char *percent = "%";
  1443.           char *buffer = variable_expand ("");
  1444.           char *o = patsubst_expand (buffer, name, pattern, percent,
  1445.                      pattern_percent, percent);
  1446.           f->stem = savestring (buffer, o - buffer);
  1447.         }
  1448.     }
  1449.       else
  1450.     {
  1451.       /* Double-colon.  Make a new record
  1452.          even if the file already has one.  */
  1453.       f = lookup_file (name);
  1454.       /* Check for both : and :: rules.  Check is_target so
  1455.          we don't lose on default suffix rules or makefiles.  */
  1456.       if (f != 0 && f->is_target && !f->double_colon)
  1457.         makefile_fatal (filename, lineno,
  1458.                 "target file `%s' has both : and :: entries",
  1459.                 f->name);
  1460.       f = enter_file (name);
  1461.       /* If there was an existing entry and it was a double-colon
  1462.          entry, enter_file will have returned a new one, making it the
  1463.          prev pointer of the old one, and setting its double_colon
  1464.          pointer to the first one.  */
  1465.       if (f->double_colon == 0)
  1466.         /* This is the first entry for this name, so we must
  1467.            set its double_colon pointer to itself.  */
  1468.         f->double_colon = f;
  1469.       f->is_target = 1;
  1470.       f->deps = this;
  1471.       f->cmds = cmds;
  1472.     }
  1473.  
  1474.       /* Free name if not needed further.  */
  1475.       if (f != 0 && name != f->name
  1476.       && (name < f->name || name > f->name + strlen (f->name)))
  1477.     {
  1478.       free (name);
  1479.       name = f->name;
  1480.     }
  1481.  
  1482.       /* See if this is first target seen whose name does
  1483.      not start with a `.', unless it contains a slash.  */
  1484.       if (default_goal_file == 0 && set_default
  1485.       && (*name != '.' || index (name, '/') != 0))
  1486.     {
  1487.       int reject = 0;
  1488.  
  1489.       /* If this file is a suffix, don't
  1490.          let it be the default goal file.  */
  1491.  
  1492.       for (d = suffix_file->deps; d != 0; d = d->next)
  1493.         {
  1494.           register struct dep *d2;
  1495.           if (*dep_name (d) != '.' && streq (name, dep_name (d)))
  1496.         {
  1497.           reject = 1;
  1498.           break;
  1499.         }
  1500.           for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
  1501.         {
  1502.           register unsigned int len = strlen (dep_name (d2));
  1503.           if (strncmp (name, dep_name (d2), len))
  1504.             continue;
  1505.           if (streq (name + len, dep_name (d)))
  1506.             {
  1507.               reject = 1;
  1508.               break;
  1509.             }
  1510.         }
  1511.           if (reject)
  1512.         break;
  1513.         }
  1514.  
  1515.       if (!reject)
  1516.         default_goal_file = f;
  1517.     }
  1518.     }
  1519.  
  1520.   if (implicit)
  1521.     {
  1522.       targets[target_idx] = 0;
  1523.       target_percents[target_idx] = 0;
  1524.       create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
  1525.       free ((char *) target_percents);
  1526.     }
  1527. }
  1528.  
  1529. /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
  1530.    Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
  1531.    Quoting backslashes are removed from STRING by compacting it into
  1532.    itself.  Returns a pointer to the first unquoted STOPCHAR if there is
  1533.    one, or nil if there are none.  */
  1534.  
  1535. char *
  1536. find_char_unquote (string, stopchars, blank)
  1537.      char *string;
  1538.      char *stopchars;
  1539.      int blank;
  1540. {
  1541.   unsigned int string_len = strlen (string);
  1542.   register char *p = string;
  1543.  
  1544.   while (1)
  1545.     {
  1546.       while (*p != '\0' && index (stopchars, *p) == 0
  1547.          && (!blank || !isblank (*p)))
  1548.     ++p;
  1549.       if (*p == '\0')
  1550.     break;
  1551.  
  1552.       if (p > string && p[-1] == '\\')
  1553.     {
  1554.       /* Search for more backslashes.  */
  1555.       register int i = -2;
  1556.       while (&p[i] >= string && p[i] == '\\')
  1557.         --i;
  1558.       ++i;
  1559.       /* The number of backslashes is now -I.
  1560.          Copy P over itself to swallow half of them.  */
  1561.       bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
  1562.       p += i / 2;
  1563.       if (i % 2 == 0)
  1564.         /* All the backslashes quoted each other; the STOPCHAR was
  1565.            unquoted.  */
  1566.         return p;
  1567.  
  1568.       /* The STOPCHAR was quoted by a backslash.  Look for another.  */
  1569.     }
  1570.       else
  1571.     /* No backslash in sight.  */
  1572.     return p;
  1573.     }
  1574.  
  1575.   /* Never hit a STOPCHAR or blank (with BLANK nonzero).  */
  1576.   return 0;
  1577. }
  1578.  
  1579. /* Search PATTERN for an unquoted %.  */
  1580.  
  1581. char *
  1582. find_percent (pattern)
  1583.      char *pattern;
  1584. {
  1585.   return find_char_unquote (pattern, "%", 0);
  1586. }
  1587.  
  1588. /* Search STRING for an unquoted ; that is not after an unquoted #.  */
  1589.  
  1590. static char *
  1591. find_semicolon (string)
  1592.      char *string;
  1593. {
  1594.   char *match = find_char_unquote (string, ";#", 0);
  1595.   if (match != 0 && *match == '#')
  1596.     /* We found a comment before a semicolon.  No match.  */
  1597.     match = 0;
  1598.   return match;
  1599. }
  1600.  
  1601. /* Parse a string into a sequence of filenames represented as a
  1602.    chain of struct nameseq's in reverse order and return that chain.
  1603.  
  1604.    The string is passed as STRINGP, the address of a string pointer.
  1605.    The string pointer is updated to point at the first character
  1606.    not parsed, which either is a null char or equals STOPCHAR.
  1607.  
  1608.    SIZE is how big to construct chain elements.
  1609.    This is useful if we want them actually to be other structures
  1610.    that have room for additional info.
  1611.  
  1612.    If STRIP is nonzero, strip `./'s off the beginning.  */
  1613.  
  1614. struct nameseq *
  1615. parse_file_seq (stringp, stopchar, size, strip)
  1616.      char **stringp;
  1617.      char stopchar;
  1618.      unsigned int size;
  1619.      int strip;
  1620. {
  1621.   register struct nameseq *new = 0;
  1622.   register struct nameseq *new1, *lastnew1;
  1623.   register char *p = *stringp;
  1624.   char *q;
  1625.   char *name;
  1626.   char stopchars[2];
  1627.   stopchars[0] = stopchar;
  1628.   stopchars[1] = '\0';
  1629.  
  1630.   while (1)
  1631.     {
  1632.       /* Skip whitespace; see if any more names are left.  */
  1633.       p = next_token (p);
  1634.       if (*p == '\0')
  1635.     break;
  1636.       if (*p == stopchar)
  1637.     break;
  1638.       /* Yes, find end of next name.  */
  1639.       q = p;
  1640.       while (1)
  1641.         {
  1642.         int c = *p++;
  1643.         if (c == '\0')
  1644.           break;
  1645.         else if (c == '\\'
  1646.           && (*p == '\\' || isblank (*p) || *p == stopchar))
  1647.           ++p;
  1648. #if defined (__MSDOS__) || defined (WIN32)
  1649.         else if (c == stopchar && (*p == '/' || *p == '\\'))
  1650.           ++p;
  1651. #endif
  1652.         else if (isblank (c) || c == stopchar)
  1653.           break;
  1654.         }
  1655.       p--;
  1656.  
  1657.       if (strip)
  1658.     /* Skip leading `./'s.  */
  1659.     while (p - q > 2 && q[0] == '.' && q[1] == '/')
  1660.       {
  1661.         q += 2;        /* Skip "./".  */
  1662.         while (q < p && *q == '/')
  1663.           /* Skip following slashes: ".//foo" is "foo", not "/foo".  */
  1664.           ++q;
  1665.       }
  1666.  
  1667.       /* Extract the filename just found, and skip it.  */
  1668.  
  1669.       if (q == p)
  1670.     /* ".///" was stripped to "".  */
  1671.     name = savestring ("./", 2);
  1672.       else
  1673.     name = savestring (q, p - q);
  1674.  
  1675.       /* Add it to the front of the chain.  */
  1676.       new1 = (struct nameseq *) xmalloc (size);
  1677.       new1->name = name;
  1678.       new1->next = new;
  1679.       new = new1;
  1680.     }
  1681.  
  1682. #ifndef NO_ARCHIVES
  1683.  
  1684.   /* Look for multi-word archive references.
  1685.      They are indicated by a elt ending with an unmatched `)' and
  1686.      an elt further down the chain (i.e., previous in the file list)
  1687.      with an unmatched `(' (e.g., "lib(mem").  */
  1688.  
  1689.   new1 = new;
  1690.   lastnew1 = 0;
  1691.   while (new1 != 0)
  1692.     if (new1->name[0] != '('    /* Don't catch "(%)" and suchlike.  */
  1693.     && new1->name[strlen (new1->name) - 1] == ')'
  1694.     && index (new1->name, '(') == 0)
  1695.       {
  1696.     /* NEW1 ends with a `)' but does not contain a `('.
  1697.        Look back for an elt with an opening `(' but no closing `)'.  */
  1698.  
  1699.     struct nameseq *n = new1->next, *lastn = new1;
  1700.     char *paren;
  1701.     while (n != 0 && (paren = index (n->name, '(')) == 0)
  1702.       {
  1703.         lastn = n;
  1704.         n = n->next;
  1705.       }
  1706.     if (n != 0
  1707.         /* Ignore something starting with `(', as that cannot actually
  1708.            be an archive-member reference (and treating it as such
  1709.            results in an empty file name, which causes much lossage).  */
  1710.         && n->name[0] != '(')
  1711.       {
  1712.         /* N is the first element in the archive group.
  1713.            Its name looks like "lib(mem" (with no closing `)').  */
  1714.  
  1715.         char *libname;
  1716.  
  1717.         /* Copy "lib(" into LIBNAME.  */
  1718.         ++paren;
  1719.         libname = (char *) alloca (paren - n->name + 1);
  1720.         bcopy (n->name, libname, paren - n->name);
  1721.         libname[paren - n->name] = '\0';
  1722.  
  1723.         if (*paren == '\0')
  1724.           {
  1725.         /* N was just "lib(", part of something like "lib( a b)".
  1726.            Edit it out of the chain and free its storage.  */
  1727.         lastn->next = n->next;
  1728.         free (n->name);
  1729.         free ((char *) n);
  1730.         /* LASTN->next is the new stopping elt for the loop below.  */
  1731.         n = lastn->next;
  1732.           }
  1733.         else
  1734.           {
  1735.         /* Replace N's name with the full archive reference.  */
  1736.         name = concat (libname, paren, ")");
  1737.         free (n->name);
  1738.         n->name = name;
  1739.           }
  1740.  
  1741.         if (new1->name[1] == '\0')
  1742.           {
  1743.         /* NEW1 is just ")", part of something like "lib(a b )".
  1744.            Omit it from the chain and free its storage.  */
  1745.         if (lastnew1 == 0)
  1746.           new = new1->next;
  1747.         else
  1748.           lastnew1->next = new1->next;
  1749.         lastn = new1;
  1750.         new1 = new1->next;
  1751.         free (lastn->name);
  1752.         free ((char *) lastn);
  1753.           }
  1754.         else
  1755.           {
  1756.         /* Replace also NEW1->name, which already has closing `)'.  */
  1757.         name = concat (libname, new1->name, "");
  1758.         free (new1->name);
  1759.         new1->name = name;
  1760.         new1 = new1->next;
  1761.           }
  1762.  
  1763.         /* Trace back from NEW1 (the end of the list) until N
  1764.            (the beginning of the list), rewriting each name
  1765.            with the full archive reference.  */
  1766.         
  1767.         while (new1 != n)
  1768.           {
  1769.         name = concat (libname, new1->name, ")");
  1770.         free (new1->name);
  1771.         new1->name = name;
  1772.         lastnew1 = new1;
  1773.         new1 = new1->next;
  1774.           }
  1775.       }
  1776.     else
  1777.       {
  1778.         /* No frobnication happening.  Just step down the list.  */
  1779.         lastnew1 = new1;
  1780.         new1 = new1->next;
  1781.       }
  1782.       }
  1783.     else
  1784.       {
  1785.     lastnew1 = new1;
  1786.     new1 = new1->next;
  1787.       }
  1788.  
  1789. #endif
  1790.  
  1791.   *stringp = p;
  1792.   return new;
  1793. }
  1794.  
  1795. /* Read a line of text from STREAM into LINEBUFFER.
  1796.    Combine continuation lines into one line.
  1797.    Return the number of actual lines read (> 1 if hacked continuation lines).
  1798.  */
  1799.  
  1800. static unsigned int
  1801. readline (linebuffer, stream, filename, lineno)
  1802.      struct linebuffer *linebuffer;
  1803.      FILE *stream;
  1804.      char *filename;
  1805.      unsigned int lineno;
  1806. {
  1807.   char *buffer = linebuffer->buffer;
  1808.   register char *p = linebuffer->buffer;
  1809.   register char *end = p + linebuffer->size;
  1810.   register int len, lastlen = 0;
  1811.   register char *p2;
  1812.   register unsigned int nlines = 0;
  1813.   register int backslash;
  1814.  
  1815.   *p = '\0';
  1816.  
  1817.   while (fgets (p, end - p, stream) != 0)
  1818.     {
  1819.       len = strlen (p);
  1820.       if (len == 0)
  1821.     {
  1822.       /* This only happens when the first thing on the line is a '\0'.
  1823.          It is a pretty hopeless case, but (wonder of wonders) Athena
  1824.          lossage strikes again!  (xmkmf puts NULs in its makefiles.)
  1825.          There is nothing really to be done; we synthesize a newline so
  1826.          the following line doesn't appear to be part of this line.  */
  1827.       makefile_error (filename, lineno,
  1828.               "warning: NUL character seen; rest of line ignored");
  1829.       p[0] = '\n';
  1830.       len = 1;
  1831.     }
  1832.  
  1833.       p += len;
  1834.       if (p[-1] != '\n')
  1835.     {
  1836.       /* Probably ran out of buffer space.  */
  1837.       register unsigned int p_off = p - buffer;
  1838.       linebuffer->size *= 2;
  1839.       buffer = (char *) xrealloc (buffer, linebuffer->size);
  1840.       p = buffer + p_off;
  1841.       end = buffer + linebuffer->size;
  1842.       linebuffer->buffer = buffer;
  1843.       *p = '\0';
  1844.       lastlen = len;
  1845.       continue;
  1846.     }
  1847.  
  1848.       ++nlines;
  1849.  
  1850.       if (len == 1 && p > buffer)
  1851.     /* P is pointing at a newline and it's the beginning of
  1852.        the buffer returned by the last fgets call.  However,
  1853.        it is not necessarily the beginning of a line if P is
  1854.        pointing past the beginning of the holding buffer.
  1855.        If the buffer was just enlarged (right before the newline),
  1856.        we must account for that, so we pretend that the two lines
  1857.        were one line.  */
  1858.     len += lastlen;
  1859.       lastlen = len;
  1860.       backslash = 0;
  1861.       for (p2 = p - 2; --len > 0; --p2)
  1862.     {
  1863.       if (*p2 == '\\')
  1864.         backslash = !backslash;
  1865.       else
  1866.         break;
  1867.     }
  1868.       
  1869.       if (!backslash)
  1870.     {
  1871.       p[-1] = '\0';
  1872.       break;
  1873.     }
  1874.  
  1875.       if (end - p <= 1)
  1876.     {
  1877.       /* Enlarge the buffer.  */
  1878.       register unsigned int p_off = p - buffer;
  1879.       linebuffer->size *= 2;
  1880.       buffer = (char *) xrealloc (buffer, linebuffer->size);
  1881.       p = buffer + p_off;
  1882.       end = buffer + linebuffer->size;
  1883.       linebuffer->buffer = buffer;
  1884.     }
  1885.     }
  1886.  
  1887.   if (ferror (stream))
  1888.     pfatal_with_name (filename);
  1889.  
  1890.   return nlines;
  1891. }
  1892.  
  1893. /* Construct the list of include directories
  1894.    from the arguments and the default list.  */
  1895.  
  1896. void
  1897. construct_include_path (arg_dirs)
  1898.      char **arg_dirs;
  1899. {
  1900.   register unsigned int i;
  1901.   struct stat stbuf;
  1902.  
  1903.   /* Table to hold the dirs.  */
  1904.  
  1905.   register unsigned int defsize = (sizeof (default_include_directories)
  1906.                    / sizeof (default_include_directories[0]));
  1907.   register unsigned int max = 5;
  1908.   register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
  1909.   register unsigned int idx = 0;
  1910.  
  1911.   /* First consider any dirs specified with -I switches.
  1912.      Ignore dirs that don't exist.  */
  1913.  
  1914.   if (arg_dirs != 0)
  1915.     while (*arg_dirs != 0)
  1916.       {
  1917.     char *dir = *arg_dirs++;
  1918.  
  1919.     if (dir[0] == '~')
  1920.       {
  1921.         char *expanded = tilde_expand (dir);
  1922.         if (expanded != 0)
  1923.           dir = expanded;
  1924.       }
  1925.  
  1926.     if (safe_stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
  1927.       {
  1928.         if (idx == max - 1)
  1929.           {
  1930.         max += 5;
  1931.         dirs = (char **)
  1932.           xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
  1933.           }
  1934.         dirs[idx++] = dir;
  1935.       }
  1936.     else if (dir != arg_dirs[-1])
  1937.       free (dir);
  1938.       }
  1939.  
  1940.   /* Now add at the end the standard default dirs.  */
  1941.  
  1942.   for (i = 0; default_include_directories[i] != 0; ++i)
  1943.     if (safe_stat (default_include_directories[i], &stbuf) == 0
  1944.     && S_ISDIR (stbuf.st_mode))
  1945.       dirs[idx++] = default_include_directories[i];
  1946.  
  1947.   dirs[idx] = 0;
  1948.  
  1949.   /* Now compute the maximum length of any name in it.  */
  1950.  
  1951.   max_incl_len = 0;
  1952.   for (i = 0; i < idx; ++i)
  1953.     {
  1954.       unsigned int len = strlen (dirs[i]);
  1955.       /* If dir name is written with a trailing slash, discard it.  */
  1956.       if (dirs[i][len - 1] == '/')
  1957.     /* We can't just clobber a null in because it may have come from
  1958.        a literal string and literal strings may not be writable.  */
  1959.     dirs[i] = savestring (dirs[i], len - 1);
  1960.       if (len > max_incl_len)
  1961.     max_incl_len = len;
  1962.     }
  1963.  
  1964.   include_directories = dirs;
  1965. }
  1966.  
  1967. /* Expand ~ or ~USER at the beginning of NAME.
  1968.    Return a newly malloc'd string or 0.  */
  1969.  
  1970. char *
  1971. tilde_expand (name)
  1972.      char *name;
  1973. {
  1974.   if (name[1] == '/' || name[1] == '\0')
  1975.     {
  1976.       extern char *getenv ();
  1977.       char *home_dir;
  1978.       int is_variable;
  1979.  
  1980.       {
  1981.     /* Turn off --warn-undefined-variables while we expand HOME.  */
  1982.     int save = warn_undefined_variables_flag;
  1983.     warn_undefined_variables_flag = 0;
  1984.  
  1985.     home_dir = allocated_variable_expand ("$(HOME)");
  1986.  
  1987.     warn_undefined_variables_flag = save;
  1988.       }
  1989.   
  1990.       is_variable = home_dir[0] != '\0';
  1991.       if (!is_variable)
  1992.     {
  1993.       free (home_dir);
  1994.       home_dir = getenv ("HOME");
  1995.     }
  1996. #ifndef WIN32
  1997.       if (home_dir == 0 || home_dir[0] == '\0')
  1998.     {
  1999.       extern char *getlogin ();
  2000.       char *name = getlogin ();
  2001.       home_dir = 0;
  2002.       if (name != 0)
  2003.         {
  2004.           struct passwd *p = getpwnam (name);
  2005.           if (p != 0)
  2006.         home_dir = p->pw_dir;
  2007.         }
  2008.     }
  2009. #endif
  2010.       if (home_dir != 0)
  2011.     {
  2012.       char *new = concat (home_dir, "", name + 1);
  2013.       if (is_variable)
  2014.         free (home_dir);
  2015.       return new;
  2016.     }
  2017.     }
  2018.   else
  2019.     {
  2020.       struct passwd *pwent;
  2021.       char *userend = index (name + 1, '/');
  2022.       if (userend != 0)
  2023.     *userend = '\0';
  2024. #ifdef WIN32
  2025.       if (userend != 0)
  2026.         *userend = '/';
  2027. #else
  2028.       pwent = getpwnam (name + 1);
  2029.       if (pwent != 0)
  2030.     {
  2031.       if (userend == 0)
  2032.         return savestring (pwent->pw_dir, strlen (pwent->pw_dir));
  2033.       else
  2034.         return concat (pwent->pw_dir, "/", userend + 1);
  2035.     }
  2036.       else if (userend != 0)
  2037.     *userend = '/';
  2038. #endif
  2039.     }
  2040.  
  2041.   return 0;
  2042. }
  2043.  
  2044. /* Given a chain of struct nameseq's describing a sequence of filenames,
  2045.    in reverse of the intended order, return a new chain describing the
  2046.    result of globbing the filenames.  The new chain is in forward order.
  2047.    The links of the old chain are freed or used in the new chain.
  2048.    Likewise for the names in the old chain.
  2049.  
  2050.    SIZE is how big to construct chain elements.
  2051.    This is useful if we want them actually to be other structures
  2052.    that have room for additional info.  */
  2053.  
  2054. struct nameseq *
  2055. multi_glob (chain, size)
  2056.      struct nameseq *chain;
  2057.      unsigned int size;
  2058. {
  2059.   register struct nameseq *new = 0;
  2060.   register struct nameseq *old;
  2061.   struct nameseq *nexto;
  2062.  
  2063.   for (old = chain; old != 0; old = nexto)
  2064.     {
  2065.       glob_t gl;
  2066. #ifndef NO_ARCHIVES
  2067.       char *memname;
  2068. #endif
  2069.  
  2070.       nexto = old->next;
  2071.  
  2072.       if (old->name[0] == '~')
  2073.     {
  2074.       char *newname = tilde_expand (old->name);
  2075.       if (newname != 0)
  2076.         {
  2077.           free (old->name);
  2078.           old->name = newname;
  2079.         }
  2080.     }
  2081.  
  2082. #ifndef NO_ARCHIVES
  2083.       if (ar_name (old->name))
  2084.     {
  2085.       /* OLD->name is an archive member reference.
  2086.          Replace it with the archive file name,
  2087.          and save the member name in MEMNAME.
  2088.          We will glob on the archive name and then
  2089.          reattach MEMNAME later.  */
  2090.       char *arname;
  2091.       ar_parse_name (old->name, &arname, &memname);
  2092.       free (old->name);
  2093.       old->name = arname;
  2094.     }
  2095.       else
  2096.     memname = 0;
  2097. #endif
  2098.  
  2099.       switch (glob (old->name, GLOB_NOCHECK, NULL, &gl))
  2100.     {
  2101.     case 0:            /* Success.  */
  2102.       {
  2103.         register int i = gl.gl_pathc;
  2104.         while (i-- > 0)
  2105.           {
  2106. #ifndef NO_ARCHIVES
  2107.         if (memname != 0)
  2108.           {
  2109.             /* Try to glob on MEMNAME within the archive.  */
  2110.             struct nameseq *found
  2111.               = ar_glob (gl.gl_pathv[i], memname, size);
  2112.             if (found == 0)
  2113.               {
  2114.             /* No matches.  Use MEMNAME as-is.  */
  2115.             struct nameseq *elt
  2116.               = (struct nameseq *) xmalloc (size);
  2117.             unsigned int alen = strlen (gl.gl_pathv[i]);
  2118.             unsigned int mlen = strlen (memname);
  2119.             elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
  2120.             bcopy (gl.gl_pathv[i], elt->name, alen);
  2121.             elt->name[alen] = '(';
  2122.             bcopy (memname, &elt->name[alen + 1], mlen);
  2123.             elt->name[alen + 1 + mlen] = ')';
  2124.             elt->name[alen + 1 + mlen + 1] = '\0';
  2125.             elt->next = new;
  2126.             new = elt;
  2127.               }
  2128.             else
  2129.               {
  2130.             /* Find the end of the FOUND chain.  */
  2131.             struct nameseq *f = found;
  2132.             while (f->next != 0)
  2133.               f = f->next;
  2134.  
  2135.             /* Attach the chain being built to the end of the FOUND
  2136.                chain, and make FOUND the new NEW chain.  */
  2137.             f->next = new;
  2138.             new = found;
  2139.               }
  2140.  
  2141.             free (memname);
  2142.           }
  2143.         else
  2144. #endif
  2145.           {
  2146.             struct nameseq *elt = (struct nameseq *) xmalloc (size);
  2147.             elt->name = savestring (gl.gl_pathv[i],
  2148.                         strlen (gl.gl_pathv[i]));
  2149.             elt->next = new;
  2150.             new = elt;
  2151.           }
  2152.           }
  2153.         globfree (&gl);
  2154.         free (old->name);
  2155.         free (old);
  2156.         break;
  2157.       }
  2158.  
  2159.     case GLOB_NOSPACE:
  2160.       fatal ("virtual memory exhausted");
  2161.       break;
  2162.  
  2163.     default:
  2164.       old->next = new;
  2165.       new = old;
  2166.       break;
  2167.     }
  2168.     }
  2169.  
  2170.   return new;
  2171. }
  2172.