2024-09-19 12:00:18 -04:00
|
|
|
#include "base.h"
|
2024-09-07 08:40:46 -04:00
|
|
|
#include "ctype.h"
|
|
|
|
#include "terminal.h"
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
2024-09-19 12:00:18 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
int print_int_base(int32_t number, const char *prefix, const char *base);
|
2024-09-07 08:40:46 -04:00
|
|
|
|
2024-09-20 06:40:36 -04:00
|
|
|
static int get_level(const char *str)
|
|
|
|
{
|
|
|
|
if (!str || !isdigit(str[0]))
|
|
|
|
return 8;
|
|
|
|
return str[0] - '0';
|
|
|
|
}
|
|
|
|
|
2024-09-07 08:40:46 -04:00
|
|
|
static int print_flag(char flag, va_list *ap)
|
|
|
|
{
|
|
|
|
switch (flag) {
|
|
|
|
case '%':
|
|
|
|
return terminal_putchar('%');
|
|
|
|
case 'i':
|
2024-09-19 12:00:18 -04:00
|
|
|
return print_int_base(va_arg(*ap, int), NULL, BASE_DECA);
|
2024-09-07 08:40:46 -04:00
|
|
|
case 'd':
|
2024-09-19 12:00:18 -04:00
|
|
|
return print_int_base(va_arg(*ap, int), NULL, BASE_DECA);
|
2024-09-07 08:40:46 -04:00
|
|
|
case 'c':
|
|
|
|
return terminal_putchar(va_arg(*ap, int));
|
|
|
|
case 's':
|
|
|
|
return terminal_writestring(va_arg(*ap, char *));
|
2024-09-19 12:00:18 -04:00
|
|
|
case 'p':
|
|
|
|
return print_int_base(va_arg(*ap, int), "0x", BASE_HEXA);
|
|
|
|
case 'x':
|
|
|
|
return print_int_base(va_arg(*ap, int), "0x", BASE_HEXA);
|
|
|
|
case 'u':
|
|
|
|
return print_int_base(va_arg(*ap, uint32_t), NULL, BASE_DECA);
|
2024-09-07 08:40:46 -04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-09-20 06:40:36 -04:00
|
|
|
int kvprintf(const char *restrict format, va_list ap)
|
2024-09-07 08:40:46 -04:00
|
|
|
{
|
|
|
|
const char *start = format;
|
|
|
|
int ret = 0;
|
|
|
|
|
2024-09-20 06:40:36 -04:00
|
|
|
int level = get_level(format);
|
|
|
|
set_color_level(level);
|
|
|
|
if (level != 8)
|
|
|
|
start++;
|
2024-09-07 08:40:46 -04:00
|
|
|
while (*start != '\0') {
|
|
|
|
if (*start == '%' && *(start + 1) != '\0') {
|
|
|
|
ret += print_flag(*(start + 1), &ap);
|
|
|
|
start++;
|
|
|
|
} else {
|
|
|
|
ret += terminal_putchar(*start);
|
|
|
|
}
|
|
|
|
start++;
|
|
|
|
}
|
2024-09-20 06:40:36 -04:00
|
|
|
set_color_level(8);
|
2024-09-07 23:03:50 -04:00
|
|
|
update_cursor();
|
2024-09-07 08:40:46 -04:00
|
|
|
return ret;
|
|
|
|
}
|