diff options
author | Patrick Williams <iawillia@us.ibm.com> | 2011-03-05 10:01:45 -0600 |
---|---|---|
committer | Patrick Williams <iawillia@us.ibm.com> | 2011-03-05 10:01:45 -0600 |
commit | 706243ac48cf646d503a3f1ec9e6a28c916694bd (patch) | |
tree | 5d583486a145a9646eccb9d3c4bce4dad45a2a84 /src/kernel/console.C | |
parent | 5c20d316d21e231daee6455f0a78d5940d59cf23 (diff) | |
download | talos-hostboot-706243ac48cf646d503a3f1ec9e6a28c916694bd.tar.gz talos-hostboot-706243ac48cf646d503a3f1ec9e6a28c916694bd.zip |
Merge of PowerHAL project up to commit:
dd45c30bd53d8e6c123165b83842d08117558a3c
Diffstat (limited to 'src/kernel/console.C')
-rw-r--r-- | src/kernel/console.C | 255 |
1 files changed, 7 insertions, 248 deletions
diff --git a/src/kernel/console.C b/src/kernel/console.C index 6cc2d1dac..6912b8450 100644 --- a/src/kernel/console.C +++ b/src/kernel/console.C @@ -1,5 +1,7 @@ #include <util/singleton.H> #include <kernel/console.H> +#include <util/sprintf.H> +#include <util/functor.H> #include <stdarg.h> char kernel_printk_buffer[Console::BUFFER_SIZE]; @@ -22,260 +24,17 @@ int Console::putc(int c) return c; } -class ConsoleTraits -{ - public: - enum trait { NONE, HEX, DEC, }; -}; - -template <typename _T, ConsoleTraits::trait _S = ConsoleTraits::NONE> -class ConsoleDisplay -{ - public: - static void display(Console& c, _T value) {}; -}; - -template <ConsoleTraits::trait _S> -class ConsoleDisplay<char*, _S> -{ - public: - static void display(Console&c, char* value) - { - while(*value != '\0') - { - c.putc(*value); - value++; - } - } -}; - -template <> -class ConsoleDisplay<char, ConsoleTraits::NONE> -{ - public: - static void display(Console&c, char value) - { - c.putc(value); - } -}; - -template <typename _T> -class ConsoleDisplay<_T, ConsoleTraits::DEC> -{ - public: - static void display(Console&c, _T value) - { - if (value == 0) - { - c.putc('0'); - } - else if (value < 0) - { - c.putc('-'); - value *= -1; - } - else - subdisplay(c, value); - } - - static void subdisplay(Console&c, _T value) - { - if (value != 0) - { - subdisplay(c, value / 10); - c.putc('0' + (value % 10)); - } - } -}; - -template<typename _T> -class ConsoleDisplay<_T, ConsoleTraits::HEX> -{ - public: - static void display(Console&c, _T value) - { - size_t length = sizeof(_T) * 2; - subdisplay(c, value, length); - } - - static void subdisplay(Console&c, _T value, size_t length) - { - if (length == 0) return; - subdisplay(c, value / 16, length-1); - char nibble = value % 16; - if (nibble >= 0x0a) - c.putc('A' + (nibble - 0x0a)); - else - c.putc('0' + nibble); - } -}; - void printk(const char* str, ...) { + using Util::mem_ptr_fun; + using Util::vasprintf; + va_list args; va_start(args, str); Console& console = Singleton<Console>::instance(); - - bool format = false; - int size = 0; - - while('\0' != *str) - { - if (('%' == *str) || (format)) - switch (*str) - { - case '%': - { - if (format) - { - ConsoleDisplay<char>::display(console, '%'); - format = false; - } - else - { - format = true; - size = 2; - } - break; - } - case 'c': - { - format = false; - ConsoleDisplay<char> - ::display(console, - (char)va_arg(args,int)); - break; - } - case 'h': - { - size--; - break; - } - case 'l': - { - size++; - break; - } - case 'z': // size_t or ssize_t - { - size = 4; - break; - } - case 'd': // decimal - { - format = false; - switch(size) - { - case 0: - ConsoleDisplay<char, ConsoleTraits::DEC> - ::display(console, - (char)va_arg(args,int)); - break; - - case 1: - ConsoleDisplay<short, ConsoleTraits::DEC> - ::display(console, - (short)va_arg(args,int)); - break; - - case 2: - ConsoleDisplay<int, ConsoleTraits::DEC> - ::display(console, - va_arg(args,int)); - break; - - case 3: - case 4: - ConsoleDisplay<long, ConsoleTraits::DEC> - ::display(console, - va_arg(args,long)); - break; - } - break; - } - case 'u': // unsigned decimal - { - format = false; - switch(size) - { - case 0: - ConsoleDisplay<unsigned char, ConsoleTraits::DEC> - ::display(console, - (unsigned char) - va_arg(args,unsigned int)); - break; - - case 1: - ConsoleDisplay<unsigned short, ConsoleTraits::DEC> - ::display(console, - (unsigned short) - va_arg(args,unsigned int)); - break; - - case 2: - ConsoleDisplay<unsigned int, ConsoleTraits::DEC> - ::display(console, - va_arg(args,unsigned int)); - break; - - case 3: - case 4: - ConsoleDisplay<unsigned long, ConsoleTraits::DEC> - ::display(console, - va_arg(args,unsigned long)); - break; - } - break; - } - case 'x': // unsigned hex - case 'X': - { - format = false; - switch(size) - { - case 0: - ConsoleDisplay<unsigned char, ConsoleTraits::HEX> - ::display(console, - (unsigned char) - va_arg(args,unsigned int)); - break; - - case 1: - ConsoleDisplay<unsigned short, ConsoleTraits::HEX> - ::display(console, - (unsigned short) - va_arg(args,unsigned int)); - break; - - case 2: - ConsoleDisplay<unsigned int, ConsoleTraits::HEX> - ::display(console, - va_arg(args,unsigned int)); - break; - - case 3: - case 4: - ConsoleDisplay<unsigned long, ConsoleTraits::HEX> - ::display(console, - va_arg(args,unsigned long)); - break; - } - break; - } - case 's': // string - { - format = false; - ConsoleDisplay<char*>::display(console, - (char*)va_arg(args,void*)); - break; - } - } - else - ConsoleDisplay<char>::display(console, *str); - - str++; - } + vasprintf(mem_ptr_fun(console, &Console::putc), + str, args); va_end(args); } |