e525e5ec0245822ffd918f83cd0d982c07038042
[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                         /*
358                          * Except in oldaskconfig mode, we show only menus that
359                          * contain new symbols.
360                          */
361                         if (input_mode != oldaskconfig && rootEntry != menu) {
362                                 check_conf(menu);
363                                 return;
364                         }
365                         /* fall through */
366                 case P_COMMENT:
367                         prompt = menu_get_prompt(menu);
368                         if (prompt)
369                                 printf("%*c\n%*c %s\n%*c\n",
370                                         indent, '*',
371                                         indent, '*', _(prompt),
372                                         indent, '*');
373                 default:
374                         ;
375                 }
376         }
377
378         if (!sym)
379                 goto conf_childs;
380
381         if (sym_is_choice(sym)) {
382                 conf_choice(menu);
383                 if (sym->curr.tri != mod)
384                         return;
385                 goto conf_childs;
386         }
387
388         switch (sym->type) {
389         case S_INT:
390         case S_HEX:
391         case S_STRING:
392                 conf_string(menu);
393                 break;
394         default:
395                 conf_sym(menu);
396                 break;
397         }
398
399 conf_childs:
400         if (sym)
401                 indent += 2;
402         for (child = menu->list; child; child = child->next)
403                 conf(child);
404         if (sym)
405                 indent -= 2;
406 }
407
408 static void check_conf(struct menu *menu)
409 {
410         struct symbol *sym;
411         struct menu *child;
412
413         if (!menu_is_visible(menu))
414                 return;
415
416         sym = menu->sym;
417         if (sym && !sym_has_value(sym)) {
418                 if (sym_is_changable(sym) ||
419                     (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
420                         if (input_mode == listnewconfig) {
421                                 if (sym->name) {
422                                         const char *str;
423
424                                         if (sym->type == S_STRING) {
425                                                 str = sym_get_string_value(sym);
426                                                 str = sym_escape_string_value(str);
427                                                 printf("%s%s=%s\n", CONFIG_, sym->name, str);
428                                                 free((void *)str);
429                                         } else {
430                                                 str = sym_get_string_value(sym);
431                                                 printf("%s%s=%s\n", CONFIG_, sym->name, str);
432                                         }
433                                 }
434                         } else {
435                                 if (!conf_cnt++)
436                                         printf(_("*\n* Restart config...\n*\n"));
437                                 rootEntry = menu_get_parent_menu(menu);
438                                 conf(rootEntry);
439                         }
440                 }
441         }
442
443         for (child = menu->list; child; child = child->next)
444                 check_conf(child);
445 }
446
447 static struct option long_opts[] = {
448         {"askconfig",       no_argument,       NULL, oldaskconfig},
449         {"config",          no_argument,       NULL, oldconfig},
450         {"defconfig",       optional_argument, NULL, defconfig},
451         {"savedefconfig",   required_argument, NULL, savedefconfig},
452         {"allnoconfig",     no_argument,       NULL, allnoconfig},
453         {"allyesconfig",    no_argument,       NULL, allyesconfig},
454         {"allmodconfig",    no_argument,       NULL, allmodconfig},
455         {"alldefconfig",    no_argument,       NULL, alldefconfig},
456         {"randconfig",      no_argument,       NULL, randconfig},
457         {"listnewconfig",   no_argument,       NULL, listnewconfig},
458         {"noconfig",        no_argument,       NULL, oldnoconfig},
459         {NULL, 0, NULL, 0}
460 };
461
462 static void conf_usage(const char *progname)
463 {
464
465         printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
466         printf("[option] is _one_ of the following:\n");
467         printf("  --listnewconfig         List new options\n");
468         printf("  --askconfig             Start a new configuration using a line-oriented program\n");
469         printf("  --config                Update a configuration using a provided .config as base\n");
470         printf("  --silentconfig          Same as config, but quietly, additionally update deps\n");
471         printf("  --noconfig              Same as silentconfig but set new symbols to no\n");
472         printf("  --defconfig <file>      New config with default defined in <file>\n");
473         printf("  --savedefconfig <file>  Save the minimal current configuration to <file>\n");
474         printf("  --allnoconfig           New config where all options are answered with no\n");
475         printf("  --allyesconfig          New config where all options are answered with yes\n");
476         printf("  --allmodconfig          New config where all options are answered with mod\n");
477         printf("  --alldefconfig          New config with all symbols set to default\n");
478         printf("  --randconfig            New config with random answer to all options\n");
479 }
480
481 int main(int ac, char **av)
482 {
483         const char *progname = av[0];
484         int opt;
485         const char *name, *defconfig_file = NULL /* gcc uninit */;
486         struct stat tmpstat;
487
488         setlocale(LC_ALL, "");
489         bindtextdomain(PACKAGE, LOCALEDIR);
490         textdomain(PACKAGE);
491
492         tty_stdio = isatty(0) && isatty(1);
493
494         while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
495                 if (opt == 's') {
496                         conf_set_message_callback(NULL);
497                         continue;
498                 }
499                 input_mode = (enum input_mode)opt;
500                 switch (opt) {
501                 case defconfig:
502                 case savedefconfig:
503                         defconfig_file = optarg;
504                         break;
505                 case randconfig:
506                 {
507                         struct timeval now;
508                         unsigned int seed;
509                         char *seed_env;
510
511                         /*
512                          * Use microseconds derived seed,
513                          * compensate for systems where it may be zero
514                          */
515                         gettimeofday(&now, NULL);
516                         seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
517
518                         seed_env = getenv("KCONFIG_SEED");
519                         if( seed_env && *seed_env ) {
520                                 char *endp;
521                                 int tmp = (int)strtol(seed_env, &endp, 0);
522                                 if (*endp == '\0') {
523                                         seed = tmp;
524                                 }
525                         }
526                         fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
527                         srand(seed);
528                         break;
529                 }
530                 case oldaskconfig:
531                 case oldconfig:
532                 case allnoconfig:
533                 case allyesconfig:
534                 case allmodconfig:
535                 case alldefconfig:
536                 case listnewconfig:
537                 case oldnoconfig:
538                         break;
539                 case '?':
540                         conf_usage(progname);
541                         exit(1);
542                         break;
543                 }
544         }
545         if (ac == optind) {
546                 fprintf(stderr, _("%s: Kconfig file missing\n"), av[0]);
547                 conf_usage(progname);
548                 exit(1);
549         }
550         name = av[optind];
551         conf_parse(name);
552         //zconfdump(stdout);
553
554         switch (input_mode) {
555         case defconfig:
556                 if (!defconfig_file)
557                         defconfig_file = conf_get_default_confname();
558                 if (conf_read(defconfig_file)) {
559                         fprintf(stderr,
560                                 _("***\n"
561                                   "*** Can't find default configuration \"%s\"!\n"
562                                   "***\n"),
563                                 defconfig_file);
564                         exit(1);
565                 }
566                 break;
567         case savedefconfig:
568         case oldaskconfig:
569         case oldconfig:
570         case listnewconfig:
571         case oldnoconfig:
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         case oldnoconfig:
641                 /* Update until a loop caused no more changes */
642                 do {
643                         conf_cnt = 0;
644                         check_conf(&rootmenu);
645                 } while (conf_cnt);
646                 break;
647         }
648
649         if (input_mode == savedefconfig) {
650                 if (conf_write_defconfig(defconfig_file)) {
651                         fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
652                                 defconfig_file);
653                         return 1;
654                 }
655         } else if (input_mode != listnewconfig) {
656                 /*
657                  * build so we shall update autoconf.
658                  */
659                 if (conf_write(NULL)) {
660                         fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
661                         exit(1);
662                 }
663                 if (conf_write_autoconf()) {
664                         fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
665                         return 1;
666                 }
667         }
668         return 0;
669 }