[BACK]Return to cond.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / usr.bin / make

Annotation of src/usr.bin/make/cond.c, Revision 1.22

1.22    ! sjg         1: /*     $NetBSD: cond.c,v 1.21 2004/04/08 01:35:33 sjg Exp $    */
1.6       christos    2:
1.1       cgd         3: /*
                      4:  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
1.17      agc         5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * Adam de Boor.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. Neither the name of the University nor the names of its contributors
                     19:  *    may be used to endorse or promote products derived from this software
                     20:  *    without specific prior written permission.
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     23:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     24:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     26:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     32:  * SUCH DAMAGE.
                     33:  */
                     34:
                     35: /*
1.1       cgd        36:  * Copyright (c) 1988, 1989 by Adam de Boor
                     37:  * Copyright (c) 1989 by Berkeley Softworks
                     38:  * All rights reserved.
                     39:  *
                     40:  * This code is derived from software contributed to Berkeley by
                     41:  * Adam de Boor.
                     42:  *
                     43:  * Redistribution and use in source and binary forms, with or without
                     44:  * modification, are permitted provided that the following conditions
                     45:  * are met:
                     46:  * 1. Redistributions of source code must retain the above copyright
                     47:  *    notice, this list of conditions and the following disclaimer.
                     48:  * 2. Redistributions in binary form must reproduce the above copyright
                     49:  *    notice, this list of conditions and the following disclaimer in the
                     50:  *    documentation and/or other materials provided with the distribution.
                     51:  * 3. All advertising materials mentioning features or use of this software
                     52:  *    must display the following acknowledgement:
                     53:  *     This product includes software developed by the University of
                     54:  *     California, Berkeley and its contributors.
                     55:  * 4. Neither the name of the University nor the names of its contributors
                     56:  *    may be used to endorse or promote products derived from this software
                     57:  *    without specific prior written permission.
                     58:  *
                     59:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     60:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     61:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     62:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     63:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     64:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     65:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     66:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     67:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     68:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     69:  * SUCH DAMAGE.
                     70:  */
                     71:
1.9       lukem      72: #ifdef MAKE_BOOTSTRAP
1.22    ! sjg        73: static char rcsid[] = "$NetBSD: cond.c,v 1.21 2004/04/08 01:35:33 sjg Exp $";
1.9       lukem      74: #else
1.8       christos   75: #include <sys/cdefs.h>
1.1       cgd        76: #ifndef lint
1.6       christos   77: #if 0
1.7       christos   78: static char sccsid[] = "@(#)cond.c     8.2 (Berkeley) 1/2/94";
1.6       christos   79: #else
1.22    ! sjg        80: __RCSID("$NetBSD: cond.c,v 1.21 2004/04/08 01:35:33 sjg Exp $");
1.6       christos   81: #endif
1.1       cgd        82: #endif /* not lint */
1.9       lukem      83: #endif
1.1       cgd        84:
                     85: /*-
                     86:  * cond.c --
                     87:  *     Functions to handle conditionals in a makefile.
                     88:  *
                     89:  * Interface:
                     90:  *     Cond_Eval       Evaluate the conditional in the passed line.
                     91:  *
                     92:  */
                     93:
1.4       cgd        94: #include    <ctype.h>
1.13      wiz        95:
1.1       cgd        96: #include    "make.h"
1.4       cgd        97: #include    "hash.h"
                     98: #include    "dir.h"
                     99: #include    "buf.h"
1.1       cgd       100:
                    101: /*
                    102:  * The parsing of conditional expressions is based on this grammar:
                    103:  *     E -> F || E
                    104:  *     E -> F
                    105:  *     F -> T && F
                    106:  *     F -> T
                    107:  *     T -> defined(variable)
                    108:  *     T -> make(target)
                    109:  *     T -> exists(file)
                    110:  *     T -> empty(varspec)
                    111:  *     T -> target(name)
1.12      christos  112:  *     T -> commands(name)
1.1       cgd       113:  *     T -> symbol
                    114:  *     T -> $(varspec) op value
                    115:  *     T -> $(varspec) == "string"
                    116:  *     T -> $(varspec) != "string"
                    117:  *     T -> ( E )
                    118:  *     T -> ! T
                    119:  *     op -> == | != | > | < | >= | <=
                    120:  *
                    121:  * 'symbol' is some other symbol to which the default function (condDefProc)
                    122:  * is applied.
                    123:  *
                    124:  * Tokens are scanned from the 'condExpr' string. The scanner (CondToken)
                    125:  * will return And for '&' and '&&', Or for '|' and '||', Not for '!',
                    126:  * LParen for '(', RParen for ')' and will evaluate the other terminal
                    127:  * symbols, using either the default function or the function given in the
                    128:  * terminal, and return the result as either True or False.
                    129:  *
                    130:  * All Non-Terminal functions (CondE, CondF and CondT) return Err on error.
                    131:  */
                    132: typedef enum {
                    133:     And, Or, Not, True, False, LParen, RParen, EndOfFile, None, Err
                    134: } Token;
                    135:
                    136: /*-
                    137:  * Structures to handle elegantly the different forms of #if's. The
                    138:  * last two fields are stored in condInvert and condDefProc, respectively.
                    139:  */
1.13      wiz       140: static void CondPushBack(Token);
1.16      christos  141: static int CondGetArg(char **, char **, const char *, Boolean);
1.13      wiz       142: static Boolean CondDoDefined(int, char *);
                    143: static int CondStrMatch(ClientData, ClientData);
                    144: static Boolean CondDoMake(int, char *);
                    145: static Boolean CondDoExists(int, char *);
                    146: static Boolean CondDoTarget(int, char *);
                    147: static Boolean CondDoCommands(int, char *);
1.19      sjg       148: static char * CondCvtArg(char *, double *);
1.13      wiz       149: static Token CondToken(Boolean);
                    150: static Token CondT(Boolean);
                    151: static Token CondF(Boolean);
                    152: static Token CondE(Boolean);
1.1       cgd       153:
                    154: static struct If {
1.16      christos  155:     const char *form;        /* Form of if */
1.1       cgd       156:     int                formlen;      /* Length of form */
                    157:     Boolean    doNot;        /* TRUE if default function should be negated */
1.13      wiz       158:     Boolean    (*defProc)(int, char *); /* Default function to apply */
1.1       cgd       159: } ifs[] = {
1.4       cgd       160:     { "ifdef",   5,      FALSE,  CondDoDefined },
                    161:     { "ifndef",          6,      TRUE,   CondDoDefined },
                    162:     { "ifmake",          6,      FALSE,  CondDoMake },
                    163:     { "ifnmake",  7,     TRUE,   CondDoMake },
                    164:     { "if",      2,      FALSE,  CondDoDefined },
1.7       christos  165:     { NULL,      0,      FALSE,  NULL }
1.1       cgd       166: };
                    167:
                    168: static Boolean   condInvert;           /* Invert the default function */
