shell: Add shell history feature

Extending shell with terminal-like  history feature.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruscinski 2018-08-09 10:38:15 +02:00 committed by Anas Nashif
commit 82ca811661
6 changed files with 338 additions and 20 deletions

View file

@ -9,6 +9,7 @@
#include <zephyr.h>
#include <shell/shell_types.h>
#include <shell/shell_history.h>
#include <shell/shell_fprintf.h>
#include <logging/log_backend.h>
#include <logging/log_instance.h>
@ -339,6 +340,8 @@ struct shell {
const struct shell_transport *iface; /*!< Transport interface.*/
struct shell_ctx *ctx; /*!< Internal context.*/
struct shell_history *history;
const struct shell_fprintf *fprintf_ctx;
LOG_INSTANCE_PTR_DECLARE(log);
@ -360,26 +363,28 @@ struct shell {
* '\\n' or '\\r'.
* @param[in] log_queue_size Logger processing queue size.
*/
#define SHELL_DEFINE(_name, shell_prefix, transport_iface, \
newline_ch, log_queue_size) \
static const struct shell _name; \
static struct shell_ctx UTIL_CAT(_name, _ctx); \
static u8_t _name##_out_buffer[CONFIG_SHELL_PRINTF_BUFF_SIZE]; \
SHELL_FPRINTF_DEFINE(_name## _fprintf, &_name, _name##_out_buffer, \
CONFIG_SHELL_PRINTF_BUFF_SIZE, \
true, shell_print_stream); \
#define SHELL_DEFINE(_name, shell_prefix, transport_iface, \
newline_ch, log_queue_size) \
static const struct shell _name; \
static struct shell_ctx UTIL_CAT(_name, _ctx); \
static u8_t _name##_out_buffer[CONFIG_SHELL_PRINTF_BUFF_SIZE]; \
SHELL_HISTORY_DEFINE(_name, 128, 8);/*todo*/ \
SHELL_FPRINTF_DEFINE(_name## _fprintf, &_name, _name##_out_buffer, \
CONFIG_SHELL_PRINTF_BUFF_SIZE, \
true, shell_print_stream); \
LOG_INSTANCE_REGISTER(shell, _name, CONFIG_SHELL_LOG_LEVEL); \
static struct k_thread _name##_thread; \
static K_THREAD_STACK_DEFINE(_name##_stack, CONFIG_SHELL_STACK_SIZE); \
static const struct shell _name = { \
.name = shell_prefix, \
.iface = transport_iface, \
.ctx = &UTIL_CAT(_name, _ctx), \
.fprintf_ctx = &_name##_fprintf, \
LOG_INSTANCE_PTR_INIT(log, shell, _name) \
.newline_char = newline_ch, \
.thread = &_name##_thread, \
.stack = _name##_stack \
static K_THREAD_STACK_DEFINE(_name##_stack, CONFIG_SHELL_STACK_SIZE);\
static struct k_thread _name##_thread; \
static const struct shell _name = { \
.name = shell_prefix, \
.iface = transport_iface, \
.ctx = &UTIL_CAT(_name, _ctx), \
.history = SHELL_HISTORY_PTR(_name), \
.fprintf_ctx = &_name##_fprintf, \
LOG_INSTANCE_PTR_INIT(log, shell, _name) \
.newline_char = newline_ch, \
.thread = &_name##_thread, \
.stack = _name##_stack \
}
/*

View file

@ -0,0 +1,61 @@
/*
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef SHELL_HISTORY_H__
#define SHELL_HISTORY_H__
#include <zephyr.h>
#include <misc/util.h>
#include <misc/dlist.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
struct shell_history {
struct k_mem_slab *mem_slab;
sys_dlist_t list;
sys_dnode_t *current;
};
#if CONFIG_SHELL_HISTORY
#define SHELL_HISTORY_DEFINE(_name, block_size, block_count) \
\
K_MEM_SLAB_DEFINE(_name##_history_memslab, \
block_size, block_count, 4); \
static struct shell_history _name##_history = { \
.mem_slab = &_name##_history_memslab \
}
#define SHELL_HISTORY_PTR(_name) (&_name##_history)
#else /* CONFIG_SHELL_HISTORY */
#define SHELL_HISTORY_DEFINE(_name, block_size, block_count) /*empty*/
#define SHELL_HISTORY_PTR(_name) NULL
#endif
void shell_history_init(struct shell_history *history);
void shell_history_purge(struct shell_history *history);
void shell_history_mode_exit(struct shell_history *history);
/* returns true if remains in history mode.*/
bool shell_history_get(struct shell_history *history, bool up,
u8_t *dst, size_t *len);
void shell_history_put(struct shell_history *history, u8_t *line, size_t len);
static inline bool shell_history_active(struct shell_history *history)
{
return (history->current) ? true : false;
}
#ifdef __cplusplus
}
#endif
#endif /* SHELL_HISTORY_H__ */