| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | "unicode" |
| 7 | "unicode/utf8" |
| 8 | ) |
| 9 | |
| 10 | // shellQuote quotes a word the way bash's `printf %q` does in a UTF-8 |
| 11 | // locale, so the commands status suggests can be pasted back into a |
| 12 | // shell and say exactly what bin/valley's own printf said. A branch name |
| 13 | // may hold most punctuation, and every character a shell would read |
| 14 | // specially is escaped with a backslash. A word holding a character that |
| 15 | // is not printable, or bytes that are not UTF-8, is written in $'...' |
| 16 | // form instead, with each such byte as an octal escape. |
| 17 | func shellQuote(s string) string { |
| 18 | if s == "" { |
| 19 | return "''" |
| 20 | } |
| 21 | if !printable(s) { |
| 22 | return ansiCQuote(s) |
| 23 | } |
| 24 | var b strings.Builder |
| 25 | for i, r := range s { |
| 26 | if r < utf8.RuneSelf && backslashed(byte(r), s, i) { |
| 27 | b.WriteByte('\\') |
| 28 | } |
| 29 | b.WriteRune(r) |
| 30 | } |
| 31 | return b.String() |
| 32 | } |
| 33 | |
| 34 | // backslashed is bash's table of characters a backslash must quote, plus |
| 35 | // the two it quotes by position: # opening a word starts a comment, and ~ |
| 36 | // opening a word or following : or = is a tilde expansion. |
| 37 | func backslashed(c byte, s string, i int) bool { |
| 38 | switch c { |
| 39 | case '\t', '\n', ' ', '!', '"', '$', '&', '\'', '(', ')', '*', ',', ';', '<', '>', |
| 40 | '?', '[', '\\', ']', '^', '`', '{', '|', '}': |
| 41 | return true |
| 42 | case '#': |
| 43 | return i == 0 |
| 44 | case '~': |
| 45 | return i == 0 || s[i-1] == ':' || s[i-1] == '=' |
| 46 | } |
| 47 | return false |
| 48 | } |
| 49 | |
| 50 | func printable(s string) bool { |
| 51 | if !utf8.ValidString(s) { |
| 52 | return false |
| 53 | } |
| 54 | for _, r := range s { |
| 55 | if !unicode.IsPrint(r) { |
| 56 | return false |
| 57 | } |
| 58 | } |
| 59 | return true |
| 60 | } |
| 61 | |
| 62 | // ansiCQuote is the $'...' form. A backslash and a single quote are |
| 63 | // escaped, the control characters with a name are written by it, and |
| 64 | // every other byte that is not part of a printable character is written |
| 65 | // as three octal digits. |
| 66 | func ansiCQuote(s string) string { |
| 67 | var b strings.Builder |
| 68 | b.WriteString("$'") |
| 69 | for i := 0; i < len(s); { |
| 70 | r, size := utf8.DecodeRuneInString(s[i:]) |
| 71 | switch { |
| 72 | case r == '\\' || r == '\'': |
| 73 | b.WriteByte('\\') |
| 74 | b.WriteRune(r) |
| 75 | case named[r] != 0: |
| 76 | b.WriteByte('\\') |
| 77 | b.WriteByte(named[r]) |
| 78 | case (r != utf8.RuneError || size > 1) && unicode.IsPrint(r): |
| 79 | b.WriteString(s[i : i+size]) |
| 80 | default: |
| 81 | for _, c := range []byte(s[i : i+size]) { |
| 82 | fmt.Fprintf(&b, "\\%03o", c) |
| 83 | } |
| 84 | } |
| 85 | i += size |
| 86 | } |
| 87 | b.WriteByte('\'') |
| 88 | return b.String() |
| 89 | } |
| 90 | |
| 91 | var named = map[rune]byte{ |
| 92 | '\x1b': 'E', '\a': 'a', '\v': 'v', '\b': 'b', '\f': 'f', '\n': 'n', '\r': 'r', '\t': 't', |
| 93 | } |