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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//! **rsbash:** run bash commands from rust.
//!
//! Our macros [`rash!`](macro@rash) and [`rashf!`](macro@rashf) allow you to call out to a bash shell, just as you would typically from a terminal.
//! Since this is accomplished by interacting with libc, these macros can only be used on unix-like platforms (Linux, macOS etc).
//!
//! ## Motivation
//!
//! Making a shell command with the native [`std::process::Command`](https://doc.rust-lang.org/std/process/struct.Command.html) builder is _quite_ involved.
//!
//! Suppose you wanted to write "Hello world!" to stdout.
//!```
//! use std::io::Write;
//! use std::process::Command;
//!
//! let command = Command::new("echo")
//!               .arg("Hello world!")
//!               .output()
//!               .expect("Uh oh, couldn't say hello!");
//! std::io::stdout().write_all(&command.stdout).unwrap();
//!
//! assert_eq!(std::str::from_utf8(&command.stdout).unwrap(), "Hello world!\n");
//! ```
//!
//! Now suppose you wanted to pipe the output to a second command, and then write the result to stdout:
//! ```
//! use std::process::{Command, Stdio};
//! use std::io::Write;
//!
//! let echo = Command::new("echo")
//!            .arg("Hello world!")
//! 		   .stdout(Stdio::piped())
//! 		   .spawn()
//! 		   .expect("Uh oh, couldn't say hello!");
//! 					   
//! let grep = Command::new("grep")
//!            .arg("Hello")
//!            .stdin(Stdio::from(echo.stdout.unwrap()))
//!            .output()
//!            .expect("Uh oh, couldn't grep for Hello!");
//!     
//! std::io::stdout().write_all(&grep.stdout).unwrap();
//!
//! assert_eq!(std::str::from_utf8(&grep.stdout).unwrap(), "Hello world!\n");
//! ```
//!
//! With [`rash!`](macro@rash) the same command is as simple as:
//!
//!```
//! use rsbash::rash;
//!
//! let (ret_val, stdout, stderr) = rash!("echo 'Hello world!' | grep 'Hello'").unwrap();
//! assert_eq!(stdout, "Hello world!\n");
//! ```
//!
//! See the [`rash!`](macro@rash) and [`rashf!`](macro@rashf) macros, and the [`RashError`](enum@RashError) for more information.
#[macro_use]
extern crate lazy_static;

pub use crate::error::RashError;

mod command;
mod error;
mod process;
#[doc(hidden)]
pub mod shell;

/// Run a bash command.
///
/// #### Arguments:
/// `rash!` expects a single argument of a String or string literal (more specifically, any `AsRef<str>`).
///
/// #### Returns:
/// `rash!` returns a `Result<(i32, String, String), RashError>`.
///
/// The `(i32, String, String)` tuple contains the **return value**, the **stdout** and the **stderr** of the command, respectively.
///
/// See [`RashError`](enum@RashError) for more details of the error.
///
/// # Examples
///#### A simple command:
///```
/// use rsbash::{rash, RashError};
///
/// pub fn simple() -> Result<(), RashError> {
///     let (ret_val, stdout, stderr) = rash!("echo -n 'Hello world!'")?;
///     assert_eq!(ret_val, 0);
///     assert_eq!(stdout, "Hello world!");
///     assert_eq!(stderr, "");
///     Ok(())
/// }
/// ```
///
/// #### A _less_ simple command:
/// ```
/// use rsbash::{rash, RashError};
///
/// pub fn less_simple() -> Result<(), RashError> {
///     let (ret_val, stdout, _) =
///         rash!("echo -n 'Hello ' | cat - && printf '%s' $(echo -n 'world!')")?;
///     assert_eq!(ret_val, 0);
///     assert_eq!(stdout, "Hello world!");
///     Ok(())
/// }
/// ```
///
/// #### Using non-string literals:
///
///```
/// use rsbash::{rash, RashError};
///
/// const SCRIPT: &'static str = r#"
/// s="*"
/// for i in {1..3}; do
///     echo "$s"
///     s="$s *"
/// done;
/// "#;
///
/// const OUTPUT: &'static str = r#"\
/// *
/// * *
/// * * *"#;
///
/// pub fn non_string_literals() -> Result<(), RashError> {
///     let (ret_val, stdout, stderr) = rash!(SCRIPT)?;
///     assert_eq!(ret_val, 0);
///     assert_eq!(stdout, OUTPUT);
///     assert_eq!(stderr, "");
///
///     Ok(assert_eq!(rash!(String::from("echo hi >&2"))?, (0, "".to_string(), "hi".to_string())))
/// }
/// ```
///
/// # Compile errors
/// #### Passing a non-string literal as an argument:
/// ```compile_fail
/// use rsbash::{rash, RashError};
///
/// pub fn wrong_type() -> Result<(), RashError> {
///     let (ret_val, stdout, stderr) = rash!(35345)?;          // "the trait `AsRef<str>` is not implemented for `{integer}`"
///     Ok(())
/// }
/// ```
///
/// #### Passing either no arguments, or more than one argument:
/// ```compile_fail
/// use rsbash::{rash, RashError};
///
/// pub fn wrong_arg_count() -> Result<(), RashError> {
///     let (ret_val, stdout, stderr) = rash!()?;               // "missing tokens in macro arguments."
///     let (ret_val, stdout, stderr) = rash!("blah", "blah")?; // "no rules expected this token in macro call."
///     Ok(())
/// }
/// ```
///
#[cfg(unix)]
#[macro_export]
macro_rules! rash {
    ($arg:expr) => {
        $crate::shell::__command($arg)
    };
}

