symbol.c 29.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
 * Released under the terms of the GNU GPL v2.0.
 */

#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <sys/utsname.h>

#include "lkc.h"

struct symbol symbol_yes = {
	.name = "y",
	.curr = { "y", yes },
17
	.flags = SYMBOL_CONST|SYMBOL_VALID,
L
Linus Torvalds 已提交
18 19 20
}, symbol_mod = {
	.name = "m",
	.curr = { "m", mod },
21
	.flags = SYMBOL_CONST|SYMBOL_VALID,
L
Linus Torvalds 已提交
22 23 24
}, symbol_no = {
	.name = "n",
	.curr = { "n", no },
25
	.flags = SYMBOL_CONST|SYMBOL_VALID,
L
Linus Torvalds 已提交
26 27 28 29 30 31
}, symbol_empty = {
	.name = "",
	.curr = { "", no },
	.flags = SYMBOL_VALID,
};

32
struct symbol *sym_defconfig_list;
L
Linus Torvalds 已提交
33 34 35
struct symbol *modules_sym;
tristate modules_val;

36 37
struct expr *sym_env_list;

T
Trevor Keith 已提交
38
static void sym_add_default(struct symbol *sym, const char *def)
L
Linus Torvalds 已提交
39 40 41
{
	struct property *prop = prop_alloc(P_DEFAULT, sym);

R
Roman Zippel 已提交
42
	prop->expr = expr_alloc_symbol(sym_lookup(def, SYMBOL_CONST));
L
Linus Torvalds 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
}

void sym_init(void)
{
	struct symbol *sym;
	struct utsname uts;
	static bool inited = false;

	if (inited)
		return;
	inited = true;

	uname(&uts);

	sym = sym_lookup("UNAME_RELEASE", 0);
	sym->type = S_STRING;
	sym->flags |= SYMBOL_AUTO;
	sym_add_default(sym, uts.release);
}

enum symbol_type sym_get_type(struct symbol *sym)
{
	enum symbol_type type = sym->type;

	if (type == S_TRISTATE) {
		if (sym_is_choice_value(sym) && sym->visible == yes)
			type = S_BOOLEAN;
		else if (modules_val == no)
			type = S_BOOLEAN;
	}
	return type;
}

const char *sym_type_name(enum symbol_type type)
{
	switch (type) {
	case S_BOOLEAN:
		return "boolean";
	case S_TRISTATE:
		return "tristate";
	case S_INT:
		return "integer";
	case S_HEX:
		return "hex";
	case S_STRING:
		return "string";
	case S_UNKNOWN:
		return "unknown";
	case S_OTHER:
		break;
	}
	return "???";
}

struct property *sym_get_choice_prop(struct symbol *sym)
{
	struct property *prop;

	for_all_choices(sym, prop)
		return prop;
	return NULL;
}

106 107 108 109 110 111 112 113 114
struct property *sym_get_env_prop(struct symbol *sym)
{
	struct property *prop;

	for_all_properties(sym, prop, P_ENV)
		return prop;
	return NULL;
}

115
static struct property *sym_get_default_prop(struct symbol *sym)
L
Linus Torvalds 已提交
116 117 118 119 120 121 122 123 124 125 126
{
	struct property *prop;

	for_all_defaults(sym, prop) {
		prop->visible.tri = expr_calc_value(prop->visible.expr);
		if (prop->visible.tri != no)
			return prop;
	}
	return NULL;
}

T
Trevor Keith 已提交
127
static struct property *sym_get_range_prop(struct symbol *sym)
L
Linus Torvalds 已提交
128 129 130 131 132 133 134 135 136 137 138
{
	struct property *prop;

	for_all_properties(sym, prop, P_RANGE) {
		prop->visible.tri = expr_calc_value(prop->visible.expr);
		if (prop->visible.tri != no)
			return prop;
	}
	return NULL;
}

139
static long long sym_get_range_val(struct symbol *sym, int base)
140 141 142 143 144 145 146 147 148 149 150 151
{
	sym_calc_value(sym);
	switch (sym->type) {
	case S_INT:
		base = 10;
		break;
	case S_HEX:
		base = 16;
		break;
	default:
		break;
	}
152
	return strtoll(sym->curr.val, NULL, base);
153 154 155 156 157
}

static void sym_validate_range(struct symbol *sym)
{
	struct property *prop;
158 159
	int base;
	long long val, val2;
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	char str[64];

	switch (sym->type) {
	case S_INT:
		base = 10;
		break;
	case S_HEX:
		base = 16;
		break;
	default:
		return;
	}
	prop = sym_get_range_prop(sym);
	if (!prop)
		return;
175
	val = strtoll(sym->curr.val, NULL, base);
176 177 178 179 180 181 182
	val2 = sym_get_range_val(prop->expr->left.sym, base);
	if (val >= val2) {
		val2 = sym_get_range_val(prop->expr->right.sym, base);
		if (val <= val2)
			return;
	}
	if (sym->type == S_INT)
183
		sprintf(str, "%lld", val2);
184
	else
185
		sprintf(str, "0x%llx", val2);
186 187 188
	sym->curr.val = strdup(str);
}

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
static void sym_set_changed(struct symbol *sym)
{
	struct property *prop;

	sym->flags |= SYMBOL_CHANGED;
	for (prop = sym->prop; prop; prop = prop->next) {
		if (prop->menu)
			prop->menu->flags |= MENU_CHANGED;
	}
}