1.13      wiz       169: static Boolean   (*condDefProc)(int, char *);  /* Default function to apply */
1.1       cgd       170: static char      *condExpr;            /* The expression to parse */
                    171: static Token     condPushBack=None;    /* Single push-back token used in
                    172:                                         * parsing */
                    173:
1.22    ! sjg       174: #define        MAXIF           64        /* greatest depth of #if'ing */
1.1       cgd       175:
1.22    ! sjg       176: static Boolean   finalElse[MAXIF+1][MAXIF+1]; /* Seen final else (stack) */
1.1       cgd       177: static Boolean   condStack[MAXIF];     /* Stack of conditionals's values */
                    178: static int       condTop = MAXIF;      /* Top-most conditional */
                    179: static int       skipIfLevel=0;        /* Depth of skipped conditionals */
                    180: static Boolean   skipLine = FALSE;     /* Whether the parse module is skipping
                    181:                                         * lines */
                    182:
                    183: /*-
                    184:  *-----------------------------------------------------------------------
                    185:  * CondPushBack --
                    186:  *     Push back the most recent token read. We only need one level of
                    187:  *     this, so the thing is just stored in 'condPushback'.
                    188:  *
1.13      wiz       189:  * Input:
                    190:  *     t               Token to push back into the "stream"
                    191:  *
1.1       cgd       192:  * Results:
                    193:  *     None.
                    194:  *
                    195:  * Side Effects:
                    196:  *     condPushback is overwritten.
                    197:  *
                    198:  *-----------------------------------------------------------------------
                    199:  */
                    200: static void
1.13      wiz       201: CondPushBack(Token t)
1.1       cgd       202: {
                    203:     condPushBack = t;
                    204: }
                    205: 
                    206: /*-
                    207:  *-----------------------------------------------------------------------
                    208:  * CondGetArg --
                    209:  *     Find the argument of a built-in function.
                    210:  *
1.13      wiz       211:  * Input:
                    212:  *     parens          TRUE if arg should be bounded by parens
                    213:  *
1.1       cgd       214:  * Results:
                    215:  *     The length of the argument and the address of the argument.
                    216:  *
                    217:  * Side Effects:
                    218:  *     The pointer is set to point to the closing parenthesis of the
                    219:  *     function call.
                    220:  *
                    221:  *-----------------------------------------------------------------------
                    222:  */
                    223: static int
1.16      christos  224: CondGetArg(char **linePtr, char **argPtr, const char *func, Boolean parens)
1.1       cgd       225: {
1.13      wiz       226:     char         *cp;
1.1       cgd       227:     int                  argLen;
1.13      wiz       228:     Buffer       buf;
1.1       cgd       229:
                    230:     cp = *linePtr;
                    231:     if (parens) {
                    232:        while (*cp != '(' && *cp != '\0') {
                    233:            cp++;
                    234:        }
                    235:        if (*cp == '(') {
                    236:            cp++;
                    237:        }
                    238:     }
                    239:
                    240:     if (*cp == '\0') {
                    241:        /*
                    242:         * No arguments whatsoever. Because 'make' and 'defined' aren't really
                    243:         * "reserved words", we don't print a message. I think this is better
                    244:         * than hitting the user with a warning message every time s/he uses
                    245:         * the word 'make' or 'defined' at the beginning of a symbol...
                    246:         */
                    247:        *argPtr = cp;
                    248:        return (0);
                    249:     }
                    250:
                    251:     while (*cp == ' ' || *cp == '\t') {
                    252:        cp++;
                    253:     }
                    254:
                    255:     /*
                    256:      * Create a buffer for the argument and start it out at 16 characters
                    257:      * long. Why 16? Why not?
                    258:      */
                    259:     buf = Buf_Init(16);
1.7       christos  260:
1.4       cgd       261:     while ((strchr(" \t)&|", *cp) == (char *)NULL) && (*cp != '\0')) {
1.1       cgd       262:        if (*cp == '$') {
                    263:            /*
                    264:             * Parse the variable spec and install it as part of the argument
                    265:             * if it's valid. We tell Var_Parse to complain on an undefined
                    266:             * variable, so we don't do it too. Nor do we return an error,
                    267:             * though perhaps we should...
                    268:             */
                    269:            char        *cp2;
                    270:            int         len;
                    271:            Boolean     doFree;
                    272:
                    273:            cp2 = Var_Parse(cp, VAR_CMD, TRUE, &len, &doFree);
                    274:
                    275:            Buf_AddBytes(buf, strlen(cp2), (Byte *)cp2);
                    276:            if (doFree) {
                    277:                free(cp2);
                    278:            }
                    279:            cp += len;
                    280:        } else {
                    281:            Buf_AddByte(buf, (Byte)*cp);
                    282:            cp++;
                    283:        }
                    284:     }
                    285:
                    286:     Buf_AddByte(buf, (Byte)'\0');
                    287:     *argPtr = (char *)Buf_GetAll(buf, &argLen);
                    288:     Buf_Destroy(buf, FALSE);
                    289:
                    290:     while (*cp == ' ' || *cp == '\t') {
                    291:        cp++;
                    292:     }
                    293:     if (parens && *cp != ')') {
                    294:        Parse_Error (PARSE_WARNING, "Missing closing parenthesis for %s()",
                    295:                     func);
                    296:        return (0);
                    297:     } else if (parens) {
                    298:        /*
                    299:         * Advance pointer past close parenthesis.
                    300:         */
                    301:        cp++;
                    302:     }
1.7       christos  303:
1.1       cgd       304:     *linePtr = cp;
                    305:     return (argLen);
                    306: }
                    307: 
                    308: /*-
                    309:  *-----------------------------------------------------------------------
                    310:  * CondDoDefined --
                    311:  *     Handle the 'defined' function for conditionals.
                    312:  *
                    313:  * Results:
                    314:  *     TRUE if the given variable is defined.
                    315:  *
                    316:  * Side Effects:
                    317:  *     None.
                    318:  *
                    319:  *-----------------------------------------------------------------------
                    320:  */
                    321: static Boolean
1.13      wiz       322: CondDoDefined(int argLen, char *arg)
1.1       cgd       323: {
                    324:     char    savec = arg[argLen];
1.5       jtc       325:     char    *p1;
1.1       cgd       326:     Boolean result;
                    327:
                    328:     arg[argLen] = '\0';
1.5       jtc       329:     if (Var_Value (arg, VAR_CMD, &p1) != (char *)NULL) {
1.1       cgd       330:        result = TRUE;
                    331:     } else {
                    332:        result = FALSE;
                    333:     }
1.5       jtc       334:     if (p1)
                    335:        free(p1);
1.1       cgd       336:     arg[argLen] = savec;
                    337:     return (result);
                    338: }
                    339: 
                    340: /*-
                    341:  *-----------------------------------------------------------------------
                    342:  * CondStrMatch --
                    343:  *     Front-end for Str_Match so it returns 0 on match and non-zero
                    344:  *     on mismatch. Callback function for CondDoMake via Lst_Find
                    345:  *
                    346:  * Results:
                    347:  *     0 if string matches pattern
                    348:  *
                    349:  * Side Effects:
                    350:  *     None
                    351:  *
                    352:  *-----------------------------------------------------------------------
                    353:  */
                    354: static int
