iPXE
Functions
editstring.c File Reference

Editable strings. More...

#include <assert.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <ipxe/keys.h>
#include <ipxe/editstring.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static __attribute__ ((nonnull(1)))
 
static int insert_character (struct edit_string *string, unsigned int character)
 Insert character at current cursor position. More...
 
static void delete_character (struct edit_string *string)
 Delete character at current cursor position. More...
 
static void backspace (struct edit_string *string)
 Delete character to left of current cursor position. More...
 
static void previous_word (struct edit_string *string)
 Move to start of previous word. More...
 
static void kill_word (struct edit_string *string)
 Delete to end of previous word. More...
 
static void kill_sol (struct edit_string *string)
 Delete to start of line. More...
 
static void kill_eol (struct edit_string *string)
 Delete to end of line. More...
 
int replace_string (struct edit_string *string, const char *replacement)
 Replace editable string. More...
 
int edit_string (struct edit_string *string, int key)
 Edit editable string. More...
 

Detailed Description

Editable strings.

Definition in file editstring.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ __attribute__()

static __attribute__ ( (nonnull(1))  )
static

Definition at line 40 of file editstring.c.

61  {
62  size_t old_len, max_delete_len, move_len, insert_len, new_len;
63  char *buf;
64  char *tmp;
65 
66  /* Prepare edit history */
67  string->mod_start = string->cursor;
68  string->mod_end = string->cursor;
69 
70  /* Calculate lengths */
71  buf = *(string->buf);
72  old_len = ( buf ? strlen ( buf ) : 0 );
73  assert ( string->cursor <= old_len );
74  max_delete_len = ( old_len - string->cursor );
75  if ( delete_len > max_delete_len )
76  delete_len = max_delete_len;
77  move_len = ( max_delete_len - delete_len );
78  insert_len = ( insert_text ? strlen ( insert_text ) : 0 );
79  new_len = ( old_len - delete_len + insert_len );
80 
81  /* Delete existing text */
82  memmove ( ( buf + string->cursor ),
83  ( buf + string->cursor + delete_len ), move_len );
84 
85  /* Reallocate string, ignoring failures if shrinking */
86  tmp = realloc ( buf, ( new_len + 1 /* NUL */ ) );
87  if ( tmp ) {
88  buf = tmp;
89  *(string->buf) = buf;
90  } else if ( ( new_len > old_len ) || ( ! buf ) ) {
91  return -ENOMEM;
92  }
93 
94  /* Create space for inserted text */
95  memmove ( ( buf + string->cursor + insert_len ),
96  ( buf + string->cursor ), move_len );
97 
98  /* Copy inserted text to cursor position */
99  memcpy ( ( buf + string->cursor ), insert_text, insert_len );
100  string->cursor += insert_len;
101 
102  /* Terminate string */
103  buf[new_len] = '\0';
104 
105  /* Update edit history */
106  string->mod_end = ( ( new_len > old_len ) ? new_len : old_len );
107 
108  return 0;
109 }
uint32_t string
Definition: multiboot.h:14
unsigned long tmp
Definition: linux_pci.h:53
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
void * memmove(void *dest, const void *src, size_t len) __nonnull
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:521

References assert(), ENOMEM, memcpy(), memmove(), realloc(), string, strlen(), and tmp.

◆ insert_character()

static int insert_character ( struct edit_string string,
unsigned int  character 
)
static

Insert character at current cursor position.

Parameters
stringEditable string
characterCharacter to insert
Return values
rcReturn status code

Definition at line 118 of file editstring.c.

119  {
120  char insert_text[2] = { character, '\0' };
121 
122  return insert_delete ( string, 0, insert_text );
123 }

Referenced by edit_string().

◆ delete_character()

static void delete_character ( struct edit_string string)
static

Delete character at current cursor position.

Parameters
stringEditable string

Definition at line 130 of file editstring.c.