static void sym_set_all_changed(void)
{
	struct symbol *sym;
	int i;

	for_all_symbols(i, sym)
		sym_set_changed(sym);
}

L
Linus Torvalds 已提交
209 210 211 212 213 214 215 216 217
static void sym_calc_visibility(struct symbol *sym)
{
	struct property *prop;
	tristate tri;

	/* any prompt visible? */
	tri = no;
	for_all_prompts(sym, prop) {
		prop->visible.tri = expr_calc_value(prop->visible.expr);
218
		tri = EXPR_OR(tri, prop->visible.tri);
L
Linus Torvalds 已提交
219 220 221 222 223 224 225 226 227
	}
	if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
		tri = yes;
	if (sym->visible != tri) {
		sym->visible = tri;
		sym_set_changed(sym);
	}
	if (sym_is_choice_value(sym))
		return;
228 229 230 231 232 233 234 235 236 237
	/* defaulting to "yes" if no explicit "depends on" are given */
	tri = yes;
	if (sym->dir_dep.expr)
		tri = expr_calc_value(sym->dir_dep.expr);
	if (tri == mod)
		tri = yes;
	if (sym->dir_dep.tri != tri) {
		sym->dir_dep.tri = tri;
		sym_set_changed(sym);
	}
L
Linus Torvalds 已提交
238 239 240 241 242 243 244 245 246 247 248
	tri = no;
	if (sym->rev_dep.expr)
		tri = expr_calc_value(sym->rev_dep.expr);
	if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
		tri = yes;
	if (sym->rev_dep.tri != tri) {
		sym->rev_dep.tri = tri;
		sym_set_changed(sym);
	}
}

249 250 251 252 253 254 255
/*
 * Find the default symbol for a choice.
 * First try the default values for the choice symbol
 * Next locate the first visible choice value
 * Return NULL if none was found
 */
struct symbol *sym_choice_default(struct symbol *sym)
L
Linus Torvalds 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
{
	struct symbol *def_sym;
	struct property *prop;
	struct expr *e;

	/* any of the defaults visible? */
	for_all_defaults(sym, prop) {
		prop->visible.tri = expr_calc_value(prop->visible.expr);
		if (prop->visible.tri == no)
			continue;
		def_sym = prop_get_symbol(prop);
		if (def_sym->visible != no)
			return def_sym;
	}

	/* just get the first visible value */
	prop = sym_get_choice_prop(sym);
273
	expr_list_for_each_sym(prop->expr, e, def_sym)
L
Linus Torvalds 已提交
274 275 276
		if (def_sym->visible != no)
			return def_sym;

277
	/* failed to locate any defaults */
L
Linus Torvalds 已提交
278 279 280
	return NULL;
}

281 282 283 284 285
static struct symbol *sym_calc_choice(struct symbol *sym)
{
	struct symbol *def_sym;
	struct property *prop;
	struct expr *e;
286
	int flags;
287 288

	/* first calculate all choice values' visibilities */
289
	flags = sym->flags;
290
	prop = sym_get_choice_prop(sym);
291
	expr_list_for_each_sym(prop->expr, e, def_sym) {
292
		sym_calc_visibility(def_sym);
293 294 295 296 297
		if (def_sym->visible != no)
			flags &= def_sym->flags;
	}

	sym->flags &= flags | ~SYMBOL_DEF_USER;
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

	/* is the user choice visible? */
	def_sym = sym->def[S_DEF_USER].val;
	if (def_sym && def_sym->visible != no)
		return def_sym;

	def_sym = sym_choice_default(sym);

	if (def_sym == NULL)
		/* no choice? reset tristate value */
		sym->curr.tri = no;

	return def_sym;
}