1.13      wiz       355: CondStrMatch(ClientData string, ClientData pattern)
1.1       cgd       356: {
1.5       jtc       357:     return(!Str_Match((char *) string,(char *) pattern));
1.1       cgd       358: }
                    359: 
                    360: /*-
                    361:  *-----------------------------------------------------------------------
                    362:  * CondDoMake --
                    363:  *     Handle the 'make' function for conditionals.
                    364:  *
                    365:  * Results:
                    366:  *     TRUE if the given target is being made.
                    367:  *
                    368:  * Side Effects:
                    369:  *     None.
                    370:  *
                    371:  *-----------------------------------------------------------------------
                    372:  */
                    373: static Boolean
1.13      wiz       374: CondDoMake(int argLen, char *arg)
1.1       cgd       375: {
                    376:     char    savec = arg[argLen];
                    377:     Boolean result;
                    378:
                    379:     arg[argLen] = '\0';
                    380:     if (Lst_Find (create, (ClientData)arg, CondStrMatch) == NILLNODE) {
                    381:        result = FALSE;
                    382:     } else {
                    383:        result = TRUE;
                    384:     }
                    385:     arg[argLen] = savec;
                    386:     return (result);
                    387: }
                    388: 
                    389: /*-
                    390:  *-----------------------------------------------------------------------
                    391:  * CondDoExists --
                    392:  *     See if the given file exists.
                    393:  *
                    394:  * Results:
                    395:  *     TRUE if the file exists and FALSE if it does not.
                    396:  *
                    397:  * Side Effects:
                    398:  *     None.
                    399:  *
                    400:  *-----------------------------------------------------------------------
                    401:  */
                    402: static Boolean
1.13      wiz       403: CondDoExists(int argLen, char *arg)
1.1       cgd       404: {
                    405:     char    savec = arg[argLen];
                    406:     Boolean result;
                    407:     char    *path;
                    408:
                    409:     arg[argLen] = '\0';
                    410:     path = Dir_FindFile(arg, dirSearchPath);
                    411:     if (path != (char *)NULL) {
                    412:        result = TRUE;
                    413:        free(path);
                    414:     } else {
                    415:        result = FALSE;
                    416:     }
                    417:     arg[argLen] = savec;
                    418:     return (result);
                    419: }
                    420: 
                    421: /*-
                    422:  *-----------------------------------------------------------------------
                    423:  * CondDoTarget --
                    424:  *     See if the given node exists and is an actual target.
                    425:  *
                    426:  * Results:
                    427:  *     TRUE if the node exists as a target and FALSE if it does not.
                    428:  *
                    429:  * Side Effects:
                    430:  *     None.
                    431:  *
                    432:  *-----------------------------------------------------------------------
                    433:  */
                    434: static Boolean
1.13      wiz       435: CondDoTarget(int argLen, char *arg)
1.1       cgd       436: {
                    437:     char    savec = arg[argLen];
                    438:     Boolean result;
                    439:     GNode   *gn;
                    440:
                    441:     arg[argLen] = '\0';
                    442:     gn = Targ_FindNode(arg, TARG_NOCREATE);
                    443:     if ((gn != NILGNODE) && !OP_NOP(gn->type)) {
                    444:        result = TRUE;
                    445:     } else {
                    446:        result = FALSE;
                    447:     }
                    448:     arg[argLen] = savec;
                    449:     return (result);
                    450: }
                    451:
1.12      christos  452: /*-
                    453:  *-----------------------------------------------------------------------
                    454:  * CondDoCommands --
                    455:  *     See if the given node exists and is an actual target with commands
                    456:  *     associated with it.
                    457:  *
                    458:  * Results:
                    459:  *     TRUE if the node exists as a target and has commands associated with
                    460:  *     it and FALSE if it does not.
                    461:  *
                    462:  * Side Effects:
                    463:  *     None.
                    464:  *
                    465:  *-----------------------------------------------------------------------
                    466:  */
                    467: static Boolean
1.13      wiz       468: CondDoCommands(int argLen, char *arg)
1.12      christos  469: {
                    470:     char    savec = arg[argLen];
                    471:     Boolean result;
                    472:     GNode   *gn;
                    473:
                    474:     arg[argLen] = '\0';
                    475:     gn = Targ_FindNode(arg, TARG_NOCREATE);
                    476:     if ((gn != NILGNODE) && !OP_NOP(gn->type) && !Lst_IsEmpty(gn->commands)) {
                    477:        result = TRUE;
                    478:     } else {
                    479:        result = FALSE;
                    480:     }
                    481:     arg[argLen] = savec;
                    482:     return (result);
                    483: }
1.1       cgd       484: 
                    485: /*-
                    486:  *-----------------------------------------------------------------------
                    487:  * CondCvtArg --
                    488:  *     Convert the given number into a double. If the number begins
1.4       cgd       489:  *     with 0x, it is interpreted as a hexadecimal integer
1.1       cgd       490:  *     and converted to a double from there. All other strings just have
1.4       cgd       491:  *     strtod called on them.
1.1       cgd       492:  *
                    493:  * Results:
1.4       cgd       494:  *     Sets 'value' to double value of string.
1.19      sjg       495:  *     Returns NULL if string was fully consumed,
                    496:  *     else returns remaining input.
1.1       cgd       497:  *
                    498:  * Side Effects:
1.4       cgd       499:  *     Can change 'value' even if string is not a valid number.
1.7       christos  500:  *
1.1       cgd       501:  *
                    502:  *-----------------------------------------------------------------------
                    503:  */
1.19      sjg       504: static char *
1.13      wiz       505: CondCvtArg(char *str, double *value)
1.1       cgd       506: {
1.4       cgd       507:     if ((*str == '0') && (str[1] == 'x')) {
1.13      wiz       508:        long i;
1.1       cgd       509:
1.4       cgd       510:        for (str += 2, i = 0; *str; str++) {
                    511:            int x;
                    512:            if (isdigit((unsigned char) *str))
                    513:                x  = *str - '0';
                    514:            else if (isxdigit((unsigned char) *str))
                    515:                x = 10 + *str - isupper((unsigned char) *str) ? 'A' : 'a';
                    516:            else
1.19      sjg       517:                break;
1.4       cgd       518:            i = (i << 4) + x;
1.1       cgd       519:        }
1.4       cgd       520:        *value = (double) i;
1.19      sjg       521:        return *str ? str : NULL;
                    522:     } else {
1.4       cgd       523:        char *eptr;
                    524:        *value = strtod(str, &eptr);
1.19      sjg       525:        return *eptr ? eptr : NULL;
1.1       cgd       526:     }
                    527: }
                    528: 
                    529: /*-
                    530:  *-----------------------------------------------------------------------
                    531:  * CondToken --
                    532:  *     Return the next token from the input.
                    533:  *
                    534:  * Results:
                    535:  *     A Token for the next lexical token in the stream.
                    536:  *
                    537:  * Side Effects:
                    538:  *     condPushback will be set back to None if it is used.
                    539:  *
                    540:  *-----------------------------------------------------------------------
                    541:  */
                    542: static Token
