c4bc255a95d5232b0249e63d99a529da2064ed9d
[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 && !sym_is_choice_value(sym)) {
422                                         printf("%s%s\n", CONFIG_, sym->name);
423                                 }
424                         } else {
425                                 if (!conf_cnt++)
426                                         printf(_("*\n* Restart config...\n*\n"));
427                                 rootEntry = menu_get_parent_menu(menu);
428                                 conf(rootEntry);
429                         }
430                 }
431         }
432
433         for (child = menu->list; child; child = child->next)
434                 check_conf(child);
435 }
436
437 static struct option long_opts[] = {
438         {"askconfig",       no_argument,       NULL, oldaskconfig},
439         {"config",          no_argument,       NULL, oldconfig},
440         {"defconfig",       optional_argument, NULL, defconfig},
441         {"savedefconfig",   required_argument, NULL, savedefconfig},
442         {"allnoconfig",     no_argument,       NULL, allnoconfig},
443         {"allyesconfig",    no_argument,       NULL, allyesconfig},
444         {"allmodconfig",    no_argument,       NULL, allmodconfig},
445         {"alldefconfig",    no_argument,       NULL, alldefconfig},
446         {"randconfig",      no_argument,       NULL, randconfig},
447         {"listnewconfig",   no_argument,       NULL, listnewconfig},
448         {"noconfig",        no_argument,       NULL, oldnoconfig},
449         {NULL, 0, NULL, 0}
450 };
451
452 static void conf_usage(const char *progname)
453 {
454
455         printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
456         printf("[option] is _one_ of the following:\n");
457         printf("  --listnewconfig         List new options\n");
458         printf("  --askconfig             Start a new configuration using a line-oriented program\n");
459         printf("  --config                Update a configuration using a provided .config as base\n");
460         printf("  --silentconfig          Same as config, but quietly, additionally update deps\n");
461         printf("  --noconfig              Same as silentconfig but set new symbols to no\n");
462         printf("  --defconfig <file>      New config with default defined in <file>\n");
463         printf("  --savedefconfig <file>  Save the minimal current configuration to <file>\n");
464         printf("  --allnoconfig           New config where all options are answered with no\n");
465         printf("  --allyesconfig          New config where all options are answered with yes\n");
466         printf("  --allmodconfig          New config where all options are answered with mod\n");
467         printf("  --alldefconfig          New config with all symbols set to default\n");
468         printf("  --randconfig            New config with random answer to all options\n");
469 }
470
471 int main(int ac, char **av)
472 {
473         const char *progname = av[0];
474         int opt;
475         const char *name, *defconfig_file = NULL /* gcc uninit */;
476         struct stat tmpstat;
477
478         setlocale(LC_ALL, "");
479         bindtextdomain(PACKAGE, LOCALEDIR);
480         textdomain(PACKAGE);
481
482         tty_stdio = isatty(0) && isatty(1);
483
484         while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
485                 if (opt == 's') {
486                         conf_set_message_callback(NULL);
487                         continue;
488                 }
489                 input_mode = (enum input_mode)opt;
490                 switch (opt) {
491                 case defconfig:
492                 case savedefconfig:
493                         defconfig_file = optarg;
494                         break;
495                 case randconfig:
496                 {
497                         struct timeval now;
498                         unsigned int seed;
499                         char *seed_env;
500
501                         /*
502                          * Use microseconds derived seed,
503                          * compensate for systems where it may be zero
504                          */
505                         gettimeofday(&now, NULL);
506                         seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
507
508                         seed_env = getenv("KCONFIG_SEED");
509                         if( seed_env && *seed_env ) {
510                                 char *endp;
511                                 int tmp = (int)strtol(seed_env, &endp, 0);
512                                 if (*endp == '\0') {
513                                         seed = tmp;
514                                 }
515                         }
516                         fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
517                         srand(seed);
518                         break;
519                 }
520                 case oldaskconfig:
521                 case oldconfig:
522                 case allnoconfig:
523                 case allyesconfig:
524                 case allmodconfig:
525                 case alldefconfig:
526                 case listnewconfig:
527                 case oldnoconfig:
528                         break;
529                 case '?':
530                         conf_usage(progname);
531                         exit(1);
532                         break;
533                 }
534         }
535         if (ac == optind) {
536                 fprintf(stderr, _("%s: Kconfig file missing\n"), av[0]);
537                 conf_usage(progname);
538                 exit(1);
539         }
540         name = av[optind];
541         conf_parse(name);
542         //zconfdump(stdout);
543
544         switch (input_mode) {
545         case defconfig:
546                 if (!defconfig_file)
547                         defconfig_file = conf_get_default_confname();
548                 if (conf_read(defconfig_file)) {
549                         fprintf(stderr,
550                                 _("***\n"
551                                   "*** Can't find default configuration \"%s\"!\n"
552                                   "***\n"),
553                                 defconfig_file);
554                         exit(1);
555                 }
556                 break;
557         case savedefconfig:
558         case oldaskconfig:
559         case oldconfig:
560         case listnewconfig:
561         case oldnoconfig:
562                 conf_read(NULL);
563                 break;
564         case allnoconfig:
565         case allyesconfig:
566         case allmodconfig:
567         case alldefconfig:
568         case randconfig:
569                 name = getenv("KCONFIG_ALLCONFIG");
570                 if (!name)
571                         break;
572                 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
573                         if (conf_read_simple(name, S_DEF_USER)) {
574                                 fprintf(stderr,
575                                         _("*** Can't read seed configuration \"%s\"!\n"),
576                                         name);
577                                 exit(1);
578                         }
579                         break;
580                 }
581                 switch (input_mode) {
582                 case allnoconfig:       name = "allno.config"; break;
583                 case allyesconfig:      name = "allyes.config"; break;
584                 case allmodconfig:      name = "allmod.config"; break;
585                 case alldefconfig:      name = "alldef.config"; break;
586                 case randconfig:        name = "allrandom.config"; break;
587                 default: break;
588                 }
589                 if (conf_read_simple(name, S_DEF_USER) &&
590                     conf_read_simple("all.config", S_DEF_USER)) {
591                         fprintf(stderr,
592                                 _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
593                                 name);
594                         exit(1);
595                 }
596                 break;
597         default:
598                 break;
599         }
600
601         switch (input_mode) {
602         case allnoconfig:
603                 conf_set_all_new_symbols(def_no);
604                 break;
605         case allyesconfig:
606                 conf_set_all_new_symbols(def_yes);
607                 break;
608         case allmodconfig:
609                 conf_set_all_new_symbols(def_mod);
610                 break;
611         case alldefconfig:
612                 conf_set_all_new_symbols(def_default);
613                 break;
614         case randconfig:
615                 /* Really nothing to do in this loop */
616                 while (conf_set_all_new_symbols(def_random)) ;
617                 break;
618         case defconfig:
619                 conf_set_all_new_symbols(def_default);
620                 break;
621         case savedefconfig:
622                 break;
623         case oldaskconfig:
624                 rootEntry = &rootmenu;
625                 conf(&rootmenu);
626                 input_mode = oldconfig;
627                 /* fall through */
628         case oldconfig:
629         case listnewconfig:
630         case oldnoconfig:
631                 /* Update until a loop caused no more changes */
632                 do {
633                         conf_cnt = 0;
634                         check_conf(&rootmenu);
635                 } while (conf_cnt);
636                 break;
637         }
638
639         if (input_mode == savedefconfig) {
640                 if (conf_write_defconfig(defconfig_file)) {
641                         fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
642                                 defconfig_file);
643                         return 1;
644                 }
645         } else if (input_mode != listnewconfig) {
646                 /*
647                  * build so we shall update autoconf.
648                  */
649                 if (conf_write(NULL)) {
650                         fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
651                         exit(1);
652                 }
653                 if (conf_write_autoconf()) {
654                         fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
655                         return 1;
656                 }
657         }
658         return 0;
659 }