1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#![cfg(feature = "fish")]

use crate::{Quotable, QuoteInto};

/// Quote byte strings for use with fish.
///
/// # ⚠️ Warning
///
/// Prior to version 3.6.2, fish did not correctly handle some Unicode code
/// points encoded as UTF-8. From the [version 3.6.2 release notes][]:
///
/// > fish uses certain Unicode non-characters internally for marking wildcards
/// > and expansions. It incorrectly allowed these markers to be read on command
/// > substitution output, rather than transforming them into a safe internal
/// > representation.
///
/// [version 3.6.2 release notes]:
///   https://github.com/fish-shell/fish-shell/releases/tag/3.6.2
///
/// At present this crate has **no workaround** for this issue. Please use fish
/// 3.6.2 or later.
///
/// # Notes
///
/// The documentation on [quoting][] and [escaping characters][] in fish is
/// confusing at first, especially when coming from a Bourne-like shell, but
/// essentially we have to be able to move and and out of a quoted string
/// context. For example, the escape sequence `\t` for a tab _must_ be outside
/// of quotes, single or double, to be recognised as a tab character by fish:
///
/// ```fish
/// echo 'foo'\t'bar'
/// ```
///
/// This emphasises the importance of using the correct quoting module for the
/// target shell.
///
/// [quoting]: https://fishshell.com/docs/current/language.html#quotes
/// [escaping characters]:
///     https://fishshell.com/docs/current/language.html#escaping-characters
#[derive(Debug, Clone, Copy)]
pub struct Fish;

impl QuoteInto<Vec<u8>> for Fish {
    fn quote_into<'q, S: ?Sized + Into<Quotable<'q>>>(s: S, out: &mut Vec<u8>) {
        Self::quote_into_vec(s, out);
    }
}

impl QuoteInto<String> for Fish {
    fn quote_into<'q, S: ?Sized + Into<Quotable<'q>>>(s: S, out: &mut String) {
        Self::quote_into_vec(s, unsafe { out.as_mut_vec() })
    }
}

#[cfg(unix)]
impl QuoteInto<std::ffi::OsString> for Fish {
    fn quote_into<'q, S: ?Sized + Into<Quotable<'q>>>(s: S, out: &mut std::ffi::OsString) {
        use std::os::unix::ffi::OsStringExt;
        let s = Self::quote_vec(s);
        let s = std::ffi::OsString::from_vec(s);
        out.push(s);
    }
}

#[cfg(feature = "bstr")]
impl QuoteInto<bstr::BString> for Fish {
    fn quote_into<'q, S: ?Sized + Into<Quotable<'q>>>(s: S, out: &mut bstr::BString) {
        let s = Self::quote_vec(s);
        out.extend(s);
    }
}

impl Fish {
    /// Quote a string of bytes into a new `Vec<u8>`.
    ///
    /// This will return one of the following:
    /// - The string as-is, if no escaping is necessary.
    /// - An escaped string, like `'foo \'bar'`, `\a'ABC'`
    ///
    /// See [`quote_into_vec`][`Self::quote_into_vec`] for a variant that
    /// extends an existing `Vec` instead of allocating a new one.
    ///
    /// # Examples
    ///
    /// ```
    /// # use shell_quote::Fish;
    /// assert_eq!(Fish::quote_vec("foobar"), b"foobar");
    /// assert_eq!(Fish::quote_vec("foo 'bar"), b"foo' \\'bar'");
    /// ```
    pub fn quote_vec<'a, S: ?Sized + Into<Quotable<'a>>>(s: S) -> Vec<u8> {
        match s.into() {
            Quotable::Bytes(bytes) => match bytes::escape_prepare(bytes) {
                bytes::Prepared::Empty => vec![b'\'', b'\''],
                bytes::Prepared::Inert => bytes.into(),
                bytes::Prepared::Escape(esc) => {
                    let mut sout = Vec::new();
                    bytes::escape_chars(esc, &mut sout);
                    sout
                }
            },
            Quotable::Text(text) => match text::escape_prepare(text) {
                text::Prepared::Empty => vec![b'\'', b'\''],
                text::Prepared::Inert => text.into(),
                text::Prepared::Escape(esc) => {
                    let mut sout = Vec::new();
                    text::escape_chars(esc, &mut sout);
                    sout
                }
            },
        }
    }