1.13      wiz       543: CondToken(Boolean doEval)
1.1       cgd       544: {
                    545:     Token        t;
                    546:
                    547:     if (condPushBack == None) {
                    548:        while (*condExpr == ' ' || *condExpr == '\t') {
                    549:            condExpr++;
                    550:        }
                    551:        switch (*condExpr) {
                    552:            case '(':
                    553:                t = LParen;
                    554:                condExpr++;
                    555:                break;
                    556:            case ')':
                    557:                t = RParen;
                    558:                condExpr++;
                    559:                break;
                    560:            case '|':
                    561:                if (condExpr[1] == '|') {
                    562:                    condExpr++;
                    563:                }
                    564:                condExpr++;
                    565:                t = Or;
                    566:                break;
                    567:            case '&':
                    568:                if (condExpr[1] == '&') {
                    569:                    condExpr++;
                    570:                }
                    571:                condExpr++;
                    572:                t = And;
                    573:                break;
                    574:            case '!':
                    575:                t = Not;
                    576:                condExpr++;
                    577:                break;
1.14      sjg       578:            case '#':
1.1       cgd       579:            case '\n':
                    580:            case '\0':
                    581:                t = EndOfFile;
                    582:                break;
                    583:            case '$': {
                    584:                char    *lhs;
                    585:                char    *rhs;
                    586:                char    *op;
                    587:                int     varSpecLen;
                    588:                Boolean doFree;
                    589:
                    590:                /*
                    591:                 * Parse the variable spec and skip over it, saving its
                    592:                 * value in lhs.
                    593:                 */
                    594:                t = Err;
                    595:                lhs = Var_Parse(condExpr, VAR_CMD, doEval,&varSpecLen,&doFree);
                    596:                if (lhs == var_Error) {
                    597:                    /*
                    598:                     * Even if !doEval, we still report syntax errors, which
                    599:                     * is what getting var_Error back with !doEval means.
                    600:                     */
                    601:                    return(Err);
                    602:                }
                    603:                condExpr += varSpecLen;
                    604:
1.5       jtc       605:                if (!isspace((unsigned char) *condExpr) &&
                    606:                    strchr("!=><", *condExpr) == NULL) {
1.4       cgd       607:                    Buffer buf;
                    608:                    char *cp;
                    609:
                    610:                    buf = Buf_Init(0);
                    611:
                    612:                    for (cp = lhs; *cp; cp++)
                    613:                        Buf_AddByte(buf, (Byte)*cp);
                    614:
                    615:                    if (doFree)
                    616:                        free(lhs);
                    617:
1.5       jtc       618:                    for (;*condExpr && !isspace((unsigned char) *condExpr);
                    619:                         condExpr++)
1.4       cgd       620:                        Buf_AddByte(buf, (Byte)*condExpr);
                    621:
                    622:                    Buf_AddByte(buf, (Byte)'\0');
                    623:                    lhs = (char *)Buf_GetAll(buf, &varSpecLen);
                    624:                    Buf_Destroy(buf, FALSE);
                    625:
                    626:                    doFree = TRUE;
                    627:                }
                    628:
1.1       cgd       629:                /*
                    630:                 * Skip whitespace to get to the operator
                    631:                 */
1.5       jtc       632:                while (isspace((unsigned char) *condExpr))
1.1       cgd       633:                    condExpr++;
1.4       cgd       634:
1.1       cgd       635:                /*
                    636:                 * Make sure the operator is a valid one. If it isn't a
                    637:                 * known relational operator, pretend we got a
                    638:                 * != 0 comparison.
                    639:                 */
                    640:                op = condExpr;
                    641:                switch (*condExpr) {
                    642:                    case '!':
                    643:                    case '=':
                    644:                    case '<':
                    645:                    case '>':
                    646:                        if (condExpr[1] == '=') {
                    647:                            condExpr += 2;
                    648:                        } else {
                    649:                            condExpr += 1;
                    650:                        }
                    651:                        break;
                    652:                    default:
1.16      christos  653:                        op = UNCONST("!=");
                    654:                        rhs = UNCONST("0");
1.1       cgd       655:
                    656:                        goto do_compare;
                    657:                }
1.5       jtc       658:                while (isspace((unsigned char) *condExpr)) {
1.1       cgd       659:                    condExpr++;
                    660:                }
                    661:                if (*condExpr == '\0') {
                    662:                    Parse_Error(PARSE_WARNING,
                    663:                                "Missing right-hand-side of operator");
                    664:                    goto error;
                    665:                }
                    666:                rhs = condExpr;
                    667: do_compare:
                    668:                if (*rhs == '"') {
                    669:                    /*
                    670:                     * Doing a string comparison. Only allow == and != for
                    671:                     * operators.
                    672:                     */
                    673:                    char    *string;
                    674:                    char    *cp, *cp2;
1.4       cgd       675:                    int     qt;
1.1       cgd       676:                    Buffer  buf;
                    677:
1.4       cgd       678: do_string_compare:
1.1       cgd       679:                    if (((*op != '!') && (*op != '=')) || (op[1] != '=')) {
                    680:                        Parse_Error(PARSE_WARNING,
                    681:                "String comparison operator should be either == or !=");
                    682:                        goto error;
                    683:                    }
                    684:
                    685:                    buf = Buf_Init(0);
1.4       cgd       686:                    qt = *rhs == '"' ? 1 : 0;
1.7       christos  687:
                    688:                    for (cp = &rhs[qt];
                    689:                         ((qt && (*cp != '"')) ||
                    690:                          (!qt && strchr(" \t)", *cp) == NULL)) &&
1.4       cgd       691:                         (*cp != '\0'); cp++) {
1.1       cgd       692:                        if ((*cp == '\\') && (cp[1] != '\0')) {
                    693:                            /*
                    694:                             * Backslash escapes things -- skip over next
                    695:                             * character, if it exists.
                    696:                             */
                    697:                            cp++;
                    698:                            Buf_AddByte(buf, (Byte)*cp);
                    699:                        } else if (*cp == '$') {
                    700:                            int len;
                    701:                            Boolean freeIt;
1.7       christos  702:
1.1       cgd       703:                            cp2 = Var_Parse(cp, VAR_CMD, doEval,&len, &freeIt);
                    704:                            if (cp2 != var_Error) {
                    705:                                Buf_AddBytes(buf, strlen(cp2), (Byte *)cp2);
                    706:                                if (freeIt) {
                    707:                                    free(cp2);
                    708:                                }
                    709:                                cp += len - 1;
                    710:                            } else {
                    711:                                Buf_AddByte(buf, (Byte)*cp);
                    712:                            }
                    713:                        } else {
                    714:                            Buf_AddByte(buf, (Byte)*cp);
                    715:                        }
                    716:                    }
                    717:
                    718:                    Buf_AddByte(buf, (Byte)0);
                    719:
                    720:                    string = (char *)Buf_GetAll(buf, (int *)0);
                    721:                    Buf_Destroy(buf, FALSE);
                    722:
                    723:                    if (DEBUG(COND)) {
                    724:                        printf("lhs = \"%s\", rhs = \"%s\", op = %.2s\n",
                    725:                               lhs, string, op);
                    726:                    }
                    727:                    /*
                    728:                     * Null-terminate rhs and perform the comparison.
                    729:                     * t is set to the result.
                    730:                     */
                    731:                    if (*op == '=') {
                    732:                        t = strcmp(lhs, string) ? False : True;
                    733:                    } else {
                    734:                        t = strcmp(lhs, string) ? True : False;
                    735:                    }
                    736:                    free(string);
                    737:                    if (rhs == condExpr) {
1.4       cgd       738:                        if (!qt && *cp == ')')
                    739:                            condExpr = cp;
                    740:                        else
                    741:                            condExpr = cp + 1;
1.1       cgd       742:                    }
                    743:                } else {
                    744:                    /*
                    745:                     * rhs is either a float or an integer. Convert both the
                    746:                     * lhs and the rhs to a double and compare the two.
                    747:                     */
                    748:                    double      left, right;
                    749:                    char        *string;
                    750:
1.19      sjg       751:                    if (CondCvtArg(lhs, &left))
1.4       cgd       752:                        goto do_string_compare;
1.1       cgd       753:                    if (*rhs == '$') {
                    754:                        int     len;
                    755:                        Boolean freeIt;
1.7       christos  756:
1.1       cgd       757:                        string = Var_Parse(rhs, VAR_CMD, doEval,&len,&freeIt);
                    758:                        if (string == var_Error) {
                    759:                            right = 0.0;
                    760:                        } else {
1.19      sjg       761:                            if (CondCvtArg(string, &right)) {
1.4       cgd       762:                                if (freeIt)
                    763:                                    free(string);
                    764:                                goto do_string_compare;
                    765:                            }
                    766:                            if (freeIt)
1.1       cgd       767:                                free(string);
1.4       cgd       768:                            if (rhs == condExpr)
1.1       cgd       769:                                condExpr += len;
                    770:                        }
                    771:                    } else {
1.19      sjg       772:                        char *cp;
                    773:
                    774:                        if ((cp = CondCvtArg(rhs, &right)) &&
                    775:                            cp == rhs)
1.4       cgd       776:                            goto do_string_compare;
1.1       cgd       777:                        if (rhs == condExpr) {
                    778:                            /*
                    779:                             * Skip over the right-hand side
                    780:                             */
1.19      sjg       781:                            if (cp)
                    782:                                condExpr = cp;
                    783:                            else
                    784:                                condExpr = strchr(rhs, '\0');
1.1       cgd       785:                        }
                    786:                    }
1.7       christos  787:
1.1       cgd       788:                    if (DEBUG(COND)) {
                    789:                        printf("left = %f, right = %f, op = %.2s\n", left,
                    790:                               right, op);
                    791:                    }
                    792:                    switch(op[0]) {
                    793:                    case '!':
                    794:                        if (op[1] != '=') {
                    795:                            Parse_Error(PARSE_WARNING,
                    796:                                        "Unknown operator");
                    797:                            goto error;
                    798:                        }
                    799:                        t = (left != right ? True : False);
                    800:                        break;
                    801:                    case '=':
                    802:                        if (op[1] != '=') {
                    803:                            Parse_Error(PARSE_WARNING,
                    804:                                        "Unknown operator");
                    805:                            goto error;
                    806:                        }
                    807:                        t = (left == right ? True : False);
                    808:                        break;
                    809:                    case '<':
                    810:                        if (op[1] == '=') {
                    811:                            t = (left <= right ? True : False);
                    812:                        } else {
                    813:                            t = (left < right ? True : False);
                    814:                        }
                    815:                        break;
                    816:                    case '>':
                    817:                        if (op[1] == '=') {
                    818:                            t = (left >= right ? True : False);
                    819:                        } else {
                    820:                            t = (left > right ? True : False);
                    821:                        }
                    822:                        break;
                    823:                    }
                    824:                }
                    825: error:
1.4       cgd       826:                if (doFree)
1.1       cgd       827:                    free(lhs);
                    828:                break;
                    829:            }
                    830:            default: {
1.13      wiz       831:                Boolean (*evalProc)(int, char *);
1.1       cgd       832:                Boolean invert = FALSE;
                    833:                char    *arg;
                    834:                int     arglen;
1.7       christos  835:
1.1       cgd       836:                if (strncmp (condExpr, "defined", 7) == 0) {
                    837:                    /*
                    838:                     * Use CondDoDefined to evaluate the argument and
                    839:                     * CondGetArg to extract the argument from the 'function
                    840:                     * call'.
                    841:                     */
                    842:                    evalProc = CondDoDefined;
                    843:                    condExpr += 7;
                    844:                    arglen = CondGetArg (&condExpr, &arg, "defined", TRUE);
                    845:                    if (arglen == 0) {
                    846:                        condExpr -= 7;
                    847:                        goto use_default;
                    848:                    }
                    849:                } else if (strncmp (condExpr, "make", 4) == 0) {
                    850:                    /*
                    851:                     * Use CondDoMake to evaluate the argument and
                    852:                     * CondGetArg to extract the argument from the 'function
                    853:                     * call'.
                    854:                     */
                    855:                    evalProc = CondDoMake;
                    856:                    condExpr += 4;
                    857:                    arglen = CondGetArg (&condExpr, &arg, "make", TRUE);
                    858:                    if (arglen == 0) {
                    859:                        condExpr -= 4;
                    860:                        goto use_default;
                    861:                    }
                    862:                } else if (strncmp (condExpr, "exists", 6) == 0) {
                    863:                    /*
                    864:                     * Use CondDoExists to evaluate the argument and
                    865:                     * CondGetArg to extract the argument from the
                    866:                     * 'function call'.
                    867:                     */
                    868:                    evalProc = CondDoExists;
                    869:                    condExpr += 6;
                    870:                    arglen = CondGetArg(&condExpr, &arg, "exists", TRUE);
                    871:                    if (arglen == 0) {
                    872:                        condExpr -= 6;
                    873:                        goto use_default;
                    874:                    }
                    875:                } else if (strncmp(condExpr, "empty", 5) == 0) {
                    876:                    /*
                    877:                     * Use Var_Parse to parse the spec in parens and return
                    878:                     * True if the resulting string is empty.
                    879:                     */
                    880:                    int     length;
                    881:                    Boolean doFree;
                    882:                    char    *val;
                    883:
                    884:                    condExpr += 5;
                    885:
                    886:                    for (arglen = 0;
                    887:                         condExpr[arglen] != '(' && condExpr[arglen] != '\0';
                    888:                         arglen += 1)
1.5       jtc       889:                        continue;
                    890:
1.1       cgd       891:                    if (condExpr[arglen] != '\0') {
                    892:                        val = Var_Parse(&condExpr[arglen - 1], VAR_CMD,
1.18      sjg       893:                                        FALSE, &length, &doFree);
1.1       cgd       894:                        if (val == var_Error) {
                    895:                            t = Err;
                    896:                        } else {
1.7       christos  897:                            /*
                    898:                             * A variable is empty when it just contains
1.4       cgd       899:                             * spaces... 4/15/92, christos
                    900:                             */
                    901:                            char *p;
1.5       jtc       902:                            for (p = val; *p && isspace((unsigned char)*p); p++)
1.4       cgd       903:                                continue;
                    904:                            t = (*p == '\0') ? True : False;
1.1       cgd       905:                        }
                    906:                        if (doFree) {
                    907:                            free(val);
                    908:                        }
                    909:                        /*
                    910:                         * Advance condExpr to beyond the closing ). Note that
                    911:                         * we subtract one from arglen + length b/c length
                    912:                         * is calculated from condExpr[arglen - 1].
                    913:                         */
                    914:                        condExpr += arglen + length - 1;
                    915:                    } else {
                    916:                        condExpr -= 5;
                    917:                        goto use_default;
                    918:                    }
                    919:                    break;
                    920:                } else if (strncmp (condExpr, "target", 6) == 0) {
                    921:                    /*
                    922:                     * Use CondDoTarget to evaluate the argument and
                    923:                     * CondGetArg to extract the argument from the
                    924:                     * 'function call'.
                    925:                     */
                    926:                    evalProc = CondDoTarget;
                    927:                    condExpr += 6;
                    928:                    arglen = CondGetArg(&condExpr, &arg, "target", TRUE);
                    929:                    if (arglen == 0) {
                    930:                        condExpr -= 6;
1.12      christos  931:                        goto use_default;
                    932:                    }
                    933:                } else if (strncmp (condExpr, "commands", 8) == 0) {
                    934:                    /*
                    935:                     * Use CondDoCommands to evaluate the argument and
                    936:                     * CondGetArg to extract the argument from the
                    937:                     * 'function call'.
                    938:                     */
                    939:                    evalProc = CondDoCommands;
                    940:                    condExpr += 8;
                    941:                    arglen = CondGetArg(&condExpr, &arg, "commands", TRUE);
                    942:                    if (arglen == 0) {
                    943:                        condExpr -= 8;
1.1       cgd       944:                        goto use_default;
                    945:                    }
                    946:                } else {
                    947:                    /*
                    948:                     * The symbol is itself the argument to the default
                    949:                     * function. We advance condExpr to the end of the symbol
                    950:                     * by hand (the next whitespace, closing paren or
                    951:                     * binary operator) and set to invert the evaluation
                    952:                     * function if condInvert is TRUE.
                    953:                     */
                    954:                use_default:
                    955:                    invert = condInvert;
                    956:                    evalProc = condDefProc;
                    957:                    arglen = CondGetArg(&condExpr, &arg, "", FALSE);
                    958:                }
                    959:
                    960:                /*
                    961:                 * Evaluate the argument using the set function. If invert
                    962:                 * is TRUE, we invert the sense of the function.
                    963:                 */
                    964:                t = (!doEval || (* evalProc) (arglen, arg) ?
                    965:                     (invert ? False : True) :
                    966:                     (invert ? True : False));
                    967:                free(arg);
                    968:                break;
                    969:            }
                    970:        }
                    971:     } else {
                    972:        t = condPushBack;
                    973:        condPushBack = None;
                    974:     }
                    975:     return (t);
                    976: }
                    977: 
                    978: /*-
                    979:  *-----------------------------------------------------------------------
                    980:  * CondT --
                    981:  *     Parse a single term in the expression. This consists of a terminal
                    982:  *     symbol or Not and a terminal symbol (not including the binary
                    983:  *     operators):
                    984:  *         T -> defined(variable) | make(target) | exists(file) | symbol
                    985:  *         T -> ! T | ( E )
                    986:  *
                    987:  * Results:
                    988:  *     True, False or Err.
                    989:  *
                    990:  * Side Effects:
                    991:  *     Tokens are consumed.
                    992:  *
                    993:  *-----------------------------------------------------------------------
                    994:  */
                    995: static Token
