2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
21 static void conf(struct menu
*menu
);
22 static void check_conf(struct menu
*menu
);
23 static void xfgets(char *str
, int size
, FILE *in
);
38 } input_mode
= oldaskconfig
;
40 static int indent
= 1;
42 static int valid_stdin
= 1;
43 static int sync_kconfig
;
45 static char line
[PATH_MAX
];
46 static struct menu
*rootEntry
;
48 static void print_help(struct menu
*menu
)
50 struct gstr help
= str_new();
52 menu_get_ext_help(menu
, &help
);
54 printf("\n%s\n", str_get(&help
));
58 static void strip(char *str
)
67 memmove(str
, p
, l
+ 1);
75 static void check_stdin(void)
78 printf(_("aborted!\n\n"));
79 printf(_("Console input/output is redirected. "));
80 printf(_("Run 'make oldconfig' to update configuration.\n\n"));
85 static int conf_askvalue(struct symbol
*sym
, const char *def
)
87 enum symbol_type type
= sym_get_type(sym
);
89 if (!sym_has_value(sym
))
95 if (!sym_is_changable(sym
)) {
102 switch (input_mode
) {
104 case silentoldconfig
:
105 if (sym_has_value(sym
)) {
113 xfgets(line
, sizeof(line
), stdin
);
134 static int conf_string(struct menu
*menu
)
136 struct symbol
*sym
= menu
->sym
;
140 printf("%*s%s ", indent
- 1, "", _(menu
->prompt
->text
));
141 printf("(%s) ", sym
->name
);
142 def
= sym_get_string_value(sym
);
143 if (sym_get_string_value(sym
))
144 printf("[%s] ", def
);
145 if (!conf_askvalue(sym
, def
))
152 if (line
[1] == '\n') {
159 line
[strlen(line
)-1] = 0;
162 if (def
&& sym_set_string_value(sym
, def
))
167 static int conf_sym(struct menu
*menu
)
169 struct symbol
*sym
= menu
->sym
;
170 tristate oldval
, newval
;
173 printf("%*s%s ", indent
- 1, "", _(menu
->prompt
->text
));
175 printf("(%s) ", sym
->name
);
177 oldval
= sym_get_tristate_value(sym
);
189 if (oldval
!= no
&& sym_tristate_within_range(sym
, no
))
191 if (oldval
!= mod
&& sym_tristate_within_range(sym
, mod
))
193 if (oldval
!= yes
&& sym_tristate_within_range(sym
, yes
))
195 if (menu_has_help(menu
))
198 if (!conf_askvalue(sym
, sym_get_string_value(sym
)))
206 if (!line
[1] || !strcmp(&line
[1], "o"))
218 if (!line
[1] || !strcmp(&line
[1], "es"))
229 if (sym_set_tristate_value(sym
, newval
))
236 static int conf_choice(struct menu
*menu
)
238 struct symbol
*sym
, *def_sym
;
243 is_new
= !sym_has_value(sym
);
244 if (sym_is_changable(sym
)) {
247 switch (sym_get_tristate_value(sym
)) {
256 switch (sym_get_tristate_value(sym
)) {
260 printf("%*s%s\n", indent
- 1, "", _(menu_get_prompt(menu
)));
270 printf("%*s%s\n", indent
- 1, "", _(menu_get_prompt(menu
)));
271 def_sym
= sym_get_choice_value(sym
);
274 for (child
= menu
->list
; child
; child
= child
->next
) {
275 if (!menu_is_visible(child
))
278 printf("%*c %s\n", indent
, '*', _(menu_get_prompt(child
)));
282 if (child
->sym
== def_sym
) {
284 printf("%*c", indent
, '>');
286 printf("%*c", indent
, ' ');
287 printf(" %d. %s", cnt
, _(menu_get_prompt(child
)));
288 if (child
->sym
->name
)
289 printf(" (%s)", child
->sym
->name
);
290 if (!sym_has_value(child
->sym
))
294 printf(_("%*schoice"), indent
- 1, "");
299 printf("[1-%d", cnt
);
300 if (menu_has_help(menu
))
303 switch (input_mode
) {
305 case silentoldconfig
:
315 xfgets(line
, sizeof(line
), stdin
);
317 if (line
[0] == '?') {
323 else if (isdigit(line
[0]))
333 for (child
= menu
->list
; child
; child
= child
->next
) {
334 if (!child
->sym
|| !menu_is_visible(child
))
341 if (line
[0] && line
[strlen(line
) - 1] == '?') {
345 sym_set_choice_value(sym
, child
->sym
);
346 for (child
= child
->list
; child
; child
= child
->next
) {
355 static void conf(struct menu
*menu
)
358 struct property
*prop
;
361 if (!menu_is_visible(menu
))
369 switch (prop
->type
) {
371 if ((input_mode
== silentoldconfig
||
372 input_mode
== listnewconfig
||
373 input_mode
== olddefconfig
) &&
380 prompt
= menu_get_prompt(menu
);
382 printf("%*c\n%*c %s\n%*c\n",
384 indent
, '*', _(prompt
),
394 if (sym_is_choice(sym
)) {
396 if (sym
->curr
.tri
!= mod
)
415 for (child
= menu
->list
; child
; child
= child
->next
)
421 static void check_conf(struct menu
*menu
)
426 if (!menu_is_visible(menu
))
430 if (sym
&& !sym_has_value(sym
)) {
431 if (sym_is_changable(sym
) ||
432 (sym_is_choice(sym
) && sym_get_tristate_value(sym
) == yes
)) {
433 if (input_mode
== listnewconfig
) {
434 if (sym
->name
&& !sym_is_choice_value(sym
)) {
435 printf("%s%s\n", CONFIG_
, sym
->name
);
437 } else if (input_mode
!= olddefconfig
) {
439 printf(_("*\n* Restart config...\n*\n"));
440 rootEntry
= menu_get_parent_menu(menu
);
446 for (child
= menu
->list
; child
; child
= child
->next
)
450 static struct option long_opts
[] = {
451 {"oldaskconfig", no_argument
, NULL
, oldaskconfig
},
452 {"oldconfig", no_argument
, NULL
, oldconfig
},
453 {"silentoldconfig", no_argument
, NULL
, silentoldconfig
},
454 {"defconfig", optional_argument
, NULL
, defconfig
},
455 {"savedefconfig", required_argument
, NULL
, savedefconfig
},
456 {"allnoconfig", no_argument
, NULL
, allnoconfig
},
457 {"allyesconfig", no_argument
, NULL
, allyesconfig
},
458 {"allmodconfig", no_argument
, NULL
, allmodconfig
},
459 {"alldefconfig", no_argument
, NULL
, alldefconfig
},
460 {"randconfig", no_argument
, NULL
, randconfig
},
461 {"listnewconfig", no_argument
, NULL
, listnewconfig
},
462 {"olddefconfig", no_argument
, NULL
, olddefconfig
},
464 * oldnoconfig is an alias of olddefconfig, because people already
465 * are dependent on its behavior(sets new symbols to their default
466 * value but not 'n') with the counter-intuitive name.
468 {"oldnoconfig", no_argument
, NULL
, olddefconfig
},
472 static void conf_usage(const char *progname
)
475 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname
);
476 printf("[option] is _one_ of the following:\n");
477 printf(" --listnewconfig List new options\n");
478 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
479 printf(" --oldconfig Update a configuration using a provided .config as base\n");
480 printf(" --silentoldconfig Same as oldconfig, but quietly, additionally update deps\n");
481 printf(" --olddefconfig Same as silentoldconfig but sets new symbols to their default value\n");
482 printf(" --oldnoconfig An alias of olddefconfig\n");
483 printf(" --defconfig <file> New config with default defined in <file>\n");
484 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
485 printf(" --allnoconfig New config where all options are answered with no\n");
486 printf(" --allyesconfig New config where all options are answered with yes\n");
487 printf(" --allmodconfig New config where all options are answered with mod\n");
488 printf(" --alldefconfig New config with all symbols set to default\n");
489 printf(" --randconfig New config with random answer to all options\n");
492 int main(int ac
, char **av
)
494 const char *progname
= av
[0];
496 const char *name
, *defconfig_file
= NULL
/* gcc uninit */;
498 const char *input_file
= NULL
, *output_file
= NULL
;
500 setlocale(LC_ALL
, "");
501 bindtextdomain(PACKAGE
, LOCALEDIR
);
504 tty_stdio
= isatty(0) && isatty(1) && isatty(2);
506 while ((opt
= getopt_long(ac
, av
, "r:w:s", long_opts
, NULL
)) != -1) {
508 conf_set_message_callback(NULL
);
512 case silentoldconfig
:
517 defconfig_file
= optarg
;
526 * Use microseconds derived seed,
527 * compensate for systems where it may be zero
529 gettimeofday(&now
, NULL
);
530 seed
= (unsigned int)((now
.tv_sec
+ 1) * (now
.tv_usec
+ 1));
532 seed_env
= getenv("KCONFIG_SEED");
533 if( seed_env
&& *seed_env
) {
535 int tmp
= (int)strtol(seed_env
, &endp
, 0);
540 fprintf( stderr
, "KCONFIG_SEED=0x%X\n", seed
);
557 output_file
= optarg
;
560 conf_usage(progname
);
564 input_mode
= (enum input_mode
)opt
;
567 printf(_("%s: Kconfig file missing\n"), av
[0]);
568 conf_usage(progname
);
575 name
= conf_get_configname();
576 if (stat(name
, &tmpstat
)) {
577 fprintf(stderr
, _("***\n"
578 "*** Configuration file \"%s\" not found!\n"
580 "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
581 "*** \"make menuconfig\" or \"make xconfig\").\n"
587 switch (input_mode
) {
590 defconfig_file
= conf_get_default_confname();
591 if (conf_read(defconfig_file
)) {
593 "*** Can't find default configuration \"%s\"!\n"
594 "***\n"), defconfig_file
);
599 case silentoldconfig
:
609 conf_read(input_file
);
616 if (conf_get_changed()) {
617 name
= getenv("KCONFIG_NOSILENTUPDATE");
620 _("\n*** The configuration requires explicit update.\n\n"));
624 valid_stdin
= tty_stdio
;
627 switch (input_mode
) {
629 conf_set_all_new_symbols(def_no
);
632 conf_set_all_new_symbols(def_yes
);
635 conf_set_all_new_symbols(def_mod
);
638 conf_set_all_new_symbols(def_default
);
641 /* Really nothing to do in this loop */
642 while (conf_set_all_new_symbols(def_random
)) ;
645 conf_set_all_new_symbols(def_default
);
650 rootEntry
= &rootmenu
;
652 input_mode
= silentoldconfig
;
657 case silentoldconfig
:
658 /* Update until a loop caused no more changes */
661 check_conf(&rootmenu
);
663 (input_mode
!= listnewconfig
&&
664 input_mode
!= olddefconfig
));
669 /* silentoldconfig is used during the build so we shall update autoconf.
670 * All other commands are only used to generate a config.
672 if ((output_file
|| conf_get_changed()) &&
673 conf_write(output_file
)) {
674 fprintf(stderr
, _("\n*** Error during writing of the configuration.\n\n"));
677 if (conf_write_autoconf()) {
678 fprintf(stderr
, _("\n*** Error during update of the configuration.\n\n"));
681 } else if (input_mode
== savedefconfig
) {
682 if (conf_write_defconfig(defconfig_file
)) {
683 fprintf(stderr
, _("n*** Error while saving defconfig to: %s\n\n"),
687 } else if (input_mode
!= listnewconfig
) {
688 if (conf_write(output_file
)) {
689 fprintf(stderr
, _("\n*** Error during writing of the configuration.\n\n"));
697 * Helper function to facilitate fgets() by Jean Sacren.
699 void xfgets(char *str
, int size
, FILE *in
)
701 if (fgets(str
, size
, in
) == NULL
)
702 fprintf(stderr
, "\nError in reading or end of file.\n");