diff --git a/Cargo.lock b/Cargo.lock index 10cfef1..30018b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,170 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "bitflags" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" + +[[package]] +name = "cc" +version = "1.0.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" +dependencies = [ + "libc", +] + +[[package]] +name = "colored" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2674ec482fbc38012cf31e6c42ba0177b431a0cb6f15fe40efa5aab1bda516f6" +dependencies = [ + "is-terminal", + "lazy_static", + "windows-sys", +] + +[[package]] +name = "errno" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "136526188508e25c6fef639d7927dfb3e0e3084488bf202267829cf7fc23dbdd" +dependencies = [ + "errno-dragonfly", + "libc", + "windows-sys", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + +[[package]] +name = "hermit-abi" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b" + +[[package]] +name = "is-terminal" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" +dependencies = [ + "hermit-abi", + "rustix", + "windows-sys", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.147" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" + +[[package]] +name = "linux-raw-sys" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57bcfdad1b858c2db7c38303a6d2ad4dfaf5eb53dfeb0910128b2c26d6158503" + +[[package]] +name = "rustix" +version = "0.38.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0c3dde1fc030af041adc40e79c0e7fbcf431dd24870053d187d7c66e4b87453" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + [[package]] name = "stoopid_shell" version = "0.1.0" +dependencies = [ + "colored", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/Cargo.toml b/Cargo.toml index b243a7c..7ecdc53 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +colored = "2.0.4" diff --git a/README.md b/README.md index f778f86..b69be4d 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A new Shell written in rust ## Roadmap -- [ ] Add basic shell functionality +- [X] Add basic shell functionality - [ ] Reimplement alot of basic shell features - [ ] echo - [ ] ls diff --git a/src/commands.rs b/src/commands.rs new file mode 100644 index 0000000..0e98a80 --- /dev/null +++ b/src/commands.rs @@ -0,0 +1,57 @@ +use std::process::Command; + +fn syscalls(args: Vec<&str>) -> i8 { + let mut cmd_args: Vec = vec![]; + + let mut i = 1; + + while i < args.len() { + let mut arg: String = args[i].to_string(); + + if arg.starts_with("\"") { + i += 1; + + for j in i..args.len() { + arg = format!("{} {}", arg, args[j]); + + if arg.ends_with("\"") { + break; + } + + i += 1; + } + } + + cmd_args.push(arg); + + i += 1; + } + + let mut cmd = Command::new(args[0]); + + if cmd_args.len() > 0 { + cmd.args(&cmd_args); + } + + return match cmd.status() { + Ok(status) => { + if status.success() { + 0 + } else { + 1 + } + } + Err(_) => { + println!("Command not found: {}", args[0]); + 1 + } + }; +} + +pub fn input_handler(args: Vec<&str>) -> i8 { + match args[0] { + "exit" => return -1, + + _ => return syscalls(args), + } +} diff --git a/src/main.rs b/src/main.rs index e7a11a9..889767f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,30 @@ +use std::io::{stdin, stdout, Write}; + +mod commands; +mod prompt; + +use commands::input_handler; +use prompt::get_prompt; + fn main() { - println!("Hello, world!"); + let running = true; + let mut exit_code: i8 = 0; + + while running { + let mut command = String::new(); + + print!("{}", get_prompt(exit_code)); + stdout().flush().expect("Failed to flush stdout"); + stdin() + .read_line(&mut command) + .expect("Failed to read line"); + + let args: Vec<&str> = command.trim().split(" ").collect(); + + exit_code = input_handler(args); + + if exit_code == -1 { + break; + } + } } diff --git a/src/prompt.rs b/src/prompt.rs new file mode 100644 index 0000000..0ea0680 --- /dev/null +++ b/src/prompt.rs @@ -0,0 +1,13 @@ +use colored::Colorize; + +pub fn get_prompt(exit_code: i8) -> String { + let mut prompt = String::new(); + + if exit_code == 0 { + prompt.push_str(&"➜ ".green().to_string()); + } else { + prompt.push_str(&"➜ ".red().to_string()); + } + + return prompt; +}