From f8e0c75e67594a15a24dcbde21966a90b9eee31e Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Tue, 17 Nov 2015 15:23:35 -0700
Subject: [PATCH] Add fork/exec to terminal, add exit status to terminal

---
 main.rs | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/main.rs b/main.rs
index 5403b9ee..4e08f6bd 100644
--- a/main.rs
+++ b/main.rs
@@ -9,19 +9,11 @@ use redox::env::*;
 use redox::time::Duration;
 use redox::to_num::*;
 use redox::hashmap::HashMap;
+use redox::syscall::{sys_clone, sys_waitpid};
 
-/* Magic Macros { */
+/* Magic { */
 static mut application: *mut Application<'static> = 0 as *mut Application;
-
-/// Execute a command
-macro_rules! exec {
-    ($cmd:expr) => ({
-        unsafe {
-            (*application).on_command(&$cmd.to_string());
-        }
-    })
-}
-/* } Magic Macros */
+/* } Magic */
 
 /// Structure which represents a Terminal's command.
 /// This command structure contains a name, and the code which run the functionnality associated to this one, with zero, one or several argument(s).
@@ -111,7 +103,14 @@ impl<'a> Command<'a> {
                         args_str.push(arg);
                     }
 
-                    File::exec(arg, &args_str);
+                    let pid = unsafe { sys_clone(0) } as isize;
+                    if pid == 0 {
+                        File::exec(arg, &args_str);
+                    } else {
+                        let mut status: usize = 0;
+                        unsafe { sys_waitpid(pid, &mut status, 0) };
+                        unsafe{ (*application).set_var("?", &format!("{}", status)) };
+                    }
                 }
             }),
         });
@@ -202,7 +201,9 @@ impl<'a> Command<'a> {
                     }
 
                     for command in commands.split('\n') {
-                        exec!(command);
+                        unsafe {
+                            (*application).on_command(&command);
+                        }
                     }
                 }
             }),
-- 
GitLab