2018-08-09 15:56:10 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Nordic Semiconductor ASA
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <shell/shell_fprintf.h>
|
2018-10-05 16:49:08 +08:00
|
|
|
#include <shell/shell.h>
|
2018-08-09 15:56:10 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_NEWLIB_LIBC
|
|
|
|
typedef int (*out_func_t)(int c, void *ctx);
|
2019-03-15 09:42:51 +08:00
|
|
|
extern void z_vprintk(out_func_t out, void *ctx, const char *fmt, va_list ap);
|
2018-08-09 15:56:10 +08:00
|
|
|
#else
|
2019-03-15 09:42:51 +08:00
|
|
|
extern int z_prf(int (*func)(), void *dest, char *format, va_list vargs);
|
2018-08-09 15:56:10 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static int out_func(int c, void *ctx)
|
|
|
|
{
|
|
|
|
const struct shell_fprintf *sh_fprintf;
|
2018-10-05 16:49:08 +08:00
|
|
|
const struct shell *shell;
|
2018-08-09 15:56:10 +08:00
|
|
|
|
|
|
|
sh_fprintf = (const struct shell_fprintf *)ctx;
|
2018-10-05 16:49:08 +08:00
|
|
|
shell = (const struct shell *)sh_fprintf->user_ctx;
|
|
|
|
|
|
|
|
if ((shell->shell_flag == SHELL_FLAG_OLF_CRLF) && (c == '\n')) {
|
|
|
|
(void)out_func('\r', ctx);
|
|
|
|
}
|
2018-08-09 15:56:10 +08:00
|
|
|
|
|
|
|
sh_fprintf->buffer[sh_fprintf->ctrl_blk->buffer_cnt] = (u8_t)c;
|
|
|
|
sh_fprintf->ctrl_blk->buffer_cnt++;
|
|
|
|
|
|
|
|
if (sh_fprintf->ctrl_blk->buffer_cnt == sh_fprintf->buffer_size) {
|
|
|
|
shell_fprintf_buffer_flush(sh_fprintf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void shell_fprintf_fmt(const struct shell_fprintf *sh_fprintf,
|
|
|
|
const char *fmt, va_list args)
|
|
|
|
{
|
2018-09-24 21:57:52 +08:00
|
|
|
#if !defined(CONFIG_NEWLIB_LIBC) && !defined(CONFIG_ARCH_POSIX)
|
2019-03-15 09:42:51 +08:00
|
|
|
(void)z_prf(out_func, (void *)sh_fprintf, (char *)fmt, args);
|
2018-08-09 15:56:10 +08:00
|
|
|
#else
|
2019-03-15 09:42:51 +08:00
|
|
|
z_vprintk(out_func, (void *)sh_fprintf, fmt, args);
|
2018-08-09 15:56:10 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (sh_fprintf->ctrl_blk->autoflush) {
|
|
|
|
shell_fprintf_buffer_flush(sh_fprintf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void shell_fprintf_buffer_flush(const struct shell_fprintf *sh_fprintf)
|
|
|
|
{
|
|
|
|
sh_fprintf->fwrite(sh_fprintf->user_ctx, sh_fprintf->buffer,
|
|
|
|
sh_fprintf->ctrl_blk->buffer_cnt);
|
|
|
|
sh_fprintf->ctrl_blk->buffer_cnt = 0;
|
|
|
|
}
|