2
2
3
3
# # shell-like command parsing ##
4
4
5
+ const shell_special = " #{}()[]<>|&*?~;"
6
+
5
7
function shell_parse (str:: AbstractString , interpolate:: Bool = true )
6
8
s = lstrip (str)
7
9
# strips the end but respects the space when the string ends with "\\ "
@@ -92,6 +94,8 @@ function shell_parse(str::AbstractString, interpolate::Bool=true)
92
94
update_arg (s[i: j- 1 ]); i = k
93
95
c, k = next (s,k)
94
96
end
97
+ elseif ! in_single_quotes && ! in_double_quotes && c in shell_special
98
+ depwarn (" special characters \" $shell_special \" should now be quoted in commands" , :shell_parse )
95
99
end
96
100
j = k
97
101
end
@@ -122,14 +126,14 @@ function shell_split(s::AbstractString)
122
126
args
123
127
end
124
128
125
- function print_shell_word (io:: IO , word:: AbstractString )
129
+ function print_shell_word (io:: IO , word:: AbstractString , special :: AbstractString = shell_special )
126
130
if isempty (word)
127
131
print (io, " ''" )
128
132
end
129
133
has_single = false
130
134
has_special = false
131
135
for c in word
132
- if isspace (c) || c== ' \\ ' || c== ' \' ' || c== ' "' || c== ' $'
136
+ if isspace (c) || c== ' \\ ' || c== ' \' ' || c== ' "' || c== ' $' || c in special
133
137
has_special = true
134
138
if c == ' \' '
135
139
has_single = true
@@ -152,13 +156,17 @@ function print_shell_word(io::IO, word::AbstractString)
152
156
end
153
157
end
154
158
155
- function print_shell_escaped (io:: IO , cmd:: AbstractString , args:: AbstractString... )
156
- print_shell_word (io, cmd)
159
+ function print_shell_escaped (
160
+ io:: IO , cmd:: AbstractString , args:: AbstractString... ;
161
+ special:: AbstractString = shell_special
162
+ )
163
+ print_shell_word (io, cmd, special)
157
164
for arg in args
158
165
print (io, ' ' )
159
- print_shell_word (io, arg)
166
+ print_shell_word (io, arg, special )
160
167
end
161
168
end
162
- print_shell_escaped (io:: IO ) = nothing
169
+ print_shell_escaped (io:: IO ; special :: String = shell_special ) = nothing
163
170
164
- shell_escape (args:: AbstractString... ) = sprint (print_shell_escaped, args... )
171
+ shell_escape (args:: AbstractString... ; special:: AbstractString = shell_special) =
172
+ sprint (io-> print_shell_escaped (io, args... , special= special))
0 commit comments