blob: 629ad32d4708d852b3d8b6cc0274b8b152c45f2c [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
Trevor Keith4356f482009-09-18 12:49:23 -0700821static int conf_split_config(void)
Roman Zippel2e3646e2006-06-08 22:12:42 -0700822{
Markus Heidelberg12122f62009-05-18 01:36:54 +0200823 const char *name;
Will Newton1408b152010-09-22 15:59:13 +0100824 char path[PATH_MAX+1];
Roman Zippel2e3646e2006-06-08 22:12:42 -0700825 char *s, *d, c;
826 struct symbol *sym;
827 struct stat sb;
828 int res, i, fd;
829
Markus Heidelberg12122f62009-05-18 01:36:54 +0200830 name = conf_get_autoconfig_name();
Roman Zippel2e3646e2006-06-08 22:12:42 -0700831 conf_read_simple(name, S_DEF_AUTO);
Al Viro6b87b702016-01-14 18:13:49 +0000832 sym_calc_value(modules_sym);
Roman Zippel2e3646e2006-06-08 22:12:42 -0700833
834 if (chdir("include/config"))
835 return 1;
836
837 res = 0;
838 for_all_symbols(i, sym) {
839 sym_calc_value(sym);
Dirk Gouders693359f2018-07-03 14:43:31 +0200840 if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
Roman Zippel2e3646e2006-06-08 22:12:42 -0700841 continue;
842 if (sym->flags & SYMBOL_WRITE) {
843 if (sym->flags & SYMBOL_DEF_AUTO) {
844 /*
845 * symbol has old and new value,
846 * so compare them...
847 */
848 switch (sym->type) {
849 case S_BOOLEAN:
850 case S_TRISTATE:
851 if (sym_get_tristate_value(sym) ==
852 sym->def[S_DEF_AUTO].tri)
853 continue;
854 break;
855 case S_STRING:
856 case S_HEX:
857 case S_INT:
858 if (!strcmp(sym_get_string_value(sym),
859 sym->def[S_DEF_AUTO].val))
860 continue;
861 break;
862 default:
863 break;
864 }
865 } else {
866 /*
867 * If there is no old value, only 'no' (unset)
868 * is allowed as new value.
869 */
870 switch (sym->type) {
871 case S_BOOLEAN:
872 case S_TRISTATE:
873 if (sym_get_tristate_value(sym) == no)
874 continue;
875 break;
876 default:
877 break;
878 }
879 }
880 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
881 /* There is neither an old nor a new value. */
882 continue;
883 /* else
884 * There is an old value, but no new value ('no' (unset)
885 * isn't saved in auto.conf, so the old value is always
886 * different from 'no').
887 */
888
889 /* Replace all '_' and append ".h" */
890 s = sym->name;
891 d = path;
892 while ((c = *s++)) {
893 c = tolower(c);
894 *d++ = (c == '_') ? '/' : c;
895 }
896 strcpy(d, ".h");
897
898 /* Assume directory path already exists. */
899 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
900 if (fd == -1) {
901 if (errno != ENOENT) {
902 res = 1;
903 break;
904 }
905 /*
906 * Create directory components,
907 * unless they exist already.
908 */
909 d = path;
910 while ((d = strchr(d, '/'))) {
911 *d = 0;
912 if (stat(path, &sb) && mkdir(path, 0755)) {
913 res = 1;
914 goto out;
915 }
916 *d++ = '/';
917 }
918 /* Try it again. */
919 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
920 if (fd == -1) {
921 res = 1;
922 break;
923 }
924 }
925 close(fd);
926 }
927out:
928 if (chdir("../.."))
929 return 1;
930
931 return res;
932}
933
Roman Zippelc955cca2006-06-08 22:12:39 -0700934int conf_write_autoconf(void)
935{
936 struct symbol *sym;
Markus Heidelberg12122f62009-05-18 01:36:54 +0200937 const char *name;
Michal Marekbc081dd2009-12-07 16:38:33 +0100938 FILE *out, *tristate, *out_h;
Sam Ravnborg49192f22010-07-31 23:35:33 +0200939 int i;
Roman Zippelc955cca2006-06-08 22:12:39 -0700940
Roman Zippel2e3646e2006-06-08 22:12:42 -0700941 sym_clear_all_valid();
942
Roman Zippelc955cca2006-06-08 22:12:39 -0700943 file_write_dep("include/config/auto.conf.cmd");
944
Roman Zippel2e3646e2006-06-08 22:12:42 -0700945 if (conf_split_config())
946 return 1;
947
Roman Zippelc955cca2006-06-08 22:12:39 -0700948 out = fopen(".tmpconfig", "w");
949 if (!out)
950 return 1;
951
Michal Marekbc081dd2009-12-07 16:38:33 +0100952 tristate = fopen(".tmpconfig_tristate", "w");
953 if (!tristate) {
954 fclose(out);
955 return 1;
956 }
957
Roman Zippelc955cca2006-06-08 22:12:39 -0700958 out_h = fopen(".tmpconfig.h", "w");
959 if (!out_h) {
960 fclose(out);
Michal Marekbc081dd2009-12-07 16:38:33 +0100961 fclose(tristate);
Roman Zippelc955cca2006-06-08 22:12:39 -0700962 return 1;
963 }
964
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400965 conf_write_heading(out, &kconfig_printer_cb, NULL);
966
967 conf_write_heading(tristate, &tristate_printer_cb, NULL);
968
969 conf_write_heading(out_h, &header_printer_cb, NULL);
Roman Zippelc955cca2006-06-08 22:12:39 -0700970
Roman Zippelc955cca2006-06-08 22:12:39 -0700971 for_all_symbols(i, sym) {
Arnaud Lacombe953742c2011-08-16 01:20:20 -0400972 sym_calc_value(sym);
Paul Gortmakera9596132012-04-12 19:46:33 -0400973 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
Arnaud Lacombe953742c2011-08-16 01:20:20 -0400974 continue;
975
Paul Gortmakera9596132012-04-12 19:46:33 -0400976 /* write symbol to auto.conf, tristate and header files */
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400977 conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200978
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400979 conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
980
981 conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
Roman Zippelc955cca2006-06-08 22:12:39 -0700982 }
983 fclose(out);
Michal Marekbc081dd2009-12-07 16:38:33 +0100984 fclose(tristate);
Roman Zippelc955cca2006-06-08 22:12:39 -0700985 fclose(out_h);
986
987 name = getenv("KCONFIG_AUTOHEADER");
988 if (!name)
Sam Ravnborg264a2682009-10-18 00:49:24 +0200989 name = "include/generated/autoconf.h";
Roman Zippelc955cca2006-06-08 22:12:39 -0700990 if (rename(".tmpconfig.h", name))
991 return 1;
Michal Marekbc081dd2009-12-07 16:38:33 +0100992 name = getenv("KCONFIG_TRISTATE");
993 if (!name)
994 name = "include/config/tristate.conf";
995 if (rename(".tmpconfig_tristate", name))
996 return 1;
Markus Heidelberg12122f62009-05-18 01:36:54 +0200997 name = conf_get_autoconfig_name();
Roman Zippelc955cca2006-06-08 22:12:39 -0700998 /*
999 * This must be the last step, kbuild has a dependency on auto.conf
1000 * and this marks the successful completion of the previous steps.
1001 */
1002 if (rename(".tmpconfig", name))
1003 return 1;
1004
1005 return 0;
1006}
Karsten Wieseb3214292006-12-13 00:34:06 -08001007
Karsten Wiesebfc10002006-12-13 00:34:07 -08001008static int sym_change_count;
Karsten Wiese3b354c52006-12-13 00:34:08 -08001009static void (*conf_changed_callback)(void);
Karsten Wiesebfc10002006-12-13 00:34:07 -08001010
1011void sym_set_change_count(int count)
1012{
Karsten Wiese3b354c52006-12-13 00:34:08 -08001013 int _sym_change_count = sym_change_count;
Karsten Wiesebfc10002006-12-13 00:34:07 -08001014 sym_change_count = count;
Karsten Wiese3b354c52006-12-13 00:34:08 -08001015 if (conf_changed_callback &&
1016 (bool)_sym_change_count != (bool)count)
1017 conf_changed_callback();
Karsten Wiesebfc10002006-12-13 00:34:07 -08001018}
1019
1020void sym_add_change_count(int count)
1021{
Karsten Wiese3b354c52006-12-13 00:34:08 -08001022 sym_set_change_count(count + sym_change_count);
Karsten Wiesebfc10002006-12-13 00:34:07 -08001023}
1024
Karsten Wieseb3214292006-12-13 00:34:06 -08001025bool conf_get_changed(void)
1026{
1027 return sym_change_count;
1028}
Karsten Wiese3b354c52006-12-13 00:34:08 -08001029
1030void conf_set_changed_callback(void (*fn)(void))
1031{
1032 conf_changed_callback = fn;
1033}
Roman Zippeldc7862e2008-05-06 04:55:55 +02001034
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001035static bool randomize_choice_values(struct symbol *csym)
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001036{
1037 struct property *prop;
1038 struct symbol *sym;
1039 struct expr *e;
1040 int cnt, def;
1041
1042 /*
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001043 * If choice is mod then we may have more items selected
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001044 * and if no then no-one.
1045 * In both cases stop.
1046 */
1047 if (csym->curr.tri != yes)
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001048 return false;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001049
1050 prop = sym_get_choice_prop(csym);
1051
1052 /* count entries in choice block */
1053 cnt = 0;
1054 expr_list_for_each_sym(prop->expr, e, sym)
1055 cnt++;
1056
1057 /*
1058 * find a random value and set it to yes,
1059 * set the rest to no so we have only one set
1060 */
1061 def = (rand() % cnt);
1062
1063 cnt = 0;
1064 expr_list_for_each_sym(prop->expr, e, sym) {
1065 if (def == cnt++) {
1066 sym->def[S_DEF_USER].tri = yes;
1067 csym->def[S_DEF_USER].val = sym;
1068 }
1069 else {
1070 sym->def[S_DEF_USER].tri = no;
1071 }
Yann E. MORINe6abf122013-04-28 17:33:15 +02001072 sym->flags |= SYMBOL_DEF_USER;
1073 /* clear VALID to get value calculated */
1074 sym->flags &= ~SYMBOL_VALID;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001075 }
1076 csym->flags |= SYMBOL_DEF_USER;
1077 /* clear VALID to get value calculated */
1078 csym->flags &= ~(SYMBOL_VALID);
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001079
1080 return true;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001081}
1082
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001083void set_all_choice_values(struct symbol *csym)
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001084{
1085 struct property *prop;
1086 struct symbol *sym;
1087 struct expr *e;
1088
1089 prop = sym_get_choice_prop(csym);
1090
1091 /*
1092 * Set all non-assinged choice values to no
1093 */
1094 expr_list_for_each_sym(prop->expr, e, sym) {
1095 if (!sym_has_value(sym))
1096 sym->def[S_DEF_USER].tri = no;
1097 }
1098 csym->flags |= SYMBOL_DEF_USER;
1099 /* clear VALID to get value calculated */
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001100 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001101}
Roman Zippeldc7862e2008-05-06 04:55:55 +02001102
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001103bool conf_set_all_new_symbols(enum conf_def_mode mode)
Roman Zippeldc7862e2008-05-06 04:55:55 +02001104{
1105 struct symbol *sym, *csym;
Masahiro Yamadab92d8042017-12-16 00:38:02 +09001106 int i, cnt, pby, pty, ptm; /* pby: probability of bool = y
Yann E. MORINe43956e2013-04-13 17:18:36 +02001107 * pty: probability of tristate = y
1108 * ptm: probability of tristate = m
1109 */
1110
1111 pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1112 * below, otherwise gcc whines about
1113 * -Wmaybe-uninitialized */
1114 if (mode == def_random) {
1115 int n, p[3];
1116 char *env = getenv("KCONFIG_PROBABILITY");
1117 n = 0;
1118 while( env && *env ) {
1119 char *endp;
1120 int tmp = strtol( env, &endp, 10 );
1121 if( tmp >= 0 && tmp <= 100 ) {
1122 p[n++] = tmp;
1123 } else {
1124 errno = ERANGE;
1125 perror( "KCONFIG_PROBABILITY" );
1126 exit( 1 );
1127 }
1128 env = (*endp == ':') ? endp+1 : endp;
1129 if( n >=3 ) {
1130 break;
1131 }
1132 }
1133 switch( n ) {
1134 case 1:
1135 pby = p[0]; ptm = pby/2; pty = pby-ptm;
1136 break;
1137 case 2:
1138 pty = p[0]; ptm = p[1]; pby = pty + ptm;
1139 break;
1140 case 3:
1141 pby = p[0]; pty = p[1]; ptm = p[2];
1142 break;
1143 }
1144
1145 if( pty+ptm > 100 ) {
1146 errno = ERANGE;
1147 perror( "KCONFIG_PROBABILITY" );
1148 exit( 1 );
1149 }
1150 }
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001151 bool has_changed = false;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001152
1153 for_all_symbols(i, sym) {
Yann E. MORINcfa98f22013-04-24 22:00:04 +02001154 if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
Roman Zippeldc7862e2008-05-06 04:55:55 +02001155 continue;
1156 switch (sym_get_type(sym)) {
1157 case S_BOOLEAN:
1158 case S_TRISTATE:
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001159 has_changed = true;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001160 switch (mode) {
1161 case def_yes:
1162 sym->def[S_DEF_USER].tri = yes;
1163 break;
1164 case def_mod:
1165 sym->def[S_DEF_USER].tri = mod;
1166 break;
1167 case def_no:
Josh Triplett5d2acfc2014-04-07 15:39:09 -07001168 if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
1169 sym->def[S_DEF_USER].tri = yes;
1170 else
1171 sym->def[S_DEF_USER].tri = no;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001172 break;
1173 case def_random:
Yann E. MORINe43956e2013-04-13 17:18:36 +02001174 sym->def[S_DEF_USER].tri = no;
1175 cnt = rand() % 100;
1176 if (sym->type == S_TRISTATE) {
1177 if (cnt < pty)
1178 sym->def[S_DEF_USER].tri = yes;
1179 else if (cnt < (pty+ptm))
1180 sym->def[S_DEF_USER].tri = mod;
1181 } else if (cnt < pby)
1182 sym->def[S_DEF_USER].tri = yes;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001183 break;
1184 default:
1185 continue;
1186 }
Sam Ravnborg184832c2009-03-15 11:05:12 +01001187 if (!(sym_is_choice(sym) && mode == def_random))
Roman Zippeldc7862e2008-05-06 04:55:55 +02001188 sym->flags |= SYMBOL_DEF_USER;
1189 break;
1190 default:
1191 break;
1192 }
1193
1194 }
1195
Al Viroce97e132008-10-26 05:12:34 +00001196 sym_clear_all_valid();
Roman Zippeldc7862e2008-05-06 04:55:55 +02001197
Sam Ravnborg184832c2009-03-15 11:05:12 +01001198 /*
1199 * We have different type of choice blocks.
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001200 * If curr.tri equals to mod then we can select several
Sam Ravnborg184832c2009-03-15 11:05:12 +01001201 * choice symbols in one block.
1202 * In this case we do nothing.
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001203 * If curr.tri equals yes then only one symbol can be
Sam Ravnborg184832c2009-03-15 11:05:12 +01001204 * selected in a choice block and we set it to yes,
1205 * and the rest to no.
1206 */
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001207 if (mode != def_random) {
1208 for_all_symbols(i, csym) {
1209 if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1210 sym_is_choice_value(csym))
1211 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1212 }
1213 }
1214
Roman Zippeldc7862e2008-05-06 04:55:55 +02001215 for_all_symbols(i, csym) {
1216 if (sym_has_value(csym) || !sym_is_choice(csym))
1217 continue;
1218
1219 sym_calc_value(csym);
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001220 if (mode == def_random)
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001221 has_changed = randomize_choice_values(csym);
1222 else {
1223 set_all_choice_values(csym);
1224 has_changed = true;
1225 }
Roman Zippeldc7862e2008-05-06 04:55:55 +02001226 }
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001227
1228 return has_changed;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001229}