home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / gnu / make-3.71-src.lha / src / amiga / make-3.71 / read.c < prev    next >
C/C++ Source or Header  |  1994-06-22  |  54KB  |  2,039 lines

  1. /* Reading and parsing of makefiles for GNU Make.
  2. Copyright (C) 1988, 89, 90, 91, 92, 93, 94 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. #ifndef amigados
  85.     INCLUDEDIR,
  86.     "/usr/gnu/include",
  87.     "/usr/local/include",
  88.     "/usr/include",
  89. #else
  90.     "/gnu/include",
  91.     "/gnu/g++-include",
  92.     "/gnu/os-include",
  93.     "/local/include",
  94.     "/local/g++-include",
  95.     "/local/os-include",
  96. #endif
  97.     0
  98.   };
  99.  
  100. /* List of directories to search for include files in  */
  101.  
  102. static char **include_directories;
  103.  
  104. /* Maximum length of an element of the above.  */
  105.  
  106. static unsigned int max_incl_len;
  107.  
  108. /* The filename and pointer to line number of the
  109.    makefile currently being read in.  */
  110.  
  111. char *reading_filename;
  112. unsigned int *reading_lineno_ptr;
  113.  
  114. /* The chain of makefiles read by read_makefile.  */
  115.  
  116. static struct dep *read_makefiles = 0;
  117.  
  118. /* Read in all the makefiles and return the chain of their names.  */
  119.  
  120. struct dep *
  121. read_all_makefiles (makefiles)
  122.      char **makefiles;
  123. {
  124.   unsigned int num_makefiles = 0;
  125.  
  126.   if (debug_flag)
  127.     puts ("Reading makefiles...");
  128.  
  129.   /* If there's a non-null variable MAKEFILES, its value is a list of
  130.      files to read first thing.  But don't let it prevent reading the
  131.      default makefiles and don't let the default goal come from there.  */
  132.  
  133.   {
  134.     char *value;
  135.     char *name, *p;
  136.     unsigned int length;
  137.  
  138.     {
  139.       /* Turn off --warn-undefined-variables while we expand MAKEFILES.  */
  140.       int save = warn_undefined_variables_flag;
  141.       warn_undefined_variables_flag = 0;
  142.  
  143.       value = allocated_variable_expand ("$(MAKEFILES)");
  144.  
  145.       warn_undefined_variables_flag = save;
  146.     }
  147.  
  148.     /* Set NAME to the start of next token and LENGTH to its length.
  149.        MAKEFILES is updated for finding remaining tokens.  */
  150.     p = value;
  151.     while ((name = find_next_token (&p, &length)) != 0)
  152.       {
  153.     if (*p != '\0')
  154.       *p++ = '\0';
  155.     (void) read_makefile (name,
  156.                   RM_NO_DEFAULT_GOAL | RM_INCLUDED | RM_DONTCARE);
  157.       }
  158.  
  159.     free (value);
  160.   }
  161.  
  162.   /* Read makefiles specified with -f switches.  */
  163.  
  164.   if (makefiles != 0)
  165.     while (*makefiles != 0)
  166.       {
  167.     struct dep *tail = read_makefiles;
  168.     register struct dep *d;
  169.  
  170.     if (! read_makefile (*makefiles, 0))
  171.       perror_with_name ("", *makefiles);
  172.  
  173.     /* Find the right element of read_makefiles.  */
  174.     d = read_makefiles;
  175.     while (d->next != tail)
  176.       d = d->next;
  177.  
  178.     /* Use the storage read_makefile allocates.  */
  179.     *makefiles = dep_name (d);
  180.     ++num_makefiles;
  181.     ++makefiles;
  182.       }
  183.  
  184.   /* If there were no -f switches, try the default names.  */
  185.  
  186.   if (num_makefiles == 0)
  187.     {
  188.       static char *default_makefiles[] =
  189.     { "GNUmakefile", "makefile", "Makefile", 0 };
  190.       register char **p = default_makefiles;
  191.       while (*p != 0 && !file_exists_p (*p))
  192.     ++p;
  193.  
  194.       if (*p != 0)
  195.     {
  196.       if (! read_makefile (*p, 0))
  197.         perror_with_name ("", *p);
  198.     }
  199.       else
  200.     {
  201.       /* No default makefile was found.  Add the default makefiles to the
  202.          `read_makefiles' chain so they will be updated if possible.  */
  203.       struct dep *tail = read_makefiles;
  204.       for (p = default_makefiles; *p != 0; ++p)
  205.         {
  206.           struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
  207.           d->name = 0;
  208.           d->file = enter_file (*p);
  209.           d->file->dontcare = 1;
  210.           /* Tell update_goal_chain to bail out as soon as this file is
  211.          made, and main not to die if we can't make this file.  */
  212.           d->changed = RM_DONTCARE;
  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.  
  229.    FLAGS contains bits as above.
  230.  
  231.    FILENAME is added to the `read_makefiles' chain.
  232.  
  233.    Returns 1 if a file was found and read, 0 if not.  */
  234.  
  235. static int
  236. read_makefile (filename, flags)
  237.      char *filename;
  238.      int flags;
  239. {
  240.   static char *collapsed = 0;
  241.   static unsigned int collapsed_length = 0;
  242.   register FILE *infile;
  243.   struct linebuffer lb;
  244.   unsigned int commands_len = 200;
  245.   char *commands = (char *) xmalloc (200);
  246.   unsigned int commands_idx = 0;
  247.   unsigned int commands_started;
  248.   register char *p;
  249.   char *p2;
  250.   int ignoring = 0, in_ignored_define = 0;
  251.   int no_targets = 0;        /* Set when reading a rule without targets.  */
  252.  
  253.   struct nameseq *filenames = 0;
  254.   struct dep *deps;
  255.   unsigned int lineno = 1;
  256.   unsigned int nlines = 0;
  257.   int two_colon;
  258.   char *pattern = 0, *pattern_percent;
  259.  
  260.   int makefile_errno;
  261.  
  262. #define record_waiting_files()                              \
  263.   do                                          \
  264.     {                                           \
  265.       if (filenames != 0)                              \
  266.     record_files (filenames, pattern, pattern_percent, deps,          \
  267.               commands_started, commands, commands_idx,              \
  268.               two_colon, filename, lineno,                  \
  269.               !(flags & RM_NO_DEFAULT_GOAL));                       \
  270.       filenames = 0;                                  \
  271.       commands_idx = 0;                                  \
  272.       pattern = 0;                                  \
  273.     } while (0)
  274.  
  275. #ifdef    lint    /* Suppress `used before set' messages.  */
  276.   two_colon = 0;
  277. #endif
  278.  
  279.   if (debug_flag)
  280.     {
  281.       printf ("Reading makefile `%s'", filename);
  282.       if (flags & RM_NO_DEFAULT_GOAL)
  283.     printf (" (no default goal)");
  284.       if (flags & RM_INCLUDED)
  285.     printf (" (search path)");
  286.       if (flags & RM_DONTCARE)
  287.     printf (" (don't care)");
  288.       if (flags & RM_NO_TILDE)
  289.     printf (" (no ~ expansion)");
  290.       puts ("...");
  291.     }
  292.  
  293.   /* First, get a stream to read.  */
  294.  
  295.   /* Expand ~ in FILENAME unless it came from `include',
  296.      in which case it was already done.  */
  297.   if (!(flags & RM_NO_TILDE) && filename[0] == '~')
  298.     {
  299.       char *expanded = tilde_expand (filename)