ucm: add show_err parameter to uc_mgr_get_variable

Add bool show_err parameter to uc_mgr_get_variable() to control whether
an error message is displayed when a variable is not defined. This
centralizes error reporting and eliminates redundant error messages
in callers.

Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
Jaroslav Kysela 2026-02-18 13:23:26 +01:00
parent a74521f371
commit d8ca4a7cc4
5 changed files with 22 additions and 29 deletions

View file

@ -1781,7 +1781,7 @@ int snd_use_case_mgr_open(snd_use_case_mgr_t **uc_mgr,
card_name = parse_open_variables(mgr, card_name); card_name = parse_open_variables(mgr, card_name);
/* Application developers: This argument is not supposed to be set for standard applications. */ /* Application developers: This argument is not supposed to be set for standard applications. */
if (uc_mgr_get_variable(mgr, "@InBoot")) if (uc_mgr_get_variable(mgr, "@InBoot", false))
mgr->in_boot = true; mgr->in_boot = true;
err = uc_mgr_card_open(mgr); err = uc_mgr_card_open(mgr);

View file

@ -527,7 +527,7 @@ static int evaluate_macro1(snd_use_case_mgr_t *uc_mgr,
if (err < 0) if (err < 0)
goto __err_path; goto __err_path;
snprintf(name, sizeof(name), "__%s", id); snprintf(name, sizeof(name), "__%s", id);
if (uc_mgr_get_variable(uc_mgr, name)) { if (uc_mgr_get_variable(uc_mgr, name, false)) {
snd_error(UCM, "Macro argument '%s' is already defined", name); snd_error(UCM, "Macro argument '%s' is already defined", name);
goto __err_path; goto __err_path;
} }

View file

@ -352,7 +352,8 @@ int uc_mgr_add_value(struct list_head *base, const char *key, char *val);
int uc_mgr_check_value(struct list_head *value_list, const char *identifier); int uc_mgr_check_value(struct list_head *value_list, const char *identifier);
const char *uc_mgr_get_variable(snd_use_case_mgr_t *uc_mgr, const char *uc_mgr_get_variable(snd_use_case_mgr_t *uc_mgr,
const char *name); const char *name,
bool show_err);
int uc_mgr_set_variable(snd_use_case_mgr_t *uc_mgr, int uc_mgr_set_variable(snd_use_case_mgr_t *uc_mgr,
const char *name, const char *name,

View file

@ -234,11 +234,9 @@ static char *rval_card_info(snd_use_case_mgr_t *uc_mgr, const char *query)
if (card_str[0] == '$') { if (card_str[0] == '$') {
tmp = card_str + 1; tmp = card_str + 1;
card_str = uc_mgr_get_variable(uc_mgr, tmp); card_str = uc_mgr_get_variable(uc_mgr, tmp, true);
if (card_str == NULL) { if (card_str == NULL)
snd_error(UCM, "info-card: variable '%s' not found", tmp);
goto __error; goto __error;
}
} }
if (snd_config_search(config, "field", &d)) { if (snd_config_search(config, "field", &d)) {
@ -250,11 +248,9 @@ static char *rval_card_info(snd_use_case_mgr_t *uc_mgr, const char *query)
if (field_str[0] == '$') { if (field_str[0] == '$') {
tmp = field_str + 1; tmp = field_str + 1;
field_str = uc_mgr_get_variable(uc_mgr, tmp); field_str = uc_mgr_get_variable(uc_mgr, tmp, true);
if (field_str == NULL) { if (field_str == NULL)
snd_error(UCM, "info-card: variable '%s' not found", tmp);
goto __error; goto __error;
}
} }
if (safe_strtol(card_str, &card_num) == 0) if (safe_strtol(card_str, &card_num) == 0)
@ -351,9 +347,9 @@ static char *rval_lookup_main(snd_use_case_mgr_t *uc_mgr,
goto null; goto null;
if (s[0] == '$' && uc_mgr->conf_format >= 9) { if (s[0] == '$' && uc_mgr->conf_format >= 9) {
tmp = s + 1; tmp = s + 1;
s = uc_mgr_get_variable(uc_mgr, tmp); s = uc_mgr_get_variable(uc_mgr, tmp, true);
if (s == NULL) if (s == NULL)
goto var_not_found; goto null;
} }
for (fcn = iter->fcns ; fcn; fcn++) { for (fcn = iter->fcns ; fcn; fcn++) {
if (strcasecmp(fcn->name, s) == 0) { if (strcasecmp(fcn->name, s) == 0) {
@ -373,9 +369,9 @@ static char *rval_lookup_main(snd_use_case_mgr_t *uc_mgr,
goto null; goto null;
if (s[0] == '$' && uc_mgr->conf_format >= 9) { if (s[0] == '$' && uc_mgr->conf_format >= 9) {
tmp = s + 1; tmp = s + 1;
s = uc_mgr_get_variable(uc_mgr, tmp); s = uc_mgr_get_variable(uc_mgr, tmp, true);
if (s == NULL) if (s == NULL)
goto var_not_found; goto null;
} }
err = regcomp(&re, s, REG_EXTENDED | REG_ICASE); err = regcomp(&re, s, REG_EXTENDED | REG_ICASE);
if (err) { if (err) {
@ -399,8 +395,6 @@ fin:
if (iter->done) if (iter->done)
iter->done(iter); iter->done(iter);
return result; return result;
var_not_found:
snd_error(UCM, "lookup: variable '%s' not found", tmp);
null: null:
result = NULL; result = NULL;
goto fin; goto fin;
@ -533,11 +527,9 @@ static int rval_pcm_lookup_init(snd_use_case_mgr_t *uc_mgr,
snd_config_get_string(d, &s) == 0) { snd_config_get_string(d, &s) == 0) {
if (s[0] == '$' && uc_mgr->conf_format >= 9) { if (s[0] == '$' && uc_mgr->conf_format >= 9) {
tmp = s + 1; tmp = s + 1;
s = uc_mgr_get_variable(uc_mgr, tmp); s = uc_mgr_get_variable(uc_mgr, tmp, true);
if (s == NULL) { if (s == NULL)
snd_error(UCM, "pcm lookup: variable '%s' not found", tmp);
return -EINVAL; return -EINVAL;
}
} }
if (strcasecmp(s, "playback") == 0) if (strcasecmp(s, "playback") == 0)
stream = SND_PCM_STREAM_PLAYBACK; stream = SND_PCM_STREAM_PLAYBACK;
@ -596,11 +588,9 @@ static int rval_device_lookup_init(snd_use_case_mgr_t *uc_mgr,
} }
if (s[0] == '$' && uc_mgr->conf_format >= 9) { if (s[0] == '$' && uc_mgr->conf_format >= 9) {
tmp = s + 1; tmp = s + 1;
s = uc_mgr_get_variable(uc_mgr, tmp); s = uc_mgr_get_variable(uc_mgr, tmp, true);
if (s == NULL) { if (s == NULL)
snd_error(UCM, "device lookup: variable '%s' not found", tmp);
return -EINVAL; return -EINVAL;
}
} }
for (t = types; t->name; t++) for (t = types; t->name; t++)
if (strcasecmp(t->name, s) == 0) if (strcasecmp(t->name, s) == 0)
@ -848,7 +838,7 @@ static char *rval_var(snd_use_case_mgr_t *uc_mgr, const char *id)
} else if (id[0] == '@') { } else if (id[0] == '@') {
ignore_not_found = true; ignore_not_found = true;
} }
v = uc_mgr_get_variable(uc_mgr, id); v = uc_mgr_get_variable(uc_mgr, id, false);
if (v == NULL && ignore_not_found) if (v == NULL && ignore_not_found)
v = ""; v = "";
if (v) if (v)
@ -864,7 +854,7 @@ static int rval_eval_var_cb(snd_config_t **dst, const char *s, void *private_dat
snd_use_case_mgr_t *uc_mgr = private_data; snd_use_case_mgr_t *uc_mgr = private_data;
const char *v; const char *v;
v = uc_mgr_get_variable(uc_mgr, s); v = uc_mgr_get_variable(uc_mgr, s, false);
if (v == NULL) if (v == NULL)
return -ENOENT; return -ENOENT;
return snd_config_imake_string(dst, NULL, v); return snd_config_imake_string(dst, NULL, v);
@ -1062,7 +1052,7 @@ __match2:
if (*v2 == '$' && uc_mgr->conf_format >= 3) { if (*v2 == '$' && uc_mgr->conf_format >= 3) {
if (strncmp(value, "${eval:", 7) == 0) if (strncmp(value, "${eval:", 7) == 0)
goto __direct_fcn2; goto __direct_fcn2;
tmp = uc_mgr_get_variable(uc_mgr, v2 + 1); tmp = uc_mgr_get_variable(uc_mgr, v2 + 1, false);
if (tmp == NULL) { if (tmp == NULL) {
snd_error(UCM, "define '%s' is not reachable in this context!", v2 + 1); snd_error(UCM, "define '%s' is not reachable in this context!", v2 + 1);
rval = NULL; rval = NULL;

View file

@ -672,7 +672,7 @@ int uc_mgr_remove_device(struct use_case_verb *verb, const char *name)
return found == 0 ? -ENODEV : 0; return found == 0 ? -ENODEV : 0;
} }
const char *uc_mgr_get_variable(snd_use_case_mgr_t *uc_mgr, const char *name) const char *uc_mgr_get_variable(snd_use_case_mgr_t *uc_mgr, const char *name, bool show_err)
{ {
struct list_head *pos; struct list_head *pos;
struct ucm_value *value; struct ucm_value *value;
@ -682,6 +682,8 @@ const char *uc_mgr_get_variable(snd_use_case_mgr_t *uc_mgr, const char *name)
if (strcmp(value->name, name) == 0) if (strcmp(value->name, name) == 0)
return value->data; return value->data;
} }
if (show_err)
snd_error(UCM, "variable '%s' is not defined", name);
return NULL; return NULL;
} }