source: mainline/libc/generic/io/stream.c@ 44c6d88d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 44c6d88d was 44c6d88d, checked in by Ondrej Palkovsky <ondrap@…>, 20 years ago

Added some additional functionality.

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