/*
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  Support for the verb/device/modifier core logic and API,
 *  command line tool and file parser was kindly sponsored by
 *  Texas Instruments Inc.
 *  Support for multiple active modifiers and devices,
 *  transition sequences, multiple client access and user defined use
 *  cases was kindly sponsored by Wolfson Microelectronics PLC.
 *
 *  Copyright (C) 2019 Red Hat Inc.
 *  Authors: Jaroslav Kysela <perex@perex.cz>
 */

#include "ucm_local.h"
#include <regex.h>

static int get_string(snd_config_t *compound, const char *key, const char **str)
{
	snd_config_t *node;
	int err;

	err = snd_config_search(compound, key, &node);
	if (err < 0)
		return err;
	return snd_config_get_string(node, str);
}

typedef int (*string_compare_t)(const char *s1, const char *s2);

static int compare_strings(snd_use_case_mgr_t *uc_mgr, snd_config_t *eval,
			   const char *key1, const char *key2,
			   string_compare_t compare)
{
	const char *string1 = NULL, *string2 = NULL;
	char *s1, *s2;
	int err, result;

	err = get_string(eval, key1, &string1);
	if (err < 0 && err != -ENOENT) {
		snd_error(UCM, "String error (If.Condition.%s)", key1);
		return -EINVAL;
	}

	err = get_string(eval, key2, &string2);
	if (err < 0 && err != -ENOENT) {
		snd_error(UCM, "String error (If.Condition.%s)", key2);
		return -EINVAL;
	}

	if (!string1 && !string2)
		return -ENOENT; /* not found */

	if (!string1) {
		snd_error(UCM, "If.Condition.%s not defined", key1);
		return -EINVAL;
	}
	if (!string2) {
		snd_error(UCM, "If.Condition.%s not defined", key2);
		return -EINVAL;
	}

	err = uc_mgr_get_substituted_value(uc_mgr, &s1, string1);
	if (err < 0)
		return err;

	err = uc_mgr_get_substituted_value(uc_mgr, &s2, string2);
	if (err < 0) {
		free(s1);
		return err;
	}

	result = compare(s1, s2);
	free(s2);
	free(s1);
	return result;
}

static int string_equal(const char *s1, const char *s2)
{
	return strcasecmp(s1, s2) == 0;
}

static int string_contains(const char *s1, const char *s2)
{
	return strstr(s1, s2) != NULL;
}

static int if_eval_string(snd_use_case_mgr_t *uc_mgr, snd_config_t *eval)
{
	const char *string1 = NULL;
	char *s1;
	int err;

	if (uc_mgr->conf_format >= 3) {
		err = get_string(eval, "Empty", &string1);
		if (err < 0 && err != -ENOENT) {
			snd_error(UCM, "String error (If.Condition.Empty)");
			return -EINVAL;
		}

		if (string1) {
			err = uc_mgr_get_substituted_value(uc_mgr, &s1, string1);
			if (err < 0)
				return err;
			err = s1 == NULL || s1[0] == '\0';
			free(s1);
			return err;
		}
	}

	err = compare_strings(uc_mgr, eval, "String1", "String2", string_equal);
	if (err != -ENOENT) /* -ENOENT means not found, continue checking */
		return err;

	err = compare_strings(uc_mgr, eval, "Haystack", "Needle", string_contains);
	if (err != -ENOENT)
		return err;

	snd_error(UCM, "Unknown String condition arguments");
	return -EINVAL;
}

static int if_eval_regex_match(snd_use_case_mgr_t *uc_mgr, snd_config_t *eval)
{
	const char *string, *regex_string;
	char *s;
	regex_t re;
	int options = REG_EXTENDED | REG_ICASE;
	regmatch_t match[1];
	int err;

	err = get_string(eval, "String", &string);
	if (err < 0) {
		snd_error(UCM, "RegexMatch error (If.Condition.String)");
		return -EINVAL;
	}

	err = get_string(eval, "Regex", &regex_string);
	if (err < 0) {
		snd_error(UCM, "RegexMatch error (If.Condition.Regex)");
		return -EINVAL;
	}

	err = uc_mgr_get_substituted_value(uc_mgr, &s, regex_string);
	if (err < 0)
		return err;
	err = regcomp(&re, s, options);
	if (err) {
		snd_error(UCM, "Regex '%s' compilation failed (code %d)", s, err);
		free(s);
		return -EINVAL;
	}
	free(s);

	err = uc_mgr_get_substituted_value(uc_mgr, &s, string);
	if (err < 0) {
		regfree(&re);
		return err;
	}
	err = regexec(&re, s, ARRAY_SIZE(match), match, 0);
	free(s);
	regfree(&re);
	return err == 0;
}

