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

1.64    ! joerg       1: /*     $NetBSD: cond.c,v 1.63 2012/05/21 06:30:02 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.24      ross       72: #ifndef MAKE_NATIVE
1.64    ! joerg      73: static char rcsid[] = "$NetBSD: cond.c,v 1.63 2012/05/21 06:30:02 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.64    ! joerg      80: __RCSID("$NetBSD: cond.c,v 1.63 2012/05/21 06:30:02 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.46      dsl        95: #include    <errno.h>    /* For strtoul() error checking */
1.13      wiz        96:
1.1       cgd        97: #include    "make.h"
1.4       cgd        98: #include    "hash.h"
                     99: #include    "dir.h"
                    100: #include    "buf.h"
1.1       cgd       101:
                    102: /*
                    103:  * The parsing of conditional expressions is based on this grammar:
                    104:  *     E -> F || E
                    105:  *     E -> F
                    106:  *     F -> T && F
                    107:  *     F -> T
                    108:  *     T -> defined(variable)
                    109:  *     T -> make(target)
                    110:  *     T -> exists(file)
                    111:  *     T -> empty(varspec)
                    112:  *     T -> target(name)
1.12      christos  113:  *     T -> commands(name)
1.1       cgd       114:  *     T -> symbol
                    115:  *     T -> $(varspec) op value
                    116:  *     T -> $(varspec) == "string"
                    117:  *     T -> $(varspec) != "string"
1.23      sjg       118:  *     T -> "string"
1.1       cgd       119:  *     T -> ( E )
                    120:  *     T -> ! T
                    121:  *     op -> == | != | > | < | >= | <=
                    122:  *
                    123:  * 'symbol' is some other symbol to which the default function (condDefProc)
                    124:  * is applied.
                    125:  *
                    126:  * Tokens are scanned from the 'condExpr' string. The scanner (CondToken)
1.60      dsl       127:  * will return TOK_AND for '&' and '&&', TOK_OR for '|' and '||',
                    128:  * TOK_NOT for '!', TOK_LPAREN for '(', TOK_RPAREN for ')' and will evaluate
                    129:  * the other terminal symbols, using either the default function or the
                    130:  * function given in the terminal, and return the result as either TOK_TRUE
                    131:  * or TOK_FALSE.
1.1       cgd       132:  *
1.60      dsl       133:  * TOK_FALSE is 0 and TOK_TRUE 1 so we can directly assign C comparisons.
                    134:  *
                    135:  * All Non-Terminal functions (CondE, CondF and CondT) return TOK_ERROR on
                    136:  * error.
1.1       cgd       137:  */
                    138: typedef enum {
1.60      dsl       139:     TOK_FALSE = 0, TOK_TRUE = 1, TOK_AND, TOK_OR, TOK_NOT,
                    140:     TOK_LPAREN, TOK_RPAREN, TOK_EOF, TOK_NONE, TOK_ERROR
1.1       cgd       141: } Token;
                    142:
                    143: /*-
                    144:  * Structures to handle elegantly the different forms of #if's. The
                    145:  * last two fields are stored in condInvert and condDefProc, respectively.
                    146:  */
1.13      wiz       147: static void CondPushBack(Token);
1.56      dsl       148: static int CondGetArg(char **, char **, const char *);
1.55      dsl       149: static Boolean CondDoDefined(int, const char *);
                    150: static int CondStrMatch(const void *, const void *);
                    151: static Boolean CondDoMake(int, const char *);
                    152: static Boolean CondDoExists(int, const char *);
                    153: static Boolean CondDoTarget(int, const char *);
                    154: static Boolean CondDoCommands(int, const char *);
1.46      dsl       155: static Boolean CondCvtArg(char *, double *);
1.13      wiz       156: static Token CondToken(Boolean);
                    157: static Token CondT(Boolean);
                    158: static Token CondF(Boolean);
                    159: static Token CondE(Boolean);
1.56      dsl       160: static int do_Cond_EvalExpression(Boolean *);
1.1       cgd       161:
1.36      dsl       162: static const struct If {
1.16      christos  163:     const char *form;        /* Form of if */
1.41      christos  164:     int                formlen;      /* Length of form */
1.1       cgd       165:     Boolean    doNot;        /* TRUE if default function should be negated */
1.55      dsl       166:     Boolean    (*defProc)(int, const char *); /* Default function to apply */
1.1       cgd       167: } ifs[] = {
1.36      dsl       168:     { "def",     3,      FALSE,  CondDoDefined },
                    169:     { "ndef",    4,      TRUE,   CondDoDefined },
                    170:     { "make",    4,      FALSE,  CondDoMake },
                    171:     { "nmake",   5,      TRUE,   CondDoMake },
                    172:     { "",        0,      FALSE,  CondDoDefined },
1.7       christos  173:     { NULL,      0,      FALSE,  NULL }
1.1       cgd       174: };
                    175:
1.56      dsl       176: static const struct If *if_info;        /* Info for current statement */
1.1       cgd       177: static char      *condExpr;            /* The expression to parse */
1.59      dsl       178: static Token     condPushBack=TOK_NONE;        /* Single push-back token used in
1.1       cgd       179:                                         * parsing */
                    180:
1.37      dsl       181: static unsigned int    cond_depth = 0;         /* current .if nesting level */
                    182: static unsigned int    cond_min_depth = 0;     /* depth at makefile open */
1.1       cgd       183:
1.26      christos  184: static int
                    185: istoken(const char *str, const char *tok, size_t len)
                    186: {
                    187:        return strncmp(str, tok, len) == 0 && !isalpha((unsigned char)str[len]);
                    188: }
                    189:
1.1       cgd       190: /*-
                    191:  *-----------------------------------------------------------------------
                    192:  * CondPushBack --
                    193:  *     Push back the most recent token read. We only need one level of
                    194:  *     this, so the thing is just stored in 'condPushback'.
                    195:  *
1.13      wiz       196:  * Input:
                    197:  *     t               Token to push back into the "stream"
                    198:  *
1.1       cgd       199:  * Results:
                    200:  *     None.
                    201:  *
                    202:  * Side Effects:
                    203:  *     condPushback is overwritten.
                    204:  *
                    205:  *-----------------------------------------------------------------------
                    206:  */
                    207: static void
1.13      wiz       208: CondPushBack(Token t)
1.1       cgd       209: {
                    210:     condPushBack = t;
                    211: }
                    212: 
                    213: /*-
                    214:  *-----------------------------------------------------------------------
                    215:  * CondGetArg --
                    216:  *     Find the argument of a built-in function.
                    217:  *
1.13      wiz       218:  * Input:
                    219:  *     parens          TRUE if arg should be bounded by parens
                    220:  *
1.1       cgd       221:  * Results:
                    222:  *     The length of the argument and the address of the argument.
                    223:  *
                    224:  * Side Effects:
                    225:  *     The pointer is set to point to the closing parenthesis of the
                    226:  *     function call.
                    227:  *
                    228:  *-----------------------------------------------------------------------
                    229:  */
                    230: static int
