summaryrefslogtreecommitdiff
path: root/src/user/knob/format.c
blob: c5d24477c2e8e4aae9a57a71c790999a31febfad (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <knob/block.h>
#include <knob/panic.h>
#include <knob/heap.h>

#include <pland/syscall.h>
#include <pland/pcrt.h>

#include <stdarg.h>

#define FORMAT_BUF_INIT_SIZE 200
#define FORMAT_BUF_CHUNK_SIZE 50

#define BAD_SPEC "%%UNKNOWN FORMAT SPEC%%"
#define BAD_SPEC_LEN 23

static char *buf;
static uint32_t buf_s;
static char *buf_i;

static const char *const hextab = "0123456789abcdef";

static void ensure(uint32_t extra) {
  const uint32_t total_len = buf_i - buf + extra;
  if (total_len < buf_s)
    return;

  buf_s = (total_len / FORMAT_BUF_CHUNK_SIZE + 1) * FORMAT_BUF_CHUNK_SIZE;

  char *const new_buf = get_block(buf_s);
  if (!new_buf)
    PANIC("out of memory in knob format");
  blockcpy(new_buf, buf, buf_i - buf);
  free_block(buf);
  buf_i += new_buf - buf;
  buf = new_buf;
}

struct format_spec {
  uint32_t len;
  enum {
    UNKNOWN,
    CHAR,
    STRING,
    UNSIGNED_DECIMAL,
    HEXADECIMAL
    //TODO: signed decimal
  } kind;
};

static const char *get_format(const char *from, struct format_spec *format_out) {
  if (*from == 'n') {
    ++from;
    format_out->len = -1;
  }
  else {
    uint32_t len = 0;
    while ((*from >= '0') && (*from <= '9'))
      len = len * 10 + *(from++) - '0';
    format_out->len = len;
  }

  switch (*from) {
  case 'c':
    format_out->kind = CHAR;
    break;
  case 's':
    format_out->kind = STRING;
    break;
  case 'u':
    format_out->kind = UNSIGNED_DECIMAL;
    break;
  case 'h':
  case 'x':
    format_out->kind = HEXADECIMAL;
    break;
  default:
    format_out->kind = UNKNOWN;
    break;
  }

  return from + 1;
}

//char debug[] = "-- format_v: fmt = \"   \"...";

//allocates new memory
char *format_v(const char *fmt, va_list args) {
  buf = get_block(FORMAT_BUF_INIT_SIZE);
  if (!buf)
    PANIC("out of memory in knob format");
  buf_s = FORMAT_BUF_INIT_SIZE;
  buf_i = buf;

  while (*fmt) {
  //debug[20] = *fmt;
  //debug[21] = fmt[1];
  //debug[22] = fmt[2];
  //_system_log(debug);

    if (*fmt != '%') {
      ensure(1);
      *(buf_i++) = *(fmt++);
    }
    else if (fmt[1] == '%') {
      ensure(1);
      *(buf_i++) = '%';
      fmt += 2;
    }
    else {
      struct format_spec form;
      fmt = get_format(fmt + 1, &form);
      if (form.len == -1)
      //should passing zero still have the special meaning?
        form.len = va_arg(args, uint32_t);
      switch (form.kind) {
      case UNKNOWN:
        ensure(BAD_SPEC_LEN);
        blockcpy(buf_i, BAD_SPEC, BAD_SPEC_LEN);
        buf_i += BAD_SPEC_LEN;
        continue;

        uint32_t ch;
      case CHAR:
        ch = va_arg(args, uint32_t);
        ensure(1);
        *(buf_i++) = (char)ch;
        continue;

        const char *str;
      case STRING:
        str = va_arg(args, const char *);
        if (!form.len)
          form.len = strlen(str);
        ensure(form.len);
        blockcpy(buf_i, str, form.len);
        buf_i += form.len;
        continue;

        uint32_t k;
      case UNSIGNED_DECIMAL:
        k = va_arg(args, uint32_t);
        if (!form.len) {
          if (k >= 1000000000)
            form.len = 10;
          else {
            uint32_t n = 10;
            ++form.len;
            while (k >= n) {
              ++form.len;
              n *= 10;
            }
          }
        }
        ensure(form.len);
        const uint32_t len_backup = form.len;
        while (form.len--) {
          buf_i[form.len] = (k % 10) + '0';
          k /= 10;
        }
        buf_i += len_backup;
        continue;

      case HEXADECIMAL:
        k = va_arg(args, uint32_t);
        if (!form.len)
          form.len = 8;
        ensure(form.len);
        const uint32_t hlen_backup = form.len;
        while (form.len--) {
          buf_i[form.len] = hextab[k % 16];
          k >>= 4;
        }
        buf_i += hlen_backup;
        continue;
      }
    }
  }

  *buf_i = '\0';
  return buf;
}

//allocates new memory
char *format(const char *fmt, ...) {
  va_list args;
  va_start(args, fmt);
  char *const res = format_v(fmt, args);
  va_end(args);
  return res;
}

void syslogf_v(const char *fmt, va_list args) {
  char *const msg = format_v(fmt, args);
  _system_log(msg);
  free_block(msg);
}

void syslogf(const char *fmt, ...) {
  va_list args;
  va_start(args, fmt);
  syslogf_v(fmt, args);
  va_end(args);
}

//reads a unsigned decimal terminated by either null or whitespace
//returns length of string plus length of whitespace
//returns 0 on failure
uint32_t try_swtou(const char *from, uint32_t *i_out) {
  const char *const old_from = from;
  uint32_t v = 0;
  while (*from && (*from != '\n') && (*from != ' ')) {
    if ((*from < '0') || (*from > '9'))
      return 0;
    v = v * 10 + *(from++) - '0';
  }
  *i_out = v;
  while ((*from == '\n') || (*from == ' '))
    ++from;
  return from - old_from;
}

//reads a hexadecimal terminated by either null or whitespace
//returns length of string plus length of whitespace
//returns 0 on failure
uint32_t try_swtoh(const char *from, uint32_t *i_out) {
  const char *const old_from = from;
  uint32_t v = 0;
  while (*from && (*from != '\n') && (*from != ' ')) {
    if ((*from >= '0') && (*from <= '9'))
      v = v * 16 + *(from++) - '0';
    else if ((*from >= 'a') && (*from <= 'f'))
      v = v * 16 + *(from++) - 'a' + 10;
    else if ((*from >= 'A') && (*from <= 'F'))
      v = v * 16 + *(from++) - 'A' + 10;
    else
      return 0;
  }
  *i_out = v;
  while ((*from == '\n') || (*from == ' '))
    ++from;
  return from - old_from;
}