blob: 307bc3f7b945cdb2dbb054a3d28a39f87b8348e6 [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>
Markus Mayer74dba802015-12-09 14:56:12 -08008#include <limits.h>
Randy Dunlap9dfb5632006-04-18 22:21:53 -07009#include <stdio.h>
Ladislav Michl75ff4302008-01-09 16:36:19 +010010#include <stdlib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <time.h>
Ladislav Michl75ff4302008-01-09 16:36:19 +010013#include <unistd.h>
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020014#include <getopt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <sys/stat.h>
Ingo Molnarb0fe5512009-03-12 15:15:31 +010016#include <sys/time.h>
Yann E. MORIN0d8024c2013-04-13 22:49:13 +020017#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "lkc.h"
20
21static void conf(struct menu *menu);
22static void check_conf(struct menu *menu);
23
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020024enum input_mode {
25 oldaskconfig,
26 silentoldconfig,
27 oldconfig,
28 allnoconfig,
29 allyesconfig,
30 allmodconfig,
Sam Ravnborg0748cb32010-07-31 23:35:31 +020031 alldefconfig,
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020032 randconfig,
33 defconfig,
Sam Ravnborg7cf3d732010-07-31 23:35:34 +020034 savedefconfig,
Sam Ravnborg861b4ea2010-07-31 23:35:28 +020035 listnewconfig,
Adam Leefb16d892012-09-01 01:05:17 +080036 olddefconfig,
Masahiro Yamada52e58a32018-01-11 22:39:39 +090037};
38static enum input_mode input_mode = oldaskconfig;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static int indent = 1;
Ben Hutchings62dc9892013-02-19 02:24:26 +020041static int tty_stdio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static int valid_stdin = 1;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +020043static int sync_kconfig;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static int conf_cnt;
Markus Mayer74dba802015-12-09 14:56:12 -080045static char line[PATH_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static struct menu *rootEntry;
47
Cheng Renquan66c4bd82009-07-12 16:11:48 +080048static void print_help(struct menu *menu)
Sam Ravnborg03d29122007-07-21 00:00:36 +020049{
Cheng Renquan66c4bd82009-07-12 16:11:48 +080050 struct gstr help = str_new();
51
52 menu_get_ext_help(menu, &help);
53
54 printf("\n%s\n", str_get(&help));
55 str_free(&help);
Sam Ravnborg03d29122007-07-21 00:00:36 +020056}
57
J.A. Magallon48b9d032005-06-25 14:59:22 -070058static void strip(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
J.A. Magallon48b9d032005-06-25 14:59:22 -070060 char *p = str;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 int l;
62
63 while ((isspace(*p)))
64 p++;
65 l = strlen(p);
66 if (p != str)
67 memmove(str, p, l + 1);
68 if (!l)
69 return;
70 p = str + l - 1;
71 while ((isspace(*p)))
72 *p-- = 0;
73}
74
75static void check_stdin(void)
76{
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +020077 if (!valid_stdin) {
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -070078 printf(_("aborted!\n\n"));
79 printf(_("Console input/output is redirected. "));
80 printf(_("Run 'make oldconfig' to update configuration.\n\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 exit(1);
82 }
83}
84
Masahiro Yamada5a3dc712018-01-11 22:39:40 +090085/* Helper function to facilitate fgets() by Jean Sacren. */
86static void xfgets(char *str, int size, FILE *in)
87{
88 if (!fgets(str, size, in))
89 fprintf(stderr, "\nError in reading or end of file.\n");
90}
91
Roman Zippelf82f3f92007-08-30 05:06:17 +020092static int conf_askvalue(struct symbol *sym, const char *def)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 enum symbol_type type = sym_get_type(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 if (!sym_has_value(sym))
EGRY Gabor534a4502008-01-11 23:44:39 +010097 printf(_("(NEW) "));
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 line[0] = '\n';
100 line[1] = 0;
101
102 if (!sym_is_changable(sym)) {
103 printf("%s\n", def);
104 line[0] = '\n';
105 line[1] = 0;
Roman Zippelf82f3f92007-08-30 05:06:17 +0200106 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
109 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200110 case oldconfig:
111 case silentoldconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (sym_has_value(sym)) {
113 printf("%s\n", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116 check_stdin();
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400117 /* fall through */
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200118 case oldaskconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 fflush(stdout);
Markus Mayer74dba802015-12-09 14:56:12 -0800120 xfgets(line, sizeof(line), stdin);
Ben Hutchings62dc9892013-02-19 02:24:26 +0200121 if (!tty_stdio)
122 printf("\n");
Roman Zippelf82f3f92007-08-30 05:06:17 +0200123 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 default:
125 break;
126 }
127
128 switch (type) {
129 case S_INT:
130 case S_HEX:
131 case S_STRING:
132 printf("%s\n", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200133 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 default:
135 ;
136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 printf("%s", line);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200138 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Trevor Keith4356f482009-09-18 12:49:23 -0700141static int conf_string(struct menu *menu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 struct symbol *sym = menu->sym;
Sam Ravnborg03d29122007-07-21 00:00:36 +0200144 const char *def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 while (1) {
EGRY Gabor534a4502008-01-11 23:44:39 +0100147 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 printf("(%s) ", sym->name);
149 def = sym_get_string_value(sym);
150 if (sym_get_string_value(sym))
151 printf("[%s] ", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200152 if (!conf_askvalue(sym, def))
153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 switch (line[0]) {
155 case '\n':
156 break;
157 case '?':
158 /* print help */
159 if (line[1] == '\n') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800160 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 def = NULL;
162 break;
163 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400164 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 default:
166 line[strlen(line)-1] = 0;
167 def = line;
168 }
169 if (def && sym_set_string_value(sym, def))
170 return 0;
171 }
172}
173
174static int conf_sym(struct menu *menu)
175{
176 struct symbol *sym = menu->sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 tristate oldval, newval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 while (1) {
EGRY Gabor534a4502008-01-11 23:44:39 +0100180 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (sym->name)
182 printf("(%s) ", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 putchar('[');
184 oldval = sym_get_tristate_value(sym);
185 switch (oldval) {
186 case no:
187 putchar('N');
188 break;
189 case mod:
190 putchar('M');
191 break;
192 case yes:
193 putchar('Y');
194 break;
195 }
196 if (oldval != no && sym_tristate_within_range(sym, no))
197 printf("/n");
198 if (oldval != mod && sym_tristate_within_range(sym, mod))
199 printf("/m");
200 if (oldval != yes && sym_tristate_within_range(sym, yes))
201 printf("/y");
Sam Ravnborg03d29122007-07-21 00:00:36 +0200202 if (menu_has_help(menu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 printf("/?");
204 printf("] ");
Roman Zippelf82f3f92007-08-30 05:06:17 +0200205 if (!conf_askvalue(sym, sym_get_string_value(sym)))
206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 strip(line);
208
209 switch (line[0]) {
210 case 'n':
211 case 'N':
212 newval = no;
213 if (!line[1] || !strcmp(&line[1], "o"))
214 break;
215 continue;
216 case 'm':
217 case 'M':
218 newval = mod;
219 if (!line[1])
220 break;
221 continue;
222 case 'y':
223 case 'Y':
224 newval = yes;
225 if (!line[1] || !strcmp(&line[1], "es"))
226 break;
227 continue;
228 case 0:
229 newval = oldval;
230 break;
231 case '?':
232 goto help;
233 default:
234 continue;
235 }
236 if (sym_set_tristate_value(sym, newval))
237 return 0;
238help:
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800239 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241}
242
243static int conf_choice(struct menu *menu)
244{
245 struct symbol *sym, *def_sym;
246 struct menu *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 bool is_new;
248
249 sym = menu->sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 is_new = !sym_has_value(sym);
251 if (sym_is_changable(sym)) {
252 conf_sym(menu);
253 sym_calc_value(sym);
254 switch (sym_get_tristate_value(sym)) {
255 case no:
256 return 1;
257 case mod:
258 return 0;
259 case yes:
260 break;
261 }
262 } else {
263 switch (sym_get_tristate_value(sym)) {
264 case no:
265 return 1;
266 case mod:
EGRY Gabor534a4502008-01-11 23:44:39 +0100267 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return 0;
269 case yes:
270 break;
271 }
272 }
273
274 while (1) {
275 int cnt, def;
276
EGRY Gabor534a4502008-01-11 23:44:39 +0100277 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 def_sym = sym_get_choice_value(sym);
279 cnt = def = 0;
Roman Zippel40aee722006-04-09 17:26:39 +0200280 line[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 for (child = menu->list; child; child = child->next) {
282 if (!menu_is_visible(child))
283 continue;
284 if (!child->sym) {
EGRY Gabor534a4502008-01-11 23:44:39 +0100285 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 continue;
287 }
288 cnt++;
289 if (child->sym == def_sym) {
290 def = cnt;
291 printf("%*c", indent, '>');
292 } else
293 printf("%*c", indent, ' ');
EGRY Gabor534a4502008-01-11 23:44:39 +0100294 printf(" %d. %s", cnt, _(menu_get_prompt(child)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (child->sym->name)
296 printf(" (%s)", child->sym->name);
297 if (!sym_has_value(child->sym))
EGRY Gabor534a4502008-01-11 23:44:39 +0100298 printf(_(" (NEW)"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 printf("\n");
300 }
EGRY Gabor534a4502008-01-11 23:44:39 +0100301 printf(_("%*schoice"), indent - 1, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (cnt == 1) {
303 printf("[1]: 1\n");
304 goto conf_childs;
305 }
306 printf("[1-%d", cnt);
Sam Ravnborg03d29122007-07-21 00:00:36 +0200307 if (menu_has_help(menu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 printf("?");
309 printf("]: ");
310 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200311 case oldconfig:
312 case silentoldconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (!is_new) {
314 cnt = def;
315 printf("%d\n", cnt);
316 break;
317 }
318 check_stdin();
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400319 /* fall through */
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200320 case oldaskconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 fflush(stdout);
Markus Mayer74dba802015-12-09 14:56:12 -0800322 xfgets(line, sizeof(line), stdin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 strip(line);
324 if (line[0] == '?') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800325 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 continue;
327 }
328 if (!line[0])
329 cnt = def;
330 else if (isdigit(line[0]))
331 cnt = atoi(line);
332 else
333 continue;
334 break;
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200335 default:
336 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338
339 conf_childs:
340 for (child = menu->list; child; child = child->next) {
341 if (!child->sym || !menu_is_visible(child))
342 continue;
343 if (!--cnt)
344 break;
345 }
346 if (!child)
347 continue;
Ben Hutchings3ba41622011-04-23 18:42:56 +0100348 if (line[0] && line[strlen(line) - 1] == '?') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800349 print_help(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 continue;
351 }
352 sym_set_choice_value(sym, child->sym);
Jan Beulichf5eaa322008-01-24 11:54:23 +0000353 for (child = child->list; child; child = child->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 indent += 2;
Jan Beulichf5eaa322008-01-24 11:54:23 +0000355 conf(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 indent -= 2;
357 }
358 return 1;
359 }
360}
361
362static void conf(struct menu *menu)
363{
364 struct symbol *sym;
365 struct property *prop;
366 struct menu *child;
367
368 if (!menu_is_visible(menu))
369 return;
370
371 sym = menu->sym;
372 prop = menu->prompt;
373 if (prop) {
374 const char *prompt;
375
376 switch (prop->type) {
377 case P_MENU:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200378 if ((input_mode == silentoldconfig ||
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200379 input_mode == listnewconfig ||
Adam Leefb16d892012-09-01 01:05:17 +0800380 input_mode == olddefconfig) &&
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400381 rootEntry != menu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 check_conf(menu);
383 return;
384 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400385 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 case P_COMMENT:
387 prompt = menu_get_prompt(menu);
388 if (prompt)
389 printf("%*c\n%*c %s\n%*c\n",
390 indent, '*',
EGRY Gabor534a4502008-01-11 23:44:39 +0100391 indent, '*', _(prompt),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 indent, '*');
393 default:
394 ;
395 }
396 }
397
398 if (!sym)
399 goto conf_childs;
400
401 if (sym_is_choice(sym)) {
402 conf_choice(menu);
403 if (sym->curr.tri != mod)
404 return;
405 goto conf_childs;
406 }
407
408 switch (sym->type) {
409 case S_INT:
410 case S_HEX:
411 case S_STRING:
412 conf_string(menu);
413 break;
414 default:
415 conf_sym(menu);
416 break;
417 }
418
419conf_childs:
420 if (sym)
421 indent += 2;
422 for (child = menu->list; child; child = child->next)
423 conf(child);
424 if (sym)
425 indent -= 2;
426}
427
428static void check_conf(struct menu *menu)
429{
430 struct symbol *sym;
431 struct menu *child;
432
433 if (!menu_is_visible(menu))
434 return;
435
436 sym = menu->sym;
Roman Zippel3f23ca22005-11-08 21:34:48 -0800437 if (sym && !sym_has_value(sym)) {
438 if (sym_is_changable(sym) ||
439 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200440 if (input_mode == listnewconfig) {
441 if (sym->name && !sym_is_choice_value(sym)) {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400442 printf("%s%s\n", CONFIG_, sym->name);
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400443 }
Adam Leefb16d892012-09-01 01:05:17 +0800444 } else if (input_mode != olddefconfig) {
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400445 if (!conf_cnt++)
446 printf(_("*\n* Restart config...\n*\n"));
447 rootEntry = menu_get_parent_menu(menu);
448 conf(rootEntry);
449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452
453 for (child = menu->list; child; child = child->next)
454 check_conf(child);
455}
456
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200457static struct option long_opts[] = {
458 {"oldaskconfig", no_argument, NULL, oldaskconfig},
459 {"oldconfig", no_argument, NULL, oldconfig},
460 {"silentoldconfig", no_argument, NULL, silentoldconfig},
461 {"defconfig", optional_argument, NULL, defconfig},
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200462 {"savedefconfig", required_argument, NULL, savedefconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200463 {"allnoconfig", no_argument, NULL, allnoconfig},
464 {"allyesconfig", no_argument, NULL, allyesconfig},
465 {"allmodconfig", no_argument, NULL, allmodconfig},
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200466 {"alldefconfig", no_argument, NULL, alldefconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200467 {"randconfig", no_argument, NULL, randconfig},
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200468 {"listnewconfig", no_argument, NULL, listnewconfig},
Adam Leefb16d892012-09-01 01:05:17 +0800469 {"olddefconfig", no_argument, NULL, olddefconfig},
470 /*
471 * oldnoconfig is an alias of olddefconfig, because people already
472 * are dependent on its behavior(sets new symbols to their default
473 * value but not 'n') with the counter-intuitive name.
474 */
475 {"oldnoconfig", no_argument, NULL, olddefconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200476 {NULL, 0, NULL, 0}
477};
478
Arnaud Lacombe32543992010-11-02 00:26:33 -0400479static void conf_usage(const char *progname)
480{
481
Michal Marek0a1f00a2015-04-08 13:30:42 +0200482 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
Arnaud Lacombe32543992010-11-02 00:26:33 -0400483 printf("[option] is _one_ of the following:\n");
484 printf(" --listnewconfig List new options\n");
485 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
486 printf(" --oldconfig Update a configuration using a provided .config as base\n");
Marc Herbertcedd55d2018-01-26 14:59:00 -0800487 printf(" --silentoldconfig Similar to oldconfig but generates configuration in\n"
488 " include/{generated/,config/} (oldconfig used to be more verbose)\n");
489 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
Adam Leefb16d892012-09-01 01:05:17 +0800490 printf(" --oldnoconfig An alias of olddefconfig\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400491 printf(" --defconfig <file> New config with default defined in <file>\n");
492 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
493 printf(" --allnoconfig New config where all options are answered with no\n");
494 printf(" --allyesconfig New config where all options are answered with yes\n");
495 printf(" --allmodconfig New config where all options are answered with mod\n");
496 printf(" --alldefconfig New config with all symbols set to default\n");
497 printf(" --randconfig New config with random answer to all options\n");
498}
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500int main(int ac, char **av)
501{
Arnaud Lacombe32543992010-11-02 00:26:33 -0400502 const char *progname = av[0];
Andres Salomon2f4b4892007-12-17 01:34:58 -0500503 int opt;
Arnaud Lacombe275744c2010-10-13 20:43:28 -0400504 const char *name, *defconfig_file = NULL /* gcc uninit */;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct stat tmpstat;
506
EGRY Gabor534a4502008-01-11 23:44:39 +0100507 setlocale(LC_ALL, "");
508 bindtextdomain(PACKAGE, LOCALEDIR);
509 textdomain(PACKAGE);
510
Ben Hutchings62dc9892013-02-19 02:24:26 +0200511 tty_stdio = isatty(0) && isatty(1) && isatty(2);
512
Michal Marek0a1f00a2015-04-08 13:30:42 +0200513 while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
514 if (opt == 's') {
515 conf_set_message_callback(NULL);
516 continue;
517 }
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200518 input_mode = (enum input_mode)opt;
Andres Salomon2f4b4892007-12-17 01:34:58 -0500519 switch (opt) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200520 case silentoldconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200521 sync_kconfig = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200523 case defconfig:
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200524 case savedefconfig:
Andres Salomon2f4b4892007-12-17 01:34:58 -0500525 defconfig_file = optarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200527 case randconfig:
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100528 {
529 struct timeval now;
530 unsigned int seed;
Yann E. MORIN0d8024c2013-04-13 22:49:13 +0200531 char *seed_env;
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100532
533 /*
534 * Use microseconds derived seed,
535 * compensate for systems where it may be zero
536 */
537 gettimeofday(&now, NULL);
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100538 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
Yann E. MORIN0d8024c2013-04-13 22:49:13 +0200539
540 seed_env = getenv("KCONFIG_SEED");
541 if( seed_env && *seed_env ) {
542 char *endp;
Yann E. MORINe85ac122013-05-20 23:17:34 +0200543 int tmp = (int)strtol(seed_env, &endp, 0);
Yann E. MORIN0d8024c2013-04-13 22:49:13 +0200544 if (*endp == '\0') {
545 seed = tmp;
546 }
547 }
Yann E. MORINa5f6d792013-05-20 23:09:03 +0200548 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100549 srand(seed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100551 }
Arnaud Lacombe32543992010-11-02 00:26:33 -0400552 case oldaskconfig:
553 case oldconfig:
554 case allnoconfig:
555 case allyesconfig:
556 case allmodconfig:
557 case alldefconfig:
558 case listnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800559 case olddefconfig:
Arnaud Lacombe32543992010-11-02 00:26:33 -0400560 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200561 case '?':
Arnaud Lacombe32543992010-11-02 00:26:33 -0400562 conf_usage(progname);
Andres Salomon2f4b4892007-12-17 01:34:58 -0500563 exit(1);
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200564 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566 }
Andres Salomon2f4b4892007-12-17 01:34:58 -0500567 if (ac == optind) {
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700568 printf(_("%s: Kconfig file missing\n"), av[0]);
Arnaud Lacombe32543992010-11-02 00:26:33 -0400569 conf_usage(progname);
Randy Dunlap250725a2006-06-08 22:12:50 -0700570 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Andres Salomon2f4b4892007-12-17 01:34:58 -0500572 name = av[optind];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 conf_parse(name);
574 //zconfdump(stdout);
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200575 if (sync_kconfig) {
Markus Heidelberg284026c2009-05-18 01:36:53 +0200576 name = conf_get_configname();
577 if (stat(name, &tmpstat)) {
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200578 fprintf(stderr, _("***\n"
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400579 "*** Configuration file \"%s\" not found!\n"
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200580 "***\n"
581 "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
582 "*** \"make menuconfig\" or \"make xconfig\").\n"
Markus Heidelberg284026c2009-05-18 01:36:53 +0200583 "***\n"), name);
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200584 exit(1);
585 }
586 }
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200589 case defconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (!defconfig_file)
591 defconfig_file = conf_get_default_confname();
592 if (conf_read(defconfig_file)) {
EGRY Gabor534a4502008-01-11 23:44:39 +0100593 printf(_("***\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 "*** Can't find default configuration \"%s\"!\n"
EGRY Gabor534a4502008-01-11 23:44:39 +0100595 "***\n"), defconfig_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 exit(1);
597 }
598 break;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200599 case savedefconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200600 case silentoldconfig:
601 case oldaskconfig:
602 case oldconfig:
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200603 case listnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800604 case olddefconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 conf_read(NULL);
606 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200607 case allnoconfig:
608 case allyesconfig:
609 case allmodconfig:
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200610 case alldefconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200611 case randconfig:
Roman Zippel90389162005-11-08 21:34:49 -0800612 name = getenv("KCONFIG_ALLCONFIG");
Eric W. Biederman9f420bf2012-05-07 05:37:45 -0700613 if (!name)
614 break;
615 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700616 if (conf_read_simple(name, S_DEF_USER)) {
617 fprintf(stderr,
618 _("*** Can't read seed configuration \"%s\"!\n"),
619 name);
620 exit(1);
621 }
Roman Zippel90389162005-11-08 21:34:49 -0800622 break;
623 }
624 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200625 case allnoconfig: name = "allno.config"; break;
626 case allyesconfig: name = "allyes.config"; break;
627 case allmodconfig: name = "allmod.config"; break;
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200628 case alldefconfig: name = "alldef.config"; break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200629 case randconfig: name = "allrandom.config"; break;
Roman Zippel90389162005-11-08 21:34:49 -0800630 default: break;
631 }
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700632 if (conf_read_simple(name, S_DEF_USER) &&
633 conf_read_simple("all.config", S_DEF_USER)) {
634 fprintf(stderr,
635 _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
636 name);
637 exit(1);
638 }
Roman Zippel90389162005-11-08 21:34:49 -0800639 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 default:
641 break;
642 }
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200643
644 if (sync_kconfig) {
645 if (conf_get_changed()) {
646 name = getenv("KCONFIG_NOSILENTUPDATE");
647 if (name && *name) {
648 fprintf(stderr,
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400649 _("\n*** The configuration requires explicit update.\n\n"));
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200650 return 1;
651 }
652 }
Ben Hutchings62dc9892013-02-19 02:24:26 +0200653 valid_stdin = tty_stdio;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200654 }
655
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200656 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200657 case allnoconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200658 conf_set_all_new_symbols(def_no);
659 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200660 case allyesconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200661 conf_set_all_new_symbols(def_yes);
662 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200663 case allmodconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200664 conf_set_all_new_symbols(def_mod);
665 break;
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200666 case alldefconfig:
667 conf_set_all_new_symbols(def_default);
668 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200669 case randconfig:
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +0200670 /* Really nothing to do in this loop */
671 while (conf_set_all_new_symbols(def_random)) ;
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200672 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200673 case defconfig:
Sam Ravnborg09748e12008-06-30 23:02:59 +0200674 conf_set_all_new_symbols(def_default);
675 break;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200676 case savedefconfig:
677 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200678 case oldaskconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200679 rootEntry = &rootmenu;
680 conf(&rootmenu);
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200681 input_mode = silentoldconfig;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200682 /* fall through */
Sam Ravnborg14828342010-08-06 07:13:54 +0200683 case oldconfig:
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200684 case listnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800685 case olddefconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200686 case silentoldconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200687 /* Update until a loop caused no more changes */
688 do {
689 conf_cnt = 0;
690 check_conf(&rootmenu);
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400691 } while (conf_cnt &&
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200692 (input_mode != listnewconfig &&
Adam Leefb16d892012-09-01 01:05:17 +0800693 input_mode != olddefconfig));
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200694 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200696
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200697 if (sync_kconfig) {
698 /* silentoldconfig is used during the build so we shall update autoconf.
699 * All other commands are only used to generate a config.
700 */
701 if (conf_get_changed() && conf_write(NULL)) {
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400702 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200703 exit(1);
704 }
705 if (conf_write_autoconf()) {
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400706 fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200707 return 1;
708 }
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200709 } else if (input_mode == savedefconfig) {
710 if (conf_write_defconfig(defconfig_file)) {
711 fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900712 defconfig_file);
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200713 return 1;
714 }
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200715 } else if (input_mode != listnewconfig) {
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200716 if (conf_write(NULL)) {
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400717 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200718 exit(1);
719 }
Roman Zippelc955cca2006-06-08 22:12:39 -0700720 }
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200721 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}