Commit 4c16deed3e

Andrea Orru <andrea@orru.io>
2018-03-16 01:57:56
Some POSIX stuff, including a primitive write
1 parent 681c629
Changed files (1)
std
std/os/zen.zig
@@ -7,13 +7,21 @@ pub const Message = struct {
     receiver: MailboxId,
     payload:  usize,
 
-    pub fn withReceiver(mailbox_id: &const MailboxId) Message {
+    pub fn from(mailbox_id: &const MailboxId) Message {
         return Message {
             .sender   = undefined,
             .receiver = *mailbox_id,
             .payload  = undefined,
         };
     }
+
+    pub fn to(mailbox_id: &const MailboxId, payload: usize) Message {
+        return Message {
+            .sender   = MailboxId.This,
+            .receiver = *mailbox_id,
+            .payload  = payload,
+        };
+    }
 };
 
 pub const MailboxId = union(enum) {
@@ -29,11 +37,40 @@ pub const MailboxId = union(enum) {
 ///////////////////////////////////////
 
 pub const Service = struct {
-    pub const Terminal = MailboxId { .Port = 0 };
-    pub const Keyboard = MailboxId { .Port = 1 };
+    pub const Keyboard = MailboxId { .Port = 0 };
+    pub const Terminal = MailboxId { .Port = 1 };
 };
 
 
+////////////////////////
+////  POSIX things  ////
+////////////////////////
+
+// Standard streams.
+pub const  STDIN_FILENO = 0;
+pub const STDOUT_FILENO = 1;
+pub const STDERR_FILENO = 2;
+
+// FIXME: let's borrow Linux's error numbers for now.
+pub const getErrno = @import("linux/index.zig").getErrno;
+use @import("linux/errno.zig");
+
+// TODO: implement this correctly.
+pub fn write(fd: i32, buf: &const u8, count: usize) usize {
+    switch (fd) {
+        STDIN_FILENO => unreachable,
+        STDOUT_FILENO, STDERR_FILENO => {
+            var i: usize = 0;
+            while (i < count) : (i += 1) {
+                send(Message.to(Service.Terminal, buf[i]));
+            }
+        },
+        else => unreachable,
+    }
+    return count;
+}
+
+
 ///////////////////////////
 ////  Syscall numbers  ////
 ///////////////////////////