123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
-
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <console.h>
- #include <gpxe/keys.h>
- #include <gpxe/editstring.h>
- #include <readline/readline.h>
-
-
-
- #define READLINE_MAX 256
-
- static void sync_console ( struct edit_string *string ) __nonnull;
-
-
- static void sync_console ( struct edit_string *string ) {
- unsigned int mod_start = string->mod_start;
- unsigned int mod_end = string->mod_end;
- unsigned int cursor = string->last_cursor;
- size_t len = strlen ( string->buf );
-
-
- if ( mod_start > string->last_cursor )
- mod_start = string->last_cursor;
-
-
- if ( mod_end < string->cursor )
- mod_end = string->cursor;
-
-
- while ( cursor > mod_start ) {
- putchar ( '\b' );
- cursor--;
- }
-
-
- while ( cursor < mod_end ) {
- putchar ( ( cursor >= len ) ? ' ' : string->buf[cursor] );
- cursor++;
- }
-
-
- while ( cursor > string->cursor ) {
- putchar ( '\b' );
- cursor--;
- }
- }
-
-
- char * readline ( const char *prompt ) {
- char buf[READLINE_MAX];
- struct edit_string string;
- int key;
- char *line;
-
- if ( prompt )
- printf ( "%s", prompt );
-
- memset ( &string, 0, sizeof ( string ) );
- string.buf = buf;
- string.len = sizeof ( buf );
- buf[0] = '\0';
-
- while ( 1 ) {
- key = edit_string ( &string, getkey() );
- sync_console ( &string );
- switch ( key ) {
- case CR:
- case LF:
- putchar ( '\n' );
- line = strdup ( buf );
- if ( ! line )
- printf ( "Out of memory\n" );
- return line;
- case CTRL_C:
- putchar ( '\n' );
- return NULL;
- default:
-
- break;
- }
- }
- }
|