home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / make3_60.lzh / MAKE3_60 / VPATH.C < prev   
C/C++ Source or Header  |  1993-07-30  |  12KB  |  418 lines

  1. /* Copyright (C) 1988, 1989 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. #include "make.h"
  19. #include "file.h"
  20. #include "variable.h"
  21.  
  22.  
  23. /* Structure used to represent a selective VPATH searchpath.  */
  24.  
  25. struct vpath
  26.   {
  27.     struct vpath *next;    /* Pointer to next struct in the linked list.  */
  28.     char *pattern;    /* The pattern to match.  */
  29.     char *percent;    /* Pointer into `pattern' where the `%' is.  */
  30.     unsigned int patlen;/* Length of the pattern.  */
  31.     char **searchpath;    /* Null-terminated list of directories.  */
  32.     unsigned int maxlen;/* Maximum length of any entry in the list.  */
  33.   };
  34.  
  35. /* Linked-list of all selective VPATHs.  */
  36.  
  37. static struct vpath *vpaths;
  38.  
  39. /* Structure for the general VPATH given in the variable.  */
  40.  
  41. static struct vpath *general_vpath;
  42.  
  43. static int selective_vpath_search ();
  44.  
  45. /* Reverse the chain of selective VPATH lists so they
  46.    will be searched in the order given in the makefiles
  47.    and construct the list from the VPATH variable.  */
  48.  
  49. void
  50. build_vpath_lists ()
  51. {
  52.   register struct vpath *new = 0;
  53.   register struct vpath *old, *nexto;
  54.   register char *p;
  55.  
  56.   /* Reverse the chain.  */
  57.   for (old = vpaths; old != 0; old = nexto)
  58.     {
  59.       nexto = old->next;
  60.       old->next = new;
  61.       new = old;
  62.     }
  63.  
  64.   vpaths = new;
  65.  
  66.   /* If there is a VPATH variable with a nonnull value, construct the
  67.      general VPATH list from it.  We use variable_expand rather than just
  68.      calling lookup_variable so that it will be recursively expanded.  */
  69.   p = variable_expand ("$(VPATH)");
  70.   if (*p != '\0')
  71.     {
  72.       construct_vpath_list ("%", p);
  73.       /* VPATHS will be nil if there have been no previous `vpath'
  74.      directives and none of the given directories exists.  */
  75.       if (vpaths == 0)
  76.     general_vpath = 0;
  77.       else
  78.     {
  79.       general_vpath = vpaths;
  80.       /* It was just put into the linked list,
  81.          but we don't want it there, so we must remove it.  */
  82.       vpaths = general_vpath->next;
  83.     }
  84.     }
  85. }
  86.  
  87. /* Construct the VPATH listing for the pattern and searchpath given.
  88.  
  89.    This function is called to generate selective VPATH lists and also for
  90.    the general VPATH list (which is in fact just a selective VPATH that
  91.    is applied to everything).  The returned pointer is either put in the
  92.    linked list of all selective VPATH lists or in the GENERAL_VPATH
  93.    variable.
  94.  
  95.    If SEARCHPATH is nil, remove all previous listings with the same
  96.    pattern.  If PATTERN is nil, remove all VPATH listings.
  97.    Existing and readable directories that are not "." given in the
  98.    searchpath separated by colons are loaded into the directory hash
  99.    table if they are not there already and put in the VPATH searchpath
  100.    for the given pattern with trailing slashes stripped off if present
  101.    (and if the directory is not the root, "/").
  102.    The length of the longest entry in the list is put in the structure as well.
  103.    The new entry will be at the head of the VPATHS chain.  */
  104.  
  105. void
  106. construct_vpath_list (pattern, dirpath)
  107.      char *pattern, *dirpath;
  108. {
  109.   register unsigned int elem;
  110.   register char *p;
  111.   register char **vpath;
  112.   register unsigned int maxvpath;
  113.   unsigned int maxelem;
  114.   char *percent;
  115.  
  116.   if (pattern != 0)
  117.     {
  118.       pattern = savestring (pattern, strlen (pattern));
  119.       percent = find_percent (pattern);
  120.     }
  121.  
  122.   if (dirpath == 0)
  123.     {
  124.       /* Remove matching listings.  */
  125.       register struct vpath *path, *lastpath;
  126.  
  127.       lastpath = vpaths;
  128.       for (path = vpaths; path != 0; lastpath = path, path = path->next)
  129.     if (pattern == 0
  130.         || (((percent == 0 && path->percent == 0)
  131.          || (percent - pattern == path->percent - path->pattern))
  132.         && streq (pattern, path->pattern)))
  133.       {
  134.         /* Remove it from the linked list.  */
  135.         if (lastpath == vpaths)
  136.           vpaths = path->next;
  137.         else
  138.           lastpath->next = path->next;
  139.  
  140.         /* Free its unused storage.  */
  141.         free (path->pattern);
  142.         free ((char *) path->searchpath);
  143.         free ((char *) path);
  144.       }
  145.       if (pattern != 0)
  146.     free (pattern);
  147.       return;
  148.     }
  149.  
  150.   /* Skip over any initial colons.  */
  151.   p = dirpath;
  152.   while (*p == ':')
  153.     ++p;
  154.  
  155.   /* Figure out the maximum number of VPATH entries and
  156.      put it in MAXELEM.  We start with 2, one before the
  157.      first colon and one nil, the list terminator and
  158.      increment our estimated number for each colon we find.  */
  159.   maxelem = 2;
  160.   while (*p != '\0')
  161.     if (*p++ == ':')
  162.       ++maxelem;
  163.  
  164.   vpath = (char **) xmalloc (maxelem * sizeof (char *));
  165.   maxvpath = 0;
  166.  
  167.   elem = 0;
  168.   p = dirpath;
  169.   while (*p != '\0')
  170.     {
  171.       char *v;
  172.       unsigned int len;
  173.  
  174.       /* Find the next entry.  */
  175.       while (*p != '\0' && *p == ':')
  176.     ++p;
  177.       if (*p == '\0')
  178.     break;
  179.  
  180.       /* Find the end of this entry.  */
  181.       v = p;
  182.       while (*p != '\0' && *p != ':')
  183.     ++p;
  184.  
  185.       len = p - v;
  186.       /* Make sure there's no trailing slash,
  187.      but still allow "/" as a directory.  */
  188.       if (len > 1 && p[-1] == '/')
  189.     --len;
  190.  
  191.       if (len == 1 && *v == '.')
  192.     continue;
  193.  
  194.       v = savestring (v, len);
  195.       if (dir_file_exists_p (v, ""))
  196.     {
  197.       vpath[elem++] = dir_name (v);
  198.       free (v);
  199.       if (len > maxvpath)
  200.         maxvpath = len;
  201.     }
  202.       else
  203.     free (v);
  204.     }
  205.  
  206.   if (elem > 0)
  207.     {
  208.       struct vpath *path;
  209.       /* ELEM is now incremented one element past the last
  210.      entry, to where the nil-pointer terminator goes.
  211.      Usually this is maxelem - 1.  If not, shrink down.  */
  212.       if (elem < (maxelem - 1))
  213.     vpath = (char **) xrealloc ((char *) vpath,
  214.                     (elem + 1) * sizeof (char *));
  215.  
  216.       /* Put the nil-pointer terminator on the end of the VPATH list.  */
  217.       vpath[elem] = 0;
  218.  
  219.       /* Construct the vpath structure and put it into the linked list.  */
  220.       path = (struct vpath *) xmalloc (sizeof (struct vpath));
  221.       path->searchpath = vpath;
  222.       path->maxlen = maxvpath;
  223.       path->next = vpaths;
  224.       vpaths = path;
  225.  
  226.       /* Set up the members.  */
  227.       path->pattern = pattern;
  228.       path->percent = percent;
  229.       path->patlen = strlen (pattern);
  230.     }
  231.   else
  232.     /* There were no entries, so free whatever space we allocated.  */
  233.     free ((char *) vpath);
  234. }
  235.  
  236. /* Search the VPATH list whose pattern matches *FILE for a directory
  237.    where the name pointed to by FILE exists.  If it is found, the pointer
  238.    in FILE is set to the newly malloc'd name of the existing file and
  239.    we return 1.  Otherwise we return 0.  */
  240.  
  241. int
  242. vpath_search (file)
  243.      char **file;
  244. {
  245.   register struct vpath *v;
  246.  
  247.   /* If there are no VPATH entries or FILENAME starts at the root,
  248.      there is nothing we can do.  */
  249.  
  250.   if (**file == '/' || (vpaths == 0 && general_vpath == 0))
  251.     return 0;
  252.  
  253.   for (v = vpaths; v != 0; v = v->next)
  254.     if (pattern_matches (v->pattern, v->percent, *file))
  255.       if (selective_vpath_search (v, file))
  256.     return 1;
  257.  
  258.   if (general_vpath != 0
  259.       && selective_vpath_search (general_vpath, file))
  260.     return 1;
  261.  
  262.   return 0;
  263. }
  264.  
  265.  
  266. /* Search the given VPATH list for a directory where the name pointed
  267.    to by FILE exists.  If it is found, the pointer in FILE
  268.    is set to the newly malloc'd name of the existing file and we return 1.
  269.    Otherwise we return 0.  */
  270.  
  271. static int
  272. selective_vpath_search (path, file)
  273.      struct vpath *path;
  274.      char **file;
  275. {
  276.   int not_target;
  277.   char *name, *n;
  278.   char *filename;
  279.   register char **vpath = path->searchpath;
  280.   unsigned int maxvpath = path->maxlen;
  281.   register unsigned int i;
  282.   unsigned int flen, vlen, name_dplen;
  283.   int exists = 0;
  284.  
  285.   /* Find out if *FILE is a target.
  286.      If and only if it is NOT a target, we will accept prospective
  287.      files that don't exist but are mentioned in a makefile.  */
  288.   {
  289.     struct file *f = lookup_file (*file);
  290.     not_target = f == 0 || !f->is_target;
  291.   }
  292.  
  293.   flen = strlen (*file);
  294.  
  295.   /* Split *FILE into a directory prefix and a name-within-directory.
  296.      NAME_DPLEN gets the length of the prefix; FILENAME gets the
  297.      pointer to the name-within-directory and FLEN is its length.  */
  298.  
  299.   n = rindex (*file, '/');
  300.   name_dplen = n != 0 ? n - *file : 0;
  301.   filename = name_dplen > 0 ? n + 1 : *file;
  302.   if (name_dplen > 0)
  303.     flen -= name_dplen + 1;
  304.  
  305.   /* Allocate enough space for the biggest VPATH entry,
  306.      a slash, the directory prefix that came with *FILE,
  307.      another slash (although this one may not always be
  308.      necessary), the filename, and a null terminator.  */
  309.   name = (char *) alloca (maxvpath + 1 + name_dplen + 1 + flen + 1);
  310.  
  311.   /* Try each VPATH entry.  */
  312.   for (i = 0; vpath[i] != 0; ++i)
  313.     {
  314.       n = name;
  315.  
  316.       /* Put the next VPATH entry into NAME at N and increment N past it.  */
  317.       vlen = strlen (vpath[i]);
  318.       bcopy (vpath[i], n, vlen);
  319.       n += vlen;
  320.  
  321.       /* Add the directory prefix already in *FILE.  */
  322.       if (name_dplen > 0)
  323.     {
  324.       *n++ = '/';
  325.       bcopy (*file, n, name_dplen);
  326.       n += name_dplen;
  327.     }
  328.  
  329.       /* Now add the name-within-directory at the end of NAME.  */
  330.       if (n != name && n[-1] != '/')
  331.     *n = '/';
  332.       bcopy (filename, n + 1, flen + 1);
  333.  
  334.       if (not_target)
  335.     /* Since *FILE is not a target, if the file is
  336.        mentioned in a makefile, we consider it existent.  */
  337.     exists = lookup_file (name) != 0;
  338.  
  339.       if (!exists)
  340.     {
  341.       /* That file wasn't mentioned in the makefile.
  342.          See if it actually exists.  */
  343.  
  344.       /* Clobber a null into the name at the last slash.
  345.          Now NAME is the name of the directory to look in.  */
  346.       *n = '\0';
  347.  
  348.       /* Make sure the directory exists and we know its contents.  */
  349.       if (name_dplen > 0 && !dir_file_exists_p (name, ""))
  350.         /* It doesn't exist.  */
  351.         continue;
  352.  
  353.       /* We know the directory is in the hash table now because either
  354.          construct_vpath_list or the code just above put it there.
  355.          Does the file we seek exist in it?  */
  356.       exists = dir_file_exists_p (name, filename);
  357.     }
  358.  
  359.       if (exists)
  360.     {
  361.       /* We have found a file.
  362.          Store the name we found into *FILE for the caller.  */
  363.  
  364.       /* Put the slash back in NAME.  */
  365.       *n = '/';
  366.  
  367.       *file = savestring (name, (n + 1 - name) + flen);
  368.  
  369.       return 1;
  370.     }
  371.     }
  372.  
  373.   return 0;
  374. }
  375.  
  376. /* Print the data base of VPATH search paths.  */
  377.  
  378. void
  379. print_vpath_data_base ()
  380. {
  381.   register unsigned int nvpaths;
  382.   register struct vpath *v;
  383.  
  384.   puts ("\n# VPATH Search Paths\n");
  385.  
  386.   nvpaths = 0;
  387.   for (v = vpaths; v != 0; v = v->next)
  388.     {
  389.       register unsigned int i;
  390.  
  391.       ++nvpaths;
  392.  
  393.       printf ("vpath %s ", v->pattern);
  394.  
  395.       for (i = 0; v->searchpath[i] != 0; ++i)
  396.     printf ("%s%c", v->searchpath[i],
  397.         v->searchpath[i + 1] == 0 ? '\n' : ':');
  398.     }
  399.  
  400.   if (vpaths == 0)
  401.     puts ("# No `vpath' search paths.");
  402.   else
  403.     printf ("\n# %u `vpath' search paths.\n", nvpaths);
  404.  
  405.   if (general_vpath == 0)
  406.     puts ("\n# No general (`VPATH' variable) search path.");
  407.   else
  408.     {
  409.       register char **path = general_vpath->searchpath;
  410.       register unsigned int i;
  411.  
  412.       fputs ("\n# General (`VPATH' variable) search path:\n# ", stdout);
  413.  
  414.       for (i = 0; path[i] != 0; ++i)
  415.     printf ("%s%c", path[i], path[i + 1] == 0 ? '\n' : ':');
  416.     }
  417. }
  418.