blob: 866369f10ff84f4d2cae14eb6dfbe7e353c08adf [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);
Arnaud Lacombeab63f582011-07-02 00:59:41 -040023static void xfgets(char *str, int size, FILE *in);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020025enum input_mode {
26 oldaskconfig,
27 silentoldconfig,
28 oldconfig,
29 allnoconfig,
30 allyesconfig,
31 allmodconfig,
Sam Ravnborg0748cb32010-07-31 23:35:31 +020032 alldefconfig,
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020033 randconfig,
34 defconfig,
Sam Ravnborg7cf3d732010-07-31 23:35:34 +020035 savedefconfig,
Sam Ravnborg861b4ea2010-07-31 23:35:28 +020036 listnewconfig,
Adam Leefb16d892012-09-01 01:05:17 +080037 olddefconfig,
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020038} input_mode = oldaskconfig;
39
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
Roman Zippelf82f3f92007-08-30 05:06:17 +020085static int conf_askvalue(struct symbol *sym, const char *def)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 enum symbol_type type = sym_get_type(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 if (!sym_has_value(sym))
EGRY Gabor534a4502008-01-11 23:44:39 +010090 printf(_("(NEW) "));
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
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;
Roman Zippelf82f3f92007-08-30 05:06:17 +020099 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101
102 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200103 case oldconfig:
104 case silentoldconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (sym_has_value(sym)) {
106 printf("%s\n", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109 check_stdin();
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400110 /* fall through */
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200111 case oldaskconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 fflush(stdout);
Markus Mayer74dba802015-12-09 14:56:12 -0800113 xfgets(line, sizeof(line), stdin);
Ben Hutchings62dc9892013-02-19 02:24:26 +0200114 if (!tty_stdio)
115 printf("\n");
Roman Zippelf82f3f92007-08-30 05:06:17 +0200116 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 default:
118 break;
119 }
120
121 switch (type) {
122 case S_INT:
123 case S_HEX:
124 case S_STRING:
125 printf("%s\n", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200126 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 default:
128 ;
129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 printf("%s", line);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200131 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Trevor Keith4356f482009-09-18 12:49:23 -0700134static int conf_string(struct menu *menu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 struct symbol *sym = menu->sym;
Sam Ravnborg03d29122007-07-21 00:00:36 +0200137 const char *def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 while (1) {
EGRY Gabor534a4502008-01-11 23:44:39 +0100140 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 printf("(%s) ", sym->name);
142 def = sym_get_string_value(sym);
143 if (sym_get_string_value(sym))
144 printf("[%s] ", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200145 if (!conf_askvalue(sym, def))
146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 switch (line[0]) {
148 case '\n':
149 break;
150 case '?':
151 /* print help */
152 if (line[1] == '\n') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800153 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 def = NULL;
155 break;
156 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400157 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 default:
159 line[strlen(line)-1] = 0;
160 def = line;
161 }
162 if (def && sym_set_string_value(sym, def))
163 return 0;
164 }
165}
166
167static int conf_sym(struct menu *menu)
168{
169 struct symbol *sym = menu->sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 tristate oldval, newval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 while (1) {
EGRY Gabor534a4502008-01-11 23:44:39 +0100173 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (sym->name)
175 printf("(%s) ", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 putchar('[');
177 oldval = sym_get_tristate_value(sym);
178 switch (oldval) {
179 case no:
180 putchar('N');
181 break;
182 case mod:
183 putchar('M');
184 break;
185 case yes:
186 putchar('Y');
187 break;
188 }
189 if (oldval != no && sym_tristate_within_range(sym, no))
190 printf("/n");
191 if (oldval != mod && sym_tristate_within_range(sym, mod))
192 printf("/m");
193 if (oldval != yes && sym_tristate_within_range(sym, yes))
194 printf("/y");
Sam Ravnborg03d29122007-07-21 00:00:36 +0200195 if (menu_has_help(menu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 printf("/?");
197 printf("] ");
Roman Zippelf82f3f92007-08-30 05:06:17 +0200198 if (!conf_askvalue(sym, sym_get_string_value(sym)))
199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 strip(line);
201
202 switch (line[0]) {
203 case 'n':
204 case 'N':
205 newval = no;
206 if (!line[1] || !strcmp(&line[1], "o"))
207 break;
208 continue;
209 case 'm':
210 case 'M':
211 newval = mod;
212 if (!line[1])
213 break;
214 continue;
215 case 'y':
216 case 'Y':
217 newval = yes;
218 if (!line[1] || !strcmp(&line[1], "es"))
219 break;
220 continue;
221 case 0:
222 newval = oldval;
223 break;
224 case '?':
225 goto help;
226 default:
227 continue;
228 }
229 if (sym_set_tristate_value(sym, newval))
230 return 0;
231help:
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800232 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234}
235
236static int conf_choice(struct menu *menu)
237{
238 struct symbol *sym, *def_sym;
239 struct menu *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 bool is_new;
241
242 sym = menu->sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 is_new = !sym_has_value(sym);
244 if (sym_is_changable(sym)) {
245 conf_sym(menu);
246 sym_calc_value(sym);
247 switch (sym_get_tristate_value(sym)) {
248 case no:
249 return 1;
250 case mod:
251 return 0;
252 case yes:
253 break;
254 }
255 } else {
256 switch (sym_get_tristate_value(sym)) {
257 case no:
258 return 1;
259 case mod:
EGRY Gabor534a4502008-01-11 23:44:39 +0100260 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return 0;
262 case yes:
263 break;
264 }
265 }
266
267 while (1) {
268 int cnt, def;
269
EGRY Gabor534a4502008-01-11 23:44:39 +0100270 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 def_sym = sym_get_choice_value(sym);
272 cnt = def = 0;
Roman Zippel40aee722006-04-09 17:26:39 +0200273 line[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 for (child = menu->list; child; child = child->next) {
275 if (!menu_is_visible(child))
276 continue;
277 if (!child->sym) {
EGRY Gabor534a4502008-01-11 23:44:39 +0100278 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 continue;
280 }
281 cnt++;
282 if (child->sym == def_sym) {
283 def = cnt;
284 printf("%*c", indent, '>');
285 } else
286 printf("%*c", indent, ' ');
EGRY Gabor534a4502008-01-11 23:44:39 +0100287 printf(" %d. %s", cnt, _(menu_get_prompt(child)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (child->sym->name)
289 printf(" (%s)", child->sym->name);
290 if (!sym_has_value(child->sym))
EGRY Gabor534a4502008-01-11 23:44:39 +0100291 printf(_(" (NEW)"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 printf("\n");
293 }
EGRY Gabor534a4502008-01-11 23:44:39 +0100294 printf(_("%*schoice"), indent - 1, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (cnt == 1) {
296 printf("[1]: 1\n");
297 goto conf_childs;
298 }
299 printf("[1-%d", cnt);
Sam Ravnborg03d29122007-07-21 00:00:36 +0200300 if (menu_has_help(menu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 printf("?");
302 printf("]: ");
303 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200304 case oldconfig:
305 case silentoldconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (!is_new) {
307 cnt = def;
308 printf("%d\n", cnt);
309 break;
310 }
311 check_stdin();
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400312 /* fall through */
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200313 case oldaskconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 fflush(stdout);
Markus Mayer74dba802015-12-09 14:56:12 -0800315 xfgets(line, sizeof(line), stdin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 strip(line);
317 if (line[0] == '?') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800318 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 continue;
320 }
321 if (!line[0])
322 cnt = def;
323 else if (isdigit(line[0]))
324 cnt = atoi(line);
325 else
326 continue;
327 break;
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200328 default:
329 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331
332 conf_childs:
333 for (child = menu->list; child; child = child->next) {
334 if (!child->sym || !menu_is_visible(child))
335 continue;
336 if (!--cnt)
337 break;
338 }
339 if (!child)
340 continue;
Ben Hutchings3ba41622011-04-23 18:42:56 +0100341 if (line[0] && line[strlen(line) - 1] == '?') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800342 print_help(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 continue;
344 }
345 sym_set_choice_value(sym, child->sym);
Jan Beulichf5eaa322008-01-24 11:54:23 +0000346 for (child = child->list; child; child = child->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 indent += 2;
Jan Beulichf5eaa322008-01-24 11:54:23 +0000348 conf(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 indent -= 2;
350 }
351 return 1;
352 }
353}
354
355static void conf(struct menu *menu)
356{
357 struct symbol *sym;
358 struct property *prop;
359 struct menu *child;
360
361 if (!menu_is_visible(menu))
362 return;
363
364 sym = menu->sym;
365 prop = menu->prompt;
366 if (prop) {
367 const char *prompt;
368
369 switch (prop->type) {
370 case P_MENU:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200371 if ((input_mode == silentoldconfig ||
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200372 input_mode == listnewconfig ||
Adam Leefb16d892012-09-01 01:05:17 +0800373 input_mode == olddefconfig) &&
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400374 rootEntry != menu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 check_conf(menu);
376 return;
377 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400378 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 case P_COMMENT:
380 prompt = menu_get_prompt(menu);
381 if (prompt)
382 printf("%*c\n%*c %s\n%*c\n",
383 indent, '*',
EGRY Gabor534a4502008-01-11 23:44:39 +0100384 indent, '*', _(prompt),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 indent, '*');
386 default:
387 ;
388 }
389 }
390
391 if (!sym)
392 goto conf_childs;
393
394 if (sym_is_choice(sym)) {
395 conf_choice(menu);
396 if (sym->curr.tri != mod)
397 return;
398 goto conf_childs;
399 }
400
401 switch (sym->type) {
402 case S_INT:
403 case S_HEX:
404 case S_STRING:
405 conf_string(menu);
406 break;
407 default:
408 conf_sym(menu);
409 break;
410 }
411
412conf_childs:
413 if (sym)
414 indent += 2;
415 for (child = menu->list; child; child = child->next)
416 conf(child);
417 if (sym)
418 indent -= 2;
419}
420
421static void check_conf(struct menu *menu)
422{
423 struct symbol *sym;
424 struct menu *child;
425
426 if (!menu_is_visible(menu))
427 return;
428
429 sym = menu->sym;
Roman Zippel3f23ca22005-11-08 21:34:48 -0800430 if (sym && !sym_has_value(sym)) {
431 if (sym_is_changable(sym) ||
432 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200433 if (input_mode == listnewconfig) {
434 if (sym->name && !sym_is_choice_value(sym)) {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400435 printf("%s%s\n", CONFIG_, sym->name);
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400436 }
Adam Leefb16d892012-09-01 01:05:17 +0800437 } else if (input_mode != olddefconfig) {
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400438 if (!conf_cnt++)
439 printf(_("*\n* Restart config...\n*\n"));
440 rootEntry = menu_get_parent_menu(menu);
441 conf(rootEntry);
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
446 for (child = menu->list; child; child = child->next)
447 check_conf(child);
448}
449
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200450static struct option long_opts[] = {
451 {"oldaskconfig", no_argument, NULL, oldaskconfig},
452 {"oldconfig", no_argument, NULL, oldconfig},
453 {"silentoldconfig", no_argument, NULL, silentoldconfig},
454 {"defconfig", optional_argument, NULL, defconfig},
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200455 {"savedefconfig", required_argument, NULL, savedefconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200456 {"allnoconfig", no_argument, NULL, allnoconfig},
457 {"allyesconfig", no_argument, NULL, allyesconfig},
458 {"allmodconfig", no_argument, NULL, allmodconfig},
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200459 {"alldefconfig", no_argument, NULL, alldefconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200460 {"randconfig", no_argument, NULL, randconfig},
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200461 {"listnewconfig", no_argument, NULL, listnewconfig},
Adam Leefb16d892012-09-01 01:05:17 +0800462 {"olddefconfig", no_argument, NULL, olddefconfig},
463 /*
464 * oldnoconfig is an alias of olddefconfig, because people already
465 * are dependent on its behavior(sets new symbols to their default
466 * value but not 'n') with the counter-intuitive name.
467 */
468 {"oldnoconfig", no_argument, NULL, olddefconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200469 {NULL, 0, NULL, 0}
470};
471
Arnaud Lacombe32543992010-11-02 00:26:33 -0400472static void conf_usage(const char *progname)
473{
474
Michal Marek0a1f00a2015-04-08 13:30:42 +0200475 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
Arnaud Lacombe32543992010-11-02 00:26:33 -0400476 printf("[option] is _one_ of the following:\n");
477 printf(" --listnewconfig List new options\n");
478 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
479 printf(" --oldconfig Update a configuration using a provided .config as base\n");
480 printf(" --silentoldconfig Same as oldconfig, but quietly, additionally update deps\n");
Adam Leefb16d892012-09-01 01:05:17 +0800481 printf(" --olddefconfig Same as silentoldconfig but sets new symbols to their default value\n");
482 printf(" --oldnoconfig An alias of olddefconfig\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400483 printf(" --defconfig <file> New config with default defined in <file>\n");
484 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
485 printf(" --allnoconfig New config where all options are answered with no\n");
486 printf(" --allyesconfig New config where all options are answered with yes\n");
487 printf(" --allmodconfig New config where all options are answered with mod\n");
488 printf(" --alldefconfig New config with all symbols set to default\n");
489 printf(" --randconfig New config with random answer to all options\n");
490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492int main(int ac, char **av)
493{
Arnaud Lacombe32543992010-11-02 00:26:33 -0400494 const char *progname = av[0];
Andres Salomon2f4b4892007-12-17 01:34:58 -0500495 int opt;
Arnaud Lacombe275744c2010-10-13 20:43:28 -0400496 const char *name, *defconfig_file = NULL /* gcc uninit */;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct stat tmpstat;
498
EGRY Gabor534a4502008-01-11 23:44:39 +0100499 setlocale(LC_ALL, "");
500 bindtextdomain(PACKAGE, LOCALEDIR);
501 textdomain(PACKAGE);
502
Ben Hutchings62dc9892013-02-19 02:24:26 +0200503 tty_stdio = isatty(0) && isatty(1) && isatty(2);
504
Michal Marek0a1f00a2015-04-08 13:30:42 +0200505 while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
506 if (opt == 's') {
507 conf_set_message_callback(NULL);
508 continue;
509 }
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200510 input_mode = (enum input_mode)opt;
Andres Salomon2f4b4892007-12-17 01:34:58 -0500511 switch (opt) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200512 case silentoldconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200513 sync_kconfig = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200515 case defconfig:
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200516 case savedefconfig:
Andres Salomon2f4b4892007-12-17 01:34:58 -0500517 defconfig_file = optarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200519 case randconfig:
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100520 {
521 struct timeval now;
522 unsigned int seed;
Yann E. MORIN0d8024c2013-04-13 22:49:13 +0200523 char *seed_env;
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100524
525 /*
526 * Use microseconds derived seed,
527 * compensate for systems where it may be zero
528 */
529 gettimeofday(&now, NULL);
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100530 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
Yann E. MORIN0d8024c2013-04-13 22:49:13 +0200531
532 seed_env = getenv("KCONFIG_SEED");
533 if( seed_env && *seed_env ) {
534 char *endp;
Yann E. MORINe85ac122013-05-20 23:17:34 +0200535 int tmp = (int)strtol(seed_env, &endp, 0);
Yann E. MORIN0d8024c2013-04-13 22:49:13 +0200536 if (*endp == '\0') {
537 seed = tmp;
538 }
539 }
Yann E. MORINa5f6d792013-05-20 23:09:03 +0200540 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100541 srand(seed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 break;
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100543 }
Arnaud Lacombe32543992010-11-02 00:26:33 -0400544 case oldaskconfig:
545 case oldconfig:
546 case allnoconfig:
547 case allyesconfig:
548 case allmodconfig:
549 case alldefconfig:
550 case listnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800551 case olddefconfig:
Arnaud Lacombe32543992010-11-02 00:26:33 -0400552 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200553 case '?':
Arnaud Lacombe32543992010-11-02 00:26:33 -0400554 conf_usage(progname);
Andres Salomon2f4b4892007-12-17 01:34:58 -0500555 exit(1);
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200556 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 }
Andres Salomon2f4b4892007-12-17 01:34:58 -0500559 if (ac == optind) {
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700560 printf(_("%s: Kconfig file missing\n"), av[0]);
Arnaud Lacombe32543992010-11-02 00:26:33 -0400561 conf_usage(progname);
Randy Dunlap250725a2006-06-08 22:12:50 -0700562 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
Andres Salomon2f4b4892007-12-17 01:34:58 -0500564 name = av[optind];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 conf_parse(name);
566 //zconfdump(stdout);
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200567 if (sync_kconfig) {
Markus Heidelberg284026c2009-05-18 01:36:53 +0200568 name = conf_get_configname();
569 if (stat(name, &tmpstat)) {
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200570 fprintf(stderr, _("***\n"
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400571 "*** Configuration file \"%s\" not found!\n"
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200572 "***\n"
573 "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
574 "*** \"make menuconfig\" or \"make xconfig\").\n"
Markus Heidelberg284026c2009-05-18 01:36:53 +0200575 "***\n"), name);
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200576 exit(1);
577 }
578 }
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200581 case defconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (!defconfig_file)
583 defconfig_file = conf_get_default_confname();
584 if (conf_read(defconfig_file)) {
EGRY Gabor534a4502008-01-11 23:44:39 +0100585 printf(_("***\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 "*** Can't find default configuration \"%s\"!\n"
EGRY Gabor534a4502008-01-11 23:44:39 +0100587 "***\n"), defconfig_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 exit(1);
589 }
590 break;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200591 case savedefconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200592 case silentoldconfig:
593 case oldaskconfig:
594 case oldconfig:
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200595 case listnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800596 case olddefconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 conf_read(NULL);
598 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200599 case allnoconfig:
600 case allyesconfig:
601 case allmodconfig:
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200602 case alldefconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200603 case randconfig:
Roman Zippel90389162005-11-08 21:34:49 -0800604 name = getenv("KCONFIG_ALLCONFIG");
Eric W. Biederman9f420bf2012-05-07 05:37:45 -0700605 if (!name)
606 break;
607 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700608 if (conf_read_simple(name, S_DEF_USER)) {
609 fprintf(stderr,
610 _("*** Can't read seed configuration \"%s\"!\n"),
611 name);
612 exit(1);
613 }
Roman Zippel90389162005-11-08 21:34:49 -0800614 break;
615 }
616 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200617 case allnoconfig: name = "allno.config"; break;
618 case allyesconfig: name = "allyes.config"; break;
619 case allmodconfig: name = "allmod.config"; break;
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200620 case alldefconfig: name = "alldef.config"; break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200621 case randconfig: name = "allrandom.config"; break;
Roman Zippel90389162005-11-08 21:34:49 -0800622 default: break;
623 }
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700624 if (conf_read_simple(name, S_DEF_USER) &&
625 conf_read_simple("all.config", S_DEF_USER)) {
626 fprintf(stderr,
627 _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
628 name);
629 exit(1);
630 }
Roman Zippel90389162005-11-08 21:34:49 -0800631 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 default:
633 break;
634 }
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200635
636 if (sync_kconfig) {
637 if (conf_get_changed()) {
638 name = getenv("KCONFIG_NOSILENTUPDATE");
639 if (name && *name) {
640 fprintf(stderr,
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400641 _("\n*** The configuration requires explicit update.\n\n"));
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200642 return 1;
643 }
644 }
Ben Hutchings62dc9892013-02-19 02:24:26 +0200645 valid_stdin = tty_stdio;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200646 }
647
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200648 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200649 case allnoconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200650 conf_set_all_new_symbols(def_no);
651 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200652 case allyesconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200653 conf_set_all_new_symbols(def_yes);
654 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200655 case allmodconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200656 conf_set_all_new_symbols(def_mod);
657 break;
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200658 case alldefconfig:
659 conf_set_all_new_symbols(def_default);
660 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200661 case randconfig:
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +0200662 /* Really nothing to do in this loop */
663 while (conf_set_all_new_symbols(def_random)) ;
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200664 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200665 case defconfig:
Sam Ravnborg09748e12008-06-30 23:02:59 +0200666 conf_set_all_new_symbols(def_default);
667 break;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200668 case savedefconfig:
669 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200670 case oldaskconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200671 rootEntry = &rootmenu;
672 conf(&rootmenu);
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200673 input_mode = silentoldconfig;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200674 /* fall through */
Sam Ravnborg14828342010-08-06 07:13:54 +0200675 case oldconfig:
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200676 case listnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800677 case olddefconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200678 case silentoldconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200679 /* Update until a loop caused no more changes */
680 do {
681 conf_cnt = 0;
682 check_conf(&rootmenu);
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400683 } while (conf_cnt &&
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200684 (input_mode != listnewconfig &&
Adam Leefb16d892012-09-01 01:05:17 +0800685 input_mode != olddefconfig));
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200686 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200688
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200689 if (sync_kconfig) {
690 /* silentoldconfig is used during the build so we shall update autoconf.
691 * All other commands are only used to generate a config.
692 */
693 if (conf_get_changed() && conf_write(NULL)) {
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400694 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200695 exit(1);
696 }
697 if (conf_write_autoconf()) {
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400698 fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200699 return 1;
700 }
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200701 } else if (input_mode == savedefconfig) {
702 if (conf_write_defconfig(defconfig_file)) {
703 fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900704 defconfig_file);
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200705 return 1;
706 }
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200707 } else if (input_mode != listnewconfig) {
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200708 if (conf_write(NULL)) {
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400709 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200710 exit(1);
711 }
Roman Zippelc955cca2006-06-08 22:12:39 -0700712 }
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
Arnaud Lacombeab63f582011-07-02 00:59:41 -0400715
Jean Sacren4418a2b2010-08-04 16:03:16 -0600716/*
717 * Helper function to facilitate fgets() by Jean Sacren.
718 */
Arnaud Lacombeab63f582011-07-02 00:59:41 -0400719void xfgets(char *str, int size, FILE *in)
Jean Sacren4418a2b2010-08-04 16:03:16 -0600720{
721 if (fgets(str, size, in) == NULL)
722 fprintf(stderr, "\nError in reading or end of file.\n");
723}