static int if_eval_control_exists(snd_use_case_mgr_t *uc_mgr, snd_config_t *eval)
{
	snd_ctl_t *ctl;
	struct ctl_list *ctl_list;
	const char *device = NULL, *ctldef, *enumval = NULL, *name;
	snd_ctl_elem_id_t *elem_id;
	snd_ctl_elem_info_t *elem_info;
	snd_ctl_elem_type_t type;
	char *s;
	int err, i, items;

	snd_ctl_elem_id_alloca(&elem_id);
	snd_ctl_elem_info_alloca(&elem_info);

	err = get_string(eval, "Device", &device);
	if (err < 0 && err != -ENOENT) {
		snd_error(UCM, "ControlExists error (If.Condition.Device)");
		return -EINVAL;
	}

	err = get_string(eval, "Control", &ctldef);
	if (err < 0) {
		snd_error(UCM, "ControlExists error (If.Condition.Control)");
		return -EINVAL;
	}

	err = get_string(eval, "ControlEnum", &enumval);
	if (err < 0 && err != -ENOENT) {
		snd_error(UCM, "ControlExists error (If.Condition.ControlEnum)");
		return -EINVAL;
	}

	err = uc_mgr_get_substituted_value(uc_mgr, &s, ctldef);
	if (err < 0)
		return err;
	err = snd_ctl_ascii_elem_id_parse(elem_id, s);
	free(s);
	if (err < 0) {
		snd_error(UCM, "unable to parse element identificator (%s)", ctldef);
		return -EINVAL;
	}

	if (device == NULL) {
		ctl = uc_mgr_get_ctl(uc_mgr);
		if (ctl == NULL) {
			snd_error(UCM, "cannot determine control device");
			return -EINVAL;
		}
	} else {
		err = uc_mgr_get_substituted_value(uc_mgr, &s, device);
		if (err < 0)
			return err;
		err = uc_mgr_open_ctl(uc_mgr, &ctl_list, s, 1);
		free(s);
		if (err < 0)
			return err;
		ctl = ctl_list->ctl;
	}

	snd_ctl_elem_info_set_id(elem_info, elem_id);
	err = snd_ctl_elem_info(ctl, elem_info);
	if (err < 0)
		return 0;

	if (enumval) {
		type = snd_ctl_elem_info_get_type(elem_info);
		if (type != SND_CTL_ELEM_TYPE_ENUMERATED)
			return 0;
		err = uc_mgr_get_substituted_value(uc_mgr, &s, enumval);
		if (err < 0)
			return err;
		items = snd_ctl_elem_info_get_items(elem_info);
		for (i = 0; i < items; i++) {
			snd_ctl_elem_info_set_item(elem_info, i);
			err = snd_ctl_elem_info(ctl, elem_info);
			if (err < 0) {
				free(s);
				return err;
			}
			name = snd_ctl_elem_info_get_item_name(elem_info);
			if (strcasecmp(name, s) == 0) {
				free(s);
				return 1;
			}
		}
		free(s);
		return 0;
	}

	return 1;
}

static int if_eval_integer(snd_use_case_mgr_t *uc_mgr, snd_config_t *eval)
{
	const char *value1_str = NULL, *value2_str = NULL, *operation = NULL;
	char *s1, *s2;
	long long val1, val2;
	int err, err1, err2;

	if (uc_mgr->conf_format < 9) {
		snd_error(UCM, "Integer condition is supported in v9+ syntax");
		return -EINVAL;
	}

	err = get_string(eval, "Operation", &operation);
	if (err < 0) {
		snd_error(UCM, "Integer error (If.Condition.Operation)");
		return -EINVAL;
	}

	err = get_string(eval, "Value1", &value1_str);
	if (err < 0) {
		snd_error(UCM, "Integer error (If.Condition.Value1)");
		return -EINVAL;
	}

	err = get_string(eval, "Value2", &value2_str);
	if (err < 0) {
		snd_error(UCM, "Integer error (If.Condition.Value2)");
		return -EINVAL;
	}

	err = uc_mgr_get_substituted_value(uc_mgr, &s1, value1_str);
	if (err < 0)
		return err;

	err = uc_mgr_get_substituted_value(uc_mgr, &s2, value2_str);
	if (err < 0) {
		free(s1);
		return err;
	}

	err1 = safe_strtoll(s1, &val1);
	err2 = safe_strtoll(s2, &val2);

	if (err1 < 0 || err2 < 0) {
		if (err1 < 0)
			snd_error(UCM, "Integer conversion error for Value1 '%s'", s1);
		if (err2 < 0)
			snd_error(UCM, "Integer conversion error for Value2 '%s'", s2);
		free(s2);
		free(s1);
		return -EINVAL;
	}

	free(s2);
	free(s1);

	if (strcmp(operation, "==") == 0) {
		return val1 == val2;
	} else if (strcmp(operation, "!=") == 0) {
		return val1 != val2;
	} else if (strcmp(operation, "<") == 0) {
		return val1 < val2;
	} else if (strcmp(operation, ">") == 0) {
		return val1 > val2;
	} else if (strcmp(operation, "<=") == 0) {
		return val1 <= val2;
	} else if (strcmp(operation, ">=") == 0) {
		return val1 >= val2;
	} else {
		snd_error(UCM, "Integer unknown operation '%s'", operation);
		return -EINVAL;
	}
}