/// Format and run a bash command.
///
/// #### Arguments:
/// `rashf!` expects at least a single argument of a string literal representing the command to run.
/// Any further arguments should be formatting arguments to the command.
///
/// This syntax is the exact syntax of the well-known and well-loved `format!` macro, see [`std::fmt`](https://doc.rust-lang.org/stable/std/fmt/) for more details.
///
/// #### Returns:
/// `rashf!` returns a `Result<(i32, String, String), RashError>`.
///
/// The `(i32, String, String)` tuple contains the **return value**, the **stdout** and the **stderr** of the command, respectively.
///
/// See [`RashError`](enum@RashError) for more details of the error.
///
/// # Examples
///
/// #### Formatting:
///
/// Format `rashf!` commands just like you would [`format!`](https://doc.rust-lang.org/stable/std/fmt/) strings normally!
///
///```
/// use rsbash::{rashf, RashError};
///
/// pub fn simple_formatting() -> Result<(), RashError> {
///     let what = "Hello";
///     let who = "world!";
///     let (ret_val, stdout, stderr) = rashf!("echo -n '{} {}!'", what, who)?;
///     assert_eq!(ret_val, 0);
///     assert_eq!(stdout, "Hello world!");
///     assert_eq!(stderr, "");
///     Ok(())
/// }
/// ```
///
/// ```
/// use rsbash::rashf;
/// use tempfile::TempDir;
///
/// const MESSAGE: &'static str = "Hi from within foo.sh!";
///
/// pub fn formatting() -> anyhow::Result<()> {
///     let dir = TempDir::new()?;
///     let path = dir.path().to_str().unwrap();
///     let (ret_val, stdout, stderr) = rashf!(
///        "cd {path}; echo -n \"echo -n '{msg}'\" > foo.sh; chmod u+x foo.sh; ./foo.sh;",
///        msg = MESSAGE
///     )?;
///
///     assert_eq!(ret_val, 0);
///     assert_eq!(stdout, MESSAGE);
///     assert_eq!(stderr, "");
///     Ok(())
/// }
/// ```
///
/// # Compile errors
/// #### Passing a non-string literal as an argument:
/// ```compile_fail
/// use rsbash::{rash, RashError};
///
/// pub fn wrong_type() -> Result<(), RashError> {
///     let (ret_val, stdout, stderr) = rash!(35345)?; // "format argument must be a string literal"
///     Ok(())
/// }
/// ```
///
/// #### Passing no arguments:
/// ```compile_fail
/// use rsbash::{rash, RashError};
///
/// pub fn no_args() -> Result<(), RashError> {
///     let (ret_val, stdout, stderr) = rash!()?;     // "requires at least a format string argument"
///     Ok(())
/// }
/// ```
///
/// # A word on security
/// Sometimes the ease and flexibility of bash is exactly what you're after.
/// But, with great power comes great responsibility, and so I'd be remiss if I wasn't to mention
/// that formatting bash commands in this manner exposes a vulnerability in the form of a SQL injection-like attack:
///
/// ```
/// use rsbash::{rashf, RashError};
///
/// pub fn vulnerability() -> Result<(), RashError> {
///     let untrustworthy_user = "";                       // Suppose untrustworthy_user was set to "; reboot;"
///     let (ret_val, stdout, stderr) =                    // Uh oh! The command would have been formatted into
///         rashf!("echo -n Hello {untrustworthy_user}")?; // "echo -n Hello; reboot";
///     Ok(())
/// }
/// ```
///
/// Of course, best practices such as proper escaping, validating user input and so on would have circumvented
/// the above vulnerability. But, as a general rule only use formatted `rashf!` commands in situations
/// where you know for certain you can trust the inputs.
///
#[cfg(unix)]
#[macro_export]
macro_rules! rashf {
    ($($arg:tt)*) => {
        $crate::shell::__command(format!($($arg)*))
    };
}

