PPaste!

toto

Home - All the pastes - Authored by Thooms

Raw version

 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
// I am the shell you are currently executing
use std::io::{self, BufRead, Write};
use std::process::{Command, Stdio};
use std::env;
use std::str;

fn valid(s: &[u8]) -> bool {
    s.iter().all(|&c| (b'a' <= c && c <= b'z') || c == b' ' || c == b'>')
}

fn prompt() {
    print!("$ ");
    io::stdout().flush().unwrap();
}

fn run(cmd: &str) -> Vec<u8> {
    Command::new("/bin/bash")
        .arg("-c")
        .arg(cmd)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .output()
        .unwrap()
        .stdout
}

fn main() {
    env::set_current_dir("/dev/shm").unwrap();

    let stdin = io::stdin();
    let mut stdout = io::stdout();

    prompt();
    for l in stdin.lock().split(b'\n') {
        let l = l.unwrap();
        if valid(&l) {
            let l = String::from_utf8(l).unwrap();
            let data = run(&l);
            stdout.write(&data).unwrap();
        } else {
            println!("Bad character");
        }
        prompt();
    }
}