Subversion Repositories HelenOS

Rev

Rev 2479 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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