From 866d9529245e27a2b6f71c839d4b35ed60e86cf3 Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Sun, 4 Mar 2018 08:10:42 -0700
Subject: [PATCH] Support building for Redox

---
 .gitmodules               |  3 +++
 Cargo.lock                | 22 +++++++++++++++++++---
 platform/Cargo.toml       |  5 ++++-
 platform/src/lib.rs       | 17 ++++++++++++++++-
 platform/src/redox/mod.rs | 20 ++++++++++++++++++--
 ralloc                    |  2 +-
 src/stdio/Cargo.toml      |  2 +-
 src/string/src/lib.rs     | 11 +----------
 va_list                   |  1 +
 9 files changed, 64 insertions(+), 19 deletions(-)
 create mode 160000 va_list

diff --git a/.gitmodules b/.gitmodules
index 6d57d168a..cde224a0c 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -7,3 +7,6 @@
 [submodule "ralloc"]
 	path = ralloc
 	url = https://github.com/redox-os/ralloc.git
+[submodule "va_list"]
+	path = va_list
+	url = https://github.com/redox-os/va_list-rs.git
diff --git a/Cargo.lock b/Cargo.lock
index 8be22825d..ce821e074 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -97,6 +97,11 @@ name = "fuchsia-zircon-sys"
 version = "0.3.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
+[[package]]
+name = "gcc"
+version = "0.3.54"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [[package]]
 name = "grp"
 version = "0.1.0"
@@ -177,6 +182,7 @@ dependencies = [
 name = "platform"
 version = "0.1.0"
 dependencies = [
+ "redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)",
  "syscall 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
@@ -197,6 +203,7 @@ dependencies = [
 name = "ralloc_shim"
 version = "0.1.1"
 dependencies = [
+ "redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)",
  "sc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
@@ -411,7 +418,7 @@ version = "0.1.0"
 dependencies = [
  "cbindgen 0.5.0",
  "platform 0.1.0",
- "va_list 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "va_list 0.1.0",
 ]
 
 [[package]]
@@ -534,7 +541,16 @@ dependencies = [
 [[package]]
 name = "va_list"
 version = "0.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "va_list-helper 0.0.2",
+]
+
+[[package]]
+name = "va_list-helper"
+version = "0.0.2"
+dependencies = [
+ "gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)",
+]
 
 [[package]]
 name = "vec_map"
@@ -580,6 +596,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab"
 "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
 "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
+"checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb"
 "checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c"
 "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
 "checksum libc 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)" = "56aebce561378d99a0bb578f8cb15b6114d2a1814a6c7949bbe646d968bb4fa9"
@@ -624,7 +641,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 "checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f"
 "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
 "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
-"checksum va_list 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7365862faee55ed0dbc112491aa5f0451ca01bf98afcc6463183b5aaa5bd3128"
 "checksum vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "887b5b631c2ad01628bbbaa7dd4c869f80d3186688f8d0b6f58774fbe324988c"
 "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
 "checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3"
diff --git a/platform/Cargo.toml b/platform/Cargo.toml
index e7deb89c1..c7722d069 100644
--- a/platform/Cargo.toml
+++ b/platform/Cargo.toml
@@ -3,5 +3,8 @@ name = "platform"
 version = "0.1.0"
 authors = ["Jeremy Soller <jackpot51@gmail.com>"]
 
-[dependencies]
+[target.'cfg(target_os = "linux")'.dependencies]
 syscall = "0.2"
+
+[target.'cfg(target_os = "redox")'.dependencies]
+redox_syscall = "0.1"
diff --git a/platform/src/lib.rs b/platform/src/lib.rs
index bdddb79a6..00529af02 100644
--- a/platform/src/lib.rs
+++ b/platform/src/lib.rs
@@ -20,7 +20,22 @@ pub mod types;
 
 use core::fmt;
 
-use types::c_int;
+use types::*;
+
+pub unsafe fn c_str(s: *const c_char) -> &'static [u8] {
+    use core::slice;
+
+    let mut size = 0;
+
+    loop {
+        if *s.offset(size) == 0 {
+            break;
+        }
+        size += 1;
+    }
+
+    slice::from_raw_parts(s as *const u8, size as usize)
+}
 
 pub struct FileWriter(pub c_int);
 
diff --git a/platform/src/redox/mod.rs b/platform/src/redox/mod.rs
index 239d06a6b..b1178bf55 100644
--- a/platform/src/redox/mod.rs
+++ b/platform/src/redox/mod.rs
@@ -1,8 +1,24 @@
+use syscall;
+
+use c_str;
+use types::*;
+
+pub fn close(fd: c_int) -> c_int {
+    syscall::close(fd as usize);
+    0
+}
+
 pub fn exit(status: c_int) -> ! {
-    syscall::exit(status);
+    syscall::exit(status as usize);
+    loop {}
+}
+
+pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int {
+    let path = unsafe { c_str(path) };
+    syscall::open(path, (oflag as usize) | (mode as usize)).unwrap() as c_int
 }
 
 pub fn write(fd: c_int, buf: &[u8]) -> ssize_t {
-    syscall::write(fd, buf);
+    syscall::write(fd as usize, buf);
     buf.len() as ssize_t
 }
diff --git a/ralloc b/ralloc
index b521bac28..31b781a28 160000
--- a/ralloc
+++ b/ralloc
@@ -1 +1 @@
-Subproject commit b521bac28702de86c7c4eef9641ec83c6ac38224
+Subproject commit 31b781a287748c10671a6c7e4415093d2dd4df4a
diff --git a/src/stdio/Cargo.toml b/src/stdio/Cargo.toml
index 78c082e15..c74b04885 100644
--- a/src/stdio/Cargo.toml
+++ b/src/stdio/Cargo.toml
@@ -9,4 +9,4 @@ cbindgen = { path = "../../cbindgen" }
 
 [dependencies]
 platform = { path = "../../platform" }
-va_list = { version = "0.1", features = ["no_std"] }
+va_list = { path = "../../va_list", features = ["no_std"] }
diff --git a/src/string/src/lib.rs b/src/string/src/lib.rs
index eb9891135..e59753276 100644
--- a/src/string/src/lib.rs
+++ b/src/string/src/lib.rs
@@ -104,16 +104,7 @@ pub extern "C" fn strerror(errnum: c_int) -> *mut c_char {
 
 #[no_mangle]
 pub unsafe extern "C" fn strlen(s: *const c_char) -> size_t {
-    let mut size = 0;
-
-    loop {
-        if *s.offset(size) == 0 {
-            break;
-        }
-        size += 1;
-    }
-
-    size as size_t
+    platform::c_str(s).len() as size_t
 }
 
 #[no_mangle]
diff --git a/va_list b/va_list
new file mode 160000
index 000000000..4762a1845
--- /dev/null
+++ b/va_list
@@ -0,0 +1 @@
+Subproject commit 4762a184501beedbb58ea218f0c84fea85685c35
-- 
GitLab