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
RevLine 
[afa6e74]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>
[79460ae]10#include <console.h>
[afa6e74]11
12#define FDS 32
13
[04552a80]14typedef struct stream_t {
[afa6e74]15 pwritefn_t w;
16 preadfn_t r;
17 void * param;
[04552a80]18} stream_t;
[afa6e74]19
[79460ae]20int console_phone = -1;
[afa6e74]21
[04552a80]22stream_t streams[FDS] = {{0, 0, 0}};
[afa6e74]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
[79460ae]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)
[afa6e74]38{
39 ipcarg_t r0,r1;
[79460ae]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);
[afa6e74]44}
[79460ae]45static ssize_t write_stdout(void *param, const void *buf, size_t count)
[afa6e74]46{
47 int i;
[79460ae]48 ipcarg_t r0,r1;
49
[04552a80]50 for (i = 0; i < count; i++)
[79460ae]51 ipc_call_sync_2(console_phone, CONSOLE_PUTCHAR, 0, ((const char *)buf)[i], &r0, &r1);
[afa6e74]52
53 return count;
54 //return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
55}
56
57
58
[79460ae]59static stream_t open_stdin(void)
[afa6e74]60{
61 stream_t stream;
62 int phoneid;
63 int res;
64
[79460ae]65 if (console_phone < 0) {
66 while ((console_phone = ipc_connect_me_to(PHONE_NS, SERVICE_CONSOLE, 0)) < 0) {
[44c6d88d]67 usleep(10000);
[79460ae]68 }
[afa6e74]69 }
70
[79460ae]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;
[afa6e74]80
[79460ae]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 }
[afa6e74]87
[79460ae]88 stream.w = write_stdout;
89 stream.param = 0;
[afa6e74]90 return stream;
91}
92
[04552a80]93fd_t open(const char *fname, int flags)
[afa6e74]94{
[04552a80]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")) {
[79460ae]103 streams[c] = open_stdin();
[afa6e74]104 return c;
105 }
106
[04552a80]107 if (!strcmp(fname, "stdout")) {
108 //streams[c].w = write_stdout;
[afa6e74]109 //return c;
[79460ae]110 streams[c] = open_stdout();
[afa6e74]111 return c;
112 }
113
[04552a80]114 if (!strcmp(fname, "stderr")) {
115 streams[c].w = write_stderr;
[afa6e74]116 return c;
117 }
118}
119
120
[04552a80]121ssize_t write(int fd, const void *buf, size_t count)
[afa6e74]122{
[04552a80]123 if (fd < FDS)
124 return streams[fd].w(streams[fd].param, buf, count);
125
[afa6e74]126 return 0;
127}
Note: See TracBrowser for help on using the repository browser.