-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmquote.sh
244 lines (210 loc) · 5.78 KB
/
mquote.sh
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/bin/bash
# vi:set ts=8 sw=4 et:
#
# Author: Clark Wang <dearvoid at gmail.com>
# _uplevel [-u "var1 var2 ..."] "cmd .." [args ..]
function _uplevel()
{
if [[ $1 == '-u' ]]; then
unset -v -- $2 || return
shift 2
fi
eval "shift; $1"
}
function _quote()
{
local str i ch ch2 ret
local safe_pat='@([-+=%@:,./[:word:]])'
local dq_needsEsc='@([\\"`$])'
local dq_safe='@([^\\!"`$])'
local left_quote=''
str="$2"
printf -v ret '%q' "$str"
if [[ $ret == "$str" || $ret == "\$'"* ]]; then
local "$1" && _uplevel -u "$1" "$1="'"$1"' "$ret"
return
fi
ret=''
for ((i = 0; i < ${#str}; ++i)); do
ch=${str:i:1}
ch2=${str:i+1:1}
# '..
if [[ $left_quote == \' ]]; then
# '.. + '
if [[ $ch == \' ]]; then
ret+="'\\'"
left_quote=''
# '.. + x
else
ret+="$ch"
fi
# "''..
elif [[ $left_quote == \" ]]; then
# "''.. + !
if [[ $ch == '!' ]]; then
ret+='"'
left_quote=''
(( i-- ))
# "''.. + $
elif [[ $ch == $dq_needsEsc ]]; then
# All attempts to optimize this have failed. DO NOT TRY!
ret+="\\$ch"
# "''.. + {x,'}
else
ret+="$ch"
fi
# .. + x
elif [[ $ch == $safe_pat ]]; then
ret+="$ch"
# no more chars
elif [[ -z $ch2 ]]; then
# .. + <space><END>
if [[ $ch == ' ' ]]; then
# We don't want the final result to end with <space> which
# is not copy-n-paste friendly.
ret+="' '"
# .. + ^<END>
else
ret+="\\$ch"
fi
# .. + '
elif [[ $ch == \' ]]; then
# .. + ''
if [[ $ch2 == \' ]]; then
ret+="\"'"
left_quote='"'
# .. + 'x
else
ret+="\\'"
fi
# .. + ^^
# .. + !^
# .. + !!
elif [[ $ch2 != \' ]]; then
ret+="'$ch"
left_quote="'"
# .. + ^'
elif [[ $ch2 == \' && $ch == $dq_safe ]]; then
ret+="\"$ch"
left_quote=\"
# .. + $'
# .. + !'
else
ret+="\\$ch"
fi
done
if [[ -n $left_quote ]]; then
ret+="$left_quote"
fi
if [[ -z $ret ]]; then
ret="''"
fi
local "$1" && _uplevel -u "$1" "$1="'"$1"' "$ret"
}
function _magic_quote()
{
local -a locals=( locals \
major minor bash44 \
no_run use_declare use_printf use_Q verbose \
arr cmd opt pat sep iter \
i s )
###=1=###
local ${locals[@]}
use_printf=0 use_declare=0 use_Q=0 no_run=0 verbose=0
bash44=0
sep='``'
arr=()
major=${BASH_VERSINFO[0]}
minor=${BASH_VERSINFO[1]}
if (( (major == 4 && minor >= 4) || major > 4 )); then
bash44=1
fi
cmd=$( history 1 )
if [[ $cmd == *$'\n'* ]]; then
printf '!!! does not support multi-lined command\n' >&2
return 1
fi
pat='^ *[0-9]+ +([^ ]+)( +([^ ]|[^ ].*[^ ]))? *$'
# (1 )(2 (3 ))
if [[ $cmd =~ $pat ]]; then
if [[ ${BASH_REMATCH[1]} != mquote ]]; then
printf '!!! mquote must be the first word in the command\n' >&2
return 1
fi
cmd=${BASH_REMATCH[3]}
else
printf '!!! invalid history entry: %s\n' "$cmd" >&2
return 1
fi
pat='^ *-([dnpQv]+) *(.*)$'
# ^^__ Here cannot use " +" or "mquote -v" would not work.
while [[ $cmd =~ $pat ]]; do
opt=${BASH_REMATCH[1]}
cmd=${BASH_REMATCH[2]}
[[ $opt == *d* ]] && use_declare=1
[[ $opt == *n* ]] && no_run=1
[[ $opt == *p* ]] && use_printf=1
[[ $opt == *Q* ]] && use_Q=1
[[ $opt == *v* ]] && verbose=1
done
if (( ! bash44 )); then
use_Q=0
fi
if [[ $cmd != *"$sep"* ]]; then
no_run=1
cmd="$sep$cmd$sep"
fi
for ((i = 0; ; ++i)); do
s=${cmd%%"$sep"*}
arr[i]=$s
if [[ $cmd != *"$sep"* ]]; then
break
else
cmd=${cmd#*"$sep"}
fi
done
if (( ${#arr[@]} % 2 != 1 )); then
printf '!!! %s\n' "$sep .. $sep does not match" >&2
return 1
fi
cmd=''
pat='^[{]([0-9]+)[}](.*)$'
# (1 ) (2 )
for ((i = 0; i < ${#arr[@]}; ++i)); do
s=${arr[i]}
if (( i % 2 == 1 )); then
if [[ $s =~ $pat ]]; then
iter=${BASH_REMATCH[1]}
s=${BASH_REMATCH[2]}
else
iter=1
fi
if (( iter > 9 )); then
printf "!!! %s\n" "n must be < 10 for $sep{n}..$sep" >&2
return 1
fi
for (( ; iter; --iter)); do
if (( use_printf )); then
#s=$( printf '%q' "$s" )
printf -v s '%q' "$s"
elif (( use_Q )); then
s=${s@Q}
elif (( use_declare )); then
s=$( declare -p s )
s=${s#*=}
else
# use my own quoting
_quote s "$s"
fi
done
fi
cmd="$cmd$s"
done
if (( verbose || no_run )); then
printf '>>> %s\n' "$cmd" >&2
fi
if (( ! no_run )); then
_uplevel -u "${locals[*]}" "$cmd"
fi
}
alias mquote='_magic_quote #'