You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/07/04 21:22:39 UTC

[GitHub] andrzej-kaczmarek closed pull request #1241: sys/console: Few fixes for console history handling

andrzej-kaczmarek closed pull request #1241: sys/console: Few fixes for console history handling
URL: https://github.com/apache/mynewt-core/pull/1241
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/sys/console/full/src/console.c b/sys/console/full/src/console.c
index 12b482524d..78bf9cc89c 100644
--- a/sys/console/full/src/console.c
+++ b/sys/console/full/src/console.c
@@ -173,7 +173,7 @@ cursor_backward(unsigned int count)
     console_printf("\x1b[%uD", count);
 }
 
-#if MYNEWT_VAL(CONSOLE_HISTORY)
+#if MYNEWT_VAL(CONSOLE_HISTORY_SIZE) > 0
 static inline void
 cursor_clear_line(void)
 {
@@ -260,24 +260,31 @@ del_char(char *pos, uint16_t end)
     cursor_restore();
 }
 
-#if MYNEWT_VAL(CONSOLE_HISTORY)
-struct console_hist {
-    struct console_input buffer[MYNEWT_VAL(CONSOLE_HISTORY_SIZE)];
+#if MYNEWT_VAL(CONSOLE_HISTORY_SIZE) > 0
+static char console_hist_lines[ MYNEWT_VAL(CONSOLE_HISTORY_SIZE) ][ MYNEWT_VAL(CONSOLE_MAX_INPUT_LEN) ];
+
+static struct console_hist {
     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;
+
+    memset(console_hist_lines, 0, sizeof(console_hist_lines));
+    memset(&console_hist, 0, sizeof(console_hist));
 
-    sh->head = 0;
-    sh->tail = 0;
-    sh->curr = 0;
-    sh->size = MYNEWT_VAL(CONSOLE_HISTORY_SIZE);
+    sh->size = MYNEWT_VAL(CONSOLE_HISTORY_SIZE) + 1;
+
+    for (i = 0; i < sh->size - 1; i++) {
+        sh->lines[i] = console_hist_lines[i];
+    }
 }
 
 static size_t
@@ -327,46 +334,48 @@ ring_buf_next(uint8_t i, uint8_t size)
 static uint8_t
 ring_buf_prev(uint8_t i, uint8_t size)
 {
-    return (uint8_t) ((i - 1) % size);
+    return i == 0 ? i = size - 1 : --i;
 }
 
 static bool
-console_hist_full(void)
-{
-    struct console_hist *sh = &console_hist;
-
-    return sh->head == sh->tail;
-}
-
-static void
-console_hist_next(void)
+console_hist_is_full(void)
 {
     struct console_hist *sh = &console_hist;
 
-    sh->head = (uint8_t) ring_buf_next(sh->head, sh->size);
-
-    /* buffer full, start overwriting old history */
-    if (console_hist_full()) {
-        sh->tail = (uint8_t) ring_buf_next(sh->tail, sh->size);
-    }
+    return ring_buf_next(sh->head, sh->size) == sh->tail;
 }
 
 static bool
-console_hist_find(char *line)
+console_hist_move_to_head(char *line)
 {
     struct console_hist *sh = &console_hist;
-    uint8_t curr;
+    char *match = NULL;
+    uint8_t prev, curr;
 
     curr = sh->tail;
     while (curr != sh->head) {
-        if (strcmp(sh->buffer[curr].line, line) == 0) {
-            return true;
+        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);
     }
 
-    return false;
+    sh->lines[prev] = match;
+
+    return true;
 }
 
 static void
@@ -384,12 +393,25 @@ console_hist_add(char *line)
         return;
     }
 
-    if (console_hist_find(buf)) {
+    if (console_hist_move_to_head(buf)) {
         return;
     }
 
-    strcpy(sh->buffer[sh->head].line, buf);
-    console_hist_next();
+    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);
+    }
+
+    strcpy(sh->lines[sh->head], buf);
+    sh->head = ring_buf_next(sh->head, sh->size);
+
     /* Reset current pointer */
     sh->curr = sh->head;
 }
@@ -425,8 +447,8 @@ console_hist_move(char *line, uint8_t direction)
     }
 
     console_clear_line();
-    str = sh->buffer[sh->curr].line;
-    while (*str != '\0') {
+    str = sh->lines[sh->curr];
+    while (str && *str != '\0') {
         insert_char(&line[cur], *str, end);
         ++str;
     }
@@ -473,7 +495,7 @@ handle_ansi(uint8_t byte, char *line)
 
 ansi_cmd:
     switch (byte) {
-#if MYNEWT_VAL(CONSOLE_HISTORY)
+#if MYNEWT_VAL(CONSOLE_HISTORY_SIZE) > 0
     case ANSI_UP:
     case ANSI_DOWN:
         console_blocking_mode();
@@ -696,7 +718,7 @@ console_handle_char(uint8_t byte)
             cur = 0;
             end = 0;
             os_eventq_put(lines_queue, ev);
-#if MYNEWT_VAL(CONSOLE_HISTORY)
+#if MYNEWT_VAL(CONSOLE_HISTORY_SIZE) > 0
             console_hist_add(input->line);
 #endif
 
@@ -796,7 +818,7 @@ console_pkg_init(void)
 
     os_eventq_init(&avail_queue);
 
-#if MYNEWT_VAL(CONSOLE_HISTORY)
+#if MYNEWT_VAL(CONSOLE_HISTORY_SIZE) > 0
     console_hist_init();
 #endif
 
diff --git a/sys/console/full/syscfg.yml b/sys/console/full/syscfg.yml
index 779a6a35bd..647160e28f 100644
--- a/sys/console/full/syscfg.yml
+++ b/sys/console/full/syscfg.yml
@@ -43,12 +43,11 @@ syscfg.defs:
     CONSOLE_MAX_INPUT_LEN:
         description: 'Maximum input line length'
         value: 256
-    CONSOLE_HISTORY:
-        description: 'Enable history functionality'
-        value: 0
     CONSOLE_HISTORY_SIZE:
-        description: 'Number of lines in history'
-        value: 16
+        description: >
+            Number of lines to be stored in console history.
+            Set to "0" to disable console history.
+        value: 0
 
     CONSOLE_UART_BAUD:
         description: 'Console UART baud rate.'


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services