1.56      dsl       231: CondGetArg(char **linePtr, char **argPtr, const char *func)
1.1       cgd       232: {
1.13      wiz       233:     char         *cp;
1.41      christos  234:     int                  argLen;
1.13      wiz       235:     Buffer       buf;
1.56      dsl       236:     int           paren_depth;
                    237:     char          ch;
1.1       cgd       238:
                    239:     cp = *linePtr;
1.56      dsl       240:     if (func != NULL)
                    241:        /* Skip opening '(' - verfied by caller */
                    242:        cp++;
1.1       cgd       243:
                    244:     if (*cp == '\0') {
                    245:        /*
                    246:         * No arguments whatsoever. Because 'make' and 'defined' aren't really
                    247:         * "reserved words", we don't print a message. I think this is better
                    248:         * than hitting the user with a warning message every time s/he uses
                    249:         * the word 'make' or 'defined' at the beginning of a symbol...
                    250:         */
1.30      christos  251:        *argPtr = NULL;
1.1       cgd       252:        return (0);
                    253:     }
                    254:
                    255:     while (*cp == ' ' || *cp == '\t') {
                    256:        cp++;
                    257:     }
                    258:
                    259:     /*
                    260:      * Create a buffer for the argument and start it out at 16 characters
                    261:      * long. Why 16? Why not?
                    262:      */
1.50      dsl       263:     Buf_Init(&buf, 16);
1.7       christos  264:
1.56      dsl       265:     paren_depth = 0;
                    266:     for (;;) {
                    267:        ch = *cp;
                    268:        if (ch == 0 || ch == ' ' || ch == '\t')
                    269:            break;
                    270:        if ((ch == '&' || ch == '|') && paren_depth == 0)
                    271:            break;
1.1       cgd       272:        if (*cp == '$') {
                    273:            /*
                    274:             * Parse the variable spec and install it as part of the argument
                    275:             * if it's valid. We tell Var_Parse to complain on an undefined
                    276:             * variable, so we don't do it too. Nor do we return an error,
                    277:             * though perhaps we should...
                    278:             */
                    279:            char        *cp2;
1.41      christos  280:            int         len;
1.30      christos  281:            void        *freeIt;
1.1       cgd       282:
1.30      christos  283:            cp2 = Var_Parse(cp, VAR_CMD, TRUE, &len, &freeIt);
1.50      dsl       284:            Buf_AddBytes(&buf, strlen(cp2), cp2);
1.30      christos  285:            if (freeIt)
                    286:                free(freeIt);
1.1       cgd       287:            cp += len;
1.56      dsl       288:            continue;
1.1       cgd       289:        }
1.56      dsl       290:        if (ch == '(')
                    291:            paren_depth++;
                    292:        else
                    293:            if (ch == ')' && --paren_depth < 0)
                    294:                break;
                    295:        Buf_AddByte(&buf, *cp);
                    296:        cp++;
1.1       cgd       297:     }
                    298:
1.50      dsl       299:     *argPtr = Buf_GetAll(&buf, &argLen);
                    300:     Buf_Destroy(&buf, FALSE);
1.1       cgd       301:
                    302:     while (*cp == ' ' || *cp == '\t') {
                    303:        cp++;
                    304:     }
1.56      dsl       305:
                    306:     if (func != NULL && *cp++ != ')') {
1.25      christos  307:        Parse_Error(PARSE_WARNING, "Missing closing parenthesis for %s()",
1.1       cgd       308:                     func);
                    309:        return (0);
1.50      dsl       310:     }
                    311:
1.1       cgd       312:     *linePtr = cp;
                    313:     return (argLen);
                    314: }
                    315: 
                    316: /*-
                    317:  *-----------------------------------------------------------------------
                    318:  * CondDoDefined --
                    319:  *     Handle the 'defined' function for conditionals.
                    320:  *
                    321:  * Results:
                    322:  *     TRUE if the given variable is defined.
                    323:  *
                    324:  * Side Effects:
                    325:  *     None.
                    326:  *
                    327:  *-----------------------------------------------------------------------
                    328:  */
                    329: static Boolean
1.64    ! joerg     330: CondDoDefined(int argLen MAKE_ATTR_UNUSED, const char *arg)
1.1       cgd       331: {
1.5       jtc       332:     char    *p1;
1.1       cgd       333:     Boolean result;
                    334:
1.29      christos  335:     if (Var_Value(arg, VAR_CMD, &p1) != NULL) {
1.1       cgd       336:        result = TRUE;
                    337:     } else {
                    338:        result = FALSE;
                    339:     }
1.5       jtc       340:     if (p1)
                    341:        free(p1);
1.1       cgd       342:     return (result);
                    343: }
                    344: 
                    345: /*-
                    346:  *-----------------------------------------------------------------------
                    347:  * CondStrMatch --
                    348:  *     Front-end for Str_Match so it returns 0 on match and non-zero
                    349:  *     on mismatch. Callback function for CondDoMake via Lst_Find
                    350:  *
                    351:  * Results:
                    352:  *     0 if string matches pattern
                    353:  *
                    354:  * Side Effects:
                    355:  *     None
                    356:  *
                    357:  *-----------------------------------------------------------------------
                    358:  */
                    359: static int
1.55      dsl       360: CondStrMatch(const void *string, const void *pattern)
1.1       cgd       361: {
1.54      dsl       362:     return(!Str_Match(string, pattern));
1.1       cgd       363: }
                    364: 
                    365: /*-
                    366:  *-----------------------------------------------------------------------
                    367:  * CondDoMake --
                    368:  *     Handle the 'make' function for conditionals.
                    369:  *
                    370:  * Results:
                    371:  *     TRUE if the given target is being made.
                    372:  *
                    373:  * Side Effects:
                    374:  *     None.
                    375:  *
                    376:  *-----------------------------------------------------------------------
                    377:  */
                    378: static Boolean
1.64    ! joerg     379: CondDoMake(int argLen MAKE_ATTR_UNUSED, const char *arg)
1.1       cgd       380: {
1.53      dsl       381:     return Lst_Find(create, arg, CondStrMatch) != NULL;
1.1       cgd       382: }
                    383: 
                    384: /*-
                    385:  *-----------------------------------------------------------------------
                    386:  * CondDoExists --
                    387:  *     See if the given file exists.
                    388:  *
                    389:  * Results:
                    390:  *     TRUE if the file exists and FALSE if it does not.
                    391:  *
                    392:  * Side Effects:
                    393:  *     None.
                    394:  *
                    395:  *-----------------------------------------------------------------------
                    396:  */
                    397: static Boolean
1.64    ! joerg     398: CondDoExists(int argLen MAKE_ATTR_UNUSED, const char *arg)
1.1       cgd       399: {
                    400:     Boolean result;
                    401:     char    *path;
                    402:
                    403:     path = Dir_FindFile(arg, dirSearchPath);
1.62      sjg       404:     if (DEBUG(COND)) {
                    405:        fprintf(debug_file, "exists(%s) result is \"%s\"\n",
                    406:               arg, path ? path : "");
                    407:     }
1.29      christos  408:     if (path != NULL) {
1.1       cgd       409:        result = TRUE;
                    410:        free(path);
                    411:     } else {
                    412:        result = FALSE;
                    413:     }
                    414:     return (result);
                    415: }
                    416: 
                    417: /*-
                    418:  *-----------------------------------------------------------------------
                    419:  * CondDoTarget --
                    420:  *     See if the given node exists and is an actual target.
                    421:  *
                    422:  * Results:
                    423:  *     TRUE if the node exists as a target and FALSE if it does not.
                    424:  *
                    425:  * Side Effects:
                    426:  *     None.
                    427:  *
                    428:  *-----------------------------------------------------------------------
                    429:  */
                    430: static Boolean
