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