kconfig: use C89 random functions in conf.c
[safe/jmp/linux-2.6] / scripts / kconfig / conf.c
index b86c64f..01c2f35 100644 (file)
@@ -4,10 +4,11 @@
  */
 
 #include <ctype.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
 #include <time.h>
+#include <unistd.h>
 #include <sys/stat.h>
 
 #define LKC_DIRECT_LINK
@@ -36,6 +37,14 @@ static struct menu *rootEntry;
 
 static char nohelp_text[] = N_("Sorry, no help available for this option yet.\n");
 
+static const char *get_help(struct menu *menu)
+{
+       if (menu_has_help(menu))
+               return menu_get_help(menu);
+       else
+               return nohelp_text;
+}
+
 static void strip(char *str)
 {
        char *p = str;
@@ -63,21 +72,7 @@ static void check_stdin(void)
        }
 }
 
-static char *fgets_check_stream(char *s, int size, FILE *stream)
-{
-       char *ret = fgets(s, size, stream);
-
-       if (ret == NULL && feof(stream)) {
-               printf(_("aborted!\n\n"));
-               printf(_("Console input is closed. "));
-               printf(_("Run 'make oldconfig' to update configuration.\n\n"));
-               exit(1);
-       }
-
-       return ret;
-}
-
-static void conf_askvalue(struct symbol *sym, const char *def)
+static int conf_askvalue(struct symbol *sym, const char *def)
 {
        enum symbol_type type = sym_get_type(sym);
        tristate val;
@@ -92,7 +87,7 @@ static void conf_askvalue(struct symbol *sym, const char *def)
                printf("%s\n", def);
                line[0] = '\n';
                line[1] = 0;
-               return;
+               return 0;
        }
 
        switch (input_mode) {
@@ -102,23 +97,23 @@ static void conf_askvalue(struct symbol *sym, const char *def)
        case set_random:
                if (sym_has_value(sym)) {
                        printf("%s\n", def);
-                       return;
+                       return 0;
                }
                break;
        case ask_new:
        case ask_silent:
                if (sym_has_value(sym)) {
                        printf("%s\n", def);
-                       return;
+                       return 0;
                }
                check_stdin();
        case ask_all:
                fflush(stdout);
-               fgets_check_stream(line, 128, stdin);
-               return;
+               fgets(line, 128, stdin);
+               return 1;
        case set_default:
                printf("%s\n", def);
-               return;
+               return 1;
        default:
                break;
        }
@@ -128,7 +123,7 @@ static void conf_askvalue(struct symbol *sym, const char *def)
        case S_HEX:
        case S_STRING:
                printf("%s\n", def);
-               return;
+               return 1;
        default:
                ;
        }
@@ -165,7 +160,7 @@ static void conf_askvalue(struct symbol *sym, const char *def)
                }
        case set_random:
                do {
-                       val = (tristate)(random() % 3);
+                       val = (tristate)(rand() % 3);
                } while (!sym_tristate_within_range(sym, val));
                switch (val) {
                case no: line[0] = 'n'; break;
@@ -179,12 +174,13 @@ static void conf_askvalue(struct symbol *sym, const char *def)
                break;
        }
        printf("%s", line);