1.64    ! joerg     431: CondDoTarget(int argLen MAKE_ATTR_UNUSED, const char *arg)
1.1       cgd       432: {
                    433:     GNode   *gn;
                    434:
                    435:     gn = Targ_FindNode(arg, TARG_NOCREATE);
1.53      dsl       436:     return (gn != NULL) && !OP_NOP(gn->type);
1.1       cgd       437: }
                    438:
1.12      christos  439: /*-
                    440:  *-----------------------------------------------------------------------
                    441:  * CondDoCommands --
                    442:  *     See if the given node exists and is an actual target with commands
                    443:  *     associated with it.
                    444:  *
                    445:  * Results:
                    446:  *     TRUE if the node exists as a target and has commands associated with
                    447:  *     it and FALSE if it does not.
                    448:  *
                    449:  * Side Effects:
                    450:  *     None.
                    451:  *
                    452:  *-----------------------------------------------------------------------
                    453:  */
                    454: static Boolean
1.64    ! joerg     455: CondDoCommands(int argLen MAKE_ATTR_UNUSED, const char *arg)
1.12      christos  456: {
                    457:     GNode   *gn;
                    458:
                    459:     gn = Targ_FindNode(arg, TARG_NOCREATE);
1.53      dsl       460:     return (gn != NULL) && !OP_NOP(gn->type) && !Lst_IsEmpty(gn->commands);
1.12      christos  461: }
1.1       cgd       462: 
                    463: /*-
                    464:  *-----------------------------------------------------------------------
                    465:  * CondCvtArg --
1.46      dsl       466:  *     Convert the given number into a double.
                    467:  *     We try a base 10 or 16 integer conversion first, if that fails
                    468:  *     then we try a floating point conversion instead.
1.1       cgd       469:  *
                    470:  * Results:
1.4       cgd       471:  *     Sets 'value' to double value of string.
1.46      dsl       472:  *     Returns 'true' if the convertion suceeded
1.1       cgd       473:  *
                    474:  *-----------------------------------------------------------------------
                    475:  */
1.46      dsl       476: static Boolean
1.13      wiz       477: CondCvtArg(char *str, double *value)
1.1       cgd       478: {
1.46      dsl       479:     char *eptr, ech;
                    480:     unsigned long l_val;
                    481:     double d_val;
                    482:
                    483:     errno = 0;
                    484:     l_val = strtoul(str, &eptr, str[1] == 'x' ? 16 : 10);
                    485:     ech = *eptr;
                    486:     if (ech == 0 && errno != ERANGE) {
                    487:        d_val = str[0] == '-' ? -(double)-l_val : (double)l_val;
1.19      sjg       488:     } else {
1.46      dsl       489:        if (ech != 0 && ech != '.' && ech != 'e' && ech != 'E')
                    490:            return FALSE;
                    491:        d_val = strtod(str, &eptr);
                    492:        if (*eptr)
                    493:            return FALSE;
1.1       cgd       494:     }
1.46      dsl       495:
                    496:     *value = d_val;
                    497:     return TRUE;
1.1       cgd       498: }
1.46      dsl       499:
1.1       cgd       500: /*-
                    501:  *-----------------------------------------------------------------------
1.23      sjg       502:  * CondGetString --
                    503:  *     Get a string from a variable reference or an optionally quoted
                    504:  *     string.  This is called for the lhs and rhs of string compares.
                    505:  *
                    506:  * Results:
1.30      christos  507:  *     Sets freeIt if needed,
1.23      sjg       508:  *     Sets quoted if string was quoted,
                    509:  *     Returns NULL on error,
                    510:  *     else returns string - absent any quotes.
                    511:  *
                    512:  * Side Effects:
                    513:  *     Moves condExpr to end of this token.
                    514:  *
                    515:  *
                    516:  *-----------------------------------------------------------------------
                    517:  */
1.30      christos  518: /* coverity:[+alloc : arg-*2] */
1.23      sjg       519: static char *
1.30      christos  520: CondGetString(Boolean doEval, Boolean *quoted, void **freeIt)
1.23      sjg       521: {
                    522:     Buffer buf;
                    523:     char *cp;
                    524:     char *str;
1.41      christos  525:     int        len;
1.23      sjg       526:     int qt;
                    527:     char *start;
                    528:
1.50      dsl       529:     Buf_Init(&buf, 0);
1.23      sjg       530:     str = NULL;
1.30      christos  531:     *freeIt = NULL;
1.23      sjg       532:     *quoted = qt = *condExpr == '"' ? 1 : 0;
                    533:     if (qt)
                    534:        condExpr++;
                    535:     for (start = condExpr; *condExpr && str == NULL; condExpr++) {
                    536:        switch (*condExpr) {
                    537:        case '\\':
                    538:            if (condExpr[1] != '\0') {
                    539:                condExpr++;
1.50      dsl       540:                Buf_AddByte(&buf, *condExpr);
1.23      sjg       541:            }
                    542:            break;
                    543:        case '"':
                    544:            if (qt) {
                    545:                condExpr++;             /* we don't want the quotes */
                    546:                goto got_str;
                    547:            } else
1.50      dsl       548:                Buf_AddByte(&buf, *condExpr); /* likely? */
1.23      sjg       549:            break;
                    550:        case ')':
                    551:        case '!':
                    552:        case '=':
                    553:        case '>':
                    554:        case '<':
                    555:        case ' ':
                    556:        case '\t':
                    557:            if (!qt)
                    558:                goto got_str;
                    559:            else
1.50      dsl       560:                Buf_AddByte(&buf, *condExpr);
1.23      sjg       561:            break;
                    562:        case '$':
                    563:            /* if we are in quotes, then an undefined variable is ok */
                    564:            str = Var_Parse(condExpr, VAR_CMD, (qt ? 0 : doEval),
1.30      christos  565:                            &len, freeIt);
1.23      sjg       566:            if (str == var_Error) {
1.30      christos  567:                if (*freeIt) {
                    568:                    free(*freeIt);
                    569:                    *freeIt = NULL;
                    570:                }
1.23      sjg       571:                /*
                    572:                 * Even if !doEval, we still report syntax errors, which
                    573:                 * is what getting var_Error back with !doEval means.
                    574:                 */
                    575:                str = NULL;
                    576:                goto cleanup;
                    577:            }
                    578:            condExpr += len;
                    579:            /*
                    580:             * If the '$' was first char (no quotes), and we are
                    581:             * followed by space, the operator or end of expression,
                    582:             * we are done.
                    583:             */
                    584:            if ((condExpr == start + len) &&
                    585:                (*condExpr == '\0' ||
                    586:                 isspace((unsigned char) *condExpr) ||
                    587:                 strchr("!=><)", *condExpr))) {
                    588:                goto cleanup;
                    589:            }
                    590:            /*
                    591:             * Nope, we better copy str to buf
                    592:             */
                    593:            for (cp = str; *cp; cp++) {
1.50      dsl       594:                Buf_AddByte(&buf, *cp);
1.23      sjg       595:            }
1.30      christos  596:            if (*freeIt) {
                    597:                free(*freeIt);
                    598:                *freeIt = NULL;
                    599:            }
1.23      sjg       600:            str = NULL;                 /* not finished yet */
                    601:            condExpr--;                 /* don't skip over next char */
                    602:            break;
                    603:        default:
1.50      dsl       604:            Buf_AddByte(&buf, *condExpr);
1.23      sjg       605:            break;
                    606:        }
                    607:     }
                    608:  got_str:
1.50      dsl       609:     str = Buf_GetAll(&buf, NULL);
1.30      christos  610:     *freeIt = str;
1.23      sjg       611:  cleanup:
1.50      dsl       612:     Buf_Destroy(&buf, FALSE);
1.23      sjg       613:     return str;
                    614: }
                    615: 
                    616: /*-
                    617:  *-----------------------------------------------------------------------
1.1       cgd       618:  * CondToken --
                    619:  *     Return the next token from the input.
                    620:  *
                    621:  * Results:
                    622:  *     A Token for the next lexical token in the stream.
                    623:  *
                    624:  * Side Effects:
1.59      dsl       625:  *     condPushback will be set back to TOK_NONE if it is used.
1.1       cgd       626:  *
                    627:  *-----------------------------------------------------------------------
                    628:  */
                    629: static Token
