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