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 37*14cdd3c4SRoman Zippel const char *conf_get_configname(void) 38*14cdd3c4SRoman Zippel { 39*14cdd3c4SRoman Zippel char *name = getenv("KCONFIG_CONFIG"); 40*14cdd3c4SRoman Zippel 41*14cdd3c4SRoman Zippel return name ? name : ".config"; 42*14cdd3c4SRoman Zippel } 43*14cdd3c4SRoman 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 99*14cdd3c4SRoman 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'); 1961da177e4SLinus Torvalds if (p2) 1971da177e4SLinus Torvalds *p2 = 0; 198669bfad9SRoman Zippel if (def == S_DEF_USER) { 1991da177e4SLinus Torvalds sym = sym_find(line + 7); 2001da177e4SLinus Torvalds if (!sym) { 201c1a0f5e3SRoman Zippel conf_warning("trying to assign nonexistent symbol %s", line + 7); 202c1a0f5e3SRoman Zippel break; 203669bfad9SRoman Zippel } 204669bfad9SRoman Zippel } else { 205669bfad9SRoman Zippel sym = sym_lookup(line + 7, 0); 206669bfad9SRoman Zippel if (sym->type == S_UNKNOWN) 207669bfad9SRoman Zippel sym->type = S_OTHER; 208669bfad9SRoman Zippel } 209669bfad9SRoman Zippel if (sym->flags & def_flags) { 210c1a0f5e3SRoman Zippel conf_warning("trying to reassign symbol %s", sym->name); 2111da177e4SLinus Torvalds break; 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds switch (sym->type) { 2141da177e4SLinus Torvalds case S_TRISTATE: 2151da177e4SLinus Torvalds if (p[0] == 'm') { 216669bfad9SRoman Zippel sym->def[def].tri = mod; 217669bfad9SRoman Zippel sym->flags |= def_flags; 2181da177e4SLinus Torvalds break; 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds case S_BOOLEAN: 2211da177e4SLinus Torvalds if (p[0] == 'y') { 222669bfad9SRoman Zippel sym->def[def].tri = yes; 223669bfad9SRoman Zippel sym->flags |= def_flags; 2241da177e4SLinus Torvalds break; 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds if (p[0] == 'n') { 227669bfad9SRoman Zippel sym->def[def].tri = no; 228669bfad9SRoman Zippel sym->flags |= def_flags; 2291da177e4SLinus Torvalds break; 2301da177e4SLinus Torvalds } 231c1a0f5e3SRoman Zippel conf_warning("symbol value '%s' invalid for %s", p, sym->name); 2321da177e4SLinus Torvalds break; 233669bfad9SRoman Zippel case S_OTHER: 234669bfad9SRoman Zippel if (*p != '"') { 235669bfad9SRoman Zippel for (p2 = p; *p2 && !isspace(*p2); p2++) 236669bfad9SRoman Zippel ; 237669bfad9SRoman Zippel sym->type = S_STRING; 238669bfad9SRoman Zippel goto done; 239669bfad9SRoman Zippel } 2401da177e4SLinus Torvalds case S_STRING: 2411da177e4SLinus Torvalds if (*p++ != '"') 2421da177e4SLinus Torvalds break; 2431da177e4SLinus Torvalds for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) { 2441da177e4SLinus Torvalds if (*p2 == '"') { 2451da177e4SLinus Torvalds *p2 = 0; 2461da177e4SLinus Torvalds break; 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds memmove(p2, p2 + 1, strlen(p2)); 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds if (!p2) { 251c1a0f5e3SRoman Zippel conf_warning("invalid string found"); 252c1a0f5e3SRoman Zippel continue; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds case S_INT: 2551da177e4SLinus Torvalds case S_HEX: 256669bfad9SRoman Zippel done: 2571da177e4SLinus Torvalds if (sym_string_valid(sym, p)) { 258669bfad9SRoman Zippel sym->def[def].val = strdup(p); 259669bfad9SRoman Zippel sym->flags |= def_flags; 2601da177e4SLinus Torvalds } else { 261c1a0f5e3SRoman Zippel conf_warning("symbol value '%s' invalid for %s", p, sym->name); 262c1a0f5e3SRoman Zippel continue; 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds break; 2651da177e4SLinus Torvalds default: 2661da177e4SLinus Torvalds ; 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds break; 2691da177e4SLinus Torvalds case '\n': 2701da177e4SLinus Torvalds break; 2711da177e4SLinus Torvalds default: 272c1a0f5e3SRoman Zippel conf_warning("unexpected data"); 2731da177e4SLinus Torvalds continue; 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds if (sym && sym_is_choice_value(sym)) { 2761da177e4SLinus Torvalds struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym)); 277669bfad9SRoman Zippel switch (sym->def[def].tri) { 2781da177e4SLinus Torvalds case no: 2791da177e4SLinus Torvalds break; 2801da177e4SLinus Torvalds case mod: 281669bfad9SRoman Zippel if (cs->def[def].tri == yes) { 282c1a0f5e3SRoman Zippel conf_warning("%s creates inconsistent choice state", sym->name); 283669bfad9SRoman Zippel cs->flags &= ~def_flags; 284c1a0f5e3SRoman Zippel } 2851da177e4SLinus Torvalds break; 2861da177e4SLinus Torvalds case yes: 287669bfad9SRoman Zippel if (cs->def[def].tri != no) { 288c1a0f5e3SRoman Zippel conf_warning("%s creates inconsistent choice state", sym->name); 289669bfad9SRoman Zippel cs->flags &= ~def_flags; 290c1a0f5e3SRoman Zippel } else 291669bfad9SRoman Zippel cs->def[def].val = sym; 2921da177e4SLinus Torvalds break; 2931da177e4SLinus Torvalds } 294669bfad9SRoman Zippel cs->def[def].tri = E_OR(cs->def[def].tri, sym->def[def].tri); 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds fclose(in); 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds if (modules_sym) 3001da177e4SLinus Torvalds sym_calc_value(modules_sym); 30190389160SRoman Zippel return 0; 30290389160SRoman Zippel } 30390389160SRoman Zippel 30490389160SRoman Zippel int conf_read(const char *name) 30590389160SRoman Zippel { 30690389160SRoman Zippel struct symbol *sym; 30790389160SRoman Zippel struct property *prop; 30890389160SRoman Zippel struct expr *e; 309669bfad9SRoman Zippel int i, flags; 31090389160SRoman Zippel 311ddc97cacSRoman Zippel sym_change_count = 0; 312ddc97cacSRoman Zippel 313669bfad9SRoman Zippel if (conf_read_simple(name, S_DEF_USER)) 31490389160SRoman Zippel return 1; 31590389160SRoman Zippel 3161da177e4SLinus Torvalds for_all_symbols(i, sym) { 3171da177e4SLinus Torvalds sym_calc_value(sym); 318c1a0f5e3SRoman Zippel if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO)) 319c1a0f5e3SRoman Zippel goto sym_ok; 320c1a0f5e3SRoman Zippel if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) { 321c1a0f5e3SRoman Zippel /* check that calculated value agrees with saved value */ 322c1a0f5e3SRoman Zippel switch (sym->type) { 323c1a0f5e3SRoman Zippel case S_BOOLEAN: 324c1a0f5e3SRoman Zippel case S_TRISTATE: 3250c1822e6SRoman Zippel if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym)) 326c1a0f5e3SRoman Zippel break; 327c1a0f5e3SRoman Zippel if (!sym_is_choice(sym)) 328c1a0f5e3SRoman Zippel goto sym_ok; 329c1a0f5e3SRoman Zippel default: 3300c1822e6SRoman Zippel if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val)) 331c1a0f5e3SRoman Zippel goto sym_ok; 332c1a0f5e3SRoman Zippel break; 333c1a0f5e3SRoman Zippel } 334c1a0f5e3SRoman Zippel } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE)) 335c1a0f5e3SRoman Zippel /* no previous value and not saved */ 336c1a0f5e3SRoman Zippel goto sym_ok; 337c1a0f5e3SRoman Zippel conf_unsaved++; 338c1a0f5e3SRoman Zippel /* maybe print value in verbose mode... */ 339c1a0f5e3SRoman Zippel sym_ok: 3401da177e4SLinus Torvalds if (sym_has_value(sym) && !sym_is_choice_value(sym)) { 3411da177e4SLinus Torvalds if (sym->visible == no) 342669bfad9SRoman Zippel sym->flags &= ~SYMBOL_DEF_USER; 3431da177e4SLinus Torvalds switch (sym->type) { 3441da177e4SLinus Torvalds case S_STRING: 3451da177e4SLinus Torvalds case S_INT: 3461da177e4SLinus Torvalds case S_HEX: 347669bfad9SRoman Zippel if (!sym_string_within_range(sym, sym->def[S_DEF_USER].val)) 348669bfad9SRoman Zippel sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER); 3491da177e4SLinus Torvalds default: 3501da177e4SLinus Torvalds break; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds if (!sym_is_choice(sym)) 3541da177e4SLinus Torvalds continue; 3551da177e4SLinus Torvalds prop = sym_get_choice_prop(sym); 356669bfad9SRoman Zippel flags = sym->flags; 3571da177e4SLinus Torvalds for (e = prop->expr; e; e = e->left.expr) 3581da177e4SLinus Torvalds if (e->right.sym->visible != no) 359669bfad9SRoman Zippel flags &= e->right.sym->flags; 360669bfad9SRoman Zippel sym->flags |= flags & SYMBOL_DEF_USER; 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds 363ddc97cacSRoman Zippel sym_change_count += conf_warnings || conf_unsaved; 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds return 0; 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds int conf_write(const char *name) 3691da177e4SLinus Torvalds { 370c955ccafSRoman Zippel FILE *out; 3711da177e4SLinus Torvalds struct symbol *sym; 3721da177e4SLinus Torvalds struct menu *menu; 3731da177e4SLinus Torvalds const char *basename; 3741da177e4SLinus Torvalds char dirname[128], tmpname[128], newname[128]; 3751da177e4SLinus Torvalds int type, l; 3761da177e4SLinus Torvalds const char *str; 3771da177e4SLinus Torvalds time_t now; 3781da177e4SLinus Torvalds int use_timestamp = 1; 3791da177e4SLinus Torvalds char *env; 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds dirname[0] = 0; 3821da177e4SLinus Torvalds if (name && name[0]) { 3831da177e4SLinus Torvalds struct stat st; 3841da177e4SLinus Torvalds char *slash; 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds if (!stat(name, &st) && S_ISDIR(st.st_mode)) { 3871da177e4SLinus Torvalds strcpy(dirname, name); 3881da177e4SLinus Torvalds strcat(dirname, "/"); 389*14cdd3c4SRoman Zippel basename = conf_get_configname(); 3901da177e4SLinus Torvalds } else if ((slash = strrchr(name, '/'))) { 3911da177e4SLinus Torvalds int size = slash - name + 1; 3921da177e4SLinus Torvalds memcpy(dirname, name, size); 3931da177e4SLinus Torvalds dirname[size] = 0; 3941da177e4SLinus Torvalds if (slash[1]) 3951da177e4SLinus Torvalds basename = slash + 1; 3961da177e4SLinus Torvalds else 397*14cdd3c4SRoman Zippel basename = conf_get_configname(); 3981da177e4SLinus Torvalds } else 3991da177e4SLinus Torvalds basename = name; 4001da177e4SLinus Torvalds } else 401*14cdd3c4SRoman Zippel basename = conf_get_configname(); 4021da177e4SLinus Torvalds 403*14cdd3c4SRoman Zippel sprintf(newname, "%s%s", dirname, basename); 404*14cdd3c4SRoman Zippel env = getenv("KCONFIG_OVERWRITECONFIG"); 405*14cdd3c4SRoman Zippel if (!env || !*env) { 406*14cdd3c4SRoman Zippel sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid()); 407*14cdd3c4SRoman Zippel out = fopen(tmpname, "w"); 408*14cdd3c4SRoman Zippel } else { 409*14cdd3c4SRoman Zippel *tmpname = 0; 4101da177e4SLinus Torvalds out = fopen(newname, "w"); 411*14cdd3c4SRoman Zippel } 4121da177e4SLinus Torvalds if (!out) 4131da177e4SLinus Torvalds return 1; 414*14cdd3c4SRoman Zippel 4152244cbd8SSam Ravnborg sym = sym_lookup("KERNELVERSION", 0); 4161da177e4SLinus Torvalds sym_calc_value(sym); 4171da177e4SLinus Torvalds time(&now); 4181da177e4SLinus Torvalds env = getenv("KCONFIG_NOTIMESTAMP"); 4191da177e4SLinus Torvalds if (env && *env) 4201da177e4SLinus Torvalds use_timestamp = 0; 4211da177e4SLinus Torvalds 4223b9fa093SArnaldo Carvalho de Melo fprintf(out, _("#\n" 4231da177e4SLinus Torvalds "# Automatically generated make config: don't edit\n" 4241da177e4SLinus Torvalds "# Linux kernel version: %s\n" 4251da177e4SLinus Torvalds "%s%s" 4263b9fa093SArnaldo Carvalho de Melo "#\n"), 4271da177e4SLinus Torvalds sym_get_string_value(sym), 4281da177e4SLinus Torvalds use_timestamp ? "# " : "", 4291da177e4SLinus Torvalds use_timestamp ? ctime(&now) : ""); 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds if (!sym_change_count) 4321da177e4SLinus Torvalds sym_clear_all_valid(); 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds menu = rootmenu.list; 4351da177e4SLinus Torvalds while (menu) { 4361da177e4SLinus Torvalds sym = menu->sym; 4371da177e4SLinus Torvalds if (!sym) { 4381da177e4SLinus Torvalds if (!menu_is_visible(menu)) 4391da177e4SLinus Torvalds goto next; 4401da177e4SLinus Torvalds str = menu_get_prompt(menu); 4411da177e4SLinus Torvalds fprintf(out, "\n" 4421da177e4SLinus Torvalds "#\n" 4431da177e4SLinus Torvalds "# %s\n" 4441da177e4SLinus Torvalds "#\n", str); 4451da177e4SLinus Torvalds } else if (!(sym->flags & SYMBOL_CHOICE)) { 4461da177e4SLinus Torvalds sym_calc_value(sym); 4471da177e4SLinus Torvalds if (!(sym->flags & SYMBOL_WRITE)) 4481da177e4SLinus Torvalds goto next; 4491da177e4SLinus Torvalds sym->flags &= ~SYMBOL_WRITE; 4501da177e4SLinus Torvalds type = sym->type; 4511da177e4SLinus Torvalds if (type == S_TRISTATE) { 4521da177e4SLinus Torvalds sym_calc_value(modules_sym); 4531da177e4SLinus Torvalds if (modules_sym->curr.tri == no) 4541da177e4SLinus Torvalds type = S_BOOLEAN; 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds switch (type) { 4571da177e4SLinus Torvalds case S_BOOLEAN: 4581da177e4SLinus Torvalds case S_TRISTATE: 4591da177e4SLinus Torvalds switch (sym_get_tristate_value(sym)) { 4601da177e4SLinus Torvalds case no: 4611da177e4SLinus Torvalds fprintf(out, "# CONFIG_%s is not set\n", sym->name); 4621da177e4SLinus Torvalds break; 4631da177e4SLinus Torvalds case mod: 4641da177e4SLinus Torvalds fprintf(out, "CONFIG_%s=m\n", sym->name); 4651da177e4SLinus Torvalds break; 4661da177e4SLinus Torvalds case yes: 4671da177e4SLinus Torvalds fprintf(out, "CONFIG_%s=y\n", sym->name); 4681da177e4SLinus Torvalds break; 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds break; 4711da177e4SLinus Torvalds case S_STRING: 4721da177e4SLinus Torvalds str = sym_get_string_value(sym); 4731da177e4SLinus Torvalds fprintf(out, "CONFIG_%s=\"", sym->name); 474c955ccafSRoman Zippel while (1) { 4751da177e4SLinus Torvalds l = strcspn(str, "\"\\"); 4761da177e4SLinus Torvalds if (l) { 4771da177e4SLinus Torvalds fwrite(str, l, 1, out); 4781da177e4SLinus Torvalds str += l; 4791da177e4SLinus Torvalds } 480c955ccafSRoman Zippel if (!*str) 481c955ccafSRoman Zippel break; 482c955ccafSRoman Zippel fprintf(out, "\\%c", *str++); 483c955ccafSRoman Zippel } 4841da177e4SLinus Torvalds fputs("\"\n", out); 4851da177e4SLinus Torvalds break; 4861da177e4SLinus Torvalds case S_HEX: 4871da177e4SLinus Torvalds str = sym_get_string_value(sym); 4881da177e4SLinus Torvalds if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) { 4891da177e4SLinus Torvalds fprintf(out, "CONFIG_%s=%s\n", sym->name, str); 4901da177e4SLinus Torvalds break; 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds case S_INT: 4931da177e4SLinus Torvalds str = sym_get_string_value(sym); 4941da177e4SLinus Torvalds fprintf(out, "CONFIG_%s=%s\n", sym->name, str); 4951da177e4SLinus Torvalds break; 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds next: 5001da177e4SLinus Torvalds if (menu->list) { 5011da177e4SLinus Torvalds menu = menu->list; 5021da177e4SLinus Torvalds continue; 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds if (menu->next) 5051da177e4SLinus Torvalds menu = menu->next; 5061da177e4SLinus Torvalds else while ((menu = menu->parent)) { 5071da177e4SLinus Torvalds if (menu->next) { 5081da177e4SLinus Torvalds menu = menu->next; 5091da177e4SLinus Torvalds break; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds fclose(out); 514*14cdd3c4SRoman Zippel 515*14cdd3c4SRoman Zippel if (*tmpname) { 516*14cdd3c4SRoman Zippel strcat(dirname, name ? name : conf_get_configname()); 517*14cdd3c4SRoman Zippel strcat(dirname, ".old"); 518*14cdd3c4SRoman Zippel rename(newname, dirname); 519*14cdd3c4SRoman Zippel if (rename(tmpname, newname)) 5201da177e4SLinus Torvalds return 1; 521*14cdd3c4SRoman Zippel } 5221da177e4SLinus Torvalds 523ddc97cacSRoman Zippel printf(_("#\n" 524ddc97cacSRoman Zippel "# configuration written to %s\n" 525*14cdd3c4SRoman Zippel "#\n"), newname); 526ddc97cacSRoman Zippel 5271da177e4SLinus Torvalds sym_change_count = 0; 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds return 0; 5301da177e4SLinus Torvalds } 531c955ccafSRoman Zippel 5322e3646e5SRoman Zippel int conf_split_config(void) 5332e3646e5SRoman Zippel { 5342e3646e5SRoman Zippel char *name, path[128]; 5352e3646e5SRoman Zippel char *s, *d, c; 5362e3646e5SRoman Zippel struct symbol *sym; 5372e3646e5SRoman Zippel struct stat sb; 5382e3646e5SRoman Zippel int res, i, fd; 5392e3646e5SRoman Zippel 5402e3646e5SRoman Zippel name = getenv("KCONFIG_AUTOCONFIG"); 5412e3646e5SRoman Zippel if (!name) 5422e3646e5SRoman Zippel name = "include/config/auto.conf"; 5432e3646e5SRoman Zippel conf_read_simple(name, S_DEF_AUTO); 5442e3646e5SRoman Zippel 5452e3646e5SRoman Zippel if (chdir("include/config")) 5462e3646e5SRoman Zippel return 1; 5472e3646e5SRoman Zippel 5482e3646e5SRoman Zippel res = 0; 5492e3646e5SRoman Zippel for_all_symbols(i, sym) { 5502e3646e5SRoman Zippel sym_calc_value(sym); 5512e3646e5SRoman Zippel if ((sym->flags & SYMBOL_AUTO) || !sym->name) 5522e3646e5SRoman Zippel continue; 5532e3646e5SRoman Zippel if (sym->flags & SYMBOL_WRITE) { 5542e3646e5SRoman Zippel if (sym->flags & SYMBOL_DEF_AUTO) { 5552e3646e5SRoman Zippel /* 5562e3646e5SRoman Zippel * symbol has old and new value, 5572e3646e5SRoman Zippel * so compare them... 5582e3646e5SRoman Zippel */ 5592e3646e5SRoman Zippel switch (sym->type) { 5602e3646e5SRoman Zippel case S_BOOLEAN: 5612e3646e5SRoman Zippel case S_TRISTATE: 5622e3646e5SRoman Zippel if (sym_get_tristate_value(sym) == 5632e3646e5SRoman Zippel sym->def[S_DEF_AUTO].tri) 5642e3646e5SRoman Zippel continue; 5652e3646e5SRoman Zippel break; 5662e3646e5SRoman Zippel case S_STRING: 5672e3646e5SRoman Zippel case S_HEX: 5682e3646e5SRoman Zippel case S_INT: 5692e3646e5SRoman Zippel if (!strcmp(sym_get_string_value(sym), 5702e3646e5SRoman Zippel sym->def[S_DEF_AUTO].val)) 5712e3646e5SRoman Zippel continue; 5722e3646e5SRoman Zippel break; 5732e3646e5SRoman Zippel default: 5742e3646e5SRoman Zippel break; 5752e3646e5SRoman Zippel } 5762e3646e5SRoman Zippel } else { 5772e3646e5SRoman Zippel /* 5782e3646e5SRoman Zippel * If there is no old value, only 'no' (unset) 5792e3646e5SRoman Zippel * is allowed as new value. 5802e3646e5SRoman Zippel */ 5812e3646e5SRoman Zippel switch (sym->type) { 5822e3646e5SRoman Zippel case S_BOOLEAN: 5832e3646e5SRoman Zippel case S_TRISTATE: 5842e3646e5SRoman Zippel if (sym_get_tristate_value(sym) == no) 5852e3646e5SRoman Zippel continue; 5862e3646e5SRoman Zippel break; 5872e3646e5SRoman Zippel default: 5882e3646e5SRoman Zippel break; 5892e3646e5SRoman Zippel } 5902e3646e5SRoman Zippel } 5912e3646e5SRoman Zippel } else if (!(sym->flags & SYMBOL_DEF_AUTO)) 5922e3646e5SRoman Zippel /* There is neither an old nor a new value. */ 5932e3646e5SRoman Zippel continue; 5942e3646e5SRoman Zippel /* else 5952e3646e5SRoman Zippel * There is an old value, but no new value ('no' (unset) 5962e3646e5SRoman Zippel * isn't saved in auto.conf, so the old value is always 5972e3646e5SRoman Zippel * different from 'no'). 5982e3646e5SRoman Zippel */ 5992e3646e5SRoman Zippel 6002e3646e5SRoman Zippel /* Replace all '_' and append ".h" */ 6012e3646e5SRoman Zippel s = sym->name; 6022e3646e5SRoman Zippel d = path; 6032e3646e5SRoman Zippel while ((c = *s++)) { 6042e3646e5SRoman Zippel c = tolower(c); 6052e3646e5SRoman Zippel *d++ = (c == '_') ? '/' : c; 6062e3646e5SRoman Zippel } 6072e3646e5SRoman Zippel strcpy(d, ".h"); 6082e3646e5SRoman Zippel 6092e3646e5SRoman Zippel /* Assume directory path already exists. */ 6102e3646e5SRoman Zippel fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); 6112e3646e5SRoman Zippel if (fd == -1) { 6122e3646e5SRoman Zippel if (errno != ENOENT) { 6132e3646e5SRoman Zippel res = 1; 6142e3646e5SRoman Zippel break; 6152e3646e5SRoman Zippel } 6162e3646e5SRoman Zippel /* 6172e3646e5SRoman Zippel * Create directory components, 6182e3646e5SRoman Zippel * unless they exist already. 6192e3646e5SRoman Zippel */ 6202e3646e5SRoman Zippel d = path; 6212e3646e5SRoman Zippel while ((d = strchr(d, '/'))) { 6222e3646e5SRoman Zippel *d = 0; 6232e3646e5SRoman Zippel if (stat(path, &sb) && mkdir(path, 0755)) { 6242e3646e5SRoman Zippel res = 1; 6252e3646e5SRoman Zippel goto out; 6262e3646e5SRoman Zippel } 6272e3646e5SRoman Zippel *d++ = '/'; 6282e3646e5SRoman Zippel } 6292e3646e5SRoman Zippel /* Try it again. */ 6302e3646e5SRoman Zippel fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); 6312e3646e5SRoman Zippel if (fd == -1) { 6322e3646e5SRoman Zippel res = 1; 6332e3646e5SRoman Zippel break; 6342e3646e5SRoman Zippel } 6352e3646e5SRoman Zippel } 6362e3646e5SRoman Zippel close(fd); 6372e3646e5SRoman Zippel } 6382e3646e5SRoman Zippel out: 6392e3646e5SRoman Zippel if (chdir("../..")) 6402e3646e5SRoman Zippel return 1; 6412e3646e5SRoman Zippel 6422e3646e5SRoman Zippel return res; 6432e3646e5SRoman Zippel } 6442e3646e5SRoman Zippel 645c955ccafSRoman Zippel int conf_write_autoconf(void) 646c955ccafSRoman Zippel { 647c955ccafSRoman Zippel struct symbol *sym; 648c955ccafSRoman Zippel const char *str; 649c955ccafSRoman Zippel char *name; 650c955ccafSRoman Zippel FILE *out, *out_h; 651c955ccafSRoman Zippel time_t now; 652c955ccafSRoman Zippel int i, l; 653c955ccafSRoman Zippel 6542e3646e5SRoman Zippel sym_clear_all_valid(); 6552e3646e5SRoman Zippel 656c955ccafSRoman Zippel file_write_dep("include/config/auto.conf.cmd"); 657c955ccafSRoman Zippel 6582e3646e5SRoman Zippel if (conf_split_config()) 6592e3646e5SRoman Zippel return 1; 6602e3646e5SRoman Zippel 661c955ccafSRoman Zippel out = fopen(".tmpconfig", "w"); 662c955ccafSRoman Zippel if (!out) 663c955ccafSRoman Zippel return 1; 664c955ccafSRoman Zippel 665c955ccafSRoman Zippel out_h = fopen(".tmpconfig.h", "w"); 666c955ccafSRoman Zippel if (!out_h) { 667c955ccafSRoman Zippel fclose(out); 668c955ccafSRoman Zippel return 1; 669c955ccafSRoman Zippel } 670c955ccafSRoman Zippel 671c955ccafSRoman Zippel sym = sym_lookup("KERNELVERSION", 0); 672c955ccafSRoman Zippel sym_calc_value(sym); 673c955ccafSRoman Zippel time(&now); 674c955ccafSRoman Zippel fprintf(out, "#\n" 675c955ccafSRoman Zippel "# Automatically generated make config: don't edit\n" 676c955ccafSRoman Zippel "# Linux kernel version: %s\n" 677c955ccafSRoman Zippel "# %s" 678c955ccafSRoman Zippel "#\n", 679c955ccafSRoman Zippel sym_get_string_value(sym), ctime(&now)); 680c955ccafSRoman Zippel fprintf(out_h, "/*\n" 681c955ccafSRoman Zippel " * Automatically generated C config: don't edit\n" 682c955ccafSRoman Zippel " * Linux kernel version: %s\n" 683c955ccafSRoman Zippel " * %s" 684c955ccafSRoman Zippel " */\n" 685c955ccafSRoman Zippel "#define AUTOCONF_INCLUDED\n", 686c955ccafSRoman Zippel sym_get_string_value(sym), ctime(&now)); 687c955ccafSRoman Zippel 688c955ccafSRoman Zippel for_all_symbols(i, sym) { 689c955ccafSRoman Zippel sym_calc_value(sym); 690c955ccafSRoman Zippel if (!(sym->flags & SYMBOL_WRITE) || !sym->name) 691c955ccafSRoman Zippel continue; 692c955ccafSRoman Zippel switch (sym->type) { 693c955ccafSRoman Zippel case S_BOOLEAN: 694c955ccafSRoman Zippel case S_TRISTATE: 695c955ccafSRoman Zippel switch (sym_get_tristate_value(sym)) { 696c955ccafSRoman Zippel case no: 697c955ccafSRoman Zippel break; 698c955ccafSRoman Zippel case mod: 699c955ccafSRoman Zippel fprintf(out, "CONFIG_%s=m\n", sym->name); 700c955ccafSRoman Zippel fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name); 701c955ccafSRoman Zippel break; 702c955ccafSRoman Zippel case yes: 703c955ccafSRoman Zippel fprintf(out, "CONFIG_%s=y\n", sym->name); 704c955ccafSRoman Zippel fprintf(out_h, "#define CONFIG_%s 1\n", sym->name); 705c955ccafSRoman Zippel break; 706c955ccafSRoman Zippel } 707c955ccafSRoman Zippel break; 708c955ccafSRoman Zippel case S_STRING: 709c955ccafSRoman Zippel str = sym_get_string_value(sym); 710c955ccafSRoman Zippel fprintf(out, "CONFIG_%s=\"", sym->name); 711c955ccafSRoman Zippel fprintf(out_h, "#define CONFIG_%s \"", sym->name); 712c955ccafSRoman Zippel while (1) { 713c955ccafSRoman Zippel l = strcspn(str, "\"\\"); 714c955ccafSRoman Zippel if (l) { 715c955ccafSRoman Zippel fwrite(str, l, 1, out); 716c955ccafSRoman Zippel fwrite(str, l, 1, out_h); 717c955ccafSRoman Zippel str += l; 718c955ccafSRoman Zippel } 719c955ccafSRoman Zippel if (!*str) 720c955ccafSRoman Zippel break; 721c955ccafSRoman Zippel fprintf(out, "\\%c", *str); 722c955ccafSRoman Zippel fprintf(out_h, "\\%c", *str); 723c955ccafSRoman Zippel str++; 724c955ccafSRoman Zippel } 725c955ccafSRoman Zippel fputs("\"\n", out); 726c955ccafSRoman Zippel fputs("\"\n", out_h); 727c955ccafSRoman Zippel break; 728c955ccafSRoman Zippel case S_HEX: 729c955ccafSRoman Zippel str = sym_get_string_value(sym); 730c955ccafSRoman Zippel if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) { 731c955ccafSRoman Zippel fprintf(out, "CONFIG_%s=%s\n", sym->name, str); 732c955ccafSRoman Zippel fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str); 733c955ccafSRoman Zippel break; 734c955ccafSRoman Zippel } 735c955ccafSRoman Zippel case S_INT: 736c955ccafSRoman Zippel str = sym_get_string_value(sym); 737c955ccafSRoman Zippel fprintf(out, "CONFIG_%s=%s\n", sym->name, str); 738c955ccafSRoman Zippel fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str); 739c955ccafSRoman Zippel break; 740c955ccafSRoman Zippel default: 741c955ccafSRoman Zippel break; 742c955ccafSRoman Zippel } 743c955ccafSRoman Zippel } 744c955ccafSRoman Zippel fclose(out); 745c955ccafSRoman Zippel fclose(out_h); 746c955ccafSRoman Zippel 747c955ccafSRoman Zippel name = getenv("KCONFIG_AUTOHEADER"); 748c955ccafSRoman Zippel if (!name) 749c955ccafSRoman Zippel name = "include/linux/autoconf.h"; 750c955ccafSRoman Zippel if (rename(".tmpconfig.h", name)) 751c955ccafSRoman Zippel return 1; 752c955ccafSRoman Zippel name = getenv("KCONFIG_AUTOCONFIG"); 753c955ccafSRoman Zippel if (!name) 754c955ccafSRoman Zippel name = "include/config/auto.conf"; 755c955ccafSRoman Zippel /* 756c955ccafSRoman Zippel * This must be the last step, kbuild has a dependency on auto.conf 757c955ccafSRoman Zippel * and this marks the successful completion of the previous steps. 758c955ccafSRoman Zippel */ 759c955ccafSRoman Zippel if (rename(".tmpconfig", name)) 760c955ccafSRoman Zippel return 1; 761c955ccafSRoman Zippel 762c955ccafSRoman Zippel return 0; 763c955ccafSRoman Zippel } 764