Rev 2479 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1465 | cejka | 1 | /* |
2071 | jermar | 2 | * Copyright (c) 2006 Josef Cejka |
3 | * Copyright (c) 2006 Jakub Vana |
||
1465 | cejka | 4 | * All rights reserved. |
5 | * |
||
6 | * Redistribution and use in source and binary forms, with or without |
||
7 | * modification, are permitted provided that the following conditions |
||
8 | * are met: |
||
9 | * |
||
10 | * - Redistributions of source code must retain the above copyright |
||
11 | * notice, this list of conditions and the following disclaimer. |
||
12 | * - Redistributions in binary form must reproduce the above copyright |
||
13 | * notice, this list of conditions and the following disclaimer in the |
||
14 | * documentation and/or other materials provided with the distribution. |
||
15 | * - The name of the author may not be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
1719 | decky | 30 | /** @addtogroup libc |
1653 | cejka | 31 | * @{ |
32 | */ |
||
33 | /** @file |
||
34 | */ |
||
1465 | cejka | 35 | |
1363 | vana | 36 | #include <io/io.h> |
37 | #include <io/stream.h> |
||
38 | #include <string.h> |
||
39 | #include <malloc.h> |
||
40 | #include <libc.h> |
||
41 | #include <ipc/ipc.h> |
||
42 | #include <ipc/ns.h> |
||
43 | #include <ipc/fb.h> |
||
44 | #include <ipc/services.h> |
||
1451 | cejka | 45 | #include <console.h> |
1465 | cejka | 46 | #include <unistd.h> |
1519 | palkovsky | 47 | #include <async.h> |
1363 | vana | 48 | |
49 | #define FDS 32 |
||
50 | |||
1366 | decky | 51 | typedef struct stream_t { |
1363 | vana | 52 | pwritefn_t w; |
53 | preadfn_t r; |
||
54 | void * param; |
||
1519 | palkovsky | 55 | int phone; |
1366 | decky | 56 | } stream_t; |
1363 | vana | 57 | |
1519 | palkovsky | 58 | static int console_phone = -1; |
1536 | palkovsky | 59 | static stream_t streams[FDS]; |
1363 | vana | 60 | |
1451 | cejka | 61 | static ssize_t write_stderr(void *param, const void *buf, size_t count) |
1363 | vana | 62 | { |
1451 | cejka | 63 | return count; |
64 | } |
||
65 | |||
1465 | cejka | 66 | static ssize_t read_stdin(void *param, void *buf, size_t count) |
1451 | cejka | 67 | { |
2495 | jermar | 68 | ipcarg_t r0, r1; |
1465 | cejka | 69 | size_t i = 0; |
70 | |||
71 | while (i < count) { |
||
2495 | jermar | 72 | if (async_req_2(streams[0].phone, CONSOLE_GETCHAR, 0, 0, &r0, |
73 | &r1) < 0) { |
||
1465 | cejka | 74 | return -1; |
75 | } |
||
1719 | decky | 76 | ((char *) buf)[i++] = r0; |
1465 | cejka | 77 | } |
78 | return i; |
||
1363 | vana | 79 | } |
1465 | cejka | 80 | |
1451 | cejka | 81 | static ssize_t write_stdout(void *param, const void *buf, size_t count) |
1363 | vana | 82 | { |
83 | int i; |
||
1451 | cejka | 84 | |
1366 | decky | 85 | for (i = 0; i < count; i++) |
2495 | jermar | 86 | async_msg(streams[1].phone, CONSOLE_PUTCHAR, |
87 | ((const char *) buf)[i]); |
||
1363 | vana | 88 | |
89 | return count; |
||
90 | } |
||
91 | |||
1451 | cejka | 92 | static stream_t open_stdin(void) |
1363 | vana | 93 | { |
94 | stream_t stream; |
||
95 | |||
1451 | cejka | 96 | if (console_phone < 0) { |
2495 | jermar | 97 | while ((console_phone = ipc_connect_me_to(PHONE_NS, |
98 | SERVICE_CONSOLE, 0)) < 0) { |
||
1452 | palkovsky | 99 | usleep(10000); |
1451 | cejka | 100 | } |
1363 | vana | 101 | } |
102 | |||
1451 | cejka | 103 | stream.r = read_stdin; |
1719 | decky | 104 | stream.w = NULL; |
1451 | cejka | 105 | stream.param = 0; |
1519 | palkovsky | 106 | stream.phone = console_phone; |
107 | |||
1451 | cejka | 108 | return stream; |
109 | } |
||
110 | |||
111 | static stream_t open_stdout(void) |
||
112 | { |
||
113 | stream_t stream; |
||
1519 | palkovsky | 114 | |
1451 | cejka | 115 | if (console_phone < 0) { |
2495 | jermar | 116 | while ((console_phone = ipc_connect_me_to(PHONE_NS, |
117 | SERVICE_CONSOLE, 0)) < 0) { |
||
1476 | cejka | 118 | usleep(10000); |
1451 | cejka | 119 | } |
120 | } |
||
1363 | vana | 121 | |
1719 | decky | 122 | stream.r = NULL; |
1451 | cejka | 123 | stream.w = write_stdout; |
1536 | palkovsky | 124 | stream.phone = console_phone; |
1451 | cejka | 125 | stream.param = 0; |
1719 | decky | 126 | |
1363 | vana | 127 | return stream; |
128 | } |
||
129 | |||
1508 | vana | 130 | static ssize_t write_null(void *param, const void *buf, size_t count) |
131 | { |
||
132 | return count; |
||
133 | } |
||
134 | |||
1366 | decky | 135 | fd_t open(const char *fname, int flags) |
1363 | vana | 136 | { |
1366 | decky | 137 | int c = 0; |
1610 | palkovsky | 138 | |
1366 | decky | 139 | while (((streams[c].w) || (streams[c].r)) && (c < FDS)) |
140 | c++; |
||
1719 | decky | 141 | |
1366 | decky | 142 | if (c == FDS) |
143 | return EMFILE; |
||
144 | |||
145 | if (!strcmp(fname, "stdin")) { |
||
1451 | cejka | 146 | streams[c] = open_stdin(); |
1363 | vana | 147 | return c; |
148 | } |
||
149 | |||
1366 | decky | 150 | if (!strcmp(fname, "stdout")) { |
1451 | cejka | 151 | streams[c] = open_stdout(); |
1363 | vana | 152 | return c; |
153 | } |
||
154 | |||
1366 | decky | 155 | if (!strcmp(fname, "stderr")) { |
156 | streams[c].w = write_stderr; |
||
1363 | vana | 157 | return c; |
158 | } |
||
1719 | decky | 159 | |
1508 | vana | 160 | if (!strcmp(fname, "null")) { |
161 | streams[c].w = write_null; |
||
162 | return c; |
||
163 | } |
||
1719 | decky | 164 | |
165 | return -1; |
||
1363 | vana | 166 | } |
167 | |||
168 | |||
1366 | decky | 169 | ssize_t write(int fd, const void *buf, size_t count) |
1363 | vana | 170 | { |
1872 | jermar | 171 | if (fd < FDS && streams[fd].w) |
1366 | decky | 172 | return streams[fd].w(streams[fd].param, buf, count); |
173 | |||
1363 | vana | 174 | return 0; |
175 | } |
||
1465 | cejka | 176 | |
177 | ssize_t read(int fd, void *buf, size_t count) |
||
178 | { |
||
1872 | jermar | 179 | if (fd < FDS && streams[fd].r) |
1465 | cejka | 180 | return streams[fd].r(streams[fd].param, buf, count); |
181 | |||
182 | return 0; |
||
183 | } |
||
184 | |||
1519 | palkovsky | 185 | int get_fd_phone(int fd) |
186 | { |
||
187 | if (fd >= FDS || fd < 0) |
||
188 | return -1; |
||
189 | return streams[fd].phone; |
||
190 | } |
||
1653 | cejka | 191 | |
1719 | decky | 192 | /** @} |
1653 | cejka | 193 | */ |