iPXE
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...

Macros

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

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
void history_free (struct readline_history *history)
 Free history buffer.
int readline_history (const char *prompt, const char *prefill, struct readline_history *history, unsigned long timeout, 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.

Macro Definition Documentation

◆ READLINE_HISTORY_MAX_DEPTH

#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 29 of file readline.h.

Referenced by history_move().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ history_free()

void history_free ( struct readline_history * history)
extern

Free history buffer.

Parameters
historyHistory buffer

Definition at line 232 of file readline.c.

232 {
233 struct readline_history_entry *entry;
234 unsigned int i;
235
236 /* Discard any temporary strings */
237 for ( i = 0 ; i < ( sizeof ( history->entries ) /
238 sizeof ( history->entries[0] ) ) ; i++ ) {
239 entry = &history->entries[i];
240 assert ( entry->temp == NULL );
241 free ( entry->string );
242 }
243}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
A readline history entry.
Definition readline.h:14
char * temp
Temporary copy of string.
Definition readline.h:22
char * string
Persistent copy of string.
Definition readline.h:16
struct readline_history_entry entries[READLINE_HISTORY_MAX_DEPTH+1]
History entries.
Definition readline.h:39

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

Referenced by shell().

◆ readline_history()

int readline_history ( const char * prompt,
const char * prefill,
struct readline_history * history,
unsigned long timeout,
char ** line )
extern

Read line from console (with history)

Parameters
promptPrompt string
prefillPrefill string, or NULL for no prefill
historyHistory buffer, or NULL for no history
timeoutTimeout period, in ticks (0=indefinite)
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 258 of file readline.c.

260 {
261 struct edit_string string;
262 int key;
263 int move_by;
264 const char *new_string;
265 int rc;
266
267 /* Display prompt, if applicable */
268 if ( prompt )
269 printf ( "%s", prompt );
270
271 /* Ensure cursor is visible */
272 printf ( "\033[?25h" );
273
274 /* Initialise editable string */
275 *line = NULL;
276 memset ( &string, 0, sizeof ( string ) );
277 init_editstring ( &string, line );
278
279 /* Prefill string */
280 if ( ( rc = replace_string ( &string, prefill ) ) != 0 )
281 goto error;
282 sync_console ( &string );
283
284 while ( 1 ) {
285
286 /* Get keypress */
287 key = getkey ( timeout );
288 if ( key < 0 ) {
289 rc = -ETIMEDOUT;
290 goto error;
291 }
292 timeout = 0;
293
294 /* Handle keypress */
295 key = edit_string ( &string, key );
296 sync_console ( &string );
297 move_by = 0;
298 switch ( key ) {
299 case CR:
300 case LF:
301 rc = 0;
302 goto done;
303 case CTRL_C:
304 rc = -ECANCELED;
305 goto error;
306 case KEY_UP:
307 move_by = 1;
308 break;
309 case KEY_DOWN:
310 move_by = -1;
311 break;
312 default:
313 /* Do nothing for unrecognised keys or edit errors */
314 break;
315 }
316
317 /* Handle history movement, if applicable */
318 if ( move_by && history ) {
319 new_string = history_move ( history, move_by, *line );
320 if ( new_string ) {
321 replace_string ( &string, new_string );
322 sync_console ( &string );
323 }
324 }
325 }
326
327 error:
328 free ( *line );
329 *line = NULL;
330 done:
331 putchar ( '\n' );
332 if ( history ) {
333 if ( *line && (*line)[0] )
334 history_append ( history, *line );
335 history_cleanup ( history );
336 }
337 assert ( ( rc == 0 ) ^ ( *line == NULL ) );
338 return rc;
339}
union @162305117151260234136356364136041353210355154177 key
Sense key.
Definition scsi.h:3
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
struct arbelprm_completion_with_error error
Definition arbel.h:1
struct bofm_section_header done
Definition bofm_test.c:46
int putchar(int character)
Write a single character to each console device.
Definition console.c:29
void timeout(int)
int edit_string(struct edit_string *string, int key)
Edit editable string.
Definition editstring.c:256
int replace_string(struct edit_string *string, const char *replacement)
Replace editable string.
Definition editstring.c:229
static __nonnull void init_editstring(struct edit_string *string, char **buf)
Initialise editable string.
Definition editstring.h:47
int getkey(unsigned long timeout)
Get single keypress.
Definition getkey.c:72
#define ETIMEDOUT
Connection timed out.
Definition errno.h:670
#define ECANCELED
Operation canceled.
Definition errno.h:344
void * memset(void *dest, int character, size_t len) __nonnull
#define KEY_DOWN
Down arrow.
Definition keys.h:107
#define LF
Definition keys.h:48
#define CTRL_C
Definition keys.h:21
#define CR
Definition keys.h:49
#define KEY_UP
Up arrow.
Definition keys.h:106
uint32_t string
Definition multiboot.h:2
int prompt(const char *text, unsigned long timeout, int key)
Prompt for keypress.
Definition prompt.c:49
static void sync_console(struct edit_string *string)
Synchronise console with edited string.
Definition readline.c:47
static void history_cleanup(struct readline_history *history)
Clean up history after editing.
Definition readline.c:207
static void history_append(struct readline_history *history, const char *string)
Append new history entry.
Definition readline.c:178
static const char * history_move(struct readline_history *history, int offset, const char *old_string)
Move to new history depth.
Definition readline.c:151
An editable string.
Definition editstring.h:14
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition vsprintf.c:465

References assert, CR, CTRL_C, done, ECANCELED, edit_string(), error, ETIMEDOUT, free, getkey(), history_append(), history_cleanup(), history_move(), init_editstring(), key, KEY_DOWN, KEY_UP, LF, memset(), NULL, printf(), prompt(), putchar(), rc, replace_string(), string, sync_console(), and timeout().

Referenced by readline(), and shell().

◆ readline()

char *__malloc readline ( const char * prompt)
extern

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 350 of file readline.c.

350 {
351 char *line;
352
353 readline_history ( prompt, NULL, NULL, 0, &line );
354 return line;
355}
int readline_history(const char *prompt, const char *prefill, struct readline_history *history, unsigned long timeout, char **line)
Read line from console (with history)
Definition readline.c:258

References NULL, prompt(), and readline_history().