|
Lines 8-13
__included_shell_config=1
Link Here
|
| 8 |
|
8 |
|
| 9 |
__shell_config_comment='#' |
9 |
__shell_config_comment='#' |
| 10 |
|
10 |
|
|
|
11 |
__shell_config_quote_value() { |
| 12 |
local out="$*" |
| 13 |
if [ -z "${out##*[\\\\/]*}" ]; then |
| 14 |
out="$(printf %s "$out" |sed -e 's/[\\\/]/\\&/g')" || |
| 15 |
return 1 |
| 16 |
fi |
| 17 |
printf %s "$out" |
| 18 |
} |
| 19 |
|
| 11 |
shell_config_get() { |
20 |
shell_config_get() { |
| 12 |
[ "$#" -ge 2 -a "$#" -le 3 ] || |
21 |
[ "$#" -ge 2 -a "$#" -le 3 ] || |
| 13 |
fatal "Usage: shell_config_get file name [delim]" |
22 |
fatal "Usage: shell_config_get file name [delim]" |
|
Lines 23-29
shell_config_set() {
Link Here
|
| 23 |
[ "$#" -ge 3 -a "$#" -le 5 ] || |
32 |
[ "$#" -ge 3 -a "$#" -le 5 ] || |
| 24 |
fatal "Usage: shell_config_get file name value [read-delim [write-delim]]" |
33 |
fatal "Usage: shell_config_get file name value [read-delim [write-delim]]" |
| 25 |
|
34 |
|
| 26 |
local n v created= |
35 |
local n v nv= created= |
| 27 |
|
36 |
|
| 28 |
if [ ! -f "$1" ]; then |
37 |
if [ ! -f "$1" ]; then |
| 29 |
if [ ! -w "${1%/*}" ]; then |
38 |
if [ ! -w "${1%/*}" ]; then |
|
Lines 39-53
shell_config_set() {
Link Here
|
| 39 |
n="$(quote_sed_regexp "$2")" |
48 |
n="$(quote_sed_regexp "$2")" |
| 40 |
|
49 |
|
| 41 |
if v="$(grep --mmap -m1 "^[[:space:]]*$n${4-=}" -- "$1" 2>/dev/null)"; then |
50 |
if v="$(grep --mmap -m1 "^[[:space:]]*$n${4-=}" -- "$1" 2>/dev/null)"; then |
| 42 |
[ "${v#*$2${4-=}}" = "$3" ] || |
51 |
if [ "${v#*$2${4-=}}" != "$3" ]; then |
| 43 |
sed -i -e "s/^[[:space:]]*$n${4-=}.*/$2${5-=}$3/" -- "$1" |
52 |
nv="$(__shell_config_quote_value "$2${5-=}$3")" |
|
|
53 |
sed -i -e "s/^[[:space:]]*$n${4-=}.*/$nv/" -- "$1" |
| 54 |
fi |
| 44 |
return |
55 |
return |
| 45 |
fi |
56 |
fi |
| 46 |
|
57 |
|
| 47 |
if [ -n "${__shell_config_comment-}" ] && |
58 |
if [ -n "${__shell_config_comment-}" ] && |
| 48 |
v="$(grep --mmap -m1 "^[[:space:]]*${__shell_config_comment:-#}[[:space:]]*$n${4-=}" -- "$1" 2>/dev/null)"; then |
59 |
v="$(grep --mmap -m1 "^[[:space:]]*${__shell_config_comment:-#}[[:space:]]*$n${4-=}" -- "$1" 2>/dev/null)"; then |
| 49 |
v="$(quote_sed_regexp "$v")" |
60 |
v="$(quote_sed_regexp "$v")" |
| 50 |
sed -i -e "s/^$v\$/$2${5-=}$3/" -- "$1" |
61 |
nv="$(__shell_config_quote_value "$2${5-=}$3")" |
|
|
62 |
sed -i -e "s/^$v\$/$nv/" -- "$1" |
| 51 |
return |
63 |
return |
| 52 |
fi |
64 |
fi |
| 53 |
fi |
65 |
fi |