Subversion Repositories HelenOS-historic

Rev

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

Rev 1451 Rev 1452
1
#include <io/io.h>
1
#include <io/io.h>
2
#include <io/stream.h>
2
#include <io/stream.h>
3
#include <string.h>
3
#include <string.h>
4
#include <malloc.h>
4
#include <malloc.h>
5
#include <libc.h>
5
#include <libc.h>
6
#include <ipc/ipc.h>
6
#include <ipc/ipc.h>
7
#include <ipc/ns.h>
7
#include <ipc/ns.h>
8
#include <ipc/fb.h>
8
#include <ipc/fb.h>
9
#include <ipc/services.h>
9
#include <ipc/services.h>
10
#include <console.h>
10
#include <console.h>
11
 
11
 
12
#define FDS 32
12
#define FDS 32
13
 
13
 
14
typedef struct stream_t {
14
typedef struct stream_t {
15
    pwritefn_t w;
15
    pwritefn_t w;
16
    preadfn_t r;
16
    preadfn_t r;
17
    void * param;
17
    void * param;
18
} stream_t;
18
} stream_t;
19
 
19
 
20
int console_phone = -1;
20
int console_phone = -1;
21
 
21
 
22
stream_t streams[FDS] = {{0, 0, 0}};
22
stream_t streams[FDS] = {{0, 0, 0}};
23
 
23
 
24
/*
24
/*
25
ssize_t write_stdout(void *param, const void * buf, size_t count);
25
ssize_t write_stdout(void *param, const void * buf, size_t count);
26
ssize_t write_stdout(void *param, const void * buf, size_t count)
26
ssize_t write_stdout(void *param, const void * buf, size_t count)
27
{
27
{
28
    return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
28
    return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
29
}*/
29
}*/
30
 
30
 
31
static ssize_t write_stderr(void *param, const void *buf, size_t count)
31
static ssize_t write_stderr(void *param, const void *buf, size_t count)
32
{
32
{
33
    return count;
33
    return count;
34
    //return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
34
    //return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
35
}
35
}
36
 
36
 
37
static char read_stdin(void)
37
static char read_stdin(void)
38
{
38
{
39
    ipcarg_t r0,r1;
39
    ipcarg_t r0,r1;
40
    ipc_call_sync_2(console_phone, CONSOLE_GETCHAR, 0, 0, &r0, &r1);
40
    ipc_call_sync_2(console_phone, CONSOLE_GETCHAR, 0, 0, &r0, &r1);
41
   
41
   
42
    return r0;
42
    return r0;
43
    //return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
43
    //return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
44
}
44
}
45
static ssize_t write_stdout(void *param, const void *buf, size_t count)
45
static ssize_t write_stdout(void *param, const void *buf, size_t count)
46
{
46
{
47
    int i;
47
    int i;
48
    ipcarg_t r0,r1;
48
    ipcarg_t r0,r1;
49
 
49
 
50
    for (i = 0; i < count; i++)
50
    for (i = 0; i < count; i++)
51
        ipc_call_sync_2(console_phone, CONSOLE_PUTCHAR, 0, ((const char *)buf)[i], &r0, &r1);
51
        ipc_call_sync_2(console_phone, CONSOLE_PUTCHAR, 0, ((const char *)buf)[i], &r0, &r1);
52
   
52
   
53
    return count;
53
    return count;
54
    //return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
54
    //return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
55
}
55
}
56
 
56
 
57
 
57
 
58
 
58
 
59
static stream_t open_stdin(void)
59
static stream_t open_stdin(void)
60
{
60
{
61
    stream_t stream;
61
    stream_t stream;
62
    int phoneid;
62
    int phoneid;
63
    int res;
63
    int res;
64
   
64
   
65
    if (console_phone < 0) {
65
    if (console_phone < 0) {
66
        while ((console_phone = ipc_connect_me_to(PHONE_NS, SERVICE_CONSOLE, 0)) < 0) {
66
        while ((console_phone = ipc_connect_me_to(PHONE_NS, SERVICE_CONSOLE, 0)) < 0) {
67
            volatile int a;
67
            usleep(10000);
68
            for (a = 0; a < 1048576; a++);
-
 
69
        }
68
        }
70
    }
69
    }
71
   
70
   
72
    stream.r = read_stdin;
71
    stream.r = read_stdin;
73
    stream.param = 0;
72
    stream.param = 0;
74
    return stream;
73
    return stream;
75
}
74
}
76
 
75
 
77
static stream_t open_stdout(void)
76
static stream_t open_stdout(void)
78
{
77
{
79
    stream_t stream;
78
    stream_t stream;
80
    int res;
79
    int res;
81
   
80
   
82
    if (console_phone < 0) {
81
    if (console_phone < 0) {
83
        while ((console_phone = ipc_connect_me_to(PHONE_NS, SERVICE_CONSOLE, 0)) < 0) {
82
        while ((console_phone = ipc_connect_me_to(PHONE_NS, SERVICE_CONSOLE, 0)) < 0) {
84
            volatile int a;
83
            volatile int a;
85
            for (a = 0; a < 1048576; a++);
84
            for (a = 0; a < 1048576; a++);
86
        }
85
        }
87
    }
86
    }
88
   
87
   
89
    stream.w = write_stdout;
88
    stream.w = write_stdout;
90
    stream.param = 0;
89
    stream.param = 0;
91
    return stream;
90
    return stream;
92
}
91
}
93
 
92
 
94
fd_t open(const char *fname, int flags)
93
fd_t open(const char *fname, int flags)
95
{
94
{
96
    int c = 0;
95
    int c = 0;
97
   
96
   
98
    while (((streams[c].w) || (streams[c].r)) && (c < FDS))
97
    while (((streams[c].w) || (streams[c].r)) && (c < FDS))
99
        c++;
98
        c++;
100
    if (c == FDS)
99
    if (c == FDS)
101
        return EMFILE;
100
        return EMFILE;
102
   
101
   
103
    if (!strcmp(fname, "stdin")) {
102
    if (!strcmp(fname, "stdin")) {
104
        streams[c] = open_stdin();
103
        streams[c] = open_stdin();
105
        return c;
104
        return c;
106
    }
105
    }
107
   
106
   
108
    if (!strcmp(fname, "stdout")) {
107
    if (!strcmp(fname, "stdout")) {
109
        //streams[c].w = write_stdout;
108
        //streams[c].w = write_stdout;
110
        //return c;
109
        //return c;
111
        streams[c] = open_stdout();
110
        streams[c] = open_stdout();
112
        return c;
111
        return c;
113
    }
112
    }
114
   
113
   
115
    if (!strcmp(fname, "stderr")) {
114
    if (!strcmp(fname, "stderr")) {
116
        streams[c].w = write_stderr;
115
        streams[c].w = write_stderr;
117
        return c;
116
        return c;
118
    }
117
    }
119
}
118
}
120
 
119
 
121
 
120
 
122
ssize_t write(int fd, const void *buf, size_t count)
121
ssize_t write(int fd, const void *buf, size_t count)
123
{
122
{
124
    if (fd < FDS)
123
    if (fd < FDS)
125
        return streams[fd].w(streams[fd].param, buf, count);
124
        return streams[fd].w(streams[fd].param, buf, count);
126
   
125
   
127
    return 0;
126
    return 0;
128
}
127
}
129
 
128