Rev 2541 | 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> |
2541 | jermar | 48 | #include <sys/types.h> |
1363 | vana | 49 | |
50 | #define FDS 32 |
||
51 | |||
1366 | decky | 52 | typedef struct stream_t { |
1363 | vana | 53 | pwritefn_t w; |
54 | preadfn_t r; |
||
55 | void * param; |
||
1519 | palkovsky | 56 | int phone; |
1366 | decky | 57 | } stream_t; |
1363 | vana | 58 | |
1519 | palkovsky | 59 | static int console_phone = -1; |
1536 | palkovsky | 60 | static stream_t streams[FDS]; |
1363 | vana | 61 | |
1451 | cejka | 62 | static ssize_t write_stderr(void *param, const void *buf, size_t count) |
1363 | vana | 63 | { |
1451 | cejka | 64 | return count; |
65 | } |
||
66 | |||
1465 | cejka | 67 | static ssize_t read_stdin(void *param, void *buf, size_t count) |
1451 | cejka | 68 | { |
2495 | jermar | 69 | ipcarg_t r0, r1; |
1465 | cejka | 70 | size_t i = 0; |
71 | |||
72 | while (i < count) { |
||
2621 | jermar | 73 | if (async_req_0_2(streams[0].phone, CONSOLE_GETCHAR, &r0, |
2495 | jermar | 74 | &r1) < 0) { |
1465 | cejka | 75 | return -1; |
76 | } |
||
1719 | decky | 77 | ((char *) buf)[i++] = r0; |
1465 | cejka | 78 | } |
79 | return i; |
||
1363 | vana | 80 | } |
1465 | cejka | 81 | |
1451 | cejka | 82 | static ssize_t write_stdout(void *param, const void *buf, size_t count) |
1363 | vana | 83 | { |
84 | int i; |
||
1451 | cejka | 85 | |
1366 | decky | 86 | for (i = 0; i < count; i++) |
2621 | jermar | 87 | async_msg_1(streams[1].phone, CONSOLE_PUTCHAR, |
2495 | jermar | 88 | ((const char *) buf)[i]); |
1363 | vana | 89 | |
90 | return count; |
||
91 | } |
||
92 | |||
1451 | cejka | 93 | static stream_t open_stdin(void) |
1363 | vana | 94 | { |
95 | stream_t stream; |
||
96 | |||
1451 | cejka | 97 | if (console_phone < 0) { |
2495 | jermar | 98 | while ((console_phone = ipc_connect_me_to(PHONE_NS, |
99 | SERVICE_CONSOLE, 0)) < 0) { |
||
1452 | palkovsky | 100 | usleep(10000); |
1451 | cejka | 101 | } |
1363 | vana | 102 | } |
103 | |||
1451 | cejka | 104 | stream.r = read_stdin; |
1719 | decky | 105 | stream.w = NULL; |
1451 | cejka | 106 | stream.param = 0; |
1519 | palkovsky | 107 | stream.phone = console_phone; |
108 | |||
1451 | cejka | 109 | return stream; |
110 | } |
||
111 | |||
112 | static stream_t open_stdout(void) |
||
113 | { |
||
114 | stream_t stream; |
||
1519 | palkovsky | 115 | |
1451 | cejka | 116 | if (console_phone < 0) { |
2495 | jermar | 117 | while ((console_phone = ipc_connect_me_to(PHONE_NS, |
118 | SERVICE_CONSOLE, 0)) < 0) { |
||
1476 | cejka | 119 | usleep(10000); |
1451 | cejka | 120 | } |
121 | } |
||
1363 | vana | 122 | |
1719 | decky | 123 | stream.r = NULL; |
1451 | cejka | 124 | stream.w = write_stdout; |
1536 | palkovsky | 125 | stream.phone = console_phone; |
1451 | cejka | 126 | stream.param = 0; |
1719 | decky | 127 | |
1363 | vana | 128 | return stream; |
129 | } |
||
130 | |||
1508 | vana | 131 | static ssize_t write_null(void *param, const void *buf, size_t count) |
132 | { |
||
133 | return count; |
||
134 | } |
||
135 | |||
1366 | decky | 136 | fd_t open(const char *fname, int flags) |
1363 | vana | 137 | { |
1366 | decky | 138 | int c = 0; |
1610 | palkovsky | 139 | |
1366 | decky | 140 | while (((streams[c].w) || (streams[c].r)) && (c < FDS)) |
141 | c++; |
||
1719 | decky | 142 | |
1366 | decky | 143 | if (c == FDS) |
144 | return EMFILE; |
||
145 | |||
146 | if (!strcmp(fname, "stdin")) { |
||
1451 | cejka | 147 | streams[c] = open_stdin(); |
1363 | vana | 148 | return c; |
149 | } |
||
150 | |||
1366 | decky | 151 | if (!strcmp(fname, "stdout")) { |
1451 | cejka | 152 | streams[c] = open_stdout(); |
1363 | vana | 153 | return c; |
154 | } |
||
155 | |||
1366 | decky | 156 | if (!strcmp(fname, "stderr")) { |
157 | streams[c].w = write_stderr; |
||
1363 | vana | 158 | return c; |
159 | } |
||
1719 | decky | 160 | |
1508 | vana | 161 | if (!strcmp(fname, "null")) { |
162 | streams[c].w = write_null; |
||
163 | return c; |
||
164 | } |
||
1719 | decky | 165 | |
166 | return -1; |
||
1363 | vana | 167 | } |
168 | |||
169 | |||
1366 | decky | 170 | ssize_t write(int fd, const void *buf, size_t count) |
1363 | vana | 171 | { |
1872 | jermar | 172 | if (fd < FDS && streams[fd].w) |
1366 | decky | 173 | return streams[fd].w(streams[fd].param, buf, count); |
174 | |||
1363 | vana | 175 | return 0; |
176 | } |
||
1465 | cejka | 177 | |
178 | ssize_t read(int fd, void *buf, size_t count) |
||
179 | { |
||
1872 | jermar | 180 | if (fd < FDS && streams[fd].r) |
1465 | cejka | 181 | return streams[fd].r(streams[fd].param, buf, count); |
182 | |||
183 | return 0; |
||
184 | } |
||
185 | |||
1519 | palkovsky | 186 | int get_fd_phone(int fd) |
187 | { |
||
188 | if (fd >= FDS || fd < 0) |
||
189 | return -1; |
||
190 | return streams[fd].phone; |
||
191 | } |
||
1653 | cejka | 192 | |
1719 | decky | 193 | /** @} |
1653 | cejka | 194 | */ |