blob: af73c1d0d556c9895775483406ea55912f93e71d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# Copyright (c) 2016 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "Config file sharing" project.
# For details, see https://github.com/egor-tensin/config-links.
# Distributed under the MIT License.
# Variable resolution
declare -A cached_paths
readonly root_var_name='CONFIG_LINKS_ROOT'
resolve_variable() {
if [ "$#" -ne 1 ]; then
echo "usage: ${FUNCNAME[0]} VAR_NAME" >&2
return 1
fi
local var_name="$1"
if [ -n "${cached_paths[$var_name]+x}" ]; then
echo "${cached_paths[$var_name]}"
return 0
fi
if [ "$var_name" = "$root_var_name" ]; then
echo '/'
return 0
fi
if [ -z "${!var_name+x}" ]; then
dump "variable is not set: $var_name" >&2
return 1
fi
local var_path="${!var_name}"
traverse_path --exist --directory -- "$var_path"
}
cache_variable() {
local var_name
for var_name; do
[ -n "${cached_paths[$var_name]+x}" ] && continue
cached_paths[$var_name]="$( resolve_variable "$var_name" )"
done
}
readonly var_name_regex='%\([_[:alpha:]][_[:alnum:]]*\)%'
extract_variable_name() {
local s
for s; do
local var_name
if ! var_name="$( expr "$s" : "$var_name_regex/" )"; then
dump "couldn't extract variable name from: $s" >&2
return 1
fi
echo "$var_name"
done
}
|