| ... | @@ -5,26 +5,39 @@ | ... | @@ -5,26 +5,39 @@ |
| 5 | pub const Message = struct { | 5 | pub const Message = struct { |
| 6 | sender: MailboxId, | 6 | sender: MailboxId, |
| 7 | receiver: MailboxId, | 7 | receiver: MailboxId, |
| | 8 | type: usize, |
| 8 | payload: usize, | 9 | payload: usize, |
| 9 | | 10 | |
| 10 | pub fn from(mailbox_id: &const MailboxId) Message { | 11 | pub fn from(mailbox_id: &const MailboxId) Message { |
| 11 | return Message { | 12 | return Message { |
| 12 | .sender = undefined, | 13 | .sender = MailboxId.Undefined, |
| 13 | .receiver = *mailbox_id, | 14 | .receiver = *mailbox_id, |
| 14 | .payload = undefined, | 15 | .type = 0, |
| | 16 | .payload = 0, |
| 15 | }; | 17 | }; |
| 16 | } | 18 | } |
| 17 | | 19 | |
| 18 | pub fn to(mailbox_id: &const MailboxId, payload: usize) Message { | 20 | pub fn to(mailbox_id: &const MailboxId, msg_type: usize) Message { |
| 19 | return Message { | 21 | return Message { |
| 20 | .sender = MailboxId.This, | 22 | .sender = MailboxId.This, |
| 21 | .receiver = *mailbox_id, | 23 | .receiver = *mailbox_id, |
| | 24 | .type = msg_type, |
| | 25 | .payload = 0, |
| | 26 | }; |
| | 27 | } |
| | 28 | |
| | 29 | pub fn withData(mailbox_id: &const MailboxId, msg_type: usize, payload: usize) Message { |
| | 30 | return Message { |
| | 31 | .sender = MailboxId.This, |
| | 32 | .receiver = *mailbox_id, |
| | 33 | .type = msg_type, |
| 22 | .payload = payload, | 34 | .payload = payload, |
| 23 | }; | 35 | }; |
| 24 | } | 36 | } |
| 25 | }; | 37 | }; |
| 26 | | 38 | |
| 27 | pub const MailboxId = union(enum) { | 39 | pub const MailboxId = union(enum) { |
| | 40 | Undefined, |
| 28 | This, | 41 | This, |
| 29 | Kernel, | 42 | Kernel, |
| 30 | Port: u16, | 43 | Port: u16, |
| ... | @@ -55,14 +68,32 @@ pub const STDERR_FILENO = 2; | ... | @@ -55,14 +68,32 @@ pub const STDERR_FILENO = 2; |
| 55 | pub const getErrno = @import("linux/index.zig").getErrno; | 68 | pub const getErrno = @import("linux/index.zig").getErrno; |
| 56 | use @import("linux/errno.zig"); | 69 | use @import("linux/errno.zig"); |
| 57 | | 70 | |
| | 71 | // TODO: implement this correctly. |
| | 72 | pub fn read(fd: i32, buf: &u8, count: usize) usize { |
| | 73 | switch (fd) { |
| | 74 | STDIN_FILENO => { |
| | 75 | var i: usize = 0; |
| | 76 | while (i < count) : (i += 1) { |
| | 77 | send(Message.to(Server.Keyboard, 0)); |
| | 78 | |
| | 79 | var message = Message.from(MailboxId.This); |
| | 80 | receive(&message); |
| | 81 | |
| | 82 | buf[i] = u8(message.payload); |
| | 83 | } |
| | 84 | }, |
| | 85 | else => unreachable, |
| | 86 | } |
| | 87 | return count; |
| | 88 | } |
| | 89 | |
| 58 | // TODO: implement this correctly. | 90 | // TODO: implement this correctly. |
| 59 | pub fn write(fd: i32, buf: &const u8, count: usize) usize { | 91 | pub fn write(fd: i32, buf: &const u8, count: usize) usize { |
| 60 | switch (fd) { | 92 | switch (fd) { |
| 61 | STDIN_FILENO => unreachable, | | |
| 62 | STDOUT_FILENO, STDERR_FILENO => { | 93 | STDOUT_FILENO, STDERR_FILENO => { |
| 63 | var i: usize = 0; | 94 | var i: usize = 0; |
| 64 | while (i < count) : (i += 1) { | 95 | while (i < count) : (i += 1) { |
| 65 | send(Message.to(Server.Terminal, buf[i])); | 96 | send(Message.withData(Server.Terminal, 1, buf[i])); |
| 66 | } | 97 | } |
| 67 | }, | 98 | }, |
| 68 | else => unreachable, | 99 | else => unreachable, |