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

231  {
233  unsigned int i;
234 
235  /* Discard any temporary strings */
236  for ( i = 0 ; i < ( sizeof ( history->entries ) /
237  sizeof ( history->entries[0] ) ) ; i++ ) {
238  entry = &history->entries[i];
239  assert ( entry->temp == NULL );
240  free ( entry->string );
241  }
242 }
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 257 of file readline.c.

259  {
260  struct edit_string string;
261  int key;
262  int move_by;
263  const char *new_string;
264  int rc;
265 
266  /* Display prompt, if applicable */
267  if ( prompt )
268  printf ( "%s", prompt );
269 
270  /* Ensure cursor is visible */
271  printf ( "\033[?25h" );
272 
273  /* Initialise editable string */
274  *line = NULL;
275  memset ( &string, 0, sizeof ( string ) );
276  init_editstring ( &string, line );
277 
278  /* Prefill string */
279  if ( ( rc = replace_string ( &string, prefill ) ) != 0 )
280  goto error;
281  sync_console ( &string );
282 
283  while ( 1 ) {
284 
285  /* Get keypress */
286  key = getkey ( timeout );
287  if ( key < 0 ) {
288  rc = -ETIMEDOUT;
289  goto error;
290  }
291  timeout = 0;
292 
293  /* Handle keypress */
294  key = edit_string ( &string, key );
295  sync_console ( &string );
296  move_by = 0;
297  switch ( key ) {
298  case CR:
299  case LF:
300  rc = 0;
301  goto done;
302  case CTRL_C:
303  rc = -ECANCELED;
304  goto error;
305  case KEY_UP:
306  move_by = 1;
307  break;
308  case KEY_DOWN:
309  move_by = -1;
310  break;
311  default:
312  /* Do nothing for unrecognised keys or edit errors */
313  break;
314  }
315 
316  /* Handle history movement, if applicable */
317  if ( move_by && history ) {
318  new_string = history_move ( history, move_by, *line );
319  if ( new_string ) {
320  replace_string ( &string, new_string );
321  sync_console ( &string );
322  }
323  }
324  }
325 
326  error:
327  free ( *line );
328  *line = NULL;
329  done:
330  putchar ( '\n' );
331  if ( history ) {
332  if ( *line && (*line)[0] )
333  history_append ( history, *line );
334  history_cleanup ( history );
335  }
336  assert ( ( rc == 0 ) ^ ( *line == NULL ) );
337  return rc;
338 }
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
int replace_string(struct edit_string *string, const char *replacement)
Replace editable string.
Definition: editstring.c:228
struct arbelprm_completion_with_error error
Definition: arbel.h:12
static const char * history_move(struct readline_history *history, int offset, const char *old_string)
Move to new history depth.
Definition: readline.c:150
static void history_cleanup(struct readline_history *history)
Clean up history after editing.
Definition: readline.c:206
int edit_string(struct edit_string *string, int key)
Edit editable string.
Definition: editstring.c:255
uint32_t string
Definition: multiboot.h:14
#define ECANCELED
Operation canceled.
Definition: errno.h:343
#define KEY_DOWN
Down arrow.
Definition: keys.h:105
#define KEY_UP
Up arrow.
Definition: keys.h:104
static void sync_console(struct edit_string *string)
Synchronise console with edited string.
Definition: readline.c:46
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static __nonnull void init_editstring(struct edit_string *string, char **buf)
Initialise editable string.
Definition: editstring.h:46
#define CTRL_C
Definition: keys.h:20
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
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:177
An editable string.
Definition: editstring.h:13
#define CR
Definition: keys.h:48
void timeout(int)
#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: crypto.h:284
void * memset(void *dest, int character, size_t len) __nonnull

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)

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

349  {
350  char *line;
351 
352  readline_history ( prompt, NULL, NULL, 0, &line );
353  return line;
354 }
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:257
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().