From 491d3fbff384d4b04483b54e5bb78d23bb1181c5 Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Thu, 28 Mar 2024 23:26:02 -0600 Subject: Remove hard-coded PATH Use $PATH, instead of a hard-coded PATH from main(). This means that there is no longer a need to pass around PATH to repl()/recite()/path::refresh(), since path::refresh() can call env::var directly. Since the hard-coded paths were removed, there needs to be some way to define $PATH. When running the debug build, dwvsh will look in 'dist/etc/dwvshrc' for the initial environment setup. For the release target, dwvsh will look in '/etc/dwvshrc'. After the global rc file is sourced, dwvsh will try to source ~/.dwvshrc if it exists, so users can extend their environment without root access (assuming a release install). --- src/main.rs | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 951c46f..ce5f611 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,31 +1,28 @@ use std::io::{self, Write}; -use std::path::Path; use std::sync::{Arc, Mutex}; mod path; mod poem; use poem::{read::Readable, recite::Reciteable, Poem}; +mod compose; /// Starts the main shell loop /// /// # Arguments -/// * `path` - A reference to a vector that holds a list to the shell $PATHs /// * `prompt` - A string slice indicating the shell's prompt /// * `at_prompt` - A mutex, indicating whether or not user is at the prompt /// /// # Examples /// ``` /// fn main() { -/// let path = vec!["/bin"]; -/// let path = path.into_iter().map(Path::new).collect(); /// let prompt = "|> "; /// let mut at_prompt = Arc::new(Mutex::new(false)); /// ... -/// repl(&path, prompt, &mut at_prompt); +/// repl(prompt, &mut at_prompt); /// } /// ``` -fn repl(path: &Vec<&Path>, prompt: &str, at_prompt: &mut Arc>) { +fn repl(prompt: &str, at_prompt: &mut Arc>) { // Initial path refresh on startup - let mut bins: Vec = path::refresh(path); + let mut bins: Vec = path::refresh(); // Main shell loop loop { @@ -70,7 +67,7 @@ fn repl(path: &Vec<&Path>, prompt: &str, at_prompt: &mut Arc>) { }; // Recite the poem - match poem.recite(path, &mut bins, None) { + match poem.recite(&mut bins, None) { Ok(_) => {} Err(e) => eprintln!("dwvsh: {}", e.to_string().to_lowercase()), } @@ -81,17 +78,10 @@ fn repl(path: &Vec<&Path>, prompt: &str, at_prompt: &mut Arc>) { /// /// Shell setup and entry fn main() { - // Define paths - // TODO: Hardcoded path should only be the fallback - let path = vec![ - "/bin", - "/sbin", - "/usr/bin", - "/usr/sbin", - "/usr/local/bin", - "/usr/local/sbin", - ]; - let path = path.into_iter().map(Path::new).collect(); + // Compose the environment for dwvsh + // TODO: All instances of `env!("HOME")` need to be changed to use env::var + // TODO: Will probably need to set $HOME in dwv{profile,login} via passwd + compose::env(); // Set the prompt let prompt = "|> "; @@ -112,5 +102,5 @@ fn main() { }; // Begin evaluating commands - repl(&path, prompt, &mut at_prompt); + repl(prompt, &mut at_prompt); } -- cgit v1.2.3