aboutsummaryrefslogtreecommitdiff
path: root/lib/stdio/printf.c
blob: 25cce40637ce9989c1ceef3e1ebd619a9bbd0754 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#include <devices/uart_16550.h>
#include <devices/vga.h>

static char buffer[3 * sizeof(int) + 2];

int
printf(const char *restrict format, ...)
{
  int written = 0;
  va_list params;
  va_start(params, format);

  int s = 0;
  int l = 0;
  for (int i = 0; format[i] != '\0'; ++i) {
    if (format[i] == '%') {
      written += uart_puts(COM1, &format[s], l);
      vga_puts(&format[s], l);
      s = i + 2;
      ++i;

      switch (format[i]) {
      case 's': {
        const char *arg = va_arg(params, const char *);
        written += uart_puts(COM1, arg, -1);
        vga_puts(arg, -1);
      } break;
      case 'c': {
        const int arg = va_arg(params, int);
        uart_write(COM1, arg);
        vga_putc(arg);
        ++written;
      } break;
      case 'd': {
        const char *arg = itoa(buffer, va_arg(params, int), 10);
        written += uart_puts(COM1, arg, -1);
        vga_puts(arg, -1);
      } break;
      case 'u': {
        const char *arg = utoa(buffer, va_arg(params, unsigned int), 10);
        written += uart_puts(COM1, arg, -1);
        vga_puts(arg, -1);
      } break;
      case 'x': {
        const char *arg = utoa(buffer, va_arg(params, unsigned int), 16);
        written += uart_puts(COM1, arg, -1);
        vga_puts(arg, -1);
      } break;
      }

      l = 0;
    }

    else
      ++l;
  }

  if (l > 0) {
    written += uart_puts(COM1, &format[s], l);
    vga_puts(&format[s], l);
  }

  va_end(params);
  // vga_update_cursor();
  return written;
}