L
Linus Torvalds 已提交
313 314 315 316 317 318 319 320 321 322 323
void sym_calc_value(struct symbol *sym)
{
	struct symbol_value newval, oldval;
	struct property *prop;
	struct expr *e;

	if (!sym)
		return;

	if (sym->flags & SYMBOL_VALID)
		return;
324 325 326 327 328 329 330 331

	if (sym_is_choice_value(sym) &&
	    sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES) {
		sym->flags &= ~SYMBOL_NEED_SET_CHOICE_VALUES;
		prop = sym_get_choice_prop(sym);
		sym_calc_value(prop_get_symbol(prop));
	}

L
Linus Torvalds 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
	sym->flags |= SYMBOL_VALID;

	oldval = sym->curr;

	switch (sym->type) {
	case S_INT:
	case S_HEX:
	case S_STRING:
		newval = symbol_empty.curr;
		break;
	case S_BOOLEAN:
	case S_TRISTATE:
		newval = symbol_no.curr;
		break;
	default:
		sym->curr.val = sym->name;
		sym->curr.tri = no;
		return;
	}
	if (!sym_is_choice_value(sym))
		sym->flags &= ~SYMBOL_WRITE;

	sym_calc_visibility(sym);

	/* set default if recursively called */
	sym->curr = newval;

	switch (sym_get_type(sym)) {
	case S_BOOLEAN:
	case S_TRISTATE:
		if (sym_is_choice_value(sym) && sym->visible == yes) {
			prop = sym_get_choice_prop(sym);
			newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
365 366 367 368 369 370 371 372 373 374 375
		} else {
			if (sym->visible != no) {
				/* if the symbol is visible use the user value
				 * if available, otherwise try the default value
				 */
				sym->flags |= SYMBOL_WRITE;
				if (sym_has_value(sym)) {
					newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
							      sym->visible);
					goto calc_newval;
				}
L
Linus Torvalds 已提交
376
			}
377
			if (sym->rev_dep.tri != no)
L
Linus Torvalds 已提交
378
				sym->flags |= SYMBOL_WRITE;
379 380 381 382 383 384 385
			if (!sym_is_choice(sym)) {
				prop = sym_get_default_prop(sym);
				if (prop) {
					sym->flags |= SYMBOL_WRITE;
					newval.tri = EXPR_AND(expr_calc_value(prop->expr),
							      prop->visible.tri);
				}
L
Linus Torvalds 已提交
386
			}
387
		calc_newval:
388
			if (sym->dir_dep.tri == no && sym->rev_dep.tri != no) {
389 390 391
				struct expr *e;
				e = expr_simplify_unmet_dep(sym->rev_dep.expr,
				    sym->dir_dep.expr);
392
				fprintf(stderr, "warning: (");
393
				expr_fprint(e, stderr);
394 395 396 397
				fprintf(stderr, ") selects %s which has unmet direct dependencies (",
					sym->name);
				expr_fprint(sym->dir_dep.expr, stderr);
				fprintf(stderr, ")\n");
398
				expr_free(e);
399
			}
400
			newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
L
Linus Torvalds 已提交
401 402 403 404 405 406 407 408 409 410
		}
		if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
			newval.tri = yes;
		break;
	case S_STRING:
	case S_HEX:
	case S_INT:
		if (sym->visible != no) {
			sym->flags |= SYMBOL_WRITE;
			if (sym_has_value(sym)) {
411
				newval.val = sym->def[S_DEF_USER].val;
L
Linus Torvalds 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
				break;
			}
		}
		prop = sym_get_default_prop(sym);
		if (prop) {
			struct symbol *ds = prop_get_symbol(prop);
			if (ds) {
				sym->flags |= SYMBOL_WRITE;
				sym_calc_value(ds);
				newval.val = ds->curr.val;
			}
		}
		break;
	default:
		;
	}

	sym->curr = newval;
	if (sym_is_choice(sym) && newval.tri == yes)
		sym->curr.val = sym_calc_choice(sym);
432
	sym_validate_range(sym);
L
Linus Torvalds 已提交
433

434
	if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
L
Linus Torvalds 已提交
435
		sym_set_changed(sym);
436 437 438 439 440
		if (modules_sym == sym) {
			sym_set_all_changed();
			modules_val = modules_sym->curr.tri;
		}
	}
L
Linus Torvalds 已提交
441 442

	if (sym_is_choice(sym)) {
443 444
		struct symbol *choice_sym;

L
Linus Torvalds 已提交
445
		prop = sym_get_choice_prop(sym);
446
		expr_list_for_each_sym(prop->expr, e, choice_sym) {
447 448 449 450
			if ((sym->flags & SYMBOL_WRITE) &&
			    choice_sym->visible != no)
				choice_sym->flags |= SYMBOL_WRITE;
			if (sym->flags & SYMBOL_CHANGED)
451
				sym_set_changed(choice_sym);
L
Linus Torvalds 已提交
452 453
		}
	}
R
Roman Zippel 已提交
454 455 456

	if (sym->flags & SYMBOL_AUTO)
		sym->flags &= ~SYMBOL_WRITE;
457 458 459

	if (sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES)
		set_all_choice_values(sym);
L
Linus Torvalds 已提交
460 461 462 463 464 465 466 467 468
}

void sym_clear_all_valid(void)
{
	struct symbol *sym;
	int i;

	for_all_symbols(i, sym)
		sym->flags &= ~SYMBOL_VALID;
469
	sym_add_change_count(1);
470
	sym_calc_value(modules_sym);
L
Linus Torvalds 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
}

bool sym_tristate_within_range(struct symbol *sym, tristate val)
{
	int type = sym_get_type(sym);

	if (sym->visible == no)
		return false;

	if (type != S_BOOLEAN && type != S_TRISTATE)
		return false;

	if (type == S_BOOLEAN && val == mod)
		return false;
	if (sym->visible <= sym->rev_dep.tri)
		return false;
	if (sym_is_choice_value(sym) && sym->visible == yes)
		return val == yes;
	return val >= sym->rev_dep.tri && val <= sym->visible;
}

bool sym_set_tristate_value(struct symbol *sym, tristate val)
{
	tristate oldval = sym_get_tristate_value(sym);

	if (oldval != val && !sym_tristate_within_range(sym, val))
		return false;

499 500
	if (!(sym->flags & SYMBOL_DEF_USER)) {
		sym->flags |= SYMBOL_DEF_USER;
L
Linus Torvalds 已提交
501 502
		sym_set_changed(sym);
	}
503 504 505 506
	/*
	 * setting a choice value also resets the new flag of the choice
	 * symbol and all other choice values.
	 */
L
Linus Torvalds 已提交
507 508
	if (sym_is_choice_value(sym) && val == yes) {
		struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
509 510
		struct property *prop;
		struct expr *e;
L
Linus Torvalds 已提交
511

512
		cs->def[S_DEF_USER].val = sym;
513
		cs->flags |= SYMBOL_DEF_USER;
514 515 516
		prop = sym_get_choice_prop(cs);
		for (e = prop->expr; e; e = e->left.expr) {
			if (e->right.sym->visible != no)
517
				e->right.sym->flags |= SYMBOL_DEF_USER;
518
		}
L
Linus Torvalds 已提交
519 520
	}

521
	sym->def[S_DEF_USER].tri = val;
522
	if (oldval != val)
L
Linus Torvalds 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
		sym_clear_all_valid();

	return true;
}

