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