menu: simplify usage for clients
[barebox-mini2440.git] / lib / readline_simple.c
blob9dd21545e2406c9c22d70bc087fc836baa7db284
1 #include <common.h>
2 #include <watchdog.h>
4 static char erase_seq[] = "\b \b"; /* erase sequence */
5 static char tab_seq[] = " "; /* used to expand TABs */
7 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
9 char *s;
11 if (*np == 0) {
12 return (p);
15 if (*(--p) == '\t') { /* will retype the whole line */
16 while (*colp > plen) {
17 puts (erase_seq);
18 (*colp)--;
20 for (s=buffer; s<p; ++s) {
21 if (*s == '\t') {
22 puts (tab_seq+((*colp) & 07));
23 *colp += 8 - ((*colp) & 07);
24 } else {
25 ++(*colp);
26 putchar (*s);
29 } else {
30 puts (erase_seq);
31 (*colp)--;
33 (*np)--;
34 return (p);
38 * Prompt for input and read a line.
39 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
40 * time out when time goes past endtime (timebase time in ticks).
41 * Return: number of read characters
42 * -1 if break
43 * -2 if timed out
45 int readline (const char *prompt, char *line, int len)
47 char *p = line;
48 int n = 0; /* buffer index */
49 int plen = 0; /* prompt length */
50 int col; /* output column cnt */
51 char c;
53 /* print prompt */
54 if (prompt) {
55 plen = strlen (prompt);
56 puts (prompt);
58 col = plen;
60 for (;;) {
61 #ifdef CONFIG_BOOT_RETRY_TIME
62 while (!tstc()) { /* while no incoming data */
63 if (retry_time >= 0 && get_ticks() > endtime)
64 return (-2); /* timed out */
66 #endif
67 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
69 #ifdef CONFIG_SHOW_ACTIVITY
70 while (!tstc()) {
71 extern void show_activity(int arg);
72 show_activity(0);
74 #endif
75 c = getc();
78 * Special character handling
80 switch (c) {
81 case '\r': /* Enter */
82 case '\n':
83 *p = '\0';
84 puts ("\r\n");
85 return (p - line);
87 case '\0': /* nul */
88 continue;
90 case 0x03: /* ^C - break */
91 line[0] = '\0'; /* discard input */
92 return (-1);
94 case 0x15: /* ^U - erase line */
95 while (col > plen) {
96 puts (erase_seq);
97 --col;
99 p = line;
100 n = 0;
101 continue;
103 case 0x17: /* ^W - erase word */
104 p=delete_char(line, p, &col, &n, plen);
105 while ((n > 0) && (*p != ' ')) {
106 p=delete_char(line, p, &col, &n, plen);
108 continue;
110 case 0x08: /* ^H - backspace */
111 case 0x7F: /* DEL - backspace */
112 p=delete_char(line, p, &col, &n, plen);
113 continue;
115 default:
117 * Must be a normal character then
119 if (n < CONFIG_CBSIZE-2) {
120 if (c == '\t') { /* expand TABs */
121 puts (tab_seq+(col&07));
122 col += 8 - (col&07);
123 } else {
124 ++col; /* echo input */
125 putchar (c);
127 *p++ = c;
128 ++n;
129 } else { /* Buffer full */
130 putchar ('\a');
137 * @file
138 * @brief Primitiv Line Parser
141 /** @page readline_parser Primitive Line Parser
143 * There is still a primtive line parser as a alternative to the hush shell
144 * environment available. This is for persons who like the old fashion way of
145 * edititing and command entering.
147 * Enable the "Simple parser" in "General Settings", "Select your shell" to
148 * get back the old console feeling.