blob: 10958f1e41d659a6d5f5f33f6a052e7476680fc2 [file] [log] [blame]
Masahiro Yamada0c874102018-12-18 21:13:35 +09001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
6#include <ctype.h>
Markus Mayer74dba802015-12-09 14:56:12 -08007#include <limits.h>
Randy Dunlap9dfb5632006-04-18 22:21:53 -07008#include <stdio.h>
Ladislav Michl75ff4302008-01-09 16:36:19 +01009#include <stdlib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <time.h>
Ladislav Michl75ff4302008-01-09 16:36:19 +010012#include <unistd.h>
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020013#include <getopt.h>
Ingo Molnarb0fe5512009-03-12 15:15:31 +010014#include <sys/time.h>
Yann E. MORIN0d8024c2013-04-13 22:49:13 +020015#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "lkc.h"
18
19static void conf(struct menu *menu);
20static void check_conf(struct menu *menu);
21
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020022enum input_mode {
23 oldaskconfig,
Masahiro Yamada911a91c2018-03-01 15:34:37 +090024 syncconfig,
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020025 oldconfig,
26 allnoconfig,
27 allyesconfig,
28 allmodconfig,
Sam Ravnborg0748cb32010-07-31 23:35:31 +020029 alldefconfig,
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020030 randconfig,
31 defconfig,
Sam Ravnborg7cf3d732010-07-31 23:35:34 +020032 savedefconfig,
Sam Ravnborg861b4ea2010-07-31 23:35:28 +020033 listnewconfig,
Laura Abbott5d8b42a2019-11-04 17:10:08 -050034 helpnewconfig,
Adam Leefb16d892012-09-01 01:05:17 +080035 olddefconfig,
Tetsuo Handa89b90602019-12-17 18:42:06 +090036 yes2modconfig,
37 mod2yesconfig,
Masahiro Yamada52e58a32018-01-11 22:39:39 +090038};
39static enum input_mode input_mode = oldaskconfig;
Masahiro Yamadaed562c52021-03-14 04:48:25 +090040static int input_mode_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static int indent = 1;
Ben Hutchings62dc9892013-02-19 02:24:26 +020042static int tty_stdio;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +020043static int sync_kconfig;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static int conf_cnt;
Markus Mayer74dba802015-12-09 14:56:12 -080045static char line[PATH_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static struct menu *rootEntry;
47
Cheng Renquan66c4bd82009-07-12 16:11:48 +080048static void print_help(struct menu *menu)
Sam Ravnborg03d29122007-07-21 00:00:36 +020049{
Cheng Renquan66c4bd82009-07-12 16:11:48 +080050 struct gstr help = str_new();
51
52 menu_get_ext_help(menu, &help);
53
54 printf("\n%s\n", str_get(&help));
55 str_free(&help);
Sam Ravnborg03d29122007-07-21 00:00:36 +020056}
57
J.A. Magallon48b9d032005-06-25 14:59:22 -070058static void strip(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
J.A. Magallon48b9d032005-06-25 14:59:22 -070060 char *p = str;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 int l;
62
63 while ((isspace(*p)))
64 p++;
65 l = strlen(p);
66 if (p != str)
67 memmove(str, p, l + 1);
68 if (!l)
69 return;
70 p = str + l - 1;
71 while ((isspace(*p)))
72 *p-- = 0;
73}
74
Masahiro Yamada5a3dc712018-01-11 22:39:40 +090075/* Helper function to facilitate fgets() by Jean Sacren. */
76static void xfgets(char *str, int size, FILE *in)
77{
78 if (!fgets(str, size, in))
79 fprintf(stderr, "\nError in reading or end of file.\n");
Masahiro Yamadaf3ff6fb2018-02-08 14:56:40 +090080
81 if (!tty_stdio)
82 printf("%s", str);
Masahiro Yamada5a3dc712018-01-11 22:39:40 +090083}
84
Masahiro Yamada89145642021-03-14 04:48:24 +090085static void set_randconfig_seed(void)
86{
87 unsigned int seed;
88 char *env;
89 bool seed_set = false;
90
91 env = getenv("KCONFIG_SEED");
92 if (env && *env) {
93 char *endp;
94
95 seed = strtol(env, &endp, 0);
96 if (*endp == '\0')
97 seed_set = true;
98 }
99
100 if (!seed_set) {
101 struct timeval now;
102
103 /*
104 * Use microseconds derived seed, compensate for systems where it may
105 * be zero.
106 */
107 gettimeofday(&now, NULL);
108 seed = (now.tv_sec + 1) * (now.tv_usec + 1);
109 }
110
111 printf("KCONFIG_SEED=0x%X\n", seed);
112 srand(seed);
113}
114
Masahiro Yamada98f84752021-03-14 04:48:30 +0900115static bool randomize_choice_values(struct symbol *csym)
116{
117 struct property *prop;
118 struct symbol *sym;
119 struct expr *e;
120 int cnt, def;
121
122 /*
123 * If choice is mod then we may have more items selected
124 * and if no then no-one.
125 * In both cases stop.
126 */
127 if (csym->curr.tri != yes)
128 return false;
129
130 prop = sym_get_choice_prop(csym);
131
132 /* count entries in choice block */
133 cnt = 0;
134 expr_list_for_each_sym(prop->expr, e, sym)
135 cnt++;
136
137 /*
138 * find a random value and set it to yes,
139 * set the rest to no so we have only one set
140 */
141 def = rand() % cnt;
142
143 cnt = 0;
144 expr_list_for_each_sym(prop->expr, e, sym) {
145 if (def == cnt++) {
146 sym->def[S_DEF_USER].tri = yes;
147 csym->def[S_DEF_USER].val = sym;
148 } else {
149 sym->def[S_DEF_USER].tri = no;
150 }
151 sym->flags |= SYMBOL_DEF_USER;
152 /* clear VALID to get value calculated */
153 sym->flags &= ~SYMBOL_VALID;
154 }
155 csym->flags |= SYMBOL_DEF_USER;
156 /* clear VALID to get value calculated */
157 csym->flags &= ~SYMBOL_VALID;
158
159 return true;
160}
161
162enum conf_def_mode {
163 def_default,
164 def_yes,
165 def_mod,
166 def_y2m,
167 def_m2y,
168 def_no,
169 def_random
170};
171
172static bool conf_set_all_new_symbols(enum conf_def_mode mode)
173{
174 struct symbol *sym, *csym;
175 int i, cnt;
176 /*
177 * can't go as the default in switch-case below, otherwise gcc whines
178 * about -Wmaybe-uninitialized
179 */
180 int pby = 50; /* probability of bool = y */
181 int pty = 33; /* probability of tristate = y */
182 int ptm = 33; /* probability of tristate = m */
183 bool has_changed = false;
184
185 if (mode == def_random) {
186 int n, p[3];
187 char *env = getenv("KCONFIG_PROBABILITY");
188
189 n = 0;
190 while (env && *env) {
191 char *endp;
192 int tmp = strtol(env, &endp, 10);
193
194 if (tmp >= 0 && tmp <= 100) {
195 p[n++] = tmp;
196 } else {
197 errno = ERANGE;
198 perror("KCONFIG_PROBABILITY");
199 exit(1);
200 }
201 env = (*endp == ':') ? endp + 1 : endp;
202 if (n >= 3)
203 break;
204 }
205 switch (n) {
206 case 1:
207 pby = p[0];
208 ptm = pby / 2;
209 pty = pby - ptm;
210 break;
211 case 2:
212 pty = p[0];
213 ptm = p[1];
214 pby = pty + ptm;
215 break;
216 case 3:
217 pby = p[0];
218 pty = p[1];
219 ptm = p[2];
220 break;
221 }
222
223 if (pty + ptm > 100) {
224 errno = ERANGE;
225 perror("KCONFIG_PROBABILITY");
226 exit(1);
227 }
228 }
229
230 for_all_symbols(i, sym) {
231 if (sym_has_value(sym) || sym->flags & SYMBOL_VALID)
232 continue;
233 switch (sym_get_type(sym)) {
234 case S_BOOLEAN:
235 case S_TRISTATE:
236 has_changed = true;
237 switch (mode) {
238 case def_yes:
239 sym->def[S_DEF_USER].tri = yes;
240 break;
241 case def_mod:
242 sym->def[S_DEF_USER].tri = mod;
243 break;
244 case def_no:
245 if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
246 sym->def[S_DEF_USER].tri = yes;
247 else
248 sym->def[S_DEF_USER].tri = no;
249 break;
250 case def_random:
251 sym->def[S_DEF_USER].tri = no;
252 cnt = rand() % 100;
253 if (sym->type == S_TRISTATE) {
254 if (cnt < pty)
255 sym->def[S_DEF_USER].tri = yes;
256 else if (cnt < pty + ptm)
257 sym->def[S_DEF_USER].tri = mod;
258 } else if (cnt < pby)
259 sym->def[S_DEF_USER].tri = yes;
260 break;
261 default:
262 continue;
263 }
264 if (!(sym_is_choice(sym) && mode == def_random))
265 sym->flags |= SYMBOL_DEF_USER;
266 break;
267 default:
268 break;
269 }
270
271 }
272
273 sym_clear_all_valid();
274
275 /*
276 * We have different type of choice blocks.
277 * If curr.tri equals to mod then we can select several
278 * choice symbols in one block.
279 * In this case we do nothing.
280 * If curr.tri equals yes then only one symbol can be
281 * selected in a choice block and we set it to yes,
282 * and the rest to no.
283 */
284 if (mode != def_random) {
285 for_all_symbols(i, csym) {
286 if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
287 sym_is_choice_value(csym))
288 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
289 }
290 }
291
292 for_all_symbols(i, csym) {
293 if (sym_has_value(csym) || !sym_is_choice(csym))
294 continue;
295
296 sym_calc_value(csym);
297 if (mode == def_random)
298 has_changed |= randomize_choice_values(csym);
299 else {
300 set_all_choice_values(csym);
301 has_changed = true;
302 }
303 }
304
305 return has_changed;
306}
307
Masahiro Yamada15e68d02021-03-14 04:48:29 +0900308static void conf_rewrite_mod_or_yes(enum conf_def_mode mode)
309{
310 struct symbol *sym;
311 int i;
312 tristate old_val = (mode == def_y2m) ? yes : mod;
313 tristate new_val = (mode == def_y2m) ? mod : yes;
314
315 for_all_symbols(i, sym) {
316 if (sym_get_type(sym) == S_TRISTATE &&
317 sym->def[S_DEF_USER].tri == old_val)
318 sym->def[S_DEF_USER].tri = new_val;
319 }
320 sym_clear_all_valid();
321}
322
Roman Zippelf82f3f92007-08-30 05:06:17 +0200323static int conf_askvalue(struct symbol *sym, const char *def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (!sym_has_value(sym))
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200326 printf("(NEW) ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 line[0] = '\n';
329 line[1] = 0;
330
Marco Ammonbaa23ec2019-07-04 12:50:41 +0200331 if (!sym_is_changeable(sym)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 printf("%s\n", def);
333 line[0] = '\n';
334 line[1] = 0;
Roman Zippelf82f3f92007-08-30 05:06:17 +0200335 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337
338 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200339 case oldconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900340 case syncconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (sym_has_value(sym)) {
342 printf("%s\n", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200343 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400345 /* fall through */
Masahiro Yamada102a1a72021-02-21 18:26:23 +0900346 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 fflush(stdout);
Markus Mayer74dba802015-12-09 14:56:12 -0800348 xfgets(line, sizeof(line), stdin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 break;
350 }
351
Roman Zippelf82f3f92007-08-30 05:06:17 +0200352 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Trevor Keith4356f482009-09-18 12:49:23 -0700355static int conf_string(struct menu *menu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 struct symbol *sym = menu->sym;
Sam Ravnborg03d29122007-07-21 00:00:36 +0200358 const char *def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 while (1) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200361 printf("%*s%s ", indent - 1, "", menu->prompt->text);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 printf("(%s) ", sym->name);
363 def = sym_get_string_value(sym);
Mickaël Salaünf82bd802021-02-15 19:15:09 +0100364 if (def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 printf("[%s] ", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200366 if (!conf_askvalue(sym, def))
367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 switch (line[0]) {
369 case '\n':
370 break;
371 case '?':
372 /* print help */
373 if (line[1] == '\n') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800374 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 def = NULL;
376 break;
377 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400378 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 default:
380 line[strlen(line)-1] = 0;
381 def = line;
382 }
383 if (def && sym_set_string_value(sym, def))
384 return 0;
385 }
386}
387
388static int conf_sym(struct menu *menu)
389{
390 struct symbol *sym = menu->sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 tristate oldval, newval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 while (1) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200394 printf("%*s%s ", indent - 1, "", menu->prompt->text);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (sym->name)
396 printf("(%s) ", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 putchar('[');
398 oldval = sym_get_tristate_value(sym);
399 switch (oldval) {
400 case no:
401 putchar('N');
402 break;
403 case mod:
404 putchar('M');
405 break;
406 case yes:
407 putchar('Y');
408 break;
409 }
410 if (oldval != no && sym_tristate_within_range(sym, no))
411 printf("/n");
412 if (oldval != mod && sym_tristate_within_range(sym, mod))
413 printf("/m");
414 if (oldval != yes && sym_tristate_within_range(sym, yes))
415 printf("/y");
Masahiro Yamada4f208f32018-02-06 09:34:43 +0900416 printf("/?] ");
Roman Zippelf82f3f92007-08-30 05:06:17 +0200417 if (!conf_askvalue(sym, sym_get_string_value(sym)))
418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 strip(line);
420
421 switch (line[0]) {
422 case 'n':
423 case 'N':
424 newval = no;
425 if (!line[1] || !strcmp(&line[1], "o"))
426 break;
427 continue;
428 case 'm':
429 case 'M':
430 newval = mod;
431 if (!line[1])
432 break;
433 continue;
434 case 'y':
435 case 'Y':
436 newval = yes;
437 if (!line[1] || !strcmp(&line[1], "es"))
438 break;
439 continue;
440 case 0:
441 newval = oldval;
442 break;
443 case '?':
444 goto help;
445 default:
446 continue;
447 }
448 if (sym_set_tristate_value(sym, newval))
449 return 0;
450help:
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800451 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453}
454
455static int conf_choice(struct menu *menu)
456{
457 struct symbol *sym, *def_sym;
458 struct menu *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 bool is_new;
460
461 sym = menu->sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 is_new = !sym_has_value(sym);
Marco Ammonbaa23ec2019-07-04 12:50:41 +0200463 if (sym_is_changeable(sym)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 conf_sym(menu);
465 sym_calc_value(sym);
466 switch (sym_get_tristate_value(sym)) {
467 case no:
468 return 1;
469 case mod:
470 return 0;
471 case yes:
472 break;
473 }
474 } else {
475 switch (sym_get_tristate_value(sym)) {
476 case no:
477 return 1;
478 case mod:
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200479 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return 0;
481 case yes:
482 break;
483 }
484 }
485
486 while (1) {
487 int cnt, def;
488
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200489 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 def_sym = sym_get_choice_value(sym);
491 cnt = def = 0;
Roman Zippel40aee722006-04-09 17:26:39 +0200492 line[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 for (child = menu->list; child; child = child->next) {
494 if (!menu_is_visible(child))
495 continue;
496 if (!child->sym) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200497 printf("%*c %s\n", indent, '*', menu_get_prompt(child));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 continue;
499 }
500 cnt++;
501 if (child->sym == def_sym) {
502 def = cnt;
503 printf("%*c", indent, '>');
504 } else
505 printf("%*c", indent, ' ');
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200506 printf(" %d. %s", cnt, menu_get_prompt(child));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (child->sym->name)
508 printf(" (%s)", child->sym->name);
509 if (!sym_has_value(child->sym))
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200510 printf(" (NEW)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 printf("\n");
512 }
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200513 printf("%*schoice", indent - 1, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (cnt == 1) {
515 printf("[1]: 1\n");
516 goto conf_childs;
517 }
Masahiro Yamada4f208f32018-02-06 09:34:43 +0900518 printf("[1-%d?]: ", cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200520 case oldconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900521 case syncconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (!is_new) {
523 cnt = def;
524 printf("%d\n", cnt);
525 break;
526 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400527 /* fall through */
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200528 case oldaskconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 fflush(stdout);
Markus Mayer74dba802015-12-09 14:56:12 -0800530 xfgets(line, sizeof(line), stdin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 strip(line);
532 if (line[0] == '?') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800533 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 continue;
535 }
536 if (!line[0])
537 cnt = def;
538 else if (isdigit(line[0]))
539 cnt = atoi(line);
540 else
541 continue;
542 break;
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200543 default:
544 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546
547 conf_childs:
548 for (child = menu->list; child; child = child->next) {
549 if (!child->sym || !menu_is_visible(child))
550 continue;
551 if (!--cnt)
552 break;
553 }
554 if (!child)
555 continue;
Ben Hutchings3ba41622011-04-23 18:42:56 +0100556 if (line[0] && line[strlen(line) - 1] == '?') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800557 print_help(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 continue;
559 }
560 sym_set_choice_value(sym, child->sym);
Jan Beulichf5eaa322008-01-24 11:54:23 +0000561 for (child = child->list; child; child = child->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 indent += 2;
Jan Beulichf5eaa322008-01-24 11:54:23 +0000563 conf(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 indent -= 2;
565 }
566 return 1;
567 }
568}
569
570static void conf(struct menu *menu)
571{
572 struct symbol *sym;
573 struct property *prop;
574 struct menu *child;
575
576 if (!menu_is_visible(menu))
577 return;
578
579 sym = menu->sym;
580 prop = menu->prompt;
581 if (prop) {
582 const char *prompt;
583
584 switch (prop->type) {
585 case P_MENU:
Masahiro Yamada2aad9b82018-02-28 09:15:24 +0900586 /*
587 * Except in oldaskconfig mode, we show only menus that
588 * contain new symbols.
589 */
590 if (input_mode != oldaskconfig && rootEntry != menu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 check_conf(menu);
592 return;
593 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400594 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 case P_COMMENT:
596 prompt = menu_get_prompt(menu);
597 if (prompt)
598 printf("%*c\n%*c %s\n%*c\n",
599 indent, '*',
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200600 indent, '*', prompt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 indent, '*');
602 default:
603 ;
604 }
605 }
606
607 if (!sym)
608 goto conf_childs;
609
610 if (sym_is_choice(sym)) {
611 conf_choice(menu);
612 if (sym->curr.tri != mod)
613 return;
614 goto conf_childs;
615 }
616
617 switch (sym->type) {
618 case S_INT:
619 case S_HEX:
620 case S_STRING:
621 conf_string(menu);
622 break;
623 default:
624 conf_sym(menu);
625 break;
626 }
627
628conf_childs:
629 if (sym)
630 indent += 2;
631 for (child = menu->list; child; child = child->next)
632 conf(child);
633 if (sym)
634 indent -= 2;
635}
636
637static void check_conf(struct menu *menu)
638{
639 struct symbol *sym;
640 struct menu *child;
641
642 if (!menu_is_visible(menu))
643 return;
644
645 sym = menu->sym;
Masahiro Yamadaa4cff322021-02-21 18:26:22 +0900646 if (sym && !sym_has_value(sym) &&
647 (sym_is_changeable(sym) ||
648 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes))) {
Don Zickus17baab62018-04-11 15:15:37 -0400649
Masahiro Yamadaa4cff322021-02-21 18:26:22 +0900650 switch (input_mode) {
651 case listnewconfig:
652 if (sym->name) {
653 const char *str;
654
655 if (sym->type == S_STRING) {
656 str = sym_get_string_value(sym);
657 str = sym_escape_string_value(str);
658 printf("%s%s=%s\n", CONFIG_, sym->name, str);
659 free((void *)str);
660 } else {
661 str = sym_get_string_value(sym);
662 printf("%s%s=%s\n", CONFIG_, sym->name, str);
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400663 }
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400664 }
Masahiro Yamadaa4cff322021-02-21 18:26:22 +0900665 break;
666 case helpnewconfig:
667 printf("-----\n");
668 print_help(menu);
669 printf("-----\n");
670 break;
671 default:
672 if (!conf_cnt++)
673 printf("*\n* Restart config...\n*\n");
674 rootEntry = menu_get_parent_menu(menu);
675 conf(rootEntry);
676 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
680 for (child = menu->list; child; child = child->next)
681 check_conf(child);
682}
683
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200684static struct option long_opts[] = {
Masahiro Yamadabafc4792021-03-14 04:48:26 +0900685 {"help", no_argument, NULL, 'h'},
686 {"silent", no_argument, NULL, 's'},
Masahiro Yamadaed562c52021-03-14 04:48:25 +0900687 {"oldaskconfig", no_argument, &input_mode_opt, oldaskconfig},
688 {"oldconfig", no_argument, &input_mode_opt, oldconfig},
689 {"syncconfig", no_argument, &input_mode_opt, syncconfig},
690 {"defconfig", required_argument, &input_mode_opt, defconfig},
691 {"savedefconfig", required_argument, &input_mode_opt, savedefconfig},
692 {"allnoconfig", no_argument, &input_mode_opt, allnoconfig},
693 {"allyesconfig", no_argument, &input_mode_opt, allyesconfig},
694 {"allmodconfig", no_argument, &input_mode_opt, allmodconfig},
695 {"alldefconfig", no_argument, &input_mode_opt, alldefconfig},
696 {"randconfig", no_argument, &input_mode_opt, randconfig},
697 {"listnewconfig", no_argument, &input_mode_opt, listnewconfig},
698 {"helpnewconfig", no_argument, &input_mode_opt, helpnewconfig},
699 {"olddefconfig", no_argument, &input_mode_opt, olddefconfig},
700 {"yes2modconfig", no_argument, &input_mode_opt, yes2modconfig},
701 {"mod2yesconfig", no_argument, &input_mode_opt, mod2yesconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200702 {NULL, 0, NULL, 0}
703};
704
Arnaud Lacombe32543992010-11-02 00:26:33 -0400705static void conf_usage(const char *progname)
706{
Masahiro Yamadaee4c6f02021-03-14 04:48:27 +0900707 printf("Usage: %s [options] <kconfig-file>\n", progname);
708 printf("\n");
709 printf("Generic options:\n");
710 printf(" -h, --help Print this message and exit.\n");
711 printf(" -s, --silent Do not print log.\n");
712 printf("\n");
713 printf("Mode options:\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400714 printf(" --listnewconfig List new options\n");
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500715 printf(" --helpnewconfig List new options and help text\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400716 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
717 printf(" --oldconfig Update a configuration using a provided .config as base\n");
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900718 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
719 " include/{generated/,config/}\n");
Marc Herbertcedd55d2018-01-26 14:59:00 -0800720 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400721 printf(" --defconfig <file> New config with default defined in <file>\n");
722 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
723 printf(" --allnoconfig New config where all options are answered with no\n");
724 printf(" --allyesconfig New config where all options are answered with yes\n");
725 printf(" --allmodconfig New config where all options are answered with mod\n");
726 printf(" --alldefconfig New config with all symbols set to default\n");
727 printf(" --randconfig New config with random answer to all options\n");
Tetsuo Handa89b90602019-12-17 18:42:06 +0900728 printf(" --yes2modconfig Change answers from yes to mod if possible\n");
729 printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
Masahiro Yamadaae8da722021-02-21 22:03:17 +0900730 printf(" (If none of the above is given, --oldaskconfig is the default)\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400731}
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733int main(int ac, char **av)
734{
Arnaud Lacombe32543992010-11-02 00:26:33 -0400735 const char *progname = av[0];
Andres Salomon2f4b4892007-12-17 01:34:58 -0500736 int opt;
Arnaud Lacombe275744c2010-10-13 20:43:28 -0400737 const char *name, *defconfig_file = NULL /* gcc uninit */;
Masahiro Yamada16952b72018-07-20 16:46:30 +0900738 int no_conf_write = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Masahiro Yamadaf3ff6fb2018-02-08 14:56:40 +0900740 tty_stdio = isatty(0) && isatty(1);
Ben Hutchings62dc9892013-02-19 02:24:26 +0200741
Masahiro Yamadaa2af62c2021-02-21 22:03:16 +0900742 while ((opt = getopt_long(ac, av, "hs", long_opts, NULL)) != -1) {
Andres Salomon2f4b4892007-12-17 01:34:58 -0500743 switch (opt) {
Masahiro Yamadaa2af62c2021-02-21 22:03:16 +0900744 case 'h':
Arnaud Lacombe32543992010-11-02 00:26:33 -0400745 conf_usage(progname);
Andres Salomon2f4b4892007-12-17 01:34:58 -0500746 exit(1);
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200747 break;
Masahiro Yamadaed562c52021-03-14 04:48:25 +0900748 case 's':
749 conf_set_message_callback(NULL);
750 break;
751 case 0:
752 input_mode = input_mode_opt;
753 switch (input_mode) {
754 case syncconfig:
755 /*
756 * syncconfig is invoked during the build stage.
757 * Suppress distracting
758 * "configuration written to ..."
759 */
760 conf_set_message_callback(NULL);
761 sync_kconfig = 1;
762 break;
763 case defconfig:
764 case savedefconfig:
765 defconfig_file = optarg;
766 break;
767 case randconfig:
768 set_randconfig_seed();
769 break;
770 default:
771 break;
772 }
773 default:
774 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776 }
Andres Salomon2f4b4892007-12-17 01:34:58 -0500777 if (ac == optind) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200778 fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
Arnaud Lacombe32543992010-11-02 00:26:33 -0400779 conf_usage(progname);
Randy Dunlap250725a2006-06-08 22:12:50 -0700780 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
Masahiro Yamada9a3c3bc2021-03-14 04:48:28 +0900782 conf_parse(av[optind]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 //zconfdump(stdout);
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200786 case defconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (conf_read(defconfig_file)) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900788 fprintf(stderr,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200789 "***\n"
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900790 "*** Can't find default configuration \"%s\"!\n"
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200791 "***\n",
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900792 defconfig_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 exit(1);
794 }
795 break;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200796 case savedefconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900797 case syncconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200798 case oldaskconfig:
799 case oldconfig:
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200800 case listnewconfig:
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500801 case helpnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800802 case olddefconfig:
Tetsuo Handa89b90602019-12-17 18:42:06 +0900803 case yes2modconfig:
804 case mod2yesconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 conf_read(NULL);
806 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200807 case allnoconfig:
808 case allyesconfig:
809 case allmodconfig:
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200810 case alldefconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200811 case randconfig:
Roman Zippel90389162005-11-08 21:34:49 -0800812 name = getenv("KCONFIG_ALLCONFIG");
Eric W. Biederman9f420bf2012-05-07 05:37:45 -0700813 if (!name)
814 break;
815 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700816 if (conf_read_simple(name, S_DEF_USER)) {
817 fprintf(stderr,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200818 "*** Can't read seed configuration \"%s\"!\n",
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700819 name);
820 exit(1);
821 }
Roman Zippel90389162005-11-08 21:34:49 -0800822 break;
823 }
824 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200825 case allnoconfig: name = "allno.config"; break;
826 case allyesconfig: name = "allyes.config"; break;
827 case allmodconfig: name = "allmod.config"; break;
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200828 case alldefconfig: name = "alldef.config"; break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200829 case randconfig: name = "allrandom.config"; break;
Roman Zippel90389162005-11-08 21:34:49 -0800830 default: break;
831 }
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700832 if (conf_read_simple(name, S_DEF_USER) &&
833 conf_read_simple("all.config", S_DEF_USER)) {
834 fprintf(stderr,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200835 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700836 name);
837 exit(1);
838 }
Roman Zippel90389162005-11-08 21:34:49 -0800839 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 default:
841 break;
842 }
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200843
844 if (sync_kconfig) {
Masahiro Yamada16952b72018-07-20 16:46:30 +0900845 name = getenv("KCONFIG_NOSILENTUPDATE");
846 if (name && *name) {
847 if (conf_get_changed()) {
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200848 fprintf(stderr,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200849 "\n*** The configuration requires explicit update.\n\n");
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200850 return 1;
851 }
Masahiro Yamada16952b72018-07-20 16:46:30 +0900852 no_conf_write = 1;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200853 }
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200854 }
855
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200856 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200857 case allnoconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200858 conf_set_all_new_symbols(def_no);
859 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200860 case allyesconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200861 conf_set_all_new_symbols(def_yes);
862 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200863 case allmodconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200864 conf_set_all_new_symbols(def_mod);
865 break;
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200866 case alldefconfig:
867 conf_set_all_new_symbols(def_default);
868 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200869 case randconfig:
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +0200870 /* Really nothing to do in this loop */
871 while (conf_set_all_new_symbols(def_random)) ;
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200872 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200873 case defconfig:
Sam Ravnborg09748e12008-06-30 23:02:59 +0200874 conf_set_all_new_symbols(def_default);
875 break;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200876 case savedefconfig:
877 break;
Tetsuo Handa89b90602019-12-17 18:42:06 +0900878 case yes2modconfig:
879 conf_rewrite_mod_or_yes(def_y2m);
880 break;
881 case mod2yesconfig:
882 conf_rewrite_mod_or_yes(def_m2y);
883 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200884 case oldaskconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200885 rootEntry = &rootmenu;
886 conf(&rootmenu);
Masahiro Yamada2aad9b82018-02-28 09:15:24 +0900887 input_mode = oldconfig;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200888 /* fall through */
Sam Ravnborg14828342010-08-06 07:13:54 +0200889 case oldconfig:
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200890 case listnewconfig:
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500891 case helpnewconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900892 case syncconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200893 /* Update until a loop caused no more changes */
894 do {
895 conf_cnt = 0;
896 check_conf(&rootmenu);
Masahiro Yamada99f0b652018-02-28 09:15:23 +0900897 } while (conf_cnt);
Masahiro Yamada59a80b52018-02-28 09:15:21 +0900898 break;
899 case olddefconfig:
900 default:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200901 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200903
Masahiro Yamada00c864f2018-07-20 16:46:31 +0900904 if (input_mode == savedefconfig) {
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200905 if (conf_write_defconfig(defconfig_file)) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200906 fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900907 defconfig_file);
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200908 return 1;
909 }
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500910 } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
Masahiro Yamada00c864f2018-07-20 16:46:31 +0900911 if (!no_conf_write && conf_write(NULL)) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200912 fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200913 exit(1);
914 }
Masahiro Yamada00c864f2018-07-20 16:46:31 +0900915
916 /*
917 * Create auto.conf if it does not exist.
918 * This prevents GNU Make 4.1 or older from emitting
919 * "include/config/auto.conf: No such file or directory"
920 * in the top-level Makefile
921 *
922 * syncconfig always creates or updates auto.conf because it is
923 * used during the build.
924 */
925 if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
926 fprintf(stderr,
927 "\n*** Error during sync of the configuration.\n\n");
928 return 1;
929 }
Roman Zippelc955cca2006-06-08 22:12:39 -0700930 }
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200931 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}