aboutsummaryrefslogtreecommitdiff
path: root/src/builtins.rs
blob: ac83efaf59cfec1742e365e8b1b1e040714a1f8f (plain)
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
use std::path::Path;
use std::process::Command;

pub fn cd(
    args: &[&str],
    home: &std::path::PathBuf,
) -> Result<std::path::PathBuf, std::string::String> {
    if args.len() > 1 {
        return Err("Too many arguments passed to cd".to_string());
    }

    if args.len() == 0 {
        return Ok(home.to_path_buf());
    }

    let root = match std::fs::canonicalize(Path::new(args[0])) {
        Ok(p) => p,
        Err(_) => Path::new("/").to_path_buf(),
    };

    match std::env::set_current_dir(&root) {
        Ok(_) => Ok(root.to_path_buf()),
        Err(e) => Err(format!("Could not set current dir: {}", e)),
    }
}

pub fn run(command: &str, args: &[&str]) {
    let mut child = Command::new(command).args(args).spawn().unwrap();
    child.wait().unwrap();
}
/*
    match command {

        command => {
            let stdin = match previous_command {
                None => Stdio::inherit(),
                Some(cmd) => Stdio::from(cmd.stdout.unwrap()),
            };
            let stdout = match commands.peek().is_some() {
                true => Stdio::piped(),
                false => Stdio::inherit(),
            };

            let child = Command::new(command)
                .args(args)
                .stdin(stdin)
                .stdout(stdout)
                .spawn();
            match child {
                Ok(c) => previous_command = Some(c),
                Err(e) => {
                    previous_command = None;
                    eprintln!("{}", e);
                }
            }
        }
    }
}

if let Some(mut final_command) = previous_command {
    match final_command.wait() {
        Ok(ret) => match ret.code() {
            Some(code) => {
                if cfg!(debug_assertions) {
                    println!("exit code [{}]", code);
                }
            }
            None => println!("Process termed by signal"),
        },
        Err(e) => eprintln!("error waiting on final command: {}", e),
    }
}*/