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