2005-08-27 21:24:26 +00:00
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2005-11-08 02:13:30 +00:00
|
|
|
|
#define __NO_INLINE__ /* to avoid inline putchar() */
|
2005-08-27 21:24:26 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include "console.h"
|
|
|
|
|
|
2005-09-02 23:53:02 +00:00
|
|
|
|
#define LEFT_ARROW() if (pos > 0) \
|
|
|
|
|
{ \
|
|
|
|
|
putchar('\b'); \
|
|
|
|
|
pos--; \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define RIGHT_ARROW() if (pos < l) \
|
|
|
|
|
{ \
|
|
|
|
|
putchar(s[pos]);\
|
|
|
|
|
pos++; \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define DELETE() if (pos < l) \
|
|
|
|
|
{ \
|
|
|
|
|
strcpy(s + pos, s + pos + 1); \
|
|
|
|
|
l--; \
|
|
|
|
|
console_cursor_save(); \
|
|
|
|
|
printf("%s", s + pos); \
|
|
|
|
|
putchar(' '); \
|
|
|
|
|
console_cursor_restore(); \
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-27 21:24:26 +00:00
|
|
|
|
void cli_edit(char *s, int length)
|
|
|
|
|
{
|
|
|
|
|
int l = strlen(s);
|
|
|
|
|
int pos = l;
|
2005-09-05 19:36:12 +00:00
|
|
|
|
int c;
|
2005-08-27 21:24:26 +00:00
|
|
|
|
int i;
|
|
|
|
|
|
2005-09-02 23:53:02 +00:00
|
|
|
|
console_cursor_off();
|
2005-08-27 21:24:26 +00:00
|
|
|
|
printf("%s", s);
|
2005-09-02 23:53:02 +00:00
|
|
|
|
console_cursor_on();
|
2005-08-27 21:24:26 +00:00
|
|
|
|
|
|
|
|
|
while ((c = console_getchar()) != '\r')
|
|
|
|
|
{
|
2005-09-02 23:53:02 +00:00
|
|
|
|
retry:
|
2005-09-05 19:36:12 +00:00
|
|
|
|
if (c == 0)
|
|
|
|
|
continue;
|
2005-08-27 21:24:26 +00:00
|
|
|
|
if ( (c > 0x1f) && (c < 0x7f) && (l < length - 1) )
|
|
|
|
|
{
|
|
|
|
|
for (i = l; i > pos; i--)
|
|
|
|
|
s[i] = s[i - 1];
|
|
|
|
|
s[pos] = c;
|
2005-08-28 21:42:18 +00:00
|
|
|
|
putchar(c);
|
2005-08-27 21:24:26 +00:00
|
|
|
|
pos++;
|
2005-08-28 21:42:18 +00:00
|
|
|
|
l++;
|
|
|
|
|
console_cursor_save();
|
|
|
|
|
printf("%s", s + pos);
|
|
|
|
|
console_cursor_restore();
|
2005-08-27 21:24:26 +00:00
|
|
|
|
}
|
|
|
|
|
else switch(c)
|
|
|
|
|
{
|
2005-09-02 23:53:02 +00:00
|
|
|
|
case '':
|
2005-09-05 19:36:12 +00:00
|
|
|
|
while ((c = console_getchar()) == 0);
|
|
|
|
|
if ( c != '[' )
|
2005-09-02 23:53:02 +00:00
|
|
|
|
goto retry;
|
2005-09-05 19:36:12 +00:00
|
|
|
|
while ((c = console_getchar()) == 0);
|
|
|
|
|
switch(c)
|
2005-08-27 21:24:26 +00:00
|
|
|
|
{
|
2005-09-02 23:53:02 +00:00
|
|
|
|
case 'D':
|
|
|
|
|
LEFT_ARROW();
|
|
|
|
|
break;
|
|
|
|
|
case 'C':
|
|
|
|
|
RIGHT_ARROW();
|
|
|
|
|
break;
|
|
|
|
|
case '3':
|
|
|
|
|
if (console_getchar() == '~')
|
|
|
|
|
DELETE();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
goto retry;
|
2005-08-27 21:24:26 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
2005-09-02 23:53:02 +00:00
|
|
|
|
case '':
|
|
|
|
|
LEFT_ARROW();
|
|
|
|
|
break;
|
2005-08-27 21:24:26 +00:00
|
|
|
|
case '':
|
2005-09-02 23:53:02 +00:00
|
|
|
|
RIGHT_ARROW();
|
2005-08-27 21:24:26 +00:00
|
|
|
|
break;
|
|
|
|
|
case '\b': /* backspace */
|
|
|
|
|
if (pos > 0)
|
|
|
|
|
{
|
|
|
|
|
putchar('\b');
|
|
|
|
|
pos--;
|
2005-08-28 21:42:18 +00:00
|
|
|
|
l--;
|
2005-08-27 21:24:26 +00:00
|
|
|
|
strcpy(s + pos, s + pos + 1);
|
2005-08-28 21:42:18 +00:00
|
|
|
|
console_cursor_save();
|
2005-08-27 21:24:26 +00:00
|
|
|
|
printf("%s", s + pos);
|
|
|
|
|
putchar(' ');
|
2005-08-28 21:42:18 +00:00
|
|
|
|
console_cursor_restore();
|
2005-08-27 21:24:26 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 0x7f: /* Delete */
|
2005-09-02 23:53:02 +00:00
|
|
|
|
DELETE();
|
2005-08-27 21:24:26 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|