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>
|
2020-11-04 05:14:16 +08:00
|
|
|
#include <sys/cbprintf.h>
|
2018-08-09 15:56:10 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
sh_fprintf->buffer[sh_fprintf->ctrl_blk->buffer_cnt] = (uint8_t)c;
|
2018-08-09 15:56:10 +08:00
|
|
|
sh_fprintf->ctrl_blk->buffer_cnt++;
|
|
|
|
|
|
|
|
if (sh_fprintf->ctrl_blk->buffer_cnt == sh_fprintf->buffer_size) {
|
2020-12-06 18:22:27 +08:00
|
|
|
z_shell_fprintf_buffer_flush(sh_fprintf);
|
2018-08-09 15:56:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-06 18:22:27 +08:00
|
|
|
void z_shell_fprintf_fmt(const struct shell_fprintf *sh_fprintf,
|
|
|
|
const char *fmt, va_list args)
|
2018-08-09 15:56:10 +08:00
|
|
|
{
|
2020-11-04 05:14:16 +08:00
|
|
|
(void)cbvprintf(out_func, (void *)sh_fprintf, fmt, args);
|
2018-08-09 15:56:10 +08:00
|
|
|
|
|
|
|
if (sh_fprintf->ctrl_blk->autoflush) {
|
2020-12-06 18:22:27 +08:00
|
|
|
z_shell_fprintf_buffer_flush(sh_fprintf);
|
2018-08-09 15:56:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-06 18:22:27 +08:00
|
|
|
void z_shell_fprintf_buffer_flush(const struct shell_fprintf *sh_fprintf)
|
2018-08-09 15:56:10 +08:00
|
|
|
{
|
|
|
|
sh_fprintf->fwrite(sh_fprintf->user_ctx, sh_fprintf->buffer,
|
|
|
|
sh_fprintf->ctrl_blk->buffer_cnt);
|
|
|
|
sh_fprintf->ctrl_blk->buffer_cnt = 0;
|
|
|
|
}
|