skylake: update processor power limits configuration
[coreboot.git] / util / kconfig / conf.c
blob7aae1c7b578c11a97310b113ad237f4081170e81
1 /*
2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
4 */
6 #include <locale.h>
7 #include <ctype.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>
18 #include "lkc.h"
20 int kconfig_warnings = 0;
22 static void conf(struct menu *menu);
23 static void check_conf(struct menu *menu);
24 static void xfgets(char *str, int size, FILE *in);
26 enum input_mode {
27 oldaskconfig,
28 silentoldconfig,
29 oldconfig,
30 allnoconfig,
31 allyesconfig,
32 allmodconfig,
33 alldefconfig,
34 randconfig,
35 defconfig,
36 savedefconfig,
37 listnewconfig,
38 olddefconfig,
39 } input_mode = oldaskconfig;
41 static int indent = 1;
42 static int tty_stdio;
43 static int valid_stdin = 1;
44 static int sync_kconfig;
45 static int conf_cnt;
46 static char line[128];
47 static struct menu *rootEntry;
49 static void print_help(struct menu *menu)
51 struct gstr help = str_new();
53 menu_get_ext_help(menu, &help);
55 printf("\n%s\n", str_get(&help));
56 str_free(&help);
59 static void strip(char *str)
61 char *p = str;
62 int l;
64 while ((isspace(*p)))
65 p++;
66 l = strlen(p);
67 if (p != str)
68 memmove(str, p, l + 1);
69 if (!l)
70 return;
71 p = str + l - 1;
72 while ((isspace(*p)))
73 *p-- = 0;
76 static void check_stdin(void)
78 if (!valid_stdin) {
79 printf(_("aborted!\n\n"));
80 printf(_("Console input/output is redirected. "));
81 printf(_("Run 'make oldconfig' to update configuration.\n\n"));
82 exit(1);
86 static int conf_askvalue(struct symbol *sym, const char *def)
88 enum symbol_type type = sym_get_type(sym);
90 if (!sym_has_value(sym))
91 printf(_("(NEW) "));
93 line[0] = '\n';
94 line[1] = 0;
96 if (!sym_is_changable(sym)) {
97 printf("%s\n", def);
98 line[0] = '\n';
99 line[1] = 0;
100 return 0;
103 switch (input_mode) {
104 case oldconfig:
105 case silentoldconfig:
106 if (sym_has_value(sym)) {
107 printf("%s\n", def);
108 return 0;
110 check_stdin();
111 /* fall through */
112 case oldaskconfig:
113 fflush(stdout);
114 xfgets(line, 128, stdin);
115 if (!tty_stdio)
116 printf("\n");
117 return 1;
118 default:
119 break;
122 switch (type) {
123 case S_INT:
124 case S_HEX:
125 case S_STRING:
126 printf("%s\n", def);
127 return 1;
128 default:
131 printf("%s", line);
132 return 1;
135 static int conf_string(struct menu *menu)
137 struct symbol *sym = menu->sym;
138 const char *def;
140 while (1) {
141 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
142 printf("(%s) ", sym->name);
143 def = sym_get_string_value(sym);
144 if (sym_get_string_value(sym))
145 printf("[%s] ", def);
146 if (!conf_askvalue(sym, def))
147 return 0;
148 switch (line[0]) {
149 case '\n':
150 break;
151 case '?':
152 /* print help */
153 if (line[1] == '\n') {
154 print_help(menu);
155 def = NULL;
156 break;
158 /* fall through */
159 default:
160 line[strlen(line)-1] = 0;
161 def = line;
163 if (def && sym_set_string_value(sym, def))
164 return 0;
168 static int conf_sym(struct menu *menu)
170 struct symbol *sym = menu->sym;
171 tristate oldval, newval;
173 while (1) {
174 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
175 if (sym->name)
176 printf("(%s) ", sym->name);
177 putchar('[');
178 oldval = sym_get_tristate_value(sym);
179 switch (oldval) {
180 case no:
181 putchar('N');
182 break;
183 case mod:
184 putchar('M');
185 break;
186 case yes:
187 putchar('Y');
188 break;
190 if (oldval != no && sym_tristate_within_range(sym, no))
191 printf("/n");
192 if (oldval != mod && sym_tristate_within_range(sym, mod))
193 printf("/m");
194 if (oldval != yes && sym_tristate_within_range(sym, yes))
195 printf("/y");
196 if (menu_has_help(menu))
197 printf("/?");
198 printf("] ");
199 if (!conf_askvalue(sym, sym_get_string_value(sym)))
200 return 0;
201 strip(line);
203 switch (line[0]) {
204 case 'n':
205 case 'N':
206 newval = no;
207 if (!line[1] || !strcmp(&line[1], "o"))
208 break;
209 continue;
210 case 'm':
211 case 'M':
212 newval = mod;
213 if (!line[1])
214 break;
215 continue;
216 case 'y':
217 case 'Y':
218 newval = yes;
219 if (!line[1] || !strcmp(&line[1], "es"))
220 break;
221 continue;
222 case 0:
223 newval = oldval;
224 break;
225 case '?':
226 goto help;
227 default:
228 continue;
230 if (sym_set_tristate_value(sym, newval))
231 return 0;
232 help:
233 print_help(menu);
237 static int conf_choice(struct menu *menu)
239 struct symbol *sym, *def_sym;
240 struct menu *child;
241 bool is_new;
243 sym = menu->sym;
244 is_new = !sym_has_value(sym);
245 if (sym_is_changable(sym)) {
246 conf_sym(menu);
247 sym_calc_value(sym);
248 switch (sym_get_tristate_value(sym)) {
249 case no:
250 return 1;
251 case mod:
252 return 0;
253 case yes:
254 break;
256 } else {
257 switch (sym_get_tristate_value(sym)) {
258 case no:
259 return 1;
260 case mod:
261 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
262 return 0;
263 case yes:
264 break;
268 while (1) {
269 int cnt, def;
271 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
272 def_sym = sym_get_choice_value(sym);
273 cnt = def = 0;
274 line[0] = 0;
275 for (child = menu->list; child; child = child->next) {
276 if (!menu_is_visible(child))
277 continue;
278 if (!child->sym) {
279 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
280 continue;
282 cnt++;
283 if (child->sym == def_sym) {
284 def = cnt;
285 printf("%*c", indent, '>');
286 } else
287 printf("%*c", indent, ' ');
288 printf(" %d. %s", cnt, _(menu_get_prompt(child)));
289 if (child->sym->name)
290 printf(" (%s)", child->sym->name);
291 if (!sym_has_value(child->sym))
292 printf(_(" (NEW)"));
293 printf("\n");
295 printf(_("%*schoice"), indent - 1, "");
296 if (cnt == 1) {
297 printf("[1]: 1\n");
298 goto conf_childs;
300 printf("[1-%d", cnt);
301 if (menu_has_help(menu))
302 printf("?");
303 printf("]: ");
304 switch (input_mode) {
305 case oldconfig:
306 case silentoldconfig:
307 if (!is_new) {
308 cnt = def;
309 printf("%d\n", cnt);
310 break;
312 check_stdin();
313 /* fall through */
314 case oldaskconfig:
315 fflush(stdout);
316 xfgets(line, 128, stdin);
317 strip(line);
318 if (line[0] == '?') {
319 print_help(menu);
320 continue;
322 if (!line[0])
323 cnt = def;
324 else if (isdigit(line[0]))
325 cnt = atoi(line);
326 else
327 continue;
328 break;
329 default:
330 break;
333 conf_childs:
334 for (child = menu->list; child; child = child->next) {
335 if (!child->sym || !menu_is_visible(child))
336 continue;
337 if (!--cnt)
338 break;
340 if (!child)
341 continue;
342 if (line[0] && line[strlen(line) - 1] == '?') {
343 print_help(child);
344 continue;
346 sym_set_choice_value(sym, child->sym);
347 for (child = child->list; child; child = child->next) {
348 indent += 2;
349 conf(child);
350 indent -= 2;
352 return 1;
356 static void conf(struct menu *menu)
358 struct symbol *sym;
359 struct property *prop;
360 struct menu *child;
362 if (!menu_is_visible(menu))
363 return;
365 sym = menu->sym;
366 prop = menu->prompt;
367 if (prop) {
368 const char *prompt;
370 switch (prop->type) {
371 case P_MENU:
372 if ((input_mode == silentoldconfig ||
373 input_mode == listnewconfig ||
374 input_mode == olddefconfig) &&
375 rootEntry != menu) {
376 check_conf(menu);
377 return;
379 /* fall through */
380 case P_COMMENT:
381 prompt = menu_get_prompt(menu);
382 if (prompt)
383 printf("%*c\n%*c %s\n%*c\n",
384 indent, '*',
385 indent, '*', _(prompt),
386 indent, '*');
387 default:
392 if (!sym)
393 goto conf_childs;
395 if (sym_is_choice(sym)) {
396 conf_choice(menu);
397 if (sym->curr.tri != mod)
398 return;
399 goto conf_childs;
402 switch (sym->type) {
403 case S_INT:
404 case S_HEX:
405 case S_STRING:
406 conf_string(menu);
407 break;
408 default:
409 conf_sym(menu);
410 break;
413 conf_childs:
414 if (sym)
415 indent += 2;
416 for (child = menu->list; child; child = child->next)
417 conf(child);
418 if (sym)
419 indent -= 2;
422 static void check_conf(struct menu *menu)
424 struct symbol *sym;
425 struct menu *child;
427 if (!menu_is_visible(menu))
428 return;
430 sym = menu->sym;
431 if (sym && !sym_has_value(sym)) {
432 if (sym_is_changable(sym) ||
433 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
434 if (input_mode == listnewconfig) {
435 if (sym->name && !sym_is_choice_value(sym)) {
436 printf("%s%s\n", CONFIG_, sym->name);
438 } else if (input_mode != olddefconfig) {
439 if (!conf_cnt++)
440 printf(_("*\n* Restart config...\n*\n"));
441 rootEntry = menu_get_parent_menu(menu);
442 conf(rootEntry);
447 for (child = menu->list; child; child = child->next)
448 check_conf(child);
451 static struct option long_opts[] = {
452 {"oldaskconfig", no_argument, NULL, oldaskconfig},
453 {"oldconfig", no_argument, NULL, oldconfig},
454 {"silentoldconfig", no_argument, NULL, silentoldconfig},
455 {"defconfig", optional_argument, NULL, defconfig},
456 {"savedefconfig", required_argument, NULL, savedefconfig},
457 {"allnoconfig", no_argument, NULL, allnoconfig},
458 {"allyesconfig", no_argument, NULL, allyesconfig},
459 {"allmodconfig", no_argument, NULL, allmodconfig},
460 {"alldefconfig", no_argument, NULL, alldefconfig},
461 {"randconfig", no_argument, NULL, randconfig},
462 {"listnewconfig", no_argument, NULL, listnewconfig},
463 {"olddefconfig", no_argument, NULL, olddefconfig},
465 * oldnoconfig is an alias of olddefconfig, because people already
466 * are dependent on its behavior(sets new symbols to their default
467 * value but not 'n') with the counter-intuitive name.
469 {"oldnoconfig", no_argument, NULL, olddefconfig},
470 {NULL, 0, NULL, 0}
473 static void conf_usage(const char *progname)
476 printf("Usage: %s [option] <kconfig-file>\n", progname);
477 printf("[option] is _one_ of the following:\n");
478 printf(" --listnewconfig List new options\n");
479 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
480 printf(" --oldconfig Update a configuration using a provided .config as base\n");
481 printf(" --silentoldconfig Same as oldconfig, but quietly, additionally update deps\n");
482 printf(" --olddefconfig Same as silentoldconfig but sets new symbols to their default value\n");
483 printf(" --oldnoconfig An alias of olddefconfig\n");
484 printf(" --defconfig <file> New config with default defined in <file>\n");
485 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
486 printf(" --allnoconfig New config where all options are answered with no\n");
487 printf(" --allyesconfig New config where all options are answered with yes\n");
488 printf(" --allmodconfig New config where all options are answered with mod\n");
489 printf(" --alldefconfig New config with all symbols set to default\n");
490 printf(" --randconfig New config with random answer to all options\n");
493 int main(int ac, char **av)
495 const char *progname = av[0];
496 int opt;
497 const char *name, *defconfig_file = NULL /* gcc uninit */;
498 char *env;
499 struct stat tmpstat;
501 setlocale(LC_ALL, "");
502 bindtextdomain(PACKAGE, LOCALEDIR);
503 textdomain(PACKAGE);
505 tty_stdio = isatty(0) && isatty(1) && isatty(2);
507 while ((opt = getopt_long(ac, av, "", long_opts, NULL)) != -1) {
508 input_mode = (enum input_mode)opt;
509 switch (opt) {
510 case silentoldconfig:
511 sync_kconfig = 1;
512 break;
513 case defconfig:
514 case savedefconfig:
515 defconfig_file = optarg;
516 break;
517 case randconfig:
519 struct timeval now;
520 unsigned int seed;
521 char *seed_env;
524 * Use microseconds derived seed,
525 * compensate for systems where it may be zero
527 gettimeofday(&now, NULL);
528 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
530 seed_env = getenv("KCONFIG_SEED");
531 if( seed_env && *seed_env ) {
532 char *endp;
533 int tmp = (int)strtol(seed_env, &endp, 0);
534 if (*endp == '\0') {
535 seed = tmp;
538 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
539 srand(seed);
540 break;
542 case oldaskconfig:
543 case oldconfig:
544 case allnoconfig:
545 case allyesconfig:
546 case allmodconfig:
547 case alldefconfig:
548 case listnewconfig:
549 case olddefconfig:
550 break;
551 case '?':
552 conf_usage(progname);
553 exit(1);
554 break;
557 if (ac == optind) {
558 printf(_("%s: Kconfig file missing\n"), av[0]);
559 conf_usage(progname);
560 exit(1);
562 name = av[optind];
563 conf_parse(name);
564 //zconfdump(stdout);
565 if (sync_kconfig) {
566 name = conf_get_configname();
567 if (stat(name, &tmpstat)) {
568 fprintf(stderr, _("***\n"
569 "*** Configuration file \"%s\" not found!\n"
570 "***\n"
571 "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
572 "*** \"make menuconfig\" or \"make xconfig\").\n"
573 "***\n"), name);
574 exit(1);
578 switch (input_mode) {
579 case defconfig:
580 if (!defconfig_file)
581 defconfig_file = conf_get_default_confname();
582 if (conf_read(defconfig_file)) {
583 printf(_("***\n"
584 "*** Can't find default configuration \"%s\"!\n"
585 "***\n"), defconfig_file);
586 exit(1);
588 break;
589 case savedefconfig:
590 case silentoldconfig:
591 case oldaskconfig:
592 case oldconfig:
593 case listnewconfig:
594 case olddefconfig:
595 conf_read(NULL);
596 break;
597 case allnoconfig:
598 case allyesconfig:
599 case allmodconfig:
600 case alldefconfig:
601 case randconfig:
602 name = getenv("KCONFIG_ALLCONFIG");
603 if (!name)
604 break;
605 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
606 if (conf_read_simple(name, S_DEF_USER)) {
607 fprintf(stderr,
608 _("*** Can't read seed configuration \"%s\"!\n"),
609 name);
610 exit(1);
612 break;
614 switch (input_mode) {
615 case allnoconfig: name = "allno.config"; break;
616 case allyesconfig: name = "allyes.config"; break;
617 case allmodconfig: name = "allmod.config"; break;
618 case alldefconfig: name = "alldef.config"; break;
619 case randconfig: name = "allrandom.config"; break;
620 default: break;
622 if (conf_read_simple(name, S_DEF_USER) &&
623 conf_read_simple("all.config", S_DEF_USER)) {
624 fprintf(stderr,
625 _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
626 name);
627 exit(1);
629 break;
630 default:
631 break;
634 if (sync_kconfig) {
635 if (conf_get_changed()) {
636 name = getenv("KCONFIG_NOSILENTUPDATE");
637 if (name && *name) {
638 fprintf(stderr,
639 _("\n*** The configuration requires explicit update.\n\n"));
640 return 1;
643 valid_stdin = tty_stdio;
646 switch (input_mode) {
647 case allnoconfig:
648 conf_set_all_new_symbols(def_no);
649 break;
650 case allyesconfig:
651 conf_set_all_new_symbols(def_yes);
652 break;
653 case allmodconfig:
654 conf_set_all_new_symbols(def_mod);
655 break;
656 case alldefconfig:
657 conf_set_all_new_symbols(def_default);
658 break;
659 case randconfig:
660 /* Really nothing to do in this loop */
661 while (conf_set_all_new_symbols(def_random)) ;
662 break;
663 case defconfig:
664 conf_set_all_new_symbols(def_default);
665 break;
666 case savedefconfig:
667 break;
668 case oldaskconfig:
669 rootEntry = &rootmenu;
670 conf(&rootmenu);
671 input_mode = silentoldconfig;
672 /* fall through */
673 case oldconfig:
674 case listnewconfig:
675 case olddefconfig:
676 case silentoldconfig:
677 /* Update until a loop caused no more changes */
678 do {
679 conf_cnt = 0;
680 check_conf(&rootmenu);
681 } while (conf_cnt &&
682 (input_mode != listnewconfig &&
683 input_mode != olddefconfig));
684 break;
687 env = getenv("KCONFIG_STRICT");
688 if (env && *env && kconfig_warnings) {
689 fprintf(stderr, _("\n*** ERROR: %d warnings encountered, and "
690 "warnings are errors.\n\n"), kconfig_warnings);
691 exit(1);
694 if (sync_kconfig) {
695 /* silentoldconfig is used during the build so we shall update autoconf.
696 * All other commands are only used to generate a config.
698 if (conf_get_changed() && conf_write(NULL)) {
699 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
700 exit(1);
702 if (conf_write_autoconf()) {
703 fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
704 return 1;
706 } else if (input_mode == savedefconfig) {
707 if (conf_write_defconfig(defconfig_file)) {
708 fprintf(stderr, _("\n*** Error while saving defconfig to: %s\n\n"),
709 defconfig_file);
710 return 1;
712 } else if (input_mode != listnewconfig) {
713 if (conf_write(NULL)) {
714 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
715 exit(1);
718 return 0;
722 * Helper function to facilitate fgets() by Jean Sacren.
724 void xfgets(char *str, int size, FILE *in)
726 if (fgets(str, size, in) == NULL)
727 fprintf(stderr, "\nError in reading or end of file.\n");