ucm: allow to ignore errors for the value substitution

It may be useful to ignore the errors where the environment
or sysfs values are not defined for the specific hardware.

Enhance substitution for 'syntax 3' so $${} substitution
means ignore the errors (return an empty string).

Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
Jaroslav Kysela 2020-06-18 15:09:23 +02:00
parent 20e003a63d
commit e6be544227

View file

@ -268,6 +268,7 @@ int uc_mgr_get_substituted_value(snd_use_case_mgr_t *uc_mgr,
size_t size, nsize, idsize, rvalsize, dpos = 0; size_t size, nsize, idsize, rvalsize, dpos = 0;
const char *tmp; const char *tmp;
char *r, *nr, *rval, v2[32]; char *r, *nr, *rval, v2[32];
bool ignore_error, allow_empty;
int err; int err;
if (value == NULL) if (value == NULL)
@ -279,9 +280,20 @@ int uc_mgr_get_substituted_value(snd_use_case_mgr_t *uc_mgr,
return -ENOMEM; return -ENOMEM;
while (*value) { while (*value) {
if (*value == '$' && *(value+1) == '{') { if (*value != '$') {
bool allow_empty = false; __std:
r[dpos++] = *value;
value++;
continue;
}
ignore_error = false;
if (value[1] == '$' && value[2] == '{' && uc_mgr->conf_format >= 3) {
value++;
ignore_error = true;
} else if (value[1] != '{') {
goto __std;
}
allow_empty = false;
MATCH_VARIABLE(value, "${OpenName}", rval_open_name, false); MATCH_VARIABLE(value, "${OpenName}", rval_open_name, false);
MATCH_VARIABLE(value, "${ConfTopDir}", rval_conf_topdir, false); MATCH_VARIABLE(value, "${ConfTopDir}", rval_conf_topdir, false);
MATCH_VARIABLE(value, "${ConfDir}", rval_conf_dir, false); MATCH_VARIABLE(value, "${ConfDir}", rval_conf_dir, false);
@ -309,6 +321,10 @@ int uc_mgr_get_substituted_value(snd_use_case_mgr_t *uc_mgr,
__rval: __rval:
if (rval == NULL || (!allow_empty && rval[0] == '\0')) { if (rval == NULL || (!allow_empty && rval[0] == '\0')) {
free(rval); free(rval);
if (ignore_error) {
value += idsize;
continue;
}
strncpy(r, value, idsize); strncpy(r, value, idsize);
r[idsize] = '\0'; r[idsize] = '\0';
uc_error("variable '%s' is not defined in this context!", r); uc_error("variable '%s' is not defined in this context!", r);
@ -331,10 +347,6 @@ __rval:
strcpy(r + dpos, rval); strcpy(r + dpos, rval);
dpos += rvalsize; dpos += rvalsize;
free(rval); free(rval);
} else {
r[dpos++] = *value;
value++;
}
} }
r[dpos] = '\0'; r[dpos] = '\0';