support for nonint_oldconfig
[linux-2.6.git] / scripts / kconfig / 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 <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <time.h>
12 #include <sys/stat.h>
13
14 #define LKC_DIRECT_LINK
15 #include "lkc.h"
16
17 static void conf(struct menu *menu);
18 static void check_conf(struct menu *menu);
19
20 enum {
21         ask_all,
22         ask_new,
23         ask_silent,
24         dont_ask,
25         set_default,
26         set_yes,
27         set_mod,
28         set_no,
29         set_random
30 } input_mode = ask_all;
31 char *defconfig_file;
32
33 static int indent = 1;
34 static int valid_stdin = 1;
35 static int conf_cnt;
36 static char line[128];
37 static struct menu *rootEntry;
38
39 static char nohelp_text[] = N_("Sorry, no help available for this option yet.\n");
40
41 static int return_value = 0;
42
43 static void strip(char *str)
44 {
45         char *p = str;
46         int l;
47
48         while ((isspace(*p)))
49                 p++;
50         l = strlen(p);
51         if (p != str)
52                 memmove(str, p, l + 1);
53         if (!l)
54                 return;
55         p = str + l - 1;
56         while ((isspace(*p)))
57                 *p-- = 0;
58 }
59
60 static void check_stdin(void)
61 {
62         if (!valid_stdin && input_mode == ask_silent) {
63                 printf(_("aborted!\n\n"));
64                 printf(_("Console input/output is redirected. "));
65                 printf(_("Run 'make oldconfig' to update configuration.\n\n"));
66                 exit(1);
67         }
68 }
69
70 static char *fgets_check_stream(char *s, int size, FILE *stream)
71 {
72         char *ret = fgets(s, size, stream);
73
74         if (ret == NULL && feof(stream)) {
75                 printf(_("aborted!\n\n"));
76                 printf(_("Console input is closed. "));
77                 printf(_("Run 'make oldconfig' to update configuration.\n\n"));
78                 exit(1);
79         }
80
81         return ret;
82 }
83
84 static void conf_askvalue(struct symbol *sym, const char *def)
85 {
86         enum symbol_type type = sym_get_type(sym);
87         tristate val;
88
89         if (!sym_has_value(sym))
90                 printf("(NEW) ");
91
92         line[0] = '\n';
93         line[1] = 0;
94
95         if (!sym_is_changable(sym)) {
96                 printf("%s\n", def);
97                 line[0] = '\n';
98                 line[1] = 0;
99                 return;
100         }
101
102         switch (input_mode) {
103         case set_no:
104         case set_mod:
105         case set_yes:
106         case set_random:
107                 if (sym_has_value(sym)) {
108                         printf("%s\n", def);
109                         return;
110                 }
111                 break;
112         case ask_new:
113         case ask_silent:
114                 if (sym_has_value(sym)) {
115                         printf("%s\n", def);
116                         return;
117                 }
118                 check_stdin();
119         case ask_all:
120                 fflush(stdout);
121                 fgets_check_stream(line, 128, stdin);
122                 return;
123         case dont_ask:
124                 if (!sym_has_value(sym)) {
125                         fprintf(stderr,"CONFIG_%s\n",sym->name);
126                         return_value++;
127                 }
128                 return;
129         case set_default:
130                 printf("%s\n", def);
131                 return;
132         default:
133                 break;
134         }
135
136         switch (type) {
137         case S_INT:
138         case S_HEX:
139         case S_STRING:
140                 printf("%s\n", def);
141                 return;
142         default:
143                 ;
144         }
145         switch (input_mode) {
146         case set_yes:
147                 if (sym_tristate_within_range(sym, yes)) {
148                         line[0] = 'y';
149                         line[1] = '\n';
150                         line[2] = 0;
151                         break;
152                 }
153         case set_mod:
154                 if (type == S_TRISTATE) {
155                         if (sym_tristate_within_range(sym, mod)) {
156                                 line[0] = 'm';
157                                 line[1] = '\n';
158                                 line[2] = 0;
159                                 break;
160                         }
161                 } else {
162                         if (sym_tristate_within_range(sym, yes)) {
163                                 line[0] = 'y';
164                                 line[1] = '\n';
165                                 line[2] = 0;
166                                 break;
167                         }
168                 }
169         case set_no:
170                 if (sym_tristate_within_range(sym, no)) {
171                         line[0] = 'n';
172                         line[1] = '\n';
173                         line[2] = 0;
174                         break;
175                 }
176         case set_random:
177                 do {
178                         val = (tristate)(random() % 3);
179                 } while (!sym_tristate_within_range(sym, val));
180                 switch (val) {
181                 case no: line[0] = 'n'; break;
182                 case mod: line[0] = 'm'; break;
183                 case yes: line[0] = 'y'; break;
184                 }
185                 line[1] = '\n';
186                 line[2] = 0;
187                 break;
188         default:
189                 break;
190         }
191         printf("%s", line);
192 }
193
194 int conf_string(struct menu *menu)
195 {
196         struct symbol *sym = menu->sym;
197         const char *def, *help;
198
199         while (1) {
200                 printf("%*s%s ", indent - 1, "", menu->prompt->text);
201                 printf("(%s) ", sym->name);
202                 def = sym_get_string_value(sym);
203                 if (sym_get_string_value(sym))
204                         printf("[%s] ", def);
205                 conf_askvalue(sym, def);
206                 switch (line[0]) {
207                 case '\n':
208                         break;
209                 case '?':
210                         /* print help */
211                         if (line[1] == '\n') {
212                                 help = nohelp_text;
213                                 if (menu->sym->help)
214                                         help = menu->sym->help;
215                                 printf("\n%s\n", menu->sym->help);
216                                 def = NULL;
217                                 break;
218                         }
219                 default:
220                         line[strlen(line)-1] = 0;
221                         def = line;
222                 }
223                 if (def && sym_set_string_value(sym, def))
224                         return 0;
225         }
226 }
227
228 static int conf_sym(struct menu *menu)
229 {
230         struct symbol *sym = menu->sym;
231         int type;
232         tristate oldval, newval;
233         const char *help;
234
235         while (1) {
236                 printf("%*s%s ", indent - 1, "", menu->prompt->text);
237                 if (sym->name)
238                         printf("(%s) ", sym->name);
239                 type = sym_get_type(sym);
240                 putchar('[');
241                 oldval = sym_get_tristate_value(sym);
242                 switch (oldval) {
243                 case no:
244                         putchar('N');
245                         break;
246                 case mod:
247                         putchar('M');
248                         break;
249                 case yes:
250                         putchar('Y');
251                         break;
252                 }
253                 if (oldval != no && sym_tristate_within_range(sym, no))
254                         printf("/n");
255                 if (oldval != mod && sym_tristate_within_range(sym, mod))
256                         printf("/m");
257                 if (oldval != yes && sym_tristate_within_range(sym, yes))
258                         printf("/y");
259                 if (sym->help)
260                         printf("/?");
261                 printf("] ");
262                 conf_askvalue(sym, sym_get_string_value(sym));
263                 strip(line);
264
265                 switch (line[0]) {
266                 case 'n':
267                 case 'N':
268                         newval = no;
269                         if (!line[1] || !strcmp(&line[1], "o"))
270                                 break;
271                         continue;
272                 case 'm':
273                 case 'M':
274                         newval = mod;
275                         if (!line[1])
276                                 break;
277                         continue;
278                 case 'y':
279                 case 'Y':
280                         newval = yes;
281                         if (!line[1] || !strcmp(&line[1], "es"))
282                                 break;
283                         continue;
284                 case 0:
285                         newval = oldval;
286                         break;
287                 case '?':
288                         goto help;
289                 default:
290                         continue;
291                 }
292                 if (sym_set_tristate_value(sym, newval))
293                         return 0;
294 help:
295                 help = nohelp_text;
296                 if (sym->help)
297                         help = sym->help;
298                 printf("\n%s\n", help);
299         }
300 }
301
302 static int conf_choice(struct menu *menu)
303 {
304         struct symbol *sym, *def_sym;
305         struct menu *child;
306         int type;
307         bool is_new;
308
309         sym = menu->sym;
310         type = sym_get_type(sym);
311         is_new = !sym_has_value(sym);
312         if (sym_is_changable(sym)) {
313                 conf_sym(menu);
314                 sym_calc_value(sym);
315                 switch (sym_get_tristate_value(sym)) {
316                 case no:
317                         return 1;
318                 case mod:
319                         return 0;
320                 case yes:
321                         break;
322                 }
323         } else {
324                 switch (sym_get_tristate_value(sym)) {
325                 case no:
326                         return 1;
327                 case mod:
328                         printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
329                         return 0;
330                 case yes:
331                         break;
332                 }
333         }
334
335         while (1) {
336                 int cnt, def;
337
338                 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
339                 def_sym = sym_get_choice_value(sym);
340                 cnt = def = 0;
341                 line[0] = '0';
342                 line[1] = 0;
343                 for (child = menu->list; child; child = child->next) {
344                         if (!menu_is_visible(child))
345                                 continue;
346                         if (!child->sym) {
347                                 printf("%*c %s\n", indent, '*', menu_get_prompt(child));
348                                 continue;
349                         }
350                         cnt++;
351                         if (child->sym == def_sym) {
352                                 def = cnt;
353                                 printf("%*c", indent, '>');
354                         } else
355                                 printf("%*c", indent, ' ');
356                         printf(" %d. %s", cnt, menu_get_prompt(child));
357                         if (child->sym->name)
358                                 printf(" (%s)", child->sym->name);
359                         if (!sym_has_value(child->sym))
360                                 printf(" (NEW)");
361                         printf("\n");
362                 }
363                 printf("%*schoice", indent - 1, "");
364                 if (cnt == 1) {
365                         printf("[1]: 1\n");
366                         goto conf_childs;
367                 }
368                 printf("[1-%d", cnt);
369                 if (sym->help)
370                         printf("?");
371                 printf("]: ");
372                 switch (input_mode) {
373                 case dont_ask:
374                         cnt = def;
375                         printf("%d\n", cnt);
376                         break;
377                 case ask_new:
378                 case ask_silent:
379                         if (!is_new) {
380                                 cnt = def;
381                                 printf("%d\n", cnt);
382                                 break;
383                         }
384                         check_stdin();
385                 case ask_all:
386                         fflush(stdout);
387                         fgets_check_stream(line, 128, stdin);
388                         strip(line);
389                         if (line[0] == '?') {
390                                 printf("\n%s\n", menu->sym->help ?
391                                         menu->sym->help : nohelp_text);
392                                 continue;
393                         }
394                         if (!line[0])
395                                 cnt = def;
396                         else if (isdigit(line[0]))
397                                 cnt = atoi(line);
398                         else
399                                 continue;
400                         break;
401                 case set_random:
402                         def = (random() % cnt) + 1;
403                 case set_default:
404                 case set_yes:
405                 case set_mod:
406                 case set_no:
407                         cnt = def;
408                         printf("%d\n", cnt);
409                         break;
410                 }
411
412         conf_childs:
413                 for (child = menu->list; child; child = child->next) {
414                         if (!child->sym || !menu_is_visible(child))
415                                 continue;
416                         if (!--cnt)
417                                 break;
418                 }
419                 if (!child)
420                         continue;
421                 if (line[strlen(line) - 1] == '?') {
422                         printf("\n%s\n", child->sym->help ?
423                                 child->sym->help : nohelp_text);
424                         continue;
425                 }
426                 sym_set_choice_value(sym, child->sym);
427                 if (child->list) {
428                         indent += 2;
429                         conf(child->list);
430                         indent -= 2;
431                 }
432                 return 1;
433         }
434 }
435
436 static void conf(struct menu *menu)
437 {
438         struct symbol *sym;
439         struct property *prop;
440         struct menu *child;
441
442         if (!menu_is_visible(menu))
443                 return;
444
445         sym = menu->sym;
446         prop = menu->prompt;
447         if (prop) {
448                 const char *prompt;
449
450                 switch (prop->type) {
451                 case P_MENU:
452                         if (input_mode == ask_silent && rootEntry != menu) {
453                                 check_conf(menu);
454                                 return;
455                         }
456                 case P_COMMENT:
457                         prompt = menu_get_prompt(menu);
458                         if (prompt)
459                                 printf("%*c\n%*c %s\n%*c\n",
460                                         indent, '*',
461                                         indent, '*', prompt,
462                                         indent, '*');
463                 default:
464                         ;
465                 }
466         }
467
468         if (!sym)
469                 goto conf_childs;
470
471         if (sym_is_choice(sym)) {
472                 conf_choice(menu);
473                 if (sym->curr.tri != mod)
474                         return;
475                 goto conf_childs;
476         }
477
478         switch (sym->type) {
479         case S_INT:
480         case S_HEX:
481         case S_STRING:
482                 conf_string(menu);
483                 break;
484         default:
485                 conf_sym(menu);
486                 break;
487         }
488
489 conf_childs:
490         if (sym)
491                 indent += 2;
492         for (child = menu->list; child; child = child->next)
493                 conf(child);
494         if (sym)
495                 indent -= 2;
496 }
497
498 static void check_conf(struct menu *menu)
499 {
500         struct symbol *sym;
501         struct menu *child;
502
503         if (!menu_is_visible(menu))
504                 return;
505
506         sym = menu->sym;
507         if (sym && !sym_has_value(sym)) {
508                 if (sym_is_changable(sym) ||
509                     (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
510                         if (!conf_cnt++)
511                                 printf(_("*\n* Restart config...\n*\n"));
512                         rootEntry = menu_get_parent_menu(menu);
513                         if (input_mode == dont_ask)
514                                 fprintf(stderr,"CONFIG_%s\n",sym->name);
515                         else
516                                 conf(rootEntry);
517                 }
518         }
519
520         for (child = menu->list; child; child = child->next)
521                 check_conf(child);
522 }
523
524 int main(int ac, char **av)
525 {
526         int i = 1;
527         const char *name;
528         struct stat tmpstat;
529
530         if (ac > i && av[i][0] == '-') {
531                 switch (av[i++][1]) {
532                 case 'o':
533                         input_mode = ask_new;
534                         break;
535                 case 'b':
536                         input_mode = dont_ask;
537                         break;
538                 case 's':
539                         input_mode = ask_silent;
540                         valid_stdin = isatty(0) && isatty(1) && isatty(2);
541                         break;
542                 case 'd':
543                         input_mode = set_default;
544                         break;
545                 case 'D':
546                         input_mode = set_default;
547                         defconfig_file = av[i++];
548                         if (!defconfig_file) {
549                                 printf(_("%s: No default config file specified\n"),
550                                         av[0]);
551                                 exit(1);
552                         }
553                         break;
554                 case 'n':
555                         input_mode = set_no;
556                         break;
557                 case 'm':
558                         input_mode = set_mod;
559                         break;
560                 case 'y':
561                         input_mode = set_yes;
562                         break;
563                 case 'r':
564                         input_mode = set_random;
565                         srandom(time(NULL));
566                         break;
567                 case 'h':
568                 case '?':
569                         printf("%s [-o|-s] config\n", av[0]);
570                         exit(0);
571                 }
572         }
573         name = av[i];
574         if (!name) {
575                 printf(_("%s: Kconfig file missing\n"), av[0]);
576         }
577         conf_parse(name);
578         //zconfdump(stdout);
579         switch (input_mode) {
580         case set_default:
581                 if (!defconfig_file)
582                         defconfig_file = conf_get_default_confname();
583                 if (conf_read(defconfig_file)) {
584                         printf("***\n"
585                                 "*** Can't find default configuration \"%s\"!\n"
586                                 "***\n", defconfig_file);
587                         exit(1);
588                 }
589                 break;
590         case ask_silent:
591                 if (stat(".config", &tmpstat)) {
592                         printf(_("***\n"
593                                 "*** You have not yet configured your kernel!\n"
594                                 "***\n"
595                                 "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
596                                 "*** \"make menuconfig\" or \"make xconfig\").\n"
597                                 "***\n"));
598                         exit(1);
599                 }
600         case ask_all:
601         case ask_new:
602         case dont_ask:
603                 conf_read(NULL);
604                 break;
605         case set_no:
606         case set_mod:
607         case set_yes:
608         case set_random:
609                 name = getenv("KCONFIG_ALLCONFIG");
610                 if (name && !stat(name, &tmpstat)) {
611                         conf_read_simple(name);
612                         break;
613                 }
614                 switch (input_mode) {
615                 case set_no:     name = "allno.config"; break;
616                 case set_mod:    name = "allmod.config"; break;
617                 case set_yes:    name = "allyes.config"; break;
618                 case set_random: name = "allrandom.config"; break;
619                 default: break;
620                 }
621                 if (!stat(name, &tmpstat))
622                         conf_read_simple(name);
623                 else if (!stat("all.config", &tmpstat))
624                         conf_read_simple("all.config");
625                 break;
626         default:
627                 break;
628         }
629
630         if (input_mode != ask_silent) {
631                 rootEntry = &rootmenu;
632                 conf(&rootmenu);
633                 if (input_mode == ask_all) {
634                         input_mode = ask_silent;
635                         valid_stdin = 1;
636                 }
637         }
638         do {
639                 conf_cnt = 0;
640                 check_conf(&rootmenu);
641         } while ((conf_cnt) && (input_mode != dont_ask));
642         if (conf_write(NULL)) {
643                 fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
644                 return 1;
645         }
646         return return_value;
647 }