2007-09-17 22:56:40 +00:00
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* (c) 2005-2007 Laurent Vivier <Laurent@lvivier.info>
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define __NO_INLINE__ /* to avoid inline putchar() */
|
|
|
|
|
#include <stdio.h>
|
2007-09-17 23:27:28 +00:00
|
|
|
|
#include <malloc.h>
|
2007-09-17 22:56:40 +00:00
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
2007-09-17 23:27:28 +00:00
|
|
|
|
#include "libui.h"
|
2007-09-17 22:56:40 +00:00
|
|
|
|
|
|
|
|
|
#define LEFT_ARROW() if (pos > 0) \
|
|
|
|
|
{ \
|
|
|
|
|
putchar('\b'); \
|
|
|
|
|
pos--; \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define RIGHT_ARROW() if (pos < l) \
|
|
|
|
|
{ \
|
2007-09-17 23:27:28 +00:00
|
|
|
|
putchar(buf[pos]);\
|
2007-09-17 22:56:40 +00:00
|
|
|
|
pos++; \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define DELETE() if (pos < l) \
|
|
|
|
|
{ \
|
2007-09-17 23:27:28 +00:00
|
|
|
|
strcpy(buf + pos, buf + pos + 1);\
|
2007-09-17 22:56:40 +00:00
|
|
|
|
l--; \
|
|
|
|
|
console_cursor_save(); \
|
2007-09-17 23:27:28 +00:00
|
|
|
|
printf("%s", buf + pos); \
|
2007-09-17 22:56:40 +00:00
|
|
|
|
putchar(' '); \
|
|
|
|
|
console_cursor_restore(); \
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-17 23:27:28 +00:00
|
|
|
|
void emile_edit(char *line, int length)
|
2007-09-17 22:56:40 +00:00
|
|
|
|
{
|
2007-09-17 23:27:28 +00:00
|
|
|
|
int l = strlen(line);
|
2007-09-17 22:56:40 +00:00
|
|
|
|
int pos = l;
|
|
|
|
|
int c;
|
|
|
|
|
int i;
|
2007-09-17 23:27:28 +00:00
|
|
|
|
int saved_wait = wait_char;
|
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
|
|
buf = malloc(length);
|
|
|
|
|
memcpy(buf, line, length);
|
2007-09-17 22:56:40 +00:00
|
|
|
|
|
|
|
|
|
console_cursor_off();
|
2007-09-17 23:27:28 +00:00
|
|
|
|
printf("%s", buf);
|
2007-09-17 22:56:40 +00:00
|
|
|
|
console_cursor_on();
|
|
|
|
|
|
2007-09-17 23:27:28 +00:00
|
|
|
|
wait_char = 1;
|
|
|
|
|
while (1)
|
2007-09-17 22:56:40 +00:00
|
|
|
|
{
|
2007-09-17 23:27:28 +00:00
|
|
|
|
console_keypressed(0);
|
|
|
|
|
c = console_getchar();
|
|
|
|
|
if (c == '\r')
|
|
|
|
|
{
|
|
|
|
|
strcpy(line, buf);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2007-09-17 22:56:40 +00:00
|
|
|
|
retry:
|
|
|
|
|
if ( (c > 0x1f) && (c < 0x7f) && (l < length - 1) )
|
|
|
|
|
{
|
|
|
|
|
for (i = l; i > pos; i--)
|
2007-09-17 23:27:28 +00:00
|
|
|
|
buf[i] = buf[i - 1];
|
|
|
|
|
buf[pos] = c;
|
2007-09-17 22:56:40 +00:00
|
|
|
|
putchar(c);
|
|
|
|
|
pos++;
|
|
|
|
|
l++;
|
|
|
|
|
console_cursor_save();
|
2007-09-17 23:27:28 +00:00
|
|
|
|
printf("%s", buf + pos);
|
2007-09-17 22:56:40 +00:00
|
|
|
|
console_cursor_restore();
|
|
|
|
|
}
|
|
|
|
|
else switch(c)
|
|
|
|
|
{
|
|
|
|
|
case '':
|
2007-09-17 23:27:28 +00:00
|
|
|
|
c = console_getchar();
|
2007-09-17 22:56:40 +00:00
|
|
|
|
if ( c != '[' )
|
2007-09-17 23:27:28 +00:00
|
|
|
|
goto exit;
|
|
|
|
|
c = console_getchar();
|
2007-09-17 22:56:40 +00:00
|
|
|
|
switch(c)
|
|
|
|
|
{
|
|
|
|
|
case 'D':
|
|
|
|
|
LEFT_ARROW();
|
|
|
|
|
break;
|
|
|
|
|
case 'C':
|
|
|
|
|
RIGHT_ARROW();
|
|
|
|
|
break;
|
|
|
|
|
case '3':
|
|
|
|
|
if (console_getchar() == '~')
|
|
|
|
|
DELETE();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
goto retry;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '':
|
|
|
|
|
LEFT_ARROW();
|
|
|
|
|
break;
|
|
|
|
|
case '':
|
|
|
|
|
RIGHT_ARROW();
|
|
|
|
|
break;
|
|
|
|
|
case '\b': /* backspace */
|
|
|
|
|
if (pos > 0)
|
|
|
|
|
{
|
|
|
|
|
putchar('\b');
|
|
|
|
|
pos--;
|
|
|
|
|
l--;
|
2007-09-17 23:27:28 +00:00
|
|
|
|
strcpy(buf + pos, buf + pos + 1);
|
2007-09-17 22:56:40 +00:00
|
|
|
|
console_cursor_save();
|
2007-09-17 23:27:28 +00:00
|
|
|
|
printf("%s", buf + pos);
|
2007-09-17 22:56:40 +00:00
|
|
|
|
putchar(' ');
|
|
|
|
|
console_cursor_restore();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 0x7f: /* Delete */
|
|
|
|
|
DELETE();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-09-17 23:27:28 +00:00
|
|
|
|
exit:
|
|
|
|
|
wait_char = saved_wait;
|
|
|
|
|
free(buf);
|
2007-09-17 22:56:40 +00:00
|
|
|
|
}
|