-
Notifications
You must be signed in to change notification settings - Fork 9
/
shell-quote
163 lines (148 loc) · 4.26 KB
/
shell-quote
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/sh -efu
### This file is covered by the GNU General Public License,
### which should be included with libshell as the file LICENSE.
### All copyright information are listed in the COPYING.
if [ -z "${__included_shell_quote-}" ]; then
__included_shell_quote=1
. shell-version
. shell-error
. shell-string
### Quote argument for sed basic regular expression and store result into variable.
### Usage example:
### quote_sed_regexp_variable var_pattern "$pattern"
### quote_sed_regexp_variable var_replace "$replace"
### sed "s/$var_pattern/$var_replace/"
quote_sed_regexp_variable()
{
local __quote_set_regexp_variable_var __quote_set_regexp_variable_out
__quote_set_regexp_variable_var="$1"; shift
__quote_set_regexp_variable_out="$*"
if [ -z "${__quote_set_regexp_variable_out##*[\[\].*&^\$\\\\/]*}" ]; then
__quote_set_regexp_variable_out="$(printf %s "$__quote_set_regexp_variable_out" |
sed -e 's/[].*&^$[\/]/\\&/g')" ||
return 1
fi
eval "$__quote_set_regexp_variable_var=\"\$__quote_set_regexp_variable_out\""
}
### Quote given arguments for sed basic regular expression.
### Usage example: sed "s/$(quote_sed_regexp "$var_pattern")/$(quote_sed_regexp "$var_replacement")/"
quote_sed_regexp()
{
local result
quote_sed_regexp_variable result "$@"
printf %s "$result"
}
### Quote argument for shell and store result into variable.
### Usage example:
### quote_shell_variable var_name "$var_value"
### printf '%s\n' "$var_name"
quote_shell_variable()
{
local __quote_shell_variable_var __quote_shell_variable_out
__quote_shell_variable_var="$1"; shift
__quote_shell_variable_out="$*"
if [ -z "${__quote_shell_variable_out##*[\"\$\`\\\\]*}" ]; then
__quote_shell_variable_out="$(printf %s "$__quote_shell_variable_out" |
sed -e 's/[\"$\`\\]/\\&/g')" ||
return 1
fi
eval "$__quote_shell_variable_var=\"\$__quote_shell_variable_out\""
}
### Quote argument for shell.
### Usage example: eval "$var_name=\"$(quote_shell "$var_value")\""
quote_shell()
{
local result
quote_shell_variable result "$@"
printf %s "$result"
}
### Quote argument for shell and store result into variable.
###
### Usage example:
### quote_shell_args args "$var_args"
### eval "set -- $args"
quote_shell_args()
{
### This is an internal function to avoid the use of ugly namespace.
__quote_shell_args() {
local m='' r='' c='' l="$1"
### backslash/double/single quote mode
local bq='' dq='' sq=''
__quote_shell_args_toggle() {
### toggle $1 value
eval [ -n \"\$$1\" ] && eval "$1=" || eval "$1=\$$2"
}
fill_mask m "$l"
while [ -n "$l" ]; do
c="${l%$m}"
l="${l#?}"
m="${m#?}"
case "$c" in
\")
### toggle double quote mode unless
### in backslash or single quote mode
[ -n "$bq$sq" ] || __quote_shell_args_toggle dq c
;;
\')
### toggle single quote mode unless
### in backslash or double quote mode
[ -n "$bq$dq" ] || __quote_shell_args_toggle sq c
;;
\$|\`)
### quote special character unless
### in backslash or single quote mode
[ -n "$bq$sq" ] || bq=\\
;;
\\)
### toggle backslash quote mode unless
### in single quote mode
if [ -z "$sq" ]; then
if [ -z "$bq" ]; then
### enter backslash quote mode
bq=\\
continue
else
### leave backslash quote mode
r="$r\\"
bq=
fi
fi
;;
[!A-Za-z0-9_\ \ ])
### quote non-regular character unless
### in any quote mode
[ -n "$bq$dq$sq" ] || bq=\\
;;
esac
r="$r$bq$c"
### leave backslash quote mode if any
bq=
done
[ -z "$bq$dq$sq" ] ||
{ message "unmatched character ($bq$dq$sq) found"; return 1; }
__quote_shell_args_out="$r"
}
local __quote_shell_args_out='' __quote_shell_args_rc=0
__quote_shell_args "$2" ||
__quote_shell_args_rc=1
eval "$1=\"\$__quote_shell_args_out\""
### Remove internal functions from user namespace.
unset -f __quote_shell_args __quote_shell_args_toggle
return $__quote_shell_args_rc
}
if [ -n "${__export_compatibility_string_quote_remove-}" ]; then
### Obsolete function. You shouldn't use it.
string_quote_remove()
{
local out="$1"
if [ -z "${1##*\'}${1%%\'*}" ]; then
out="${1#\'}"
out="${out%\'}"
elif [ -z "${1##*\"}${1%%\"*}" ]; then
out="${1#\"}"
out="${out%\"}"
fi
printf %s "$out"
}
fi # __export_compatibility_string_quote_remove
fi #__included_shell_quote