1.13      wiz       996: CondT(Boolean doEval)
1.1       cgd       997: {
                    998:     Token   t;
                    999:
                   1000:     t = CondToken(doEval);
                   1001:
                   1002:     if (t == EndOfFile) {
                   1003:        /*
                   1004:         * If we reached the end of the expression, the expression
                   1005:         * is malformed...
                   1006:         */
                   1007:        t = Err;
                   1008:     } else if (t == LParen) {
                   1009:        /*
                   1010:         * T -> ( E )
                   1011:         */
                   1012:        t = CondE(doEval);
                   1013:        if (t != Err) {
                   1014:            if (CondToken(doEval) != RParen) {
                   1015:                t = Err;
                   1016:            }
                   1017:        }
                   1018:     } else if (t == Not) {
                   1019:        t = CondT(doEval);
                   1020:        if (t == True) {
                   1021:            t = False;
                   1022:        } else if (t == False) {
                   1023:            t = True;
                   1024:        }
                   1025:     }
                   1026:     return (t);
                   1027: }
                   1028: 
                   1029: /*-
                   1030:  *-----------------------------------------------------------------------
                   1031:  * CondF --
                   1032:  *     Parse a conjunctive factor (nice name, wot?)
                   1033:  *         F -> T && F | T
                   1034:  *
                   1035:  * Results:
                   1036:  *     True, False or Err
                   1037:  *
                   1038:  * Side Effects:
                   1039:  *     Tokens are consumed.
                   1040:  *
                   1041:  *-----------------------------------------------------------------------
                   1042:  */
                   1043: static Token
