Add ShellEscapeIncludingSpaces(string) am: ddb5ed7e1f
Original change: https://android-review.googlesource.com/c/platform/build/blueprint/+/1620991 MUST ONLY BE SUBMITTED BY AUTOMERGER Change-Id: I65daf1c2bb22165fb0a1d65c7837c576a98cf3a2
This commit is contained in:
commit
41ea2da6af
2 changed files with 75 additions and 18 deletions
|
@ -56,30 +56,44 @@ func ShellEscapeList(slice []string) []string {
|
|||
|
||||
}
|
||||
|
||||
// ShellEscapeList takes string that may contain characters that are meaningful to bash and
|
||||
func shellUnsafeChar(r rune) bool {
|
||||
switch {
|
||||
case 'A' <= r && r <= 'Z',
|
||||
'a' <= r && r <= 'z',
|
||||
'0' <= r && r <= '9',
|
||||
r == '_',
|
||||
r == '+',
|
||||
r == '-',
|
||||
r == '=',
|
||||
r == '.',
|
||||
r == ',',
|
||||
r == '/':
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// ShellEscape takes string that may contain characters that are meaningful to bash and
|
||||
// escapes it if necessary by wrapping it in single quotes, and replacing internal single quotes with
|
||||
// '\'' (one single quote to end the quoting, a shell-escaped single quote to insert a real single
|
||||
// quote, and then a single quote to restarting quoting.
|
||||
func ShellEscape(s string) string {
|
||||
shellUnsafeChar := func(r rune) bool {
|
||||
switch {
|
||||
case 'A' <= r && r <= 'Z',
|
||||
'a' <= r && r <= 'z',
|
||||
'0' <= r && r <= '9',
|
||||
r == '_',
|
||||
r == '+',
|
||||
r == '-',
|
||||
r == '=',
|
||||
r == '.',
|
||||
r == ',',
|
||||
r == '/',
|
||||
r == ' ':
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
shellUnsafeCharNotSpace := func(r rune) bool {
|
||||
return r != ' ' && shellUnsafeChar(r)
|
||||
}
|
||||
|
||||
if strings.IndexFunc(s, shellUnsafeCharNotSpace) == -1 {
|
||||
// No escaping necessary
|
||||
return s
|
||||
}
|
||||
|
||||
return `'` + singleQuoteReplacer.Replace(s) + `'`
|
||||
}
|
||||
|
||||
// ShellEscapeIncludingSpaces escapes the input `s` in a similar way to ShellEscape except that
|
||||
// this treats spaces as meaningful characters.
|
||||
func ShellEscapeIncludingSpaces(s string) string {
|
||||
if strings.IndexFunc(s, shellUnsafeChar) == -1 {
|
||||
// No escaping necessary
|
||||
return s
|
||||
|
|
|
@ -91,6 +91,24 @@ var shellEscapeTestCase = []escapeTestCase{
|
|||
},
|
||||
}
|
||||
|
||||
var shellEscapeIncludingSpacesTestCase = []escapeTestCase{
|
||||
{
|
||||
name: "no escaping",
|
||||
in: `test`,
|
||||
out: `test`,
|
||||
},
|
||||
{
|
||||
name: "spacing",
|
||||
in: `arg1 arg2`,
|
||||
out: `'arg1 arg2'`,
|
||||
},
|
||||
{
|
||||
name: "single quote",
|
||||
in: `'arg'`,
|
||||
out: `''\''arg'\'''`,
|
||||
},
|
||||
}
|
||||
|
||||
func TestNinjaEscaping(t *testing.T) {
|
||||
for _, testCase := range ninjaEscapeTestCase {
|
||||
got := NinjaEscape(testCase.in)
|
||||
|
@ -109,6 +127,15 @@ func TestShellEscaping(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestShellEscapeIncludingSpaces(t *testing.T) {
|
||||
for _, testCase := range shellEscapeIncludingSpacesTestCase {
|
||||
got := ShellEscapeIncludingSpaces(testCase.in)
|
||||
if got != testCase.out {
|
||||
t.Errorf("%s: expected `%s` got `%s`", testCase.name, testCase.out, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExternalShellEscaping(t *testing.T) {
|
||||
if testing.Short() {
|
||||
return
|
||||
|
@ -124,3 +151,19 @@ func TestExternalShellEscaping(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExternalShellEscapeIncludingSpaces(t *testing.T) {
|
||||
if testing.Short() {
|
||||
return
|
||||
}
|
||||
for _, testCase := range shellEscapeIncludingSpacesTestCase {
|
||||
cmd := "echo -n " + ShellEscapeIncludingSpaces(testCase.in)
|
||||
got, err := exec.Command("/bin/sh", "-c", cmd).Output()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if string(got) != testCase.in {
|
||||
t.Errorf("%s: expected `%s` got `%s`", testCase.name, testCase.in, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue