source: mainline/libc/generic/io/stream.c@ 04552a80

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 04552a80 was 04552a80, checked in by Martin Decky <martin@…>, 19 years ago

code cleanup (somebody should read the coding style guide)
remove DONT_OPEN_STDIO (this has to be done in a different way, ppc32 linker segfaults on initiating extern variable)
remove deprecated libipc stuff

  • Property mode set to 100644
File size: 2.2 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
11#define FDS 32
12
13typedef struct stream_t {
14 pwritefn_t w;
15 preadfn_t r;
16 void * param;
17} stream_t;
18
19
20typedef struct vfb_descriptor_t {
21 int phone;
22 int vfb;
23} vfb_descriptor_t;
24
25
26stream_t streams[FDS] = {{0, 0, 0}};
27
28/*
29ssize_t write_stdout(void *param, const void * buf, size_t count);
30ssize_t write_stdout(void *param, const void * buf, size_t count)
31{
32 return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
33}*/
34
35static void vfb_send_char(vfb_descriptor_t *d, char c)
36{
37 ipcarg_t r0,r1;
38 ipc_call_sync_2(d->phone, FB_PUTCHAR, d->vfb, c, &r0, &r1);
39}
40
41static ssize_t write_vfb(void *param, const void *buf, size_t count)
42{
43 int i;
44 for (i = 0; i < count; i++)
45 vfb_send_char((vfb_descriptor_t *) param, ((char *) buf)[i]);
46
47 return count;
48 //return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
49}
50
51
52static ssize_t write_stderr(void *param, const void *buf, size_t count)
53{
54 return count;
55 //return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
56}
57
58
59stream_t open_vfb(void)
60{
61 stream_t stream;
62 vfb_descriptor_t *vfb;
63 int phoneid;
64 int res;
65 ipcarg_t vfb_no;
66
67 while ((phoneid = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
68 volatile int a;
69
70 for (a = 0; a < 1048576; a++);
71 }
72
73 ipc_call_sync(phoneid, FB_GET_VFB, 0, &vfb_no);
74 vfb = malloc(sizeof(vfb_descriptor_t));
75
76 vfb->phone = phoneid;
77 vfb->vfb = vfb_no;
78
79 stream.w = write_vfb;
80 stream.param = vfb;
81 return stream;
82}
83
84
85fd_t open(const char *fname, int flags)
86{
87 int c = 0;
88
89 while (((streams[c].w) || (streams[c].r)) && (c < FDS))
90 c++;
91 if (c == FDS)
92 return EMFILE;
93
94 if (!strcmp(fname, "stdin")) {
95 streams[c].r = (preadfn_t)1;
96 return c;
97 }
98
99 if (!strcmp(fname, "stdout")) {
100 //streams[c].w = write_stdout;
101 //return c;
102 streams[c] = open_vfb();
103 return c;
104 }
105
106 if (!strcmp(fname, "stderr")) {
107 streams[c].w = write_stderr;
108 return c;
109 }
110}
111
112
113ssize_t write(int fd, const void *buf, size_t count)
114{
115 if (fd < FDS)
116 return streams[fd].w(streams[fd].param, buf, count);
117
118 return 0;
119}
Note: See TracBrowser for help on using the repository browser.