blob: e39fcd8143ea78b2c9c10a2983610fe75bbcb2e3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
4 */
5
EGRY Gabor534a4502008-01-11 23:44:39 +01006#include <locale.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <ctype.h>
Randy Dunlap9dfb5632006-04-18 22:21:53 -07008#include <stdio.h>
Ladislav Michl75ff4302008-01-09 16:36:19 +01009#include <stdlib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <time.h>
Ladislav Michl75ff4302008-01-09 16:36:19 +010012#include <unistd.h>
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020013#include <getopt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <sys/stat.h>
Ingo Molnarb0fe5512009-03-12 15:15:31 +010015#include <sys/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "lkc.h"
18
19static void conf(struct menu *menu);
20static void check_conf(struct menu *menu);
Arnaud Lacombeab63f582011-07-02 00:59:41 -040021static void xfgets(char *str, int size, FILE *in);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020023enum input_mode {
24 oldaskconfig,
25 silentoldconfig,
26 oldconfig,
27 allnoconfig,
28 allyesconfig,
29 allmodconfig,
Sam Ravnborg0748cb32010-07-31 23:35:31 +020030 alldefconfig,
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020031 randconfig,
32 defconfig,
Sam Ravnborg7cf3d732010-07-31 23:35:34 +020033 savedefconfig,
Sam Ravnborg861b4ea2010-07-31 23:35:28 +020034 listnewconfig,
Adam Leefb16d892012-09-01 01:05:17 +080035 olddefconfig,
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020036} input_mode = oldaskconfig;
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static int indent = 1;
Ben Hutchings62dc9892013-02-19 02:24:26 +020039static int tty_stdio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static int valid_stdin = 1;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +020041static int sync_kconfig;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static int conf_cnt;
J.A. Magallon48b9d032005-06-25 14:59:22 -070043static char line[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static struct menu *rootEntry;
45
Cheng Renquan66c4bd82009-07-12 16:11:48 +080046static void print_help(struct menu *menu)
Sam Ravnborg03d29122007-07-21 00:00:36 +020047{
Cheng Renquan66c4bd82009-07-12 16:11:48 +080048 struct gstr help = str_new();
49
50 menu_get_ext_help(menu, &help);
51
52 printf("\n%s\n", str_get(&help));
53 str_free(&help);
Sam Ravnborg03d29122007-07-21 00:00:36 +020054}
55
J.A. Magallon48b9d032005-06-25 14:59:22 -070056static void strip(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
J.A. Magallon48b9d032005-06-25 14:59:22 -070058 char *p = str;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 int l;
60
61 while ((isspace(*p)))
62 p++;
63 l = strlen(p);
64 if (p != str)
65 memmove(str, p, l + 1);
66 if (!l)
67 return;
68 p = str + l - 1;
69 while ((isspace(*p)))
70 *p-- = 0;
71}
72
73static void check_stdin(void)
74{
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +020075 if (!valid_stdin) {
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -070076 printf(_("aborted!\n\n"));
77 printf(_("Console input/output is redirected. "));
78 printf(_("Run 'make oldconfig' to update configuration.\n\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 exit(1);
80 }
81}
82
Roman Zippelf82f3f92007-08-30 05:06:17 +020083static int conf_askvalue(struct symbol *sym, const char *def)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 enum symbol_type type = sym_get_type(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 if (!sym_has_value(sym))
EGRY Gabor534a4502008-01-11 23:44:39 +010088 printf(_("(NEW) "));
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 line[0] = '\n';
91 line[1] = 0;
92
93 if (!sym_is_changable(sym)) {
94 printf("%s\n", def);
95 line[0] = '\n';
96 line[1] = 0;
Roman Zippelf82f3f92007-08-30 05:06:17 +020097 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99
100 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200101 case oldconfig:
102 case silentoldconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (sym_has_value(sym)) {
104 printf("%s\n", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107 check_stdin();
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400108 /* fall through */
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200109 case oldaskconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 fflush(stdout);
Jean Sacren4418a2b2010-08-04 16:03:16 -0600111 xfgets(line, 128, stdin);
Ben Hutchings62dc9892013-02-19 02:24:26 +0200112 if (!tty_stdio)
113 printf("\n");
Roman Zippelf82f3f92007-08-30 05:06:17 +0200114 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 default:
116 break;
117 }
118
119 switch (type) {
120 case S_INT:
121 case S_HEX:
122 case S_STRING:
123 printf("%s\n", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200124 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 default:
126 ;
127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 printf("%s", line);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200129 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Trevor Keith4356f482009-09-18 12:49:23 -0700132static int conf_string(struct menu *menu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 struct symbol *sym = menu->sym;
Sam Ravnborg03d29122007-07-21 00:00:36 +0200135 const char *def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 while (1) {
EGRY Gabor534a4502008-01-11 23:44:39 +0100138 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 printf("(%s) ", sym->name);
140 def = sym_get_string_value(sym);
141 if (sym_get_string_value(sym))
142 printf("[%s] ", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200143 if (!conf_askvalue(sym, def))
144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 switch (line[0]) {
146 case '\n':
147 break;
148 case '?':
149 /* print help */
150 if (line[1] == '\n') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800151 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 def = NULL;
153 break;
154 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400155 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 default:
157 line[strlen(line)-1] = 0;
158 def = line;
159 }
160 if (def && sym_set_string_value(sym, def))
161 return 0;
162 }
163}
164
165static int conf_sym(struct menu *menu)
166{
167 struct symbol *sym = menu->sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 tristate oldval, newval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 while (1) {
EGRY Gabor534a4502008-01-11 23:44:39 +0100171 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (sym->name)
173 printf("(%s) ", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 putchar('[');
175 oldval = sym_get_tristate_value(sym);
176 switch (oldval) {
177 case no:
178 putchar('N');
179 break;
180 case mod:
181 putchar('M');
182 break;
183 case yes:
184 putchar('Y');
185 break;
186 }
187 if (oldval != no && sym_tristate_within_range(sym, no))
188 printf("/n");
189 if (oldval != mod && sym_tristate_within_range(sym, mod))
190 printf("/m");
191 if (oldval != yes && sym_tristate_within_range(sym, yes))
192 printf("/y");
Sam Ravnborg03d29122007-07-21 00:00:36 +0200193 if (menu_has_help(menu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 printf("/?");
195 printf("] ");
Roman Zippelf82f3f92007-08-30 05:06:17 +0200196 if (!conf_askvalue(sym, sym_get_string_value(sym)))
197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 strip(line);
199
200 switch (line[0]) {
201 case 'n':
202 case 'N':
203 newval = no;
204 if (!line[1] || !strcmp(&line[1], "o"))
205 break;
206 continue;
207 case 'm':
208 case 'M':
209 newval = mod;
210 if (!line[1])
211 break;
212 continue;
213 case 'y':
214 case 'Y':
215 newval = yes;
216 if (!line[1] || !strcmp(&line[1], "es"))
217 break;
218 continue;
219 case 0:
220 newval = oldval;
221 break;
222 case '?':
223 goto help;
224 default:
225 continue;
226 }
227 if (sym_set_tristate_value(sym, newval))
228 return 0;
229help:
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800230 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232}
233
234static int conf_choice(struct menu *menu)
235{
236 struct symbol *sym, *def_sym;
237 struct menu *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 bool is_new;
239
240 sym = menu->sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 is_new = !sym_has_value(sym);
242 if (sym_is_changable(sym)) {
243 conf_sym(menu);
244 sym_calc_value(sym);
245 switch (sym_get_tristate_value(sym)) {
246 case no:
247 return 1;
248 case mod:
249 return 0;
250 case yes:
251 break;
252 }
253 } else {
254 switch (sym_get_tristate_value(sym)) {
255 case no:
256 return 1;
257 case mod:
EGRY Gabor534a4502008-01-11 23:44:39 +0100258 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return 0;
260 case yes:
261 break;
262 }
263 }
264
265 while (1) {
266 int cnt, def;
267
EGRY Gabor534a4502008-01-11 23:44:39 +0100268 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 def_sym = sym_get_choice_value(sym);
270 cnt = def = 0;
Roman Zippel40aee722006-04-09 17:26:39 +0200271 line[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 for (child = menu->list; child; child = child->next) {
273 if (!menu_is_visible(child))
274 continue;
275 if (!child->sym) {
EGRY Gabor534a4502008-01-11 23:44:39 +0100276 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 continue;
278 }
279 cnt++;
280 if (child->sym == def_sym) {
281 def = cnt;
282 printf("%*c", indent, '>');
283 } else
284 printf("%*c", indent, ' ');
EGRY Gabor534a4502008-01-11 23:44:39 +0100285 printf(" %d. %s", cnt, _(menu_get_prompt(child)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (child->sym->name)
287 printf(" (%s)", child->sym->name);
288 if (!sym_has_value(child->sym))
EGRY Gabor534a4502008-01-11 23:44:39 +0100289 printf(_(" (NEW)"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 printf("\n");
291 }
EGRY Gabor534a4502008-01-11 23:44:39 +0100292 printf(_("%*schoice"), indent - 1, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (cnt == 1) {
294 printf("[1]: 1\n");
295 goto conf_childs;
296 }
297 printf("[1-%d", cnt);
Sam Ravnborg03d29122007-07-21 00:00:36 +0200298 if (menu_has_help(menu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 printf("?");
300 printf("]: ");
301 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200302 case oldconfig:
303 case silentoldconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (!is_new) {
305 cnt = def;
306 printf("%d\n", cnt);
307 break;
308 }
309 check_stdin();
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400310 /* fall through */
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200311 case oldaskconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 fflush(stdout);
Jean Sacren4418a2b2010-08-04 16:03:16 -0600313 xfgets(line, 128, stdin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 strip(line);
315 if (line[0] == '?') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800316 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 continue;
318 }
319 if (!line[0])
320 cnt = def;
321 else if (isdigit(line[0]))
322 cnt = atoi(line);
323 else
324 continue;
325 break;
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200326 default:
327 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
330 conf_childs:
331 for (child = menu->list; child; child = child->next) {
332 if (!child->sym || !menu_is_visible(child))
333 continue;
334 if (!--cnt)
335 break;
336 }
337 if (!child)
338 continue;
Ben Hutchings3ba41622011-04-23 18:42:56 +0100339 if (line[0] && line[strlen(line) - 1] == '?') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800340 print_help(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 continue;
342 }
343 sym_set_choice_value(sym, child->sym);
Jan Beulichf5eaa322008-01-24 11:54:23 +0000344 for (child = child->list; child; child = child->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 indent += 2;
Jan Beulichf5eaa322008-01-24 11:54:23 +0000346 conf(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 indent -= 2;
348 }
349 return 1;
350 }
351}
352
353static void conf(struct menu *menu)
354{
355 struct symbol *sym;
356 struct property *prop;
357 struct menu *child;
358
359 if (!menu_is_visible(menu))
360 return;
361
362 sym = menu->sym;
363 prop = menu->prompt;
364 if (prop) {
365 const char *prompt;
366
367 switch (prop->type) {
368 case P_MENU:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200369 if ((input_mode == silentoldconfig ||
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200370 input_mode == listnewconfig ||
Adam Leefb16d892012-09-01 01:05:17 +0800371 input_mode == olddefconfig) &&
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400372 rootEntry != menu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 check_conf(menu);
374 return;
375 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400376 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 case P_COMMENT:
378 prompt = menu_get_prompt(menu);
379 if (prompt)
380 printf("%*c\n%*c %s\n%*c\n",
381 indent, '*',
EGRY Gabor534a4502008-01-11 23:44:39 +0100382 indent, '*', _(prompt),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 indent, '*');
384 default:
385 ;
386 }
387 }
388
389 if (!sym)
390 goto conf_childs;
391
392 if (sym_is_choice(sym)) {
393 conf_choice(menu);
394 if (sym->curr.tri != mod)
395 return;
396 goto conf_childs;
397 }
398
399 switch (sym->type) {
400 case S_INT:
401 case S_HEX:
402 case S_STRING:
403 conf_string(menu);
404 break;
405 default:
406 conf_sym(menu);
407 break;
408 }
409
410conf_childs:
411 if (sym)
412 indent += 2;
413 for (child = menu->list; child; child = child->next)
414 conf(child);
415 if (sym)
416 indent -= 2;
417}
418
419static void check_conf(struct menu *menu)
420{
421 struct symbol *sym;
422 struct menu *child;
423
424 if (!menu_is_visible(menu))
425 return;
426
427 sym = menu->sym;
Roman Zippel3f23ca22005-11-08 21:34:48 -0800428 if (sym && !sym_has_value(sym)) {
429 if (sym_is_changable(sym) ||
430 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200431 if (input_mode == listnewconfig) {
432 if (sym->name && !sym_is_choice_value(sym)) {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400433 printf("%s%s\n", CONFIG_, sym->name);
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400434 }
Adam Leefb16d892012-09-01 01:05:17 +0800435 } else if (input_mode != olddefconfig) {
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400436 if (!conf_cnt++)
437 printf(_("*\n* Restart config...\n*\n"));
438 rootEntry = menu_get_parent_menu(menu);
439 conf(rootEntry);
440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443
444 for (child = menu->list; child; child = child->next)
445 check_conf(child);
446}
447
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200448static struct option long_opts[] = {
449 {"oldaskconfig", no_argument, NULL, oldaskconfig},
450 {"oldconfig", no_argument, NULL, oldconfig},
451 {"silentoldconfig", no_argument, NULL, silentoldconfig},
452 {"defconfig", optional_argument, NULL, defconfig},
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200453 {"savedefconfig", required_argument, NULL, savedefconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200454 {"allnoconfig", no_argument, NULL, allnoconfig},
455 {"allyesconfig", no_argument, NULL, allyesconfig},
456 {"allmodconfig", no_argument, NULL, allmodconfig},
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200457 {"alldefconfig", no_argument, NULL, alldefconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200458 {"randconfig", no_argument, NULL, randconfig},
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200459 {"listnewconfig", no_argument, NULL, listnewconfig},
Adam Leefb16d892012-09-01 01:05:17 +0800460 {"olddefconfig", no_argument, NULL, olddefconfig},
461 /*
462 * oldnoconfig is an alias of olddefconfig, because people already
463 * are dependent on its behavior(sets new symbols to their default
464 * value but not 'n') with the counter-intuitive name.
465 */
466 {"oldnoconfig", no_argument, NULL, olddefconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200467 {NULL, 0, NULL, 0}
468};
469
Arnaud Lacombe32543992010-11-02 00:26:33 -0400470static void conf_usage(const char *progname)
471{
472
473 printf("Usage: %s [option] <kconfig-file>\n", progname);
474 printf("[option] is _one_ of the following:\n");
475 printf(" --listnewconfig List new options\n");
476 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
477 printf(" --oldconfig Update a configuration using a provided .config as base\n");
478 printf(" --silentoldconfig Same as oldconfig, but quietly, additionally update deps\n");
Adam Leefb16d892012-09-01 01:05:17 +0800479 printf(" --olddefconfig Same as silentoldconfig but sets new symbols to their default value\n");
480 printf(" --oldnoconfig An alias of olddefconfig\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400481 printf(" --defconfig <file> New config with default defined in <file>\n");
482 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
483 printf(" --allnoconfig New config where all options are answered with no\n");
484 printf(" --allyesconfig New config where all options are answered with yes\n");
485 printf(" --allmodconfig New config where all options are answered with mod\n");
486 printf(" --alldefconfig New config with all symbols set to default\n");
487 printf(" --randconfig New config with random answer to all options\n");
488}
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490int main(int ac, char **av)
491{
Arnaud Lacombe32543992010-11-02 00:26:33 -0400492 const char *progname = av[0];
Andres Salomon2f4b4892007-12-17 01:34:58 -0500493 int opt;
Arnaud Lacombe275744c2010-10-13 20:43:28 -0400494 const char *name, *defconfig_file = NULL /* gcc uninit */;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct stat tmpstat;
496
EGRY Gabor534a4502008-01-11 23:44:39 +0100497 setlocale(LC_ALL, "");
498 bindtextdomain(PACKAGE, LOCALEDIR);
499 textdomain(PACKAGE);
500
Ben Hutchings62dc9892013-02-19 02:24:26 +0200501 tty_stdio = isatty(0) && isatty(1) && isatty(2);
502
Arnaud Lacombec94d3fb2010-08-23 12:01:24 -0400503 while ((opt = getopt_long(ac, av, "", long_opts, NULL)) != -1) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200504 input_mode = (enum input_mode)opt;
Andres Salomon2f4b4892007-12-17 01:34:58 -0500505 switch (opt) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200506 case silentoldconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200507 sync_kconfig = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200509 case defconfig:
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200510 case savedefconfig:
Andres Salomon2f4b4892007-12-17 01:34:58 -0500511 defconfig_file = optarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200513 case randconfig:
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100514 {
515 struct timeval now;
516 unsigned int seed;
517
518 /*
519 * Use microseconds derived seed,
520 * compensate for systems where it may be zero
521 */
522 gettimeofday(&now, NULL);
523
524 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
525 srand(seed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 break;
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100527 }
Arnaud Lacombe32543992010-11-02 00:26:33 -0400528 case oldaskconfig:
529 case oldconfig:
530 case allnoconfig:
531 case allyesconfig:
532 case allmodconfig:
533 case alldefconfig:
534 case listnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800535 case olddefconfig:
Arnaud Lacombe32543992010-11-02 00:26:33 -0400536 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200537 case '?':
Arnaud Lacombe32543992010-11-02 00:26:33 -0400538 conf_usage(progname);
Andres Salomon2f4b4892007-12-17 01:34:58 -0500539 exit(1);
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200540 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542 }
Andres Salomon2f4b4892007-12-17 01:34:58 -0500543 if (ac == optind) {
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700544 printf(_("%s: Kconfig file missing\n"), av[0]);
Arnaud Lacombe32543992010-11-02 00:26:33 -0400545 conf_usage(progname);
Randy Dunlap250725a2006-06-08 22:12:50 -0700546 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
Andres Salomon2f4b4892007-12-17 01:34:58 -0500548 name = av[optind];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 conf_parse(name);
550 //zconfdump(stdout);
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200551 if (sync_kconfig) {
Markus Heidelberg284026c2009-05-18 01:36:53 +0200552 name = conf_get_configname();
553 if (stat(name, &tmpstat)) {
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200554 fprintf(stderr, _("***\n"
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400555 "*** Configuration file \"%s\" not found!\n"
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200556 "***\n"
557 "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
558 "*** \"make menuconfig\" or \"make xconfig\").\n"
Markus Heidelberg284026c2009-05-18 01:36:53 +0200559 "***\n"), name);
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200560 exit(1);
561 }
562 }
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200565 case defconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (!defconfig_file)
567 defconfig_file = conf_get_default_confname();
568 if (conf_read(defconfig_file)) {
EGRY Gabor534a4502008-01-11 23:44:39 +0100569 printf(_("***\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 "*** Can't find default configuration \"%s\"!\n"
EGRY Gabor534a4502008-01-11 23:44:39 +0100571 "***\n"), defconfig_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 exit(1);
573 }
574 break;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200575 case savedefconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200576 case silentoldconfig:
577 case oldaskconfig:
578 case oldconfig:
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200579 case listnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800580 case olddefconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 conf_read(NULL);
582 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200583 case allnoconfig:
584 case allyesconfig:
585 case allmodconfig:
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200586 case alldefconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200587 case randconfig:
Roman Zippel90389162005-11-08 21:34:49 -0800588 name = getenv("KCONFIG_ALLCONFIG");
Eric W. Biederman9f420bf2012-05-07 05:37:45 -0700589 if (!name)
590 break;
591 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700592 if (conf_read_simple(name, S_DEF_USER)) {
593 fprintf(stderr,
594 _("*** Can't read seed configuration \"%s\"!\n"),
595 name);
596 exit(1);
597 }
Roman Zippel90389162005-11-08 21:34:49 -0800598 break;
599 }
600 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200601 case allnoconfig: name = "allno.config"; break;
602 case allyesconfig: name = "allyes.config"; break;
603 case allmodconfig: name = "allmod.config"; break;
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200604 case alldefconfig: name = "alldef.config"; break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200605 case randconfig: name = "allrandom.config"; break;
Roman Zippel90389162005-11-08 21:34:49 -0800606 default: break;
607 }
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700608 if (conf_read_simple(name, S_DEF_USER) &&
609 conf_read_simple("all.config", S_DEF_USER)) {
610 fprintf(stderr,
611 _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
612 name);
613 exit(1);
614 }
Roman Zippel90389162005-11-08 21:34:49 -0800615 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 default:
617 break;
618 }
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200619
620 if (sync_kconfig) {
621 if (conf_get_changed()) {
622 name = getenv("KCONFIG_NOSILENTUPDATE");
623 if (name && *name) {
624 fprintf(stderr,
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400625 _("\n*** The configuration requires explicit update.\n\n"));
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200626 return 1;
627 }
628 }
Ben Hutchings62dc9892013-02-19 02:24:26 +0200629 valid_stdin = tty_stdio;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200630 }
631
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200632 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200633 case allnoconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200634 conf_set_all_new_symbols(def_no);
635 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200636 case allyesconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200637 conf_set_all_new_symbols(def_yes);
638 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200639 case allmodconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200640 conf_set_all_new_symbols(def_mod);
641 break;
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200642 case alldefconfig:
643 conf_set_all_new_symbols(def_default);
644 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200645 case randconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200646 conf_set_all_new_symbols(def_random);
647 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200648 case defconfig:
Sam Ravnborg09748e12008-06-30 23:02:59 +0200649 conf_set_all_new_symbols(def_default);
650 break;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200651 case savedefconfig:
652 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200653 case oldaskconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200654 rootEntry = &rootmenu;
655 conf(&rootmenu);
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200656 input_mode = silentoldconfig;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200657 /* fall through */
Sam Ravnborg14828342010-08-06 07:13:54 +0200658 case oldconfig:
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200659 case listnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800660 case olddefconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200661 case silentoldconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200662 /* Update until a loop caused no more changes */
663 do {
664 conf_cnt = 0;
665 check_conf(&rootmenu);
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400666 } while (conf_cnt &&
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200667 (input_mode != listnewconfig &&
Adam Leefb16d892012-09-01 01:05:17 +0800668 input_mode != olddefconfig));
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200669 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200671
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200672 if (sync_kconfig) {
673 /* silentoldconfig is used during the build so we shall update autoconf.
674 * All other commands are only used to generate a config.
675 */
676 if (conf_get_changed() && conf_write(NULL)) {
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400677 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200678 exit(1);
679 }
680 if (conf_write_autoconf()) {
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400681 fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200682 return 1;
683 }
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200684 } else if (input_mode == savedefconfig) {
685 if (conf_write_defconfig(defconfig_file)) {
686 fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
687 defconfig_file);
688 return 1;
689 }
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200690 } else if (input_mode != listnewconfig) {
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200691 if (conf_write(NULL)) {
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400692 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200693 exit(1);
694 }
Roman Zippelc955cca2006-06-08 22:12:39 -0700695 }
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
Arnaud Lacombeab63f582011-07-02 00:59:41 -0400698
Jean Sacren4418a2b2010-08-04 16:03:16 -0600699/*
700 * Helper function to facilitate fgets() by Jean Sacren.
701 */
Arnaud Lacombeab63f582011-07-02 00:59:41 -0400702void xfgets(char *str, int size, FILE *in)
Jean Sacren4418a2b2010-08-04 16:03:16 -0600703{
704 if (fgets(str, size, in) == NULL)
705 fprintf(stderr, "\nError in reading or end of file.\n");
706}