blob: 92111a07768051cee177ffd46a6f9ebd6299fea2 [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;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +020042static int sync_kconfig;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static int conf_cnt;
Markus Mayer74dba802015-12-09 14:56:12 -080044static char line[PATH_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static struct menu *rootEntry;
46
Cheng Renquan66c4bd82009-07-12 16:11:48 +080047static void print_help(struct menu *menu)
Sam Ravnborg03d29122007-07-21 00:00:36 +020048{
Cheng Renquan66c4bd82009-07-12 16:11:48 +080049 struct gstr help = str_new();
50
51 menu_get_ext_help(menu, &help);
52
53 printf("\n%s\n", str_get(&help));
54 str_free(&help);
Sam Ravnborg03d29122007-07-21 00:00:36 +020055}
56
J.A. Magallon48b9d032005-06-25 14:59:22 -070057static void strip(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
J.A. Magallon48b9d032005-06-25 14:59:22 -070059 char *p = str;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 int l;
61
62 while ((isspace(*p)))
63 p++;
64 l = strlen(p);
65 if (p != str)
66 memmove(str, p, l + 1);
67 if (!l)
68 return;
69 p = str + l - 1;
70 while ((isspace(*p)))
71 *p-- = 0;
72}
73
Masahiro Yamada5a3dc712018-01-11 22:39:40 +090074/* Helper function to facilitate fgets() by Jean Sacren. */
75static void xfgets(char *str, int size, FILE *in)
76{
77 if (!fgets(str, size, in))
78 fprintf(stderr, "\nError in reading or end of file.\n");
79}
80
Roman Zippelf82f3f92007-08-30 05:06:17 +020081static int conf_askvalue(struct symbol *sym, const char *def)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 enum symbol_type type = sym_get_type(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 if (!sym_has_value(sym))
EGRY Gabor534a4502008-01-11 23:44:39 +010086 printf(_("(NEW) "));
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 line[0] = '\n';
89 line[1] = 0;
90
91 if (!sym_is_changable(sym)) {
92 printf("%s\n", def);
93 line[0] = '\n';
94 line[1] = 0;
Roman Zippelf82f3f92007-08-30 05:06:17 +020095 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
97
98 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020099 case oldconfig:
100 case silentoldconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (sym_has_value(sym)) {
102 printf("%s\n", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400105 /* fall through */
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200106 case oldaskconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 fflush(stdout);
Markus Mayer74dba802015-12-09 14:56:12 -0800108 xfgets(line, sizeof(line), stdin);
Ben Hutchings62dc9892013-02-19 02:24:26 +0200109 if (!tty_stdio)
110 printf("\n");
Roman Zippelf82f3f92007-08-30 05:06:17 +0200111 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 default:
113 break;
114 }
115
116 switch (type) {
117 case S_INT:
118 case S_HEX:
119 case S_STRING:
120 printf("%s\n", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200121 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 default:
123 ;
124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 printf("%s", line);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200126 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Trevor Keith4356f482009-09-18 12:49:23 -0700129static int conf_string(struct menu *menu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct symbol *sym = menu->sym;
Sam Ravnborg03d29122007-07-21 00:00:36 +0200132 const char *def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 while (1) {
EGRY Gabor534a4502008-01-11 23:44:39 +0100135 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 printf("(%s) ", sym->name);
137 def = sym_get_string_value(sym);
138 if (sym_get_string_value(sym))
139 printf("[%s] ", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200140 if (!conf_askvalue(sym, def))
141 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 switch (line[0]) {
143 case '\n':
144 break;
145 case '?':
146 /* print help */
147 if (line[1] == '\n') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800148 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 def = NULL;
150 break;
151 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400152 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 default:
154 line[strlen(line)-1] = 0;
155 def = line;
156 }
157 if (def && sym_set_string_value(sym, def))
158 return 0;
159 }
160}
161
162static int conf_sym(struct menu *menu)
163{
164 struct symbol *sym = menu->sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 tristate oldval, newval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 while (1) {
EGRY Gabor534a4502008-01-11 23:44:39 +0100168 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (sym->name)
170 printf("(%s) ", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 putchar('[');
172 oldval = sym_get_tristate_value(sym);
173 switch (oldval) {
174 case no:
175 putchar('N');
176 break;
177 case mod:
178 putchar('M');
179 break;
180 case yes:
181 putchar('Y');
182 break;
183 }
184 if (oldval != no && sym_tristate_within_range(sym, no))
185 printf("/n");
186 if (oldval != mod && sym_tristate_within_range(sym, mod))
187 printf("/m");
188 if (oldval != yes && sym_tristate_within_range(sym, yes))
189 printf("/y");
Masahiro Yamada4f208f32018-02-06 09:34:43 +0900190 printf("/?] ");
Roman Zippelf82f3f92007-08-30 05:06:17 +0200191 if (!conf_askvalue(sym, sym_get_string_value(sym)))
192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 strip(line);
194
195 switch (line[0]) {
196 case 'n':
197 case 'N':
198 newval = no;
199 if (!line[1] || !strcmp(&line[1], "o"))
200 break;
201 continue;
202 case 'm':
203 case 'M':
204 newval = mod;
205 if (!line[1])
206 break;
207 continue;
208 case 'y':
209 case 'Y':
210 newval = yes;
211 if (!line[1] || !strcmp(&line[1], "es"))
212 break;
213 continue;
214 case 0:
215 newval = oldval;
216 break;
217 case '?':
218 goto help;
219 default:
220 continue;
221 }
222 if (sym_set_tristate_value(sym, newval))
223 return 0;
224help:
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800225 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227}
228
229static int conf_choice(struct menu *menu)
230{
231 struct symbol *sym, *def_sym;
232 struct menu *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 bool is_new;
234
235 sym = menu->sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 is_new = !sym_has_value(sym);
237 if (sym_is_changable(sym)) {
238 conf_sym(menu);
239 sym_calc_value(sym);
240 switch (sym_get_tristate_value(sym)) {
241 case no:
242 return 1;
243 case mod:
244 return 0;
245 case yes:
246 break;
247 }
248 } else {
249 switch (sym_get_tristate_value(sym)) {
250 case no:
251 return 1;
252 case mod:
EGRY Gabor534a4502008-01-11 23:44:39 +0100253 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return 0;
255 case yes:
256 break;
257 }
258 }
259
260 while (1) {
261 int cnt, def;
262
EGRY Gabor534a4502008-01-11 23:44:39 +0100263 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 def_sym = sym_get_choice_value(sym);
265 cnt = def = 0;
Roman Zippel40aee722006-04-09 17:26:39 +0200266 line[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 for (child = menu->list; child; child = child->next) {
268 if (!menu_is_visible(child))
269 continue;
270 if (!child->sym) {
EGRY Gabor534a4502008-01-11 23:44:39 +0100271 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 continue;
273 }
274 cnt++;
275 if (child->sym == def_sym) {
276 def = cnt;
277 printf("%*c", indent, '>');
278 } else
279 printf("%*c", indent, ' ');
EGRY Gabor534a4502008-01-11 23:44:39 +0100280 printf(" %d. %s", cnt, _(menu_get_prompt(child)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (child->sym->name)
282 printf(" (%s)", child->sym->name);
283 if (!sym_has_value(child->sym))
EGRY Gabor534a4502008-01-11 23:44:39 +0100284 printf(_(" (NEW)"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 printf("\n");
286 }
EGRY Gabor534a4502008-01-11 23:44:39 +0100287 printf(_("%*schoice"), indent - 1, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (cnt == 1) {
289 printf("[1]: 1\n");
290 goto conf_childs;
291 }
Masahiro Yamada4f208f32018-02-06 09:34:43 +0900292 printf("[1-%d?]: ", cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200294 case oldconfig:
295 case silentoldconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (!is_new) {
297 cnt = def;
298 printf("%d\n", cnt);
299 break;
300 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400301 /* fall through */
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200302 case oldaskconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 fflush(stdout);
Markus Mayer74dba802015-12-09 14:56:12 -0800304 xfgets(line, sizeof(line), stdin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 strip(line);
306 if (line[0] == '?') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800307 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 continue;
309 }
310 if (!line[0])
311 cnt = def;
312 else if (isdigit(line[0]))
313 cnt = atoi(line);
314 else
315 continue;
316 break;
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200317 default:
318 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
321 conf_childs:
322 for (child = menu->list; child; child = child->next) {
323 if (!child->sym || !menu_is_visible(child))
324 continue;
325 if (!--cnt)
326 break;
327 }
328 if (!child)
329 continue;
Ben Hutchings3ba41622011-04-23 18:42:56 +0100330 if (line[0] && line[strlen(line) - 1] == '?') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800331 print_help(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 continue;
333 }
334 sym_set_choice_value(sym, child->sym);
Jan Beulichf5eaa322008-01-24 11:54:23 +0000335 for (child = child->list; child; child = child->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 indent += 2;
Jan Beulichf5eaa322008-01-24 11:54:23 +0000337 conf(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 indent -= 2;
339 }
340 return 1;
341 }
342}
343
344static void conf(struct menu *menu)
345{
346 struct symbol *sym;
347 struct property *prop;
348 struct menu *child;
349
350 if (!menu_is_visible(menu))
351 return;
352
353 sym = menu->sym;
354 prop = menu->prompt;
355 if (prop) {
356 const char *prompt;
357
358 switch (prop->type) {
359 case P_MENU:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200360 if ((input_mode == silentoldconfig ||
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200361 input_mode == listnewconfig ||
Adam Leefb16d892012-09-01 01:05:17 +0800362 input_mode == olddefconfig) &&
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400363 rootEntry != menu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 check_conf(menu);
365 return;
366 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400367 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 case P_COMMENT:
369 prompt = menu_get_prompt(menu);
370 if (prompt)
371 printf("%*c\n%*c %s\n%*c\n",
372 indent, '*',
EGRY Gabor534a4502008-01-11 23:44:39 +0100373 indent, '*', _(prompt),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 indent, '*');
375 default:
376 ;
377 }
378 }
379
380 if (!sym)
381 goto conf_childs;
382
383 if (sym_is_choice(sym)) {
384 conf_choice(menu);
385 if (sym->curr.tri != mod)
386 return;
387 goto conf_childs;
388 }
389
390 switch (sym->type) {
391 case S_INT:
392 case S_HEX:
393 case S_STRING:
394 conf_string(menu);
395 break;
396 default:
397 conf_sym(menu);
398 break;
399 }
400
401conf_childs:
402 if (sym)
403 indent += 2;
404 for (child = menu->list; child; child = child->next)
405 conf(child);
406 if (sym)
407 indent -= 2;
408}
409
410static void check_conf(struct menu *menu)
411{
412 struct symbol *sym;
413 struct menu *child;
414
415 if (!menu_is_visible(menu))
416 return;
417
418 sym = menu->sym;
Roman Zippel3f23ca22005-11-08 21:34:48 -0800419 if (sym && !sym_has_value(sym)) {
420 if (sym_is_changable(sym) ||
421 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200422 if (input_mode == listnewconfig) {
423 if (sym->name && !sym_is_choice_value(sym)) {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400424 printf("%s%s\n", CONFIG_, sym->name);
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400425 }
Adam Leefb16d892012-09-01 01:05:17 +0800426 } else if (input_mode != olddefconfig) {
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400427 if (!conf_cnt++)
428 printf(_("*\n* Restart config...\n*\n"));
429 rootEntry = menu_get_parent_menu(menu);
430 conf(rootEntry);
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
435 for (child = menu->list; child; child = child->next)
436 check_conf(child);
437}
438
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200439static struct option long_opts[] = {
440 {"oldaskconfig", no_argument, NULL, oldaskconfig},
441 {"oldconfig", no_argument, NULL, oldconfig},
442 {"silentoldconfig", no_argument, NULL, silentoldconfig},
443 {"defconfig", optional_argument, NULL, defconfig},
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200444 {"savedefconfig", required_argument, NULL, savedefconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200445 {"allnoconfig", no_argument, NULL, allnoconfig},
446 {"allyesconfig", no_argument, NULL, allyesconfig},
447 {"allmodconfig", no_argument, NULL, allmodconfig},
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200448 {"alldefconfig", no_argument, NULL, alldefconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200449 {"randconfig", no_argument, NULL, randconfig},
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200450 {"listnewconfig", no_argument, NULL, listnewconfig},
Adam Leefb16d892012-09-01 01:05:17 +0800451 {"olddefconfig", no_argument, NULL, olddefconfig},
452 /*
453 * oldnoconfig is an alias of olddefconfig, because people already
454 * are dependent on its behavior(sets new symbols to their default
455 * value but not 'n') with the counter-intuitive name.
456 */
457 {"oldnoconfig", no_argument, NULL, olddefconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200458 {NULL, 0, NULL, 0}
459};
460
Arnaud Lacombe32543992010-11-02 00:26:33 -0400461static void conf_usage(const char *progname)
462{
463
Michal Marek0a1f00a2015-04-08 13:30:42 +0200464 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
Arnaud Lacombe32543992010-11-02 00:26:33 -0400465 printf("[option] is _one_ of the following:\n");
466 printf(" --listnewconfig List new options\n");
467 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
468 printf(" --oldconfig Update a configuration using a provided .config as base\n");
Marc Herbertcedd55d2018-01-26 14:59:00 -0800469 printf(" --silentoldconfig Similar to oldconfig but generates configuration in\n"
470 " include/{generated/,config/} (oldconfig used to be more verbose)\n");
471 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
Adam Leefb16d892012-09-01 01:05:17 +0800472 printf(" --oldnoconfig An alias of olddefconfig\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400473 printf(" --defconfig <file> New config with default defined in <file>\n");
474 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
475 printf(" --allnoconfig New config where all options are answered with no\n");
476 printf(" --allyesconfig New config where all options are answered with yes\n");
477 printf(" --allmodconfig New config where all options are answered with mod\n");
478 printf(" --alldefconfig New config with all symbols set to default\n");
479 printf(" --randconfig New config with random answer to all options\n");
480}
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482int main(int ac, char **av)
483{
Arnaud Lacombe32543992010-11-02 00:26:33 -0400484 const char *progname = av[0];
Andres Salomon2f4b4892007-12-17 01:34:58 -0500485 int opt;
Arnaud Lacombe275744c2010-10-13 20:43:28 -0400486 const char *name, *defconfig_file = NULL /* gcc uninit */;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 struct stat tmpstat;
488
EGRY Gabor534a4502008-01-11 23:44:39 +0100489 setlocale(LC_ALL, "");
490 bindtextdomain(PACKAGE, LOCALEDIR);
491 textdomain(PACKAGE);
492
Ben Hutchings62dc9892013-02-19 02:24:26 +0200493 tty_stdio = isatty(0) && isatty(1) && isatty(2);
494
Michal Marek0a1f00a2015-04-08 13:30:42 +0200495 while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
496 if (opt == 's') {
497 conf_set_message_callback(NULL);
498 continue;
499 }
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200500 input_mode = (enum input_mode)opt;
Andres Salomon2f4b4892007-12-17 01:34:58 -0500501 switch (opt) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200502 case silentoldconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200503 sync_kconfig = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200505 case defconfig:
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200506 case savedefconfig:
Andres Salomon2f4b4892007-12-17 01:34:58 -0500507 defconfig_file = optarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200509 case randconfig:
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100510 {
511 struct timeval now;
512 unsigned int seed;
Yann E. MORIN0d8024c2013-04-13 22:49:13 +0200513 char *seed_env;
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100514
515 /*
516 * Use microseconds derived seed,
517 * compensate for systems where it may be zero
518 */
519 gettimeofday(&now, NULL);
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100520 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
Yann E. MORIN0d8024c2013-04-13 22:49:13 +0200521
522 seed_env = getenv("KCONFIG_SEED");
523 if( seed_env && *seed_env ) {
524 char *endp;
Yann E. MORINe85ac122013-05-20 23:17:34 +0200525 int tmp = (int)strtol(seed_env, &endp, 0);
Yann E. MORIN0d8024c2013-04-13 22:49:13 +0200526 if (*endp == '\0') {
527 seed = tmp;
528 }
529 }
Yann E. MORINa5f6d792013-05-20 23:09:03 +0200530 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100531 srand(seed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 break;
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100533 }
Arnaud Lacombe32543992010-11-02 00:26:33 -0400534 case oldaskconfig:
535 case oldconfig:
536 case allnoconfig:
537 case allyesconfig:
538 case allmodconfig:
539 case alldefconfig:
540 case listnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800541 case olddefconfig:
Arnaud Lacombe32543992010-11-02 00:26:33 -0400542 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200543 case '?':
Arnaud Lacombe32543992010-11-02 00:26:33 -0400544 conf_usage(progname);
Andres Salomon2f4b4892007-12-17 01:34:58 -0500545 exit(1);
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200546 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548 }
Andres Salomon2f4b4892007-12-17 01:34:58 -0500549 if (ac == optind) {
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700550 printf(_("%s: Kconfig file missing\n"), av[0]);
Arnaud Lacombe32543992010-11-02 00:26:33 -0400551 conf_usage(progname);
Randy Dunlap250725a2006-06-08 22:12:50 -0700552 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Andres Salomon2f4b4892007-12-17 01:34:58 -0500554 name = av[optind];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 conf_parse(name);
556 //zconfdump(stdout);
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200557 if (sync_kconfig) {
Markus Heidelberg284026c2009-05-18 01:36:53 +0200558 name = conf_get_configname();
559 if (stat(name, &tmpstat)) {
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200560 fprintf(stderr, _("***\n"
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400561 "*** Configuration file \"%s\" not found!\n"
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200562 "***\n"
563 "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
564 "*** \"make menuconfig\" or \"make xconfig\").\n"
Markus Heidelberg284026c2009-05-18 01:36:53 +0200565 "***\n"), name);
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200566 exit(1);
567 }
568 }
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200571 case defconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (!defconfig_file)
573 defconfig_file = conf_get_default_confname();
574 if (conf_read(defconfig_file)) {
EGRY Gabor534a4502008-01-11 23:44:39 +0100575 printf(_("***\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 "*** Can't find default configuration \"%s\"!\n"
EGRY Gabor534a4502008-01-11 23:44:39 +0100577 "***\n"), defconfig_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 exit(1);
579 }
580 break;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200581 case savedefconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200582 case silentoldconfig:
583 case oldaskconfig:
584 case oldconfig:
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200585 case listnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800586 case olddefconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 conf_read(NULL);
588 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200589 case allnoconfig:
590 case allyesconfig:
591 case allmodconfig:
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200592 case alldefconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200593 case randconfig:
Roman Zippel90389162005-11-08 21:34:49 -0800594 name = getenv("KCONFIG_ALLCONFIG");
Eric W. Biederman9f420bf2012-05-07 05:37:45 -0700595 if (!name)
596 break;
597 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700598 if (conf_read_simple(name, S_DEF_USER)) {
599 fprintf(stderr,
600 _("*** Can't read seed configuration \"%s\"!\n"),
601 name);
602 exit(1);
603 }
Roman Zippel90389162005-11-08 21:34:49 -0800604 break;
605 }
606 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200607 case allnoconfig: name = "allno.config"; break;
608 case allyesconfig: name = "allyes.config"; break;
609 case allmodconfig: name = "allmod.config"; break;
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200610 case alldefconfig: name = "alldef.config"; break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200611 case randconfig: name = "allrandom.config"; break;
Roman Zippel90389162005-11-08 21:34:49 -0800612 default: break;
613 }
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700614 if (conf_read_simple(name, S_DEF_USER) &&
615 conf_read_simple("all.config", S_DEF_USER)) {
616 fprintf(stderr,
617 _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
618 name);
619 exit(1);
620 }
Roman Zippel90389162005-11-08 21:34:49 -0800621 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 default:
623 break;
624 }
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200625
626 if (sync_kconfig) {
627 if (conf_get_changed()) {
628 name = getenv("KCONFIG_NOSILENTUPDATE");
629 if (name && *name) {
630 fprintf(stderr,
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400631 _("\n*** The configuration requires explicit update.\n\n"));
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200632 return 1;
633 }
634 }
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200635 }
636
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200637 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200638 case allnoconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200639 conf_set_all_new_symbols(def_no);
640 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200641 case allyesconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200642 conf_set_all_new_symbols(def_yes);
643 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200644 case allmodconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200645 conf_set_all_new_symbols(def_mod);
646 break;
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200647 case alldefconfig:
648 conf_set_all_new_symbols(def_default);
649 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200650 case randconfig:
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +0200651 /* Really nothing to do in this loop */
652 while (conf_set_all_new_symbols(def_random)) ;
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200653 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200654 case defconfig:
Sam Ravnborg09748e12008-06-30 23:02:59 +0200655 conf_set_all_new_symbols(def_default);
656 break;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200657 case savedefconfig:
658 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200659 case oldaskconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200660 rootEntry = &rootmenu;
661 conf(&rootmenu);
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200662 input_mode = silentoldconfig;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200663 /* fall through */
Sam Ravnborg14828342010-08-06 07:13:54 +0200664 case oldconfig:
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200665 case listnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800666 case olddefconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200667 case silentoldconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200668 /* Update until a loop caused no more changes */
669 do {
670 conf_cnt = 0;
671 check_conf(&rootmenu);
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400672 } while (conf_cnt &&
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200673 (input_mode != listnewconfig &&
Adam Leefb16d892012-09-01 01:05:17 +0800674 input_mode != olddefconfig));
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200675 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200677
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200678 if (sync_kconfig) {
679 /* silentoldconfig is used during the build so we shall update autoconf.
680 * All other commands are only used to generate a config.
681 */
682 if (conf_get_changed() && conf_write(NULL)) {
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400683 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200684 exit(1);
685 }
686 if (conf_write_autoconf()) {
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400687 fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200688 return 1;
689 }
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200690 } else if (input_mode == savedefconfig) {
691 if (conf_write_defconfig(defconfig_file)) {
692 fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900693 defconfig_file);
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200694 return 1;
695 }
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200696 } else if (input_mode != listnewconfig) {
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200697 if (conf_write(NULL)) {
Arnaud Lacombe652cf982010-08-14 23:51:40 -0400698 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200699 exit(1);
700 }
Roman Zippelc955cca2006-06-08 22:12:39 -0700701 }
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}