示例#1
0
int
let_variable(const char *cmd)
{
	char name[VAR_NAME_MAX + 1];
	char *p;
	int append = 0;
	var_t res_var;
	char *str_var;
	ParsingErrors parsing_error;

	assert(initialized);

	/* currently we support only environment variables */
	if(*cmd != '$')
	{
		text_buffer_add("Incorrect variable type");
		return -1;
	}
	cmd++;

	/* copy variable name */
	p = name;
	while(*cmd != '\0' && char_is_one_of(ENV_VAR_NAME_CHARS, *cmd) &&
			*cmd != '.' && *cmd != '=' && p - name < sizeof(name) - 1)
	{
		if(*cmd != '_' && !isalnum(*cmd))
		{
			text_buffer_add("Incorrect variable name");
			return -1;
		}
		*p++ = *cmd++;
	}
	/* test for empty variable name */
	if(p == name)
	{
		text_buffer_addf("%s: %s", "Unsupported variable name", "empty name");
		return -1;
	}
	*p = '\0';

	cmd = skip_whitespace(cmd);

	/* check for dot and skip it */
	if(*cmd == '.')
	{
		append = 1;
		cmd++;
	}

	/* check for equal sign and skip it */
	if(*cmd != '=')
	{
		text_buffer_addf("%s: %s", "Incorrect :let statement", "'=' expected");
		return -1;
	}

	parsing_error = parse(cmd + 1, &res_var);
	if(parsing_error != PE_NO_ERROR)
	{
		report_parsing_error(parsing_error);
		return -1;
	}

	if(get_last_position() != NULL && *get_last_position() != '\0')
	{
		text_buffer_addf("%s: %s", "Incorrect :let statement",
				"trailing characters");
		return -1;
	}

	/* update environment variable */
	str_var = var_to_string(res_var);
	if(append)
		append_envvar(name, str_var);
	else
		set_envvar(name, str_var);
	free(str_var);

	var_free(res_var);

	return 0;
}
示例#2
0
文件: variables.c 项目: sklnd/vifm
int
let_variable(const char *cmd)
{
	char name[VAR_NAME_MAX + 1];
	char *p;
	const char *cp;
	int append = 0;

	assert(initialized);

	/* currently we support only environment variables */
	if(*cmd != '$')
	{
		print_msg(1, "", "Incorrect variable type");
		return -1;
	}
	cmd++;

	/* copy variable name */
	p = name;
	while(*cmd != '\0' && !isspace(*cmd) && *cmd != '.' && *cmd != '=' &&
			p - name < sizeof(name) - 1)
	{
		if(*cmd != '_' && !isalnum(*cmd))
		{
			print_msg(1, "", "Incorrect variable name");
			return -1;
		}
		*p++ = *cmd++;
	}
	/* test for empty variable name */
	if(p == name)
	{
		print_msg(1, "Unsupported variable name", "empty name");
		return -1;
	}
	*p = '\0';

	/* skip spaces */
	while(isspace(*cmd))
		cmd++;

	/* check for dot and skip it */
	if(*cmd == '.')
	{
		append = 1;
		cmd++;
	}

	/* check for equal sign and skip it and all whitespace */
	if(*cmd != '=')
	{
		print_msg(1, "Incorrect :let statement", "'=' expected");
		return -1;
	}
	cmd++;
	while(isspace(*cmd))
		cmd++;

	/* ensure value starts with quotes */
	if(*cmd != '\'' && *cmd != '"')
	{
		print_msg(1, "Incorrect :let statement", "expected single or double quote");
		return -1;
	}

	/* parse value */
	cp = parse_val(cmd);
	if(cp == NULL)
		return -1;

	/* update environment variable */
	if(append)
		append_envvar(name, cp);
	else
		set_envvar(name, cp);

	return 0;
}