linux/scripts/kconfig/confdata.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
   3 * Released under the terms of the GNU GPL v2.0.
   4 */
   5
   6#include <sys/stat.h>
   7#include <ctype.h>
   8#include <fcntl.h>
   9#include <stdio.h>
  10#include <stdlib.h>
  11#include <string.h>
  12#include <time.h>
  13#include <unistd.h>
  14
  15#define LKC_DIRECT_LINK
  16#include "lkc.h"
  17
  18static void conf_warning(const char *fmt, ...)
  19        __attribute__ ((format (printf, 1, 2)));
  20
  21static const char *conf_filename;
  22static int conf_lineno, conf_warnings, conf_unsaved;
  23
  24const char conf_defname[] = "arch/$ARCH/defconfig";
  25
  26static void conf_warning(const char *fmt, ...)
  27{
  28        va_list ap;
  29        va_start(ap, fmt);
  30        fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
  31        vfprintf(stderr, fmt, ap);
  32        fprintf(stderr, "\n");
  33        va_end(ap);
  34        conf_warnings++;
  35}
  36
  37const char *conf_get_configname(void)
  38{
  39        char *name = getenv("KCONFIG_CONFIG");
  40
  41        return name ? name : ".config";
  42}
  43
  44const char *conf_get_autoconfig_name(void)
  45{
  46        char *name = getenv("KCONFIG_AUTOCONFIG");
  47
  48        return name ? name : "include/config/auto.conf";
  49}
  50
  51static char *conf_expand_value(const char *in)
  52{
  53        struct symbol *sym;
  54        const char *src;
  55        static char res_value[SYMBOL_MAXLENGTH];
  56        char *dst, name[SYMBOL_MAXLENGTH];
  57
  58        res_value[0] = 0;
  59        dst = name;
  60        while ((src = strchr(in, '$'))) {
  61                strncat(res_value, in, src - in);
  62                src++;
  63                dst = name;
  64                while (isalnum(*src) || *src == '_')
  65                        *dst++ = *src++;
  66                *dst = 0;
  67                sym = sym_lookup(name, 0);
  68                sym_calc_value(sym);
  69                strcat(res_value, sym_get_string_value(sym));
  70                in = src;
  71        }
  72        strcat(res_value, in);
  73
  74        return res_value;
  75}
  76
  77char *conf_get_default_confname(void)
  78{
  79        struct stat buf;
  80        static char fullname[PATH_MAX+1];
  81        char *env, *name;
  82
  83        name = conf_expand_value(conf_defname);
  84        env = getenv(SRCTREE);
  85        if (env) {
  86                sprintf(fullname, "%s/%s", env, name);
  87                if (!stat(fullname, &buf))
  88                        return fullname;
  89        }
  90        return name;
  91}
  92
  93static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
  94{
  95        char *p2;
  96
  97        switch (sym->type) {
  98        case S_TRISTATE:
  99                if (p[0] == 'm') {
 100                        sym->def[def].tri = mod;
 101                        sym->flags |= def_flags;
 102                        break;
 103                }
 104        case S_BOOLEAN:
 105                if (p[0] == 'y') {
 106                        sym->def[def].tri = yes;
 107                        sym->flags |= def_flags;
 108                        break;
 109                }
 110                if (p[0] == 'n') {
 111                        sym->def[def].tri = no;
 112                        sym->flags |= def_flags;
 113                        break;
 114                }
 115                conf_warning("symbol value '%s' invalid for %s", p, sym->name);
 116                break;
 117        case S_OTHER:
 118                if (*p != '"') {
 119                        for (p2 = p; *p2 && !isspace(*p2); p2++)
 120                                ;
 121                        sym->type = S_STRING;
 122                        goto done;
 123                }
 124        case S_STRING:
 125                if (*p++ != '"')
 126                        break;
 127                for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
 128                        if (*p2 == '"') {
 129                                *p2 = 0;
 130                                break;
 131                        }
 132                        memmove(p2, p2 + 1, strlen(p2));
 133                }
 134                if (!p2) {
 135                        conf_warning("invalid string found");
 136                        return 1;
 137                }
 138        case S_INT:
 139        case S_HEX:
 140        done:
 141                if (sym_string_valid(sym, p)) {
 142                        sym->def[def].val = strdup(p);
 143                        sym->flags |= def_flags;
 144                } else {
 145                        conf_warning("symbol value '%s' invalid for %s", p, sym->name);
 146                        return 1;
 147                }
 148                break;
 149        default:
 150                ;
 151        }
 152        return 0;
 153}
 154
 155int conf_read_simple(const char *name, int def)
 156{
 157        FILE *in = NULL;
 158        char line[1024];
 159        char *p, *p2;
 160        struct symbol *sym;
 161        int i, def_flags;
 162
 163        if (name) {
 164                in = zconf_fopen(name);
 165        } else {
 166                struct property *prop;
 167
 168                name = conf_get_configname();
 169                in = zconf_fopen(name);
 170                if (in)
 171                        goto load;
 172                sym_add_change_count(1);
 173                if (!sym_defconfig_list) {
 174                        if (modules_sym)
 175                                sym_calc_value(modules_sym);
 176                        return 1;
 177                }
 178
 179                for_all_defaults(sym_defconfig_list, prop) {
 180                        if (expr_calc_value(prop->visible.expr) == no ||
 181                            prop->expr->type != E_SYMBOL)
 182                                continue;
 183                        name = conf_expand_value(prop->expr->left.sym->name);
 184                        in = zconf_fopen(name);
 185                        if (in) {
 186                                printf(_("#\n"
 187                                         "# using defaults found in %s\n"
 188                                         "#\n"), name);
 189                                goto load;
 190                        }
 191                }
 192        }
 193        if (!in)
 194                return 1;
 195
 196load:
 197        conf_filename = name;
 198        conf_lineno = 0;
 199        conf_warnings = 0;
 200        conf_unsaved = 0;
 201
 202        def_flags = SYMBOL_DEF << def;
 203        for_all_symbols(i, sym) {
 204                sym->flags |= SYMBOL_CHANGED;
 205                sym->flags &= ~(def_flags|SYMBOL_VALID);
 206                if (sym_is_choice(sym))
 207                        sym->flags |= def_flags;
 208                switch (sym->type) {
 209                case S_INT:
 210                case S_HEX:
 211                case S_STRING:
 212                        if (sym->def[def].val)
 213                                free(sym->def[def].val);
 214                default:
 215                        sym->def[def].val = NULL;
 216                        sym->def[def].tri = no;
 217                }
 218        }
 219
 220        while (fgets(line, sizeof(line), in)) {
 221                conf_lineno++;
 222                sym = NULL;
 223                switch (line[0]) {
 224                case '#':
 225                        if (memcmp(line + 2, "CONFIG_", 7))
 226                                continue;
 227                        p = strchr(line + 9, ' ');
 228                        if (!p)
 229                                continue;
 230                        *p++ = 0;
 231                        if (strncmp(p, "is not set", 10))
 232                                continue;
 233                        if (def == S_DEF_USER) {
 234                                sym = sym_find(line + 9);
 235                                if (!sym) {
 236                                        sym_add_change_count(1);
 237                                        break;
 238                                }
 239                        } else {
 240                                sym = sym_lookup(line + 9, 0);
 241                                if (sym->type == S_UNKNOWN)
 242                                        sym->type = S_BOOLEAN;
 243                        }
 244                        if (sym->flags & def_flags) {
 245                                conf_warning("override: reassigning to symbol %s", sym->name);
 246                        }
 247                        switch (sym->type) {
 248                        case S_BOOLEAN:
 249                        case S_TRISTATE:
 250                                sym->def[def].tri = no;
 251                                sym->flags |= def_flags;
 252                                break;
 253                        default:
 254                                ;
 255                        }
 256                        break;
 257                case 'C':
 258                        if (memcmp(line, "CONFIG_", 7)) {
 259                                conf_warning("unexpected data");
 260                                continue;
 261                        }
 262                        p = strchr(line + 7, '=');
 263                        if (!p)
 264                                continue;
 265                        *p++ = 0;
 266                        p2 = strchr(p, '\n');
 267                        if (p2) {
 268                                *p2-- = 0;
 269                                if (*p2 == '\r')
 270                                        *p2 = 0;
 271                        }
 272                        if (def == S_DEF_USER) {
 273                                sym = sym_find(line + 7);
 274                                if (!sym) {
 275                                        sym_add_change_count(1);
 276                                        break;
 277                                }
 278                        } else {
 279                                sym = sym_lookup(line + 7, 0);
 280                                if (sym->type == S_UNKNOWN)
 281                                        sym->type = S_OTHER;
 282                        }
 283                        if (sym->flags & def_flags) {
 284                                conf_warning("override: reassigning to symbol %s", sym->name);
 285                        }
 286                        if (conf_set_sym_val(sym, def, def_flags, p))
 287                                continue;
 288                        break;
 289                case '\r':
 290                case '\n':
 291                        break;
 292                default:
 293                        conf_warning("unexpected data");
 294                        continue;
 295                }
 296                if (sym && sym_is_choice_value(sym)) {
 297                        struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
 298                        switch (sym->def[def].tri) {
 299                        case no:
 300                                break;
 301                        case mod:
 302                                if (cs->def[def].tri == yes) {
 303                                        conf_warning("%s creates inconsistent choice state", sym->name);
 304                                        cs->flags &= ~def_flags;
 305                                }
 306                                break;
 307                        case yes:
 308                                if (cs->def[def].tri != no)
 309                                        conf_warning("override: %s changes choice state", sym->name);
 310                                cs->def[def].val = sym;
 311                                break;
 312                        }
 313                        cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
 314                }
 315        }
 316        fclose(in);
 317
 318        if (modules_sym)
 319                sym_calc_value(modules_sym);
 320        return 0;
 321}
 322
 323int conf_read(const char *name)
 324{
 325        struct symbol *sym, *choice_sym;
 326        struct property *prop;
 327        struct expr *e;
 328        int i, flags;
 329
 330        sym_set_change_count(0);
 331
 332        if (conf_read_simple(name, S_DEF_USER))
 333                return 1;
 334
 335        for_all_symbols(i, sym) {
 336                sym_calc_value(sym);
 337                if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
 338                        goto sym_ok;
 339                if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
 340                        /* check that calculated value agrees with saved value */
 341                        switch (sym->type) {
 342                        case S_BOOLEAN:
 343                        case S_TRISTATE:
 344                                if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
 345                                        break;
 346                                if (!sym_is_choice(sym))
 347                                        goto sym_ok;
 348                        default:
 349                                if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
 350                                        goto sym_ok;
 351                                break;
 352                        }
 353                } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
 354                        /* no previous value and not saved */
 355                        goto sym_ok;
 356                conf_unsaved++;
 357                /* maybe print value in verbose mode... */
 358        sym_ok:
 359                if (!sym_is_choice(sym))
 360                        continue;
 361                /* The choice symbol only has a set value (and thus is not new)
 362                 * if all its visible childs have values.
 363                 */
 364                prop = sym_get_choice_prop(sym);
 365                flags = sym->flags;
 366                expr_list_for_each_sym(prop->expr, e, choice_sym)
 367                        if (choice_sym->visible != no)
 368                                flags &= choice_sym->flags;
 369                sym->flags &= flags | ~SYMBOL_DEF_USER;
 370        }
 371
 372        for_all_symbols(i, sym) {
 373                if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
 374                        /* Reset values of generates values, so they'll appear
 375                         * as new, if they should become visible, but that
 376                         * doesn't quite work if the Kconfig and the saved
 377                         * configuration disagree.
 378                         */
 379                        if (sym->visible == no && !conf_unsaved)
 380                                sym->flags &= ~SYMBOL_DEF_USER;
 381                        switch (sym->type) {
 382                        case S_STRING:
 383                        case S_INT:
 384                        case S_HEX:
 385                                /* Reset a string value if it's out of range */
 386                                if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
 387                                        break;
 388                                sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
 389                                conf_unsaved++;
 390                                break;
 391                        default:
 392                                break;
 393                        }
 394                }
 395        }
 396
 397        sym_add_change_count(conf_warnings || conf_unsaved);
 398
 399        return 0;
 400}
 401
 402/* Write a S_STRING */
 403static void conf_write_string(bool headerfile, const char *name,
 404                              const char *str, FILE *out)
 405{
 406        int l;
 407        if (headerfile)
 408                fprintf(out, "#define CONFIG_%s \"", name);
 409        else
 410                fprintf(out, "CONFIG_%s=\"", name);
 411
 412        while (1) {
 413                l = strcspn(str, "\"\\");
 414                if (l) {
 415                        xfwrite(str, l, 1, out);
 416                        str += l;
 417                }
 418                if (!*str)
 419                        break;
 420                fprintf(out, "\\%c", *str++);
 421        }
 422        fputs("\"\n", out);
 423}
 424
 425static void conf_write_symbol(struct symbol *sym, enum symbol_type type,
 426                              FILE *out, bool write_no)
 427{
 428        const char *str;
 429
 430        switch (type) {
 431        case S_BOOLEAN:
 432        case S_TRISTATE:
 433                switch (sym_get_tristate_value(sym)) {
 434                case no:
 435                        if (write_no)
 436                                fprintf(out, "# CONFIG_%s is not set\n", sym->name);
 437                        break;
 438                case mod:
 439                        fprintf(out, "CONFIG_%s=m\n", sym->name);
 440                        break;
 441                case yes:
 442                        fprintf(out, "CONFIG_%s=y\n", sym->name);
 443                        break;
 444                }
 445                break;
 446        case S_STRING:
 447                conf_write_string(false, sym->name, sym_get_string_value(sym), out);
 448                break;
 449        case S_HEX:
 450        case S_INT:
 451                str = sym_get_string_value(sym);
 452                fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
 453                break;
 454        case S_OTHER:
 455        case S_UNKNOWN:
 456                break;
 457        }
 458}
 459
 460/*
 461 * Write out a minimal config.
 462 * All values that has default values are skipped as this is redundant.
 463 */
 464int conf_write_defconfig(const char *filename)
 465{
 466        struct symbol *sym;
 467        struct menu *menu;
 468        FILE *out;
 469
 470        out = fopen(filename, "w");
 471        if (!out)
 472                return 1;
 473
 474        sym_clear_all_valid();
 475
 476        /* Traverse all menus to find all relevant symbols */
 477        menu = rootmenu.list;
 478
 479        while (menu != NULL)
 480        {
 481                sym = menu->sym;
 482                if (sym == NULL) {
 483                        if (!menu_is_visible(menu))
 484                                goto next_menu;
 485                } else if (!sym_is_choice(sym)) {
 486                        sym_calc_value(sym);
 487                        if (!(sym->flags & SYMBOL_WRITE))
 488                                goto next_menu;
 489                        sym->flags &= ~SYMBOL_WRITE;
 490                        /* If we cannot change the symbol - skip */
 491                        if (!sym_is_changable(sym))
 492                                goto next_menu;
 493                        /* If symbol equals to default value - skip */
 494                        if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
 495                                goto next_menu;
 496
 497                        /*
 498                         * If symbol is a choice value and equals to the
 499                         * default for a choice - skip.
 500                         * But only if value is bool and equal to "y" and
 501                         * choice is not "optional".
 502                         * (If choice is "optional" then all values can be "n")
 503                         */
 504                        if (sym_is_choice_value(sym)) {
 505                                struct symbol *cs;
 506                                struct symbol *ds;
 507
 508                                cs = prop_get_symbol(sym_get_choice_prop(sym));
 509                                ds = sym_choice_default(cs);
 510                                if (!sym_is_optional(cs) && sym == ds) {
 511                                        if ((sym->type == S_BOOLEAN) &&
 512                                            sym_get_tristate_value(sym) == yes)
 513                                                goto next_menu;
 514                                }
 515                        }
 516                        conf_write_symbol(sym, sym->type, out, true);
 517                }
 518next_menu:
 519                if (menu->list != NULL) {
 520                        menu = menu->list;
 521                }
 522                else if (menu->next != NULL) {
 523                        menu = menu->next;
 524                } else {
 525                        while ((menu = menu->parent)) {
 526                                if (menu->next != NULL) {
 527                                        menu = menu->next;
 528                                        break;
 529                                }
 530                        }
 531                }
 532        }
 533        fclose(out);
 534        return 0;
 535}
 536
 537int conf_write(const char *name)
 538{
 539        FILE *out;
 540        struct symbol *sym;
 541        struct menu *menu;
 542        const char *basename;
 543        const char *str;
 544        char dirname[128], tmpname[128], newname[128];
 545        enum symbol_type type;
 546        time_t now;
 547        int use_timestamp = 1;
 548        char *env;
 549
 550        dirname[0] = 0;
 551        if (name && name[0]) {
 552                struct stat st;
 553                char *slash;
 554
 555                if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
 556                        strcpy(dirname, name);
 557                        strcat(dirname, "/");
 558                        basename = conf_get_configname();
 559                } else if ((slash = strrchr(name, '/'))) {
 560                        int size = slash - name + 1;
 561                        memcpy(dirname, name, size);
 562                        dirname[size] = 0;
 563                        if (slash[1])
 564                                basename = slash + 1;
 565                        else
 566                                basename = conf_get_configname();
 567                } else
 568                        basename = name;
 569        } else
 570                basename = conf_get_configname();
 571
 572        sprintf(newname, "%s%s", dirname, basename);
 573        env = getenv("KCONFIG_OVERWRITECONFIG");
 574        if (!env || !*env) {
 575                sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
 576                out = fopen(tmpname, "w");
 577        } else {
 578                *tmpname = 0;
 579                out = fopen(newname, "w");
 580        }
 581        if (!out)
 582                return 1;
 583
 584        sym = sym_lookup("KERNELVERSION", 0);
 585        sym_calc_value(sym);
 586        time(&now);
 587        env = getenv("KCONFIG_NOTIMESTAMP");
 588        if (env && *env)
 589                use_timestamp = 0;
 590
 591        fprintf(out, _("#\n"
 592                       "# Automatically generated make config: don't edit\n"
 593                       "# Linux kernel version: %s\n"
 594                       "%s%s"
 595                       "#\n"),
 596                     sym_get_string_value(sym),
 597                     use_timestamp ? "# " : "",
 598                     use_timestamp ? ctime(&now) : "");
 599
 600        if (!conf_get_changed())
 601                sym_clear_all_valid();
 602
 603        menu = rootmenu.list;
 604        while (menu) {
 605                sym = menu->sym;
 606                if (!sym) {
 607                        if (!menu_is_visible(menu))
 608                                goto next;
 609                        str = menu_get_prompt(menu);
 610                        fprintf(out, "\n"
 611                                     "#\n"
 612                                     "# %s\n"
 613                                     "#\n", str);
 614                } else if (!(sym->flags & SYMBOL_CHOICE)) {
 615                        sym_calc_value(sym);
 616                        if (!(sym->flags & SYMBOL_WRITE))
 617                                goto next;
 618                        sym->flags &= ~SYMBOL_WRITE;
 619                        type = sym->type;
 620                        if (type == S_TRISTATE) {
 621                                sym_calc_value(modules_sym);
 622                                if (modules_sym->curr.tri == no)
 623                                        type = S_BOOLEAN;
 624                        }
 625                        /* Write config symbol to file */
 626                        conf_write_symbol(sym, type, out, true);
 627                }
 628
 629next:
 630                if (menu->list) {
 631                        menu = menu->list;
 632                        continue;
 633                }
 634                if (menu->next)
 635                        menu = menu->next;
 636                else while ((menu = menu->parent)) {
 637                        if (menu->next) {
 638                                menu = menu->next;
 639                                break;
 640                        }
 641                }
 642        }
 643        fclose(out);
 644
 645        if (*tmpname) {
 646                strcat(dirname, basename);
 647                strcat(dirname, ".old");
 648                rename(newname, dirname);
 649                if (rename(tmpname, newname))
 650                        return 1;
 651        }
 652
 653        printf(_("#\n"
 654                 "# configuration written to %s\n"
 655                 "#\n"), newname);
 656
 657        sym_set_change_count(0);
 658
 659        return 0;
 660}
 661
 662static int conf_split_config(void)
 663{
 664        const char *name;
 665        char path[128];
 666        char *s, *d, c;
 667        struct symbol *sym;
 668        struct stat sb;
 669        int res, i, fd;
 670
 671        name = conf_get_autoconfig_name();
 672        conf_read_simple(name, S_DEF_AUTO);
 673
 674        if (chdir("include/config"))
 675                return 1;
 676
 677        res = 0;
 678        for_all_symbols(i, sym) {
 679                sym_calc_value(sym);
 680                if ((sym->flags & SYMBOL_AUTO) || !sym->name)
 681                        continue;
 682                if (sym->flags & SYMBOL_WRITE) {
 683                        if (sym->flags & SYMBOL_DEF_AUTO) {
 684                                /*
 685                                 * symbol has old and new value,
 686                                 * so compare them...
 687                                 */
 688                                switch (sym->type) {
 689                                case S_BOOLEAN:
 690                                case S_TRISTATE:
 691                                        if (sym_get_tristate_value(sym) ==
 692                                            sym->def[S_DEF_AUTO].tri)
 693                                                continue;
 694                                        break;
 695                                case S_STRING:
 696                                case S_HEX:
 697                                case S_INT:
 698                                        if (!strcmp(sym_get_string_value(sym),
 699                                                    sym->def[S_DEF_AUTO].val))
 700                                                continue;
 701                                        break;
 702                                default:
 703                                        break;
 704                                }
 705                        } else {
 706                                /*
 707                                 * If there is no old value, only 'no' (unset)
 708                                 * is allowed as new value.
 709                                 */
 710                                switch (sym->type) {
 711                                case S_BOOLEAN:
 712                                case S_TRISTATE:
 713                                        if (sym_get_tristate_value(sym) == no)
 714                                                continue;
 715                                        break;
 716                                default:
 717                                        break;
 718                                }
 719                        }
 720                } else if (!(sym->flags & SYMBOL_DEF_AUTO))
 721                        /* There is neither an old nor a new value. */
 722                        continue;
 723                /* else
 724                 *      There is an old value, but no new value ('no' (unset)
 725                 *      isn't saved in auto.conf, so the old value is always
 726                 *      different from 'no').
 727                 */
 728
 729                /* Replace all '_' and append ".h" */
 730                s = sym->name;
 731                d = path;
 732                while ((c = *s++)) {
 733                        c = tolower(c);
 734                        *d++ = (c == '_') ? '/' : c;
 735                }
 736                strcpy(d, ".h");
 737
 738                /* Assume directory path already exists. */
 739                fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
 740                if (fd == -1) {
 741                        if (errno != ENOENT) {
 742                                res = 1;
 743                                break;
 744                        }
 745                        /*
 746                         * Create directory components,
 747                         * unless they exist already.
 748                         */
 749                        d = path;
 750                        while ((d = strchr(d, '/'))) {
 751                                *d = 0;
 752                                if (stat(path, &sb) && mkdir(path, 0755)) {
 753                                        res = 1;
 754                                        goto out;
 755                                }
 756                                *d++ = '/';
 757                        }
 758                        /* Try it again. */
 759                        fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
 760                        if (fd == -1) {
 761                                res = 1;
 762                                break;
 763                        }
 764                }
 765                close(fd);
 766        }
 767out:
 768        if (chdir("../.."))
 769                return 1;
 770
 771        return res;
 772}
 773
 774int conf_write_autoconf(void)
 775{
 776        struct symbol *sym;
 777        const char *str;
 778        const char *name;
 779        FILE *out, *tristate, *out_h;
 780        time_t now;
 781        int i;
 782
 783        sym_clear_all_valid();
 784
 785        file_write_dep("include/config/auto.conf.cmd");
 786
 787        if (conf_split_config())
 788                return 1;
 789
 790        out = fopen(".tmpconfig", "w");
 791        if (!out)
 792                return 1;
 793
 794        tristate = fopen(".tmpconfig_tristate", "w");
 795        if (!tristate) {
 796                fclose(out);
 797                return 1;
 798        }
 799
 800        out_h = fopen(".tmpconfig.h", "w");
 801        if (!out_h) {
 802                fclose(out);
 803                fclose(tristate);
 804                return 1;
 805        }
 806
 807        sym = sym_lookup("KERNELVERSION", 0);
 808        sym_calc_value(sym);
 809        time(&now);
 810        fprintf(out, "#\n"
 811                     "# Automatically generated make config: don't edit\n"
 812                     "# Linux kernel version: %s\n"
 813                     "# %s"
 814                     "#\n",
 815                     sym_get_string_value(sym), ctime(&now));
 816        fprintf(tristate, "#\n"
 817                          "# Automatically generated - do not edit\n"
 818                          "\n");
 819        fprintf(out_h, "/*\n"
 820                       " * Automatically generated C config: don't edit\n"
 821                       " * Linux kernel version: %s\n"
 822                       " * %s"
 823                       " */\n"
 824                       "#define AUTOCONF_INCLUDED\n",
 825                       sym_get_string_value(sym), ctime(&now));
 826
 827        for_all_symbols(i, sym) {
 828                sym_calc_value(sym);
 829                if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
 830                        continue;
 831
 832                /* write symbol to config file */
 833                conf_write_symbol(sym, sym->type, out, false);
 834
 835                /* update autoconf and tristate files */
 836                switch (sym->type) {
 837                case S_BOOLEAN:
 838                case S_TRISTATE:
 839                        switch (sym_get_tristate_value(sym)) {
 840                        case no:
 841                                break;
 842                        case mod:
 843                                fprintf(tristate, "CONFIG_%s=M\n", sym->name);
 844                                fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
 845                                break;
 846                        case yes:
 847                                if (sym->type == S_TRISTATE)
 848                                        fprintf(tristate, "CONFIG_%s=Y\n",
 849                                                        sym->name);
 850                                fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
 851                                break;
 852                        }
 853                        break;
 854                case S_STRING:
 855                        conf_write_string(true, sym->name, sym_get_string_value(sym), out_h);
 856                        break;
 857                case S_HEX:
 858                        str = sym_get_string_value(sym);
 859                        if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
 860                                fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
 861                                break;
 862                        }
 863                case S_INT:
 864                        str = sym_get_string_value(sym);
 865                        fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
 866                        break;
 867                default:
 868                        break;
 869                }
 870        }
 871        fclose(out);
 872        fclose(tristate);
 873        fclose(out_h);
 874
 875        name = getenv("KCONFIG_AUTOHEADER");
 876        if (!name)
 877                name = "include/generated/autoconf.h";
 878        if (rename(".tmpconfig.h", name))
 879                return 1;
 880        name = getenv("KCONFIG_TRISTATE");
 881        if (!name)
 882                name = "include/config/tristate.conf";
 883        if (rename(".tmpconfig_tristate", name))
 884                return 1;
 885        name = conf_get_autoconfig_name();
 886        /*
 887         * This must be the last step, kbuild has a dependency on auto.conf
 888         * and this marks the successful completion of the previous steps.
 889         */
 890        if (rename(".tmpconfig", name))
 891                return 1;
 892
 893        return 0;
 894}
 895
 896static int sym_change_count;
 897static void (*conf_changed_callback)(void);
 898
 899void sym_set_change_count(int count)
 900{
 901        int _sym_change_count = sym_change_count;
 902        sym_change_count = count;
 903        if (conf_changed_callback &&
 904            (bool)_sym_change_count != (bool)count)
 905                conf_changed_callback();
 906}
 907
 908void sym_add_change_count(int count)
 909{
 910        sym_set_change_count(count + sym_change_count);
 911}
 912
 913bool conf_get_changed(void)
 914{
 915        return sym_change_count;
 916}
 917
 918void conf_set_changed_callback(void (*fn)(void))
 919{
 920        conf_changed_callback = fn;
 921}
 922
 923static void randomize_choice_values(struct symbol *csym)
 924{
 925        struct property *prop;
 926        struct symbol *sym;
 927        struct expr *e;
 928        int cnt, def;
 929
 930        /*
 931         * If choice is mod then we may have more items slected
 932         * and if no then no-one.
 933         * In both cases stop.
 934         */
 935        if (csym->curr.tri != yes)
 936                return;
 937
 938        prop = sym_get_choice_prop(csym);
 939
 940        /* count entries in choice block */
 941        cnt = 0;
 942        expr_list_for_each_sym(prop->expr, e, sym)
 943                cnt++;
 944
 945        /*
 946         * find a random value and set it to yes,
 947         * set the rest to no so we have only one set
 948         */
 949        def = (rand() % cnt);
 950
 951        cnt = 0;
 952        expr_list_for_each_sym(prop->expr, e, sym) {
 953                if (def == cnt++) {
 954                        sym->def[S_DEF_USER].tri = yes;
 955                        csym->def[S_DEF_USER].val = sym;
 956                }
 957                else {
 958                        sym->def[S_DEF_USER].tri = no;
 959                }
 960        }
 961        csym->flags |= SYMBOL_DEF_USER;
 962        /* clear VALID to get value calculated */
 963        csym->flags &= ~(SYMBOL_VALID);
 964}
 965
 966static void set_all_choice_values(struct symbol *csym)
 967{
 968        struct property *prop;
 969        struct symbol *sym;
 970        struct expr *e;
 971
 972        prop = sym_get_choice_prop(csym);
 973
 974        /*
 975         * Set all non-assinged choice values to no
 976         */
 977        expr_list_for_each_sym(prop->expr, e, sym) {
 978                if (!sym_has_value(sym))
 979                        sym->def[S_DEF_USER].tri = no;
 980        }
 981        csym->flags |= SYMBOL_DEF_USER;
 982        /* clear VALID to get value calculated */
 983        csym->flags &= ~(SYMBOL_VALID);
 984}
 985
 986void conf_set_all_new_symbols(enum conf_def_mode mode)
 987{
 988        struct symbol *sym, *csym;
 989        int i, cnt;
 990
 991        for_all_symbols(i, sym) {
 992                if (sym_has_value(sym))
 993                        continue;
 994                switch (sym_get_type(sym)) {
 995                case S_BOOLEAN:
 996                case S_TRISTATE:
 997                        switch (mode) {
 998                        case def_yes:
 999                                sym->def[S_DEF_USER].tri = yes;
1000                                break;
1001                        case def_mod:
1002                                sym->def[S_DEF_USER].tri = mod;
1003                                break;
1004                        case def_no:
1005                                sym->def[S_DEF_USER].tri = no;
1006                                break;
1007                        case def_random:
1008                                cnt = sym_get_type(sym) == S_TRISTATE ? 3 : 2;
1009                                sym->def[S_DEF_USER].tri = (tristate)(rand() % cnt);
1010                                break;
1011                        default:
1012                                continue;
1013                        }
1014                        if (!(sym_is_choice(sym) && mode == def_random))
1015                                sym->flags |= SYMBOL_DEF_USER;
1016                        break;
1017                default:
1018                        break;
1019                }
1020
1021        }
1022
1023        sym_clear_all_valid();
1024
1025        /*
1026         * We have different type of choice blocks.
1027         * If curr.tri equal to mod then we can select several
1028         * choice symbols in one block.
1029         * In this case we do nothing.
1030         * If curr.tri equal yes then only one symbol can be
1031         * selected in a choice block and we set it to yes,
1032         * and the rest to no.
1033         */
1034        for_all_symbols(i, csym) {
1035                if (sym_has_value(csym) || !sym_is_choice(csym))
1036                        continue;
1037
1038                sym_calc_value(csym);
1039                if (mode == def_random)
1040                        randomize_choice_values(csym);
1041                else
1042                        set_all_choice_values(csym);
1043        }
1044}
1045