diff --git a/Cargo.lock b/Cargo.lock index c5efd79..6b999e0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,6 +52,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3aa954171903797d5623e047d9ab69d91b493657917bdfb8c2c80ecaf9cdb6f4" dependencies = [ "color-print-proc-macro", + "lazy_static", + "terminfo", ] [[package]] @@ -60,7 +62,7 @@ version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "692186b5ebe54007e45a59aea47ece9eb4108e141326c304cdc91699a7118a22" dependencies = [ - "nom", + "nom 7.1.3", "proc-macro2", "quote", "syn", @@ -86,12 +88,49 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "dirs" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "either" version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "home" version = "0.5.11" @@ -111,6 +150,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.169" @@ -125,7 +170,7 @@ dependencies = [ "const_format", "home", "mlua", - "thiserror", + "thiserror 2.0.9", "uzers", "whoami", ] @@ -140,6 +185,16 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "libredox" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +dependencies = [ + "bitflags", + "libc", +] + [[package]] name = "lock_api" version = "0.4.12" @@ -204,6 +259,16 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "nom" +version = "5.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08959a387a676302eebf4ddbcbc611da04285579f76f88ee0506c63b1a61dd4b" +dependencies = [ + "memchr", + "version_check", +] + [[package]] name = "nom" version = "7.1.3" @@ -252,6 +317,44 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "phf" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" +dependencies = [ + "phf_shared", +] + +[[package]] +name = "phf_codegen" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8d39688d359e6b34654d328e262234662d16cc0f60ec8dcbe5e718709342a5a" +dependencies = [ + "phf_generator", + "phf_shared", +] + +[[package]] +name = "phf_generator" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0" +dependencies = [ + "phf_shared", + "rand", +] + +[[package]] +name = "phf_shared" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" +dependencies = [ + "siphasher", +] + [[package]] name = "pkg-config" version = "0.3.31" @@ -276,6 +379,21 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" + [[package]] name = "redox_syscall" version = "0.5.8" @@ -285,6 +403,17 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_users" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" +dependencies = [ + "getrandom", + "libredox", + "thiserror 1.0.69", +] + [[package]] name = "rustc-hash" version = "2.1.0" @@ -323,6 +452,12 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "siphasher" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" + [[package]] name = "smallvec" version = "1.13.2" @@ -340,13 +475,46 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "terminfo" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da31aef70da0f6352dbcb462683eb4dd2bfad01cf3fc96cf204547b9a839a585" +dependencies = [ + "dirs", + "fnv", + "nom 5.1.3", + "phf", + "phf_codegen", +] + +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl 1.0.69", +] + [[package]] name = "thiserror" version = "2.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f072643fd0190df67a8bab670c20ef5d8737177d6ac6b2e9a236cb096206b2cc" dependencies = [ - "thiserror-impl", + "thiserror-impl 2.0.9", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -382,6 +550,18 @@ dependencies = [ "log", ] +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wasite" version = "0.1.0" @@ -463,6 +643,28 @@ dependencies = [ "web-sys", ] +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-sys" version = "0.59.0" diff --git a/Cargo.toml b/Cargo.toml index 6983611..a283a9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -color-print = "0.3.7" +color-print = { version = "0.3.7", features = ["terminfo"] } const_format = "0.2.33" home = "0.5.9" mlua = { version = "0.10.0", features = ["luau-jit"] } diff --git a/src/commands.rs b/src/commands.rs index e9fa766..4d73f8f 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -28,10 +28,8 @@ trait ChangeDirectory { impl PathBufIsValid for PathBuf { fn is_valid(&self) -> Result { match self.try_exists() { - Ok(root_folder_exist) => match root_folder_exist { - true => Ok(self.to_path_buf()), - false => Err(ValidStatus::NoRootFolder) - }, + Ok(true) => Ok(self.to_path_buf()), + Ok(false) => Err(ValidStatus::NoRootFolder), Err(trye_error) => Err(ValidStatus::TryExists(trye_error)) } } @@ -52,20 +50,11 @@ impl PathBufIsValid for PathBuf { impl ChangeDirectory for Command { fn set_current_dir(&self, new_path: &Path) -> Option { - match std::env::set_current_dir(new_path) { - Ok(()) => Some(new_path.to_path_buf()), - Err(set_cd_err) => { - println!("{set_cd_err}"); - None - }, - } + std::env::set_current_dir(new_path).map_or_else(|cd_err| {println!("{cd_err}"); None}, |()| Some(new_path.to_path_buf())) } fn home_dir(&self) -> Option { - match home::home_dir() { - Some(home_path_buf) => self.set_current_dir(&home_path_buf), - None => self.set_current_dir(Path::new("/")), - } + home::home_dir().map_or(self.set_current_dir(Path::new("/")), |home_pathbuf| self.set_current_dir(&home_pathbuf)) } fn previous_dir(&self) -> Option { @@ -107,6 +96,7 @@ impl ChangeDirectory for Command { fn change_directory(&self, args: SplitWhitespace) -> Option { let vec_args: Vec = args.map(|arg| arg.to_owned()).collect(); match vec_args.first() { + None => self.home_dir(), Some(arg) => match arg.as_str() { "/" => self.set_current_dir(Path::new("/")), "-" => self.previous_dir(), @@ -121,7 +111,6 @@ impl ChangeDirectory for Command { } } }, - None => self.home_dir(), } } } @@ -134,18 +123,18 @@ impl Command { } pub fn spawn(&self, command_process: io::Result) -> ProcessExitStatus { - match command_process { + match command_process { + Err(e) => { + println!("{e}"); + None + }, Ok(mut child) => Some(match child.wait() { Ok(exit_status) => exit_status, Err(exit_status_err) => { println!("{exit_status_err}"); - return None; + return None } - }), - Err(e) => { - println!("{e}"); - return None; - } + }) } } @@ -153,7 +142,7 @@ impl Command { let mut args = self.0.split_whitespace(); args.next().and_then(|command| match command { "cd" => { - Self::change_directory(self, args); + self.change_directory(args); None } command => self.spawn(process::Command::new(command).args(args).spawn()), diff --git a/src/lib.rs b/src/lib.rs index 1427739..308e929 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,4 +4,7 @@ pub mod shell; mod commands; mod ps; -mod rc; \ No newline at end of file +mod rc; + +#[path = "./luau/vm.rs"] +mod vm; \ No newline at end of file diff --git a/src/luau/alias.rs b/src/luau/alias.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/luau/print.rs b/src/luau/print.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/luau/vm.rs b/src/luau/vm.rs new file mode 100644 index 0000000..584174e --- /dev/null +++ b/src/luau/vm.rs @@ -0,0 +1,60 @@ +use mlua::{ + Lua as Luau, + Result as lResult, + MultiValue +}; + +fn new_instance() -> lResult { + let instance = Luau::new(); + instance.sandbox(true)?; + instance.globals().set("getfenv", mlua::Nil)?; + instance.globals().set("setfenv", mlua::Nil)?; + Ok(instance) +} + +fn out(args: MultiValue) -> String { + let mut print: Vec = Vec::new(); + let mut print_append = |v: String| { + if !print.is_empty() { + print.push(" ".to_owned()); + } + print.push(v) + }; + args.iter().for_each(|arg| + arg.to_string().map_or(print_append("".to_owned()), + |s_arg| print_append(s_arg) + ) + ); + print.concat() +} + +trait Globals { + fn print(&self) -> lResult<()>; +} +impl Globals for Vm { + fn print(&self) -> lResult<()> { + self.0.globals().set("print", self.0.create_function(|_, args: MultiValue| -> lResult<()> { + color_print::cprintln!("{}", out(args)); + Ok(()) + })?)?; + self.0.globals().set("printraw", self.0.create_function(|_, args: MultiValue| -> lResult<()> { + println!("{}", out(args)); + Ok(()) + })?) + } +} + +struct Vm(Luau); +impl Vm { + pub fn new() -> Option { + new_instance().map_or(None, |l| Some(Self(l))) + } + + fn set_shell_globals(&self) -> mlua::Result<()> { + todo!() + } + + pub fn exec(&self, source: String) -> mlua::Result<()> { + self.set_shell_globals().and(self.0.load(source).exec()) + } +} \ No newline at end of file diff --git a/src/shell.rs b/src/shell.rs index 400e9d3..948fe04 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -1,5 +1,5 @@ use crate::{ps, commands, rc}; -use std::io; +use std::{fs, io::{self}}; pub struct Config { pub norc: bool @@ -49,9 +49,10 @@ impl LambdaShell { pub fn start(&mut self) { let rc_file = match self.config.norc { - true => rc::config_file(), - false => rc::none(), + true => rc::none(), + false => rc::config_file(), }; + ps::display(&self.storage.ps1); loop {