1.44      dsl       630: compare_expression(Boolean doEval)
                    631: {
                    632:     Token      t;
                    633:     char       *lhs;
                    634:     char       *rhs;
                    635:     char       *op;
                    636:     void       *lhsFree;
                    637:     void       *rhsFree;
                    638:     Boolean lhsQuoted;
                    639:     Boolean rhsQuoted;
1.58      dsl       640:     double     left, right;
1.44      dsl       641:
1.59      dsl       642:     t = TOK_ERROR;
1.44      dsl       643:     rhs = NULL;
                    644:     lhsFree = rhsFree = FALSE;
                    645:     lhsQuoted = rhsQuoted = FALSE;
                    646:
                    647:     /*
                    648:      * Parse the variable spec and skip over it, saving its
                    649:      * value in lhs.
                    650:      */
                    651:     lhs = CondGetString(doEval, &lhsQuoted, &lhsFree);
1.58      dsl       652:     if (!lhs)
                    653:        goto done;
                    654:
1.44      dsl       655:     /*
                    656:      * Skip whitespace to get to the operator
                    657:      */
                    658:     while (isspace((unsigned char) *condExpr))
                    659:        condExpr++;
                    660:
                    661:     /*
                    662:      * Make sure the operator is a valid one. If it isn't a
                    663:      * known relational operator, pretend we got a
                    664:      * != 0 comparison.
                    665:      */
                    666:     op = condExpr;
                    667:     switch (*condExpr) {
                    668:        case '!':
                    669:        case '=':
                    670:        case '<':
                    671:        case '>':
                    672:            if (condExpr[1] == '=') {
                    673:                condExpr += 2;
                    674:            } else {
                    675:                condExpr += 1;
                    676:            }
                    677:            break;
                    678:        default:
1.58      dsl       679:            if (!doEval) {
1.59      dsl       680:                t = TOK_FALSE;
1.58      dsl       681:                goto done;
                    682:            }
                    683:            /* For .ifxxx "..." check for non-empty string. */
                    684:            if (lhsQuoted) {
1.60      dsl       685:                t = lhs[0] != 0;
1.58      dsl       686:                goto done;
                    687:            }
                    688:            /* For .ifxxx <number> compare against zero */
                    689:            if (CondCvtArg(lhs, &left)) {
1.60      dsl       690:                t = left != 0.0;
1.58      dsl       691:                goto done;
                    692:            }
                    693:            /* For .if ${...} check for non-empty string (defProc is ifdef). */
                    694:            if (if_info->form[0] == 0) {
1.60      dsl       695:                t = lhs[0] != 0;
1.58      dsl       696:                goto done;
                    697:            }
                    698:            /* Otherwise action default test ... */
1.60      dsl       699:            t = if_info->defProc(strlen(lhs), lhs) != if_info->doNot;
1.58      dsl       700:            goto done;
                    701:     }
1.44      dsl       702:
1.58      dsl       703:     while (isspace((unsigned char)*condExpr))
1.44      dsl       704:        condExpr++;
1.58      dsl       705:
1.44      dsl       706:     if (*condExpr == '\0') {
                    707:        Parse_Error(PARSE_WARNING,
                    708:                    "Missing right-hand-side of operator");
1.58      dsl       709:        goto done;
1.44      dsl       710:     }
1.58      dsl       711:
1.44      dsl       712:     rhs = CondGetString(doEval, &rhsQuoted, &rhsFree);
1.58      dsl       713:     if (!rhs)
                    714:        goto done;
                    715:
1.44      dsl       716:     if (rhsQuoted || lhsQuoted) {
                    717: do_string_compare:
                    718:        if (((*op != '!') && (*op != '=')) || (op[1] != '=')) {
                    719:            Parse_Error(PARSE_WARNING,
                    720:     "String comparison operator should be either == or !=");
1.58      dsl       721:            goto done;
1.44      dsl       722:        }
                    723:
                    724:        if (DEBUG(COND)) {
                    725:            fprintf(debug_file, "lhs = \"%s\", rhs = \"%s\", op = %.2s\n",
                    726:                   lhs, rhs, op);
                    727:        }
                    728:        /*
                    729:         * Null-terminate rhs and perform the comparison.
                    730:         * t is set to the result.
                    731:         */
                    732:        if (*op == '=') {
1.60      dsl       733:            t = strcmp(lhs, rhs) == 0;
1.44      dsl       734:        } else {
1.60      dsl       735:            t = strcmp(lhs, rhs) != 0;
1.44      dsl       736:        }
                    737:     } else {
                    738:        /*
                    739:         * rhs is either a float or an integer. Convert both the
                    740:         * lhs and the rhs to a double and compare the two.
                    741:         */
                    742:
1.46      dsl       743:        if (!CondCvtArg(lhs, &left) || !CondCvtArg(rhs, &right))
1.44      dsl       744:            goto do_string_compare;
                    745:
                    746:        if (DEBUG(COND)) {
                    747:            fprintf(debug_file, "left = %f, right = %f, op = %.2s\n", left,
                    748:                   right, op);
                    749:        }
                    750:        switch(op[0]) {
                    751:        case '!':
                    752:            if (op[1] != '=') {
                    753:                Parse_Error(PARSE_WARNING,
                    754:                            "Unknown operator");
1.58      dsl       755:                goto done;
1.44      dsl       756:            }
1.60      dsl       757:            t = (left != right);
1.44      dsl       758:            break;
                    759:        case '=':
                    760:            if (op[1] != '=') {
                    761:                Parse_Error(PARSE_WARNING,
                    762:                            "Unknown operator");
1.58      dsl       763:                goto done;
1.44      dsl       764:            }
1.60      dsl       765:            t = (left == right);
1.44      dsl       766:            break;
                    767:        case '<':
                    768:            if (op[1] == '=') {
1.60      dsl       769:                t = (left <= right);
1.44      dsl       770:            } else {
1.60      dsl       771:                t = (left < right);
1.44      dsl       772:            }
                    773:            break;
                    774:        case '>':
                    775:            if (op[1] == '=') {
1.60      dsl       776:                t = (left >= right);
1.44      dsl       777:            } else {
1.60      dsl       778:                t = (left > right);
1.44      dsl       779:            }
                    780:            break;
                    781:        }
                    782:     }
1.58      dsl       783:
                    784: done:
1.44      dsl       785:     if (lhsFree)
                    786:        free(lhsFree);
                    787:     if (rhsFree)
                    788:        free(rhsFree);
                    789:     return t;
                    790: }
                    791:
