xaizek / vifm (License: GPLv2+) (since 2018-12-07)
Vifm is a file manager with curses interface, which provides Vi[m]-like environment for managing objects within file systems, extended with some useful ideas from mutt.
<root> / src / engine / variables.c (c0ab5db7354fb9ca95b843beb7b44ff7a69d68da) (21KiB) (mode 100644) [raw]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 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 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 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 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 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 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 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 710 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 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
/* vifm
 * Copyright (C) 2012 xaizek.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "variables.h"

#include <assert.h> /* assert() */
#include <ctype.h>
#include <stddef.h> /* NULL size_t */
#include <stdio.h>
#include <stdlib.h> /* free() realloc() */
#include <string.h> /* strcmp() */

#include "../compat/reallocarray.h"
#include "../utils/env.h"
#include "../utils/macros.h"
#include "../utils/str.h"
#include "../utils/string_array.h"
#include "completion.h"
#include "options.h"
#include "parsing.h"
#include "text_buffer.h"
#include "var.h"

#define VAR_NAME_MAX 64
#define VAL_LEN_MAX 2048

/* Types of supported variables. */
typedef enum
{
	VT_ENVVAR,        /* Environment variable. */
	VT_GVAR,          /* Global variable. */
	VT_ANY_OPTION,    /* Global and local options (if local exists). */
	VT_GLOBAL_OPTION, /* Global option. */
	VT_LOCAL_OPTION,  /* Local option. */
}
VariableType;

/* Supported operations. */
typedef enum
{
	VO_ASSIGN, /* Assigning a variable (=). */
	VO_APPEND, /* Appending to a string (.=). */
	VO_ADD,    /* Adding to numbers or composite values (+=). */
	VO_SUB,    /* Subtracting from numbers or removing from composites (-=). */
}
VariableOperation;

/* Description of a variable. */
typedef struct
{
	char *name; /* Name of the variable (including "[gv]:" prefix). */
	var_t val;  /* Current value of the variable. */
}
var_info_t;

typedef struct
{
	char *name;
	char *val;
	char *initial;
	int from_parent;
	int removed;
}
envvar_t;

const char ENV_VAR_NAME_FIRST_CHAR[] = "abcdefghijklmnopqrstuvwxyz"
	"ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
const char ENV_VAR_NAME_CHARS[] = "abcdefghijklmnopqrstuvwxyz"
	"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";

static void init_var(const char name[], const char value[]);
static void clear_internal_vars(void);
static int extract_name(const char **in, VariableType *type, size_t buf_len,
		char buf[]);
static int extract_op(const char **in, VariableOperation *vo);
static int parse_name(const char **in, const char first[], const char other[],
		size_t buf_len, char buf[]);
static int is_valid_op(const char name[], VariableType vt,
		VariableOperation vo);
static int perform_op(const char name[], VariableType vt, VariableOperation vo,
		var_t val, const char value[]);
static void append_envvar(const char *name, const char *val);
static void set_envvar(const char *name, const char *val);
static int append_internal_var(var_info_t *var, var_t val);
static int perform_opt_op(const char name[], VariableType vt,
		VariableOperation vo, const char value[]);
static envvar_t * get_record(const char *name);
static char * skip_non_whitespace(const char str[]);
static envvar_t * find_record(const char *name);
static void free_record(envvar_t *record);
static void clear_record(envvar_t *record);
static void complete_envvars(const char var[], const char **start);
static void complete_internal_vars(const char var[], const char **start);
static var_info_t * find_internal_var(const char varname[], int create);

static int initialized;
static envvar_t *env_vars;
static size_t env_var_count;
/* List of internal variables. */
static var_info_t *internal_vars;
/* Number of internal variables. */
static size_t internal_var_count;

void
init_variables(void)
{
	int env_count;
	char **env_lst;

	if(env_var_count > 0)
	{
		clear_envvars();
	}

	env_lst = env_list(&env_count);
	if(env_count > 0)
	{
		int i;

		/* Allocate memory for environment variables. */
		env_vars = reallocarray(NULL, env_count, sizeof(*env_vars));
		assert(env_vars != NULL && "Failed to allocate memory for env vars.");

		/* Initialize variable list. */
		for(i = 0; i < env_count; ++i)
		{
			init_var(env_lst[i], env_get(env_lst[i]));
		}
	}

	free_string_array(env_lst, env_count);

	vle_parser_init(&local_getenv);

	initialized = 1;
}

