summaryrefslogtreecommitdiff
path: root/src/libc/stdio.c
blob: e0b87a84f590a13655c6456e3db1b0a1d992770a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "stdio.h"
#include <stddef.h>
#include "stdlib.h"
#include "string.h"

#ifdef OS_ABAOS	
#include "kernel.h"
#endif

#ifdef OS_ABAOS
console_t *stdio_console = NULL;
#endif

static void print_string( const char *s );
static void print_char( const char c );
static void print_newline( void );

#ifdef OS_ABAOS
static void print_string( const char *s )
{
	if( stdio_console == NULL ) {
		kernel_panic( "stdio_console not set!" );
	}

	console_put_string( stdio_console, s );
}

static void print_char( const char c )
{
	if( stdio_console == NULL ) {
		kernel_panic( "stdio_console not set!" );
	}

	console_put_char( stdio_console, c );
}

static void print_newline( void )
{
	if( stdio_console == NULL ) {
		kernel_panic( "stdio_console not set!" );
	}

	console_put_newline( stdio_console );
}
#endif

#ifdef OS_LINUX
static void print_string( const char *s )
{
	syscall3( __NR_write, STDOUT_FILENO, (long)s, strlen( s ) );
}

static void print_char( const char c )
{
	char s[2]; s[0] = c;
	syscall3( __NR_write, STDOUT_FILENO, (long)s, 1 );
}

static void print_newline( void )
{
	char s[2]; s[0] = '\n';
	syscall3( __NR_write, STDOUT_FILENO, (long)s, 1 );
}
#endif

int puts( const char *s )
{
	print_string( s );
	print_newline( );

	return 1;
}

int printf( const char *format, ... )
{
	va_list args;
	
	va_start( args, format );
	int res = vprintf( format, args );
	va_end( args );
	
	return res;
}

int vprintf( const char *format, va_list args )
{
	const char *s = format;
	int n = 0;
	
	while( *s != '\0' ) {
		switch( *s ) {
			case '\n':
				print_newline( );
				n++;
				break;
			
			case '%':
				s++;
				if( *s == '\0' ) {
					print_string( "<truncated % found at end of format string>" );
					print_newline( );
					return -1;
				}
				
				switch( *s ) {
					case '%':
						print_char( '%' );
						break;
						
					case 'X': {
						char buf[19];
						itoa( va_arg( args, int ), (char *)buf, 16 );
						print_string( buf );
						n += strlen( buf );
						}
						break;
					
					case 'd': {
						char buf[19];
						itoa( va_arg( args, int ), (char *)buf, 10 );
						print_string( buf );
						n += strlen( buf );
						}
						break;

					case 'c':
						print_char( va_arg( args, int ) );
						break;
						
					case 's':
						print_string( va_arg( args, const char * ) );
						break;

					default:
						print_string( "<illegal format string %" );
						print_char( *s );
						print_string( ">" );
						print_newline( );
				}
				
				break;				
			
			default:
				print_char( *s );
				n++;
		}
		s++;
	}
	
	return n;
}

int snprintf( char *buf, size_t n, const char *format, ... )
{
	va_list args;
	
	va_start( args, format );
	int res = vsnprintf( buf, n, format, args );
	va_end( args );
	
	return res;
}

#define EMIT( x ) if( len < n ) { *buf = ( x ); buf++; *buf = '\0'; len++; }

#define APPEND_STRING( s, s2 ) \
	strlcat( s, s2, n - len ); \
	s += strlen( s2 ); \
	len += strlen( s2 ); \
	if( len < n ) { \
		*s = '\0'; \
	}

int vsnprintf( char *buf, size_t n, const char *format, va_list args )
{
	const char *s = format;
	int len = 0;

	*buf = '\0';	
	while( *s != '\0' && len < n ) {
		switch( *s ) {
			case '\n':
				EMIT( '\n' );
				break;
			
			case '%':
				s++;
				if( *s == '\0' ) {
					strlcpy( buf, "<truncated % found at end of format string>", n - len );
					strlcat( buf, "\n", n - len - 1 );
					return -1;
				}
				
				switch( *s ) {
					case '%':
						EMIT( '%' );
						break;
						
					case 'X': {
						char buf2[19];
						itoa( va_arg( args, int ), (char *)buf2, 16 );
						APPEND_STRING( buf, buf2 );
						}
						break;
					
					case 'd': {
						char buf2[19];
						itoa( va_arg( args, int ), (char *)buf2, 10 );
						APPEND_STRING( buf, buf2 );
						}
						break;

					case 'c': {
						char buf2[2];
						buf2[0] = (char)va_arg( args, int );
						buf2[1] = '\0';
						APPEND_STRING( buf, buf2 );
						}
						break;
						
					case 's': {
						const char *s = va_arg( args, const char * );
						APPEND_STRING( buf, s );
						}
						break;

					default: {
						APPEND_STRING( buf, "<illegal format string %" );
						char buf2[2];
						buf2[0] = (char)( *s );
						buf2[1] = '\0';
						APPEND_STRING( buf, buf2 );
						buf2[0] = '\n';
						APPEND_STRING( buf, buf2 );
						}
				}
				
				break;				
			
			default: {
				char buf2[2];
				buf2[0] = (char)( *s );
				buf2[1] = '\0';
				APPEND_STRING( buf, buf2 );
				}
		}
		s++;
	}
	
	return len;
}


#ifdef OS_ABAOS
void __stdio_set_console( console_t *console )
{
	stdio_console = console;
}
#endif