blob: 063c9e7a34c1916090ff31754c8497d3a3f72b36 [file] [log] [blame]
Masahiro Yamada0c874102018-12-18 21:13:35 +09001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
6#include <ctype.h>
Markus Mayer74dba802015-12-09 14:56:12 -08007#include <limits.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>
Ingo Molnarb0fe5512009-03-12 15:15:31 +010014#include <sys/time.h>
Yann E. MORIN0d8024c2013-04-13 22:49:13 +020015#include <errno.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);
21
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020022enum input_mode {
23 oldaskconfig,
Masahiro Yamada911a91c2018-03-01 15:34:37 +090024 syncconfig,
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020025 oldconfig,
26 allnoconfig,
27 allyesconfig,
28 allmodconfig,
Sam Ravnborg0748cb32010-07-31 23:35:31 +020029 alldefconfig,
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020030 randconfig,
31 defconfig,
Sam Ravnborg7cf3d732010-07-31 23:35:34 +020032 savedefconfig,
Sam Ravnborg861b4ea2010-07-31 23:35:28 +020033 listnewconfig,
Laura Abbott5d8b42a2019-11-04 17:10:08 -050034 helpnewconfig,
Adam Leefb16d892012-09-01 01:05:17 +080035 olddefconfig,
Tetsuo Handa89b90602019-12-17 18:42:06 +090036 yes2modconfig,
37 mod2yesconfig,
Masahiro Yamada52e58a32018-01-11 22:39:39 +090038};
39static enum input_mode input_mode = oldaskconfig;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +020040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static int indent = 1;
Ben Hutchings62dc9892013-02-19 02:24:26 +020042static int tty_stdio;
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
Masahiro Yamada5a3dc712018-01-11 22:39:40 +090075/* Helper function to facilitate fgets() by Jean Sacren. */
76static void xfgets(char *str, int size, FILE *in)
77{
78 if (!fgets(str, size, in))
79 fprintf(stderr, "\nError in reading or end of file.\n");
Masahiro Yamadaf3ff6fb2018-02-08 14:56:40 +090080
81 if (!tty_stdio)
82 printf("%s", str);
Masahiro Yamada5a3dc712018-01-11 22:39:40 +090083}
84
Masahiro Yamada89145642021-03-14 04:48:24 +090085static void set_randconfig_seed(void)
86{
87 unsigned int seed;
88 char *env;
89 bool seed_set = false;
90
91 env = getenv("KCONFIG_SEED");
92 if (env && *env) {
93 char *endp;
94
95 seed = strtol(env, &endp, 0);
96 if (*endp == '\0')
97 seed_set = true;
98 }
99
100 if (!seed_set) {
101 struct timeval now;
102
103 /*
104 * Use microseconds derived seed, compensate for systems where it may
105 * be zero.
106 */
107 gettimeofday(&now, NULL);
108 seed = (now.tv_sec + 1) * (now.tv_usec + 1);
109 }
110
111 printf("KCONFIG_SEED=0x%X\n", seed);
112 srand(seed);
113}
114
Roman Zippelf82f3f92007-08-30 05:06:17 +0200115static int conf_askvalue(struct symbol *sym, const char *def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (!sym_has_value(sym))
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200118 printf("(NEW) ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 line[0] = '\n';
121 line[1] = 0;
122
Marco Ammonbaa23ec2019-07-04 12:50:41 +0200123 if (!sym_is_changeable(sym)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 printf("%s\n", def);
125 line[0] = '\n';
126 line[1] = 0;
Roman Zippelf82f3f92007-08-30 05:06:17 +0200127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129
130 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200131 case oldconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900132 case syncconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (sym_has_value(sym)) {
134 printf("%s\n", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400137 /* fall through */
Masahiro Yamada102a1a72021-02-21 18:26:23 +0900138 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 fflush(stdout);
Markus Mayer74dba802015-12-09 14:56:12 -0800140 xfgets(line, sizeof(line), stdin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 break;
142 }
143
Roman Zippelf82f3f92007-08-30 05:06:17 +0200144 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Trevor Keith4356f482009-09-18 12:49:23 -0700147static int conf_string(struct menu *menu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 struct symbol *sym = menu->sym;
Sam Ravnborg03d29122007-07-21 00:00:36 +0200150 const char *def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 while (1) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200153 printf("%*s%s ", indent - 1, "", menu->prompt->text);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 printf("(%s) ", sym->name);
155 def = sym_get_string_value(sym);
Mickaël Salaünf82bd802021-02-15 19:15:09 +0100156 if (def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 printf("[%s] ", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200158 if (!conf_askvalue(sym, def))
159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 switch (line[0]) {
161 case '\n':
162 break;
163 case '?':
164 /* print help */
165 if (line[1] == '\n') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800166 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 def = NULL;
168 break;
169 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400170 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 default:
172 line[strlen(line)-1] = 0;
173 def = line;
174 }
175 if (def && sym_set_string_value(sym, def))
176 return 0;
177 }
178}
179
180static int conf_sym(struct menu *menu)
181{
182 struct symbol *sym = menu->sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 tristate oldval, newval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 while (1) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200186 printf("%*s%s ", indent - 1, "", menu->prompt->text);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (sym->name)
188 printf("(%s) ", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 putchar('[');
190 oldval = sym_get_tristate_value(sym);
191 switch (oldval) {
192 case no:
193 putchar('N');
194 break;
195 case mod:
196 putchar('M');
197 break;
198 case yes:
199 putchar('Y');
200 break;
201 }
202 if (oldval != no && sym_tristate_within_range(sym, no))
203 printf("/n");
204 if (oldval != mod && sym_tristate_within_range(sym, mod))
205 printf("/m");
206 if (oldval != yes && sym_tristate_within_range(sym, yes))
207 printf("/y");
Masahiro Yamada4f208f32018-02-06 09:34:43 +0900208 printf("/?] ");
Roman Zippelf82f3f92007-08-30 05:06:17 +0200209 if (!conf_askvalue(sym, sym_get_string_value(sym)))
210 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 strip(line);
212
213 switch (line[0]) {
214 case 'n':
215 case 'N':
216 newval = no;
217 if (!line[1] || !strcmp(&line[1], "o"))
218 break;
219 continue;
220 case 'm':
221 case 'M':
222 newval = mod;
223 if (!line[1])
224 break;
225 continue;
226 case 'y':
227 case 'Y':
228 newval = yes;
229 if (!line[1] || !strcmp(&line[1], "es"))
230 break;
231 continue;
232 case 0:
233 newval = oldval;
234 break;
235 case '?':
236 goto help;
237 default:
238 continue;
239 }
240 if (sym_set_tristate_value(sym, newval))
241 return 0;
242help:
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800243 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245}
246
247static int conf_choice(struct menu *menu)
248{
249 struct symbol *sym, *def_sym;
250 struct menu *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 bool is_new;
252
253 sym = menu->sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 is_new = !sym_has_value(sym);
Marco Ammonbaa23ec2019-07-04 12:50:41 +0200255 if (sym_is_changeable(sym)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 conf_sym(menu);
257 sym_calc_value(sym);
258 switch (sym_get_tristate_value(sym)) {
259 case no:
260 return 1;
261 case mod:
262 return 0;
263 case yes:
264 break;
265 }
266 } else {
267 switch (sym_get_tristate_value(sym)) {
268 case no:
269 return 1;
270 case mod:
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200271 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return 0;
273 case yes:
274 break;
275 }
276 }
277
278 while (1) {
279 int cnt, def;
280
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200281 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 def_sym = sym_get_choice_value(sym);
283 cnt = def = 0;
Roman Zippel40aee722006-04-09 17:26:39 +0200284 line[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 for (child = menu->list; child; child = child->next) {
286 if (!menu_is_visible(child))
287 continue;
288 if (!child->sym) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200289 printf("%*c %s\n", indent, '*', menu_get_prompt(child));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 continue;
291 }
292 cnt++;
293 if (child->sym == def_sym) {
294 def = cnt;
295 printf("%*c", indent, '>');
296 } else
297 printf("%*c", indent, ' ');
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200298 printf(" %d. %s", cnt, menu_get_prompt(child));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (child->sym->name)
300 printf(" (%s)", child->sym->name);
301 if (!sym_has_value(child->sym))
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200302 printf(" (NEW)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 printf("\n");
304 }
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200305 printf("%*schoice", indent - 1, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (cnt == 1) {
307 printf("[1]: 1\n");
308 goto conf_childs;
309 }
Masahiro Yamada4f208f32018-02-06 09:34:43 +0900310 printf("[1-%d?]: ", cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200312 case oldconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900313 case syncconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (!is_new) {
315 cnt = def;
316 printf("%d\n", cnt);
317 break;
318 }
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:
Masahiro Yamada2aad9b82018-02-28 09:15:24 +0900378 /*
379 * Except in oldaskconfig mode, we show only menus that
380 * contain new symbols.
381 */
382 if (input_mode != oldaskconfig && rootEntry != menu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 check_conf(menu);
384 return;
385 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400386 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 case P_COMMENT:
388 prompt = menu_get_prompt(menu);
389 if (prompt)
390 printf("%*c\n%*c %s\n%*c\n",
391 indent, '*',
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200392 indent, '*', prompt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 indent, '*');
394 default:
395 ;
396 }
397 }
398
399 if (!sym)
400 goto conf_childs;
401
402 if (sym_is_choice(sym)) {
403 conf_choice(menu);
404 if (sym->curr.tri != mod)
405 return;
406 goto conf_childs;
407 }
408
409 switch (sym->type) {
410 case S_INT:
411 case S_HEX:
412 case S_STRING:
413 conf_string(menu);
414 break;
415 default:
416 conf_sym(menu);
417 break;
418 }
419
420conf_childs:
421 if (sym)
422 indent += 2;
423 for (child = menu->list; child; child = child->next)
424 conf(child);
425 if (sym)
426 indent -= 2;
427}
428
429static void check_conf(struct menu *menu)
430{
431 struct symbol *sym;
432 struct menu *child;
433
434 if (!menu_is_visible(menu))
435 return;
436
437 sym = menu->sym;
Masahiro Yamadaa4cff322021-02-21 18:26:22 +0900438 if (sym && !sym_has_value(sym) &&
439 (sym_is_changeable(sym) ||
440 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes))) {
Don Zickus17baab62018-04-11 15:15:37 -0400441
Masahiro Yamadaa4cff322021-02-21 18:26:22 +0900442 switch (input_mode) {
443 case listnewconfig:
444 if (sym->name) {
445 const char *str;
446
447 if (sym->type == S_STRING) {
448 str = sym_get_string_value(sym);
449 str = sym_escape_string_value(str);
450 printf("%s%s=%s\n", CONFIG_, sym->name, str);
451 free((void *)str);
452 } else {
453 str = sym_get_string_value(sym);
454 printf("%s%s=%s\n", CONFIG_, sym->name, str);
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400455 }
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400456 }
Masahiro Yamadaa4cff322021-02-21 18:26:22 +0900457 break;
458 case helpnewconfig:
459 printf("-----\n");
460 print_help(menu);
461 printf("-----\n");
462 break;
463 default:
464 if (!conf_cnt++)
465 printf("*\n* Restart config...\n*\n");
466 rootEntry = menu_get_parent_menu(menu);
467 conf(rootEntry);
468 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471
472 for (child = menu->list; child; child = child->next)
473 check_conf(child);
474}
475
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200476static struct option long_opts[] = {
477 {"oldaskconfig", no_argument, NULL, oldaskconfig},
478 {"oldconfig", no_argument, NULL, oldconfig},
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900479 {"syncconfig", no_argument, NULL, syncconfig},
Masahiro Yamadab6f7e9f2019-05-27 23:37:22 +0900480 {"defconfig", required_argument, NULL, defconfig},
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200481 {"savedefconfig", required_argument, NULL, savedefconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200482 {"allnoconfig", no_argument, NULL, allnoconfig},
483 {"allyesconfig", no_argument, NULL, allyesconfig},
484 {"allmodconfig", no_argument, NULL, allmodconfig},
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200485 {"alldefconfig", no_argument, NULL, alldefconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200486 {"randconfig", no_argument, NULL, randconfig},
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200487 {"listnewconfig", no_argument, NULL, listnewconfig},
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500488 {"helpnewconfig", no_argument, NULL, helpnewconfig},
Adam Leefb16d892012-09-01 01:05:17 +0800489 {"olddefconfig", no_argument, NULL, olddefconfig},
Tetsuo Handa89b90602019-12-17 18:42:06 +0900490 {"yes2modconfig", no_argument, NULL, yes2modconfig},
491 {"mod2yesconfig", no_argument, NULL, mod2yesconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200492 {NULL, 0, NULL, 0}
493};
494
Arnaud Lacombe32543992010-11-02 00:26:33 -0400495static void conf_usage(const char *progname)
496{
497
Michal Marek0a1f00a2015-04-08 13:30:42 +0200498 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
Arnaud Lacombe32543992010-11-02 00:26:33 -0400499 printf("[option] is _one_ of the following:\n");
500 printf(" --listnewconfig List new options\n");
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500501 printf(" --helpnewconfig List new options and help text\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400502 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
503 printf(" --oldconfig Update a configuration using a provided .config as base\n");
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900504 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
505 " include/{generated/,config/}\n");
Marc Herbertcedd55d2018-01-26 14:59:00 -0800506 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400507 printf(" --defconfig <file> New config with default defined in <file>\n");
508 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
509 printf(" --allnoconfig New config where all options are answered with no\n");
510 printf(" --allyesconfig New config where all options are answered with yes\n");
511 printf(" --allmodconfig New config where all options are answered with mod\n");
512 printf(" --alldefconfig New config with all symbols set to default\n");
513 printf(" --randconfig New config with random answer to all options\n");
Tetsuo Handa89b90602019-12-17 18:42:06 +0900514 printf(" --yes2modconfig Change answers from yes to mod if possible\n");
515 printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
Masahiro Yamadaae8da722021-02-21 22:03:17 +0900516 printf(" (If none of the above is given, --oldaskconfig is the default)\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400517}
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519int main(int ac, char **av)
520{
Arnaud Lacombe32543992010-11-02 00:26:33 -0400521 const char *progname = av[0];
Andres Salomon2f4b4892007-12-17 01:34:58 -0500522 int opt;
Arnaud Lacombe275744c2010-10-13 20:43:28 -0400523 const char *name, *defconfig_file = NULL /* gcc uninit */;
Masahiro Yamada16952b72018-07-20 16:46:30 +0900524 int no_conf_write = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Masahiro Yamadaf3ff6fb2018-02-08 14:56:40 +0900526 tty_stdio = isatty(0) && isatty(1);
Ben Hutchings62dc9892013-02-19 02:24:26 +0200527
Masahiro Yamadaa2af62c2021-02-21 22:03:16 +0900528 while ((opt = getopt_long(ac, av, "hs", long_opts, NULL)) != -1) {
Michal Marek0a1f00a2015-04-08 13:30:42 +0200529 if (opt == 's') {
530 conf_set_message_callback(NULL);
531 continue;
532 }
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200533 input_mode = (enum input_mode)opt;
Andres Salomon2f4b4892007-12-17 01:34:58 -0500534 switch (opt) {
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900535 case syncconfig:
Masahiro Yamada9a9ddcf2018-08-16 16:48:26 +0900536 /*
537 * syncconfig is invoked during the build stage.
538 * Suppress distracting "configuration written to ..."
539 */
540 conf_set_message_callback(NULL);
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200541 sync_kconfig = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200543 case defconfig:
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200544 case savedefconfig:
Andres Salomon2f4b4892007-12-17 01:34:58 -0500545 defconfig_file = optarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200547 case randconfig:
Masahiro Yamada89145642021-03-14 04:48:24 +0900548 set_randconfig_seed();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 break;
Arnaud Lacombe32543992010-11-02 00:26:33 -0400550 case oldaskconfig:
551 case oldconfig:
552 case allnoconfig:
553 case allyesconfig:
554 case allmodconfig:
555 case alldefconfig:
556 case listnewconfig:
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500557 case helpnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800558 case olddefconfig:
Tetsuo Handa89b90602019-12-17 18:42:06 +0900559 case yes2modconfig:
560 case mod2yesconfig:
Arnaud Lacombe32543992010-11-02 00:26:33 -0400561 break;
Masahiro Yamadaa2af62c2021-02-21 22:03:16 +0900562 case 'h':
Arnaud Lacombe32543992010-11-02 00:26:33 -0400563 conf_usage(progname);
Andres Salomon2f4b4892007-12-17 01:34:58 -0500564 exit(1);
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200565 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567 }
Andres Salomon2f4b4892007-12-17 01:34:58 -0500568 if (ac == optind) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200569 fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
Arnaud Lacombe32543992010-11-02 00:26:33 -0400570 conf_usage(progname);
Randy Dunlap250725a2006-06-08 22:12:50 -0700571 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
Andres Salomon2f4b4892007-12-17 01:34:58 -0500573 name = av[optind];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 conf_parse(name);
575 //zconfdump(stdout);
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200578 case defconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (conf_read(defconfig_file)) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900580 fprintf(stderr,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200581 "***\n"
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900582 "*** Can't find default configuration \"%s\"!\n"
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200583 "***\n",
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900584 defconfig_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 exit(1);
586 }
587 break;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200588 case savedefconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900589 case syncconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200590 case oldaskconfig:
591 case oldconfig:
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200592 case listnewconfig:
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500593 case helpnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800594 case olddefconfig:
Tetsuo Handa89b90602019-12-17 18:42:06 +0900595 case yes2modconfig:
596 case mod2yesconfig:
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,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200610 "*** Can't read seed configuration \"%s\"!\n",
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700611 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,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200627 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700628 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) {
Masahiro Yamada16952b72018-07-20 16:46:30 +0900637 name = getenv("KCONFIG_NOSILENTUPDATE");
638 if (name && *name) {
639 if (conf_get_changed()) {
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200640 fprintf(stderr,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200641 "\n*** The configuration requires explicit update.\n\n");
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200642 return 1;
643 }
Masahiro Yamada16952b72018-07-20 16:46:30 +0900644 no_conf_write = 1;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200645 }
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;
Tetsuo Handa89b90602019-12-17 18:42:06 +0900670 case yes2modconfig:
671 conf_rewrite_mod_or_yes(def_y2m);
672 break;
673 case mod2yesconfig:
674 conf_rewrite_mod_or_yes(def_m2y);
675 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200676 case oldaskconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200677 rootEntry = &rootmenu;
678 conf(&rootmenu);
Masahiro Yamada2aad9b82018-02-28 09:15:24 +0900679 input_mode = oldconfig;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200680 /* fall through */
Sam Ravnborg14828342010-08-06 07:13:54 +0200681 case oldconfig:
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200682 case listnewconfig:
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500683 case helpnewconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900684 case syncconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200685 /* Update until a loop caused no more changes */
686 do {
687 conf_cnt = 0;
688 check_conf(&rootmenu);
Masahiro Yamada99f0b652018-02-28 09:15:23 +0900689 } while (conf_cnt);
Masahiro Yamada59a80b52018-02-28 09:15:21 +0900690 break;
691 case olddefconfig:
692 default:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200693 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200695
Masahiro Yamada00c864f2018-07-20 16:46:31 +0900696 if (input_mode == savedefconfig) {
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200697 if (conf_write_defconfig(defconfig_file)) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200698 fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900699 defconfig_file);
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200700 return 1;
701 }
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500702 } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
Masahiro Yamada00c864f2018-07-20 16:46:31 +0900703 if (!no_conf_write && conf_write(NULL)) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200704 fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200705 exit(1);
706 }
Masahiro Yamada00c864f2018-07-20 16:46:31 +0900707
708 /*
709 * Create auto.conf if it does not exist.
710 * This prevents GNU Make 4.1 or older from emitting
711 * "include/config/auto.conf: No such file or directory"
712 * in the top-level Makefile
713 *
714 * syncconfig always creates or updates auto.conf because it is
715 * used during the build.
716 */
717 if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
718 fprintf(stderr,
719 "\n*** Error during sync of the configuration.\n\n");
720 return 1;
721 }
Roman Zippelc955cca2006-06-08 22:12:39 -0700722 }
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}