1.47      dsl       792: static int
1.64    ! joerg     793: get_mpt_arg(char **linePtr, char **argPtr, const char *func MAKE_ATTR_UNUSED)
1.47      dsl       794: {
                    795:     /*
                    796:      * Use Var_Parse to parse the spec in parens and return
1.59      dsl       797:      * TOK_TRUE if the resulting string is empty.
1.47      dsl       798:      */
                    799:     int            length;
                    800:     void    *freeIt;
                    801:     char    *val;
                    802:     char    *cp = *linePtr;
                    803:
                    804:     /* We do all the work here and return the result as the length */
                    805:     *argPtr = NULL;
                    806:
                    807:     val = Var_Parse(cp - 1, VAR_CMD, FALSE, &length, &freeIt);
                    808:     /*
                    809:      * Advance *linePtr to beyond the closing ). Note that
                    810:      * we subtract one because 'length' is calculated from 'cp - 1'.
                    811:      */
                    812:     *linePtr = cp - 1 + length;
                    813:
                    814:     if (val == var_Error) {
                    815:        free(freeIt);
                    816:        return -1;
                    817:     }
                    818:
                    819:     /* A variable is empty when it just contains spaces... 4/15/92, christos */
                    820:     while (isspace(*(unsigned char *)val))
                    821:        val++;
                    822:
                    823:     /*
                    824:      * For consistency with the other functions we can't generate the
                    825:      * true/false here.
                    826:      */
                    827:     length = *val ? 2 : 1;
                    828:     if (freeIt)
                    829:        free(freeIt);
                    830:     return length;
                    831: }
                    832:
                    833: static Boolean
1.64    ! joerg     834: CondDoEmpty(int arglen, const char *arg MAKE_ATTR_UNUSED)
1.47      dsl       835: {
                    836:     return arglen == 1;
                    837: }
                    838:
1.44      dsl       839: static Token
                    840: compare_function(Boolean doEval)
                    841: {
1.47      dsl       842:     static const struct fn_def {
                    843:        const char  *fn_name;
                    844:        int         fn_name_len;
1.56      dsl       845:         int         (*fn_getarg)(char **, char **, const char *);
1.55      dsl       846:        Boolean     (*fn_proc)(int, const char *);
1.47      dsl       847:     } fn_defs[] = {
                    848:        { "defined",   7, CondGetArg, CondDoDefined },
                    849:        { "make",      4, CondGetArg, CondDoMake },
                    850:        { "exists",    6, CondGetArg, CondDoExists },
                    851:        { "empty",     5, get_mpt_arg, CondDoEmpty },
                    852:        { "target",    6, CondGetArg, CondDoTarget },
                    853:        { "commands",  8, CondGetArg, CondDoCommands },
                    854:        { NULL,        0, NULL, NULL },
                    855:     };
                    856:     const struct fn_def *fn_def;
1.44      dsl       857:     Token      t;
                    858:     char       *arg = NULL;
1.47      dsl       859:     int        arglen;
                    860:     char *cp = condExpr;
1.48      dsl       861:     char *cp1;
1.44      dsl       862:
1.47      dsl       863:     for (fn_def = fn_defs; fn_def->fn_name != NULL; fn_def++) {
                    864:        if (!istoken(cp, fn_def->fn_name, fn_def->fn_name_len))
                    865:            continue;
                    866:        cp += fn_def->fn_name_len;
                    867:        /* There can only be whitespace before the '(' */
                    868:        while (isspace(*(unsigned char *)cp))
                    869:            cp++;
                    870:        if (*cp != '(')
                    871:            break;
1.44      dsl       872:
1.56      dsl       873:        arglen = fn_def->fn_getarg(&cp, &arg, fn_def->fn_name);
1.47      dsl       874:        if (arglen <= 0) {
1.52      dsl       875:            condExpr = cp;
1.59      dsl       876:            return arglen < 0 ? TOK_ERROR : TOK_FALSE;
1.44      dsl       877:        }
1.47      dsl       878:        /* Evaluate the argument using the required function. */
1.60      dsl       879:        t = !doEval || fn_def->fn_proc(arglen, arg);
1.47      dsl       880:        if (arg)
                    881:            free(arg);
                    882:        condExpr = cp;
1.44      dsl       883:        return t;
                    884:     }
                    885:
1.47      dsl       886:     /* Push anything numeric through the compare expression */
1.48      dsl       887:     cp = condExpr;
                    888:     if (isdigit((unsigned char)cp[0]) || strchr("+-", cp[0]))
1.47      dsl       889:        return compare_expression(doEval);
                    890:
1.44      dsl       891:     /*
1.48      dsl       892:      * Most likely we have a naked token to apply the default function to.
                    893:      * However ".if a == b" gets here when the "a" is unquoted and doesn't
1.58      dsl       894:      * start with a '$'. This surprises people.
1.48      dsl       895:      * If what follows the function argument is a '=' or '!' then the syntax
                    896:      * would be invalid if we did "defined(a)" - so instead treat as an
                    897:      * expression.
                    898:      */
1.56      dsl       899:     arglen = CondGetArg(&cp, &arg, NULL);
1.48      dsl       900:     for (cp1 = cp; isspace(*(unsigned char *)cp1); cp1++)
                    901:        continue;
                    902:     if (*cp1 == '=' || *cp1 == '!')
                    903:        return compare_expression(doEval);
                    904:     condExpr = cp;
                    905:
                    906:     /*
1.58      dsl       907:      * Evaluate the argument using the default function.
                    908:      * This path always treats .if as .ifdef. To get here the character
                    909:      * after .if must have been taken literally, so the argument cannot
                    910:      * be empty - even if it contained a variable expansion.
1.44      dsl       911:      */
1.60      dsl       912:     t = !doEval || if_info->defProc(arglen, arg) != if_info->doNot;
1.44      dsl       913:     if (arg)
                    914:        free(arg);
                    915:     return t;
                    916: }
                    917:
                    918: static Token