static int if_eval_path(snd_use_case_mgr_t *uc_mgr, snd_config_t *eval)
{
	const char *path, *mode = "";
	int err, amode = F_OK;
	char *s;

	if (uc_mgr->conf_format < 4) {
		snd_error(UCM, "Path condition is supported in v4+ syntax");
		return -EINVAL;
	}

	err = get_string(eval, "Path", &path);
	if (err < 0) {
		snd_error(UCM, "Path error (If.Condition.Path)");
		return -EINVAL;
	}

	err = get_string(eval, "Mode", &mode);
	if (err < 0 && err != -ENOENT) {
		snd_error(UCM, "Path error (If.Condition.Mode)");
		return -EINVAL;
	}

	if (uc_mgr->conf_format < 7) {
		s = (char *)mode;
	} else {
		err = uc_mgr_get_substituted_value(uc_mgr, &s, mode);
		if (err < 0)
			return err;
	}
	if (strncasecmp(s, "exist", 5) == 0) {
		amode = F_OK;
	} else if (strcasecmp(s, "read") == 0) {
		amode = R_OK;
	} else if (strcasecmp(s, "write") == 0) {
		amode = W_OK;
	} else if (strcasecmp(s, "exec") == 0) {
		amode = X_OK;
	} else {
		snd_error(UCM, "Path unknown mode '%s' (If.Condition.Mode)", s);
		if (s != mode)
			free(s);
		return -EINVAL;
	}
	if (s != mode)
		free(s);

	if (uc_mgr->conf_format < 7) {
		s = (char *)path;
	} else {
		err = uc_mgr_get_substituted_value(uc_mgr, &s, path);
		if (err < 0)
			return err;
	}
#ifdef HAVE_EACCESS
	err = eaccess(s, amode);
#else
	err = access(s, amode);
#endif
	if (s != path)
		free(s);
	return err ? 0 : 1;
}

static int if_eval(snd_use_case_mgr_t *uc_mgr, snd_config_t *eval)
{
	const char *type;
	int err;

	if (snd_config_get_type(eval) != SND_CONFIG_TYPE_COMPOUND) {
		snd_error(UCM, "compound type expected for If.Condition");
		return -EINVAL;
	}

	err = get_string(eval, "Type", &type);
	if (err < 0) {
		snd_error(UCM, "type block error (If.Condition)");
		return -EINVAL;
	}

	if (strcmp(type, "AlwaysTrue") == 0)
		return 1;

	if (strcmp(type, "String") == 0)
		return if_eval_string(uc_mgr, eval);

	if (strcmp(type, "ControlExists") == 0)
		return if_eval_control_exists(uc_mgr, eval);

	if (strcmp(type, "RegexMatch") == 0)
		return if_eval_regex_match(uc_mgr, eval);

	if (strcmp(type, "Path") == 0)
		return if_eval_path(uc_mgr, eval);

	if (strcmp(type, "Integer") == 0)
		return if_eval_integer(uc_mgr, eval);

	snd_error(UCM, "unknown If.Condition.Type");
	return -EINVAL;
}

