aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-09-21 17:40:05 +0300
committerAqua-sama <aqua@iserlohn-fortress.net>2020-09-21 17:48:33 +0300
commit42cca401d21085709da7333069dbfcca99177d8d (patch)
tree9addbac29cdce1e3430b40c267f5c6d4d816a5d2 /src/main.rs
downloadrshell-42cca401d21085709da7333069dbfcca99177d8d.tar.xz
Initial commit
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..a4f3c25
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,80 @@
+use std::process::Command;
+use std::process::Stdio;
+
+mod builtins;
+mod prompt;
+
+fn main() {
+ println!("rshell motd");
+
+ let mut prompt = prompt::Prompt::new();
+
+ loop {
+ prompt.print();
+
+ let mut input = String::new();
+ match std::io::stdin().read_line(&mut input) {
+ Ok(0) => return, // EOF
+ Ok(_) => {} // okay
+ Err(e) => {
+ eprintln!("error reading line: {}", e);
+ return;
+ }
+ };
+
+ let mut commands = input.trim().split(" | ").peekable();
+ let mut previous_command: std::option::Option<std::process::Child> = None;
+
+ while let Some(command) = commands.next() {
+ let mut parts = command.trim().split_whitespace();
+ let command = match parts.next() {
+ Some(cmd) => cmd,
+ None => break,
+ };
+
+ let args = parts;
+
+ match command {
+ "cd" => {
+ prompt.pwd = builtins::cd(args);
+ previous_command = None;
+ }
+ "exit" => return,
+
+ command => {
+ let stdin = match previous_command {
+ None => Stdio::inherit(),
+ Some(cmd) => Stdio::from(cmd.stdout.unwrap()),
+ };
+ let stdout = match commands.peek().is_some() {
+ true => Stdio::piped(),
+ false => Stdio::inherit(),
+ };
+
+ let child = Command::new(command)
+ .args(args)
+ .stdin(stdin)
+ .stdout(stdout)
+ .spawn();
+ match child {
+ Ok(c) => previous_command = Some(c),
+ Err(e) => {
+ previous_command = None;
+ eprintln!("{}", e);
+ }
+ }
+ }
+ }
+ }
+
+ if let Some(mut final_command) = previous_command {
+ match final_command.wait() {
+ Ok(ret) => match ret.code() {
+ Some(code) => println!("exit code [{}]", code),
+ None => println!("Process termed by signal"),
+ },
+ Err(e) => eprintln!("error waiting on final command: {}", e),
+ }
+ }
+ } // loop
+}