1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // Copyright (C) 2018 Masahiro Yamada <[email protected]> 4 5 #include <ctype.h> 6 #include <stdarg.h> 7 #include <stdbool.h> 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <string.h> 11 12 #include "list.h" 13 #include "lkc.h" 14 #include "preprocess.h" 15 16 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 17 18 static char *expand_string_with_args(const char *in, int argc, char *argv[]); 19 static char *expand_string(const char *in); 20 21 static void __attribute__((noreturn)) pperror(const char *format, ...) 22 { 23 va_list ap; 24 25 fprintf(stderr, "%s:%d: ", current_file->name, yylineno); 26 va_start(ap, format); 27 vfprintf(stderr, format, ap); 28 va_end(ap); 29 fprintf(stderr, "\n"); 30 31 exit(1); 32 } 33 34 /* 35 * Environment variables 36 */ 37 static LIST_HEAD(env_list); 38 39 struct env { 40 char *name; 41 char *value; 42 struct list_head node; 43 }; 44 45 static void env_add(const char *name, const char *value) 46 { 47 struct env *e; 48 49 e = xmalloc(sizeof(*e)); 50 e->name = xstrdup(name); 51 e->value = xstrdup(value); 52 53 list_add_tail(&e->node, &env_list); 54 } 55 56 static void env_del(struct env *e) 57 { 58 list_del(&e->node); 59 free(e->name); 60 free(e->value); 61 free(e); 62 } 63 64 /* The returned pointer must be freed when done */ 65 static char *env_expand(const char *name) 66 { 67 struct env *e; 68 const char *value; 69 70 if (!*name) 71 return NULL; 72 73 list_for_each_entry(e, &env_list, node) { 74 if (!strcmp(name, e->name)) 75 return xstrdup(e->value); 76 } 77 78 value = getenv(name); 79 if (!value) 80 return NULL; 81 82 /* 83 * We need to remember all referenced environment variables. 84 * They will be written out to include/config/auto.conf.cmd 85 */ 86 env_add(name, value); 87 88 return xstrdup(value); 89 } 90 91 void env_write_dep(struct gstr *s) 92 { 93 struct env *e, *tmp; 94 95 list_for_each_entry_safe(e, tmp, &env_list, node) { 96 str_printf(s, 97 "\n" 98 "ifneq \"$(%s)\" \"%s\"\n" 99 "$(autoconfig): FORCE\n" 100 "endif\n", 101 e->name, e->value); 102 env_del(e); 103 } 104 } 105 106 /* 107 * Built-in functions 108 */ 109 struct function { 110 const char *name; 111 unsigned int min_args; 112 unsigned int max_args; 113 char *(*func)(int argc, char *argv[]); 114 }; 115 116 static char *do_error_if(int argc, char *argv[]) 117 { 118 if (!strcmp(argv[0], "y")) 119 pperror("%s", argv[1]); 120 121 return xstrdup(""); 122 } 123 124 static char *do_filename(int argc, char *argv[]) 125 { 126 return xstrdup(current_file->name); 127 } 128 129 static char *do_info(int argc, char *argv[]) 130 { 131 printf("%s\n", argv[0]); 132 133 return xstrdup(""); 134 } 135 136 static char *do_lineno(int argc, char *argv[]) 137 { 138 char buf[16]; 139 140 sprintf(buf, "%d", yylineno); 141 142 return xstrdup(buf); 143 } 144 145 static char *do_shell(int argc, char *argv[]) 146 { 147 FILE *p; 148 char buf[4096]; 149 char *cmd; 150 size_t nread; 151 int i; 152 153 cmd = argv[0]; 154 155 p = popen(cmd, "r"); 156 if (!p) { 157 perror(cmd); 158 exit(1); 159 } 160 161 nread = fread(buf, 1, sizeof(buf), p); 162 if (nread == sizeof(buf)) 163 nread--; 164 165 /* remove trailing new lines */ 166 while (nread > 0 && buf[nread - 1] == '\n') 167 nread--; 168 169 buf[nread] = 0; 170 171 /* replace a new line with a space */ 172 for (i = 0; i < nread; i++) { 173 if (buf[i] == '\n') 174 buf[i] = ' '; 175 } 176 177 if (pclose(p) == -1) { 178 perror(cmd); 179 exit(1); 180 } 181 182 return xstrdup(buf); 183 } 184 185 static char *do_warning_if(int argc, char *argv[]) 186 { 187 if (!strcmp(argv[0], "y")) 188 fprintf(stderr, "%s:%d: %s\n", 189 current_file->name, yylineno, argv[1]); 190 191 return xstrdup(""); 192 } 193 194 static const struct function function_table[] = { 195 /* Name MIN MAX Function */ 196 { "error-if", 2, 2, do_error_if }, 197 { "filename", 0, 0, do_filename }, 198 { "info", 1, 1, do_info }, 199 { "lineno", 0, 0, do_lineno }, 200 { "shell", 1, 1, do_shell }, 201 { "warning-if", 2, 2, do_warning_if }, 202 }; 203 204 #define FUNCTION_MAX_ARGS 16 205 206 static char *function_expand(const char *name, int argc, char *argv[]) 207 { 208 const struct function *f; 209 int i; 210 211 for (i = 0; i < ARRAY_SIZE(function_table); i++) { 212 f = &function_table[i]; 213 if (strcmp(f->name, name)) 214 continue; 215 216 if (argc < f->min_args) 217 pperror("too few function arguments passed to '%s'", 218 name); 219 220 if (argc > f->max_args) 221 pperror("too many function arguments passed to '%s'", 222 name); 223 224 return f->func(argc, argv); 225 } 226 227 return NULL; 228 } 229 230 /* 231 * Variables (and user-defined functions) 232 */ 233 static LIST_HEAD(variable_list); 234 235 struct variable { 236 char *name; 237 char *value; 238 enum variable_flavor flavor; 239 int exp_count; 240 struct list_head node; 241 }; 242 243 static struct variable *variable_lookup(const char *name) 244 { 245 struct variable *v; 246 247 list_for_each_entry(v, &variable_list, node) { 248 if (!strcmp(name, v->name)) 249 return v; 250 } 251 252 return NULL; 253 } 254 255 static char *variable_expand(const char *name, int argc, char *argv[]) 256 { 257 struct variable *v; 258 char *res; 259 260 v = variable_lookup(name); 261 if (!v) 262 return NULL; 263 264 if (argc == 0 && v->exp_count) 265 pperror("Recursive variable '%s' references itself (eventually)", 266 name); 267 268 if (v->exp_count > 1000) 269 pperror("Too deep recursive expansion"); 270 271 v->exp_count++; 272 273 if (v->flavor == VAR_RECURSIVE) 274 res = expand_string_with_args(v->value, argc, argv); 275 else 276 res = xstrdup(v->value); 277 278 v->exp_count--; 279 280 return res; 281 } 282 283 void variable_add(const char *name, const char *value, 284 enum variable_flavor flavor) 285 { 286 struct variable *v; 287 char *new_value; 288 bool append = false; 289 290 v = variable_lookup(name); 291 if (v) { 292 /* For defined variables, += inherits the existing flavor */ 293 if (flavor == VAR_APPEND) { 294 flavor = v->flavor; 295 append = true; 296 } else { 297 free(v->value); 298 } 299 } else { 300 /* For undefined variables, += assumes the recursive flavor */ 301 if (flavor == VAR_APPEND) 302 flavor = VAR_RECURSIVE; 303 304 v = xmalloc(sizeof(*v)); 305 v->name = xstrdup(name); 306 v->exp_count = 0; 307 list_add_tail(&v->node, &variable_list); 308 } 309 310 v->flavor = flavor; 311 312 if (flavor == VAR_SIMPLE) 313 new_value = expand_string(value); 314 else 315 new_value = xstrdup(value); 316 317 if (append) { 318 v->value = xrealloc(v->value, 319 strlen(v->value) + strlen(new_value) + 2); 320 strcat(v->value, " "); 321 strcat(v->value, new_value); 322 free(new_value); 323 } else { 324 v->value = new_value; 325 } 326 } 327 328 static void variable_del(struct variable *v) 329 { 330 list_del(&v->node); 331 free(v->name); 332 free(v->value); 333 free(v); 334 } 335 336 void variable_all_del(void) 337 { 338 struct variable *v, *tmp; 339 340 list_for_each_entry_safe(v, tmp, &variable_list, node) 341 variable_del(v); 342 } 343 344 /* 345 * Evaluate a clause with arguments. argc/argv are arguments from the upper 346 * function call. 347 * 348 * Returned string must be freed when done 349 */ 350 static char *eval_clause(const char *str, size_t len, int argc, char *argv[]) 351 { 352 char *tmp, *name, *res, *endptr, *prev, *p; 353 int new_argc = 0; 354 char *new_argv[FUNCTION_MAX_ARGS]; 355 int nest = 0; 356 int i; 357 unsigned long n; 358 359 tmp = xstrndup(str, len); 360 361 /* 362 * If variable name is '1', '2', etc. It is generally an argument 363 * from a user-function call (i.e. local-scope variable). If not 364 * available, then look-up global-scope variables. 365 */ 366 n = strtoul(tmp, &endptr, 10); 367 if (!*endptr && n > 0 && n <= argc) { 368 res = xstrdup(argv[n - 1]); 369 goto free_tmp; 370 } 371 372 prev = p = tmp; 373 374 /* 375 * Split into tokens 376 * The function name and arguments are separated by a comma. 377 * For example, if the function call is like this: 378 * $(foo,$(x),$(y)) 379 * 380 * The input string for this helper should be: 381 * foo,$(x),$(y) 382 * 383 * and split into: 384 * new_argv[0] = 'foo' 385 * new_argv[1] = '$(x)' 386 * new_argv[2] = '$(y)' 387 */ 388 while (*p) { 389 if (nest == 0 && *p == ',') { 390 *p = 0; 391 if (new_argc >= FUNCTION_MAX_ARGS) 392 pperror("too many function arguments"); 393 new_argv[new_argc++] = prev; 394 prev = p + 1; 395 } else if (*p == '(') { 396 nest++; 397 } else if (*p == ')') { 398 nest--; 399 } 400 401 p++; 402 } 403 404 if (new_argc >= FUNCTION_MAX_ARGS) 405 pperror("too many function arguments"); 406 new_argv[new_argc++] = prev; 407 408 /* 409 * Shift arguments 410 * new_argv[0] represents a function name or a variable name. Put it 411 * into 'name', then shift the rest of the arguments. This simplifies 412 * 'const' handling. 413 */ 414 name = expand_string_with_args(new_argv[0], argc, argv); 415 new_argc--; 416 for (i = 0; i < new_argc; i++) 417 new_argv[i] = expand_string_with_args(new_argv[i + 1], 418 argc, argv); 419 420 /* Search for variables */ 421 res = variable_expand(name, new_argc, new_argv); 422 if (res) 423 goto free; 424 425 /* Look for built-in functions */ 426 res = function_expand(name, new_argc, new_argv); 427 if (res) 428 goto free; 429 430 /* Last, try environment variable */ 431 if (new_argc == 0) { 432 res = env_expand(name); 433 if (res) 434 goto free; 435 } 436 437 res = xstrdup(""); 438 free: 439 for (i = 0; i < new_argc; i++) 440 free(new_argv[i]); 441 free(name); 442 free_tmp: 443 free(tmp); 444 445 return res; 446 } 447 448 /* 449 * Expand a string that follows '$' 450 * 451 * For example, if the input string is 452 * ($(FOO)$($(BAR)))$(BAZ) 453 * this helper evaluates 454 * $($(FOO)$($(BAR))) 455 * and returns a new string containing the expansion (note that the string is 456 * recursively expanded), also advancing 'str' to point to the next character 457 * after the corresponding closing parenthesis, in this case, *str will be 458 * $(BAR) 459 */ 460 static char *expand_dollar_with_args(const char **str, int argc, char *argv[]) 461 { 462 const char *p = *str; 463 const char *q; 464 int nest = 0; 465 466 /* 467 * In Kconfig, variable/function references always start with "$(". 468 * Neither single-letter variables as in $A nor curly braces as in ${CC} 469 * are supported. '$' not followed by '(' loses its special meaning. 470 */ 471 if (*p != '(') { 472 *str = p; 473 return xstrdup("$"); 474 } 475 476 p++; 477 q = p; 478 while (*q) { 479 if (*q == '(') { 480 nest++; 481 } else if (*q == ')') { 482 if (nest-- == 0) 483 break; 484 } 485 q++; 486 } 487 488 if (!*q) 489 pperror("unterminated reference to '%s': missing ')'", p); 490 491 /* Advance 'str' to after the expanded initial portion of the string */ 492 *str = q + 1; 493 494 return eval_clause(p, q - p, argc, argv); 495 } 496 497 char *expand_dollar(const char **str) 498 { 499 return expand_dollar_with_args(str, 0, NULL); 500 } 501 502 static char *__expand_string(const char **str, bool (*is_end)(char c), 503 int argc, char *argv[]) 504 { 505 const char *in, *p; 506 char *expansion, *out; 507 size_t in_len, out_len; 508 509 out = xmalloc(1); 510 *out = 0; 511 out_len = 1; 512 513 p = in = *str; 514 515 while (1) { 516 if (*p == '$') { 517 in_len = p - in; 518 p++; 519 expansion = expand_dollar_with_args(&p, argc, argv); 520 out_len += in_len + strlen(expansion); 521 out = xrealloc(out, out_len); 522 strncat(out, in, in_len); 523 strcat(out, expansion); 524 free(expansion); 525 in = p; 526 continue; 527 } 528 529 if (is_end(*p)) 530 break; 531 532 p++; 533 } 534 535 in_len = p - in; 536 out_len += in_len; 537 out = xrealloc(out, out_len); 538 strncat(out, in, in_len); 539 540 /* Advance 'str' to the end character */ 541 *str = p; 542 543 return out; 544 } 545 546 static bool is_end_of_str(char c) 547 { 548 return !c; 549 } 550 551 /* 552 * Expand variables and functions in the given string. Undefined variables 553 * expand to an empty string. 554 * The returned string must be freed when done. 555 */ 556 static char *expand_string_with_args(const char *in, int argc, char *argv[]) 557 { 558 return __expand_string(&in, is_end_of_str, argc, argv); 559 } 560 561 static char *expand_string(const char *in) 562 { 563 return expand_string_with_args(in, 0, NULL); 564 } 565 566 static bool is_end_of_token(char c) 567 { 568 return !(isalnum(c) || c == '_' || c == '-'); 569 } 570 571 /* 572 * Expand variables in a token. The parsing stops when a token separater 573 * (in most cases, it is a whitespace) is encountered. 'str' is updated to 574 * point to the next character. 575 * 576 * The returned string must be freed when done. 577 */ 578 char *expand_one_token(const char **str) 579 { 580 return __expand_string(str, is_end_of_token, 0, NULL); 581 } 582