    /// Quote a string of bytes into an existing `Vec<u8>`.
    ///
    /// See [`quote_vec`][`Self::quote_vec`] for more details.
    ///
    /// # Examples
    ///
    /// ```
    /// # use shell_quote::Fish;
    /// let mut buf = Vec::with_capacity(128);
    /// Fish::quote_into_vec("foobar", &mut buf);
    /// buf.push(b' ');  // Add a space.
    /// Fish::quote_into_vec("foo 'bar", &mut buf);
    /// assert_eq!(buf, b"foobar foo' \\'bar'");
    /// ```
    ///
    pub fn quote_into_vec<'a, S: ?Sized + Into<Quotable<'a>>>(s: S, sout: &mut Vec<u8>) {
        match s.into() {
            Quotable::Bytes(bytes) => match bytes::escape_prepare(bytes) {
                bytes::Prepared::Empty => sout.extend(b"''"),
                bytes::Prepared::Inert => sout.extend(bytes),
                bytes::Prepared::Escape(esc) => bytes::escape_chars(esc, sout),
            },
            Quotable::Text(text) => match text::escape_prepare(text) {
                text::Prepared::Empty => sout.extend(b"''"),
                text::Prepared::Inert => sout.extend(text.as_bytes()),
                text::Prepared::Escape(esc) => text::escape_chars(esc, sout),
            },
        }
    }
}

// ----------------------------------------------------------------------------

mod bytes {
    use super::u8_to_hex_escape_uppercase_x;
    use crate::ascii::Char;

    pub enum Prepared {
        Empty,
        Inert,
        Escape(Vec<Char>),
    }

    pub fn escape_prepare(sin: &[u8]) -> Prepared {
        let esc: Vec<_> = sin.iter().map(Char::from).collect();
        // An optimisation: if the string is not empty and contains only "safe"
        // characters we can avoid further work.
        if esc.is_empty() {
            Prepared::Empty
        } else if esc.iter().all(Char::is_inert) {
            Prepared::Inert
        } else {
            Prepared::Escape(esc)
        }
    }

    pub fn escape_chars(esc: Vec<Char>, sout: &mut Vec<u8>) {
        #[derive(PartialEq)]
        enum QuoteStyle {
            Inside,
            Outside,
            Whatever,
        }
        use QuoteStyle::*;

        let mut inside_quotes_now = false;
        let mut push_literal = |style: QuoteStyle, literal: &[u8]| {
            match (inside_quotes_now, style) {
                (true, Outside) => {
                    sout.push(b'\'');
                    inside_quotes_now = false;
                }
                (false, Inside) => {
                    sout.push(b'\'');
                    inside_quotes_now = true;
                }
                _ => (),
            }
            sout.extend(literal);
        };
        for mode in esc {
            use Char::*;
            match mode {
                Bell => push_literal(Outside, b"\\a"),
                Backspace => push_literal(Outside, b"\\b"),
                Escape => push_literal(Outside, b"\\e"),
                FormFeed => push_literal(Outside, b"\\f"),
                NewLine => push_literal(Outside, b"\\n"),
                CarriageReturn => push_literal(Outside, b"\\r"),
                HorizontalTab => push_literal(Outside, b"\\t"),
                VerticalTab => push_literal(Outside, b"\\v"),
                Control(ch) => push_literal(Outside, &u8_to_hex_escape_uppercase_x(ch)),
                Backslash => push_literal(Whatever, b"\\\\"),
                SingleQuote => push_literal(Whatever, b"\\'"),
                DoubleQuote => push_literal(Inside, b"\""),
                Delete => push_literal(Outside, b"\\X7F"),
                PrintableInert(ch) => push_literal(Whatever, &ch.to_le_bytes()),
                Printable(ch) => push_literal(Inside, &ch.to_le_bytes()),
                Extended(ch) => push_literal(Outside, &u8_to_hex_escape_uppercase_x(ch)),
            }
        }
        if inside_quotes_now {
            sout.push(b'\'');
        }
    }
}

