[BACK]Return to suff.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / make

Annotation of src/usr.bin/make/suff.c, Revision 1.16

1.16    ! espie       1: /*     $OpenBSD: suff.c,v 1.15 1999/12/06 22:28:44 espie Exp $ */
1.5       millert     2: /*     $NetBSD: suff.c,v 1.13 1996/11/06 17:59:25 christos Exp $       */
1.1       deraadt     3:
                      4: /*
1.5       millert     5:  * Copyright (c) 1988, 1989, 1990, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
1.1       deraadt     7:  * Copyright (c) 1989 by Berkeley Softworks
                      8:  * All rights reserved.
                      9:  *
                     10:  * This code is derived from software contributed to Berkeley by
                     11:  * Adam de Boor.
                     12:  *
                     13:  * Redistribution and use in source and binary forms, with or without
                     14:  * modification, are permitted provided that the following conditions
                     15:  * are met:
                     16:  * 1. Redistributions of source code must retain the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer.
                     18:  * 2. Redistributions in binary form must reproduce the above copyright
                     19:  *    notice, this list of conditions and the following disclaimer in the
                     20:  *    documentation and/or other materials provided with the distribution.
                     21:  * 3. All advertising materials mentioning features or use of this software
                     22:  *    must display the following acknowledgement:
                     23:  *     This product includes software developed by the University of
                     24:  *     California, Berkeley and its contributors.
                     25:  * 4. Neither the name of the University nor the names of its contributors
                     26:  *    may be used to endorse or promote products derived from this software
                     27:  *    without specific prior written permission.
                     28:  *
                     29:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     30:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     31:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     32:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     33:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     34:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     35:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     36:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     37:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     38:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     39:  * SUCH DAMAGE.
                     40:  */
                     41:
                     42: #ifndef lint
                     43: #if 0
1.5       millert    44: static char sccsid[] = "@(#)suff.c     8.4 (Berkeley) 3/21/94";
1.1       deraadt    45: #else
1.16    ! espie      46: static char rcsid[] = "$OpenBSD: suff.c,v 1.15 1999/12/06 22:28:44 espie Exp $";
1.1       deraadt    47: #endif
                     48: #endif /* not lint */
                     49:
                     50: /*-
                     51:  * suff.c --
                     52:  *     Functions to maintain suffix lists and find implicit dependents
                     53:  *     using suffix transformation rules
                     54:  *
                     55:  * Interface:
                     56:  *     Suff_Init               Initialize all things to do with suffixes.
                     57:  *
                     58:  *     Suff_End                Cleanup the module
                     59:  *
                     60:  *     Suff_DoPaths            This function is used to make life easier
                     61:  *                             when searching for a file according to its
                     62:  *                             suffix. It takes the global search path,
                     63:  *                             as defined using the .PATH: target, and appends
                     64:  *                             its directories to the path of each of the
                     65:  *                             defined suffixes, as specified using
                     66:  *                             .PATH<suffix>: targets. In addition, all
                     67:  *                             directories given for suffixes labeled as
                     68:  *                             include files or libraries, using the .INCLUDES
                     69:  *                             or .LIBS targets, are played with using
                     70:  *                             Dir_MakeFlags to create the .INCLUDES and
                     71:  *                             .LIBS global variables.
                     72:  *
                     73:  *     Suff_ClearSuffixes      Clear out all the suffixes and defined
                     74:  *                             transformations.
                     75:  *
                     76:  *     Suff_IsTransform        Return TRUE if the passed string is the lhs
                     77:  *                             of a transformation rule.
                     78:  *
                     79:  *     Suff_AddSuffix          Add the passed string as another known suffix.
                     80:  *
                     81:  *     Suff_GetPath            Return the search path for the given suffix.
                     82:  *
                     83:  *     Suff_AddInclude         Mark the given suffix as denoting an include
                     84:  *                             file.
                     85:  *
                     86:  *     Suff_AddLib             Mark the given suffix as denoting a library.
                     87:  *
                     88:  *     Suff_AddTransform       Add another transformation to the suffix
                     89:  *                             graph. Returns  GNode suitable for framing, I
                     90:  *                             mean, tacking commands, attributes, etc. on.
                     91:  *
                     92:  *     Suff_SetNull            Define the suffix to consider the suffix of
                     93:  *                             any file that doesn't have a known one.
                     94:  *
                     95:  *     Suff_FindDeps           Find implicit sources for and the location of
                     96:  *                             a target based on its suffix. Returns the
                     97:  *                             bottom-most node added to the graph or NILGNODE
                     98:  *                             if the target had no implicit sources.
                     99:  */
                    100:
                    101: #include         <stdio.h>
                    102: #include         "make.h"
                    103: #include         "hash.h"
                    104: #include         "dir.h"
                    105:
                    106: static Lst       sufflist;     /* Lst of suffixes */
1.14      espie     107: #ifdef CLEANUP
1.1       deraadt   108: static Lst      suffClean;     /* Lst of suffixes to be cleaned */
1.14      espie     109: #endif
1.1       deraadt   110: static Lst      srclist;       /* Lst of sources */
                    111: static Lst       transforms;   /* Lst of transformation rules */
                    112:
                    113: static int        sNum = 0;    /* Counter for assigning suffix numbers */
                    114:
                    115: /*
                    116:  * Structure describing an individual suffix.
                    117:  */
                    118: typedef struct _Suff {
                    119:     char         *name;                /* The suffix itself */
                    120:     int                 nameLen;       /* Length of the suffix */
                    121:     short       flags;         /* Type of suffix */
                    122: #define SUFF_INCLUDE     0x01      /* One which is #include'd */
                    123: #define SUFF_LIBRARY     0x02      /* One which contains a library */
                    124: #define SUFF_NULL        0x04      /* The empty suffix */
                    125:     Lst         searchPath;    /* The path along which files of this suffix
                    126:                                 * may be found */
                    127:     int          sNum;         /* The suffix number */
                    128:     int                 refCount;      /* Reference count of list membership */
                    129:     Lst          parents;      /* Suffixes we have a transformation to */
                    130:     Lst          children;     /* Suffixes we have a transformation from */
                    131:     Lst                 ref;           /* List of lists this suffix is referenced */
                    132: } Suff;
                    133:
                    134: /*
                    135:  * Structure used in the search for implied sources.
                    136:  */
                    137: typedef struct _Src {
                    138:     char            *file;     /* The file to look for */
                    139:     char           *pref;      /* Prefix from which file was formed */
                    140:     Suff            *suff;     /* The suffix on the file */
                    141:     struct _Src     *parent;   /* The Src for which this is a source */
                    142:     GNode           *node;     /* The node describing the file */
                    143:     int                    children;   /* Count of existing children (so we don't free
                    144:                                 * this thing too early or never nuke it) */
                    145: #ifdef DEBUG_SRC
                    146:     Lst                    cp;         /* Debug; children list */
                    147: #endif
                    148: } Src;
                    149:
                    150: /*
                    151:  * A structure for passing more than one argument to the Lst-library-invoked
                    152:  * function...
                    153:  */
                    154: typedef struct {
                    155:     Lst            l;
                    156:     Src            *s;
                    157: } LstSrc;
                    158:
                    159: static Suff        *suffNull;  /* The NULL suffix for this run */
                    160: static Suff        *emptySuff; /* The empty suffix required for POSIX
                    161:                                 * single-suffix transformation rules */
                    162:
                    163:
                    164: static char *SuffStrIsPrefix __P((char *, char *));
                    165: static char *SuffSuffIsSuffix __P((Suff *, char *));
                    166: static int SuffSuffIsSuffixP __P((ClientData, ClientData));
                    167: static int SuffSuffHasNameP __P((ClientData, ClientData));
                    168: static int SuffSuffIsPrefix __P((ClientData, ClientData));
                    169: static int SuffGNHasNameP __P((ClientData, ClientData));
1.5       millert   170: static void SuffUnRef __P((ClientData, ClientData));
1.1       deraadt   171: static void SuffFree __P((ClientData));
                    172: static void SuffInsert __P((Lst, Suff *));
1.10      millert   173: static Suff *SuffRemove __P((Lst, Suff *));
1.1       deraadt   174: static Boolean SuffParseTransform __P((char *, Suff **, Suff **));
                    175: static int SuffRebuildGraph __P((ClientData, ClientData));
                    176: static int SuffAddSrc __P((ClientData, ClientData));
                    177: static int SuffRemoveSrc __P((Lst));
                    178: static void SuffAddLevel __P((Lst, Src *));
                    179: static Src *SuffFindThem __P((Lst, Lst));
                    180: static Src *SuffFindCmds __P((Src *, Lst));
                    181: static int SuffExpandChildren __P((ClientData, ClientData));
                    182: static Boolean SuffApplyTransform __P((GNode *, GNode *, Suff *, Suff *));
                    183: static void SuffFindDeps __P((GNode *, Lst));
                    184: static void SuffFindArchiveDeps __P((GNode *, Lst));
                    185: static void SuffFindNormalDeps __P((GNode *, Lst));
                    186: static int SuffPrintName __P((ClientData, ClientData));
                    187: static int SuffPrintSuff __P((ClientData, ClientData));
                    188: static int SuffPrintTrans __P((ClientData, ClientData));
                    189:
                    190:        /*************** Lst Predicates ****************/
                    191: /*-
                    192:  *-----------------------------------------------------------------------
                    193:  * SuffStrIsPrefix  --
                    194:  *     See if pref is a prefix of str.
                    195:  *
                    196:  * Results:
                    197:  *     NULL if it ain't, pointer to character in str after prefix if so
                    198:  *
                    199:  * Side Effects:
                    200:  *     None
                    201:  *-----------------------------------------------------------------------
                    202:  */
                    203: static char    *
                    204: SuffStrIsPrefix (pref, str)
                    205:     register char  *pref;      /* possible prefix */
                    206:     register char  *str;       /* string to check */
                    207: {
                    208:     while (*str && *pref == *str) {
                    209:        pref++;
                    210:        str++;
                    211:     }
                    212:
                    213:     return (*pref ? NULL : str);
                    214: }
                    215:
                    216: /*-
                    217:  *-----------------------------------------------------------------------
                    218:  * SuffSuffIsSuffix  --
                    219:  *     See if suff is a suffix of str. Str should point to THE END of the
                    220:  *     string to check. (THE END == the null byte)
                    221:  *
                    222:  * Results:
                    223:  *     NULL if it ain't, pointer to character in str before suffix if
                    224:  *     it is.
                    225:  *
                    226:  * Side Effects:
                    227:  *     None
                    228:  *-----------------------------------------------------------------------
                    229:  */
                    230: static char *
                    231: SuffSuffIsSuffix (s, str)
                    232:     register Suff  *s;         /* possible suffix */
                    233:     char           *str;       /* string to examine */
                    234: {
                    235:     register char  *p1;                /* Pointer into suffix name */
                    236:     register char  *p2;                /* Pointer into string being examined */
                    237:
                    238:     p1 = s->name + s->nameLen;
                    239:     p2 = str;
                    240:
                    241:     while (p1 >= s->name && *p1 == *p2) {
                    242:        p1--;
                    243:        p2--;
                    244:     }
                    245:
                    246:     return (p1 == s->name - 1 ? p2 : NULL);
                    247: }
                    248:
                    249: /*-
                    250:  *-----------------------------------------------------------------------
                    251:  * SuffSuffIsSuffixP --
                    252:  *     Predicate form of SuffSuffIsSuffix. Passed as the callback function
                    253:  *     to Lst_Find.
                    254:  *
                    255:  * Results:
                    256:  *     0 if the suffix is the one desired, non-zero if not.
                    257:  *
                    258:  * Side Effects:
                    259:  *     None.
                    260:  *
                    261:  *-----------------------------------------------------------------------
                    262:  */
                    263: static int
                    264: SuffSuffIsSuffixP(s, str)
                    265:     ClientData   s;
                    266:     ClientData   str;
                    267: {
                    268:     return(!SuffSuffIsSuffix((Suff *) s, (char *) str));
                    269: }
                    270:
                    271: /*-
                    272:  *-----------------------------------------------------------------------
                    273:  * SuffSuffHasNameP --
                    274:  *     Callback procedure for finding a suffix based on its name. Used by
                    275:  *     Suff_GetPath.
                    276:  *
                    277:  * Results:
                    278:  *     0 if the suffix is of the given name. non-zero otherwise.
                    279:  *
                    280:  * Side Effects:
                    281:  *     None
                    282:  *-----------------------------------------------------------------------
                    283:  */
                    284: static int
                    285: SuffSuffHasNameP (s, sname)
                    286:     ClientData    s;               /* Suffix to check */
                    287:     ClientData    sname;           /* Desired name */
                    288: {
                    289:     return (strcmp ((char *) sname, ((Suff *) s)->name));
                    290: }
                    291:
                    292: /*-
                    293:  *-----------------------------------------------------------------------
                    294:  * SuffSuffIsPrefix  --
                    295:  *     See if the suffix described by s is a prefix of the string. Care
                    296:  *     must be taken when using this to search for transformations and
                    297:  *     what-not, since there could well be two suffixes, one of which
                    298:  *     is a prefix of the other...
                    299:  *
                    300:  * Results:
                    301:  *     0 if s is a prefix of str. non-zero otherwise
                    302:  *
                    303:  * Side Effects:
                    304:  *     None
                    305:  *-----------------------------------------------------------------------
                    306:  */
                    307: static int
                    308: SuffSuffIsPrefix (s, str)
                    309:     ClientData   s;            /* suffix to compare */
                    310:     ClientData   str;  /* string to examine */
                    311: {
                    312:     return (SuffStrIsPrefix (((Suff *) s)->name, (char *) str) == NULL ? 1 : 0);
                    313: }
                    314:
                    315: /*-
                    316:  *-----------------------------------------------------------------------
                    317:  * SuffGNHasNameP  --
                    318:  *     See if the graph node has the desired name
                    319:  *
                    320:  * Results:
                    321:  *     0 if it does. non-zero if it doesn't
                    322:  *
                    323:  * Side Effects:
                    324:  *     None
                    325:  *-----------------------------------------------------------------------
                    326:  */
                    327: static int
                    328: SuffGNHasNameP (gn, name)
                    329:     ClientData      gn;                /* current node we're looking at */
                    330:     ClientData      name;      /* name we're looking for */
                    331: {
                    332:     return (strcmp ((char *) name, ((GNode *) gn)->name));
                    333: }
                    334:
                    335:            /*********** Maintenance Functions ************/
                    336:
                    337: static void
                    338: SuffUnRef(lp, sp)
                    339:     ClientData lp;
                    340:     ClientData sp;
                    341: {
                    342:     Lst l = (Lst) lp;
                    343:
                    344:     LstNode ln = Lst_Member(l, sp);
                    345:     if (ln != NILLNODE) {
                    346:        Lst_Remove(l, ln);
                    347:        ((Suff *) sp)->refCount--;
                    348:     }
                    349: }
                    350:
                    351: /*-
                    352:  *-----------------------------------------------------------------------
                    353:  * SuffFree  --
                    354:  *     Free up all memory associated with the given suffix structure.
                    355:  *
                    356:  * Results:
                    357:  *     none
                    358:  *
                    359:  * Side Effects:
                    360:  *     the suffix entry is detroyed
                    361:  *-----------------------------------------------------------------------
                    362:  */
                    363: static void
                    364: SuffFree (sp)
                    365:     ClientData sp;
                    366: {
                    367:     Suff           *s = (Suff *) sp;
                    368:
                    369:     if (s == suffNull)
                    370:        suffNull = NULL;
                    371:
                    372:     if (s == emptySuff)
                    373:        emptySuff = NULL;
                    374:
1.10      millert   375: #ifdef notdef
                    376:     /* We don't delete suffixes in order, so we cannot use this */
                    377:     if (s->refCount)
                    378:        Punt("Internal error deleting suffix `%s' with refcount = %d", s->name,
                    379:            s->refCount);
                    380: #endif
                    381:
1.1       deraadt   382:     Lst_Destroy (s->ref, NOFREE);
                    383:     Lst_Destroy (s->children, NOFREE);
                    384:     Lst_Destroy (s->parents, NOFREE);
                    385:     Lst_Destroy (s->searchPath, Dir_Destroy);
                    386:
                    387:     free ((Address)s->name);
                    388:     free ((Address)s);
                    389: }
                    390:
                    391: /*-
                    392:  *-----------------------------------------------------------------------
                    393:  * SuffRemove  --
                    394:  *     Remove the suffix into the list
                    395:  *
                    396:  * Results:
                    397:  *     None
                    398:  *
                    399:  * Side Effects:
                    400:  *     The reference count for the suffix is decremented and the
                    401:  *     suffix is possibly freed
                    402:  *-----------------------------------------------------------------------
                    403:  */
