iPXE
Data Structures | Defines | Functions
readline.h File Reference

Minmal readline. More...

Go to the source code of this file.

Data Structures

struct  readline_history_entry
 A readline history entry. More...
struct  readline_history
 A readline history buffer. More...

Defines

#define READLINE_HISTORY_MAX_DEPTH   ( ( 1 << 3 ) - 1 )
 Maximum depth of a readline history buffer.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
void history_free (struct readline_history *history)
 Free history buffer.
int readline_history (const char *prompt, const char *prefill, struct readline_history *history, char **line)
 Read line from console (with history)
char *__malloc readline (const char *prompt)
 Read line from console.

Detailed Description

Minmal readline.

Definition in file readline.h.


Define Documentation

#define READLINE_HISTORY_MAX_DEPTH   ( ( 1 << 3 ) - 1 )

Maximum depth of a readline history buffer.

Must be one less than a power of two.

Definition at line 28 of file readline.h.

Referenced by history_move().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
void history_free ( struct readline_history history)

Free history buffer.

Parameters:
historyHistory buffer

Definition at line 232 of file readline.c.

References assert, readline_history::entries, entry, free, NULL, readline_history_entry::string, and readline_history_entry::temp.

Referenced by shell().

                                                       {
        struct readline_history_entry *entry;
        unsigned int i;

        /* Discard any temporary strings */
        for ( i = 0 ; i < ( sizeof ( history->entries ) /
                            sizeof ( history->entries[0] ) ) ; i++ ) {
                entry = &history->entries[i];
                assert ( entry->temp == NULL );
                free ( entry->string );
        }
}
int readline_history ( const char *  prompt,
const char *  prefill,
struct readline_history history,
char **  line 
)

Read line from console (with history)

Parameters:
promptPrompt string
prefillPrefill string, or NULL for no prefill
historyHistory buffer, or NULL for no history
Return values:
lineLine read from console (excluding terminating newline)
rcReturn status code

The returned line is allocated with malloc(); the caller must eventually call free() to release the storage.

Definition at line 257 of file readline.c.

References assert, CR, CTRL_C, done, ECANCELED, edit_string(), ENOMEM, getkey(), history_append(), history_cleanup(), history_move(), init_editstring(), key, KEY_DOWN, KEY_UP, LF, memset(), NULL, printf(), putchar(), rc, READLINE_MAX, replace_string(), strdup(), and sync_console().

Referenced by readline(), and shell().

                                                                       {
        char buf[READLINE_MAX];
        struct edit_string string;
        int key;
        int move_by;
        const char *new_string;
        int rc;

        /* Avoid returning uninitialised data on error */
        *line = NULL;

        /* Display prompt, if applicable */
        if ( prompt )
                printf ( "%s", prompt );

        /* Ensure cursor is visible */
        printf ( "\033[?25h" );

        /* Initialise editable string */
        memset ( &string, 0, sizeof ( string ) );
        init_editstring ( &string, buf, sizeof ( buf ) );
        buf[0] = '\0';

        /* Prefill string, if applicable */
        if ( prefill ) {
                replace_string ( &string, prefill );
                sync_console ( &string );
        }

        while ( 1 ) {
                /* Handle keypress */
                key = edit_string ( &string, getkey ( 0 ) );
                sync_console ( &string );
                move_by = 0;
                switch ( key ) {
                case CR:
                case LF:
                        *line = strdup ( buf );
                        rc = ( ( *line ) ? 0 : -ENOMEM );
                        goto done;
                case CTRL_C:
                        rc = -ECANCELED;
                        goto done;
                case KEY_UP:
                        move_by = 1;
                        break;
                case KEY_DOWN:
                        move_by = -1;
                        break;
                default:
                        /* Do nothing */
                        break;
                }

                /* Handle history movement, if applicable */
                if ( move_by && history ) {
                        new_string = history_move ( history, move_by, buf );
                        if ( new_string ) {
                                replace_string ( &string, new_string );
                                sync_console ( &string );
                        }
                }
        }

 done:
        putchar ( '\n' );
        if ( history ) {
                if ( *line && (*line)[0] )
                        history_append ( history, *line );
                history_cleanup ( history );
        }
        assert ( ( rc == 0 ) ^ ( *line == NULL ) );
        return rc;
}
char* __malloc readline ( const char *  prompt)

Read line from console.

Parameters:
promptPrompt string
Return values:
lineLine 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 342 of file readline.c.

References NULL, and readline_history().

                                       {
        char *line;

        readline_history ( prompt, NULL, NULL, &line );
        return line;
}