+       return 1;
 }
 
 int conf_string(struct menu *menu)
 {
        struct symbol *sym = menu->sym;
-       const char *def, *help;
+       const char *def;
 
        while (1) {
                printf("%*s%s ", indent - 1, "", menu->prompt->text);
@@ -192,17 +188,15 @@ int conf_string(struct menu *menu)
                def = sym_get_string_value(sym);
                if (sym_get_string_value(sym))
                        printf("[%s] ", def);
-               conf_askvalue(sym, def);
+               if (!conf_askvalue(sym, def))
+                       return 0;
                switch (line[0]) {
                case '\n':
                        break;
                case '?':
                        /* print help */
                        if (line[1] == '\n') {
-                               help = nohelp_text;
-                               if (menu->sym->help)
-                                       help = menu->sym->help;
-                               printf("\n%s\n", menu->sym->help);
+                               printf("\n%s\n", get_help(menu));
                                def = NULL;
                                break;
                        }
@@ -220,7 +214,6 @@ static int conf_sym(struct menu *menu)
        struct symbol *sym = menu->sym;
        int type;
        tristate oldval, newval;
-       const char *help;
 
        while (1) {
                printf("%*s%s ", indent - 1, "", menu->prompt->text);
@@ -246,10 +239,11 @@ static int conf_sym(struct menu *menu)
                        printf("/m");
                if (oldval != yes && sym_tristate_within_range(sym, yes))
                        printf("/y");
-               if (sym->help)
+               if (menu_has_help(menu))
                        printf("/?");
                printf("] ");
-               conf_askvalue(sym, sym_get_string_value(sym));
+               if (!conf_askvalue(sym, sym_get_string_value(sym)))
+                       return 0;
                strip(line);
 
                switch (line[0]) {
@@ -282,10 +276,7 @@ static int conf_sym(struct menu *menu)
                if (sym_set_tristate_value(sym, newval))
                        return 0;
 help:
-               help = nohelp_text;
-               if (sym->help)
-                       help = sym->help;
-               printf("\n%s\n", help);
+               printf("\n%s\n", get_help(menu));
        }
 }
 
@@ -355,7 +346,7 @@ static int conf_choice(struct menu *menu)
                        goto conf_childs;
                }
                printf("[1-%d", cnt);
-               if (sym->help)
+               if (menu_has_help(menu))
                        printf("?");
                printf("]: ");
                switch (input_mode) {
@@ -369,11 +360,10 @@ static int conf_choice(struct menu *menu)
                        check_stdin();
                case ask_all:
                        fflush(stdout);
-                       fgets_check_stream(line, 128, stdin);
+                       fgets(line, 128, stdin);
                        strip(line);
                        if (line[0] == '?') {
-                               printf("\n%s\n", menu->sym->help ?
-                                       menu->sym->help : nohelp_text);
+                               printf("\n%s\n", get_help(menu));
                                continue;
                        }
                        if (!line[0])
@@ -384,7 +374,8 @@ static int conf_choice(struct menu *menu)
                                continue;
                        break;
                case set_random:
-                       def = (random() % cnt) + 1;
+                       if (is_new)
+                               def = (rand() % cnt) + 1;
                case set_default:
                case set_yes:
                case set_mod:
@@ -404,8 +395,7 @@ static int conf_choice(struct menu *menu)
                if (!child)
                        continue;
                if (line[strlen(line) - 1] == '?') {
-                       printf("\n%s\n", child->sym->help ?
-                               child->sym->help : nohelp_text);
+                       printf("\n%s\n", get_help(child));
                        continue;
                }
                sym_set_choice_value(sym, child->sym);
@@ -505,12 +495,12 @@ static void check_conf(struct menu *menu)
 
 int main(int ac, char **av)
 {
-       int i = 1;
+       int opt;
        const char *name;
        struct stat tmpstat;
 
-       if (ac > i && av[i][0] == '-') {
-               switch (av[i++][1]) {
+       while ((opt = getopt(ac, av, "osdD:nmyrh")) != -1) {
+               switch (opt) {
                case 'o':
                        input_mode = ask_new;
                        break;
@@ -523,12 +513,7 @@ int main(int ac, char **av)
                        break;
                case 'D':
                        input_mode = set_default;
-                       defconfig_file = av[i++];
-                       if (!defconfig_file) {
-                               printf(_("%s: No default config file specified\n"),
-                                       av[0]);
-                               exit(1);
-                       }
+                       defconfig_file = optarg;
                        break;
                case 'n':
                        input_mode = set_no;
@@ -541,18 +526,22 @@ int main(int ac, char **av)
                        break;
                case 'r':
                        input_mode = set_random;
-                       srandom(time(NULL));
+                       srand(time(NULL));
                        break;
                case 'h':
-               case '?':
-                       printf("%s [-o|-s] config\n", av[0]);
+                       printf("See README for usage info\n");
                        exit(0);
+                       break;
+               default:
+                       fprintf(stderr, "See README for usage info\n");
+                       exit(1);
                }
        }
-       name = av[i];
-       if (!name) {
+       if (ac == optind) {
                printf(_("%s: Kconfig file missing\n"), av[0]);
+               exit(1);
        }
+       name = av[optind];
        conf_parse(name);
        //zconfdump(stdout);
        switch (input_mode) {
@@ -570,6 +559,7 @@ int main(int ac, char **av)
                if (stat(".config", &tmpstat)) {
                        printf(_("***\n"
                                "*** You have not yet configured your kernel!\n"
+                               "*** (missing kernel .config file)\n"
                                "***\n"
                                "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
                                "*** \"make menuconfig\" or \"make xconfig\").\n"
@@ -586,7 +576,7 @@ int main(int ac, char **av)
        case set_random:
                name = getenv("KCONFIG_ALLCONFIG");
                if (name && !stat(name, &tmpstat)) {
-                       conf_read_simple(name);
+                       conf_read_simple(name, S_DEF_USER);
                        break;
                }
                switch (input_mode) {
@@ -597,9 +587,9 @@ int main(int ac, char **av)
                default: break;
                }
                if (!stat(name, &tmpstat))
-                       conf_read_simple(name);
+                       conf_read_simple(name, S_DEF_USER);
                else if (!stat("all.config", &tmpstat))
-                       conf_read_simple("all.config");
+                       conf_read_simple("all.config", S_DEF_USER);
                break;
        default:
                break;
@@ -612,7 +602,15 @@ int main(int ac, char **av)
                        input_mode = ask_silent;
                        valid_stdin = 1;
                }
-       }
+       } else if (conf_get_changed()) {
+               name = getenv("KCONFIG_NOSILENTUPDATE");
+               if (name && *name) {
+                       fprintf(stderr, _("\n*** Kernel configuration requires explicit update.\n\n"));
+                       return 1;
+               }
+       } else
+               goto skip_check;
+
        do {
                conf_cnt = 0;
                check_conf(&rootmenu);
@@ -621,5 +619,11 @@ int main(int ac, char **av)
                fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
                return 1;
        }
+skip_check:
+       if (input_mode == ask_silent && conf_write_autoconf()) {
+               fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
+               return 1;
+       }
+
        return 0;
 }