1.10      millert   404: static Suff *
1.1       deraadt   405: SuffRemove(l, s)
                    406:     Lst l;
                    407:     Suff *s;
                    408: {
                    409:     SuffUnRef((ClientData) l, (ClientData) s);
1.10      millert   410:     if (s->refCount == 0) {
                    411:        SuffUnRef ((ClientData) sufflist, (ClientData) s);
1.1       deraadt   412:        SuffFree((ClientData) s);
1.10      millert   413:        s = NULL;
                    414:     }
                    415:     return (s);
1.1       deraadt   416: }
                    417: 
                    418: /*-
                    419:  *-----------------------------------------------------------------------
                    420:  * SuffInsert  --
                    421:  *     Insert the suffix into the list keeping the list ordered by suffix
                    422:  *     numbers.
                    423:  *
                    424:  * Results:
                    425:  *     None
                    426:  *
                    427:  * Side Effects:
                    428:  *     The reference count of the suffix is incremented
                    429:  *-----------------------------------------------------------------------
                    430:  */
                    431: static void
                    432: SuffInsert (l, s)
                    433:     Lst           l;           /* the list where in s should be inserted */
                    434:     Suff          *s;          /* the suffix to insert */
                    435: {
                    436:     LstNode      ln;           /* current element in l we're examining */
                    437:     Suff          *s2 = NULL;  /* the suffix descriptor in this element */
                    438:
                    439:     if (Lst_Open (l) == FAILURE) {
                    440:        return;
                    441:     }
                    442:     while ((ln = Lst_Next (l)) != NILLNODE) {
                    443:        s2 = (Suff *) Lst_Datum (ln);
                    444:        if (s2->sNum >= s->sNum) {
                    445:            break;
                    446:        }
                    447:     }
                    448:
                    449:     Lst_Close (l);
                    450:     if (DEBUG(SUFF)) {
                    451:        printf("inserting %s(%d)...", s->name, s->sNum);
                    452:     }
                    453:     if (ln == NILLNODE) {
                    454:        if (DEBUG(SUFF)) {
                    455:            printf("at end of list\n");
                    456:        }
                    457:        (void)Lst_AtEnd (l, (ClientData)s);
                    458:        s->refCount++;
                    459:        (void)Lst_AtEnd(s->ref, (ClientData) l);
                    460:     } else if (s2->sNum != s->sNum) {
                    461:        if (DEBUG(SUFF)) {
                    462:            printf("before %s(%d)\n", s2->name, s2->sNum);
                    463:        }
                    464:        (void)Lst_Insert (l, ln, (ClientData)s);
                    465:        s->refCount++;
                    466:        (void)Lst_AtEnd(s->ref, (ClientData) l);
                    467:     } else if (DEBUG(SUFF)) {
                    468:        printf("already there\n");
                    469:     }
                    470: }
                    471:
                    472: /*-
                    473:  *-----------------------------------------------------------------------
                    474:  * Suff_ClearSuffixes --
                    475:  *     This is gross. Nuke the list of suffixes but keep all transformation
                    476:  *     rules around. The transformation graph is destroyed in this process,
                    477:  *     but we leave the list of rules so when a new graph is formed the rules
                    478:  *     will remain.
                    479:  *     This function is called from the parse module when a
                    480:  *     .SUFFIXES:\n line is encountered.
                    481:  *
                    482:  * Results:
                    483:  *     none
                    484:  *
                    485:  * Side Effects:
                    486:  *     the sufflist and its graph nodes are destroyed
                    487:  *-----------------------------------------------------------------------
                    488:  */
                    489: void
                    490: Suff_ClearSuffixes ()
                    491: {
1.14      espie     492: #ifdef CLEANUP
1.1       deraadt   493:     Lst_Concat (suffClean, sufflist, LST_CONCLINK);
1.14      espie     494: #endif
1.1       deraadt   495:     sufflist = Lst_Init(FALSE);
                    496:     sNum = 0;
                    497:     suffNull = emptySuff;
                    498: }
                    499:
                    500: /*-
                    501:  *-----------------------------------------------------------------------
                    502:  * SuffParseTransform --
                    503:  *     Parse a transformation string to find its two component suffixes.
                    504:  *
                    505:  * Results:
                    506:  *     TRUE if the string is a valid transformation and FALSE otherwise.
                    507:  *
                    508:  * Side Effects:
                    509:  *     The passed pointers are overwritten.
                    510:  *
                    511:  *-----------------------------------------------------------------------
                    512:  */
                    513: static Boolean
                    514: SuffParseTransform(str, srcPtr, targPtr)
                    515:     char               *str;           /* String being parsed */
                    516:     Suff               **srcPtr;       /* Place to store source of trans. */
                    517:     Suff               **targPtr;      /* Place to store target of trans. */
                    518: {
                    519:     register LstNode   srcLn;      /* element in suffix list of trans source*/
                    520:     register Suff      *src;       /* Source of transformation */
                    521:     register LstNode    targLn;            /* element in suffix list of trans target*/
                    522:     register char      *str2;      /* Extra pointer (maybe target suffix) */
                    523:     LstNode            singleLn;   /* element in suffix list of any suffix
                    524:                                     * that exactly matches str */
                    525:     Suff               *single = NULL;/* Source of possible transformation to
                    526:                                     * null suffix */
                    527:
                    528:     srcLn = NILLNODE;
                    529:     singleLn = NILLNODE;
1.5       millert   530:
1.1       deraadt   531:     /*
                    532:      * Loop looking first for a suffix that matches the start of the
                    533:      * string and then for one that exactly matches the rest of it. If
                    534:      * we can find two that meet these criteria, we've successfully
                    535:      * parsed the string.
                    536:      */
                    537:     for (;;) {
                    538:        if (srcLn == NILLNODE) {
                    539:            srcLn = Lst_Find(sufflist, (ClientData)str, SuffSuffIsPrefix);
                    540:        } else {
                    541:            srcLn = Lst_FindFrom (sufflist, Lst_Succ(srcLn), (ClientData)str,
                    542:                                  SuffSuffIsPrefix);
                    543:        }
                    544:        if (srcLn == NILLNODE) {
                    545:            /*
                    546:             * Ran out of source suffixes -- no such rule
                    547:             */
                    548:            if (singleLn != NILLNODE) {
                    549:                /*
                    550:                 * Not so fast Mr. Smith! There was a suffix that encompassed
                    551:                 * the entire string, so we assume it was a transformation
                    552:                 * to the null suffix (thank you POSIX). We still prefer to
                    553:                 * find a double rule over a singleton, hence we leave this
                    554:                 * check until the end.
                    555:                 *
                    556:                 * XXX: Use emptySuff over suffNull?
                    557:                 */
                    558:                *srcPtr = single;
                    559:                *targPtr = suffNull;
                    560:                return(TRUE);
                    561:            }
                    562:            return (FALSE);
                    563:        }
                    564:        src = (Suff *) Lst_Datum (srcLn);
                    565:        str2 = str + src->nameLen;
                    566:        if (*str2 == '\0') {
                    567:            single = src;
                    568:            singleLn = srcLn;
                    569:        } else {
                    570:            targLn = Lst_Find(sufflist, (ClientData)str2, SuffSuffHasNameP);
                    571:            if (targLn != NILLNODE) {
                    572:                *srcPtr = src;
                    573:                *targPtr = (Suff *)Lst_Datum(targLn);
                    574:                return (TRUE);
                    575:            }
                    576:        }
                    577:     }
                    578: }
                    579:
                    580: /*-
                    581:  *-----------------------------------------------------------------------
                    582:  * Suff_IsTransform  --
                    583:  *     Return TRUE if the given string is a transformation rule
                    584:  *
                    585:  *
                    586:  * Results:
                    587:  *     TRUE if the string is a concatenation of two known suffixes.
                    588:  *     FALSE otherwise
                    589:  *
                    590:  * Side Effects:
                    591:  *     None
                    592:  *-----------------------------------------------------------------------
                    593:  */
                    594: Boolean
                    595: Suff_IsTransform (str)
                    596:     char          *str;                /* string to check */
                    597: {
                    598:     Suff         *src, *targ;
                    599:
                    600:     return (SuffParseTransform(str, &src, &targ));
                    601: }
                    602:
                    603: /*-
                    604:  *-----------------------------------------------------------------------
                    605:  * Suff_AddTransform --
                    606:  *     Add the transformation rule described by the line to the
                    607:  *     list of rules and place the transformation itself in the graph
                    608:  *
                    609:  * Results:
                    610:  *     The node created for the transformation in the transforms list
                    611:  *
                    612:  * Side Effects:
                    613:  *     The node is placed on the end of the transforms Lst and links are
                    614:  *     made between the two suffixes mentioned in the target name
                    615:  *-----------------------------------------------------------------------
                    616:  */
                    617: GNode *
                    618: Suff_AddTransform (line)
                    619:     char          *line;       /* name of transformation to add */
                    620: {
                    621:     GNode         *gn;         /* GNode of transformation rule */
                    622:     Suff          *s,          /* source suffix */
                    623:                   *t;          /* target suffix */
                    624:     LstNode      ln;           /* Node for existing transformation */
                    625:
                    626:     ln = Lst_Find (transforms, (ClientData)line, SuffGNHasNameP);
                    627:     if (ln == NILLNODE) {
                    628:        /*
                    629:         * Make a new graph node for the transformation. It will be filled in
1.5       millert   630:         * by the Parse module.
1.1       deraadt   631:         */
                    632:        gn = Targ_NewGN (line);
                    633:        (void)Lst_AtEnd (transforms, (ClientData)gn);
                    634:     } else {
                    635:        /*
                    636:         * New specification for transformation rule. Just nuke the old list
                    637:         * of commands so they can be filled in again... We don't actually
                    638:         * free the commands themselves, because a given command can be
                    639:         * attached to several different transformations.
                    640:         */
                    641:        gn = (GNode *) Lst_Datum (ln);
                    642:        Lst_Destroy (gn->commands, NOFREE);
                    643:        Lst_Destroy (gn->children, NOFREE);
                    644:        gn->commands = Lst_Init (FALSE);
                    645:        gn->children = Lst_Init (FALSE);
                    646:     }
                    647:
                    648:     gn->type = OP_TRANSFORM;
                    649:
                    650:     (void)SuffParseTransform(line, &s, &t);
                    651:
                    652:     /*
1.5       millert   653:      * link the two together in the proper relationship and order
1.1       deraadt   654:      */
                    655:     if (DEBUG(SUFF)) {
                    656:        printf("defining transformation from `%s' to `%s'\n",
                    657:                s->name, t->name);
                    658:     }
                    659:     SuffInsert (t->children, s);
                    660:     SuffInsert (s->parents, t);
                    661:
                    662:     return (gn);
                    663: }
                    664:
                    665: /*-
                    666:  *-----------------------------------------------------------------------
                    667:  * Suff_EndTransform --
                    668:  *     Handle the finish of a transformation definition, removing the
                    669:  *     transformation from the graph if it has neither commands nor
                    670:  *     sources. This is a callback procedure for the Parse module via
                    671:  *     Lst_ForEach
                    672:  *
                    673:  * Results:
                    674:  *     === 0
                    675:  *
                    676:  * Side Effects:
                    677:  *     If the node has no commands or children, the children and parents
                    678:  *     lists of the affected suffices are altered.
                    679:  *
                    680:  *-----------------------------------------------------------------------
                    681:  */
                    682: int
                    683: Suff_EndTransform(gnp, dummy)
                    684:     ClientData   gnp;          /* Node for transformation */
                    685:     ClientData   dummy;        /* Node for transformation */
                    686: {
                    687:     GNode *gn = (GNode *) gnp;
1.5       millert   688:
1.1       deraadt   689:     if ((gn->type & OP_TRANSFORM) && Lst_IsEmpty(gn->commands) &&
                    690:        Lst_IsEmpty(gn->children))
                    691:     {
                    692:        Suff    *s, *t;
                    693:
                    694:        (void)SuffParseTransform(gn->name, &s, &t);
                    695:
                    696:        if (DEBUG(SUFF)) {
1.8       millert   697:            printf("deleting transformation from `%s' to `%s'\n",
1.1       deraadt   698:                    s->name, t->name);
                    699:        }
                    700:
                    701:        /*
                    702:         * Remove the source from the target's children list. We check for a
                    703:         * nil return to handle a beanhead saying something like
                    704:         *  .c.o .c.o:
                    705:         *
                    706:         * We'll be called twice when the next target is seen, but .c and .o
                    707:         * are only linked once...
                    708:         */
1.10      millert   709:        s = SuffRemove(t->children, s);
1.1       deraadt   710:
                    711:        /*
                    712:         * Remove the target from the source's parents list
                    713:         */
1.13      millert   714:        if (s != NULL)
                    715:            SuffRemove(s->parents, t);
1.1       deraadt   716:     } else if ((gn->type & OP_TRANSFORM) && DEBUG(SUFF)) {
                    717:        printf("transformation %s complete\n", gn->name);
                    718:     }
                    719:
                    720:     return(dummy ? 0 : 0);
                    721: }
                    722:
                    723: /*-
                    724:  *-----------------------------------------------------------------------
                    725:  * SuffRebuildGraph --
                    726:  *     Called from Suff_AddSuffix via Lst_ForEach to search through the
                    727:  *     list of existing transformation rules and rebuild the transformation
                    728:  *     graph when it has been destroyed by Suff_ClearSuffixes. If the
                    729:  *     given rule is a transformation involving this suffix and another,
                    730:  *     existing suffix, the proper relationship is established between
                    731:  *     the two.
                    732:  *
                    733:  * Results:
                    734:  *     Always 0.
                    735:  *
                    736:  * Side Effects:
                    737:  *     The appropriate links will be made between this suffix and
                    738:  *     others if transformation rules exist for it.
                    739:  *
                    740:  *-----------------------------------------------------------------------
                    741:  */
                    742: static int
                    743: SuffRebuildGraph(transformp, sp)
                    744:     ClientData  transformp; /* Transformation to test */
                    745:     ClientData  sp;        /* Suffix to rebuild */
                    746: {
                    747:     GNode      *transform = (GNode *) transformp;
                    748:     Suff       *s = (Suff *) sp;
                    749:     char       *cp;
                    750:     LstNode    ln;
                    751:     Suff       *s2;
                    752:
                    753:     /*
                    754:      * First see if it is a transformation from this suffix.
                    755:      */
                    756:     cp = SuffStrIsPrefix(s->name, transform->name);
                    757:     if (cp != (char *)NULL) {
                    758:        ln = Lst_Find(sufflist, (ClientData)cp, SuffSuffHasNameP);
                    759:        if (ln != NILLNODE) {
                    760:            /*
                    761:             * Found target. Link in and return, since it can't be anything
                    762:             * else.
                    763:             */
                    764:            s2 = (Suff *)Lst_Datum(ln);
                    765:            SuffInsert(s2->children, s);
                    766:            SuffInsert(s->parents, s2);
                    767:            return(0);
                    768:        }
                    769:     }
                    770:
                    771:     /*
                    772:      * Not from, maybe to?
                    773:      */
                    774:     cp = SuffSuffIsSuffix(s, transform->name + strlen(transform->name));
                    775:     if (cp != (char *)NULL) {
                    776:        /*
                    777:         * Null-terminate the source suffix in order to find it.
                    778:         */
                    779:        cp[1] = '\0';
                    780:        ln = Lst_Find(sufflist, (ClientData)transform->name, SuffSuffHasNameP);
                    781:        /*
                    782:         * Replace the start of the target suffix
                    783:         */
                    784:        cp[1] = s->name[0];
                    785:        if (ln != NILLNODE) {
                    786:            /*
                    787:             * Found it -- establish the proper relationship
                    788:             */
                    789:            s2 = (Suff *)Lst_Datum(ln);
                    790:            SuffInsert(s->children, s2);
                    791:            SuffInsert(s2->parents, s);
                    792:        }
                    793:     }
                    794:     return(0);
                    795: }
                    796:
                    797: /*-
                    798:  *-----------------------------------------------------------------------
                    799:  * Suff_AddSuffix --
                    800:  *     Add the suffix in string to the end of the list of known suffixes.
                    801:  *     Should we restructure the suffix graph? Make doesn't...
                    802:  *
                    803:  * Results:
                    804:  *     None
                    805:  *
                    806:  * Side Effects:
                    807:  *     A GNode is created for the suffix and a Suff structure is created and
                    808:  *     added to the suffixes list unless the suffix was already known.
                    809:  *-----------------------------------------------------------------------
                    810:  */
                    811: void
                    812: Suff_AddSuffix (str)
                    813:     char          *str;            /* the name of the suffix to add */
                    814: {
                    815:     Suff          *s;      /* new suffix descriptor */
                    816:     LstNode      ln;
                    817:
                    818:     ln = Lst_Find (sufflist, (ClientData)str, SuffSuffHasNameP);
                    819:     if (ln == NILLNODE) {
                    820:        s = (Suff *) emalloc (sizeof (Suff));
                    821:
1.4       briggs    822:        s->name =       estrdup (str);
1.1       deraadt   823:        s->nameLen =    strlen (s->name);
                    824:        s->searchPath = Lst_Init (FALSE);
                    825:        s->children =   Lst_Init (FALSE);
                    826:        s->parents =    Lst_Init (FALSE);
                    827:        s->ref =        Lst_Init (FALSE);
                    828:        s->sNum =       sNum++;
                    829:        s->flags =      0;
                    830:        s->refCount =   0;
                    831:
                    832:        (void)Lst_AtEnd (sufflist, (ClientData)s);
                    833:        /*
                    834:         * Look for any existing transformations from or to this suffix.
                    835:         * XXX: Only do this after a Suff_ClearSuffixes?
                    836:         */
                    837:        Lst_ForEach (transforms, SuffRebuildGraph, (ClientData)s);
1.5       millert   838:     }
1.1       deraadt   839: }
                    840:
                    841: /*-
                    842:  *-----------------------------------------------------------------------
                    843:  * Suff_GetPath --
                    844:  *     Return the search path for the given suffix, if it's defined.
                    845:  *
                    846:  * Results:
                    847:  *     The searchPath for the desired suffix or NILLST if the suffix isn't
                    848:  *     defined.
                    849:  *
                    850:  * Side Effects:
                    851:  *     None
                    852:  *-----------------------------------------------------------------------
                    853:  */
                    854: Lst
                    855: Suff_GetPath (sname)
                    856:     char         *sname;
                    857: {
                    858:     LstNode      ln;
                    859:     Suff         *s;
                    860:
                    861:     ln = Lst_Find (sufflist, (ClientData)sname, SuffSuffHasNameP);
                    862:     if (ln == NILLNODE) {
                    863:        return (NILLST);
                    864:     } else {
                    865:        s = (Suff *) Lst_Datum (ln);
                    866:        return (s->searchPath);
                    867:     }
                    868: }
                    869:
                    870: /*-
                    871:  *-----------------------------------------------------------------------
                    872:  * Suff_DoPaths --
                    873:  *     Extend the search paths for all suffixes to include the default
                    874:  *     search path.
                    875:  *
                    876:  * Results:
                    877:  *     None.
                    878:  *
                    879:  * Side Effects:
                    880:  *     The searchPath field of all the suffixes is extended by the
                    881:  *     directories in dirSearchPath. If paths were specified for the
                    882:  *     ".h" suffix, the directories are stuffed into a global variable
                    883:  *     called ".INCLUDES" with each directory preceeded by a -I. The same
                    884:  *     is done for the ".a" suffix, except the variable is called
                    885:  *     ".LIBS" and the flag is -L.
                    886:  *-----------------------------------------------------------------------
                    887:  */
                    888: void
                    889: Suff_DoPaths()
                    890: {
                    891:     register Suff      *s;
                    892:     register LstNode   ln;
                    893:     char               *ptr;
                    894:     Lst                        inIncludes; /* Cumulative .INCLUDES path */
                    895:     Lst                        inLibs;     /* Cumulative .LIBS path */
                    896:
                    897:     if (Lst_Open (sufflist) == FAILURE) {
                    898:        return;
                    899:     }
                    900:
                    901:     inIncludes = Lst_Init(FALSE);
                    902:     inLibs = Lst_Init(FALSE);
                    903:
                    904:     while ((ln = Lst_Next (sufflist)) != NILLNODE) {
                    905:        s = (Suff *) Lst_Datum (ln);
                    906:        if (!Lst_IsEmpty (s->searchPath)) {
                    907: #ifdef INCLUDES
                    908:            if (s->flags & SUFF_INCLUDE) {
                    909:                Dir_Concat(inIncludes, s->searchPath);
                    910:            }
                    911: #endif /* INCLUDES */
                    912: #ifdef LIBRARIES
                    913:            if (s->flags & SUFF_LIBRARY) {
                    914:                Dir_Concat(inLibs, s->searchPath);
                    915:            }
                    916: #endif /* LIBRARIES */
                    917:            Dir_Concat(s->searchPath, dirSearchPath);
                    918:        } else {
                    919:            Lst_Destroy (s->searchPath, Dir_Destroy);
                    920:            s->searchPath = Lst_Duplicate(dirSearchPath, Dir_CopyDir);
                    921:        }
                    922:     }
                    923:
                    924:     Var_Set(".INCLUDES", ptr = Dir_MakeFlags("-I", inIncludes), VAR_GLOBAL);
                    925:     free(ptr);
                    926:     Var_Set(".LIBS", ptr = Dir_MakeFlags("-L", inLibs), VAR_GLOBAL);
                    927:     free(ptr);
                    928:
                    929:     Lst_Destroy(inIncludes, Dir_Destroy);
                    930:     Lst_Destroy(inLibs, Dir_Destroy);
                    931:
                    932:     Lst_Close (sufflist);
                    933: }
                    934:
                    935: /*-
                    936:  *-----------------------------------------------------------------------
                    937:  * Suff_AddInclude --
                    938:  *     Add the given suffix as a type of file which gets included.
                    939:  *     Called from the parse module when a .INCLUDES line is parsed.
                    940:  *     The suffix must have already been defined.
                    941:  *
                    942:  * Results:
                    943:  *     None.
                    944:  *
                    945:  * Side Effects:
                    946:  *     The SUFF_INCLUDE bit is set in the suffix's flags field
                    947:  *
                    948:  *-----------------------------------------------------------------------
                    949:  */
                    950: void
                    951: Suff_AddInclude (sname)
                    952:     char         *sname;     /* Name of suffix to mark */
                    953: {
                    954:     LstNode      ln;
                    955:     Suff         *s;
                    956:
                    957:     ln = Lst_Find (sufflist, (ClientData)sname, SuffSuffHasNameP);
                    958:     if (ln != NILLNODE) {
                    959:        s = (Suff *) Lst_Datum (ln);
                    960:        s->flags |= SUFF_INCLUDE;
                    961:     }
                    962: }
                    963:
                    964: /*-
                    965:  *-----------------------------------------------------------------------
                    966:  * Suff_AddLib --
                    967:  *     Add the given suffix as a type of file which is a library.
                    968:  *     Called from the parse module when parsing a .LIBS line. The
                    969:  *     suffix must have been defined via .SUFFIXES before this is
                    970:  *     called.
                    971:  *
                    972:  * Results:
                    973:  *     None.
                    974:  *
                    975:  * Side Effects:
                    976:  *     The SUFF_LIBRARY bit is set in the suffix's flags field
                    977:  *
                    978:  *-----------------------------------------------------------------------
                    979:  */
                    980: void
                    981: Suff_AddLib (sname)
                    982:     char         *sname;     /* Name of suffix to mark */
                    983: {
                    984:     LstNode      ln;
                    985:     Suff         *s;
                    986:
                    987:     ln = Lst_Find (sufflist, (ClientData)sname, SuffSuffHasNameP);
                    988:     if (ln != NILLNODE) {
                    989:        s = (Suff *) Lst_Datum (ln);
                    990:        s->flags |= SUFF_LIBRARY;
                    991:     }
                    992: }
                    993:
                    994:          /********** Implicit Source Search Functions *********/
                    995:
                    996: /*-
                    997:  *-----------------------------------------------------------------------
                    998:  * SuffAddSrc  --
                    999:  *     Add a suffix as a Src structure to the given list with its parent
                   1000:  *     being the given Src structure. If the suffix is the null suffix,
                   1001:  *     the prefix is used unaltered as the file name in the Src structure.
                   1002:  *
                   1003:  * Results:
                   1004:  *     always returns 0
                   1005:  *
                   1006:  * Side Effects:
                   1007:  *     A Src structure is created and tacked onto the end of the list
                   1008:  *-----------------------------------------------------------------------
                   1009:  */
                   1010: static int
                   1011: SuffAddSrc (sp, lsp)
                   1012:     ClientData sp;         /* suffix for which to create a Src structure */
                   1013:     ClientData  lsp;       /* list and parent for the new Src */
                   1014: {
                   1015:     Suff       *s = (Suff *) sp;
                   1016:     LstSrc      *ls = (LstSrc *) lsp;
                   1017:     Src         *s2;       /* new Src structure */
                   1018:     Src        *targ;      /* Target structure */
                   1019:
                   1020:     targ = ls->s;
1.5       millert  1021:
1.1       deraadt  1022:     if ((s->flags & SUFF_NULL) && (*s->name != '\0')) {
                   1023:        /*
                   1024:         * If the suffix has been marked as the NULL suffix, also create a Src
                   1025:         * structure for a file with no suffix attached. Two birds, and all
                   1026:         * that...
                   1027:         */
                   1028:        s2 = (Src *) emalloc (sizeof (Src));
1.4       briggs   1029:        s2->file =      estrdup(targ->pref);
1.1       deraadt  1030:        s2->pref =      targ->pref;
                   1031:        s2->parent =    targ;
                   1032:        s2->node =      NILGNODE;
                   1033:        s2->suff =      s;
                   1034:        s->refCount++;
                   1035:        s2->children =  0;
                   1036:        targ->children += 1;
                   1037:        (void)Lst_AtEnd (ls->l, (ClientData)s2);
                   1038: #ifdef DEBUG_SRC
                   1039:        s2->cp = Lst_Init(FALSE);
                   1040:        Lst_AtEnd(targ->cp, (ClientData) s2);
                   1041:        printf("1 add %x %x to %x:", targ, s2, ls->l);
                   1042:        Lst_ForEach(ls->l, PrintAddr, (ClientData) 0);
                   1043:        printf("\n");
                   1044: #endif
                   1045:     }
                   1046:     s2 = (Src *) emalloc (sizeof (Src));
                   1047:     s2->file =             str_concat (targ->pref, s->name, 0);
                   1048:     s2->pref =     targ->pref;
                   1049:     s2->parent =    targ;
                   1050:     s2->node =             NILGNODE;
                   1051:     s2->suff =             s;
                   1052:     s->refCount++;
                   1053:     s2->children =  0;
                   1054:     targ->children += 1;
                   1055:     (void)Lst_AtEnd (ls->l, (ClientData)s2);
                   1056: #ifdef DEBUG_SRC
                   1057:     s2->cp = Lst_Init(FALSE);
                   1058:     Lst_AtEnd(targ->cp, (ClientData) s2);
                   1059:     printf("2 add %x %x to %x:", targ, s2, ls->l);
                   1060:     Lst_ForEach(ls->l, PrintAddr, (ClientData) 0);
                   1061:     printf("\n");
                   1062: #endif
                   1063:
                   1064:     return(0);
                   1065: }
                   1066:
                   1067: /*-
                   1068:  *-----------------------------------------------------------------------
                   1069:  * SuffAddLevel  --
                   1070:  *     Add all the children of targ as Src structures to the given list
                   1071:  *
                   1072:  * Results:
                   1073:  *     None
                   1074:  *
                   1075:  * Side Effects:
                   1076:  *     Lots of structures are created and added to the list
                   1077:  *-----------------------------------------------------------------------
                   1078:  */
                   1079: static void
                   1080: SuffAddLevel (l, targ)
                   1081:     Lst            l;          /* list to which to add the new level */
                   1082:     Src            *targ;      /* Src structure to use as the parent */
                   1083: {
                   1084:     LstSrc         ls;
                   1085:
                   1086:     ls.s = targ;
                   1087:     ls.l = l;
                   1088:
                   1089:     Lst_ForEach (targ->suff->children, SuffAddSrc, (ClientData)&ls);
                   1090: }
                   1091:
                   1092: /*-
                   1093:  *----------------------------------------------------------------------
                   1094:  * SuffRemoveSrc --
                   1095:  *     Free all src structures in list that don't have a reference count
                   1096:  *
                   1097:  * Results:
                   1098:  *     Ture if an src was removed
                   1099:  *
                   1100:  * Side Effects:
                   1101:  *     The memory is free'd.
                   1102:  *----------------------------------------------------------------------
                   1103:  */
                   1104: static int
                   1105: SuffRemoveSrc (l)
                   1106:     Lst l;
                   1107: {
                   1108:     LstNode ln;
                   1109:     Src *s;
                   1110:     int t = 0;
                   1111:
                   1112:     if (Lst_Open (l) == FAILURE) {
                   1113:        return 0;
                   1114:     }
                   1115: #ifdef DEBUG_SRC
                   1116:     printf("cleaning %lx: ", (unsigned long) l);
                   1117:     Lst_ForEach(l, PrintAddr, (ClientData) 0);
                   1118:     printf("\n");
                   1119: #endif
                   1120:
                   1121:
                   1122:     while ((ln = Lst_Next (l)) != NILLNODE) {
                   1123:        s = (Src *) Lst_Datum (ln);
                   1124:        if (s->children == 0) {
                   1125:            free ((Address)s->file);
                   1126:            if (!s->parent)
                   1127:                free((Address)s->pref);
                   1128:            else {
                   1129: #ifdef DEBUG_SRC
                   1130:                LstNode ln = Lst_Member(s->parent->cp, (ClientData)s);
                   1131:                if (ln != NILLNODE)
                   1132:                    Lst_Remove(s->parent->cp, ln);
                   1133: #endif
                   1134:                --s->parent->children;
                   1135:            }
                   1136: #ifdef DEBUG_SRC
                   1137:            printf("free: [l=%x] p=%x %d\n", l, s, s->children);
                   1138:            Lst_Destroy(s->cp, NOFREE);
                   1139: #endif
                   1140:            Lst_Remove(l, ln);
                   1141:            free ((Address)s);
                   1142:            t |= 1;
                   1143:            Lst_Close(l);
                   1144:            return TRUE;
                   1145:        }
                   1146: #ifdef DEBUG_SRC
                   1147:        else {
                   1148:            printf("keep: [l=%x] p=%x %d: ", l, s, s->children);
                   1149:            Lst_ForEach(s->cp, PrintAddr, (ClientData) 0);
                   1150:            printf("\n");
                   1151:        }
                   1152: #endif
                   1153:     }
                   1154:
                   1155:     Lst_Close(l);
                   1156:
                   1157:     return t;
                   1158: }
                   1159:
                   1160: /*-
                   1161:  *-----------------------------------------------------------------------
                   1162:  * SuffFindThem --
                   1163:  *     Find the first existing file/target in the list srcs
                   1164:  *
                   1165:  * Results:
                   1166:  *     The lowest structure in the chain of transformations
                   1167:  *
                   1168:  * Side Effects:
                   1169:  *     None
                   1170:  *-----------------------------------------------------------------------
                   1171:  */
                   1172: static Src *
                   1173: SuffFindThem (srcs, slst)
                   1174:     Lst            srcs;       /* list of Src structures to search through */
                   1175:     Lst                   slst;
                   1176: {
                   1177:     Src            *s;         /* current Src */
                   1178:     Src                   *rs;         /* returned Src */
                   1179:     char          *ptr;
                   1180:
                   1181:     rs = (Src *) NULL;
                   1182:
                   1183:     while (!Lst_IsEmpty (srcs)) {
                   1184:        s = (Src *) Lst_DeQueue (srcs);
                   1185:
                   1186:        if (DEBUG(SUFF)) {
                   1187:            printf ("\ttrying %s...", s->file);
                   1188:        }
                   1189:
                   1190:        /*
                   1191:         * A file is considered to exist if either a node exists in the
                   1192:         * graph for it or the file actually exists.
                   1193:         */
                   1194:        if (Targ_FindNode(s->file, TARG_NOCREATE) != NILGNODE) {
                   1195: #ifdef DEBUG_SRC
                   1196:            printf("remove %x from %x\n", s, srcs);
                   1197: #endif
                   1198:            rs = s;
                   1199:            break;
                   1200:        }
                   1201:
                   1202:        if ((ptr = Dir_FindFile (s->file, s->suff->searchPath)) != NULL) {
                   1203:            rs = s;
                   1204: #ifdef DEBUG_SRC
                   1205:            printf("remove %x from %x\n", s, srcs);
                   1206: #endif
                   1207:            free(ptr);
                   1208:            break;
                   1209:        }
                   1210:
                   1211:        if (DEBUG(SUFF)) {
                   1212:            printf ("not there\n");
                   1213:        }
                   1214:
                   1215:        SuffAddLevel (srcs, s);
                   1216:        Lst_AtEnd(slst, (ClientData) s);
                   1217:     }
                   1218:
                   1219:     if (DEBUG(SUFF) && rs) {
                   1220:        printf ("got it\n");
                   1221:     }
                   1222:     return (rs);
                   1223: }
                   1224:
                   1225: /*-
                   1226:  *-----------------------------------------------------------------------
                   1227:  * SuffFindCmds --
                   1228:  *     See if any of the children of the target in the Src structure is
                   1229:  *     one from which the target can be transformed. If there is one,
                   1230:  *     a Src structure is put together for it and returned.
                   1231:  *
                   1232:  * Results:
                   1233:  *     The Src structure of the "winning" child, or NIL if no such beast.
                   1234:  *
                   1235:  * Side Effects:
                   1236:  *     A Src structure may be allocated.
                   1237:  *
                   1238:  *-----------------------------------------------------------------------
                   1239:  */
                   1240: static Src *
                   1241: SuffFindCmds (targ, slst)
                   1242:     Src                *targ;  /* Src structure to play with */
                   1243:     Lst                slst;
                   1244: {
                   1245:     LstNode            ln;     /* General-purpose list node */
                   1246:     register GNode     *t,     /* Target GNode */
                   1247:                        *s;     /* Source GNode */
                   1248:     int                        prefLen;/* The length of the defined prefix */
                   1249:     Suff               *suff;  /* Suffix on matching beastie */
                   1250:     Src                        *ret;   /* Return value */
                   1251:     char               *cp;
                   1252:
                   1253:     t = targ->node;
                   1254:     (void) Lst_Open (t->children);
                   1255:     prefLen = strlen (targ->pref);
                   1256:
                   1257:     while ((ln = Lst_Next (t->children)) != NILLNODE) {
                   1258:        s = (GNode *)Lst_Datum (ln);
                   1259:
                   1260:        cp = strrchr (s->name, '/');
                   1261:        if (cp == (char *)NULL) {
                   1262:            cp = s->name;
                   1263:        } else {
                   1264:            cp++;
                   1265:        }
                   1266:        if (strncmp (cp, targ->pref, prefLen) == 0) {
                   1267:            /*
                   1268:             * The node matches the prefix ok, see if it has a known
                   1269:             * suffix.
                   1270:             */
                   1271:            ln = Lst_Find (sufflist, (ClientData)&cp[prefLen],
                   1272:                           SuffSuffHasNameP);
                   1273:            if (ln != NILLNODE) {
                   1274:                /*
                   1275:                 * It even has a known suffix, see if there's a transformation
                   1276:                 * defined between the node's suffix and the target's suffix.
                   1277:                 *
                   1278:                 * XXX: Handle multi-stage transformations here, too.
                   1279:                 */
                   1280:                suff = (Suff *)Lst_Datum (ln);
                   1281:
                   1282:                if (Lst_Member (suff->parents,
                   1283:                                (ClientData)targ->suff) != NILLNODE)
                   1284:                {
                   1285:                    /*
                   1286:                     * Hot Damn! Create a new Src structure to describe
                   1287:                     * this transformation (making sure to duplicate the
                   1288:                     * source node's name so Suff_FindDeps can free it
                   1289:                     * again (ick)), and return the new structure.
                   1290:                     */
                   1291:                    ret = (Src *)emalloc (sizeof (Src));
1.4       briggs   1292:                    ret->file = estrdup(s->name);
1.1       deraadt  1293:                    ret->pref = targ->pref;
                   1294:                    ret->suff = suff;
                   1295:                    suff->refCount++;
                   1296:                    ret->parent = targ;
                   1297:                    ret->node = s;
                   1298:                    ret->children = 0;
                   1299:                    targ->children += 1;
                   1300: #ifdef DEBUG_SRC
                   1301:                    ret->cp = Lst_Init(FALSE);
                   1302:                    printf("3 add %x %x\n", targ, ret);
                   1303:                    Lst_AtEnd(targ->cp, (ClientData) ret);
                   1304: #endif
                   1305:                    Lst_AtEnd(slst, (ClientData) ret);
                   1306:                    if (DEBUG(SUFF)) {
                   1307:                        printf ("\tusing existing source %s\n", s->name);
                   1308:                    }
                   1309:                    return (ret);
                   1310:                }
                   1311:            }
                   1312:        }
                   1313:     }
                   1314:     Lst_Close (t->children);
                   1315:     return ((Src *)NULL);
                   1316: }
                   1317:
                   1318: /*-
                   1319:  *-----------------------------------------------------------------------
                   1320:  * SuffExpandChildren --
                   1321:  *     Expand the names of any children of a given node that contain
                   1322:  *     variable invocations or file wildcards into actual targets.
                   1323:  *
                   1324:  * Results:
                   1325:  *     === 0 (continue)
                   1326:  *
                   1327:  * Side Effects:
                   1328:  *     The expanded node is removed from the parent's list of children,
                   1329:  *     and the parent's unmade counter is decremented, but other nodes
                   1330:  *     may be added.
                   1331:  *
                   1332:  *-----------------------------------------------------------------------
                   1333:  */
                   1334: static int
                   1335: SuffExpandChildren(cgnp, pgnp)
                   1336:     ClientData  cgnp;      /* Child to examine */
                   1337:     ClientData  pgnp;      /* Parent node being processed */
                   1338: {
                   1339:     GNode      *cgn = (GNode *) cgnp;
                   1340:     GNode      *pgn = (GNode *) pgnp;
                   1341:     GNode      *gn;        /* New source 8) */
                   1342:     LstNode    prevLN;    /* Node after which new source should be put */
                   1343:     LstNode    ln;         /* List element for old source */
                   1344:     char       *cp;        /* Expanded value */
                   1345:
                   1346:     /*
                   1347:      * New nodes effectively take the place of the child, so place them
                   1348:      * after the child
                   1349:      */
                   1350:     prevLN = Lst_Member(pgn->children, (ClientData)cgn);
1.5       millert  1351:
1.1       deraadt  1352:     /*
                   1353:      * First do variable expansion -- this takes precedence over
                   1354:      * wildcard expansion. If the result contains wildcards, they'll be gotten
                   1355:      * to later since the resulting words are tacked on to the end of
                   1356:      * the children list.
                   1357:      */
                   1358:     if (strchr(cgn->name, '$') != (char *)NULL) {
                   1359:        if (DEBUG(SUFF)) {
                   1360:            printf("Expanding \"%s\"...", cgn->name);
                   1361:        }
1.16    ! espie    1362:        cp = Var_Subst(cgn->name, pgn, TRUE);
1.1       deraadt  1363:
                   1364:        if (cp != (char *)NULL) {
                   1365:            Lst     members = Lst_Init(FALSE);
1.5       millert  1366:
1.1       deraadt  1367:            if (cgn->type & OP_ARCHV) {
                   1368:                /*
                   1369:                 * Node was an archive(member) target, so we want to call
                   1370:                 * on the Arch module to find the nodes for us, expanding
                   1371:                 * variables in the parent's context.
                   1372:                 */
                   1373:                char    *sacrifice = cp;
                   1374:
                   1375:                (void)Arch_ParseArchive(&sacrifice, members, pgn);
                   1376:            } else {
                   1377:                /*
                   1378:                 * Break the result into a vector of strings whose nodes
                   1379:                 * we can find, then add those nodes to the members list.
                   1380:                 * Unfortunately, we can't use brk_string b/c it
                   1381:                 * doesn't understand about variable specifications with
                   1382:                 * spaces in them...
                   1383:                 */
                   1384:                char        *start;
                   1385:                char        *initcp = cp;   /* For freeing... */
                   1386:
1.5       millert  1387:                for (start = cp; *start == ' ' || *start == '\t'; start++)
1.1       deraadt  1388:                    continue;
                   1389:                for (cp = start; *cp != '\0'; cp++) {
                   1390:                    if (*cp == ' ' || *cp == '\t') {
                   1391:                        /*
                   1392:                         * White-space -- terminate element, find the node,
                   1393:                         * add it, skip any further spaces.
                   1394:                         */
                   1395:                        *cp++ = '\0';
                   1396:                        gn = Targ_FindNode(start, TARG_CREATE);
                   1397:                        (void)Lst_AtEnd(members, (ClientData)gn);
                   1398:                        while (*cp == ' ' || *cp == '\t') {
                   1399:                            cp++;
                   1400:                        }
                   1401:                        /*
                   1402:                         * Adjust cp for increment at start of loop, but
                   1403:                         * set start to first non-space.
                   1404:                         */
                   1405:                        start = cp--;
                   1406:                    } else if (*cp == '$') {
                   1407:                        /*
                   1408:                         * Start of a variable spec -- contact variable module
                   1409:                         * to find the end so we can skip over it.
                   1410:                         */
                   1411:                        char    *junk;
                   1412:                        int     len;
                   1413:                        Boolean doFree;
                   1414:
                   1415:                        junk = Var_Parse(cp, pgn, TRUE, &len, &doFree);
                   1416:                        if (junk != var_Error) {
                   1417:                            cp += len - 1;
                   1418:                        }
                   1419:
                   1420:                        if (doFree) {
                   1421:                            free(junk);
                   1422:                        }
                   1423:                    } else if (*cp == '\\' && *cp != '\0') {
                   1424:                        /*
                   1425:                         * Escaped something -- skip over it
                   1426:                         */
                   1427:                        cp++;
                   1428:                    }
                   1429:                }
                   1430:
                   1431:                if (cp != start) {
                   1432:                    /*
                   1433:                     * Stuff left over -- add it to the list too
                   1434:                     */
                   1435:                    gn = Targ_FindNode(start, TARG_CREATE);
                   1436:                    (void)Lst_AtEnd(members, (ClientData)gn);
                   1437:                }
                   1438:                /*
                   1439:                 * Point cp back at the beginning again so the variable value
                   1440:                 * can be freed.
                   1441:                 */
                   1442:                cp = initcp;
                   1443:            }
                   1444:            /*
                   1445:             * Add all elements of the members list to the parent node.
                   1446:             */
1.2       deraadt  1447:            while(!Lst_IsEmpty(members)) {
1.1       deraadt  1448:                gn = (GNode *)Lst_DeQueue(members);
                   1449:
                   1450:                if (DEBUG(SUFF)) {
                   1451:                    printf("%s...", gn->name);
                   1452:                }
                   1453:                if (Lst_Member(pgn->children, (ClientData)gn) == NILLNODE) {
                   1454:                    (void)Lst_Append(pgn->children, prevLN, (ClientData)gn);
                   1455:                    prevLN = Lst_Succ(prevLN);
                   1456:                    (void)Lst_AtEnd(gn->parents, (ClientData)pgn);
                   1457:                    pgn->unmade++;
                   1458:                }
                   1459:            }
                   1460:            Lst_Destroy(members, NOFREE);
                   1461:            /*
                   1462:             * Free the result
                   1463:             */
                   1464:            free((char *)cp);
                   1465:        }
                   1466:        /*
                   1467:         * Now the source is expanded, remove it from the list of children to
                   1468:         * keep it from being processed.
                   1469:         */
                   1470:        ln = Lst_Member(pgn->children, (ClientData)cgn);
                   1471:        pgn->unmade--;
                   1472:        Lst_Remove(pgn->children, ln);
                   1473:        if (DEBUG(SUFF)) {
                   1474:            printf("\n");
                   1475:        }
                   1476:     } else if (Dir_HasWildcards(cgn->name)) {
                   1477:        Lst     exp;        /* List of expansions */
                   1478:        Lst     path;       /* Search path along which to expand */
                   1479:
                   1480:        /*
                   1481:         * Find a path along which to expand the word.
                   1482:         *
                   1483:         * If the word has a known suffix, use that path.
                   1484:         * If it has no known suffix and we're allowed to use the null
                   1485:         *   suffix, use its path.
                   1486:         * Else use the default system search path.
                   1487:         */
                   1488:        cp = cgn->name + strlen(cgn->name);
                   1489:        ln = Lst_Find(sufflist, (ClientData)cp, SuffSuffIsSuffixP);
                   1490:
                   1491:        if (DEBUG(SUFF)) {
                   1492:            printf("Wildcard expanding \"%s\"...", cgn->name);
                   1493:        }
1.5       millert  1494:
1.1       deraadt  1495:        if (ln != NILLNODE) {
                   1496:            Suff    *s = (Suff *)Lst_Datum(ln);
                   1497:
                   1498:            if (DEBUG(SUFF)) {
                   1499:                printf("suffix is \"%s\"...", s->name);
                   1500:            }
                   1501:            path = s->searchPath;
                   1502:        } else {
                   1503:            /*
                   1504:             * Use default search path
                   1505:             */
                   1506:            path = dirSearchPath;
                   1507:        }
                   1508:
                   1509:        /*
                   1510:         * Expand the word along the chosen path
                   1511:         */
                   1512:        exp = Lst_Init(FALSE);
                   1513:        Dir_Expand(cgn->name, path, exp);
                   1514:
                   1515:        while (!Lst_IsEmpty(exp)) {
                   1516:            /*
                   1517:             * Fetch next expansion off the list and find its GNode
                   1518:             */
                   1519:            cp = (char *)Lst_DeQueue(exp);
                   1520:
                   1521:            if (DEBUG(SUFF)) {
                   1522:                printf("%s...", cp);
                   1523:            }
                   1524:            gn = Targ_FindNode(cp, TARG_CREATE);
                   1525:
                   1526:            /*
                   1527:             * If gn isn't already a child of the parent, make it so and
                   1528:             * up the parent's count of unmade children.
                   1529:             */
                   1530:            if (Lst_Member(pgn->children, (ClientData)gn) == NILLNODE) {
                   1531:                (void)Lst_Append(pgn->children, prevLN, (ClientData)gn);
                   1532:                prevLN = Lst_Succ(prevLN);
                   1533:                (void)Lst_AtEnd(gn->parents, (ClientData)pgn);
                   1534:                pgn->unmade++;
                   1535:            }
                   1536:        }
                   1537:
                   1538:        /*
                   1539:         * Nuke what's left of the list
                   1540:         */
                   1541:        Lst_Destroy(exp, NOFREE);
1.5       millert  1542:
1.1       deraadt  1543:        /*
                   1544:         * Now the source is expanded, remove it from the list of children to
                   1545:         * keep it from being processed.
                   1546:         */
                   1547:        ln = Lst_Member(pgn->children, (ClientData)cgn);
                   1548:        pgn->unmade--;
                   1549:        Lst_Remove(pgn->children, ln);
                   1550:        if (DEBUG(SUFF)) {
                   1551:            printf("\n");
                   1552:        }
                   1553:     }
                   1554:
                   1555:     return(0);
                   1556: }
                   1557:
                   1558: /*-
                   1559:  *-----------------------------------------------------------------------
                   1560:  * SuffApplyTransform --
                   1561:  *     Apply a transformation rule, given the source and target nodes
                   1562:  *     and suffixes.
                   1563:  *
                   1564:  * Results:
                   1565:  *     TRUE if successful, FALSE if not.
                   1566:  *
                   1567:  * Side Effects:
                   1568:  *     The source and target are linked and the commands from the
                   1569:  *     transformation are added to the target node's commands list.
                   1570:  *     All attributes but OP_DEPMASK and OP_TRANSFORM are applied
                   1571:  *     to the target. The target also inherits all the sources for
                   1572:  *     the transformation rule.
                   1573:  *
                   1574:  *-----------------------------------------------------------------------
                   1575:  */
                   1576: static Boolean
                   1577: SuffApplyTransform(tGn, sGn, t, s)
                   1578:     GNode      *tGn;       /* Target node */
                   1579:     GNode      *sGn;       /* Source node */
                   1580:     Suff       *t;         /* Target suffix */
                   1581:     Suff       *s;         /* Source suffix */
                   1582: {
                   1583:     LstNode    ln;         /* General node */
                   1584:     char       *tname;     /* Name of transformation rule */
                   1585:     GNode      *gn;        /* Node for same */
                   1586:
                   1587:     if (Lst_Member(tGn->children, (ClientData)sGn) == NILLNODE) {
                   1588:        /*
                   1589:         * Not already linked, so form the proper links between the
                   1590:         * target and source.
                   1591:         */
                   1592:        (void)Lst_AtEnd(tGn->children, (ClientData)sGn);
                   1593:        (void)Lst_AtEnd(sGn->parents, (ClientData)tGn);
                   1594:        tGn->unmade += 1;
                   1595:     }
                   1596:
                   1597:     if ((sGn->type & OP_OPMASK) == OP_DOUBLEDEP) {
                   1598:        /*
                   1599:         * When a :: node is used as the implied source of a node, we have
                   1600:         * to link all its cohorts in as sources as well. Only the initial
                   1601:         * sGn gets the target in its iParents list, however, as that
                   1602:         * will be sufficient to get the .IMPSRC variable set for tGn
                   1603:         */
                   1604:        for (ln=Lst_First(sGn->cohorts); ln != NILLNODE; ln=Lst_Succ(ln)) {
                   1605:            gn = (GNode *)Lst_Datum(ln);
                   1606:
                   1607:            if (Lst_Member(tGn->children, (ClientData)gn) == NILLNODE) {
                   1608:                /*
                   1609:                 * Not already linked, so form the proper links between the
                   1610:                 * target and source.
                   1611:                 */
                   1612:                (void)Lst_AtEnd(tGn->children, (ClientData)gn);
                   1613:                (void)Lst_AtEnd(gn->parents, (ClientData)tGn);
                   1614:                tGn->unmade += 1;
                   1615:            }
                   1616:        }
                   1617:     }
                   1618:     /*
                   1619:      * Locate the transformation rule itself
                   1620:      */
                   1621:     tname = str_concat(s->name, t->name, 0);
                   1622:     ln = Lst_Find(transforms, (ClientData)tname, SuffGNHasNameP);
                   1623:     free(tname);
                   1624:
                   1625:     if (ln == NILLNODE) {
                   1626:        /*
                   1627:         * Not really such a transformation rule (can happen when we're
                   1628:         * called to link an OP_MEMBER and OP_ARCHV node), so return
                   1629:         * FALSE.
                   1630:         */
                   1631:        return(FALSE);
                   1632:     }
                   1633:
                   1634:     gn = (GNode *)Lst_Datum(ln);
1.5       millert  1635:
1.1       deraadt  1636:     if (DEBUG(SUFF)) {
                   1637:        printf("\tapplying %s -> %s to \"%s\"\n", s->name, t->name, tGn->name);
                   1638:     }
                   1639:
                   1640:     /*
                   1641:      * Record last child for expansion purposes
                   1642:      */
                   1643:     ln = Lst_Last(tGn->children);
1.5       millert  1644:
1.1       deraadt  1645:     /*
                   1646:      * Pass the buck to Make_HandleUse to apply the rule
                   1647:      */
                   1648:     (void)Make_HandleUse(gn, tGn);
                   1649:
                   1650:     /*
                   1651:      * Deal with wildcards and variables in any acquired sources
                   1652:      */
                   1653:     ln = Lst_Succ(ln);
                   1654:     if (ln != NILLNODE) {
                   1655:        Lst_ForEachFrom(tGn->children, ln,
                   1656:                        SuffExpandChildren, (ClientData)tGn);
                   1657:     }
                   1658:
                   1659:     /*
                   1660:      * Keep track of another parent to which this beast is transformed so
                   1661:      * the .IMPSRC variable can be set correctly for the parent.
                   1662:      */
                   1663:     (void)Lst_AtEnd(sGn->iParents, (ClientData)tGn);
                   1664:
                   1665:     return(TRUE);
                   1666: }
                   1667:
                   1668:
                   1669: /*-
                   1670:  *-----------------------------------------------------------------------
                   1671:  * SuffFindArchiveDeps --
                   1672:  *     Locate dependencies for an OP_ARCHV node.
                   1673:  *
                   1674:  * Results:
                   1675:  *     None
                   1676:  *
                   1677:  * Side Effects:
                   1678:  *     Same as Suff_FindDeps
                   1679:  *
                   1680:  *-----------------------------------------------------------------------
                   1681:  */
                   1682: static void
                   1683: SuffFindArchiveDeps(gn, slst)
                   1684:     GNode      *gn;        /* Node for which to locate dependencies */
                   1685:     Lst                slst;
                   1686: {
                   1687:     char       *eoarch;    /* End of archive portion */
                   1688:     char       *eoname;    /* End of member portion */
                   1689:     GNode      *mem;       /* Node for member */
                   1690:     static char        *copy[] = { /* Variables to be copied from the member node */
                   1691:        TARGET,             /* Must be first */
                   1692:        PREFIX,             /* Must be second */
                   1693:     };
                   1694:     int                i;          /* Index into copy and vals */
                   1695:     Suff       *ms;        /* Suffix descriptor for member */
                   1696:     char       *name;      /* Start of member's name */
1.5       millert  1697:
1.1       deraadt  1698:     /*
                   1699:      * The node is an archive(member) pair. so we must find a
                   1700:      * suffix for both of them.
                   1701:      */
                   1702:     eoarch = strchr (gn->name, '(');
                   1703:     eoname = strchr (eoarch, ')');
1.6       millert  1704:     if (eoarch == NULL || eoname == NULL)
                   1705:        return;
1.1       deraadt  1706:
                   1707:     *eoname = '\0';      /* Nuke parentheses during suffix search */
                   1708:     *eoarch = '\0';      /* So a suffix can be found */
                   1709:
                   1710:     name = eoarch + 1;
1.5       millert  1711:
1.1       deraadt  1712:     /*
                   1713:      * To simplify things, call Suff_FindDeps recursively on the member now,
                   1714:      * so we can simply compare the member's .PREFIX and .TARGET variables
                   1715:      * to locate its suffix. This allows us to figure out the suffix to
                   1716:      * use for the archive without having to do a quadratic search over the
                   1717:      * suffix list, backtracking for each one...
                   1718:      */
                   1719:     mem = Targ_FindNode(name, TARG_CREATE);
                   1720:     SuffFindDeps(mem, slst);
                   1721:
                   1722:     /*
                   1723:      * Create the link between the two nodes right off
                   1724:      */
                   1725:     if (Lst_Member(gn->children, (ClientData)mem) == NILLNODE) {
                   1726:        (void)Lst_AtEnd(gn->children, (ClientData)mem);
                   1727:        (void)Lst_AtEnd(mem->parents, (ClientData)gn);
                   1728:        gn->unmade += 1;
                   1729:     }
1.5       millert  1730:
1.1       deraadt  1731:     /*
                   1732:      * Copy in the variables from the member node to this one.
                   1733:      */
                   1734:     for (i = (sizeof(copy)/sizeof(copy[0]))-1; i >= 0; i--) {
1.15      espie    1735:        Var_Set(copy[i], Var_Value(copy[i], mem), gn);
1.1       deraadt  1736:
                   1737:     }
                   1738:
                   1739:     ms = mem->suffix;
                   1740:     if (ms == NULL) {
                   1741:        /*
                   1742:         * Didn't know what it was -- use .NULL suffix if not in make mode
                   1743:         */
                   1744:        if (DEBUG(SUFF)) {
                   1745:            printf("using null suffix\n");
                   1746:        }
                   1747:        ms = suffNull;
                   1748:     }
                   1749:
                   1750:
                   1751:     /*
                   1752:      * Set the other two local variables required for this target.
                   1753:      */
                   1754:     Var_Set (MEMBER, name, gn);
                   1755:     Var_Set (ARCHIVE, gn->name, gn);
                   1756:
                   1757:     if (ms != NULL) {
                   1758:        /*
                   1759:         * Member has a known suffix, so look for a transformation rule from
                   1760:         * it to a possible suffix of the archive. Rather than searching
                   1761:         * through the entire list, we just look at suffixes to which the
                   1762:         * member's suffix may be transformed...
                   1763:         */
                   1764:        LstNode     ln;
                   1765:
                   1766:        /*
                   1767:         * Use first matching suffix...
                   1768:         */
                   1769:        ln = Lst_Find(ms->parents, eoarch, SuffSuffIsSuffixP);
                   1770:
                   1771:        if (ln != NILLNODE) {
                   1772:            /*
                   1773:             * Got one -- apply it
                   1774:             */
                   1775:            if (!SuffApplyTransform(gn, mem, (Suff *)Lst_Datum(ln), ms) &&
                   1776:                DEBUG(SUFF))
                   1777:            {
                   1778:                printf("\tNo transformation from %s -> %s\n",
                   1779:                       ms->name, ((Suff *)Lst_Datum(ln))->name);
                   1780:            }
                   1781:        }
                   1782:     }
                   1783:
                   1784:     /*
                   1785:      * Replace the opening and closing parens now we've no need of the separate
                   1786:      * pieces.
                   1787:      */
                   1788:     *eoarch = '('; *eoname = ')';
                   1789:
                   1790:     /*
                   1791:      * Pretend gn appeared to the left of a dependency operator so
                   1792:      * the user needn't provide a transformation from the member to the
                   1793:      * archive.
                   1794:      */
                   1795:     if (OP_NOP(gn->type)) {
                   1796:        gn->type |= OP_DEPENDS;
                   1797:     }
                   1798:
                   1799:     /*
                   1800:      * Flag the member as such so we remember to look in the archive for
                   1801:      * its modification time.
                   1802:      */
                   1803:     mem->type |= OP_MEMBER;
                   1804: }
                   1805:
                   1806: /*-
                   1807:  *-----------------------------------------------------------------------
                   1808:  * SuffFindNormalDeps --
                   1809:  *     Locate implicit dependencies for regular targets.
                   1810:  *
                   1811:  * Results:
                   1812:  *     None.
                   1813:  *
                   1814:  * Side Effects:
                   1815:  *     Same as Suff_FindDeps...
                   1816:  *
                   1817:  *-----------------------------------------------------------------------
                   1818:  */
                   1819: static void
                   1820: SuffFindNormalDeps(gn, slst)
                   1821:     GNode      *gn;        /* Node for which to find sources */
                   1822:     Lst                slst;
                   1823: {
                   1824:     char       *eoname;    /* End of name */
                   1825:     char       *sopref;    /* Start of prefix */
                   1826:     LstNode    ln;         /* Next suffix node to check */
                   1827:     Lst                srcs;       /* List of sources at which to look */
                   1828:     Lst                targs;      /* List of targets to which things can be
                   1829:                             * transformed. They all have the same file,
                   1830:                             * but different suff and pref fields */
                   1831:     Src                *bottom;    /* Start of found transformation path */
                   1832:     Src        *src;       /* General Src pointer */
                   1833:     char       *pref;      /* Prefix to use */
                   1834:     Src                *targ;      /* General Src target pointer */
                   1835:
                   1836:
                   1837:     eoname = gn->name + strlen(gn->name);
                   1838:
                   1839:     sopref = gn->name;
1.5       millert  1840:
1.1       deraadt  1841:     /*
                   1842:      * Begin at the beginning...
                   1843:      */
                   1844:     ln = Lst_First(sufflist);
                   1845:     srcs = Lst_Init(FALSE);
                   1846:     targs = Lst_Init(FALSE);
                   1847:
                   1848:     /*
                   1849:      * We're caught in a catch-22 here. On the one hand, we want to use any
                   1850:      * transformation implied by the target's sources, but we can't examine
                   1851:      * the sources until we've expanded any variables/wildcards they may hold,
                   1852:      * and we can't do that until we've set up the target's local variables
                   1853:      * and we can't do that until we know what the proper suffix for the
                   1854:      * target is (in case there are two suffixes one of which is a suffix of
                   1855:      * the other) and we can't know that until we've found its implied
                   1856:      * source, which we may not want to use if there's an existing source
                   1857:      * that implies a different transformation.
                   1858:      *
                   1859:      * In an attempt to get around this, which may not work all the time,
                   1860:      * but should work most of the time, we look for implied sources first,
                   1861:      * checking transformations to all possible suffixes of the target,
                   1862:      * use what we find to set the target's local variables, expand the
                   1863:      * children, then look for any overriding transformations they imply.
                   1864:      * Should we find one, we discard the one we found before.
                   1865:      */
                   1866:
                   1867:     while (ln != NILLNODE) {
                   1868:        /*
                   1869:         * Look for next possible suffix...
                   1870:         */
                   1871:        ln = Lst_FindFrom(sufflist, ln, eoname, SuffSuffIsSuffixP);
                   1872:
                   1873:        if (ln != NILLNODE) {
                   1874:            int     prefLen;        /* Length of the prefix */
                   1875:            Src     *targ;
1.5       millert  1876:
1.1       deraadt  1877:            /*
                   1878:             * Allocate a Src structure to which things can be transformed
                   1879:             */
                   1880:            targ = (Src *)emalloc(sizeof (Src));
1.4       briggs   1881:            targ->file = estrdup(gn->name);
1.1       deraadt  1882:            targ->suff = (Suff *)Lst_Datum(ln);
                   1883:            targ->suff->refCount++;
                   1884:            targ->node = gn;
                   1885:            targ->parent = (Src *)NULL;
                   1886:            targ->children = 0;
                   1887: #ifdef DEBUG_SRC
                   1888:            targ->cp = Lst_Init(FALSE);
                   1889: #endif
1.5       millert  1890:
1.1       deraadt  1891:            /*
                   1892:             * Allocate room for the prefix, whose end is found by subtracting
                   1893:             * the length of the suffix from the end of the name.
                   1894:             */
                   1895:            prefLen = (eoname - targ->suff->nameLen) - sopref;
                   1896:            targ->pref = emalloc(prefLen + 1);
                   1897:            memcpy(targ->pref, sopref, prefLen);
                   1898:            targ->pref[prefLen] = '\0';
                   1899:
                   1900:            /*
                   1901:             * Add nodes from which the target can be made
                   1902:             */
                   1903:            SuffAddLevel(srcs, targ);
                   1904:
                   1905:            /*
                   1906:             * Record the target so we can nuke it
                   1907:             */
                   1908:            (void)Lst_AtEnd(targs, (ClientData)targ);
                   1909:
                   1910:            /*
                   1911:             * Search from this suffix's successor...
                   1912:             */
                   1913:            ln = Lst_Succ(ln);
                   1914:        }
                   1915:     }
                   1916:
                   1917:     /*
                   1918:      * Handle target of unknown suffix...
                   1919:      */
                   1920:     if (Lst_IsEmpty(targs) && suffNull != NULL) {
                   1921:        if (DEBUG(SUFF)) {
1.5       millert  1922:            printf("\tNo known suffix on %s. Using .NULL suffix\n", gn->name);
1.1       deraadt  1923:        }
1.5       millert  1924:
1.1       deraadt  1925:        targ = (Src *)emalloc(sizeof (Src));
1.4       briggs   1926:        targ->file = estrdup(gn->name);
1.1       deraadt  1927:        targ->suff = suffNull;
                   1928:        targ->suff->refCount++;
                   1929:        targ->node = gn;
                   1930:        targ->parent = (Src *)NULL;
                   1931:        targ->children = 0;
1.4       briggs   1932:        targ->pref = estrdup(sopref);
1.1       deraadt  1933: #ifdef DEBUG_SRC
                   1934:        targ->cp = Lst_Init(FALSE);
                   1935: #endif
                   1936:
                   1937:        /*
                   1938:         * Only use the default suffix rules if we don't have commands
                   1939:         * or dependencies defined for this gnode
                   1940:         */
                   1941:        if (Lst_IsEmpty(gn->commands) && Lst_IsEmpty(gn->children))
                   1942:            SuffAddLevel(srcs, targ);
                   1943:        else {
1.5       millert  1944:            if (DEBUG(SUFF))
1.1       deraadt  1945:                printf("not ");
                   1946:        }
                   1947:
1.5       millert  1948:        if (DEBUG(SUFF))
1.1       deraadt  1949:            printf("adding suffix rules\n");
                   1950:
                   1951:        (void)Lst_AtEnd(targs, (ClientData)targ);
                   1952:     }
1.5       millert  1953:
1.1       deraadt  1954:     /*
                   1955:      * Using the list of possible sources built up from the target suffix(es),
                   1956:      * try and find an existing file/target that matches.
                   1957:      */
                   1958:     bottom = SuffFindThem(srcs, slst);
                   1959:
                   1960:     if (bottom == (Src *)NULL) {
                   1961:        /*
                   1962:         * No known transformations -- use the first suffix found for setting
                   1963:         * the local variables.
                   1964:         */
                   1965:        if (!Lst_IsEmpty(targs)) {
                   1966:            targ = (Src *)Lst_Datum(Lst_First(targs));
                   1967:        } else {
                   1968:            targ = (Src *)NULL;
                   1969:        }
                   1970:     } else {
                   1971:        /*
                   1972:         * Work up the transformation path to find the suffix of the
                   1973:         * target to which the transformation was made.
                   1974:         */
                   1975:        for (targ = bottom; targ->parent != NULL; targ = targ->parent)
                   1976:            continue;
                   1977:     }
                   1978:
                   1979:     /*
                   1980:      * The .TARGET variable we always set to be the name at this point,
                   1981:      * since it's only set to the path if the thing is only a source and
                   1982:      * if it's only a source, it doesn't matter what we put here as far
                   1983:      * as expanding sources is concerned, since it has none...
                   1984:      */
                   1985:     Var_Set(TARGET, gn->name, gn);
                   1986:
                   1987:     pref = (targ != NULL) ? targ->pref : gn->name;
                   1988:     Var_Set(PREFIX, pref, gn);
                   1989:
                   1990:     /*
                   1991:      * Now we've got the important local variables set, expand any sources
                   1992:      * that still contain variables or wildcards in their names.
                   1993:      */
                   1994:     Lst_ForEach(gn->children, SuffExpandChildren, (ClientData)gn);
1.5       millert  1995:
1.1       deraadt  1996:     if (targ == NULL) {
                   1997:        if (DEBUG(SUFF)) {
                   1998:            printf("\tNo valid suffix on %s\n", gn->name);
                   1999:        }
                   2000:
                   2001: sfnd_abort:
                   2002:        /*
                   2003:         * Deal with finding the thing on the default search path if the
                   2004:         * node is only a source (not on the lhs of a dependency operator
                   2005:         * or [XXX] it has neither children or commands).
                   2006:         */
                   2007:        if (OP_NOP(gn->type) ||
                   2008:            (Lst_IsEmpty(gn->children) && Lst_IsEmpty(gn->commands)))
                   2009:        {
                   2010:            gn->path = Dir_FindFile(gn->name,
                   2011:                                    (targ == NULL ? dirSearchPath :
                   2012:                                     targ->suff->searchPath));
                   2013:            if (gn->path != NULL) {
1.2       deraadt  2014:                char *ptr;
1.1       deraadt  2015:                Var_Set(TARGET, gn->path, gn);
                   2016:
                   2017:                if (targ != NULL) {
                   2018:                    /*
                   2019:                     * Suffix known for the thing -- trim the suffix off
                   2020:                     * the path to form the proper .PREFIX variable.
                   2021:                     */
1.2       deraadt  2022:                    int         savep = strlen(gn->path) - targ->suff->nameLen;
1.1       deraadt  2023:                    char        savec;
                   2024:
                   2025:                    if (gn->suffix)
                   2026:                        gn->suffix->refCount--;
                   2027:                    gn->suffix = targ->suff;
                   2028:                    gn->suffix->refCount++;
                   2029:
1.2       deraadt  2030:                    savec = gn->path[savep];
                   2031:                    gn->path[savep] = '\0';
1.1       deraadt  2032:
1.2       deraadt  2033:                    if ((ptr = strrchr(gn->path, '/')) != NULL)
                   2034:                        ptr++;
                   2035:                    else
                   2036:                        ptr = gn->path;
1.1       deraadt  2037:
1.2       deraadt  2038:                    Var_Set(PREFIX, ptr, gn);
                   2039:
                   2040:                    gn->path[savep] = savec;
1.1       deraadt  2041:                } else {
                   2042:                    /*
                   2043:                     * The .PREFIX gets the full path if the target has
                   2044:                     * no known suffix.
                   2045:                     */
                   2046:                    if (gn->suffix)
                   2047:                        gn->suffix->refCount--;
                   2048:                    gn->suffix = NULL;
                   2049:
1.2       deraadt  2050:                    if ((ptr = strrchr(gn->path, '/')) != NULL)
                   2051:                        ptr++;
                   2052:                    else
                   2053:                        ptr = gn->path;
                   2054:
                   2055:                    Var_Set(PREFIX, ptr, gn);
1.1       deraadt  2056:                }
                   2057:            }
                   2058:        } else {
                   2059:            /*
                   2060:             * Not appropriate to search for the thing -- set the
                   2061:             * path to be the name so Dir_MTime won't go grovelling for
                   2062:             * it.
                   2063:             */
                   2064:            if (gn->suffix)
                   2065:                gn->suffix->refCount--;
                   2066:            gn->suffix = (targ == NULL) ? NULL : targ->suff;
                   2067:            if (gn->suffix)
                   2068:                gn->suffix->refCount++;
1.11      espie    2069:            efree(gn->path);
1.4       briggs   2070:            gn->path = estrdup(gn->name);
1.1       deraadt  2071:        }
1.5       millert  2072:
1.1       deraadt  2073:        goto sfnd_return;
                   2074:     }
                   2075:
                   2076:     /*
                   2077:      * If the suffix indicates that the target is a library, mark that in
                   2078:      * the node's type field.
                   2079:      */
                   2080:     if (targ->suff->flags & SUFF_LIBRARY) {
                   2081:        gn->type |= OP_LIB;
                   2082:     }
                   2083:
                   2084:     /*
                   2085:      * Check for overriding transformation rule implied by sources
                   2086:      */
                   2087:     if (!Lst_IsEmpty(gn->children)) {
                   2088:        src = SuffFindCmds(targ, slst);
                   2089:
                   2090:        if (src != (Src *)NULL) {
                   2091:            /*
                   2092:             * Free up all the Src structures in the transformation path
                   2093:             * up to, but not including, the parent node.
                   2094:             */
                   2095:            while (bottom && bottom->parent != NULL) {
                   2096:                if (Lst_Member(slst, (ClientData) bottom) == NILLNODE) {
                   2097:                    Lst_AtEnd(slst, (ClientData) bottom);
                   2098:                }
                   2099:                bottom = bottom->parent;
                   2100:            }
                   2101:            bottom = src;
                   2102:        }
                   2103:     }
                   2104:
                   2105:     if (bottom == NULL) {
                   2106:        /*
                   2107:         * No idea from where it can come -- return now.
                   2108:         */
                   2109:        goto sfnd_abort;
                   2110:     }
                   2111:
                   2112:     /*
                   2113:      * We now have a list of Src structures headed by 'bottom' and linked via
                   2114:      * their 'parent' pointers. What we do next is create links between
                   2115:      * source and target nodes (which may or may not have been created)
                   2116:      * and set the necessary local variables in each target. The
                   2117:      * commands for each target are set from the commands of the
                   2118:      * transformation rule used to get from the src suffix to the targ
                   2119:      * suffix. Note that this causes the commands list of the original
                   2120:      * node, gn, to be replaced by the commands of the final
                   2121:      * transformation rule. Also, the unmade field of gn is incremented.
1.5       millert  2122:      * Etc.
1.1       deraadt  2123:      */
                   2124:     if (bottom->node == NILGNODE) {
                   2125:        bottom->node = Targ_FindNode(bottom->file, TARG_CREATE);
                   2126:     }
1.5       millert  2127:
1.1       deraadt  2128:     for (src = bottom; src->parent != (Src *)NULL; src = src->parent) {
                   2129:        targ = src->parent;
                   2130:
                   2131:        if (src->node->suffix)
                   2132:            src->node->suffix->refCount--;
                   2133:        src->node->suffix = src->suff;
                   2134:        src->node->suffix->refCount++;
                   2135:
                   2136:        if (targ->node == NILGNODE) {
                   2137:            targ->node = Targ_FindNode(targ->file, TARG_CREATE);
                   2138:        }
                   2139:
                   2140:        SuffApplyTransform(targ->node, src->node,
                   2141:                           targ->suff, src->suff);
                   2142:
                   2143:        if (targ->node != gn) {
                   2144:            /*
                   2145:             * Finish off the dependency-search process for any nodes
                   2146:             * between bottom and gn (no point in questing around the
                   2147:             * filesystem for their implicit source when it's already
                   2148:             * known). Note that the node can't have any sources that
                   2149:             * need expanding, since SuffFindThem will stop on an existing
                   2150:             * node, so all we need to do is set the standard and System V
                   2151:             * variables.
                   2152:             */
                   2153:            targ->node->type |= OP_DEPS_FOUND;
                   2154:
                   2155:            Var_Set(PREFIX, targ->pref, targ->node);
1.5       millert  2156:
1.1       deraadt  2157:            Var_Set(TARGET, targ->node->name, targ->node);
                   2158:        }
                   2159:     }
                   2160:
                   2161:     if (gn->suffix)
                   2162:        gn->suffix->refCount--;
                   2163:     gn->suffix = src->suff;
                   2164:     gn->suffix->refCount++;
                   2165:
                   2166:     /*
                   2167:      * So Dir_MTime doesn't go questing for it...
                   2168:      */
1.11      espie    2169:     efree(gn->path);
1.4       briggs   2170:     gn->path = estrdup(gn->name);
1.1       deraadt  2171:
                   2172:     /*
                   2173:      * Nuke the transformation path and the Src structures left over in the
                   2174:      * two lists.
                   2175:      */
                   2176: sfnd_return:
                   2177:     if (bottom)
                   2178:        if (Lst_Member(slst, (ClientData) bottom) == NILLNODE)
                   2179:            Lst_AtEnd(slst, (ClientData) bottom);
                   2180:
                   2181:     while (SuffRemoveSrc(srcs) || SuffRemoveSrc(targs))
                   2182:        continue;
                   2183:
                   2184:     Lst_Concat(slst, srcs, LST_CONCLINK);
                   2185:     Lst_Concat(slst, targs, LST_CONCLINK);
                   2186: }