130  {
131  int rc;
132 
133  rc = insert_delete ( string, 1, NULL );
134  assert ( ( rc == 0 ) || ( *(string->buf) == NULL ) );
135 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint32_t string
Definition: multiboot.h:14
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References assert(), NULL, rc, and string.

Referenced by backspace(), and edit_string().

◆ backspace()

static void backspace ( struct edit_string string)
static

Delete character to left of current cursor position.

Parameters
stringEditable string

Definition at line 142 of file editstring.c.

142  {
143 
144  if ( string->cursor > 0 ) {
145  string->cursor--;
146  delete_character ( string );
147  }
148 }
uint32_t string
Definition: multiboot.h:14
static void delete_character(struct edit_string *string)
Delete character at current cursor position.
Definition: editstring.c:130

References delete_character(), and string.

Referenced by edit_string().

◆ previous_word()

static void previous_word ( struct edit_string string)
static

Move to start of previous word.

Parameters
stringEditable string

Definition at line 155 of file editstring.c.

155  {
156  const char *buf = *(string->buf);
157  size_t cursor = string->cursor;
158 
159  while ( cursor && isspace ( buf[ cursor - 1 ] ) ) {
160  cursor--;
161  }
162  while ( cursor && ( ! isspace ( buf[ cursor - 1 ] ) ) ) {
163  cursor--;
164  }
165  string->cursor = cursor;
166 }
int isspace(int character)
Check to see if character is a space.
Definition: ctype.c:41

References isspace().

Referenced by kill_word().

◆ kill_word()

static void kill_word ( struct edit_string string)
static

Delete to end of previous word.

Parameters
stringEditable string

Definition at line 173 of file editstring.c.

173  {
174  size_t old_cursor = string->cursor;
175  int rc;
176 
177  previous_word ( string );
178  rc = insert_delete ( string, ( old_cursor - string->cursor ), NULL );
179  assert ( ( rc == 0 ) || ( *(string->buf) == NULL ) );
180 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint32_t string
Definition: multiboot.h:14
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
static void previous_word(struct edit_string *string)
Move to start of previous word.
Definition: editstring.c:155

References assert(), NULL, previous_word(), rc, and string.

Referenced by edit_string().

◆ kill_sol()

static void kill_sol ( struct edit_string string)
static

Delete to start of line.

Parameters
stringEditable string

Definition at line 187 of file editstring.c.

187  {
188  size_t old_cursor = string->cursor;
189  int rc;
190 
191  string->cursor = 0;
192  rc = insert_delete ( string, old_cursor, NULL );
193  assert ( ( rc == 0 ) || ( *(string->buf) == NULL ) );
194 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint32_t string
Definition: multiboot.h:14
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References assert(), NULL, rc, and string.

Referenced by edit_string().

◆ kill_eol()

static void kill_eol ( struct edit_string string)
static

Delete to end of line.

Parameters
stringEditable string

Definition at line 201 of file editstring.c.

201  {
202  int rc;
203 
204  rc = insert_delete ( string, ~( ( size_t ) 0 ), NULL );
205  assert ( ( rc == 0 ) || ( *(string->buf) == NULL ) );
206 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint32_t string
Definition: multiboot.h:14
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References assert(), NULL, rc, and string.

Referenced by edit_string().

◆ replace_string()

int replace_string ( struct edit_string string,
const char *  replacement 
)

Replace editable string.

Parameters
stringEditable string
replacementReplacement string, or NULL to empty the string
Return values
rcReturn status code

Replace the entire content of the editable string and update the edit history to allow the caller to bring the display into sync with the string content.

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

Upon success, the string buffer is guaranteed to be non-NULL (even if the replacement string is NULL or empty).

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 228 of file editstring.c.

228  {
229 
230  string->cursor = 0;
231  return insert_delete ( string, ~( ( size_t ) 0 ), replacement );
232 }
const char * replacement
Definition: editstring.h:53

References replacement.

Referenced by editstring_okx(), and readline_history().

◆ edit_string()

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