kconfig/conf.c
changeset 2448 a103abae1560
parent 1524 1031ea5af395
child 2452 ff170381d13a
     1.1 --- a/kconfig/conf.c	Tue Sep 08 22:42:48 2009 +0200
     1.2 +++ b/kconfig/conf.c	Sun May 08 14:14:40 2011 +0200
     1.3 @@ -10,7 +10,9 @@
     1.4  #include <string.h>
     1.5  #include <time.h>
     1.6  #include <unistd.h>
     1.7 +#include <getopt.h>
     1.8  #include <sys/stat.h>
     1.9 +#include <sys/time.h>
    1.10  
    1.11  #define LKC_DIRECT_LINK
    1.12  #include "lkc.h"
    1.13 @@ -18,16 +20,21 @@
    1.14  static void conf(struct menu *menu);
    1.15  static void check_conf(struct menu *menu);
    1.16  
    1.17 -enum {
    1.18 -	ask_all,
    1.19 -	ask_new,
    1.20 -	ask_silent,
    1.21 -	set_default,
    1.22 -	set_yes,
    1.23 -	set_mod,
    1.24 -	set_no,
    1.25 -	set_random
    1.26 -} input_mode = ask_all;
    1.27 +enum input_mode {
    1.28 +	oldaskconfig,
    1.29 +	silentoldconfig,
    1.30 +	oldconfig,
    1.31 +	allnoconfig,
    1.32 +	allyesconfig,
    1.33 +	allmodconfig,
    1.34 +	alldefconfig,
    1.35 +	randconfig,
    1.36 +	defconfig,
    1.37 +	savedefconfig,
    1.38 +	listnewconfig,
    1.39 +	oldnoconfig,
    1.40 +} input_mode = oldaskconfig;
    1.41 +
    1.42  char *defconfig_file;
    1.43  
    1.44  static int indent = 1;
    1.45 @@ -37,14 +44,14 @@
    1.46  static char line[128];
    1.47  static struct menu *rootEntry;
    1.48  
    1.49 -static char nohelp_text[] = N_("Sorry, no help available for this option yet.\n");
    1.50 +static void print_help(struct menu *menu)
    1.51 +{
    1.52 +	struct gstr help = str_new();
    1.53  
    1.54 -static const char *get_help(struct menu *menu)
    1.55 -{
    1.56 -	if (menu_has_help(menu))
    1.57 -		return _(menu_get_help(menu));
    1.58 -	else
    1.59 -		return nohelp_text;
    1.60 +	menu_get_ext_help(menu, &help);
    1.61 +
    1.62 +	printf("\n%s\n", str_get(&help));
    1.63 +	str_free(&help);
    1.64  }
    1.65  
    1.66  static void strip(char *str)
    1.67 @@ -92,16 +99,16 @@
    1.68  	}
    1.69  
    1.70  	switch (input_mode) {
    1.71 -	case ask_new:
    1.72 -	case ask_silent:
    1.73 +	case oldconfig:
    1.74 +	case silentoldconfig:
    1.75  		if (sym_has_value(sym)) {
    1.76  			printf("%s\n", def);
    1.77  			return 0;
    1.78  		}
    1.79  		check_stdin();
    1.80 -	case ask_all:
    1.81 +	case oldaskconfig:
    1.82  		fflush(stdout);
    1.83 -		fgets(line, 128, stdin);
    1.84 +		xfgets(line, 128, stdin);
    1.85  		return 1;
    1.86  	default:
    1.87  		break;
    1.88 @@ -120,7 +127,7 @@
    1.89  	return 1;
    1.90  }
    1.91  
    1.92 -int conf_string(struct menu *menu)
    1.93 +static int conf_string(struct menu *menu)
    1.94  {
    1.95  	struct symbol *sym = menu->sym;
    1.96  	const char *def;
    1.97 @@ -139,7 +146,7 @@
    1.98  		case '?':
    1.99  			/* print help */
   1.100  			if (line[1] == '\n') {
   1.101 -				printf("\n%s\n", get_help(menu));
   1.102 +				print_help(menu);
   1.103  				def = NULL;
   1.104  				break;
   1.105  			}
   1.106 @@ -155,14 +162,12 @@
   1.107  static int conf_sym(struct menu *menu)
   1.108  {
   1.109  	struct symbol *sym = menu->sym;
   1.110 -	int type;
   1.111  	tristate oldval, newval;
   1.112  
   1.113  	while (1) {
   1.114  		printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
   1.115  		if (sym->name)
   1.116  			printf("(%s) ", sym->name);
   1.117 -		type = sym_get_type(sym);
   1.118  		putchar('[');
   1.119  		oldval = sym_get_tristate_value(sym);
   1.120  		switch (oldval) {
   1.121 @@ -219,7 +224,7 @@
   1.122  		if (sym_set_tristate_value(sym, newval))
   1.123  			return 0;
   1.124  help:
   1.125 -		printf("\n%s\n", get_help(menu));
   1.126 +		print_help(menu);
   1.127  	}
   1.128  }
   1.129  
   1.130 @@ -227,11 +232,9 @@
   1.131  {
   1.132  	struct symbol *sym, *def_sym;
   1.133  	struct menu *child;
   1.134 -	int type;
   1.135  	bool is_new;
   1.136  
   1.137  	sym = menu->sym;
   1.138 -	type = sym_get_type(sym);
   1.139  	is_new = !sym_has_value(sym);
   1.140  	if (sym_is_changable(sym)) {
   1.141  		conf_sym(menu);
   1.142 @@ -293,20 +296,20 @@
   1.143  			printf("?");
   1.144  		printf("]: ");
   1.145  		switch (input_mode) {
   1.146 -		case ask_new:
   1.147 -		case ask_silent:
   1.148 +		case oldconfig:
   1.149 +		case silentoldconfig:
   1.150  			if (!is_new) {
   1.151  				cnt = def;
   1.152  				printf("%d\n", cnt);
   1.153  				break;
   1.154  			}
   1.155  			check_stdin();
   1.156 -		case ask_all:
   1.157 +		case oldaskconfig:
   1.158  			fflush(stdout);
   1.159 -			fgets(line, 128, stdin);
   1.160 +			xfgets(line, 128, stdin);
   1.161  			strip(line);
   1.162  			if (line[0] == '?') {
   1.163 -				printf("\n%s\n", get_help(menu));
   1.164 +				print_help(menu);
   1.165  				continue;
   1.166  			}
   1.167  			if (!line[0])
   1.168 @@ -329,8 +332,8 @@
   1.169  		}
   1.170  		if (!child)
   1.171  			continue;
   1.172 -		if (line[strlen(line) - 1] == '?') {
   1.173 -			printf("\n%s\n", get_help(child));
   1.174 +		if (line[0] && line[strlen(line) - 1] == '?') {
   1.175 +			print_help(child);
   1.176  			continue;
   1.177  		}
   1.178  		sym_set_choice_value(sym, child->sym);
   1.179 @@ -359,7 +362,10 @@
   1.180  
   1.181  		switch (prop->type) {
   1.182  		case P_MENU:
   1.183 -			if (input_mode == ask_silent && rootEntry != menu) {
   1.184 +			if ((input_mode == silentoldconfig ||
   1.185 +			     input_mode == listnewconfig ||
   1.186 +			     input_mode == oldnoconfig) &&
   1.187 +			    rootEntry != menu) {
   1.188  				check_conf(menu);
   1.189  				return;
   1.190  			}
   1.191 @@ -417,10 +423,16 @@
   1.192  	if (sym && !sym_has_value(sym)) {
   1.193  		if (sym_is_changable(sym) ||
   1.194  		    (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
   1.195 -			if (!conf_cnt++)
   1.196 -				printf(_("*\n* Restart config...\n*\n"));
   1.197 -			rootEntry = menu_get_parent_menu(menu);
   1.198 -			conf(rootEntry);
   1.199 +			if (input_mode == listnewconfig) {
   1.200 +				if (sym->name && !sym_is_choice_value(sym)) {
   1.201 +					printf("%s%s\n", CONFIG_, sym->name);
   1.202 +				}
   1.203 +			} else if (input_mode != oldnoconfig) {
   1.204 +				if (!conf_cnt++)
   1.205 +					printf(_("*\n* Restart config...\n*\n"));
   1.206 +				rootEntry = menu_get_parent_menu(menu);
   1.207 +				conf(rootEntry);
   1.208 +			}
   1.209  		}
   1.210  	}
   1.211  
   1.212 @@ -428,6 +440,22 @@
   1.213  		check_conf(child);
   1.214  }
   1.215  
   1.216 +static struct option long_opts[] = {
   1.217 +	{"oldaskconfig",    no_argument,       NULL, oldaskconfig},
   1.218 +	{"oldconfig",       no_argument,       NULL, oldconfig},
   1.219 +	{"silentoldconfig", no_argument,       NULL, silentoldconfig},
   1.220 +	{"defconfig",       optional_argument, NULL, defconfig},
   1.221 +	{"savedefconfig",   required_argument, NULL, savedefconfig},
   1.222 +	{"allnoconfig",     no_argument,       NULL, allnoconfig},
   1.223 +	{"allyesconfig",    no_argument,       NULL, allyesconfig},
   1.224 +	{"allmodconfig",    no_argument,       NULL, allmodconfig},
   1.225 +	{"alldefconfig",    no_argument,       NULL, alldefconfig},
   1.226 +	{"randconfig",      no_argument,       NULL, randconfig},
   1.227 +	{"listnewconfig",   no_argument,       NULL, listnewconfig},
   1.228 +	{"oldnoconfig",     no_argument,       NULL, oldnoconfig},
   1.229 +	{NULL, 0, NULL, 0}
   1.230 +};
   1.231 +
   1.232  int main(int ac, char **av)
   1.233  {
   1.234  	int opt;
   1.235 @@ -438,42 +466,35 @@
   1.236  	bindtextdomain(PACKAGE, LOCALEDIR);
   1.237  	textdomain(PACKAGE);
   1.238  
   1.239 -	while ((opt = getopt(ac, av, "osdD:nmyrh")) != -1) {
   1.240 +	while ((opt = getopt_long(ac, av, "", long_opts, NULL)) != -1) {
   1.241 +		input_mode = (enum input_mode)opt;
   1.242  		switch (opt) {
   1.243 -		case 'o':
   1.244 -			input_mode = ask_silent;
   1.245 -			break;
   1.246 -		case 's':
   1.247 -			input_mode = ask_silent;
   1.248 +		case silentoldconfig:
   1.249  			sync_kconfig = 1;
   1.250  			break;
   1.251 -		case 'd':
   1.252 -			input_mode = set_default;
   1.253 -			break;
   1.254 -		case 'D':
   1.255 -			input_mode = set_default;
   1.256 +		case defconfig:
   1.257 +		case savedefconfig:
   1.258  			defconfig_file = optarg;
   1.259  			break;
   1.260 -		case 'n':
   1.261 -			input_mode = set_no;
   1.262 +		case randconfig:
   1.263 +		{
   1.264 +			struct timeval now;
   1.265 +			unsigned int seed;
   1.266 +
   1.267 +			/*
   1.268 +			 * Use microseconds derived seed,
   1.269 +			 * compensate for systems where it may be zero
   1.270 +			 */
   1.271 +			gettimeofday(&now, NULL);
   1.272 +
   1.273 +			seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
   1.274 +			srand(seed);
   1.275  			break;
   1.276 -		case 'm':
   1.277 -			input_mode = set_mod;
   1.278 -			break;
   1.279 -		case 'y':
   1.280 -			input_mode = set_yes;
   1.281 -			break;
   1.282 -		case 'r':
   1.283 -			input_mode = set_random;
   1.284 -			srand(time(NULL));
   1.285 -			break;
   1.286 -		case 'h':
   1.287 -			printf(_("See README for usage info\n"));
   1.288 -			exit(0);
   1.289 -			break;
   1.290 -		default:
   1.291 +		}
   1.292 +		case '?':
   1.293  			fprintf(stderr, _("See README for usage info\n"));
   1.294  			exit(1);
   1.295 +			break;
   1.296  		}
   1.297  	}
   1.298  	if (ac == optind) {
   1.299 @@ -484,16 +505,20 @@
   1.300  	conf_parse(name);
   1.301  	//zconfdump(stdout);
   1.302  	if (sync_kconfig) {
   1.303 -		if (stat(".config", &tmpstat)) {
   1.304 +		name = conf_get_configname();
   1.305 +		if (stat(name, &tmpstat)) {
   1.306  			fprintf(stderr, _("***\n"
   1.307 -				"*** Please run some configurator (e.g. \"make menuconfig\").\n"
   1.308 -				"***\n"));
   1.309 +				"*** Configuration file \"%s\" not found!\n"
   1.310 +				"***\n"
   1.311 +				"*** Please run some configurator (e.g. \"make oldconfig\" or\n"
   1.312 +				"*** \"make menuconfig\" or \"make xconfig\").\n"
   1.313 +				"***\n"), name);
   1.314  			exit(1);
   1.315  		}
   1.316  	}
   1.317  
   1.318  	switch (input_mode) {
   1.319 -	case set_default:
   1.320 +	case defconfig:
   1.321  		if (!defconfig_file)
   1.322  			defconfig_file = conf_get_default_confname();
   1.323  		if (conf_read(defconfig_file)) {
   1.324 @@ -503,25 +528,30 @@
   1.325  			exit(1);
   1.326  		}
   1.327  		break;
   1.328 -	case ask_silent:
   1.329 -	case ask_all:
   1.330 -	case ask_new:
   1.331 +	case savedefconfig:
   1.332 +	case silentoldconfig:
   1.333 +	case oldaskconfig:
   1.334 +	case oldconfig:
   1.335 +	case listnewconfig:
   1.336 +	case oldnoconfig:
   1.337  		conf_read(NULL);
   1.338  		break;
   1.339 -	case set_no:
   1.340 -	case set_mod:
   1.341 -	case set_yes:
   1.342 -	case set_random:
   1.343 +	case allnoconfig:
   1.344 +	case allyesconfig:
   1.345 +	case allmodconfig:
   1.346 +	case alldefconfig:
   1.347 +	case randconfig:
   1.348  		name = getenv("KCONFIG_ALLCONFIG");
   1.349  		if (name && !stat(name, &tmpstat)) {
   1.350  			conf_read_simple(name, S_DEF_USER);
   1.351  			break;
   1.352  		}
   1.353  		switch (input_mode) {
   1.354 -		case set_no:	 name = "allno.config"; break;
   1.355 -		case set_mod:	 name = "allmod.config"; break;
   1.356 -		case set_yes:	 name = "allyes.config"; break;
   1.357 -		case set_random: name = "allrandom.config"; break;
   1.358 +		case allnoconfig:	name = "allno.config"; break;
   1.359 +		case allyesconfig:	name = "allyes.config"; break;
   1.360 +		case allmodconfig:	name = "allmod.config"; break;
   1.361 +		case alldefconfig:	name = "alldef.config"; break;
   1.362 +		case randconfig:	name = "allrandom.config"; break;
   1.363  		default: break;
   1.364  		}
   1.365  		if (!stat(name, &tmpstat))
   1.366 @@ -538,7 +568,7 @@
   1.367  			name = getenv("KCONFIG_NOSILENTUPDATE");
   1.368  			if (name && *name) {
   1.369  				fprintf(stderr,
   1.370 -					_("\n*** Configuration requires explicit update.\n\n"));
   1.371 +					_("\n*** The configuration requires explicit update.\n\n"));
   1.372  				return 1;
   1.373  			}
   1.374  		}
   1.375 @@ -546,33 +576,42 @@
   1.376  	}
   1.377  
   1.378  	switch (input_mode) {
   1.379 -	case set_no:
   1.380 +	case allnoconfig:
   1.381  		conf_set_all_new_symbols(def_no);
   1.382  		break;
   1.383 -	case set_yes:
   1.384 +	case allyesconfig:
   1.385  		conf_set_all_new_symbols(def_yes);
   1.386  		break;
   1.387 -	case set_mod:
   1.388 +	case allmodconfig:
   1.389  		conf_set_all_new_symbols(def_mod);
   1.390  		break;
   1.391 -	case set_random:
   1.392 +	case alldefconfig:
   1.393 +		conf_set_all_new_symbols(def_default);
   1.394 +		break;
   1.395 +	case randconfig:
   1.396  		conf_set_all_new_symbols(def_random);
   1.397  		break;
   1.398 -	case set_default:
   1.399 +	case defconfig:
   1.400  		conf_set_all_new_symbols(def_default);
   1.401  		break;
   1.402 -	case ask_new:
   1.403 -	case ask_all:
   1.404 +	case savedefconfig:
   1.405 +		break;
   1.406 +	case oldaskconfig:
   1.407  		rootEntry = &rootmenu;
   1.408  		conf(&rootmenu);
   1.409 -		input_mode = ask_silent;
   1.410 +		input_mode = silentoldconfig;
   1.411  		/* fall through */
   1.412 -	case ask_silent:
   1.413 +	case oldconfig:
   1.414 +	case listnewconfig:
   1.415 +	case oldnoconfig:
   1.416 +	case silentoldconfig:
   1.417  		/* Update until a loop caused no more changes */
   1.418  		do {
   1.419  			conf_cnt = 0;
   1.420  			check_conf(&rootmenu);
   1.421 -		} while (conf_cnt);
   1.422 +		} while (conf_cnt &&
   1.423 +			 (input_mode != listnewconfig &&
   1.424 +			  input_mode != oldnoconfig));
   1.425  		break;
   1.426  	}
   1.427  
   1.428 @@ -584,7 +623,14 @@
   1.429  			fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
   1.430  			exit(1);
   1.431  		}
   1.432 -	} else {
   1.433 +		/* In crosstool-NG, we do not use the autoconf stuff */
   1.434 +	} else if (input_mode == savedefconfig) {
   1.435 +		if (conf_write_defconfig(defconfig_file)) {
   1.436 +			fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
   1.437 +			        defconfig_file);
   1.438 +			return 1;
   1.439 +		}
   1.440 +	} else if (input_mode != listnewconfig) {
   1.441  		if (conf_write(NULL)) {
   1.442  			fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
   1.443  			exit(1);
   1.444 @@ -592,3 +638,14 @@
   1.445  	}
   1.446  	return 0;
   1.447  }
   1.448 +/*
   1.449 + * Helper function to facilitate fgets() by Jean Sacren.
   1.450 + */
   1.451 +void xfgets(str, size, in)
   1.452 +	char *str;
   1.453 +	int size;
   1.454 +	FILE *in;
   1.455 +{
   1.456 +	if (fgets(str, size, in) == NULL)
   1.457 +		fprintf(stderr, "\nError in reading or end of file.\n");
   1.458 +}