kconfig: add alldefconfig
[firefly-linux-kernel-4.4.55.git] / scripts / kconfig / 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 <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12 #include <unistd.h>
13 #include <getopt.h>
14 #include <sys/stat.h>
15 #include <sys/time.h>
16
17 #define LKC_DIRECT_LINK
18 #include "lkc.h"
19
20 static void conf(struct menu *menu);
21 static void check_conf(struct menu *menu);
22
23 enum input_mode {
24         oldaskconfig,
25         silentoldconfig,
26         oldconfig,
27         allnoconfig,
28         allyesconfig,
29         allmodconfig,
30         alldefconfig,
31         randconfig,
32         defconfig,
33         listnewconfig,
34         oldnoconfig,
35 } input_mode = oldaskconfig;
36
37 char *defconfig_file;
38
39 static int indent = 1;
40 static int valid_stdin = 1;
41 static int sync_kconfig;
42 static int conf_cnt;
43 static char line[128];
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 oldconfig' to update configuration.\n\n"));
79                 exit(1);
80         }
81 }
82
83 static int conf_askvalue(struct symbol *sym, const char *def)
84 {
85         enum symbol_type type = sym_get_type(sym);
86
87         if (!sym_has_value(sym))
88                 printf(_("(NEW) "));
89
90         line[0] = '\n';
91         line[1] = 0;
92
93         if (!sym_is_changable(sym)) {
94                 printf("%s\n", def);
95                 line[0] = '\n';
96                 line[1] = 0;
97                 return 0;
98         }
99
100         switch (input_mode) {
101         case oldconfig:
102         case silentoldconfig:
103                 if (sym_has_value(sym)) {
104                         printf("%s\n", def);
105                         return 0;
106                 }
107                 check_stdin();
108         case oldaskconfig:
109                 fflush(stdout);
110                 fgets(line, 128, stdin);
111                 return 1;
112         default:
113                 break;
114         }
115
116         switch (type) {
117         case S_INT:
118         case S_HEX:
119         case S_STRING:
120                 printf("%s\n", def);
121                 return 1;
122         default:
123                 ;
124         }
125         printf("%s", line);
126         return 1;
127 }
128
129 static int conf_string(struct menu *menu)
130 {
131         struct symbol *sym = menu->sym;
132         const char *def;
133
134         while (1) {
135                 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
136                 printf("(%s) ", sym->name);
137                 def = sym_get_string_value(sym);
138                 if (sym_get_string_value(sym))
139                         printf("[%s] ", def);
140                 if (!conf_askvalue(sym, def))
141                         return 0;
142                 switch (line[0]) {
143                 case '\n':
144                         break;
145                 case '?':
146                         /* print help */
147                         if (line[1] == '\n') {
148                                 print_help(menu);
149                                 def = NULL;
150                                 break;
151                         }
152                 default:
153                         line[strlen(line)-1] = 0;
154                         def = line;
155                 }
156                 if (def && sym_set_string_value(sym, def))
157                         return 0;
158         }
159 }
160
161 static int conf_sym(struct menu *menu)
162 {
163         struct symbol *sym = menu->sym;
164         tristate oldval, newval;
165
166         while (1) {
167                 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
168                 if (sym->name)
169                         printf("(%s) ", sym->name);
170                 putchar('[');
171                 oldval = sym_get_tristate_value(sym);
172                 switch (oldval) {
173                 case no:
174                         putchar('N');
175                         break;
176                 case mod:
177                         putchar('M');
178                         break;
179                 case yes:
180                         putchar('Y');
181                         break;
182                 }
183                 if (oldval != no && sym_tristate_within_range(sym, no))
184                         printf("/n");
185                 if (oldval != mod && sym_tristate_within_range(sym, mod))
186                         printf("/m");
187                 if (oldval != yes && sym_tristate_within_range(sym, yes))
188                         printf("/y");
189                 if (menu_has_help(menu))
190                         printf("/?");
191                 printf("] ");
192                 if (!conf_askvalue(sym, sym_get_string_value(sym)))
193                         return 0;
194                 strip(line);
195
196                 switch (line[0]) {
197                 case 'n':
198                 case 'N':
199                         newval = no;
200                         if (!line[1] || !strcmp(&line[1], "o"))
201                                 break;
202                         continue;
203                 case 'm':
204                 case 'M':
205                         newval = mod;
206                         if (!line[1])
207                                 break;
208                         continue;
209                 case 'y':
210                 case 'Y':
211                         newval = yes;
212                         if (!line[1] || !strcmp(&line[1], "es"))
213                                 break;
214                         continue;
215                 case 0:
216                         newval = oldval;
217                         break;
218                 case '?':
219                         goto help;
220                 default:
221                         continue;
222                 }
223                 if (sym_set_tristate_value(sym, newval))
224                         return 0;
225 help:
226                 print_help(menu);
227         }
228 }
229
230 static int conf_choice(struct menu *menu)
231 {
232         struct symbol *sym, *def_sym;
233         struct menu *child;
234         bool is_new;
235
236         sym = menu->sym;
237         is_new = !sym_has_value(sym);
238         if (sym_is_changable(sym)) {
239                 conf_sym(menu);
240                 sym_calc_value(sym);
241                 switch (sym_get_tristate_value(sym)) {
242                 case no:
243                         return 1;
244                 case mod:
245                         return 0;
246                 case yes:
247                         break;
248                 }
249         } else {
250                 switch (sym_get_tristate_value(sym)) {
251                 case no:
252                         return 1;
253                 case mod:
254                         printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
255                         return 0;
256                 case yes:
257                         break;
258                 }
259         }
260
261         while (1) {
262                 int cnt, def;
263
264                 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
265                 def_sym = sym_get_choice_value(sym);
266                 cnt = def = 0;
267                 line[0] = 0;
268                 for (child = menu->list; child; child = child->next) {
269                         if (!menu_is_visible(child))
270                                 continue;
271                         if (!child->sym) {
272                                 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
273                                 continue;
274                         }
275                         cnt++;
276                         if (child->sym == def_sym) {
277                                 def = cnt;
278                                 printf("%*c", indent, '>');
279                         } else
280                                 printf("%*c", indent, ' ');
281                         printf(" %d. %s", cnt, _(menu_get_prompt(child)));
282                         if (child->sym->name)
283                                 printf(" (%s)", child->sym->name);
284                         if (!sym_has_value(child->sym))
285                                 printf(_(" (NEW)"));
286                         printf("\n");
287                 }
288                 printf(_("%*schoice"), indent - 1, "");
289                 if (cnt == 1) {
290                         printf("[1]: 1\n");
291                         goto conf_childs;
292                 }
293                 printf("[1-%d", cnt);
294                 if (menu_has_help(menu))
295                         printf("?");
296                 printf("]: ");
297                 switch (input_mode) {
298                 case oldconfig:
299                 case silentoldconfig:
300                         if (!is_new) {
301                                 cnt = def;
302                                 printf("%d\n", cnt);
303                                 break;
304                         }
305                         check_stdin();
306                 case oldaskconfig:
307                         fflush(stdout);
308                         fgets(line, 128, stdin);
309                         strip(line);
310                         if (line[0] == '?') {
311                                 print_help(menu);
312                                 continue;
313                         }
314                         if (!line[0])
315                                 cnt = def;
316                         else if (isdigit(line[0]))
317                                 cnt = atoi(line);
318                         else
319                                 continue;
320                         break;
321                 default:
322                         break;
323                 }
324
325         conf_childs:
326                 for (child = menu->list; child; child = child->next) {
327                         if (!child->sym || !menu_is_visible(child))
328                                 continue;
329                         if (!--cnt)
330                                 break;
331                 }
332                 if (!child)
333                         continue;
334                 if (line[strlen(line) - 1] == '?') {
335                         print_help(child);
336                         continue;
337                 }
338                 sym_set_choice_value(sym, child->sym);
339                 for (child = child->list; child; child = child->next) {
340                         indent += 2;
341                         conf(child);
342                         indent -= 2;
343                 }
344                 return 1;
345         }
346 }
347
348 static void conf(struct menu *menu)
349 {
350         struct symbol *sym;
351         struct property *prop;
352         struct menu *child;
353
354         if (!menu_is_visible(menu))
355                 return;
356
357         sym = menu->sym;
358         prop = menu->prompt;
359         if (prop) {
360                 const char *prompt;
361
362                 switch (prop->type) {
363                 case P_MENU:
364                         if ((input_mode == silentoldconfig ||
365                              input_mode == listnewconfig ||
366                              input_mode == oldnoconfig) &&
367                             rootEntry != menu) {
368                                 check_conf(menu);
369                                 return;
370                         }
371                 case P_COMMENT:
372                         prompt = menu_get_prompt(menu);
373                         if (prompt)
374                                 printf("%*c\n%*c %s\n%*c\n",
375                                         indent, '*',
376                                         indent, '*', _(prompt),
377                                         indent, '*');
378                 default:
379                         ;
380                 }
381         }
382
383         if (!sym)
384                 goto conf_childs;
385
386         if (sym_is_choice(sym)) {
387                 conf_choice(menu);
388                 if (sym->curr.tri != mod)
389                         return;
390                 goto conf_childs;
391         }
392
393         switch (sym->type) {
394         case S_INT:
395         case S_HEX:
396         case S_STRING:
397                 conf_string(menu);
398                 break;
399         default:
400                 conf_sym(menu);
401                 break;
402         }
403
404 conf_childs:
405         if (sym)
406                 indent += 2;
407         for (child = menu->list; child; child = child->next)
408                 conf(child);
409         if (sym)
410                 indent -= 2;
411 }
412
413 static void check_conf(struct menu *menu)
414 {
415         struct symbol *sym;
416         struct menu *child;
417
418         if (!menu_is_visible(menu))
419                 return;
420
421         sym = menu->sym;
422         if (sym && !sym_has_value(sym)) {
423                 if (sym_is_changable(sym) ||
424                     (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
425                         if (input_mode == listnewconfig) {
426                                 if (sym->name && !sym_is_choice_value(sym)) {
427                                         printf("CONFIG_%s\n", sym->name);
428                                 }
429                         } else {
430                                 if (!conf_cnt++)
431                                         printf(_("*\n* Restart config...\n*\n"));
432                                 rootEntry = menu_get_parent_menu(menu);
433                                 conf(rootEntry);
434                         }
435                 }
436         }
437
438         for (child = menu->list; child; child = child->next)
439                 check_conf(child);
440 }
441
442 static struct option long_opts[] = {
443         {"oldaskconfig",    no_argument,       NULL, oldaskconfig},
444         {"oldconfig",       no_argument,       NULL, oldconfig},
445         {"silentoldconfig", no_argument,       NULL, silentoldconfig},
446         {"defconfig",       optional_argument, NULL, defconfig},
447         {"allnoconfig",     no_argument,       NULL, allnoconfig},
448         {"allyesconfig",    no_argument,       NULL, allyesconfig},
449         {"allmodconfig",    no_argument,       NULL, allmodconfig},
450         {"alldefconfig",    no_argument,       NULL, alldefconfig},
451         {"randconfig",      no_argument,       NULL, randconfig},
452         {"listnewconfig",   no_argument,       NULL, listnewconfig},
453         {"oldnoconfig",     no_argument,       NULL, oldnoconfig},
454         {NULL, 0, NULL, 0}
455 };
456
457 int main(int ac, char **av)
458 {
459         int opt;
460         const char *name;
461         struct stat tmpstat;
462
463         setlocale(LC_ALL, "");
464         bindtextdomain(PACKAGE, LOCALEDIR);
465         textdomain(PACKAGE);
466
467         while ((opt = getopt_long_only(ac, av, "", long_opts, NULL)) != -1) {
468                 input_mode = (enum input_mode)opt;
469                 switch (opt) {
470                 case silentoldconfig:
471                         sync_kconfig = 1;
472                         break;
473                 case defconfig:
474                         defconfig_file = optarg;
475                         break;
476                 case randconfig:
477                 {
478                         struct timeval now;
479                         unsigned int seed;
480
481                         /*
482                          * Use microseconds derived seed,
483                          * compensate for systems where it may be zero
484                          */
485                         gettimeofday(&now, NULL);
486
487                         seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
488                         srand(seed);
489                         break;
490                 }
491                 case '?':
492                         fprintf(stderr, _("See README for usage info\n"));
493                         exit(1);
494                         break;
495                 }
496         }
497         if (ac == optind) {
498                 printf(_("%s: Kconfig file missing\n"), av[0]);
499                 exit(1);
500         }
501         name = av[optind];
502         conf_parse(name);
503         //zconfdump(stdout);
504         if (sync_kconfig) {
505                 name = conf_get_configname();
506                 if (stat(name, &tmpstat)) {
507                         fprintf(stderr, _("***\n"
508                                 "*** You have not yet configured your kernel!\n"
509                                 "*** (missing kernel config file \"%s\")\n"
510                                 "***\n"
511                                 "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
512                                 "*** \"make menuconfig\" or \"make xconfig\").\n"
513                                 "***\n"), name);
514                         exit(1);
515                 }
516         }
517
518         switch (input_mode) {
519         case defconfig:
520                 if (!defconfig_file)
521                         defconfig_file = conf_get_default_confname();
522                 if (conf_read(defconfig_file)) {
523                         printf(_("***\n"
524                                 "*** Can't find default configuration \"%s\"!\n"
525                                 "***\n"), defconfig_file);
526                         exit(1);
527                 }
528                 break;
529         case silentoldconfig:
530         case oldaskconfig:
531         case oldconfig:
532         case listnewconfig:
533         case oldnoconfig:
534                 conf_read(NULL);
535                 break;
536         case allnoconfig:
537         case allyesconfig:
538         case allmodconfig:
539         case alldefconfig:
540         case randconfig:
541                 name = getenv("KCONFIG_ALLCONFIG");
542                 if (name && !stat(name, &tmpstat)) {
543                         conf_read_simple(name, S_DEF_USER);
544                         break;
545                 }
546                 switch (input_mode) {
547                 case allnoconfig:       name = "allno.config"; break;
548                 case allyesconfig:      name = "allyes.config"; break;
549                 case allmodconfig:      name = "allmod.config"; break;
550                 case alldefconfig:      name = "alldef.config"; break;
551                 case randconfig:        name = "allrandom.config"; break;
552                 default: break;
553                 }
554                 if (!stat(name, &tmpstat))
555                         conf_read_simple(name, S_DEF_USER);
556                 else if (!stat("all.config", &tmpstat))
557                         conf_read_simple("all.config", S_DEF_USER);
558                 break;
559         default:
560                 break;
561         }
562
563         if (sync_kconfig) {
564                 if (conf_get_changed()) {
565                         name = getenv("KCONFIG_NOSILENTUPDATE");
566                         if (name && *name) {
567                                 fprintf(stderr,
568                                         _("\n*** Kernel configuration requires explicit update.\n\n"));
569                                 return 1;
570                         }
571                 }
572                 valid_stdin = isatty(0) && isatty(1) && isatty(2);
573         }
574
575         switch (input_mode) {
576         case allnoconfig:
577                 conf_set_all_new_symbols(def_no);
578                 break;
579         case allyesconfig:
580                 conf_set_all_new_symbols(def_yes);
581                 break;
582         case allmodconfig:
583                 conf_set_all_new_symbols(def_mod);
584                 break;
585         case alldefconfig:
586                 conf_set_all_new_symbols(def_default);
587                 break;
588         case randconfig:
589                 conf_set_all_new_symbols(def_random);
590                 break;
591         case defconfig:
592                 conf_set_all_new_symbols(def_default);
593                 break;
594         case oldconfig:
595         case oldaskconfig:
596                 rootEntry = &rootmenu;
597                 conf(&rootmenu);
598                 input_mode = silentoldconfig;
599                 /* fall through */
600         case listnewconfig:
601         case oldnoconfig:
602         case silentoldconfig:
603                 /* Update until a loop caused no more changes */
604                 do {
605                         conf_cnt = 0;
606                         check_conf(&rootmenu);
607                 } while (conf_cnt &&
608                          (input_mode != listnewconfig &&
609                           input_mode != oldnoconfig));
610                 break;
611         }
612
613         if (sync_kconfig) {
614                 /* silentoldconfig is used during the build so we shall update autoconf.
615                  * All other commands are only used to generate a config.
616                  */
617                 if (conf_get_changed() && conf_write(NULL)) {
618                         fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
619                         exit(1);
620                 }
621                 if (conf_write_autoconf()) {
622                         fprintf(stderr, _("\n*** Error during update of the kernel configuration.\n\n"));
623                         return 1;
624                 }
625         } else if (input_mode != listnewconfig) {
626                 if (conf_write(NULL)) {
627                         fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
628                         exit(1);
629                 }
630         }
631         return 0;
632 }