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