Blame src/ucm/ucm_subs.c

Packit 4a16fb
/*
Packit 4a16fb
 *  This library is free software; you can redistribute it and/or
Packit 4a16fb
 *  modify it under the terms of the GNU Lesser General Public
Packit 4a16fb
 *  License as published by the Free Software Foundation; either
Packit 4a16fb
 *  version 2 of the License, or (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *  This library is distributed in the hope that it will be useful,
Packit 4a16fb
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 4a16fb
 *  Lesser General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *  You should have received a copy of the GNU Lesser General Public
Packit 4a16fb
 *  License along with this library; if not, write to the Free Software
Packit 4a16fb
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 *
Packit 4a16fb
 *  Support for the verb/device/modifier core logic and API,
Packit 4a16fb
 *  command line tool and file parser was kindly sponsored by
Packit 4a16fb
 *  Texas Instruments Inc.
Packit 4a16fb
 *  Support for multiple active modifiers and devices,
Packit 4a16fb
 *  transition sequences, multiple client access and user defined use
Packit 4a16fb
 *  cases was kindly sponsored by Wolfson Microelectronics PLC.
Packit 4a16fb
 *
Packit 4a16fb
 *  Copyright (C) 2019 Red Hat Inc.
Packit 4a16fb
 *  Authors: Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
#include "ucm_local.h"
Packit 4a16fb
#include <sys/stat.h>
Packit 4a16fb
#include <limits.h>
Packit 4a16fb
Packit 4a16fb
static char *rval_conf_name(snd_use_case_mgr_t *uc_mgr)
Packit 4a16fb
{
Packit 4a16fb
	if (uc_mgr->conf_file_name[0])
Packit 4a16fb
		return strdup(uc_mgr->conf_file_name);
Packit 4a16fb
	return NULL;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static char *rval_card_id(snd_use_case_mgr_t *uc_mgr)
Packit 4a16fb
{
Packit 4a16fb
	struct ctl_list *ctl_list;
Packit 4a16fb
Packit 4a16fb
	ctl_list = uc_mgr_get_one_ctl(uc_mgr);
Packit 4a16fb
	if (ctl_list == NULL)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	return strdup(snd_ctl_card_info_get_id(ctl_list->ctl_info));
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static char *rval_card_driver(snd_use_case_mgr_t *uc_mgr)
Packit 4a16fb
{
Packit 4a16fb
	struct ctl_list *ctl_list;
Packit 4a16fb
Packit 4a16fb
	ctl_list = uc_mgr_get_one_ctl(uc_mgr);
Packit 4a16fb
	if (ctl_list == NULL)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	return strdup(snd_ctl_card_info_get_driver(ctl_list->ctl_info));
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static char *rval_card_name(snd_use_case_mgr_t *uc_mgr)
Packit 4a16fb
{
Packit 4a16fb
	struct ctl_list *ctl_list;
Packit 4a16fb
Packit 4a16fb
	ctl_list = uc_mgr_get_one_ctl(uc_mgr);
Packit 4a16fb
	if (ctl_list == NULL)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	return strdup(snd_ctl_card_info_get_name(ctl_list->ctl_info));
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static char *rval_card_longname(snd_use_case_mgr_t *uc_mgr)
Packit 4a16fb
{
Packit 4a16fb
	struct ctl_list *ctl_list;
Packit 4a16fb
Packit 4a16fb
	ctl_list = uc_mgr_get_one_ctl(uc_mgr);
Packit 4a16fb
	if (ctl_list == NULL)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	return strdup(snd_ctl_card_info_get_longname(ctl_list->ctl_info));
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static char *rval_card_components(snd_use_case_mgr_t *uc_mgr)
Packit 4a16fb
{
Packit 4a16fb
	struct ctl_list *ctl_list;
Packit 4a16fb
Packit 4a16fb
	ctl_list = uc_mgr_get_one_ctl(uc_mgr);
Packit 4a16fb
	if (ctl_list == NULL)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	return strdup(snd_ctl_card_info_get_components(ctl_list->ctl_info));
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static char *rval_env(snd_use_case_mgr_t *uc_mgr ATTRIBUTE_UNUSED, const char *id)
Packit 4a16fb
{
Packit 4a16fb
	char *e;
Packit 4a16fb
Packit 4a16fb
	e = getenv(id);
Packit 4a16fb
	if (e)
Packit 4a16fb
		return strdup(e);
Packit 4a16fb
	return NULL;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static char *rval_sysfs(snd_use_case_mgr_t *uc_mgr ATTRIBUTE_UNUSED, const char *id)
Packit 4a16fb
{
Packit 4a16fb
	char path[PATH_MAX], link[PATH_MAX + 1];
Packit 4a16fb
	struct stat sb;
Packit 4a16fb
	ssize_t len;
Packit 4a16fb
	char *e;
Packit 4a16fb
	int fd;
Packit 4a16fb
Packit 4a16fb
	e = getenv("SYSFS_PATH");
Packit 4a16fb
	if (e == NULL)
Packit 4a16fb
		e = "/sys";
Packit 4a16fb
	if (id[0] == '/')
Packit 4a16fb
		id++;
Packit 4a16fb
	snprintf(path, sizeof(path), "%s/%s", e, id);
Packit 4a16fb
	if (lstat(path, &sb) != 0)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	if (S_ISLNK(sb.st_mode)) {
Packit 4a16fb
		len = readlink(path, link, sizeof(link) - 1);
Packit 4a16fb
		if (len <= 0) {
Packit 4a16fb
			uc_error("sysfs: cannot read link '%s' (%d)", path, errno);
Packit 4a16fb
			return NULL;
Packit 4a16fb
		}
Packit 4a16fb
		link[len] = '\0';
Packit 4a16fb
		e = strrchr(link, '/');
Packit 4a16fb
		if (e)
Packit 4a16fb
			return strdup(e + 1);
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	if (S_ISDIR(sb.st_mode))
Packit 4a16fb
		return NULL;
Packit 4a16fb
	if ((sb.st_mode & S_IRUSR) == 0)
Packit 4a16fb
		return NULL;
Packit 4a16fb
Packit 4a16fb
	fd = open(path, O_RDONLY);
Packit 4a16fb
	if (fd < 0) {
Packit 4a16fb
		uc_error("sysfs open failed for '%s' (%d)", path, errno);
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	len = read(fd, path, sizeof(path)-1);
Packit 4a16fb
	close(fd);
Packit 4a16fb
	if (len < 0) {
Packit 4a16fb
		uc_error("sysfs unable to read value '%s' (%d)", path, errno);
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	while (len > 0 && path[len-1] == '\n')
Packit 4a16fb
		len--;
Packit 4a16fb
	path[len] = '\0';
Packit 4a16fb
	return strdup(path);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#define MATCH_VARIABLE(name, id, fcn)					\
Packit 4a16fb
	if (strncmp((name), (id), sizeof(id) - 1) == 0) { 		\
Packit 4a16fb
		rval = fcn(uc_mgr);					\
Packit 4a16fb
		idsize = sizeof(id) - 1;				\
Packit 4a16fb
		goto __rval;						\
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
#define MATCH_VARIABLE2(name, id, fcn)					\
Packit 4a16fb
	if (strncmp((name), (id), sizeof(id) - 1) == 0) {		\
Packit 4a16fb
		idsize = sizeof(id) - 1;				\
Packit 4a16fb
		tmp = strchr(value + idsize, '}');			\
Packit 4a16fb
		if (tmp) {						\
Packit 4a16fb
			rvalsize = tmp - (value + idsize);		\
Packit 4a16fb
			if (rvalsize > sizeof(v2)) {			\
Packit 4a16fb
				err = -ENOMEM;				\
Packit 4a16fb
				goto __error;				\
Packit 4a16fb
			}						\
Packit 4a16fb
			strncpy(v2, value + idsize, rvalsize);		\
Packit 4a16fb
			v2[rvalsize] = '\0';				\
Packit 4a16fb
			idsize += rvalsize + 1;				\
Packit 4a16fb
			rval = fcn(uc_mgr, v2);				\
Packit 4a16fb
			goto __rval;					\
Packit 4a16fb
		}							\
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
int uc_mgr_get_substituted_value(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
				 char **_rvalue,
Packit 4a16fb
				 const char *value)
Packit 4a16fb
{
Packit 4a16fb
	size_t size, nsize, idsize, rvalsize, dpos = 0;
Packit 4a16fb
	const char *tmp;
Packit 4a16fb
	char *r, *nr, *rval, v2[32];
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	if (value == NULL)
Packit 4a16fb
		return -ENOENT;
Packit 4a16fb
Packit 4a16fb
	size = strlen(value) + 1;
Packit 4a16fb
	r = malloc(size);
Packit 4a16fb
	if (r == NULL)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
Packit 4a16fb
	while (*value) {
Packit 4a16fb
		if (*value == '$' && *(value+1) == '{') {
Packit 4a16fb
			MATCH_VARIABLE(value, "${ConfName}", rval_conf_name);
Packit 4a16fb
			MATCH_VARIABLE(value, "${CardId}", rval_card_id);
Packit 4a16fb
			MATCH_VARIABLE(value, "${CardDriver}", rval_card_driver);
Packit 4a16fb
			MATCH_VARIABLE(value, "${CardName}", rval_card_name);
Packit 4a16fb
			MATCH_VARIABLE(value, "${CardLongName}", rval_card_longname);
Packit 4a16fb
			MATCH_VARIABLE(value, "${CardComponents}", rval_card_components);
Packit 4a16fb
			MATCH_VARIABLE2(value, "${env:", rval_env);
Packit 4a16fb
			MATCH_VARIABLE2(value, "${sys:", rval_sysfs);
Packit 4a16fb
			err = -EINVAL;
Packit 4a16fb
			tmp = strchr(value, '}');
Packit 4a16fb
			if (tmp) {
Packit 4a16fb
				strncpy(r, value, tmp + 1 - value);
Packit 4a16fb
				r[tmp + 1 - value] = '\0';
Packit 4a16fb
				uc_error("variable '%s' is not known!", r);
Packit 4a16fb
			} else {
Packit 4a16fb
				uc_error("variable reference '%s' is not complete", value);
Packit 4a16fb
			}
Packit 4a16fb
			goto __error;
Packit 4a16fb
__rval:
Packit 4a16fb
			if (rval == NULL || rval[0] == '\0') {
Packit 4a16fb
				free(rval);
Packit 4a16fb
				strncpy(r, value, idsize);
Packit 4a16fb
				r[idsize] = '\0';
Packit 4a16fb
				uc_error("variable '%s' is not defined in this context!", r);
Packit 4a16fb
				err = -EINVAL;
Packit 4a16fb
				goto __error;
Packit 4a16fb
			}
Packit 4a16fb
			value += idsize;
Packit 4a16fb
			rvalsize = strlen(rval);
Packit 4a16fb
			nsize = size + rvalsize - idsize;
Packit 4a16fb
			if (nsize > size) {
Packit 4a16fb
				nr = realloc(r, nsize);
Packit 4a16fb
				if (nr == NULL) {
Packit 4a16fb
					free(rval);
Packit 4a16fb
					err = -ENOMEM;
Packit 4a16fb
					goto __error;
Packit 4a16fb
				}
Packit 4a16fb
				size = nsize;
Packit 4a16fb
				r = nr;
Packit 4a16fb
			}
Packit 4a16fb
			strcpy(r + dpos, rval);
Packit 4a16fb
			dpos += rvalsize;
Packit 4a16fb
			free(rval);
Packit 4a16fb
		} else {
Packit 4a16fb
			r[dpos++] = *value;
Packit 4a16fb
			value++;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	r[dpos] = '\0';
Packit 4a16fb
Packit 4a16fb
	*_rvalue = r;
Packit 4a16fb
	return 0;
Packit 4a16fb
Packit 4a16fb
__error:
Packit 4a16fb
	free(r);
Packit 4a16fb
	return err;
Packit 4a16fb
}