const char *
local_getenv(const char envname[])
{
	const char *value = local_getenv_null(envname);
	return (value == NULL ? "" : value);
}

const char *
local_getenv_null(const char envname[])
{
	envvar_t *record = find_record(envname);
	return (record == NULL || record->removed) ? NULL : record->val;
}

/* Initializes environment variable inherited from parent process. */
static void
init_var(const char name[], const char value[])
{
	envvar_t *const record = get_record(name);
	if(record == NULL)
	{
		return;
	}

	record->from_parent = 1;

	(void)replace_string(&record->initial, value);
	(void)replace_string(&record->val, value);
	if(record->initial == NULL || record->val == NULL)
	{
		free_record(record);
	}
}

void
clear_variables(void)
{
	assert(initialized);

	clear_internal_vars();
	clear_envvars();
}

/* Clears the list of internal variables. */
static void
clear_internal_vars(void)
{
	size_t i;
	for(i = 0U; i < internal_var_count; ++i)
	{
		free(internal_vars[i].name);
		var_free(internal_vars[i].val);
	}
	internal_var_count = 0U;
	free(internal_vars);
	internal_vars = NULL;
}

void
clear_envvars(void)
{
	size_t i;
	assert(initialized);

	/* Free memory. */
	for(i = 0U; i < env_var_count; ++i)
	{
		if(env_vars[i].name == NULL)
			continue;

		if(env_vars[i].from_parent)
		{
			set_envvar(env_vars[i].name, env_vars[i].initial);
		}
		else
		{
			env_remove(env_vars[i].name);
		}
		free_record(&env_vars[i]);
	}

	env_var_count = 0;
	free(env_vars);
	env_vars = NULL;
}

int
let_variables(const char cmd[])
{
	char name[VAR_NAME_MAX + 1];
	int error;
	char *str_val;
	VariableType type;
	VariableOperation op;

	assert(initialized);

	if(extract_name(&cmd, &type, sizeof(name), name) != 0)
	{
		return 1;
	}

	cmd = skip_whitespace(cmd);

	if(extract_op(&cmd, &op) != 0)
	{
		return -1;
	}

	cmd = skip_whitespace(cmd);

	parsing_result_t result = vle_parser_eval(cmd, /*interactive=*/1);
	if(result.error != PE_NO_ERROR)
	{
		vle_parser_report(&result);
		goto fail;
	}

	if(result.last_position != NULL && result.last_position[0] != '\0')
	{
		vle_tb_append_linef(vle_err, "%s: %s", "Incorrect :let statement",
				"trailing characters");
		goto fail;
	}

	if(!is_valid_op(name, type, op))
	{
		vle_tb_append_line(vle_err, "Wrong variable type for this operation");
		goto fail;
	}

	str_val = var_to_str(result.value);

	error = perform_op(name, type, op, result.value, str_val);
	free(str_val);

	if(error)
	{
		vle_tb_append_line(vle_err, "Failed to perform an operation");
		goto fail;
	}

	var_free(result.value);
	return error;

fail:
	var_free(result.value);
	return -1;
}

/* Extracts name from the string.  Returns zero on success, otherwise non-zero
 * is returned. */