// ----------------------------------------------------------------------------

mod text {
    use super::u8_to_hex_escape_uppercase_x;
    use crate::utf8::Char;

    pub enum Prepared {
        Empty,
        Inert,
        Escape(Vec<Char>),
    }

    pub fn escape_prepare(sin: &str) -> Prepared {
        let esc: Vec<_> = sin.chars().map(Char::from).collect();
        // An optimisation: if the string is not empty and contains only "safe"
        // characters we can avoid further work.
        if esc.is_empty() {
            Prepared::Empty
        } else if esc.iter().all(Char::is_inert) {
            Prepared::Inert
        } else {
            Prepared::Escape(esc)
        }
    }

    pub fn escape_chars(esc: Vec<Char>, sout: &mut Vec<u8>) {
        #[derive(PartialEq)]
        enum QuoteStyle {
            Inside,
            Outside,
            Whatever,
        }
        use QuoteStyle::*;

        let mut inside_quotes_now = false;
        let mut push_literal = |style: QuoteStyle, literal: &[u8]| {
            match (inside_quotes_now, style) {
                (true, Outside) => {
                    sout.push(b'\'');
                    inside_quotes_now = false;
                }
                (false, Inside) => {
                    sout.push(b'\'');
                    inside_quotes_now = true;
                }
                _ => (),
            }
            sout.extend(literal);
        };
        let buf = &mut [0u8; 4];
        for mode in esc {
            use Char::*;
            match mode {
                Bell => push_literal(Outside, b"\\a"),
                Backspace => push_literal(Outside, b"\\b"),
                Escape => push_literal(Outside, b"\\e"),
                FormFeed => push_literal(Outside, b"\\f"),
                NewLine => push_literal(Outside, b"\\n"),
                CarriageReturn => push_literal(Outside, b"\\r"),
                HorizontalTab => push_literal(Outside, b"\\t"),
                VerticalTab => push_literal(Outside, b"\\v"),
                Control(ch) => push_literal(Outside, &u8_to_hex_escape_uppercase_x(ch)),
                Backslash => push_literal(Whatever, b"\\\\"),
                SingleQuote => push_literal(Whatever, b"\\'"),
                DoubleQuote => push_literal(Inside, b"\""),
                Delete => push_literal(Outside, b"\\X7F"),
                PrintableInert(ch) => push_literal(Whatever, &ch.to_le_bytes()),
                Printable(ch) => push_literal(Inside, &ch.to_le_bytes()),
                Utf8(char) => push_literal(Inside, char.encode_utf8(buf).as_bytes()),
            }
        }
        if inside_quotes_now {
            sout.push(b'\'');
        }
    }
}

// ----------------------------------------------------------------------------

/// Escape a byte as a 4-byte hex escape sequence _with uppercase "X"_.
///
/// The `\\XHH` format (backslash, a literal "X", two hex characters) is
/// understood by fish. The `\\xHH` format is _also_ understood, but until fish
/// 3.6.0 it had a weirdness. From the [release notes][]:
///
/// > The `\\x` and `\\X` escape syntax is now equivalent. `\\xAB` previously
/// > behaved the same as `\\XAB`, except that it would error if the value “AB”
/// > was larger than “7f” (127 in decimal, the highest ASCII value).
///
/// [release notes]: https://github.com/fish-shell/fish-shell/releases/tag/3.6.0
///
#[inline]
fn u8_to_hex_escape_uppercase_x(ch: u8) -> [u8; 4] {
    const HEX_DIGITS: &[u8] = b"0123456789ABCDEF";
    [
        b'\\',
        b'X',
        HEX_DIGITS[(ch >> 4) as usize],
        HEX_DIGITS[(ch & 0xF) as usize],
    ]
}

#[cfg(test)]
#[test]
fn test_u8_to_hex_escape_uppercase_x() {
    for ch in u8::MIN..=u8::MAX {
        let expected = format!("\\X{ch:02X}");
        let observed = u8_to_hex_escape_uppercase_x(ch);
        let observed = std::str::from_utf8(&observed).unwrap();
        assert_eq!(observed, &expected);
    }
}