1.5       millert  2187:
                   2188:
1.1       deraadt  2189: /*-
                   2190:  *-----------------------------------------------------------------------
                   2191:  * Suff_FindDeps  --
                   2192:  *     Find implicit sources for the target described by the graph node
                   2193:  *     gn
                   2194:  *
                   2195:  * Results:
                   2196:  *     Nothing.
                   2197:  *
                   2198:  * Side Effects:
                   2199:  *     Nodes are added to the graph below the passed-in node. The nodes
                   2200:  *     are marked to have their IMPSRC variable filled in. The
                   2201:  *     PREFIX variable is set for the given node and all its
                   2202:  *     implied children.
                   2203:  *
                   2204:  * Notes:
                   2205:  *     The path found by this target is the shortest path in the
                   2206:  *     transformation graph, which may pass through non-existent targets,
                   2207:  *     to an existing target. The search continues on all paths from the
                   2208:  *     root suffix until a file is found. I.e. if there's a path
                   2209:  *     .o -> .c -> .l -> .l,v from the root and the .l,v file exists but
                   2210:  *     the .c and .l files don't, the search will branch out in
                   2211:  *     all directions from .o and again from all the nodes on the
                   2212:  *     next level until the .l,v node is encountered.
                   2213:  *
                   2214:  *-----------------------------------------------------------------------
                   2215:  */
                   2216:
                   2217: void
                   2218: Suff_FindDeps(gn)
                   2219:     GNode *gn;
                   2220: {
1.5       millert  2221:
1.1       deraadt  2222:     SuffFindDeps(gn, srclist);
                   2223:     while (SuffRemoveSrc(srclist))
                   2224:        continue;
                   2225: }
                   2226:
                   2227:
                   2228: static void
                   2229: SuffFindDeps (gn, slst)
                   2230:     GNode         *gn;         /* node we're dealing with */
                   2231:     Lst                  slst;
                   2232: {
                   2233:     if (gn->type & OP_DEPS_FOUND) {
                   2234:        /*
                   2235:         * If dependencies already found, no need to do it again...
                   2236:         */
                   2237:        return;
                   2238:     } else {
                   2239:        gn->type |= OP_DEPS_FOUND;
                   2240:     }
1.5       millert  2241:
1.1       deraadt  2242:     if (DEBUG(SUFF)) {
                   2243:        printf ("SuffFindDeps (%s)\n", gn->name);
                   2244:     }
1.5       millert  2245:
1.1       deraadt  2246:     if (gn->type & OP_ARCHV) {
                   2247:        SuffFindArchiveDeps(gn, slst);
                   2248:     } else if (gn->type & OP_LIB) {
                   2249:        /*
                   2250:         * If the node is a library, it is the arch module's job to find it
                   2251:         * and set the TARGET variable accordingly. We merely provide the
                   2252:         * search path, assuming all libraries end in ".a" (if the suffix
                   2253:         * hasn't been defined, there's nothing we can do for it, so we just
                   2254:         * set the TARGET variable to the node's name in order to give it a
                   2255:         * value).
                   2256:         */
                   2257:        LstNode ln;
                   2258:        Suff    *s;
1.5       millert  2259:
1.1       deraadt  2260:        ln = Lst_Find (sufflist, (ClientData)LIBSUFF, SuffSuffHasNameP);
                   2261:        if (gn->suffix)
                   2262:            gn->suffix->refCount--;
                   2263:        if (ln != NILLNODE) {
                   2264:            gn->suffix = s = (Suff *) Lst_Datum (ln);
                   2265:            gn->suffix->refCount++;
                   2266:            Arch_FindLib (gn, s->searchPath);
                   2267:        } else {
                   2268:            gn->suffix = NULL;
                   2269:            Var_Set (TARGET, gn->name, gn);
                   2270:        }
                   2271:        /*
                   2272:         * Because a library (-lfoo) target doesn't follow the standard
                   2273:         * filesystem conventions, we don't set the regular variables for
                   2274:         * the thing. .PREFIX is simply made empty...
                   2275:         */
                   2276:        Var_Set(PREFIX, "", gn);
                   2277:     } else {
                   2278:        SuffFindNormalDeps(gn, slst);
                   2279:     }
                   2280: }
                   2281:
                   2282: /*-
                   2283:  *-----------------------------------------------------------------------
                   2284:  * Suff_SetNull --
                   2285:  *     Define which suffix is the null suffix.
                   2286:  *
                   2287:  * Results:
                   2288:  *     None.
                   2289:  *
                   2290:  * Side Effects:
                   2291:  *     'suffNull' is altered.
                   2292:  *
                   2293:  * Notes:
                   2294:  *     Need to handle the changing of the null suffix gracefully so the
                   2295:  *     old transformation rules don't just go away.
                   2296:  *
                   2297:  *-----------------------------------------------------------------------
                   2298:  */
                   2299: void
                   2300: Suff_SetNull(name)
                   2301:     char    *name;         /* Name of null suffix */
                   2302: {
                   2303:     Suff    *s;
                   2304:     LstNode ln;
                   2305:
                   2306:     ln = Lst_Find(sufflist, (ClientData)name, SuffSuffHasNameP);
                   2307:     if (ln != NILLNODE) {
                   2308:        s = (Suff *)Lst_Datum(ln);
                   2309:        if (suffNull != (Suff *)NULL) {
                   2310:            suffNull->flags &= ~SUFF_NULL;
                   2311:        }
                   2312:        s->flags |= SUFF_NULL;
                   2313:        /*
                   2314:         * XXX: Here's where the transformation mangling would take place
                   2315:         */
                   2316:        suffNull = s;
                   2317:     } else {
                   2318:        Parse_Error (PARSE_WARNING, "Desired null suffix %s not defined.",
                   2319:                     name);
                   2320:     }
                   2321: }
                   2322:
                   2323: /*-
                   2324:  *-----------------------------------------------------------------------
                   2325:  * Suff_Init --
                   2326:  *     Initialize suffixes module
                   2327:  *
                   2328:  * Results:
                   2329:  *     None
                   2330:  *
                   2331:  * Side Effects:
                   2332:  *     Many
                   2333:  *-----------------------------------------------------------------------
                   2334:  */
                   2335: void
                   2336: Suff_Init ()
                   2337: {
                   2338:     sufflist = Lst_Init (FALSE);
1.14      espie    2339: #ifdef CLEANUP
1.1       deraadt  2340:     suffClean = Lst_Init(FALSE);
1.14      espie    2341: #endif
1.1       deraadt  2342:     srclist = Lst_Init (FALSE);
                   2343:     transforms = Lst_Init (FALSE);
                   2344:
                   2345:     sNum = 0;
                   2346:     /*
                   2347:      * Create null suffix for single-suffix rules (POSIX). The thing doesn't
                   2348:      * actually go on the suffix list or everyone will think that's its
                   2349:      * suffix.
                   2350:      */
                   2351:     emptySuff = suffNull = (Suff *) emalloc (sizeof (Suff));
                   2352:
1.4       briggs   2353:     suffNull->name =               estrdup ("");
1.1       deraadt  2354:     suffNull->nameLen =     0;
                   2355:     suffNull->searchPath =  Lst_Init (FALSE);
                   2356:     Dir_Concat(suffNull->searchPath, dirSearchPath);
                   2357:     suffNull->children =    Lst_Init (FALSE);
                   2358:     suffNull->parents =            Lst_Init (FALSE);
                   2359:     suffNull->ref =        Lst_Init (FALSE);
                   2360:     suffNull->sNum =               sNum++;
                   2361:     suffNull->flags =              SUFF_NULL;
                   2362:     suffNull->refCount =    1;
                   2363:
                   2364: }
                   2365:
                   2366:
                   2367: /*-
                   2368:  *----------------------------------------------------------------------
                   2369:  * Suff_End --
                   2370:  *     Cleanup the this module
                   2371:  *
                   2372:  * Results:
                   2373:  *     None
                   2374:  *
                   2375:  * Side Effects:
                   2376:  *     The memory is free'd.
                   2377:  *----------------------------------------------------------------------
                   2378:  */
                   2379:
                   2380: void
                   2381: Suff_End()
                   2382: {
1.14      espie    2383: #ifdef CLEANUP
1.1       deraadt  2384:     Lst_Destroy(sufflist, SuffFree);
                   2385:     Lst_Destroy(suffClean, SuffFree);
                   2386:     if (suffNull)
                   2387:        SuffFree(suffNull);
                   2388:     Lst_Destroy(srclist, NOFREE);
                   2389:     Lst_Destroy(transforms, NOFREE);
1.14      espie    2390: #endif
1.1       deraadt  2391: }
                   2392:
                   2393:
                   2394: /********************* DEBUGGING FUNCTIONS **********************/
                   2395:
                   2396: static int SuffPrintName(s, dummy)
                   2397:     ClientData s;
                   2398:     ClientData dummy;
                   2399: {
                   2400:     printf ("%s ", ((Suff *) s)->name);
                   2401:     return (dummy ? 0 : 0);
                   2402: }
                   2403:
                   2404: static int
                   2405: SuffPrintSuff (sp, dummy)
                   2406:     ClientData sp;
                   2407:     ClientData dummy;
                   2408: {
                   2409:     Suff    *s = (Suff *) sp;
                   2410:     int            flags;
                   2411:     int            flag;
                   2412:
                   2413:     printf ("# `%s' [%d] ", s->name, s->refCount);
1.5       millert  2414:
1.1       deraadt  2415:     flags = s->flags;
                   2416:     if (flags) {
                   2417:        fputs (" (", stdout);
                   2418:        while (flags) {
                   2419:            flag = 1 << (ffs(flags) - 1);
                   2420:            flags &= ~flag;
                   2421:            switch (flag) {
                   2422:                case SUFF_NULL:
                   2423:                    printf ("NULL");
                   2424:                    break;
                   2425:                case SUFF_INCLUDE:
                   2426:                    printf ("INCLUDE");
                   2427:                    break;
                   2428:                case SUFF_LIBRARY:
                   2429:                    printf ("LIBRARY");
                   2430:                    break;
                   2431:            }
                   2432:            fputc(flags ? '|' : ')', stdout);
                   2433:        }
                   2434:     }
                   2435:     fputc ('\n', stdout);
                   2436:     printf ("#\tTo: ");
                   2437:     Lst_ForEach (s->parents, SuffPrintName, (ClientData)0);
                   2438:     fputc ('\n', stdout);
                   2439:     printf ("#\tFrom: ");
                   2440:     Lst_ForEach (s->children, SuffPrintName, (ClientData)0);
                   2441:     fputc ('\n', stdout);
                   2442:     printf ("#\tSearch Path: ");
                   2443:     Dir_PrintPath (s->searchPath);
                   2444:     fputc ('\n', stdout);
                   2445:     return (dummy ? 0 : 0);
                   2446: }
                   2447:
                   2448: static int
                   2449: SuffPrintTrans (tp, dummy)
                   2450:     ClientData tp;
                   2451:     ClientData dummy;
                   2452: {
                   2453:     GNode   *t = (GNode *) tp;
                   2454:
                   2455:     printf ("%-16s: ", t->name);
                   2456:     Targ_PrintType (t->type);
                   2457:     fputc ('\n', stdout);
                   2458:     Lst_ForEach (t->commands, Targ_PrintCmd, (ClientData)0);
                   2459:     fputc ('\n', stdout);
                   2460:     return(dummy ? 0 : 0);
                   2461: }
                   2462:
                   2463: void
                   2464: Suff_PrintAll()
                   2465: {
                   2466:     printf ("#*** Suffixes:\n");
                   2467:     Lst_ForEach (sufflist, SuffPrintSuff, (ClientData)0);
                   2468:
                   2469:     printf ("#*** Transformations:\n");
                   2470:     Lst_ForEach (transforms, SuffPrintTrans, (ClientData)0);
                   2471: }