Colin Cross | 41ca49f | 2016-09-29 13:19:34 -0700 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package proptools |
| 16 | |
| 17 | import "strings" |
| 18 | |
Colin Cross | cc1ec0f | 2018-09-19 16:05:43 -0700 | [diff] [blame] | 19 | // NinjaEscapeList takes a slice of strings that may contain characters that are meaningful to ninja |
Colin Cross | 41ca49f | 2016-09-29 13:19:34 -0700 | [diff] [blame] | 20 | // ($), and escapes each string so they will be passed to bash. It is not necessary on input, |
| 21 | // output, or dependency names, those are handled by ModuleContext.Build. It is generally required |
| 22 | // on strings from properties in Blueprint files that are used as Args to ModuleContext.Build. A |
| 23 | // new slice containing the escaped strings is returned. |
Colin Cross | cc1ec0f | 2018-09-19 16:05:43 -0700 | [diff] [blame] | 24 | func NinjaEscapeList(slice []string) []string { |
Colin Cross | 41ca49f | 2016-09-29 13:19:34 -0700 | [diff] [blame] | 25 | slice = append([]string(nil), slice...) |
| 26 | for i, s := range slice { |
Colin Cross | cc1ec0f | 2018-09-19 16:05:43 -0700 | [diff] [blame] | 27 | slice[i] = NinjaEscape(s) |
Colin Cross | 41ca49f | 2016-09-29 13:19:34 -0700 | [diff] [blame] | 28 | } |
| 29 | return slice |
| 30 | } |
| 31 | |
Colin Cross | cc1ec0f | 2018-09-19 16:05:43 -0700 | [diff] [blame] | 32 | // NinjaEscapeList takes a string that may contain characters that are meaningful to ninja |
| 33 | // ($), and escapes it so it will be passed to bash. It is not necessary on input, |
| 34 | // output, or dependency names, those are handled by ModuleContext.Build. It is generally required |
| 35 | // on strings from properties in Blueprint files that are used as Args to ModuleContext.Build. A |
| 36 | // new slice containing the escaped strings is returned. |
| 37 | func NinjaEscape(s string) string { |
| 38 | return ninjaEscaper.Replace(s) |
| 39 | } |
| 40 | |
Colin Cross | 41ca49f | 2016-09-29 13:19:34 -0700 | [diff] [blame] | 41 | var ninjaEscaper = strings.NewReplacer( |
| 42 | "$", "$$") |
| 43 | |
Colin Cross | cc1ec0f | 2018-09-19 16:05:43 -0700 | [diff] [blame] | 44 | // ShellEscapeList takes a slice of strings that may contain characters that are meaningful to bash and |
| 45 | // escapes them if necessary by wrapping them in single quotes, and replacing internal single quotes with |
Colin Cross | 41ca49f | 2016-09-29 13:19:34 -0700 | [diff] [blame] | 46 | // '\'' (one single quote to end the quoting, a shell-escaped single quote to insert a real single |
| 47 | // quote, and then a single quote to restarting quoting. A new slice containing the escaped strings |
| 48 | // is returned. |
Colin Cross | cc1ec0f | 2018-09-19 16:05:43 -0700 | [diff] [blame] | 49 | func ShellEscapeList(slice []string) []string { |
| 50 | slice = append([]string(nil), slice...) |
| 51 | |
| 52 | for i, s := range slice { |
| 53 | slice[i] = ShellEscape(s) |
| 54 | } |
| 55 | return slice |
| 56 | |
| 57 | } |
| 58 | |
Jooyung Han | ddb5ed7 | 2021-03-08 20:42:32 +0900 | [diff] [blame] | 59 | func shellUnsafeChar(r rune) bool { |
| 60 | switch { |
| 61 | case 'A' <= r && r <= 'Z', |
| 62 | 'a' <= r && r <= 'z', |
| 63 | '0' <= r && r <= '9', |
| 64 | r == '_', |
| 65 | r == '+', |
| 66 | r == '-', |
| 67 | r == '=', |
| 68 | r == '.', |
| 69 | r == ',', |
| 70 | r == '/': |
| 71 | return false |
| 72 | default: |
| 73 | return true |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // ShellEscape takes string that may contain characters that are meaningful to bash and |
Colin Cross | cc1ec0f | 2018-09-19 16:05:43 -0700 | [diff] [blame] | 78 | // escapes it if necessary by wrapping it in single quotes, and replacing internal single quotes with |
| 79 | // '\'' (one single quote to end the quoting, a shell-escaped single quote to insert a real single |
| 80 | // quote, and then a single quote to restarting quoting. |
| 81 | func ShellEscape(s string) string { |
Jooyung Han | ddb5ed7 | 2021-03-08 20:42:32 +0900 | [diff] [blame] | 82 | shellUnsafeCharNotSpace := func(r rune) bool { |
| 83 | return r != ' ' && shellUnsafeChar(r) |
Colin Cross | 41ca49f | 2016-09-29 13:19:34 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Jooyung Han | ddb5ed7 | 2021-03-08 20:42:32 +0900 | [diff] [blame] | 86 | if strings.IndexFunc(s, shellUnsafeCharNotSpace) == -1 { |
| 87 | // No escaping necessary |
| 88 | return s |
| 89 | } |
| 90 | |
| 91 | return `'` + singleQuoteReplacer.Replace(s) + `'` |
| 92 | } |
| 93 | |
| 94 | // ShellEscapeIncludingSpaces escapes the input `s` in a similar way to ShellEscape except that |
| 95 | // this treats spaces as meaningful characters. |
| 96 | func ShellEscapeIncludingSpaces(s string) string { |
Colin Cross | cc1ec0f | 2018-09-19 16:05:43 -0700 | [diff] [blame] | 97 | if strings.IndexFunc(s, shellUnsafeChar) == -1 { |
| 98 | // No escaping necessary |
| 99 | return s |
Colin Cross | 41ca49f | 2016-09-29 13:19:34 -0700 | [diff] [blame] | 100 | } |
Colin Cross | 41ca49f | 2016-09-29 13:19:34 -0700 | [diff] [blame] | 101 | |
Colin Cross | cc1ec0f | 2018-09-19 16:05:43 -0700 | [diff] [blame] | 102 | return `'` + singleQuoteReplacer.Replace(s) + `'` |
Colin Cross | 41ca49f | 2016-09-29 13:19:34 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Colin Cross | cc1ec0f | 2018-09-19 16:05:43 -0700 | [diff] [blame] | 105 | func NinjaAndShellEscapeList(slice []string) []string { |
| 106 | return ShellEscapeList(NinjaEscapeList(slice)) |
| 107 | } |
| 108 | |
| 109 | func NinjaAndShellEscape(s string) string { |
| 110 | return ShellEscape(NinjaEscape(s)) |
Colin Cross | 41ca49f | 2016-09-29 13:19:34 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | var singleQuoteReplacer = strings.NewReplacer(`'`, `'\''`) |