toaruos/kernel/misc/kprintf.c

174 lines
3.3 KiB
C
Raw Normal View History

/* vim: tabstop=4 shiftwidth=4 noexpandtab
*
2011-04-15 06:02:44 +04:00
* Kernel printf implementation
*
* Simple, painfully lacking, implementation of printf(),
* for the kernel of all things.
*/
2011-01-17 19:24:55 +03:00
#include <system.h>
#include <process.h>
2011-12-15 03:47:30 +04:00
#include <va_list.h>
#include <fs.h>
2011-01-17 19:24:55 +03:00
/*
2011-04-15 06:02:44 +04:00
* Integer to string
2011-01-17 19:24:55 +03:00
*/
2012-12-08 07:06:43 +04:00
static void print_dec(unsigned int value, unsigned int width, char * buf, int * ptr ) {
unsigned int n_width = 1;
unsigned int i = 9;
while (value > i && i < UINT32_MAX) {
n_width += 1;
i *= 10;
i += 9;
2011-01-17 19:24:55 +03:00
}
2012-12-08 07:06:43 +04:00
int printed = 0;
while (n_width + printed < width) {
buf[*ptr] = '0';
*ptr += 1;
printed += 1;
}
i = n_width;
while (i > 0) {
unsigned int n = value / 10;
int r = value % 10;
buf[*ptr + i - 1] = r + '0';
i--;
value = n;
2011-01-17 19:24:55 +03:00
}
2012-12-08 07:06:43 +04:00
*ptr += n_width;
2011-01-17 19:24:55 +03:00
}
/*
2011-04-15 06:02:44 +04:00
* Hexadecimal to string
2011-01-17 19:24:55 +03:00
*/
2012-12-08 07:06:43 +04:00
static void print_hex(unsigned int value, unsigned int width, char * buf, int * ptr) {
int i = width;
if (i == 0) i = 8;
unsigned int n_width = 1;
unsigned int j = 0x0F;
while (value > j && j < UINT32_MAX) {
n_width += 1;
j *= 0x10;
j += 0x0F;
}
2012-12-08 07:29:54 +04:00
while (i > (int)n_width) {
2012-12-08 07:06:43 +04:00
buf[*ptr] = '0';
*ptr += 1;
i--;
}
i = (int)n_width;
2011-01-17 19:24:55 +03:00
while (i-- > 0) {
buf[*ptr] = "0123456789abcdef"[(value>>(i*4))&0xF];
2012-12-08 07:06:43 +04:00
*ptr += + 1;
2011-01-17 19:24:55 +03:00
}
}
2011-12-15 05:19:51 +04:00
/*
* vasprintf()
2011-01-17 19:24:55 +03:00
*/
2011-12-15 05:19:51 +04:00
size_t
vasprintf(char * buf, const char *fmt, va_list args) {
2011-01-17 19:24:55 +03:00
int i = 0;
char *s;
2011-12-15 05:19:51 +04:00
int ptr = 0;
int len = strlen(fmt);
for ( ; i < len && fmt[i]; ++i) {
2011-04-15 06:02:44 +04:00
if (fmt[i] != '%') {
2011-01-17 19:24:55 +03:00
buf[ptr++] = fmt[i];
continue;
}
2012-12-08 07:06:43 +04:00
++i;
unsigned int arg_width = 0;
while (fmt[i] >= '0' && fmt[i] <= '9') {
arg_width *= 10;
arg_width += fmt[i] - '0';
++i;
}
2011-01-17 19:24:55 +03:00
/* fmt[i] == '%' */
2012-12-08 07:06:43 +04:00
switch (fmt[i]) {
2011-04-15 06:02:44 +04:00
case 's': /* String pointer -> String */
s = (char *)va_arg(args, char *);
2011-01-17 19:24:55 +03:00
while (*s) {
buf[ptr++] = *s++;
}
break;
2011-04-15 06:02:44 +04:00
case 'c': /* Single character */
buf[ptr++] = (char)va_arg(args, int);
2011-01-17 19:24:55 +03:00
break;
2011-04-15 06:02:44 +04:00
case 'x': /* Hexadecimal number */
2012-12-08 07:06:43 +04:00
print_hex((unsigned long)va_arg(args, unsigned long), arg_width, buf, &ptr);
2011-01-17 19:24:55 +03:00
break;
2011-04-15 06:02:44 +04:00
case 'd': /* Decimal number */
2012-12-08 07:06:43 +04:00
print_dec((unsigned long)va_arg(args, unsigned long), arg_width, buf, &ptr);
2011-01-17 19:24:55 +03:00
break;
2011-04-15 06:02:44 +04:00
case '%': /* Escape */
2011-01-17 19:24:55 +03:00
buf[ptr++] = '%';
break;
2011-04-15 06:02:44 +04:00
default: /* Nothing at all, just dump it */
2011-01-17 19:24:55 +03:00
buf[ptr++] = fmt[i];
break;
}
}
2011-04-15 06:02:44 +04:00
/* Ensure the buffer ends in a null */
2011-01-17 19:24:55 +03:00
buf[ptr] = '\0';
2011-12-15 05:19:51 +04:00
return ptr;
}
void * kprint_to_file = NULL;
2014-03-16 10:56:10 +04:00
static unsigned short * textmemptr = (unsigned short *)0xB8000;
static void placech(unsigned char c, int x, int y, int attr) {
unsigned short *where;
unsigned att = attr << 8;
where = textmemptr + (y * 80 + x);
*where = c | att;
}
2011-12-15 05:19:51 +04:00
/**
* (Kernel) Print a formatted string.
* %s, %c, %x, %d, %%
*
* @param fmt Formatted string to print
* @param ... Additional arguments to format
*/
int
2011-12-15 05:19:51 +04:00
kprintf(
const char *fmt,
...
) {
char buf[1024] = {-1};
va_list args;
va_start(args, fmt);
int out = vasprintf(buf, fmt, args);
2011-04-15 06:02:44 +04:00
/* We're done with our arguments */
va_end(args);
/* Registered output file */
if (kprint_to_file) {
fs_node_t * node = (fs_node_t *)kprint_to_file;
uint32_t out = write_fs(node, node->offset, strlen(buf), (uint8_t *)buf);
node->offset += out;
}
return out;
}
2011-11-18 01:55:59 +04:00
int
sprintf(
char * buf,
const char *fmt,
...
) {
va_list args;
va_start(args, fmt);
2011-12-15 05:19:51 +04:00
int out = vasprintf(buf, fmt, args);
va_end(args);
return out;
2011-11-18 01:55:59 +04:00
}