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