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