diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-10-17 21:55:42 +0300 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-10-17 21:55:42 +0300 |
commit | dade76c84c2dac619938571b83eab4d8465b3927 (patch) | |
tree | db1b9a764f405a440bc379d886b6c9270abcdce4 | |
parent | Add new tokenizer (diff) | |
download | rshell-dade76c84c2dac619938571b83eab4d8465b3927.tar.xz |
Update manpage
Add makefile to build manpages with
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | man/rs.1.scd | 40 | ||||
-rw-r--r-- | man/rs.5.scd | 24 | ||||
-rw-r--r-- | src/main.rs | 8 | ||||
-rw-r--r-- | src/parser/command.rs | 59 |
5 files changed, 80 insertions, 57 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ec06e96 --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +PREFIX ?= /usr/local +MAN_PREFIX ?= ${PREFIX}/man + +manpage: + scdoc < man/rs.1.scd > man/rs.1 + diff --git a/man/rs.1.scd b/man/rs.1.scd index f6ac4ef..6256453 100644 --- a/man/rs.1.scd +++ b/man/rs.1.scd @@ -10,15 +10,37 @@ 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 +|[ *Builtin* +:- Arguments +:- Description +| ! +: +:[ Show the status of the last command run. +| cd +: (dest) +:[ Change the current directory. If one is not provided, go to home instead. +| exit +: +:[ Quit the shell +| set +: key value +:[ Set the 'key' environment variable to 'value'. +| unset +: key +:[ Remove the 'key' environment variable. + +# MOTD +The motd printed on startup is read from /etc/motd. + +# 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 # AUTHORS Maintained by Aqua <aqua@iserlohn-fortress.net>. The source code can be found diff --git a/man/rs.5.scd b/man/rs.5.scd deleted file mode 100644 index 952c350..0000000 --- a/man/rs.5.scd +++ /dev/null @@ -1,24 +0,0 @@ -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/main.rs b/src/main.rs index ed0a576..cb67e45 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,16 +12,17 @@ fn main() { let prompt = prompt::Prompt::new().unwrap(); let mut cwd = std::env::current_dir().unwrap(); - let mut status: Option<std::process::ExitStatus> = None; + let mut status = None; // `()` can be used when no completer is required let mut rl = Editor::<()>::new(); + /*if rl.load_history("history.txt").is_err() { println!("No previous history."); }*/ + 'repl: loop { - let readline = rl.readline(&prompt.print(&cwd)); - match readline { + match rl.readline(&prompt.print(&cwd)) { Ok(line) => { rl.add_history_entry(line.as_str()); @@ -46,5 +47,6 @@ fn main() { } } } + //rl.save_history("history.txt").unwrap(); } diff --git a/src/parser/command.rs b/src/parser/command.rs index 496614f..52b4666 100644 --- a/src/parser/command.rs +++ b/src/parser/command.rs @@ -1,5 +1,6 @@ use std::io::{Error, ErrorKind}; use std::path::{Path, PathBuf}; +use std::process::{Command, ExitStatus}; #[derive(Debug)] pub enum Redirect { @@ -16,7 +17,7 @@ pub enum RunOn { } pub enum RunResult { - Command(std::process::ExitStatus), + Command(ExitStatus), Builtin, } @@ -40,34 +41,28 @@ impl CommandInfo { &self, home: &PathBuf, cwd: &mut PathBuf, - status: &Option<std::process::ExitStatus>, + status: &Option<ExitStatus>, ) -> Result<RunResult, Error> { match self.args[0].as_str() { "!" => { println!("{:?}", status); Ok(RunResult::Builtin) } - "cd" => match cd(&self.args[1..], home) { - Ok(p) => { - *cwd = p; - Ok(RunResult::Builtin) - } - Err(e) => Err(e), - }, + "cd" => cd(&self.args[1..], home, cwd), "exit" => { std::process::exit(0); } + "set" => set(&self.args[1..]), + "unset" => unset(&self.args[1..]), _ => { - let mut child = std::process::Command::new(&self.args[0]) - .args(&self.args[1..]) - .spawn()?; + let mut child = Command::new(&self.args[0]).args(&self.args[1..]).spawn()?; Ok(RunResult::Command(child.wait().unwrap())) } } } } -fn cd(args: &[String], home: &PathBuf) -> Result<PathBuf, Error> { +fn cd(args: &[String], home: &PathBuf, cwd: &mut PathBuf) -> Result<RunResult, Error> { if args.len() > 1 { return Err(Error::new( ErrorKind::InvalidInput, @@ -75,17 +70,39 @@ fn cd(args: &[String], home: &PathBuf) -> Result<PathBuf, Error> { )); } - if args.len() == 0 { - return Ok(home.to_path_buf()); - } - - let root = match std::fs::canonicalize(Path::new(args[0].as_str())) { - Ok(p) => p, - Err(_) => Path::new("/").to_path_buf(), + let root = if args.len() == 0 { + home.to_path_buf() + } else { + std::fs::canonicalize(Path::new(args[0].as_str()))? }; match std::env::set_current_dir(&root) { - Ok(_) => Ok(root.to_path_buf()), + Ok(_) => { + *cwd = root; + Ok(RunResult::Builtin) + } Err(e) => Err(e), } } + +fn set(args: &[String]) -> Result<RunResult, Error> { + if args.len() != 2 { + return Err(Error::new( + ErrorKind::InvalidInput, + format!("set requires 2 arguments, got {}", args.len()), + )); + } + std::env::set_var(&args[0], &args[1]); + Ok(RunResult::Builtin) +} + +fn unset(args: &[String]) -> Result<RunResult, Error> { + if args.len() != 1 { + return Err(Error::new( + ErrorKind::InvalidInput, + format!("unset requires 1 argument, got {}", args.len()), + )); + } + std::env::remove_var(&args[0]); + Ok(RunResult::Builtin) +} |