stdio: refactor printk to printf and friends
This commit is contained in:
parent
474974b779
commit
4db1702078
10 changed files with 354 additions and 246 deletions
|
@ -25,7 +25,7 @@ extern uint32_t _sheap; /* heap start */
|
||||||
extern uint32_t _eheap; /* heap end */
|
extern uint32_t _eheap; /* heap end */
|
||||||
|
|
||||||
/* implementation in init/main.c */
|
/* implementation in init/main.c */
|
||||||
void main(void);
|
extern int main(void);
|
||||||
|
|
||||||
__naked void irq_reset(void)
|
__naked void irq_reset(void)
|
||||||
{
|
{
|
||||||
|
@ -38,7 +38,7 @@ __naked void irq_reset(void)
|
||||||
malloc_init(&_sheap, (size_t)(&_eheap) - (size_t)(&_sheap));
|
malloc_init(&_sheap, (size_t)(&_eheap) - (size_t)(&_sheap));
|
||||||
|
|
||||||
/* start the Kernel */
|
/* start the Kernel */
|
||||||
do_bootstrap();
|
main();
|
||||||
|
|
||||||
/* halt (this should never be reached) */
|
/* halt (this should never be reached) */
|
||||||
while (1);
|
while (1);
|
||||||
|
|
|
@ -1,25 +0,0 @@
|
||||||
/* See the end of this file for copyright, license, and warranty information. */
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Print to the kernel log buffer.
|
|
||||||
*
|
|
||||||
* @param fmt: A printf-style format string.
|
|
||||||
* @return The amount of bytes written, or a negative POSIX number
|
|
||||||
*/
|
|
||||||
__attribute__(( format(printf, 1, 2) ))
|
|
||||||
int printk(const char *fmt, ...);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file is part of Ardix.
|
|
||||||
* Copyright (c) 2020, 2021 Felix Kopp <owo@fef.moe>.
|
|
||||||
*
|
|
||||||
* Ardix is non-violent software: you may only use, redistribute,
|
|
||||||
* and/or modify it under the terms of the CNPLv6+ as found in
|
|
||||||
* the LICENSE file in the source code root directory or at
|
|
||||||
* <https://git.pixie.town/thufie/CNPL>.
|
|
||||||
*
|
|
||||||
* Ardix comes with ABSOLUTELY NO WARRANTY, to the extent
|
|
||||||
* permitted by applicable law. See the CNPLv6+ for details.
|
|
||||||
*/
|
|
|
@ -22,6 +22,7 @@
|
||||||
#define CONFIG_SCHED_MAXPROC @CONFIG_SCHED_MAXPROC@
|
#define CONFIG_SCHED_MAXPROC @CONFIG_SCHED_MAXPROC@
|
||||||
#define CONFIG_SERIAL_BAUD @CONFIG_SERIAL_BAUD@
|
#define CONFIG_SERIAL_BAUD @CONFIG_SERIAL_BAUD@
|
||||||
#define CONFIG_SERIAL_BUFSZ @CONFIG_SERIAL_BUFSZ@
|
#define CONFIG_SERIAL_BUFSZ @CONFIG_SERIAL_BUFSZ@
|
||||||
|
#define CONFIG_PRINTF_BUFSZ @CONFIG_PRINTF_BUFSZ@
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of Ardix.
|
* This file is part of Ardix.
|
||||||
|
|
44
include/stdio.h
Normal file
44
include/stdio.h
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
/* See the end of this file for copyright, license, and warranty information. */
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <toolchain.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* We sacrifice performance for less memory usage by not having a userland
|
||||||
|
* structure for `FILE`. Instead, we cast the file descriptor to a pointer and
|
||||||
|
* use that for syscalls.
|
||||||
|
*/
|
||||||
|
struct _user_file { void *_undefined; };
|
||||||
|
#define FILE struct _user_file
|
||||||
|
|
||||||
|
#define _file_to_fd(f) ((int)( (intptr_t)(f) ))
|
||||||
|
#define _fd_to_file(fd) ((FILE *)( (intptr_t)(fd) ))
|
||||||
|
|
||||||
|
#define stdin _fd_to_file(0)
|
||||||
|
#define stdout _fd_to_file(1)
|
||||||
|
#define stderr _fd_to_file(2)
|
||||||
|
|
||||||
|
__shared int printf(const char *restrict fmt, ...)
|
||||||
|
__attribute__(( format(printf, 1, 2) ));
|
||||||
|
|
||||||
|
__shared int vprintf(const char *restrict fmt, va_list args);
|
||||||
|
|
||||||
|
__shared int fprintf(FILE *f, const char *restrict fmt, ...)
|
||||||
|
__attribute__(( format(printf, 2, 3) ));
|
||||||
|
|
||||||
|
__shared int vfprintf(FILE *f, const char *restrict fmt, va_list args);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Ardix.
|
||||||
|
* Copyright (c) 2020, 2021 Felix Kopp <owo@fef.moe>.
|
||||||
|
*
|
||||||
|
* Ardix is non-violent software: you may only use, redistribute,
|
||||||
|
* and/or modify it under the terms of the CNPLv6+ as found in
|
||||||
|
* the LICENSE file in the source code root directory or at
|
||||||
|
* <https://git.pixie.town/thufie/CNPL>.
|
||||||
|
*
|
||||||
|
* Ardix comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||||
|
* permitted by applicable law. See the CNPLv6+ for details.
|
||||||
|
*/
|
22
init/main.c
22
init/main.c
|
@ -4,12 +4,12 @@
|
||||||
|
|
||||||
#include <ardix/io.h>
|
#include <ardix/io.h>
|
||||||
#include <ardix/kent.h>
|
#include <ardix/kent.h>
|
||||||
#include <ardix/printk.h>
|
|
||||||
#include <ardix/sched.h>
|
#include <ardix/sched.h>
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#define REG_PIOB_PER (*(uint32_t *)0x400E1000U)
|
#define REG_PIOB_PER (*(uint32_t *)0x400E1000U)
|
||||||
|
@ -25,7 +25,7 @@
|
||||||
* This is invoked from the startup code (usually) located in
|
* This is invoked from the startup code (usually) located in
|
||||||
* arch/<architecture>/startup.c.
|
* arch/<architecture>/startup.c.
|
||||||
*/
|
*/
|
||||||
void main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
uint32_t count = 0;
|
uint32_t count = 0;
|
||||||
unsigned int print_count = 0;
|
unsigned int print_count = 0;
|
||||||
|
@ -34,19 +34,27 @@ void main(void)
|
||||||
REG_PIOB_PER = 1 << 27;
|
REG_PIOB_PER = 1 << 27;
|
||||||
REG_PIOB_CODR = 1 << 27;
|
REG_PIOB_CODR = 1 << 27;
|
||||||
|
|
||||||
kent_root_init();
|
int err = kent_root_init();
|
||||||
|
if (err != 0)
|
||||||
|
return err;
|
||||||
|
|
||||||
sched_init();
|
err = sched_init();
|
||||||
|
if (err != 0)
|
||||||
|
return err;
|
||||||
|
|
||||||
devices_init();
|
err = devices_init();
|
||||||
|
if (err != 0)
|
||||||
|
return err;
|
||||||
|
|
||||||
io_init();
|
err = io_init();
|
||||||
|
if (err != 0)
|
||||||
|
return err;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if (count++ != 1000000)
|
if (count++ != 1000000)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
printk("hello, world (%u)!\n", print_count);
|
printf("hello, world (%u)!\n", print_count);
|
||||||
|
|
||||||
print_count++;
|
print_count++;
|
||||||
if (print_count % 2)
|
if (print_count % 2)
|
||||||
|
|
|
@ -13,7 +13,6 @@ target_sources(ardix_kernel PRIVATE
|
||||||
dma.c
|
dma.c
|
||||||
io.c
|
io.c
|
||||||
kent.c
|
kent.c
|
||||||
printk.c
|
|
||||||
ringbuf.c
|
ringbuf.c
|
||||||
sched.c
|
sched.c
|
||||||
serial.c
|
serial.c
|
||||||
|
|
211
kernel/printk.c
211
kernel/printk.c
|
@ -1,211 +0,0 @@
|
||||||
/* See the end of this file for copyright, license, and warranty information. */
|
|
||||||
|
|
||||||
#include <arch/serial.h>
|
|
||||||
|
|
||||||
#include <ardix/dma.h>
|
|
||||||
#include <ardix/printk.h>
|
|
||||||
#include <ardix/ringbuf.h>
|
|
||||||
#include <ardix/serial.h>
|
|
||||||
|
|
||||||
#include <errno.h>
|
|
||||||
/* Using GCC's stdarg.h is recommended even with -nodefaultlibs and -fno-builtin */
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <toolchain.h>
|
|
||||||
|
|
||||||
#ifndef CONFIG_PRINTK_BUFSZ
|
|
||||||
#define CONFIG_PRINTK_BUFSZ 64
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
|
||||||
* TODO: THIS CAUSES A STACK BUFFER OVERFLOW ON SYSTEMS WHERE INT IS 64 BITS
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* 10 decimal digits of 4294967295 (2 ** 32 - 1) */
|
|
||||||
#define PRINTK_UINT_BUFSZ 10
|
|
||||||
|
|
||||||
static ssize_t printk_flush(struct ringbuf *buf)
|
|
||||||
{
|
|
||||||
ssize_t ret;
|
|
||||||
struct dmabuf *dma = dmabuf_create(&serial_default_device->device, buf->len);
|
|
||||||
if (dma == NULL)
|
|
||||||
return -ENOMEM;
|
|
||||||
|
|
||||||
ringbuf_read(dma->data, buf, buf->len);
|
|
||||||
ret = serial_write_dma(serial_default_device, dma);
|
|
||||||
dmabuf_put(dma);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
__rodata static const char fmt_hex_table[] = {
|
|
||||||
'0', '1', '2', '3',
|
|
||||||
'4', '5', '6', '7',
|
|
||||||
'8', '9', 'a', 'b',
|
|
||||||
'c', 'd', 'e', 'f',
|
|
||||||
};
|
|
||||||
|
|
||||||
static int fmt_handle_ptr(struct ringbuf *buf, uintptr_t ptr)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
/* 2 chars per byte, plus 2 for the "0x" hex prefix */
|
|
||||||
char str[2 * sizeof(uintptr_t) + 2];
|
|
||||||
char *pos = &str[2 * sizeof(uintptr_t) + 1];
|
|
||||||
|
|
||||||
str[0] = '0';
|
|
||||||
str[1] = 'x';
|
|
||||||
|
|
||||||
do {
|
|
||||||
*pos-- = fmt_hex_table[ptr & 0xf];
|
|
||||||
ptr >>= 4;
|
|
||||||
} while (pos != &str[2]);
|
|
||||||
|
|
||||||
ret = ringbuf_write(buf, &str[0], 2 * sizeof(uintptr_t) + 2);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int fmt_handle_uint(struct ringbuf *buf, unsigned int u)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
char str[PRINTK_UINT_BUFSZ];
|
|
||||||
char *pos = &str[PRINTK_UINT_BUFSZ - 1];
|
|
||||||
|
|
||||||
do {
|
|
||||||
/* stupid big endian humans, forcing us to do the whole thing in reverse */
|
|
||||||
*pos-- = (char)(u % 10) + '0'; /* convert to ASCII */
|
|
||||||
u /= 10;
|
|
||||||
} while (u != 0);
|
|
||||||
pos++;
|
|
||||||
|
|
||||||
ret = ringbuf_write(buf, pos, PRINTK_UINT_BUFSZ - (pos - &str[0]));
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int fmt_handle_int(struct ringbuf *buf, int i)
|
|
||||||
{
|
|
||||||
int ret = 0;
|
|
||||||
char minus = '-';
|
|
||||||
|
|
||||||
if (i < 0) {
|
|
||||||
ret = ringbuf_write(buf, &minus, sizeof(minus));
|
|
||||||
i = -i;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret += fmt_handle_uint(buf, i);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse a formatting escape sequence, fetch the parameter from the `va_arg`
|
|
||||||
* stack, and print the resulting string to the standard serial console.
|
|
||||||
*
|
|
||||||
* @param pos: A reference to the pointer to the fmt sequence beginner (`%`).
|
|
||||||
* This is updated to point to the first character *after* the entire
|
|
||||||
* escape sequence.
|
|
||||||
* @param args: A pointer to the varargs list. Will be manipulated.
|
|
||||||
* @returns The amount of bytes written, or a negative POSIX error code.
|
|
||||||
*/
|
|
||||||
static inline int fmt_handle(struct ringbuf *buf, const char **pos, va_list args)
|
|
||||||
{
|
|
||||||
int ret = 0;
|
|
||||||
union {
|
|
||||||
int c;
|
|
||||||
int d;
|
|
||||||
uintptr_t p;
|
|
||||||
char *s;
|
|
||||||
unsigned int u;
|
|
||||||
} val;
|
|
||||||
|
|
||||||
switch (**pos) {
|
|
||||||
case '%': /* literal percent sign */
|
|
||||||
ret = ringbuf_write(buf, *pos, sizeof(**pos));
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'c': /* char */
|
|
||||||
val.c = va_arg(args, typeof(val.c));
|
|
||||||
ret = ringbuf_write(buf, &val.c, sizeof(val.c));
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'd': /* int */
|
|
||||||
val.d = va_arg(args, typeof(val.d));
|
|
||||||
ret = fmt_handle_int(buf, val.d);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'p': /* ptr */
|
|
||||||
val.p = va_arg(args, typeof(val.p));
|
|
||||||
ret = fmt_handle_ptr(buf, val.p);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 's': /* string */
|
|
||||||
val.s = va_arg(args, typeof(val.s));
|
|
||||||
ret = (int)strlen(val.s);
|
|
||||||
ret = ringbuf_write(buf, val.s, (size_t)ret);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'u': /* unsigned int */
|
|
||||||
val.u = va_arg(args, typeof(val.u));
|
|
||||||
ret = fmt_handle_uint(buf, val.u);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
(*pos)++;
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int printk(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
va_list args;
|
|
||||||
int ret = 0;
|
|
||||||
int tmpret;
|
|
||||||
const char *tmp = fmt;
|
|
||||||
struct ringbuf *buf = ringbuf_create(CONFIG_PRINTK_BUFSZ);
|
|
||||||
|
|
||||||
if (buf == NULL)
|
|
||||||
return -ENOMEM;
|
|
||||||
|
|
||||||
va_start(args, fmt);
|
|
||||||
|
|
||||||
while (*tmp != '\0') {
|
|
||||||
if (*tmp++ == '%') {
|
|
||||||
/* flush out everything we have so far (minus one char for %) */
|
|
||||||
ret += (int)ringbuf_write(buf, fmt, (size_t)tmp - (size_t)fmt - 1);
|
|
||||||
|
|
||||||
tmpret = fmt_handle(buf, &tmp, args);
|
|
||||||
/*
|
|
||||||
* act as if the current position were the beginning in
|
|
||||||
* order to make the first step of this if block easier
|
|
||||||
*/
|
|
||||||
fmt = tmp;
|
|
||||||
|
|
||||||
if (tmpret < 0)
|
|
||||||
break;
|
|
||||||
ret += tmpret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tmp != fmt && ret >= 0)
|
|
||||||
ret += ringbuf_write(buf, fmt, (size_t)tmp - (size_t)fmt);
|
|
||||||
|
|
||||||
va_end(args);
|
|
||||||
|
|
||||||
printk_flush(buf);
|
|
||||||
ringbuf_destroy(buf);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file is part of Ardix.
|
|
||||||
* Copyright (c) 2020, 2021 Felix Kopp <owo@fef.moe>.
|
|
||||||
*
|
|
||||||
* Ardix is non-violent software: you may only use, redistribute,
|
|
||||||
* and/or modify it under the terms of the CNPLv6+ as found in
|
|
||||||
* the LICENSE file in the source code root directory or at
|
|
||||||
* <https://git.pixie.town/thufie/CNPL>.
|
|
||||||
*
|
|
||||||
* Ardix comes with ABSOLUTELY NO WARRANTY, to the extent
|
|
||||||
* permitted by applicable law. See the CNPLv6+ for details.
|
|
||||||
*/
|
|
|
@ -11,6 +11,7 @@ target_sources(ardix_lib PRIVATE
|
||||||
errno.c
|
errno.c
|
||||||
list.c
|
list.c
|
||||||
malloc.c
|
malloc.c
|
||||||
|
printf.c
|
||||||
string.c
|
string.c
|
||||||
unistd.c
|
unistd.c
|
||||||
)
|
)
|
||||||
|
|
289
lib/printf.c
Normal file
289
lib/printf.c
Normal file
|
@ -0,0 +1,289 @@
|
||||||
|
/* See the end of this file for copyright, license, and warranty information. */
|
||||||
|
|
||||||
|
#include <ardix/malloc.h>
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
/* Using GCC's stdarg.h is recommended even with -nodefaultlibs and -fno-builtin */
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <config.h>
|
||||||
|
#include <toolchain.h>
|
||||||
|
|
||||||
|
#if __SIZEOF_INT__ == 2
|
||||||
|
/* 5 decimal digits of 65535 (2 ** 16 - 1) */
|
||||||
|
# define PRINTF_UINT_BUFSZ 5
|
||||||
|
#elif __SIZEOF_INT__ == 4
|
||||||
|
/* 10 decimal digits of 4294967295 (2 ** 32 - 1) */
|
||||||
|
# define PRINTF_UINT_BUFSZ 10
|
||||||
|
#else
|
||||||
|
/* 20 decimal digits of 18446744073709551616 (2 ** 64 - 1) */
|
||||||
|
# define PRINTF_UINT_BUFSZ 20
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct printf_buf {
|
||||||
|
size_t len;
|
||||||
|
int fd;
|
||||||
|
uint8_t data[0];
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct printf_buf *printf_buf_create(int fd)
|
||||||
|
{
|
||||||
|
struct printf_buf *buf = malloc(sizeof(*buf) + CONFIG_PRINTF_BUFSZ);
|
||||||
|
|
||||||
|
if (buf != NULL) {
|
||||||
|
buf->len = 0;
|
||||||
|
buf->fd = fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void printf_buf_destroy(struct printf_buf *buf)
|
||||||
|
{
|
||||||
|
if (buf->len != 0)
|
||||||
|
write(buf->fd, &buf->data[0], buf->len);
|
||||||
|
|
||||||
|
free(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t printf_buf_write(struct printf_buf *buf, const void *data, size_t len)
|
||||||
|
{
|
||||||
|
ssize_t ret = 0;
|
||||||
|
const uint8_t *tmp = data;
|
||||||
|
|
||||||
|
while (ret != (ssize_t)len) {
|
||||||
|
if (buf->len == CONFIG_PRINTF_BUFSZ) {
|
||||||
|
/*
|
||||||
|
* TODO: We don't need to take the syscall detour
|
||||||
|
* if we are already in kernel context
|
||||||
|
*/
|
||||||
|
ssize_t write_ret = write(buf->fd, &buf->data[0], buf->len);
|
||||||
|
if (write_ret < 0) {
|
||||||
|
ret = write_ret;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (write_ret == 0) {
|
||||||
|
/*
|
||||||
|
* assume something has failed spectacularly
|
||||||
|
* if write() didn't even write a single byte
|
||||||
|
*/
|
||||||
|
ret = -EIO;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (write_ret != (ssize_t)buf->len)
|
||||||
|
memmove(&buf->data[0], &buf->data[write_ret], buf->len - write_ret);
|
||||||
|
|
||||||
|
buf->len -= write_ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
buf->data[buf->len++] = *tmp++;
|
||||||
|
ret++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int fmt_handle_ptr(struct printf_buf *buf, uintptr_t ptr)
|
||||||
|
{
|
||||||
|
static const char fmt_hex_table[] = {
|
||||||
|
'0', '1', '2', '3',
|
||||||
|
'4', '5', '6', '7',
|
||||||
|
'8', '9', 'a', 'b',
|
||||||
|
'c', 'd', 'e', 'f',
|
||||||
|
};
|
||||||
|
|
||||||
|
int ret;
|
||||||
|
/* 2 chars per byte, plus 2 for the "0x" hex prefix */
|
||||||
|
char str[2 * sizeof(uintptr_t) + 2];
|
||||||
|
char *pos = &str[2 * sizeof(uintptr_t) + 1];
|
||||||
|
|
||||||
|
str[0] = '0';
|
||||||
|
str[1] = 'x';
|
||||||
|
|
||||||
|
do {
|
||||||
|
*pos-- = fmt_hex_table[ptr & 0xf];
|
||||||
|
ptr >>= 4;
|
||||||
|
} while (pos != &str[2]);
|
||||||
|
|
||||||
|
ret = printf_buf_write(buf, &str[0], 2 * sizeof(uintptr_t) + 2);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int fmt_handle_uint(struct printf_buf *buf, unsigned int u)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
char str[PRINTF_UINT_BUFSZ];
|
||||||
|
char *pos = &str[PRINTF_UINT_BUFSZ - 1];
|
||||||
|
|
||||||
|
do {
|
||||||
|
/* stupid big endian humans, forcing us to do the whole thing in reverse */
|
||||||
|
*pos-- = (char)(u % 10) + '0'; /* convert to ASCII */
|
||||||
|
u /= 10;
|
||||||
|
} while (u != 0);
|
||||||
|
pos++;
|
||||||
|
|
||||||
|
ret = printf_buf_write(buf, pos, PRINTF_UINT_BUFSZ - (pos - &str[0]));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int fmt_handle_int(struct printf_buf *buf, int i)
|
||||||
|
{
|
||||||
|
int ret = 0;
|
||||||
|
char minus = '-';
|
||||||
|
|
||||||
|
if (i < 0) {
|
||||||
|
ret = printf_buf_write(buf, &minus, sizeof(minus));
|
||||||
|
i = -i;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret += fmt_handle_uint(buf, i);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse a formatting escape sequence, fetch the parameter from the `va_arg`
|
||||||
|
* stack, and print the resulting string to the standard serial console.
|
||||||
|
*
|
||||||
|
* @param pos: A reference to the pointer to the fmt sequence beginner (`%`).
|
||||||
|
* This is updated to point to the first character *after* the entire
|
||||||
|
* escape sequence.
|
||||||
|
* @param args: A pointer to the varargs list. Will be manipulated.
|
||||||
|
* @returns The amount of bytes written, or a negative POSIX error code.
|
||||||
|
*/
|
||||||
|
static inline int fmt_handle(struct printf_buf *buf, const char **pos, va_list args)
|
||||||
|
{
|
||||||
|
int ret = 0;
|
||||||
|
union {
|
||||||
|
int c;
|
||||||
|
int d;
|
||||||
|
uintptr_t p;
|
||||||
|
char *s;
|
||||||
|
unsigned int u;
|
||||||
|
} val;
|
||||||
|
|
||||||
|
switch (**pos) {
|
||||||
|
case '%': /* literal percent sign */
|
||||||
|
ret = printf_buf_write(buf, *pos, sizeof(**pos));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'c': /* char */
|
||||||
|
val.c = va_arg(args, typeof(val.c));
|
||||||
|
ret = printf_buf_write(buf, &val.c, sizeof(val.c));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'd': /* int */
|
||||||
|
val.d = va_arg(args, typeof(val.d));
|
||||||
|
ret = fmt_handle_int(buf, val.d);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'p': /* ptr */
|
||||||
|
val.p = va_arg(args, typeof(val.p));
|
||||||
|
ret = fmt_handle_ptr(buf, val.p);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 's': /* string */
|
||||||
|
val.s = va_arg(args, typeof(val.s));
|
||||||
|
ret = (int)strlen(val.s);
|
||||||
|
ret = printf_buf_write(buf, val.s, (size_t)ret);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'u': /* unsigned int */
|
||||||
|
val.u = va_arg(args, typeof(val.u));
|
||||||
|
ret = fmt_handle_uint(buf, val.u);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
(*pos)++;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int vfprintf(FILE *f, const char *fmt, va_list args)
|
||||||
|
{
|
||||||
|
ssize_t ret = 0;
|
||||||
|
const char *tmp = fmt;
|
||||||
|
struct printf_buf *buf = printf_buf_create(_file_to_fd(f));
|
||||||
|
|
||||||
|
if (buf == NULL)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
while (*tmp != '\0') {
|
||||||
|
if (*tmp++ == '%') {
|
||||||
|
/* flush out everything we have so far (minus one char for %) */
|
||||||
|
ssize_t tmpret = printf_buf_write(buf, fmt, (size_t)tmp - (size_t)fmt - 1);
|
||||||
|
if (tmpret < 0) {
|
||||||
|
ret = tmpret;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ret += tmpret;
|
||||||
|
|
||||||
|
tmpret = fmt_handle(buf, &tmp, args);
|
||||||
|
/*
|
||||||
|
* act as if the current position were the beginning in
|
||||||
|
* order to make the first step of this if block easier
|
||||||
|
*/
|
||||||
|
fmt = tmp;
|
||||||
|
|
||||||
|
if (tmpret < 0)
|
||||||
|
break;
|
||||||
|
ret += tmpret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tmp != fmt && ret >= 0)
|
||||||
|
ret += printf_buf_write(buf, fmt, (size_t)tmp - (size_t)fmt);
|
||||||
|
|
||||||
|
printf_buf_destroy(buf);
|
||||||
|
|
||||||
|
return (int)ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int vprintf(const char *fmt, va_list args)
|
||||||
|
{
|
||||||
|
return vfprintf(stdout, fmt, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
int fprintf(FILE *f, const char *fmt, ...)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
va_list args;
|
||||||
|
|
||||||
|
va_start(args, fmt);
|
||||||
|
ret = vfprintf(f, fmt, args);
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int printf(const char *fmt, ...)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
va_list args;
|
||||||
|
|
||||||
|
va_start(args, fmt);
|
||||||
|
ret = vfprintf(stdout, fmt, args);
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Ardix.
|
||||||
|
* Copyright (c) 2020, 2021 Felix Kopp <owo@fef.moe>.
|
||||||
|
*
|
||||||
|
* Ardix is non-violent software: you may only use, redistribute,
|
||||||
|
* and/or modify it under the terms of the CNPLv6+ as found in
|
||||||
|
* the LICENSE file in the source code root directory or at
|
||||||
|
* <https://git.pixie.town/thufie/CNPL>.
|
||||||
|
*
|
||||||
|
* Ardix comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||||
|
* permitted by applicable law. See the CNPLv6+ for details.
|
||||||
|
*/
|
|
@ -19,6 +19,8 @@ set_property(CACHE CONFIG_SERIAL_BAUD PROPERTY STRINGS
|
||||||
|
|
||||||
set(CONFIG_SERIAL_BUFSZ 256 CACHE STRING "Default serial buffer size in bytes")
|
set(CONFIG_SERIAL_BUFSZ 256 CACHE STRING "Default serial buffer size in bytes")
|
||||||
|
|
||||||
|
set(CONFIG_PRINTF_BUFSZ 64 CACHE STRING "Default buffer size for printf() and friends")
|
||||||
|
|
||||||
# This file is part of Ardix.
|
# This file is part of Ardix.
|
||||||
# Copyright (c) 2021 Felix Kopp <owo@fef.moe>.
|
# Copyright (c) 2021 Felix Kopp <owo@fef.moe>.
|
||||||
#
|
#
|
||||||
|
|
Loading…
Reference in a new issue