tristate sym_toggle_tristate_value(struct symbol *sym)
{
	tristate oldval, newval;

	oldval = newval = sym_get_tristate_value(sym);
	do {
		switch (newval) {
		case no:
			newval = mod;
			break;
		case mod:
			newval = yes;
			break;
		case yes:
			newval = no;
			break;
		}
		if (sym_set_tristate_value(sym, newval))
			break;
	} while (oldval != newval);
	return newval;
}

bool sym_string_valid(struct symbol *sym, const char *str)
{
	signed char ch;

	switch (sym->type) {
	case S_STRING:
		return true;
	case S_INT:
		ch = *str++;
		if (ch == '-')
			ch = *str++;
		if (!isdigit(ch))
			return false;
		if (ch == '0' && *str != 0)
			return false;
		while ((ch = *str++)) {
			if (!isdigit(ch))
				return false;
		}
		return true;
	case S_HEX:
		if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
			str += 2;
		ch = *str++;
		do {
			if (!isxdigit(ch))
				return false;
		} while ((ch = *str++));
		return true;
	case S_BOOLEAN:
	case S_TRISTATE:
		switch (str[0]) {
		case 'y': case 'Y':
		case 'm': case 'M':
		case 'n': case 'N':
			return true;
		}
		return false;
	default:
		return false;
	}
}

bool sym_string_within_range(struct symbol *sym, const char *str)
{
	struct property *prop;
597
	long long val;
L
Linus Torvalds 已提交
598 599 600 601 602 603 604 605 606 607

	switch (sym->type) {
	case S_STRING:
		return sym_string_valid(sym, str);
	case S_INT:
		if (!sym_string_valid(sym, str))
			return false;
		prop = sym_get_range_prop(sym);
		if (!prop)
			return true;
608
		val = strtoll(str, NULL, 10);
609 610
		return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
		       val <= sym_get_range_val(prop->expr->right.sym, 10);
L
Linus Torvalds 已提交
611 612 613 614 615 616
	case S_HEX:
		if (!sym_string_valid(sym, str))
			return false;
		prop = sym_get_range_prop(sym);
		if (!prop)
			return true;
617
		val = strtoll(str, NULL, 16);
618 619
		return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
		       val <= sym_get_range_val(prop->expr->right.sym, 16);
L
Linus Torvalds 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
	case S_BOOLEAN:
	case S_TRISTATE:
		switch (str[0]) {
		case 'y': case 'Y':
			return sym_tristate_within_range(sym, yes);
		case 'm': case 'M':
			return sym_tristate_within_range(sym, mod);
		case 'n': case 'N':
			return sym_tristate_within_range(sym, no);
		}
		return false;
	default:
		return false;
	}
}

bool sym_set_string_value(struct symbol *sym, const char *newval)
{
	const char *oldval;
	char *val;
	int size;

	switch (sym->type) {
	case S_BOOLEAN:
	case S_TRISTATE:
		switch (newval[0]) {
		case 'y': case 'Y':
			return sym_set_tristate_value(sym, yes);
		case 'm': case 'M':
			return sym_set_tristate_value(sym, mod);
		case 'n': case 'N':
			return sym_set_tristate_value(sym, no);
		}
		return false;
	default:
		;
	}

	if (!sym_string_within_range(sym, newval))
		return false;

661 662
	if (!(sym->flags & SYMBOL_DEF_USER)) {
		sym->flags |= SYMBOL_DEF_USER;
L
Linus Torvalds 已提交
663 664 665
		sym_set_changed(sym);
	}

666
	oldval = sym->def[S_DEF_USER].val;
L
Linus Torvalds 已提交
667 668 669
	size = strlen(newval) + 1;
	if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
		size += 2;
670
		sym->def[S_DEF_USER].val = val = xmalloc(size);
L
Linus Torvalds 已提交
671 672 673
		*val++ = '0';
		*val++ = 'x';
	} else if (!oldval || strcmp(oldval, newval))
674
		sym->def[S_DEF_USER].val = val = xmalloc(size);
L
Linus Torvalds 已提交
675 676 677 678 679 680 681 682 683 684
	else
		return true;

	strcpy(val, newval);
	free((void *)oldval);
	sym_clear_all_valid();

	return true;
}

S
Sam Ravnborg 已提交
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
/*
 * Find the default value associated to a symbol.
 * For tristate symbol handle the modules=n case
 * in which case "m" becomes "y".
 * If the symbol does not have any default then fallback
 * to the fixed default values.
 */
