mirror of
https://github.com/caddyserver/caddy.git
synced 2025-05-23 02:13:38 +08:00
Parse Windows commands differently than Unix commands
Stinkin' backslashes
This commit is contained in:
@ -2,6 +2,9 @@ package middleware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"runtime"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/flynn/go-shlex"
|
||||
)
|
||||
@ -9,11 +12,19 @@ import (
|
||||
// SplitCommandAndArgs takes a command string and parses it
|
||||
// shell-style into the command and its separate arguments.
|
||||
func SplitCommandAndArgs(command string) (cmd string, args []string, err error) {
|
||||
parts, err := shlex.Split(command)
|
||||
if err != nil {
|
||||
err = errors.New("error parsing command: " + err.Error())
|
||||
return
|
||||
} else if len(parts) == 0 {
|
||||
var parts []string
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
parts = parseWindowsCommand(command) // parse it Windows-style
|
||||
} else {
|
||||
parts, err = shlex.Split(command) // parse it Unix-style
|
||||
if err != nil {
|
||||
err = errors.New("error parsing command: " + err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if len(parts) == 0 {
|
||||
err = errors.New("no command contained in '" + command + "'")
|
||||
return
|
||||
}
|
||||
@ -25,3 +36,64 @@ func SplitCommandAndArgs(command string) (cmd string, args []string, err error)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// parseWindowsCommand is a sad but good-enough attempt to
|
||||
// split a command into the command and its arguments like
|
||||
// the Windows command line would; only basic parsing is
|
||||
// supported. This function has to be used on Windows instead
|
||||
// of the shlex package because this function treats backslash
|
||||
// characters properly.
|
||||
//
|
||||
// Loosely based off the rules here: http://stackoverflow.com/a/4094897/1048862
|
||||
// True parsing is much, much trickier.
|
||||
func parseWindowsCommand(cmd string) []string {
|
||||
var parts []string
|
||||
var part string
|
||||
var quoted bool
|
||||
var backslashes int
|
||||
|
||||
for _, ch := range cmd {
|
||||
if ch == '\\' {
|
||||
backslashes++
|
||||
continue
|
||||
}
|
||||
var evenBacksl = (backslashes % 2) == 0
|
||||
if backslashes > 0 && ch != '\\' {
|
||||
numBacksl := (backslashes / 2) + 1
|
||||
if ch == '"' {
|
||||
numBacksl--
|
||||
}
|
||||
part += strings.Repeat(`\`, numBacksl)
|
||||
backslashes = 0
|
||||
}
|
||||
|
||||
if quoted {
|
||||
if ch == '"' && evenBacksl {
|
||||
quoted = false
|
||||
continue
|
||||
}
|
||||
part += string(ch)
|
||||
continue
|
||||
}
|
||||
|
||||
if unicode.IsSpace(ch) && len(part) > 0 {
|
||||
parts = append(parts, part)
|
||||
part = ""
|
||||
continue
|
||||
}
|
||||
|
||||
if ch == '"' && evenBacksl {
|
||||
quoted = true
|
||||
continue
|
||||
}
|
||||
|
||||
part += string(ch)
|
||||
}
|
||||
|
||||
if len(part) > 0 {
|
||||
parts = append(parts, part)
|
||||
part = ""
|
||||
}
|
||||
|
||||
return parts
|
||||
}
|
||||
|
Reference in New Issue
Block a user