home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / MAK358AS.ZIP / READ.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-22  |  42.8 KB  |  1,580 lines

  1. /* Copyright (C) 1988, 1989, 1990 Free Software Foundation, Inc.
  2. This file is part of GNU Make.
  3.  
  4. GNU Make is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 1, or (at your option)
  7. any later version.
  8.  
  9. GNU Make is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with GNU Make; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /*
  19.  * MS-DOS port (c) 1990 by Thorsten Ohl <ohl@gnu.ai.mit.edu>
  20.  *
  21.  * To this port, the same copying conditions apply as to the
  22.  * original release.
  23.  *
  24.  * IMPORTANT:
  25.  * This file is not identical to the original GNU release!
  26.  * You should have received this code as patch to the official
  27.  * GNU release.
  28.  *
  29.  * MORE IMPORTANT:
  30.  * This port comes with ABSOLUTELY NO WARRANTY.
  31.  *
  32.  * $Header: e:/gnu/make/RCS/read.c'v 3.58.0.3 90/07/23 22:52:53 tho Exp $
  33.  */
  34.  
  35. #include "make.h"
  36. #include "commands.h"
  37. #include "dep.h"
  38. #include "file.h"
  39. #include "variable.h"
  40.  
  41. #ifndef MSDOS
  42. #include <pwd.h>
  43. struct passwd *getpwnam ();
  44. #endif /* not MSDOS */
  45.   
  46. #ifdef MSDOS
  47. #include <ctype.h>
  48. static  void read_makefile (char *filename, int type);
  49. static  unsigned int readline (struct linebuffer *linebuffer, FILE *stream,
  50.                    char *filename);
  51. static  unsigned int do_define (char *name, unsigned int namelen,
  52.         enum variable_origin origin, unsigned int lineno,
  53.         FILE *infile, char *filename);
  54. static  int conditional_line (char *line, char *filename, unsigned int lineno);
  55. static  void record_files (struct nameseq *filenames, char *pattern,
  56.         char *pattern_percent, struct dep *deps,
  57.         unsigned int commands_started, char *commands,
  58.         unsigned int commands_idx, int two_colon, char *filename,
  59.         unsigned int lineno, int set_default);
  60. static char *find_semicolon (char *string);
  61. #else /* not MSDOS */
  62. static void read_makefile ();
  63. static unsigned int readline (), do_define ();
  64. static int conditional_line ();
  65. static void record_files ();
  66. static char *find_semicolon ();
  67. #endif /* not MSDOS */
  68.  
  69.  
  70. /* A `struct linebuffer' is a structure which holds a line of text.
  71.    `readline' reads a line from a stream into a linebuffer
  72.    and works regardless of the length of the line.  */
  73.  
  74. struct linebuffer
  75.   {
  76.     /* Note:  This is the number of bytes malloc'ed for `buffer'
  77.        It does not indicate `buffer's real length.
  78.        Instead, a null char indicates end-of-string.  */
  79.     unsigned int size;
  80.     char *buffer;
  81.   };
  82.  
  83. #define initbuffer(lb) (lb)->buffer = (char *) xmalloc ((lb)->size = 200)
  84. #define freebuffer(lb) free ((lb)->buffer)
  85.  
  86.  
  87. /* A `struct conditionals' contains the information describing
  88.    all the active conditionals in a makefile.
  89.  
  90.    The global variable `conditionals' contains the conditionals
  91.    information for the current makefile.  It is initialized from
  92.    the static structure `toplevel_conditionals' and is later changed
  93.    to new structures for included makefiles.  */
  94.  
  95. struct conditionals
  96.   {
  97.     unsigned int if_cmds;
  98.     unsigned int max_ignoring;
  99.     char *ignoring;
  100.   };
  101.  
  102. static struct conditionals toplevel_conditionals;
  103. static struct conditionals *conditionals = &toplevel_conditionals;
  104.   
  105.  
  106. /* Default directories to search for include files in  */
  107.  
  108. static char *default_include_directories[] =
  109.   {
  110.     "/usr/gnu/include",
  111.     "/usr/local/include",
  112.     "/usr/include",
  113.     0
  114.   };
  115.  
  116. /* List of directories to search for include files in  */
  117.  
  118. static char **include_directories;
  119.  
  120. /* Maximum length of an element of the above.  */
  121.  
  122. static unsigned int max_incl_len;
  123.  
  124. /* The filename and pointer to line number of the
  125.    makefile currently being read in.  */
  126.  
  127. char *reading_filename;
  128. unsigned int *reading_lineno_ptr;
  129.  
  130. /* The chain of makefiles read by read_makefile.  */
  131.  
  132. static struct dep *read_makefiles = 0;
  133.  
  134. /* Read in all the makefiles and return the chain of their names.  */
  135.  
  136. struct dep *
  137. read_all_makefiles (makefiles)
  138.      char **makefiles;
  139. {
  140.   unsigned int num_makefiles = 0;
  141.  
  142.   if (debug_flag)
  143.     puts ("Reading makefiles...");
  144.  
  145.   /* If there's a non-null variable MAKEFILES, its value is a list of
  146.      files to read first thing.  But don't let it prevent reading the
  147.      default makefiles and don't let the default goal come from there.  */
  148.  
  149.   {
  150.     char *makefiles = variable_expand ("$(MAKEFILES)");
  151.     char *name;
  152.     unsigned int length;
  153.  
  154.     /* Set NAME to the start of next token and LENGTH to its length.
  155.        MAKEFILES is updated for finding remaining tokens.  */
  156.     while ((name = find_next_token (&makefiles, &length)) != 0)
  157.       read_makefile (name, 1);
  158.   }
  159.  
  160.   /* Read makefiles specified with -f switches.  */
  161.  
  162.   if (makefiles != 0)
  163.     while (*makefiles != 0)
  164.       {
  165.     struct dep *tail = read_makefiles;
  166.     register struct dep *d;
  167.  
  168.     read_makefile (*makefiles, 0);
  169.  
  170.     /* Find the right element of read_makefiles.  */
  171.     d = read_makefiles;
  172.     while (d->next != tail)
  173.       d = d->next;
  174.  
  175.     /* Use the storage read_filename allocates.  */
  176.     free (*makefiles);
  177.     *makefiles = dep_name (d);
  178.     ++num_makefiles;
  179.     ++makefiles;
  180.       }
  181.  
  182.   /* If there were no -f switches, try the default names.  */
  183.  
  184.   if (num_makefiles == 0)
  185.     {
  186.       static char *default_makefiles[] =
  187. #ifdef MSDOS
  188.     { "makefile.gnu", "makefile", 0 };
  189. #else /* not MSDOS */
  190.     { "GNUmakefile", "makefile", "Makefile", 0 };
  191. #endif /* not MSDOS */
  192.       register char **p = default_makefiles;
  193.       while (*p != 0 && !file_exists_p (*p))
  194.     ++p;
  195.  
  196.       if (*p != 0)
  197.     read_makefile (*p, 0);
  198.       else
  199.     {
  200.       /* No default makefile was found.  Add the default makefiles to the
  201.          `read_makefiles' chain so they will be updated if possible.  */
  202.       struct dep *tail = read_makefiles;
  203.       for (p = default_makefiles; *p != 0; ++p)
  204.         {
  205.           struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
  206.           d->name = 0;
  207.           d->file = enter_file (*p);
  208.           d->file->dontcare = 1;
  209.           /* Setting the `changed' member to 1 will make failure to
  210.          update or find this makefile as if it had come from the
  211.          MAKEFILES variable: we don't care, so we won't die.  */
  212.           d->changed = 1;
  213.           if (tail == 0)
  214.         read_makefiles = d;
  215.           else
  216.         tail->next = d;
  217.           tail = d;
  218.         }
  219.       if (tail != 0)
  220.         tail->next = 0;
  221.     }
  222.     }
  223.  
  224.   return read_makefiles;
  225. }
  226.  
  227. /* Read file FILENAME as a makefile and add its contents to the data base.
  228.    TYPE indicates what flavor of makefile this is: 0 => a default or -f
  229.    makefile (the basis for comparison); 1 => from the MAKEFILES variable:
  230.    cannot determine the default goal, and it's not an error if it doesn't
  231.    exist; 2 => an included makefile: is searched for in the search path.
  232.    FILENAME is added to the `read_makefiles' chain.  */
  233.  
  234. static void
  235. read_makefile (filename, type)
  236.      char *filename;
  237.      int type;
  238. {
  239.   static char *collapsed = 0;
  240.   static unsigned int collapsed_length = 0;
  241.   register FILE *infile;
  242.   struct linebuffer lb;
  243.   unsigned int commands_len = 200;
  244.   char *commands = (char *) xmalloc (200);
  245.   unsigned int commands_idx = 0;
  246.   unsigned int commands_started;
  247.   register char *p;
  248.   char *p2;
  249.   int ignoring = 0;
  250.  
  251.   struct nameseq *filenames = 0;
  252.   struct dep *deps;
  253.   unsigned int lineno = 1;
  254.   unsigned int nlines = 0;
  255.   int two_colon;
  256.   char *pattern = 0, *pattern_percent;
  257.  
  258. #define record_waiting_files()                              \
  259.   do                                          \
  260.     {                                           \
  261.       record_files (filenames, pattern, pattern_percent, deps,              \
  262.             commands_started, commands, commands_idx,              \
  263.             two_colon, filename, lineno, type != 1);              \
  264.       filenames = 0;                                  \
  265.       commands_idx = 0;                                  \
  266.       pattern = 0;                                  \
  267.     } while (0)
  268.  
  269. #ifdef    lint    /* Suppress `used before set' messages.  */
  270.   two_colon = 0;
  271. #endif
  272.  
  273.   /* First, get a stream to read.  */
  274.  
  275.   infile = fopen (filename, "r");
  276.  
  277.   /* If the makefile wasn't found and it's an included makefile (type 2),
  278.      search the included makefile search path for this makefile.  */
  279.  
  280.   if (infile == 0 && type == 2 && *filename != '/')
  281.     {
  282.       register unsigned int i;
  283.       for (i = 0; include_directories[i] != 0; ++i)
  284.     {
  285.       char *name = concat (include_directories[i], "/", filename);
  286.       infile = fopen (name, "r");
  287.       if (infile != 0)
  288.         {
  289.           filename = name;
  290.           break;
  291.         }
  292.       else
  293.         free (name);
  294.     }
  295.     }
  296.  
  297.   /* Add FILENAME to the chain of read makefiles.  */
  298.   deps = (struct dep *) xmalloc (sizeof (struct dep));
  299.   deps->next = read_makefiles;
  300.   read_makefiles = deps;
  301.   deps->name = 0;
  302.   deps->file = lookup_file (filename);
  303.   if (deps->file == 0)
  304.     {
  305.       deps->file = enter_file (savestring (filename, strlen (filename)));
  306.       if (type == 1)
  307.     deps->file->dontcare = 1;
  308.     }
  309.   filename = deps->file->name;
  310.   deps->file->precious = 1;
  311.   deps->changed = type;
  312.   deps = 0;
  313.  
  314.   /* If the makefile can't be found at all,
  315.      either ignore it or give up entirely.  */
  316.  
  317.   if (infile == 0)
  318.     {
  319.       if (type != 1)
  320.     perror_with_name ("fopen: ", filename);
  321.       return;
  322.     }
  323.  
  324.   reading_filename = filename;
  325.   reading_lineno_ptr = &lineno;
  326.  
  327.   /* Loop over lines in the file.
  328.      The strategy is to accumulate target names in FILENAMES,
  329.      dependencies in DEPS and commands in COMMANDS.
  330.      These are used to define a rule
  331.      when the start of the next rule (or eof) is encountered.  */
  332.  
  333.   initbuffer (&lb);
  334.  
  335.   while (!feof (infile))
  336.     {
  337.       lineno += nlines;
  338.       nlines = readline (&lb, infile, filename);
  339.  
  340.       if (collapsed_length < lb.size)
  341.     {
  342.       collapsed_length = lb.size;
  343.       if (collapsed != 0)
  344.         free (collapsed);
  345.       collapsed = (char *) xmalloc (collapsed_length);
  346.     }
  347.       strcpy (collapsed, lb.buffer);
  348.       collapse_line (collapsed);
  349.  
  350.       p = next_token (collapsed);
  351.       if (*p == '\0')
  352.     continue;
  353.  
  354. #define    word1eq(s, l)     ((p[l] == '\0' || p[l] == ' ' || p[l] == '\t') && \
  355.              !strncmp (s, p, l))
  356.       if (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
  357.       || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
  358.       || word1eq ("else", 4) || word1eq ("endif", 5))
  359.     {
  360.       int i = conditional_line (p, filename, lineno);
  361.       if (i >= 0)
  362.         {
  363.           ignoring = i;
  364.           continue;
  365.         }
  366.       else
  367.         fatal ("%s:%u: invalid syntax in conditional", filename, lineno);
  368.     }
  369.       if (ignoring)
  370.     continue;
  371.       else if (lb.buffer[0] == '\t')
  372.     {
  373.       /* This line is a shell command.  */
  374.       unsigned int len;
  375.  
  376.       if (filenames == 0)
  377.         fatal ("%s:%u: commands commence before first target",
  378.                filename, lineno);
  379.  
  380.       /* Add this command line to end of the line being accumulated.  */
  381.       p = lb.buffer;
  382.       if (commands_idx == 0)
  383.         commands_started = lineno;
  384.       len = strlen (p);
  385.       if (len + 1 + commands_idx > commands_len)
  386.         {
  387.           commands_len = (len + 1 + commands_idx) * 2;
  388.           commands = (char *) xrealloc (commands, commands_len);
  389.         }
  390.       bcopy (p, &commands[commands_idx], len);
  391.       commands_idx += len;
  392.       commands[commands_idx++] = '\n';
  393.     }
  394.       else if (word1eq ("define", 6))
  395.     {
  396.       p2 = next_token (p + 6);
  397.       p = end_of_token (p2);
  398.       lineno = do_define (p2, p - p2, o_file, lineno, infile, filename);
  399.       continue;
  400.     }
  401.       else if (word1eq ("endef", 5))
  402.     fatal ("%s:%u: extraneous `endef'", filename, lineno);
  403.       else if (word1eq ("override", 8))
  404.     {
  405.       p2 = next_token (p + 8);
  406.       if (p2 == 0)
  407.         error ("%s:%u: empty `override' directive", filename, lineno);
  408.       if (!strncmp (p2, "define", 6))
  409.         {
  410.           unsigned int len;
  411.           p = find_next_token (&p2, &len);
  412.           lineno = do_define (p, len, o_override,
  413.                   lineno, infile, filename);
  414.         }
  415.       else if (!try_variable_definition (p2, o_override))
  416.         error ("%s:%u: Empty `override' directive", filename, lineno);
  417.       continue;
  418.     }
  419.       else if (word1eq ("include", 7))
  420.     {
  421.       /* We have found an `include' line specifying a nested
  422.          makefile to be read at this point.  */
  423.       struct conditionals *save = conditionals;
  424.       struct conditionals new_conditionals;
  425.       p = allocated_variable_expand (next_token (p + 8));
  426.       if (*p == '\0')
  427.         {
  428.           error ("%s:%u: no filename for `include'", filename, lineno);
  429.           continue;
  430.         }
  431.       p2 = end_of_token (p);
  432.       if (*p2 != '\0')
  433.         {
  434.           *p2++ = '\0';
  435.           if (*next_token (p2) != '\0')
  436.         error ("%s:%u: extraneous text after `include'",
  437.                filename, lineno);
  438.         }
  439.       bzero ((char *) &new_conditionals, sizeof new_conditionals);
  440.       conditionals = &new_conditionals;
  441.       /* Record the rules that are waiting so they will determine
  442.          the default goal before those in the included makefile.  */
  443.       record_waiting_files ();
  444.       read_makefile (p, 2);
  445.       free (p);
  446.       conditionals = save;
  447.       reading_filename = filename;
  448.       reading_lineno_ptr = &lineno;
  449.       continue;
  450.     }
  451.       else if (word1eq ("vpath", 5))
  452.     {
  453.       char *pattern;
  454.       unsigned int len;
  455.       p2 = variable_expand (p + 5);
  456.       p = find_next_token (&p2, &len);
  457.       if (p != 0)
  458.         {
  459.           pattern = savestring (p, len);
  460.           p = find_next_token (&p2, &len);
  461.           if (p != 0)
  462.         {
  463.           p = savestring (p, len);
  464.           if (find_next_token (&p2, (unsigned int *) 0) != 0)
  465.             error ("%s:%u: extraneous text after `vpath' directive",
  466.                filename, lineno);
  467.         }
  468.           /* No searchpath means remove all previous
  469.          selective VPATH's with the same pattern.  */
  470.         }
  471.       else
  472.         /* No pattern means remove all previous selective VPATH's.  */
  473.         pattern = 0;
  474.       construct_vpath_list (pattern, p);
  475.       if (pattern != 0)
  476.         free (pattern);
  477.       if (p != 0)
  478.         free (p);
  479.       continue;
  480.     }
  481. #undef    word1eq
  482.       else if (try_variable_definition (p, o_file))
  483.     continue;
  484.       else
  485.     {
  486.       /* This line describes some target files.  */
  487.  
  488.       char *cmdleft;
  489.  
  490.       /* Record the previous rule.  */
  491.  
  492.       record_waiting_files ();
  493.  
  494.       /* Look for a semicolon in the unexpanded line.  */
  495.       cmdleft = find_semicolon (lb.buffer);
  496.       if (cmdleft != 0)
  497.         /* Found one.  Cut the line short there before expanding it.  */
  498.         *cmdleft = '\0';
  499.  
  500.       /* Expand variable and function references before doing anything
  501.          else so that special characters can be inside variables.  */
  502.       p = variable_expand (lb.buffer);
  503.  
  504.       if (cmdleft == 0)
  505.         /* Look for a semicolon in the expanded line.  */
  506.         cmdleft = find_semicolon (p);
  507.  
  508.       if (cmdleft != 0)
  509.         /* Cut the line short at the semicolon.  */
  510.         *cmdleft = '\0';
  511.  
  512.       /* Remove comments from the line.  */
  513.       collapse_line (p);
  514.  
  515.       p2 = next_token (p);
  516.       if (*p2 == '\0')
  517.         /* This line contained a variable reference that
  518.            expanded to nothing but whitespace.  */
  519.         continue;
  520.       else if (*p2 == ':')
  521.         fatal ("%s:%u: missing target name", filename, lineno);
  522.  
  523.       filenames = multi_glob (parse_file_seq (&p2, ':',
  524.                           sizeof (struct nameseq)),
  525.                   sizeof (struct nameseq));
  526.       if (*p2++ == '\0')
  527.         fatal ("%s:%u: missing separator", filename, lineno);
  528.       /* Is this a one-colon or two-colon entry?  */
  529.       two_colon = *p2 == ':';
  530.       if (two_colon)
  531.         p2++;
  532.  
  533.       /* Is this a static pattern rule: `target: %targ: %dep; ...'?  */
  534.       p = index (p2, ':');
  535. #ifdef MSDOS
  536.       while (p != 0 && p[-1] == '\\' && (isspace (p[1]) || p != p2 + 2))
  537. #else /* not MSDOS */
  538.       while (p != 0 && p[-1] == '\\')
  539. #endif /* not MSDOS */
  540.         {
  541.           register char *q = &p[-1];
  542.           register int backslash = 0;
  543.           while (*q-- == '\\')
  544.         backslash = !backslash;
  545.           if (backslash)
  546.         p = index (p + 1, ':');
  547.           else
  548.         break;
  549.         }
  550. #ifdef MSDOS
  551.       if (p != 0 && (isspace (p[1]) || p != p2 + 2))
  552. #else /* not MSDOS */
  553.       if (p != 0)
  554. #endif /* not MSDOS */
  555.         {
  556.           struct nameseq *target;
  557.           target = parse_file_seq (&p2, ':', sizeof (struct nameseq));
  558.           ++p2;
  559.           if (target == 0)
  560.         fatal ("%s:%u: missing target pattern", filename, lineno);
  561.           else if (target->next != 0)
  562.         fatal ("%s:%u: multiple target patterns", filename, lineno);
  563.           pattern = target->name;
  564.           pattern_percent = find_percent (pattern);
  565.           if (pattern_percent == 0)
  566.         fatal ("%s:%u: target pattern contains no `%%'",
  567.                filename, lineno);
  568.         }
  569.       else
  570.         pattern = 0;
  571.  
  572.       /* Parse the dependencies.  */
  573.       deps = (struct dep *)
  574.         multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep)),
  575.             sizeof (struct dep));
  576.  
  577.       commands_idx = 0;
  578.       if (cmdleft != 0)
  579.         {
  580.           /* Semicolon means rest of line is a command */
  581.           unsigned int len = strlen (cmdleft + 1);
  582.  
  583.           commands_started = lineno;
  584.  
  585.           /* Add this command line to the buffer.  */
  586.           if (len + 2 > commands_len)
  587.         {
  588.           commands_len = (len + 2) * 2;
  589.           commands = (char *) xrealloc (commands, commands_len);
  590.         }
  591.           bcopy (cmdleft + 1, commands, len);
  592.           commands_idx += len;
  593.           commands[commands_idx++] = '\n';
  594.         }
  595.     }
  596.     }
  597.  
  598.   if (ignoring)
  599.     fatal ("%s:%u: missing `endif'", filename, lineno);
  600.  
  601.   /* At eof, record the last rule.  */
  602.   record_waiting_files ();
  603.  
  604.   freebuffer (&lb);
  605.   free ((char *) commands);
  606.   fclose (infile);
  607.  
  608.   reading_filename = 0;
  609.   reading_lineno_ptr = 0;
  610. }
  611.  
  612. /* Execute a `define' directive.
  613.    The first line has already been read, and NAME is the name of
  614.    the variable to be defined.  The following lines remain to be read.
  615.    LINENO, INFILE and FILENAME refer to the makefile being read.
  616.    The value returned is LINENO, updated for lines read here.  */
  617.  
  618. static unsigned int
  619. do_define (name, namelen, origin, lineno, infile, filename)
  620.      char *name;
  621.      unsigned int namelen;
  622.      enum variable_origin origin;
  623.      unsigned int lineno;
  624.      FILE *infile;
  625.      char *filename;
  626. {
  627.   struct linebuffer lb;
  628.   unsigned int nlines = 0;
  629.   unsigned int length = 100;
  630.   char *definition = (char *) xmalloc (100);
  631.   register unsigned int idx = 0;
  632.   register char *p;
  633.  
  634.   initbuffer (&lb);
  635.   while (!feof (infile))
  636.     {
  637.       lineno += nlines;
  638.       nlines = readline (&lb, infile, filename);
  639.       p = lb.buffer;
  640.  
  641.       if ((p[5] == '\0' || p[5] == ' ' || p[5] == '\t')
  642.       && !strncmp (p, "endef", 5))
  643.     {
  644.       p += 5;
  645.       collapse_line (p);
  646.       if (*next_token (p) != '\0')
  647.         error ("%s:%u: Extraneous text after `endef' directive",
  648.            filename, lineno);
  649.       /* Define the variable.  */
  650.       definition[idx] = '\0';
  651.       (void) define_variable (name, namelen, definition, origin, 1);
  652.       return lineno;
  653.     }
  654.       else
  655.     {
  656.       unsigned int len = strlen (p);
  657.  
  658.       /* Increase the buffer size if necessary.  */
  659.       if (idx + len > length)
  660.         {
  661.           length = (idx + len) * 2;
  662.           definition = (char *) xrealloc (definition, length + 1);
  663.         }
  664.  
  665.       bcopy (p, &definition[idx], len);
  666.       idx += len;
  667.       /* Separate lines with a newline.  */
  668.       definition[idx++] = '\n';
  669.     }
  670.     }
  671.  
  672.   /* No `endef'!!  */
  673.   fatal ("%s:%u: missing `endef', unterminated `define'", filename, lineno);
  674.   return 0;
  675. }
  676.  
  677. /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
  678.    "ifneq", "else" and "endif".
  679.    LINE is the input line, with the command as its first word.
  680.  
  681.    FILENAME and LINENO are the filename and line number in the
  682.    current makefile.  They are used for error messages.
  683.  
  684.    Value is -1 if the line is invalid,
  685.    0 if following text should be interpreted,
  686.    1 if following text should be ignored.  */
  687.  
  688. static int
  689. conditional_line (line, filename, lineno)
  690.      char *line;
  691.      char *filename;
  692.      unsigned int lineno;
  693. {
  694.   int notdef;
  695.   char *cmdname;
  696.   register unsigned int i;
  697.  
  698.   if (*line == 'i')
  699.     {
  700.       /* It's an "if..." command.  */
  701.       notdef = line[2] == 'n';
  702.       if (notdef)
  703.     {
  704.       cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
  705.       line += cmdname[3] == 'd' ? 7 : 6;
  706.     }
  707.       else
  708.     {
  709.       cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
  710.       line += cmdname[2] == 'd' ? 6 : 5;
  711.     }
  712.     }
  713.   else
  714.     {
  715.       /* It's an "else" or "endif" command.  */
  716.       notdef = line[1] == 'n';
  717.       cmdname = notdef ? "endif" : "else";
  718.       line += notdef ? 5 : 4;
  719.     }
  720.  
  721.   line = next_token (line);
  722.  
  723.   if (*cmdname == 'e')
  724.     {
  725.       if (*line != '\0')
  726.     error ("%s:%u: Extraneous text after `%s' directive",
  727.            filename, lineno, cmdname);
  728.       /* "Else" or "endif".  */
  729.       if (conditionals->if_cmds == 0)
  730.     fatal ("%s:%u: extraneous `%s'\n", filename, lineno, cmdname);
  731.       /* NOTDEF indicates an `endif' command.  */
  732.       if (notdef)
  733.     --conditionals->if_cmds;
  734.       else
  735.     conditionals->ignoring[conditionals->if_cmds - 1]
  736.       = !conditionals->ignoring[conditionals->if_cmds - 1];
  737.       for (i = 0; i < conditionals->if_cmds; ++i)
  738.     if (conditionals->ignoring[i])
  739.       return 1;
  740.       return 0;
  741.     }
  742.  
  743.   if (conditionals->max_ignoring == 0)
  744.     {
  745.       conditionals->max_ignoring = 5;
  746.       conditionals->ignoring = (char *) xmalloc (conditionals->max_ignoring);
  747.     }
  748.  
  749.   ++conditionals->if_cmds;
  750.   if (conditionals->if_cmds > conditionals->max_ignoring)
  751.     {
  752.       conditionals->max_ignoring += 5;
  753.       conditionals->ignoring = (char *)
  754.     xrealloc (conditionals->ignoring, conditionals->max_ignoring);
  755.     }
  756.  
  757.   if (cmdname[notdef ? 3 : 2] == 'd')
  758.     {
  759.       /* "Ifdef" or "ifndef".  */
  760.       struct variable *v;
  761.       register char *p = end_of_token (line);
  762.       i = p - line;
  763.       p = next_token (p);
  764.       if (*p != '\0')
  765.     return -1;
  766.       v = lookup_variable (line, i);
  767.       conditionals->ignoring[conditionals->if_cmds - 1]
  768.     = (v != 0 && *v->value != '\0') == notdef;
  769.       for (i = 0; i < conditionals->if_cmds; ++i)
  770.     if (conditionals->ignoring[i])
  771.       return 1;
  772.       return 0;
  773.     }
  774.   else
  775.     {
  776.       /* "Ifeq" or "ifneq".  */
  777.       char *s1, *s2;
  778.       unsigned int len;
  779.       char termin = *line == '(' ? ',' : *line;
  780.  
  781.       if (termin != ',' && termin != '"' && termin != '\'')
  782.     return -1;
  783.  
  784.       s1 = ++line;
  785.       /* Find the end of the first string.  */
  786.       if (termin == ',')
  787.     {
  788.       register int count = 0;
  789.       for (; *line != '\0'; ++line)
  790.         if (*line == '(')
  791.           ++count;
  792.         else if (*line == ')')
  793.           --count;
  794.         else if (*line == ',' && count <= 0)
  795.           break;
  796.     }
  797.       else
  798.     while (*line != '\0' && *line != termin)
  799.       ++line;
  800.  
  801.       if (*line == '\0')
  802.     return -1;
  803.  
  804.       *line++ = '\0';
  805.  
  806.       s2 = variable_expand (s1);
  807.       /* We must allocate a new copy of the expanded string because
  808.      variable_expand re-uses the same buffer.  */
  809.       len = strlen (s2);
  810.       s1 = (char *) alloca (len + 1);
  811.       bcopy (s2, s1, len + 1);
  812.  
  813.       if (termin != ',')
  814.     /* Find the start of the second string.  */
  815.     line = next_token (line);
  816.  
  817.       termin = termin == ',' ? ')' : *line;
  818.       if (termin != ')' && termin != '"' && termin != '\'')
  819.     return -1;
  820.  
  821.       /* Find the end of the second string.  */
  822.       if (termin == ')')
  823.     {
  824.       register int count = 0;
  825.       s2 = next_token (line);
  826.       for (line = s2; *line != '\0'; ++line)
  827.         {
  828.           if (*line == '(')
  829.         ++count;
  830.           else if (*line == ')')
  831.         if (count <= 0)
  832.           break;
  833.         else
  834.           --count;
  835.         }
  836.     }
  837.       else
  838.     {
  839.       ++line;
  840.       s2 = line;
  841.       while (*line != '\0' && *line != termin)
  842.         ++line;
  843.     }
  844.  
  845.       if (*line == '\0')
  846.     return -1;
  847.  
  848.       *line = '\0';
  849.       line = next_token (++line);
  850.       if (*line != '\0')
  851.     error ("%s:%u: Extraneous text after `%s' directive",
  852.            filename, lineno, cmdname);
  853.  
  854.       s2 = variable_expand (s2);
  855.       conditionals->ignoring[conditionals->if_cmds - 1]
  856.     = streq (s1, s2) == notdef;
  857.       for (i = 0; i < conditionals->if_cmds; ++i)
  858.     if (conditionals->ignoring[i])
  859.       return 1;
  860.       return 0;
  861.     }
  862. }
  863.  
  864. /* Remove duplicate dependencies in CHAIN.  */
  865.  
  866. void
  867. uniquize_deps (chain)
  868.      struct dep *chain;
  869. {
  870.   register struct dep *d;
  871.  
  872.   /* Make sure that no dependencies are repeated.  This does not
  873.      really matter for the purpose of updating targets, but it
  874.      might make some names be listed twice for $^ and $?.  */
  875.  
  876.   for (d = chain; d != 0; d = d->next)
  877.     {
  878.       struct dep *last, *next;
  879.  
  880.       last = d;
  881.       next = d->next;
  882.       while (next != 0)
  883.     if (streq (dep_name (d), dep_name (next)))
  884.       {
  885.         struct dep *n = next->next;
  886.         last->next = n;
  887.         if (next->name != 0 && next->name != d->name)
  888.           free (next->name);
  889.         if (next != d)
  890.           free ((char *) next);
  891.         next = n;
  892.       }
  893.     else
  894.       {
  895.         last = next;
  896.         next = next->next;
  897.       }
  898.     }
  899. }
  900.  
  901. /* Record a description line for files FILENAMES,
  902.    with dependencies DEPS, commands to execute described
  903.    by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
  904.    TWO_COLON is nonzero if a double colon was used.
  905.    If not nil, PATTERN is the `%' pattern to make this
  906.    a static pattern rule, and PATTERN_PERCENT is a pointer
  907.    to the `%' within it.
  908.  
  909.    The links of FILENAMES are freed, and so are any names in it
  910.    that are not incorporated into other data structures.  */
  911.  
  912. static void
  913. record_files (filenames, pattern, pattern_percent, deps, commands_started,
  914.           commands, commands_idx, two_colon, filename, lineno, set_default)
  915.      struct nameseq *filenames;
  916.      char *pattern, *pattern_percent;
  917.      struct dep *deps;
  918.      unsigned int commands_started;
  919.      char *commands;
  920.      unsigned int commands_idx;
  921.      int two_colon;
  922.      char *filename;
  923.      unsigned int lineno;
  924.      int set_default;
  925. {
  926.   struct nameseq *nextf;
  927.   int implicit = 0;
  928.   unsigned int max_targets, target_idx;
  929.   char **targets = 0, **target_percents = 0;
  930.   struct commands *cmds;
  931.  
  932.   if (commands_idx > 0)
  933.     {
  934.       cmds = (struct commands *) xmalloc (sizeof (struct commands));
  935.       cmds->filename = filename;
  936.       cmds->lineno = commands_started;
  937.       cmds->commands = savestring (commands, commands_idx);
  938.       cmds->command_lines = 0;
  939.     }
  940.   else
  941.     cmds = 0;
  942.  
  943.  
  944.   for (; filenames != 0; filenames = nextf)
  945.     {
  946.       register char *name = filenames->name;
  947.       register struct file *f;
  948.       register struct dep *d;
  949.       struct dep *this;
  950.       char *implicit_percent;
  951.  
  952.       nextf = filenames->next;
  953.       free ((char *) filenames);
  954.  
  955.       implicit_percent = find_percent (name);
  956.       implicit |= implicit_percent != 0;
  957.  
  958.       if (implicit && pattern != 0)
  959.     fatal ("%s:%u: mixed implicit and static pattern rules",
  960.            filename, lineno);
  961.  
  962.       if (implicit && implicit_percent == 0)
  963.     fatal ("%s:%u: mixed implicit and normal rules", filename, lineno);
  964.  
  965.       if (implicit)
  966.     {
  967.       if (targets == 0)
  968.         {
  969.           max_targets = 5;
  970.           targets = (char **) xmalloc (5 * sizeof (char *));
  971.           target_percents = (char **) xmalloc (5 * sizeof (char *));
  972.           target_idx = 0;
  973.         }
  974.       else if (target_idx == max_targets - 1)
  975.         {
  976.           max_targets += 5;
  977.           targets = (char **) xrealloc ((char *) targets,
  978.                         max_targets * sizeof (char *));
  979.           target_percents
  980.         = (char **) xrealloc ((char *) target_percents,
  981.                       max_targets * sizeof (char *));
  982.         }
  983.       targets[target_idx] = name;
  984.       target_percents[target_idx] = implicit_percent;
  985.       ++target_idx;
  986.       continue;
  987.     }
  988.  
  989.       /* If there are multiple filenames, copy the chain DEPS
  990.      for all but the last one.  It is not safe for the same deps
  991.      to go in more than one place in the data base.  */
  992.       this = nextf != 0 ? copy_dep_chain (deps) : deps;
  993.  
  994.       if (pattern != 0)
  995.     /* If this is an extended static rule:
  996.        `targets: target%pattern: dep%pattern; cmds',
  997.        translate each dependency pattern into a plain filename
  998.        using the target pattern and this target's name.  */
  999.     if (!pattern_matches (pattern, pattern_percent, name))
  1000.       {
  1001.         /* Give a warning if the rule is meaningless.  */
  1002.         error ("%s:%u: target `%s' doesn't match the target pattern",
  1003.            filename, lineno, name);
  1004.         this = 0;
  1005.       }
  1006.     else
  1007.       {
  1008.         /* We use patsubst_expand to do the work of translating
  1009.            the target pattern, the target's name and the dependencies'
  1010.            patterns into plain dependency names.  */
  1011.         char *buffer = variable_expand ("");
  1012.  
  1013.         for (d = this; d != 0; d = d->next)
  1014.           {
  1015.         char *o;
  1016.         char *percent = find_percent (d->name);
  1017.         if (percent == 0)
  1018.           continue;
  1019.         o = patsubst_expand (buffer, name, pattern, d->name,
  1020.                      pattern_percent, percent);
  1021.         free (d->name);
  1022.         d->name = savestring (buffer, o - buffer);
  1023.           }
  1024.       }
  1025.       
  1026.       if (!two_colon)
  1027.     {
  1028.       /* Single-colon.  Combine these dependencies
  1029.          with any others in file's existing record, if any.  */
  1030.       f = enter_file (name);
  1031.       if (f->double_colon)
  1032.         fatal ("target file `%s' has both : and :: entries", f->name);
  1033.       /* If CMDS == F->CMDS, this target was listed in this rule
  1034.          more than once.  Just give a warning since this is harmless.  */
  1035.       if (cmds != 0 && cmds == f->cmds)
  1036.         error ("%s:%u: target `%s' given more than once in the same rule.",
  1037.            filename, lineno, f->name);
  1038.       /* Check for two single-colon entries both with commands.
  1039.          Check is_target so that we don't lose on files such as .c.o
  1040.          whose commands were preinitialized.  */
  1041.       else if (cmds != 0 && f->cmds != 0 && f->is_target)
  1042.         fatal ("%s:%u: commands for target `%s' were \
  1043. already specified at %s:%u",
  1044.            cmds->filename, cmds->lineno,
  1045.            f->name, f->cmds->filename, f->cmds->lineno);
  1046.       f->is_target = 1;
  1047.       /* Defining .DEFAULT with no deps or cmds clears it.  */
  1048.       if (f == default_file && this == 0 && cmds == 0)
  1049.         f->cmds = 0;
  1050.       if (cmds != 0)
  1051.         f->cmds = cmds;
  1052.       /* Defining .SUFFIXES with no dependencies
  1053.          clears out the list of suffixes.  */
  1054.       if (f == suffix_file && this == 0)
  1055.         f->deps = 0;
  1056.       else if (f->deps != 0)
  1057.         {
  1058.           d = f->deps;
  1059.           while (d->next != 0)
  1060.         d = d->next;
  1061.           d->next = this;
  1062.         }
  1063.       else
  1064.         f->deps = this;
  1065.  
  1066.       uniquize_deps (f->deps);
  1067.  
  1068.       /* If this is a static pattern rule, set the file's stem to
  1069.          the part of its name that matched the `%' in the pattern,
  1070.          so you can use $* in the commands.  */
  1071.       if (pattern != 0)
  1072.         {
  1073.           static char *percent = "%";
  1074.           char *buffer = variable_expand ("");
  1075.           char *o = patsubst_expand (buffer, name, pattern, percent,
  1076.                      pattern_percent, percent);
  1077.           f->stem = savestring (buffer, o - buffer);
  1078.         }
  1079.     }
  1080.       else
  1081.     {
  1082.       /* Double-colon.  Make a new record
  1083.          even if the file already has one.  */
  1084.       f = lookup_file (name);
  1085.       /* Check for both : and :: rules.  Check is_target so
  1086.          we don't lose on default suffix rules or makefiles.  */
  1087.       if (f != 0 && f->is_target && !f->double_colon)
  1088.         fatal ("target file `%s' has both : and :: entries", f->name);
  1089.       f = enter_file (name);
  1090.       /* If there was an existing entry and it was a
  1091.          double-colon entry, enter_file will have returned a
  1092.          new one, making it the prev pointer of the old one.  */
  1093.       f->is_target = 1;
  1094.       f->double_colon = 1;
  1095.       f->deps = this;
  1096.       f->cmds = cmds;
  1097.     }
  1098.  
  1099.       /* Free name if not needed further.  */
  1100.       if (f != 0 && name != f->name
  1101.       && !(f->name == name - 2 && name[0] == '.' && name[1] == '/'))
  1102.     {
  1103.       free (name);
  1104.       name = f->name;
  1105.     }
  1106.  
  1107.       /* See if this is first target seen whose name does
  1108.      not start with a `.', unless it contains a slash.  */
  1109.       if (default_goal_file == 0 && set_default
  1110.       && (*name != '.' || index (name, '/') != 0))
  1111.     {
  1112.       int reject = 0;
  1113.  
  1114.       /* If this file is a suffix, don't
  1115.          let it be the default goal file.  */
  1116.  
  1117.       for (d = suffix_file->deps; d != 0; d = d->next)
  1118.         {
  1119.           register struct dep *d2;
  1120.           if (*dep_name (d) != '.' && streq (name, dep_name (d)))
  1121.         {
  1122.           reject = 1;
  1123.           break;
  1124.         }
  1125.           for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
  1126.         {
  1127.           register unsigned int len = strlen (dep_name (d2));
  1128.           if (strncmp (name, dep_name (d2), len))
  1129.             continue;
  1130.           if (streq (name + len, dep_name (d)))
  1131.             {
  1132.               reject = 1;
  1133.               break;
  1134.             }
  1135.         }
  1136.           if (reject)
  1137.         break;
  1138.         }
  1139.  
  1140.       if (!reject)
  1141.         default_goal_file = f;
  1142.     }
  1143.     }
  1144.  
  1145.   if (implicit)
  1146.     {
  1147.       targets[target_idx] = 0;
  1148.       target_percents[target_idx] = 0;
  1149.       create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
  1150.       free ((char *) target_percents);
  1151.     }
  1152. }
  1153.  
  1154. /* Search STRING for an unquoted ; that is not after an unquoted #.  */
  1155.  
  1156. static char *
  1157. find_semicolon (string)
  1158.      char *string;
  1159. {
  1160.   char *found, *p;
  1161.  
  1162.   found = index (string, ';');
  1163.   while (found != 0 && found[-1] == '\\')
  1164.     {
  1165.       register char *q = &found[-1];
  1166.       register int backslash = 0;
  1167.       while (*q-- == '\\')
  1168.     backslash = !backslash;
  1169.       if (backslash)
  1170.     found = index (found + 1, ';');
  1171.       else
  1172.     break;
  1173.     }
  1174.   if (found == 0)
  1175.     return 0;
  1176.  
  1177.   /* Look for a comment character (#) before the ; we found.  */
  1178.   p = lindex (string, found, '#');
  1179.   while (p != 0 && p[-1] == '\\')
  1180.     {
  1181.       register char *q = &p[-1];
  1182.       register int backslash = 0;
  1183.       while (*q-- == '\\')
  1184.     backslash = !backslash;
  1185.       if (backslash)
  1186.     p = lindex (p + 1, found, '#');
  1187.       else
  1188.     break;
  1189.     }
  1190.   if (p == 0)
  1191.     return found;
  1192.   return 0;
  1193. }
  1194.  
  1195. /* Search PATTERN for an unquoted %.  Backslashes quote % and backslash.
  1196.    Quoting backslashes are removed from PATTERN by compacting it into
  1197.    itself.  Returns a pointer to the first unquoted % if there is one,
  1198.    or nil if there are none.  */
  1199.  
  1200. char *
  1201. find_percent (pattern)
  1202.      char *pattern;
  1203. {
  1204.   unsigned int pattern_len = strlen (pattern);
  1205.   register char *p = pattern;
  1206.  
  1207.   while ((p = index (p, '%')) != 0)
  1208.     if (p > pattern && p[-1] == '\\')
  1209.       {
  1210.     /* Search for more backslashes.  */
  1211.     register int i = -2;
  1212.     register unsigned int len;
  1213.     while (&p[i] >= pattern && p[i] == '\\')
  1214.       --i;
  1215.     ++i;
  1216.     /* The number of backslashes is now -I.
  1217.        Copy P over itself to swallow half of them.  */
  1218.     len = pattern_len - (p - pattern);
  1219.     bcopy (&p[i / 2], &p[i], len - (i / 2));
  1220.     p -= i / 2;
  1221.     p[len - 1] = '\0';
  1222.     if (i % 2 == 0)
  1223.       /* All the backslashes quoted each other; the % was unquoted.  */
  1224.       return p;
  1225.     else
  1226.       /* The % was quoted by a backslash.  Look for another.  */
  1227.       ++p;
  1228.       }
  1229.     else
  1230.       /* No backslash in sight.  */
  1231.       return p;
  1232.  
  1233.   /* Never hit a %.  */
  1234.   return 0;
  1235. }
  1236.  
  1237. /* Parse a string into a sequence of filenames represented as a
  1238.    chain of struct nameseq's in reverse order and return that chain.
  1239.  
  1240.    The string is passed as STRINGP, the address of a string pointer.
  1241.    The string pointer is updated to point at the first character
  1242.    not parsed, which either is a null char or equals STOPCHAR.
  1243.  
  1244.    SIZE is how big to construct chain elements.
  1245.    This is useful if we want them actually to be other structures
  1246.    that have room for additional info.  */
  1247.  
  1248. struct nameseq *
  1249. parse_file_seq (stringp, stopchar, size)
  1250.      char **stringp;
  1251.      char stopchar;
  1252.      unsigned int size;
  1253. {
  1254.   register struct nameseq *new = 0;
  1255.   register struct nameseq *new1;
  1256.   register char *p = *stringp;
  1257.   char *q;
  1258.   char *name;
  1259.   register int c;
  1260.  
  1261.   while (1)
  1262.     {
  1263.       /* Skip whitespace; see if any more names are left.  */
  1264.       p = next_token (p);
  1265.       if (*p == '\0')
  1266.     break;
  1267.       if (*p == stopchar)
  1268.     break;
  1269.       /* Yes, find end of next name.  */
  1270.       q = p;
  1271. #ifdef MSDOS        /* interpret "c:foo" as file on different drive.  */
  1272.       if (isalpha (p[0]) && p[1] == ':' && !isspace (p[2]))
  1273.     p += 3;
  1274. #endif /* MSDOS */
  1275.       while (1)
  1276.     {
  1277.       c = *p++;
  1278.       if (c == '\0')
  1279.         break;
  1280.       else if (c == '\\' &&
  1281.                (*p == '\\' || *p == ' ' || *p == '\t' || *p == stopchar))
  1282.         ++p;
  1283.       else if (c == ' ' || c == '\t' || c == stopchar)
  1284.         break;
  1285.     }
  1286.       p--;
  1287.  
  1288.       /* Remove leading `./' sequences.  */
  1289.       while (p - q > 2 && q[0] == '.' && q[1] == '/')
  1290.     q += 2;
  1291.  
  1292.       /* Extract the filename just found, and skip it.  */
  1293.       name = savestring (q, p - q);
  1294.  
  1295.       /* Add it to the front of the chain.  */
  1296.       new1 = (struct nameseq *) xmalloc (size);
  1297.       new1->name = name;
  1298.       new1->next = new;
  1299.       new = new1;
  1300.     }
  1301.  
  1302.   *stringp = p;
  1303.   return new;
  1304. }
  1305.  
  1306. /* Read a line of text from STREAM into LINEBUFFER.
  1307.    Combine continuation lines into one line.
  1308.    Return the number of actual lines read (> 1 if hacked continuation lines).
  1309.  */
  1310.  
  1311. static unsigned int
  1312. readline (linebuffer, stream, filename)
  1313.      struct linebuffer *linebuffer;
  1314.      FILE *stream;
  1315.      char *filename;
  1316. {
  1317.   char *buffer = linebuffer->buffer;
  1318.   register char *p = linebuffer->buffer;
  1319.   register char *end = p + linebuffer->size;
  1320.   register int len, lastlen = 0;
  1321.   register char *p2;
  1322.   register unsigned int nlines = 0;
  1323.   register int backslash;
  1324.  
  1325.   *p = '\0';
  1326.  
  1327.   while (1)
  1328.     {
  1329.       if (fgets (p, end - p, stream) == 0)
  1330.     if (feof (stream))
  1331.       return nlines;
  1332.     else
  1333.       pfatal_with_name (filename);
  1334.  
  1335.       len = strlen (p);
  1336.       if (len == 0 || (p += len)[-1] != '\n')
  1337.     {
  1338.       /* Probably ran out of buffer space.  */
  1339.       register unsigned int p_off = p - buffer;
  1340.       linebuffer->size *= 2;
  1341.       buffer = (char *) xrealloc (buffer, linebuffer->size);
  1342.       p = buffer + p_off;
  1343.       end = buffer + linebuffer->size;
  1344.       linebuffer->buffer = buffer;
  1345.       *p = '\0';
  1346.       lastlen = len;
  1347.       continue;
  1348.     }
  1349.  
  1350.       ++nlines;
  1351.  
  1352.       if (len == 1 && p > buffer)
  1353.     /* P is pointing at a newline and it's the beginning of
  1354.        the buffer returned by the last fgets call.  However,
  1355.        it is not necessarily the beginning of a line if P is
  1356.        pointing past the beginning of the holding buffer.
  1357.        If the buffer was just enlarged (right before the newline),
  1358.        we must account for that, so we pretend that the two lines
  1359.        were one line.  */
  1360.     len += lastlen;
  1361.       lastlen = len;
  1362.       backslash = 0;
  1363.       for (p2 = p - 2; --len > 0; --p2)
  1364.     {
  1365.       if (*p2 == '\\')
  1366.         backslash = !backslash;
  1367.       else
  1368.         break;
  1369.     }
  1370.       
  1371.       if (!backslash)
  1372.     {
  1373.       p[-1] = '\0';
  1374.       return nlines;
  1375.     }
  1376.  
  1377.       if (end - p <= 1)
  1378.     {
  1379.       /* Enlarge the buffer.  */
  1380.       register unsigned int p_off = p - buffer;
  1381.       linebuffer->size *= 2;
  1382.       buffer = (char *) xrealloc (buffer, linebuffer->size);
  1383.       p = buffer + p_off;
  1384.       end = buffer + linebuffer->size;
  1385.       linebuffer->buffer = buffer;
  1386.     }
  1387.     }
  1388. }
  1389.  
  1390. /* Construct the list of include directories
  1391.    from the arguments and the default list.  */
  1392.  
  1393. void
  1394. construct_include_path (arg_dirs)
  1395.      char **arg_dirs;
  1396. {
  1397.   register unsigned int i;
  1398.   struct stat stbuf;
  1399.  
  1400.   /* Table to hold the dirs.  */
  1401.  
  1402.   register unsigned int defsize = (sizeof (default_include_directories)
  1403.                    / sizeof (default_include_directories[0]));
  1404.   register unsigned int max = 5;
  1405.   register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
  1406.   register unsigned int idx = 0;
  1407.  
  1408.   /* First consider any dirs specified with -I switches.
  1409.      Ignore dirs that don't exist.  */
  1410.  
  1411.   if (arg_dirs != 0)
  1412.     while (*arg_dirs != 0)
  1413.       {
  1414.     char *dir = *arg_dirs++;
  1415.     if (stat (dir, &stbuf) == 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR)
  1416.       {
  1417.         if (idx == max - 1)
  1418.           {
  1419.         max += 5;
  1420.         dirs = (char **)
  1421.           xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
  1422.           }
  1423.         dirs[idx++] = dir;
  1424.       }
  1425.       }
  1426.  
  1427.   /* Now add at the end the standard default dirs.  */
  1428.  
  1429.   for (i = 0; default_include_directories[i] != 0; ++i)
  1430.     if (stat (default_include_directories[i], &stbuf) == 0
  1431.     && (stbuf.st_mode & S_IFMT) == S_IFDIR)
  1432.       dirs[idx++] = default_include_directories[i];
  1433.  
  1434.   dirs[idx] = 0;
  1435.  
  1436.   /* Now compute the maximum length of any name in it.  */
  1437.  
  1438.   max_incl_len = 0;
  1439.   for (i = 0; i < idx; ++i)
  1440.     {
  1441.       unsigned int len = strlen (dirs[i]);
  1442.       /* If dir name is written with a trailing slash, discard it.  */
  1443.       if (dirs[i][len - 1] == '/')
  1444.     /* We can't just clobber a null in because it may have come from
  1445.        a literal string and literal strings may not be writable.  */
  1446.     dirs[i] = savestring (dirs[i], len - 1);
  1447.       if (len > max_incl_len)
  1448.     max_incl_len = len;
  1449.     }
  1450.  
  1451.   include_directories = dirs;
  1452. }
  1453.  
  1454. /* Given a chain of struct nameseq's describing a sequence of filenames,
  1455.    in reverse of the intended order, return a new chain describing the
  1456.    result of globbing the filenames.  The new chain is in forward order.
  1457.    The links of the old chain are freed or used in the new chain.
  1458.    Likewise for the names in the old chain.
  1459.  
  1460.    SIZE is how big to construct chain elements.
  1461.    This is useful if we want them actually to be other structures
  1462.    that have room for additional info.  */
  1463.  
  1464. struct nameseq *
  1465. multi_glob (chain, size)
  1466.      struct nameseq *chain;
  1467.      unsigned int size;
  1468. {
  1469.   register struct nameseq *new = 0;
  1470.   register struct nameseq *tem;
  1471.   register struct nameseq *old;
  1472.   register char **vec;
  1473.   register unsigned int i;
  1474.   unsigned int length;
  1475.   struct nameseq *nexto;
  1476.  
  1477.   for (old = chain; old != 0; old = nexto)
  1478.     {
  1479.       nexto = old->next;
  1480.  
  1481. #ifndef MSDOS
  1482.       if (*old->name == '~')
  1483.     {
  1484.       if (old->name[1] == '/' || old->name[1] == '\0')
  1485.         {
  1486.           extern char *getenv ();
  1487.           char *home_dir = allocated_variable_expand ("$(HOME)");
  1488.           char is_variable = *home_dir != '\0';
  1489.           if (!is_variable)
  1490.         {
  1491.           free (home_dir);
  1492.           home_dir = getenv ("HOME");
  1493.         }
  1494.           if (home_dir == 0)
  1495.         {
  1496.           unsigned int len = strlen (old->name + 1);
  1497.           bcopy (old->name + 1, old->name, len);
  1498.           old->name[len] = '\0';
  1499.         }
  1500.           else
  1501.         {
  1502.           char *new = concat (home_dir, "", old->name + 1);
  1503.           if (is_variable)
  1504.             free (home_dir);
  1505.           free (old->name);
  1506.           old->name = new;
  1507.         }
  1508.         }
  1509.       else
  1510.         {
  1511.           struct passwd *pwent;
  1512.           char *userend = index (old->name + 1, '/');
  1513.           if (userend != 0)
  1514.         *userend = '\0';
  1515.           pwent = getpwnam (old->name + 1);
  1516.           if (userend == 0)
  1517.         {
  1518.           free (old->name);
  1519.           if (pwent == 0)
  1520.             old->name = savestring ((char *) 0, 0);
  1521.           else
  1522.             old->name = savestring (pwent->pw_dir,
  1523.                         strlen (pwent->pw_dir));
  1524.         }
  1525.           else if (pwent == 0)
  1526.         {
  1527.           unsigned int len;
  1528.           ++userend;
  1529.           len = strlen (userend);
  1530.           bcopy (userend, old->name, len);
  1531.           old->name[len] = '\0';
  1532.         }
  1533.           else
  1534.         {
  1535.           char *new = concat (pwent->pw_dir, "/", userend + 1);
  1536.           free (old->name);
  1537.           old->name = new;
  1538.         }
  1539.         }
  1540.     }
  1541. #endif /* ! MSDOS */
  1542.  
  1543.       if (glob_pattern_p (old->name))
  1544.     {
  1545.       vec = glob_filename (old->name);
  1546.       if (vec == 0)
  1547.         fatal ("virtual memory exhausted");
  1548.         
  1549.       free (old->name);
  1550.  
  1551.       /* NOSTRICT */
  1552.       if (vec != (char **) -1)
  1553.         {
  1554.           i = 0;
  1555.           while (vec[i] != 0)
  1556.         ++i;
  1557.           length = i;
  1558.           qsort ((char *) vec, length, sizeof (char *), alpha_compare);
  1559.           for (i = length; i > 0; --i)
  1560.         {
  1561.           tem = (struct nameseq *) xmalloc (size);
  1562.           tem->name = vec[i - 1];
  1563.           tem->next = new;
  1564.           new = tem;
  1565.         }
  1566.           free ((char *) vec);
  1567.         }
  1568.  
  1569.       free ((char *) old);
  1570.     }
  1571.       else
  1572.     {
  1573.       old->next = new;
  1574.       new = old;
  1575.     }
  1576.     }
  1577.  
  1578.   return new;
  1579. }
  1580.