-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from nils865/main
Added basic shell functionality
- Loading branch information
Showing
6 changed files
with
264 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use std::process::Command; | ||
|
||
fn syscalls(args: Vec<&str>) -> i8 { | ||
let mut cmd_args: Vec<String> = 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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |