iPXE
Functions
readline.c File Reference

Minimal readline. More...

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

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static void sync_console (struct edit_string *string)
 Synchronise console with edited string. More...
 
static struct readline_history_entryhistory_entry (struct readline_history *history, unsigned int depth)
 Locate history entry. More...
 
static const char * history_fetch (struct readline_history *history, unsigned int depth)
 Read string from history buffer. More...
 
static void history_store (struct readline_history *history, unsigned int depth, const char *string)
 Write temporary string copy to history buffer. More...
 
static const char * history_move (struct readline_history *history, int offset, const char *old_string)
 Move to new history depth. More...
 
static void history_append (struct readline_history *history, const char *string)
 Append new history entry. More...
 
static void history_cleanup (struct readline_history *history)
 Clean up history after editing. More...
 
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 * readline (const char *prompt)
 Read line from console. More...
 

Detailed Description

Minimal readline.

Definition in file readline.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ sync_console()

static void sync_console ( struct edit_string string)
static

Synchronise console with edited string.

Parameters
stringEditable string

Definition at line 46 of file readline.c.

46  {
47  unsigned int mod_start = string->mod_start;
48  unsigned int mod_end = string->mod_end;
49  unsigned int cursor = string->last_cursor;
50  const char *buf = *(string->buf);
51  size_t len = strlen ( buf );
52 
53  /* Expand region back to old cursor position if applicable */
54  if ( mod_start > string->last_cursor )
55  mod_start = string->last_cursor;
56 
57  /* Expand region forward to new cursor position if applicable */
58  if ( mod_end < string->cursor )
59  mod_end = string->cursor;
60 
61  /* Backspace to start of region */
62  while ( cursor > mod_start ) {
63  putchar ( '\b' );
64  cursor--;
65  }
66 
67  /* Print modified region */
68  while ( cursor < mod_end ) {
69  putchar ( ( cursor >= len ) ? ' ' : buf[cursor] );
70  cursor++;
71  }
72 
73  /* Backspace to new cursor position */
74  while ( cursor > string->cursor ) {
75  putchar ( '\b' );
76  cursor--;
77  }
78 }
uint32_t mod_start
Definition: multiboot.h:12
uint32_t mod_end
Definition: multiboot.h:13
uint32_t string
Definition: multiboot.h:14
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
uint32_t len
Length.
Definition: ena.h:14
int putchar(int character)
Write a single character to each console device.
Definition: console.c:28

References len, mod_end, mod_start, putchar(), string, and strlen().

Referenced by readline_history().

◆ history_entry()

static struct readline_history_entry* history_entry ( struct readline_history history,
unsigned int  depth 
)
static

Locate history entry.

Parameters
historyHistory buffer
depthDepth within history buffer
Return values
entryHistory entry

Definition at line 88 of file readline.c.

88  {
89  unsigned int offset;
90 
91  offset = ( ( history->next - depth ) %
92  ( sizeof ( history->entries ) /
93  sizeof ( history->entries[0] ) ) );
94  return &history->entries[offset];
95 }
struct readline_history_entry entries[READLINE_HISTORY_MAX_DEPTH+1]
History entries.
Definition: readline.h:38
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
unsigned int next
Position of next entry within buffer.
Definition: readline.h:44

References readline_history::entries, readline_history::next, and offset.

Referenced by history_append(), history_cleanup(), history_fetch(), and history_store().

◆ history_fetch()

static const char* history_fetch ( struct readline_history history,
unsigned int  depth 
)
static

Read string from history buffer.

Parameters
historyHistory buffer
depthDepth within history buffer
Return values
stringString

Definition at line 104 of file readline.c.

105  {
107 
108  /* Return the temporary copy if it exists, otherwise return
109  * the persistent copy.
110  */
111  entry = history_entry ( history, depth );
112  return ( entry->temp ? entry->temp : entry->string );
113 }
static struct readline_history_entry * history_entry(struct readline_history *history, unsigned int depth)
Locate history entry.
Definition: readline.c:88
union aes_table_entry entry[256]
Table entries, indexed by S(N)
Definition: aes.c:26
A readline history entry.
Definition: readline.h:13

References entry, and history_entry().

Referenced by history_move().

◆ history_store()

static void history_store ( struct readline_history history,
unsigned int  depth,
const char *  string 
)
static

Write temporary string copy to history buffer.

Parameters
historyHistory buffer
depthDepth within history buffer
stringString

Definition at line 122 of file readline.c.

