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