1.13      wiz       919: CondToken(Boolean doEval)
1.1       cgd       920: {
1.47      dsl       921:     Token t;
                    922:
                    923:     t = condPushBack;
1.59      dsl       924:     if (t != TOK_NONE) {
                    925:        condPushBack = TOK_NONE;
1.47      dsl       926:        return t;
                    927:     }
                    928:
                    929:     while (*condExpr == ' ' || *condExpr == '\t') {
                    930:        condExpr++;
                    931:     }
                    932:
                    933:     switch (*condExpr) {
                    934:
                    935:     case '(':
                    936:        condExpr++;
1.59      dsl       937:        return TOK_LPAREN;
1.47      dsl       938:
                    939:     case ')':
                    940:        condExpr++;
1.59      dsl       941:        return TOK_RPAREN;
1.47      dsl       942:
                    943:     case '|':
                    944:        if (condExpr[1] == '|') {
                    945:            condExpr++;
                    946:        }
                    947:        condExpr++;
1.59      dsl       948:        return TOK_OR;
1.1       cgd       949:
1.47      dsl       950:     case '&':
                    951:        if (condExpr[1] == '&') {
1.1       cgd       952:            condExpr++;
                    953:        }
1.47      dsl       954:        condExpr++;
1.59      dsl       955:        return TOK_AND;
1.47      dsl       956:
                    957:     case '!':
                    958:        condExpr++;
1.59      dsl       959:        return TOK_NOT;
1.4       cgd       960:
1.47      dsl       961:     case '#':
                    962:     case '\n':
                    963:     case '\0':
1.59      dsl       964:        return TOK_EOF;
1.47      dsl       965:
                    966:     case '"':
                    967:     case '$':
                    968:        return compare_expression(doEval);
1.1       cgd       969:
1.47      dsl       970:     default:
                    971:        return compare_function(doEval);
1.1       cgd       972:     }
                    973: }
1.47      dsl       974:
1.1       cgd       975: /*-
                    976:  *-----------------------------------------------------------------------
                    977:  * CondT --
                    978:  *     Parse a single term in the expression. This consists of a terminal
1.59      dsl       979:  *     symbol or TOK_NOT and a terminal symbol (not including the binary
1.1       cgd       980:  *     operators):
                    981:  *         T -> defined(variable) | make(target) | exists(file) | symbol
                    982:  *         T -> ! T | ( E )
                    983:  *
                    984:  * Results:
1.59      dsl       985:  *     TOK_TRUE, TOK_FALSE or TOK_ERROR.
1.1       cgd       986:  *
                    987:  * Side Effects:
                    988:  *     Tokens are consumed.
                    989:  *
                    990:  *-----------------------------------------------------------------------
                    991:  */
                    992: static Token
1.13      wiz       993: CondT(Boolean doEval)
1.1       cgd       994: {
                    995:     Token   t;
                    996:
                    997:     t = CondToken(doEval);
                    998:
1.59      dsl       999:     if (t == TOK_EOF) {
1.1       cgd      1000:        /*
                   1001:         * If we reached the end of the expression, the expression
                   1002:         * is malformed...
                   1003:         */
1.59      dsl      1004:        t = TOK_ERROR;
                   1005:     } else if (t == TOK_LPAREN) {
1.1       cgd      1006:        /*
                   1007:         * T -> ( E )
                   1008:         */
                   1009:        t = CondE(doEval);
1.59      dsl      1010:        if (t != TOK_ERROR) {
                   1011:            if (CondToken(doEval) != TOK_RPAREN) {
                   1012:                t = TOK_ERROR;
1.1       cgd      1013:            }
                   1014:        }
1.59      dsl      1015:     } else if (t == TOK_NOT) {
1.1       cgd      1016:        t = CondT(doEval);
1.59      dsl      1017:        if (t == TOK_TRUE) {
                   1018:            t = TOK_FALSE;
                   1019:        } else if (t == TOK_FALSE) {
                   1020:            t = TOK_TRUE;
1.1       cgd      1021:        }
                   1022:     }
                   1023:     return (t);
                   1024: }
                   1025: 
                   1026: /*-
                   1027:  *-----------------------------------------------------------------------
                   1028:  * CondF --
                   1029:  *     Parse a conjunctive factor (nice name, wot?)
                   1030:  *         F -> T && F | T
                   1031:  *
                   1032:  * Results:
1.59      dsl      1033:  *     TOK_TRUE, TOK_FALSE or TOK_ERROR
1.1       cgd      1034:  *
                   1035:  * Side Effects:
                   1036:  *     Tokens are consumed.
                   1037:  *
                   1038:  *-----------------------------------------------------------------------
                   1039:  */
                   1040: static Token
1.13      wiz      1041: CondF(Boolean doEval)
1.1       cgd      1042: {
                   1043:     Token   l, o;
                   1044:
                   1045:     l = CondT(doEval);
1.59      dsl      1046:     if (l != TOK_ERROR) {
1.1       cgd      1047:        o = CondToken(doEval);
                   1048:
1.59      dsl      1049:        if (o == TOK_AND) {
1.1       cgd      1050:            /*
                   1051:             * F -> T && F
                   1052:             *
1.59      dsl      1053:             * If T is TOK_FALSE, the whole thing will be TOK_FALSE, but we have to
1.1       cgd      1054:             * parse the r.h.s. anyway (to throw it away).
1.59      dsl      1055:             * If T is TOK_TRUE, the result is the r.h.s., be it an TOK_ERROR or no.
1.1       cgd      1056:             */
1.59      dsl      1057:            if (l == TOK_TRUE) {
1.1       cgd      1058:                l = CondF(doEval);
                   1059:            } else {
1.28      christos 1060:                (void)CondF(FALSE);
1.1       cgd      1061:            }
                   1062:        } else {
                   1063:            /*
                   1064:             * F -> T
                   1065:             */
1.25      christos 1066:            CondPushBack(o);
1.1       cgd      1067:        }
                   1068:     }
                   1069:     return (l);
                   1070: }
                   1071: 
                   1072: /*-
                   1073:  *-----------------------------------------------------------------------
                   1074:  * CondE --
                   1075:  *     Main expression production.
                   1076:  *         E -> F || E | F
                   1077:  *
                   1078:  * Results:
1.59      dsl      1079:  *     TOK_TRUE, TOK_FALSE or TOK_ERROR.
1.1       cgd      1080:  *
                   1081:  * Side Effects:
                   1082:  *     Tokens are, of course, consumed.
                   1083:  *
                   1084:  *-----------------------------------------------------------------------
                   1085:  */
                   1086: static Token
1.13      wiz      1087: CondE(Boolean doEval)
1.1       cgd      1088: {
                   1089:     Token   l, o;
                   1090:
                   1091:     l = CondF(doEval);
1.59      dsl      1092:     if (l != TOK_ERROR) {
1.1       cgd      1093:        o = CondToken(doEval);
                   1094:
1.59      dsl      1095:        if (o == TOK_OR) {
1.1       cgd      1096:            /*
                   1097:             * E -> F || E
                   1098:             *
                   1099:             * A similar thing occurs for ||, except that here we make sure
1.59      dsl      1100:             * the l.h.s. is TOK_FALSE before we bother to evaluate the r.h.s.
                   1101:             * Once again, if l is TOK_FALSE, the result is the r.h.s. and once
                   1102:             * again if l is TOK_TRUE, we parse the r.h.s. to throw it away.
1.1       cgd      1103:             */
1.59      dsl      1104:            if (l == TOK_FALSE) {
1.1       cgd      1105:                l = CondE(doEval);
                   1106:            } else {
1.28      christos 1107:                (void)CondE(FALSE);
1.1       cgd      1108:            }
                   1109:        } else {
                   1110:            /*
                   1111:             * E -> F
                   1112:             */
1.25      christos 1113:            CondPushBack(o);
1.1       cgd      1114:        }
                   1115:     }
                   1116:     return (l);
                   1117: }
