Rev 2495 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2495 | Rev 2541 | ||
---|---|---|---|
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 | #include <io/io.h> |
36 | #include <io/io.h> |
37 | #include <io/stream.h> |
37 | #include <io/stream.h> |
38 | #include <string.h> |
38 | #include <string.h> |
39 | #include <malloc.h> |
39 | #include <malloc.h> |
40 | #include <libc.h> |
40 | #include <libc.h> |
41 | #include <ipc/ipc.h> |
41 | #include <ipc/ipc.h> |
42 | #include <ipc/ns.h> |
42 | #include <ipc/ns.h> |
43 | #include <ipc/fb.h> |
43 | #include <ipc/fb.h> |
44 | #include <ipc/services.h> |
44 | #include <ipc/services.h> |
45 | #include <console.h> |
45 | #include <console.h> |
46 | #include <unistd.h> |
46 | #include <unistd.h> |
47 | #include <async.h> |
47 | #include <async.h> |
- | 48 | #include <sys/types.h> |
|
48 | 49 | ||
49 | #define FDS 32 |
50 | #define FDS 32 |
50 | 51 | ||
51 | typedef struct stream_t { |
52 | typedef struct stream_t { |
52 | pwritefn_t w; |
53 | pwritefn_t w; |
53 | preadfn_t r; |
54 | preadfn_t r; |
54 | void * param; |
55 | void * param; |
55 | int phone; |
56 | int phone; |
56 | } stream_t; |
57 | } stream_t; |
57 | 58 | ||
58 | static int console_phone = -1; |
59 | static int console_phone = -1; |
59 | static stream_t streams[FDS]; |
60 | static stream_t streams[FDS]; |
60 | 61 | ||
61 | 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) |
62 | { |
63 | { |
63 | return count; |
64 | return count; |
64 | } |
65 | } |
65 | 66 | ||
66 | 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) |
67 | { |
68 | { |
68 | ipcarg_t r0, r1; |
69 | ipcarg_t r0, r1; |
69 | size_t i = 0; |
70 | size_t i = 0; |
70 | 71 | ||
71 | while (i < count) { |
72 | while (i < count) { |
72 | if (async_req_2(streams[0].phone, CONSOLE_GETCHAR, 0, 0, &r0, |
73 | if (async_req_2(streams[0].phone, CONSOLE_GETCHAR, 0, 0, &r0, |
73 | &r1) < 0) { |
74 | &r1) < 0) { |
74 | return -1; |
75 | return -1; |
75 | } |
76 | } |
76 | ((char *) buf)[i++] = r0; |
77 | ((char *) buf)[i++] = r0; |
77 | } |
78 | } |
78 | return i; |
79 | return i; |
79 | } |
80 | } |
80 | 81 | ||
81 | static ssize_t write_stdout(void *param, const void *buf, size_t count) |
82 | static ssize_t write_stdout(void *param, const void *buf, size_t count) |
82 | { |
83 | { |
83 | int i; |
84 | int i; |
84 | 85 | ||
85 | for (i = 0; i < count; i++) |
86 | for (i = 0; i < count; i++) |
86 | async_msg(streams[1].phone, CONSOLE_PUTCHAR, |
87 | async_msg(streams[1].phone, CONSOLE_PUTCHAR, |
87 | ((const char *) buf)[i]); |
88 | ((const char *) buf)[i]); |
88 | 89 | ||
89 | return count; |
90 | return count; |
90 | } |
91 | } |
91 | 92 | ||
92 | static stream_t open_stdin(void) |
93 | static stream_t open_stdin(void) |
93 | { |
94 | { |
94 | stream_t stream; |
95 | stream_t stream; |
95 | 96 | ||
96 | if (console_phone < 0) { |
97 | if (console_phone < 0) { |
97 | while ((console_phone = ipc_connect_me_to(PHONE_NS, |
98 | while ((console_phone = ipc_connect_me_to(PHONE_NS, |
98 | SERVICE_CONSOLE, 0)) < 0) { |
99 | SERVICE_CONSOLE, 0)) < 0) { |
99 | usleep(10000); |
100 | usleep(10000); |
100 | } |
101 | } |
101 | } |
102 | } |
102 | 103 | ||
103 | stream.r = read_stdin; |
104 | stream.r = read_stdin; |
104 | stream.w = NULL; |
105 | stream.w = NULL; |
105 | stream.param = 0; |
106 | stream.param = 0; |
106 | stream.phone = console_phone; |
107 | stream.phone = console_phone; |
107 | 108 | ||
108 | return stream; |
109 | return stream; |
109 | } |
110 | } |
110 | 111 | ||
111 | static stream_t open_stdout(void) |
112 | static stream_t open_stdout(void) |
112 | { |
113 | { |
113 | stream_t stream; |
114 | stream_t stream; |
114 | 115 | ||
115 | if (console_phone < 0) { |
116 | if (console_phone < 0) { |
116 | while ((console_phone = ipc_connect_me_to(PHONE_NS, |
117 | while ((console_phone = ipc_connect_me_to(PHONE_NS, |
117 | SERVICE_CONSOLE, 0)) < 0) { |
118 | SERVICE_CONSOLE, 0)) < 0) { |
118 | usleep(10000); |
119 | usleep(10000); |
119 | } |
120 | } |
120 | } |
121 | } |
121 | 122 | ||
122 | stream.r = NULL; |
123 | stream.r = NULL; |
123 | stream.w = write_stdout; |
124 | stream.w = write_stdout; |
124 | stream.phone = console_phone; |
125 | stream.phone = console_phone; |
125 | stream.param = 0; |
126 | stream.param = 0; |
126 | 127 | ||
127 | return stream; |
128 | return stream; |
128 | } |
129 | } |
129 | 130 | ||
130 | static ssize_t write_null(void *param, const void *buf, size_t count) |
131 | static ssize_t write_null(void *param, const void *buf, size_t count) |
131 | { |
132 | { |
132 | return count; |
133 | return count; |
133 | } |
134 | } |
134 | 135 | ||
135 | fd_t open(const char *fname, int flags) |
136 | fd_t open(const char *fname, int flags) |
136 | { |
137 | { |
137 | int c = 0; |
138 | int c = 0; |
138 | 139 | ||
139 | while (((streams[c].w) || (streams[c].r)) && (c < FDS)) |
140 | while (((streams[c].w) || (streams[c].r)) && (c < FDS)) |
140 | c++; |
141 | c++; |
141 | 142 | ||
142 | if (c == FDS) |
143 | if (c == FDS) |
143 | return EMFILE; |
144 | return EMFILE; |
144 | 145 | ||
145 | if (!strcmp(fname, "stdin")) { |
146 | if (!strcmp(fname, "stdin")) { |
146 | streams[c] = open_stdin(); |
147 | streams[c] = open_stdin(); |
147 | return c; |
148 | return c; |
148 | } |
149 | } |
149 | 150 | ||
150 | if (!strcmp(fname, "stdout")) { |
151 | if (!strcmp(fname, "stdout")) { |
151 | streams[c] = open_stdout(); |
152 | streams[c] = open_stdout(); |
152 | return c; |
153 | return c; |
153 | } |
154 | } |
154 | 155 | ||
155 | if (!strcmp(fname, "stderr")) { |
156 | if (!strcmp(fname, "stderr")) { |
156 | streams[c].w = write_stderr; |
157 | streams[c].w = write_stderr; |
157 | return c; |
158 | return c; |
158 | } |
159 | } |
159 | 160 | ||
160 | if (!strcmp(fname, "null")) { |
161 | if (!strcmp(fname, "null")) { |
161 | streams[c].w = write_null; |
162 | streams[c].w = write_null; |
162 | return c; |
163 | return c; |
163 | } |
164 | } |
164 | 165 | ||
165 | return -1; |
166 | return -1; |
166 | } |
167 | } |
167 | 168 | ||
168 | 169 | ||
169 | ssize_t write(int fd, const void *buf, size_t count) |
170 | ssize_t write(int fd, const void *buf, size_t count) |
170 | { |
171 | { |
171 | if (fd < FDS && streams[fd].w) |
172 | if (fd < FDS && streams[fd].w) |
172 | return streams[fd].w(streams[fd].param, buf, count); |
173 | return streams[fd].w(streams[fd].param, buf, count); |
173 | 174 | ||
174 | return 0; |
175 | return 0; |
175 | } |
176 | } |
176 | 177 | ||
177 | ssize_t read(int fd, void *buf, size_t count) |
178 | ssize_t read(int fd, void *buf, size_t count) |
178 | { |
179 | { |
179 | if (fd < FDS && streams[fd].r) |
180 | if (fd < FDS && streams[fd].r) |
180 | return streams[fd].r(streams[fd].param, buf, count); |
181 | return streams[fd].r(streams[fd].param, buf, count); |
181 | 182 | ||
182 | return 0; |
183 | return 0; |
183 | } |
184 | } |
184 | 185 | ||
185 | int get_fd_phone(int fd) |
186 | int get_fd_phone(int fd) |
186 | { |
187 | { |
187 | if (fd >= FDS || fd < 0) |
188 | if (fd >= FDS || fd < 0) |
188 | return -1; |
189 | return -1; |
189 | return streams[fd].phone; |
190 | return streams[fd].phone; |
190 | } |
191 | } |
191 | 192 | ||
192 | /** @} |
193 | /** @} |
193 | */ |
194 | */ |
194 | 195 |