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> 894bedecaSArnaud Lacombe #include <errno.h> 92e3646e5SRoman Zippel #include <fcntl.h> 1010a4b277SArnaud Lacombe #include <stdarg.h> 111da177e4SLinus Torvalds #include <stdio.h> 121da177e4SLinus Torvalds #include <stdlib.h> 131da177e4SLinus Torvalds #include <string.h> 141da177e4SLinus Torvalds #include <time.h> 151da177e4SLinus Torvalds #include <unistd.h> 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include "lkc.h" 181da177e4SLinus Torvalds 19c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...) 20c1a0f5e3SRoman Zippel __attribute__ ((format (printf, 1, 2))); 21c1a0f5e3SRoman Zippel 2242368c37SMichal Marek static void conf_message(const char *fmt, ...) 2342368c37SMichal Marek __attribute__ ((format (printf, 1, 2))); 2442368c37SMichal Marek 25c1a0f5e3SRoman Zippel static const char *conf_filename; 26c1a0f5e3SRoman Zippel static int conf_lineno, conf_warnings, conf_unsaved; 27c1a0f5e3SRoman Zippel 281da177e4SLinus Torvalds const char conf_defname[] = "arch/$ARCH/defconfig"; 291da177e4SLinus Torvalds 30c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...) 31c1a0f5e3SRoman Zippel { 32c1a0f5e3SRoman Zippel va_list ap; 33c1a0f5e3SRoman Zippel va_start(ap, fmt); 34c1a0f5e3SRoman Zippel fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno); 35c1a0f5e3SRoman Zippel vfprintf(stderr, fmt, ap); 36c1a0f5e3SRoman Zippel fprintf(stderr, "\n"); 37c1a0f5e3SRoman Zippel va_end(ap); 38c1a0f5e3SRoman Zippel conf_warnings++; 39c1a0f5e3SRoman Zippel } 40c1a0f5e3SRoman Zippel 4142368c37SMichal Marek static void conf_default_message_callback(const char *fmt, va_list ap) 4242368c37SMichal Marek { 4342368c37SMichal Marek printf("#\n# "); 4442368c37SMichal Marek vprintf(fmt, ap); 4542368c37SMichal Marek printf("\n#\n"); 4642368c37SMichal Marek } 4742368c37SMichal Marek 4842368c37SMichal Marek static void (*conf_message_callback) (const char *fmt, va_list ap) = 4942368c37SMichal Marek conf_default_message_callback; 5042368c37SMichal Marek void conf_set_message_callback(void (*fn) (const char *fmt, va_list ap)) 5142368c37SMichal Marek { 5242368c37SMichal Marek conf_message_callback = fn; 5342368c37SMichal Marek } 5442368c37SMichal Marek 5542368c37SMichal Marek static void conf_message(const char *fmt, ...) 5642368c37SMichal Marek { 5742368c37SMichal Marek va_list ap; 5842368c37SMichal Marek 5942368c37SMichal Marek va_start(ap, fmt); 6042368c37SMichal Marek if (conf_message_callback) 6142368c37SMichal Marek conf_message_callback(fmt, ap); 6242368c37SMichal Marek } 6342368c37SMichal Marek 6414cdd3c4SRoman Zippel const char *conf_get_configname(void) 6514cdd3c4SRoman Zippel { 6614cdd3c4SRoman Zippel char *name = getenv("KCONFIG_CONFIG"); 6714cdd3c4SRoman Zippel 6814cdd3c4SRoman Zippel return name ? name : ".config"; 6914cdd3c4SRoman Zippel } 7014cdd3c4SRoman Zippel 7112122f62SMarkus Heidelberg const char *conf_get_autoconfig_name(void) 7212122f62SMarkus Heidelberg { 7312122f62SMarkus Heidelberg char *name = getenv("KCONFIG_AUTOCONFIG"); 7412122f62SMarkus Heidelberg 7512122f62SMarkus Heidelberg return name ? name : "include/config/auto.conf"; 7612122f62SMarkus Heidelberg } 7712122f62SMarkus Heidelberg 7848b9d03cSJ.A. Magallon static char *conf_expand_value(const char *in) 791da177e4SLinus Torvalds { 801da177e4SLinus Torvalds struct symbol *sym; 8148b9d03cSJ.A. Magallon const char *src; 821da177e4SLinus Torvalds static char res_value[SYMBOL_MAXLENGTH]; 831da177e4SLinus Torvalds char *dst, name[SYMBOL_MAXLENGTH]; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds res_value[0] = 0; 861da177e4SLinus Torvalds dst = name; 871da177e4SLinus Torvalds while ((src = strchr(in, '$'))) { 881da177e4SLinus Torvalds strncat(res_value, in, src - in); 891da177e4SLinus Torvalds src++; 901da177e4SLinus Torvalds dst = name; 911da177e4SLinus Torvalds while (isalnum(*src) || *src == '_') 921da177e4SLinus Torvalds *dst++ = *src++; 931da177e4SLinus Torvalds *dst = 0; 941da177e4SLinus Torvalds sym = sym_lookup(name, 0); 951da177e4SLinus Torvalds sym_calc_value(sym); 961da177e4SLinus Torvalds strcat(res_value, sym_get_string_value(sym)); 971da177e4SLinus Torvalds in = src; 981da177e4SLinus Torvalds } 991da177e4SLinus Torvalds strcat(res_value, in); 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds return res_value; 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds char *conf_get_default_confname(void) 1051da177e4SLinus Torvalds { 1061da177e4SLinus Torvalds struct stat buf; 1071da177e4SLinus Torvalds static char fullname[PATH_MAX+1]; 1081da177e4SLinus Torvalds char *env, *name; 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds name = conf_expand_value(conf_defname); 1111da177e4SLinus Torvalds env = getenv(SRCTREE); 1121da177e4SLinus Torvalds if (env) { 1131da177e4SLinus Torvalds sprintf(fullname, "%s/%s", env, name); 1141da177e4SLinus Torvalds if (!stat(fullname, &buf)) 1151da177e4SLinus Torvalds return fullname; 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds return name; 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds 1209c900a9cSSam Ravnborg static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p) 1219c900a9cSSam Ravnborg { 1229c900a9cSSam Ravnborg char *p2; 1239c900a9cSSam Ravnborg 1249c900a9cSSam Ravnborg switch (sym->type) { 1259c900a9cSSam Ravnborg case S_TRISTATE: 1269c900a9cSSam Ravnborg if (p[0] == 'm') { 1279c900a9cSSam Ravnborg sym->def[def].tri = mod; 1289c900a9cSSam Ravnborg sym->flags |= def_flags; 1299c900a9cSSam Ravnborg break; 1309c900a9cSSam Ravnborg } 131d8fc3200SArnaud Lacombe /* fall through */ 1329c900a9cSSam Ravnborg case S_BOOLEAN: 1339c900a9cSSam Ravnborg if (p[0] == 'y') { 1349c900a9cSSam Ravnborg sym->def[def].tri = yes; 1359c900a9cSSam Ravnborg sym->flags |= def_flags; 1369c900a9cSSam Ravnborg break; 1379c900a9cSSam Ravnborg } 1389c900a9cSSam Ravnborg if (p[0] == 'n') { 1399c900a9cSSam Ravnborg sym->def[def].tri = no; 1409c900a9cSSam Ravnborg sym->flags |= def_flags; 1419c900a9cSSam Ravnborg break; 1429c900a9cSSam Ravnborg } 1439c900a9cSSam Ravnborg conf_warning("symbol value '%s' invalid for %s", p, sym->name); 14475f1468bSArnaud Lacombe return 1; 1459c900a9cSSam Ravnborg case S_OTHER: 1469c900a9cSSam Ravnborg if (*p != '"') { 1479c900a9cSSam Ravnborg for (p2 = p; *p2 && !isspace(*p2); p2++) 1489c900a9cSSam Ravnborg ; 1499c900a9cSSam Ravnborg sym->type = S_STRING; 1509c900a9cSSam Ravnborg goto done; 1519c900a9cSSam Ravnborg } 152d8fc3200SArnaud Lacombe /* fall through */ 1539c900a9cSSam Ravnborg case S_STRING: 1549c900a9cSSam Ravnborg if (*p++ != '"') 1559c900a9cSSam Ravnborg break; 1569c900a9cSSam Ravnborg for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) { 1579c900a9cSSam Ravnborg if (*p2 == '"') { 1589c900a9cSSam Ravnborg *p2 = 0; 1599c900a9cSSam Ravnborg break; 1609c900a9cSSam Ravnborg } 1619c900a9cSSam Ravnborg memmove(p2, p2 + 1, strlen(p2)); 1629c900a9cSSam Ravnborg } 1639c900a9cSSam Ravnborg if (!p2) { 1649c900a9cSSam Ravnborg conf_warning("invalid string found"); 1659c900a9cSSam Ravnborg return 1; 1669c900a9cSSam Ravnborg } 167d8fc3200SArnaud Lacombe /* fall through */ 1689c900a9cSSam Ravnborg case S_INT: 1699c900a9cSSam Ravnborg case S_HEX: 1709c900a9cSSam Ravnborg done: 1719c900a9cSSam Ravnborg if (sym_string_valid(sym, p)) { 1729c900a9cSSam Ravnborg sym->def[def].val = strdup(p); 1739c900a9cSSam Ravnborg sym->flags |= def_flags; 1749c900a9cSSam Ravnborg } else { 1759c900a9cSSam Ravnborg conf_warning("symbol value '%s' invalid for %s", p, sym->name); 1769c900a9cSSam Ravnborg return 1; 1779c900a9cSSam Ravnborg } 1789c900a9cSSam Ravnborg break; 1799c900a9cSSam Ravnborg default: 1809c900a9cSSam Ravnborg ; 1819c900a9cSSam Ravnborg } 1829c900a9cSSam Ravnborg return 0; 1839c900a9cSSam Ravnborg } 1849c900a9cSSam Ravnborg 185669bfad9SRoman Zippel int conf_read_simple(const char *name, int def) 1861da177e4SLinus Torvalds { 1871da177e4SLinus Torvalds FILE *in = NULL; 1881da177e4SLinus Torvalds char line[1024]; 1891da177e4SLinus Torvalds char *p, *p2; 1901da177e4SLinus Torvalds struct symbol *sym; 191669bfad9SRoman Zippel int i, def_flags; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds if (name) { 1941da177e4SLinus Torvalds in = zconf_fopen(name); 1951da177e4SLinus Torvalds } else { 196face4374SRoman Zippel struct property *prop; 197face4374SRoman Zippel 19814cdd3c4SRoman Zippel name = conf_get_configname(); 199ddc97cacSRoman Zippel in = zconf_fopen(name); 200ddc97cacSRoman Zippel if (in) 201ddc97cacSRoman Zippel goto load; 202bfc10001SKarsten Wiese sym_add_change_count(1); 203ac1ffde1SUlf Magnusson if (!sym_defconfig_list) { 204ac1ffde1SUlf Magnusson if (modules_sym) 205ac1ffde1SUlf Magnusson sym_calc_value(modules_sym); 206face4374SRoman Zippel return 1; 207ac1ffde1SUlf Magnusson } 208face4374SRoman Zippel 209face4374SRoman Zippel for_all_defaults(sym_defconfig_list, prop) { 210face4374SRoman Zippel if (expr_calc_value(prop->visible.expr) == no || 211face4374SRoman Zippel prop->expr->type != E_SYMBOL) 212face4374SRoman Zippel continue; 213face4374SRoman Zippel name = conf_expand_value(prop->expr->left.sym->name); 2141da177e4SLinus Torvalds in = zconf_fopen(name); 2151da177e4SLinus Torvalds if (in) { 21642368c37SMichal Marek conf_message(_("using defaults found in %s"), 21742368c37SMichal Marek name); 218ddc97cacSRoman Zippel goto load; 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds if (!in) 2231da177e4SLinus Torvalds return 1; 2241da177e4SLinus Torvalds 225ddc97cacSRoman Zippel load: 226c1a0f5e3SRoman Zippel conf_filename = name; 227c1a0f5e3SRoman Zippel conf_lineno = 0; 228c1a0f5e3SRoman Zippel conf_warnings = 0; 229c1a0f5e3SRoman Zippel conf_unsaved = 0; 230c1a0f5e3SRoman Zippel 231669bfad9SRoman Zippel def_flags = SYMBOL_DEF << def; 2321da177e4SLinus Torvalds for_all_symbols(i, sym) { 233669bfad9SRoman Zippel sym->flags |= SYMBOL_CHANGED; 234669bfad9SRoman Zippel sym->flags &= ~(def_flags|SYMBOL_VALID); 235c1a0f5e3SRoman Zippel if (sym_is_choice(sym)) 236669bfad9SRoman Zippel sym->flags |= def_flags; 2371da177e4SLinus Torvalds switch (sym->type) { 2381da177e4SLinus Torvalds case S_INT: 2391da177e4SLinus Torvalds case S_HEX: 2401da177e4SLinus Torvalds case S_STRING: 241669bfad9SRoman Zippel if (sym->def[def].val) 242669bfad9SRoman Zippel free(sym->def[def].val); 243d8fc3200SArnaud Lacombe /* fall through */ 2441da177e4SLinus Torvalds default: 245669bfad9SRoman Zippel sym->def[def].val = NULL; 246669bfad9SRoman Zippel sym->def[def].tri = no; 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds while (fgets(line, sizeof(line), in)) { 251c1a0f5e3SRoman Zippel conf_lineno++; 2521da177e4SLinus Torvalds sym = NULL; 2538baefd30SArnaud Lacombe if (line[0] == '#') { 254ffb5957bSArnaud Lacombe if (memcmp(line + 2, CONFIG_, strlen(CONFIG_))) 2551da177e4SLinus Torvalds continue; 256ffb5957bSArnaud Lacombe p = strchr(line + 2 + strlen(CONFIG_), ' '); 2571da177e4SLinus Torvalds if (!p) 2581da177e4SLinus Torvalds continue; 2591da177e4SLinus Torvalds *p++ = 0; 2601da177e4SLinus Torvalds if (strncmp(p, "is not set", 10)) 2611da177e4SLinus Torvalds continue; 262669bfad9SRoman Zippel if (def == S_DEF_USER) { 263ffb5957bSArnaud Lacombe sym = sym_find(line + 2 + strlen(CONFIG_)); 264661b0680Szippel@linux-m68k.org if (!sym) { 265661b0680Szippel@linux-m68k.org sym_add_change_count(1); 2668bea7548SNaohiro Aota goto setsym; 267661b0680Szippel@linux-m68k.org } 268669bfad9SRoman Zippel } else { 269ffb5957bSArnaud Lacombe sym = sym_lookup(line + 2 + strlen(CONFIG_), 0); 270669bfad9SRoman Zippel if (sym->type == S_UNKNOWN) 271669bfad9SRoman Zippel sym->type = S_BOOLEAN; 272669bfad9SRoman Zippel } 273669bfad9SRoman Zippel if (sym->flags & def_flags) { 274d84876f9SJan Engelhardt conf_warning("override: reassigning to symbol %s", sym->name); 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds switch (sym->type) { 2771da177e4SLinus Torvalds case S_BOOLEAN: 2781da177e4SLinus Torvalds case S_TRISTATE: 279669bfad9SRoman Zippel sym->def[def].tri = no; 280669bfad9SRoman Zippel sym->flags |= def_flags; 2811da177e4SLinus Torvalds break; 2821da177e4SLinus Torvalds default: 2831da177e4SLinus Torvalds ; 2841da177e4SLinus Torvalds } 285ffb5957bSArnaud Lacombe } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) { 286ffb5957bSArnaud Lacombe p = strchr(line + strlen(CONFIG_), '='); 2871da177e4SLinus Torvalds if (!p) 2881da177e4SLinus Torvalds continue; 2891da177e4SLinus Torvalds *p++ = 0; 2901da177e4SLinus Torvalds p2 = strchr(p, '\n'); 291d3660a8cSMatthew Wilcox if (p2) { 292d3660a8cSMatthew Wilcox *p2-- = 0; 293d3660a8cSMatthew Wilcox if (*p2 == '\r') 2941da177e4SLinus Torvalds *p2 = 0; 295d3660a8cSMatthew Wilcox } 296669bfad9SRoman Zippel if (def == S_DEF_USER) { 297ffb5957bSArnaud Lacombe sym = sym_find(line + strlen(CONFIG_)); 298661b0680Szippel@linux-m68k.org if (!sym) { 299661b0680Szippel@linux-m68k.org sym_add_change_count(1); 3008bea7548SNaohiro Aota goto setsym; 301661b0680Szippel@linux-m68k.org } 302669bfad9SRoman Zippel } else { 303ffb5957bSArnaud Lacombe sym = sym_lookup(line + strlen(CONFIG_), 0); 304669bfad9SRoman Zippel if (sym->type == S_UNKNOWN) 305669bfad9SRoman Zippel sym->type = S_OTHER; 306669bfad9SRoman Zippel } 307669bfad9SRoman Zippel if (sym->flags & def_flags) { 308d84876f9SJan Engelhardt conf_warning("override: reassigning to symbol %s", sym->name); 3091da177e4SLinus Torvalds } 3109c900a9cSSam Ravnborg if (conf_set_sym_val(sym, def, def_flags, p)) 311c1a0f5e3SRoman Zippel continue; 3128baefd30SArnaud Lacombe } else { 3138baefd30SArnaud Lacombe if (line[0] != '\r' && line[0] != '\n') 314c1a0f5e3SRoman Zippel conf_warning("unexpected data"); 3151da177e4SLinus Torvalds continue; 3161da177e4SLinus Torvalds } 3178bea7548SNaohiro Aota setsym: 3181da177e4SLinus Torvalds if (sym && sym_is_choice_value(sym)) { 3191da177e4SLinus Torvalds struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym)); 320669bfad9SRoman Zippel switch (sym->def[def].tri) { 3211da177e4SLinus Torvalds case no: 3221da177e4SLinus Torvalds break; 3231da177e4SLinus Torvalds case mod: 324669bfad9SRoman Zippel if (cs->def[def].tri == yes) { 325c1a0f5e3SRoman Zippel conf_warning("%s creates inconsistent choice state", sym->name); 326669bfad9SRoman Zippel cs->flags &= ~def_flags; 327c1a0f5e3SRoman Zippel } 3281da177e4SLinus Torvalds break; 3291da177e4SLinus Torvalds case yes: 330d84876f9SJan Engelhardt if (cs->def[def].tri != no) 331d84876f9SJan Engelhardt conf_warning("override: %s changes choice state", sym->name); 332669bfad9SRoman Zippel cs->def[def].val = sym; 3331da177e4SLinus Torvalds break; 3341da177e4SLinus Torvalds } 335d6ee3576SSam Ravnborg cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri); 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds } 3381da177e4SLinus Torvalds fclose(in); 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds if (modules_sym) 3411da177e4SLinus Torvalds sym_calc_value(modules_sym); 34290389160SRoman Zippel return 0; 34390389160SRoman Zippel } 34490389160SRoman Zippel 34590389160SRoman Zippel int conf_read(const char *name) 34690389160SRoman Zippel { 3477a962923SRoman Zippel struct symbol *sym, *choice_sym; 34890389160SRoman Zippel struct property *prop; 34990389160SRoman Zippel struct expr *e; 350669bfad9SRoman Zippel int i, flags; 35190389160SRoman Zippel 352bfc10001SKarsten Wiese sym_set_change_count(0); 353ddc97cacSRoman Zippel 354669bfad9SRoman Zippel if (conf_read_simple(name, S_DEF_USER)) 35590389160SRoman Zippel return 1; 35690389160SRoman Zippel 3571da177e4SLinus Torvalds for_all_symbols(i, sym) { 3581da177e4SLinus Torvalds sym_calc_value(sym); 359c1a0f5e3SRoman Zippel if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO)) 360c1a0f5e3SRoman Zippel goto sym_ok; 361c1a0f5e3SRoman Zippel if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) { 362c1a0f5e3SRoman Zippel /* check that calculated value agrees with saved value */ 363c1a0f5e3SRoman Zippel switch (sym->type) { 364c1a0f5e3SRoman Zippel case S_BOOLEAN: 365c1a0f5e3SRoman Zippel case S_TRISTATE: 3660c1822e6SRoman Zippel if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym)) 367c1a0f5e3SRoman Zippel break; 368c1a0f5e3SRoman Zippel if (!sym_is_choice(sym)) 369c1a0f5e3SRoman Zippel goto sym_ok; 370d8fc3200SArnaud Lacombe /* fall through */ 371c1a0f5e3SRoman Zippel default: 3720c1822e6SRoman Zippel if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val)) 373c1a0f5e3SRoman Zippel goto sym_ok; 374c1a0f5e3SRoman Zippel break; 375c1a0f5e3SRoman Zippel } 376c1a0f5e3SRoman Zippel } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE)) 377c1a0f5e3SRoman Zippel /* no previous value and not saved */ 378c1a0f5e3SRoman Zippel goto sym_ok; 379c1a0f5e3SRoman Zippel conf_unsaved++; 380c1a0f5e3SRoman Zippel /* maybe print value in verbose mode... */ 381c1a0f5e3SRoman Zippel sym_ok: 3821da177e4SLinus Torvalds if (!sym_is_choice(sym)) 3831da177e4SLinus Torvalds continue; 384d8982ba1SRoman Zippel /* The choice symbol only has a set value (and thus is not new) 385d8982ba1SRoman Zippel * if all its visible childs have values. 386d8982ba1SRoman Zippel */ 3871da177e4SLinus Torvalds prop = sym_get_choice_prop(sym); 388669bfad9SRoman Zippel flags = sym->flags; 3897a962923SRoman Zippel expr_list_for_each_sym(prop->expr, e, choice_sym) 3907a962923SRoman Zippel if (choice_sym->visible != no) 3917a962923SRoman Zippel flags &= choice_sym->flags; 392002d27b1SRoman Zippel sym->flags &= flags | ~SYMBOL_DEF_USER; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 395d8982ba1SRoman Zippel for_all_symbols(i, sym) { 396d8982ba1SRoman Zippel if (sym_has_value(sym) && !sym_is_choice_value(sym)) { 397d8982ba1SRoman Zippel /* Reset values of generates values, so they'll appear 398d8982ba1SRoman Zippel * as new, if they should become visible, but that 399d8982ba1SRoman Zippel * doesn't quite work if the Kconfig and the saved 400d8982ba1SRoman Zippel * configuration disagree. 401d8982ba1SRoman Zippel */ 402d8982ba1SRoman Zippel if (sym->visible == no && !conf_unsaved) 403d8982ba1SRoman Zippel sym->flags &= ~SYMBOL_DEF_USER; 404d8982ba1SRoman Zippel switch (sym->type) { 405d8982ba1SRoman Zippel case S_STRING: 406d8982ba1SRoman Zippel case S_INT: 407d8982ba1SRoman Zippel case S_HEX: 408d8982ba1SRoman Zippel /* Reset a string value if it's out of range */ 409d8982ba1SRoman Zippel if (sym_string_within_range(sym, sym->def[S_DEF_USER].val)) 410d8982ba1SRoman Zippel break; 411d8982ba1SRoman Zippel sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER); 412d8982ba1SRoman Zippel conf_unsaved++; 413d8982ba1SRoman Zippel break; 414d8982ba1SRoman Zippel default: 415d8982ba1SRoman Zippel break; 416d8982ba1SRoman Zippel } 417d8982ba1SRoman Zippel } 418d8982ba1SRoman Zippel } 419d8982ba1SRoman Zippel 420bfc10001SKarsten Wiese sym_add_change_count(conf_warnings || conf_unsaved); 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds return 0; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds 425e54e692bSArnaud Lacombe /* 426e54e692bSArnaud Lacombe * Kconfig configuration printer 427e54e692bSArnaud Lacombe * 428e54e692bSArnaud Lacombe * This printer is used when generating the resulting configuration after 429e54e692bSArnaud Lacombe * kconfig invocation and `defconfig' files. Unset symbol might be omitted by 430e54e692bSArnaud Lacombe * passing a non-NULL argument to the printer. 431e54e692bSArnaud Lacombe * 432e54e692bSArnaud Lacombe */ 433e54e692bSArnaud Lacombe static void 434e54e692bSArnaud Lacombe kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg) 43549192f26SSam Ravnborg { 43649192f26SSam Ravnborg 4370dce6310SArnaud Lacombe switch (sym->type) { 43849192f26SSam Ravnborg case S_BOOLEAN: 43949192f26SSam Ravnborg case S_TRISTATE: 440e54e692bSArnaud Lacombe if (*value == 'n') { 441e54e692bSArnaud Lacombe bool skip_unset = (arg != NULL); 442e54e692bSArnaud Lacombe 443e54e692bSArnaud Lacombe if (!skip_unset) 444e54e692bSArnaud Lacombe fprintf(fp, "# %s%s is not set\n", 445ffb5957bSArnaud Lacombe CONFIG_, sym->name); 446e54e692bSArnaud Lacombe return; 44749192f26SSam Ravnborg } 44849192f26SSam Ravnborg break; 449e54e692bSArnaud Lacombe default: 45049192f26SSam Ravnborg break; 451e54e692bSArnaud Lacombe } 452e54e692bSArnaud Lacombe 453e54e692bSArnaud Lacombe fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value); 454e54e692bSArnaud Lacombe } 455e54e692bSArnaud Lacombe 456e54e692bSArnaud Lacombe static void 457e54e692bSArnaud Lacombe kconfig_print_comment(FILE *fp, const char *value, void *arg) 458e54e692bSArnaud Lacombe { 459e54e692bSArnaud Lacombe const char *p = value; 460e54e692bSArnaud Lacombe size_t l; 461e54e692bSArnaud Lacombe 462e54e692bSArnaud Lacombe for (;;) { 463e54e692bSArnaud Lacombe l = strcspn(p, "\n"); 464e54e692bSArnaud Lacombe fprintf(fp, "#"); 465e54e692bSArnaud Lacombe if (l) { 466e54e692bSArnaud Lacombe fprintf(fp, " "); 467e54e692bSArnaud Lacombe fwrite(p, l, 1, fp); 468e54e692bSArnaud Lacombe p += l; 469e54e692bSArnaud Lacombe } 470e54e692bSArnaud Lacombe fprintf(fp, "\n"); 471e54e692bSArnaud Lacombe if (*p++ == '\0') 47249192f26SSam Ravnborg break; 473e54e692bSArnaud Lacombe } 474e54e692bSArnaud Lacombe } 475e54e692bSArnaud Lacombe 476e54e692bSArnaud Lacombe static struct conf_printer kconfig_printer_cb = 477e54e692bSArnaud Lacombe { 478e54e692bSArnaud Lacombe .print_symbol = kconfig_print_symbol, 479e54e692bSArnaud Lacombe .print_comment = kconfig_print_comment, 480e54e692bSArnaud Lacombe }; 481e54e692bSArnaud Lacombe 482e54e692bSArnaud Lacombe /* 483e54e692bSArnaud Lacombe * Header printer 484e54e692bSArnaud Lacombe * 485e54e692bSArnaud Lacombe * This printer is used when generating the `include/generated/autoconf.h' file. 486e54e692bSArnaud Lacombe */ 487e54e692bSArnaud Lacombe static void 488e54e692bSArnaud Lacombe header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg) 489e54e692bSArnaud Lacombe { 490e54e692bSArnaud Lacombe 491e54e692bSArnaud Lacombe switch (sym->type) { 492e54e692bSArnaud Lacombe case S_BOOLEAN: 493*eb4cf5a6SArnaud Lacombe case S_TRISTATE: { 494*eb4cf5a6SArnaud Lacombe const char *suffix = ""; 495*eb4cf5a6SArnaud Lacombe 496e54e692bSArnaud Lacombe switch (*value) { 497e54e692bSArnaud Lacombe case 'n': 498e54e692bSArnaud Lacombe return; 499e54e692bSArnaud Lacombe case 'm': 500e54e692bSArnaud Lacombe suffix = "_MODULE"; 501*eb4cf5a6SArnaud Lacombe /* fall through */ 502e54e692bSArnaud Lacombe default: 503e54e692bSArnaud Lacombe value = "1"; 504e54e692bSArnaud Lacombe } 505*eb4cf5a6SArnaud Lacombe fprintf(fp, "#define %s%s%s %s\n", 506*eb4cf5a6SArnaud Lacombe CONFIG_, sym->name, suffix, value); 507*eb4cf5a6SArnaud Lacombe break; 508*eb4cf5a6SArnaud Lacombe } 509*eb4cf5a6SArnaud Lacombe case S_HEX: { 510*eb4cf5a6SArnaud Lacombe const char *prefix = ""; 511*eb4cf5a6SArnaud Lacombe 512*eb4cf5a6SArnaud Lacombe if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X')) 513*eb4cf5a6SArnaud Lacombe prefix = "0x"; 514*eb4cf5a6SArnaud Lacombe fprintf(fp, "#define %s%s %s%s\n", 515*eb4cf5a6SArnaud Lacombe CONFIG_, sym->name, prefix, value); 516*eb4cf5a6SArnaud Lacombe break; 517*eb4cf5a6SArnaud Lacombe } 518*eb4cf5a6SArnaud Lacombe case S_STRING: 519*eb4cf5a6SArnaud Lacombe case S_INT: 520*eb4cf5a6SArnaud Lacombe fprintf(fp, "#define %s%s %s\n", 521*eb4cf5a6SArnaud Lacombe CONFIG_, sym->name, value); 522e54e692bSArnaud Lacombe break; 523e54e692bSArnaud Lacombe default: 524e54e692bSArnaud Lacombe break; 525e54e692bSArnaud Lacombe } 526e54e692bSArnaud Lacombe 527e54e692bSArnaud Lacombe } 528e54e692bSArnaud Lacombe 529e54e692bSArnaud Lacombe static void 530e54e692bSArnaud Lacombe header_print_comment(FILE *fp, const char *value, void *arg) 531e54e692bSArnaud Lacombe { 532e54e692bSArnaud Lacombe const char *p = value; 533e54e692bSArnaud Lacombe size_t l; 534e54e692bSArnaud Lacombe 535e54e692bSArnaud Lacombe fprintf(fp, "/*\n"); 536e54e692bSArnaud Lacombe for (;;) { 537e54e692bSArnaud Lacombe l = strcspn(p, "\n"); 538e54e692bSArnaud Lacombe fprintf(fp, " *"); 539e54e692bSArnaud Lacombe if (l) { 540e54e692bSArnaud Lacombe fprintf(fp, " "); 541e54e692bSArnaud Lacombe fwrite(p, l, 1, fp); 542e54e692bSArnaud Lacombe p += l; 543e54e692bSArnaud Lacombe } 544e54e692bSArnaud Lacombe fprintf(fp, "\n"); 545e54e692bSArnaud Lacombe if (*p++ == '\0') 546e54e692bSArnaud Lacombe break; 547e54e692bSArnaud Lacombe } 548e54e692bSArnaud Lacombe fprintf(fp, " */\n"); 549e54e692bSArnaud Lacombe } 550e54e692bSArnaud Lacombe 551e54e692bSArnaud Lacombe static struct conf_printer header_printer_cb = 552e54e692bSArnaud Lacombe { 553e54e692bSArnaud Lacombe .print_symbol = header_print_symbol, 554e54e692bSArnaud Lacombe .print_comment = header_print_comment, 555e54e692bSArnaud Lacombe }; 556e54e692bSArnaud Lacombe 557e54e692bSArnaud Lacombe /* 558e54e692bSArnaud Lacombe * Function-style header printer 559e54e692bSArnaud Lacombe * 560e54e692bSArnaud Lacombe * This printer is used to generate the config_is_xxx() function-style macros 561e54e692bSArnaud Lacombe * in `include/generated/autoconf.h' 562e54e692bSArnaud Lacombe */ 563e54e692bSArnaud Lacombe static void 564e54e692bSArnaud Lacombe header_function_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg) 565e54e692bSArnaud Lacombe { 566e54e692bSArnaud Lacombe int val = 0; 567e54e692bSArnaud Lacombe char c; 568e54e692bSArnaud Lacombe char *tmp, *d; 569e54e692bSArnaud Lacombe 570e54e692bSArnaud Lacombe switch (sym->type) { 571e54e692bSArnaud Lacombe case S_BOOLEAN: 572e54e692bSArnaud Lacombe case S_TRISTATE: 573e54e692bSArnaud Lacombe break; 574e54e692bSArnaud Lacombe default: 575e54e692bSArnaud Lacombe return; 576e54e692bSArnaud Lacombe } 577e54e692bSArnaud Lacombe if (*value == 'm') 578e54e692bSArnaud Lacombe val = 2; 579e54e692bSArnaud Lacombe else if (*value == 'y') 580e54e692bSArnaud Lacombe val = 1; 581e54e692bSArnaud Lacombe 582e54e692bSArnaud Lacombe d = strdup(CONFIG_); 583e54e692bSArnaud Lacombe tmp = d; 584e54e692bSArnaud Lacombe while ((c = *d)) { 585e54e692bSArnaud Lacombe *d = tolower(c); 586e54e692bSArnaud Lacombe d++; 587e54e692bSArnaud Lacombe } 588e54e692bSArnaud Lacombe 589e54e692bSArnaud Lacombe fprintf(fp, "#define %sis_", tmp); 590e54e692bSArnaud Lacombe free(tmp); 591e54e692bSArnaud Lacombe 592e54e692bSArnaud Lacombe d = strdup(sym->name); 593e54e692bSArnaud Lacombe tmp = d; 594e54e692bSArnaud Lacombe while ((c = *d)) { 595e54e692bSArnaud Lacombe *d = tolower(c); 596e54e692bSArnaud Lacombe d++; 597e54e692bSArnaud Lacombe } 598e54e692bSArnaud Lacombe fprintf(fp, "%s%s() %d\n", tmp, (val > 1) ? "_module" : "", 599e54e692bSArnaud Lacombe val ? 1 : 0); 600e54e692bSArnaud Lacombe free(tmp); 601e54e692bSArnaud Lacombe } 602e54e692bSArnaud Lacombe 603e54e692bSArnaud Lacombe static struct conf_printer header_function_printer_cb = 604e54e692bSArnaud Lacombe { 605e54e692bSArnaud Lacombe .print_symbol = header_function_print_symbol, 606e54e692bSArnaud Lacombe }; 607e54e692bSArnaud Lacombe 608e54e692bSArnaud Lacombe 609e54e692bSArnaud Lacombe /* 610e54e692bSArnaud Lacombe * Tristate printer 611e54e692bSArnaud Lacombe * 612e54e692bSArnaud Lacombe * This printer is used when generating the `include/config/tristate.conf' file. 613e54e692bSArnaud Lacombe */ 614e54e692bSArnaud Lacombe static void 615e54e692bSArnaud Lacombe tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg) 616e54e692bSArnaud Lacombe { 617e54e692bSArnaud Lacombe 618e54e692bSArnaud Lacombe if (sym->type == S_TRISTATE && *value != 'n') 619e54e692bSArnaud Lacombe fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value)); 620e54e692bSArnaud Lacombe } 621e54e692bSArnaud Lacombe 622e54e692bSArnaud Lacombe static struct conf_printer tristate_printer_cb = 623e54e692bSArnaud Lacombe { 624e54e692bSArnaud Lacombe .print_symbol = tristate_print_symbol, 625e54e692bSArnaud Lacombe .print_comment = kconfig_print_comment, 626e54e692bSArnaud Lacombe }; 627e54e692bSArnaud Lacombe 628e54e692bSArnaud Lacombe static void conf_write_symbol(FILE *fp, struct symbol *sym, 629e54e692bSArnaud Lacombe struct conf_printer *printer, void *printer_arg) 630e54e692bSArnaud Lacombe { 631e54e692bSArnaud Lacombe const char *str; 632e54e692bSArnaud Lacombe 633e54e692bSArnaud Lacombe switch (sym->type) { 63449192f26SSam Ravnborg case S_OTHER: 63549192f26SSam Ravnborg case S_UNKNOWN: 63649192f26SSam Ravnborg break; 637e54e692bSArnaud Lacombe case S_STRING: 638e54e692bSArnaud Lacombe str = sym_get_string_value(sym); 639e54e692bSArnaud Lacombe str = sym_escape_string_value(str); 640e54e692bSArnaud Lacombe printer->print_symbol(fp, sym, str, printer_arg); 641e54e692bSArnaud Lacombe free((void *)str); 642e54e692bSArnaud Lacombe break; 643e54e692bSArnaud Lacombe default: 644e54e692bSArnaud Lacombe str = sym_get_string_value(sym); 645e54e692bSArnaud Lacombe printer->print_symbol(fp, sym, str, printer_arg); 64649192f26SSam Ravnborg } 64749192f26SSam Ravnborg } 64849192f26SSam Ravnborg 649e54e692bSArnaud Lacombe static void 650e54e692bSArnaud Lacombe conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg) 651e54e692bSArnaud Lacombe { 652e54e692bSArnaud Lacombe char buf[256]; 653e54e692bSArnaud Lacombe 654e54e692bSArnaud Lacombe snprintf(buf, sizeof(buf), 655e54e692bSArnaud Lacombe "\n" 656e54e692bSArnaud Lacombe "Automatically generated file; DO NOT EDIT.\n" 657e54e692bSArnaud Lacombe "%s\n", 658e54e692bSArnaud Lacombe rootmenu.prompt->text); 659e54e692bSArnaud Lacombe 660e54e692bSArnaud Lacombe printer->print_comment(fp, buf, printer_arg); 661e54e692bSArnaud Lacombe } 662e54e692bSArnaud Lacombe 6637cf3d73bSSam Ravnborg /* 6647cf3d73bSSam Ravnborg * Write out a minimal config. 6657cf3d73bSSam Ravnborg * All values that has default values are skipped as this is redundant. 6667cf3d73bSSam Ravnborg */ 6677cf3d73bSSam Ravnborg int conf_write_defconfig(const char *filename) 6687cf3d73bSSam Ravnborg { 6697cf3d73bSSam Ravnborg struct symbol *sym; 6707cf3d73bSSam Ravnborg struct menu *menu; 6717cf3d73bSSam Ravnborg FILE *out; 6727cf3d73bSSam Ravnborg 6737cf3d73bSSam Ravnborg out = fopen(filename, "w"); 6747cf3d73bSSam Ravnborg if (!out) 6757cf3d73bSSam Ravnborg return 1; 6767cf3d73bSSam Ravnborg 6777cf3d73bSSam Ravnborg sym_clear_all_valid(); 6787cf3d73bSSam Ravnborg 6797cf3d73bSSam Ravnborg /* Traverse all menus to find all relevant symbols */ 6807cf3d73bSSam Ravnborg menu = rootmenu.list; 6817cf3d73bSSam Ravnborg 6827cf3d73bSSam Ravnborg while (menu != NULL) 6837cf3d73bSSam Ravnborg { 6847cf3d73bSSam Ravnborg sym = menu->sym; 6857cf3d73bSSam Ravnborg if (sym == NULL) { 6867cf3d73bSSam Ravnborg if (!menu_is_visible(menu)) 6877cf3d73bSSam Ravnborg goto next_menu; 6887cf3d73bSSam Ravnborg } else if (!sym_is_choice(sym)) { 6897cf3d73bSSam Ravnborg sym_calc_value(sym); 6907cf3d73bSSam Ravnborg if (!(sym->flags & SYMBOL_WRITE)) 6917cf3d73bSSam Ravnborg goto next_menu; 6927cf3d73bSSam Ravnborg sym->flags &= ~SYMBOL_WRITE; 6937cf3d73bSSam Ravnborg /* If we cannot change the symbol - skip */ 6947cf3d73bSSam Ravnborg if (!sym_is_changable(sym)) 6957cf3d73bSSam Ravnborg goto next_menu; 6967cf3d73bSSam Ravnborg /* If symbol equals to default value - skip */ 6977cf3d73bSSam Ravnborg if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0) 6987cf3d73bSSam Ravnborg goto next_menu; 6997cf3d73bSSam Ravnborg 7007cf3d73bSSam Ravnborg /* 7017cf3d73bSSam Ravnborg * If symbol is a choice value and equals to the 7027cf3d73bSSam Ravnborg * default for a choice - skip. 70384062dd3SSam Ravnborg * But only if value is bool and equal to "y" and 70484062dd3SSam Ravnborg * choice is not "optional". 70584062dd3SSam Ravnborg * (If choice is "optional" then all values can be "n") 7067cf3d73bSSam Ravnborg */ 7077cf3d73bSSam Ravnborg if (sym_is_choice_value(sym)) { 7087cf3d73bSSam Ravnborg struct symbol *cs; 7097cf3d73bSSam Ravnborg struct symbol *ds; 7107cf3d73bSSam Ravnborg 7117cf3d73bSSam Ravnborg cs = prop_get_symbol(sym_get_choice_prop(sym)); 7127cf3d73bSSam Ravnborg ds = sym_choice_default(cs); 71384062dd3SSam Ravnborg if (!sym_is_optional(cs) && sym == ds) { 714801690caSSam Ravnborg if ((sym->type == S_BOOLEAN) && 7157cf3d73bSSam Ravnborg sym_get_tristate_value(sym) == yes) 7167cf3d73bSSam Ravnborg goto next_menu; 7177cf3d73bSSam Ravnborg } 7187cf3d73bSSam Ravnborg } 719e54e692bSArnaud Lacombe conf_write_symbol(out, sym, &kconfig_printer_cb, NULL); 7207cf3d73bSSam Ravnborg } 7217cf3d73bSSam Ravnborg next_menu: 7227cf3d73bSSam Ravnborg if (menu->list != NULL) { 7237cf3d73bSSam Ravnborg menu = menu->list; 7247cf3d73bSSam Ravnborg } 7257cf3d73bSSam Ravnborg else if (menu->next != NULL) { 7267cf3d73bSSam Ravnborg menu = menu->next; 7277cf3d73bSSam Ravnborg } else { 7287cf3d73bSSam Ravnborg while ((menu = menu->parent)) { 7297cf3d73bSSam Ravnborg if (menu->next != NULL) { 7307cf3d73bSSam Ravnborg menu = menu->next; 7317cf3d73bSSam Ravnborg break; 7327cf3d73bSSam Ravnborg } 7337cf3d73bSSam Ravnborg } 7347cf3d73bSSam Ravnborg } 7357cf3d73bSSam Ravnborg } 7367cf3d73bSSam Ravnborg fclose(out); 7377cf3d73bSSam Ravnborg return 0; 7387cf3d73bSSam Ravnborg } 7397cf3d73bSSam Ravnborg 7401da177e4SLinus Torvalds int conf_write(const char *name) 7411da177e4SLinus Torvalds { 742c955ccafSRoman Zippel FILE *out; 7431da177e4SLinus Torvalds struct symbol *sym; 7441da177e4SLinus Torvalds struct menu *menu; 7451da177e4SLinus Torvalds const char *basename; 7461da177e4SLinus Torvalds const char *str; 7471408b15bSWill Newton char dirname[PATH_MAX+1], tmpname[PATH_MAX+1], newname[PATH_MAX+1]; 7481da177e4SLinus Torvalds char *env; 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds dirname[0] = 0; 7511da177e4SLinus Torvalds if (name && name[0]) { 7521da177e4SLinus Torvalds struct stat st; 7531da177e4SLinus Torvalds char *slash; 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds if (!stat(name, &st) && S_ISDIR(st.st_mode)) { 7561da177e4SLinus Torvalds strcpy(dirname, name); 7571da177e4SLinus Torvalds strcat(dirname, "/"); 75814cdd3c4SRoman Zippel basename = conf_get_configname(); 7591da177e4SLinus Torvalds } else if ((slash = strrchr(name, '/'))) { 7601da177e4SLinus Torvalds int size = slash - name + 1; 7611da177e4SLinus Torvalds memcpy(dirname, name, size); 7621da177e4SLinus Torvalds dirname[size] = 0; 7631da177e4SLinus Torvalds if (slash[1]) 7641da177e4SLinus Torvalds basename = slash + 1; 7651da177e4SLinus Torvalds else 76614cdd3c4SRoman Zippel basename = conf_get_configname(); 7671da177e4SLinus Torvalds } else 7681da177e4SLinus Torvalds basename = name; 7691da177e4SLinus Torvalds } else 77014cdd3c4SRoman Zippel basename = conf_get_configname(); 7711da177e4SLinus Torvalds 77214cdd3c4SRoman Zippel sprintf(newname, "%s%s", dirname, basename); 77314cdd3c4SRoman Zippel env = getenv("KCONFIG_OVERWRITECONFIG"); 77414cdd3c4SRoman Zippel if (!env || !*env) { 77514cdd3c4SRoman Zippel sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid()); 77614cdd3c4SRoman Zippel out = fopen(tmpname, "w"); 77714cdd3c4SRoman Zippel } else { 77814cdd3c4SRoman Zippel *tmpname = 0; 7791da177e4SLinus Torvalds out = fopen(newname, "w"); 78014cdd3c4SRoman Zippel } 7811da177e4SLinus Torvalds if (!out) 7821da177e4SLinus Torvalds return 1; 78314cdd3c4SRoman Zippel 784e54e692bSArnaud Lacombe conf_write_heading(out, &kconfig_printer_cb, NULL); 7851da177e4SLinus Torvalds 786b3214293SKarsten Wiese if (!conf_get_changed()) 7871da177e4SLinus Torvalds sym_clear_all_valid(); 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds menu = rootmenu.list; 7901da177e4SLinus Torvalds while (menu) { 7911da177e4SLinus Torvalds sym = menu->sym; 7921da177e4SLinus Torvalds if (!sym) { 7931da177e4SLinus Torvalds if (!menu_is_visible(menu)) 7941da177e4SLinus Torvalds goto next; 7951da177e4SLinus Torvalds str = menu_get_prompt(menu); 7961da177e4SLinus Torvalds fprintf(out, "\n" 7971da177e4SLinus Torvalds "#\n" 7981da177e4SLinus Torvalds "# %s\n" 7991da177e4SLinus Torvalds "#\n", str); 8001da177e4SLinus Torvalds } else if (!(sym->flags & SYMBOL_CHOICE)) { 8011da177e4SLinus Torvalds sym_calc_value(sym); 8021da177e4SLinus Torvalds if (!(sym->flags & SYMBOL_WRITE)) 8031da177e4SLinus Torvalds goto next; 8041da177e4SLinus Torvalds sym->flags &= ~SYMBOL_WRITE; 805e54e692bSArnaud Lacombe 806e54e692bSArnaud Lacombe conf_write_symbol(out, sym, &kconfig_printer_cb, NULL); 8071da177e4SLinus Torvalds } 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds next: 8101da177e4SLinus Torvalds if (menu->list) { 8111da177e4SLinus Torvalds menu = menu->list; 8121da177e4SLinus Torvalds continue; 8131da177e4SLinus Torvalds } 8141da177e4SLinus Torvalds if (menu->next) 8151da177e4SLinus Torvalds menu = menu->next; 8161da177e4SLinus Torvalds else while ((menu = menu->parent)) { 8171da177e4SLinus Torvalds if (menu->next) { 8181da177e4SLinus Torvalds menu = menu->next; 8191da177e4SLinus Torvalds break; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds } 8231da177e4SLinus Torvalds fclose(out); 82414cdd3c4SRoman Zippel 82514cdd3c4SRoman Zippel if (*tmpname) { 8269a3d0fe8SSam Ravnborg strcat(dirname, basename); 82714cdd3c4SRoman Zippel strcat(dirname, ".old"); 82814cdd3c4SRoman Zippel rename(newname, dirname); 82914cdd3c4SRoman Zippel if (rename(tmpname, newname)) 8301da177e4SLinus Torvalds return 1; 83114cdd3c4SRoman Zippel } 8321da177e4SLinus Torvalds 83342368c37SMichal Marek conf_message(_("configuration written to %s"), newname); 834ddc97cacSRoman Zippel 835bfc10001SKarsten Wiese sym_set_change_count(0); 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds return 0; 8381da177e4SLinus Torvalds } 839c955ccafSRoman Zippel 8404356f489STrevor Keith static int conf_split_config(void) 8412e3646e5SRoman Zippel { 84212122f62SMarkus Heidelberg const char *name; 8431408b15bSWill Newton char path[PATH_MAX+1]; 8442e3646e5SRoman Zippel char *s, *d, c; 8452e3646e5SRoman Zippel struct symbol *sym; 8462e3646e5SRoman Zippel struct stat sb; 8472e3646e5SRoman Zippel int res, i, fd; 8482e3646e5SRoman Zippel 84912122f62SMarkus Heidelberg name = conf_get_autoconfig_name(); 8502e3646e5SRoman Zippel conf_read_simple(name, S_DEF_AUTO); 8512e3646e5SRoman Zippel 8522e3646e5SRoman Zippel if (chdir("include/config")) 8532e3646e5SRoman Zippel return 1; 8542e3646e5SRoman Zippel 8552e3646e5SRoman Zippel res = 0; 8562e3646e5SRoman Zippel for_all_symbols(i, sym) { 8572e3646e5SRoman Zippel sym_calc_value(sym); 8582e3646e5SRoman Zippel if ((sym->flags & SYMBOL_AUTO) || !sym->name) 8592e3646e5SRoman Zippel continue; 8602e3646e5SRoman Zippel if (sym->flags & SYMBOL_WRITE) { 8612e3646e5SRoman Zippel if (sym->flags & SYMBOL_DEF_AUTO) { 8622e3646e5SRoman Zippel /* 8632e3646e5SRoman Zippel * symbol has old and new value, 8642e3646e5SRoman Zippel * so compare them... 8652e3646e5SRoman Zippel */ 8662e3646e5SRoman Zippel switch (sym->type) { 8672e3646e5SRoman Zippel case S_BOOLEAN: 8682e3646e5SRoman Zippel case S_TRISTATE: 8692e3646e5SRoman Zippel if (sym_get_tristate_value(sym) == 8702e3646e5SRoman Zippel sym->def[S_DEF_AUTO].tri) 8712e3646e5SRoman Zippel continue; 8722e3646e5SRoman Zippel break; 8732e3646e5SRoman Zippel case S_STRING: 8742e3646e5SRoman Zippel case S_HEX: 8752e3646e5SRoman Zippel case S_INT: 8762e3646e5SRoman Zippel if (!strcmp(sym_get_string_value(sym), 8772e3646e5SRoman Zippel sym->def[S_DEF_AUTO].val)) 8782e3646e5SRoman Zippel continue; 8792e3646e5SRoman Zippel break; 8802e3646e5SRoman Zippel default: 8812e3646e5SRoman Zippel break; 8822e3646e5SRoman Zippel } 8832e3646e5SRoman Zippel } else { 8842e3646e5SRoman Zippel /* 8852e3646e5SRoman Zippel * If there is no old value, only 'no' (unset) 8862e3646e5SRoman Zippel * is allowed as new value. 8872e3646e5SRoman Zippel */ 8882e3646e5SRoman Zippel switch (sym->type) { 8892e3646e5SRoman Zippel case S_BOOLEAN: 8902e3646e5SRoman Zippel case S_TRISTATE: 8912e3646e5SRoman Zippel if (sym_get_tristate_value(sym) == no) 8922e3646e5SRoman Zippel continue; 8932e3646e5SRoman Zippel break; 8942e3646e5SRoman Zippel default: 8952e3646e5SRoman Zippel break; 8962e3646e5SRoman Zippel } 8972e3646e5SRoman Zippel } 8982e3646e5SRoman Zippel } else if (!(sym->flags & SYMBOL_DEF_AUTO)) 8992e3646e5SRoman Zippel /* There is neither an old nor a new value. */ 9002e3646e5SRoman Zippel continue; 9012e3646e5SRoman Zippel /* else 9022e3646e5SRoman Zippel * There is an old value, but no new value ('no' (unset) 9032e3646e5SRoman Zippel * isn't saved in auto.conf, so the old value is always 9042e3646e5SRoman Zippel * different from 'no'). 9052e3646e5SRoman Zippel */ 9062e3646e5SRoman Zippel 9072e3646e5SRoman Zippel /* Replace all '_' and append ".h" */ 9082e3646e5SRoman Zippel s = sym->name; 9092e3646e5SRoman Zippel d = path; 9102e3646e5SRoman Zippel while ((c = *s++)) { 9112e3646e5SRoman Zippel c = tolower(c); 9122e3646e5SRoman Zippel *d++ = (c == '_') ? '/' : c; 9132e3646e5SRoman Zippel } 9142e3646e5SRoman Zippel strcpy(d, ".h"); 9152e3646e5SRoman Zippel 9162e3646e5SRoman Zippel /* Assume directory path already exists. */ 9172e3646e5SRoman Zippel fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); 9182e3646e5SRoman Zippel if (fd == -1) { 9192e3646e5SRoman Zippel if (errno != ENOENT) { 9202e3646e5SRoman Zippel res = 1; 9212e3646e5SRoman Zippel break; 9222e3646e5SRoman Zippel } 9232e3646e5SRoman Zippel /* 9242e3646e5SRoman Zippel * Create directory components, 9252e3646e5SRoman Zippel * unless they exist already. 9262e3646e5SRoman Zippel */ 9272e3646e5SRoman Zippel d = path; 9282e3646e5SRoman Zippel while ((d = strchr(d, '/'))) { 9292e3646e5SRoman Zippel *d = 0; 9302e3646e5SRoman Zippel if (stat(path, &sb) && mkdir(path, 0755)) { 9312e3646e5SRoman Zippel res = 1; 9322e3646e5SRoman Zippel goto out; 9332e3646e5SRoman Zippel } 9342e3646e5SRoman Zippel *d++ = '/'; 9352e3646e5SRoman Zippel } 9362e3646e5SRoman Zippel /* Try it again. */ 9372e3646e5SRoman Zippel fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); 9382e3646e5SRoman Zippel if (fd == -1) { 9392e3646e5SRoman Zippel res = 1; 9402e3646e5SRoman Zippel break; 9412e3646e5SRoman Zippel } 9422e3646e5SRoman Zippel } 9432e3646e5SRoman Zippel close(fd); 9442e3646e5SRoman Zippel } 9452e3646e5SRoman Zippel out: 9462e3646e5SRoman Zippel if (chdir("../..")) 9472e3646e5SRoman Zippel return 1; 9482e3646e5SRoman Zippel 9492e3646e5SRoman Zippel return res; 9502e3646e5SRoman Zippel } 9512e3646e5SRoman Zippel 952c955ccafSRoman Zippel int conf_write_autoconf(void) 953c955ccafSRoman Zippel { 954c955ccafSRoman Zippel struct symbol *sym; 95512122f62SMarkus Heidelberg const char *name; 956bc081dd6SMichal Marek FILE *out, *tristate, *out_h; 95749192f26SSam Ravnborg int i; 958c955ccafSRoman Zippel 9592e3646e5SRoman Zippel sym_clear_all_valid(); 9602e3646e5SRoman Zippel 961c955ccafSRoman Zippel file_write_dep("include/config/auto.conf.cmd"); 962c955ccafSRoman Zippel 9632e3646e5SRoman Zippel if (conf_split_config()) 9642e3646e5SRoman Zippel return 1; 9652e3646e5SRoman Zippel 966c955ccafSRoman Zippel out = fopen(".tmpconfig", "w"); 967c955ccafSRoman Zippel if (!out) 968c955ccafSRoman Zippel return 1; 969c955ccafSRoman Zippel 970bc081dd6SMichal Marek tristate = fopen(".tmpconfig_tristate", "w"); 971bc081dd6SMichal Marek if (!tristate) { 972bc081dd6SMichal Marek fclose(out); 973bc081dd6SMichal Marek return 1; 974bc081dd6SMichal Marek } 975bc081dd6SMichal Marek 976c955ccafSRoman Zippel out_h = fopen(".tmpconfig.h", "w"); 977c955ccafSRoman Zippel if (!out_h) { 978c955ccafSRoman Zippel fclose(out); 979bc081dd6SMichal Marek fclose(tristate); 980c955ccafSRoman Zippel return 1; 981c955ccafSRoman Zippel } 982c955ccafSRoman Zippel 983e54e692bSArnaud Lacombe conf_write_heading(out, &kconfig_printer_cb, NULL); 984e54e692bSArnaud Lacombe 985e54e692bSArnaud Lacombe conf_write_heading(tristate, &tristate_printer_cb, NULL); 986e54e692bSArnaud Lacombe 987e54e692bSArnaud Lacombe conf_write_heading(out_h, &header_printer_cb, NULL); 988c955ccafSRoman Zippel 989c955ccafSRoman Zippel for_all_symbols(i, sym) { 990c955ccafSRoman Zippel sym_calc_value(sym); 991c955ccafSRoman Zippel if (!(sym->flags & SYMBOL_WRITE) || !sym->name) 992c955ccafSRoman Zippel continue; 99349192f26SSam Ravnborg 994e54e692bSArnaud Lacombe /* write symbol to auto.conf, tristate and header files */ 995e54e692bSArnaud Lacombe conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1); 99649192f26SSam Ravnborg 997e54e692bSArnaud Lacombe conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1); 998e54e692bSArnaud Lacombe 999e54e692bSArnaud Lacombe conf_write_symbol(out_h, sym, &header_printer_cb, NULL); 1000e54e692bSArnaud Lacombe conf_write_symbol(out_h, sym, &header_function_printer_cb, NULL); 1001c955ccafSRoman Zippel } 1002c955ccafSRoman Zippel fclose(out); 1003bc081dd6SMichal Marek fclose(tristate); 1004c955ccafSRoman Zippel fclose(out_h); 1005c955ccafSRoman Zippel 1006c955ccafSRoman Zippel name = getenv("KCONFIG_AUTOHEADER"); 1007c955ccafSRoman Zippel if (!name) 1008264a2683SSam Ravnborg name = "include/generated/autoconf.h"; 1009c955ccafSRoman Zippel if (rename(".tmpconfig.h", name)) 1010c955ccafSRoman Zippel return 1; 1011bc081dd6SMichal Marek name = getenv("KCONFIG_TRISTATE"); 1012bc081dd6SMichal Marek if (!name) 1013bc081dd6SMichal Marek name = "include/config/tristate.conf"; 1014bc081dd6SMichal Marek if (rename(".tmpconfig_tristate", name)) 1015bc081dd6SMichal Marek return 1; 101612122f62SMarkus Heidelberg name = conf_get_autoconfig_name(); 1017c955ccafSRoman Zippel /* 1018c955ccafSRoman Zippel * This must be the last step, kbuild has a dependency on auto.conf 1019c955ccafSRoman Zippel * and this marks the successful completion of the previous steps. 1020c955ccafSRoman Zippel */ 1021c955ccafSRoman Zippel if (rename(".tmpconfig", name)) 1022c955ccafSRoman Zippel return 1; 1023c955ccafSRoman Zippel 1024c955ccafSRoman Zippel return 0; 1025c955ccafSRoman Zippel } 1026b3214293SKarsten Wiese 1027bfc10001SKarsten Wiese static int sym_change_count; 10283b354c55SKarsten Wiese static void (*conf_changed_callback)(void); 1029bfc10001SKarsten Wiese 1030bfc10001SKarsten Wiese void sym_set_change_count(int count) 1031bfc10001SKarsten Wiese { 10323b354c55SKarsten Wiese int _sym_change_count = sym_change_count; 1033bfc10001SKarsten Wiese sym_change_count = count; 10343b354c55SKarsten Wiese if (conf_changed_callback && 10353b354c55SKarsten Wiese (bool)_sym_change_count != (bool)count) 10363b354c55SKarsten Wiese conf_changed_callback(); 1037bfc10001SKarsten Wiese } 1038bfc10001SKarsten Wiese 1039bfc10001SKarsten Wiese void sym_add_change_count(int count) 1040bfc10001SKarsten Wiese { 10413b354c55SKarsten Wiese sym_set_change_count(count + sym_change_count); 1042bfc10001SKarsten Wiese } 1043bfc10001SKarsten Wiese 1044b3214293SKarsten Wiese bool conf_get_changed(void) 1045b3214293SKarsten Wiese { 1046b3214293SKarsten Wiese return sym_change_count; 1047b3214293SKarsten Wiese } 10483b354c55SKarsten Wiese 10493b354c55SKarsten Wiese void conf_set_changed_callback(void (*fn)(void)) 10503b354c55SKarsten Wiese { 10513b354c55SKarsten Wiese conf_changed_callback = fn; 10523b354c55SKarsten Wiese } 1053dc7862e5SRoman Zippel 1054a64b44eaSSam Ravnborg static void randomize_choice_values(struct symbol *csym) 1055a64b44eaSSam Ravnborg { 1056a64b44eaSSam Ravnborg struct property *prop; 1057a64b44eaSSam Ravnborg struct symbol *sym; 1058a64b44eaSSam Ravnborg struct expr *e; 1059a64b44eaSSam Ravnborg int cnt, def; 1060a64b44eaSSam Ravnborg 1061a64b44eaSSam Ravnborg /* 1062579fb8e7SArnaud Lacombe * If choice is mod then we may have more items selected 1063a64b44eaSSam Ravnborg * and if no then no-one. 1064a64b44eaSSam Ravnborg * In both cases stop. 1065a64b44eaSSam Ravnborg */ 1066a64b44eaSSam Ravnborg if (csym->curr.tri != yes) 1067a64b44eaSSam Ravnborg return; 1068a64b44eaSSam Ravnborg 1069a64b44eaSSam Ravnborg prop = sym_get_choice_prop(csym); 1070a64b44eaSSam Ravnborg 1071a64b44eaSSam Ravnborg /* count entries in choice block */ 1072a64b44eaSSam Ravnborg cnt = 0; 1073a64b44eaSSam Ravnborg expr_list_for_each_sym(prop->expr, e, sym) 1074a64b44eaSSam Ravnborg cnt++; 1075a64b44eaSSam Ravnborg 1076a64b44eaSSam Ravnborg /* 1077a64b44eaSSam Ravnborg * find a random value and set it to yes, 1078a64b44eaSSam Ravnborg * set the rest to no so we have only one set 1079a64b44eaSSam Ravnborg */ 1080a64b44eaSSam Ravnborg def = (rand() % cnt); 1081a64b44eaSSam Ravnborg 1082a64b44eaSSam Ravnborg cnt = 0; 1083a64b44eaSSam Ravnborg expr_list_for_each_sym(prop->expr, e, sym) { 1084a64b44eaSSam Ravnborg if (def == cnt++) { 1085a64b44eaSSam Ravnborg sym->def[S_DEF_USER].tri = yes; 1086a64b44eaSSam Ravnborg csym->def[S_DEF_USER].val = sym; 1087a64b44eaSSam Ravnborg } 1088a64b44eaSSam Ravnborg else { 1089a64b44eaSSam Ravnborg sym->def[S_DEF_USER].tri = no; 1090a64b44eaSSam Ravnborg } 1091a64b44eaSSam Ravnborg } 1092a64b44eaSSam Ravnborg csym->flags |= SYMBOL_DEF_USER; 1093a64b44eaSSam Ravnborg /* clear VALID to get value calculated */ 1094a64b44eaSSam Ravnborg csym->flags &= ~(SYMBOL_VALID); 1095a64b44eaSSam Ravnborg } 1096a64b44eaSSam Ravnborg 1097a64b44eaSSam Ravnborg static void set_all_choice_values(struct symbol *csym) 1098a64b44eaSSam Ravnborg { 1099a64b44eaSSam Ravnborg struct property *prop; 1100a64b44eaSSam Ravnborg struct symbol *sym; 1101a64b44eaSSam Ravnborg struct expr *e; 1102a64b44eaSSam Ravnborg 1103a64b44eaSSam Ravnborg prop = sym_get_choice_prop(csym); 1104a64b44eaSSam Ravnborg 1105a64b44eaSSam Ravnborg /* 1106a64b44eaSSam Ravnborg * Set all non-assinged choice values to no 1107a64b44eaSSam Ravnborg */ 1108a64b44eaSSam Ravnborg expr_list_for_each_sym(prop->expr, e, sym) { 1109a64b44eaSSam Ravnborg if (!sym_has_value(sym)) 1110a64b44eaSSam Ravnborg sym->def[S_DEF_USER].tri = no; 1111a64b44eaSSam Ravnborg } 1112a64b44eaSSam Ravnborg csym->flags |= SYMBOL_DEF_USER; 1113a64b44eaSSam Ravnborg /* clear VALID to get value calculated */ 1114a64b44eaSSam Ravnborg csym->flags &= ~(SYMBOL_VALID); 1115a64b44eaSSam Ravnborg } 1116dc7862e5SRoman Zippel 1117dc7862e5SRoman Zippel void conf_set_all_new_symbols(enum conf_def_mode mode) 1118dc7862e5SRoman Zippel { 1119dc7862e5SRoman Zippel struct symbol *sym, *csym; 1120a64b44eaSSam Ravnborg int i, cnt; 1121dc7862e5SRoman Zippel 1122dc7862e5SRoman Zippel for_all_symbols(i, sym) { 1123dc7862e5SRoman Zippel if (sym_has_value(sym)) 1124dc7862e5SRoman Zippel continue; 1125dc7862e5SRoman Zippel switch (sym_get_type(sym)) { 1126dc7862e5SRoman Zippel case S_BOOLEAN: 1127dc7862e5SRoman Zippel case S_TRISTATE: 1128dc7862e5SRoman Zippel switch (mode) { 1129dc7862e5SRoman Zippel case def_yes: 1130dc7862e5SRoman Zippel sym->def[S_DEF_USER].tri = yes; 1131dc7862e5SRoman Zippel break; 1132dc7862e5SRoman Zippel case def_mod: 1133dc7862e5SRoman Zippel sym->def[S_DEF_USER].tri = mod; 1134dc7862e5SRoman Zippel break; 1135dc7862e5SRoman Zippel case def_no: 1136dc7862e5SRoman Zippel sym->def[S_DEF_USER].tri = no; 1137dc7862e5SRoman Zippel break; 1138dc7862e5SRoman Zippel case def_random: 11391244b41dSPeter Korsgaard cnt = sym_get_type(sym) == S_TRISTATE ? 3 : 2; 11401244b41dSPeter Korsgaard sym->def[S_DEF_USER].tri = (tristate)(rand() % cnt); 1141dc7862e5SRoman Zippel break; 1142dc7862e5SRoman Zippel default: 1143dc7862e5SRoman Zippel continue; 1144dc7862e5SRoman Zippel } 1145184832c9SSam Ravnborg if (!(sym_is_choice(sym) && mode == def_random)) 1146dc7862e5SRoman Zippel sym->flags |= SYMBOL_DEF_USER; 1147dc7862e5SRoman Zippel break; 1148dc7862e5SRoman Zippel default: 1149dc7862e5SRoman Zippel break; 1150dc7862e5SRoman Zippel } 1151dc7862e5SRoman Zippel 1152dc7862e5SRoman Zippel } 1153dc7862e5SRoman Zippel 1154ce97e13eSAl Viro sym_clear_all_valid(); 1155dc7862e5SRoman Zippel 1156184832c9SSam Ravnborg /* 1157184832c9SSam Ravnborg * We have different type of choice blocks. 1158579fb8e7SArnaud Lacombe * If curr.tri equals to mod then we can select several 1159184832c9SSam Ravnborg * choice symbols in one block. 1160184832c9SSam Ravnborg * In this case we do nothing. 1161579fb8e7SArnaud Lacombe * If curr.tri equals yes then only one symbol can be 1162184832c9SSam Ravnborg * selected in a choice block and we set it to yes, 1163184832c9SSam Ravnborg * and the rest to no. 1164184832c9SSam Ravnborg */ 1165dc7862e5SRoman Zippel for_all_symbols(i, csym) { 1166dc7862e5SRoman Zippel if (sym_has_value(csym) || !sym_is_choice(csym)) 1167dc7862e5SRoman Zippel continue; 1168dc7862e5SRoman Zippel 1169dc7862e5SRoman Zippel sym_calc_value(csym); 1170a64b44eaSSam Ravnborg if (mode == def_random) 1171a64b44eaSSam Ravnborg randomize_choice_values(csym); 1172a64b44eaSSam Ravnborg else 1173a64b44eaSSam Ravnborg set_all_choice_values(csym); 1174dc7862e5SRoman Zippel } 1175dc7862e5SRoman Zippel } 1176