-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathee_printf.c
More file actions
737 lines (624 loc) · 18.3 KB
/
ee_printf.c
File metadata and controls
737 lines (624 loc) · 18.3 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/*
* Copyright (c) 2015-2017 Contributors as noted in the AUTHORS file
*
* This file is part of Solo5, a unikernel base layer.
*
* Permission to use, copy, modify, and/or distribute this software
* for any purpose with or without fee is hereby granted, provided
* that the above copyright notice and this permission notice appear
* in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* This code is based on a file that contains the following:
*
* Copyright (C) 2002 Michael Ringgaard. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "kernel.h"
#undef HAS_FLOAT
#define ZEROPAD (1<<0)/* Pad with zero */
#define SIGN (1<<1)/* Unsigned/signed long */
#define PLUS (1<<2)/* Show plus */
#define SPACE (1<<3)/* Spacer */
#define LEFT (1<<4)/* Left justified */
#define HEX_PREP (1<<5)/* 0x */
#define UPPERCASE (1<<6)/* 'ABCDEF' */
#define is_digit(c) ((c) >= '0' && (c) <= '9')
struct outbuf {
int size;
char *ptr;
int num_printed;
};
static void fill_outbuf(struct outbuf *o, char c)
{
if (o->num_printed++ < o->size)
*o->ptr++ = c;
}
static char *lower_digits = "0123456789abcdefghijklmnopqrstuvwxyz";
static char *upper_digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static size_t strnlen(const char *s, size_t count);
static size_t strnlen(const char *s, size_t count)
{
const char *sc;
for (sc = s; *sc != '\0' && count--; ++sc)
;
return sc - s;
}
static int ee_skip_atoi(const char **s)
{
int i = 0;
while (is_digit(**s))
i = i*10 + *((*s)++) - '0';
return i;
}
static void out_number(struct outbuf *out, long num, int base, int size,
int precision, int type){
char c, sign, tmp[66];
char *dig = lower_digits;
int i;
if (type & UPPERCASE)
dig = upper_digits;
if (type & LEFT)
type &= ~ZEROPAD;
if (base < 2 || base > 36)
return;
c = (type & ZEROPAD) ? '0' : ' ';
sign = 0;
if (type & SIGN) {
if (num < 0) {
sign = '-';
num = -num;
size--;
} else if (type & PLUS) {
sign = '+';
size--;
} else if (type & SPACE) {
sign = ' ';
size--;
}
}
if (type & HEX_PREP) {
if (base == 16)
size -= 2;
else if (base == 8)
size--;
}
i = 0;
if (num == 0) {
tmp[i++] = '0';
} else {
while (num != 0) {
tmp[i++] = dig[((unsigned long) num) % (unsigned int) base];
num = ((unsigned long) num) / (unsigned int) base;
}
}
if (i > precision)
precision = i;
size -= precision;
if (!(type & (ZEROPAD | LEFT)))
while (size-- > 0)
fill_outbuf(out, ' ');
if (sign)
fill_outbuf(out, sign);
if (type & HEX_PREP) {
if (base == 8) {
fill_outbuf(out, '0');
} else if (base == 16) {
fill_outbuf(out, '0');
fill_outbuf(out, lower_digits[33]);
}
}
if (!(type & LEFT))
while (size-- > 0)
fill_outbuf(out, c);
while (i < precision--)
fill_outbuf(out, '0');
while (i-- > 0)
fill_outbuf(out, tmp[i]);
while (size-- > 0)
fill_outbuf(out, ' ');
}
static void out_eaddr(struct outbuf *out, unsigned char *addr, int size,
__attribute__((unused)) int precision, int type)
{
char tmp[24];
char *dig = lower_digits;
int i, len;
if (type & UPPERCASE)
dig = upper_digits;
len = 0;
for (i = 0; i < 6; i++) {
if (i != 0)
tmp[len++] = ':';
tmp[len++] = dig[addr[i] >> 4];
tmp[len++] = dig[addr[i] & 0x0F];
}
if (!(type & LEFT))
while (len < size--)
fill_outbuf(out, ' ');
for (i = 0; i < len; ++i)
fill_outbuf(out, tmp[i]);
while (len < size--)
fill_outbuf(out, ' ');
}
static void out_iaddr(struct outbuf *out, unsigned char *addr, int size,
__attribute__((unused)) int precision, int type)
{
char tmp[24];
int i, n, len;
len = 0;
for (i = 0; i < 4; i++) {
if (i != 0)
tmp[len++] = '.';
n = addr[i];
if (n == 0) {
tmp[len++] = lower_digits[0];
} else {
if (n >= 100) {
tmp[len++] = lower_digits[n / 100];
n = n % 100;
tmp[len++] = lower_digits[n / 10];
n = n % 10;
} else if (n >= 10) {
tmp[len++] = lower_digits[n / 10];
n = n % 10;
}
tmp[len++] = lower_digits[n];
}
}
if (!(type & LEFT))
while (len < size--)
fill_outbuf(out, ' ');
for (i = 0; i < len; ++i)
fill_outbuf(out, tmp[i]);
while (len < size--)
fill_outbuf(out, ' ');
}
#ifdef HAS_FLOAT
char *ecvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf);
char *fcvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf);
static void ee_bufcpy(char *d, char *s, int count);
void ee_bufcpy(char *pd, char *ps, int count)
{
char *pe = ps + count;
while (ps != pe)
*pd++ = *ps++;
}
static void parse_float(double value, char *buffer, char fmt, int precision)
{
int decpt, sign, exp, pos;
char *fdigits = NULL;
char cvtbuf[80];
int capexp = 0;
int magnitude;
if (fmt == 'G' || fmt == 'E') {
capexp = 1;
fmt += 'a' - 'A';
}
if (fmt == 'g') {
fdigits = ecvtbuf(value, precision, &decpt, &sign, cvtbuf);
magnitude = decpt - 1;
if (magnitude < -4 || magnitude > precision - 1) {
fmt = 'e';
precision -= 1;
} else {
fmt = 'f';
precision -= decpt;
}
}
if (fmt == 'e') {
fdigits = ecvtbuf(value, precision + 1, &decpt, &sign, cvtbuf);
if (sign)
*buffer++ = '-';
*buffer++ = *fdigits;
if (precision > 0)
*buffer++ = '.';
ee_bufcpy(buffer, fdigits + 1, precision);
buffer += precision;
*buffer++ = capexp ? 'E' : 'e';
if (decpt == 0) {
if (value == 0.0)
exp = 0;
else
exp = -1;
} else
exp = decpt - 1;
if (exp < 0) {
*buffer++ = '-';
exp = -exp;
} else
*buffer++ = '+';
buffer[2] = (exp % 10) + '0';
exp = exp / 10;
buffer[1] = (exp % 10) + '0';
exp = exp / 10;
buffer[0] = (exp % 10) + '0';
buffer += 3;
} else if (fmt == 'f') {
fdigits = fcvtbuf(value, precision, &decpt, &sign, cvtbuf);
if (sign)
*buffer++ = '-';
if (*fdigits) {
if (decpt <= 0) {
*buffer++ = '0';
*buffer++ = '.';
for (pos = 0; pos < -decpt; pos++)
*buffer++ = '0';
while (*fdigits)
*buffer++ = *fdigits++;
} else {
pos = 0;
while (*fdigits) {
if (pos++ == decpt)
*buffer++ = '.';
*buffer++ = *fdigits++;
}
}
} else {
*buffer++ = '0';
if (precision > 0) {
*buffer++ = '.';
for (pos = 0; pos < precision; pos++)
*buffer++ = '0';
}
}
}
*buffer = '\0';
}
static void decimal_point(char *buffer)
{
while (*buffer) {
if (*buffer == '.')
return;
if (*buffer == 'e' || *buffer == 'E')
break;
buffer++;
}
if (*buffer) {
int n = strnlen(buffer, 256);
while (n > 0) {
buffer[n + 1] = buffer[n];
n--;
}
*buffer = '.';
} else {
*buffer++ = '.';
*buffer = '\0';
}
}
static void cropzeros(char *buffer)
{
char *stop;
while (*buffer && *buffer != '.')
buffer++;
if (*buffer++) {
while (*buffer && *buffer != 'e' && *buffer != 'E')
buffer++;
stop = buffer--;
while (*buffer == '0')
buffer--;
if (*buffer == '.')
buffer--;
while (buffer != stop)
*++buffer = 0;
}
}
static char *flt(char *str, double num, int size, int precision,
char fmt, int flags)
{
char tmp[80];
char c, sign;
int n, i;
/* Left align means no zero padding */
if (flags & LEFT)
flags &= ~ZEROPAD;
/* Determine padding and sign char */
c = (flags & ZEROPAD) ? '0' : ' ';
sign = 0;
if (flags & SIGN) {
if (num < 0.0) {
sign = '-';
num = -num;
size--;
} else if (flags & PLUS) {
sign = '+';
size--;
} else if (flags & SPACE) {
sign = ' ';
size--;
}
}
/* Compute the precision value */
if (precision < 0)
precision = 6; /* Default precision: 6 */
/* Convert floating point number to text */
parse_float(num, tmp, fmt, precision);
if ((flags & HEX_PREP) && precision == 0)
decimal_point(tmp);
if (fmt == 'g' && !(flags & HEX_PREP))
cropzeros(tmp);
n = strnlen(tmp, 256);
/* Output number with alignment and padding */
size -= n;
if (!(flags & (ZEROPAD | LEFT)))
while (size-- > 0)
*str++ = ' ';
if (sign)
*str++ = sign;
if (!(flags & LEFT))
while (size-- > 0)
*str++ = c;
for (i = 0; i < n; i++)
*str++ = tmp[i];
while (size-- > 0)
*str++ = ' ';
return str;
}
static void out_flt(struct outbuf *out, double num, int size, int precision,
char fmt, int flags)
{
char tmp[80];
char c, sign;
int n, i;
/* Left align means no zero padding */
if (flags & LEFT)
flags &= ~ZEROPAD;
/* Determine padding and sign char */
c = (flags & ZEROPAD) ? '0' : ' ';
sign = 0;
if (flags & SIGN) {
if (num < 0.0) {
sign = '-';
num = -num;
size--;
} else if (flags & PLUS) {
sign = '+';
size--;
} else if (flags & SPACE) {
sign = ' ';
size--;
}
}
/* Compute the precision value */
if (precision < 0)
precision = 6; /* Default precision: 6 */
/* Convert floating point number to text */
parse_float(num, tmp, fmt, precision);
if ((flags & HEX_PREP) && precision == 0)
decimal_point(tmp);
if (fmt == 'g' && !(flags & HEX_PREP))
cropzeros(tmp);
n = strnlen(tmp, 256);
/* Output number with alignment and padding */
size -= n;
if (!(flags & (ZEROPAD | LEFT)))
while (size-- > 0)
fill_outbuf(out, ' ');
if (sign)
fill_outbuf(out, sign);
if (!(flags & LEFT))
while (size-- > 0)
fill_outbuf(out, c);
for (i = 0; i < n; i++)
fill_outbuf(out, tmp[i]);
while (size-- > 0)
fill_outbuf(out, ' ');
}
#endif
int vsnprintf(char *buf, size_t size,
const char *fmt, va_list args)
{
int len;
unsigned long num;
int i, base;
char *s;
int flags; /* Flags to number() */
int field_width; /* Width of output field */
int precision; /* Min. # of digits for integers, */
/* max number of chars for from string */
int qualifier; /* 'h', 'l', or 'L' for integer fields */
struct outbuf out;
out.size = size;
out.ptr = buf;
out.num_printed = 0;
for (; *fmt; fmt++) {
if (*fmt != '%') {
fill_outbuf(&out, *fmt);
continue;
}
/* Process flags */
flags = 0;
repeat:
fmt++; /* This also skips first '%' */
switch (*fmt) {
case '-':
flags |= LEFT;
goto repeat;
case '+':
flags |= PLUS;
goto repeat;
case ' ':
flags |= SPACE;
goto repeat;
case '#':
flags |= HEX_PREP;
goto repeat;
case '0':
flags |= ZEROPAD;
goto repeat;
}
/* Get field width */
field_width = -1;
if (is_digit(*fmt)) {
field_width = ee_skip_atoi(&fmt);
} else if (*fmt == '*') {
fmt++;
field_width = va_arg(args, int);
if (field_width < 0) {
field_width = -field_width;
flags |= LEFT;
}
}
/* Get the precision */
precision = -1;
if (*fmt == '.') {
++fmt;
if (is_digit(*fmt)) {
precision = ee_skip_atoi(&fmt);
} else if (*fmt == '*') {
++fmt;
precision = va_arg(args, int);
}
if (precision < 0)
precision = 0;
}
/* Get the conversion qualifier */
qualifier = -1;
if (*fmt == 'l' || *fmt == 'L') {
qualifier = *fmt;
fmt++;
}
/* Default base */
base = 10;
switch (*fmt) {
case 'c':
if (!(flags & LEFT))
while (--field_width > 0)
fill_outbuf(&out, ' ');
fill_outbuf(&out, (unsigned char) va_arg(args, int));
while (--field_width > 0)
fill_outbuf(&out, ' ');
continue;
case 's':
s = va_arg(args, char *);
if (!s)
s = "<NULL>";
len = strnlen(s, precision);
if (!(flags & LEFT))
while (len < field_width--)
fill_outbuf(&out, ' ');
for (i = 0; i < len; ++i)
fill_outbuf(&out, *s++);
while (len < field_width--)
fill_outbuf(&out, ' ');
continue;
case 'p':
if (field_width == -1) {
field_width = 2 * sizeof(void *);
flags |= ZEROPAD;
}
out_number(&out, (unsigned long) va_arg(args, void *),
16, field_width, precision, flags);
continue;
case 'A':
flags |= UPPERCASE;
case 'a':
if (qualifier == 'l')
out_eaddr(&out, va_arg(args, unsigned char *),
field_width, precision, flags);
else
out_iaddr(&out, va_arg(args, unsigned char *),
field_width, precision, flags);
continue;
/* Integer number formats - set up the flags and "break" */
case 'o':
base = 8;
break;
case 'X':
flags |= UPPERCASE;
case 'x':
base = 16;
break;
case 'd':
case 'i':
flags |= SIGN;
case 'u':
break;
#ifdef HAS_FLOAT
case 'f':
out_flt(&out, va_arg(args, double),
field_width, precision, *fmt, flags | SIGN);
continue;
#endif
default:
if (*fmt != '%')
fill_outbuf(&out, '%');
if (*fmt)
fill_outbuf(&out, *fmt);
else
--fmt;
continue;
}
if (qualifier == 'l')
num = va_arg(args, unsigned long);
else if (flags & SIGN)
num = va_arg(args, int);
else
num = va_arg(args, unsigned int);
out_number(&out, num, base, field_width, precision, flags);
}
fill_outbuf(&out, '\0');
return out.num_printed - 1; /* don't include terminating null byte */
}
#define PRINTF_BUF_LEN (4*80)
static int ee_vprintf(const char *fmt, va_list args)
{
/* XXX Replace ee_printf() with a version that does not need buffers */
char buf[PRINTF_BUF_LEN];
int n;
n = vsnprintf(buf, PRINTF_BUF_LEN, fmt, args);
assert(n < PRINTF_BUF_LEN);
return solo5_console_write(buf, n);
}
int printf(const char *fmt, ...)
{
va_list args;
int ret;
va_start(args, fmt);
ret = ee_vprintf(fmt, args);
va_end(args);
return ret;
}
int snprintf(char *str, size_t size, const char *fmt, ...)
{
va_list args;
int ret;
va_start(args, fmt);
ret = vsnprintf(str, size, fmt, args);
va_end(args);
return ret;
}