source: mainline/uspace/lib/libc/generic/io/stream.c@ b61d47d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b61d47d was b61d47d, checked in by Josef Cejka <malyzelenyhnus@…>, 18 years ago

Function ipc_connect_me_to sends 3 user defined arguments now.
One argument added also to ipc_forward_fast.
Fixed devmap and improved its test.

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[b27a97bb]1/*
[df4ed85]2 * Copyright (c) 2006 Josef Cejka
3 * Copyright (c) 2006 Jakub Vana
[b27a97bb]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
[afa6e74]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>
[79460ae]45#include <console.h>
[b27a97bb]46#include <unistd.h>
[b917098]47#include <async.h>
[5d4e90f0]48#include <sys/types.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{
[975f62f]69 ipcarg_t r0, r1;
[b27a97bb]70 size_t i = 0;
71
72 while (i < count) {
[0cc4313]73 if (async_req_0_2(streams[0].phone, CONSOLE_GETCHAR, &r0,
[975f62f]74 &r1) < 0) {
[b27a97bb]75 return -1;
76 }
[a46da63]77 ((char *) buf)[i++] = r0;
[b27a97bb]78 }
79 return i;
[afa6e74]80}
[b27a97bb]81
[79460ae]82static ssize_t write_stdout(void *param, const void *buf, size_t count)
[afa6e74]83{
84 int i;
[79460ae]85
[04552a80]86 for (i = 0; i < count; i++)
[0cc4313]87 async_msg_1(streams[1].phone, CONSOLE_PUTCHAR,
[975f62f]88 ((const char *) buf)[i]);
[afa6e74]89
90 return count;
91}
92
[79460ae]93static stream_t open_stdin(void)
[afa6e74]94{
95 stream_t stream;
96
[79460ae]97 if (console_phone < 0) {
[975f62f]98 while ((console_phone = ipc_connect_me_to(PHONE_NS,
[b61d47d]99 SERVICE_CONSOLE, 0, 0)) < 0) {
[44c6d88d]100 usleep(10000);
[79460ae]101 }
[afa6e74]102 }
103
[79460ae]104 stream.r = read_stdin;
[a46da63]105 stream.w = NULL;
[79460ae]106 stream.param = 0;
[b917098]107 stream.phone = console_phone;
108
[79460ae]109 return stream;
110}
111
112static stream_t open_stdout(void)
113{
114 stream_t stream;
[b917098]115
[79460ae]116 if (console_phone < 0) {
[975f62f]117 while ((console_phone = ipc_connect_me_to(PHONE_NS,
[b61d47d]118 SERVICE_CONSOLE, 0, 0)) < 0) {
[ad123964]119 usleep(10000);
[79460ae]120 }
121 }
[afa6e74]122
[a46da63]123 stream.r = NULL;
[79460ae]124 stream.w = write_stdout;
[6c46350]125 stream.phone = console_phone;
[79460ae]126 stream.param = 0;
[a46da63]127
[afa6e74]128 return stream;
129}
130
[37458472]131static ssize_t write_null(void *param, const void *buf, size_t count)
132{
133 return count;
134}
135
[04552a80]136fd_t open(const char *fname, int flags)
[afa6e74]137{
[04552a80]138 int c = 0;
[085bd54]139
[04552a80]140 while (((streams[c].w) || (streams[c].r)) && (c < FDS))
141 c++;
[a46da63]142
[04552a80]143 if (c == FDS)
144 return EMFILE;
145
146 if (!strcmp(fname, "stdin")) {
[79460ae]147 streams[c] = open_stdin();
[afa6e74]148 return c;
149 }
150
[04552a80]151 if (!strcmp(fname, "stdout")) {
[79460ae]152 streams[c] = open_stdout();
[afa6e74]153 return c;
154 }
155
[04552a80]156 if (!strcmp(fname, "stderr")) {
157 streams[c].w = write_stderr;
[afa6e74]158 return c;
159 }
[a46da63]160
[37458472]161 if (!strcmp(fname, "null")) {
162 streams[c].w = write_null;
163 return c;
164 }
[a46da63]165
166 return -1;
[afa6e74]167}
168
169
[04552a80]170ssize_t write(int fd, const void *buf, size_t count)
[afa6e74]171{
[9dae51d7]172 if (fd < FDS && streams[fd].w)
[04552a80]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{
[9dae51d7]180 if (fd < FDS && streams[fd].r)
[b27a97bb]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
[a46da63]193/** @}
[b2951e2]194 */
Note: See TracBrowser for help on using the repository browser.