const char *sym_get_string_default(struct symbol *sym)
{
	struct property *prop;
	struct symbol *ds;
	const char *str;
	tristate val;

	sym_calc_visibility(sym);
	sym_calc_value(modules_sym);
	val = symbol_no.curr.tri;
	str = symbol_empty.curr.val;

	/* If symbol has a default value look it up */
	prop = sym_get_default_prop(sym);
	if (prop != NULL) {
		switch (sym->type) {
		case S_BOOLEAN:
		case S_TRISTATE:
A
Arnaud Lacombe 已提交
710
			/* The visibility may limit the value from yes => mod */
S
Sam Ravnborg 已提交
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
			val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
			break;
		default:
			/*
			 * The following fails to handle the situation
			 * where a default value is further limited by
			 * the valid range.
			 */
			ds = prop_get_symbol(prop);
			if (ds != NULL) {
				sym_calc_value(ds);
				str = (const char *)ds->curr.val;
			}
		}
	}

	/* Handle select statements */
	val = EXPR_OR(val, sym->rev_dep.tri);

	/* transpose mod to yes if modules are not enabled */
	if (val == mod)
		if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
			val = yes;

	/* transpose mod to yes if type is bool */
	if (sym->type == S_BOOLEAN && val == mod)
		val = yes;

	switch (sym->type) {
	case S_BOOLEAN:
	case S_TRISTATE:
		switch (val) {
		case no: return "n";
		case mod: return "m";
		case yes: return "y";
		}
	case S_INT:
	case S_HEX:
		return str;
	case S_STRING:
		return str;
	case S_OTHER:
	case S_UNKNOWN:
		break;
	}
	return "";
}

L
Linus Torvalds 已提交
759 760 761 762 763 764 765 766 767 768 769 770
const char *sym_get_string_value(struct symbol *sym)
{
	tristate val;

	switch (sym->type) {
	case S_BOOLEAN:
	case S_TRISTATE:
		val = sym_get_tristate_value(sym);
		switch (val) {
		case no:
			return "n";
		case mod:
771 772
			sym_calc_value(modules_sym);
			return (modules_sym->curr.tri == no) ? "n" : "m";
L
Linus Torvalds 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
		case yes:
			return "y";
		}
		break;
	default:
		;
	}
	return (const char *)sym->curr.val;
}

bool sym_is_changable(struct symbol *sym)
{
	return sym->visible > sym->rev_dep.tri;
}

A
Andi Kleen 已提交
788 789 790 791 792 793 794 795 796
static unsigned strhash(const char *s)
{
	/* fnv32 hash */
	unsigned hash = 2166136261U;
	for (; *s; s++)
		hash = (hash ^ *s) * 0x01000193;
	return hash;
}

R
Roman Zippel 已提交
797
struct symbol *sym_lookup(const char *name, int flags)
L
Linus Torvalds 已提交
798 799 800
{
	struct symbol *symbol;
	char *new_name;
A
Andi Kleen 已提交
801
	int hash;
L
Linus Torvalds 已提交
802 803 804 805 806 807 808 809 810

	if (name) {
		if (name[0] && !name[1]) {
			switch (name[0]) {
			case 'y': return &symbol_yes;
			case 'm': return &symbol_mod;
			case 'n': return &symbol_no;
			}
		}
A
Andi Kleen 已提交
811
		hash = strhash(name) % SYMBOL_HASHSIZE;
L
Linus Torvalds 已提交
812 813

		for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
A
Andi Kleen 已提交
814 815
			if (symbol->name &&
			    !strcmp(symbol->name, name) &&
R
Roman Zippel 已提交
816 817 818
			    (flags ? symbol->flags & flags
				   : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
				return symbol;
L
Linus Torvalds 已提交
819 820 821 822
		}
		new_name = strdup(name);
	} else {
		new_name = NULL;
A
Andi Kleen 已提交
823
		hash = 0;
L
Linus Torvalds 已提交
824 825
	}

826
	symbol = xmalloc(sizeof(*symbol));
L
Linus Torvalds 已提交
827 828 829
	memset(symbol, 0, sizeof(*symbol));
	symbol->name = new_name;
	symbol->type = S_UNKNOWN;
R
Roman Zippel 已提交
830
	symbol->flags |= flags;
L
Linus Torvalds 已提交
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852

	symbol->next = symbol_hash[hash];
	symbol_hash[hash] = symbol;

	return symbol;
}

struct symbol *sym_find(const char *name)
{
	struct symbol *symbol = NULL;
	int hash = 0;

	if (!name)
		return NULL;

	if (name[0] && !name[1]) {
		switch (name[0]) {
		case 'y': return &symbol_yes;
		case 'm': return &symbol_mod;
		case 'n': return &symbol_no;
		}
	}
A
Andi Kleen 已提交
853
	hash = strhash(name) % SYMBOL_HASHSIZE;
L
Linus Torvalds 已提交
854 855

	for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
A
Andi Kleen 已提交
856 857
		if (symbol->name &&
		    !strcmp(symbol->name, name) &&
L
Linus Torvalds 已提交
858 859 860 861 862 863 864
		    !(symbol->flags & SYMBOL_CONST))
				break;
	}

	return symbol;
}

865 866 867 868 869 870 871 872 873 874 875 876
/*
 * Expand symbol's names embedded in the string given in argument. Symbols'
 * name to be expanded shall be prefixed by a '$'. Unknown symbol expands to
 * the empty string.
 */
