-
Notifications
You must be signed in to change notification settings - Fork 19
/
command.go
66 lines (63 loc) · 1.51 KB
/
command.go
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
package log4shell
// CommandLineToArgs splits a command line into individual argument
// strings, following the Windows conventions documented
// at http://daviddeley.com/autohotkey/parameters/parameters.htm#WINARGV
func CommandLineToArgs(cmd string) []string {
var args []string
for len(cmd) > 0 {
if cmd[0] == ' ' || cmd[0] == '\t' {
cmd = cmd[1:]
continue
}
var arg []byte
arg, cmd = readNextArg(cmd)
args = append(args, string(arg))
}
return args
}
// readNextArg splits command line string cmd into next
// argument and command line remainder.
func readNextArg(cmd string) (arg []byte, rest string) {
var b []byte
var inQuote bool
var nSlash int
for ; len(cmd) > 0; cmd = cmd[1:] {
c := cmd[0]
switch c {
case ' ', '\t':
if !inQuote {
return appendBSBytes(b, nSlash), cmd[1:]
}
case '"':
b = appendBSBytes(b, nSlash/2)
if nSlash%2 == 0 {
// use "Prior to 2008" rule from
// http://daviddeley.com/autohotkey/parameters/parameters.htm
// section 5.2 to deal with double quotes
if inQuote && len(cmd) > 1 && cmd[1] == '"' {
b = append(b, c)
cmd = cmd[1:]
}
inQuote = !inQuote
} else {
b = append(b, c)
}
nSlash = 0
continue
case '\\':
nSlash++
continue
}
b = appendBSBytes(b, nSlash)
nSlash = 0
b = append(b, c)
}
return appendBSBytes(b, nSlash), ""
}
// appendBSBytes appends n '\\' bytes to b and returns the resulting slice.
func appendBSBytes(b []byte, n int) []byte {
for ; n > 0; n-- {
b = append(b, '\\')
}
return b
}