static int
extract_name(const char **in, VariableType *type, size_t buf_len, char buf[])
{
	int error;

	/* Copy variable name. */
	if(**in == '$')
	{
		++*in;
		error = (parse_name(in, ENV_VAR_NAME_FIRST_CHAR, ENV_VAR_NAME_CHARS,
				buf_len, buf) != 0);
		*type = VT_ENVVAR;
	}
	else if((*in)[0] == 'g' && (*in)[1] == ':')
	{
		*in += 2;
		copy_str(buf, buf_len, "g:");
		error = (parse_name(in, ENV_VAR_NAME_FIRST_CHAR, ENV_VAR_NAME_CHARS,
				buf_len - 2, buf + 2) != 0);
		*type = VT_GVAR;
	}
	else if(**in == '&')
	{
		++*in;

		*type = VT_ANY_OPTION;
		if(((*in)[0] == 'l' || (*in)[0] == 'g') && (*in)[1] == ':')
		{
			*type = (*in[0] == 'l') ? VT_LOCAL_OPTION : VT_GLOBAL_OPTION;
			*in += 2;
		}

		error = (parse_name(in, OPT_NAME_FIRST_CHAR, OPT_NAME_CHARS, buf_len,
					buf) != 0);
	}
	else
	{
		/* Currently we support only environment variables and options. */
		vle_tb_append_line(vle_err, "Incorrect variable type");
		return -1;
	}
	if(error)
	{
		vle_tb_append_line(vle_err, "Incorrect variable name");
		return -1;
	}

	/* Test for empty variable name. */
	if(buf[0] == '\0')
	{
		vle_tb_append_linef(vle_err, "%s: %s", "Unsupported variable name",
				"empty name");
		return -1;
	}

	return 0;
}

/* Extracts operation from the string.  Sets operation advancing the
 * pointer if needed.  Returns zero on success, otherwise non-zero returned. */
static int
extract_op(const char **in, VariableOperation *vo)
{
	/* Check first operator char and skip it. */
	if(**in == '.')
	{
		++*in;
		*vo = VO_APPEND;
	}
	else if(**in == '+')
	{
		*vo = VO_ADD;
		++*in;
	}
	else if(**in == '-')
	{
		*vo = VO_SUB;
		++*in;
	}
	else
	{
		*vo = VO_ASSIGN;
	}

	/* Check for equal sign and skip it. */
	if(**in != '=')
	{
		vle_tb_append_linef(vle_err, "%s: '=' expected at %s",
				"Incorrect :let statement", *in);
		return 1;
	}
	++*in;

	return 0;
}

/* Parses name of the form `first { other }`.  Returns zero on success,
 * otherwise non-zero is returned. */
static int
parse_name(const char **in, const char first[], const char other[],
		size_t buf_len, char buf[])
{
	if(buf_len == 0UL || !char_is_one_of(first, **in))
	{
		return 1;
	}

	buf[0] = '\0';

	do
	{
		strcatch(buf, **in);
		++*in;
	}
	while(--buf_len > 1UL && char_is_one_of(other, **in));

	return 0;
}

/* Validates operation on a specific variable type.  Returns non-zero for valid
 * operation, otherwise zero is returned. */
static int
is_valid_op(const char name[], VariableType vt, VariableOperation vo)
{
	opt_t *opt;

	if(vt == VT_ENVVAR)
	{
		return (vo == VO_ASSIGN || vo == VO_APPEND);
	}

	if(vt == VT_GVAR)
	{
		var_info_t *var = find_internal_var(name, /*create=*/0);
		if(var == NULL)
		{
			/* Let this error be handled somewhere else. */
			return (vo == VO_ASSIGN);
		}

		switch(var->val.type)
		{
			case VTYPE_ERROR:
				/* Unreachable. */
				return 0;
			case VTYPE_STRING:
				return ONE_OF(vo, VO_ASSIGN, VO_APPEND);
			case VTYPE_INT:
				return ONE_OF(vo, VO_ASSIGN, VO_ADD, VO_SUB);
		}
		/* Unreachable. */
		return 0;
	}

	opt = vle_opts_find(name, OPT_GLOBAL);
	if(opt == NULL)
	{
		/* Let this error be handled somewhere else. */
		return 1;
	}

	if(opt->type == OPT_BOOL)
	{
		return 0;
	}

	if(opt->type == OPT_STR)
	{
		return (vo == VO_ASSIGN || vo == VO_APPEND);
	}

	return (vo == VO_ASSIGN || vo == VO_ADD || vo == VO_SUB);
}

/* Performs operation on a value.  Returns zero on success, otherwise non-zero
 * is returned. */
