blob: 369615d6c97ee91b074aee0a961f848d2b053f0e [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
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))
Sam Ravnborg694c49a2018-05-22 21:36:12 +020090 printf("(NEW) ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 line[0] = '\n';
93 line[1] = 0;
94
Marco Ammonbaa23ec2019-07-04 12:50:41 +020095 if (!sym_is_changeable(sym)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 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:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900104 case syncconfig:
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 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400109 /* fall through */
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200110 case oldaskconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 fflush(stdout);
Markus Mayer74dba802015-12-09 14:56:12 -0800112 xfgets(line, sizeof(line), stdin);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200113 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 default:
115 break;
116 }
117
118 switch (type) {
119 case S_INT:
120 case S_HEX:
121 case S_STRING:
122 printf("%s\n", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200123 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 default:
125 ;
126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 printf("%s", line);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200128 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Trevor Keith4356f482009-09-18 12:49:23 -0700131static int conf_string(struct menu *menu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 struct symbol *sym = menu->sym;
Sam Ravnborg03d29122007-07-21 00:00:36 +0200134 const char *def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 while (1) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200137 printf("%*s%s ", indent - 1, "", menu->prompt->text);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 printf("(%s) ", sym->name);
139 def = sym_get_string_value(sym);
Mickaël Salaünf82bd802021-02-15 19:15:09 +0100140 if (def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 printf("[%s] ", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200142 if (!conf_askvalue(sym, def))
143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 switch (line[0]) {
145 case '\n':
146 break;
147 case '?':
148 /* print help */
149 if (line[1] == '\n') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800150 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 def = NULL;
152 break;
153 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400154 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 default:
156 line[strlen(line)-1] = 0;
157 def = line;
158 }
159 if (def && sym_set_string_value(sym, def))
160 return 0;
161 }
162}
163
164static int conf_sym(struct menu *menu)
165{
166 struct symbol *sym = menu->sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 tristate oldval, newval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 while (1) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200170 printf("%*s%s ", indent - 1, "", menu->prompt->text);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (sym->name)
172 printf("(%s) ", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 putchar('[');
174 oldval = sym_get_tristate_value(sym);
175 switch (oldval) {
176 case no:
177 putchar('N');
178 break;
179 case mod:
180 putchar('M');
181 break;
182 case yes:
183 putchar('Y');
184 break;
185 }
186 if (oldval != no && sym_tristate_within_range(sym, no))
187 printf("/n");
188 if (oldval != mod && sym_tristate_within_range(sym, mod))
189 printf("/m");
190 if (oldval != yes && sym_tristate_within_range(sym, yes))
191 printf("/y");
Masahiro Yamada4f208f32018-02-06 09:34:43 +0900192 printf("/?] ");
Roman Zippelf82f3f92007-08-30 05:06:17 +0200193 if (!conf_askvalue(sym, sym_get_string_value(sym)))
194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 strip(line);
196
197 switch (line[0]) {
198 case 'n':
199 case 'N':
200 newval = no;
201 if (!line[1] || !strcmp(&line[1], "o"))
202 break;
203 continue;
204 case 'm':
205 case 'M':
206 newval = mod;
207 if (!line[1])
208 break;
209 continue;
210 case 'y':
211 case 'Y':
212 newval = yes;
213 if (!line[1] || !strcmp(&line[1], "es"))
214 break;
215 continue;
216 case 0:
217 newval = oldval;
218 break;
219 case '?':
220 goto help;
221 default:
222 continue;
223 }
224 if (sym_set_tristate_value(sym, newval))
225 return 0;
226help:
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800227 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229}
230
231static int conf_choice(struct menu *menu)
232{
233 struct symbol *sym, *def_sym;
234 struct menu *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 bool is_new;
236
237 sym = menu->sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 is_new = !sym_has_value(sym);
Marco Ammonbaa23ec2019-07-04 12:50:41 +0200239 if (sym_is_changeable(sym)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 conf_sym(menu);
241 sym_calc_value(sym);
242 switch (sym_get_tristate_value(sym)) {
243 case no:
244 return 1;
245 case mod:
246 return 0;
247 case yes:
248 break;
249 }
250 } else {
251 switch (sym_get_tristate_value(sym)) {
252 case no:
253 return 1;
254 case mod:
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200255 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return 0;
257 case yes:
258 break;
259 }
260 }
261
262 while (1) {
263 int cnt, def;
264
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200265 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 def_sym = sym_get_choice_value(sym);
267 cnt = def = 0;
Roman Zippel40aee722006-04-09 17:26:39 +0200268 line[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 for (child = menu->list; child; child = child->next) {
270 if (!menu_is_visible(child))
271 continue;
272 if (!child->sym) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200273 printf("%*c %s\n", indent, '*', menu_get_prompt(child));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 continue;
275 }
276 cnt++;
277 if (child->sym == def_sym) {
278 def = cnt;
279 printf("%*c", indent, '>');
280 } else
281 printf("%*c", indent, ' ');
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200282 printf(" %d. %s", cnt, menu_get_prompt(child));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (child->sym->name)
284 printf(" (%s)", child->sym->name);
285 if (!sym_has_value(child->sym))
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200286 printf(" (NEW)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 printf("\n");
288 }
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200289 printf("%*schoice", indent - 1, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (cnt == 1) {
291 printf("[1]: 1\n");
292 goto conf_childs;
293 }
Masahiro Yamada4f208f32018-02-06 09:34:43 +0900294 printf("[1-%d?]: ", cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200296 case oldconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900297 case syncconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (!is_new) {
299 cnt = def;
300 printf("%d\n", cnt);
301 break;
302 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400303 /* fall through */
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200304 case oldaskconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 fflush(stdout);
Markus Mayer74dba802015-12-09 14:56:12 -0800306 xfgets(line, sizeof(line), stdin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 strip(line);
308 if (line[0] == '?') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800309 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 continue;
311 }
312 if (!line[0])
313 cnt = def;
314 else if (isdigit(line[0]))
315 cnt = atoi(line);
316 else
317 continue;
318 break;
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200319 default:
320 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322
323 conf_childs:
324 for (child = menu->list; child; child = child->next) {
325 if (!child->sym || !menu_is_visible(child))
326 continue;
327 if (!--cnt)
328 break;
329 }
330 if (!child)
331 continue;
Ben Hutchings3ba41622011-04-23 18:42:56 +0100332 if (line[0] && line[strlen(line) - 1] == '?') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800333 print_help(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 continue;
335 }
336 sym_set_choice_value(sym, child->sym);
Jan Beulichf5eaa322008-01-24 11:54:23 +0000337 for (child = child->list; child; child = child->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 indent += 2;
Jan Beulichf5eaa322008-01-24 11:54:23 +0000339 conf(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 indent -= 2;
341 }
342 return 1;
343 }
344}
345
346static void conf(struct menu *menu)
347{
348 struct symbol *sym;
349 struct property *prop;
350 struct menu *child;
351
352 if (!menu_is_visible(menu))
353 return;
354
355 sym = menu->sym;
356 prop = menu->prompt;
357 if (prop) {
358 const char *prompt;
359
360 switch (prop->type) {
361 case P_MENU:
Masahiro Yamada2aad9b82018-02-28 09:15:24 +0900362 /*
363 * Except in oldaskconfig mode, we show only menus that
364 * contain new symbols.
365 */
366 if (input_mode != oldaskconfig && rootEntry != menu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 check_conf(menu);
368 return;
369 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400370 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 case P_COMMENT:
372 prompt = menu_get_prompt(menu);
373 if (prompt)
374 printf("%*c\n%*c %s\n%*c\n",
375 indent, '*',
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200376 indent, '*', prompt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 indent, '*');
378 default:
379 ;
380 }
381 }
382
383 if (!sym)
384 goto conf_childs;
385
386 if (sym_is_choice(sym)) {
387 conf_choice(menu);
388 if (sym->curr.tri != mod)
389 return;
390 goto conf_childs;
391 }
392
393 switch (sym->type) {
394 case S_INT:
395 case S_HEX:
396 case S_STRING:
397 conf_string(menu);
398 break;
399 default:
400 conf_sym(menu);
401 break;
402 }
403
404conf_childs:
405 if (sym)
406 indent += 2;
407 for (child = menu->list; child; child = child->next)
408 conf(child);
409 if (sym)
410 indent -= 2;
411}
412
413static void check_conf(struct menu *menu)
414{
415 struct symbol *sym;
416 struct menu *child;
417
418 if (!menu_is_visible(menu))
419 return;
420
421 sym = menu->sym;
Masahiro Yamadaa4cff322021-02-21 18:26:22 +0900422 if (sym && !sym_has_value(sym) &&
423 (sym_is_changeable(sym) ||
424 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes))) {
Don Zickus17baab62018-04-11 15:15:37 -0400425
Masahiro Yamadaa4cff322021-02-21 18:26:22 +0900426 switch (input_mode) {
427 case listnewconfig:
428 if (sym->name) {
429 const char *str;
430
431 if (sym->type == S_STRING) {
432 str = sym_get_string_value(sym);
433 str = sym_escape_string_value(str);
434 printf("%s%s=%s\n", CONFIG_, sym->name, str);
435 free((void *)str);
436 } else {
437 str = sym_get_string_value(sym);
438 printf("%s%s=%s\n", CONFIG_, sym->name, str);
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400439 }
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400440 }
Masahiro Yamadaa4cff322021-02-21 18:26:22 +0900441 break;
442 case helpnewconfig:
443 printf("-----\n");
444 print_help(menu);
445 printf("-----\n");
446 break;
447 default:
448 if (!conf_cnt++)
449 printf("*\n* Restart config...\n*\n");
450 rootEntry = menu_get_parent_menu(menu);
451 conf(rootEntry);
452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
456 for (child = menu->list; child; child = child->next)
457 check_conf(child);
458}
459
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200460static struct option long_opts[] = {
461 {"oldaskconfig", no_argument, NULL, oldaskconfig},
462 {"oldconfig", no_argument, NULL, oldconfig},
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900463 {"syncconfig", no_argument, NULL, syncconfig},
Masahiro Yamadab6f7e9f2019-05-27 23:37:22 +0900464 {"defconfig", required_argument, NULL, defconfig},
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200465 {"savedefconfig", required_argument, NULL, savedefconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200466 {"allnoconfig", no_argument, NULL, allnoconfig},
467 {"allyesconfig", no_argument, NULL, allyesconfig},
468 {"allmodconfig", no_argument, NULL, allmodconfig},
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200469 {"alldefconfig", no_argument, NULL, alldefconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200470 {"randconfig", no_argument, NULL, randconfig},
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200471 {"listnewconfig", no_argument, NULL, listnewconfig},
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500472 {"helpnewconfig", no_argument, NULL, helpnewconfig},
Adam Leefb16d892012-09-01 01:05:17 +0800473 {"olddefconfig", no_argument, NULL, olddefconfig},
Tetsuo Handa89b90602019-12-17 18:42:06 +0900474 {"yes2modconfig", no_argument, NULL, yes2modconfig},
475 {"mod2yesconfig", no_argument, NULL, mod2yesconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200476 {NULL, 0, NULL, 0}
477};
478
Arnaud Lacombe32543992010-11-02 00:26:33 -0400479static void conf_usage(const char *progname)
480{
481
Michal Marek0a1f00a2015-04-08 13:30:42 +0200482 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
Arnaud Lacombe32543992010-11-02 00:26:33 -0400483 printf("[option] is _one_ of the following:\n");
484 printf(" --listnewconfig List new options\n");
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500485 printf(" --helpnewconfig List new options and help text\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400486 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
487 printf(" --oldconfig Update a configuration using a provided .config as base\n");
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900488 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
489 " include/{generated/,config/}\n");
Marc Herbertcedd55d2018-01-26 14:59:00 -0800490 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400491 printf(" --defconfig <file> New config with default defined in <file>\n");
492 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
493 printf(" --allnoconfig New config where all options are answered with no\n");
494 printf(" --allyesconfig New config where all options are answered with yes\n");
495 printf(" --allmodconfig New config where all options are answered with mod\n");
496 printf(" --alldefconfig New config with all symbols set to default\n");
497 printf(" --randconfig New config with random answer to all options\n");
Tetsuo Handa89b90602019-12-17 18:42:06 +0900498 printf(" --yes2modconfig Change answers from yes to mod if possible\n");
499 printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400500}
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502int main(int ac, char **av)
503{
Arnaud Lacombe32543992010-11-02 00:26:33 -0400504 const char *progname = av[0];
Andres Salomon2f4b4892007-12-17 01:34:58 -0500505 int opt;
Arnaud Lacombe275744c2010-10-13 20:43:28 -0400506 const char *name, *defconfig_file = NULL /* gcc uninit */;
Masahiro Yamada16952b72018-07-20 16:46:30 +0900507 int no_conf_write = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Masahiro Yamadaf3ff6fb2018-02-08 14:56:40 +0900509 tty_stdio = isatty(0) && isatty(1);
Ben Hutchings62dc9892013-02-19 02:24:26 +0200510
Michal Marek0a1f00a2015-04-08 13:30:42 +0200511 while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
512 if (opt == 's') {
513 conf_set_message_callback(NULL);
514 continue;
515 }
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200516 input_mode = (enum input_mode)opt;
Andres Salomon2f4b4892007-12-17 01:34:58 -0500517 switch (opt) {
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900518 case syncconfig:
Masahiro Yamada9a9ddcf2018-08-16 16:48:26 +0900519 /*
520 * syncconfig is invoked during the build stage.
521 * Suppress distracting "configuration written to ..."
522 */
523 conf_set_message_callback(NULL);
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200524 sync_kconfig = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200526 case defconfig:
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200527 case savedefconfig:
Andres Salomon2f4b4892007-12-17 01:34:58 -0500528 defconfig_file = optarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200530 case randconfig:
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100531 {
532 struct timeval now;
533 unsigned int seed;
Yann E. MORIN0d8024c2013-04-13 22:49:13 +0200534 char *seed_env;
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100535
536 /*
537 * Use microseconds derived seed,
538 * compensate for systems where it may be zero
539 */
540 gettimeofday(&now, NULL);
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100541 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
Yann E. MORIN0d8024c2013-04-13 22:49:13 +0200542
543 seed_env = getenv("KCONFIG_SEED");
544 if( seed_env && *seed_env ) {
545 char *endp;
Yann E. MORINe85ac122013-05-20 23:17:34 +0200546 int tmp = (int)strtol(seed_env, &endp, 0);
Yann E. MORIN0d8024c2013-04-13 22:49:13 +0200547 if (*endp == '\0') {
548 seed = tmp;
549 }
550 }
Yann E. MORINa5f6d792013-05-20 23:09:03 +0200551 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100552 srand(seed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 break;
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100554 }
Arnaud Lacombe32543992010-11-02 00:26:33 -0400555 case oldaskconfig:
556 case oldconfig:
557 case allnoconfig:
558 case allyesconfig:
559 case allmodconfig:
560 case alldefconfig:
561 case listnewconfig:
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500562 case helpnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800563 case olddefconfig:
Tetsuo Handa89b90602019-12-17 18:42:06 +0900564 case yes2modconfig:
565 case mod2yesconfig:
Arnaud Lacombe32543992010-11-02 00:26:33 -0400566 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200567 case '?':
Arnaud Lacombe32543992010-11-02 00:26:33 -0400568 conf_usage(progname);
Andres Salomon2f4b4892007-12-17 01:34:58 -0500569 exit(1);
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200570 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572 }
Andres Salomon2f4b4892007-12-17 01:34:58 -0500573 if (ac == optind) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200574 fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
Arnaud Lacombe32543992010-11-02 00:26:33 -0400575 conf_usage(progname);
Randy Dunlap250725a2006-06-08 22:12:50 -0700576 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Andres Salomon2f4b4892007-12-17 01:34:58 -0500578 name = av[optind];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 conf_parse(name);
580 //zconfdump(stdout);
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200583 case defconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (conf_read(defconfig_file)) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900585 fprintf(stderr,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200586 "***\n"
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900587 "*** Can't find default configuration \"%s\"!\n"
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200588 "***\n",
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900589 defconfig_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 exit(1);
591 }
592 break;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200593 case savedefconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900594 case syncconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200595 case oldaskconfig:
596 case oldconfig:
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200597 case listnewconfig:
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500598 case helpnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800599 case olddefconfig:
Tetsuo Handa89b90602019-12-17 18:42:06 +0900600 case yes2modconfig:
601 case mod2yesconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 conf_read(NULL);
603 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200604 case allnoconfig:
605 case allyesconfig:
606 case allmodconfig:
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200607 case alldefconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200608 case randconfig:
Roman Zippel90389162005-11-08 21:34:49 -0800609 name = getenv("KCONFIG_ALLCONFIG");
Eric W. Biederman9f420bf2012-05-07 05:37:45 -0700610 if (!name)
611 break;
612 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700613 if (conf_read_simple(name, S_DEF_USER)) {
614 fprintf(stderr,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200615 "*** Can't read seed configuration \"%s\"!\n",
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700616 name);
617 exit(1);
618 }
Roman Zippel90389162005-11-08 21:34:49 -0800619 break;
620 }
621 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200622 case allnoconfig: name = "allno.config"; break;
623 case allyesconfig: name = "allyes.config"; break;
624 case allmodconfig: name = "allmod.config"; break;
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200625 case alldefconfig: name = "alldef.config"; break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200626 case randconfig: name = "allrandom.config"; break;
Roman Zippel90389162005-11-08 21:34:49 -0800627 default: break;
628 }
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700629 if (conf_read_simple(name, S_DEF_USER) &&
630 conf_read_simple("all.config", S_DEF_USER)) {
631 fprintf(stderr,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200632 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700633 name);
634 exit(1);
635 }
Roman Zippel90389162005-11-08 21:34:49 -0800636 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 default:
638 break;
639 }
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200640
641 if (sync_kconfig) {
Masahiro Yamada16952b72018-07-20 16:46:30 +0900642 name = getenv("KCONFIG_NOSILENTUPDATE");
643 if (name && *name) {
644 if (conf_get_changed()) {
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200645 fprintf(stderr,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200646 "\n*** The configuration requires explicit update.\n\n");
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200647 return 1;
648 }
Masahiro Yamada16952b72018-07-20 16:46:30 +0900649 no_conf_write = 1;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200650 }
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200651 }
652
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200653 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200654 case allnoconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200655 conf_set_all_new_symbols(def_no);
656 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200657 case allyesconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200658 conf_set_all_new_symbols(def_yes);
659 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200660 case allmodconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200661 conf_set_all_new_symbols(def_mod);
662 break;
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200663 case alldefconfig:
664 conf_set_all_new_symbols(def_default);
665 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200666 case randconfig:
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +0200667 /* Really nothing to do in this loop */
668 while (conf_set_all_new_symbols(def_random)) ;
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200669 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200670 case defconfig:
Sam Ravnborg09748e12008-06-30 23:02:59 +0200671 conf_set_all_new_symbols(def_default);
672 break;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200673 case savedefconfig:
674 break;
Tetsuo Handa89b90602019-12-17 18:42:06 +0900675 case yes2modconfig:
676 conf_rewrite_mod_or_yes(def_y2m);
677 break;
678 case mod2yesconfig:
679 conf_rewrite_mod_or_yes(def_m2y);
680 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200681 case oldaskconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200682 rootEntry = &rootmenu;
683 conf(&rootmenu);
Masahiro Yamada2aad9b82018-02-28 09:15:24 +0900684 input_mode = oldconfig;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200685 /* fall through */
Sam Ravnborg14828342010-08-06 07:13:54 +0200686 case oldconfig:
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200687 case listnewconfig:
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500688 case helpnewconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900689 case syncconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200690 /* Update until a loop caused no more changes */
691 do {
692 conf_cnt = 0;
693 check_conf(&rootmenu);
Masahiro Yamada99f0b652018-02-28 09:15:23 +0900694 } while (conf_cnt);
Masahiro Yamada59a80b52018-02-28 09:15:21 +0900695 break;
696 case olddefconfig:
697 default:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200698 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200700
Masahiro Yamada00c864f2018-07-20 16:46:31 +0900701 if (input_mode == savedefconfig) {
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200702 if (conf_write_defconfig(defconfig_file)) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200703 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 }
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500707 } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
Masahiro Yamada00c864f2018-07-20 16:46:31 +0900708 if (!no_conf_write && conf_write(NULL)) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200709 fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200710 exit(1);
711 }
Masahiro Yamada00c864f2018-07-20 16:46:31 +0900712
713 /*
714 * Create auto.conf if it does not exist.
715 * This prevents GNU Make 4.1 or older from emitting
716 * "include/config/auto.conf: No such file or directory"
717 * in the top-level Makefile
718 *
719 * syncconfig always creates or updates auto.conf because it is
720 * used during the build.
721 */
722 if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
723 fprintf(stderr,
724 "\n*** Error during sync of the configuration.\n\n");
725 return 1;
726 }
Roman Zippelc955cca2006-06-08 22:12:39 -0700727 }
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200728 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}