123  {
125  char *temp;
126 
127  /* Create temporary copy of string */
128  temp = strdup ( string );
129  if ( ! temp ) {
130  /* Just discard the string; there's nothing we can do */
131  DBGC ( history, "READLINE %p could not store string\n",
132  history );
133  return;
134  }
135 
136  /* Store temporary copy */
137  entry = history_entry ( history, depth );
138  free ( entry->temp );
139  entry->temp = temp;
140 }
static struct readline_history_entry * history_entry(struct readline_history *history, unsigned int depth)
Locate history entry.
Definition: readline.c:88
#define DBGC(...)
Definition: compiler.h:505
char * temp
Temporary copy of string.
Definition: readline.h:21
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
char * strdup(const char *src)
Duplicate string.
Definition: string.c:380
A readline history entry.
Definition: readline.h:13

References DBGC, entry, free, history_entry(), strdup(), and readline_history_entry::temp.

Referenced by history_move().

◆ history_move()

static const char* history_move ( struct readline_history history,
int  offset,
const char *  old_string 
)
static

Move to new history depth.

Parameters
historyHistory buffer
offsetOffset by which to change depth
old_stringString (possibly modified) at current depth
Return values
new_stringString at new depth, or NULL for no movement

Definition at line 150 of file readline.c.

151  {
152  unsigned int new_depth = ( history->depth + offset );
153  const char * new_string = history_fetch ( history, new_depth );
154 
155  /* Depth checks */
156  if ( new_depth > READLINE_HISTORY_MAX_DEPTH )
157  return NULL;
158  if ( ! new_string )
159  return NULL;
160 
161  /* Store temporary copy of old string at current depth */
162  history_store ( history, history->depth, old_string );
163 
164  /* Update depth */
165  history->depth = new_depth;
166 
167  /* Return new string */
168  return new_string;
169 }
unsigned int depth
Current depth within history buffer.
Definition: readline.h:49
static void history_store(struct readline_history *history, unsigned int depth, const char *string)
Write temporary string copy to history buffer.
Definition: readline.c:122
#define READLINE_HISTORY_MAX_DEPTH
Maximum depth of a readline history buffer.
Definition: readline.h:28
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
static const char * history_fetch(struct readline_history *history, unsigned int depth)
Read string from history buffer.
Definition: readline.c:104
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References readline_history::depth, history_fetch(), history_store(), NULL, offset, and READLINE_HISTORY_MAX_DEPTH.

Referenced by readline_history().

◆ history_append()

static void history_append ( struct readline_history history,
const char *  string 
)
static

Append new history entry.

Parameters
historyHistory buffer
stringString

Definition at line 177 of file readline.c.

178  {
180 
181  /* Store new entry */
182  entry = history_entry ( history, 0 );
183  assert ( entry->string == NULL );
184  entry->string = strdup ( string );
185  if ( ! entry->string ) {
186  /* Just discard the string; there's nothing we can do */
187  DBGC ( history, "READLINE %p could not append string\n",
188  history );
189  return;
190  }
191 
192  /* Increment history position */
193  history->next++;
194 
195  /* Prepare empty "next" slot */
196  entry = history_entry ( history, 0 );
197  free ( entry->string );
198  entry->string = NULL;
199 }
static struct readline_history_entry * history_entry(struct readline_history *history, unsigned int depth)
Locate history entry.
Definition: readline.c:88
#define DBGC(...)
Definition: compiler.h:505
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
unsigned int next
Position of next entry within buffer.
Definition: readline.h:44
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
char * strdup(const char *src)
Duplicate string.
Definition: string.c:380
A readline history entry.
Definition: readline.h:13
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References assert(), DBGC, entry, free, history_entry(), readline_history::next, NULL, and strdup().

Referenced by readline_history().

◆ history_cleanup()

static void history_cleanup ( struct readline_history history)
static

Clean up history after editing.

Parameters
historyHistory buffer

Definition at line 206 of file readline.c.

206  {
208  unsigned int i;
209 
210  /* Discard any temporary strings */
211  for ( i = 0 ; i < ( sizeof ( history->entries ) /
212  sizeof ( history->entries[0] ) ) ; i++ ) {
213  entry = &history->entries[i];
214  free ( entry->temp );
215  entry->temp = NULL;
216  }
217 
218  /* Reset depth */
219  history->depth = 0;
220 
221  /* Sanity check */
222  entry = history_entry ( history, 0 );
223  assert ( entry->string == NULL );
224 }
unsigned int depth
Current depth within history buffer.
Definition: readline.h:49
static struct readline_history_entry * history_entry(struct readline_history *history, unsigned int depth)
Locate history entry.
Definition: readline.c:88
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::depth, readline_history::entries, entry, free, history_entry(), and NULL.

Referenced by readline_history().

◆ 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* 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().