blob: 37e17934b67a29c172cd07f0ab08d54242783c9f [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{
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (!sym_has_value(sym))
Sam Ravnborg694c49a2018-05-22 21:36:12 +020088 printf("(NEW) ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 line[0] = '\n';
91 line[1] = 0;
92
Marco Ammonbaa23ec2019-07-04 12:50:41 +020093 if (!sym_is_changeable(sym)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 printf("%s\n", def);
95 line[0] = '\n';
96 line[1] = 0;
Roman Zippelf82f3f92007-08-30 05:06:17 +020097 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99
100 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200101 case oldconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900102 case syncconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (sym_has_value(sym)) {
104 printf("%s\n", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400107 /* fall through */
Masahiro Yamada102a1a72021-02-21 18:26:23 +0900108 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 fflush(stdout);
Markus Mayer74dba802015-12-09 14:56:12 -0800110 xfgets(line, sizeof(line), stdin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 break;
112 }
113
Roman Zippelf82f3f92007-08-30 05:06:17 +0200114 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Trevor Keith4356f482009-09-18 12:49:23 -0700117static int conf_string(struct menu *menu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 struct symbol *sym = menu->sym;
Sam Ravnborg03d29122007-07-21 00:00:36 +0200120 const char *def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 while (1) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200123 printf("%*s%s ", indent - 1, "", menu->prompt->text);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 printf("(%s) ", sym->name);
125 def = sym_get_string_value(sym);
Mickaël Salaünf82bd802021-02-15 19:15:09 +0100126 if (def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 printf("[%s] ", def);
Roman Zippelf82f3f92007-08-30 05:06:17 +0200128 if (!conf_askvalue(sym, def))
129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 switch (line[0]) {
131 case '\n':
132 break;
133 case '?':
134 /* print help */
135 if (line[1] == '\n') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800136 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 def = NULL;
138 break;
139 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400140 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 default:
142 line[strlen(line)-1] = 0;
143 def = line;
144 }
145 if (def && sym_set_string_value(sym, def))
146 return 0;
147 }
148}
149
150static int conf_sym(struct menu *menu)
151{
152 struct symbol *sym = menu->sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 tristate oldval, newval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 while (1) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200156 printf("%*s%s ", indent - 1, "", menu->prompt->text);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (sym->name)
158 printf("(%s) ", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 putchar('[');
160 oldval = sym_get_tristate_value(sym);
161 switch (oldval) {
162 case no:
163 putchar('N');
164 break;
165 case mod:
166 putchar('M');
167 break;
168 case yes:
169 putchar('Y');
170 break;
171 }
172 if (oldval != no && sym_tristate_within_range(sym, no))
173 printf("/n");
174 if (oldval != mod && sym_tristate_within_range(sym, mod))
175 printf("/m");
176 if (oldval != yes && sym_tristate_within_range(sym, yes))
177 printf("/y");
Masahiro Yamada4f208f32018-02-06 09:34:43 +0900178 printf("/?] ");
Roman Zippelf82f3f92007-08-30 05:06:17 +0200179 if (!conf_askvalue(sym, sym_get_string_value(sym)))
180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 strip(line);
182
183 switch (line[0]) {
184 case 'n':
185 case 'N':
186 newval = no;
187 if (!line[1] || !strcmp(&line[1], "o"))
188 break;
189 continue;
190 case 'm':
191 case 'M':
192 newval = mod;
193 if (!line[1])
194 break;
195 continue;
196 case 'y':
197 case 'Y':
198 newval = yes;
199 if (!line[1] || !strcmp(&line[1], "es"))
200 break;
201 continue;
202 case 0:
203 newval = oldval;
204 break;
205 case '?':
206 goto help;
207 default:
208 continue;
209 }
210 if (sym_set_tristate_value(sym, newval))
211 return 0;
212help:
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800213 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215}
216
217static int conf_choice(struct menu *menu)
218{
219 struct symbol *sym, *def_sym;
220 struct menu *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 bool is_new;
222
223 sym = menu->sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 is_new = !sym_has_value(sym);
Marco Ammonbaa23ec2019-07-04 12:50:41 +0200225 if (sym_is_changeable(sym)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 conf_sym(menu);
227 sym_calc_value(sym);
228 switch (sym_get_tristate_value(sym)) {
229 case no:
230 return 1;
231 case mod:
232 return 0;
233 case yes:
234 break;
235 }
236 } else {
237 switch (sym_get_tristate_value(sym)) {
238 case no:
239 return 1;
240 case mod:
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200241 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return 0;
243 case yes:
244 break;
245 }
246 }
247
248 while (1) {
249 int cnt, def;
250
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200251 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 def_sym = sym_get_choice_value(sym);
253 cnt = def = 0;
Roman Zippel40aee722006-04-09 17:26:39 +0200254 line[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 for (child = menu->list; child; child = child->next) {
256 if (!menu_is_visible(child))
257 continue;
258 if (!child->sym) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200259 printf("%*c %s\n", indent, '*', menu_get_prompt(child));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 continue;
261 }
262 cnt++;
263 if (child->sym == def_sym) {
264 def = cnt;
265 printf("%*c", indent, '>');
266 } else
267 printf("%*c", indent, ' ');
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200268 printf(" %d. %s", cnt, menu_get_prompt(child));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (child->sym->name)
270 printf(" (%s)", child->sym->name);
271 if (!sym_has_value(child->sym))
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200272 printf(" (NEW)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 printf("\n");
274 }
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200275 printf("%*schoice", indent - 1, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (cnt == 1) {
277 printf("[1]: 1\n");
278 goto conf_childs;
279 }
Masahiro Yamada4f208f32018-02-06 09:34:43 +0900280 printf("[1-%d?]: ", cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200282 case oldconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900283 case syncconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (!is_new) {
285 cnt = def;
286 printf("%d\n", cnt);
287 break;
288 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400289 /* fall through */
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200290 case oldaskconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 fflush(stdout);
Markus Mayer74dba802015-12-09 14:56:12 -0800292 xfgets(line, sizeof(line), stdin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 strip(line);
294 if (line[0] == '?') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800295 print_help(menu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 continue;
297 }
298 if (!line[0])
299 cnt = def;
300 else if (isdigit(line[0]))
301 cnt = atoi(line);
302 else
303 continue;
304 break;
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200305 default:
306 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
309 conf_childs:
310 for (child = menu->list; child; child = child->next) {
311 if (!child->sym || !menu_is_visible(child))
312 continue;
313 if (!--cnt)
314 break;
315 }
316 if (!child)
317 continue;
Ben Hutchings3ba41622011-04-23 18:42:56 +0100318 if (line[0] && line[strlen(line) - 1] == '?') {
Cheng Renquan66c4bd82009-07-12 16:11:48 +0800319 print_help(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 continue;
321 }
322 sym_set_choice_value(sym, child->sym);
Jan Beulichf5eaa322008-01-24 11:54:23 +0000323 for (child = child->list; child; child = child->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 indent += 2;
Jan Beulichf5eaa322008-01-24 11:54:23 +0000325 conf(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 indent -= 2;
327 }
328 return 1;
329 }
330}
331
332static void conf(struct menu *menu)
333{
334 struct symbol *sym;
335 struct property *prop;
336 struct menu *child;
337
338 if (!menu_is_visible(menu))
339 return;
340
341 sym = menu->sym;
342 prop = menu->prompt;
343 if (prop) {
344 const char *prompt;
345
346 switch (prop->type) {
347 case P_MENU:
Masahiro Yamada2aad9b82018-02-28 09:15:24 +0900348 /*
349 * Except in oldaskconfig mode, we show only menus that
350 * contain new symbols.
351 */
352 if (input_mode != oldaskconfig && rootEntry != menu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 check_conf(menu);
354 return;
355 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400356 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 case P_COMMENT:
358 prompt = menu_get_prompt(menu);
359 if (prompt)
360 printf("%*c\n%*c %s\n%*c\n",
361 indent, '*',
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200362 indent, '*', prompt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 indent, '*');
364 default:
365 ;
366 }
367 }
368
369 if (!sym)
370 goto conf_childs;
371
372 if (sym_is_choice(sym)) {
373 conf_choice(menu);
374 if (sym->curr.tri != mod)
375 return;
376 goto conf_childs;
377 }
378
379 switch (sym->type) {
380 case S_INT:
381 case S_HEX:
382 case S_STRING:
383 conf_string(menu);
384 break;
385 default:
386 conf_sym(menu);
387 break;
388 }
389
390conf_childs:
391 if (sym)
392 indent += 2;
393 for (child = menu->list; child; child = child->next)
394 conf(child);
395 if (sym)
396 indent -= 2;
397}
398
399static void check_conf(struct menu *menu)
400{
401 struct symbol *sym;
402 struct menu *child;
403
404 if (!menu_is_visible(menu))
405 return;
406
407 sym = menu->sym;
Masahiro Yamadaa4cff322021-02-21 18:26:22 +0900408 if (sym && !sym_has_value(sym) &&
409 (sym_is_changeable(sym) ||
410 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes))) {
Don Zickus17baab62018-04-11 15:15:37 -0400411
Masahiro Yamadaa4cff322021-02-21 18:26:22 +0900412 switch (input_mode) {
413 case listnewconfig:
414 if (sym->name) {
415 const char *str;
416
417 if (sym->type == S_STRING) {
418 str = sym_get_string_value(sym);
419 str = sym_escape_string_value(str);
420 printf("%s%s=%s\n", CONFIG_, sym->name, str);
421 free((void *)str);
422 } else {
423 str = sym_get_string_value(sym);
424 printf("%s%s=%s\n", CONFIG_, sym->name, str);
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400425 }
Aristeu Rozanskif0778c82010-05-06 12:48:34 -0400426 }
Masahiro Yamadaa4cff322021-02-21 18:26:22 +0900427 break;
428 case helpnewconfig:
429 printf("-----\n");
430 print_help(menu);
431 printf("-----\n");
432 break;
433 default:
434 if (!conf_cnt++)
435 printf("*\n* Restart config...\n*\n");
436 rootEntry = menu_get_parent_menu(menu);
437 conf(rootEntry);
438 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
442 for (child = menu->list; child; child = child->next)
443 check_conf(child);
444}
445
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200446static struct option long_opts[] = {
447 {"oldaskconfig", no_argument, NULL, oldaskconfig},
448 {"oldconfig", no_argument, NULL, oldconfig},
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900449 {"syncconfig", no_argument, NULL, syncconfig},
Masahiro Yamadab6f7e9f2019-05-27 23:37:22 +0900450 {"defconfig", required_argument, NULL, defconfig},
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200451 {"savedefconfig", required_argument, NULL, savedefconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200452 {"allnoconfig", no_argument, NULL, allnoconfig},
453 {"allyesconfig", no_argument, NULL, allyesconfig},
454 {"allmodconfig", no_argument, NULL, allmodconfig},
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200455 {"alldefconfig", no_argument, NULL, alldefconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200456 {"randconfig", no_argument, NULL, randconfig},
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200457 {"listnewconfig", no_argument, NULL, listnewconfig},
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500458 {"helpnewconfig", no_argument, NULL, helpnewconfig},
Adam Leefb16d892012-09-01 01:05:17 +0800459 {"olddefconfig", no_argument, NULL, olddefconfig},
Tetsuo Handa89b90602019-12-17 18:42:06 +0900460 {"yes2modconfig", no_argument, NULL, yes2modconfig},
461 {"mod2yesconfig", no_argument, NULL, mod2yesconfig},
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200462 {NULL, 0, NULL, 0}
463};
464
Arnaud Lacombe32543992010-11-02 00:26:33 -0400465static void conf_usage(const char *progname)
466{
467
Michal Marek0a1f00a2015-04-08 13:30:42 +0200468 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
Arnaud Lacombe32543992010-11-02 00:26:33 -0400469 printf("[option] is _one_ of the following:\n");
470 printf(" --listnewconfig List new options\n");
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500471 printf(" --helpnewconfig List new options and help text\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400472 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
473 printf(" --oldconfig Update a configuration using a provided .config as base\n");
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900474 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
475 " include/{generated/,config/}\n");
Marc Herbertcedd55d2018-01-26 14:59:00 -0800476 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400477 printf(" --defconfig <file> New config with default defined in <file>\n");
478 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
479 printf(" --allnoconfig New config where all options are answered with no\n");
480 printf(" --allyesconfig New config where all options are answered with yes\n");
481 printf(" --allmodconfig New config where all options are answered with mod\n");
482 printf(" --alldefconfig New config with all symbols set to default\n");
483 printf(" --randconfig New config with random answer to all options\n");
Tetsuo Handa89b90602019-12-17 18:42:06 +0900484 printf(" --yes2modconfig Change answers from yes to mod if possible\n");
485 printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
Arnaud Lacombe32543992010-11-02 00:26:33 -0400486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488int main(int ac, char **av)
489{
Arnaud Lacombe32543992010-11-02 00:26:33 -0400490 const char *progname = av[0];
Andres Salomon2f4b4892007-12-17 01:34:58 -0500491 int opt;
Arnaud Lacombe275744c2010-10-13 20:43:28 -0400492 const char *name, *defconfig_file = NULL /* gcc uninit */;
Masahiro Yamada16952b72018-07-20 16:46:30 +0900493 int no_conf_write = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Masahiro Yamadaf3ff6fb2018-02-08 14:56:40 +0900495 tty_stdio = isatty(0) && isatty(1);
Ben Hutchings62dc9892013-02-19 02:24:26 +0200496
Masahiro Yamadaa2af62c2021-02-21 22:03:16 +0900497 while ((opt = getopt_long(ac, av, "hs", long_opts, NULL)) != -1) {
Michal Marek0a1f00a2015-04-08 13:30:42 +0200498 if (opt == 's') {
499 conf_set_message_callback(NULL);
500 continue;
501 }
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200502 input_mode = (enum input_mode)opt;
Andres Salomon2f4b4892007-12-17 01:34:58 -0500503 switch (opt) {
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900504 case syncconfig:
Masahiro Yamada9a9ddcf2018-08-16 16:48:26 +0900505 /*
506 * syncconfig is invoked during the build stage.
507 * Suppress distracting "configuration written to ..."
508 */
509 conf_set_message_callback(NULL);
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200510 sync_kconfig = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200512 case defconfig:
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200513 case savedefconfig:
Andres Salomon2f4b4892007-12-17 01:34:58 -0500514 defconfig_file = optarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200516 case randconfig:
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100517 {
518 struct timeval now;
519 unsigned int seed;
Yann E. MORIN0d8024c2013-04-13 22:49:13 +0200520 char *seed_env;
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100521
522 /*
523 * Use microseconds derived seed,
524 * compensate for systems where it may be zero
525 */
526 gettimeofday(&now, NULL);
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100527 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
Yann E. MORIN0d8024c2013-04-13 22:49:13 +0200528
529 seed_env = getenv("KCONFIG_SEED");
530 if( seed_env && *seed_env ) {
531 char *endp;
Yann E. MORINe85ac122013-05-20 23:17:34 +0200532 int tmp = (int)strtol(seed_env, &endp, 0);
Yann E. MORIN0d8024c2013-04-13 22:49:13 +0200533 if (*endp == '\0') {
534 seed = tmp;
535 }
536 }
Yann E. MORINa5f6d792013-05-20 23:09:03 +0200537 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100538 srand(seed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 break;
Ingo Molnarb0fe5512009-03-12 15:15:31 +0100540 }
Arnaud Lacombe32543992010-11-02 00:26:33 -0400541 case oldaskconfig:
542 case oldconfig:
543 case allnoconfig:
544 case allyesconfig:
545 case allmodconfig:
546 case alldefconfig:
547 case listnewconfig:
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500548 case helpnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800549 case olddefconfig:
Tetsuo Handa89b90602019-12-17 18:42:06 +0900550 case yes2modconfig:
551 case mod2yesconfig:
Arnaud Lacombe32543992010-11-02 00:26:33 -0400552 break;
Masahiro Yamadaa2af62c2021-02-21 22:03:16 +0900553 case 'h':
Arnaud Lacombe32543992010-11-02 00:26:33 -0400554 conf_usage(progname);
Andres Salomon2f4b4892007-12-17 01:34:58 -0500555 exit(1);
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200556 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 }
Andres Salomon2f4b4892007-12-17 01:34:58 -0500559 if (ac == optind) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200560 fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
Arnaud Lacombe32543992010-11-02 00:26:33 -0400561 conf_usage(progname);
Randy Dunlap250725a2006-06-08 22:12:50 -0700562 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
Andres Salomon2f4b4892007-12-17 01:34:58 -0500564 name = av[optind];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 conf_parse(name);
566 //zconfdump(stdout);
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200569 case defconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (conf_read(defconfig_file)) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900571 fprintf(stderr,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200572 "***\n"
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900573 "*** Can't find default configuration \"%s\"!\n"
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200574 "***\n",
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900575 defconfig_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 exit(1);
577 }
578 break;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200579 case savedefconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900580 case syncconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200581 case oldaskconfig:
582 case oldconfig:
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200583 case listnewconfig:
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500584 case helpnewconfig:
Adam Leefb16d892012-09-01 01:05:17 +0800585 case olddefconfig:
Tetsuo Handa89b90602019-12-17 18:42:06 +0900586 case yes2modconfig:
587 case mod2yesconfig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 conf_read(NULL);
589 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200590 case allnoconfig:
591 case allyesconfig:
592 case allmodconfig:
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200593 case alldefconfig:
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200594 case randconfig:
Roman Zippel90389162005-11-08 21:34:49 -0800595 name = getenv("KCONFIG_ALLCONFIG");
Eric W. Biederman9f420bf2012-05-07 05:37:45 -0700596 if (!name)
597 break;
598 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700599 if (conf_read_simple(name, S_DEF_USER)) {
600 fprintf(stderr,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200601 "*** Can't read seed configuration \"%s\"!\n",
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700602 name);
603 exit(1);
604 }
Roman Zippel90389162005-11-08 21:34:49 -0800605 break;
606 }
607 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200608 case allnoconfig: name = "allno.config"; break;
609 case allyesconfig: name = "allyes.config"; break;
610 case allmodconfig: name = "allmod.config"; break;
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200611 case alldefconfig: name = "alldef.config"; break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200612 case randconfig: name = "allrandom.config"; break;
Roman Zippel90389162005-11-08 21:34:49 -0800613 default: break;
614 }
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700615 if (conf_read_simple(name, S_DEF_USER) &&
616 conf_read_simple("all.config", S_DEF_USER)) {
617 fprintf(stderr,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200618 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
Eric W. Biederman5efe2412012-04-26 01:51:32 -0700619 name);
620 exit(1);
621 }
Roman Zippel90389162005-11-08 21:34:49 -0800622 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 default:
624 break;
625 }
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200626
627 if (sync_kconfig) {
Masahiro Yamada16952b72018-07-20 16:46:30 +0900628 name = getenv("KCONFIG_NOSILENTUPDATE");
629 if (name && *name) {
630 if (conf_get_changed()) {
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200631 fprintf(stderr,
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200632 "\n*** The configuration requires explicit update.\n\n");
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200633 return 1;
634 }
Masahiro Yamada16952b72018-07-20 16:46:30 +0900635 no_conf_write = 1;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200636 }
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200637 }
638
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200639 switch (input_mode) {
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200640 case allnoconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200641 conf_set_all_new_symbols(def_no);
642 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200643 case allyesconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200644 conf_set_all_new_symbols(def_yes);
645 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200646 case allmodconfig:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200647 conf_set_all_new_symbols(def_mod);
648 break;
Sam Ravnborg0748cb32010-07-31 23:35:31 +0200649 case alldefconfig:
650 conf_set_all_new_symbols(def_default);
651 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200652 case randconfig:
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +0200653 /* Really nothing to do in this loop */
654 while (conf_set_all_new_symbols(def_random)) ;
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200655 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200656 case defconfig:
Sam Ravnborg09748e12008-06-30 23:02:59 +0200657 conf_set_all_new_symbols(def_default);
658 break;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200659 case savedefconfig:
660 break;
Tetsuo Handa89b90602019-12-17 18:42:06 +0900661 case yes2modconfig:
662 conf_rewrite_mod_or_yes(def_y2m);
663 break;
664 case mod2yesconfig:
665 conf_rewrite_mod_or_yes(def_m2y);
666 break;
Sam Ravnborg4062f1a2010-07-31 23:35:26 +0200667 case oldaskconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200668 rootEntry = &rootmenu;
669 conf(&rootmenu);
Masahiro Yamada2aad9b82018-02-28 09:15:24 +0900670 input_mode = oldconfig;
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200671 /* fall through */
Sam Ravnborg14828342010-08-06 07:13:54 +0200672 case oldconfig:
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200673 case listnewconfig:
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500674 case helpnewconfig:
Masahiro Yamada911a91c2018-03-01 15:34:37 +0900675 case syncconfig:
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200676 /* Update until a loop caused no more changes */
677 do {
678 conf_cnt = 0;
679 check_conf(&rootmenu);
Masahiro Yamada99f0b652018-02-28 09:15:23 +0900680 } while (conf_cnt);
Masahiro Yamada59a80b52018-02-28 09:15:21 +0900681 break;
682 case olddefconfig:
683 default:
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200684 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
Sam Ravnborgf443d2e2008-06-30 22:45:38 +0200686
Masahiro Yamada00c864f2018-07-20 16:46:31 +0900687 if (input_mode == savedefconfig) {
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200688 if (conf_write_defconfig(defconfig_file)) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200689 fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900690 defconfig_file);
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200691 return 1;
692 }
Laura Abbott5d8b42a2019-11-04 17:10:08 -0500693 } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
Masahiro Yamada00c864f2018-07-20 16:46:31 +0900694 if (!no_conf_write && conf_write(NULL)) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200695 fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
zippel@linux-m68k.org204c96f2008-09-29 05:27:10 +0200696 exit(1);
697 }
Masahiro Yamada00c864f2018-07-20 16:46:31 +0900698
699 /*
700 * Create auto.conf if it does not exist.
701 * This prevents GNU Make 4.1 or older from emitting
702 * "include/config/auto.conf: No such file or directory"
703 * in the top-level Makefile
704 *
705 * syncconfig always creates or updates auto.conf because it is
706 * used during the build.
707 */
708 if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
709 fprintf(stderr,
710 "\n*** Error during sync of the configuration.\n\n");
711 return 1;
712 }
Roman Zippelc955cca2006-06-08 22:12:39 -0700713 }
Sam Ravnborg861b4ea2010-07-31 23:35:28 +0200714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}