9
0
Fork 0

svn_rev_381

change readline prototype to be independent of global console_buffer
This commit is contained in:
Sascha Hauer 2007-07-05 18:01:48 +02:00 committed by Sascha Hauer
parent af347d05ab
commit abbe1817c3
3 changed files with 23 additions and 43 deletions

View File

@ -1,5 +1,7 @@
#include <common.h> #include <common.h>
#include <readkey.h> #include <readkey.h>
#include <init.h>
#include <xfuncs.h>
extern char console_buffer[CONFIG_CBSIZE]; /* console I/O buffer */ extern char console_buffer[CONFIG_CBSIZE]; /* console I/O buffer */
@ -36,7 +38,7 @@ char hist_lines[HIST_MAX][HIST_SIZE];
#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1) #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
static void hist_init(void) static int hist_init(void)
{ {
int i; int i;
@ -49,8 +51,11 @@ static void hist_init(void)
hist_list[i] = hist_lines[i]; hist_list[i] = hist_lines[i];
hist_list[i][0] = '\0'; hist_list[i][0] = '\0';
} }
return 0;
} }
core_initcall(hist_init);
static void cread_add_to_hist(char *line) static void cread_add_to_hist(char *line)
{ {
strcpy(hist_list[hist_add_idx], line); strcpy(hist_list[hist_add_idx], line);
@ -167,7 +172,7 @@ static void cread_add_char(char ichar, int insert, unsigned long *num,
} }
} }
static int cread_line(char *buf, unsigned int *len) int readline(const char *prompt, char *buf, int len)
{ {
unsigned long num = 0; unsigned long num = 0;
unsigned long eol_num = 0; unsigned long eol_num = 0;
@ -177,6 +182,8 @@ static int cread_line(char *buf, unsigned int *len)
int insert = 1; int insert = 1;
int rc = 0; int rc = 0;
puts (prompt);
while (1) { while (1) {
rlen = 1; rlen = 1;
ichar = read_key(); ichar = read_key();
@ -279,42 +286,17 @@ static int cread_line(char *buf, unsigned int *len)
continue; continue;
} }
default: default:
cread_add_char(ichar, insert, &num, &eol_num, buf, *len); cread_add_char(ichar, insert, &num, &eol_num, buf, len);
break; break;
} }
} }
*len = eol_num; len = eol_num;
buf[eol_num] = '\0'; /* lose the newline */ buf[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);
hist_cur = hist_add_idx; hist_cur = hist_add_idx;
return (rc);
}
/*
* Prompt for input and read a line.
* If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
* time out when time goes past endtime (timebase time in ticks).
* Return: number of read characters
* -1 if break
* -2 if timed out
*/
int readline (const char *const prompt)
{
char *p = console_buffer;
unsigned int len=MAX_CMDBUF_SIZE;
int rc;
static int initted = 0;
if (!initted) {
hist_init();
initted = 1;
}
puts (prompt);
rc = cread_line(p, &len);
return rc < 0 ? rc : len; return rc < 0 ? rc : len;
} }

View File

@ -1,8 +1,6 @@
#include <common.h> #include <common.h>
#include <watchdog.h> #include <watchdog.h>
extern char console_buffer[CONFIG_CBSIZE]; /* console I/O buffer */
static char erase_seq[] = "\b \b"; /* erase sequence */ static char erase_seq[] = "\b \b"; /* erase sequence */
static char tab_seq[] = " "; /* used to expand TABs */ static char tab_seq[] = " "; /* used to expand TABs */
@ -44,9 +42,9 @@ static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
* -1 if break * -1 if break
* -2 if timed out * -2 if timed out
*/ */
int readline (const char *const prompt) int readline (const char *prompt, char *line, int len)
{ {
char *p = console_buffer; char *p = line;
int n = 0; /* buffer index */ int n = 0; /* buffer index */
int plen = 0; /* prompt length */ int plen = 0; /* prompt length */
int col; /* output column cnt */ int col; /* output column cnt */
@ -84,13 +82,13 @@ int readline (const char *const prompt)
case '\n': case '\n':
*p = '\0'; *p = '\0';
puts ("\r\n"); puts ("\r\n");
return (p - console_buffer); return (p - line);
case '\0': /* nul */ case '\0': /* nul */
continue; continue;
case 0x03: /* ^C - break */ case 0x03: /* ^C - break */
console_buffer[0] = '\0'; /* discard input */ line[0] = '\0'; /* discard input */
return (-1); return (-1);
case 0x15: /* ^U - erase line */ case 0x15: /* ^U - erase line */
@ -98,20 +96,20 @@ int readline (const char *const prompt)
puts (erase_seq); puts (erase_seq);
--col; --col;
} }
p = console_buffer; p = line;
n = 0; n = 0;
continue; continue;
case 0x17: /* ^W - erase word */ case 0x17: /* ^W - erase word */
p=delete_char(console_buffer, p, &col, &n, plen); p=delete_char(line, p, &col, &n, plen);
while ((n > 0) && (*p != ' ')) { while ((n > 0) && (*p != ' ')) {
p=delete_char(console_buffer, p, &col, &n, plen); p=delete_char(line, p, &col, &n, plen);
} }
continue; continue;
case 0x08: /* ^H - backspace */ case 0x08: /* ^H - backspace */
case 0x7F: /* DEL - backspace */ case 0x7F: /* DEL - backspace */
p=delete_char(console_buffer, p, &col, &n, plen); p=delete_char(line, p, &col, &n, plen);
continue; continue;
default: default:
@ -123,8 +121,8 @@ int readline (const char *const prompt)
#ifdef CONFIG_AUTO_COMPLETE #ifdef CONFIG_AUTO_COMPLETE
/* if auto completion triggered just continue */ /* if auto completion triggered just continue */
*p = '\0'; *p = '\0';
if (cmd_auto_complete(prompt, console_buffer, &n, &col)) { if (cmd_auto_complete(prompt, line, &n, &col)) {
p = console_buffer + n; /* reset */ p = line + n; /* reset */
continue; continue;
} }
#endif #endif

View File

@ -101,7 +101,7 @@ void print_size (ulong, const char *);
/* common/main.c */ /* common/main.c */
void main_loop (void); void main_loop (void);
int run_command (const char *cmd, int flag); int run_command (const char *cmd, int flag);
int readline (const char *const prompt); int readline (const char *prompt, char *buf, int len);
void init_cmd_timeout(void); void init_cmd_timeout(void);
void reset_cmd_timeout(void); void reset_cmd_timeout(void);