home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bsd / src / make / make-amiga / suff.c < prev    next >
C/C++ Source or Header  |  1993-09-23  |  61KB  |  2,160 lines

  1. /*
  2.  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
  3.  * Copyright (c) 1988, 1989 by Adam de Boor
  4.  * Copyright (c) 1989 by Berkeley Softworks
  5.  * All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Adam de Boor.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  * 3. All advertising materials mentioning features or use of this software
  19.  *    must display the following acknowledgement:
  20.  *    This product includes software developed by the University of
  21.  *    California, Berkeley and its contributors.
  22.  * 4. Neither the name of the University nor the names of its contributors
  23.  *    may be used to endorse or promote products derived from this software
  24.  *    without specific prior written permission.
  25.  *
  26.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  27.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  30.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36.  * SUCH DAMAGE.
  37.  */
  38.  
  39. #ifndef lint
  40. static char sccsid[] = "@(#)suff.c    5.6 (Berkeley) 6/1/90";
  41. #endif /* not lint */
  42.  
  43. /*-
  44.  * suff.c --
  45.  *    Functions to maintain suffix lists and find implicit dependents
  46.  *    using suffix transformation rules
  47.  *
  48.  * Interface:
  49.  *    Suff_Init             Initialize all things to do with suffixes.
  50.  *
  51.  *    Suff_DoPaths            This function is used to make life easier
  52.  *                          when searching for a file according to its
  53.  *                          suffix. It takes the global search path,
  54.  *                          as defined using the .PATH: target, and appends
  55.  *                          its directories to the path of each of the
  56.  *                          defined suffixes, as specified using
  57.  *                          .PATH<suffix>: targets. In addition, all
  58.  *                          directories given for suffixes labeled as
  59.  *                          include files or libraries, using the .INCLUDES
  60.  *                          or .LIBS targets, are played with using
  61.  *                          Dir_MakeFlags to create the .INCLUDES and
  62.  *                          .LIBS global variables.
  63.  *
  64.  *    Suff_ClearSuffixes      Clear out all the suffixes and defined
  65.  *                          transformations.
  66.  *
  67.  *    Suff_IsTransform        Return TRUE if the passed string is the lhs
  68.  *                          of a transformation rule.
  69.  *
  70.  *    Suff_AddSuffix            Add the passed string as another known suffix.
  71.  *
  72.  *    Suff_GetPath            Return the search path for the given suffix.
  73.  *
  74.  *    Suff_AddInclude            Mark the given suffix as denoting an include
  75.  *                          file.
  76.  *
  77.  *    Suff_AddLib            Mark the given suffix as denoting a library.
  78.  *
  79.  *    Suff_AddTransform       Add another transformation to the suffix
  80.  *                          graph. Returns  GNode suitable for framing, I
  81.  *                          mean, tacking commands, attributes, etc. on.
  82.  *
  83.  *    Suff_SetNull            Define the suffix to consider the suffix of
  84.  *                          any file that doesn't have a known one.
  85.  *
  86.  *    Suff_FindDeps            Find implicit sources for and the location of
  87.  *                          a target based on its suffix. Returns the
  88.  *                          bottom-most node added to the graph or NILGNODE
  89.  *                          if the target had no implicit sources.
  90.  */
  91.  
  92. #include          <stdio.h>
  93. #include      "make.h"
  94. #include          "bit.h"
  95.  
  96. static Lst       sufflist;    /* Lst of suffixes */
  97. static Lst       transforms;    /* Lst of transformation rules */
  98.  
  99. static int        sNum = 0;    /* Counter for assigning suffix numbers */
  100.  
  101. /*
  102.  * Structure describing an individual suffix.
  103.  */
  104. typedef struct _Suff {
  105.     char         *name;            /* The suffix itself */
  106.     int         nameLen;    /* Length of the suffix */
  107.     short     flags;          /* Type of suffix */
  108. #define SUFF_INCLUDE      0x01        /* One which is #include'd */
  109. #define SUFF_LIBRARY      0x02        /* One which contains a library */
  110. #define SUFF_NULL       0x04        /* The empty suffix */
  111.     Lst         searchPath;    /* The path along which files of this suffix
  112.                  * may be found */
  113.     int          sNum;              /* The suffix number */
  114.     Lst          parents;    /* Suffixes we have a transformation to */
  115.     Lst          children;    /* Suffixes we have a transformation from */
  116. } Suff;
  117.  
  118. /*
  119.  * Structure used in the search for implied sources.
  120.  */
  121. typedef struct _Src {
  122.     char            *file;    /* The file to look for */
  123.     char            *pref;      /* Prefix from which file was formed */
  124.     Suff            *suff;    /* The suffix on the file */
  125.     struct _Src     *parent;    /* The Src for which this is a source */
  126.     GNode           *node;    /* The node describing the file */
  127.     int                children;    /* Count of existing children (so we don't free
  128.                  * this thing too early or never nuke it) */
  129. } Src;
  130.  
  131. static Suff         *suffNull;    /* The NULL suffix for this run */
  132. static Suff         *emptySuff;    /* The empty suffix required for POSIX
  133.                  * single-suffix transformation rules */
  134.  
  135.     /*************** Lst Predicates ****************/
  136. /*-
  137.  *-----------------------------------------------------------------------
  138.  * SuffStrIsPrefix  --
  139.  *    See if pref is a prefix of str.
  140.  *
  141.  * Results:
  142.  *    NULL if it ain't, pointer to character in str after prefix if so
  143.  *
  144.  * Side Effects:
  145.  *    None
  146.  *-----------------------------------------------------------------------
  147.  */
  148. static char    *
  149. SuffStrIsPrefix (pref, str)
  150.     register char  *pref;    /* possible prefix */
  151.     register char  *str;    /* string to check */
  152. {
  153.     while (*str && *pref == *str) {
  154.     pref++;
  155.     str++;
  156.     }
  157.  
  158.     return (*pref ? NULL : str);
  159. }
  160.  
  161. /*-
  162.  *-----------------------------------------------------------------------
  163.  * SuffSuffIsSuffix  --
  164.  *    See if suff is a suffix of str. Str should point to THE END of the
  165.  *    string to check. (THE END == the null byte)
  166.  *
  167.  * Results:
  168.  *    NULL if it ain't, pointer to character in str before suffix if
  169.  *    it is.
  170.  *
  171.  * Side Effects:
  172.  *    None
  173.  *-----------------------------------------------------------------------
  174.  */
  175. static char *
  176. SuffSuffIsSuffix (s, str)
  177.     register Suff  *s;        /* possible suffix */
  178.     char           *str;    /* string to examine */
  179. {
  180.     register char  *p1;            /* Pointer into suffix name */
  181.     register char  *p2;            /* Pointer into string being examined */
  182.  
  183.     p1 = s->name + s->nameLen;
  184.     p2 = str;
  185.  
  186.     while (p1 >= s->name && *p1 == *p2) {
  187.     p1--;
  188.     p2--;
  189.     }
  190.  
  191.     return (p1 == s->name - 1 ? p2 : NULL);
  192. }
  193.  
  194. /*-
  195.  *-----------------------------------------------------------------------
  196.  * SuffSuffIsSuffixP --
  197.  *    Predicate form of SuffSuffIsSuffix. Passed as the callback function
  198.  *    to Lst_Find.
  199.  *
  200.  * Results:
  201.  *    0 if the suffix is the one desired, non-zero if not.
  202.  *
  203.  * Side Effects:
  204.  *    None.
  205.  *
  206.  *-----------------------------------------------------------------------
  207.  */
  208. SuffSuffIsSuffixP(s, str)
  209.     Suff        *s;
  210.     char        *str;
  211. {
  212.     return(!SuffSuffIsSuffix(s, str));
  213. }
  214.  
  215. /*-
  216.  *-----------------------------------------------------------------------
  217.  * SuffSuffHasNameP --
  218.  *    Callback procedure for finding a suffix based on its name. Used by
  219.  *    Suff_GetPath.
  220.  *
  221.  * Results:
  222.  *    0 if the suffix is of the given name. non-zero otherwise.
  223.  *
  224.  * Side Effects:
  225.  *    None
  226.  *-----------------------------------------------------------------------
  227.  */
  228. static int
  229. SuffSuffHasNameP (s, sname)
  230.     Suff    *s;                /* Suffix to check */
  231.     char    *sname;         /* Desired name */
  232. {
  233.     return (strcmp (sname, s