| 1 | #include <io/io.h>
|
|---|
| 2 | #include <io/stream.h>
|
|---|
| 3 | #include <string.h>
|
|---|
| 4 | #include <malloc.h>
|
|---|
| 5 | #include <libc.h>
|
|---|
| 6 | #include <ipc/ipc.h>
|
|---|
| 7 | #include <ipc/ns.h>
|
|---|
| 8 | #include <ipc/fb.h>
|
|---|
| 9 | #include <ipc/services.h>
|
|---|
| 10 | #include <console.h>
|
|---|
| 11 |
|
|---|
| 12 | #define FDS 32
|
|---|
| 13 |
|
|---|
| 14 | typedef struct stream_t {
|
|---|
| 15 | pwritefn_t w;
|
|---|
| 16 | preadfn_t r;
|
|---|
| 17 | void * param;
|
|---|
| 18 | } stream_t;
|
|---|
| 19 |
|
|---|
| 20 | int console_phone = -1;
|
|---|
| 21 |
|
|---|
| 22 | stream_t streams[FDS] = {{0, 0, 0}};
|
|---|
| 23 |
|
|---|
| 24 | /*
|
|---|
| 25 | ssize_t write_stdout(void *param, const void * buf, size_t count);
|
|---|
| 26 | ssize_t write_stdout(void *param, const void * buf, size_t count)
|
|---|
| 27 | {
|
|---|
| 28 | return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
|
|---|
| 29 | }*/
|
|---|
| 30 |
|
|---|
| 31 | static ssize_t write_stderr(void *param, const void *buf, size_t count)
|
|---|
| 32 | {
|
|---|
| 33 | return count;
|
|---|
| 34 | //return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | static char read_stdin(void)
|
|---|
| 38 | {
|
|---|
| 39 | ipcarg_t r0,r1;
|
|---|
| 40 | ipc_call_sync_2(console_phone, CONSOLE_GETCHAR, 0, 0, &r0, &r1);
|
|---|
| 41 |
|
|---|
| 42 | return r0;
|
|---|
| 43 | //return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
|
|---|
| 44 | }
|
|---|
| 45 | static ssize_t write_stdout(void *param, const void *buf, size_t count)
|
|---|
| 46 | {
|
|---|
| 47 | int i;
|
|---|
| 48 | ipcarg_t r0,r1;
|
|---|
| 49 |
|
|---|
| 50 | for (i = 0; i < count; i++)
|
|---|
| 51 | ipc_call_sync_2(console_phone, CONSOLE_PUTCHAR, 0, ((const char *)buf)[i], &r0, &r1);
|
|---|
| 52 |
|
|---|
| 53 | return count;
|
|---|
| 54 | //return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 | static stream_t open_stdin(void)
|
|---|
| 60 | {
|
|---|
| 61 | stream_t stream;
|
|---|
| 62 | int phoneid;
|
|---|
| 63 | int res;
|
|---|
| 64 |
|
|---|
| 65 | if (console_phone < 0) {
|
|---|
| 66 | while ((console_phone = ipc_connect_me_to(PHONE_NS, SERVICE_CONSOLE, 0)) < 0) {
|
|---|
| 67 | usleep(10000);
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | stream.r = read_stdin;
|
|---|
| 72 | stream.param = 0;
|
|---|
| 73 | return stream;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | static stream_t open_stdout(void)
|
|---|
| 77 | {
|
|---|
| 78 | stream_t stream;
|
|---|
| 79 | int res;
|
|---|
| 80 |
|
|---|
| 81 | if (console_phone < 0) {
|
|---|
| 82 | while ((console_phone = ipc_connect_me_to(PHONE_NS, SERVICE_CONSOLE, 0)) < 0) {
|
|---|
| 83 | volatile int a;
|
|---|
| 84 | for (a = 0; a < 1048576; a++);
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | stream.w = write_stdout;
|
|---|
| 89 | stream.param = 0;
|
|---|
| 90 | return stream;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | fd_t open(const char *fname, int flags)
|
|---|
| 94 | {
|
|---|
| 95 | int c = 0;
|
|---|
| 96 |
|
|---|
| 97 | while (((streams[c].w) || (streams[c].r)) && (c < FDS))
|
|---|
| 98 | c++;
|
|---|
| 99 | if (c == FDS)
|
|---|
| 100 | return EMFILE;
|
|---|
| 101 |
|
|---|
| 102 | if (!strcmp(fname, "stdin")) {
|
|---|
| 103 | streams[c] = open_stdin();
|
|---|
| 104 | return c;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | if (!strcmp(fname, "stdout")) {
|
|---|
| 108 | //streams[c].w = write_stdout;
|
|---|
| 109 | //return c;
|
|---|
| 110 | streams[c] = open_stdout();
|
|---|
| 111 | return c;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | if (!strcmp(fname, "stderr")) {
|
|---|
| 115 | streams[c].w = write_stderr;
|
|---|
| 116 | return c;
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 | ssize_t write(int fd, const void *buf, size_t count)
|
|---|
| 122 | {
|
|---|
| 123 | if (fd < FDS)
|
|---|
| 124 | return streams[fd].w(streams[fd].param, buf, count);
|
|---|
| 125 |
|
|---|
| 126 | return 0;
|
|---|
| 127 | }
|
|---|