zephyr/subsys/shell/shell_fprintf.c
Gerard Marull-Paretas 667eeb11fb shell: fix MISRA 5.7 violations on struct shell
MISRA Rule 5.7 requires uniqueness of tag identifiers. Shell is
frequently problematic because many code uses `const struct shell
*shell`. This causes CI noise every time one of these shell files is
edited, so let's update all of them with `const struct shell *sh`
instead.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-04-14 12:21:08 +02:00

50 lines
1.2 KiB
C

/*
* 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>
static int out_func(int c, void *ctx)
{
const struct shell_fprintf *sh_fprintf;
const struct shell *sh;
sh_fprintf = (const struct shell_fprintf *)ctx;
sh = (const struct shell *)sh_fprintf->user_ctx;
if ((sh->shell_flag == SHELL_FLAG_OLF_CRLF) && (c == '\n')) {
(void)out_func('\r', ctx);
}
sh_fprintf->buffer[sh_fprintf->ctrl_blk->buffer_cnt] = (uint8_t)c;
sh_fprintf->ctrl_blk->buffer_cnt++;
if (sh_fprintf->ctrl_blk->buffer_cnt == sh_fprintf->buffer_size) {
z_shell_fprintf_buffer_flush(sh_fprintf);
}
return 0;
}
void z_shell_fprintf_fmt(const struct shell_fprintf *sh_fprintf,
const char *fmt, va_list args)
{
(void)cbvprintf(out_func, (void *)sh_fprintf, fmt, args);
if (sh_fprintf->ctrl_blk->autoflush) {
z_shell_fprintf_buffer_flush(sh_fprintf);
}
}
void z_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;
}