kconfig: make xfgets() really static
[carl9170fw.git] / config / conf.c
1 /*
2  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3  * Released under the terms of the GNU GPL v2.0.
4  */
5
6 #include <locale.h>
7 #include <ctype.h>
8 #include <limits.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <time.h>
13 #include <unistd.h>
14 #include <getopt.h>
15 #include <sys/stat.h>
16 #include <sys/time.h>
17 #include <errno.h>
18
19 #include "lkc.h"
20
21 static void conf(struct menu *menu);
22 static void check_conf(struct menu *menu);
23
24 enum input_mode {
25         oldaskconfig,
26         oldconfig,
27         allnoconfig,
28         allyesconfig,
29         allmodconfig,
30         alldefconfig,
31         randconfig,
32         defconfig,
33         savedefconfig,
34         listnewconfig,
35         oldnoconfig,
36 };
37 static enum input_mode input_mode = oldaskconfig;
38
39 static int indent = 1;
40 static int tty_stdio;
41 static int valid_stdin = 1;
42 static int conf_cnt;
43 static char line[PATH_MAX];
44 static struct menu *rootEntry;
45
46 static void print_help(struct menu *menu)
47 {
48         struct gstr help = str_new();
49
50         menu_get_ext_help(menu, &help);
51
52         printf("\n%s\n", str_get(&help));
53         str_free(&help);
54 }
55
56 static void strip(char *str)
57 {
58         char *p = str;
59         int l;
60
61         while ((isspace(*p)))
62                 p++;
63         l = strlen(p);
64         if (p != str)
65                 memmove(str, p, l + 1);
66         if (!l)
67                 return;
68         p = str + l - 1;
69         while ((isspace(*p)))
70                 *p-- = 0;
71 }
72
73 static void check_stdin(void)
74 {
75         if (!valid_stdin) {
76                 printf(_("aborted!\n\n"));
77                 printf(_("Console input/output is redirected. "));
78                 printf(_("Run 'make config' to update configuration.\n\n"));
79                 exit(1);
80         }
81 }
82
83 /* Helper function to facilitate fgets() by Jean Sacren. */
84 static void xfgets(char *str, int size, FILE *in)
85 {
86         if (!fgets(str, size, in))
87                 fprintf(stderr, "\nError in reading or end of file.\n");
88 }
89
90 static int conf_askvalue(struct symbol *sym, const char *def)
91 {
92         enum symbol_type type = sym_get_type(sym);
93
94         if (!sym_has_value(sym))
95                 printf(_("(NEW) "));
96
97         line[0] = '\n';
98         line[1] = 0;
99
100         if (!sym_is_changable(sym)) {
101                 printf("%s\n", def);
102                 line[0] = '\n';
103                 line[1] = 0;
104                 return 0;
105         }
106
107         switch (input_mode) {
108         case oldconfig:
109                 if (sym_has_value(sym)) {
110                         printf("%s\n", def);
111                         return 0;
112                 }
113                 check_stdin();
114                 /* fall through */
115         case oldaskconfig:
116                 fflush(stdout);
117                 xfgets(line, sizeof(line), stdin);
118                 if (!tty_stdio)
119                         printf("\n");
120                 return 1;
121         default:
122                 break;
123         }
124
125         switch (type) {
126         case S_INT:
127         case S_HEX:
128         case S_STRING:
129                 printf("%s\n", def);
130                 return 1;
131         default:
132                 ;
133         }
134         printf("%s", line);
135         return 1;
136 }
137
138 static int conf_string(struct menu *menu)
139 {
140         struct symbol *sym = menu->sym;
141         const char *def;
142
143         while (1) {
144                 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
145                 printf("(%s) ", sym->name);
146                 def = sym_get_string_value(sym);
147                 if (sym_get_string_value(sym))
148                         printf("[%s] ", def);
149                 if (!conf_askvalue(sym, def))
150                         return 0;
151                 switch (line[0]) {
152                 case '\n':
153                         break;
154                 case '?':
155                         /* print help */
156                         if (line[1] == '\n') {
157                                 print_help(menu);
158                                 def = NULL;
159                                 break;
160                         }
161                         /* fall through */
162                 default:
163                         line[strlen(line)-1] = 0;
164                         def = line;
165                 }
166                 if (def && sym_set_string_value(sym, def))
167                         return 0;
168         }
169 }
170
171 static int conf_sym(struct menu *menu)
172 {
173         struct symbol *sym = menu->sym;
174         tristate oldval, newval;
175
176         while (1) {
177                 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
178                 if (sym->name)
179                         printf("(%s) ", sym->name);
180                 putchar('[');
181                 oldval = sym_get_tristate_value(sym);
182                 switch (oldval) {
183                 case no:
184                         putchar('N');
185                         break;
186                 case mod:
187                         putchar('M');
188                         break;
189                 case yes:
190                         putchar('Y');
191                         break;
192                 }
193                 if (oldval != no && sym_tristate_within_range(sym, no))
194                         printf("/n");
195                 if (oldval != mod && sym_tristate_within_range(sym, mod))
196                         printf("/m");
197                 if (oldval != yes && sym_tristate_within_range(sym, yes))
198                         printf("/y");
199                 if (menu_has_help(menu))
200                         printf("/?");
201                 printf("] ");
202                 if (!conf_askvalue(sym, sym_get_string_value(sym)))
203                         return 0;
204                 strip(line);
205
206                 switch (line[0]) {
207                 case 'n':
208                 case 'N':
209                         newval = no;
210                         if (!line[1] || !strcmp(&line[1], "o"))
211                                 break;
212                         continue;
213                 case 'm':
214                 case 'M':
215                         newval = mod;
216                         if (!line[1])
217                                 break;
218                         continue;
219                 case 'y':
220                 case 'Y':
221                         newval = yes;
222                         if (!line[1] || !strcmp(&line[1], "es"))
223                                 break;
224                         continue;
225                 case 0:
226                         newval = oldval;
227                         break;
228                 case '?':
229                         goto help;
230                 default:
231                         continue;
232                 }
233                 if (sym_set_tristate_value(sym, newval))
234                         return 0;
235 help:
236                 print_help(menu);
237         }
238 }
239
240 static int conf_choice(struct menu *menu)
241 {
242         struct symbol *sym, *def_sym;
243         struct menu *child;
244         bool is_new;
245
246         sym = menu->sym;
247         is_new = !sym_has_value(sym);
248         if (sym_is_changable(sym)) {
249                 conf_sym(menu);
250                 sym_calc_value(sym);
251                 switch (sym_get_tristate_value(sym)) {
252                 case no:
253                         return 1;
254                 case mod:
255                         return 0;
256                 case yes:
257                         break;
258                 }
259         } else {
260                 switch (sym_get_tristate_value(sym)) {
261                 case no:
262                         return 1;
263                 case mod:
264                         printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
265                         return 0;
266                 case yes:
267                         break;
268                 }
269         }
270
271         while (1) {
272                 int cnt, def;
273
274                 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
275                 def_sym = sym_get_choice_value(sym);
276                 cnt = def = 0;
277                 line[0] = 0;
278                 for (child = menu->list; child; child = child->next) {
279                         if (!menu_is_visible(child))
280                                 continue;
281                         if (!child->sym) {
282                                 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
283                                 continue;
284                         }
285                         cnt++;
286                         if (child->sym == def_sym) {
287                                 def = cnt;
288                                 printf("%*c", indent, '>');
289                         } else
290                                 printf("%*c", indent, ' ');
291                         printf(" %d. %s", cnt, _(menu_get_prompt(child)));
292                         if (child->sym->name)
293                                 printf(" (%s)", child->sym->name);
294                         if (!sym_has_value(child->sym))
295                                 printf(_(" (NEW)"));
296                         printf("\n");
297                 }
298                 printf(_("%*schoice"), indent - 1, "");
299                 if (cnt == 1) {
300                         printf("[1]: 1\n");
301                         goto conf_childs;
302                 }
303                 printf("[1-%d", cnt);
304                 if (menu_has_help(menu))
305                         printf("?");
306                 printf("]: ");
307                 switch (input_mode) {
308                 case oldconfig:
309                         if (!is_new) {
310                                 cnt = def;
311                                 printf("%d\n", cnt);
312                                 break;
313                         }
314                         check_stdin();
315                         /* fall through */
316                 case oldaskconfig:
317                         fflush(stdout);
318                         xfgets(line, sizeof(line), stdin);
319                         strip(line);
320                         if (line[0] == '?') {
321                                 print_help(menu);
322                                 continue;
323                         }
324                         if (!line[0])
325                                 cnt = def;
326                         else if (isdigit(line[0]))
327                                 cnt = atoi(line);
328                         else
329                                 continue;
330                         break;
331                 default:
332                         break;
333                 }
334
335         conf_childs:
336                 for (child = menu->list; child; child = child->next) {
337                         if (!child->sym || !menu_is_visible(child))
338                                 continue;
339                         if (!--cnt)
340                                 break;
341                 }
342                 if (!child)
343                         continue;
344                 if (line[0] && line[strlen(line) - 1] == '?') {
345                         print_help(child);
346                         continue;
347                 }
348                 sym_set_choice_value(sym, child->sym);
349                 for (child = child->list; child; child = child->next) {
350                         indent += 2;
351                         conf(child);
352                         indent -= 2;
353                 }
354                 return 1;
355         }
356 }
357
358 static void conf(struct menu *menu)
359 {
360         struct symbol *sym;
361         struct property *prop;
362         struct menu *child;
363
364         if (!menu_is_visible(menu))
365                 return;
366
367         sym = menu->sym;
368         prop = menu->prompt;
369         if (prop) {
370                 const char *prompt;
371
372                 switch (prop->type) {
373                 case P_MENU:
374                         if ((input_mode == listnewconfig ||
375                              input_mode == oldnoconfig) &&
376                             rootEntry != menu) {
377                                 check_conf(menu);
378                                 return;
379                         }
380                         /* fall through */
381                 case P_COMMENT:
382                         prompt = menu_get_prompt(menu);
383                         if (prompt)
384                                 printf("%*c\n%*c %s\n%*c\n",
385                                         indent, '*',
386                                         indent, '*', _(prompt),
387                                         indent, '*');
388                 default:
389                         ;
390                 }
391         }
392
393         if (!sym)
394                 goto conf_childs;
395
396         if (sym_is_choice(sym)) {
397                 conf_choice(menu);
398                 if (sym->curr.tri != mod)
399                         return;
400                 goto conf_childs;
401         }
402
403         switch (sym->type) {
404         case S_INT:
405         case S_HEX:
406         case S_STRING:
407                 conf_string(menu);
408                 break;
409         default:
410                 conf_sym(menu);
411                 break;
412         }
413
414 conf_childs:
415         if (sym)
416                 indent += 2;
417         for (child = menu->list; child; child = child->next)
418                 conf(child);
419         if (sym)
420                 indent -= 2;
421 }
422
423 static void check_conf(struct menu *menu)
424 {
425         struct symbol *sym;
426         struct menu *child;
427
428         if (!menu_is_visible(menu))
429                 return;
430
431         sym = menu->sym;
432         if (sym && !sym_has_value(sym)) {
433                 if (sym_is_changable(sym) ||
434                     (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
435                         if (input_mode == listnewconfig) {
436                                 if (sym->name && !sym_is_choice_value(sym)) {
437                                         printf("%s%s\n", CONFIG_, sym->name);
438                                 }
439                         } else if (input_mode != oldnoconfig) {
440                                 if (!conf_cnt++)
441                                         printf(_("*\n* Restart config...\n*\n"));
442                                 rootEntry = menu_get_parent_menu(menu);
443                                 conf(rootEntry);
444                         }
445                 }
446         }
447
448         for (child = menu->list; child; child = child->next)
449                 check_conf(child);
450 }
451
452 static struct option long_opts[] = {
453         {"askconfig",       no_argument,       NULL, oldaskconfig},
454         {"config",          no_argument,       NULL, oldconfig},
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         {"noconfig",        no_argument,       NULL, oldnoconfig},
464         {NULL, 0, NULL, 0}
465 };
466
467 static void conf_usage(const char *progname)
468 {
469
470         printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
471         printf("[option] is _one_ of the following:\n");
472         printf("  --listnewconfig         List new options\n");
473         printf("  --askconfig             Start a new configuration using a line-oriented program\n");
474         printf("  --config                Update a configuration using a provided .config as base\n");
475         printf("  --silentconfig          Same as config, but quietly, additionally update deps\n");
476         printf("  --noconfig              Same as silentconfig but set new symbols to no\n");
477         printf("  --defconfig <file>      New config with default defined in <file>\n");
478         printf("  --savedefconfig <file>  Save the minimal current configuration to <file>\n");
479         printf("  --allnoconfig           New config where all options are answered with no\n");
480         printf("  --allyesconfig          New config where all options are answered with yes\n");
481         printf("  --allmodconfig          New config where all options are answered with mod\n");
482         printf("  --alldefconfig          New config with all symbols set to default\n");
483         printf("  --randconfig            New config with random answer to all options\n");
484 }
485
486 int main(int ac, char **av)
487 {
488         const char *progname = av[0];
489         int opt;
490         const char *name, *defconfig_file = NULL /* gcc uninit */;
491         struct stat tmpstat;
492
493         setlocale(LC_ALL, "");
494         bindtextdomain(PACKAGE, LOCALEDIR);
495         textdomain(PACKAGE);
496
497         tty_stdio = isatty(0) && isatty(1) && isatty(2);
498
499         while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
500                 if (opt == 's') {
501                         conf_set_message_callback(NULL);
502                         continue;
503                 }
504                 input_mode = (enum input_mode)opt;
505                 switch (opt) {
506                 case defconfig:
507                 case savedefconfig:
508                         defconfig_file = optarg;
509                         break;
510                 case randconfig:
511                 {
512                         struct timeval now;
513                         unsigned int seed;
514                         char *seed_env;
515
516                         /*
517                          * Use microseconds derived seed,
518                          * compensate for systems where it may be zero
519                          */
520                         gettimeofday(&now, NULL);
521                         seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
522
523                         seed_env = getenv("KCONFIG_SEED");
524                         if( seed_env && *seed_env ) {
525                                 char *endp;
526                                 int tmp = (int)strtol(seed_env, &endp, 0);
527                                 if (*endp == '\0') {
528                                         seed = tmp;
529                                 }
530                         }
531                         fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
532                         srand(seed);
533                         break;
534                 }
535                 case oldaskconfig:
536                 case oldconfig:
537                 case allnoconfig:
538                 case allyesconfig:
539                 case allmodconfig:
540                 case alldefconfig:
541                 case listnewconfig:
542                 case oldnoconfig:
543                         break;
544                 case '?':
545                         conf_usage(progname);
546                         exit(1);
547                         break;
548                 }
549         }
550         if (ac == optind) {
551                 printf(_("%s: Kconfig file missing\n"), av[0]);
552                 conf_usage(progname);
553                 exit(1);
554         }
555         name = av[optind];
556         conf_parse(name);
557         //zconfdump(stdout);
558
559         switch (input_mode) {
560         case defconfig:
561                 if (!defconfig_file)
562                         defconfig_file = conf_get_default_confname();
563                 if (conf_read(defconfig_file)) {
564                         printf(_("***\n"
565                                 "*** Can't find default configuration \"%s\"!\n"
566                                 "***\n"), defconfig_file);
567                         exit(1);
568                 }
569                 break;
570         case savedefconfig:
571         case oldaskconfig:
572         case oldconfig:
573         case listnewconfig:
574         case oldnoconfig:
575                 conf_read(NULL);
576                 break;
577         case allnoconfig:
578         case allyesconfig:
579         case allmodconfig:
580         case alldefconfig:
581         case randconfig:
582                 name = getenv("KCONFIG_ALLCONFIG");
583                 if (!name)
584                         break;
585                 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
586                         if (conf_read_simple(name, S_DEF_USER)) {
587                                 fprintf(stderr,
588                                         _("*** Can't read seed configuration \"%s\"!\n"),
589                                         name);
590                                 exit(1);
591                         }
592                         break;
593                 }
594                 switch (input_mode) {
595                 case allnoconfig:       name = "allno.config"; break;
596                 case allyesconfig:      name = "allyes.config"; break;
597                 case allmodconfig:      name = "allmod.config"; break;
598                 case alldefconfig:      name = "alldef.config"; break;
599                 case randconfig:        name = "allrandom.config"; break;
600                 default: break;
601                 }
602                 if (conf_read_simple(name, S_DEF_USER) &&
603                     conf_read_simple("all.config", S_DEF_USER)) {
604                         fprintf(stderr,
605                                 _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
606                                 name);
607                         exit(1);
608                 }
609                 break;
610         default:
611                 break;
612         }
613
614         valid_stdin = tty_stdio;
615
616         switch (input_mode) {
617         case allnoconfig:
618                 conf_set_all_new_symbols(def_no);
619                 break;
620         case allyesconfig:
621                 conf_set_all_new_symbols(def_yes);
622                 break;
623         case allmodconfig:
624                 conf_set_all_new_symbols(def_mod);
625                 break;
626         case alldefconfig:
627                 conf_set_all_new_symbols(def_default);
628                 break;
629         case randconfig:
630                 /* Really nothing to do in this loop */
631                 while (conf_set_all_new_symbols(def_random)) ;
632                 break;
633         case defconfig:
634                 conf_set_all_new_symbols(def_default);
635                 break;
636         case savedefconfig:
637                 break;
638         case oldaskconfig:
639                 rootEntry = &rootmenu;
640                 conf(&rootmenu);
641                 input_mode = oldconfig;
642                 /* fall through */
643         case oldconfig:
644         case listnewconfig:
645         case oldnoconfig:
646                 /* Update until a loop caused no more changes */
647                 do {
648                         conf_cnt = 0;
649                         check_conf(&rootmenu);
650                 } while (conf_cnt &&
651                          (input_mode != listnewconfig &&
652                           input_mode != oldnoconfig));
653                 break;
654         }
655
656         if (input_mode == savedefconfig) {
657                 if (conf_write_defconfig(defconfig_file)) {
658                         fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
659                                 defconfig_file);
660                         return 1;
661                 }
662         } else if (input_mode != listnewconfig) {
663                 /*
664                  * build so we shall update autoconf.
665                  */
666                 if (conf_write(NULL)) {
667                         fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
668                         exit(1);
669                 }
670                 if (conf_write_autoconf()) {
671                         fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
672                         return 1;
673                 }
674         }
675         return 0;
676 }