|
| 1 | +using System; |
| 2 | +using System.Text; |
| 3 | + |
| 4 | +namespace Renci.SshNet |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Quotes a path in a way to be suitable to be used with a shell. |
| 8 | + /// </summary> |
| 9 | + internal class RemotePathQuoteTransformation : IRemotePathTransformation |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Quotes a path in a way to be suitable to be used with a shell. |
| 13 | + /// </summary> |
| 14 | + /// <param name="path">The path to transform.</param> |
| 15 | + /// <returns> |
| 16 | + /// A quoted path. |
| 17 | + /// </returns> |
| 18 | + /// <exception cref="ArgumentNullException"><paramref name="path"/> is <c>null</c>.</exception> |
| 19 | + /// <remarks> |
| 20 | + /// <para> |
| 21 | + /// If <paramref name="path"/> contains a single-quote, that character is embedded |
| 22 | + /// in quotation marks (eg. "'"). Sequences of single-quotes are grouped in a single |
| 23 | + /// pair of quotation marks. |
| 24 | + /// </para> |
| 25 | + /// <para> |
| 26 | + /// If a shell command contains an exclamation mark (!), the C-Shell interprets it as a |
| 27 | + /// meta-character for history substitution. This even works inside single-quotes or |
| 28 | + /// quotation marks, unless escaped with a backslash (\). |
| 29 | + /// </para> |
| 30 | + /// <para> |
| 31 | + /// References: |
| 32 | + /// <list type="bullet"> |
| 33 | + /// <item> |
| 34 | + /// <description><a href="http://pubs.opengroup.org/onlinepubs/7908799/xcu/chap2.html">Shell Command Language</a></description> |
| 35 | + /// </item> |
| 36 | + /// <item> |
| 37 | + /// <description><a href="https://earthsci.stanford.edu/computing/unix/shell/specialchars.php">Unix C-Shell special characters and their uses</a></description> |
| 38 | + /// </item> |
| 39 | + /// <item> |
| 40 | + /// <description><a href="https://docstore.mik.ua/orelly/unix3/upt/ch27_13.htm">Differences Between Bourne and C Shell Quoting</a></description> |
| 41 | + /// </item> |
| 42 | + /// </list> |
| 43 | + /// </para> |
| 44 | + /// </remarks> |
| 45 | + /// <returns> |
| 46 | + /// The transformed path. |
| 47 | + /// </returns> |
| 48 | + /// <exception cref="ArgumentNullException"><paramref name="path"/> is <c>null</c>.</exception> |
| 49 | + public string Transform(string path) |
| 50 | + { |
| 51 | + if (path == null) |
| 52 | + { |
| 53 | + throw new ArgumentNullException("path"); |
| 54 | + } |
| 55 | + |
| 56 | + // result is at least value and (likely) leading/trailing single-quotes |
| 57 | + var sb = new StringBuilder(path.Length + 2); |
| 58 | + var state = ShellQuoteState.Unquoted; |
| 59 | + |
| 60 | + foreach (var c in path) |
| 61 | + { |
| 62 | + switch (c) |
| 63 | + { |
| 64 | + case '\'': |
| 65 | + // embed a single-quote in quotes |
| 66 | + switch (state) |
| 67 | + { |
| 68 | + case ShellQuoteState.Unquoted: |
| 69 | + // Start quoted string |
| 70 | + sb.Append('"'); |
| 71 | + break; |
| 72 | + case ShellQuoteState.Quoted: |
| 73 | + // Continue quoted string |
| 74 | + break; |
| 75 | + case ShellQuoteState.SingleQuoted: |
| 76 | + // Close single-quoted string |
| 77 | + sb.Append('\''); |
| 78 | + // Start quoted string |
| 79 | + sb.Append('"'); |
| 80 | + break; |
| 81 | + } |
| 82 | + state = ShellQuoteState.Quoted; |
| 83 | + break; |
| 84 | + case '!': |
| 85 | + // In C-Shell, an exclamatation point can only be protected from shell interpretation |
| 86 | + // when escaped by a backslash |
| 87 | + // Source: |
| 88 | + // https://earthsci.stanford.edu/computing/unix/shell/specialchars.php |
| 89 | + |
| 90 | + switch (state) |
| 91 | + { |
| 92 | + case ShellQuoteState.Unquoted: |
| 93 | + sb.Append('\\'); |
| 94 | + break; |
| 95 | + case ShellQuoteState.Quoted: |
| 96 | + // Close quoted string |
| 97 | + sb.Append('"'); |
| 98 | + sb.Append('\\'); |
| 99 | + break; |
| 100 | + case ShellQuoteState.SingleQuoted: |
| 101 | + // Close single quoted string |
| 102 | + sb.Append('\''); |
| 103 | + sb.Append('\\'); |
| 104 | + break; |
| 105 | + } |
| 106 | + state = ShellQuoteState.Unquoted; |
| 107 | + break; |
| 108 | + default: |
| 109 | + switch (state) |
| 110 | + { |
| 111 | + case ShellQuoteState.Unquoted: |
| 112 | + // Start single-quoted string |
| 113 | + sb.Append('\''); |
| 114 | + break; |
| 115 | + case ShellQuoteState.Quoted: |
| 116 | + // Close quoted string |
| 117 | + sb.Append('"'); |
| 118 | + // Start single-quoted string |
| 119 | + sb.Append('\''); |
| 120 | + break; |
| 121 | + case ShellQuoteState.SingleQuoted: |
| 122 | + // Continue single-quoted string |
| 123 | + break; |
| 124 | + } |
| 125 | + state = ShellQuoteState.SingleQuoted; |
| 126 | + break; |
| 127 | + } |
| 128 | + |
| 129 | + sb.Append(c); |
| 130 | + } |
| 131 | + |
| 132 | + switch (state) |
| 133 | + { |
| 134 | + case ShellQuoteState.Unquoted: |
| 135 | + break; |
| 136 | + case ShellQuoteState.Quoted: |
| 137 | + // Close quoted string |
| 138 | + sb.Append('"'); |
| 139 | + break; |
| 140 | + case ShellQuoteState.SingleQuoted: |
| 141 | + // Close single-quoted string |
| 142 | + sb.Append('\''); |
| 143 | + break; |
| 144 | + } |
| 145 | + |
| 146 | + if (sb.Length == 0) |
| 147 | + { |
| 148 | + sb.Append("''"); |
| 149 | + } |
| 150 | + |
| 151 | + return sb.ToString(); |
| 152 | + } |
| 153 | + |
| 154 | + private enum ShellQuoteState |
| 155 | + { |
| 156 | + Unquoted = 1, |
| 157 | + SingleQuoted = 2, |
| 158 | + Quoted = 3 |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments