* Evaluate a clause with arguments. argc/argv are arguments from the upper * function call. * * Returned string must be freed when done */
| 344 | * Returned string must be freed when done |
| 345 | */ |
| 346 | static char *eval_clause(const char *str, size_t len, int argc, char *argv[]) |
| 347 | { |
| 348 | char *tmp, *name, *res, *endptr, *prev, *p; |
| 349 | int new_argc = 0; |
| 350 | char *new_argv[FUNCTION_MAX_ARGS]; |
| 351 | int nest = 0; |
| 352 | int i; |
| 353 | unsigned long n; |
| 354 | |
| 355 | tmp = xstrndup(str, len); |
| 356 | |
| 357 | /* |
| 358 | * If variable name is '1', '2', etc. It is generally an argument |
| 359 | * from a user-function call (i.e. local-scope variable). If not |
| 360 | * available, then look-up global-scope variables. |
| 361 | */ |
| 362 | n = strtoul(tmp, &endptr, 10); |
| 363 | if (!*endptr && n > 0 && n <= argc) { |
| 364 | res = xstrdup(argv[n - 1]); |
| 365 | goto free_tmp; |
| 366 | } |
| 367 | |
| 368 | prev = p = tmp; |
| 369 | |
| 370 | /* |
| 371 | * Split into tokens |
| 372 | * The function name and arguments are separated by a comma. |
| 373 | * For example, if the function call is like this: |
| 374 | * $(foo,$(x),$(y)) |
| 375 | * |
| 376 | * The input string for this helper should be: |
| 377 | * foo,$(x),$(y) |
| 378 | * |
| 379 | * and split into: |
| 380 | * new_argv[0] = 'foo' |
| 381 | * new_argv[1] = '$(x)' |
| 382 | * new_argv[2] = '$(y)' |
| 383 | */ |
| 384 | while (*p) { |
| 385 | if (nest == 0 && *p == ',') { |
| 386 | *p = 0; |
| 387 | if (new_argc >= FUNCTION_MAX_ARGS) |
| 388 | pperror("too many function arguments"); |
| 389 | new_argv[new_argc++] = prev; |
| 390 | prev = p + 1; |
| 391 | } else if (*p == '(') { |
| 392 | nest++; |
| 393 | } else if (*p == ')') { |
| 394 | nest--; |
| 395 | } |
| 396 | |
| 397 | p++; |
| 398 | } |
| 399 | |
| 400 | if (new_argc >= FUNCTION_MAX_ARGS) |
| 401 | pperror("too many function arguments"); |
| 402 | new_argv[new_argc++] = prev; |
| 403 |
no test coverage detected