source: mainline/libc/generic/io/stream.c@ 153a209

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 153a209 was a46da63, checked in by Martin Decky <martin@…>, 20 years ago

big code cleanup, compile with -Wall -Werror to enforce better coding
there is currently one warning that requires attention, please review

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[b27a97bb]1/*
2 * Copyright (C) 2006 Josef Cejka
3 * Copyright (C) 2006 Jakub Vana
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
[a46da63]30/** @addtogroup libc
[b2951e2]31 * @{
32 */
33/** @file
34 */
35
[b27a97bb]36
[afa6e74]37#include <io/io.h>
38#include <io/stream.h>
39#include <string.h>
40#include <malloc.h>
41#include <libc.h>
42#include <ipc/ipc.h>
43#include <ipc/ns.h>
44#include <ipc/fb.h>
45#include <ipc/services.h>
[79460ae]46#include <console.h>
[b27a97bb]47#include <unistd.h>
[b917098]48#include <async.h>
[afa6e74]49
50#define FDS 32
51
[04552a80]52typedef struct stream_t {
[afa6e74]53 pwritefn_t w;
54 preadfn_t r;
55 void * param;
[b917098]56 int phone;
[04552a80]57} stream_t;
[afa6e74]58
[b917098]59static int console_phone = -1;
[6c46350]60static stream_t streams[FDS];
[afa6e74]61
[79460ae]62static ssize_t write_stderr(void *param, const void *buf, size_t count)
63{
64 return count;
65}
66
[b27a97bb]67static ssize_t read_stdin(void *param, void *buf, size_t count)
[afa6e74]68{
69 ipcarg_t r0,r1;
[b27a97bb]70 size_t i = 0;
71
72 while (i < count) {
[085bd54]73 if (async_req_2(streams[0].phone, CONSOLE_GETCHAR, 0, 0, &r0, &r1) < 0) {
[b27a97bb]74 return -1;
75 }
[a46da63]76 ((char *) buf)[i++] = r0;
[b27a97bb]77 }
78 return i;
[afa6e74]79}
[b27a97bb]80
[79460ae]81static ssize_t write_stdout(void *param, const void *buf, size_t count)
[afa6e74]82{
83 int i;
[79460ae]84
[04552a80]85 for (i = 0; i < count; i++)
[a46da63]86 async_msg(streams[1].phone, CONSOLE_PUTCHAR, ((const char *) buf)[i]);
[afa6e74]87
88 return count;
89}
90
91
[79460ae]92static stream_t open_stdin(void)
[afa6e74]93{
94 stream_t stream;
95
[79460ae]96 if (console_phone < 0) {
97 while ((console_phone = ipc_connect_me_to(PHONE_NS, SERVICE_CONSOLE, 0)) < 0) {
[44c6d88d]98 usleep(10000);
[79460ae]99 }
[afa6e74]100 }
101
[79460ae]102 stream.r = read_stdin;
[a46da63]103 stream.w = NULL;
[79460ae]104 stream.param = 0;
[b917098]105 stream.phone = console_phone;
106
[79460ae]107 return stream;
108}
109
110static stream_t open_stdout(void)
111{
112 stream_t stream;
[b917098]113
[79460ae]114 if (console_phone < 0) {
115 while ((console_phone = ipc_connect_me_to(PHONE_NS, SERVICE_CONSOLE, 0)) < 0) {
[ad123964]116 usleep(10000);
[79460ae]117 }
118 }
[afa6e74]119
[a46da63]120 stream.r = NULL;
[79460ae]121 stream.w = write_stdout;
[6c46350]122 stream.phone = console_phone;
[79460ae]123 stream.param = 0;
[a46da63]124
[afa6e74]125 return stream;
126}
127
[37458472]128static ssize_t write_null(void *param, const void *buf, size_t count)
129{
130 return count;
131}
132
133
[04552a80]134fd_t open(const char *fname, int flags)
[afa6e74]135{
[04552a80]136 int c = 0;
[085bd54]137
[04552a80]138 while (((streams[c].w) || (streams[c].r)) && (c < FDS))
139 c++;
[a46da63]140
[04552a80]141 if (c == FDS)
142 return EMFILE;
143
144 if (!strcmp(fname, "stdin")) {
[79460ae]145 streams[c] = open_stdin();
[afa6e74]146 return c;
147 }
148
[04552a80]149 if (!strcmp(fname, "stdout")) {
[79460ae]150 streams[c] = open_stdout();
[afa6e74]151 return c;
152 }
153
[04552a80]154 if (!strcmp(fname, "stderr")) {
155 streams[c].w = write_stderr;
[afa6e74]156 return c;
157 }
[a46da63]158
[37458472]159 if (!strcmp(fname, "null")) {
160 streams[c].w = write_null;
161 return c;
162 }
[a46da63]163
164 return -1;
[afa6e74]165}
166
167
[04552a80]168ssize_t write(int fd, const void *buf, size_t count)
[afa6e74]169{
[085bd54]170// __SYSCALL3(SYS_IO, 1, (sysarg_t)buf, (sysarg_t) count);
171// return count;
[04552a80]172 if (fd < FDS)
173 return streams[fd].w(streams[fd].param, buf, count);
174
[afa6e74]175 return 0;
176}
[b27a97bb]177
178ssize_t read(int fd, void *buf, size_t count)
179{
180 if (fd < FDS)
181 return streams[fd].r(streams[fd].param, buf, count);
182
183 return 0;
184}
185
[b917098]186int get_fd_phone(int fd)
187{
188 if (fd >= FDS || fd < 0)
189 return -1;
190 return streams[fd].phone;
191}
[b2951e2]192
193
[a46da63]194/** @}
[b2951e2]195 */
Note: See TracBrowser for help on using the repository browser.