1.10      christos 1118:
                   1119: /*-
                   1120:  *-----------------------------------------------------------------------
                   1121:  * Cond_EvalExpression --
                   1122:  *     Evaluate an expression in the passed line. The expression
                   1123:  *     consists of &&, ||, !, make(target), defined(variable)
                   1124:  *     and parenthetical groupings thereof.
                   1125:  *
                   1126:  * Results:
                   1127:  *     COND_PARSE      if the condition was valid grammatically
                   1128:  *     COND_INVALID    if not a valid conditional.
                   1129:  *
                   1130:  *     (*value) is set to the boolean value of the condition
                   1131:  *
                   1132:  * Side Effects:
                   1133:  *     None.
                   1134:  *
                   1135:  *-----------------------------------------------------------------------
                   1136:  */
                   1137: int
1.56      dsl      1138: Cond_EvalExpression(const struct If *info, char *line, Boolean *value, int eprint)
1.10      christos 1139: {
1.56      dsl      1140:     static const struct If *dflt_info;
                   1141:     const struct If *sv_if_info = if_info;
                   1142:     char *sv_condExpr = condExpr;
                   1143:     Token sv_condPushBack = condPushBack;
                   1144:     int rval;
1.10      christos 1145:
1.11      christos 1146:     while (*line == ' ' || *line == '\t')
                   1147:        line++;
1.10      christos 1148:
1.56      dsl      1149:     if (info == NULL && (info = dflt_info) == NULL) {
                   1150:        /* Scan for the entry for .if - it can't be first */
                   1151:        for (info = ifs; ; info++)
1.57      enami    1152:            if (info->form[0] == 0)
1.56      dsl      1153:                break;
                   1154:        dflt_info = info;
                   1155:     }
                   1156:
                   1157:     if_info = info != NULL ? info : ifs + 4;
1.11      christos 1158:     condExpr = line;
1.59      dsl      1159:     condPushBack = TOK_NONE;
1.10      christos 1160:
1.56      dsl      1161:     rval = do_Cond_EvalExpression(value);
                   1162:
                   1163:     if (rval == COND_INVALID && eprint)
                   1164:        Parse_Error(PARSE_FATAL, "Malformed conditional (%s)", line);
                   1165:
                   1166:     if_info = sv_if_info;
                   1167:     condExpr = sv_condExpr;
                   1168:     condPushBack = sv_condPushBack;
                   1169:
                   1170:     return rval;
                   1171: }
                   1172:
                   1173: static int
                   1174: do_Cond_EvalExpression(Boolean *value)
                   1175: {
                   1176:
1.11      christos 1177:     switch (CondE(TRUE)) {
1.59      dsl      1178:     case TOK_TRUE:
                   1179:        if (CondToken(TRUE) == TOK_EOF) {
1.11      christos 1180:            *value = TRUE;
1.51      dsl      1181:            return COND_PARSE;
1.11      christos 1182:        }
1.51      dsl      1183:        break;
1.59      dsl      1184:     case TOK_FALSE:
                   1185:        if (CondToken(TRUE) == TOK_EOF) {
1.11      christos 1186:            *value = FALSE;
1.51      dsl      1187:            return COND_PARSE;
1.11      christos 1188:        }
1.51      dsl      1189:        break;
1.56      dsl      1190:     default:
1.59      dsl      1191:     case TOK_ERROR:
1.51      dsl      1192:        break;
1.11      christos 1193:     }
                   1194:
1.51      dsl      1195:     return COND_INVALID;
1.10      christos 1196: }
                   1197:
1.1       cgd      1198: 
                   1199: /*-
                   1200:  *-----------------------------------------------------------------------
                   1201:  * Cond_Eval --
                   1202:  *     Evaluate the conditional in the passed line. The line
                   1203:  *     looks like this:
1.36      dsl      1204:  *         .<cond-type> <expr>
1.1       cgd      1205:  *     where <cond-type> is any of if, ifmake, ifnmake, ifdef,
                   1206:  *     ifndef, elif, elifmake, elifnmake, elifdef, elifndef
                   1207:  *     and <expr> consists of &&, ||, !, make(target), defined(variable)
                   1208:  *     and parenthetical groupings thereof.
                   1209:  *
1.13      wiz      1210:  * Input:
                   1211:  *     line            Line to parse
                   1212:  *
1.1       cgd      1213:  * Results:
                   1214:  *     COND_PARSE      if should parse lines after the conditional
                   1215:  *     COND_SKIP       if should skip lines after the conditional
                   1216:  *     COND_INVALID    if not a valid conditional.
                   1217:  *
                   1218:  * Side Effects:
                   1219:  *     None.
                   1220:  *
1.36      dsl      1221:  * Note that the states IF_ACTIVE and ELSE_ACTIVE are only different in order
                   1222:  * to detect splurious .else lines (as are SKIP_TO_ELSE and SKIP_TO_ENDIF)
                   1223:  * otherwise .else could be treated as '.elif 1'.
                   1224:  *
1.1       cgd      1225:  *-----------------------------------------------------------------------
                   1226:  */
