Skip to content

Commit

Permalink
Add shell aliases (#357)
Browse files Browse the repository at this point in the history
* Add shell aliases

* Fix env output error

* Fix sort

* Read aliases from config file

* Simplify arguments parsing

* Fix test

* Clone params to spawn syscall

* Run clippy

* Revert "Clone params to spawn syscall"

This reverts commit 4c91bea.

* Disable binary stripping

* Remove exit alias

* Update doc
  • Loading branch information
vinc authored Jun 26, 2022
1 parent cd6cdce commit 82882ec
Show file tree
Hide file tree
Showing 18 changed files with 248 additions and 145 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ user-rust:
-C relocation-model=static
basename -s .rs src/bin/*.rs | xargs -I {} \
cp target/x86_64-moros/release/{} dsk/bin/{}
strip dsk/bin/*
#strip dsk/bin/*

bin = target/x86_64-moros/release/bootimage-moros.bin
img = disk.img
Expand Down
2 changes: 1 addition & 1 deletion doc/lisp.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ MOROS Lisp v0.1.0
> (+ 1 2)
3
> (exit)
> (quit)
```

And it can execute a file. For example a file located in `/tmp/fibonacci.lsp`
Expand Down
23 changes: 17 additions & 6 deletions doc/shell.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# MOROS Shell

## Config

The shell will read `/ini/shell.sh` during initialization to setup its
configuration.

## Commands

The main commands have a long name, a one-letter alias, and may have
additional common aliases.

<!--
**Alias** command:

> alias d delete

<!--
**Append** to file:
> a a.txt
Expand Down Expand Up @@ -130,20 +135,26 @@ Which is more efficient than doing:

Setting a variable in the shell environment is done with the following command:

> foo = "world"
> set foo 42

And accessing that variable is done with the `$` operator:
> set bar "Alice and Bob"

And accessing a variable is done with the `$` operator:

> print $foo
world
42

> print "hello $foo"
hello world
> print "Hello $bar"
Hello Alice and Bob

The process environment is copied to the shell environment when a session is
started. By convention a process env var should be in uppercase and a shell
env var should be lowercase.

Unsetting a variable is done like this:

> unset foo

## Globbing

MOROS Shell support filename expansion or globbing for `*` and `?` wildcard
Expand Down
Binary file modified dsk/bin/clear
Binary file not shown.
Binary file modified dsk/bin/halt
Binary file not shown.
Binary file modified dsk/bin/hello
Binary file not shown.
Binary file modified dsk/bin/print
Binary file not shown.
Binary file modified dsk/bin/reboot
Binary file not shown.
Binary file modified dsk/bin/sleep
Binary file not shown.
28 changes: 28 additions & 0 deletions dsk/ini/shell.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Command shortcuts
alias p print
alias c copy
alias d delete
alias del delete
alias e edit
alias f find
alias g goto
alias go goto
alias h help
alias l list
alias m move
alias q quit
alias r read
alias w write
alias sh shell
alias dsk disk
alias mem memory
alias kbd keyboard

# Unix compatibility
# alias cd goto
# alias cp copy
# alias echo print
# alias exit quit
# alias ls list
# alias mv move
# alias rm delete
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ fn main(boot_info: &'static BootInfo) -> ! {
print!("\x1b[?25h"); // Enable cursor
loop {
if let Some(cmd) = option_env!("MOROS_CMD") {
let mut env = usr::shell::default_env();
let prompt = usr::shell::prompt_string(true);
println!("{}{}", prompt, cmd);
usr::shell::exec(cmd, &mut env);
usr::shell::exec(cmd);
sys::acpi::shutdown();
} else {
user_boot();
Expand Down
2 changes: 1 addition & 1 deletion src/usr/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn repl() -> usr::shell::ExitCode {
prompt.history.load(history_file);

while let Some(line) = prompt.input(&prompt_string) {
if line == "exit" || line == "quit" {
if line == "quit" {
break;
}
if line.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion src/usr/chess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl Chess {
while let Some(cmd) = prompt.input(&prompt_string) {
let args: Vec<&str> = cmd.trim().split(' ').collect();
match args[0] {
"q" | "quit" | "exit" => break,
"q" | "quit" => break,
"h" | "help" => self.cmd_help(args),
"i" | "init" => self.cmd_init(args),
"t" | "time" => self.cmd_time(args),
Expand Down
34 changes: 21 additions & 13 deletions src/usr/env.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
use crate::{sys, usr};

pub fn main(args: &[&str]) -> usr::shell::ExitCode {
if args.len() == 1 {
for (key, val) in sys::process::envs() {
println!("{}={}", key, val);
match args.len() {
1 => {
for (key, val) in sys::process::envs() {
println!("{:10} \"{}\"", key, val);
}
usr::shell::ExitCode::CommandSuccessful
}
} else {
for arg in args[1..].iter() {
if let Some(i) = arg.find('=') {
let (key, mut val) = arg.split_at(i);
val = &val[1..];
sys::process::set_env(key, val);
println!("{}={}", key, val);
2 => {
let key = args[1];
if let Some(val) = sys::process::env(key) {
println!("{}", val);
usr::shell::ExitCode::CommandSuccessful
} else {
error!("Error: could not parse '{}'", arg);
return usr::shell::ExitCode::CommandError;
error!("Could not get '{}'", key);
usr::shell::ExitCode::CommandError
}
}
3 => {
sys::process::set_env(args[1], args[2]);
usr::shell::ExitCode::CommandSuccessful
}
_ => {
error!("Invalid number of arguments");
usr::shell::ExitCode::CommandError
}
}
usr::shell::ExitCode::CommandSuccessful
}
5 changes: 3 additions & 2 deletions src/usr/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ pub fn copy_files(verbose: bool) {
create_dev("/dev/random", DeviceType::Random, verbose);
create_dev("/dev/console", DeviceType::Console, verbose);

copy_file("/ini/boot.sh", include_bytes!("../../dsk/ini/boot.sh"), verbose);
copy_file("/ini/banner.txt", include_bytes!("../../dsk/ini/banner.txt"), verbose);
copy_file("/ini/version.txt", include_bytes!("../../dsk/ini/version.txt"), verbose);
copy_file("/ini/boot.sh", include_bytes!("../../dsk/ini/boot.sh"), verbose);
copy_file("/ini/palette.csv", include_bytes!("../../dsk/ini/palette.csv"), verbose);
copy_file("/ini/shell.sh", include_bytes!("../../dsk/ini/shell.sh"), verbose);
copy_file("/ini/version.txt", include_bytes!("../../dsk/ini/version.txt"), verbose);

create_dir("/ini/lisp", verbose);
copy_file("/ini/lisp/core.lsp", include_bytes!("../../dsk/ini/lisp/core.lsp"), verbose);
Expand Down
5 changes: 2 additions & 3 deletions src/usr/lisp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,7 @@ fn default_env() -> Rc<RefCell<Env>> {
data.insert("system".to_string(), Exp::Func(|args: &[Exp]| -> Result<Exp, Err> {
ensure_length_eq!(args, 1);
let cmd = string(&args[0])?;
let mut env = usr::shell::default_env();
let res = usr::shell::exec(&cmd, &mut env);
let res = usr::shell::exec(&cmd);
Ok(Exp::Num(res as u8 as f64))
}));
data.insert("print".to_string(), Exp::Func(|args: &[Exp]| -> Result<Exp, Err> {
Expand Down Expand Up @@ -673,7 +672,7 @@ fn repl(env: &mut Rc<RefCell<Env>>) -> usr::shell::ExitCode {
prompt.completion.set(&lisp_completer);

while let Some(line) = prompt.input(&prompt_string) {
if line == "(exit)" || line == "(quit)" {
if line == "(quit)" {
break;
}
if line.is_empty() {
Expand Down
Loading

0 comments on commit 82882ec

Please sign in to comment.