mirror of
https://xff.cz/git/u-boot/
synced 2025-09-03 17:52:07 +02:00
cli: Convert cread_line() to use a struct for the main vars
We want to reuse the editing code elsewhere. As a first step, move the common variables into a struct. This will allow us to eventually put the contents of the inner loop in a function, so it can be called from elsewhere. Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
@@ -188,27 +188,27 @@ void cread_print_hist_list(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define BEGINNING_OF_LINE() { \
|
#define BEGINNING_OF_LINE() { \
|
||||||
while (num) { \
|
while (cls->num) { \
|
||||||
getcmd_putch(CTL_BACKSPACE); \
|
getcmd_putch(CTL_BACKSPACE); \
|
||||||
num--; \
|
cls->num--; \
|
||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define ERASE_TO_EOL() { \
|
#define ERASE_TO_EOL() { \
|
||||||
if (num < eol_num) { \
|
if (cls->num < cls->eol_num) { \
|
||||||
printf("%*s", (int)(eol_num - num), ""); \
|
printf("%*s", (int)(cls->eol_num - cls->num), ""); \
|
||||||
do { \
|
do { \
|
||||||
getcmd_putch(CTL_BACKSPACE); \
|
getcmd_putch(CTL_BACKSPACE); \
|
||||||
} while (--eol_num > num); \
|
} while (--cls->eol_num > cls->num); \
|
||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define REFRESH_TO_EOL() { \
|
#define REFRESH_TO_EOL() { \
|
||||||
if (num < eol_num) { \
|
if (cls->num < cls->eol_num) { \
|
||||||
wlen = eol_num - num; \
|
uint wlen = cls->eol_num - cls->num; \
|
||||||
putnstr(buf + num, wlen); \
|
putnstr(buf + cls->num, wlen); \
|
||||||
num = eol_num; \
|
cls->num = cls->eol_num; \
|
||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cread_add_char(char ichar, int insert, uint *num,
|
static void cread_add_char(char ichar, int insert, uint *num,
|
||||||
@@ -257,18 +257,18 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len,
|
|||||||
int timeout)
|
int timeout)
|
||||||
{
|
{
|
||||||
struct cli_ch_state s_cch, *cch = &s_cch;
|
struct cli_ch_state s_cch, *cch = &s_cch;
|
||||||
uint num = 0;
|
struct cli_line_state s_cls, *cls = &s_cls;
|
||||||
uint eol_num = 0;
|
|
||||||
uint wlen;
|
|
||||||
char ichar;
|
char ichar;
|
||||||
int insert = 1;
|
|
||||||
int init_len = strlen(buf);
|
int init_len = strlen(buf);
|
||||||
int first = 1;
|
int first = 1;
|
||||||
|
|
||||||
cli_ch_init(cch);
|
cli_ch_init(cch);
|
||||||
|
memset(cls, '\0', sizeof(struct cli_line_state));
|
||||||
|
cls->insert = true;
|
||||||
|
|
||||||
if (init_len)
|
if (init_len)
|
||||||
cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
|
cread_add_str(buf, init_len, 1, &cls->num, &cls->eol_num, buf,
|
||||||
|
*len);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
/* Check for saved characters */
|
/* Check for saved characters */
|
||||||
@@ -309,30 +309,33 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len,
|
|||||||
*buf = '\0'; /* discard input */
|
*buf = '\0'; /* discard input */
|
||||||
return -1;
|
return -1;
|
||||||
case CTL_CH('f'):
|
case CTL_CH('f'):
|
||||||
if (num < eol_num) {
|
if (cls->num < cls->eol_num) {
|
||||||
getcmd_putch(buf[num]);
|
getcmd_putch(buf[cls->num]);
|
||||||
num++;
|
cls->num++;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case CTL_CH('b'):
|
case CTL_CH('b'):
|
||||||
if (num) {
|
if (cls->num) {
|
||||||
getcmd_putch(CTL_BACKSPACE);
|
getcmd_putch(CTL_BACKSPACE);
|
||||||
num--;
|
cls->num--;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case CTL_CH('d'):
|
case CTL_CH('d'):
|
||||||
if (num < eol_num) {
|
if (cls->num < cls->eol_num) {
|
||||||
wlen = eol_num - num - 1;
|
uint wlen;
|
||||||
|
|
||||||
|
wlen = cls->eol_num - cls->num - 1;
|
||||||
if (wlen) {
|
if (wlen) {
|
||||||
memmove(&buf[num], &buf[num+1], wlen);
|
memmove(&buf[cls->num],
|
||||||
putnstr(buf + num, wlen);
|
&buf[cls->num + 1], wlen);
|
||||||
|
putnstr(buf + cls->num, wlen);
|
||||||
}
|
}
|
||||||
|
|
||||||
getcmd_putch(' ');
|
getcmd_putch(' ');
|
||||||
do {
|
do {
|
||||||
getcmd_putch(CTL_BACKSPACE);
|
getcmd_putch(CTL_BACKSPACE);
|
||||||
} while (wlen--);
|
} while (wlen--);
|
||||||
eol_num--;
|
cls->eol_num--;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case CTL_CH('k'):
|
case CTL_CH('k'):
|
||||||
@@ -342,28 +345,28 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len,
|
|||||||
REFRESH_TO_EOL();
|
REFRESH_TO_EOL();
|
||||||
break;
|
break;
|
||||||
case CTL_CH('o'):
|
case CTL_CH('o'):
|
||||||
insert = !insert;
|
cls->insert = !cls->insert;
|
||||||
break;
|
break;
|
||||||
case CTL_CH('w'):
|
case CTL_CH('w'):
|
||||||
if (num) {
|
if (cls->num) {
|
||||||
uint base;
|
uint base, wlen;
|
||||||
|
|
||||||
for (base = num - 1;
|
for (base = cls->num - 1;
|
||||||
base >= 0 && buf[base] == ' ';)
|
base >= 0 && buf[base] == ' ';)
|
||||||
base--;
|
base--;
|
||||||
for (; base > 0 && buf[base - 1] != ' ';)
|
for (; base > 0 && buf[base - 1] != ' ';)
|
||||||
base--;
|
base--;
|
||||||
|
|
||||||
/* now delete chars from base to num */
|
/* now delete chars from base to cls->num */
|
||||||
wlen = num - base;
|
wlen = cls->num - base;
|
||||||
eol_num -= wlen;
|
cls->eol_num -= wlen;
|
||||||
memmove(&buf[base], &buf[num],
|
memmove(&buf[base], &buf[cls->num],
|
||||||
eol_num - base + 1);
|
cls->eol_num - base + 1);
|
||||||
num = base;
|
cls->num = base;
|
||||||
getcmd_putchars(wlen, CTL_BACKSPACE);
|
getcmd_putchars(wlen, CTL_BACKSPACE);
|
||||||
puts(buf + base);
|
puts(buf + base);
|
||||||
getcmd_putchars(wlen, ' ');
|
getcmd_putchars(wlen, ' ');
|
||||||
getcmd_putchars(wlen + eol_num - num,
|
getcmd_putchars(wlen + cls->eol_num - cls->num,
|
||||||
CTL_BACKSPACE);
|
CTL_BACKSPACE);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -375,17 +378,20 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len,
|
|||||||
case DEL:
|
case DEL:
|
||||||
case DEL7:
|
case DEL7:
|
||||||
case 8:
|
case 8:
|
||||||
if (num) {
|
if (cls->num) {
|
||||||
wlen = eol_num - num;
|
uint wlen;
|
||||||
num--;
|
|
||||||
memmove(&buf[num], &buf[num+1], wlen);
|
wlen = cls->eol_num - cls->num;
|
||||||
|
cls->num--;
|
||||||
|
memmove(&buf[cls->num], &buf[cls->num + 1],
|
||||||
|
wlen);
|
||||||
getcmd_putch(CTL_BACKSPACE);
|
getcmd_putch(CTL_BACKSPACE);
|
||||||
putnstr(buf + num, wlen);
|
putnstr(buf + cls->num, wlen);
|
||||||
getcmd_putch(' ');
|
getcmd_putch(' ');
|
||||||
do {
|
do {
|
||||||
getcmd_putch(CTL_BACKSPACE);
|
getcmd_putch(CTL_BACKSPACE);
|
||||||
} while (wlen--);
|
} while (wlen--);
|
||||||
eol_num--;
|
cls->eol_num--;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case CTL_CH('p'):
|
case CTL_CH('p'):
|
||||||
@@ -412,7 +418,7 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len,
|
|||||||
|
|
||||||
/* copy new line into place and display */
|
/* copy new line into place and display */
|
||||||
strcpy(buf, hline);
|
strcpy(buf, hline);
|
||||||
eol_num = strlen(buf);
|
cls->eol_num = strlen(buf);
|
||||||
REFRESH_TO_EOL();
|
REFRESH_TO_EOL();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -421,30 +427,30 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len,
|
|||||||
int num2, col;
|
int num2, col;
|
||||||
|
|
||||||
/* do not autocomplete when in the middle */
|
/* do not autocomplete when in the middle */
|
||||||
if (num < eol_num) {
|
if (cls->num < cls->eol_num) {
|
||||||
getcmd_cbeep();
|
getcmd_cbeep();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
buf[num] = '\0';
|
buf[cls->num] = '\0';
|
||||||
col = strlen(prompt) + eol_num;
|
col = strlen(prompt) + cls->eol_num;
|
||||||
num2 = num;
|
num2 = cls->num;
|
||||||
if (cmd_auto_complete(prompt, buf, &num2, &col)) {
|
if (cmd_auto_complete(prompt, buf, &num2, &col)) {
|
||||||
col = num2 - num;
|
col = num2 - cls->num;
|
||||||
num += col;
|
cls->num += col;
|
||||||
eol_num += col;
|
cls->eol_num += col;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
fallthrough;
|
fallthrough;
|
||||||
default:
|
default:
|
||||||
cread_add_char(ichar, insert, &num, &eol_num, buf,
|
cread_add_char(ichar, cls->insert, &cls->num,
|
||||||
*len);
|
&cls->eol_num, buf, *len);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*len = eol_num;
|
*len = cls->eol_num;
|
||||||
buf[eol_num] = '\0'; /* lose the newline */
|
buf[cls->eol_num] = '\0'; /* lose the newline */
|
||||||
|
|
||||||
if (buf[0] && buf[0] != CREAD_HIST_CHAR)
|
if (buf[0] && buf[0] != CREAD_HIST_CHAR)
|
||||||
cread_add_to_hist(buf);
|
cread_add_to_hist(buf);
|
||||||
|
@@ -8,6 +8,7 @@
|
|||||||
#define __CLI_H
|
#define __CLI_H
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <linux/types.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* struct cli_ch_state - state information for reading cmdline characters
|
* struct cli_ch_state - state information for reading cmdline characters
|
||||||
@@ -24,6 +25,19 @@ struct cli_ch_state {
|
|||||||
bool emitting;
|
bool emitting;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* struct cli_line_state - state of the line editor
|
||||||
|
*
|
||||||
|
* @num: Current cursor position, where 0 is the start
|
||||||
|
* @eol_num: Number of characters in the buffer
|
||||||
|
* @insert: true if in 'insert' mode
|
||||||
|
*/
|
||||||
|
struct cli_line_state {
|
||||||
|
uint num;
|
||||||
|
uint eol_num;
|
||||||
|
bool insert;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Go into the command loop
|
* Go into the command loop
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user