1.4       cgd      1227: int
1.13      wiz      1228: Cond_Eval(char *line)
1.1       cgd      1229: {
1.63      sjg      1230:     #define        MAXIF      128      /* maximum depth of .if'ing */
1.36      dsl      1231:     enum if_states {
                   1232:        IF_ACTIVE,              /* .if or .elif part active */
                   1233:        ELSE_ACTIVE,            /* .else part active */
                   1234:        SEARCH_FOR_ELIF,        /* searching for .elif/else to execute */
                   1235:        SKIP_TO_ELSE,           /* has been true, but not seen '.else' */
                   1236:        SKIP_TO_ENDIF           /* nothing else to execute */
                   1237:     };
                   1238:     static enum if_states cond_state[MAXIF + 1] = { IF_ACTIVE };
                   1239:
                   1240:     const struct If *ifp;
                   1241:     Boolean        isElif;
                   1242:     Boolean        value;
1.1       cgd      1243:     int                    level;      /* Level at which to report errors. */
1.36      dsl      1244:     enum if_states  state;
1.1       cgd      1245:
                   1246:     level = PARSE_FATAL;
                   1247:
1.36      dsl      1248:     /* skip leading character (the '.') and any whitespace */
                   1249:     for (line++; *line == ' ' || *line == '\t'; line++)
1.1       cgd      1250:        continue;
                   1251:
1.36      dsl      1252:     /* Find what type of if we're dealing with.  */
                   1253:     if (line[0] == 'e') {
                   1254:        if (line[1] != 'l') {
                   1255:            if (!istoken(line + 1, "ndif", 4))
                   1256:                return COND_INVALID;
                   1257:            /* End of conditional section */
1.37      dsl      1258:            if (cond_depth == cond_min_depth) {
1.36      dsl      1259:                Parse_Error(level, "if-less endif");
                   1260:                return COND_PARSE;
                   1261:            }
                   1262:            /* Return state for previous conditional */
                   1263:            cond_depth--;
1.43      dsl      1264:            if (cond_depth > MAXIF)
                   1265:                return COND_SKIP;
1.36      dsl      1266:            return cond_state[cond_depth] <= ELSE_ACTIVE ? COND_PARSE : COND_SKIP;
                   1267:        }
                   1268:
                   1269:        /* Quite likely this is 'else' or 'elif' */
1.1       cgd      1270:        line += 2;
1.36      dsl      1271:        if (istoken(line, "se", 2)) {
                   1272:            /* It is else... */
1.37      dsl      1273:            if (cond_depth == cond_min_depth) {
1.36      dsl      1274:                Parse_Error(level, "if-less else");
1.43      dsl      1275:                return COND_PARSE;
1.36      dsl      1276:            }
                   1277:
1.43      dsl      1278:            if (cond_depth > MAXIF)
                   1279:                return COND_SKIP;
1.36      dsl      1280:            state = cond_state[cond_depth];
                   1281:            switch (state) {
                   1282:            case SEARCH_FOR_ELIF:
                   1283:                state = ELSE_ACTIVE;
                   1284:                break;
                   1285:            case ELSE_ACTIVE:
                   1286:            case SKIP_TO_ENDIF:
                   1287:                Parse_Error(PARSE_WARNING, "extra else");
                   1288:                /* FALLTHROUGH */
                   1289:            default:
                   1290:            case IF_ACTIVE:
                   1291:            case SKIP_TO_ELSE:
                   1292:                state = SKIP_TO_ENDIF;
                   1293:                break;
1.1       cgd      1294:            }
1.36      dsl      1295:            cond_state[cond_depth] = state;
                   1296:            return state <= ELSE_ACTIVE ? COND_PARSE : COND_SKIP;
1.1       cgd      1297:        }
1.36      dsl      1298:        /* Assume for now it is an elif */
                   1299:        isElif = TRUE;
                   1300:     } else
                   1301:        isElif = FALSE;
                   1302:
                   1303:     if (line[0] != 'i' || line[1] != 'f')
                   1304:        /* Not an ifxxx or elifxxx line */
                   1305:        return COND_INVALID;
1.7       christos 1306:
1.1       cgd      1307:     /*
                   1308:      * Figure out what sort of conditional it is -- what its default
                   1309:      * function is, etc. -- by looking in the table of valid "ifs"
                   1310:      */
1.36      dsl      1311:     line += 2;
                   1312:     for (ifp = ifs; ; ifp++) {
                   1313:        if (ifp->form == NULL)
                   1314:            return COND_INVALID;
1.26      christos 1315:        if (istoken(ifp->form, line, ifp->formlen)) {
1.36      dsl      1316:            line += ifp->formlen;
1.1       cgd      1317:            break;
                   1318:        }
                   1319:     }
                   1320:
1.36      dsl      1321:     /* Now we know what sort of 'if' it is... */
                   1322:
                   1323:     if (isElif) {
1.37      dsl      1324:        if (cond_depth == cond_min_depth) {
1.36      dsl      1325:            Parse_Error(level, "if-less elif");
1.43      dsl      1326:            return COND_PARSE;
1.36      dsl      1327:        }
1.43      dsl      1328:        if (cond_depth > MAXIF)
                   1329:            /* Error reported when we saw the .if ... */
                   1330:            return COND_SKIP;
                   1331:        state = cond_state[cond_depth];
                   1332:        if (state == SKIP_TO_ENDIF || state == ELSE_ACTIVE) {
1.36      dsl      1333:            Parse_Error(PARSE_WARNING, "extra elif");
1.43      dsl      1334:            cond_state[cond_depth] = SKIP_TO_ENDIF;
                   1335:            return COND_SKIP;
                   1336:        }
1.36      dsl      1337:        if (state != SEARCH_FOR_ELIF) {
                   1338:            /* Either just finished the 'true' block, or already SKIP_TO_ELSE */
                   1339:            cond_state[cond_depth] = SKIP_TO_ELSE;
                   1340:            return COND_SKIP;
1.1       cgd      1341:        }
                   1342:     } else {
1.43      dsl      1343:        /* Normal .if */
1.36      dsl      1344:        if (cond_depth >= MAXIF) {
1.43      dsl      1345:            cond_depth++;
1.36      dsl      1346:            Parse_Error(PARSE_FATAL, "Too many nested if's. %d max.", MAXIF);
1.43      dsl      1347:            return COND_SKIP;
1.1       cgd      1348:        }
1.43      dsl      1349:        state = cond_state[cond_depth];
1.36      dsl      1350:        cond_depth++;
                   1351:        if (state > ELSE_ACTIVE) {
                   1352:            /* If we aren't parsing the data, treat as always false */
                   1353:            cond_state[cond_depth] = SKIP_TO_ELSE;
                   1354:            return COND_SKIP;
1.32      christos 1355:        }
1.1       cgd      1356:     }
                   1357:
1.36      dsl      1358:     /* And evaluate the conditional expresssion */
1.56      dsl      1359:     if (Cond_EvalExpression(ifp, line, &value, 1) == COND_INVALID) {
1.43      dsl      1360:        /* Syntax error in conditional, error message already output. */
                   1361:        /* Skip everything to matching .endif */
                   1362:        cond_state[cond_depth] = SKIP_TO_ELSE;
                   1363:        return COND_SKIP;
1.36      dsl      1364:     }
                   1365:
                   1366:     if (!value) {
                   1367:        cond_state[cond_depth] = SEARCH_FOR_ELIF;
                   1368:        return COND_SKIP;
                   1369:     }
                   1370:     cond_state[cond_depth] = IF_ACTIVE;
                   1371:     return COND_PARSE;
1.1       cgd      1372: }
1.10      christos 1373:
                   1374:
1.1       cgd      1375: 
                   1376: /*-
                   1377:  *-----------------------------------------------------------------------
                   1378:  * Cond_End --
                   1379:  *     Make sure everything's clean at the end of a makefile.
                   1380:  *
                   1381:  * Results:
                   1382:  *     None.
                   1383:  *
                   1384:  * Side Effects:
                   1385:  *     Parse_Error will be called if open conditionals are around.
                   1386:  *
                   1387:  *-----------------------------------------------------------------------
                   1388:  */
                   1389: void
1.37      dsl      1390: Cond_restore_depth(unsigned int saved_depth)
1.1       cgd      1391: {
1.37      dsl      1392:     int open_conds = cond_depth - cond_min_depth;
                   1393:
                   1394:     if (open_conds != 0 || saved_depth > cond_depth) {
                   1395:        Parse_Error(PARSE_FATAL, "%d open conditional%s", open_conds,
                   1396:                    open_conds == 1 ? "" : "s");
                   1397:        cond_depth = cond_min_depth;
1.1       cgd      1398:     }
1.37      dsl      1399:
                   1400:     cond_min_depth = saved_depth;
                   1401: }
                   1402:
                   1403: unsigned int
                   1404: Cond_save_depth(void)
                   1405: {
                   1406:     int depth = cond_min_depth;
                   1407:
                   1408:     cond_min_depth = cond_depth;
                   1409:     return depth;
1.1       cgd      1410: }

CVSweb <webmaster@jp.NetBSD.org>