diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2007-07-05 18:02:13 +0200 |
---|---|---|
committer | Sascha Hauer <sha@octopus.labnet.pengutronix.de> | 2007-07-05 18:02:13 +0200 |
commit | 4b07af6730d2811363f158f5175138116038f7b9 (patch) | |
tree | 206044270884f80204a2da69e02ca3b6f5803185 /lib/readline_simple.c | |
parent | d08c60e9d77dc0f83946cd702d383451865e66dd (diff) | |
download | barebox-4b07af6730d2811363f158f5175138116038f7b9.tar.gz barebox-4b07af6730d2811363f158f5175138116038f7b9.tar.xz |
svn_rev_643
structure cleanup
Diffstat (limited to 'lib/readline_simple.c')
-rw-r--r-- | lib/readline_simple.c | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/lib/readline_simple.c b/lib/readline_simple.c new file mode 100644 index 0000000000..7675103ea1 --- /dev/null +++ b/lib/readline_simple.c @@ -0,0 +1,142 @@ +#include <common.h> +#include <watchdog.h> + +static char erase_seq[] = "\b \b"; /* erase sequence */ +static 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) { + puts (erase_seq); + (*colp)--; + } + for (s=buffer; s<p; ++s) { + if (*s == '\t') { + puts (tab_seq+((*colp) & 07)); + *colp += 8 - ((*colp) & 07); + } else { + ++(*colp); + putc (*s); + } + } + } else { + puts (erase_seq); + (*colp)--; + } + (*np)--; + return (p); +} + +/* + * 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 *prompt, char *line, int len) +{ + char *p = line; + int n = 0; /* buffer index */ + int plen = 0; /* prompt length */ + int col; /* output column cnt */ + char c; + + /* print prompt */ + if (prompt) { + plen = strlen (prompt); + puts (prompt); + } + col = plen; + + for (;;) { +#ifdef CONFIG_BOOT_RETRY_TIME + while (!tstc()) { /* while no incoming data */ + if (retry_time >= 0 && get_ticks() > endtime) + return (-2); /* timed out */ + } +#endif + WATCHDOG_RESET(); /* Trigger watchdog, if needed */ + +#ifdef CONFIG_SHOW_ACTIVITY + while (!tstc()) { + extern void show_activity(int arg); + show_activity(0); + } +#endif + c = getc(); + + /* + * Special character handling + */ + switch (c) { + case '\r': /* Enter */ + case '\n': + *p = '\0'; + puts ("\r\n"); + return (p - line); + + case '\0': /* nul */ + continue; + + case 0x03: /* ^C - break */ + line[0] = '\0'; /* discard input */ + return (-1); + + case 0x15: /* ^U - erase line */ + while (col > plen) { + puts (erase_seq); + --col; + } + p = line; + n = 0; + continue; + + case 0x17: /* ^W - erase word */ + p=delete_char(line, p, &col, &n, plen); + while ((n > 0) && (*p != ' ')) { + p=delete_char(line, p, &col, &n, plen); + } + continue; + + case 0x08: /* ^H - backspace */ + case 0x7F: /* DEL - backspace */ + p=delete_char(line, p, &col, &n, plen); + continue; + + default: + /* + * Must be a normal character then + */ + if (n < CONFIG_CBSIZE-2) { + if (c == '\t') { /* expand TABs */ +#ifdef CONFIG_AUTO_COMPLETE + /* if auto completion triggered just continue */ + *p = '\0'; + if (cmd_auto_complete(prompt, line, &n, &col)) { + p = line + n; /* reset */ + continue; + } +#endif + puts (tab_seq+(col&07)); + col += 8 - (col&07); + } else { + ++col; /* echo input */ + putc (c); + } + *p++ = c; + ++n; + } else { /* Buffer full */ + putc ('\a'); + } + } + } +} |