1.13      wiz      1044: CondF(Boolean doEval)
1.1       cgd      1045: {
                   1046:     Token   l, o;
                   1047:
                   1048:     l = CondT(doEval);
                   1049:     if (l != Err) {
                   1050:        o = CondToken(doEval);
                   1051:
                   1052:        if (o == And) {
                   1053:            /*
                   1054:             * F -> T && F
                   1055:             *
                   1056:             * If T is False, the whole thing will be False, but we have to
                   1057:             * parse the r.h.s. anyway (to throw it away).
                   1058:             * If T is True, the result is the r.h.s., be it an Err or no.
                   1059:             */
                   1060:            if (l == True) {
                   1061:                l = CondF(doEval);
                   1062:            } else {
                   1063:                (void) CondF(FALSE);
                   1064:            }
                   1065:        } else {
                   1066:            /*
                   1067:             * F -> T
                   1068:             */
                   1069:            CondPushBack (o);
                   1070:        }
                   1071:     }
                   1072:     return (l);
                   1073: }
                   1074: 
                   1075: /*-
                   1076:  *-----------------------------------------------------------------------
                   1077:  * CondE --
                   1078:  *     Main expression production.
                   1079:  *         E -> F || E | F
                   1080:  *
                   1081:  * Results:
                   1082:  *     True, False or Err.
                   1083:  *
                   1084:  * Side Effects:
                   1085:  *     Tokens are, of course, consumed.
                   1086:  *
                   1087:  *-----------------------------------------------------------------------
                   1088:  */
                   1089: static Token
