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