Brijesh's Git Server — bsh @ master

my own shell program

src/executor.rs (view raw)

 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
use crate::{error::ShellError, parser};
use std::env;
use std::process::{Command, Stdio};

pub fn execute(input: &str) -> Result<(), ShellError> {
    // Use the new parser to get the command and arguments
    let parsed_command = parser::parse(input).map_err(|e| ShellError::ParseError(e.to_string()))?;

    match parsed_command.command.as_str() {
        // Handle built-in commands here
        "cd" => {
            let target_dir = if let Some(dir) = parsed_command.args.get(0) {
                // Expand ~ to home directory
                expand_tilde(dir)
            } else {
                // If no argument, default to home directory
                env::var("HOME").map_err(|e| ShellError::EnvVarError(e.to_string()))?
            };

            std::env::set_current_dir(&target_dir).map_err(ShellError::from)?;
        }
        _ => {
            let mut child = Command::new(&parsed_command.command)
                .args(&parsed_command.args)
                .stdin(Stdio::inherit())
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .spawn()
                .map_err(|_| ShellError::CommandNotFound(parsed_command.command.clone()))?;

            child.wait().map_err(ShellError::from)?;
        }
    }
    Ok(())
}

// Function to expand tilde (~) to home directory
fn expand_tilde(path: &str) -> String {
    if path.starts_with('~') {
        let home = env::var("HOME").unwrap_or_else(|_| String::from("/"));
        if path == "~" {
            home
        } else {
            format!("{}{}", home, &path[1..]) // Append the rest of the path after ~
        }
    } else {
        path.to_string() // Return the original path if not starting with ~
    }
}