* (C) Copyright 2005
* JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
*
- * SPDX-License-Identifier: GPL-2.0+
+ * SPDX-License-Identifier: GPL-2.0
*/
+#include "cli_readline.h"
#include "common.h"
#include <string.h>
#include <stdio.h>
#include "con-utils.h"
#include "print-utils.h"
#include "command.h"
-#include "cli_readline.h"
char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
-#ifndef CONFIG_CMDLINE_EDITING
-static const FLASH char erase_seq[] = "\b \b"; /* erase sequence */
-static const FLASH char tab_seq[] = " "; /* used to expand TABs */
-
-static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)
-{
- char *s;
-
- if (*np == 0)
- return p;
-
- if (*(--p) == '\t') { /* will retype the whole line */
- while (*colp > plen) {
- my_puts_P(erase_seq);
- (*colp)--;
- }
- for (s = buffer; s < p; ++s) {
- if (*s == '\t') {
- my_puts_P(tab_seq + ((*colp) & 07));
- *colp += 8 - ((*colp) & 07);
- } else {
- ++(*colp);
- putchar(*s);
- }
- }
- } else {
- my_puts_P(erase_seq);
- (*colp)--;
- }
- (*np)--;
-
- return p;
-}
-#endif /* CONFIG_CMDLINE_EDITING */
-
-
-#ifdef CONFIG_CMDLINE_EDITING
-
/*
* cmdline-editing related codes from vivi.
* Author: Janghoon Lyu <nandy@mizi.com>
static char *hist_list[HIST_MAX];
-static void hist_init(void)
-{
- int_fast8_t i;
-
- hist_max = 0;
- hist_add_idx = 0;
- hist_cur = -1;
-
- for (i = 0; i < HIST_MAX; i++) {
- hist_list[i] = NULL;
- }
-}
-
static void cread_add_to_hist(char *line)
{
if (hist_max) {
}
}
-static int cread_line(const FLASH char *const prompt, char *buf, uint_fast8_t *len)
+static int cread_line(const FLASH char *const prompt, char *buf,
+ uint_fast8_t *len, bool enable_history)
{
uint_fast8_t num = 0;
uint_fast8_t eol_num = 0;
case CTL_CH('k'): /* kill-line */
ERASE_TO_EOL();
break;
+ case KEY_END:
case CTL_CH('e'):
REFRESH_TO_EOL();
break;
case CTL_CH('p'): /* previous-history */
case KEY_DOWN:
case CTL_CH('n'): /* next-history */
- {
- char *hline;
-
- if (ichar == CTL_CH('p') || ichar == KEY_UP)
- hline = hist_prev();
- else
- hline = hist_next();
-
- if (!hline) {
+ if (enable_history) {
+ char *hline;
+
+ if (ichar == CTL_CH('p') || ichar == KEY_UP)
+ hline = hist_prev();
+ else
+ hline = hist_next();
+
+ if (hline) {
+ /* nuke the current line */
+ /* first, go home */
+ BEGINNING_OF_LINE();
+
+ /* erase to end of line */
+ ERASE_TO_EOL();
+
+ /* copy new line into place and display */
+ strcpy(buf, hline);
+ eol_num = strlen(buf);
+ REFRESH_TO_EOL();
+ } else {
+ getcmd_cbeep();
+ }
+ } else {
getcmd_cbeep();
- continue;
}
-
- /* nuke the current line */
- /* first, go home */
- BEGINNING_OF_LINE();
-
- /* erase to end of line */
- ERASE_TO_EOL();
-
- /* copy new line into place and display */
- strcpy(buf, hline);
- eol_num = strlen(buf);
- REFRESH_TO_EOL();
- continue;
- }
+ break;
#ifdef CONFIG_AUTO_COMPLETE
case '\t': {
int num2, col;
*len = eol_num;
buf[eol_num] = '\0'; /* lose the newline */
- if (buf[0] /* && buf[0] != CREAD_HIST_CHAR */)
- cread_add_to_hist(buf);
- hist_cur = hist_add_idx;
-
+ if (enable_history) {
+ if (buf[0])
+ cread_add_to_hist(buf);
+ hist_cur = hist_add_idx;
+ }
return 0;
}
-#endif /* CONFIG_CMDLINE_EDITING */
-
/****************************************************************************/
-static int cli_readline_into_buffer(const FLASH char *const prompt, char *buffer)
+static int cli_readline_into_buffer(const FLASH char *const prompt,
+ char *buffer, bool enable_history)
{
char *p = buffer;
-#ifdef CONFIG_CMDLINE_EDITING
uint_fast8_t len = CONFIG_SYS_CBSIZE;
int rc;
- static bool initted;
-
- if (!initted) {
- hist_init();
- initted = 1;
- }
if (prompt)
my_puts_P(prompt);
- rc = cread_line(prompt, p, &len);
+ rc = cread_line(prompt, p, &len, enable_history);
return rc < 0 ? rc : (int) len;
-
-#else /* CONFIG_CMDLINE_EDITING */
- char *p_buf = p;
- int n = 0; /* buffer index */
- int plen = 0; /* prompt length */
- int col; /* output column cnt */
- char c;
-
- /* print prompt */
- if (prompt) {
- plen = strlen_P(prompt);
- my_puts_P(prompt);
- }
- col = plen;
-
- for (;;) {
-
- c = my_getchar(1);
-
- /*
- * Special character handling
- */
- switch (c) {
- case '\r': /* Enter */
- case '\n':
- *p = '\0';
- my_puts_P(PSTR("\r\n"));
- return p - p_buf;
-
- case '\0': /* nul */
- continue;
-
- case 0x03: /* ^C - break */
- p_buf[0] = '\0'; /* discard input */
- return -1;
-
- case 0x15: /* ^U - erase line */
- while (col > plen) {
- my_puts_P(erase_seq);
- --col;
- }
- p = p_buf;
- n = 0;
- continue;
-
- case 0x17: /* ^W - erase word */
- p = delete_char(p_buf, p, &col, &n, plen);
- while ((n > 0) && (*p != ' '))
- p = delete_char(p_buf, p, &col, &n, plen);
- continue;
-
- case 0x08: /* ^H - backspace */
- case 0x7F: /* DEL - backspace */
- p = delete_char(p_buf, p, &col, &n, plen);
- continue;
-
- default:
- /*
- * Must be a normal character then
- */
- if (n < CONFIG_SYS_CBSIZE-2) {
- if (c == '\t') { /* expand TABs */
-#ifdef CONFIG_AUTO_COMPLETE
- /*
- * if auto completion triggered just
- * continue
- */
- *p = '\0';
- if (cmd_auto_complete(prompt,
- console_buffer,
- &n, &col)) {
- p = p_buf + n; /* reset */
- continue;
- }
-#endif
- my_puts_P(tab_seq + (col & 07));
- col += 8 - (col & 07);
- } else {
- ++col;
- putchar(c);
- }
- *p++ = c;
- ++n;
- } else { /* Buffer full */
- putchar('\a');
- }
- }
- }
-#endif /* CONFIG_CMDLINE_EDITING */
}
-int cli_readline(const FLASH char *const prompt)
+int cli_readline(const FLASH char *const prompt, bool enable_history)
{
/*
* If console_buffer isn't 0-length the user will be prompted to modify
*/
console_buffer[0] = '\0';
- return cli_readline_into_buffer(prompt, console_buffer);
+ return cli_readline_into_buffer(prompt, console_buffer, enable_history);
}