blob: c553f1f30c36891b1fb8385c4e497a24094b834e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Lacombe94bedec2010-08-17 01:40:20 -04008#include <errno.h>
Roman Zippel2e3646e2006-06-08 22:12:42 -07009#include <fcntl.h>
Arnaud Lacombe10a4b272011-06-01 16:00:46 -040010#include <stdarg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <time.h>
15#include <unistd.h>
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "lkc.h"
18
Michal Marekad8d40c2015-02-24 16:37:13 +010019struct conf_printer {
20 void (*print_symbol)(FILE *, struct symbol *, const char *, void *);
21 void (*print_comment)(FILE *, const char *, void *);
22};
23
Roman Zippelc1a0f5e2005-11-08 21:34:54 -080024static void conf_warning(const char *fmt, ...)
25 __attribute__ ((format (printf, 1, 2)));
26
Michal Marek42368c32010-08-17 10:21:19 +020027static void conf_message(const char *fmt, ...)
28 __attribute__ ((format (printf, 1, 2)));
29
Roman Zippelc1a0f5e2005-11-08 21:34:54 -080030static const char *conf_filename;
Masahiro Yamada84dd95d2018-01-11 22:39:41 +090031static int conf_lineno, conf_warnings;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -080032
Masahiro Yamada104daea2018-05-28 18:21:40 +090033const char conf_defname[] = "arch/$(ARCH)/defconfig";
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Roman Zippelc1a0f5e2005-11-08 21:34:54 -080035static void conf_warning(const char *fmt, ...)
36{
37 va_list ap;
38 va_start(ap, fmt);
39 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
40 vfprintf(stderr, fmt, ap);
41 fprintf(stderr, "\n");
42 va_end(ap);
43 conf_warnings++;
44}
45
Masahiro Yamada5accd7f2018-07-05 11:46:12 +090046static void conf_default_message_callback(const char *s)
Michal Marek42368c32010-08-17 10:21:19 +020047{
48 printf("#\n# ");
Masahiro Yamada5accd7f2018-07-05 11:46:12 +090049 printf("%s", s);
Michal Marek42368c32010-08-17 10:21:19 +020050 printf("\n#\n");
51}
52
Masahiro Yamada5accd7f2018-07-05 11:46:12 +090053static void (*conf_message_callback)(const char *s) =
Michal Marek42368c32010-08-17 10:21:19 +020054 conf_default_message_callback;
Masahiro Yamada5accd7f2018-07-05 11:46:12 +090055void conf_set_message_callback(void (*fn)(const char *s))
Michal Marek42368c32010-08-17 10:21:19 +020056{
57 conf_message_callback = fn;
58}
59
60static void conf_message(const char *fmt, ...)
61{
62 va_list ap;
Masahiro Yamada5accd7f2018-07-05 11:46:12 +090063 char buf[4096];
64
65 if (!conf_message_callback)
66 return;
Michal Marek42368c32010-08-17 10:21:19 +020067
68 va_start(ap, fmt);
Masahiro Yamada5accd7f2018-07-05 11:46:12 +090069
70 vsnprintf(buf, sizeof(buf), fmt, ap);
71 conf_message_callback(buf);
Colin Ian Kingb6a2ab22015-01-12 13:18:26 +000072 va_end(ap);
Michal Marek42368c32010-08-17 10:21:19 +020073}
74
Roman Zippel14cdd3c2006-06-08 22:12:51 -070075const char *conf_get_configname(void)
76{
77 char *name = getenv("KCONFIG_CONFIG");
78
79 return name ? name : ".config";
80}
81
Markus Heidelberg12122f62009-05-18 01:36:54 +020082const char *conf_get_autoconfig_name(void)
83{
84 char *name = getenv("KCONFIG_AUTOCONFIG");
85
86 return name ? name : "include/config/auto.conf";
87}
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089char *conf_get_default_confname(void)
90{
91 struct stat buf;
92 static char fullname[PATH_MAX+1];
93 char *env, *name;
94
Masahiro Yamada104daea2018-05-28 18:21:40 +090095 name = expand_string(conf_defname);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 env = getenv(SRCTREE);
97 if (env) {
98 sprintf(fullname, "%s/%s", env, name);
99 if (!stat(fullname, &buf))
100 return fullname;
101 }
102 return name;
103}
104
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100105static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
106{
107 char *p2;
108
109 switch (sym->type) {
110 case S_TRISTATE:
111 if (p[0] == 'm') {
112 sym->def[def].tri = mod;
113 sym->flags |= def_flags;
114 break;
115 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400116 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100117 case S_BOOLEAN:
118 if (p[0] == 'y') {
119 sym->def[def].tri = yes;
120 sym->flags |= def_flags;
121 break;
122 }
123 if (p[0] == 'n') {
124 sym->def[def].tri = no;
125 sym->flags |= def_flags;
126 break;
127 }
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200128 if (def != S_DEF_AUTO)
129 conf_warning("symbol value '%s' invalid for %s",
130 p, sym->name);
Arnaud Lacombe75f14682011-05-31 12:31:57 -0400131 return 1;
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100132 case S_OTHER:
133 if (*p != '"') {
134 for (p2 = p; *p2 && !isspace(*p2); p2++)
135 ;
136 sym->type = S_STRING;
137 goto done;
138 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400139 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100140 case S_STRING:
141 if (*p++ != '"')
142 break;
143 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
144 if (*p2 == '"') {
145 *p2 = 0;
146 break;
147 }
148 memmove(p2, p2 + 1, strlen(p2));
149 }
150 if (!p2) {
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200151 if (def != S_DEF_AUTO)
152 conf_warning("invalid string found");
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100153 return 1;
154 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400155 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100156 case S_INT:
157 case S_HEX:
158 done:
159 if (sym_string_valid(sym, p)) {
Masahiro Yamadacd81fc82018-02-17 03:38:31 +0900160 sym->def[def].val = xstrdup(p);
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100161 sym->flags |= def_flags;
162 } else {
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200163 if (def != S_DEF_AUTO)
164 conf_warning("symbol value '%s' invalid for %s",
165 p, sym->name);
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100166 return 1;
167 }
168 break;
169 default:
170 ;
171 }
172 return 0;
173}
174
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700175#define LINE_GROWTH 16
176static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
177{
178 char *nline;
179 size_t new_size = slen + 1;
180 if (new_size > *n) {
181 new_size += LINE_GROWTH - 1;
182 new_size *= 2;
Masahiro Yamadad717f242018-02-09 01:19:07 +0900183 nline = xrealloc(*lineptr, new_size);
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700184 if (!nline)
185 return -1;
186
187 *lineptr = nline;
188 *n = new_size;
189 }
190
191 (*lineptr)[slen] = c;
192
193 return 0;
194}
195
196static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
197{
198 char *line = *lineptr;
199 size_t slen = 0;
200
201 for (;;) {
202 int c = getc(stream);
203
204 switch (c) {
205 case '\n':
206 if (add_byte(c, &line, slen, n) < 0)
207 goto e_out;
208 slen++;
209 /* fall through */
210 case EOF:
211 if (add_byte('\0', &line, slen, n) < 0)
212 goto e_out;
213 *lineptr = line;
214 if (slen == 0)
215 return -1;
216 return slen;
217 default:
218 if (add_byte(c, &line, slen, n) < 0)
219 goto e_out;
220 slen++;
221 }
222 }
223
224e_out:
225 line[slen-1] = '\0';
226 *lineptr = line;
227 return -1;
228}
229
Roman Zippel669bfad92006-06-08 22:12:42 -0700230int conf_read_simple(const char *name, int def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 FILE *in = NULL;
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700233 char *line = NULL;
234 size_t line_asize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 char *p, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 struct symbol *sym;
Roman Zippel669bfad92006-06-08 22:12:42 -0700237 int i, def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 if (name) {
240 in = zconf_fopen(name);
241 } else {
Roman Zippelface4372006-06-08 22:12:45 -0700242 struct property *prop;
243
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700244 name = conf_get_configname();
Roman Zippelddc97ca2006-06-08 22:12:38 -0700245 in = zconf_fopen(name);
246 if (in)
247 goto load;
Karsten Wiesebfc10002006-12-13 00:34:07 -0800248 sym_add_change_count(1);
Al Viro6b87b702016-01-14 18:13:49 +0000249 if (!sym_defconfig_list)
Roman Zippelface4372006-06-08 22:12:45 -0700250 return 1;
251
252 for_all_defaults(sym_defconfig_list, prop) {
253 if (expr_calc_value(prop->visible.expr) == no ||
254 prop->expr->type != E_SYMBOL)
255 continue;
Masahiro Yamada104daea2018-05-28 18:21:40 +0900256 sym_calc_value(prop->expr->left.sym);
257 name = sym_get_string_value(prop->expr->left.sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 in = zconf_fopen(name);
259 if (in) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200260 conf_message("using defaults found in %s",
Michal Marek42368c32010-08-17 10:21:19 +0200261 name);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700262 goto load;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264 }
265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!in)
267 return 1;
268
Roman Zippelddc97ca2006-06-08 22:12:38 -0700269load:
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800270 conf_filename = name;
271 conf_lineno = 0;
272 conf_warnings = 0;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800273
Roman Zippel669bfad92006-06-08 22:12:42 -0700274 def_flags = SYMBOL_DEF << def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 for_all_symbols(i, sym) {
Roman Zippel669bfad92006-06-08 22:12:42 -0700276 sym->flags |= SYMBOL_CHANGED;
277 sym->flags &= ~(def_flags|SYMBOL_VALID);
Yann E. MORIN490f1612013-06-25 23:37:44 +0200278 if (sym_is_choice(sym))
279 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 switch (sym->type) {
281 case S_INT:
282 case S_HEX:
283 case S_STRING:
Roman Zippel669bfad92006-06-08 22:12:42 -0700284 if (sym->def[def].val)
285 free(sym->def[def].val);
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400286 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 default:
Roman Zippel669bfad92006-06-08 22:12:42 -0700288 sym->def[def].val = NULL;
289 sym->def[def].tri = no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291 }
292
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700293 while (compat_getline(&line, &line_asize, in) != -1) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800294 conf_lineno++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 sym = NULL;
Arnaud Lacombe8baefd32010-08-24 00:14:47 -0400296 if (line[0] == '#') {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400297 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 continue;
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400299 p = strchr(line + 2 + strlen(CONFIG_), ' ');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (!p)
301 continue;
302 *p++ = 0;
303 if (strncmp(p, "is not set", 10))
304 continue;
Roman Zippel669bfad92006-06-08 22:12:42 -0700305 if (def == S_DEF_USER) {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400306 sym = sym_find(line + 2 + strlen(CONFIG_));
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200307 if (!sym) {
308 sym_add_change_count(1);
Naohiro Aota8bea7542010-10-01 04:23:17 +0900309 goto setsym;
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200310 }
Roman Zippel669bfad92006-06-08 22:12:42 -0700311 } else {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400312 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
Roman Zippel669bfad92006-06-08 22:12:42 -0700313 if (sym->type == S_UNKNOWN)
314 sym->type = S_BOOLEAN;
315 }
316 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100317 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319 switch (sym->type) {
320 case S_BOOLEAN:
321 case S_TRISTATE:
Roman Zippel669bfad92006-06-08 22:12:42 -0700322 sym->def[def].tri = no;
323 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 break;
325 default:
326 ;
327 }
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400328 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
329 p = strchr(line + strlen(CONFIG_), '=');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (!p)
331 continue;
332 *p++ = 0;
333 p2 = strchr(p, '\n');
Matthew Wilcoxd3660a82006-07-13 12:54:07 -0600334 if (p2) {
335 *p2-- = 0;
336 if (*p2 == '\r')
337 *p2 = 0;
338 }
Roman Zippel669bfad92006-06-08 22:12:42 -0700339 if (def == S_DEF_USER) {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400340 sym = sym_find(line + strlen(CONFIG_));
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200341 if (!sym) {
342 sym_add_change_count(1);
Naohiro Aota8bea7542010-10-01 04:23:17 +0900343 goto setsym;
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200344 }
Roman Zippel669bfad92006-06-08 22:12:42 -0700345 } else {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400346 sym = sym_lookup(line + strlen(CONFIG_), 0);
Roman Zippel669bfad92006-06-08 22:12:42 -0700347 if (sym->type == S_UNKNOWN)
348 sym->type = S_OTHER;
349 }
350 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100351 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100353 if (conf_set_sym_val(sym, def, def_flags, p))
354 continue;
Arnaud Lacombe8baefd32010-08-24 00:14:47 -0400355 } else {
356 if (line[0] != '\r' && line[0] != '\n')
Paul Bollea4663912016-03-16 21:27:27 +0100357 conf_warning("unexpected data: %.*s",
358 (int)strcspn(line, "\r\n"), line);
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 continue;
361 }
Naohiro Aota8bea7542010-10-01 04:23:17 +0900362setsym:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (sym && sym_is_choice_value(sym)) {
364 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Roman Zippel669bfad92006-06-08 22:12:42 -0700365 switch (sym->def[def].tri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 case no:
367 break;
368 case mod:
Roman Zippel669bfad92006-06-08 22:12:42 -0700369 if (cs->def[def].tri == yes) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800370 conf_warning("%s creates inconsistent choice state", sym->name);
Yann E. MORIN490f1612013-06-25 23:37:44 +0200371 cs->flags &= ~def_flags;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 break;
374 case yes:
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100375 if (cs->def[def].tri != no)
376 conf_warning("override: %s changes choice state", sym->name);
377 cs->def[def].val = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 break;
379 }
Sam Ravnborgd6ee3572008-01-07 21:09:55 +0100380 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382 }
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700383 free(line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 fclose(in);
Roman Zippel90389162005-11-08 21:34:49 -0800385 return 0;
386}
387
388int conf_read(const char *name)
389{
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500390 struct symbol *sym;
Masahiro Yamada84dd95d2018-01-11 22:39:41 +0900391 int conf_unsaved = 0;
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500392 int i;
Roman Zippel90389162005-11-08 21:34:49 -0800393
Karsten Wiesebfc10002006-12-13 00:34:07 -0800394 sym_set_change_count(0);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700395
Al Viro6b87b702016-01-14 18:13:49 +0000396 if (conf_read_simple(name, S_DEF_USER)) {
397 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800398 return 1;
Al Viro6b87b702016-01-14 18:13:49 +0000399 }
400
401 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 for_all_symbols(i, sym) {
404 sym_calc_value(sym);
Dirk Gouders693359f2018-07-03 14:43:31 +0200405 if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500406 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800407 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
408 /* check that calculated value agrees with saved value */
409 switch (sym->type) {
410 case S_BOOLEAN:
411 case S_TRISTATE:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700412 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800413 break;
414 if (!sym_is_choice(sym))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500415 continue;
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400416 /* fall through */
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800417 default:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700418 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500419 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800420 break;
421 }
422 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
423 /* no previous value and not saved */
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500424 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800425 conf_unsaved++;
426 /* maybe print value in verbose mode... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
Roman Zippeld8982ba2007-07-09 11:43:58 -0700429 for_all_symbols(i, sym) {
430 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
431 /* Reset values of generates values, so they'll appear
432 * as new, if they should become visible, but that
433 * doesn't quite work if the Kconfig and the saved
434 * configuration disagree.
435 */
436 if (sym->visible == no && !conf_unsaved)
437 sym->flags &= ~SYMBOL_DEF_USER;
438 switch (sym->type) {
439 case S_STRING:
440 case S_INT:
441 case S_HEX:
442 /* Reset a string value if it's out of range */
443 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
444 break;
445 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
446 conf_unsaved++;
447 break;
448 default:
449 break;
450 }
451 }
452 }
453
Karsten Wiesebfc10002006-12-13 00:34:07 -0800454 sym_add_change_count(conf_warnings || conf_unsaved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 return 0;
457}
458
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400459/*
460 * Kconfig configuration printer
461 *
462 * This printer is used when generating the resulting configuration after
463 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
464 * passing a non-NULL argument to the printer.
465 *
466 */
467static void
468kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
Sam Ravnborg49192f22010-07-31 23:35:33 +0200469{
Sam Ravnborg49192f22010-07-31 23:35:33 +0200470
Arnaud Lacombe0dce6312010-12-05 01:33:16 -0500471 switch (sym->type) {
Sam Ravnborg49192f22010-07-31 23:35:33 +0200472 case S_BOOLEAN:
473 case S_TRISTATE:
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400474 if (*value == 'n') {
475 bool skip_unset = (arg != NULL);
476
477 if (!skip_unset)
478 fprintf(fp, "# %s%s is not set\n",
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400479 CONFIG_, sym->name);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400480 return;
Sam Ravnborg49192f22010-07-31 23:35:33 +0200481 }
482 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400483 default:
Sam Ravnborg49192f22010-07-31 23:35:33 +0200484 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400485 }
486
487 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
488}
489
490static void
491kconfig_print_comment(FILE *fp, const char *value, void *arg)
492{
493 const char *p = value;
494 size_t l;
495
496 for (;;) {
497 l = strcspn(p, "\n");
498 fprintf(fp, "#");
499 if (l) {
500 fprintf(fp, " ");
Peter Foley70cc01e2011-10-22 10:48:49 -0400501 xfwrite(p, l, 1, fp);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400502 p += l;
503 }
504 fprintf(fp, "\n");
505 if (*p++ == '\0')
506 break;
507 }
508}
509
510static struct conf_printer kconfig_printer_cb =
511{
512 .print_symbol = kconfig_print_symbol,
513 .print_comment = kconfig_print_comment,
514};
515
516/*
517 * Header printer
518 *
519 * This printer is used when generating the `include/generated/autoconf.h' file.
520 */
521static void
522header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
523{
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400524
525 switch (sym->type) {
526 case S_BOOLEAN:
Arnaud Lacombeeb4cf5a2011-07-14 15:31:07 -0400527 case S_TRISTATE: {
528 const char *suffix = "";
529
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400530 switch (*value) {
531 case 'n':
Michal Marek2a11c8e2011-07-20 17:38:57 +0200532 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400533 case 'm':
534 suffix = "_MODULE";
Arnaud Lacombeeb4cf5a2011-07-14 15:31:07 -0400535 /* fall through */
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400536 default:
Michal Marek2a11c8e2011-07-20 17:38:57 +0200537 fprintf(fp, "#define %s%s%s 1\n",
538 CONFIG_, sym->name, suffix);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400539 }
Arnaud Lacombeeb4cf5a2011-07-14 15:31:07 -0400540 break;
541 }
542 case S_HEX: {
543 const char *prefix = "";
544
545 if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
546 prefix = "0x";
547 fprintf(fp, "#define %s%s %s%s\n",
548 CONFIG_, sym->name, prefix, value);
549 break;
550 }
551 case S_STRING:
552 case S_INT:
553 fprintf(fp, "#define %s%s %s\n",
554 CONFIG_, sym->name, value);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200555 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400556 default:
557 break;
558 }
559
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400560}
561
562static void
563header_print_comment(FILE *fp, const char *value, void *arg)
564{
565 const char *p = value;
566 size_t l;
567
568 fprintf(fp, "/*\n");
569 for (;;) {
570 l = strcspn(p, "\n");
571 fprintf(fp, " *");
572 if (l) {
573 fprintf(fp, " ");
Peter Foley70cc01e2011-10-22 10:48:49 -0400574 xfwrite(p, l, 1, fp);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400575 p += l;
576 }
577 fprintf(fp, "\n");
578 if (*p++ == '\0')
579 break;
580 }
581 fprintf(fp, " */\n");
582}
583
584static struct conf_printer header_printer_cb =
585{
586 .print_symbol = header_print_symbol,
587 .print_comment = header_print_comment,
588};
589
590/*
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400591 * Tristate printer
592 *
593 * This printer is used when generating the `include/config/tristate.conf' file.
594 */
595static void
596tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
597{
598
599 if (sym->type == S_TRISTATE && *value != 'n')
600 fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
601}
602
603static struct conf_printer tristate_printer_cb =
604{
605 .print_symbol = tristate_print_symbol,
606 .print_comment = kconfig_print_comment,
607};
608
609static void conf_write_symbol(FILE *fp, struct symbol *sym,
610 struct conf_printer *printer, void *printer_arg)
611{
612 const char *str;
613
614 switch (sym->type) {
Sam Ravnborg49192f22010-07-31 23:35:33 +0200615 case S_OTHER:
616 case S_UNKNOWN:
617 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400618 case S_STRING:
619 str = sym_get_string_value(sym);
620 str = sym_escape_string_value(str);
621 printer->print_symbol(fp, sym, str, printer_arg);
622 free((void *)str);
623 break;
624 default:
625 str = sym_get_string_value(sym);
626 printer->print_symbol(fp, sym, str, printer_arg);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200627 }
628}
629
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400630static void
631conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
632{
633 char buf[256];
634
635 snprintf(buf, sizeof(buf),
636 "\n"
637 "Automatically generated file; DO NOT EDIT.\n"
638 "%s\n",
639 rootmenu.prompt->text);
640
641 printer->print_comment(fp, buf, printer_arg);
642}
643
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200644/*
645 * Write out a minimal config.
646 * All values that has default values are skipped as this is redundant.
647 */
648int conf_write_defconfig(const char *filename)
649{
650 struct symbol *sym;
651 struct menu *menu;
652 FILE *out;
653
654 out = fopen(filename, "w");
655 if (!out)
656 return 1;
657
658 sym_clear_all_valid();
659
660 /* Traverse all menus to find all relevant symbols */
661 menu = rootmenu.list;
662
663 while (menu != NULL)
664 {
665 sym = menu->sym;
666 if (sym == NULL) {
667 if (!menu_is_visible(menu))
668 goto next_menu;
669 } else if (!sym_is_choice(sym)) {
670 sym_calc_value(sym);
671 if (!(sym->flags & SYMBOL_WRITE))
672 goto next_menu;
673 sym->flags &= ~SYMBOL_WRITE;
674 /* If we cannot change the symbol - skip */
675 if (!sym_is_changable(sym))
676 goto next_menu;
677 /* If symbol equals to default value - skip */
678 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
679 goto next_menu;
680
681 /*
682 * If symbol is a choice value and equals to the
683 * default for a choice - skip.
Sam Ravnborg84062dd2010-08-14 23:22:16 +0200684 * But only if value is bool and equal to "y" and
685 * choice is not "optional".
686 * (If choice is "optional" then all values can be "n")
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200687 */
688 if (sym_is_choice_value(sym)) {
689 struct symbol *cs;
690 struct symbol *ds;
691
692 cs = prop_get_symbol(sym_get_choice_prop(sym));
693 ds = sym_choice_default(cs);
Sam Ravnborg84062dd2010-08-14 23:22:16 +0200694 if (!sym_is_optional(cs) && sym == ds) {
Sam Ravnborg801690c2010-08-12 09:11:51 +0200695 if ((sym->type == S_BOOLEAN) &&
696 sym_get_tristate_value(sym) == yes)
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200697 goto next_menu;
698 }
699 }
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400700 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200701 }
702next_menu:
703 if (menu->list != NULL) {
704 menu = menu->list;
705 }
706 else if (menu->next != NULL) {
707 menu = menu->next;
708 } else {
709 while ((menu = menu->parent)) {
710 if (menu->next != NULL) {
711 menu = menu->next;
712 break;
713 }
714 }
715 }
716 }
717 fclose(out);
718 return 0;
719}
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721int conf_write(const char *name)
722{
Roman Zippelc955cca2006-06-08 22:12:39 -0700723 FILE *out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 struct symbol *sym;
725 struct menu *menu;
726 const char *basename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 const char *str;
Nathan Chancellor2ae89c72018-06-02 09:02:09 -0700728 char dirname[PATH_MAX+1], tmpname[PATH_MAX+22], newname[PATH_MAX+8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 char *env;
730
731 dirname[0] = 0;
732 if (name && name[0]) {
733 struct stat st;
734 char *slash;
735
736 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
737 strcpy(dirname, name);
738 strcat(dirname, "/");
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700739 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 } else if ((slash = strrchr(name, '/'))) {
741 int size = slash - name + 1;
742 memcpy(dirname, name, size);
743 dirname[size] = 0;
744 if (slash[1])
745 basename = slash + 1;
746 else
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700747 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 } else
749 basename = name;
750 } else
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700751 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700753 sprintf(newname, "%s%s", dirname, basename);
754 env = getenv("KCONFIG_OVERWRITECONFIG");
755 if (!env || !*env) {
756 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
757 out = fopen(tmpname, "w");
758 } else {
759 *tmpname = 0;
760 out = fopen(newname, "w");
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (!out)
763 return 1;
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700764
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400765 conf_write_heading(out, &kconfig_printer_cb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Karsten Wieseb3214292006-12-13 00:34:06 -0800767 if (!conf_get_changed())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 sym_clear_all_valid();
769
770 menu = rootmenu.list;
771 while (menu) {
772 sym = menu->sym;
773 if (!sym) {
774 if (!menu_is_visible(menu))
775 goto next;
776 str = menu_get_prompt(menu);
777 fprintf(out, "\n"
778 "#\n"
779 "# %s\n"
780 "#\n", str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 } else if (!(sym->flags & SYMBOL_CHOICE)) {
782 sym_calc_value(sym);
783 if (!(sym->flags & SYMBOL_WRITE))
784 goto next;
785 sym->flags &= ~SYMBOL_WRITE;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400786
787 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
Sam Ravnborg49192f22010-07-31 23:35:33 +0200790next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (menu->list) {
792 menu = menu->list;
793 continue;
794 }
795 if (menu->next)
796 menu = menu->next;
797 else while ((menu = menu->parent)) {
798 if (menu->next) {
799 menu = menu->next;
800 break;
801 }
802 }
803 }
804 fclose(out);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700805
806 if (*tmpname) {
Sam Ravnborg9a3d0fe2006-10-01 11:48:53 +0200807 strcat(dirname, basename);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700808 strcat(dirname, ".old");
809 rename(newname, dirname);
810 if (rename(tmpname, newname))
811 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200814 conf_message("configuration written to %s", newname);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700815
Karsten Wiesebfc10002006-12-13 00:34:07 -0800816 sym_set_change_count(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 return 0;
819}
Roman Zippelc955cca2006-06-08 22:12:39 -0700820
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900821/* write a dependency file as used by kbuild to track dependencies */
822static int conf_write_dep(const char *name)
823{
824 struct file *file;
825 FILE *out;
826
827 if (!name)
828 name = ".kconfig.d";
829 out = fopen("..config.tmp", "w");
830 if (!out)
831 return 1;
832 fprintf(out, "deps_config := \\\n");
833 for (file = file_list; file; file = file->next) {
834 if (file->next)
835 fprintf(out, "\t%s \\\n", file->name);
836 else
837 fprintf(out, "\t%s\n", file->name);
838 }
839 fprintf(out, "\n%s: \\\n"
840 "\t$(deps_config)\n\n", conf_get_autoconfig_name());
841
842 env_write_dep(out, conf_get_autoconfig_name());
843
844 fprintf(out, "\n$(deps_config): ;\n");
845 fclose(out);
846 rename("..config.tmp", name);
847 return 0;
848}
849
Trevor Keith4356f482009-09-18 12:49:23 -0700850static int conf_split_config(void)
Roman Zippel2e3646e2006-06-08 22:12:42 -0700851{
Markus Heidelberg12122f62009-05-18 01:36:54 +0200852 const char *name;
Will Newton1408b152010-09-22 15:59:13 +0100853 char path[PATH_MAX+1];
Roman Zippel2e3646e2006-06-08 22:12:42 -0700854 char *s, *d, c;
855 struct symbol *sym;
856 struct stat sb;
857 int res, i, fd;
858
Markus Heidelberg12122f62009-05-18 01:36:54 +0200859 name = conf_get_autoconfig_name();
Roman Zippel2e3646e2006-06-08 22:12:42 -0700860 conf_read_simple(name, S_DEF_AUTO);
Al Viro6b87b702016-01-14 18:13:49 +0000861 sym_calc_value(modules_sym);
Roman Zippel2e3646e2006-06-08 22:12:42 -0700862
863 if (chdir("include/config"))
864 return 1;
865
866 res = 0;
867 for_all_symbols(i, sym) {
868 sym_calc_value(sym);
Dirk Gouders693359f2018-07-03 14:43:31 +0200869 if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
Roman Zippel2e3646e2006-06-08 22:12:42 -0700870 continue;
871 if (sym->flags & SYMBOL_WRITE) {
872 if (sym->flags & SYMBOL_DEF_AUTO) {
873 /*
874 * symbol has old and new value,
875 * so compare them...
876 */
877 switch (sym->type) {
878 case S_BOOLEAN:
879 case S_TRISTATE:
880 if (sym_get_tristate_value(sym) ==
881 sym->def[S_DEF_AUTO].tri)
882 continue;
883 break;
884 case S_STRING:
885 case S_HEX:
886 case S_INT:
887 if (!strcmp(sym_get_string_value(sym),
888 sym->def[S_DEF_AUTO].val))
889 continue;
890 break;
891 default:
892 break;
893 }
894 } else {
895 /*
896 * If there is no old value, only 'no' (unset)
897 * is allowed as new value.
898 */
899 switch (sym->type) {
900 case S_BOOLEAN:
901 case S_TRISTATE:
902 if (sym_get_tristate_value(sym) == no)
903 continue;
904 break;
905 default:
906 break;
907 }
908 }
909 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
910 /* There is neither an old nor a new value. */
911 continue;
912 /* else
913 * There is an old value, but no new value ('no' (unset)
914 * isn't saved in auto.conf, so the old value is always
915 * different from 'no').
916 */
917
918 /* Replace all '_' and append ".h" */
919 s = sym->name;
920 d = path;
921 while ((c = *s++)) {
922 c = tolower(c);
923 *d++ = (c == '_') ? '/' : c;
924 }
925 strcpy(d, ".h");
926
927 /* Assume directory path already exists. */
928 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
929 if (fd == -1) {
930 if (errno != ENOENT) {
931 res = 1;
932 break;
933 }
934 /*
935 * Create directory components,
936 * unless they exist already.
937 */
938 d = path;
939 while ((d = strchr(d, '/'))) {
940 *d = 0;
941 if (stat(path, &sb) && mkdir(path, 0755)) {
942 res = 1;
943 goto out;
944 }
945 *d++ = '/';
946 }
947 /* Try it again. */
948 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
949 if (fd == -1) {
950 res = 1;
951 break;
952 }
953 }
954 close(fd);
955 }
956out:
957 if (chdir("../.."))
958 return 1;
959
960 return res;
961}
962
Roman Zippelc955cca2006-06-08 22:12:39 -0700963int conf_write_autoconf(void)
964{
965 struct symbol *sym;
Markus Heidelberg12122f62009-05-18 01:36:54 +0200966 const char *name;
Michal Marekbc081dd2009-12-07 16:38:33 +0100967 FILE *out, *tristate, *out_h;
Sam Ravnborg49192f22010-07-31 23:35:33 +0200968 int i;
Roman Zippelc955cca2006-06-08 22:12:39 -0700969
Roman Zippel2e3646e2006-06-08 22:12:42 -0700970 sym_clear_all_valid();
971
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900972 conf_write_dep("include/config/auto.conf.cmd");
Roman Zippelc955cca2006-06-08 22:12:39 -0700973
Roman Zippel2e3646e2006-06-08 22:12:42 -0700974 if (conf_split_config())
975 return 1;
976
Roman Zippelc955cca2006-06-08 22:12:39 -0700977 out = fopen(".tmpconfig", "w");
978 if (!out)
979 return 1;
980
Michal Marekbc081dd2009-12-07 16:38:33 +0100981 tristate = fopen(".tmpconfig_tristate", "w");
982 if (!tristate) {
983 fclose(out);
984 return 1;
985 }
986
Roman Zippelc955cca2006-06-08 22:12:39 -0700987 out_h = fopen(".tmpconfig.h", "w");
988 if (!out_h) {
989 fclose(out);
Michal Marekbc081dd2009-12-07 16:38:33 +0100990 fclose(tristate);
Roman Zippelc955cca2006-06-08 22:12:39 -0700991 return 1;
992 }
993
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400994 conf_write_heading(out, &kconfig_printer_cb, NULL);
995
996 conf_write_heading(tristate, &tristate_printer_cb, NULL);
997
998 conf_write_heading(out_h, &header_printer_cb, NULL);
Roman Zippelc955cca2006-06-08 22:12:39 -0700999
Roman Zippelc955cca2006-06-08 22:12:39 -07001000 for_all_symbols(i, sym) {
Arnaud Lacombe953742c2011-08-16 01:20:20 -04001001 sym_calc_value(sym);
Paul Gortmakera9596132012-04-12 19:46:33 -04001002 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
Arnaud Lacombe953742c2011-08-16 01:20:20 -04001003 continue;
1004
Paul Gortmakera9596132012-04-12 19:46:33 -04001005 /* write symbol to auto.conf, tristate and header files */
Arnaud Lacombee54e6922011-05-15 23:42:09 -04001006 conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
Sam Ravnborg49192f22010-07-31 23:35:33 +02001007
Arnaud Lacombee54e6922011-05-15 23:42:09 -04001008 conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
1009
1010 conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
Roman Zippelc955cca2006-06-08 22:12:39 -07001011 }
1012 fclose(out);
Michal Marekbc081dd2009-12-07 16:38:33 +01001013 fclose(tristate);
Roman Zippelc955cca2006-06-08 22:12:39 -07001014 fclose(out_h);
1015
1016 name = getenv("KCONFIG_AUTOHEADER");
1017 if (!name)
Sam Ravnborg264a2682009-10-18 00:49:24 +02001018 name = "include/generated/autoconf.h";
Roman Zippelc955cca2006-06-08 22:12:39 -07001019 if (rename(".tmpconfig.h", name))
1020 return 1;
Michal Marekbc081dd2009-12-07 16:38:33 +01001021 name = getenv("KCONFIG_TRISTATE");
1022 if (!name)
1023 name = "include/config/tristate.conf";
1024 if (rename(".tmpconfig_tristate", name))
1025 return 1;
Markus Heidelberg12122f62009-05-18 01:36:54 +02001026 name = conf_get_autoconfig_name();
Roman Zippelc955cca2006-06-08 22:12:39 -07001027 /*
1028 * This must be the last step, kbuild has a dependency on auto.conf
1029 * and this marks the successful completion of the previous steps.
1030 */
1031 if (rename(".tmpconfig", name))
1032 return 1;
1033
1034 return 0;
1035}
Karsten Wieseb3214292006-12-13 00:34:06 -08001036
Karsten Wiesebfc10002006-12-13 00:34:07 -08001037static int sym_change_count;
Karsten Wiese3b354c52006-12-13 00:34:08 -08001038static void (*conf_changed_callback)(void);
Karsten Wiesebfc10002006-12-13 00:34:07 -08001039
1040void sym_set_change_count(int count)
1041{
Karsten Wiese3b354c52006-12-13 00:34:08 -08001042 int _sym_change_count = sym_change_count;
Karsten Wiesebfc10002006-12-13 00:34:07 -08001043 sym_change_count = count;
Karsten Wiese3b354c52006-12-13 00:34:08 -08001044 if (conf_changed_callback &&
1045 (bool)_sym_change_count != (bool)count)
1046 conf_changed_callback();
Karsten Wiesebfc10002006-12-13 00:34:07 -08001047}
1048
1049void sym_add_change_count(int count)
1050{
Karsten Wiese3b354c52006-12-13 00:34:08 -08001051 sym_set_change_count(count + sym_change_count);
Karsten Wiesebfc10002006-12-13 00:34:07 -08001052}
1053
Karsten Wieseb3214292006-12-13 00:34:06 -08001054bool conf_get_changed(void)
1055{
1056 return sym_change_count;
1057}
Karsten Wiese3b354c52006-12-13 00:34:08 -08001058
1059void conf_set_changed_callback(void (*fn)(void))
1060{
1061 conf_changed_callback = fn;
1062}
Roman Zippeldc7862e2008-05-06 04:55:55 +02001063
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001064static bool randomize_choice_values(struct symbol *csym)
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001065{
1066 struct property *prop;
1067 struct symbol *sym;
1068 struct expr *e;
1069 int cnt, def;
1070
1071 /*
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001072 * If choice is mod then we may have more items selected
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001073 * and if no then no-one.
1074 * In both cases stop.
1075 */
1076 if (csym->curr.tri != yes)
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001077 return false;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001078
1079 prop = sym_get_choice_prop(csym);
1080
1081 /* count entries in choice block */
1082 cnt = 0;
1083 expr_list_for_each_sym(prop->expr, e, sym)
1084 cnt++;
1085
1086 /*
1087 * find a random value and set it to yes,
1088 * set the rest to no so we have only one set
1089 */
1090 def = (rand() % cnt);
1091
1092 cnt = 0;
1093 expr_list_for_each_sym(prop->expr, e, sym) {
1094 if (def == cnt++) {
1095 sym->def[S_DEF_USER].tri = yes;
1096 csym->def[S_DEF_USER].val = sym;
1097 }
1098 else {
1099 sym->def[S_DEF_USER].tri = no;
1100 }
Yann E. MORINe6abf122013-04-28 17:33:15 +02001101 sym->flags |= SYMBOL_DEF_USER;
1102 /* clear VALID to get value calculated */
1103 sym->flags &= ~SYMBOL_VALID;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001104 }
1105 csym->flags |= SYMBOL_DEF_USER;
1106 /* clear VALID to get value calculated */
1107 csym->flags &= ~(SYMBOL_VALID);
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001108
1109 return true;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001110}
1111
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001112void set_all_choice_values(struct symbol *csym)
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001113{
1114 struct property *prop;
1115 struct symbol *sym;
1116 struct expr *e;
1117
1118 prop = sym_get_choice_prop(csym);
1119
1120 /*
1121 * Set all non-assinged choice values to no
1122 */
1123 expr_list_for_each_sym(prop->expr, e, sym) {
1124 if (!sym_has_value(sym))
1125 sym->def[S_DEF_USER].tri = no;
1126 }
1127 csym->flags |= SYMBOL_DEF_USER;
1128 /* clear VALID to get value calculated */
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001129 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001130}
Roman Zippeldc7862e2008-05-06 04:55:55 +02001131
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001132bool conf_set_all_new_symbols(enum conf_def_mode mode)
Roman Zippeldc7862e2008-05-06 04:55:55 +02001133{
1134 struct symbol *sym, *csym;
Masahiro Yamadab92d8042017-12-16 00:38:02 +09001135 int i, cnt, pby, pty, ptm; /* pby: probability of bool = y
Yann E. MORINe43956e2013-04-13 17:18:36 +02001136 * pty: probability of tristate = y
1137 * ptm: probability of tristate = m
1138 */
1139
1140 pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1141 * below, otherwise gcc whines about
1142 * -Wmaybe-uninitialized */
1143 if (mode == def_random) {
1144 int n, p[3];
1145 char *env = getenv("KCONFIG_PROBABILITY");
1146 n = 0;
1147 while( env && *env ) {
1148 char *endp;
1149 int tmp = strtol( env, &endp, 10 );
1150 if( tmp >= 0 && tmp <= 100 ) {
1151 p[n++] = tmp;
1152 } else {
1153 errno = ERANGE;
1154 perror( "KCONFIG_PROBABILITY" );
1155 exit( 1 );
1156 }
1157 env = (*endp == ':') ? endp+1 : endp;
1158 if( n >=3 ) {
1159 break;
1160 }
1161 }
1162 switch( n ) {
1163 case 1:
1164 pby = p[0]; ptm = pby/2; pty = pby-ptm;
1165 break;
1166 case 2:
1167 pty = p[0]; ptm = p[1]; pby = pty + ptm;
1168 break;
1169 case 3:
1170 pby = p[0]; pty = p[1]; ptm = p[2];
1171 break;
1172 }
1173
1174 if( pty+ptm > 100 ) {
1175 errno = ERANGE;
1176 perror( "KCONFIG_PROBABILITY" );
1177 exit( 1 );
1178 }
1179 }
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001180 bool has_changed = false;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001181
1182 for_all_symbols(i, sym) {
Yann E. MORINcfa98f22013-04-24 22:00:04 +02001183 if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
Roman Zippeldc7862e2008-05-06 04:55:55 +02001184 continue;
1185 switch (sym_get_type(sym)) {
1186 case S_BOOLEAN:
1187 case S_TRISTATE:
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001188 has_changed = true;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001189 switch (mode) {
1190 case def_yes:
1191 sym->def[S_DEF_USER].tri = yes;
1192 break;
1193 case def_mod:
1194 sym->def[S_DEF_USER].tri = mod;
1195 break;
1196 case def_no:
Josh Triplett5d2acfc2014-04-07 15:39:09 -07001197 if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
1198 sym->def[S_DEF_USER].tri = yes;
1199 else
1200 sym->def[S_DEF_USER].tri = no;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001201 break;
1202 case def_random:
Yann E. MORINe43956e2013-04-13 17:18:36 +02001203 sym->def[S_DEF_USER].tri = no;
1204 cnt = rand() % 100;
1205 if (sym->type == S_TRISTATE) {
1206 if (cnt < pty)
1207 sym->def[S_DEF_USER].tri = yes;
1208 else if (cnt < (pty+ptm))
1209 sym->def[S_DEF_USER].tri = mod;
1210 } else if (cnt < pby)
1211 sym->def[S_DEF_USER].tri = yes;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001212 break;
1213 default:
1214 continue;
1215 }
Sam Ravnborg184832c2009-03-15 11:05:12 +01001216 if (!(sym_is_choice(sym) && mode == def_random))
Roman Zippeldc7862e2008-05-06 04:55:55 +02001217 sym->flags |= SYMBOL_DEF_USER;
1218 break;
1219 default:
1220 break;
1221 }
1222
1223 }
1224
Al Viroce97e132008-10-26 05:12:34 +00001225 sym_clear_all_valid();
Roman Zippeldc7862e2008-05-06 04:55:55 +02001226
Sam Ravnborg184832c2009-03-15 11:05:12 +01001227 /*
1228 * We have different type of choice blocks.
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001229 * If curr.tri equals to mod then we can select several
Sam Ravnborg184832c2009-03-15 11:05:12 +01001230 * choice symbols in one block.
1231 * In this case we do nothing.
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001232 * If curr.tri equals yes then only one symbol can be
Sam Ravnborg184832c2009-03-15 11:05:12 +01001233 * selected in a choice block and we set it to yes,
1234 * and the rest to no.
1235 */
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001236 if (mode != def_random) {
1237 for_all_symbols(i, csym) {
1238 if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1239 sym_is_choice_value(csym))
1240 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1241 }
1242 }
1243
Roman Zippeldc7862e2008-05-06 04:55:55 +02001244 for_all_symbols(i, csym) {
1245 if (sym_has_value(csym) || !sym_is_choice(csym))
1246 continue;
1247
1248 sym_calc_value(csym);
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001249 if (mode == def_random)
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001250 has_changed = randomize_choice_values(csym);
1251 else {
1252 set_all_choice_values(csym);
1253 has_changed = true;
1254 }
Roman Zippeldc7862e2008-05-06 04:55:55 +02001255 }
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001256
1257 return has_changed;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001258}