[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.12

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