#include "stdio.h" #include #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( "" ); 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( "" ); print_newline( ); } break; default: print_char( *s ); n++; } s++; } return n; } #ifdef OS_ABAOS void __stdio_set_console( console_t *console ) { stdio_console = console; } #endif