From 23f44a53708ee460b25e070de28c7c42165b3087 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Wed, 14 Oct 2020 20:48:46 +0300 Subject: Check arguments passed to cd --- README.md | 5 ++++- man/rs.1.scd | 25 +++++++++++++++++++++++++ man/rs.5.scd | 24 ++++++++++++++++++++++++ src/builtins.rs | 22 ++++++++++++++++------ src/main.rs | 10 +++++++--- src/parser.rs | 2 -- 6 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 man/rs.1.scd create mode 100644 man/rs.5.scd diff --git a/README.md b/README.md index 7b49642..defd563 100644 --- a/README.md +++ b/README.md @@ -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 . 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 { + 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 -- cgit v1.2.1