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