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