aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/builtins.rs22
-rw-r--r--src/main.rs10
-rw-r--r--src/parser.rs2
3 files changed, 23 insertions, 11 deletions
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