From 91501097299a046f75515c1feffb5ed1750ae128 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Tue, 2 Oct 2012 23:07:31 -0400 Subject: [PATCH] print: Add print_func() - output text w/ custom putc() --- include/print.h | 3 ++- kernel/print.c | 58 ++++++++++++++++++++++++++++++++----------------- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/include/print.h b/include/print.h index 1b4f84e..5d2efe0 100644 --- a/include/print.h +++ b/include/print.h @@ -21,7 +21,8 @@ #ifndef PRINT_H #define PRINT_H -int print(char *fmt, ...); void print_init(void (*putc)(char)); +int print(char *fmt, ...); +int print_func(void (putcf)(char), char *fmt, ...); #endif /* PRINT_H */ diff --git a/kernel/print.c b/kernel/print.c index 130b87d..630750e 100644 --- a/kernel/print.c +++ b/kernel/print.c @@ -26,19 +26,19 @@ void putc_initial(char c) { (void)c; } -void (*putc)(char) = &putc_initial; +void (*print_putc)(char) = &putc_initial; void print_init(void (*putcfn)(char)) { if (putcfn) - putc = putcfn; + print_putc = putcfn; } -void puts(const char *s) +void puts(void (*putc)(char), const char *s) { while (*s) putc (*s++); } -void puti(int i) +void puti(void (*putc)(char), int i) { unsigned int left; char buf[1 << (sizeof(int)*8) / 10]; @@ -64,10 +64,10 @@ void puti(int i) putc(*p); } -void putx(unsigned int i) { +void putx(void (*putc)(char), unsigned int i) { int j; - puts("0x"); + puts(putc, "0x"); for (j = 0; j < 8; j++) { unsigned int toprint = (i >> (4*(7-j))) & 0xf; @@ -78,21 +78,18 @@ void putx(unsigned int i) { } } -void putb(unsigned int i) { +void putb(void (*putc)(char), unsigned int i) { int j; - puts("0b"); + puts(putc, "0b"); for (j = 0; j < 32; j++) { putc((i>>(31-j)) & 1 ? '1' : '0'); } } -int print(char *fmt, ...) { +int _print(void (*putc)(char), char *fmt, va_list arg) { char *c; - va_list arg; - - va_start(arg, fmt); for (c = fmt; *c; c++) { if (*c == '%') { switch (*++c) { @@ -100,24 +97,24 @@ int print(char *fmt, ...) { putc('%'); break; case 's': - puts(va_arg(arg, char *)); + puts(putc, va_arg(arg, char *)); break; case 'c': putc(va_arg(arg, unsigned int)); break; case 'd': - puti(va_arg(arg, int)); + puti(putc, va_arg(arg, int)); break; case 'x': - putx(va_arg(arg, unsigned int)); + putx(putc, va_arg(arg, unsigned int)); break; case 'b': - putb(va_arg(arg, unsigned int)); + putb(putc, va_arg(arg, unsigned int)); break; default: - puts("\nError: print(): Invalid formatting character: '"); + puts(putc, "\nError: print(): Invalid formatting character: '"); putc(*c); - puts("'\n"); + puts(putc, "'\n"); return -1; } } else { @@ -126,7 +123,28 @@ int print(char *fmt, ...) { putc('\r'); } } - va_end(arg); - return 0; } + +int print(char *fmt, ...) { + int ret; + va_list arg; + + va_start(arg, fmt); + ret = _print(print_putc, fmt, arg); + va_end(arg); + + return ret; +} + + +int print_func(void (*putc)(char), char *fmt, ...) { + int ret; + va_list arg; + + va_start(arg, fmt); + ret = _print(putc, fmt, arg); + va_end(arg); + + return ret; +}