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
//! How to use this crate
//! # Adding this as a dependency
//! ```rust, ignore
//! [dependencies]
//! rust_process_interface_library = "^0.1"
//! ```
//!
//! # Bringing this into scope
//! ```rust, ignore
//! use rust_process_interface_library::Command;
//! ```
//! # Tests
//! ```bash, ignore
//! cargo test --lib
//! ```

use std::collections::HashMap;
use std::env;
use std::ffi::CString;

/// The output of a finished process.
///
/// This is returned in a Result by the [`output`] method of a [`Command`].
pub struct Output {
    /// The status (exit code) of the process.
    pub status: i32,
    /// The data that the process wrote to stdout.
    pub stdout: Vec<u8>,
    /// The data that the process wrote to stderr.
    pub stderr: Vec<u8>,
}

pub mod ssvm_process {
    use std::os::raw::c_char;
    #[link(wasm_import_module = "ssvm_process")]
    extern "C" {
        pub fn ssvm_process_set_prog_name(name: *const c_char, len: u32);
        pub fn ssvm_process_add_arg(arg: *const c_char, len: u32);
        pub fn ssvm_process_add_env(
            env: *const c_char,
            env_len: u32,
            val: *const c_char,
            val_len: u32,
        );
        pub fn ssvm_process_add_stdin(buf: *const c_char, len: u32);
        pub fn ssvm_process_set_timeout(time_ms: u32);
        pub fn ssvm_process_run() -> i32;
        pub fn ssvm_process_get_exit_code() -> i32;
        pub fn ssvm_process_get_stdout_len() -> u32;
        pub fn ssvm_process_get_stdout(buf: *mut u8);
        pub fn ssvm_process_get_stderr_len() -> u32;
        pub fn ssvm_process_get_stderr(buf: *mut u8);
    }
}

pub struct Command {
    /// The program name.
    pub name: String,
    /// The argument list.
    pub args_list: Vec<String>,
    /// The environment map.
    pub envp_map: HashMap<String, String>,
    /// The timeout value (milliseconds).
    pub timeout_val: u32,
    /// Buffered stdin.
    pub stdin_str: Vec<u8>,
}

impl Command {
    pub fn new<S: AsRef<str>>(prog: S) -> Command {
        let mut envp: HashMap<String, String> = HashMap::new();
        for (key, value) in env::vars() {
            envp.insert(key, value);
        }
        Command {
            name: String::from(prog.as_ref()),
            args_list: vec![],
            envp_map: envp,
            timeout_val: 10000,
            stdin_str: vec![],
        }
    }

    pub fn arg<S: AsRef<str>>(&mut self, arg: S) -> &mut Command {
        self.args_list.push(String::from(arg.as_ref()));
        self
    }

    pub fn args<I, S>(&mut self, args: I) -> &mut Command
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        for arg in args {
            self.arg(arg.as_ref());
        }
        self
    }

    pub fn args_clear(&mut self) -> &mut Command {
        self.args_list.clear();
        self
    }

    pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
    where
        K: AsRef<str>,
        V: AsRef<str>,
    {
        self.envp_map
            .insert(String::from(key.as_ref()), String::from(val.as_ref()));
        self
    }

    pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command
    where
        I: IntoIterator<Item = (K, V)>,
        K: AsRef<str>,
        V: AsRef<str>,
    {
        for (ref key, ref val) in vars {
            self.env(key.as_ref(), val.as_ref());
        }
        self
    }

    pub fn stdin<S: AsRef<str>>(&mut self, buf: S) -> &mut Command {
        self.stdin_str
            .extend(CString::new(buf.as_ref()).expect("").as_bytes());
        self
    }

    pub fn stdin_u8(&mut self, buf: u8) -> &mut Command {
        self.stdin_str.push(buf);
        self
    }

    pub fn stdin_u8vec<S: AsRef<[u8]>>(&mut self, buf: S) -> &mut Command {
        self.stdin_str.extend(buf.as_ref());
        self
    }

    pub fn timeout(&mut self, time: u32) -> &mut Command {
        self.timeout_val = time;
        self
    }

    pub fn output(&mut self) -> Output {
        unsafe {
            // Set program name.
            let cprog = CString::new((&self.name).as_bytes()).expect("");
            ssvm_process::ssvm_process_set_prog_name(cprog.as_ptr(), cprog.as_bytes().len() as u32);

            // Set arguments.
            for arg in &self.args_list {
                let carg = CString::new(arg.as_bytes()).expect("");
                ssvm_process::ssvm_process_add_arg(carg.as_ptr(), carg.as_bytes().len() as u32);
            }

            // Set environments.
            for (key, val) in &self.envp_map {
                let ckey = CString::new(key.as_bytes()).expect("");
                let cval = CString::new(val.as_bytes()).expect("");
                ssvm_process::ssvm_process_add_env(
                    ckey.as_ptr(),
                    ckey.as_bytes().len() as u32,
                    cval.as_ptr(),
                    cval.as_bytes().len() as u32,
                );
            }

            // Set timeout.
            ssvm_process::ssvm_process_set_timeout(self.timeout_val);

            // Set stdin.
            ssvm_process::ssvm_process_add_stdin(
                self.stdin_str.as_ptr() as *const i8,
                self.stdin_str.len() as u32,
            );

            // Run.
            let exit_code = ssvm_process::ssvm_process_run();

            // Get outputs.
            let stdout_len = ssvm_process::ssvm_process_get_stdout_len();
            let stderr_len = ssvm_process::ssvm_process_get_stderr_len();
            let mut stdout_vec: Vec<u8> = vec![0; stdout_len as usize];
            let mut stderr_vec: Vec<u8> = vec![0; stderr_len as usize];
            let stdout_ptr = stdout_vec.as_mut_ptr();
            let stderr_ptr = stderr_vec.as_mut_ptr();
            ssvm_process::ssvm_process_get_stdout(stdout_ptr);
            ssvm_process::ssvm_process_get_stderr(stderr_ptr);

            Output {
                status: exit_code,
                stdout: stdout_vec,
                stderr: stderr_vec,
            }
        }
    }
}

