aboutsummaryrefslogtreecommitdiff
path: root/src/parser/command.rs
blob: 52b4666591281211e64f811f089cd1d96f1161fa (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
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
use std::io::{Error, ErrorKind};
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus};

#[derive(Debug)]
pub enum Redirect {
    Std,
    FileOverwrite(String),
    FileAppend(String),
}

#[derive(Debug, PartialEq)]
pub enum RunOn {
    Always,
    ExitSuccess,
    ExitFailure,
}

pub enum RunResult {
    Command(ExitStatus),
    Builtin,
}

#[derive(Debug)]
pub struct CommandInfo {
    pub args: Vec<String>,
    pub stdout: Redirect,
    pub when: RunOn,
}

impl CommandInfo {
    pub fn new() -> CommandInfo {
        CommandInfo {
            args: Vec::new(),
            stdout: Redirect::Std,
            when: RunOn::Always,
        }
    }

    pub fn run(
        &self,
        home: &PathBuf,
        cwd: &mut PathBuf,
        status: &Option<ExitStatus>,
    ) -> Result<RunResult, Error> {
        match self.args[0].as_str() {
            "!" => {
                println!("{:?}", status);
                Ok(RunResult::Builtin)
            }
            "cd" => cd(&self.args[1..], home, cwd),
            "exit" => {
                std::process::exit(0);
            }
            "set" => set(&self.args[1..]),
            "unset" => unset(&self.args[1..]),
            _ => {
                let mut child = Command::new(&self.args[0]).args(&self.args[1..]).spawn()?;
                Ok(RunResult::Command(child.wait().unwrap()))
            }
        }
    }
}

fn cd(args: &[String], home: &PathBuf, cwd: &mut PathBuf) -> Result<RunResult, Error> {
    if args.len() > 1 {
        return Err(Error::new(
            ErrorKind::InvalidInput,
            "Too many arguments passed to cd",
        ));
    }

    let root = if args.len() == 0 {
        home.to_path_buf()
    } else {
        std::fs::canonicalize(Path::new(args[0].as_str()))?
    };

    match std::env::set_current_dir(&root) {
        Ok(_) => {
            *cwd = root;
            Ok(RunResult::Builtin)
        }
        Err(e) => Err(e),
    }
}

fn set(args: &[String]) -> Result<RunResult, Error> {
    if args.len() != 2 {
        return Err(Error::new(
            ErrorKind::InvalidInput,
            format!("set requires 2 arguments, got {}", args.len()),
        ));
    }
    std::env::set_var(&args[0], &args[1]);
    Ok(RunResult::Builtin)
}

fn unset(args: &[String]) -> Result<RunResult, Error> {
    if args.len() != 1 {
        return Err(Error::new(
            ErrorKind::InvalidInput,
            format!("unset requires 1 argument, got {}", args.len()),
        ));
    }
    std::env::remove_var(&args[0]);
    Ok(RunResult::Builtin)
}