readline.c File Reference

Minimal readline. More...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <console.h>
#include <gpxe/keys.h>
#include <gpxe/editstring.h>
#include <readline/readline.h>

Go to the source code of this file.

Defines

#define READLINE_MAX   256

Functions

 FILE_LICENCE (GPL2_OR_LATER)
static void sync_console (struct edit_string *string)
 Synchronise console with edited string.
char * readline (const char *prompt)
 Read line from console.


Detailed Description

Minimal readline.

Definition in file readline.c.


Define Documentation

#define READLINE_MAX   256

Definition at line 35 of file readline.c.

Referenced by readline().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER   ) 

static void sync_console ( struct edit_string string  )  [static]

Synchronise console with edited string.

Parameters:
string Editable string

Definition at line 44 of file readline.c.

References edit_string::buf, edit_string::cursor, edit_string::last_cursor, putchar(), and strlen().

Referenced by readline().

00044                                                         {
00045         unsigned int mod_start = string->mod_start;
00046         unsigned int mod_end = string->mod_end;
00047         unsigned int cursor = string->last_cursor;
00048         size_t len = strlen ( string->buf );
00049 
00050         /* Expand region back to old cursor position if applicable */
00051         if ( mod_start > string->last_cursor )
00052                 mod_start = string->last_cursor;
00053 
00054         /* Expand region forward to new cursor position if applicable */
00055         if ( mod_end < string->cursor )
00056                 mod_end = string->cursor;
00057 
00058         /* Backspace to start of region */
00059         while ( cursor > mod_start ) {
00060                 putchar ( '\b' );
00061                 cursor--;
00062         }
00063 
00064         /* Print modified region */
00065         while ( cursor < mod_end ) {
00066                 putchar ( ( cursor >= len ) ? ' ' : string->buf[cursor] );
00067                 cursor++;
00068         }
00069 
00070         /* Backspace to new cursor position */
00071         while ( cursor > string->cursor ) {
00072                 putchar ( '\b' );
00073                 cursor--;
00074         }
00075 }

char* readline ( const char *  prompt  ) 

Read line from console.

Parameters:
prompt Prompt string
Return values:
line Line read from console (excluding terminating newline)
The returned line is allocated with malloc(); the caller must eventually call free() to release the storage.

Definition at line 86 of file readline.c.

References CR, CTRL_C, edit_string(), getkey(), LF, memset(), NULL, printf(), putchar(), READLINE_MAX, strdup(), and sync_console().

Referenced by shell().

00086                                        {
00087         char buf[READLINE_MAX];
00088         struct edit_string string;
00089         int key;
00090         char *line;
00091 
00092         if ( prompt )
00093                 printf ( "%s", prompt );
00094 
00095         memset ( &string, 0, sizeof ( string ) );
00096         string.buf = buf;
00097         string.len = sizeof ( buf );
00098         buf[0] = '\0';
00099 
00100         while ( 1 ) {
00101                 key = edit_string ( &string, getkey() );
00102                 sync_console ( &string );
00103                 switch ( key ) {
00104                 case CR:
00105                 case LF:
00106                         putchar ( '\n' );
00107                         line = strdup ( buf );
00108                         if ( ! line )
00109                                 printf ( "Out of memory\n" );
00110                         return line;
00111                 case CTRL_C:
00112                         putchar ( '\n' );
00113                         return NULL;
00114                 default:
00115                         /* Do nothing */
00116                         break;
00117                 }
00118         }
00119 }


Generated on Tue Apr 6 20:01:47 2010 for gPXE by  doxygen 1.5.7.1