static int if_eval_one(snd_use_case_mgr_t *uc_mgr,
		       snd_config_t *cond,
		       snd_config_t **result,
		       snd_config_t **before,
		       snd_config_t **after,
		       snd_config_t **prepend,
		       snd_config_t **append)
{
	snd_config_t *expr, *expr_eval = NULL, *_true = NULL, *_false = NULL;
	const char *s;
	char *s1;
	int err, has_condition;

	*result = NULL;
	*prepend = NULL;
	*append = NULL;

	if (snd_config_get_type(cond) != SND_CONFIG_TYPE_COMPOUND) {
		snd_error(UCM, "compound type expected for If.1");
		return -EINVAL;
	}

	/* For syntax v8+, Condition is optional if Prepend or Append is present */
	has_condition = snd_config_search(cond, "Condition", &expr) >= 0;

	if (has_condition && uc_mgr->conf_format >= 9 &&
	    snd_config_get_type(expr) == SND_CONFIG_TYPE_STRING) {
		err = snd_config_get_string(expr, &s);
		if (err < 0) {
			snd_error(UCM, "Condition string error (If)");
			return -EINVAL;
		}
		err = uc_mgr_get_substituted_value(uc_mgr, &s1, s);
		if (err >= 0) {
			err = snd_config_load_string(&expr_eval, s1, 0);
			free(s1);
		}
		if (err < 0) {
			snd_error(UCM, "Condition string parse error (If)");
			return err;
		}
		expr = expr_eval;
	}

	if (uc_mgr->conf_format >= 8) {
		/* Check for Prepend block */
		err = snd_config_search(cond, "Prepend", prepend);
		if (err < 0 && err != -ENOENT) {
			snd_error(UCM, "prepend block error (If)");
			goto __error;
		}

		/* Check for Append block */
		err = snd_config_search(cond, "Append", append);
		if (err < 0 && err != -ENOENT) {
			snd_error(UCM, "append block error (If)");
			goto __error;
		}

		/* If Prepend or Append is present, Condition can be omitted */
		if (!has_condition && (*prepend == NULL && *append == NULL)) {
			snd_error(UCM, "condition block expected (If)");
			goto __error;
		}
	} else {
		if (!has_condition) {
			snd_error(UCM, "condition block expected (If)");
			goto __error;
		}
	}

	err = snd_config_search(cond, "True", &_true);
	if (err < 0 && err != -ENOENT) {
		snd_error(UCM, "true block error (If)");
		goto __error;
	}

	err = snd_config_search(cond, "False", &_false);
	if (err < 0 && err != -ENOENT) {
		snd_error(UCM, "false block error (If)");
		goto __error;
	}

	err = snd_config_search(cond, "Before", before);
	if (err < 0 && err != -ENOENT) {
		snd_error(UCM, "before block identifier error");
		goto __error;
	}

	err = snd_config_search(cond, "After", after);
	if (err < 0 && err != -ENOENT) {
		snd_error(UCM, "before block identifier error");
		goto __error;
	}

	/* Evaluate condition if present */
	if (has_condition) {
		err = if_eval(uc_mgr, expr);
		if (err < 0)
			goto __error;
		if (err > 0) {
			*result = _true;
			goto __return;
		} else if (err == 0) {
			*result = _false;
			goto __return;
		}
	}

	/* If no condition (v8+ with Prepend/Append only), no result block */
	return 0;

__error:
	err = -EINVAL;
__return:
	if (expr_eval)
		snd_config_delete(expr_eval);
	return err;
}

#if 0
static void config_dump(snd_config_t *cfg)
{
	snd_output_t *out;
	snd_output_stdio_attach(&out, stderr, 0);
	snd_output_printf(out, "-----\n");
	snd_config_save(cfg, out);
	snd_output_close(out);
}
#endif

/*
 * put back the result from all conditions to the parent
 */
int uc_mgr_evaluate_condition(snd_use_case_mgr_t *uc_mgr,
			      snd_config_t *parent,
			      snd_config_t *cond)
{
	snd_config_iterator_t i, next;
	snd_config_t *a, *n, *before, *after, *prepend, *append;
	int err;

	if (uc_mgr->conf_format < 2) {
		snd_error(UCM, "conditions are not supported for v1 syntax");
		return -EINVAL;
	}

	if (snd_config_get_type(cond) != SND_CONFIG_TYPE_COMPOUND) {
		snd_error(UCM, "compound type expected for If");
		return -EINVAL;
	}

	snd_config_for_each(i, next, cond) {
		n = snd_config_iterator_entry(i);
		before = after = prepend = append = NULL;
		err = if_eval_one(uc_mgr, n, &a, &before, &after, &prepend, &append);
		if (err < 0)
			return err;

		/* For v8+: Handle Prepend block - prepend to parent before result */
		if (prepend != NULL) {
			err = uc_mgr_evaluate_inplace(uc_mgr, prepend);
			if (err < 0)
				return err;
			err = uc_mgr_config_tree_merge(uc_mgr, parent, prepend, before, after);
			if (err < 0)
				return err;
			snd_config_delete(prepend);
		}

		/* Merge the condition result (True or False block) */
		if (a != NULL) {
			err = uc_mgr_evaluate_inplace(uc_mgr, a);
			if (err < 0)
				return err;
			err = uc_mgr_config_tree_merge(uc_mgr, parent, a, before, after);
			if (err < 0)
				return err;
			snd_config_delete(a);
		}

		/* For v8+: Handle Append block - append to parent after result */
		if (append != NULL) {
			err = uc_mgr_evaluate_inplace(uc_mgr, append);
			if (err < 0)
				return err;
			err = uc_mgr_config_tree_merge(uc_mgr, parent, append, before, after);
			if (err < 0)
				return err;
			snd_config_delete(append);
		}
	}
	return 0;
}
