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