const char *sym_expand_string_value(const char *in)
{
	const char *src;
	char *res;
	size_t reslen;

	reslen = strlen(in) + 1;
877
	res = xmalloc(reslen);
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
	res[0] = '\0';

	while ((src = strchr(in, '$'))) {
		char *p, name[SYMBOL_MAXLENGTH];
		const char *symval = "";
		struct symbol *sym;
		size_t newlen;

		strncat(res, in, src - in);
		src++;

		p = name;
		while (isalnum(*src) || *src == '_')
			*p++ = *src++;
		*p = '\0';

		sym = sym_find(name);
		if (sym != NULL) {
			sym_calc_value(sym);
			symval = sym_get_string_value(sym);
		}

900
		newlen = strlen(res) + strlen(symval) + strlen(src) + 1;
901 902
		if (newlen > reslen) {
			reslen = newlen;
M
Michal Marek 已提交
903
			res = realloc(res, reslen);
904 905 906 907 908 909 910 911 912 913
		}

		strcat(res, symval);
		in = src;
	}
	strcat(res, in);

	return res;
}

914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
const char *sym_escape_string_value(const char *in)
{
	const char *p;
	size_t reslen;
	char *res;
	size_t l;

	reslen = strlen(in) + strlen("\"\"") + 1;

	p = in;
	for (;;) {
		l = strcspn(p, "\"\\");
		p += l;

		if (p[0] == '\0')
			break;

		reslen++;
		p++;
	}

935
	res = xmalloc(reslen);
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
	res[0] = '\0';

	strcat(res, "\"");

	p = in;
	for (;;) {
		l = strcspn(p, "\"\\");
		strncat(res, p, l);
		p += l;

		if (p[0] == '\0')
			break;

		strcat(res, "\\");
		strncat(res, p++, 1);
	}

	strcat(res, "\"");
	return res;
}

957 958 959 960 961 962 963 964 965
struct sym_match {
	struct symbol	*sym;
	off_t		so, eo;
};

/* Compare matched symbols as thus:
 * - first, symbols that match exactly
 * - then, alphabetical sort
 */
966
static int sym_rel_comp(const void *sym1, const void *sym2)
967
{
968 969
	const struct sym_match *s1 = sym1;
	const struct sym_match *s2 = sym2;
970
	int exact1, exact2;
971 972 973 974 975 976 977 978 979 980

	/* Exact match:
	 * - if matched length on symbol s1 is the length of that symbol,
	 *   then this symbol should come first;
	 * - if matched length on symbol s2 is the length of that symbol,
	 *   then this symbol should come first.
	 * Note: since the search can be a regexp, both symbols may match
	 * exactly; if this is the case, we can't decide which comes first,
	 * and we fallback to sorting alphabetically.
	 */
981 982 983
	exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
	exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
	if (exact1 && !exact2)
984
		return -1;
985
	if (!exact1 && exact2)
986 987 988 989 990 991
		return 1;

	/* As a fallback, sort symbols alphabetically */
	return strcmp(s1->sym->name, s2->sym->name);
}

L
Linus Torvalds 已提交
992 993 994
struct symbol **sym_re_search(const char *pattern)
{
	struct symbol *sym, **sym_arr = NULL;
995
	struct sym_match *sym_match_arr = NULL;
L
Linus Torvalds 已提交
996 997
	int i, cnt, size;
	regex_t re;
998
	regmatch_t match[1];
L
Linus Torvalds 已提交
999 1000 1001 1002 1003

	cnt = size = 0;
	/* Skip if empty */
	if (strlen(pattern) == 0)
		return NULL;
1004
	if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
L
Linus Torvalds 已提交
1005 1006 1007 1008 1009
		return NULL;

	for_all_symbols(i, sym) {
		if (sym->flags & SYMBOL_CONST || !sym->name)
			continue;
1010
		if (regexec(&re, sym->name, 1, match, 0))
L
Linus Torvalds 已提交
1011
			continue;
1012
		if (cnt >= size) {
1013
			void *tmp;
L
Linus Torvalds 已提交
1014
			size += 16;
1015
			tmp = realloc(sym_match_arr, size * sizeof(struct sym_match));
1016
			if (!tmp)
1017 1018
				goto sym_re_search_free;
			sym_match_arr = tmp;
L
Linus Torvalds 已提交
1019
		}
1020
		sym_calc_value(sym);
1021
		/* As regexec returned 0, we know we have a match, so
1022 1023
		 * we can use match[0].rm_[se]o without further checks
		 */
1024 1025 1026
		sym_match_arr[cnt].so = match[0].rm_so;
		sym_match_arr[cnt].eo = match[0].rm_eo;
		sym_match_arr[cnt++].sym = sym;
L
Linus Torvalds 已提交
1027
	}
1028
	if (sym_match_arr) {
1029
		qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp);
1030 1031 1032 1033
		sym_arr = malloc((cnt+1) * sizeof(struct symbol));
		if (!sym_arr)
			goto sym_re_search_free;
		for (i = 0; i < cnt; i++)
1034
			sym_arr[i] = sym_match_arr[i].sym;
L
Linus Torvalds 已提交
1035
		sym_arr[cnt] = NULL;
1036 1037
	}
sym_re_search_free:
1038 1039
	/* sym_match_arr can be NULL if no match, but free(NULL) is OK */
	free(sym_match_arr);
