diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 106 |
1 files changed, 2 insertions, 104 deletions
diff --git a/src/main.rs b/src/main.rs index e94ce0c..9d8ed49 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,18 +2,10 @@ use crate::result::Result; use crate::terminal::{Input, Terminal}; -use std::collections::HashMap; -use std::collections::HashSet; -use std::env; -use std::os::unix::fs::PermissionsExt; -use std::process::{self, Command}; +use std::process; use tokio::io::{self, AsyncWriteExt}; -#[macro_use] extern crate lalrpop_util; - -lalrpop_mod!(pub commandline); pub mod error; -pub mod path; pub mod result; pub mod terminal; @@ -67,101 +59,7 @@ async fn prompt() -> Result<()> { } -async fn execute(input: &str) -> Result<()> { - let invocation = commandline::InvocationParser::new().parse(input)?; - - match invocation.as_slice() { - ["environment", ..] => { - let environment = read_environment()?; - println!("{:?}", environment); - } - ["which", command_name, ..] => { - match find_executable_path(command_name)? { - Some(executable_path) => { - println!("{}", executable_path); - } - None => { - println!("Command not found: {}", command_name); - } - }; - }, - [command_name, ..] => { - match find_executable_path(command_name)? { - Some(executable_path) => { - let mut command = Command::new(executable_path.to_sys_path()); - - let arguments = &invocation[1..]; - command.args(arguments); - - let _status = command.status()?; - } - None => { - println!("Command not found: {}", command_name); - } - }; - }, - _ => { - println!("invocation '{:?}'", invocation); - } - } - +async fn execute(_input: &str) -> Result<()> { Ok(()) } - -fn read_environment() -> Result<HashMap<String,String>> { - Ok(env::vars().collect()) -} - - -fn get_environment(variable_name: &str) -> Result<Option<String>> { - Ok(env::vars() - .find(|(key, _)| key == variable_name) - .map(|(_, value)| value)) -} - - -fn get_search_paths() -> Result<Vec<path::AbsoluteDirectoryPath>> { - let paths = get_environment("PATH")?.unwrap_or_default(); - let paths = path::parse_path_list(&paths)?; - - let mut result = Vec::new(); - let mut seen = HashSet::new(); - for path in paths { - if seen.contains(&path) { - continue; - } - - seen.insert(path.clone()); - result.push(path); - } - - Ok(result) -} - - -fn find_executable_path(command_name: &str) - -> Result<Option<path::AbsoluteFilePath>> -{ - let file_name: path::FileName = command_name.parse()?; - let search_paths = get_search_paths()?; - - let mut executable_path: Option<path::AbsoluteFilePath> = None; - for search_path in &search_paths { - let candidate_path = search_path.concat_file_name(&file_name); - match candidate_path.to_sys_path().metadata() { - Ok(metadata) => { - if metadata.is_file() - && metadata.permissions().mode() & 0o111 != 0 - { - executable_path = Some(candidate_path); - break; - } - }, - Err(_) => { }, - } - } - - Ok(executable_path) -} - |