/
shell.cr
161 lines (143 loc) · 4.29 KB
/
shell.cr
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
class Process
# Converts a sequence of strings to one joined string with each argument shell-quoted.
#
# This is then safe to pass as part of the command when using `shell: true` or `system()`.
#
# NOTE: The actual return value is system-dependent, so it mustn't be relied on in other contexts.
# See also: `quote_posix`.
#
# ```
# files = ["my file.txt", "another.txt"]
# `grep -E 'fo+' -- #{Process.quote(files)}`
# ```
def self.quote(args : Enumerable(String)) : String
{% if flag?(:win32) %}
quote_windows(args)
{% else %}
quote_posix(args)
{% end %}
end
# Shell-quotes one item, same as `quote({arg})`.
def self.quote(arg : String) : String
quote({arg})
end
# Converts a sequence of strings to one joined string with each argument shell-quoted.
#
# This is then safe to pass to a POSIX shell.
#
# ```
# files = ["my file.txt", "another.txt"]
# Process.quote_posix(files) # => "'my file.txt' another.txt"
# ```
def self.quote_posix(args : Enumerable(String)) : String
args.join(' ') do |arg|
if arg.empty?
"''"
elsif arg.matches? %r([^a-zA-Z0-9%+,\-./:=@_]) # not all characters are safe, needs quoting
"'" + arg.gsub("'", %('"'"')) + "'" # %(foo'ba#r) becomes %('foo'"'"'ba#r')
else
arg
end
end
end
# Shell-quotes one item, same as `quote_posix({arg})`.
def self.quote_posix(arg : String) : String
quote_posix({arg})
end
# :nodoc:
#
# Converts a sequence of strings to one joined string with each argument shell-quoted.
#
# This is then safe to pass Windows API CreateProcess.
#
# NOTE: This is **not** safe to pass to the CMD shell.
#
# ```
# files = ["my file.txt", "another.txt"]
# Process.quote_windows(files) # => %("my file.txt" another.txt)
# ```
def self.quote_windows(args : Enumerable(String)) : String
String.build { |io| quote_windows(io, args) }
end
private def self.quote_windows(io : IO, args)
args.join(io, ' ') do |arg|
need_quotes = arg.empty? || arg.includes?(' ') || arg.includes?('\t')
io << '"' if need_quotes
slashes = 0
arg.each_char do |c|
case c
when '\\'
slashes += 1
when '"'
(slashes + 1).times { io << '\\' }
slashes = 0
else
slashes = 0
end
io << c
end
if need_quotes
slashes.times { io << '\\' }
io << '"'
end
end
end
# :nodoc:
#
# Shell-quotes one item, same as `quote_windows({arg})`.
#
# ```
# Process.quote_windows(%q(C:\"foo" project.txt)) # => %q("C:\\\"foo\" project.txt")
# ```
def self.quote_windows(arg : String) : String
quote_windows({arg})
end
# Split a *line* string into the array of tokens in the same way the POSIX shell.
#
# ```
# Process.parse_arguments(%q["foo bar" '\hello/' Fizz\ Buzz]) # => ["foo bar", "\\hello/", "Fizz Buzz"]
# ```
#
# See https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_03
def self.parse_arguments(line : String) : Array(String)
tokens = [] of String
reader = Char::Reader.new(line)
while reader.has_next?
# skip whitespace
while reader.current_char.ascii_whitespace?
reader.next_char
end
break unless reader.has_next?
token = String.build do |str|
while reader.has_next? && !reader.current_char.ascii_whitespace?
quote = nil
if reader.current_char.in?('\'', '"')
quote = reader.current_char
reader.next_char
end
until (char = reader.current_char) == quote || (!quote && (char.ascii_whitespace? || char.in?('\'', '"')))
break unless reader.has_next?
reader.next_char
if char == '\\' && quote != '\''
str << char if quote == '"'
char = reader.current_char
if reader.has_next?
reader.next_char
else
break if quote == '"'
char = '\\'
end
end
str << char
end
if quote
raise ArgumentError.new("Unmatched quote") unless reader.has_next?
reader.next_char
end
end
end
tokens << token
end
tokens
end
end