summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorRory Dudley2024-03-28 23:26:02 -0600
committerRory Dudley2024-03-28 23:26:02 -0600
commit491d3fbff384d4b04483b54e5bb78d23bb1181c5 (patch)
tree470b0fc2ab0a476d682e104bdb03275ffd6b8671 /src/main.rs
parent14a74aea0f02da53e0f61c572da2c5244ed80551 (diff)
downloaddwarvish-491d3fbff384d4b04483b54e5bb78d23bb1181c5.tar.gz
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).
Notes
Notes: Throughout a lot of this program, we're calling `env!("HOME")`, in order to get the user's home directory. Technically, this is not correct. The env!() macro resolves environment variables during compile time, while env::var() gets environment variables for the running process (i.e. the shell). See https://users.rust-lang.org/t/env-vs-env-var/88119 for more info. In the near future, this will need to be addressed. Might be worth looking into what other shells do, though one idea I had was to invoke '/usr/bin/id', grab the user's ID, and use it to grab the rest of the info from /etc/passwd. This would be handled in an /etc/dwvlogin or /etc/dwvprofile most likely.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs30
1 files changed, 10 insertions, 20 deletions
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<Mutex<bool>>) {
+fn repl(prompt: &str, at_prompt: &mut Arc<Mutex<bool>>) {
// Initial path refresh on startup
- let mut bins: Vec<String> = path::refresh(path);
+ let mut bins: Vec<String> = path::refresh();
// Main shell loop
loop {
@@ -70,7 +67,7 @@ fn repl(path: &Vec<&Path>, prompt: &str, at_prompt: &mut Arc<Mutex<bool>>) {
};
// 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<Mutex<bool>>) {
///
/// 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);
}