about summary refs log tree commit diff stats
path: root/tools/iso/kernel.soso/debugprint.c
blob: 186bc89f8d00da2363553d5ef843f175b84edba4 (plain) (blame)
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
71
72
73
74
75
76
77
78
79
#include "debugprint.h"
#include "common.h"
#include "fs.h"

static File* gFile = NULL;

void Debug_initialize(const char* fileName) {
    FileSystemNode* node = getFileSystemNode(fileName);

    gFile = open_fs(node, 0);
}

void Debug_PrintF(const char *format, ...) {
    char **arg = (char **) &format;
    char c;
    char buf[20];
    char buffer[512];

    int bufferIndex = 0;

    //arg++;
    __builtin_va_list vl;
    __builtin_va_start(vl, format);

    while ((c = *format++) != 0) {
        if (bufferIndex > 510) {
            break;
        }

        if (c != '%')
          buffer[bufferIndex++] = c;
        else {
            char *p;

            c = *format++;
            switch (c) {
              case 'x':
                 buf[0] = '0';
                 buf[1] = 'x';
                 //itoa (buf + 2, c, *((int *) arg++));
                 itoa (buf + 2, c, __builtin_va_arg(vl, int));
                 p = buf;
                 goto string;
                 break;
              case 'd':
              case 'u':
                //itoa (buf, c, *((int *) arg++));
                itoa (buf, c, __builtin_va_arg(vl, int));
                p = buf;
                goto string;
                break;

              case 's':
                //p = *arg++;
                p = __builtin_va_arg(vl, char*);
                if (! p)
                  p = "(null)";

              string:
                while (*p)
                  buffer[bufferIndex++] = (*p++);
                break;

              default:
                //buffer[bufferIndex++] = (*((int *) arg++));
                buffer[bufferIndex++] = __builtin_va_arg(vl, int);
                break;
              }
          }
      }

    buffer[bufferIndex] = '\0';

    if (gFile) {
        write_fs(gFile, strlen(buffer), (uint8*)buffer);
    }

    __builtin_va_end(vl);
}