PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
stdio.c
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# Copyright 2001-2009, ps2dev - http://www.ps2dev.org
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9*/
10
11#include "irx_imports.h"
12
13extern struct irx_export_table _exp_stdio;
14
15typedef struct stdio_prnt_context_
16{
17 s16 fd;
18 s16 current_length;
19 char tmp_buf[64];
21
22#ifdef _IOP
23IRX_ID("Stdio", 1, 1);
24#endif
25// Mostly based on the module from SCE SDK 1.3.4 with an addition from SDK 3.1.0.
26
27int _start(int argc, char *argv[])
28{
29 if (RegisterLibraryEntries(&_exp_stdio) != 0) {
30 return MODULE_NO_RESIDENT_END;
31 }
32 return MODULE_RESIDENT_END;
33}
34
35void stdio_prnt_callback(void *userdata, int c)
36{
37 size_t current_length;
38 char *tmp_buf;
39 stdio_prnt_context_t *context = (stdio_prnt_context_t *)userdata;
40
41 current_length = context->current_length;
42 tmp_buf = context->tmp_buf;
43
44 switch (c) {
45 case 512:
46 context->current_length = 0;
47 return;
48 case 513:
49 if (current_length == 0) {
50 return;
51 }
52 goto write_now;
53 case 10:
54 stdio_prnt_callback(context, '\r');
55 break;
56 }
57
58 tmp_buf[current_length] = c;
59 current_length += 1;
60 context->current_length = current_length;
61 if (current_length == 64) {
62 context->current_length = 0;
63 write_now:
64 io_write(context->fd, tmp_buf, current_length);
65 }
66}
67
68int fdprintf(int fd, const char *format, ...)
69{
71 va_list va;
72 int res;
73
74 va_start(va, format);
75 context.fd = fd;
76 context.current_length = 0;
77 res = prnt(&stdio_prnt_callback, &context, format, va);
78 va_end(va);
79 return res;
80}
81
82int vfdprintf(int fd, const char *format, va_list va)
83{
85
86 context.fd = fd;
87 context.current_length = 0;
88 return prnt(&stdio_prnt_callback, &context, format, va);
89}
90
91int printf(const char *format, ...)
92{
94 va_list va;
95 int res;
96
97 va_start(va, format);
98 context.fd = 1;
99 context.current_length = 0;
100 res = prnt(&stdio_prnt_callback, &context, format, va);
101 va_end(va);
102 return res;
103}
104
105static u32 tab_stop_padding = 0;
106
107int fdputc(int c, int fd)
108{
109 char buf[1];
110
111 buf[0] = c;
112 if ((char)c == '\t') {
113 io_write(fd, " ", 8 - (tab_stop_padding & 7));
114 tab_stop_padding = (tab_stop_padding & 0xFFFFFFF8) + 8;
115 } else if ((char)c == '\n') {
116 io_write(fd, "\r\n", 2);
117 tab_stop_padding = 0;
118 } else {
119 if (isprint(c) != 0) {
120 tab_stop_padding += 1;
121 }
122 io_write(fd, buf, 1);
123 }
124 return c;
125}
126
127int putchar(int c)
128{
129 return fdputc(c, 1);
130}
131
132int fdputs(const char *s, int fd)
133{
134 const char *s_s;
135
136 s_s = s;
137 if (s == NULL) {
138 s_s = "<NULL>";
139 }
140 while (*s_s != '\x00') {
141 fdputc(*s_s, fd);
142 s_s += 1;
143 }
144 return 0;
145}
146
147int puts(const char *s)
148{
149 return fdputs(s, 1);
150}
151
152int fdgetc(int fd)
153{
154 char buf[1];
155
156 io_read(fd, buf, 1);
157 return (u8)(buf[0]);
158}
159
160int getchar(void)
161{
162 return fdgetc(0);
163}
164
165char *fdgets(char *buf, int fd)
166{
167 char *v4;
168 char *v5;
169 char v6;
170 int v7;
171 char v8;
172 int v9;
173 int v10;
174 int v11;
175 int v13;
176 int v14;
177
178 v4 = buf;
179 v5 = buf + 125;
180 for (;;) {
181 v6 = fdgetc(fd);
182 if (v6 == '\n') {
183 break;
184 }
185 if (v6 < '\v') {
186 if (v6 != '\b') {
187 if (v6 == '\t') {
188 v6 = ' ';
189 v7 = ' ';
190 } else {
191 v7 = v6;
192 }
193 goto LABEL_31;
194 }
195 goto LABEL_21;
196 }
197 if (v6 == '\x16') {
198 v8 = fdgetc(fd);
199 v9 = fd;
200 if (v4 >= v5) {
201 if (!fd) {
202 v9 = 1;
203 }
204 v10 = 7;
205 goto LABEL_29;
206 }
207 *v4++ = v8;
208 v10 = v8;
209 if (!fd) {
210 v9 = 1;
211 }
212 LABEL_29:
213 fdputc(v10, v9);
214 } else {
215 if (v6 < '\x17') {
216 if (v6 == '\r') {
217 break;
218 }
219 v7 = v6;
220 goto LABEL_31;
221 }
222 if (v6 != '\x7F') {
223 v7 = v6;
224 LABEL_31:
225 if (isprint(v7) == 0) {
226 v9 = fd;
227 LABEL_37:
228 if (!v9) {
229 v9 = 1;
230 }
231 v10 = 7;
232 goto LABEL_29;
233 }
234 v9 = fd;
235 if (v4 >= v5) {
236 goto LABEL_37;
237 }
238 *v4++ = v6;
239 if (!fd) {
240 v9 = 1;
241 }
242 v10 = v7;
243 goto LABEL_29;
244 }
245 LABEL_21:
246 if (buf < v4) {
247 --v4;
248 v13 = fd;
249 if (!fd) {
250 v13 = 1;
251 }
252 fdputc('\b', v13);
253 v14 = fd;
254 if (!fd) {
255 v14 = 1;
256 }
257 fdputc(' ', v14);
258 v9 = fd;
259 if (!fd) {
260 v9 = 1;
261 }
262 v10 = 8;
263 goto LABEL_29;
264 }
265 }
266 }
267 v11 = fd;
268 if (!fd) {
269 v11 = 1;
270 }
271 fdputc('\n', v11);
272 *v4 = 0;
273 return buf;
274}
275
276char *gets(char *s)
277{
278 return fdgets(s, 0);
279}