blob: e68da2d44f0444239b748391a7c710edbacd032e [file] [log] [blame]
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001// SPDX-License-Identifier: GPL-2.0
Patrick Georgi0588d192009-08-12 15:00:51 +00002/*
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
Patrick Georgi0588d192009-08-12 15:00:51 +00004 */
5
Patrick Georgi0588d192009-08-12 15:00:51 +00006#include <ctype.h>
Patrick Georgi53ea1d42019-11-22 16:55:58 +01007#include <limits.h>
Patrick Georgi0588d192009-08-12 15:00:51 +00008#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <time.h>
12#include <unistd.h>
Patrick Georgid5208402014-04-11 20:24:06 +020013#include <getopt.h>
Patrick Georgid5208402014-04-11 20:24:06 +020014#include <sys/time.h>
15#include <errno.h>
Patrick Georgi0588d192009-08-12 15:00:51 +000016
Patrick Georgi0588d192009-08-12 15:00:51 +000017#include "lkc.h"
18
Stefan Reinauer57a31312015-08-20 11:19:34 -070019int kconfig_warnings = 0;
20
Patrick Georgi0588d192009-08-12 15:00:51 +000021static void conf(struct menu *menu);
22static void check_conf(struct menu *menu);
23
Patrick Georgid5208402014-04-11 20:24:06 +020024enum input_mode {
25 oldaskconfig,
Patrick Georgi53ea1d42019-11-22 16:55:58 +010026 syncconfig,
Patrick Georgid5208402014-04-11 20:24:06 +020027 oldconfig,
28 allnoconfig,
29 allyesconfig,
30 allmodconfig,
31 alldefconfig,
32 randconfig,
33 defconfig,
34 savedefconfig,
35 listnewconfig,
Patrick Georgi53ea1d42019-11-22 16:55:58 +010036 helpnewconfig,
Patrick Georgid5208402014-04-11 20:24:06 +020037 olddefconfig,
Patrick Georgi53ea1d42019-11-22 16:55:58 +010038 yes2modconfig,
39 mod2yesconfig,
Patrick Georgi7eb03cb2022-10-28 01:00:26 +020040 mod2noconfig,
Patrick Georgi53ea1d42019-11-22 16:55:58 +010041};
42static enum input_mode input_mode = oldaskconfig;
43static int input_mode_opt;
Patrick Georgi0588d192009-08-12 15:00:51 +000044static int indent = 1;
Patrick Georgid5208402014-04-11 20:24:06 +020045static int tty_stdio;
Patrick Georgid5208402014-04-11 20:24:06 +020046static int sync_kconfig;
Patrick Georgi0588d192009-08-12 15:00:51 +000047static int conf_cnt;
Patrick Georgi53ea1d42019-11-22 16:55:58 +010048static char line[PATH_MAX];
Patrick Georgi0588d192009-08-12 15:00:51 +000049static struct menu *rootEntry;
50
Patrick Georgid5208402014-04-11 20:24:06 +020051static void print_help(struct menu *menu)
Patrick Georgi0588d192009-08-12 15:00:51 +000052{
Patrick Georgid5208402014-04-11 20:24:06 +020053 struct gstr help = str_new();
54
55 menu_get_ext_help(menu, &help);
56
57 printf("\n%s\n", str_get(&help));
58 str_free(&help);
Patrick Georgi0588d192009-08-12 15:00:51 +000059}
60
61static void strip(char *str)
62{
63 char *p = str;
64 int l;
65
66 while ((isspace(*p)))
67 p++;
68 l = strlen(p);
69 if (p != str)
70 memmove(str, p, l + 1);
71 if (!l)
72 return;
73 p = str + l - 1;
74 while ((isspace(*p)))
75 *p-- = 0;
76}
77
Patrick Georgi53ea1d42019-11-22 16:55:58 +010078/* Helper function to facilitate fgets() by Jean Sacren. */
79static void xfgets(char *str, int size, FILE *in)
Patrick Georgi0588d192009-08-12 15:00:51 +000080{
Patrick Georgi53ea1d42019-11-22 16:55:58 +010081 if (!fgets(str, size, in))
82 fprintf(stderr, "\nError in reading or end of file.\n");
83
84 if (!tty_stdio)
85 printf("%s", str);
86}
87
88static void set_randconfig_seed(void)
89{
90 unsigned int seed;
91 char *env;
92 bool seed_set = false;
93
94 env = getenv("KCONFIG_SEED");
95 if (env && *env) {
96 char *endp;
97
98 seed = strtol(env, &endp, 0);
99 if (*endp == '\0')
100 seed_set = true;
Patrick Georgi0588d192009-08-12 15:00:51 +0000101 }
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100102
103 if (!seed_set) {
104 struct timeval now;
105
106 /*
107 * Use microseconds derived seed, compensate for systems where it may
108 * be zero.
109 */
110 gettimeofday(&now, NULL);
111 seed = (now.tv_sec + 1) * (now.tv_usec + 1);
112 }
113
114 printf("KCONFIG_SEED=0x%X\n", seed);
115 srand(seed);
116}
117
118static bool randomize_choice_values(struct symbol *csym)
119{
120 struct property *prop;
121 struct symbol *sym;
122 struct expr *e;
123 int cnt, def;
124
125 /*
126 * If choice is mod then we may have more items selected
127 * and if no then no-one.
128 * In both cases stop.
129 */
130 if (csym->curr.tri != yes)
131 return false;
132
133 prop = sym_get_choice_prop(csym);
134
135 /* count entries in choice block */
136 cnt = 0;
137 expr_list_for_each_sym(prop->expr, e, sym)
138 cnt++;
139
140 /*
141 * find a random value and set it to yes,
142 * set the rest to no so we have only one set
143 */
144 def = rand() % cnt;
145
146 cnt = 0;
147 expr_list_for_each_sym(prop->expr, e, sym) {
148 if (def == cnt++) {
149 sym->def[S_DEF_USER].tri = yes;
150 csym->def[S_DEF_USER].val = sym;
151 } else {
152 sym->def[S_DEF_USER].tri = no;
153 }
154 sym->flags |= SYMBOL_DEF_USER;
155 /* clear VALID to get value calculated */
156 sym->flags &= ~SYMBOL_VALID;
157 }
158 csym->flags |= SYMBOL_DEF_USER;
159 /* clear VALID to get value calculated */
160 csym->flags &= ~SYMBOL_VALID;
161
162 return true;
163}
164
165enum conf_def_mode {
166 def_default,
167 def_yes,
168 def_mod,
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100169 def_no,
170 def_random
171};
172
173static bool conf_set_all_new_symbols(enum conf_def_mode mode)
174{
175 struct symbol *sym, *csym;
176 int i, cnt;
177 /*
178 * can't go as the default in switch-case below, otherwise gcc whines
179 * about -Wmaybe-uninitialized
180 */
181 int pby = 50; /* probability of bool = y */
182 int pty = 33; /* probability of tristate = y */
183 int ptm = 33; /* probability of tristate = m */
184 bool has_changed = false;
185
186 if (mode == def_random) {
187 int n, p[3];
188 char *env = getenv("KCONFIG_PROBABILITY");
189
190 n = 0;
191 while (env && *env) {
192 char *endp;
193 int tmp = strtol(env, &endp, 10);
194
195 if (tmp >= 0 && tmp <= 100) {
196 p[n++] = tmp;
197 } else {
198 errno = ERANGE;
199 perror("KCONFIG_PROBABILITY");
200 exit(1);
201 }
202 env = (*endp == ':') ? endp + 1 : endp;
203 if (n >= 3)
204 break;
205 }
206 switch (n) {
207 case 1:
208 pby = p[0];
209 ptm = pby / 2;
210 pty = pby - ptm;
211 break;
212 case 2:
213 pty = p[0];
214 ptm = p[1];
215 pby = pty + ptm;
216 break;
217 case 3:
218 pby = p[0];
219 pty = p[1];
220 ptm = p[2];
221 break;
222 }
223
224 if (pty + ptm > 100) {
225 errno = ERANGE;
226 perror("KCONFIG_PROBABILITY");
227 exit(1);
228 }
229 }
230
231 for_all_symbols(i, sym) {
232 if (sym_has_value(sym) || sym->flags & SYMBOL_VALID)
233 continue;
234 switch (sym_get_type(sym)) {
235 case S_BOOLEAN:
236 case S_TRISTATE:
237 has_changed = true;
238 switch (mode) {
239 case def_yes:
240 sym->def[S_DEF_USER].tri = yes;
241 break;
242 case def_mod:
243 sym->def[S_DEF_USER].tri = mod;
244 break;
245 case def_no:
246 sym->def[S_DEF_USER].tri = no;
247 break;
248 case def_random:
249 sym->def[S_DEF_USER].tri = no;
250 cnt = rand() % 100;
251 if (sym->type == S_TRISTATE) {
252 if (cnt < pty)
253 sym->def[S_DEF_USER].tri = yes;
254 else if (cnt < pty + ptm)
255 sym->def[S_DEF_USER].tri = mod;
256 } else if (cnt < pby)
257 sym->def[S_DEF_USER].tri = yes;
258 break;
259 default:
260 continue;
261 }
262 if (!(sym_is_choice(sym) && mode == def_random))
263 sym->flags |= SYMBOL_DEF_USER;
264 break;
265 default:
266 break;
267 }
268
269 }
270
271 sym_clear_all_valid();
272
273 /*
274 * We have different type of choice blocks.
275 * If curr.tri equals to mod then we can select several
276 * choice symbols in one block.
277 * In this case we do nothing.
278 * If curr.tri equals yes then only one symbol can be
279 * selected in a choice block and we set it to yes,
280 * and the rest to no.
281 */
282 if (mode != def_random) {
283 for_all_symbols(i, csym) {
284 if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
285 sym_is_choice_value(csym))
286 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
287 }
288 }
289
290 for_all_symbols(i, csym) {
291 if (sym_has_value(csym) || !sym_is_choice(csym))
292 continue;
293
294 sym_calc_value(csym);
295 if (mode == def_random)
296 has_changed |= randomize_choice_values(csym);
297 else {
298 set_all_choice_values(csym);
299 has_changed = true;
300 }
301 }
302
303 return has_changed;
304}
305
Patrick Georgi7eb03cb2022-10-28 01:00:26 +0200306static void conf_rewrite_tristates(tristate old_val, tristate new_val)
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100307{
308 struct symbol *sym;
309 int i;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100310
311 for_all_symbols(i, sym) {
312 if (sym_get_type(sym) == S_TRISTATE &&
313 sym->def[S_DEF_USER].tri == old_val)
314 sym->def[S_DEF_USER].tri = new_val;
315 }
316 sym_clear_all_valid();
Patrick Georgi0588d192009-08-12 15:00:51 +0000317}
318
319static int conf_askvalue(struct symbol *sym, const char *def)
320{
Patrick Georgi0588d192009-08-12 15:00:51 +0000321 if (!sym_has_value(sym))
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100322 printf("(NEW) ");
Patrick Georgi0588d192009-08-12 15:00:51 +0000323
324 line[0] = '\n';
325 line[1] = 0;
326
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100327 if (!sym_is_changeable(sym)) {
Patrick Georgi0588d192009-08-12 15:00:51 +0000328 printf("%s\n", def);
329 line[0] = '\n';
330 line[1] = 0;
331 return 0;
332 }
333
334 switch (input_mode) {
Patrick Georgid5208402014-04-11 20:24:06 +0200335 case oldconfig:
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100336 case syncconfig:
Patrick Georgi0588d192009-08-12 15:00:51 +0000337 if (sym_has_value(sym)) {
338 printf("%s\n", def);
339 return 0;
340 }
Patrick Georgid5208402014-04-11 20:24:06 +0200341 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000342 default:
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100343 fflush(stdout);
344 xfgets(line, sizeof(line), stdin);
Patrick Georgi0588d192009-08-12 15:00:51 +0000345 break;
346 }
347
Patrick Georgi0588d192009-08-12 15:00:51 +0000348 return 1;
349}
350
Patrick Georgid5208402014-04-11 20:24:06 +0200351static int conf_string(struct menu *menu)
Patrick Georgi0588d192009-08-12 15:00:51 +0000352{
353 struct symbol *sym = menu->sym;
354 const char *def;
355
356 while (1) {
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100357 printf("%*s%s ", indent - 1, "", menu->prompt->text);
Patrick Georgi0588d192009-08-12 15:00:51 +0000358 printf("(%s) ", sym->name);
359 def = sym_get_string_value(sym);
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100360 if (def)
Patrick Georgi0588d192009-08-12 15:00:51 +0000361 printf("[%s] ", def);
362 if (!conf_askvalue(sym, def))
363 return 0;
364 switch (line[0]) {
365 case '\n':
366 break;
367 case '?':
368 /* print help */
369 if (line[1] == '\n') {
Patrick Georgid5208402014-04-11 20:24:06 +0200370 print_help(menu);
Patrick Georgi0588d192009-08-12 15:00:51 +0000371 def = NULL;
372 break;
373 }
Patrick Georgid5208402014-04-11 20:24:06 +0200374 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000375 default:
376 line[strlen(line)-1] = 0;
377 def = line;
378 }
379 if (def && sym_set_string_value(sym, def))
380 return 0;
381 }
382}
383
384static int conf_sym(struct menu *menu)
385{
386 struct symbol *sym = menu->sym;
Patrick Georgi0588d192009-08-12 15:00:51 +0000387 tristate oldval, newval;
388
389 while (1) {
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100390 printf("%*s%s ", indent - 1, "", menu->prompt->text);
Patrick Georgi0588d192009-08-12 15:00:51 +0000391 if (sym->name)
392 printf("(%s) ", sym->name);
Patrick Georgi0588d192009-08-12 15:00:51 +0000393 putchar('[');
394 oldval = sym_get_tristate_value(sym);
395 switch (oldval) {
396 case no:
397 putchar('N');
398 break;
399 case mod:
400 putchar('M');
401 break;
402 case yes:
403 putchar('Y');
404 break;
405 }
406 if (oldval != no && sym_tristate_within_range(sym, no))
407 printf("/n");
408 if (oldval != mod && sym_tristate_within_range(sym, mod))
409 printf("/m");
410 if (oldval != yes && sym_tristate_within_range(sym, yes))
411 printf("/y");
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100412 printf("/?] ");
Patrick Georgi0588d192009-08-12 15:00:51 +0000413 if (!conf_askvalue(sym, sym_get_string_value(sym)))
414 return 0;
415 strip(line);
416
417 switch (line[0]) {
418 case 'n':
419 case 'N':
420 newval = no;
421 if (!line[1] || !strcmp(&line[1], "o"))
422 break;
423 continue;
424 case 'm':
425 case 'M':
426 newval = mod;
427 if (!line[1])
428 break;
429 continue;
430 case 'y':
431 case 'Y':
432 newval = yes;
433 if (!line[1] || !strcmp(&line[1], "es"))
434 break;
435 continue;
436 case 0:
437 newval = oldval;
438 break;
439 case '?':
440 goto help;
441 default:
442 continue;
443 }
444 if (sym_set_tristate_value(sym, newval))
445 return 0;
446help:
Patrick Georgid5208402014-04-11 20:24:06 +0200447 print_help(menu);
Patrick Georgi0588d192009-08-12 15:00:51 +0000448 }
449}
450
451static int conf_choice(struct menu *menu)
452{
453 struct symbol *sym, *def_sym;
454 struct menu *child;
Patrick Georgi0588d192009-08-12 15:00:51 +0000455 bool is_new;
456
457 sym = menu->sym;
Patrick Georgi0588d192009-08-12 15:00:51 +0000458 is_new = !sym_has_value(sym);
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100459 if (sym_is_changeable(sym)) {
Patrick Georgi0588d192009-08-12 15:00:51 +0000460 conf_sym(menu);
461 sym_calc_value(sym);
462 switch (sym_get_tristate_value(sym)) {
463 case no:
464 return 1;
465 case mod:
466 return 0;
467 case yes:
468 break;
469 }
470 } else {
471 switch (sym_get_tristate_value(sym)) {
472 case no:
473 return 1;
474 case mod:
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100475 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
Patrick Georgi0588d192009-08-12 15:00:51 +0000476 return 0;
477 case yes:
478 break;
479 }
480 }
481
482 while (1) {
483 int cnt, def;
484
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100485 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
Patrick Georgi0588d192009-08-12 15:00:51 +0000486 def_sym = sym_get_choice_value(sym);
487 cnt = def = 0;
488 line[0] = 0;
489 for (child = menu->list; child; child = child->next) {
490 if (!menu_is_visible(child))
491 continue;
492 if (!child->sym) {
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100493 printf("%*c %s\n", indent, '*', menu_get_prompt(child));
Patrick Georgi0588d192009-08-12 15:00:51 +0000494 continue;
495 }
496 cnt++;
497 if (child->sym == def_sym) {
498 def = cnt;
499 printf("%*c", indent, '>');
500 } else
501 printf("%*c", indent, ' ');
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100502 printf(" %d. %s", cnt, menu_get_prompt(child));
Patrick Georgi0588d192009-08-12 15:00:51 +0000503 if (child->sym->name)
504 printf(" (%s)", child->sym->name);
505 if (!sym_has_value(child->sym))
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100506 printf(" (NEW)");
Patrick Georgi0588d192009-08-12 15:00:51 +0000507 printf("\n");
508 }
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100509 printf("%*schoice", indent - 1, "");
Patrick Georgi0588d192009-08-12 15:00:51 +0000510 if (cnt == 1) {
511 printf("[1]: 1\n");
512 goto conf_childs;
513 }
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100514 printf("[1-%d?]: ", cnt);
Patrick Georgi0588d192009-08-12 15:00:51 +0000515 switch (input_mode) {
Patrick Georgid5208402014-04-11 20:24:06 +0200516 case oldconfig:
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100517 case syncconfig:
Patrick Georgi0588d192009-08-12 15:00:51 +0000518 if (!is_new) {
519 cnt = def;
520 printf("%d\n", cnt);
521 break;
522 }
Patrick Georgid5208402014-04-11 20:24:06 +0200523 /* fall through */
524 case oldaskconfig:
Patrick Georgi0588d192009-08-12 15:00:51 +0000525 fflush(stdout);
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100526 xfgets(line, sizeof(line), stdin);
Patrick Georgi0588d192009-08-12 15:00:51 +0000527 strip(line);
528 if (line[0] == '?') {
Patrick Georgid5208402014-04-11 20:24:06 +0200529 print_help(menu);
Patrick Georgi0588d192009-08-12 15:00:51 +0000530 continue;
531 }
532 if (!line[0])
533 cnt = def;
534 else if (isdigit(line[0]))
535 cnt = atoi(line);
536 else
537 continue;
538 break;
Patrick Georgid5208402014-04-11 20:24:06 +0200539 default:
Patrick Georgi0588d192009-08-12 15:00:51 +0000540 break;
541 }
542
543 conf_childs:
544 for (child = menu->list; child; child = child->next) {
545 if (!child->sym || !menu_is_visible(child))
546 continue;
547 if (!--cnt)
548 break;
549 }
550 if (!child)
551 continue;
Patrick Georgid5208402014-04-11 20:24:06 +0200552 if (line[0] && line[strlen(line) - 1] == '?') {
553 print_help(child);
Patrick Georgi0588d192009-08-12 15:00:51 +0000554 continue;
555 }
556 sym_set_choice_value(sym, child->sym);
557 for (child = child->list; child; child = child->next) {
558 indent += 2;
559 conf(child);
560 indent -= 2;
561 }
562 return 1;
563 }
564}
565
566static void conf(struct menu *menu)
567{
568 struct symbol *sym;
569 struct property *prop;
570 struct menu *child;
571
572 if (!menu_is_visible(menu))
573 return;
574
575 sym = menu->sym;
576 prop = menu->prompt;
577 if (prop) {
578 const char *prompt;
579
580 switch (prop->type) {
581 case P_MENU:
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100582 /*
583 * Except in oldaskconfig mode, we show only menus that
584 * contain new symbols.
585 */
586 if (input_mode != oldaskconfig && rootEntry != menu) {
Patrick Georgi0588d192009-08-12 15:00:51 +0000587 check_conf(menu);
588 return;
589 }
Patrick Georgid5208402014-04-11 20:24:06 +0200590 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000591 case P_COMMENT:
592 prompt = menu_get_prompt(menu);
593 if (prompt)
594 printf("%*c\n%*c %s\n%*c\n",
595 indent, '*',
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100596 indent, '*', prompt,
Patrick Georgi0588d192009-08-12 15:00:51 +0000597 indent, '*');
598 default:
599 ;
600 }
601 }
602
603 if (!sym)
604 goto conf_childs;
605
606 if (sym_is_choice(sym)) {
607 conf_choice(menu);
608 if (sym->curr.tri != mod)
609 return;
610 goto conf_childs;
611 }
612
613 switch (sym->type) {
614 case S_INT:
615 case S_HEX:
616 case S_STRING:
617 conf_string(menu);
618 break;
619 default:
620 conf_sym(menu);
621 break;
622 }
623
624conf_childs:
625 if (sym)
626 indent += 2;
627 for (child = menu->list; child; child = child->next)
628 conf(child);
629 if (sym)
630 indent -= 2;
631}
632
633static void check_conf(struct menu *menu)
634{
635 struct symbol *sym;
636 struct menu *child;
637
638 if (!menu_is_visible(menu))
639 return;
640
641 sym = menu->sym;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100642 if (sym && !sym_has_value(sym) &&
643 (sym_is_changeable(sym) ||
644 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes))) {
645
646 switch (input_mode) {
647 case listnewconfig:
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200648 if (sym->name)
649 print_symbol_for_listconfig(sym);
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100650 break;
651 case helpnewconfig:
652 printf("-----\n");
653 print_help(menu);
654 printf("-----\n");
655 break;
656 default:
657 if (!conf_cnt++)
658 printf("*\n* Restart config...\n*\n");
659 rootEntry = menu_get_parent_menu(menu);
660 conf(rootEntry);
661 break;
Patrick Georgi0588d192009-08-12 15:00:51 +0000662 }
663 }
664
665 for (child = menu->list; child; child = child->next)
666 check_conf(child);
667}
668
Patrick Georgi9f7c78b2021-09-23 14:15:40 +0200669static const struct option long_opts[] = {
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100670 {"help", no_argument, NULL, 'h'},
671 {"silent", no_argument, NULL, 's'},
672 {"oldaskconfig", no_argument, &input_mode_opt, oldaskconfig},
673 {"oldconfig", no_argument, &input_mode_opt, oldconfig},
674 {"syncconfig", no_argument, &input_mode_opt, syncconfig},
675 {"defconfig", required_argument, &input_mode_opt, defconfig},
676 {"savedefconfig", required_argument, &input_mode_opt, savedefconfig},
677 {"allnoconfig", no_argument, &input_mode_opt, allnoconfig},
678 {"allyesconfig", no_argument, &input_mode_opt, allyesconfig},
679 {"allmodconfig", no_argument, &input_mode_opt, allmodconfig},
680 {"alldefconfig", no_argument, &input_mode_opt, alldefconfig},
681 {"randconfig", no_argument, &input_mode_opt, randconfig},
682 {"listnewconfig", no_argument, &input_mode_opt, listnewconfig},
683 {"helpnewconfig", no_argument, &input_mode_opt, helpnewconfig},
684 {"olddefconfig", no_argument, &input_mode_opt, olddefconfig},
685 {"yes2modconfig", no_argument, &input_mode_opt, yes2modconfig},
686 {"mod2yesconfig", no_argument, &input_mode_opt, mod2yesconfig},
Patrick Georgi7eb03cb2022-10-28 01:00:26 +0200687 {"mod2noconfig", no_argument, &input_mode_opt, mod2noconfig},
Patrick Georgid5208402014-04-11 20:24:06 +0200688 {NULL, 0, NULL, 0}
689};
690
691static void conf_usage(const char *progname)
692{
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100693 printf("Usage: %s [options] <kconfig-file>\n", progname);
694 printf("\n");
695 printf("Generic options:\n");
696 printf(" -h, --help Print this message and exit.\n");
697 printf(" -s, --silent Do not print log.\n");
698 printf("\n");
699 printf("Mode options:\n");
Patrick Georgid5208402014-04-11 20:24:06 +0200700 printf(" --listnewconfig List new options\n");
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100701 printf(" --helpnewconfig List new options and help text\n");
Patrick Georgid5208402014-04-11 20:24:06 +0200702 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
703 printf(" --oldconfig Update a configuration using a provided .config as base\n");
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100704 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
705 " include/{generated/,config/}\n");
706 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
Patrick Georgid5208402014-04-11 20:24:06 +0200707 printf(" --defconfig <file> New config with default defined in <file>\n");
708 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
709 printf(" --allnoconfig New config where all options are answered with no\n");
710 printf(" --allyesconfig New config where all options are answered with yes\n");
711 printf(" --allmodconfig New config where all options are answered with mod\n");
712 printf(" --alldefconfig New config with all symbols set to default\n");
713 printf(" --randconfig New config with random answer to all options\n");
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100714 printf(" --yes2modconfig Change answers from yes to mod if possible\n");
715 printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
Patrick Georgi7eb03cb2022-10-28 01:00:26 +0200716 printf(" --mod2noconfig Change answers from mod to no if possible\n");
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100717 printf(" (If none of the above is given, --oldaskconfig is the default)\n");
Patrick Georgid5208402014-04-11 20:24:06 +0200718}
719
Patrick Georgi0588d192009-08-12 15:00:51 +0000720int main(int ac, char **av)
721{
Patrick Georgid5208402014-04-11 20:24:06 +0200722 const char *progname = av[0];
Patrick Georgi0588d192009-08-12 15:00:51 +0000723 int opt;
Patrick Georgid5208402014-04-11 20:24:06 +0200724 const char *name, *defconfig_file = NULL /* gcc uninit */;
Stefan Reinauer57a31312015-08-20 11:19:34 -0700725 char *env;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100726 int no_conf_write = 0;
Patrick Georgi0588d192009-08-12 15:00:51 +0000727
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100728 tty_stdio = isatty(0) && isatty(1);
Patrick Georgi0588d192009-08-12 15:00:51 +0000729
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100730 while ((opt = getopt_long(ac, av, "hs", long_opts, NULL)) != -1) {
Patrick Georgi0588d192009-08-12 15:00:51 +0000731 switch (opt) {
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100732 case 'h':
Patrick Georgid5208402014-04-11 20:24:06 +0200733 conf_usage(progname);
Patrick Georgi0588d192009-08-12 15:00:51 +0000734 exit(1);
Patrick Georgid5208402014-04-11 20:24:06 +0200735 break;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100736 case 's':
737 conf_set_message_callback(NULL);
738 break;
739 case 0:
740 input_mode = input_mode_opt;
741 switch (input_mode) {
742 case syncconfig:
743 /*
744 * syncconfig is invoked during the build stage.
745 * Suppress distracting
746 * "configuration written to ..."
747 */
748 conf_set_message_callback(NULL);
749 sync_kconfig = 1;
750 break;
751 case defconfig:
752 case savedefconfig:
753 defconfig_file = optarg;
754 break;
755 case randconfig:
756 set_randconfig_seed();
757 break;
758 default:
759 break;
760 }
761 default:
762 break;
Patrick Georgi0588d192009-08-12 15:00:51 +0000763 }
764 }
765 if (ac == optind) {
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100766 fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
Patrick Georgid5208402014-04-11 20:24:06 +0200767 conf_usage(progname);
Patrick Georgi0588d192009-08-12 15:00:51 +0000768 exit(1);
769 }
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100770 conf_parse(av[optind]);
Patrick Georgi0588d192009-08-12 15:00:51 +0000771 //zconfdump(stdout);
Patrick Georgid5208402014-04-11 20:24:06 +0200772
Patrick Georgi0588d192009-08-12 15:00:51 +0000773 switch (input_mode) {
Patrick Georgid5208402014-04-11 20:24:06 +0200774 case defconfig:
Patrick Georgi0588d192009-08-12 15:00:51 +0000775 if (conf_read(defconfig_file)) {
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100776 fprintf(stderr,
777 "***\n"
778 "*** Can't find default configuration \"%s\"!\n"
779 "***\n",
780 defconfig_file);
Patrick Georgi0588d192009-08-12 15:00:51 +0000781 exit(1);
782 }
783 break;
Patrick Georgid5208402014-04-11 20:24:06 +0200784 case savedefconfig:
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100785 case syncconfig:
Patrick Georgid5208402014-04-11 20:24:06 +0200786 case oldaskconfig:
787 case oldconfig:
788 case listnewconfig:
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100789 case helpnewconfig:
Patrick Georgid5208402014-04-11 20:24:06 +0200790 case olddefconfig:
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100791 case yes2modconfig:
792 case mod2yesconfig:
Patrick Georgi7eb03cb2022-10-28 01:00:26 +0200793 case mod2noconfig:
Patrick Georgi0588d192009-08-12 15:00:51 +0000794 conf_read(NULL);
795 break;
Patrick Georgid5208402014-04-11 20:24:06 +0200796 case allnoconfig:
797 case allyesconfig:
798 case allmodconfig:
799 case alldefconfig:
800 case randconfig:
Patrick Georgi0588d192009-08-12 15:00:51 +0000801 name = getenv("KCONFIG_ALLCONFIG");
Patrick Georgid5208402014-04-11 20:24:06 +0200802 if (!name)
803 break;
804 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
805 if (conf_read_simple(name, S_DEF_USER)) {
806 fprintf(stderr,
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100807 "*** Can't read seed configuration \"%s\"!\n",
Patrick Georgid5208402014-04-11 20:24:06 +0200808 name);
809 exit(1);
810 }
Patrick Georgi0588d192009-08-12 15:00:51 +0000811 break;
812 }
813 switch (input_mode) {
Patrick Georgid5208402014-04-11 20:24:06 +0200814 case allnoconfig: name = "allno.config"; break;
815 case allyesconfig: name = "allyes.config"; break;
816 case allmodconfig: name = "allmod.config"; break;
817 case alldefconfig: name = "alldef.config"; break;
818 case randconfig: name = "allrandom.config"; break;
Patrick Georgi0588d192009-08-12 15:00:51 +0000819 default: break;
820 }
Patrick Georgid5208402014-04-11 20:24:06 +0200821 if (conf_read_simple(name, S_DEF_USER) &&
822 conf_read_simple("all.config", S_DEF_USER)) {
823 fprintf(stderr,
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100824 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
Patrick Georgid5208402014-04-11 20:24:06 +0200825 name);
826 exit(1);
827 }
Patrick Georgi0588d192009-08-12 15:00:51 +0000828 break;
829 default:
830 break;
831 }
832
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100833 env = getenv("KCONFIG_STRICT");
834 if (env && *env && kconfig_warnings) {
835 fprintf(stderr, "\n*** ERROR: %d warnings encountered, and "
836 "warnings are errors.\n\n", kconfig_warnings);
837 exit(1);
838 }
839
Patrick Georgid5208402014-04-11 20:24:06 +0200840 if (sync_kconfig) {
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100841 name = getenv("KCONFIG_NOSILENTUPDATE");
842 if (name && *name) {
843 if (conf_get_changed()) {
Patrick Georgid5208402014-04-11 20:24:06 +0200844 fprintf(stderr,
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100845 "\n*** The configuration requires explicit update.\n\n");
Patrick Georgid5208402014-04-11 20:24:06 +0200846 return 1;
847 }
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100848 no_conf_write = 1;
Patrick Georgid5208402014-04-11 20:24:06 +0200849 }
Patrick Georgid5208402014-04-11 20:24:06 +0200850 }
851
852 switch (input_mode) {
853 case allnoconfig:
854 conf_set_all_new_symbols(def_no);
855 break;
856 case allyesconfig:
857 conf_set_all_new_symbols(def_yes);
858 break;
859 case allmodconfig:
860 conf_set_all_new_symbols(def_mod);
861 break;
862 case alldefconfig:
863 conf_set_all_new_symbols(def_default);
864 break;
865 case randconfig:
866 /* Really nothing to do in this loop */
867 while (conf_set_all_new_symbols(def_random)) ;
868 break;
869 case defconfig:
870 conf_set_all_new_symbols(def_default);
871 break;
872 case savedefconfig:
873 break;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100874 case yes2modconfig:
Patrick Georgi7eb03cb2022-10-28 01:00:26 +0200875 conf_rewrite_tristates(yes, mod);
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100876 break;
877 case mod2yesconfig:
Patrick Georgi7eb03cb2022-10-28 01:00:26 +0200878 conf_rewrite_tristates(mod, yes);
879 break;
880 case mod2noconfig:
881 conf_rewrite_tristates(mod, no);
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100882 break;
Patrick Georgid5208402014-04-11 20:24:06 +0200883 case oldaskconfig:
Patrick Georgi0588d192009-08-12 15:00:51 +0000884 rootEntry = &rootmenu;
885 conf(&rootmenu);
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100886 input_mode = oldconfig;
Patrick Georgid5208402014-04-11 20:24:06 +0200887 /* fall through */
888 case oldconfig:
889 case listnewconfig:
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100890 case helpnewconfig:
891 case syncconfig:
Patrick Georgid5208402014-04-11 20:24:06 +0200892 /* Update until a loop caused no more changes */
893 do {
894 conf_cnt = 0;
895 check_conf(&rootmenu);
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100896 } while (conf_cnt);
897 break;
898 case olddefconfig:
899 default:
Patrick Georgid5208402014-04-11 20:24:06 +0200900 break;
901 }
902
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100903 if (input_mode == savedefconfig) {
Patrick Georgid5208402014-04-11 20:24:06 +0200904 if (conf_write_defconfig(defconfig_file)) {
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100905 fprintf(stderr, "\n*** Error while saving defconfig to: %s\n\n",
Stefan Reinauer3ec23b32015-04-06 00:59:23 +0200906 defconfig_file);
Patrick Georgid5208402014-04-11 20:24:06 +0200907 return 1;
908 }
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100909 } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
910 if (!no_conf_write && conf_write(NULL)) {
911 fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
Patrick Georgid5208402014-04-11 20:24:06 +0200912 exit(1);
913 }
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100914
915 /*
916 * Create auto.conf if it does not exist.
917 * This prevents GNU Make 4.1 or older from emitting
918 * "include/config/auto.conf: No such file or directory"
919 * in the top-level Makefile
920 *
921 * syncconfig always creates or updates auto.conf because it is
922 * used during the build.
923 */
924 if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
925 fprintf(stderr,
926 "\n*** Error during sync of the configuration.\n\n");
927 return 1;
928 }
Patrick Georgi0588d192009-08-12 15:00:51 +0000929 }
Patrick Georgi0588d192009-08-12 15:00:51 +0000930 return 0;
931}