L
Linus Torvalds 已提交
1040 1041 1042 1043 1044
	regfree(&re);

	return sym_arr;
}

1045 1046 1047 1048
/*
 * When we check for recursive dependencies we use a stack to save
 * current state so we can print out relevant info to user.
 * The entries are located on the call stack so no need to free memory.
1049
 * Note insert() remove() must always match to properly clear the stack.
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
 */
static struct dep_stack {
	struct dep_stack *prev, *next;
	struct symbol *sym;
	struct property *prop;
	struct expr *expr;
} *check_top;

static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
{
	memset(stack, 0, sizeof(*stack));
	if (check_top)
		check_top->next = stack;
	stack->prev = check_top;
	stack->sym = sym;
	check_top = stack;
}

static void dep_stack_remove(void)
{
	check_top = check_top->prev;
	if (check_top)
		check_top->next = NULL;
}

/*
 * Called when we have detected a recursive dependency.
 * check_top point to the top of the stact so we use
 * the ->prev pointer to locate the bottom of the stack.
 */
static void sym_check_print_recursive(struct symbol *last_sym)
{
	struct dep_stack *stack;
	struct symbol *sym, *next_sym;
	struct menu *menu = NULL;
	struct property *prop;
	struct dep_stack cv_stack;

	if (sym_is_choice_value(last_sym)) {
		dep_stack_insert(&cv_stack, last_sym);
		last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
	}

	for (stack = check_top; stack != NULL; stack = stack->prev)
		if (stack->sym == last_sym)
			break;
	if (!stack) {
		fprintf(stderr, "unexpected recursive dependency error\n");
		return;
	}

	for (; stack; stack = stack->next) {
		sym = stack->sym;
		next_sym = stack->next ? stack->next->sym : last_sym;
		prop = stack->prop;
1105 1106
		if (prop == NULL)
			prop = stack->sym->prop;
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150

		/* for choice values find the menu entry (used below) */
		if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
			for (prop = sym->prop; prop; prop = prop->next) {
				menu = prop->menu;
				if (prop->menu)
					break;
			}
		}
		if (stack->sym == last_sym)
			fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
				prop->file->name, prop->lineno);
		if (stack->expr) {
			fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
				prop->file->name, prop->lineno,
				sym->name ? sym->name : "<choice>",
				prop_get_type_name(prop->type),
				next_sym->name ? next_sym->name : "<choice>");
		} else if (stack->prop) {
			fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
				prop->file->name, prop->lineno,
				sym->name ? sym->name : "<choice>",
				next_sym->name ? next_sym->name : "<choice>");
		} else if (sym_is_choice(sym)) {
			fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
				menu->file->name, menu->lineno,
				sym->name ? sym->name : "<choice>",
				next_sym->name ? next_sym->name : "<choice>");
		} else if (sym_is_choice_value(sym)) {
			fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
				menu->file->name, menu->lineno,
				sym->name ? sym->name : "<choice>",
				next_sym->name ? next_sym->name : "<choice>");
		} else {
			fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
				prop->file->name, prop->lineno,
				sym->name ? sym->name : "<choice>",
				next_sym->name ? next_sym->name : "<choice>");
		}
	}

	if (check_top == &cv_stack)
		dep_stack_remove();
}
L
Linus Torvalds 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167

static struct symbol *sym_check_expr_deps(struct expr *e)
{
	struct symbol *sym;

	if (!e)
		return NULL;
	switch (e->type) {
	case E_OR:
	case E_AND:
		sym = sym_check_expr_deps(e->left.expr);
		if (sym)
			return sym;
		return sym_check_expr_deps(e->right.expr);
	case E_NOT:
		return sym_check_expr_deps(e->left.expr);
	case E_EQUAL:
1168 1169 1170 1171
	case E_GEQ:
	case E_GTH:
	case E_LEQ:
	case E_LTH:
L
Linus Torvalds 已提交
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
	case E_UNEQUAL:
		sym = sym_check_deps(e->left.sym);
		if (sym)
			return sym;
		return sym_check_deps(e->right.sym);
	case E_SYMBOL:
		return sym_check_deps(e->left.sym);
	default:
		break;
	}
	printf("Oops! How to check %d?\n", e->type);
	return NULL;
}

1186
/* return NULL when dependencies are OK */
1187
static struct symbol *sym_check_sym_deps(struct symbol *sym)
L
Linus Torvalds 已提交
1188 1189 1190
{
	struct symbol *sym2;
	struct property *prop;
1191 1192 1193
	struct dep_stack stack;

	dep_stack_insert(&stack, sym);
L
Linus Torvalds 已提交
1194 1195 1196

	sym2 = sym_check_expr_deps(sym->rev_dep.expr);
	if (sym2)
1197
		goto out;
L
Linus Torvalds 已提交
1198 1199 1200 1201

	for (prop = sym->prop; prop; prop = prop->next) {
		if (prop->type == P_CHOICE || prop->type == P_SELECT)
			continue;
1202
		stack.prop = prop;
L
Linus Torvalds 已提交
1203 1204
		sym2 = sym_check_expr_deps(prop->visible.expr);
		if (sym2)
1205
			break;
L
Linus Torvalds 已提交
1206 1207
		if (prop->type != P_DEFAULT || sym_is_choice(sym))
			continue;
1208
		stack.expr = prop->expr;
L
Linus Torvalds 已提交
1209 1210
		sym2 = sym_check_expr_deps(prop->expr);
		if (sym2)
1211
			break;
1212
		stack.expr = NULL;
L
Linus Torvalds 已提交
1213
	}
1214

1215 1216 1217
out:
	dep_stack_remove();

1218 1219 1220 1221 1222 1223 1224 1225
	return sym2;
}

