Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve shell #405

Merged
merged 4 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate alloc;

use bootloader::{entry_point, BootInfo};
use core::panic::PanicInfo;
use moros::{sys, usr, debug, print, println, hlt_loop};
use moros::{sys, usr, print, println, debug, hlt_loop};

entry_point!(main);

Expand Down
35 changes: 29 additions & 6 deletions src/usr/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ pub fn split_args(cmd: &str) -> Vec<String> {
n = j; // Discard comments
break;
} else if c == ' ' && !is_quote {
if i != j {
if i != j && !cmd[i..j].trim().is_empty() {
if args.is_empty() {
args.push(cmd[i..j].to_string())
args.push(cmd[i..j].to_string()) // program name
} else {
args.extend(glob(&cmd[i..j]))
args.extend(glob(&cmd[i..j])) // program args
}
}
i = j + 1;
Expand All @@ -178,12 +178,12 @@ pub fn split_args(cmd: &str) -> Vec<String> {
args.push(cmd[i..n].to_string());
} else if args.is_empty() {
args.push(cmd[i..n].to_string());
} else {
} else if !cmd[i..n].trim().is_empty() {
args.extend(glob(&cmd[i..n]))
}
}

if n == 0 || cmd.ends_with(' ') {
if n == 0 {
args.push("".to_string());
}

Expand All @@ -203,8 +203,9 @@ fn tilde_expansion(arg: &str) -> String {
fn variables_expansion(cmd: &str, config: &mut Config) -> String {
let mut cmd = cmd.to_string();

// Special case for `?` which is not alphanum (\w)
// Special cases for none alphanum (\w) variables
cmd = cmd.replace("$?", "$status");
cmd = cmd.replace("$*", "$1 $2 $3 $4 $5 $6 $7 $8 $9");

// Replace alphanum `$key` with its value in the environment or an empty string
let re = Regex::new("\\$\\w+");
Expand Down Expand Up @@ -482,6 +483,7 @@ fn exec_with_config(cmd: &str, config: &mut Config) -> Result<(), ExitCode> {
"user" => usr::user::main(&args),
"vga" => usr::vga::main(&args),
"write" => usr::write::main(&args),
"panic" => panic!("{}", args[1..].join(" ")),
_ => {
let mut path = fs::realpath(args[0]);
if path.len() > 1 {
Expand Down Expand Up @@ -627,6 +629,27 @@ fn test_shell() {
sys::fs::dismount();
}

#[test_case]
fn test_split_args() {
use alloc::vec;
assert_eq!(split_args(""), vec![""]);
assert_eq!(split_args("print"), vec!["print"]);
assert_eq!(split_args("print "), vec!["print"]);
assert_eq!(split_args("print "), vec!["print"]);
assert_eq!(split_args("print # comment"), vec!["print"]);
assert_eq!(split_args("print foo"), vec!["print", "foo"]);
assert_eq!(split_args("print foo "), vec!["print", "foo"]);
assert_eq!(split_args("print foo "), vec!["print", "foo"]);
assert_eq!(split_args("print foo # comment"), vec!["print", "foo"]);
assert_eq!(split_args("print foo bar"), vec!["print", "foo", "bar"]);
assert_eq!(split_args("print foo bar"), vec!["print", "foo", "bar"]);
assert_eq!(split_args("print foo bar"), vec!["print", "foo", "bar"]);
assert_eq!(split_args("print foo \"bar\""), vec!["print", "foo", "bar"]);
assert_eq!(split_args("print foo \"\""), vec!["print", "foo", ""]);
assert_eq!(split_args("print foo \"bar\" "), vec!["print", "foo", "bar"]);
assert_eq!(split_args("print foo \"\" "), vec!["print", "foo", ""]);
}

#[test_case]
fn test_glob_to_regex() {
assert_eq!(glob_to_regex("hello.txt"), "^hello\\.txt$");
Expand Down
3 changes: 2 additions & 1 deletion src/usr/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ pub fn login(username: &str) -> Result<(), ExitCode> {

let home = format!("/usr/{}", username);
sys::process::set_user(username);
sys::process::set_env("HOME", &home);
sys::process::set_dir(&home);
sys::process::set_env("USER", &username);
sys::process::set_env("HOME", &home);

// TODO: load shell
Ok(())
Expand Down