blob: 89c9ba83f9e78796c9a1a132953e251aa46b43a1 [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;
Masahiro Yamadaed562c52021-03-14 04:48:25 +090040static int input_mode_opt;
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
Masahiro Yamada15e68d02021-03-14 04:48:29 +0900115static void conf_rewrite_mod_or_yes(enum conf_def_mode mode)
116{
117 struct symbol *sym;
118 int i;
119 tristate old_val = (mode == def_y2m) ? yes : mod;
120 tristate new_val = (mode == def_y2m) ? mod : yes;
121
122 for_all_symbols(i, sym) {
123 if (sym_get_type(sym) == S_TRISTATE &&
124 sym->def[S_DEF_USER].tri == old_val)
125 sym->def[S_DEF_USER].tri = new_val;
126 }
127 sym_clear_all_valid();
128}
129
Roman Zippelf82f3f92007-08-30 05:06:17 +0200130static int conf_askvalue(struct symbol *sym, const char *def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (!sym_has_value(sym))
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200133 printf("(NEW) ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 line[0] = '\n';
136 line[1] = 0;
137
Marco Ammonbaa23ec2019-07-04 12:50:41 +0200138 if (!sym_is_changeable(sym)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 printf("%s\n", def);
140 line[0] = '\n';
141 line[1] = 0;
Roman Zippelf82f3f92007-08-30 05:06:17 +0200142 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144
145 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200146 case oldconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900147 case syncconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (sym_has_value(sym)) {
149 printf("%s\n", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400152 /* fall through */
Masahiro Yamada102a1a72021-02-21 18:26:23 +0900153 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 fflush(stdout);
Markus Mayer74dba802015-12-09 14:56:12 -0800155 xfgets(line, sizeof(line), stdin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 break;
157 }
158
Roman Zippelf82f3f92007-08-30 05:06:17 +0200159 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Trevor Keith4356f482009-09-18 12:49:23 -0700162static int conf_string(struct menu *menu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 struct symbol *sym = menu->sym;
Sam Ravnborg03d29122007-07-21 00:00:36 +0200165 const char *def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 while (1) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200168 printf("%*s%s ", indent - 1, "", menu->prompt->text);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 printf("(%s) ", sym->name);
170 def = sym_get_string_value(sym);
Mickaël Salaünf82bd802021-02-15 19:15:09 +0100171 if (def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 printf("[%s] ", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200173 if (!conf_askvalue(sym, def))
174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 switch (line[0]) {
176 case '\n':
177 break;
178 case '?':
179 /* print help */
180 if (line[1] == '\n') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800181 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 def = NULL;
183 break;
184 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400185 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 default:
187 line[strlen(line)-1] = 0;
188 def = line;
189 }
190 if (def && sym_set_string_value(sym, def))
191 return 0;
192 }
193}
194
195static int conf_sym(struct menu *menu)
196{
197 struct symbol *sym = menu->sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 tristate oldval, newval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 while (1) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200201 printf("%*s%s ", indent - 1, "", menu->prompt->text);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (sym->name)
203 printf("(%s) ", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 putchar('[');
205 oldval = sym_get_tristate_value(sym);
206 switch (oldval) {
207 case no:
208 putchar('N');
209 break;
210 case mod:
211 putchar('M');
212 break;
213 case yes:
214 putchar('Y');
215 break;
216 }
217 if (oldval != no && sym_tristate_within_range(sym, no))
218 printf("/n");
219 if (oldval != mod && sym_tristate_within_range(sym, mod))
220 printf("/m");
221 if (oldval != yes && sym_tristate_within_range(sym, yes))
222 printf("/y");
Masahiro Yamada4f208f32018-02-06 09:34:43 +0900223 printf("/?] ");
Roman Zippelf82f3f92007-08-30 05:06:17 +0200224 if (!conf_askvalue(sym, sym_get_string_value(sym)))
225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 strip(line);
227
228 switch (line[0]) {
229 case 'n':
230 case 'N':
231 newval = no;
232 if (!line[1] || !strcmp(&line[1], "o"))
233 break;
234 continue;
235 case 'm':
236 case 'M':
237 newval = mod;
238 if (!line[1])
239 break;
240 continue;
241 case 'y':
242 case 'Y':
243 newval = yes;
244 if (!line[1] || !strcmp(&line[1], "es"))
245 break;
246 continue;
247 case 0:
248 newval = oldval;
249 break;
250 case '?':
251 goto help;
252 default:
253 continue;
254 }
255 if (sym_set_tristate_value(sym, newval))
256 return 0;
257help:
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800258 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260}
261
262static int conf_choice(struct menu *menu)
263{
264 struct symbol *sym, *def_sym;
265 struct menu *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 bool is_new;
267
268 sym = menu->sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 is_new = !sym_has_value(sym);
Marco Ammonbaa23ec2019-07-04 12:50:41 +0200270 if (sym_is_changeable(sym)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 conf_sym(menu);
272 sym_calc_value(sym);
273 switch (sym_get_tristate_value(sym)) {
274 case no:
275 return 1;
276 case mod:
277 return 0;
278 case yes:
279 break;
280 }
281 } else {
282 switch (sym_get_tristate_value(sym)) {
283 case no:
284 return 1;
285 case mod:
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200286 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return 0;
288 case yes:
289 break;
290 }
291 }
292
293 while (1) {
294 int cnt, def;
295
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200296 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 def_sym = sym_get_choice_value(sym);
298 cnt = def = 0;
Roman Zippel40aee722006-04-09 17:26:39 +0200299 line[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 for (child = menu->list; child; child = child->next) {
301 if (!menu_is_visible(child))
302 continue;
303 if (!child->sym) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200304 printf("%*c %s\n", indent, '*', menu_get_prompt(child));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 continue;
306 }
307 cnt++;
308 if (child->sym == def_sym) {
309 def = cnt;
310 printf("%*c", indent, '>');
311 } else
312 printf("%*c", indent, ' ');
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200313 printf(" %d. %s", cnt, menu_get_prompt(child));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (child->sym->name)
315 printf(" (%s)", child->sym->name);
316 if (!sym_has_value(child->sym))
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200317 printf(" (NEW)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 printf("\n");
319 }
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200320 printf("%*schoice", indent - 1, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (cnt == 1) {
322 printf("[1]: 1\n");
323 goto conf_childs;
324 }
Masahiro Yamada4f208f32018-02-06 09:34:43 +0900325 printf("[1-%d?]: ", cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200327 case oldconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900328 case syncconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (!is_new) {
330 cnt = def;
331 printf("%d\n", cnt);
332 break;
333 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400334 /* fall through */
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200335 case oldaskconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 fflush(stdout);
Markus Mayer74dba802015-12-09 14:56:12 -0800337 xfgets(line, sizeof(line), stdin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 strip(line);
339 if (line[0] == '?') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800340 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 continue;
342 }
343 if (!line[0])
344 cnt = def;
345 else if (isdigit(line[0]))
346 cnt = atoi(line);
347 else
348 continue;
349 break;
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200350 default:
351 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353
354 conf_childs:
355 for (child = menu->list; child; child = child->next) {
356 if (!child->sym || !menu_is_visible(child))
357 continue;
358 if (!--cnt)
359 break;
360 }
361 if (!child)
362 continue;
Ben Hutchings3ba41622011-04-23 18:42:56 +0100363 if (line[0] && line[strlen(line) - 1] == '?') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800364 print_help(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 continue;
366 }
367 sym_set_choice_value(sym, child->sym);
Jan Beulichf5eaa322008-01-24 11:54:23 +0000368 for (child = child->list; child; child = child->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 indent += 2;
Jan Beulichf5eaa322008-01-24 11:54:23 +0000370 conf(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 indent -= 2;
372 }
373 return 1;
374 }
375}
376
377static void conf(struct menu *menu)
378{
379 struct symbol *sym;
380 struct property *prop;
381 struct menu *child;
382
383 if (!menu_is_visible(menu))
384 return;
385
386 sym = menu->sym;
387 prop = menu->prompt;
388 if (prop) {
389 const char *prompt;
390
391 switch (prop->type) {
392 case P_MENU:
Masahiro Yamada2aad9b82018-02-28 09:15:24 +0900393 /*
394 * Except in oldaskconfig mode, we show only menus that
395 * contain new symbols.
396 */
397 if (input_mode != oldaskconfig && rootEntry != menu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 check_conf(menu);
399 return;
400 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400401 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 case P_COMMENT:
403 prompt = menu_get_prompt(menu);
404 if (prompt)
405 printf("%*c\n%*c %s\n%*c\n",
406 indent, '*',
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200407 indent, '*', prompt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 indent, '*');
409 default:
410 ;
411 }
412 }
413
414 if (!sym)
415 goto conf_childs;
416
417 if (sym_is_choice(sym)) {
418 conf_choice(menu);
419 if (sym->curr.tri != mod)
420 return;
421 goto conf_childs;
422 }
423
424 switch (sym->type) {
425 case S_INT:
426 case S_HEX:
427 case S_STRING:
428 conf_string(menu);
429 break;
430 default:
431 conf_sym(menu);
432 break;
433 }
434
435conf_childs:
436 if (sym)
437 indent += 2;
438 for (child = menu->list; child; child = child->next)
439 conf(child);
440 if (sym)
441 indent -= 2;
442}
443
444static void check_conf(struct menu *menu)
445{
446 struct symbol *sym;
447 struct menu *child;
448
449 if (!menu_is_visible(menu))
450 return;
451
452 sym = menu->sym;
Masahiro Yamadaa4cff322021-02-21 18:26:22 +0900453 if (sym && !sym_has_value(sym) &&
454 (sym_is_changeable(sym) ||
455 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes))) {
Don Zickus17baab62018-04-11 15:15:37 -0400456
Masahiro Yamadaa4cff322021-02-21 18:26:22 +0900457 switch (input_mode) {
458 case listnewconfig:
459 if (sym->name) {
460 const char *str;
461
462 if (sym->type == S_STRING) {
463 str = sym_get_string_value(sym);
464 str = sym_escape_string_value(str);
465 printf("%s%s=%s\n", CONFIG_, sym->name, str);
466 free((void *)str);
467 } else {
468 str = sym_get_string_value(sym);
469 printf("%s%s=%s\n", CONFIG_, sym->name, str);
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400470 }
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400471 }
Masahiro Yamadaa4cff322021-02-21 18:26:22 +0900472 break;
473 case helpnewconfig:
474 printf("-----\n");
475 print_help(menu);
476 printf("-----\n");
477 break;
478 default:
479 if (!conf_cnt++)
480 printf("*\n* Restart config...\n*\n");
481 rootEntry = menu_get_parent_menu(menu);
482 conf(rootEntry);
483 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486
487 for (child = menu->list; child; child = child->next)
488 check_conf(child);
489}
490
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200491static struct option long_opts[] = {
Masahiro Yamadabafc4792021-03-14 04:48:26 +0900492 {"help", no_argument, NULL, 'h'},
493 {"silent", no_argument, NULL, 's'},
Masahiro Yamadaed562c52021-03-14 04:48:25 +0900494 {"oldaskconfig", no_argument, &input_mode_opt, oldaskconfig},
495 {"oldconfig", no_argument, &input_mode_opt, oldconfig},
496 {"syncconfig", no_argument, &input_mode_opt, syncconfig},
497 {"defconfig", required_argument, &input_mode_opt, defconfig},
498 {"savedefconfig", required_argument, &input_mode_opt, savedefconfig},
499 {"allnoconfig", no_argument, &input_mode_opt, allnoconfig},
500 {"allyesconfig", no_argument, &input_mode_opt, allyesconfig},
501 {"allmodconfig", no_argument, &input_mode_opt, allmodconfig},
502 {"alldefconfig", no_argument, &input_mode_opt, alldefconfig},
503 {"randconfig", no_argument, &input_mode_opt, randconfig},
504 {"listnewconfig", no_argument, &input_mode_opt, listnewconfig},
505 {"helpnewconfig", no_argument, &input_mode_opt, helpnewconfig},
506 {"olddefconfig", no_argument, &input_mode_opt, olddefconfig},
507 {"yes2modconfig", no_argument, &input_mode_opt, yes2modconfig},
508 {"mod2yesconfig", no_argument, &input_mode_opt, mod2yesconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200509 {NULL, 0, NULL, 0}
510};
511
Arnaud Lacombe32543992010-11-02 00:26:33 -0400512static void conf_usage(const char *progname)
513{
Masahiro Yamadaee4c6f02021-03-14 04:48:27 +0900514 printf("Usage: %s [options] <kconfig-file>\n", progname);
515 printf("\n");
516 printf("Generic options:\n");
517 printf(" -h, --help Print this message and exit.\n");
518 printf(" -s, --silent Do not print log.\n");
519 printf("\n");
520 printf("Mode options:\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400521 printf(" --listnewconfig List new options\n");
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500522 printf(" --helpnewconfig List new options and help text\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400523 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
524 printf(" --oldconfig Update a configuration using a provided .config as base\n");
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900525 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
526 " include/{generated/,config/}\n");
Marc Herbertcedd55d2018-01-26 14:59:00 -0800527 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400528 printf(" --defconfig <file> New config with default defined in <file>\n");
529 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
530 printf(" --allnoconfig New config where all options are answered with no\n");
531 printf(" --allyesconfig New config where all options are answered with yes\n");
532 printf(" --allmodconfig New config where all options are answered with mod\n");
533 printf(" --alldefconfig New config with all symbols set to default\n");
534 printf(" --randconfig New config with random answer to all options\n");
Tetsuo Handa89b90602019-12-17 18:42:06 +0900535 printf(" --yes2modconfig Change answers from yes to mod if possible\n");
536 printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
Masahiro Yamadaae8da722021-02-21 22:03:17 +0900537 printf(" (If none of the above is given, --oldaskconfig is the default)\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540int main(int ac, char **av)
541{
Arnaud Lacombe32543992010-11-02 00:26:33 -0400542 const char *progname = av[0];
Andres Salomon2f4b4892007-12-17 01:34:58 -0500543 int opt;
Arnaud Lacombe275744c2010-10-13 20:43:28 -0400544 const char *name, *defconfig_file = NULL /* gcc uninit */;
Masahiro Yamada16952b72018-07-20 16:46:30 +0900545 int no_conf_write = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Masahiro Yamadaf3ff6fb2018-02-08 14:56:40 +0900547 tty_stdio = isatty(0) && isatty(1);
Ben Hutchings62dc9892013-02-19 02:24:26 +0200548
Masahiro Yamadaa2af62c2021-02-21 22:03:16 +0900549 while ((opt = getopt_long(ac, av, "hs", long_opts, NULL)) != -1) {
Andres Salomon2f4b4892007-12-17 01:34:58 -0500550 switch (opt) {
Masahiro Yamadaa2af62c2021-02-21 22:03:16 +0900551 case 'h':
Arnaud Lacombe32543992010-11-02 00:26:33 -0400552 conf_usage(progname);
Andres Salomon2f4b4892007-12-17 01:34:58 -0500553 exit(1);
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200554 break;
Masahiro Yamadaed562c52021-03-14 04:48:25 +0900555 case 's':
556 conf_set_message_callback(NULL);
557 break;
558 case 0:
559 input_mode = input_mode_opt;
560 switch (input_mode) {
561 case syncconfig:
562 /*
563 * syncconfig is invoked during the build stage.
564 * Suppress distracting
565 * "configuration written to ..."
566 */
567 conf_set_message_callback(NULL);
568 sync_kconfig = 1;
569 break;
570 case defconfig:
571 case savedefconfig:
572 defconfig_file = optarg;
573 break;
574 case randconfig:
575 set_randconfig_seed();
576 break;
577 default:
578 break;
579 }
580 default:
581 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583 }
Andres Salomon2f4b4892007-12-17 01:34:58 -0500584 if (ac == optind) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200585 fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
Arnaud Lacombe32543992010-11-02 00:26:33 -0400586 conf_usage(progname);
Randy Dunlap250725a2006-06-08 22:12:50 -0700587 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Masahiro Yamada9a3c3bc2021-03-14 04:48:28 +0900589 conf_parse(av[optind]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 //zconfdump(stdout);
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200593 case defconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (conf_read(defconfig_file)) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900595 fprintf(stderr,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200596 "***\n"
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900597 "*** Can't find default configuration \"%s\"!\n"
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200598 "***\n",
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900599 defconfig_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 exit(1);
601 }
602 break;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200603 case savedefconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900604 case syncconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200605 case oldaskconfig:
606 case oldconfig:
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200607 case listnewconfig:
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500608 case helpnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800609 case olddefconfig:
Tetsuo Handa89b90602019-12-17 18:42:06 +0900610 case yes2modconfig:
611 case mod2yesconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 conf_read(NULL);
613 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200614 case allnoconfig:
615 case allyesconfig:
616 case allmodconfig:
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200617 case alldefconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200618 case randconfig:
Roman Zippel90389162005-11-08 21:34:49 -0800619 name = getenv("KCONFIG_ALLCONFIG");
Eric W. Biederman9f420bf2012-05-07 05:37:45 -0700620 if (!name)
621 break;
622 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700623 if (conf_read_simple(name, S_DEF_USER)) {
624 fprintf(stderr,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200625 "*** Can't read seed configuration \"%s\"!\n",
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700626 name);
627 exit(1);
628 }
Roman Zippel90389162005-11-08 21:34:49 -0800629 break;
630 }
631 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200632 case allnoconfig: name = "allno.config"; break;
633 case allyesconfig: name = "allyes.config"; break;
634 case allmodconfig: name = "allmod.config"; break;
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200635 case alldefconfig: name = "alldef.config"; break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200636 case randconfig: name = "allrandom.config"; break;
Roman Zippel90389162005-11-08 21:34:49 -0800637 default: break;
638 }
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700639 if (conf_read_simple(name, S_DEF_USER) &&
640 conf_read_simple("all.config", S_DEF_USER)) {
641 fprintf(stderr,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200642 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700643 name);
644 exit(1);
645 }
Roman Zippel90389162005-11-08 21:34:49 -0800646 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 default:
648 break;
649 }
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200650
651 if (sync_kconfig) {
Masahiro Yamada16952b72018-07-20 16:46:30 +0900652 name = getenv("KCONFIG_NOSILENTUPDATE");
653 if (name && *name) {
654 if (conf_get_changed()) {
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200655 fprintf(stderr,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200656 "\n*** The configuration requires explicit update.\n\n");
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200657 return 1;
658 }
Masahiro Yamada16952b72018-07-20 16:46:30 +0900659 no_conf_write = 1;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200660 }
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200661 }
662
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200663 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200664 case allnoconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200665 conf_set_all_new_symbols(def_no);
666 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200667 case allyesconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200668 conf_set_all_new_symbols(def_yes);
669 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200670 case allmodconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200671 conf_set_all_new_symbols(def_mod);
672 break;
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200673 case alldefconfig:
674 conf_set_all_new_symbols(def_default);
675 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200676 case randconfig:
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +0200677 /* Really nothing to do in this loop */
678 while (conf_set_all_new_symbols(def_random)) ;
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200679 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200680 case defconfig:
Sam Ravnborg09748e12008-06-30 23:02:59 +0200681 conf_set_all_new_symbols(def_default);
682 break;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200683 case savedefconfig:
684 break;
Tetsuo Handa89b90602019-12-17 18:42:06 +0900685 case yes2modconfig:
686 conf_rewrite_mod_or_yes(def_y2m);
687 break;
688 case mod2yesconfig:
689 conf_rewrite_mod_or_yes(def_m2y);
690 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200691 case oldaskconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200692 rootEntry = &rootmenu;
693 conf(&rootmenu);
Masahiro Yamada2aad9b82018-02-28 09:15:24 +0900694 input_mode = oldconfig;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200695 /* fall through */
Sam Ravnborg14828342010-08-06 07:13:54 +0200696 case oldconfig:
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200697 case listnewconfig:
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500698 case helpnewconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900699 case syncconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200700 /* Update until a loop caused no more changes */
701 do {
702 conf_cnt = 0;
703 check_conf(&rootmenu);
Masahiro Yamada99f0b652018-02-28 09:15:23 +0900704 } while (conf_cnt);
Masahiro Yamada59a80b52018-02-28 09:15:21 +0900705 break;
706 case olddefconfig:
707 default:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200708 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200710
Masahiro Yamada00c864f2018-07-20 16:46:31 +0900711 if (input_mode == savedefconfig) {
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200712 if (conf_write_defconfig(defconfig_file)) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200713 fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900714 defconfig_file);
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200715 return 1;
716 }
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500717 } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
Masahiro Yamada00c864f2018-07-20 16:46:31 +0900718 if (!no_conf_write && conf_write(NULL)) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200719 fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200720 exit(1);
721 }
Masahiro Yamada00c864f2018-07-20 16:46:31 +0900722
723 /*
724 * Create auto.conf if it does not exist.
725 * This prevents GNU Make 4.1 or older from emitting
726 * "include/config/auto.conf: No such file or directory"
727 * in the top-level Makefile
728 *
729 * syncconfig always creates or updates auto.conf because it is
730 * used during the build.
731 */
732 if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
733 fprintf(stderr,
734 "\n*** Error during sync of the configuration.\n\n");
735 return 1;
736 }
Roman Zippelc955cca2006-06-08 22:12:39 -0700737 }
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200738 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}