blob: 3a1ae8d1fd71bbfbed3d3938769243bf68f1dac8 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
4 */
5
6#include <ctype.h>
7#include <limits.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <time.h>
12#include <unistd.h>
13#include <getopt.h>
14#include <sys/stat.h>
15#include <sys/time.h>
16#include <errno.h>
17
18#include "lkc.h"
19
20static void conf(struct menu *menu);
21static void check_conf(struct menu *menu);
22
23enum input_mode {
24 oldaskconfig,
25 syncconfig,
26 oldconfig,
27 allnoconfig,
28 allyesconfig,
29 allmodconfig,
30 alldefconfig,
31 randconfig,
32 defconfig,
33 savedefconfig,
34 listnewconfig,
35 olddefconfig,
36};
37static enum input_mode input_mode = oldaskconfig;
38
39static int indent = 1;
40static int tty_stdio;
41static int valid_stdin = 1;
42static int sync_kconfig;
43static int conf_cnt;
44static char line[PATH_MAX];
45static struct menu *rootEntry;
46
47static void print_help(struct menu *menu)
48{
49 struct gstr help = str_new();
50
51 menu_get_ext_help(menu, &help);
52
53 printf("\n%s\n", str_get(&help));
54 str_free(&help);
55}
56
57static void strip(char *str)
58{
59 char *p = str;
60 int l;
61
62 while ((isspace(*p)))
63 p++;
64 l = strlen(p);
65 if (p != str)
66 memmove(str, p, l + 1);
67 if (!l)
68 return;
69 p = str + l - 1;
70 while ((isspace(*p)))
71 *p-- = 0;
72}
73
74static void check_stdin(void)
75{
76 if (!valid_stdin) {
77 printf("aborted!\n\n");
78 printf("Console input/output is redirected. ");
79 printf("Run 'make oldconfig' to update configuration.\n\n");
80 exit(1);
81 }
82}
83
84/* Helper function to facilitate fgets() by Jean Sacren. */
85static void xfgets(char *str, int size, FILE *in)
86{
87 if (!fgets(str, size, in))
88 fprintf(stderr, "\nError in reading or end of file.\n");
89
90 if (!tty_stdio)
91 printf("%s", str);
92}
93
94static int conf_askvalue(struct symbol *sym, const char *def)
95{
96 enum symbol_type type = sym_get_type(sym);
97
98 if (!sym_has_value(sym))
99 printf("(NEW) ");
100
101 line[0] = '\n';
102 line[1] = 0;
103
104 if (!sym_is_changeable(sym)) {
105 printf("%s\n", def);
106 line[0] = '\n';
107 line[1] = 0;
108 return 0;
109 }
110
111 switch (input_mode) {
112 case oldconfig:
113 case syncconfig:
114 if (sym_has_value(sym)) {
115 printf("%s\n", def);
116 return 0;
117 }
118 if (input_mode == syncconfig)
119 check_stdin();
120 /* fall through */
121 case oldaskconfig:
122 fflush(stdout);
123 xfgets(line, sizeof(line), stdin);
124 return 1;
125 default:
126 break;
127 }
128
129 switch (type) {
130 case S_INT:
131 case S_HEX:
132 case S_STRING:
133 printf("%s\n", def);
134 return 1;
135 default:
136 ;
137 }
138 printf("%s", line);
139 return 1;
140}
141
142static int conf_string(struct menu *menu)
143{
144 struct symbol *sym = menu->sym;
145 const char *def;
146
147 while (1) {
148 printf("%*s%s ", indent - 1, "", menu->prompt->text);
149 printf("(%s) ", sym->name);
150 def = sym_get_string_value(sym);
151 if (sym_get_string_value(sym))
152 printf("[%s] ", def);
153 if (!conf_askvalue(sym, def))
154 return 0;
155 switch (line[0]) {
156 case '\n':
157 break;
158 case '?':
159 /* print help */
160 if (line[1] == '\n') {
161 print_help(menu);
162 def = NULL;
163 break;
164 }
165 /* fall through */
166 default:
167 line[strlen(line)-1] = 0;
168 def = line;
169 }
170 if (def && sym_set_string_value(sym, def))
171 return 0;
172 }
173}
174
175static int conf_sym(struct menu *menu)
176{
177 struct symbol *sym = menu->sym;
178 tristate oldval, newval;
179
180 while (1) {
181 printf("%*s%s ", indent - 1, "", menu->prompt->text);
182 if (sym->name)
183 printf("(%s) ", sym->name);
184 putchar('[');
185 oldval = sym_get_tristate_value(sym);
186 switch (oldval) {
187 case no:
188 putchar('N');
189 break;
190 case mod:
191 putchar('M');
192 break;
193 case yes:
194 putchar('Y');
195 break;
196 }
197 if (oldval != no && sym_tristate_within_range(sym, no))
198 printf("/n");
199 if (oldval != mod && sym_tristate_within_range(sym, mod))
200 printf("/m");
201 if (oldval != yes && sym_tristate_within_range(sym, yes))
202 printf("/y");
203 printf("/?] ");
204 if (!conf_askvalue(sym, sym_get_string_value(sym)))
205 return 0;
206 strip(line);
207
208 switch (line[0]) {
209 case 'n':
210 case 'N':
211 newval = no;
212 if (!line[1] || !strcmp(&line[1], "o"))
213 break;
214 continue;
215 case 'm':
216 case 'M':
217 newval = mod;
218 if (!line[1])
219 break;
220 continue;
221 case 'y':
222 case 'Y':
223 newval = yes;
224 if (!line[1] || !strcmp(&line[1], "es"))
225 break;
226 continue;
227 case 0:
228 newval = oldval;
229 break;
230 case '?':
231 goto help;
232 default:
233 continue;
234 }
235 if (sym_set_tristate_value(sym, newval))
236 return 0;
237help:
238 print_help(menu);
239 }
240}
241
242static int conf_choice(struct menu *menu)
243{
244 struct symbol *sym, *def_sym;
245 struct menu *child;
246 bool is_new;
247
248 sym = menu->sym;
249 is_new = !sym_has_value(sym);
250 if (sym_is_changeable(sym)) {
251 conf_sym(menu);
252 sym_calc_value(sym);
253 switch (sym_get_tristate_value(sym)) {
254 case no:
255 return 1;
256 case mod:
257 return 0;
258 case yes:
259 break;
260 }
261 } else {
262 switch (sym_get_tristate_value(sym)) {
263 case no:
264 return 1;
265 case mod:
266 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
267 return 0;
268 case yes:
269 break;
270 }
271 }
272
273 while (1) {
274 int cnt, def;
275
276 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
277 def_sym = sym_get_choice_value(sym);
278 cnt = def = 0;
279 line[0] = 0;
280 for (child = menu->list; child; child = child->next) {
281 if (!menu_is_visible(child))
282 continue;
283 if (!child->sym) {
284 printf("%*c %s\n", indent, '*', menu_get_prompt(child));
285 continue;
286 }
287 cnt++;
288 if (child->sym == def_sym) {
289 def = cnt;
290 printf("%*c", indent, '>');
291 } else
292 printf("%*c", indent, ' ');
293 printf(" %d. %s", cnt, menu_get_prompt(child));
294 if (child->sym->name)
295 printf(" (%s)", child->sym->name);
296 if (!sym_has_value(child->sym))
297 printf(" (NEW)");
298 printf("\n");
299 }
300 printf("%*schoice", indent - 1, "");
301 if (cnt == 1) {
302 printf("[1]: 1\n");
303 goto conf_childs;
304 }
305 printf("[1-%d?]: ", cnt);
306 switch (input_mode) {
307 case oldconfig:
308 case syncconfig:
309 if (!is_new) {
310 cnt = def;
311 printf("%d\n", cnt);
312 break;
313 }
314 if (input_mode == syncconfig)
315 check_stdin();
316 /* fall through */
317 case oldaskconfig:
318 fflush(stdout);
319 xfgets(line, sizeof(line), stdin);
320 strip(line);
321 if (line[0] == '?') {
322 print_help(menu);
323 continue;
324 }
325 if (!line[0])
326 cnt = def;
327 else if (isdigit(line[0]))
328 cnt = atoi(line);
329 else
330 continue;
331 break;
332 default:
333 break;
334 }
335
336 conf_childs:
337 for (child = menu->list; child; child = child->next) {
338 if (!child->sym || !menu_is_visible(child))
339 continue;
340 if (!--cnt)
341 break;
342 }
343 if (!child)
344 continue;
345 if (line[0] && line[strlen(line) - 1] == '?') {
346 print_help(child);
347 continue;
348 }
349 sym_set_choice_value(sym, child->sym);
350 for (child = child->list; child; child = child->next) {
351 indent += 2;
352 conf(child);
353 indent -= 2;
354 }
355 return 1;
356 }
357}
358
359static void conf(struct menu *menu)
360{
361 struct symbol *sym;
362 struct property *prop;
363 struct menu *child;
364
365 if (!menu_is_visible(menu))
366 return;
367
368 sym = menu->sym;
369 prop = menu->prompt;
370 if (prop) {
371 const char *prompt;
372
373 switch (prop->type) {
374 case P_MENU:
375 /*
376 * Except in oldaskconfig mode, we show only menus that
377 * contain new symbols.
378 */
379 if (input_mode != oldaskconfig && rootEntry != menu) {
380 check_conf(menu);
381 return;
382 }
383 /* fall through */
384 case P_COMMENT:
385 prompt = menu_get_prompt(menu);
386 if (prompt)
387 printf("%*c\n%*c %s\n%*c\n",
388 indent, '*',
389 indent, '*', prompt,
390 indent, '*');
391 default:
392 ;
393 }
394 }
395
396 if (!sym)
397 goto conf_childs;
398
399 if (sym_is_choice(sym)) {
400 conf_choice(menu);
401 if (sym->curr.tri != mod)
402 return;
403 goto conf_childs;
404 }
405
406 switch (sym->type) {
407 case S_INT:
408 case S_HEX:
409 case S_STRING:
410 conf_string(menu);
411 break;
412 default:
413 conf_sym(menu);
414 break;
415 }
416
417conf_childs:
418 if (sym)
419 indent += 2;
420 for (child = menu->list; child; child = child->next)
421 conf(child);
422 if (sym)
423 indent -= 2;
424}
425
426static void check_conf(struct menu *menu)
427{
428 struct symbol *sym;
429 struct menu *child;
430
431 if (!menu_is_visible(menu))
432 return;
433
434 sym = menu->sym;
435 if (sym && !sym_has_value(sym)) {
436 valid_stdin = tty_stdio;
437 if (sym_is_changeable(sym) ||
438 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
439 if (input_mode == listnewconfig) {
440 if (sym->name) {
441 const char *str;
442
443 if (sym->type == S_STRING) {
444 str = sym_get_string_value(sym);
445 str = sym_escape_string_value(str);
446 printf("%s%s=%s\n", CONFIG_, sym->name, str);
447 free((void *)str);
448 } else {
449 str = sym_get_string_value(sym);
450 printf("%s%s=%s\n", CONFIG_, sym->name, str);
451 }
452 }
453 } else {
454 if (!conf_cnt++)
455 printf("*\n* Restart config...\n*\n");
456 rootEntry = menu_get_parent_menu(menu);
457 conf(rootEntry);
458 }
459 }
460 }
461
462 for (child = menu->list; child; child = child->next)
463 check_conf(child);
464}
465
466static struct option long_opts[] = {
467 {"oldaskconfig", no_argument, NULL, oldaskconfig},
468 {"oldconfig", no_argument, NULL, oldconfig},
469 {"syncconfig", no_argument, NULL, syncconfig},
470 {"defconfig", required_argument, NULL, defconfig},
471 {"savedefconfig", required_argument, NULL, savedefconfig},
472 {"allnoconfig", no_argument, NULL, allnoconfig},
473 {"allyesconfig", no_argument, NULL, allyesconfig},
474 {"allmodconfig", no_argument, NULL, allmodconfig},
475 {"alldefconfig", no_argument, NULL, alldefconfig},
476 {"randconfig", no_argument, NULL, randconfig},
477 {"listnewconfig", no_argument, NULL, listnewconfig},
478 {"olddefconfig", no_argument, NULL, olddefconfig},
479 {NULL, 0, NULL, 0}
480};
481
482static void conf_usage(const char *progname)
483{
484
485 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
486 printf("[option] is _one_ of the following:\n");
487 printf(" --listnewconfig List new options\n");
488 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
489 printf(" --oldconfig Update a configuration using a provided .config as base\n");
490 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
491 " include/{generated/,config/}\n");
492 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
493 printf(" --defconfig <file> New config with default defined in <file>\n");
494 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
495 printf(" --allnoconfig New config where all options are answered with no\n");
496 printf(" --allyesconfig New config where all options are answered with yes\n");
497 printf(" --allmodconfig New config where all options are answered with mod\n");
498 printf(" --alldefconfig New config with all symbols set to default\n");
499 printf(" --randconfig New config with random answer to all options\n");
500}
501
502int main(int ac, char **av)
503{
504 const char *progname = av[0];
505 int opt;
506 const char *name, *defconfig_file = NULL /* gcc uninit */;
507 int no_conf_write = 0;
508
509 tty_stdio = isatty(0) && isatty(1);
510
511 while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
512 if (opt == 's') {
513 conf_set_message_callback(NULL);
514 continue;
515 }
516 input_mode = (enum input_mode)opt;
517 switch (opt) {
518 case syncconfig:
519 /*
520 * syncconfig is invoked during the build stage.
521 * Suppress distracting "configuration written to ..."
522 */
523 conf_set_message_callback(NULL);
524 sync_kconfig = 1;
525 break;
526 case defconfig:
527 case savedefconfig:
528 defconfig_file = optarg;
529 break;
530 case randconfig:
531 {
532 struct timeval now;
533 unsigned int seed;
534 char *seed_env;
535
536 /*
537 * Use microseconds derived seed,
538 * compensate for systems where it may be zero
539 */
540 gettimeofday(&now, NULL);
541 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
542
543 seed_env = getenv("KCONFIG_SEED");
544 if( seed_env && *seed_env ) {
545 char *endp;
546 int tmp = (int)strtol(seed_env, &endp, 0);
547 if (*endp == '\0') {
548 seed = tmp;
549 }
550 }
551 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
552 srand(seed);
553 break;
554 }
555 case oldaskconfig:
556 case oldconfig:
557 case allnoconfig:
558 case allyesconfig:
559 case allmodconfig:
560 case alldefconfig:
561 case listnewconfig:
562 case olddefconfig:
563 break;
564 case '?':
565 conf_usage(progname);
566 exit(1);
567 break;
568 }
569 }
570 if (ac == optind) {
571 fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
572 conf_usage(progname);
573 exit(1);
574 }
575 name = av[optind];
576 conf_parse(name);
577 //zconfdump(stdout);
578
579 switch (input_mode) {
580 case defconfig:
581 if (conf_read(defconfig_file)) {
582 fprintf(stderr,
583 "***\n"
584 "*** Can't find default configuration \"%s\"!\n"
585 "***\n",
586 defconfig_file);
587 exit(1);
588 }
589 break;
590 case savedefconfig:
591 case syncconfig:
592 case oldaskconfig:
593 case oldconfig:
594 case listnewconfig:
595 case olddefconfig:
596 conf_read(NULL);
597 break;
598 case allnoconfig:
599 case allyesconfig:
600 case allmodconfig:
601 case alldefconfig:
602 case randconfig:
603 name = getenv("KCONFIG_ALLCONFIG");
604 if (!name)
605 break;
606 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
607 if (conf_read_simple(name, S_DEF_USER)) {
608 fprintf(stderr,
609 "*** Can't read seed configuration \"%s\"!\n",
610 name);
611 exit(1);
612 }
613 break;
614 }
615 switch (input_mode) {
616 case allnoconfig: name = "allno.config"; break;
617 case allyesconfig: name = "allyes.config"; break;
618 case allmodconfig: name = "allmod.config"; break;
619 case alldefconfig: name = "alldef.config"; break;
620 case randconfig: name = "allrandom.config"; break;
621 default: break;
622 }
623 if (conf_read_simple(name, S_DEF_USER) &&
624 conf_read_simple("all.config", S_DEF_USER)) {
625 fprintf(stderr,
626 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
627 name);
628 exit(1);
629 }
630 break;
631 default:
632 break;
633 }
634
635 if (sync_kconfig) {
636 name = getenv("KCONFIG_NOSILENTUPDATE");
637 if (name && *name) {
638 if (conf_get_changed()) {
639 fprintf(stderr,
640 "\n*** The configuration requires explicit update.\n\n");
641 return 1;
642 }
643 no_conf_write = 1;
644 }
645 }
646
647 switch (input_mode) {
648 case allnoconfig:
649 conf_set_all_new_symbols(def_no);
650 break;
651 case allyesconfig:
652 conf_set_all_new_symbols(def_yes);
653 break;
654 case allmodconfig:
655 conf_set_all_new_symbols(def_mod);
656 break;
657 case alldefconfig:
658 conf_set_all_new_symbols(def_default);
659 break;
660 case randconfig:
661 /* Really nothing to do in this loop */
662 while (conf_set_all_new_symbols(def_random)) ;
663 break;
664 case defconfig:
665 conf_set_all_new_symbols(def_default);
666 break;
667 case savedefconfig:
668 break;
669 case oldaskconfig:
670 rootEntry = &rootmenu;
671 conf(&rootmenu);
672 input_mode = oldconfig;
673 /* fall through */
674 case oldconfig:
675 case listnewconfig:
676 case syncconfig:
677 /* Update until a loop caused no more changes */
678 do {
679 conf_cnt = 0;
680 check_conf(&rootmenu);
681 } while (conf_cnt);
682 break;
683 case olddefconfig:
684 default:
685 break;
686 }
687
688 if (input_mode == savedefconfig) {
689 if (conf_write_defconfig(defconfig_file)) {
690 fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
691 defconfig_file);
692 return 1;
693 }
694 } else if (input_mode != listnewconfig) {
695 if (!no_conf_write && conf_write(NULL)) {
696 fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
697 exit(1);
698 }
699
700 /*
701 * Create auto.conf if it does not exist.
702 * This prevents GNU Make 4.1 or older from emitting
703 * "include/config/auto.conf: No such file or directory"
704 * in the top-level Makefile
705 *
706 * syncconfig always creates or updates auto.conf because it is
707 * used during the build.
708 */
709 if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
710 fprintf(stderr,
711 "\n*** Error during sync of the configuration.\n\n");
712 return 1;
713 }
714 }
715 return 0;
716}