toaruos/kernel/core/kprintf.c

385 lines
8.1 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>
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
*/
static void
parse_num(
unsigned int value,
unsigned int base,
char * buf,
int * ptr
2011-01-17 19:24:55 +03:00
) {
unsigned int n = value / base;
int r = value % base;
if (r < 0) {
r += base;
--n;
}
if (value >= base) {
parse_num(n, base, buf, ptr);
2011-01-17 19:24:55 +03:00
}
buf[*ptr] = (r+'0');
*ptr = *ptr + 1;
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
*/
static void
parse_hex(
unsigned int value,
char * buf,
int * ptr
2011-01-17 19:24:55 +03:00
) {
int i = 8;
while (i-- > 0) {
buf[*ptr] = "0123456789abcdef"[(value>>(i*4))&0xF];
*ptr = *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;
2011-01-17 19:24:55 +03:00
for ( ; 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;
}
/* fmt[i] == '%' */
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 */
parse_hex((unsigned long)va_arg(args, unsigned long), buf, &ptr);
2011-01-17 19:24:55 +03:00
break;
2011-04-15 06:02:44 +04:00
case 'd': /* Decimal number */
parse_num((unsigned long)va_arg(args, unsigned long), 10, 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;
}
/**
* (Kernel) Print a formatted string.
* %s, %c, %x, %d, %%
*
* @param fmt Formatted string to print
* @param ... Additional arguments to format
*/
void
kprintf(
const char *fmt,
...
) {
char buf[1024] = {-1};
va_list args;
va_start(args, fmt);
vasprintf(buf, fmt, args);
2011-04-15 06:02:44 +04:00
/* We're done with our arguments */
va_end(args);
2011-04-15 06:02:44 +04:00
/* Print that sucker */
if (ansi_ready) {
ansi_print(buf);
} else {
puts(buf);
}
2011-01-17 19:24:55 +03:00
}
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
}
2011-04-15 06:02:44 +04:00
/*
* gets() implementation for the kernel
*/
char * kgets_buffer = NULL;
int kgets_collected = 0;
int kgets_want = 0;
int kgets_newline = 0;
2011-02-08 03:22:03 +03:00
int kgets_cancel = 0;
2011-11-24 07:22:25 +04:00
kgets_redraw_t kgets_redraw_func = NULL;
kgets_tab_complete_t kgets_tab_complete_func = NULL;
2011-11-27 02:14:35 +04:00
kgets_special_t kgets_key_down = NULL;
kgets_special_t kgets_key_up = NULL;
kgets_special_t kgets_key_left = NULL;
kgets_special_t kgets_key_right = NULL;
volatile process_t * kgets_client = NULL;
2011-11-27 02:14:35 +04:00
2011-11-27 03:18:35 +04:00
int kgets_offset = 0;
2011-11-27 02:14:35 +04:00
uint8_t kgets_special = 0;
static void
kwrite(
char ch
) {
ansi_put(ch);
serial_send(ch);
}
2011-04-15 06:02:44 +04:00
/**
* (Internal) kgets keyboard handler
*/
void
kgets_handler(
char ch
) {
2011-11-27 02:14:35 +04:00
if (kgets_special == 1) {
if (ch == 91) {
kgets_special = 2;
return;
}
kgets_special = 0;
return;
}
if (kgets_special == 2) {
switch (ch) {
case 66:
if (kgets_key_down) {
kgets_key_down(kgets_buffer);
kgets_collected = strlen(kgets_buffer);
2011-11-27 03:18:35 +04:00
kgets_offset = kgets_collected;
2011-11-27 02:14:35 +04:00
}
break;
case 65:
if (kgets_key_up) {
kgets_key_up(kgets_buffer);
kgets_collected = strlen(kgets_buffer);
2011-11-27 03:18:35 +04:00
kgets_offset = kgets_collected;
2011-11-27 02:14:35 +04:00
}
break;
case 68:
if (kgets_key_left) {
kgets_key_left(kgets_buffer);
kgets_collected = strlen(kgets_buffer);
2011-11-27 03:18:35 +04:00
kgets_offset = kgets_collected;
} else {
if (kgets_offset > 0) {
kwrite(27);
kwrite(91);
kwrite(68);
kgets_offset--;
bochs_redraw_cursor();
}
2011-11-27 02:14:35 +04:00
}
break;
case 67:
if (kgets_key_right) {
kgets_key_right(kgets_buffer);
kgets_collected = strlen(kgets_buffer);
2011-11-27 03:18:35 +04:00
kgets_offset = kgets_collected;
} else {
if (kgets_offset < kgets_collected) {
kwrite(27);
kwrite(91);
kwrite(67);
kgets_offset++;
bochs_redraw_cursor();
}
2011-11-27 02:14:35 +04:00
}
break;
default:
kprintf("Unrecognized: %d\n", ch);
break;
}
kgets_special = 0;
return;
}
2011-02-08 00:19:12 +03:00
if (ch == 0x08) {
/* Backspace */
if (kgets_collected != 0) {
2011-11-27 03:18:35 +04:00
if (kgets_offset == 0) {
return;
} /* Clear the previous character */
kwrite(0x08);
kwrite(' ');
kwrite(0x08);
2011-11-27 03:18:35 +04:00
if (kgets_offset != kgets_collected) {
int remaining = kgets_collected - kgets_offset;
for (int i = 0; i < remaining; ++i) {
if (current_process != kgets_client) {
/* Switch page directories into the caller so we can write to its buffers */
switch_page_directory(kgets_client->thread.page_directory);
}
2011-11-27 03:18:35 +04:00
kwrite(kgets_buffer[kgets_offset + i]);
kgets_buffer[kgets_offset + i - 1] = kgets_buffer[kgets_offset + i];
}
kwrite(' ');
for (int i = 0; i < remaining + 1; ++i) {
kwrite(27);
kwrite(91);
kwrite(68);
}
kgets_offset--;
kgets_collected--;
bochs_redraw_cursor();
} else {
/* Erase the end of the buffer */
if (current_process != kgets_client) {
/* Switch page directories into the caller so we can write to its buffers */
switch_page_directory(kgets_client->thread.page_directory);
}
2011-11-27 03:18:35 +04:00
kgets_buffer[--kgets_collected] = '\0';
kgets_offset--;
}
2011-02-08 00:19:12 +03:00
}
return;
2011-10-21 22:45:06 +04:00
} else if (ch == '\x0c') {
2011-11-27 03:18:35 +04:00
kprintf("\033[H\033[2J");
2011-11-24 07:22:25 +04:00
if (kgets_redraw_func) {
kgets_redraw_func();
}
kgets_redraw_buffer();
return;
} else if (ch == '\t' && kgets_tab_complete_func) {
if (current_process != kgets_client) {
/* Switch page directories into the caller so we can write to its buffers */
switch_page_directory(kgets_client->thread.page_directory);
}
kgets_tab_complete_func(kgets_buffer);
kgets_collected = strlen(kgets_buffer);
2011-10-21 22:45:06 +04:00
return;
2011-11-27 02:14:35 +04:00
} else if (ch == 27) {
kgets_special = 1;
return;
2011-02-08 00:19:12 +03:00
} else if (ch == '\n') {
2011-04-15 06:02:44 +04:00
/* Newline finishes off the kgets() */
2011-11-27 03:18:35 +04:00
while (kgets_offset < kgets_collected) {
kwrite(27);
kwrite(91);
kwrite(67);
kgets_offset++;
}
kwrite('\n');
2011-03-29 05:43:06 +04:00
kgets_newline = 1;
return;
2011-04-19 03:12:37 +04:00
} /* Add this character to the buffer. */
2011-11-27 03:18:35 +04:00
if (kgets_offset != kgets_collected) {
IRQ_OFF;
if (current_process != kgets_client) {
/* Switch page directories into the caller so we can write to its buffers */
switch_page_directory(kgets_client->thread.page_directory);
}
2011-11-27 03:18:35 +04:00
for (int i = kgets_collected; i > kgets_offset; --i) {
kgets_buffer[i] = kgets_buffer[i-1];
}
if (kgets_collected < kgets_want) {
kgets_buffer[kgets_offset] = ch;
kgets_buffer[++kgets_collected] = '\0';
kgets_offset++;
}
for (int i = kgets_offset - 1; i < kgets_collected; ++i) {
kwrite(kgets_buffer[i]);
}
for (int i = kgets_offset; i < kgets_collected; ++i) {
kwrite(27);
kwrite(91);
kwrite(68);
}
bochs_redraw_cursor();
IRQ_ON;
2011-11-27 03:18:35 +04:00
} else {
kwrite(ch);
if (kgets_collected < kgets_want) {
IRQ_OFF;
if (current_process != kgets_client) {
/* Switch page directories into the caller so we can write to its buffers */
switch_page_directory(kgets_client->thread.page_directory);
}
2011-11-27 03:18:35 +04:00
kgets_buffer[kgets_collected] = ch;
kgets_buffer[++kgets_collected] = '\0';
kgets_offset++;
IRQ_ON;
2011-11-27 03:18:35 +04:00
}
}
}
void kgets_redraw_buffer() {
kprintf(kgets_buffer);
2011-11-27 03:18:35 +04:00
for (int i = kgets_offset; i < kgets_collected; ++i) {
kwrite(27);
kwrite(91);
kwrite(68);
}
}
2011-04-15 06:02:44 +04:00
/**
* Synchronously get a string from the keyboard.
*
* @param buffer Where to put it
* @param size Maximum size of the string to receive.
*/
int
kgets(
char *buffer,
int size
) {
2011-04-15 06:02:44 +04:00
/* Reset the buffer */
kgets_buffer = buffer;
kgets_collected = 0;
kgets_want = size;
kgets_newline = 0;
2011-10-23 04:17:35 +04:00
kgets_buffer[0] = '\0';
2011-11-27 03:18:35 +04:00
kgets_offset = 0;
2011-04-15 06:02:44 +04:00
/* Assign the keyboard handler */
kgets_client = current_process;
keyboard_buffer_handler = kgets_handler;
while ((kgets_collected < size) && (!kgets_newline)) {
2011-04-15 06:02:44 +04:00
/* Wait until the buffer is ready */
PAUSE;
}
2011-04-15 06:02:44 +04:00
/* Fix any missing nulls */
buffer[kgets_collected] = '\0';
2011-04-15 06:02:44 +04:00
/* Disable the buffer */
keyboard_buffer_handler = NULL;
2011-11-24 07:22:25 +04:00
kgets_redraw_func = NULL;
kgets_tab_complete_func = NULL;
2011-04-15 06:02:44 +04:00
/* Return the string */
return kgets_collected;
}
2011-04-15 06:02:44 +04:00