11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org> 31da177e4SLinus Torvalds * Released under the terms of the GNU GPL v2.0. 41da177e4SLinus Torvalds */ 51da177e4SLinus Torvalds 61da177e4SLinus Torvalds #include <sys/stat.h> 71da177e4SLinus Torvalds #include <ctype.h> 82e3646e5SRoman Zippel #include <fcntl.h> 91da177e4SLinus Torvalds #include <stdio.h> 101da177e4SLinus Torvalds #include <stdlib.h> 111da177e4SLinus Torvalds #include <string.h> 121da177e4SLinus Torvalds #include <time.h> 131da177e4SLinus Torvalds #include <unistd.h> 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds #define LKC_DIRECT_LINK 161da177e4SLinus Torvalds #include "lkc.h" 171da177e4SLinus Torvalds 18c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...) 19c1a0f5e3SRoman Zippel __attribute__ ((format (printf, 1, 2))); 20c1a0f5e3SRoman Zippel 21c1a0f5e3SRoman Zippel static const char *conf_filename; 22c1a0f5e3SRoman Zippel static int conf_lineno, conf_warnings, conf_unsaved; 23c1a0f5e3SRoman Zippel 241da177e4SLinus Torvalds const char conf_defname[] = "arch/$ARCH/defconfig"; 251da177e4SLinus Torvalds 26c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...) 27c1a0f5e3SRoman Zippel { 28c1a0f5e3SRoman Zippel va_list ap; 29c1a0f5e3SRoman Zippel va_start(ap, fmt); 30c1a0f5e3SRoman Zippel fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno); 31c1a0f5e3SRoman Zippel vfprintf(stderr, fmt, ap); 32c1a0f5e3SRoman Zippel fprintf(stderr, "\n"); 33c1a0f5e3SRoman Zippel va_end(ap); 34c1a0f5e3SRoman Zippel conf_warnings++; 35c1a0f5e3SRoman Zippel } 36c1a0f5e3SRoman Zippel 3714cdd3c4SRoman Zippel const char *conf_get_configname(void) 3814cdd3c4SRoman Zippel { 3914cdd3c4SRoman Zippel char *name = getenv("KCONFIG_CONFIG"); 4014cdd3c4SRoman Zippel 4114cdd3c4SRoman Zippel return name ? name : ".config"; 4214cdd3c4SRoman Zippel } 4314cdd3c4SRoman Zippel 4448b9d03cSJ.A. Magallon static char *conf_expand_value(const char *in) 451da177e4SLinus Torvalds { 461da177e4SLinus Torvalds struct symbol *sym; 4748b9d03cSJ.A. Magallon const char *src; 481da177e4SLinus Torvalds static char res_value[SYMBOL_MAXLENGTH]; 491da177e4SLinus Torvalds char *dst, name[SYMBOL_MAXLENGTH]; 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds res_value[0] = 0; 521da177e4SLinus Torvalds dst = name; 531da177e4SLinus Torvalds while ((src = strchr(in, '$'))) { 541da177e4SLinus Torvalds strncat(res_value, in, src - in); 551da177e4SLinus Torvalds src++; 561da177e4SLinus Torvalds dst = name; 571da177e4SLinus Torvalds while (isalnum(*src) || *src == '_') 581da177e4SLinus Torvalds *dst++ = *src++; 591da177e4SLinus Torvalds *dst = 0; 601da177e4SLinus Torvalds sym = sym_lookup(name, 0); 611da177e4SLinus Torvalds sym_calc_value(sym); 621da177e4SLinus Torvalds strcat(res_value, sym_get_string_value(sym)); 631da177e4SLinus Torvalds in = src; 641da177e4SLinus Torvalds } 651da177e4SLinus Torvalds strcat(res_value, in); 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds return res_value; 681da177e4SLinus Torvalds } 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds char *conf_get_default_confname(void) 711da177e4SLinus Torvalds { 721da177e4SLinus Torvalds struct stat buf; 731da177e4SLinus Torvalds static char fullname[PATH_MAX+1]; 741da177e4SLinus Torvalds char *env, *name; 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds name = conf_expand_value(conf_defname); 771da177e4SLinus Torvalds env = getenv(SRCTREE); 781da177e4SLinus Torvalds if (env) { 791da177e4SLinus Torvalds sprintf(fullname, "%s/%s", env, name); 801da177e4SLinus Torvalds if (!stat(fullname, &buf)) 811da177e4SLinus Torvalds return fullname; 821da177e4SLinus Torvalds } 831da177e4SLinus Torvalds return name; 841da177e4SLinus Torvalds } 851da177e4SLinus Torvalds 86669bfad9SRoman Zippel int conf_read_simple(const char *name, int def) 871da177e4SLinus Torvalds { 881da177e4SLinus Torvalds FILE *in = NULL; 891da177e4SLinus Torvalds char line[1024]; 901da177e4SLinus Torvalds char *p, *p2; 911da177e4SLinus Torvalds struct symbol *sym; 92669bfad9SRoman Zippel int i, def_flags; 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds if (name) { 951da177e4SLinus Torvalds in = zconf_fopen(name); 961da177e4SLinus Torvalds } else { 97face4374SRoman Zippel struct property *prop; 98face4374SRoman Zippel 9914cdd3c4SRoman Zippel name = conf_get_configname(); 100ddc97cacSRoman Zippel in = zconf_fopen(name); 101ddc97cacSRoman Zippel if (in) 102ddc97cacSRoman Zippel goto load; 103ddc97cacSRoman Zippel sym_change_count++; 104face4374SRoman Zippel if (!sym_defconfig_list) 105face4374SRoman Zippel return 1; 106face4374SRoman Zippel 107face4374SRoman Zippel for_all_defaults(sym_defconfig_list, prop) { 108face4374SRoman Zippel if (expr_calc_value(prop->visible.expr) == no || 109face4374SRoman Zippel prop->expr->type != E_SYMBOL) 110face4374SRoman Zippel continue; 111face4374SRoman Zippel name = conf_expand_value(prop->expr->left.sym->name); 1121da177e4SLinus Torvalds in = zconf_fopen(name); 1131da177e4SLinus Torvalds if (in) { 1143b9fa093SArnaldo Carvalho de Melo printf(_("#\n" 1151da177e4SLinus Torvalds "# using defaults found in %s\n" 1163b9fa093SArnaldo Carvalho de Melo "#\n"), name); 117ddc97cacSRoman Zippel goto load; 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds if (!in) 1221da177e4SLinus Torvalds return 1; 1231da177e4SLinus Torvalds 124ddc97cacSRoman Zippel load: 125c1a0f5e3SRoman Zippel conf_filename = name; 126c1a0f5e3SRoman Zippel conf_lineno = 0; 127c1a0f5e3SRoman Zippel conf_warnings = 0; 128c1a0f5e3SRoman Zippel conf_unsaved = 0; 129c1a0f5e3SRoman Zippel 130669bfad9SRoman Zippel def_flags = SYMBOL_DEF << def; 1311da177e4SLinus Torvalds for_all_symbols(i, sym) { 132669bfad9SRoman Zippel sym->flags |= SYMBOL_CHANGED; 133669bfad9SRoman Zippel sym->flags &= ~(def_flags|SYMBOL_VALID); 134c1a0f5e3SRoman Zippel if (sym_is_choice(sym)) 135669bfad9SRoman Zippel sym->flags |= def_flags; 1361da177e4SLinus Torvalds switch (sym->type) { 1371da177e4SLinus Torvalds case S_INT: 1381da177e4SLinus Torvalds case S_HEX: 1391da177e4SLinus Torvalds case S_STRING: 140669bfad9SRoman Zippel if (sym->def[def].val) 141669bfad9SRoman Zippel free(sym->def[def].val); 1421da177e4SLinus Torvalds default: 143669bfad9SRoman Zippel sym->def[def].val = NULL; 144669bfad9SRoman Zippel sym->def[def].tri = no; 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds while (fgets(line, sizeof(line), in)) { 149c1a0f5e3SRoman Zippel conf_lineno++; 1501da177e4SLinus Torvalds sym = NULL; 1511da177e4SLinus Torvalds switch (line[0]) { 1521da177e4SLinus Torvalds case '#': 1531da177e4SLinus Torvalds if (memcmp(line + 2, "CONFIG_", 7)) 1541da177e4SLinus Torvalds continue; 1551da177e4SLinus Torvalds p = strchr(line + 9, ' '); 1561da177e4SLinus Torvalds if (!p) 1571da177e4SLinus Torvalds continue; 1581da177e4SLinus Torvalds *p++ = 0; 1591da177e4SLinus Torvalds if (strncmp(p, "is not set", 10)) 1601da177e4SLinus Torvalds continue; 161669bfad9SRoman Zippel if (def == S_DEF_USER) { 1621da177e4SLinus Torvalds sym = sym_find(line + 9); 1631da177e4SLinus Torvalds if (!sym) { 164c1a0f5e3SRoman Zippel conf_warning("trying to assign nonexistent symbol %s", line + 9); 165c1a0f5e3SRoman Zippel break; 166669bfad9SRoman Zippel } 167669bfad9SRoman Zippel } else { 168669bfad9SRoman Zippel sym = sym_lookup(line + 9, 0); 169669bfad9SRoman Zippel if (sym->type == S_UNKNOWN) 170669bfad9SRoman Zippel sym->type = S_BOOLEAN; 171669bfad9SRoman Zippel } 172669bfad9SRoman Zippel if (sym->flags & def_flags) { 173c1a0f5e3SRoman Zippel conf_warning("trying to reassign symbol %s", sym->name); 1741da177e4SLinus Torvalds break; 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds switch (sym->type) { 1771da177e4SLinus Torvalds case S_BOOLEAN: 1781da177e4SLinus Torvalds case S_TRISTATE: 179669bfad9SRoman Zippel sym->def[def].tri = no; 180669bfad9SRoman Zippel sym->flags |= def_flags; 1811da177e4SLinus Torvalds break; 1821da177e4SLinus Torvalds default: 1831da177e4SLinus Torvalds ; 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds break; 1861da177e4SLinus Torvalds case 'C': 187c1a0f5e3SRoman Zippel if (memcmp(line, "CONFIG_", 7)) { 188c1a0f5e3SRoman Zippel conf_warning("unexpected data"); 1891da177e4SLinus Torvalds continue; 190c1a0f5e3SRoman Zippel } 1911da177e4SLinus Torvalds p = strchr(line + 7, '='); 1921da177e4SLinus Torvalds if (!p) 1931da177e4SLinus Torvalds continue; 1941da177e4SLinus Torvalds *p++ = 0; 1951da177e4SLinus Torvalds p2 = strchr(p, '\n'); 196*d3660a8cSMatthew Wilcox if (p2) { 197*d3660a8cSMatthew Wilcox *p2-- = 0; 198*d3660a8cSMatthew Wilcox if (*p2 == '\r') 1991da177e4SLinus Torvalds *p2 = 0; 200*d3660a8cSMatthew Wilcox } 201669bfad9SRoman Zippel if (def == S_DEF_USER) { 2021da177e4SLinus Torvalds sym = sym_find(line + 7); 2031da177e4SLinus Torvalds if (!sym) { 204c1a0f5e3SRoman Zippel conf_warning("trying to assign nonexistent symbol %s", line + 7); 205c1a0f5e3SRoman Zippel break; 206669bfad9SRoman Zippel } 207669bfad9SRoman Zippel } else { 208669bfad9SRoman Zippel sym = sym_lookup(line + 7, 0); 209669bfad9SRoman Zippel if (sym->type == S_UNKNOWN) 210669bfad9SRoman Zippel sym->type = S_OTHER; 211669bfad9SRoman Zippel } 212669bfad9SRoman Zippel if (sym->flags & def_flags) { 213c1a0f5e3SRoman Zippel conf_warning("trying to reassign symbol %s", sym->name); 2141da177e4SLinus Torvalds break; 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds switch (sym->type) { 2171da177e4SLinus Torvalds case S_TRISTATE: 2181da177e4SLinus Torvalds if (p[0] == 'm') { 219669bfad9SRoman Zippel sym->def[def].tri = mod; 220669bfad9SRoman Zippel sym->flags |= def_flags; 2211da177e4SLinus Torvalds break; 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds case S_BOOLEAN: 2241da177e4SLinus Torvalds if (p[0] == 'y') { 225669bfad9SRoman Zippel sym->def[def].tri = yes; 226669bfad9SRoman Zippel sym->flags |= def_flags; 2271da177e4SLinus Torvalds break; 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds if (p[0] == 'n') { 230669bfad9SRoman Zippel sym->def[def].tri = no; 231669bfad9SRoman Zippel sym->flags |= def_flags; 2321da177e4SLinus Torvalds break; 2331da177e4SLinus Torvalds } 234c1a0f5e3SRoman Zippel conf_warning("symbol value '%s' invalid for %s", p, sym->name); 2351da177e4SLinus Torvalds break; 236669bfad9SRoman Zippel case S_OTHER: 237669bfad9SRoman Zippel if (*p != '"') { 238669bfad9SRoman Zippel for (p2 = p; *p2 && !isspace(*p2); p2++) 239669bfad9SRoman Zippel ; 240669bfad9SRoman Zippel sym->type = S_STRING; 241669bfad9SRoman Zippel goto done; 242669bfad9SRoman Zippel } 2431da177e4SLinus Torvalds case S_STRING: 2441da177e4SLinus Torvalds if (*p++ != '"') 2451da177e4SLinus Torvalds break; 2461da177e4SLinus Torvalds for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) { 2471da177e4SLinus Torvalds if (*p2 == '"') { 2481da177e4SLinus Torvalds *p2 = 0; 2491da177e4SLinus Torvalds break; 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds memmove(p2, p2 + 1, strlen(p2)); 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds if (!p2) { 254c1a0f5e3SRoman Zippel conf_warning("invalid string found"); 255c1a0f5e3SRoman Zippel continue; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds case S_INT: 2581da177e4SLinus Torvalds case S_HEX: 259669bfad9SRoman Zippel done: 2601da177e4SLinus Torvalds if (sym_string_valid(sym, p)) { 261669bfad9SRoman Zippel sym->def[def].val = strdup(p); 262669bfad9SRoman Zippel sym->flags |= def_flags; 2631da177e4SLinus Torvalds } else { 264c1a0f5e3SRoman Zippel conf_warning("symbol value '%s' invalid for %s", p, sym->name); 265c1a0f5e3SRoman Zippel continue; 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds break; 2681da177e4SLinus Torvalds default: 2691da177e4SLinus Torvalds ; 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds break; 272*d3660a8cSMatthew Wilcox case '\r': 2731da177e4SLinus Torvalds case '\n': 2741da177e4SLinus Torvalds break; 2751da177e4SLinus Torvalds default: 276c1a0f5e3SRoman Zippel conf_warning("unexpected data"); 2771da177e4SLinus Torvalds continue; 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds if (sym && sym_is_choice_value(sym)) { 2801da177e4SLinus Torvalds struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym)); 281669bfad9SRoman Zippel switch (sym->def[def].tri) { 2821da177e4SLinus Torvalds case no: 2831da177e4SLinus Torvalds break; 2841da177e4SLinus Torvalds case mod: 285669bfad9SRoman Zippel if (cs->def[def].tri == yes) { 286c1a0f5e3SRoman Zippel conf_warning("%s creates inconsistent choice state", sym->name); 287669bfad9SRoman Zippel cs->flags &= ~def_flags; 288c1a0f5e3SRoman Zippel } 2891da177e4SLinus Torvalds break; 2901da177e4SLinus Torvalds case yes: 291669bfad9SRoman Zippel if (cs->def[def].tri != no) { 292c1a0f5e3SRoman Zippel conf_warning("%s creates inconsistent choice state", sym->name); 293669bfad9SRoman Zippel cs->flags &= ~def_flags; 294c1a0f5e3SRoman Zippel } else 295669bfad9SRoman Zippel cs->def[def].val = sym; 2961da177e4SLinus Torvalds break; 2971da177e4SLinus Torvalds } 298669bfad9SRoman Zippel cs->def[def].tri = E_OR(cs->def[def].tri, sym->def[def].tri); 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds fclose(in); 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds if (modules_sym) 3041da177e4SLinus Torvalds sym_calc_value(modules_sym); 30590389160SRoman Zippel return 0; 30690389160SRoman Zippel } 30790389160SRoman Zippel 30890389160SRoman Zippel int conf_read(const char *name) 30990389160SRoman Zippel { 31090389160SRoman Zippel struct symbol *sym; 31190389160SRoman Zippel struct property *prop; 31290389160SRoman Zippel struct expr *e; 313669bfad9SRoman Zippel int i, flags; 31490389160SRoman Zippel 315ddc97cacSRoman Zippel sym_change_count = 0; 316ddc97cacSRoman Zippel 317669bfad9SRoman Zippel if (conf_read_simple(name, S_DEF_USER)) 31890389160SRoman Zippel return 1; 31990389160SRoman Zippel 3201da177e4SLinus Torvalds for_all_symbols(i, sym) { 3211da177e4SLinus Torvalds sym_calc_value(sym); 322c1a0f5e3SRoman Zippel if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO)) 323c1a0f5e3SRoman Zippel goto sym_ok; 324c1a0f5e3SRoman Zippel if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) { 325c1a0f5e3SRoman Zippel /* check that calculated value agrees with saved value */ 326c1a0f5e3SRoman Zippel switch (sym->type) { 327c1a0f5e3SRoman Zippel case S_BOOLEAN: 328c1a0f5e3SRoman Zippel case S_TRISTATE: 3290c1822e6SRoman Zippel if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym)) 330c1a0f5e3SRoman Zippel break; 331c1a0f5e3SRoman Zippel if (!sym_is_choice(sym)) 332c1a0f5e3SRoman Zippel goto sym_ok; 333c1a0f5e3SRoman Zippel default: 3340c1822e6SRoman Zippel if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val)) 335c1a0f5e3SRoman Zippel goto sym_ok; 336c1a0f5e3SRoman Zippel break; 337c1a0f5e3SRoman Zippel } 338c1a0f5e3SRoman Zippel } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE)) 339c1a0f5e3SRoman Zippel /* no previous value and not saved */ 340c1a0f5e3SRoman Zippel goto sym_ok; 341c1a0f5e3SRoman Zippel conf_unsaved++; 342c1a0f5e3SRoman Zippel /* maybe print value in verbose mode... */ 343c1a0f5e3SRoman Zippel sym_ok: 3441da177e4SLinus Torvalds if (sym_has_value(sym) && !sym_is_choice_value(sym)) { 3451da177e4SLinus Torvalds if (sym->visible == no) 346669bfad9SRoman Zippel sym->flags &= ~SYMBOL_DEF_USER; 3471da177e4SLinus Torvalds switch (sym->type) { 3481da177e4SLinus Torvalds case S_STRING: 3491da177e4SLinus Torvalds case S_INT: 3501da177e4SLinus Torvalds case S_HEX: 351669bfad9SRoman Zippel if (!sym_string_within_range(sym, sym->def[S_DEF_USER].val)) 352669bfad9SRoman Zippel sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER); 3531da177e4SLinus Torvalds default: 3541da177e4SLinus Torvalds break; 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds if (!sym_is_choice(sym)) 3581da177e4SLinus Torvalds continue; 3591da177e4SLinus Torvalds prop = sym_get_choice_prop(sym); 360669bfad9SRoman Zippel flags = sym->flags; 3611da177e4SLinus Torvalds for (e = prop->expr; e; e = e->left.expr) 3621da177e4SLinus Torvalds if (e->right.sym->visible != no) 363669bfad9SRoman Zippel flags &= e->right.sym->flags; 364002d27b1SRoman Zippel sym->flags &= flags | ~SYMBOL_DEF_USER; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 367ddc97cacSRoman Zippel sym_change_count += conf_warnings || conf_unsaved; 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds return 0; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds int conf_write(const char *name) 3731da177e4SLinus Torvalds { 374c955ccafSRoman Zippel FILE *out; 3751da177e4SLinus Torvalds struct symbol *sym; 3761da177e4SLinus Torvalds struct menu *menu; 3771da177e4SLinus Torvalds const char *basename; 3781da177e4SLinus Torvalds char dirname[128], tmpname[128], newname[128]; 3791da177e4SLinus Torvalds int type, l; 3801da177e4SLinus Torvalds const char *str; 3811da177e4SLinus Torvalds time_t now; 3821da177e4SLinus Torvalds int use_timestamp = 1; 3831da177e4SLinus Torvalds char *env; 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds dirname[0] = 0; 3861da177e4SLinus Torvalds if (name && name[0]) { 3871da177e4SLinus Torvalds struct stat st; 3881da177e4SLinus Torvalds char *slash; 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds if (!stat(name, &st) && S_ISDIR(st.st_mode)) { 3911da177e4SLinus Torvalds strcpy(dirname, name); 3921da177e4SLinus Torvalds strcat(dirname, "/"); 39314cdd3c4SRoman Zippel basename = conf_get_configname(); 3941da177e4SLinus Torvalds } else if ((slash = strrchr(name, '/'))) { 3951da177e4SLinus Torvalds int size = slash - name + 1; 3961da177e4SLinus Torvalds memcpy(dirname, name, size); 3971da177e4SLinus Torvalds dirname[size] = 0; 3981da177e4SLinus Torvalds if (slash[1]) 3991da177e4SLinus Torvalds basename = slash + 1; 4001da177e4SLinus Torvalds else 40114cdd3c4SRoman Zippel basename = conf_get_configname(); 4021da177e4SLinus Torvalds } else 4031da177e4SLinus Torvalds basename = name; 4041da177e4SLinus Torvalds } else 40514cdd3c4SRoman Zippel basename = conf_get_configname(); 4061da177e4SLinus Torvalds 40714cdd3c4SRoman Zippel sprintf(newname, "%s%s", dirname, basename); 40814cdd3c4SRoman Zippel env = getenv("KCONFIG_OVERWRITECONFIG"); 40914cdd3c4SRoman Zippel if (!env || !*env) { 41014cdd3c4SRoman Zippel sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid()); 41114cdd3c4SRoman Zippel out = fopen(tmpname, "w"); 41214cdd3c4SRoman Zippel } else { 41314cdd3c4SRoman Zippel *tmpname = 0; 4141da177e4SLinus Torvalds out = fopen(newname, "w"); 41514cdd3c4SRoman Zippel } 4161da177e4SLinus Torvalds if (!out) 4171da177e4SLinus Torvalds return 1; 41814cdd3c4SRoman Zippel 4192244cbd8SSam Ravnborg sym = sym_lookup("KERNELVERSION", 0); 4201da177e4SLinus Torvalds sym_calc_value(sym); 4211da177e4SLinus Torvalds time(&now); 4221da177e4SLinus Torvalds env = getenv("KCONFIG_NOTIMESTAMP"); 4231da177e4SLinus Torvalds if (env && *env) 4241da177e4SLinus Torvalds use_timestamp = 0; 4251da177e4SLinus Torvalds 4263b9fa093SArnaldo Carvalho de Melo fprintf(out, _("#\n" 4271da177e4SLinus Torvalds "# Automatically generated make config: don't edit\n" 4281da177e4SLinus Torvalds "# Linux kernel version: %s\n" 4291da177e4SLinus Torvalds "%s%s" 4303b9fa093SArnaldo Carvalho de Melo "#\n"), 4311da177e4SLinus Torvalds sym_get_string_value(sym), 4321da177e4SLinus Torvalds use_timestamp ? "# " : "", 4331da177e4SLinus Torvalds use_timestamp ? ctime(&now) : ""); 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds if (!sym_change_count) 4361da177e4SLinus Torvalds sym_clear_all_valid(); 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds menu = rootmenu.list; 4391da177e4SLinus Torvalds while (menu) { 4401da177e4SLinus Torvalds sym = menu->sym; 4411da177e4SLinus Torvalds if (!sym) { 4421da177e4SLinus Torvalds if (!menu_is_visible(menu)) 4431da177e4SLinus Torvalds goto next; 4441da177e4SLinus Torvalds str = menu_get_prompt(menu); 4451da177e4SLinus Torvalds fprintf(out, "\n" 4461da177e4SLinus Torvalds "#\n" 4471da177e4SLinus Torvalds "# %s\n" 4481da177e4SLinus Torvalds "#\n", str); 4491da177e4SLinus Torvalds } else if (!(sym->flags & SYMBOL_CHOICE)) { 4501da177e4SLinus Torvalds sym_calc_value(sym); 4511da177e4SLinus Torvalds if (!(sym->flags & SYMBOL_WRITE)) 4521da177e4SLinus Torvalds goto next; 4531da177e4SLinus Torvalds sym->flags &= ~SYMBOL_WRITE; 4541da177e4SLinus Torvalds type = sym->type; 4551da177e4SLinus Torvalds if (type == S_TRISTATE) { 4561da177e4SLinus Torvalds sym_calc_value(modules_sym); 4571da177e4SLinus Torvalds if (modules_sym->curr.tri == no) 4581da177e4SLinus Torvalds type = S_BOOLEAN; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds switch (type) { 4611da177e4SLinus Torvalds case S_BOOLEAN: 4621da177e4SLinus Torvalds case S_TRISTATE: 4631da177e4SLinus Torvalds switch (sym_get_tristate_value(sym)) { 4641da177e4SLinus Torvalds case no: 4651da177e4SLinus Torvalds fprintf(out, "# CONFIG_%s is not set\n", sym->name); 4661da177e4SLinus Torvalds break; 4671da177e4SLinus Torvalds case mod: 4681da177e4SLinus Torvalds fprintf(out, "CONFIG_%s=m\n", sym->name); 4691da177e4SLinus Torvalds break; 4701da177e4SLinus Torvalds case yes: 4711da177e4SLinus Torvalds fprintf(out, "CONFIG_%s=y\n", sym->name); 4721da177e4SLinus Torvalds break; 4731da177e4SLinus Torvalds } 4741da177e4SLinus Torvalds break; 4751da177e4SLinus Torvalds case S_STRING: 4761da177e4SLinus Torvalds str = sym_get_string_value(sym); 4771da177e4SLinus Torvalds fprintf(out, "CONFIG_%s=\"", sym->name); 478c955ccafSRoman Zippel while (1) { 4791da177e4SLinus Torvalds l = strcspn(str, "\"\\"); 4801da177e4SLinus Torvalds if (l) { 4811da177e4SLinus Torvalds fwrite(str, l, 1, out); 4821da177e4SLinus Torvalds str += l; 4831da177e4SLinus Torvalds } 484c955ccafSRoman Zippel if (!*str) 485c955ccafSRoman Zippel break; 486c955ccafSRoman Zippel fprintf(out, "\\%c", *str++); 487c955ccafSRoman Zippel } 4881da177e4SLinus Torvalds fputs("\"\n", out); 4891da177e4SLinus Torvalds break; 4901da177e4SLinus Torvalds case S_HEX: 4911da177e4SLinus Torvalds str = sym_get_string_value(sym); 4921da177e4SLinus Torvalds if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) { 4931da177e4SLinus Torvalds fprintf(out, "CONFIG_%s=%s\n", sym->name, str); 4941da177e4SLinus Torvalds break; 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds case S_INT: 4971da177e4SLinus Torvalds str = sym_get_string_value(sym); 4981da177e4SLinus Torvalds fprintf(out, "CONFIG_%s=%s\n", sym->name, str); 4991da177e4SLinus Torvalds break; 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds } 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds next: 5041da177e4SLinus Torvalds if (menu->list) { 5051da177e4SLinus Torvalds menu = menu->list; 5061da177e4SLinus Torvalds continue; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds if (menu->next) 5091da177e4SLinus Torvalds menu = menu->next; 5101da177e4SLinus Torvalds else while ((menu = menu->parent)) { 5111da177e4SLinus Torvalds if (menu->next) { 5121da177e4SLinus Torvalds menu = menu->next; 5131da177e4SLinus Torvalds break; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds fclose(out); 51814cdd3c4SRoman Zippel 51914cdd3c4SRoman Zippel if (*tmpname) { 52014cdd3c4SRoman Zippel strcat(dirname, name ? name : conf_get_configname()); 52114cdd3c4SRoman Zippel strcat(dirname, ".old"); 52214cdd3c4SRoman Zippel rename(newname, dirname); 52314cdd3c4SRoman Zippel if (rename(tmpname, newname)) 5241da177e4SLinus Torvalds return 1; 52514cdd3c4SRoman Zippel } 5261da177e4SLinus Torvalds 527ddc97cacSRoman Zippel printf(_("#\n" 528ddc97cacSRoman Zippel "# configuration written to %s\n" 52914cdd3c4SRoman Zippel "#\n"), newname); 530ddc97cacSRoman Zippel 5311da177e4SLinus Torvalds sym_change_count = 0; 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds return 0; 5341da177e4SLinus Torvalds } 535c955ccafSRoman Zippel 5362e3646e5SRoman Zippel int conf_split_config(void) 5372e3646e5SRoman Zippel { 5382e3646e5SRoman Zippel char *name, path[128]; 5392e3646e5SRoman Zippel char *s, *d, c; 5402e3646e5SRoman Zippel struct symbol *sym; 5412e3646e5SRoman Zippel struct stat sb; 5422e3646e5SRoman Zippel int res, i, fd; 5432e3646e5SRoman Zippel 5442e3646e5SRoman Zippel name = getenv("KCONFIG_AUTOCONFIG"); 5452e3646e5SRoman Zippel if (!name) 5462e3646e5SRoman Zippel name = "include/config/auto.conf"; 5472e3646e5SRoman Zippel conf_read_simple(name, S_DEF_AUTO); 5482e3646e5SRoman Zippel 5492e3646e5SRoman Zippel if (chdir("include/config")) 5502e3646e5SRoman Zippel return 1; 5512e3646e5SRoman Zippel 5522e3646e5SRoman Zippel res = 0; 5532e3646e5SRoman Zippel for_all_symbols(i, sym) { 5542e3646e5SRoman Zippel sym_calc_value(sym); 5552e3646e5SRoman Zippel if ((sym->flags & SYMBOL_AUTO) || !sym->name) 5562e3646e5SRoman Zippel continue; 5572e3646e5SRoman Zippel if (sym->flags & SYMBOL_WRITE) { 5582e3646e5SRoman Zippel if (sym->flags & SYMBOL_DEF_AUTO) { 5592e3646e5SRoman Zippel /* 5602e3646e5SRoman Zippel * symbol has old and new value, 5612e3646e5SRoman Zippel * so compare them... 5622e3646e5SRoman Zippel */ 5632e3646e5SRoman Zippel switch (sym->type) { 5642e3646e5SRoman Zippel case S_BOOLEAN: 5652e3646e5SRoman Zippel case S_TRISTATE: 5662e3646e5SRoman Zippel if (sym_get_tristate_value(sym) == 5672e3646e5SRoman Zippel sym->def[S_DEF_AUTO].tri) 5682e3646e5SRoman Zippel continue; 5692e3646e5SRoman Zippel break; 5702e3646e5SRoman Zippel case S_STRING: 5712e3646e5SRoman Zippel case S_HEX: 5722e3646e5SRoman Zippel case S_INT: 5732e3646e5SRoman Zippel if (!strcmp(sym_get_string_value(sym), 5742e3646e5SRoman Zippel sym->def[S_DEF_AUTO].val)) 5752e3646e5SRoman Zippel continue; 5762e3646e5SRoman Zippel break; 5772e3646e5SRoman Zippel default: 5782e3646e5SRoman Zippel break; 5792e3646e5SRoman Zippel } 5802e3646e5SRoman Zippel } else { 5812e3646e5SRoman Zippel /* 5822e3646e5SRoman Zippel * If there is no old value, only 'no' (unset) 5832e3646e5SRoman Zippel * is allowed as new value. 5842e3646e5SRoman Zippel */ 5852e3646e5SRoman Zippel switch (sym->type) { 5862e3646e5SRoman Zippel case S_BOOLEAN: 5872e3646e5SRoman Zippel case S_TRISTATE: 5882e3646e5SRoman Zippel if (sym_get_tristate_value(sym) == no) 5892e3646e5SRoman Zippel continue; 5902e3646e5SRoman Zippel break; 5912e3646e5SRoman Zippel default: 5922e3646e5SRoman Zippel break; 5932e3646e5SRoman Zippel } 5942e3646e5SRoman Zippel } 5952e3646e5SRoman Zippel } else if (!(sym->flags & SYMBOL_DEF_AUTO)) 5962e3646e5SRoman Zippel /* There is neither an old nor a new value. */ 5972e3646e5SRoman Zippel continue; 5982e3646e5SRoman Zippel /* else 5992e3646e5SRoman Zippel * There is an old value, but no new value ('no' (unset) 6002e3646e5SRoman Zippel * isn't saved in auto.conf, so the old value is always 6012e3646e5SRoman Zippel * different from 'no'). 6022e3646e5SRoman Zippel */ 6032e3646e5SRoman Zippel 6042e3646e5SRoman Zippel /* Replace all '_' and append ".h" */ 6052e3646e5SRoman Zippel s = sym->name; 6062e3646e5SRoman Zippel d = path; 6072e3646e5SRoman Zippel while ((c = *s++)) { 6082e3646e5SRoman Zippel c = tolower(c); 6092e3646e5SRoman Zippel *d++ = (c == '_') ? '/' : c; 6102e3646e5SRoman Zippel } 6112e3646e5SRoman Zippel strcpy(d, ".h"); 6122e3646e5SRoman Zippel 6132e3646e5SRoman Zippel /* Assume directory path already exists. */ 6142e3646e5SRoman Zippel fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); 6152e3646e5SRoman Zippel if (fd == -1) { 6162e3646e5SRoman Zippel if (errno != ENOENT) { 6172e3646e5SRoman Zippel res = 1; 6182e3646e5SRoman Zippel break; 6192e3646e5SRoman Zippel } 6202e3646e5SRoman Zippel /* 6212e3646e5SRoman Zippel * Create directory components, 6222e3646e5SRoman Zippel * unless they exist already. 6232e3646e5SRoman Zippel */ 6242e3646e5SRoman Zippel d = path; 6252e3646e5SRoman Zippel while ((d = strchr(d, '/'))) { 6262e3646e5SRoman Zippel *d = 0; 6272e3646e5SRoman Zippel if (stat(path, &sb) && mkdir(path, 0755)) { 6282e3646e5SRoman Zippel res = 1; 6292e3646e5SRoman Zippel goto out; 6302e3646e5SRoman Zippel } 6312e3646e5SRoman Zippel *d++ = '/'; 6322e3646e5SRoman Zippel } 6332e3646e5SRoman Zippel /* Try it again. */ 6342e3646e5SRoman Zippel fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); 6352e3646e5SRoman Zippel if (fd == -1) { 6362e3646e5SRoman Zippel res = 1; 6372e3646e5SRoman Zippel break; 6382e3646e5SRoman Zippel } 6392e3646e5SRoman Zippel } 6402e3646e5SRoman Zippel close(fd); 6412e3646e5SRoman Zippel } 6422e3646e5SRoman Zippel out: 6432e3646e5SRoman Zippel if (chdir("../..")) 6442e3646e5SRoman Zippel return 1; 6452e3646e5SRoman Zippel 6462e3646e5SRoman Zippel return res; 6472e3646e5SRoman Zippel } 6482e3646e5SRoman Zippel 649c955ccafSRoman Zippel int conf_write_autoconf(void) 650c955ccafSRoman Zippel { 651c955ccafSRoman Zippel struct symbol *sym; 652c955ccafSRoman Zippel const char *str; 653c955ccafSRoman Zippel char *name; 654c955ccafSRoman Zippel FILE *out, *out_h; 655c955ccafSRoman Zippel time_t now; 656c955ccafSRoman Zippel int i, l; 657c955ccafSRoman Zippel 6582e3646e5SRoman Zippel sym_clear_all_valid(); 6592e3646e5SRoman Zippel 660c955ccafSRoman Zippel file_write_dep("include/config/auto.conf.cmd"); 661c955ccafSRoman Zippel 6622e3646e5SRoman Zippel if (conf_split_config()) 6632e3646e5SRoman Zippel return 1; 6642e3646e5SRoman Zippel 665c955ccafSRoman Zippel out = fopen(".tmpconfig", "w"); 666c955ccafSRoman Zippel if (!out) 667c955ccafSRoman Zippel return 1; 668c955ccafSRoman Zippel 669c955ccafSRoman Zippel out_h = fopen(".tmpconfig.h", "w"); 670c955ccafSRoman Zippel if (!out_h) { 671c955ccafSRoman Zippel fclose(out); 672c955ccafSRoman Zippel return 1; 673c955ccafSRoman Zippel } 674c955ccafSRoman Zippel 675c955ccafSRoman Zippel sym = sym_lookup("KERNELVERSION", 0); 676c955ccafSRoman Zippel sym_calc_value(sym); 677c955ccafSRoman Zippel time(&now); 678c955ccafSRoman Zippel fprintf(out, "#\n" 679c955ccafSRoman Zippel "# Automatically generated make config: don't edit\n" 680c955ccafSRoman Zippel "# Linux kernel version: %s\n" 681c955ccafSRoman Zippel "# %s" 682c955ccafSRoman Zippel "#\n", 683c955ccafSRoman Zippel sym_get_string_value(sym), ctime(&now)); 684c955ccafSRoman Zippel fprintf(out_h, "/*\n" 685c955ccafSRoman Zippel " * Automatically generated C config: don't edit\n" 686c955ccafSRoman Zippel " * Linux kernel version: %s\n" 687c955ccafSRoman Zippel " * %s" 688c955ccafSRoman Zippel " */\n" 689c955ccafSRoman Zippel "#define AUTOCONF_INCLUDED\n", 690c955ccafSRoman Zippel sym_get_string_value(sym), ctime(&now)); 691c955ccafSRoman Zippel 692c955ccafSRoman Zippel for_all_symbols(i, sym) { 693c955ccafSRoman Zippel sym_calc_value(sym); 694c955ccafSRoman Zippel if (!(sym->flags & SYMBOL_WRITE) || !sym->name) 695c955ccafSRoman Zippel continue; 696c955ccafSRoman Zippel switch (sym->type) { 697c955ccafSRoman Zippel case S_BOOLEAN: 698c955ccafSRoman Zippel case S_TRISTATE: 699c955ccafSRoman Zippel switch (sym_get_tristate_value(sym)) { 700c955ccafSRoman Zippel case no: 701c955ccafSRoman Zippel break; 702c955ccafSRoman Zippel case mod: 703c955ccafSRoman Zippel fprintf(out, "CONFIG_%s=m\n", sym->name); 704c955ccafSRoman Zippel fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name); 705c955ccafSRoman Zippel break; 706c955ccafSRoman Zippel case yes: 707c955ccafSRoman Zippel fprintf(out, "CONFIG_%s=y\n", sym->name); 708c955ccafSRoman Zippel fprintf(out_h, "#define CONFIG_%s 1\n", sym->name); 709c955ccafSRoman Zippel break; 710c955ccafSRoman Zippel } 711c955ccafSRoman Zippel break; 712c955ccafSRoman Zippel case S_STRING: 713c955ccafSRoman Zippel str = sym_get_string_value(sym); 714c955ccafSRoman Zippel fprintf(out, "CONFIG_%s=\"", sym->name); 715c955ccafSRoman Zippel fprintf(out_h, "#define CONFIG_%s \"", sym->name); 716c955ccafSRoman Zippel while (1) { 717c955ccafSRoman Zippel l = strcspn(str, "\"\\"); 718c955ccafSRoman Zippel if (l) { 719c955ccafSRoman Zippel fwrite(str, l, 1, out); 720c955ccafSRoman Zippel fwrite(str, l, 1, out_h); 721c955ccafSRoman Zippel str += l; 722c955ccafSRoman Zippel } 723c955ccafSRoman Zippel if (!*str) 724c955ccafSRoman Zippel break; 725c955ccafSRoman Zippel fprintf(out, "\\%c", *str); 726c955ccafSRoman Zippel fprintf(out_h, "\\%c", *str); 727c955ccafSRoman Zippel str++; 728c955ccafSRoman Zippel } 729c955ccafSRoman Zippel fputs("\"\n", out); 730c955ccafSRoman Zippel fputs("\"\n", out_h); 731c955ccafSRoman Zippel break; 732c955ccafSRoman Zippel case S_HEX: 733c955ccafSRoman Zippel str = sym_get_string_value(sym); 734c955ccafSRoman Zippel if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) { 735c955ccafSRoman Zippel fprintf(out, "CONFIG_%s=%s\n", sym->name, str); 736c955ccafSRoman Zippel fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str); 737c955ccafSRoman Zippel break; 738c955ccafSRoman Zippel } 739c955ccafSRoman Zippel case S_INT: 740c955ccafSRoman Zippel str = sym_get_string_value(sym); 741c955ccafSRoman Zippel fprintf(out, "CONFIG_%s=%s\n", sym->name, str); 742c955ccafSRoman Zippel fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str); 743c955ccafSRoman Zippel break; 744c955ccafSRoman Zippel default: 745c955ccafSRoman Zippel break; 746c955ccafSRoman Zippel } 747c955ccafSRoman Zippel } 748c955ccafSRoman Zippel fclose(out); 749c955ccafSRoman Zippel fclose(out_h); 750c955ccafSRoman Zippel 751c955ccafSRoman Zippel name = getenv("KCONFIG_AUTOHEADER"); 752c955ccafSRoman Zippel if (!name) 753c955ccafSRoman Zippel name = "include/linux/autoconf.h"; 754c955ccafSRoman Zippel if (rename(".tmpconfig.h", name)) 755c955ccafSRoman Zippel return 1; 756c955ccafSRoman Zippel name = getenv("KCONFIG_AUTOCONFIG"); 757c955ccafSRoman Zippel if (!name) 758c955ccafSRoman Zippel name = "include/config/auto.conf"; 759c955ccafSRoman Zippel /* 760c955ccafSRoman Zippel * This must be the last step, kbuild has a dependency on auto.conf 761c955ccafSRoman Zippel * and this marks the successful completion of the previous steps. 762c955ccafSRoman Zippel */ 763c955ccafSRoman Zippel if (rename(".tmpconfig", name)) 764c955ccafSRoman Zippel return 1; 765c955ccafSRoman Zippel 766c955ccafSRoman Zippel return 0; 767c955ccafSRoman Zippel } 768