static int
perform_op(const char name[], VariableType vt, VariableOperation vo, var_t val,
		const char value[])
{
	if(vt == VT_ENVVAR)
	{
		/* Update environment variable. */
		if(vo == VO_APPEND)
		{
			append_envvar(name, value);
		}
		else
		{
			set_envvar(name, value);
		}
		return 0;
	}

	if(vt == VT_GVAR)
	{
		var_info_t *var = find_internal_var(name, vo == VO_ASSIGN);
		if(var == NULL)
		{
			/* Must have failed to allocate new variable. */
			return 0;
		}

		switch(vo)
		{
			int a, b;

			case VO_ASSIGN:
				var_free(var->val);
				var->val = var_clone(val);
				return 0;
			case VO_ADD:
			case VO_SUB:
				a = var_to_int(var->val);
				b = var_to_int(val);
				var_free(var->val);
				var->val = var_from_int(vo == VO_ADD ? a + b : a - b);
				return 0;
			case VO_APPEND:
				return append_internal_var(var, val);
		}
		/* Unreachable. */
		return 1;
	}

	/* Update an option. */

	if(vt == VT_ANY_OPTION || vt == VT_LOCAL_OPTION)
	{
		if(perform_opt_op(name, vt, vo, value) != 0)
		{
			return 1;
		}
	}

	if(vt == VT_ANY_OPTION || vt == VT_GLOBAL_OPTION)
	{
		if(perform_opt_op(name, VT_GLOBAL_OPTION, vo, value) != 0)
		{
			return 1;
		}
	}

	return 0;
}

static void
append_envvar(const char *name, const char *val)
{
	envvar_t *record;
	char *p;

	record = find_record(name);
	if(record == NULL)
	{
		set_envvar(name, val);
		return;
	}

	p = realloc(record->val, strlen(record->val) + strlen(val) + 1);
	if(p == NULL)
	{
		vle_tb_append_line(vle_err, "Not enough memory");
		return;
	}
	record->val = p;

	strcat(record->val, val);
	env_set(name, record->val);
}

static void
set_envvar(const char *name, const char *val)
{
	envvar_t *record;
	char *p;

	record = get_record(name);
	if(record == NULL)
	{
		vle_tb_append_line(vle_err, "Not enough memory");
		return;
	}

	p = strdup(val);
	if(p == NULL)
	{
		vle_tb_append_line(vle_err, "Not enough memory");
		return;
	}
	free(record->val);
	record->val = p;
	env_set(name, val);
}

/* Appends two variables as strings, updating LHS in place.  Returns zero on
 * success. */
static int
append_internal_var(var_info_t *var, var_t val)
{
	char *current = var_to_str(var->val);
	char *suffix = var_to_str(val);

	if(current == NULL || suffix == NULL)
	{
		goto oom;
	}

	char *final = format_str("%s%s", current, suffix);
	if(final == NULL)
	{
		goto oom;
	}

	var_free(var->val);
	var->val = var_out_of_str(final);

	free(current);
	free(suffix);
	return 0;

oom:
	vle_tb_append_line(vle_err, "Not enough memory");
	free(current);
	free(suffix);
	return 1;
}

/* Performs operation on an option.  Returns zero on success, otherwise non-zero
 * is returned. */
static int
perform_opt_op(const char name[], VariableType vt, VariableOperation vo,
		const char value[])
{
	OPT_SCOPE scope = (vt == VT_ANY_OPTION || vt == VT_LOCAL_OPTION)
	                ? OPT_LOCAL
	                : OPT_GLOBAL;

	opt_t *const opt = vle_opts_find(name, scope);
	if(opt == NULL)
	{
		if(vt == VT_ANY_OPTION)
		{
			return 0;
		}

		vle_tb_append_linef(vle_err, "Unknown %s option name: %s",
				(scope == OPT_LOCAL) ? "local" : "global", name);
		return 1;
	}

	switch(vo)
	{
		case VO_ASSIGN:
			if(vle_opt_assign(opt, value) != 0)
			{
				return 1;
			}
			break;
		case VO_ADD:
		case VO_APPEND:
			if(vle_opt_add(opt, value) != 0)
			{
				return 1;
			}
			break;
		case VO_SUB:
			if(vle_opt_remove(opt, value) != 0)
			{
				return 1;
			}
			break;
	}
	return 0;
}

/* Searches for variable and creates new record if it doesn't exist yet.
 * Returns a record or NULL on error.*/