#[cfg(test)]
mod tests {
    use crate::RashError;

    const COMMAND: &'static str = "echo -n hi";

    lazy_static! {
        static ref EMPTY_STRING: String = String::default();
    }

    mod rash {
        use super::*;

        #[test]
        fn test_rash_with_a_single_string_literal() -> Result<(), RashError> {
            Ok(assert_eq!(rash!("echo -n hi")?, (0, "hi".to_string(), EMPTY_STRING.clone())))
        }

        #[test]
        fn test_rash_with_non_string_literals() -> Result<(), RashError> {
            let command = "echo -n hi".to_string();
            let expected = (0, "hi".to_string(), EMPTY_STRING.clone());

            assert_eq!(rash!(command)?, expected);
            assert_eq!(rash!(COMMAND)?, expected);
            Ok(())
        }

        #[test]
        fn test_rash_with_expressions() -> Result<(), RashError> {
            let message = "echo -n hi";
            let expected = (0, "hi".to_string(), EMPTY_STRING.clone());

            assert_eq!(rash!(message.to_string())?, expected);
            assert_eq!(rash!(format!("{message}"))?, expected);
            Ok(())
        }
    }

    mod rashf {
        use super::*;

        #[test]
        fn test_rashf_with_a_single_string_literal() -> Result<(), RashError> {
            Ok(assert_eq!(rashf!("echo -n hi")?, (0, "hi".to_string(), EMPTY_STRING.clone())))
        }

        #[test]
        fn test_rashf_with_formatted_non_string_literals() -> Result<(), RashError> {
            let command = "echo -n hi".to_string();
            let expected = (0, "hi".to_string(), EMPTY_STRING.clone());

            assert_eq!(rashf!("{}", command)?, expected);
            assert_eq!(rashf!("{}", COMMAND)?, expected);
            Ok(())
        }

        #[test]
        fn test_rashf_with_simple_formatting() -> Result<(), RashError> {
            let expected = (0, "hi bye".to_string(), EMPTY_STRING.clone());
            assert_eq!(rashf!("echo -n {} {}", "hi", "bye")?, expected);

            let hi = "hi";
            let bye = "bye".to_string();
            Ok(assert_eq!(rashf!("echo -n {} {}", hi, bye)?, expected))
        }

        #[test]
        fn test_rashf_with_variable_capture_formatting() -> Result<(), RashError> {
            let (one, two) = (1, 2);
            Ok(assert_eq!(
                rashf!("echo -n '{one} + {two}'")?,
                (0, "1 + 2".to_string(), EMPTY_STRING.clone())
            ))
        }

        #[test]
        fn test_rashf_with_positional_parameters() -> Result<(), RashError> {
            let (one, three) = (1, 3);
            Ok(assert_eq!(
                rashf!("echo -n '{one} + {two} + {three}'", two = 2)?,
                (0, "1 + 2 + 3".to_string(), EMPTY_STRING.clone())
            ))
        }
    }
}