diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-10-14 20:48:46 +0300 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-10-14 20:48:46 +0300 |
commit | 23f44a53708ee460b25e070de28c7c42165b3087 (patch) | |
tree | d120647e709cbba1dbc133b6fd5e81a2ddf96e24 | |
parent | Use crates.io/rustyline as readline (diff) | |
download | rshell-23f44a53708ee460b25e070de28c7c42165b3087.tar.xz |
Check arguments passed to cd
-rw-r--r-- | README.md | 5 | ||||
-rw-r--r-- | man/rs.1.scd | 25 | ||||
-rw-r--r-- | man/rs.5.scd | 24 | ||||
-rw-r--r-- | src/builtins.rs | 22 | ||||
-rw-r--r-- | src/main.rs | 10 | ||||
-rw-r--r-- | src/parser.rs | 2 |
6 files changed, 76 insertions, 12 deletions
@@ -13,6 +13,9 @@ It's a shell written in Rust. You build it yourself. Clone the repository, and run cargo build +To build the manpages you need scdoc: + scdoc < man/rs.1.scd > rs.1 + scdoc < man/rs.5.scd > rs.5 + ### It doesn't work, what now? -Tough luck diff --git a/man/rs.1.scd b/man/rs.1.scd new file mode 100644 index 0000000..f6ac4ef --- /dev/null +++ b/man/rs.1.scd @@ -0,0 +1,25 @@ +rs(1) + +# NAME + +rs - yet another shell + +# DESCRIPTION + +This is yet another shell + +# BUILTINS + +## cd(new_dir) +change directory to new_dir + +## exit +quit the shell +TODO: check background jobs + +# SEE ALSO +rs(5) for configuration options + +# AUTHORS +Maintained by Aqua <aqua@iserlohn-fortress.net>. The source code can be found +at https://neueland.iserlohn-fortress.net/cgit/rshell/. diff --git a/man/rs.5.scd b/man/rs.5.scd new file mode 100644 index 0000000..952c350 --- /dev/null +++ b/man/rs.5.scd @@ -0,0 +1,24 @@ +rs(5) "rs configuration" + +# NAME + +rs - yet another shell + +# DESCRIPTION + +This is yet another shell + +# MOTD +The motd printed on startup is read from /etc/motd. +TODO: disable toggle + +# PROMPT +To set the prompt, use the PROMPT environment variable. If one is not set, + " {USER}@{HOST} [{PWD}]\n{$} " +is used by default. + +- {USER} is replaced by username +- {HOST} is replaced by hostname +- {$} is replaced by '#' for root and '$' for other users +- {PWD} is replaced by current directory path + diff --git a/src/builtins.rs b/src/builtins.rs index ad41e59..ac83efa 100644 --- a/src/builtins.rs +++ b/src/builtins.rs @@ -1,17 +1,27 @@ use std::path::Path; use std::process::Command; -pub fn cd(new_dir: &str) -> std::path::PathBuf { - let root = match std::fs::canonicalize(Path::new(new_dir)) { +pub fn cd( + args: &[&str], + home: &std::path::PathBuf, +) -> Result<std::path::PathBuf, std::string::String> { + if args.len() > 1 { + return Err("Too many arguments passed to cd".to_string()); + } + + if args.len() == 0 { + return Ok(home.to_path_buf()); + } + + let root = match std::fs::canonicalize(Path::new(args[0])) { Ok(p) => p, Err(_) => Path::new("/").to_path_buf(), }; - if let Err(e) = std::env::set_current_dir(&root) { - eprintln!("{}", e); + match std::env::set_current_dir(&root) { + Ok(_) => Ok(root.to_path_buf()), + Err(e) => Err(format!("Could not set current dir: {}", e)), } - - root.to_path_buf() } pub fn run(command: &str, args: &[&str]) { diff --git a/src/main.rs b/src/main.rs index e5e7a53..aa6998d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,15 +19,19 @@ fn main() { /*if rl.load_history("history.txt").is_err() { println!("No previous history."); }*/ - loop { + 'repl: loop { let readline = rl.readline(&prompt.print(&cwd)); match readline { Ok(line) => { rl.add_history_entry(line.as_str()); let commands = parser::parse(&line); match commands[0] { - "cd" => { - cwd = builtins::cd(commands[1]); + "cd" => match builtins::cd(&commands[1..], &prompt.home) { + Ok(p) => cwd = p, + Err(e) => eprintln!("Error: {}", e), + }, + "exit" => { + break 'repl; } _ => { builtins::run(commands[0], &commands[1..]); diff --git a/src/parser.rs b/src/parser.rs index 4b37074..f69aced 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,5 +1,3 @@ -//use std::process::{Command, Stdio}; - // > overwrite // >> append // | pipe |