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

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