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 */;
499 setlocale(LC_ALL
, "");
500 bindtextdomain(PACKAGE
, LOCALEDIR
);
503 tty_stdio
= isatty(0) && isatty(1) && isatty(2);
505 while ((opt
= getopt_long(ac
, av
, "s", long_opts
, NULL
)) != -1) {
507 conf_set_message_callback(NULL
);
510 input_mode
= (enum input_mode
)opt
;
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
);
554 conf_usage(progname
);
560 printf(_("%s: Kconfig file missing\n"), av
[0]);
561 conf_usage(progname
);
568 name
= conf_get_configname();
569 if (stat(name
, &tmpstat
)) {
570 fprintf(stderr
, _("***\n"
571 "*** Configuration file \"%s\" not found!\n"
573 "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
574 "*** \"make menuconfig\" or \"make xconfig\").\n"
580 switch (input_mode
) {
583 defconfig_file
= conf_get_default_confname();
584 if (conf_read(defconfig_file
)) {
586 "*** Can't find default configuration \"%s\"!\n"
587 "***\n"), defconfig_file
);
592 case silentoldconfig
:
604 name
= getenv("KCONFIG_ALLCONFIG");
607 if ((strcmp(name
, "") != 0) && (strcmp(name
, "1") != 0)) {
608 if (conf_read_simple(name
, S_DEF_USER
)) {
610 _("*** Can't read seed configuration \"%s\"!\n"),
616 switch (input_mode
) {
617 case allnoconfig
: name
= "allno.config"; break;
618 case allyesconfig
: name
= "allyes.config"; break;
619 case allmodconfig
: name
= "allmod.config"; break;
620 case alldefconfig
: name
= "alldef.config"; break;
621 case randconfig
: name
= "allrandom.config"; break;
624 if (conf_read_simple(name
, S_DEF_USER
) &&
625 conf_read_simple("all.config", S_DEF_USER
)) {
627 _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
637 if (conf_get_changed()) {
638 name
= getenv("KCONFIG_NOSILENTUPDATE");
641 _("\n*** The configuration requires explicit update.\n\n"));
645 valid_stdin
= tty_stdio
;
648 switch (input_mode
) {
650 conf_set_all_new_symbols(def_no
);
653 conf_set_all_new_symbols(def_yes
);
656 conf_set_all_new_symbols(def_mod
);
659 conf_set_all_new_symbols(def_default
);
662 /* Really nothing to do in this loop */
663 while (conf_set_all_new_symbols(def_random
)) ;
666 conf_set_all_new_symbols(def_default
);
671 rootEntry
= &rootmenu
;
673 input_mode
= silentoldconfig
;
678 case silentoldconfig
:
679 /* Update until a loop caused no more changes */
682 check_conf(&rootmenu
);
684 (input_mode
!= listnewconfig
&&
685 input_mode
!= olddefconfig
));
690 /* silentoldconfig is used during the build so we shall update autoconf.
691 * All other commands are only used to generate a config.
693 if (conf_get_changed() && conf_write(NULL
)) {
694 fprintf(stderr
, _("\n*** Error during writing of the configuration.\n\n"));
697 if (conf_write_autoconf()) {
698 fprintf(stderr
, _("\n*** Error during update of the configuration.\n\n"));
701 } else if (input_mode
== savedefconfig
) {
702 if (conf_write_defconfig(defconfig_file
)) {
703 fprintf(stderr
, _("n*** Error while saving defconfig to: %s\n\n"),
707 } else if (input_mode
!= listnewconfig
) {
708 if (conf_write(NULL
)) {
709 fprintf(stderr
, _("\n*** Error during writing of the configuration.\n\n"));
717 * Helper function to facilitate fgets() by Jean Sacren.
719 void xfgets(char *str
, int size
, FILE *in
)
721 if (fgets(str
, size
, in
) == NULL
)
722 fprintf(stderr
, "\nError in reading or end of file.\n");