// Test
// Please use the following command so that the print statements are shown during testing
// cargo test -- --nocapture
//

#[cfg(test)]
mod tests {
    use super::Command;
    #[test]
    fn test_arg() {
        let mut cmd = Command::new("rusttest");
        cmd.arg("val1").arg("val2");
        assert_eq!(cmd.args_list[0], "val1");
        assert_eq!(cmd.args_list[1], "val2");
    }
    #[test]
    fn test_args() {
        let mut cmd = Command::new("rusttest");
        cmd.args(&["val1", "val2"]);
        assert_eq!(cmd.args_list[0], "val1");
        assert_eq!(cmd.args_list[1], "val2");
    }
    #[test]
    fn test_arg_args() {
        let mut cmd = Command::new("rusttest");
        cmd.arg("val1").arg("val2").args(&["val3", "val4"]);
        assert_eq!(cmd.args_list[0], "val1");
        assert_eq!(cmd.args_list[1], "val2");
        assert_eq!(cmd.args_list[2], "val3");
        assert_eq!(cmd.args_list[3], "val4");
    }
    #[test]
    fn test_args_clear() {
        let mut cmd = Command::new("rusttest");
        cmd.arg("val1").arg("val2").args(&["val3", "val4"]);
        cmd.args_clear();
        assert_eq!(cmd.args_list.len(), 0);
    }
    #[test]
    fn test_env() {
        let mut cmd = Command::new("rusttest");
        cmd.env("ENV1", "VALUE1").env("ENV2", "VALUE2");
        assert_eq!(cmd.envp_map["ENV1"], "VALUE1");
        assert_eq!(cmd.envp_map["ENV2"], "VALUE2");
    }
    #[test]
    fn test_envs() {
        use std::collections::HashMap;
        let mut cmd = Command::new("rusttest");
        let mut hash: HashMap<String, String> = HashMap::new();
        hash.insert(String::from("ENV1"), String::from("VALUE1"));
        hash.insert(String::from("ENV2"), String::from("VALUE2"));
        cmd.envs(hash);
        assert_eq!(cmd.envp_map["ENV1"], "VALUE1");
        assert_eq!(cmd.envp_map["ENV2"], "VALUE2");
    }
    #[test]
    fn test_env_envs() {
        use std::collections::HashMap;
        let mut cmd = Command::new("rusttest");
        let mut hash: HashMap<String, String> = HashMap::new();
        hash.insert(String::from("ENV1"), String::from("VALUE1"));
        hash.insert(String::from("ENV2"), String::from("VALUE2"));
        cmd.env("ENV3", "VALUE3").env("ENV4", "VALUE4").envs(hash);
        assert_eq!(cmd.envp_map["ENV1"], "VALUE1");
        assert_eq!(cmd.envp_map["ENV2"], "VALUE2");
        assert_eq!(cmd.envp_map["ENV3"], "VALUE3");
        assert_eq!(cmd.envp_map["ENV4"], "VALUE4");
    }
    #[test]
    fn test_stdin() {
        use std::str;
        let mut cmd = Command::new("rusttest");
        cmd.stdin("hello").stdin(" ").stdin("world");
        assert_eq!(
            str::from_utf8(&cmd.stdin_str).expect("ERROR"),
            "hello world"
        );
    }
    #[test]
    fn test_stdin_u8() {
        let mut cmd = Command::new("rusttest");
        cmd.stdin("Test").stdin_u8(0).stdin_u8(100).stdin_u8(255);
        assert_eq!(cmd.stdin_str, vec![84, 101, 115, 116, 0, 100, 255]);
    }
    #[test]
    fn test_stdin_u8vec() {
        let mut cmd = Command::new("rusttest");
        let v = vec![5, 6, 7];
        cmd.stdin("Test")
            .stdin_u8vec(&v)
            .stdin_u8(100)
            .stdin_u8(255);
        assert_eq!(cmd.stdin_str, vec![84, 101, 115, 116, 5, 6, 7, 100, 255]);
        assert_eq!(v, vec![5, 6, 7]);
    }
    #[test]
    fn test_timeout() {
        let mut cmd = Command::new("rusttest");
        cmd.timeout(666666);
        assert_eq!(cmd.timeout_val, 666666);
    }
}