1.13      wiz      1090: CondE(Boolean doEval)
1.1       cgd      1091: {
                   1092:     Token   l, o;
                   1093:
                   1094:     l = CondF(doEval);
                   1095:     if (l != Err) {
                   1096:        o = CondToken(doEval);
                   1097:
                   1098:        if (o == Or) {
                   1099:            /*
                   1100:             * E -> F || E
                   1101:             *
                   1102:             * A similar thing occurs for ||, except that here we make sure
                   1103:             * the l.h.s. is False before we bother to evaluate the r.h.s.
                   1104:             * Once again, if l is False, the result is the r.h.s. and once
                   1105:             * again if l is True, we parse the r.h.s. to throw it away.
                   1106:             */
                   1107:            if (l == False) {
                   1108:                l = CondE(doEval);
                   1109:            } else {
                   1110:                (void) CondE(FALSE);
                   1111:            }
                   1112:        } else {
                   1113:            /*
                   1114:             * E -> F
                   1115:             */
                   1116:            CondPushBack (o);
                   1117:        }
                   1118:     }
                   1119:     return (l);
                   1120: }
1.10      christos 1121:
                   1122: /*-
                   1123:  *-----------------------------------------------------------------------
                   1124:  * Cond_EvalExpression --
                   1125:  *     Evaluate an expression in the passed line. The expression
                   1126:  *     consists of &&, ||, !, make(target), defined(variable)
                   1127:  *     and parenthetical groupings thereof.
                   1128:  *
                   1129:  * Results:
                   1130:  *     COND_PARSE      if the condition was valid grammatically
                   1131:  *     COND_INVALID    if not a valid conditional.
                   1132:  *
                   1133:  *     (*value) is set to the boolean value of the condition
                   1134:  *
                   1135:  * Side Effects:
                   1136:  *     None.
                   1137:  *
                   1138:  *-----------------------------------------------------------------------
                   1139:  */
                   1140: int
1.13      wiz      1141: Cond_EvalExpression(int dosetup, char *line, Boolean *value, int eprint)
1.10      christos 1142: {
1.11      christos 1143:     if (dosetup) {
                   1144:        condDefProc = CondDoDefined;
                   1145:        condInvert = 0;
                   1146:     }
1.10      christos 1147:
1.11      christos 1148:     while (*line == ' ' || *line == '\t')
                   1149:        line++;
1.10      christos 1150:
1.11      christos 1151:     condExpr = line;
                   1152:     condPushBack = None;
1.10      christos 1153:
1.11      christos 1154:     switch (CondE(TRUE)) {
                   1155:     case True:
                   1156:        if (CondToken(TRUE) == EndOfFile) {
                   1157:            *value = TRUE;
                   1158:            break;
                   1159:        }
                   1160:        goto err;
                   1161:        /*FALLTHRU*/
                   1162:     case False:
                   1163:        if (CondToken(TRUE) == EndOfFile) {
                   1164:            *value = FALSE;
                   1165:            break;
                   1166:        }
                   1167:        /*FALLTHRU*/
                   1168:     case Err:
1.10      christos 1169: err:
1.11      christos 1170:        if (eprint)
                   1171:            Parse_Error (PARSE_FATAL, "Malformed conditional (%s)",
                   1172:                         line);
                   1173:        return (COND_INVALID);
                   1174:     default:
                   1175:        break;
                   1176:     }
                   1177:
                   1178:     return COND_PARSE;
1.10      christos 1179: }
                   1180:
1.1       cgd      1181: 
                   1182: /*-
                   1183:  *-----------------------------------------------------------------------
                   1184:  * Cond_Eval --
                   1185:  *     Evaluate the conditional in the passed line. The line
                   1186:  *     looks like this:
                   1187:  *         #<cond-type> <expr>
                   1188:  *     where <cond-type> is any of if, ifmake, ifnmake, ifdef,
                   1189:  *     ifndef, elif, elifmake, elifnmake, elifdef, elifndef
                   1190:  *     and <expr> consists of &&, ||, !, make(target), defined(variable)
                   1191:  *     and parenthetical groupings thereof.
                   1192:  *
1.13      wiz      1193:  * Input:
                   1194:  *     line            Line to parse
                   1195:  *
1.1       cgd      1196:  * Results:
                   1197:  *     COND_PARSE      if should parse lines after the conditional
                   1198:  *     COND_SKIP       if should skip lines after the conditional
                   1199:  *     COND_INVALID    if not a valid conditional.
                   1200:  *
                   1201:  * Side Effects:
                   1202:  *     None.
                   1203:  *
                   1204:  *-----------------------------------------------------------------------
                   1205:  */
