Files

50 lines
1.2 KiB
C
Raw Permalink Normal View History

2018-08-09 09:56:10 +02:00
/*
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/shell/shell_fprintf.h>
#include <zephyr/shell/shell.h>
#include <zephyr/sys/cbprintf.h>
2018-08-09 09:56:10 +02:00
static int out_func(int c, void *ctx)
{
const struct shell_fprintf *sh_fprintf;
const struct shell *sh;
2018-08-09 09:56:10 +02:00
sh_fprintf = (const struct shell_fprintf *)ctx;
sh = (const struct shell *)sh_fprintf->user_ctx;
2018-10-05 10:49:08 +02:00
if ((sh->shell_flag == SHELL_FLAG_OLF_CRLF) && (c == '\n')) {
2018-10-05 10:49:08 +02:00
(void)out_func('\r', ctx);
}
2018-08-09 09:56:10 +02:00
sh_fprintf->buffer[sh_fprintf->ctrl_blk->buffer_cnt] = (uint8_t)c;
2018-08-09 09:56:10 +02:00
sh_fprintf->ctrl_blk->buffer_cnt++;
if (sh_fprintf->ctrl_blk->buffer_cnt == sh_fprintf->buffer_size) {
2020-12-06 11:22:27 +01:00
z_shell_fprintf_buffer_flush(sh_fprintf);
2018-08-09 09:56:10 +02:00
}
return 0;
}
2020-12-06 11:22:27 +01:00
void z_shell_fprintf_fmt(const struct shell_fprintf *sh_fprintf,
const char *fmt, va_list args)
2018-08-09 09:56:10 +02:00
{
(void)cbvprintf(out_func, (void *)sh_fprintf, fmt, args);
2018-08-09 09:56:10 +02:00
if (sh_fprintf->ctrl_blk->autoflush) {
2020-12-06 11:22:27 +01:00
z_shell_fprintf_buffer_flush(sh_fprintf);
2018-08-09 09:56:10 +02:00
}
}
2020-12-06 11:22:27 +01:00
void z_shell_fprintf_buffer_flush(const struct shell_fprintf *sh_fprintf)
2018-08-09 09:56:10 +02:00
{
sh_fprintf->fwrite(sh_fprintf->user_ctx, sh_fprintf->buffer,
sh_fprintf->ctrl_blk->buffer_cnt);
sh_fprintf->ctrl_blk->buffer_cnt = 0;
}