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 user experience #274

Merged
merged 7 commits into from
Nov 27, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
- Improve user experience (#274)
- Add process table and exit syscall (#268)
- Improve UTF-8 support (#267)
- Bump acpi from 4.0.0 to 4.1.0 (#265)
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2018"
license = "MIT"
repository = "https://github.com/vinc/moros"
readme = "README.md"
default-run = "moros"

[features]
default = ["video", "rtl8139"]
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ $(img):
image: $(img)
touch src/lib.rs
env | grep MOROS
cargo bootimage --no-default-features --features $(output),$(nic) --release --bin moros
cargo bootimage --no-default-features --features $(output),$(nic) --release
dd conv=notrunc if=$(bin) of=$(img)

opts = -m 32 -cpu max -nic model=$(nic) -hda $(img) -soundhw pcspk
Expand Down
31 changes: 21 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,32 @@ fn main(boot_info: &'static BootInfo) -> ! {
moros::init(boot_info);
print!("\x1b[?25h"); // Enable cursor
loop {
let bootrc = "/ini/boot.sh";
if sys::fs::File::open(bootrc).is_some() {
usr::shell::main(&["shell", bootrc]);
if let Some(cmd) = option_env!("MOROS_CMD") {
let prompt = usr::shell::prompt_string(true);
println!("{}{}", prompt, cmd);
usr::shell::exec(cmd);
sys::acpi::shutdown();
} else {
if sys::fs::is_mounted() {
println!("Could not find '{}'", bootrc);
} else {
println!("MFS is not mounted to '/'");
}
println!("Running console in diskless mode");
usr::shell::main(&["shell"]);
user_boot();
}
}
}

fn user_boot() {
let script = "/ini/boot.sh";
if sys::fs::File::open(script).is_some() {
usr::shell::main(&["shell", script]);
} else {
if sys::fs::is_mounted() {
println!("Could not find '{}'", script);
} else {
println!("MFS is not mounted to '/'");
}
println!("Running console in diskless mode");
usr::shell::main(&["shell"]);
}
}

#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
println!("{}", info);
Expand Down
3 changes: 0 additions & 3 deletions src/sys/fs/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ pub struct Block {
buf: [u8; super::BLOCK_SIZE],
}

// TODO: Add LinkedBlock that contains the next block address used by a data
// block and give Block the full block size for the data of a bitmap block.

// Block structure:
// 0..4 => next block address
// 4..512 => block data
Expand Down
6 changes: 3 additions & 3 deletions src/sys/fs/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ impl Dir {
}

pub fn open(pathname: &str) -> Option<Self> {
let pathname = realpath(pathname);
let mut dir = Dir::root();

if !super::is_mounted() {
return None;
}

let mut dir = Dir::root();
let pathname = realpath(pathname);

if pathname == "/" {
return Some(dir);
}
Expand Down
1 change: 1 addition & 0 deletions src/sys/fs/super_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl SuperBlock {
}
}

// NOTE: FS must be mounted
pub fn read() -> Self {
let block = Block::read(SUPERBLOCK_ADDR);
let data = block.data();
Expand Down
22 changes: 12 additions & 10 deletions src/usr/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use alloc::vec::Vec;

use nom::branch::alt;
use nom::character::complete::{char, space0};
use nom::number::complete::float;
use nom::number::complete::double;
use nom::combinator::map;
use nom::multi::many0;
use nom::sequence::{delimited, tuple};
Expand All @@ -21,7 +21,7 @@ use nom::IResult;

#[derive(Debug, PartialEq)]
pub enum Exp {
Num(f32),
Num(f64),
Add(Box<Exp>, Box<Exp>),
Sub(Box<Exp>, Box<Exp>),
Mul(Box<Exp>, Box<Exp>),
Expand Down Expand Up @@ -54,7 +54,7 @@ fn parse_parens(input: &str) -> IResult<&str, Exp> {
}

fn parse_num(input: &str) -> IResult<&str, Exp> {
map(delimited(space0, float, space0), Exp::Num)(input)
map(delimited(space0, double, space0), Exp::Num)(input)
}

fn parse_exp(exp: Exp, rem: Vec<(char, Exp)>) -> Exp {
Expand All @@ -75,20 +75,20 @@ fn parse_op(tup: (char, Exp), exp1: Exp) -> Exp {

// Evaluation

fn eval(exp: Exp) -> f32 {
fn eval(exp: Exp) -> f64 {
match exp {
Exp::Num(num) => num,
Exp::Add(exp1, exp2) => eval(*exp1) + eval(*exp2),
Exp::Sub(exp1, exp2) => eval(*exp1) - eval(*exp2),
Exp::Mul(exp1, exp2) => eval(*exp1) * eval(*exp2),
Exp::Div(exp1, exp2) => eval(*exp1) / eval(*exp2),
Exp::Exp(exp1, exp2) => libm::powf(eval(*exp1), eval(*exp2)),
Exp::Exp(exp1, exp2) => libm::pow(eval(*exp1), eval(*exp2)),
}
}

// REPL

fn parse_eval(line: &str) -> Result<f32, String> {
fn parse_eval(line: &str) -> Result<f64, String> {
match parse(&line) {
Ok((line, parsed)) => {
if line.is_empty() {
Expand Down Expand Up @@ -118,6 +118,10 @@ fn repl() -> usr::shell::ExitCode {
if line == "exit" || line == "quit" {
break;
}
if line.is_empty() {
println!();
continue;
}

match parse_eval(&line) {
Ok(res) => {
Expand All @@ -129,10 +133,8 @@ fn repl() -> usr::shell::ExitCode {
}
}

if !line.is_empty() {
prompt.history.add(&line);
prompt.history.save(history_file);
}
prompt.history.add(&line);
prompt.history.save(history_file);
}
usr::shell::ExitCode::CommandSuccessful
}
Expand Down
74 changes: 33 additions & 41 deletions src/usr/lisp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ macro_rules! ensure_tonicity {
let floats = parse_list_of_floats(args)?;
let first = floats.first().ok_or(Err::Reason("Expected at least one number".to_string()))?;
let rest = &floats[1..];
fn f (prev: &f64, xs: &[f64]) -> bool {
fn func(prev: &f64, xs: &[f64]) -> bool {
match xs.first() {
Some(x) => $check_fn(*prev, *x) && f(x, &xs[1..]),
Some(x) => $check_fn(*prev, *x) && func(x, &xs[1..]),
None => true,
}
}
Ok(Exp::Bool(f(first, rest)))
Ok(Exp::Bool(func(first, rest)))
}
};
}
Expand All @@ -150,32 +150,26 @@ fn default_env<'a>() -> Env<'a> {
let mut data: BTreeMap<String, Exp> = BTreeMap::new();
data.insert(
"*".to_string(),
Exp::Func(
|args: &[Exp]| -> Result<Exp, Err> {
let res = parse_list_of_floats(args)?.iter().fold(1.0, |res, a| res * a);
Ok(Exp::Number(res))
}
)
Exp::Func(|args: &[Exp]| -> Result<Exp, Err> {
let res = parse_list_of_floats(args)?.iter().fold(1.0, |res, a| res * a);
Ok(Exp::Number(res))
})
);
data.insert(
"+".to_string(),
Exp::Func(
|args: &[Exp]| -> Result<Exp, Err> {
let res = parse_list_of_floats(args)?.iter().fold(0.0, |res, a| res + a);
Ok(Exp::Number(res))
}
)
Exp::Func(|args: &[Exp]| -> Result<Exp, Err> {
let res = parse_list_of_floats(args)?.iter().fold(0.0, |res, a| res + a);
Ok(Exp::Number(res))
})
);
data.insert(
"-".to_string(),
Exp::Func(
|args: &[Exp]| -> Result<Exp, Err> {
let floats = parse_list_of_floats(args)?;
let first = *floats.first().ok_or(Err::Reason("Expected at least one number".to_string()))?;
let sum_of_rest = floats[1..].iter().fold(0.0, |sum, a| sum + a);
Ok(Exp::Number(first - sum_of_rest))
}
)
Exp::Func(|args: &[Exp]| -> Result<Exp, Err> {
let floats = parse_list_of_floats(args)?;
let first = *floats.first().ok_or(Err::Reason("Expected at least one number".to_string()))?;
let sum_of_rest = floats[1..].iter().fold(0.0, |sum, a| sum + a);
Ok(Exp::Number(first - sum_of_rest))
})
);
data.insert(
"=".to_string(),
Expand All @@ -198,7 +192,7 @@ fn default_env<'a>() -> Env<'a> {
Exp::Func(ensure_tonicity!(|a, b| approx_eq!(f64, a, b) || a < b))
);

Env {data, outer: None}
Env { data, outer: None }
}

fn parse_list_of_floats(args: &[Exp]) -> Result<Vec<f64>, Err> {
Expand Down Expand Up @@ -236,18 +230,14 @@ fn eval_eq_args(arg_forms: &[Exp], env: &mut Env) -> Result<Exp, Err> {
match first_eval {
Exp::Symbol(a) => {
match second_eval {
Exp::Symbol(b) => {
Ok(Exp::Bool(a == b))
},
_ => Ok(Exp::Bool(false))
Exp::Symbol(b) => Ok(Exp::Bool(a == b)),
_ => Ok(Exp::Bool(false)),
}
},
Exp::List(a) => {
match second_eval {
Exp::List(b) => {
Ok(Exp::Bool(a.is_empty() && b.is_empty()))
},
_ => Ok(Exp::Bool(false))
Exp::List(b) => Ok(Exp::Bool(a.is_empty() && b.is_empty())),
_ => Ok(Exp::Bool(false))
}
},
_ => Ok(Exp::Bool(false))
Expand Down Expand Up @@ -457,8 +447,8 @@ fn eval(exp: &Exp, env: &mut Env) -> Result<Exp, Err> {
None => {
let first_eval = eval(first_form, env)?;
match first_eval {
Exp::Func(f) => {
f(&eval_forms(arg_forms, env)?)
Exp::Func(func) => {
func(&eval_forms(arg_forms, env)?)
},
Exp::Lambda(lambda) => {
let new_env = &mut env_for_lambda(lambda.params_exp, arg_forms, env)?;
Expand Down Expand Up @@ -519,22 +509,24 @@ fn repl(env: &mut Env) -> usr::shell::ExitCode {
prompt.history.load(history_file);
prompt.completion.set(&lisp_completer);

while let Some(exp) = prompt.input(&prompt_string) {
if exp == "(exit)" || exp == "(quit)" {
while let Some(line) = prompt.input(&prompt_string) {
if line == "(exit)" || line == "(quit)" {
break;
}
match parse_eval(&exp, env) {
if line.is_empty() {
println!();
continue;
}
match parse_eval(&line, env) {
Ok(res) => {
println!("{}\n", res);
}
Err(e) => match e {
Err::Reason(msg) => println!("{}Error:{} {}\n", csi_error, csi_reset, msg),
},
}
if !exp.is_empty() {
prompt.history.add(&exp);
prompt.history.save(history_file);
}
prompt.history.add(&line);
prompt.history.save(history_file);
}
usr::shell::ExitCode::CommandSuccessful
}
Expand Down