kconfig/conf.c
changeset 1 eeea35fbf182
child 39 af42eec9d383
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/kconfig/conf.c	Sat Feb 24 11:00:05 2007 +0000
     1.3 @@ -0,0 +1,623 @@
     1.4 +/*
     1.5 + * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
     1.6 + * Released under the terms of the GNU GPL v2.0.
     1.7 + */
     1.8 +
     1.9 +#include <ctype.h>
    1.10 +#include <stdlib.h>
    1.11 +#include <stdio.h>
    1.12 +#include <string.h>
    1.13 +#include <unistd.h>
    1.14 +#include <time.h>
    1.15 +#include <sys/stat.h>
    1.16 +
    1.17 +#define LKC_DIRECT_LINK
    1.18 +#include "lkc.h"
    1.19 +
    1.20 +static void conf(struct menu *menu);
    1.21 +static void check_conf(struct menu *menu);
    1.22 +
    1.23 +enum {
    1.24 +	ask_all,
    1.25 +	ask_new,
    1.26 +	ask_silent,
    1.27 +	set_default,
    1.28 +	set_yes,
    1.29 +	set_mod,
    1.30 +	set_no,
    1.31 +	set_random
    1.32 +} input_mode = ask_all;
    1.33 +char *defconfig_file;
    1.34 +
    1.35 +static int indent = 1;
    1.36 +static int valid_stdin = 1;
    1.37 +static int conf_cnt;
    1.38 +static char line[128];
    1.39 +static struct menu *rootEntry;
    1.40 +
    1.41 +static char nohelp_text[] = N_("Sorry, no help available for this option yet.\n");
    1.42 +
    1.43 +static void strip(char *str)
    1.44 +{
    1.45 +	char *p = str;
    1.46 +	int l;
    1.47 +
    1.48 +	while ((isspace(*p)))
    1.49 +		p++;
    1.50 +	l = strlen(p);
    1.51 +	if (p != str)
    1.52 +		memmove(str, p, l + 1);
    1.53 +	if (!l)
    1.54 +		return;
    1.55 +	p = str + l - 1;
    1.56 +	while ((isspace(*p)))
    1.57 +		*p-- = 0;
    1.58 +}
    1.59 +
    1.60 +static void check_stdin(void)
    1.61 +{
    1.62 +	if (!valid_stdin && input_mode == ask_silent) {
    1.63 +		printf(_("aborted!\n\n"));
    1.64 +		printf(_("Console input/output is redirected. "));
    1.65 +		printf(_("Run 'make oldconfig' to update configuration.\n\n"));
    1.66 +		exit(1);
    1.67 +	}
    1.68 +}
    1.69 +
    1.70 +static void conf_askvalue(struct symbol *sym, const char *def)
    1.71 +{
    1.72 +	enum symbol_type type = sym_get_type(sym);
    1.73 +	tristate val;
    1.74 +
    1.75 +	if (!sym_has_value(sym))
    1.76 +		printf("(NEW) ");
    1.77 +
    1.78 +	line[0] = '\n';
    1.79 +	line[1] = 0;
    1.80 +
    1.81 +	if (!sym_is_changable(sym)) {
    1.82 +		printf("%s\n", def);
    1.83 +		line[0] = '\n';
    1.84 +		line[1] = 0;
    1.85 +		return;
    1.86 +	}
    1.87 +
    1.88 +	switch (input_mode) {
    1.89 +	case set_no:
    1.90 +	case set_mod:
    1.91 +	case set_yes:
    1.92 +	case set_random:
    1.93 +		if (sym_has_value(sym)) {
    1.94 +			printf("%s\n", def);
    1.95 +			return;
    1.96 +		}
    1.97 +		break;
    1.98 +	case ask_new:
    1.99 +	case ask_silent:
   1.100 +		if (sym_has_value(sym)) {
   1.101 +			printf("%s\n", def);
   1.102 +			return;
   1.103 +		}
   1.104 +		check_stdin();
   1.105 +	case ask_all:
   1.106 +		fflush(stdout);
   1.107 +		fgets(line, 128, stdin);
   1.108 +		return;
   1.109 +	case set_default:
   1.110 +		printf("%s\n", def);
   1.111 +		return;
   1.112 +	default:
   1.113 +		break;
   1.114 +	}
   1.115 +
   1.116 +	switch (type) {
   1.117 +	case S_INT:
   1.118 +	case S_HEX:
   1.119 +	case S_STRING:
   1.120 +		printf("%s\n", def);
   1.121 +		return;
   1.122 +	default:
   1.123 +		;
   1.124 +	}
   1.125 +	switch (input_mode) {
   1.126 +	case set_yes:
   1.127 +		if (sym_tristate_within_range(sym, yes)) {
   1.128 +			line[0] = 'y';
   1.129 +			line[1] = '\n';
   1.130 +			line[2] = 0;
   1.131 +			break;
   1.132 +		}
   1.133 +	case set_mod:
   1.134 +		if (type == S_TRISTATE) {
   1.135 +			if (sym_tristate_within_range(sym, mod)) {
   1.136 +				line[0] = 'm';
   1.137 +				line[1] = '\n';
   1.138 +				line[2] = 0;
   1.139 +				break;
   1.140 +			}
   1.141 +		} else {
   1.142 +			if (sym_tristate_within_range(sym, yes)) {
   1.143 +				line[0] = 'y';
   1.144 +				line[1] = '\n';
   1.145 +				line[2] = 0;
   1.146 +				break;
   1.147 +			}
   1.148 +		}
   1.149 +	case set_no:
   1.150 +		if (sym_tristate_within_range(sym, no)) {
   1.151 +			line[0] = 'n';
   1.152 +			line[1] = '\n';
   1.153 +			line[2] = 0;
   1.154 +			break;
   1.155 +		}
   1.156 +	case set_random:
   1.157 +		do {
   1.158 +			val = (tristate)(random() % 3);
   1.159 +		} while (!sym_tristate_within_range(sym, val));
   1.160 +		switch (val) {
   1.161 +		case no: line[0] = 'n'; break;
   1.162 +		case mod: line[0] = 'm'; break;
   1.163 +		case yes: line[0] = 'y'; break;
   1.164 +		}
   1.165 +		line[1] = '\n';
   1.166 +		line[2] = 0;
   1.167 +		break;
   1.168 +	default:
   1.169 +		break;
   1.170 +	}
   1.171 +	printf("%s", line);
   1.172 +}
   1.173 +
   1.174 +int conf_string(struct menu *menu)
   1.175 +{
   1.176 +	struct symbol *sym = menu->sym;
   1.177 +	const char *def, *help;
   1.178 +
   1.179 +	while (1) {
   1.180 +		printf("%*s%s ", indent - 1, "", menu->prompt->text);
   1.181 +		printf("(%s) ", sym->name);
   1.182 +		def = sym_get_string_value(sym);
   1.183 +		if (sym_get_string_value(sym))
   1.184 +			printf("[%s] ", def);
   1.185 +		conf_askvalue(sym, def);
   1.186 +		switch (line[0]) {
   1.187 +		case '\n':
   1.188 +			break;
   1.189 +		case '?':
   1.190 +			/* print help */
   1.191 +			if (line[1] == '\n') {
   1.192 +				help = nohelp_text;
   1.193 +				if (menu->sym->help)
   1.194 +					help = menu->sym->help;
   1.195 +				printf("\n%s\n", menu->sym->help);
   1.196 +				def = NULL;
   1.197 +				break;
   1.198 +			}
   1.199 +		default:
   1.200 +			line[strlen(line)-1] = 0;
   1.201 +			def = line;
   1.202 +		}
   1.203 +		if (def && sym_set_string_value(sym, def))
   1.204 +			return 0;
   1.205 +	}
   1.206 +}
   1.207 +
   1.208 +static int conf_sym(struct menu *menu)
   1.209 +{
   1.210 +	struct symbol *sym = menu->sym;
   1.211 +	int type;
   1.212 +	tristate oldval, newval;
   1.213 +	const char *help;
   1.214 +
   1.215 +	while (1) {
   1.216 +		printf("%*s%s ", indent - 1, "", menu->prompt->text);
   1.217 +		if (sym->name)
   1.218 +			printf("(%s) ", sym->name);
   1.219 +		type = sym_get_type(sym);
   1.220 +		putchar('[');
   1.221 +		oldval = sym_get_tristate_value(sym);
   1.222 +		switch (oldval) {
   1.223 +		case no:
   1.224 +			putchar('N');
   1.225 +			break;
   1.226 +		case mod:
   1.227 +			putchar('M');
   1.228 +			break;
   1.229 +		case yes:
   1.230 +			putchar('Y');
   1.231 +			break;
   1.232 +		}
   1.233 +		if (oldval != no && sym_tristate_within_range(sym, no))
   1.234 +			printf("/n");
   1.235 +		if (oldval != mod && sym_tristate_within_range(sym, mod))
   1.236 +			printf("/m");
   1.237 +		if (oldval != yes && sym_tristate_within_range(sym, yes))
   1.238 +			printf("/y");
   1.239 +		if (sym->help)
   1.240 +			printf("/?");
   1.241 +		printf("] ");
   1.242 +		conf_askvalue(sym, sym_get_string_value(sym));
   1.243 +		strip(line);
   1.244 +
   1.245 +		switch (line[0]) {
   1.246 +		case 'n':
   1.247 +		case 'N':
   1.248 +			newval = no;
   1.249 +			if (!line[1] || !strcmp(&line[1], "o"))
   1.250 +				break;
   1.251 +			continue;
   1.252 +		case 'm':
   1.253 +		case 'M':
   1.254 +			newval = mod;
   1.255 +			if (!line[1])
   1.256 +				break;
   1.257 +			continue;
   1.258 +		case 'y':
   1.259 +		case 'Y':
   1.260 +			newval = yes;
   1.261 +			if (!line[1] || !strcmp(&line[1], "es"))
   1.262 +				break;
   1.263 +			continue;
   1.264 +		case 0:
   1.265 +			newval = oldval;
   1.266 +			break;
   1.267 +		case '?':
   1.268 +			goto help;
   1.269 +		default:
   1.270 +			continue;
   1.271 +		}
   1.272 +		if (sym_set_tristate_value(sym, newval))
   1.273 +			return 0;
   1.274 +help:
   1.275 +		help = nohelp_text;
   1.276 +		if (sym->help)
   1.277 +			help = sym->help;
   1.278 +		printf("\n%s\n", help);
   1.279 +	}
   1.280 +}
   1.281 +
   1.282 +static int conf_choice(struct menu *menu)
   1.283 +{
   1.284 +	struct symbol *sym, *def_sym;
   1.285 +	struct menu *child;
   1.286 +	int type;
   1.287 +	bool is_new;
   1.288 +
   1.289 +	sym = menu->sym;
   1.290 +	type = sym_get_type(sym);
   1.291 +	is_new = !sym_has_value(sym);
   1.292 +	if (sym_is_changable(sym)) {
   1.293 +		conf_sym(menu);
   1.294 +		sym_calc_value(sym);
   1.295 +		switch (sym_get_tristate_value(sym)) {
   1.296 +		case no:
   1.297 +			return 1;
   1.298 +		case mod:
   1.299 +			return 0;
   1.300 +		case yes:
   1.301 +			break;
   1.302 +		}
   1.303 +	} else {
   1.304 +		switch (sym_get_tristate_value(sym)) {
   1.305 +		case no:
   1.306 +			return 1;
   1.307 +		case mod:
   1.308 +			printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
   1.309 +			return 0;
   1.310 +		case yes:
   1.311 +			break;
   1.312 +		}
   1.313 +	}
   1.314 +
   1.315 +	while (1) {
   1.316 +		int cnt, def;
   1.317 +
   1.318 +		printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
   1.319 +		def_sym = sym_get_choice_value(sym);
   1.320 +		cnt = def = 0;
   1.321 +		line[0] = 0;
   1.322 +		for (child = menu->list; child; child = child->next) {
   1.323 +			if (!menu_is_visible(child))
   1.324 +				continue;
   1.325 +			if (!child->sym) {
   1.326 +				printf("%*c %s\n", indent, '*', menu_get_prompt(child));
   1.327 +				continue;
   1.328 +			}
   1.329 +			cnt++;
   1.330 +			if (child->sym == def_sym) {
   1.331 +				def = cnt;
   1.332 +				printf("%*c", indent, '>');
   1.333 +			} else
   1.334 +				printf("%*c", indent, ' ');
   1.335 +			printf(" %d. %s", cnt, menu_get_prompt(child));
   1.336 +			if (child->sym->name)
   1.337 +				printf(" (%s)", child->sym->name);
   1.338 +			if (!sym_has_value(child->sym))
   1.339 +				printf(" (NEW)");
   1.340 +			printf("\n");
   1.341 +		}
   1.342 +		printf("%*schoice", indent - 1, "");
   1.343 +		if (cnt == 1) {
   1.344 +			printf("[1]: 1\n");
   1.345 +			goto conf_childs;
   1.346 +		}
   1.347 +		printf("[1-%d", cnt);
   1.348 +		if (sym->help)
   1.349 +			printf("?");
   1.350 +		printf("]: ");
   1.351 +		switch (input_mode) {
   1.352 +		case ask_new:
   1.353 +		case ask_silent:
   1.354 +			if (!is_new) {
   1.355 +				cnt = def;
   1.356 +				printf("%d\n", cnt);
   1.357 +				break;
   1.358 +			}
   1.359 +			check_stdin();
   1.360 +		case ask_all:
   1.361 +			fflush(stdout);
   1.362 +			fgets(line, 128, stdin);
   1.363 +			strip(line);
   1.364 +			if (line[0] == '?') {
   1.365 +				printf("\n%s\n", menu->sym->help ?
   1.366 +					menu->sym->help : nohelp_text);
   1.367 +				continue;
   1.368 +			}
   1.369 +			if (!line[0])
   1.370 +				cnt = def;
   1.371 +			else if (isdigit(line[0]))
   1.372 +				cnt = atoi(line);
   1.373 +			else
   1.374 +				continue;
   1.375 +			break;
   1.376 +		case set_random:
   1.377 +			def = (random() % cnt) + 1;
   1.378 +		case set_default:
   1.379 +		case set_yes:
   1.380 +		case set_mod:
   1.381 +		case set_no:
   1.382 +			cnt = def;
   1.383 +			printf("%d\n", cnt);
   1.384 +			break;
   1.385 +		}
   1.386 +
   1.387 +	conf_childs:
   1.388 +		for (child = menu->list; child; child = child->next) {
   1.389 +			if (!child->sym || !menu_is_visible(child))
   1.390 +				continue;
   1.391 +			if (!--cnt)
   1.392 +				break;
   1.393 +		}
   1.394 +		if (!child)
   1.395 +			continue;
   1.396 +		if (line[strlen(line) - 1] == '?') {
   1.397 +			printf("\n%s\n", child->sym->help ?
   1.398 +				child->sym->help : nohelp_text);
   1.399 +			continue;
   1.400 +		}
   1.401 +		sym_set_choice_value(sym, child->sym);
   1.402 +		if (child->list) {
   1.403 +			indent += 2;
   1.404 +			conf(child->list);
   1.405 +			indent -= 2;
   1.406 +		}
   1.407 +		return 1;
   1.408 +	}
   1.409 +}
   1.410 +
   1.411 +static void conf(struct menu *menu)
   1.412 +{
   1.413 +	struct symbol *sym;
   1.414 +	struct property *prop;
   1.415 +	struct menu *child;
   1.416 +
   1.417 +	if (!menu_is_visible(menu))
   1.418 +		return;
   1.419 +
   1.420 +	sym = menu->sym;
   1.421 +	prop = menu->prompt;
   1.422 +	if (prop) {
   1.423 +		const char *prompt;
   1.424 +
   1.425 +		switch (prop->type) {
   1.426 +		case P_MENU:
   1.427 +			if (input_mode == ask_silent && rootEntry != menu) {
   1.428 +				check_conf(menu);
   1.429 +				return;
   1.430 +			}
   1.431 +		case P_COMMENT:
   1.432 +			prompt = menu_get_prompt(menu);
   1.433 +			if (prompt)
   1.434 +				printf("%*c\n%*c %s\n%*c\n",
   1.435 +					indent, '*',
   1.436 +					indent, '*', prompt,
   1.437 +					indent, '*');
   1.438 +		default:
   1.439 +			;
   1.440 +		}
   1.441 +	}
   1.442 +
   1.443 +	if (!sym)
   1.444 +		goto conf_childs;
   1.445 +
   1.446 +	if (sym_is_choice(sym)) {
   1.447 +		conf_choice(menu);
   1.448 +		if (sym->curr.tri != mod)
   1.449 +			return;
   1.450 +		goto conf_childs;
   1.451 +	}
   1.452 +
   1.453 +	switch (sym->type) {
   1.454 +	case S_INT:
   1.455 +	case S_HEX:
   1.456 +	case S_STRING:
   1.457 +		conf_string(menu);
   1.458 +		break;
   1.459 +	default:
   1.460 +		conf_sym(menu);
   1.461 +		break;
   1.462 +	}
   1.463 +
   1.464 +conf_childs:
   1.465 +	if (sym)
   1.466 +		indent += 2;
   1.467 +	for (child = menu->list; child; child = child->next)
   1.468 +		conf(child);
   1.469 +	if (sym)
   1.470 +		indent -= 2;
   1.471 +}
   1.472 +
   1.473 +static void check_conf(struct menu *menu)
   1.474 +{
   1.475 +	struct symbol *sym;
   1.476 +	struct menu *child;
   1.477 +
   1.478 +	if (!menu_is_visible(menu))
   1.479 +		return;
   1.480 +
   1.481 +	sym = menu->sym;
   1.482 +	if (sym && !sym_has_value(sym)) {
   1.483 +		if (sym_is_changable(sym) ||
   1.484 +		    (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
   1.485 +			if (!conf_cnt++)
   1.486 +				printf(_("*\n* Restart config...\n*\n"));
   1.487 +			rootEntry = menu_get_parent_menu(menu);
   1.488 +			conf(rootEntry);
   1.489 +		}
   1.490 +	}
   1.491 +
   1.492 +	for (child = menu->list; child; child = child->next)
   1.493 +		check_conf(child);
   1.494 +}
   1.495 +
   1.496 +int main(int ac, char **av)
   1.497 +{
   1.498 +	int i = 1;
   1.499 +	const char *name;
   1.500 +	struct stat tmpstat;
   1.501 +
   1.502 +	if (ac > i && av[i][0] == '-') {
   1.503 +		switch (av[i++][1]) {
   1.504 +		case 'o':
   1.505 +			input_mode = ask_new;
   1.506 +			break;
   1.507 +		case 's':
   1.508 +			input_mode = ask_silent;
   1.509 +			valid_stdin = isatty(0) && isatty(1) && isatty(2);
   1.510 +			break;
   1.511 +		case 'd':
   1.512 +			input_mode = set_default;
   1.513 +			break;
   1.514 +		case 'D':
   1.515 +			input_mode = set_default;
   1.516 +			defconfig_file = av[i++];
   1.517 +			if (!defconfig_file) {
   1.518 +				printf(_("%s: No default config file specified\n"),
   1.519 +					av[0]);
   1.520 +				exit(1);
   1.521 +			}
   1.522 +			break;
   1.523 +		case 'n':
   1.524 +			input_mode = set_no;
   1.525 +			break;
   1.526 +		case 'm':
   1.527 +			input_mode = set_mod;
   1.528 +			break;
   1.529 +		case 'y':
   1.530 +			input_mode = set_yes;
   1.531 +			break;
   1.532 +		case 'r':
   1.533 +			input_mode = set_random;
   1.534 +			srandom(time(NULL));
   1.535 +			break;
   1.536 +		case 'h':
   1.537 +		case '?':
   1.538 +			fprintf(stderr, "See README for usage info\n");
   1.539 +			exit(0);
   1.540 +		}
   1.541 +	}
   1.542 +  	name = av[i];
   1.543 +	if (!name) {
   1.544 +		printf(_("%s: Kconfig file missing\n"), av[0]);
   1.545 +		exit(1);
   1.546 +	}
   1.547 +	conf_parse(name);
   1.548 +	//zconfdump(stdout);
   1.549 +	switch (input_mode) {
   1.550 +	case set_default:
   1.551 +		if (!defconfig_file)
   1.552 +			defconfig_file = conf_get_default_confname();
   1.553 +		if (conf_read(defconfig_file)) {
   1.554 +			printf("***\n"
   1.555 +				"*** Can't find default configuration \"%s\"!\n"
   1.556 +				"***\n", defconfig_file);
   1.557 +			exit(1);
   1.558 +		}
   1.559 +		break;
   1.560 +	case ask_silent:
   1.561 +		if (stat(".config", &tmpstat)) {
   1.562 +			printf(_("***\n"
   1.563 +				"*** You have not yet configured your "PROJECT_NAME"!\n"
   1.564 +				"***\n"
   1.565 +				"*** Please run some configurator (e.g. \"make oldconfig\" or\n"
   1.566 +				"*** \"make menuconfig\" or \"make xconfig\").\n"
   1.567 +				"***\n"));
   1.568 +			exit(1);
   1.569 +		}
   1.570 +	case ask_all:
   1.571 +	case ask_new:
   1.572 +		conf_read(NULL);
   1.573 +		break;
   1.574 +	case set_no:
   1.575 +	case set_mod:
   1.576 +	case set_yes:
   1.577 +	case set_random:
   1.578 +		name = getenv("KCONFIG_ALLCONFIG");
   1.579 +		if (name && !stat(name, &tmpstat)) {
   1.580 +			conf_read_simple(name, S_DEF_USER);
   1.581 +			break;
   1.582 +		}
   1.583 +		switch (input_mode) {
   1.584 +		case set_no:	 name = "allno.config"; break;
   1.585 +		case set_mod:	 name = "allmod.config"; break;
   1.586 +		case set_yes:	 name = "allyes.config"; break;
   1.587 +		case set_random: name = "allrandom.config"; break;
   1.588 +		default: break;
   1.589 +		}
   1.590 +		if (!stat(name, &tmpstat))
   1.591 +			conf_read_simple(name, S_DEF_USER);
   1.592 +		else if (!stat("all.config", &tmpstat))
   1.593 +			conf_read_simple("all.config", S_DEF_USER);
   1.594 +		break;
   1.595 +	default:
   1.596 +		break;
   1.597 +	}
   1.598 +
   1.599 +	if (input_mode != ask_silent) {
   1.600 +		rootEntry = &rootmenu;
   1.601 +		conf(&rootmenu);
   1.602 +		if (input_mode == ask_all) {
   1.603 +			input_mode = ask_silent;
   1.604 +			valid_stdin = 1;
   1.605 +		}
   1.606 +	} else if (sym_change_count) {
   1.607 +		name = getenv("KCONFIG_NOSILENTUPDATE");
   1.608 +		if (name && *name) {
   1.609 +			fprintf(stderr, _("\n*** "PROJECT_NAME" configuration requires explicit update.\n\n"));
   1.610 +			return 1;
   1.611 +		}
   1.612 +	} else
   1.613 +		goto skip_check;
   1.614 +
   1.615 +	do {
   1.616 +		conf_cnt = 0;
   1.617 +		check_conf(&rootmenu);
   1.618 +	} while (conf_cnt);
   1.619 +
   1.620 +	if (!conf_write(NULL)) {
   1.621 +skip_check:
   1.622 +		return 0;
   1.623 +	}
   1.624 +	fprintf(stderr, _("\n*** Error writing "PROJECT_NAME" configuration.\n\n"));
   1.625 +	return 1;
   1.626 +}