1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org> 4 */ 5 6 #include <sys/stat.h> 7 #include <ctype.h> 8 #include <errno.h> 9 #include <fcntl.h> 10 #include <stdarg.h> 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <string.h> 14 #include <time.h> 15 #include <unistd.h> 16 17 #include "lkc.h" 18 19 /* return true if 'path' exists, false otherwise */ 20 static bool is_present(const char *path) 21 { 22 struct stat st; 23 24 return !stat(path, &st); 25 } 26 27 /* return true if 'path' exists and it is a directory, false otherwise */ 28 static bool is_dir(const char *path) 29 { 30 struct stat st; 31 32 if (stat(path, &st)) 33 return 0; 34 35 return S_ISDIR(st.st_mode); 36 } 37 38 /* 39 * Create the parent directory of the given path. 40 * 41 * For example, if 'include/config/auto.conf' is given, create 'include/config'. 42 */ 43 static int make_parent_dir(const char *path) 44 { 45 char tmp[PATH_MAX + 1]; 46 char *p; 47 48 strncpy(tmp, path, sizeof(tmp)); 49 tmp[sizeof(tmp) - 1] = 0; 50 51 /* Remove the base name. Just return if nothing is left */ 52 p = strrchr(tmp, '/'); 53 if (!p) 54 return 0; 55 *(p + 1) = 0; 56 57 /* Just in case it is an absolute path */ 58 p = tmp; 59 while (*p == '/') 60 p++; 61 62 while ((p = strchr(p, '/'))) { 63 *p = 0; 64 65 /* skip if the directory exists */ 66 if (!is_dir(tmp) && mkdir(tmp, 0755)) 67 return -1; 68 69 *p = '/'; 70 while (*p == '/') 71 p++; 72 } 73 74 return 0; 75 } 76 77 static char depfile_path[PATH_MAX]; 78 static size_t depfile_prefix_len; 79 80 /* touch depfile for symbol 'name' */ 81 static int conf_touch_dep(const char *name) 82 { 83 int fd, ret; 84 const char *s; 85 char *d, c; 86 87 /* check overflow: prefix + name + ".h" + '\0' must fit in buffer. */ 88 if (depfile_prefix_len + strlen(name) + 3 > sizeof(depfile_path)) 89 return -1; 90 91 d = depfile_path + depfile_prefix_len; 92 s = name; 93 94 while ((c = *s++)) 95 *d++ = (c == '_') ? '/' : tolower(c); 96 strcpy(d, ".h"); 97 98 /* Assume directory path already exists. */ 99 fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644); 100 if (fd == -1) { 101 if (errno != ENOENT) 102 return -1; 103 104 ret = make_parent_dir(depfile_path); 105 if (ret) 106 return ret; 107 108 /* Try it again. */ 109 fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644); 110 if (fd == -1) 111 return -1; 112 } 113 close(fd); 114 115 return 0; 116 } 117 118 struct conf_printer { 119 void (*print_symbol)(FILE *, struct symbol *, const char *, void *); 120 void (*print_comment)(FILE *, const char *, void *); 121 }; 122 123 static void conf_warning(const char *fmt, ...) 124 __attribute__ ((format (printf, 1, 2))); 125 126 static void conf_message(const char *fmt, ...) 127 __attribute__ ((format (printf, 1, 2))); 128 129 static const char *conf_filename; 130 static int conf_lineno, conf_warnings; 131 132 const char conf_defname[] = "arch/$(ARCH)/defconfig"; 133 134 static void conf_warning(const char *fmt, ...) 135 { 136 va_list ap; 137 va_start(ap, fmt); 138 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno); 139 vfprintf(stderr, fmt, ap); 140 fprintf(stderr, "\n"); 141 va_end(ap); 142 conf_warnings++; 143 } 144 145 static void conf_default_message_callback(const char *s) 146 { 147 printf("#\n# "); 148 printf("%s", s); 149 printf("\n#\n"); 150 } 151 152 static void (*conf_message_callback)(const char *s) = 153 conf_default_message_callback; 154 void conf_set_message_callback(void (*fn)(const char *s)) 155 { 156 conf_message_callback = fn; 157 } 158 159 static void conf_message(const char *fmt, ...) 160 { 161 va_list ap; 162 char buf[4096]; 163 164 if (!conf_message_callback) 165 return; 166 167 va_start(ap, fmt); 168 169 vsnprintf(buf, sizeof(buf), fmt, ap); 170 conf_message_callback(buf); 171 va_end(ap); 172 } 173 174 const char *conf_get_configname(void) 175 { 176 char *name = getenv("KCONFIG_CONFIG"); 177 178 return name ? name : ".config"; 179 } 180 181 const char *conf_get_autoconfig_name(void) 182 { 183 char *name = getenv("KCONFIG_AUTOCONFIG"); 184 185 return name ? name : "include/config/auto.conf"; 186 } 187 188 char *conf_get_default_confname(void) 189 { 190 static char fullname[PATH_MAX+1]; 191 char *env, *name; 192 193 name = expand_string(conf_defname); 194 env = getenv(SRCTREE); 195 if (env) { 196 sprintf(fullname, "%s/%s", env, name); 197 if (is_present(fullname)) 198 return fullname; 199 } 200 return name; 201 } 202 203 static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p) 204 { 205 char *p2; 206 207 switch (sym->type) { 208 case S_TRISTATE: 209 if (p[0] == 'm') { 210 sym->def[def].tri = mod; 211 sym->flags |= def_flags; 212 break; 213 } 214 /* fall through */ 215 case S_BOOLEAN: 216 if (p[0] == 'y') { 217 sym->def[def].tri = yes; 218 sym->flags |= def_flags; 219 break; 220 } 221 if (p[0] == 'n') { 222 sym->def[def].tri = no; 223 sym->flags |= def_flags; 224 break; 225 } 226 if (def != S_DEF_AUTO) 227 conf_warning("symbol value '%s' invalid for %s", 228 p, sym->name); 229 return 1; 230 case S_STRING: 231 if (*p++ != '"') 232 break; 233 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) { 234 if (*p2 == '"') { 235 *p2 = 0; 236 break; 237 } 238 memmove(p2, p2 + 1, strlen(p2)); 239 } 240 if (!p2) { 241 if (def != S_DEF_AUTO) 242 conf_warning("invalid string found"); 243 return 1; 244 } 245 /* fall through */ 246 case S_INT: 247 case S_HEX: 248 if (sym_string_valid(sym, p)) { 249 sym->def[def].val = xstrdup(p); 250 sym->flags |= def_flags; 251 } else { 252 if (def != S_DEF_AUTO) 253 conf_warning("symbol value '%s' invalid for %s", 254 p, sym->name); 255 return 1; 256 } 257 break; 258 default: 259 ; 260 } 261 return 0; 262 } 263 264 #define LINE_GROWTH 16 265 static int add_byte(int c, char **lineptr, size_t slen, size_t *n) 266 { 267 char *nline; 268 size_t new_size = slen + 1; 269 if (new_size > *n) { 270 new_size += LINE_GROWTH - 1; 271 new_size *= 2; 272 nline = xrealloc(*lineptr, new_size); 273 if (!nline) 274 return -1; 275 276 *lineptr = nline; 277 *n = new_size; 278 } 279 280 (*lineptr)[slen] = c; 281 282 return 0; 283 } 284 285 static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream) 286 { 287 char *line = *lineptr; 288 size_t slen = 0; 289 290 for (;;) { 291 int c = getc(stream); 292 293 switch (c) { 294 case '\n': 295 if (add_byte(c, &line, slen, n) < 0) 296 goto e_out; 297 slen++; 298 /* fall through */ 299 case EOF: 300 if (add_byte('\0', &line, slen, n) < 0) 301 goto e_out; 302 *lineptr = line; 303 if (slen == 0) 304 return -1; 305 return slen; 306 default: 307 if (add_byte(c, &line, slen, n) < 0) 308 goto e_out; 309 slen++; 310 } 311 } 312 313 e_out: 314 line[slen-1] = '\0'; 315 *lineptr = line; 316 return -1; 317 } 318 319 int conf_read_simple(const char *name, int def) 320 { 321 FILE *in = NULL; 322 char *line = NULL; 323 size_t line_asize = 0; 324 char *p, *p2; 325 struct symbol *sym; 326 int i, def_flags; 327 328 if (name) { 329 in = zconf_fopen(name); 330 } else { 331 struct property *prop; 332 333 name = conf_get_configname(); 334 in = zconf_fopen(name); 335 if (in) 336 goto load; 337 sym_add_change_count(1); 338 if (!sym_defconfig_list) 339 return 1; 340 341 for_all_defaults(sym_defconfig_list, prop) { 342 if (expr_calc_value(prop->visible.expr) == no || 343 prop->expr->type != E_SYMBOL) 344 continue; 345 sym_calc_value(prop->expr->left.sym); 346 name = sym_get_string_value(prop->expr->left.sym); 347 in = zconf_fopen(name); 348 if (in) { 349 conf_message("using defaults found in %s", 350 name); 351 goto load; 352 } 353 } 354 } 355 if (!in) 356 return 1; 357 358 load: 359 conf_filename = name; 360 conf_lineno = 0; 361 conf_warnings = 0; 362 363 def_flags = SYMBOL_DEF << def; 364 for_all_symbols(i, sym) { 365 sym->flags |= SYMBOL_CHANGED; 366 sym->flags &= ~(def_flags|SYMBOL_VALID); 367 if (sym_is_choice(sym)) 368 sym->flags |= def_flags; 369 switch (sym->type) { 370 case S_INT: 371 case S_HEX: 372 case S_STRING: 373 if (sym->def[def].val) 374 free(sym->def[def].val); 375 /* fall through */ 376 default: 377 sym->def[def].val = NULL; 378 sym->def[def].tri = no; 379 } 380 } 381 382 while (compat_getline(&line, &line_asize, in) != -1) { 383 conf_lineno++; 384 sym = NULL; 385 if (line[0] == '#') { 386 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_))) 387 continue; 388 p = strchr(line + 2 + strlen(CONFIG_), ' '); 389 if (!p) 390 continue; 391 *p++ = 0; 392 if (strncmp(p, "is not set", 10)) 393 continue; 394 if (def == S_DEF_USER) { 395 sym = sym_find(line + 2 + strlen(CONFIG_)); 396 if (!sym) { 397 sym_add_change_count(1); 398 continue; 399 } 400 } else { 401 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0); 402 if (sym->type == S_UNKNOWN) 403 sym->type = S_BOOLEAN; 404 } 405 if (sym->flags & def_flags) { 406 conf_warning("override: reassigning to symbol %s", sym->name); 407 } 408 switch (sym->type) { 409 case S_BOOLEAN: 410 case S_TRISTATE: 411 sym->def[def].tri = no; 412 sym->flags |= def_flags; 413 break; 414 default: 415 ; 416 } 417 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) { 418 p = strchr(line + strlen(CONFIG_), '='); 419 if (!p) 420 continue; 421 *p++ = 0; 422 p2 = strchr(p, '\n'); 423 if (p2) { 424 *p2-- = 0; 425 if (*p2 == '\r') 426 *p2 = 0; 427 } 428 429 sym = sym_find(line + strlen(CONFIG_)); 430 if (!sym) { 431 if (def == S_DEF_AUTO) 432 /* 433 * Reading from include/config/auto.conf 434 * If CONFIG_FOO previously existed in 435 * auto.conf but it is missing now, 436 * include/config/foo.h must be touched. 437 */ 438 conf_touch_dep(line + strlen(CONFIG_)); 439 else 440 sym_add_change_count(1); 441 continue; 442 } 443 444 if (sym->flags & def_flags) { 445 conf_warning("override: reassigning to symbol %s", sym->name); 446 } 447 if (conf_set_sym_val(sym, def, def_flags, p)) 448 continue; 449 } else { 450 if (line[0] != '\r' && line[0] != '\n') 451 conf_warning("unexpected data: %.*s", 452 (int)strcspn(line, "\r\n"), line); 453 454 continue; 455 } 456 457 if (sym && sym_is_choice_value(sym)) { 458 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym)); 459 switch (sym->def[def].tri) { 460 case no: 461 break; 462 case mod: 463 if (cs->def[def].tri == yes) { 464 conf_warning("%s creates inconsistent choice state", sym->name); 465 cs->flags &= ~def_flags; 466 } 467 break; 468 case yes: 469 if (cs->def[def].tri != no) 470 conf_warning("override: %s changes choice state", sym->name); 471 cs->def[def].val = sym; 472 break; 473 } 474 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri); 475 } 476 } 477 free(line); 478 fclose(in); 479 return 0; 480 } 481 482 int conf_read(const char *name) 483 { 484 struct symbol *sym; 485 int conf_unsaved = 0; 486 int i; 487 488 sym_set_change_count(0); 489 490 if (conf_read_simple(name, S_DEF_USER)) { 491 sym_calc_value(modules_sym); 492 return 1; 493 } 494 495 sym_calc_value(modules_sym); 496 497 for_all_symbols(i, sym) { 498 sym_calc_value(sym); 499 if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE)) 500 continue; 501 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) { 502 /* check that calculated value agrees with saved value */ 503 switch (sym->type) { 504 case S_BOOLEAN: 505 case S_TRISTATE: 506 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym)) 507 break; 508 if (!sym_is_choice(sym)) 509 continue; 510 /* fall through */ 511 default: 512 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val)) 513 continue; 514 break; 515 } 516 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE)) 517 /* no previous value and not saved */ 518 continue; 519 conf_unsaved++; 520 /* maybe print value in verbose mode... */ 521 } 522 523 for_all_symbols(i, sym) { 524 if (sym_has_value(sym) && !sym_is_choice_value(sym)) { 525 /* Reset values of generates values, so they'll appear 526 * as new, if they should become visible, but that 527 * doesn't quite work if the Kconfig and the saved 528 * configuration disagree. 529 */ 530 if (sym->visible == no && !conf_unsaved) 531 sym->flags &= ~SYMBOL_DEF_USER; 532 switch (sym->type) { 533 case S_STRING: 534 case S_INT: 535 case S_HEX: 536 /* Reset a string value if it's out of range */ 537 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val)) 538 break; 539 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER); 540 conf_unsaved++; 541 break; 542 default: 543 break; 544 } 545 } 546 } 547 548 sym_add_change_count(conf_warnings || conf_unsaved); 549 550 return 0; 551 } 552 553 /* 554 * Kconfig configuration printer 555 * 556 * This printer is used when generating the resulting configuration after 557 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by 558 * passing a non-NULL argument to the printer. 559 * 560 */ 561 static void 562 kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg) 563 { 564 565 switch (sym->type) { 566 case S_BOOLEAN: 567 case S_TRISTATE: 568 if (*value == 'n') { 569 bool skip_unset = (arg != NULL); 570 571 if (!skip_unset) 572 fprintf(fp, "# %s%s is not set\n", 573 CONFIG_, sym->name); 574 return; 575 } 576 break; 577 default: 578 break; 579 } 580 581 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value); 582 } 583 584 static void 585 kconfig_print_comment(FILE *fp, const char *value, void *arg) 586 { 587 const char *p = value; 588 size_t l; 589 590 for (;;) { 591 l = strcspn(p, "\n"); 592 fprintf(fp, "#"); 593 if (l) { 594 fprintf(fp, " "); 595 xfwrite(p, l, 1, fp); 596 p += l; 597 } 598 fprintf(fp, "\n"); 599 if (*p++ == '\0') 600 break; 601 } 602 } 603 604 static struct conf_printer kconfig_printer_cb = 605 { 606 .print_symbol = kconfig_print_symbol, 607 .print_comment = kconfig_print_comment, 608 }; 609 610 /* 611 * Header printer 612 * 613 * This printer is used when generating the `include/generated/autoconf.h' file. 614 */ 615 static void 616 header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg) 617 { 618 619 switch (sym->type) { 620 case S_BOOLEAN: 621 case S_TRISTATE: { 622 const char *suffix = ""; 623 624 switch (*value) { 625 case 'n': 626 break; 627 case 'm': 628 suffix = "_MODULE"; 629 /* fall through */ 630 default: 631 fprintf(fp, "#define %s%s%s 1\n", 632 CONFIG_, sym->name, suffix); 633 } 634 break; 635 } 636 case S_HEX: { 637 const char *prefix = ""; 638 639 if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X')) 640 prefix = "0x"; 641 fprintf(fp, "#define %s%s %s%s\n", 642 CONFIG_, sym->name, prefix, value); 643 break; 644 } 645 case S_STRING: 646 case S_INT: 647 fprintf(fp, "#define %s%s %s\n", 648 CONFIG_, sym->name, value); 649 break; 650 default: 651 break; 652 } 653 654 } 655 656 static void 657 header_print_comment(FILE *fp, const char *value, void *arg) 658 { 659 const char *p = value; 660 size_t l; 661 662 fprintf(fp, "/*\n"); 663 for (;;) { 664 l = strcspn(p, "\n"); 665 fprintf(fp, " *"); 666 if (l) { 667 fprintf(fp, " "); 668 xfwrite(p, l, 1, fp); 669 p += l; 670 } 671 fprintf(fp, "\n"); 672 if (*p++ == '\0') 673 break; 674 } 675 fprintf(fp, " */\n"); 676 } 677 678 static struct conf_printer header_printer_cb = 679 { 680 .print_symbol = header_print_symbol, 681 .print_comment = header_print_comment, 682 }; 683 684 /* 685 * Tristate printer 686 * 687 * This printer is used when generating the `include/config/tristate.conf' file. 688 */ 689 static void 690 tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg) 691 { 692 693 if (sym->type == S_TRISTATE && *value != 'n') 694 fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value)); 695 } 696 697 static struct conf_printer tristate_printer_cb = 698 { 699 .print_symbol = tristate_print_symbol, 700 .print_comment = kconfig_print_comment, 701 }; 702 703 static void conf_write_symbol(FILE *fp, struct symbol *sym, 704 struct conf_printer *printer, void *printer_arg) 705 { 706 const char *str; 707 708 switch (sym->type) { 709 case S_UNKNOWN: 710 break; 711 case S_STRING: 712 str = sym_get_string_value(sym); 713 str = sym_escape_string_value(str); 714 printer->print_symbol(fp, sym, str, printer_arg); 715 free((void *)str); 716 break; 717 default: 718 str = sym_get_string_value(sym); 719 printer->print_symbol(fp, sym, str, printer_arg); 720 } 721 } 722 723 static void 724 conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg) 725 { 726 char buf[256]; 727 728 snprintf(buf, sizeof(buf), 729 "\n" 730 "Automatically generated file; DO NOT EDIT.\n" 731 "%s\n", 732 rootmenu.prompt->text); 733 734 printer->print_comment(fp, buf, printer_arg); 735 } 736 737 /* 738 * Write out a minimal config. 739 * All values that has default values are skipped as this is redundant. 740 */ 741 int conf_write_defconfig(const char *filename) 742 { 743 struct symbol *sym; 744 struct menu *menu; 745 FILE *out; 746 747 out = fopen(filename, "w"); 748 if (!out) 749 return 1; 750 751 sym_clear_all_valid(); 752 753 /* Traverse all menus to find all relevant symbols */ 754 menu = rootmenu.list; 755 756 while (menu != NULL) 757 { 758 sym = menu->sym; 759 if (sym == NULL) { 760 if (!menu_is_visible(menu)) 761 goto next_menu; 762 } else if (!sym_is_choice(sym)) { 763 sym_calc_value(sym); 764 if (!(sym->flags & SYMBOL_WRITE)) 765 goto next_menu; 766 sym->flags &= ~SYMBOL_WRITE; 767 /* If we cannot change the symbol - skip */ 768 if (!sym_is_changable(sym)) 769 goto next_menu; 770 /* If symbol equals to default value - skip */ 771 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0) 772 goto next_menu; 773 774 /* 775 * If symbol is a choice value and equals to the 776 * default for a choice - skip. 777 * But only if value is bool and equal to "y" and 778 * choice is not "optional". 779 * (If choice is "optional" then all values can be "n") 780 */ 781 if (sym_is_choice_value(sym)) { 782 struct symbol *cs; 783 struct symbol *ds; 784 785 cs = prop_get_symbol(sym_get_choice_prop(sym)); 786 ds = sym_choice_default(cs); 787 if (!sym_is_optional(cs) && sym == ds) { 788 if ((sym->type == S_BOOLEAN) && 789 sym_get_tristate_value(sym) == yes) 790 goto next_menu; 791 } 792 } 793 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL); 794 } 795 next_menu: 796 if (menu->list != NULL) { 797 menu = menu->list; 798 } 799 else if (menu->next != NULL) { 800 menu = menu->next; 801 } else { 802 while ((menu = menu->parent)) { 803 if (menu->next != NULL) { 804 menu = menu->next; 805 break; 806 } 807 } 808 } 809 } 810 fclose(out); 811 return 0; 812 } 813 814 int conf_write(const char *name) 815 { 816 FILE *out; 817 struct symbol *sym; 818 struct menu *menu; 819 const char *basename; 820 const char *str; 821 char dirname[PATH_MAX+1], tmpname[PATH_MAX+22], newname[PATH_MAX+8]; 822 char *env; 823 824 dirname[0] = 0; 825 if (name && name[0]) { 826 char *slash; 827 828 if (is_dir(name)) { 829 strcpy(dirname, name); 830 strcat(dirname, "/"); 831 basename = conf_get_configname(); 832 } else if ((slash = strrchr(name, '/'))) { 833 int size = slash - name + 1; 834 memcpy(dirname, name, size); 835 dirname[size] = 0; 836 if (slash[1]) 837 basename = slash + 1; 838 else 839 basename = conf_get_configname(); 840 } else 841 basename = name; 842 } else 843 basename = conf_get_configname(); 844 845 sprintf(newname, "%s%s", dirname, basename); 846 env = getenv("KCONFIG_OVERWRITECONFIG"); 847 if (!env || !*env) { 848 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid()); 849 out = fopen(tmpname, "w"); 850 } else { 851 *tmpname = 0; 852 out = fopen(newname, "w"); 853 } 854 if (!out) 855 return 1; 856 857 conf_write_heading(out, &kconfig_printer_cb, NULL); 858 859 if (!conf_get_changed()) 860 sym_clear_all_valid(); 861 862 menu = rootmenu.list; 863 while (menu) { 864 sym = menu->sym; 865 if (!sym) { 866 if (!menu_is_visible(menu)) 867 goto next; 868 str = menu_get_prompt(menu); 869 fprintf(out, "\n" 870 "#\n" 871 "# %s\n" 872 "#\n", str); 873 } else if (!(sym->flags & SYMBOL_CHOICE)) { 874 sym_calc_value(sym); 875 if (!(sym->flags & SYMBOL_WRITE)) 876 goto next; 877 sym->flags &= ~SYMBOL_WRITE; 878 879 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL); 880 } 881 882 next: 883 if (menu->list) { 884 menu = menu->list; 885 continue; 886 } 887 if (menu->next) 888 menu = menu->next; 889 else while ((menu = menu->parent)) { 890 if (menu->next) { 891 menu = menu->next; 892 break; 893 } 894 } 895 } 896 fclose(out); 897 898 if (*tmpname) { 899 strcat(dirname, basename); 900 strcat(dirname, ".old"); 901 rename(newname, dirname); 902 if (rename(tmpname, newname)) 903 return 1; 904 } 905 906 conf_message("configuration written to %s", newname); 907 908 sym_set_change_count(0); 909 910 return 0; 911 } 912 913 /* write a dependency file as used by kbuild to track dependencies */ 914 static int conf_write_dep(const char *name) 915 { 916 struct file *file; 917 FILE *out; 918 919 if (!name) 920 name = ".kconfig.d"; 921 out = fopen("..config.tmp", "w"); 922 if (!out) 923 return 1; 924 fprintf(out, "deps_config := \\\n"); 925 for (file = file_list; file; file = file->next) { 926 if (file->next) 927 fprintf(out, "\t%s \\\n", file->name); 928 else 929 fprintf(out, "\t%s\n", file->name); 930 } 931 fprintf(out, "\n%s: \\\n" 932 "\t$(deps_config)\n\n", conf_get_autoconfig_name()); 933 934 env_write_dep(out, conf_get_autoconfig_name()); 935 936 fprintf(out, "\n$(deps_config): ;\n"); 937 fclose(out); 938 939 if (make_parent_dir(name)) 940 return 1; 941 rename("..config.tmp", name); 942 return 0; 943 } 944 945 static int conf_touch_deps(void) 946 { 947 const char *name; 948 struct symbol *sym; 949 int res, i; 950 951 strcpy(depfile_path, "include/config/"); 952 depfile_prefix_len = strlen(depfile_path); 953 954 name = conf_get_autoconfig_name(); 955 conf_read_simple(name, S_DEF_AUTO); 956 sym_calc_value(modules_sym); 957 958 for_all_symbols(i, sym) { 959 sym_calc_value(sym); 960 if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name) 961 continue; 962 if (sym->flags & SYMBOL_WRITE) { 963 if (sym->flags & SYMBOL_DEF_AUTO) { 964 /* 965 * symbol has old and new value, 966 * so compare them... 967 */ 968 switch (sym->type) { 969 case S_BOOLEAN: 970 case S_TRISTATE: 971 if (sym_get_tristate_value(sym) == 972 sym->def[S_DEF_AUTO].tri) 973 continue; 974 break; 975 case S_STRING: 976 case S_HEX: 977 case S_INT: 978 if (!strcmp(sym_get_string_value(sym), 979 sym->def[S_DEF_AUTO].val)) 980 continue; 981 break; 982 default: 983 break; 984 } 985 } else { 986 /* 987 * If there is no old value, only 'no' (unset) 988 * is allowed as new value. 989 */ 990 switch (sym->type) { 991 case S_BOOLEAN: 992 case S_TRISTATE: 993 if (sym_get_tristate_value(sym) == no) 994 continue; 995 break; 996 default: 997 break; 998 } 999 } 1000 } else if (!(sym->flags & SYMBOL_DEF_AUTO)) 1001 /* There is neither an old nor a new value. */ 1002 continue; 1003 /* else 1004 * There is an old value, but no new value ('no' (unset) 1005 * isn't saved in auto.conf, so the old value is always 1006 * different from 'no'). 1007 */ 1008 1009 res = conf_touch_dep(sym->name); 1010 if (res) 1011 return res; 1012 } 1013 1014 return 0; 1015 } 1016 1017 int conf_write_autoconf(int overwrite) 1018 { 1019 struct symbol *sym; 1020 const char *name; 1021 const char *autoconf_name = conf_get_autoconfig_name(); 1022 FILE *out, *tristate, *out_h; 1023 int i; 1024 1025 if (!overwrite && is_present(autoconf_name)) 1026 return 0; 1027 1028 sym_clear_all_valid(); 1029 1030 conf_write_dep("include/config/auto.conf.cmd"); 1031 1032 if (conf_touch_deps()) 1033 return 1; 1034 1035 out = fopen(".tmpconfig", "w"); 1036 if (!out) 1037 return 1; 1038 1039 tristate = fopen(".tmpconfig_tristate", "w"); 1040 if (!tristate) { 1041 fclose(out); 1042 return 1; 1043 } 1044 1045 out_h = fopen(".tmpconfig.h", "w"); 1046 if (!out_h) { 1047 fclose(out); 1048 fclose(tristate); 1049 return 1; 1050 } 1051 1052 conf_write_heading(out, &kconfig_printer_cb, NULL); 1053 1054 conf_write_heading(tristate, &tristate_printer_cb, NULL); 1055 1056 conf_write_heading(out_h, &header_printer_cb, NULL); 1057 1058 for_all_symbols(i, sym) { 1059 sym_calc_value(sym); 1060 if (!(sym->flags & SYMBOL_WRITE) || !sym->name) 1061 continue; 1062 1063 /* write symbol to auto.conf, tristate and header files */ 1064 conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1); 1065 1066 conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1); 1067 1068 conf_write_symbol(out_h, sym, &header_printer_cb, NULL); 1069 } 1070 fclose(out); 1071 fclose(tristate); 1072 fclose(out_h); 1073 1074 name = getenv("KCONFIG_AUTOHEADER"); 1075 if (!name) 1076 name = "include/generated/autoconf.h"; 1077 if (make_parent_dir(name)) 1078 return 1; 1079 if (rename(".tmpconfig.h", name)) 1080 return 1; 1081 1082 name = getenv("KCONFIG_TRISTATE"); 1083 if (!name) 1084 name = "include/config/tristate.conf"; 1085 if (make_parent_dir(name)) 1086 return 1; 1087 if (rename(".tmpconfig_tristate", name)) 1088 return 1; 1089 1090 if (make_parent_dir(autoconf_name)) 1091 return 1; 1092 /* 1093 * This must be the last step, kbuild has a dependency on auto.conf 1094 * and this marks the successful completion of the previous steps. 1095 */ 1096 if (rename(".tmpconfig", autoconf_name)) 1097 return 1; 1098 1099 return 0; 1100 } 1101 1102 static int sym_change_count; 1103 static void (*conf_changed_callback)(void); 1104 1105 void sym_set_change_count(int count) 1106 { 1107 int _sym_change_count = sym_change_count; 1108 sym_change_count = count; 1109 if (conf_changed_callback && 1110 (bool)_sym_change_count != (bool)count) 1111 conf_changed_callback(); 1112 } 1113 1114 void sym_add_change_count(int count) 1115 { 1116 sym_set_change_count(count + sym_change_count); 1117 } 1118 1119 bool conf_get_changed(void) 1120 { 1121 return sym_change_count; 1122 } 1123 1124 void conf_set_changed_callback(void (*fn)(void)) 1125 { 1126 conf_changed_callback = fn; 1127 } 1128 1129 static bool randomize_choice_values(struct symbol *csym) 1130 { 1131 struct property *prop; 1132 struct symbol *sym; 1133 struct expr *e; 1134 int cnt, def; 1135 1136 /* 1137 * If choice is mod then we may have more items selected 1138 * and if no then no-one. 1139 * In both cases stop. 1140 */ 1141 if (csym->curr.tri != yes) 1142 return false; 1143 1144 prop = sym_get_choice_prop(csym); 1145 1146 /* count entries in choice block */ 1147 cnt = 0; 1148 expr_list_for_each_sym(prop->expr, e, sym) 1149 cnt++; 1150 1151 /* 1152 * find a random value and set it to yes, 1153 * set the rest to no so we have only one set 1154 */ 1155 def = (rand() % cnt); 1156 1157 cnt = 0; 1158 expr_list_for_each_sym(prop->expr, e, sym) { 1159 if (def == cnt++) { 1160 sym->def[S_DEF_USER].tri = yes; 1161 csym->def[S_DEF_USER].val = sym; 1162 } 1163 else { 1164 sym->def[S_DEF_USER].tri = no; 1165 } 1166 sym->flags |= SYMBOL_DEF_USER; 1167 /* clear VALID to get value calculated */ 1168 sym->flags &= ~SYMBOL_VALID; 1169 } 1170 csym->flags |= SYMBOL_DEF_USER; 1171 /* clear VALID to get value calculated */ 1172 csym->flags &= ~(SYMBOL_VALID); 1173 1174 return true; 1175 } 1176 1177 void set_all_choice_values(struct symbol *csym) 1178 { 1179 struct property *prop; 1180 struct symbol *sym; 1181 struct expr *e; 1182 1183 prop = sym_get_choice_prop(csym); 1184 1185 /* 1186 * Set all non-assinged choice values to no 1187 */ 1188 expr_list_for_each_sym(prop->expr, e, sym) { 1189 if (!sym_has_value(sym)) 1190 sym->def[S_DEF_USER].tri = no; 1191 } 1192 csym->flags |= SYMBOL_DEF_USER; 1193 /* clear VALID to get value calculated */ 1194 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES); 1195 } 1196 1197 bool conf_set_all_new_symbols(enum conf_def_mode mode) 1198 { 1199 struct symbol *sym, *csym; 1200 int i, cnt, pby, pty, ptm; /* pby: probability of bool = y 1201 * pty: probability of tristate = y 1202 * ptm: probability of tristate = m 1203 */ 1204 1205 pby = 50; pty = ptm = 33; /* can't go as the default in switch-case 1206 * below, otherwise gcc whines about 1207 * -Wmaybe-uninitialized */ 1208 if (mode == def_random) { 1209 int n, p[3]; 1210 char *env = getenv("KCONFIG_PROBABILITY"); 1211 n = 0; 1212 while( env && *env ) { 1213 char *endp; 1214 int tmp = strtol( env, &endp, 10 ); 1215 if( tmp >= 0 && tmp <= 100 ) { 1216 p[n++] = tmp; 1217 } else { 1218 errno = ERANGE; 1219 perror( "KCONFIG_PROBABILITY" ); 1220 exit( 1 ); 1221 } 1222 env = (*endp == ':') ? endp+1 : endp; 1223 if( n >=3 ) { 1224 break; 1225 } 1226 } 1227 switch( n ) { 1228 case 1: 1229 pby = p[0]; ptm = pby/2; pty = pby-ptm; 1230 break; 1231 case 2: 1232 pty = p[0]; ptm = p[1]; pby = pty + ptm; 1233 break; 1234 case 3: 1235 pby = p[0]; pty = p[1]; ptm = p[2]; 1236 break; 1237 } 1238 1239 if( pty+ptm > 100 ) { 1240 errno = ERANGE; 1241 perror( "KCONFIG_PROBABILITY" ); 1242 exit( 1 ); 1243 } 1244 } 1245 bool has_changed = false; 1246 1247 for_all_symbols(i, sym) { 1248 if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID)) 1249 continue; 1250 switch (sym_get_type(sym)) { 1251 case S_BOOLEAN: 1252 case S_TRISTATE: 1253 has_changed = true; 1254 switch (mode) { 1255 case def_yes: 1256 sym->def[S_DEF_USER].tri = yes; 1257 break; 1258 case def_mod: 1259 sym->def[S_DEF_USER].tri = mod; 1260 break; 1261 case def_no: 1262 if (sym->flags & SYMBOL_ALLNOCONFIG_Y) 1263 sym->def[S_DEF_USER].tri = yes; 1264 else 1265 sym->def[S_DEF_USER].tri = no; 1266 break; 1267 case def_random: 1268 sym->def[S_DEF_USER].tri = no; 1269 cnt = rand() % 100; 1270 if (sym->type == S_TRISTATE) { 1271 if (cnt < pty) 1272 sym->def[S_DEF_USER].tri = yes; 1273 else if (cnt < (pty+ptm)) 1274 sym->def[S_DEF_USER].tri = mod; 1275 } else if (cnt < pby) 1276 sym->def[S_DEF_USER].tri = yes; 1277 break; 1278 default: 1279 continue; 1280 } 1281 if (!(sym_is_choice(sym) && mode == def_random)) 1282 sym->flags |= SYMBOL_DEF_USER; 1283 break; 1284 default: 1285 break; 1286 } 1287 1288 } 1289 1290 sym_clear_all_valid(); 1291 1292 /* 1293 * We have different type of choice blocks. 1294 * If curr.tri equals to mod then we can select several 1295 * choice symbols in one block. 1296 * In this case we do nothing. 1297 * If curr.tri equals yes then only one symbol can be 1298 * selected in a choice block and we set it to yes, 1299 * and the rest to no. 1300 */ 1301 if (mode != def_random) { 1302 for_all_symbols(i, csym) { 1303 if ((sym_is_choice(csym) && !sym_has_value(csym)) || 1304 sym_is_choice_value(csym)) 1305 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES; 1306 } 1307 } 1308 1309 for_all_symbols(i, csym) { 1310 if (sym_has_value(csym) || !sym_is_choice(csym)) 1311 continue; 1312 1313 sym_calc_value(csym); 1314 if (mode == def_random) 1315 has_changed = randomize_choice_values(csym); 1316 else { 1317 set_all_choice_values(csym); 1318 has_changed = true; 1319 } 1320 } 1321 1322 return has_changed; 1323 } 1324