1.4       cgd      1206: int
1.13      wiz      1207: Cond_Eval(char *line)
1.1       cgd      1208: {
                   1209:     struct If      *ifp;
                   1210:     Boolean        isElse;
1.4       cgd      1211:     Boolean        value = FALSE;
1.1       cgd      1212:     int                    level;      /* Level at which to report errors. */
                   1213:
                   1214:     level = PARSE_FATAL;
                   1215:
                   1216:     for (line++; *line == ' ' || *line == '\t'; line++) {
                   1217:        continue;
                   1218:     }
                   1219:
                   1220:     /*
                   1221:      * Find what type of if we're dealing with. The result is left
                   1222:      * in ifp and isElse is set TRUE if it's an elif line.
                   1223:      */
                   1224:     if (line[0] == 'e' && line[1] == 'l') {
                   1225:        line += 2;
                   1226:        isElse = TRUE;
                   1227:     } else if (strncmp (line, "endif", 5) == 0) {
                   1228:        /*
                   1229:         * End of a conditional section. If skipIfLevel is non-zero, that
                   1230:         * conditional was skipped, so lines following it should also be
                   1231:         * skipped. Hence, we return COND_SKIP. Otherwise, the conditional
                   1232:         * was read so succeeding lines should be parsed (think about it...)
                   1233:         * so we return COND_PARSE, unless this endif isn't paired with
                   1234:         * a decent if.
                   1235:         */
1.22    ! sjg      1236:        finalElse[condTop][skipIfLevel] = FALSE;
1.1       cgd      1237:        if (skipIfLevel != 0) {
                   1238:            skipIfLevel -= 1;
                   1239:            return (COND_SKIP);
                   1240:        } else {
                   1241:            if (condTop == MAXIF) {
                   1242:                Parse_Error (level, "if-less endif");
                   1243:                return (COND_INVALID);
                   1244:            } else {
                   1245:                skipLine = FALSE;
                   1246:                condTop += 1;
                   1247:                return (COND_PARSE);
                   1248:            }
                   1249:        }
                   1250:     } else {
                   1251:        isElse = FALSE;
                   1252:     }
1.7       christos 1253:
1.1       cgd      1254:     /*
                   1255:      * Figure out what sort of conditional it is -- what its default
                   1256:      * function is, etc. -- by looking in the table of valid "ifs"
                   1257:      */
                   1258:     for (ifp = ifs; ifp->form != (char *)0; ifp++) {
                   1259:        if (strncmp (ifp->form, line, ifp->formlen) == 0) {
                   1260:            break;
                   1261:        }
                   1262:     }
                   1263:
                   1264:     if (ifp->form == (char *) 0) {
                   1265:        /*
                   1266:         * Nothing fit. If the first word on the line is actually
                   1267:         * "else", it's a valid conditional whose value is the inverse
                   1268:         * of the previous if we parsed.
                   1269:         */
                   1270:        if (isElse && (line[0] == 's') && (line[1] == 'e')) {
1.22    ! sjg      1271:            if (finalElse[condTop][skipIfLevel]) {
        !          1272:                Parse_Error (PARSE_WARNING, "extra else");
        !          1273:            } else {
        !          1274:                finalElse[condTop][skipIfLevel] = TRUE;
        !          1275:            }
1.1       cgd      1276:            if (condTop == MAXIF) {
                   1277:                Parse_Error (level, "if-less else");
                   1278:                return (COND_INVALID);
                   1279:            } else if (skipIfLevel == 0) {
                   1280:                value = !condStack[condTop];
                   1281:            } else {
                   1282:                return (COND_SKIP);
                   1283:            }
                   1284:        } else {
                   1285:            /*
                   1286:             * Not a valid conditional type. No error...
                   1287:             */
                   1288:            return (COND_INVALID);
                   1289:        }
                   1290:     } else {
                   1291:        if (isElse) {
                   1292:            if (condTop == MAXIF) {
                   1293:                Parse_Error (level, "if-less elif");
                   1294:                return (COND_INVALID);
                   1295:            } else if (skipIfLevel != 0) {
                   1296:                /*
                   1297:                 * If skipping this conditional, just ignore the whole thing.
                   1298:                 * If we don't, the user might be employing a variable that's
                   1299:                 * undefined, for which there's an enclosing ifdef that
                   1300:                 * we're skipping...
                   1301:                 */
                   1302:                return(COND_SKIP);
                   1303:            }
                   1304:        } else if (skipLine) {
                   1305:            /*
                   1306:             * Don't even try to evaluate a conditional that's not an else if
                   1307:             * we're skipping things...
                   1308:             */
                   1309:            skipIfLevel += 1;
1.22    ! sjg      1310:            if (skipIfLevel >= MAXIF) {
        !          1311:                Parse_Error (PARSE_FATAL, "Too many nested if's. %d max.", MAXIF);
        !          1312:                return (COND_INVALID);
        !          1313:            }
        !          1314:            finalElse[condTop][skipIfLevel] = FALSE;
1.1       cgd      1315:            return(COND_SKIP);
                   1316:        }
                   1317:
                   1318:        /*
                   1319:         * Initialize file-global variables for parsing
                   1320:         */
                   1321:        condDefProc = ifp->defProc;
                   1322:        condInvert = ifp->doNot;
1.7       christos 1323:
1.1       cgd      1324:        line += ifp->formlen;
1.11      christos 1325:        if (Cond_EvalExpression(0, line, &value, 1) == COND_INVALID)
1.10      christos 1326:                return COND_INVALID;
1.1       cgd      1327:     }
                   1328:     if (!isElse) {
                   1329:        condTop -= 1;
1.22    ! sjg      1330:        finalElse[condTop][skipIfLevel] = FALSE;
1.1       cgd      1331:     } else if ((skipIfLevel != 0) || condStack[condTop]) {
                   1332:        /*
                   1333:         * If this is an else-type conditional, it should only take effect
                   1334:         * if its corresponding if was evaluated and FALSE. If its if was
                   1335:         * TRUE or skipped, we return COND_SKIP (and start skipping in case
                   1336:         * we weren't already), leaving the stack unmolested so later elif's
                   1337:         * don't screw up...
                   1338:         */
                   1339:        skipLine = TRUE;
                   1340:        return (COND_SKIP);
                   1341:     }
                   1342:
                   1343:     if (condTop < 0) {
                   1344:        /*
                   1345:         * This is the one case where we can definitely proclaim a fatal
                   1346:         * error. If we don't, we're hosed.
                   1347:         */
                   1348:        Parse_Error (PARSE_FATAL, "Too many nested if's. %d max.", MAXIF);
                   1349:        return (COND_INVALID);
                   1350:     } else {
                   1351:        condStack[condTop] = value;
                   1352:        skipLine = !value;
                   1353:        return (value ? COND_PARSE : COND_SKIP);
                   1354:     }
                   1355: }
1.10      christos 1356:
                   1357:
1.1       cgd      1358: 
                   1359: /*-
                   1360:  *-----------------------------------------------------------------------
                   1361:  * Cond_End --
                   1362:  *     Make sure everything's clean at the end of a makefile.
                   1363:  *
                   1364:  * Results:
                   1365:  *     None.
                   1366:  *
                   1367:  * Side Effects:
                   1368:  *     Parse_Error will be called if open conditionals are around.
                   1369:  *
                   1370:  *-----------------------------------------------------------------------
                   1371:  */
                   1372: void
1.13      wiz      1373: Cond_End(void)
1.1       cgd      1374: {
                   1375:     if (condTop != MAXIF) {
                   1376:        Parse_Error(PARSE_FATAL, "%d open conditional%s", MAXIF-condTop,
                   1377:                    MAXIF-condTop == 1 ? "" : "s");
                   1378:     }
                   1379:     condTop = MAXIF;
                   1380: }

CVSweb <webmaster@jp.NetBSD.org>