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