static envvar_t *
get_record(const char *name)
{
	envvar_t *p = NULL;
	size_t i;

	/* Search for existing variable. */
	for(i = 0U; i < env_var_count; ++i)
	{
		if(env_vars[i].name == NULL)
			p = &env_vars[i];
		else if(strcmp(env_vars[i].name, name) == 0)
			return &env_vars[i];
	}

	if(p == NULL)
	{
		/* Try to reallocate list of variables. */
		p = realloc(env_vars, sizeof(*env_vars)*(env_var_count + 1U));
		if(p == NULL)
			return NULL;
		env_vars = p;
		p = &env_vars[env_var_count];
		++env_var_count;
	}

	/* Initialize new record. */
	p->initial = strdup("");
	p->name = strdup(name);
	p->val = strdup("");
	p->from_parent = 0;
	p->removed = 0;
	if(p->initial == NULL || p->name == NULL || p->val == NULL)
	{
		free_record(p);
		return NULL;
	}
	return p;
}

int
unlet_variables(const char cmd[])
{
	int error = 0;
	assert(initialized);

	while(*cmd != '\0')
	{
		/* Options can't be removed, use this to mean "no value". */
		VariableType type = VT_ANY_OPTION;

		char name[VAR_NAME_MAX + 1];
		char *p = name;

		/* Check if it's environment variable. */
		if(*cmd == '$')
		{
			type = VT_ENVVAR;
			cmd++;
		}
		else if(starts_with_lit(cmd, "g:"))
		{
			type = VT_GVAR;
			*p++ = *cmd++;
			*p++ = *cmd++;
		}
		else if(starts_with_lit(cmd, "v:"))
		{
			/* Parse the name, but don't set the type to get a sensible error message
			 * about unsupported variable type. */
			*p++ = *cmd++;
			*p++ = *cmd++;
		}

		/* Copy variable name. */
		while(*cmd != '\0' && char_is_one_of(ENV_VAR_NAME_CHARS, *cmd) &&
				(size_t)(p - name) < sizeof(name) - 1)
		{
			*p++ = *cmd++;
		}
		*p = '\0';

		if(*cmd != '\0' && !isspace(*cmd))
		{
			vle_tb_append_line(vle_err, "Trailing characters");
			error++;
			break;
		}

		cmd = skip_whitespace(cmd);

		if(type == VT_ANY_OPTION)
		{
			vle_tb_append_linef(vle_err, "%s: %s", "Unsupported variable type", name);

			cmd = skip_non_whitespace(cmd);
			error++;
			continue;
		}

		/* Test for empty variable name. */
		if(name[0] == '\0')
		{
			vle_tb_append_linef(vle_err, "%s: %s", "Unsupported variable name",
					"empty name");
			error++;
			continue;
		}

		if(type == VT_ENVVAR)
		{
			envvar_t *record = find_record(name);
			if(record == NULL || record->removed)
			{
				vle_tb_append_linef(vle_err, "%s: %s", "No such variable", name);
				error++;
				continue;
			}

			if(record->from_parent)
				record->removed = 1;
			else
				free_record(record);
			env_remove(name);
		}
		else if(type == VT_GVAR)
		{
			var_info_t *var = find_internal_var(name, /*create=*/0);
			if(var == NULL)
			{
				vle_tb_append_linef(vle_err, "%s: %s", "No such variable", name);
				error++;
				continue;
			}

			free(var->name);
			var_free(var->val);
			*var = internal_vars[internal_var_count - 1];
			--internal_var_count;
		}
	}

	return error;
}

/* Skips consecutive non-whitespace characters.  Returns pointer to the next
 * character in the str. */
static char *
skip_non_whitespace(const char str[])
{
	while(!isspace(*str) && *str != '\0')
	{
		++str;
	}
	return (char *)str;
}

/* searches for existent variable */
static envvar_t *
find_record(const char *name)
{
	size_t i;
	for(i = 0U; i < env_var_count; ++i)
	{
		if(env_vars[i].name != NULL && stroscmp(env_vars[i].name, name) == 0)
			return &env_vars[i];
	}
	return NULL;
}

static void
free_record(envvar_t *record)
{
	free(record->initial);
	free(record->name);
	free(record->val);

	clear_record(record);
}

