You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by je...@apache.org on 2019/12/05 12:36:20 UTC
[mynewt-core] 03/06: sys/console: Use separate package for history
This is an automated email from the ASF dual-hosted git repository.
jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit 5defd588995626de4258e6c1c83af4f5404281ec
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Wed Nov 13 15:51:29 2019 +0100
sys/console: Use separate package for history
In-RAM history code from console.c was dropped in favor
of history_ram package.
Functionality should be the same.
---
sys/console/full/pkg.yml | 2 +
sys/console/full/src/console.c | 220 +++++------------------------------------
sys/console/full/syscfg.yml | 19 +++-
3 files changed, 43 insertions(+), 198 deletions(-)
diff --git a/sys/console/full/pkg.yml b/sys/console/full/pkg.yml
index 361cbbe..a035e28 100644
--- a/sys/console/full/pkg.yml
+++ b/sys/console/full/pkg.yml
@@ -30,6 +30,8 @@ pkg.deps.CONSOLE_UART:
- "@apache-mynewt-core/hw/drivers/uart"
pkg.deps.CONSOLE_RTT:
- "@apache-mynewt-core/hw/drivers/rtt"
+pkg.deps.'CONSOLE_HISTORY == "ram"':
+ - "@apache-mynewt-core/sys/console/full/history_ram"
pkg.apis: console
pkg.init:
diff --git a/sys/console/full/src/console.c b/sys/console/full/src/console.c
index 491b9c2..c296d69 100644
--- a/sys/console/full/src/console.c
+++ b/sys/console/full/src/console.c
@@ -30,6 +30,7 @@
#include "console/console.h"
#include "console/ticks.h"
#include "console_priv.h"
+#include "console/history.h"
/* Control characters */
#define ESC 0x1b
@@ -111,6 +112,8 @@ static int max_row;
static char console_prompt[MYNEWT_VAL(CONSOLE_MAX_PROMPT_LEN)];
/* Length of prompt stored in console_prompt */
static uint16_t prompt_len;
+/* Current history line, 0 no history line */
+static history_handle_t history_line;
/*
* Default implementation in case all consoles are disabled - we just ignore any
@@ -689,198 +692,36 @@ console_clear_line(void)
cursor_clear_line();
}
-#if MYNEWT_VAL(CONSOLE_HISTORY_SIZE) > 0
-
-#ifndef bssnz_t
-/* Just in case bsp.h does not define it, in this case console history will
- * not be preserved across software resets
- */
-#define bssnz_t
-#endif
-
-bssnz_t static char console_hist_lines[ MYNEWT_VAL(CONSOLE_HISTORY_SIZE) ][ MYNEWT_VAL(CONSOLE_MAX_INPUT_LEN) ];
-
-bssnz_t static struct console_hist {
- uint32_t magic;
- uint8_t head;
- uint8_t tail;
- uint8_t size;
- uint8_t curr;
- char *lines[ MYNEWT_VAL(CONSOLE_HISTORY_SIZE) + 1 ];
-} console_hist;
-
-static void
-console_hist_init(void)
-{
- struct console_hist *sh = &console_hist;
- int i;
-
- if (sh->magic != 0xBABEFACE) {
- memset(console_hist_lines, 0, sizeof(console_hist_lines));
- memset(&console_hist, 0, sizeof(console_hist));
-
- sh->size = MYNEWT_VAL(CONSOLE_HISTORY_SIZE) + 1;
-
- for (i = 0; i < sh->size - 1; i++) {
- sh->lines[i] = console_hist_lines[i];
- }
- sh->magic = 0xBABEFACE;
- }
-}
-
-static size_t
-trim_whitespace(const char *str, size_t len, char *out)
-{
- const char *end;
- size_t out_size;
-
- if (len == 0) {
- return 0;
- }
-
- /* Trim leading space */
- while (isspace((unsigned char)*str)) {
- str++;
- }
-
- if (*str == 0) { /* All spaces? */
- *out = 0;
- return 0;
- }
-
- /* Trim trailing space */
- end = str + strlen(str) - 1;
- while (end > str && isspace((unsigned char)*end)) {
- end--;
- }
-
- end++;
-
- /* Set output size to minimum of trimmed string length and buffer size minus 1 */
- out_size = min(end - str, len - 1);
-
- /* Copy trimmed string and add null terminator */
- memcpy(out, str, out_size);
- out[out_size] = 0;
-
- return out_size;
-}
-
-static uint8_t
-ring_buf_next(uint8_t i, uint8_t size)
-{
- return (uint8_t) ((i + 1) % size);
-}
-
-static uint8_t
-ring_buf_prev(uint8_t i, uint8_t size)
-{
- return i == 0 ? i = size - 1 : --i;
-}
-
-static bool
-console_hist_is_full(void)
-{
- struct console_hist *sh = &console_hist;
-
- return ring_buf_next(sh->head, sh->size) == sh->tail;
-}
-
-static bool
-console_hist_move_to_head(char *line)
-{
- struct console_hist *sh = &console_hist;
- char *match = NULL;
- uint8_t prev, curr;
-
- curr = sh->tail;
- while (curr != sh->head) {
- if (strcmp(sh->lines[curr], line) == 0) {
- match = sh->lines[curr];
- break;
- }
- curr = ring_buf_next(curr, sh->size);
- }
-
- if (!match) {
- return false;
- }
-
- prev = curr;
- curr = ring_buf_next(curr, sh->size);
- while (curr != sh->head) {
- sh->lines[prev] = sh->lines[curr];
- prev = curr;
- curr = ring_buf_next(curr, sh->size);
- }
-
- sh->lines[prev] = match;
-
- return true;
-}
+#if !MYNEWT_VAL_CHOICE(CONSOLE_HISTORY, none)
static void
-console_hist_add(char *line)
+console_history_move(char *line, history_find_type_t direction)
{
- struct console_hist *sh = &console_hist;
- char buf[MYNEWT_VAL(CONSOLE_MAX_INPUT_LEN)];
- size_t len;
+ history_handle_t new_line;
- /* Reset current pointer */
- sh->curr = sh->head;
+ new_line = console_history_find(history_line, direction, NULL);
- len = trim_whitespace(line, sizeof(buf), buf);
- if (!len) {
+ /* No more lines backward, do nothing */
+ if (!new_line && direction == HFT_PREV) {
return;
}
- if (console_hist_move_to_head(buf)) {
- return;
- }
-
- if (console_hist_is_full()) {
- /*
- * We have N buffers, but there are N+1 items in queue so one element is
- * always empty. If queue is full we need to move buffer from oldest
- * entry to current head and trim queue tail.
- */
- assert(sh->lines[sh->head] == NULL);
- sh->lines[sh->head] = sh->lines[sh->tail];
- sh->lines[sh->tail] = NULL;
- sh->tail = ring_buf_next(sh->tail, sh->size);
+ history_line = new_line;
+ console_clear_line();
+ if (new_line) {
+ cur = console_history_get(new_line, 0, line,
+ MYNEWT_VAL_CONSOLE_MAX_INPUT_LEN);
+ } else {
+ line[0] = '\0';
}
-
- strcpy(sh->lines[sh->head], buf);
- sh->head = ring_buf_next(sh->head, sh->size);
-
- /* Reset current pointer */
- sh->curr = sh->head;
+ trailing_chars = 0;
+ console_write_nolock(line, cur);
}
static void
-console_hist_move(char *line, uint8_t direction)
+console_handle_move(char *line, int direction)
{
- struct console_hist *sh = &console_hist;
- char *str = NULL;
- uint8_t limit = direction == ANSI_UP ? sh->tail : sh->head;
-
- /* no more history to return in this direction */
- if (sh->curr == limit) {
- return;
- }
-
- if (direction == ANSI_UP) {
- sh->curr = ring_buf_prev(sh->curr, sh->size);
- } else {
- sh->curr = ring_buf_next(sh->curr, sh->size);
- }
-
- console_clear_line();
- str = sh->lines[sh->curr];
- while (str && *str != '\0') {
- insert_char(&line[cur], *str);
- ++str;
- }
+ console_history_move(line, direction > 0 ? HFT_PREV : HFT_NEXT);
}
#endif
@@ -952,16 +793,10 @@ handle_ansi(uint8_t byte, char *line)
ansi_cmd:
switch (byte) {
-#if MYNEWT_VAL(CONSOLE_HISTORY_SIZE) > 0
+#if !MYNEWT_VAL_CHOICE(CONSOLE_HISTORY, none)
case ANSI_UP:
case ANSI_DOWN:
-#if MYNEWT_VAL(CONSOLE_UART_RX_BUF_SIZE) == 0
- console_blocking_mode();
-#endif
- console_hist_move(line, byte);
-#if MYNEWT_VAL(CONSOLE_UART_RX_BUF_SIZE) == 0
- console_non_blocking_mode();
-#endif
+ console_handle_move(line, byte == ANSI_UP ? 1 : -1);
break;
#endif
case ANSI_BACKWARD:
@@ -1233,9 +1068,10 @@ console_handle_char(uint8_t byte)
console_filter_out('\r');
console_filter_out('\n');
}
-#if MYNEWT_VAL(CONSOLE_HISTORY_SIZE) > 0
- console_hist_add(input->line);
-#endif
+ if (!MYNEWT_VAL_CHOICE(CONSOLE_HISTORY, none)) {
+ console_history_add(input->line);
+ history_line = 0;
+ }
console_handle_line();
break;
case '\t':
@@ -1357,10 +1193,6 @@ console_pkg_init(void)
os_eventq_init(&avail_queue);
os_mutex_init(&console_write_lock);
-#if MYNEWT_VAL(CONSOLE_HISTORY_SIZE) > 0
- console_hist_init();
-#endif
-
#if MYNEWT_VAL(CONSOLE_UART)
rc = uart_console_init();
#endif
diff --git a/sys/console/full/syscfg.yml b/sys/console/full/syscfg.yml
index a71f95f..0aebad7 100644
--- a/sys/console/full/syscfg.yml
+++ b/sys/console/full/syscfg.yml
@@ -41,11 +41,13 @@ syscfg.defs:
CONSOLE_MAX_INPUT_LEN:
description: 'Maximum input line length'
value: 256
- CONSOLE_HISTORY_SIZE:
+ CONSOLE_HISTORY:
description: >
- Number of lines to be stored in console history.
- Set to "0" to disable console history.
- value: 0
+ Select how console should handle command history.
+ value: none
+ choices:
+ - none # no history support
+ - ram # history kept in ram for current session only
CONSOLE_MAX_PROMPT_LEN:
description: 'Maximum number of characters for prompt'
value: 16
@@ -124,3 +126,12 @@ syscfg.defs:
description: >
Sysinit stage for console functionality.
value: 20
+
+ CONSOLE_HISTORY_SIZE:
+ description: >
+ This is no longer used.
+ Equivalent settings:
+ CONSOLE_HISTORY: ram
+ CONSOLE_HISTORY_RAM_HISTORY_SIZE: <n> <- previous value of CONSOLE_HISTORY_SIZE
+ value: 0
+ defunct: 1