iPXE
Data Structures | Functions | Variables
editstring.h File Reference

Editable strings. More...

Go to the source code of this file.

Data Structures

struct  edit_string
 An editable string. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static __nonnull void init_editstring (struct edit_string *string, char **buf)
 Initialise editable string. More...
 
 __attribute__ ((nonnull(1))) int replace_string(struct edit_string *string
 
__nonnull int edit_string (struct edit_string *string, int key)
 Edit editable string. More...
 

Variables

const char * replacement
 

Detailed Description

Editable strings.

Definition in file editstring.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ init_editstring()

static __nonnull void init_editstring ( struct edit_string string,
char **  buf 
)
inlinestatic

Initialise editable string.

Parameters
stringEditable string
bufDynamically allocated string buffer

The buf parameter must be the address of a caller-provided pointer to a NUL-terminated string allocated using malloc() (or equivalent, such as strdup()). Any edits made to the string will realloc() the string buffer as needed.

The caller may choose leave the initial string buffer pointer as NULL, in which case it will be allocated upon the first attempt to insert a character into the buffer. If the caller does this, then it must be prepared to find the pointer still NULL after editing, since the user may never attempt to insert any characters.

Definition at line 46 of file editstring.h.

47  {
48 
49  string->buf = buf;
50 }

Referenced by editstring_okx(), init_editbox(), and readline_history().

◆ __attribute__()

__attribute__ ( (nonnull(1))  )

◆ edit_string()

__nonnull int edit_string ( struct edit_string string,
int  key 
)

Edit editable string.

Parameters
stringEditable string
keyKey pressed by user
Return values
keyKey returned to application, zero, or negative error

Handles keypresses and updates the content of the editable string. Basic line editing facilities (delete/insert/cursor) are supported. If edit_string() understands and uses the keypress it will return zero, otherwise it will return the original key.

The string's edit history will be updated to allow the caller to efficiently bring the display into sync with the string content.

This function does not itself update the display in any way.

Errors may safely be ignored if it is deemed that subsequently failing to update the display will provide sufficient feedback to the user.

Definition at line 255 of file editstring.c.

255  {
256  const char *buf = *(string->buf);
257  size_t len = ( buf ? strlen ( buf ) : 0 );
258  int retval = 0;
259 
260  /* Prepare edit history */
261  string->last_cursor = string->cursor;
262  string->mod_start = string->cursor;
263  string->mod_end = string->cursor;
264 
265  /* Interpret key */
266  if ( ( key >= 0x20 ) && ( key <= 0x7e ) ) {
267  /* Printable character; insert at current position */
268  retval = insert_character ( string, key );
269  } else switch ( key ) {
270  case KEY_BACKSPACE:
271  /* Backspace */
272  backspace ( string );
273  break;
274  case KEY_DC:
275  case CTRL_D:
276  /* Delete character */
277  delete_character ( string );
278  break;
279  case CTRL_W:
280  /* Delete word */
281  kill_word ( string );
282  break;
283  case CTRL_U:
284  /* Delete to start of line */
285  kill_sol ( string );
286  break;
287  case CTRL_K:
288  /* Delete to end of line */
289  kill_eol ( string );
290  break;
291  case KEY_HOME:
292  case CTRL_A:
293  /* Start of line */
294  string->cursor = 0;
295  break;
296  case KEY_END:
297  case CTRL_E:
298  /* End of line */
299  string->cursor = len;
300  break;
301  case KEY_LEFT:
302  case CTRL_B:
303  /* Cursor left */
304  if ( string->cursor > 0 )
305  string->cursor--;
306  break;
307  case KEY_RIGHT:
308  case CTRL_F:
309  /* Cursor right */
310  if ( string->cursor < len )
311  string->cursor++;
312  break;
313  default:
314  retval = key;
315  break;
316  }
317 
318  return retval;
319 }
#define CTRL_B
Definition: keys.h:19
#define CTRL_E
Definition: keys.h:22
static int insert_character(struct edit_string *string, unsigned int character)
Insert character at current cursor position.
Definition: editstring.c:118
uint32_t string
Definition: multiboot.h:14
#define KEY_HOME
Home.
Definition: keys.h:109
static void backspace(struct edit_string *string)
Delete character to left of current cursor position.
Definition: editstring.c:142
#define CTRL_K
Definition: keys.h:28
#define CTRL_W
Definition: keys.h:40
#define KEY_END
End.
Definition: keys.h:108
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
#define CTRL_F
Definition: keys.h:23
#define CTRL_A
Definition: keys.h:18
#define CTRL_U
Definition: keys.h:38
static void kill_eol(struct edit_string *string)
Delete to end of line.
Definition: editstring.c:201
unsigned long retval
Definition: xen.h:45
#define KEY_DC
Delete.
Definition: keys.h:111
static void kill_sol(struct edit_string *string)
Delete to start of line.
Definition: editstring.c:187
uint32_t len
Length.
Definition: ena.h:14
#define KEY_RIGHT
Right arrow.
Definition: keys.h:106
#define CTRL_D
Definition: keys.h:21
#define KEY_BACKSPACE
Definition: keys.h:126
static void kill_word(struct edit_string *string)
Delete to end of previous word.
Definition: editstring.c:173
#define KEY_LEFT
Left arrow.
Definition: keys.h:107
union @382 key
Sense key.
Definition: crypto.h:284
static void delete_character(struct edit_string *string)
Delete character at current cursor position.
Definition: editstring.c:130

References backspace(), CTRL_A, CTRL_B, CTRL_D, CTRL_E, CTRL_F, CTRL_K, CTRL_U, CTRL_W, delete_character(), insert_character(), key, KEY_BACKSPACE, KEY_DC, KEY_END, KEY_HOME, KEY_LEFT, KEY_RIGHT, kill_eol(), kill_sol(), kill_word(), len, retval, string, and strlen().

Referenced by edit_editbox(), and readline_history().

Variable Documentation

◆ replacement

const char* replacement

Definition at line 53 of file editstring.h.

Referenced by image_exec(), image_replace(), myri10ge_net_poll(), and replace_string().