iPXE
Data Structures | Macros | 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...
 

Macros

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

Functions

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

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

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ history_free()

void history_free ( struct readline_history history)

Free history buffer.

Parameters
historyHistory buffer

Definition at line 232 of file readline.c.

232  {
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 }
struct readline_history_entry entries[READLINE_HISTORY_MAX_DEPTH+1]
History entries.
Definition: readline.h:38
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
union aes_table_entry entry[256]
Table entries, indexed by S(N)
Definition: aes.c:26
A readline history entry.
Definition: readline.h:13
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References assert(), readline_history::entries, entry, free, and NULL.

Referenced by shell().

◆ readline_history()

int readline_history ( const char *  prompt,
const char *  prefill,
struct readline_history history,
unsigned long  timeout,
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
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  char *buf;
263  int key;
264  int move_by;
265  const char *new_string;
266  int rc;
267 
268  /* Avoid returning uninitialised data on error */
269  *line = NULL;
270 
271  /* Display prompt, if applicable */
272  if ( prompt )
273  printf ( "%s", prompt );
274 
275  /* Ensure cursor is visible */
276  printf ( "\033[?25h" );
277 
278  /* Allocate buffer and initialise editable string */
279  buf = zalloc ( READLINE_MAX );
280  if ( ! buf ) {
281  rc = -ENOMEM;
282  goto done;
283  }
284  memset ( &string, 0, sizeof ( string ) );
285  init_editstring ( &string, buf, READLINE_MAX );
286 
287  /* Prefill string, if applicable */
288  if ( prefill ) {
289  replace_string ( &string, prefill );
290  sync_console ( &string );
291  }
292 
293  while ( 1 ) {
294 
295  /* Get keypress */
296  key = getkey ( timeout );
297  if ( key < 0 ) {
298  rc = -ETIMEDOUT;
299  goto done;
300  }
301  timeout = 0;
302 
303  /* Handle keypress */
304  key = edit_string ( &string, key );
305  sync_console ( &string );
306  move_by = 0;
307  switch ( key ) {
308  case CR:
309  case LF:
310  /* Shrink string (ignoring failures) */
311  *line = realloc ( buf,
312  ( strlen ( buf ) + 1 /* NUL */ ) );
313  if ( ! *line )
314  *line = buf;
315  buf = NULL;
316  rc = 0;
317  goto done;
318  case CTRL_C:
319  rc = -ECANCELED;
320  goto done;
321  case KEY_UP:
322  move_by = 1;
323  break;
324  case KEY_DOWN:
325  move_by = -1;
326  break;
327  default:
328  /* Do nothing */
329  break;
330  }
331 
332  /* Handle history movement, if applicable */
333  if ( move_by && history ) {
334  new_string = history_move ( history, move_by, buf );
335  if ( new_string ) {
336  replace_string ( &string, new_string );
337  sync_console ( &string );
338  }
339  }
340  }
341 
342  done:
343  putchar ( '\n' );
344  free ( buf );
345  if ( history ) {
346  if ( *line && (*line)[0] )
347  history_append ( history, *line );
348  history_cleanup ( history );
349  }
350  assert ( ( rc == 0 ) ^ ( *line == NULL ) );
351  return rc;
352 }
int getkey(unsigned long timeout)
Get single keypress.
Definition: getkey.c:71
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
static const char * history_move(struct readline_history *history, int offset, const char *old_string)
Move to new history depth.
Definition: readline.c:151
void replace_string(struct edit_string *string, const char *replacement)
Replace editable string.
Definition: editstring.c:173
static void history_cleanup(struct readline_history *history)
Clean up history after editing.
Definition: readline.c:207
int edit_string(struct edit_string *string, int key)
Edit editable string.
Definition: editstring.c:195
uint32_t string
Definition: multiboot.h:14
#define ECANCELED
Operation canceled.
Definition: errno.h:343
#define KEY_DOWN
Down arrow.
Definition: keys.h:66
#define ENOMEM
Not enough space.
Definition: errno.h:534
#define KEY_UP
Up arrow.
Definition: keys.h:65
static void sync_console(struct edit_string *string)
Synchronise console with edited string.
Definition: readline.c:48
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static void init_editstring(struct edit_string *string, char *buf, size_t len)
Initialise editable string.
Definition: editstring.h:38
#define CTRL_C
Definition: keys.h:20
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
int prompt(const char *text, unsigned long timeout, int key)
Prompt for keypress.
Definition: prompt.c:48
#define LF
Definition: keys.h:47
static void history_append(struct readline_history *history, const char *string)
Append new history entry.
Definition: readline.c:178
An editable string.
Definition: editstring.h:13
#define CR
Definition: keys.h:48
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:521
void timeout(int)
#define READLINE_MAX
Definition: readline.c:41
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
int putchar(int character)
Write a single character to each console device.
Definition: console.c:28
#define ETIMEDOUT
Connection timed out.
Definition: errno.h:669
struct bofm_section_header done
Definition: bofm_test.c:46
union @382 key
Sense key.
Definition: scsi.h:18
void * memset(void *dest, int character, size_t len) __nonnull
char * buf
Buffer for string.
Definition: editstring.h:15

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

Referenced by readline(), and shell().

◆ readline()

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

363  {
364  char *line;
365 
366  readline_history ( prompt, NULL, NULL, 0, &line );
367  return line;
368 }
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
int prompt(const char *text, unsigned long timeout, int key)
Prompt for keypress.
Definition: prompt.c:48
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

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