static void
clear_record(envvar_t *record)
{
	record->initial = NULL;
	record->name = NULL;
	record->val = NULL;
}

void
complete_variables(const char var[], const char **start)
{
	assert(initialized && "Variables unit is not initialized.");

	if(var[0] == '$')
	{
		complete_envvars(var, start);
	}
	else if(ONE_OF(var[0], 'v', 'g') && var[1] == ':')
	{
		complete_internal_vars(var, start);
	}
	else
	{
		*start = var;
		vle_compl_add_match(var, "");
	}
}

/* Completes environment variable name.  var should point to "$...".  *start is
 * set to completion insertion position in var. */
static void
complete_envvars(const char var[], const char **start)
{
	size_t i;
	size_t len;

	++var;
	*start = var;

	/* Add all variables that start with given beginning. */
	len = strlen(var);
	for(i = 0U; i < env_var_count; ++i)
	{
		if(env_vars[i].name == NULL)
			continue;
		if(env_vars[i].removed)
			continue;
		if(strnoscmp(env_vars[i].name, var, len) == 0)
			vle_compl_add_match(env_vars[i].name, env_vars[i].val);
	}
	vle_compl_finish_group();
	vle_compl_add_last_match(var);
}

/* Completes a variable name.  var should point to "[gv]:...".  *start is set to
 * completion insertion position in var. */
static void
complete_internal_vars(const char var[], const char **start)
{
	size_t i;
	size_t len;

	*start = var;

	/* Add all variables that start with given beginning. */
	len = strlen(var);
	for(i = 0U; i < internal_var_count; ++i)
	{
		if(strncmp(internal_vars[i].name, var, len) == 0)
		{
			char *const str_val = var_to_str(internal_vars[i].val);
			vle_compl_add_match(internal_vars[i].name, str_val);
			free(str_val);
		}
	}
	vle_compl_finish_group();
	vle_compl_add_last_match(var);
}

var_t
getvar(const char varname[])
{
	var_info_t *var = find_internal_var(varname, /*create=*/0);
	if(var == NULL)
	{
		return var_error();
	}

	return var->val;
}

int
setvar(const char varname[], var_t val)
{
	var_info_t new_var;
	void *p;

	if(!starts_with_lit(varname, "v:"))
	{
		return 1;
	}

	/* Initialize new variable before doing anything else. */
	new_var.name = strdup(varname);
	new_var.val = var_clone(val);
	if(new_var.name == NULL || new_var.val.type == VTYPE_ERROR)
	{
		free(new_var.name);
		var_free(new_var.val);
		return 1;
	}

	/* Try updating an existing variable. */
	var_info_t *var = find_internal_var(varname, /*create=*/0);
	if(var != NULL)
	{
		free(var->name);
		var_free(var->val);
		*var = new_var;
		return 0;
	}

	/* Try to reallocate list of variables. */
	p = realloc(internal_vars, sizeof(*internal_vars)*(internal_var_count + 1U));
	if(p == NULL)
	{
		free(new_var.name);
		var_free(new_var.val);
		return 1;
	}
	internal_vars = p;
	internal_vars[internal_var_count] = new_var;
	++internal_var_count;

	return 0;
}

/* Search for an existing variable by its name and optionally create it if
 * missing.  Returns NULL on unsuccessful search. */
static var_info_t *
find_internal_var(const char varname[], int create)
{
	size_t i;
	for(i = 0U; i < internal_var_count; ++i)
	{
		if(strcmp(internal_vars[i].name, varname) == 0)
		{
			return &internal_vars[i];
		}
	}

	if(!create)
	{
		return NULL;
	}

	void *p =
		realloc(internal_vars, sizeof(*internal_vars)*(internal_var_count + 1U));
	if(p == NULL)
	{
		return NULL;
	}

	internal_vars = p;

	internal_vars[internal_var_count].name = strdup(varname);
	if(internal_vars[internal_var_count].name == NULL)
	{
		return NULL;
	}

	internal_vars[internal_var_count].val = var_error();
	return &internal_vars[internal_var_count++];
}

/* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
/* vim: set cinoptions+=t0 filetype=c : */
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/vifm

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/vifm

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master