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