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