static struct symbol *sym_check_choice_deps(struct symbol *choice)
{
	struct symbol *sym, *sym2;
	struct property *prop;
	struct expr *e;
1226 1227 1228
	struct dep_stack stack;

	dep_stack_insert(&stack, choice);
1229 1230 1231 1232 1233 1234 1235 1236

	prop = sym_get_choice_prop(choice);
	expr_list_for_each_sym(prop->expr, e, sym)
		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);

	choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
	sym2 = sym_check_sym_deps(choice);
	choice->flags &= ~SYMBOL_CHECK;
1237
	if (sym2)
1238 1239 1240 1241
		goto out;

	expr_list_for_each_sym(prop->expr, e, sym) {
		sym2 = sym_check_sym_deps(sym);
1242
		if (sym2)
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
			break;
	}
out:
	expr_list_for_each_sym(prop->expr, e, sym)
		sym->flags &= ~SYMBOL_CHECK;

	if (sym2 && sym_is_choice_value(sym2) &&
	    prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
		sym2 = choice;

1253 1254
	dep_stack_remove();

1255 1256 1257 1258 1259 1260 1261 1262 1263
	return sym2;
}

struct symbol *sym_check_deps(struct symbol *sym)
{
	struct symbol *sym2;
	struct property *prop;

	if (sym->flags & SYMBOL_CHECK) {
1264
		sym_check_print_recursive(sym);
1265 1266 1267 1268 1269 1270
		return sym;
	}
	if (sym->flags & SYMBOL_CHECKED)
		return NULL;

	if (sym_is_choice_value(sym)) {
1271 1272
		struct dep_stack stack;

1273
		/* for choice groups start the check with main choice symbol */
1274
		dep_stack_insert(&stack, sym);
1275 1276
		prop = sym_get_choice_prop(sym);
		sym2 = sym_check_deps(prop_get_symbol(prop));
1277
		dep_stack_remove();
1278 1279 1280 1281 1282 1283 1284 1285
	} else if (sym_is_choice(sym)) {
		sym2 = sym_check_choice_deps(sym);
	} else {
		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
		sym2 = sym_check_sym_deps(sym);
		sym->flags &= ~SYMBOL_CHECK;
	}

1286 1287
	if (sym2 && sym2 == sym)
		sym2 = NULL;
1288

L
Linus Torvalds 已提交
1289 1290 1291 1292 1293 1294 1295 1296
	return sym2;
}

struct property *prop_alloc(enum prop_type type, struct symbol *sym)
{
	struct property *prop;
	struct property **propp;

1297
	prop = xmalloc(sizeof(*prop));
L
Linus Torvalds 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
	memset(prop, 0, sizeof(*prop));
	prop->type = type;
	prop->sym = sym;
	prop->file = current_file;
	prop->lineno = zconf_lineno();

	/* append property to the prop list of symbol */
	if (sym) {
		for (propp = &sym->prop; *propp; propp = &(*propp)->next)
			;
		*propp = prop;
	}

	return prop;
}

struct symbol *prop_get_symbol(struct property *prop)
{
	if (prop->expr && (prop->expr->type == E_SYMBOL ||
1317
			   prop->expr->type == E_LIST))
L
Linus Torvalds 已提交
1318 1319 1320 1321 1322 1323 1324 1325 1326
		return prop->expr->left.sym;
	return NULL;
}

const char *prop_get_type_name(enum prop_type type)
{
	switch (type) {
	case P_PROMPT:
		return "prompt";
1327 1328
	case P_ENV:
		return "env";
L
Linus Torvalds 已提交
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
	case P_COMMENT:
		return "comment";
	case P_MENU:
		return "menu";
	case P_DEFAULT:
		return "default";
	case P_CHOICE:
		return "choice";
	case P_SELECT:
		return "select";
	case P_RANGE:
		return "range";
1341 1342
	case P_SYMBOL:
		return "symbol";
L
Linus Torvalds 已提交
1343 1344 1345 1346 1347
	case P_UNKNOWN:
		break;
	}
	return "unknown";
}
1348

T
Trevor Keith 已提交
1349
static void prop_add_env(const char *env)
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
{
	struct symbol *sym, *sym2;
	struct property *prop;
	char *p;

	sym = current_entry->sym;
	sym->flags |= SYMBOL_AUTO;
	for_all_properties(sym, prop, P_ENV) {
		sym2 = prop_get_symbol(prop);
		if (strcmp(sym2->name, env))
			menu_warn(current_entry, "redefining environment symbol from %s",
				  sym2->name);
		return;
	}

	prop = prop_alloc(P_ENV, sym);
R
Roman Zippel 已提交
1366
	prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376

	sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
	sym_env_list->right.sym = sym;

	p = getenv(env);
	if (p)
		sym_add_default(sym, p);
	else
		menu_warn(current_entry, "environment variable %s undefined", env);
}