1 | #ifdef CONFIG_WITH_IF_CONDITIONALS
|
---|
2 | /* $Id: ifcond.c 1721 2008-09-04 03:19:08Z bird $ */
|
---|
3 | /** @file
|
---|
4 | * ifcond - C like if expressions.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (c) 2008 knut st. osmundsen <bird-src-spam@anduin.net>
|
---|
9 | *
|
---|
10 | * This file is part of kBuild.
|
---|
11 | *
|
---|
12 | * kBuild is free software; you can redistribute it and/or modify
|
---|
13 | * it under the terms of the GNU General Public License as published by
|
---|
14 | * the Free Software Foundation; either version 2 of the License, or
|
---|
15 | * (at your option) any later version.
|
---|
16 | *
|
---|
17 | * kBuild is distributed in the hope that it will be useful,
|
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | * GNU General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with kBuild; if not, write to the Free Software
|
---|
24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
25 | *
|
---|
26 | */
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include "make.h"
|
---|
32 | #include <assert.h>
|
---|
33 |
|
---|
34 | #include <glob.h>
|
---|
35 |
|
---|
36 | #include "dep.h"
|
---|
37 | #include "filedef.h"
|
---|
38 | #include "job.h"
|
---|
39 | #include "commands.h"
|
---|
40 | #include "variable.h"
|
---|
41 | #include "rule.h"
|
---|
42 | #include "debug.h"
|
---|
43 | #include "hash.h"
|
---|
44 | #include <ctype.h>
|
---|
45 | #ifdef _MSC_VER
|
---|
46 | # include <stdint.h>
|
---|
47 | #endif
|
---|
48 | #include <stdarg.h>
|
---|
49 |
|
---|
50 |
|
---|
51 | /*******************************************************************************
|
---|
52 | * Defined Constants And Macros *
|
---|
53 | *******************************************************************************/
|
---|
54 | /** The max length of a string representation of a number. */
|
---|
55 | #define IFCOND_NUM_LEN ((sizeof("-9223372036854775802") + 4) & ~3)
|
---|
56 |
|
---|
57 | /** The max operator stack depth. */
|
---|
58 | #define IFCOND_MAX_OPERATORS 72
|
---|
59 | /** The max operand depth. */
|
---|
60 | #define IFCOND_MAX_OPERANDS 128
|
---|
61 |
|
---|
62 |
|
---|
63 | /*******************************************************************************
|
---|
64 | * Structures and Typedefs *
|
---|
65 | *******************************************************************************/
|
---|
66 | /** The 64-bit signed integer type we're using. */
|
---|
67 | #ifdef _MSC_VER
|
---|
68 | typedef __int64 IFCONDINT64;
|
---|
69 | #else
|
---|
70 | # include <stdint.h>
|
---|
71 | typedef int64_t IFCONDINT64;
|
---|
72 | #endif
|
---|
73 |
|
---|
74 | /** Pointer to a evaluator instance. */
|
---|
75 | typedef struct IFCOND *PIFCOND;
|
---|
76 |
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Operand variable type.
|
---|
80 | */
|
---|
81 | typedef enum
|
---|
82 | {
|
---|
83 | /** Invalid zero entry. */
|
---|
84 | kIfCondVar_Invalid = 0,
|
---|
85 | /** A number. */
|
---|
86 | kIfCondVar_Num,
|
---|
87 | /** A string in need of expanding (perhaps). */
|
---|
88 | kIfCondVar_String,
|
---|
89 | /** A simple string that doesn't need expanding. */
|
---|
90 | kIfCondVar_SimpleString,
|
---|
91 | /** The end of the valid variable types. */
|
---|
92 | kIfCondVar_End
|
---|
93 | } IFCONDVARTYPE;
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Operand variable.
|
---|
97 | */
|
---|
98 | typedef struct
|
---|
99 | {
|
---|
100 | /** The variable type. */
|
---|
101 | IFCONDVARTYPE enmType;
|
---|
102 | /** The variable. */
|
---|
103 | union
|
---|
104 | {
|
---|
105 | /** Pointer to the string. */
|
---|
106 | char *psz;
|
---|
107 | /** The variable. */
|
---|
108 | IFCONDINT64 i;
|
---|
109 | } uVal;
|
---|
110 | } IFCONDVAR;
|
---|
111 | /** Pointer to a operand variable. */
|
---|
112 | typedef IFCONDVAR *PIFCONDVAR;
|
---|
113 | /** Pointer to a const operand variable. */
|
---|
114 | typedef IFCONDVAR const *PCIFCONDVAR;
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Operator return statuses.
|
---|
118 | */
|
---|
119 | typedef enum
|
---|
120 | {
|
---|
121 | kIfCondRet_Error = -1,
|
---|
122 | kIfCondRet_Ok = 0,
|
---|
123 | kIfCondRet_Operator,
|
---|
124 | kIfCondRet_Operand,
|
---|
125 | kIfCondRet_EndOfExpr,
|
---|
126 | kIfCondRet_End
|
---|
127 | } IFCONDRET;
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Operator.
|
---|
131 | */
|
---|
132 | typedef struct
|
---|
133 | {
|
---|
134 | /** The operator. */
|
---|
135 | char szOp[11];
|
---|
136 | /** The length of the operator string. */
|
---|
137 | char cchOp;
|
---|
138 | /** The pair operator.
|
---|
139 | * This is used with '(' and '?'. */
|
---|
140 | char chPair;
|
---|
141 | /** The precedence. Higher means higher. */
|
---|
142 | char iPrecedence;
|
---|
143 | /** The number of arguments it takes. */
|
---|
144 | signed char cArgs;
|
---|
145 | /** Pointer to the method implementing the operator. */
|
---|
146 | IFCONDRET (*pfn)(PIFCOND pThis);
|
---|
147 | } IFCONDOP;
|
---|
148 | /** Pointer to a const operator. */
|
---|
149 | typedef IFCONDOP const *PCIFCONDOP;
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Expression evaluator instance.
|
---|
153 | */
|
---|
154 | typedef struct IFCOND
|
---|
155 | {
|
---|
156 | /** The full expression. */
|
---|
157 | const char *pszExpr;
|
---|
158 | /** The current location. */
|
---|
159 | const char *psz;
|
---|
160 | /** The current file location, used for errors. */
|
---|
161 | const struct floc *pFileLoc;
|
---|
162 | /** Pending binary operator. */
|
---|
163 | PCIFCONDOP pPending;
|
---|
164 | /** Top of the operator stack. */
|
---|
165 | int iOp;
|
---|
166 | /** Top of the operand stack. */
|
---|
167 | int iVar;
|
---|
168 | /** The operator stack. */
|
---|
169 | PCIFCONDOP apOps[IFCOND_MAX_OPERATORS];
|
---|
170 | /** The operand stack. */
|
---|
171 | IFCONDVAR aVars[IFCOND_MAX_OPERANDS];
|
---|
172 | } IFCOND;
|
---|
173 |
|
---|
174 |
|
---|
175 | /*******************************************************************************
|
---|
176 | * Global Variables *
|
---|
177 | *******************************************************************************/
|
---|
178 | /** Operator start character map.
|
---|
179 | * This indicates which characters that are starting operators and which aren't. */
|
---|
180 | static char g_auchOpStartCharMap[256];
|
---|
181 | /** Whether we've initialized the map. */
|
---|
182 | static int g_fIfCondInitializedMap = 0;
|
---|
183 |
|
---|
184 |
|
---|
185 | /*******************************************************************************
|
---|
186 | * Internal Functions *
|
---|
187 | *******************************************************************************/
|
---|
188 | static void ifcond_unget_op(PIFCOND pThis);
|
---|
189 | static IFCONDRET ifcond_get_binary_or_eoe_or_rparen(PIFCOND pThis);
|
---|
190 |
|
---|
191 |
|
---|
192 |
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Displays an error message.
|
---|
196 | *
|
---|
197 | * The total string length must not exceed 256 bytes.
|
---|
198 | *
|
---|
199 | * @param pThis The evaluator instance.
|
---|
200 | * @param pszError The message format string.
|
---|
201 | * @param ... The message format args.
|
---|
202 | */
|
---|
203 | static void ifcond_error(PIFCOND pThis, const char *pszError, ...)
|
---|
204 | {
|
---|
205 | char szTmp[256];
|
---|
206 | va_list va;
|
---|
207 |
|
---|
208 | va_start(va, pszError);
|
---|
209 | vsprintf(szTmp, pszError, va);
|
---|
210 | va_end(va);
|
---|
211 |
|
---|
212 | fatal(pThis->pFileLoc, "%s", szTmp);
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Converts a number to a string.
|
---|
218 | *
|
---|
219 | * @returns pszDst.
|
---|
220 | * @param pszDst The string buffer to write into. Assumes length of IFCOND_NUM_LEN.
|
---|
221 | * @param iSrc The number to convert.
|
---|
222 | */
|
---|
223 | static char *ifcond_num_to_string(char *pszDst, IFCONDINT64 iSrc)
|
---|
224 | {
|
---|
225 | static const char s_szDigits[17] = "0123456789abcdef";
|
---|
226 | char szTmp[IFCOND_NUM_LEN];
|
---|
227 | char *psz = &szTmp[IFCOND_NUM_LEN - 1];
|
---|
228 | int fNegative;
|
---|
229 |
|
---|
230 | fNegative = iSrc < 0;
|
---|
231 | if (fNegative)
|
---|
232 | {
|
---|
233 | /** @todo this isn't right for INT64_MIN. */
|
---|
234 | iSrc = -iSrc;
|
---|
235 | }
|
---|
236 |
|
---|
237 | *psz = '\0';
|
---|
238 | do
|
---|
239 | {
|
---|
240 | #if 0
|
---|
241 | *--psz = s_szDigits[iSrc & 0xf];
|
---|
242 | iSrc >>= 4;
|
---|
243 | #else
|
---|
244 | *--psz = s_szDigits[iSrc % 10];
|
---|
245 | iSrc /= 10;
|
---|
246 | #endif
|
---|
247 | } while (iSrc);
|
---|
248 |
|
---|
249 | #if 0
|
---|
250 | *--psz = 'x';
|
---|
251 | *--psz = '0';
|
---|
252 | #endif
|
---|
253 |
|
---|
254 | if (fNegative)
|
---|
255 | *--psz = '-';
|
---|
256 |
|
---|
257 | /* copy it into the output buffer. */
|
---|
258 | psz++;
|
---|
259 | return (char *)memcpy(pszDst, psz, &szTmp[IFCOND_NUM_LEN] - psz);
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Attempts to convert a (simple) string into a number.
|
---|
265 | *
|
---|
266 | * @returns status code.
|
---|
267 | * @param pThis The evaluator instance. This is optional when fQuiet is true.
|
---|
268 | * @param piSrc Where to store the numeric value on success.
|
---|
269 | * @param pszSrc The string to try convert.
|
---|
270 | * @param fQuiet Whether we should be quiet or grumpy on failure.
|
---|
271 | */
|
---|
272 | static IFCONDRET ifcond_string_to_num(PIFCOND pThis, IFCONDINT64 *piDst, const char *pszSrc, int fQuiet)
|
---|
273 | {
|
---|
274 | IFCONDRET rc = kIfCondRet_Ok;
|
---|
275 | char const *psz = pszSrc;
|
---|
276 | IFCONDINT64 i;
|
---|
277 | unsigned uBase;
|
---|
278 | int fNegative;
|
---|
279 |
|
---|
280 |
|
---|
281 | /*
|
---|
282 | * Skip blanks.
|
---|
283 | */
|
---|
284 | while (isblank(*psz))
|
---|
285 | psz++;
|
---|
286 |
|
---|
287 | /*
|
---|
288 | * Check for '-'.
|
---|
289 | *
|
---|
290 | * At this point we will not need to deal with operators, this is
|
---|
291 | * just an indicator of negative numbers. If some operator ends up
|
---|
292 | * here it's because it came from a string expansion and thus shall
|
---|
293 | * not be interpreted. If this turns out to be an stupid restriction
|
---|
294 | * it can be fixed, but for now it stays like this.
|
---|
295 | */
|
---|
296 | fNegative = *psz == '-';
|
---|
297 | if (fNegative)
|
---|
298 | psz++;
|
---|
299 |
|
---|
300 | /*
|
---|
301 | * Determin base .
|
---|
302 | * .
|
---|
303 | * Recognize some exsotic prefixes here in addition to the two standard ones.
|
---|
304 | */
|
---|
305 | if (*psz != '0' || psz[1] == '\0' || isblank((unsigned int)psz[1]))
|
---|
306 | uBase = 10;
|
---|
307 | else if (psz[1] == 'x' || psz[1] == 'X')
|
---|
308 | {
|
---|
309 | uBase = 16;
|
---|
310 | psz += 2;
|
---|
311 | }
|
---|
312 | else if (psz[1] == 'b' || psz[1] == 'B')
|
---|
313 | {
|
---|
314 | uBase = 2;
|
---|
315 | psz += 2;
|
---|
316 | }
|
---|
317 | else if (psz[1] == 'd' || psz[1] == 'D')
|
---|
318 | {
|
---|
319 | uBase = 10;
|
---|
320 | psz += 2;
|
---|
321 | }
|
---|
322 | else if (psz[1] == 'o' || psz[1] == 'O')
|
---|
323 | {
|
---|
324 | uBase = 8;
|
---|
325 | psz += 2;
|
---|
326 | }
|
---|
327 | else if (isdigit(psz[1]) && psz[1] != '9' && psz[1] != '8')
|
---|
328 | {
|
---|
329 | uBase = 8;
|
---|
330 | psz++;
|
---|
331 | }
|
---|
332 | else
|
---|
333 | uBase = 10;
|
---|
334 |
|
---|
335 | /*
|
---|
336 | * Convert until we hit a non-digit.
|
---|
337 | */
|
---|
338 | i = 0;
|
---|
339 | for (;;)
|
---|
340 | {
|
---|
341 | int iDigit;
|
---|
342 | int ch = *psz;
|
---|
343 | switch (ch)
|
---|
344 | {
|
---|
345 | case '0': iDigit = 0; break;
|
---|
346 | case '1': iDigit = 1; break;
|
---|
347 | case '2': iDigit = 2; break;
|
---|
348 | case '3': iDigit = 3; break;
|
---|
349 | case '4': iDigit = 4; break;
|
---|
350 | case '5': iDigit = 5; break;
|
---|
351 | case '6': iDigit = 6; break;
|
---|
352 | case '7': iDigit = 7; break;
|
---|
353 | case '8': iDigit = 8; break;
|
---|
354 | case '9': iDigit = 9; break;
|
---|
355 | case 'a':
|
---|
356 | case 'A': iDigit = 10; break;
|
---|
357 | case 'b':
|
---|
358 | case 'B': iDigit = 11; break;
|
---|
359 | case 'c':
|
---|
360 | case 'C': iDigit = 12; break;
|
---|
361 | case 'd':
|
---|
362 | case 'D': iDigit = 13; break;
|
---|
363 | case 'e':
|
---|
364 | case 'E': iDigit = 14; break;
|
---|
365 | case 'f':
|
---|
366 | case 'F': iDigit = 15; break;
|
---|
367 |
|
---|
368 | default:
|
---|
369 | /* is the rest white space? */
|
---|
370 | while (isspace((unsigned int)*psz))
|
---|
371 | psz++;
|
---|
372 | if (*psz != '\0')
|
---|
373 | {
|
---|
374 | iDigit = uBase;
|
---|
375 | break;
|
---|
376 | }
|
---|
377 | /* fall thru */
|
---|
378 |
|
---|
379 | case '\0':
|
---|
380 | if (fNegative)
|
---|
381 | i = -i;
|
---|
382 | *piDst = i;
|
---|
383 | return rc;
|
---|
384 | }
|
---|
385 | if (iDigit >= uBase)
|
---|
386 | {
|
---|
387 | if (fNegative)
|
---|
388 | i = -i;
|
---|
389 | *piDst = i;
|
---|
390 | if (!fQuiet)
|
---|
391 | ifcond_error(pThis, "Invalid a number \"%.80s\"", pszSrc);
|
---|
392 | return kIfCondRet_Error;
|
---|
393 | }
|
---|
394 |
|
---|
395 | /* add the digit and advance */
|
---|
396 | i *= uBase;
|
---|
397 | i += iDigit;
|
---|
398 | psz++;
|
---|
399 | }
|
---|
400 | /* not reached */
|
---|
401 | }
|
---|
402 |
|
---|
403 |
|
---|
404 | /**
|
---|
405 | * Checks if the variable is a string or not.
|
---|
406 | *
|
---|
407 | * @returns 1 if it's a string, 0 otherwise.
|
---|
408 | * @param pVar The variable.
|
---|
409 | */
|
---|
410 | static int ifcond_var_is_string(PCIFCONDVAR pVar)
|
---|
411 | {
|
---|
412 | return pVar->enmType >= kIfCondVar_String;
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 | /**
|
---|
417 | * Deletes a variable.
|
---|
418 | *
|
---|
419 | * @param pVar The variable.
|
---|
420 | */
|
---|
421 | static void ifcond_var_delete(PIFCONDVAR pVar)
|
---|
422 | {
|
---|
423 | if (ifcond_var_is_string(pVar))
|
---|
424 | {
|
---|
425 | free(pVar->uVal.psz);
|
---|
426 | pVar->uVal.psz = NULL;
|
---|
427 | }
|
---|
428 | pVar->enmType = kIfCondVar_Invalid;
|
---|
429 | }
|
---|
430 |
|
---|
431 |
|
---|
432 | /**
|
---|
433 | * Initializes a new variables with a sub-string value.
|
---|
434 | *
|
---|
435 | * @param pVar The new variable.
|
---|
436 | * @param psz The start of the string value.
|
---|
437 | * @param cch The number of chars to copy.
|
---|
438 | * @param enmType The string type.
|
---|
439 | */
|
---|
440 | static void ifcond_var_init_substring(PIFCONDVAR pVar, const char *psz, size_t cch, IFCONDVARTYPE enmType)
|
---|
441 | {
|
---|
442 | if ( enmType != kIfCondVar_SimpleString
|
---|
443 | && memchr(psz, '$', cch))
|
---|
444 | pVar->enmType = kIfCondVar_String;
|
---|
445 | else
|
---|
446 | pVar->enmType = kIfCondVar_SimpleString;
|
---|
447 | pVar->uVal.psz = xmalloc(cch + 1);
|
---|
448 | memcpy(pVar->uVal.psz, psz, cch);
|
---|
449 | pVar->uVal.psz[cch] = '\0';
|
---|
450 | }
|
---|
451 |
|
---|
452 |
|
---|
453 | #if 0 /* unused */
|
---|
454 | /**
|
---|
455 | * Initializes a new variables with a string value.
|
---|
456 | *
|
---|
457 | * @param pVar The new variable.
|
---|
458 | * @param psz The string value.
|
---|
459 | * @param enmType The string type.
|
---|
460 | */
|
---|
461 | static void ifcond_var_init_string(PIFCONDVAR pVar, const char *psz, IFCONDVARTYPE enmType)
|
---|
462 | {
|
---|
463 | ifcond_var_init_substring(pVar, psz, strlen(psz), enmType);
|
---|
464 | }
|
---|
465 |
|
---|
466 |
|
---|
467 | /**
|
---|
468 | * Assigns a sub-string value to a variable.
|
---|
469 | *
|
---|
470 | * @param pVar The new variable.
|
---|
471 | * @param psz The start of the string value.
|
---|
472 | * @param cch The number of chars to copy.
|
---|
473 | * @param enmType The string type.
|
---|
474 | */
|
---|
475 | static void ifcond_var_assign_substring(PIFCONDVAR pVar, const char *psz, size_t cch, IFCONDVARTYPE enmType)
|
---|
476 | {
|
---|
477 | ifcond_var_delete(pVar);
|
---|
478 | ifcond_var_init_substring(pVar, psz, cch, enmType);
|
---|
479 | }
|
---|
480 |
|
---|
481 |
|
---|
482 | /**
|
---|
483 | * Assignes a string value to a variable.
|
---|
484 | *
|
---|
485 | * @param pVar The variable.
|
---|
486 | * @param psz The string value.
|
---|
487 | * @param enmType The string type.
|
---|
488 | */
|
---|
489 | static void ifcond_var_assign_string(PIFCONDVAR pVar, const char *psz, IFCONDVARTYPE enmType)
|
---|
490 | {
|
---|
491 | ifcond_var_delete(pVar);
|
---|
492 | ifcond_var_init_string(pVar, psz, enmType);
|
---|
493 | }
|
---|
494 | #endif /* unused */
|
---|
495 |
|
---|
496 |
|
---|
497 | /**
|
---|
498 | * Simplifies a string variable.
|
---|
499 | *
|
---|
500 | * @param pVar The variable.
|
---|
501 | */
|
---|
502 | static void ifcond_var_make_simple_string(PIFCONDVAR pVar)
|
---|
503 | {
|
---|
504 | switch (pVar->enmType)
|
---|
505 | {
|
---|
506 | case kIfCondVar_Num:
|
---|
507 | {
|
---|
508 | char *psz = (char *)xmalloc(IFCOND_NUM_LEN);
|
---|
509 | ifcond_num_to_string(psz, pVar->uVal.i);
|
---|
510 | pVar->uVal.psz = psz;
|
---|
511 | pVar->enmType = kIfCondVar_SimpleString;
|
---|
512 | break;
|
---|
513 | }
|
---|
514 |
|
---|
515 | case kIfCondVar_String:
|
---|
516 | {
|
---|
517 | char *psz;
|
---|
518 | assert(strchr(pVar->uVal.psz, '$'));
|
---|
519 |
|
---|
520 | psz = allocated_variable_expand(pVar->uVal.psz);
|
---|
521 | free(pVar->uVal.psz);
|
---|
522 | pVar->uVal.psz = psz;
|
---|
523 |
|
---|
524 | pVar->enmType = kIfCondVar_SimpleString;
|
---|
525 | break;
|
---|
526 | }
|
---|
527 |
|
---|
528 | case kIfCondVar_SimpleString:
|
---|
529 | /* nothing to do. */
|
---|
530 | break;
|
---|
531 |
|
---|
532 | default:
|
---|
533 | assert(0);
|
---|
534 | }
|
---|
535 | }
|
---|
536 |
|
---|
537 |
|
---|
538 | #if 0 /* unused */
|
---|
539 | /**
|
---|
540 | * Turns a variable into a string value.
|
---|
541 | *
|
---|
542 | * @param pVar The variable.
|
---|
543 | */
|
---|
544 | static void ifcond_var_make_string(PIFCONDVAR pVar)
|
---|
545 | {
|
---|
546 | switch (pVar->enmType)
|
---|
547 | {
|
---|
548 | case kIfCondVar_Num:
|
---|
549 | ifcond_var_make_simple_string(pVar);
|
---|
550 |
|
---|
551 | case kIfCondVar_String:
|
---|
552 | case kIfCondVar_SimpleString:
|
---|
553 | /* nothing to do. */
|
---|
554 | break;
|
---|
555 |
|
---|
556 | default:
|
---|
557 | assert(0);
|
---|
558 | }
|
---|
559 | }
|
---|
560 | #endif /* unused */
|
---|
561 |
|
---|
562 |
|
---|
563 | /**
|
---|
564 | * Initializes a new variables with a integer value.
|
---|
565 | *
|
---|
566 | * @param pVar The new variable.
|
---|
567 | * @param i The integer value.
|
---|
568 | */
|
---|
569 | static void ifcond_var_init_num(PIFCONDVAR pVar, IFCONDINT64 i)
|
---|
570 | {
|
---|
571 | pVar->enmType = kIfCondVar_Num;
|
---|
572 | pVar->uVal.i = i;
|
---|
573 | }
|
---|
574 |
|
---|
575 |
|
---|
576 | /**
|
---|
577 | * Assigns a integer value to a variable.
|
---|
578 | *
|
---|
579 | * @param pVar The variable.
|
---|
580 | * @param i The integer value.
|
---|
581 | */
|
---|
582 | static void ifcond_var_assign_num(PIFCONDVAR pVar, IFCONDINT64 i)
|
---|
583 | {
|
---|
584 | ifcond_var_delete(pVar);
|
---|
585 | ifcond_var_init_num(pVar, i);
|
---|
586 | }
|
---|
587 |
|
---|
588 |
|
---|
589 | /**
|
---|
590 | * Turns the variable into a number.
|
---|
591 | *
|
---|
592 | * @returns status code.
|
---|
593 | * @param pThis The evaluator instance.
|
---|
594 | * @param pVar The variable.
|
---|
595 | */
|
---|
596 | static IFCONDRET ifcond_var_make_num(PIFCOND pThis, PIFCONDVAR pVar)
|
---|
597 | {
|
---|
598 | switch (pVar->enmType)
|
---|
599 | {
|
---|
600 | case kIfCondVar_Num:
|
---|
601 | /* nothing to do. */
|
---|
602 | break;
|
---|
603 |
|
---|
604 | case kIfCondVar_String:
|
---|
605 | ifcond_var_make_simple_string(pVar);
|
---|
606 | /* fall thru */
|
---|
607 | case kIfCondVar_SimpleString:
|
---|
608 | {
|
---|
609 | IFCONDINT64 i;
|
---|
610 | IFCONDRET rc = ifcond_string_to_num(pThis, &i, pVar->uVal.psz, 0 /* fQuiet */);
|
---|
611 | if (rc < kIfCondRet_Ok)
|
---|
612 | return rc;
|
---|
613 | ifcond_var_assign_num(pVar, i);
|
---|
614 | break;
|
---|
615 | }
|
---|
616 |
|
---|
617 | default:
|
---|
618 | assert(0);
|
---|
619 | return kIfCondRet_Error;
|
---|
620 | }
|
---|
621 |
|
---|
622 | return kIfCondRet_Ok;
|
---|
623 | }
|
---|
624 |
|
---|
625 |
|
---|
626 | /**
|
---|
627 | * Initializes a new variables with a boolean value.
|
---|
628 | *
|
---|
629 | * @param pVar The new variable.
|
---|
630 | * @param f The boolean value.
|
---|
631 | */
|
---|
632 | static void ifcond_var_init_bool(PIFCONDVAR pVar, int f)
|
---|
633 | {
|
---|
634 | pVar->enmType = kIfCondVar_Num;
|
---|
635 | pVar->uVal.i = !!f;
|
---|
636 | }
|
---|
637 |
|
---|
638 |
|
---|
639 | /**
|
---|
640 | * Assigns a boolean value to a variable.
|
---|
641 | *
|
---|
642 | * @param pVar The variable.
|
---|
643 | * @param f The boolean value.
|
---|
644 | */
|
---|
645 | static void ifcond_var_assign_bool(PIFCONDVAR pVar, int f)
|
---|
646 | {
|
---|
647 | ifcond_var_delete(pVar);
|
---|
648 | ifcond_var_init_bool(pVar, f);
|
---|
649 | }
|
---|
650 |
|
---|
651 |
|
---|
652 | /**
|
---|
653 | * Turns the variable into an boolean.
|
---|
654 | *
|
---|
655 | * @returns the boolean interpretation.
|
---|
656 | * @param pVar The variable.
|
---|
657 | */
|
---|
658 | static int ifcond_var_make_bool(PIFCONDVAR pVar)
|
---|
659 | {
|
---|
660 | switch (pVar->enmType)
|
---|
661 | {
|
---|
662 | case kIfCondVar_Num:
|
---|
663 | pVar->uVal.i = !!pVar->uVal.i;
|
---|
664 | break;
|
---|
665 |
|
---|
666 | case kIfCondVar_String:
|
---|
667 | ifcond_var_make_simple_string(pVar);
|
---|
668 | /* fall thru */
|
---|
669 | case kIfCondVar_SimpleString:
|
---|
670 | {
|
---|
671 | /*
|
---|
672 | * Try convert it to a number. If that fails, use the
|
---|
673 | * GNU make boolean logic - not empty string means true.
|
---|
674 | */
|
---|
675 | IFCONDINT64 iVal;
|
---|
676 | char const *psz = pVar->uVal.psz;
|
---|
677 | while (isblank((unsigned char)*psz))
|
---|
678 | psz++;
|
---|
679 | if ( *psz
|
---|
680 | && ifcond_string_to_num(NULL, &iVal, psz, 1 /* fQuiet */) >= kIfCondRet_Ok)
|
---|
681 | ifcond_var_assign_bool(pVar, iVal != 0);
|
---|
682 | else
|
---|
683 | ifcond_var_assign_bool(pVar, *psz != '\0');
|
---|
684 | break;
|
---|
685 | }
|
---|
686 |
|
---|
687 | default:
|
---|
688 | assert(0);
|
---|
689 | break;
|
---|
690 | }
|
---|
691 |
|
---|
692 | return pVar->uVal.i;
|
---|
693 | }
|
---|
694 |
|
---|
695 |
|
---|
696 | /**
|
---|
697 | * Pops a varable off the stack and deletes it.
|
---|
698 | * @param pThis The evaluator instance.
|
---|
699 | */
|
---|
700 | static void ifcond_pop_and_delete_var(PIFCOND pThis)
|
---|
701 | {
|
---|
702 | ifcond_var_delete(&pThis->aVars[pThis->iVar]);
|
---|
703 | pThis->iVar--;
|
---|
704 | }
|
---|
705 |
|
---|
706 |
|
---|
707 | /**
|
---|
708 | * Logical NOT.
|
---|
709 | *
|
---|
710 | * @returns Status code.
|
---|
711 | * @param pThis The instance.
|
---|
712 | */
|
---|
713 | static IFCONDRET ifcond_op_logical_not(PIFCOND pThis)
|
---|
714 | {
|
---|
715 | PIFCONDVAR pVar = &pThis->aVars[pThis->iVar];
|
---|
716 | assert(pThis->iVar >= 0);
|
---|
717 |
|
---|
718 | ifcond_var_assign_bool(pVar, !ifcond_var_make_bool(pVar));
|
---|
719 |
|
---|
720 | return kIfCondRet_Ok;
|
---|
721 | }
|
---|
722 |
|
---|
723 |
|
---|
724 | /////
|
---|
725 |
|
---|
726 |
|
---|
727 | /**
|
---|
728 | * Not equal.
|
---|
729 | *
|
---|
730 | * @returns Status code.
|
---|
731 | * @param pThis The instance.
|
---|
732 | */
|
---|
733 | static IFCONDRET ifcond_op_equal(PIFCOND pThis)
|
---|
734 | {
|
---|
735 | PIFCONDVAR pVar1 = &pThis->aVars[pThis->iVar - 1];
|
---|
736 | PIFCONDVAR pVar2 = &pThis->aVars[pThis->iVar];
|
---|
737 | assert(pThis->iVar >= 1);
|
---|
738 |
|
---|
739 | /*
|
---|
740 | * If it's the same type, things are simple.
|
---|
741 | */
|
---|
742 | if (ifcond_var_is_string(pVar1) == ifcond_var_is_string(pVar2))
|
---|
743 | {
|
---|
744 | if (ifcond_var_is_string(pVar1))
|
---|
745 | {
|
---|
746 | ifcond_var_make_simple_string(pVar1);
|
---|
747 | ifcond_var_make_simple_string(pVar2);
|
---|
748 | ifcond_var_assign_bool(pVar1, !strcmp(pVar1->uVal.psz, pVar2->uVal.psz));
|
---|
749 | }
|
---|
750 | else
|
---|
751 | ifcond_var_assign_bool(pVar1, pVar1->uVal.i == pVar2->uVal.i);
|
---|
752 | }
|
---|
753 | else
|
---|
754 | {
|
---|
755 | /*
|
---|
756 | * If the type differs, there are now two options:
|
---|
757 | * 1. Convert the string to a valid number and compare the numbers.
|
---|
758 | * 2. Convert an empty string to a 'false' boolean value and compare
|
---|
759 | * numerically. This one is a bit questionable, so it's currently
|
---|
760 | * not enabled.
|
---|
761 | */
|
---|
762 | IFCONDINT64 iVal1;
|
---|
763 | PIFCONDVAR pVarRet = pVar1;
|
---|
764 |
|
---|
765 | /* switch so pVar1 is the string. */
|
---|
766 | if (!ifcond_var_is_string(pVar1))
|
---|
767 | {
|
---|
768 | pVar1 = pVar2;
|
---|
769 | pVar2 = pVarRet;
|
---|
770 | }
|
---|
771 |
|
---|
772 | ifcond_var_make_simple_string(pVar1);
|
---|
773 | if (ifcond_string_to_num(NULL, &iVal1, pVar1->uVal.psz, 1 /* fQuiet */) >= kIfCondRet_Ok)
|
---|
774 | ifcond_var_assign_bool(pVar1, iVal1 == pVar2->uVal.i);
|
---|
775 | else
|
---|
776 | {
|
---|
777 | #if 0 /* bogus consept */
|
---|
778 | const char *psz = pVar1->uVal.psz;
|
---|
779 | while (isspace((unsigned char)*psz))
|
---|
780 | psz++;
|
---|
781 | if (!*psz)
|
---|
782 | ifcond_var_assign_bool(pVar1, iVal1 == pVar2->uVal.i);
|
---|
783 | else
|
---|
784 | #endif
|
---|
785 | {
|
---|
786 | ifcond_error(pThis, "Cannot compare strings and numbers");
|
---|
787 | return kIfCondRet_Error;
|
---|
788 | }
|
---|
789 | }
|
---|
790 | }
|
---|
791 |
|
---|
792 | ifcond_pop_and_delete_var(pThis);
|
---|
793 | return kIfCondRet_Ok;
|
---|
794 | }
|
---|
795 |
|
---|
796 |
|
---|
797 | /**
|
---|
798 | * Not equal.
|
---|
799 | *
|
---|
800 | * @returns Status code.
|
---|
801 | * @param pThis The instance.
|
---|
802 | */
|
---|
803 | static IFCONDRET ifcond_op_not_equal(PIFCOND pThis)
|
---|
804 | {
|
---|
805 | IFCONDRET rc = ifcond_op_equal(pThis);
|
---|
806 | if (rc >= kIfCondRet_Ok)
|
---|
807 | rc = ifcond_op_logical_not(pThis);
|
---|
808 | return rc;
|
---|
809 | }
|
---|
810 |
|
---|
811 |
|
---|
812 | /**
|
---|
813 | * Bitwise AND.
|
---|
814 | *
|
---|
815 | * @returns Status code.
|
---|
816 | * @param pThis The instance.
|
---|
817 | */
|
---|
818 | static IFCONDRET ifcond_op_bitwise_and(PIFCOND pThis)
|
---|
819 | {
|
---|
820 | IFCONDRET rc;
|
---|
821 | assert(pThis->iVar >= 1);
|
---|
822 |
|
---|
823 | rc = ifcond_var_make_num(pThis, &pThis->aVars[pThis->iVar - 1]);
|
---|
824 | if (rc >= kIfCondRet_Ok)
|
---|
825 | {
|
---|
826 | rc = ifcond_var_make_num(pThis, &pThis->aVars[pThis->iVar]);
|
---|
827 | if (rc >= kIfCondRet_Ok)
|
---|
828 | pThis->aVars[pThis->iVar - 1].uVal.i &= pThis->aVars[pThis->iVar].uVal.i;
|
---|
829 | }
|
---|
830 |
|
---|
831 | ifcond_pop_and_delete_var(pThis);
|
---|
832 | return kIfCondRet_Ok;
|
---|
833 | }
|
---|
834 |
|
---|
835 |
|
---|
836 | /**
|
---|
837 | * Bitwise XOR.
|
---|
838 | *
|
---|
839 | * @returns Status code.
|
---|
840 | * @param pThis The instance.
|
---|
841 | */
|
---|
842 | static IFCONDRET ifcond_op_bitwise_xor(PIFCOND pThis)
|
---|
843 | {
|
---|
844 | IFCONDRET rc;
|
---|
845 | assert(pThis->iVar >= 1);
|
---|
846 |
|
---|
847 | rc = ifcond_var_make_num(pThis, &pThis->aVars[pThis->iVar - 1]);
|
---|
848 | if (rc >= kIfCondRet_Ok)
|
---|
849 | {
|
---|
850 | rc = ifcond_var_make_num(pThis, &pThis->aVars[pThis->iVar]);
|
---|
851 | if (rc >= kIfCondRet_Ok)
|
---|
852 | pThis->aVars[pThis->iVar - 1].uVal.i ^= pThis->aVars[pThis->iVar].uVal.i;
|
---|
853 | }
|
---|
854 |
|
---|
855 | ifcond_pop_and_delete_var(pThis);
|
---|
856 | return kIfCondRet_Ok;
|
---|
857 | }
|
---|
858 |
|
---|
859 |
|
---|
860 | /**
|
---|
861 | * Bitwise OR.
|
---|
862 | *
|
---|
863 | * @returns Status code.
|
---|
864 | * @param pThis The instance.
|
---|
865 | */
|
---|
866 | static IFCONDRET ifcond_op_bitwise_or(PIFCOND pThis)
|
---|
867 | {
|
---|
868 | IFCONDRET rc;
|
---|
869 | assert(pThis->iVar >= 1);
|
---|
870 |
|
---|
871 | rc = ifcond_var_make_num(pThis, &pThis->aVars[pThis->iVar - 1]);
|
---|
872 | if (rc >= kIfCondRet_Ok)
|
---|
873 | {
|
---|
874 | rc = ifcond_var_make_num(pThis, &pThis->aVars[pThis->iVar]);
|
---|
875 | if (rc >= kIfCondRet_Ok)
|
---|
876 | pThis->aVars[pThis->iVar - 1].uVal.i |= pThis->aVars[pThis->iVar].uVal.i;
|
---|
877 | }
|
---|
878 |
|
---|
879 | ifcond_pop_and_delete_var(pThis);
|
---|
880 | return kIfCondRet_Ok;
|
---|
881 | }
|
---|
882 |
|
---|
883 |
|
---|
884 | /**
|
---|
885 | * Logical AND.
|
---|
886 | *
|
---|
887 | * @returns Status code.
|
---|
888 | * @param pThis The instance.
|
---|
889 | */
|
---|
890 | static IFCONDRET ifcond_op_logical_and(PIFCOND pThis)
|
---|
891 | {
|
---|
892 | assert(pThis->iVar >= 1);
|
---|
893 | if ( ifcond_var_make_bool(&pThis->aVars[pThis->iVar - 1])
|
---|
894 | && ifcond_var_make_bool(&pThis->aVars[pThis->iVar]))
|
---|
895 | ifcond_var_assign_bool(&pThis->aVars[pThis->iVar - 1], 1);
|
---|
896 | else
|
---|
897 | ifcond_var_assign_bool(&pThis->aVars[pThis->iVar - 1], 0);
|
---|
898 |
|
---|
899 | ifcond_pop_and_delete_var(pThis);
|
---|
900 | return kIfCondRet_Ok;
|
---|
901 | }
|
---|
902 |
|
---|
903 |
|
---|
904 | /**
|
---|
905 | * Logical OR.
|
---|
906 | *
|
---|
907 | * @returns Status code.
|
---|
908 | * @param pThis The instance.
|
---|
909 | */
|
---|
910 | static IFCONDRET ifcond_op_logical_or(PIFCOND pThis)
|
---|
911 | {
|
---|
912 | assert(pThis->iVar >= 1);
|
---|
913 | if ( ifcond_var_make_bool(&pThis->aVars[pThis->iVar - 1])
|
---|
914 | || ifcond_var_make_bool(&pThis->aVars[pThis->iVar]))
|
---|
915 | ifcond_var_assign_bool(&pThis->aVars[pThis->iVar - 1], 1);
|
---|
916 | else
|
---|
917 | ifcond_var_assign_bool(&pThis->aVars[pThis->iVar - 1], 0);
|
---|
918 |
|
---|
919 | ifcond_pop_and_delete_var(pThis);
|
---|
920 | return kIfCondRet_Ok;
|
---|
921 | }
|
---|
922 |
|
---|
923 |
|
---|
924 | /**
|
---|
925 | * Left parenthesis.
|
---|
926 | *
|
---|
927 | * @returns Status code.
|
---|
928 | * @param pThis The instance.
|
---|
929 | */
|
---|
930 | static IFCONDRET ifcond_op_left_parenthesis(PIFCOND pThis)
|
---|
931 | {
|
---|
932 | /*
|
---|
933 | * There should be a right parenthesis operator lined up for us now,
|
---|
934 | * eat it. If not found there is an inbalance.
|
---|
935 | */
|
---|
936 | IFCONDRET rc = ifcond_get_binary_or_eoe_or_rparen(pThis);
|
---|
937 | if ( rc == kIfCondRet_Operator
|
---|
938 | && pThis->apOps[pThis->iOp]->szOp[0] == ')')
|
---|
939 | {
|
---|
940 | /* pop it and get another one which we can leave pending. */
|
---|
941 | pThis->iOp--;
|
---|
942 | rc = ifcond_get_binary_or_eoe_or_rparen(pThis);
|
---|
943 | if (rc >= kIfCondRet_Ok)
|
---|
944 | ifcond_unget_op(pThis);
|
---|
945 | }
|
---|
946 | else
|
---|
947 | {
|
---|
948 | ifcond_error(pThis, "Missing ')'");
|
---|
949 | rc = kIfCondRet_Error;
|
---|
950 | }
|
---|
951 |
|
---|
952 | return rc;
|
---|
953 | }
|
---|
954 |
|
---|
955 |
|
---|
956 | /**
|
---|
957 | * Right parenthesis, dummy that's never actually called.
|
---|
958 | *
|
---|
959 | * @returns Status code.
|
---|
960 | * @param pThis The instance.
|
---|
961 | */
|
---|
962 | static IFCONDRET ifcond_op_right_parenthesis(PIFCOND pThis)
|
---|
963 | {
|
---|
964 | return kIfCondRet_Ok;
|
---|
965 | }
|
---|
966 |
|
---|
967 |
|
---|
968 |
|
---|
969 |
|
---|
970 |
|
---|
971 | /**
|
---|
972 | * The operator table.
|
---|
973 | *
|
---|
974 | * This table is NOT ordered by precedence, but for linear search
|
---|
975 | * allowing for first match to return the correct operator. This
|
---|
976 | * means that || must come before |, or else | will match all.
|
---|
977 | */
|
---|
978 | static const IFCONDOP g_aIfCondOps[] =
|
---|
979 | {
|
---|
980 | #define IFCOND_OP(szOp, iPrecedence, cArgs, pfn) { szOp, sizeof(szOp) - 1, '\0', iPrecedence, cArgs, pfn }
|
---|
981 | /* Name, iPrecedence, cArgs, pfn */
|
---|
982 | #if 0
|
---|
983 | IFCOND_OP("defined", 90, 1, ifcond_op_defined),
|
---|
984 | IFCOND_OP("+", 80, 1, ifcond_op_pluss),
|
---|
985 | IFCOND_OP("-", 80, 1, ifcond_op_minus),
|
---|
986 | IFCOND_OP("~", 80, 1, ifcond_op_bitwise_not),
|
---|
987 | IFCOND_OP("*", 75, 2, ifcond_op_multiply),
|
---|
988 | IFCOND_OP("/", 75, 2, ifcond_op_divide),
|
---|
989 | IFCOND_OP("%", 75, 2, ifcond_op_mod),
|
---|
990 | IFCOND_OP("+", 70, 2, ifcond_op_add),
|
---|
991 | IFCOND_OP("-", 70, 2, ifcond_op_sub),
|
---|
992 | IFCOND_OP("<<", 65, 2, ifcond_op_shift_left),
|
---|
993 | IFCOND_OP(">>", 65, 2, ifcond_op_shift_right),
|
---|
994 | IFCOND_OP("<=", 60, 2, ifcond_op_less_or_equal_than),
|
---|
995 | IFCOND_OP("<", 60, 2, ifcond_op_less_than),
|
---|
996 | IFCOND_OP(">=", 60, 2, ifcond_op_greater_or_equal_than),
|
---|
997 | IFCOND_OP(">", 60, 2, ifcond_op_greater_than),
|
---|
998 | #endif
|
---|
999 | IFCOND_OP("==", 55, 2, ifcond_op_equal),
|
---|
1000 | IFCOND_OP("!=", 55, 2, ifcond_op_not_equal),
|
---|
1001 | IFCOND_OP("!", 80, 1, ifcond_op_logical_not),
|
---|
1002 | IFCOND_OP("^", 45, 2, ifcond_op_bitwise_xor),
|
---|
1003 | IFCOND_OP("&&", 35, 2, ifcond_op_logical_and),
|
---|
1004 | IFCOND_OP("&", 50, 2, ifcond_op_bitwise_and),
|
---|
1005 | IFCOND_OP("||", 30, 2, ifcond_op_logical_or),
|
---|
1006 | IFCOND_OP("|", 40, 2, ifcond_op_bitwise_or),
|
---|
1007 | { "(", 1, ')', 10, 1, ifcond_op_left_parenthesis },
|
---|
1008 | { ")", 1, '(', 10, 0, ifcond_op_right_parenthesis },
|
---|
1009 | /* { "?", 1, ':', 5, 2, ifcond_op_question },
|
---|
1010 | { ":", 1, '?', 5, 2, ifcond_op_colon }, -- too weird for now. */
|
---|
1011 | #undef IFCOND_OP
|
---|
1012 | };
|
---|
1013 |
|
---|
1014 | /** Dummy end of expression fake. */
|
---|
1015 | static const IFCONDOP g_IfCondEndOfExpOp =
|
---|
1016 | {
|
---|
1017 | "", 0, '\0', 0, 0, NULL
|
---|
1018 | };
|
---|
1019 |
|
---|
1020 |
|
---|
1021 | /**
|
---|
1022 | * Initializes the opcode character map if necessary.
|
---|
1023 | */
|
---|
1024 | static void ifcond_map_init(void)
|
---|
1025 | {
|
---|
1026 | int i;
|
---|
1027 | if (g_fIfCondInitializedMap)
|
---|
1028 | return;
|
---|
1029 |
|
---|
1030 | /*
|
---|
1031 | * Initialize it.
|
---|
1032 | */
|
---|
1033 | memset(&g_auchOpStartCharMap, 0, sizeof(g_auchOpStartCharMap));
|
---|
1034 | for (i = 0; i < sizeof(g_aIfCondOps) / sizeof(g_aIfCondOps[0]); i++)
|
---|
1035 | {
|
---|
1036 | unsigned int ch = (unsigned int)g_aIfCondOps[i].szOp[0];
|
---|
1037 | if (!g_auchOpStartCharMap[ch])
|
---|
1038 | g_auchOpStartCharMap[ch] = (i << 1) | 1;
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | g_fIfCondInitializedMap = 1;
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 |
|
---|
1045 | /**
|
---|
1046 | * Looks up a character in the map.
|
---|
1047 | *
|
---|
1048 | * @returns the value for that char.
|
---|
1049 | * @retval 0 if not a potential opcode start char.
|
---|
1050 | * @retval non-zero if it's a potential operator. The low bit is always set
|
---|
1051 | * while the remaining 7 bits is the index into the operator table
|
---|
1052 | * of the first match.
|
---|
1053 | *
|
---|
1054 | * @param ch The character.
|
---|
1055 | */
|
---|
1056 | static unsigned char ifcond_map_get(char ch)
|
---|
1057 | {
|
---|
1058 | return g_auchOpStartCharMap[(unsigned int)ch];
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 |
|
---|
1062 | /**
|
---|
1063 | * Searches the operator table given a potential operator start char.
|
---|
1064 | *
|
---|
1065 | * @returns Pointer to the matching operator. NULL if not found.
|
---|
1066 | * @param psz Pointer to what can be an operator.
|
---|
1067 | * @param uchVal The ifcond_map_get value.
|
---|
1068 | * @param fUnary Whether it must be an unary operator or not.
|
---|
1069 | */
|
---|
1070 | static PCIFCONDOP ifcond_lookup_op(char const *psz, unsigned char uchVal, int fUnary)
|
---|
1071 | {
|
---|
1072 | char ch = *psz;
|
---|
1073 | int i;
|
---|
1074 |
|
---|
1075 | for (i = uchVal >> 1; i < sizeof(g_aIfCondOps) / sizeof(g_aIfCondOps[0]); i++)
|
---|
1076 | {
|
---|
1077 | /* compare the string... */
|
---|
1078 | switch (g_aIfCondOps[i].cchOp)
|
---|
1079 | {
|
---|
1080 | case 1:
|
---|
1081 | if (g_aIfCondOps[i].szOp[0] != ch)
|
---|
1082 | continue;
|
---|
1083 | break;
|
---|
1084 | case 2:
|
---|
1085 | if ( g_aIfCondOps[i].szOp[0] != ch
|
---|
1086 | || g_aIfCondOps[i].szOp[1] != psz[1])
|
---|
1087 | continue;
|
---|
1088 | break;
|
---|
1089 | default:
|
---|
1090 | if ( g_aIfCondOps[i].szOp[0] != ch
|
---|
1091 | || strncmp(&g_aIfCondOps[i].szOp[1], psz + 1, g_aIfCondOps[i].cchOp - 1))
|
---|
1092 | continue;
|
---|
1093 | break;
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | /* ... and the operator type. */
|
---|
1097 | if (fUnary == (g_aIfCondOps[i].cArgs == 1))
|
---|
1098 | {
|
---|
1099 | /* got a match! */
|
---|
1100 | return &g_aIfCondOps[i];
|
---|
1101 | }
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 | return NULL;
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 |
|
---|
1108 | /**
|
---|
1109 | * Ungets a binary operator.
|
---|
1110 | *
|
---|
1111 | * The operator is poped from the stack and put in the pending position.
|
---|
1112 | *
|
---|
1113 | * @param pThis The evaluator instance.
|
---|
1114 | */
|
---|
1115 | static void ifcond_unget_op(PIFCOND pThis)
|
---|
1116 | {
|
---|
1117 | assert(pThis->pPending == NULL);
|
---|
1118 | assert(pThis->iOp >= 0);
|
---|
1119 |
|
---|
1120 | pThis->pPending = pThis->apOps[pThis->iOp];
|
---|
1121 | pThis->apOps[pThis->iOp] = NULL;
|
---|
1122 | pThis->iOp--;
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 |
|
---|
1126 |
|
---|
1127 | /**
|
---|
1128 | * Get the next token, it should be a binary operator, or the end of
|
---|
1129 | * the expression, or a right parenthesis.
|
---|
1130 | *
|
---|
1131 | * The operator is pushed onto the stack and the status code indicates
|
---|
1132 | * which of the two we found.
|
---|
1133 | *
|
---|
1134 | * @returns status code. Will grumble on failure.
|
---|
1135 | * @retval kIfCondRet_EndOfExpr if we encountered the end of the expression.
|
---|
1136 | * @retval kIfCondRet_Operator if we encountered a binary operator or right
|
---|
1137 | * parenthesis. It's on the operator stack.
|
---|
1138 | *
|
---|
1139 | * @param pThis The evaluator instance.
|
---|
1140 | */
|
---|
1141 | static IFCONDRET ifcond_get_binary_or_eoe_or_rparen(PIFCOND pThis)
|
---|
1142 | {
|
---|
1143 | /*
|
---|
1144 | * See if there is anything pending first.
|
---|
1145 | */
|
---|
1146 | PCIFCONDOP pOp = pThis->pPending;
|
---|
1147 | if (pOp)
|
---|
1148 | pThis->pPending = NULL;
|
---|
1149 | else
|
---|
1150 | {
|
---|
1151 | /*
|
---|
1152 | * Eat more of the expression.
|
---|
1153 | */
|
---|
1154 | char const *psz = pThis->psz;
|
---|
1155 |
|
---|
1156 | /* spaces */
|
---|
1157 | while (isspace((unsigned int)*psz))
|
---|
1158 | psz++;
|
---|
1159 | /* see what we've got. */
|
---|
1160 | if (*psz)
|
---|
1161 | {
|
---|
1162 | unsigned char uchVal = ifcond_map_get(*psz);
|
---|
1163 | if (uchVal)
|
---|
1164 | pOp = ifcond_lookup_op(psz, uchVal, 0 /* fUnary */);
|
---|
1165 | if (!pOp)
|
---|
1166 | {
|
---|
1167 | ifcond_error(pThis, "Expected binary operator, found \"%.42s\"...", psz);
|
---|
1168 | return kIfCondRet_Error;
|
---|
1169 | }
|
---|
1170 | psz += pOp->cchOp;
|
---|
1171 | }
|
---|
1172 | else
|
---|
1173 | pOp = &g_IfCondEndOfExpOp;
|
---|
1174 | pThis->psz = psz;
|
---|
1175 | }
|
---|
1176 |
|
---|
1177 | /*
|
---|
1178 | * Push it.
|
---|
1179 | */
|
---|
1180 | if (pThis->iOp >= IFCOND_MAX_OPERATORS - 1)
|
---|
1181 | {
|
---|
1182 | ifcond_error(pThis, "Operator stack overflow");
|
---|
1183 | return kIfCondRet_Error;
|
---|
1184 | }
|
---|
1185 | pThis->apOps[++pThis->iOp] = pOp;
|
---|
1186 |
|
---|
1187 | return pOp->iPrecedence
|
---|
1188 | ? kIfCondRet_Operator
|
---|
1189 | : kIfCondRet_EndOfExpr;
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 |
|
---|
1193 |
|
---|
1194 | /**
|
---|
1195 | * Get the next token, it should be an unary operator or an operand.
|
---|
1196 | *
|
---|
1197 | * This will fail if encountering the end of the expression since
|
---|
1198 | * it is implied that there should be something more.
|
---|
1199 | *
|
---|
1200 | * The token is pushed onto the respective stack and the status code
|
---|
1201 | * indicates which it is.
|
---|
1202 | *
|
---|
1203 | * @returns status code. On failure we'll be done bitching already.
|
---|
1204 | * @retval kIfCondRet_Operator if we encountered an unary operator.
|
---|
1205 | * It's on the operator stack.
|
---|
1206 | * @retval kIfCondRet_Operand if we encountered an operand operator.
|
---|
1207 | * It's on the operand stack.
|
---|
1208 | *
|
---|
1209 | * @param This The evaluator instance.
|
---|
1210 | */
|
---|
1211 | static IFCONDRET ifcond_get_unary_or_operand(PIFCOND pThis)
|
---|
1212 | {
|
---|
1213 | IFCONDRET rc;
|
---|
1214 | unsigned char uchVal;
|
---|
1215 | PCIFCONDOP pOp;
|
---|
1216 | char const *psz = pThis->psz;
|
---|
1217 |
|
---|
1218 | /*
|
---|
1219 | * Eat white space and make sure there is something after it.
|
---|
1220 | */
|
---|
1221 | while (isspace((unsigned int)*psz))
|
---|
1222 | psz++;
|
---|
1223 | if (!*psz)
|
---|
1224 | {
|
---|
1225 | ifcond_error(pThis, "Unexpected end of expression");
|
---|
1226 | return kIfCondRet_Error;
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | /*
|
---|
1230 | * Is it an operator?
|
---|
1231 | */
|
---|
1232 | pOp = NULL;
|
---|
1233 | uchVal = ifcond_map_get(*psz);
|
---|
1234 | if (uchVal)
|
---|
1235 | pOp = ifcond_lookup_op(psz, uchVal, 1 /* fUnary */);
|
---|
1236 | if (pOp)
|
---|
1237 | {
|
---|
1238 | /*
|
---|
1239 | * Push the operator onto the stack.
|
---|
1240 | */
|
---|
1241 | if (pThis->iVar < IFCOND_MAX_OPERANDS - 1)
|
---|
1242 | {
|
---|
1243 | pThis->apOps[++pThis->iOp] = pOp;
|
---|
1244 | rc = kIfCondRet_Operator;
|
---|
1245 | }
|
---|
1246 | else
|
---|
1247 | {
|
---|
1248 | ifcond_error(pThis, "Operator stack overflow");
|
---|
1249 | rc = kIfCondRet_Error;
|
---|
1250 | }
|
---|
1251 | psz += pOp->cchOp;
|
---|
1252 | }
|
---|
1253 | else if (pThis->iVar < IFCOND_MAX_OPERANDS - 1)
|
---|
1254 | {
|
---|
1255 | /*
|
---|
1256 | * It's an operand. Figure out where it ends and
|
---|
1257 | * push it onto the stack.
|
---|
1258 | */
|
---|
1259 | const char *pszStart = psz;
|
---|
1260 |
|
---|
1261 | rc = kIfCondRet_Ok;
|
---|
1262 | if (*psz == '"')
|
---|
1263 | {
|
---|
1264 | pszStart++;
|
---|
1265 | while (*psz && *psz != '"')
|
---|
1266 | psz++;
|
---|
1267 | ifcond_var_init_substring(&pThis->aVars[++pThis->iVar], pszStart, psz - pszStart, kIfCondVar_String);
|
---|
1268 | }
|
---|
1269 | else if (*psz == '\'')
|
---|
1270 | {
|
---|
1271 | pszStart++;
|
---|
1272 | while (*psz && *psz != '\'')
|
---|
1273 | psz++;
|
---|
1274 | ifcond_var_init_substring(&pThis->aVars[++pThis->iVar], pszStart, psz - pszStart, kIfCondVar_SimpleString);
|
---|
1275 | }
|
---|
1276 | else
|
---|
1277 | {
|
---|
1278 | char achPars[20];
|
---|
1279 | int iPar = -1;
|
---|
1280 | char chEndPar = '\0';
|
---|
1281 | char ch;
|
---|
1282 |
|
---|
1283 | while ((ch = *psz) != '\0')
|
---|
1284 | {
|
---|
1285 | /* $(adsf) or ${asdf} needs special handling. */
|
---|
1286 | if ( ch == '$'
|
---|
1287 | && ( psz[1] == '('
|
---|
1288 | || psz[1] == '{'))
|
---|
1289 | {
|
---|
1290 | psz++;
|
---|
1291 | if (iPar > sizeof(achPars) / sizeof(achPars[0]))
|
---|
1292 | {
|
---|
1293 | ifcond_error(pThis, "Too deep nesting of variable expansions");
|
---|
1294 | rc = kIfCondRet_Error;
|
---|
1295 | break;
|
---|
1296 | }
|
---|
1297 | achPars[++iPar] = chEndPar = ch == '(' ? ')' : '}';
|
---|
1298 | }
|
---|
1299 | else if (ch == chEndPar)
|
---|
1300 | {
|
---|
1301 | iPar--;
|
---|
1302 | chEndPar = iPar >= 0 ? achPars[iPar] : '\0';
|
---|
1303 | }
|
---|
1304 | else if (!chEndPar)
|
---|
1305 | {
|
---|
1306 | /** @todo combine isspace and ifcond_map_get! */
|
---|
1307 | unsigned chVal = ifcond_map_get(ch);
|
---|
1308 | if (chVal)
|
---|
1309 | {
|
---|
1310 | PCIFCONDOP pOp = ifcond_lookup_op(psz, uchVal, 0 /* fUnary */);
|
---|
1311 | if (pOp)
|
---|
1312 | break;
|
---|
1313 | }
|
---|
1314 | if (isspace((unsigned char)ch))
|
---|
1315 | break;
|
---|
1316 | }
|
---|
1317 |
|
---|
1318 | /* next */
|
---|
1319 | psz++;
|
---|
1320 | }
|
---|
1321 |
|
---|
1322 | if (rc == kIfCondRet_Ok)
|
---|
1323 | ifcond_var_init_substring(&pThis->aVars[++pThis->iVar], pszStart, psz - pszStart, kIfCondVar_String);
|
---|
1324 | }
|
---|
1325 | }
|
---|
1326 | else
|
---|
1327 | {
|
---|
1328 | ifcond_error(pThis, "Operand stack overflow");
|
---|
1329 | rc = kIfCondRet_Error;
|
---|
1330 | }
|
---|
1331 | pThis->psz = psz;
|
---|
1332 |
|
---|
1333 | return rc;
|
---|
1334 | }
|
---|
1335 |
|
---|
1336 |
|
---|
1337 | /**
|
---|
1338 | * Evaluates the current expression.
|
---|
1339 | *
|
---|
1340 | * @returns status code.
|
---|
1341 | *
|
---|
1342 | * @param pThis The instance.
|
---|
1343 | */
|
---|
1344 | static IFCONDRET ifcond_eval(PIFCOND pThis)
|
---|
1345 | {
|
---|
1346 | IFCONDRET rc;
|
---|
1347 | PCIFCONDOP pOp;
|
---|
1348 |
|
---|
1349 | /*
|
---|
1350 | * The main loop.
|
---|
1351 | */
|
---|
1352 | for (;;)
|
---|
1353 | {
|
---|
1354 | /*
|
---|
1355 | * Eat unary operators until we hit an operand.
|
---|
1356 | */
|
---|
1357 | do rc = ifcond_get_unary_or_operand(pThis);
|
---|
1358 | while (rc == kIfCondRet_Operator);
|
---|
1359 | if (rc < kIfCondRet_Error)
|
---|
1360 | break;
|
---|
1361 |
|
---|
1362 | /*
|
---|
1363 | * Look for a binary operator, right parenthesis or end of expression.
|
---|
1364 | */
|
---|
1365 | rc = ifcond_get_binary_or_eoe_or_rparen(pThis);
|
---|
1366 | if (rc < kIfCondRet_Error)
|
---|
1367 | break;
|
---|
1368 | ifcond_unget_op(pThis);
|
---|
1369 |
|
---|
1370 | /*
|
---|
1371 | * Pop operators and apply them.
|
---|
1372 | *
|
---|
1373 | * Parenthesis will be handed via precedence, where the left parenthesis
|
---|
1374 | * will go pop the right one and make another operator pending.
|
---|
1375 | */
|
---|
1376 | while ( pThis->iOp >= 0
|
---|
1377 | && pThis->apOps[pThis->iOp]->iPrecedence >= pThis->pPending->iPrecedence)
|
---|
1378 | {
|
---|
1379 | pOp = pThis->apOps[pThis->iOp--];
|
---|
1380 | rc = pOp->pfn(pThis);
|
---|
1381 | if (rc < kIfCondRet_Error)
|
---|
1382 | break;
|
---|
1383 | }
|
---|
1384 | if (rc < kIfCondRet_Error)
|
---|
1385 | break;
|
---|
1386 |
|
---|
1387 | /*
|
---|
1388 | * Get the next binary operator or end of expression.
|
---|
1389 | * There should be no right parenthesis here.
|
---|
1390 | */
|
---|
1391 | rc = ifcond_get_binary_or_eoe_or_rparen(pThis);
|
---|
1392 | if (rc < kIfCondRet_Error)
|
---|
1393 | break;
|
---|
1394 | pOp = pThis->apOps[pThis->iOp];
|
---|
1395 | if (!pOp->iPrecedence)
|
---|
1396 | break; /* end of expression */
|
---|
1397 | if (!pOp->cArgs)
|
---|
1398 | {
|
---|
1399 | ifcond_error(pThis, "Unexpected \"%s\"", pOp->szOp);
|
---|
1400 | rc = kIfCondRet_Error;
|
---|
1401 | break;
|
---|
1402 | }
|
---|
1403 | }
|
---|
1404 |
|
---|
1405 | return rc;
|
---|
1406 | }
|
---|
1407 |
|
---|
1408 |
|
---|
1409 | /**
|
---|
1410 | * Destroys the given instance.
|
---|
1411 | *
|
---|
1412 | * @param pThis The instance to destroy.
|
---|
1413 | */
|
---|
1414 | static void ifcond_destroy(PIFCOND pThis)
|
---|
1415 | {
|
---|
1416 | while (pThis->iVar >= 0)
|
---|
1417 | {
|
---|
1418 | ifcond_var_delete(pThis->aVars);
|
---|
1419 | pThis->iVar--;
|
---|
1420 | }
|
---|
1421 | free(pThis);
|
---|
1422 | }
|
---|
1423 |
|
---|
1424 |
|
---|
1425 | /**
|
---|
1426 | * Instantiates an expression evaluator.
|
---|
1427 | *
|
---|
1428 | * @returns The instance.
|
---|
1429 | *
|
---|
1430 | * @param pszExpr What to parse.
|
---|
1431 | * This must stick around until ifcond_destroy.
|
---|
1432 | */
|
---|
1433 | static PIFCOND ifcond_create(char const *pszExpr)
|
---|
1434 | {
|
---|
1435 | PIFCOND pThis = (PIFCOND)xmalloc(sizeof(*pThis));
|
---|
1436 | pThis->pszExpr = pszExpr;
|
---|
1437 | pThis->psz = pszExpr;
|
---|
1438 | pThis->pFileLoc = NULL;
|
---|
1439 | pThis->pPending = NULL;
|
---|
1440 | pThis->iVar = -1;
|
---|
1441 | pThis->iOp = -1;
|
---|
1442 |
|
---|
1443 | ifcond_map_init();
|
---|
1444 | return pThis;
|
---|
1445 | }
|
---|
1446 |
|
---|
1447 |
|
---|
1448 | /**
|
---|
1449 | * Evaluates the given if expression.
|
---|
1450 | *
|
---|
1451 | * @returns -1, 0 or 1.
|
---|
1452 | * @retval -1 if the expression is invalid.
|
---|
1453 | * @retval 0 if the expression is true
|
---|
1454 | * @retval 1 if the expression is false.
|
---|
1455 | *
|
---|
1456 | * @param line The expression. Can modify this as we like.
|
---|
1457 | * @param flocp The file location, used for errors.
|
---|
1458 | */
|
---|
1459 | int ifcond(char *line, const struct floc *flocp)
|
---|
1460 | {
|
---|
1461 | /*
|
---|
1462 | * Instantiate the expression evaluator and let
|
---|
1463 | * it have a go at it.
|
---|
1464 | */
|
---|
1465 | int rc = -1;
|
---|
1466 | PIFCOND pIfCond = ifcond_create(line);
|
---|
1467 | pIfCond->pFileLoc = flocp;
|
---|
1468 | if (ifcond_eval(pIfCond) >= kIfCondRet_Ok)
|
---|
1469 | {
|
---|
1470 | /*
|
---|
1471 | * Convert the result (on top of the stack) to boolean and
|
---|
1472 | * set our return value accordingly.
|
---|
1473 | */
|
---|
1474 | if (ifcond_var_make_bool(&pIfCond->aVars[0]))
|
---|
1475 | rc = 0;
|
---|
1476 | else
|
---|
1477 | rc = 1;
|
---|
1478 | }
|
---|
1479 | ifcond_destroy(pIfCond);
|
---|
1480 |
|
---|
1481 | return rc;
|
---|
1482 | }
|
---|
1483 |
|
---|
1484 |
|
---|
1485 | #endif /* CONFIG_WITH_IF_CONDITIONALS */
|
---|
1486 |
|
---|