:: limine / test / e9print.c 1.8 KB raw

1
#include <e9print.h>
2
#include <stddef.h>
3
#include <stdint.h>
4
5
#if defined (_LIMINE_PROTO)
6
#include <flanterm.h>
7
extern struct flanterm_context *ft_ctx;
8
#endif
9
10
static const char CONVERSION_TABLE[] = "0123456789abcdef";
11
12
void e9_putc(char c) {
13
#if defined (__x86_64__) || defined (__i386__)
14
    __asm__ __volatile__ ("outb %0, %1" :: "a" (c), "Nd" (0xe9) : "memory");
15
#endif
16
#if defined (_LIMINE_PROTO)
17
    if (ft_ctx != NULL) {
18
        if (c == '\n') {
19
            flanterm_write(ft_ctx, "\r", 1);
20
        }
21
        flanterm_write(ft_ctx, &c, 1);
22
    }
23
#endif
24
}
25
26
void e9_print(const char *msg) {
27
    for (size_t i = 0; msg[i]; i++) {
28
        e9_putc(msg[i]);
29
    }
30
}
31
32
void e9_puts(const char *msg) {
33
    e9_print(msg);
34
    e9_putc('\n');
35
}
36
37
static void e9_printhex(size_t num) {
38
    int i;
39
    char buf[17];
40
41
    if (!num) {
42
        e9_print("0x0");
43
        return;
44
    }
45
46
    buf[16] = 0;
47
48
    for (i = 15; num; i--) {
49
        buf[i] = CONVERSION_TABLE[num % 16];
50
        num /= 16;
51
    }
52
53
    i++;
54
    e9_print("0x");
55
    e9_print(&buf[i]);
56
}
57
58
static void e9_printdec(size_t num) {
59
    int i;
60
    char buf[21] = {0};
61
62
    if (!num) {
63
        e9_putc('0');
64
        return;
65
    }
66
67
    for (i = 19; num; i--) {
68
        buf[i] = (num % 10) + 0x30;
69
        num = num / 10;
70
    }
71
72
    i++;
73
    e9_print(buf + i);
74
}
75
76
void e9_printf(const char *format, ...) {
77
    va_list argp;
78
    va_start(argp, format);
79
80
    while (*format != '\0') {
81
        if (*format == '%') {
82
            format++;
83
            if (*format == 'x') {
84
                e9_printhex(va_arg(argp, size_t));
85
            } else if (*format == 'd') {
86
                e9_printdec(va_arg(argp, size_t));
87
            } else if (*format == 's') {
88
                e9_print(va_arg(argp, char*));
89
            }
90
        } else {
91
            e9_putc(*format);
92
        }
93
        format++;
94
    }
95
96
    e9_putc('\n');
97
    va_end(argp);
98
}
tab: 248 wrap: offon