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