iPXE
Functions
editstring.c File Reference

Editable strings. More...

#include <assert.h>
#include <string.h>
#include <ctype.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 void insert_delete (struct edit_string *string, size_t delete_len, const char *insert_text)
 Insert and/or delete text within an editable string. More...
 
static void 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...
 
void 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  )

◆ insert_delete()

static void insert_delete ( struct edit_string string,
size_t  delete_len,
const char *  insert_text 
)
static

Insert and/or delete text within an editable string.

Parameters
stringEditable string
delete_lenLength of text to delete from current cursor position
insert_textText to insert at current cursor position, or NULL

Definition at line 57 of file editstring.c.

58  {
59  size_t old_len, max_delete_len, insert_len, max_insert_len, new_len;
60 
61  /* Calculate lengths */
62  old_len = strlen ( string->buf );
63  assert ( string->cursor <= old_len );
64  max_delete_len = ( old_len - string->cursor );
65  if ( delete_len > max_delete_len )
66  delete_len = max_delete_len;
67  insert_len = ( insert_text ? strlen ( insert_text ) : 0 );
68  max_insert_len = ( ( string->len - 1 ) - ( old_len - delete_len ) );
69  if ( insert_len > max_insert_len )
70  insert_len = max_insert_len;
71  new_len = ( old_len - delete_len + insert_len );
72 
73  /* Fill in edit history */
74  string->mod_start = string->cursor;
75  string->mod_end = ( ( new_len > old_len ) ? new_len : old_len );
76 
77  /* Move data following the cursor */
78  memmove ( ( string->buf + string->cursor + insert_len ),
79  ( string->buf + string->cursor + delete_len ),
80  ( max_delete_len + 1 - delete_len ) );
81 
82  /* Copy inserted text to cursor position */
83  memcpy ( ( string->buf + string->cursor ), insert_text, insert_len );
84  string->cursor += insert_len;
85 }
uint32_t string
Definition: multiboot.h:14
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:228
void * memmove(void *dest, const void *src, size_t len) __nonnull

References assert(), memcpy(), memmove(), string, and strlen().

Referenced by delete_character(), insert_character(), kill_eol(), kill_sol(), kill_word(), and replace_string().

◆ insert_character()

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

Insert character at current cursor position.

Parameters
stringEditable string
characterCharacter to insert

Definition at line 93 of file editstring.c.

94  {
95  char insert_text[2] = { character, '\0' };
96  insert_delete ( string, 0, insert_text );
97 }
static void insert_delete(struct edit_string *string, size_t delete_len, const char *insert_text) __attribute__((nonnull(1)))
Insert and/or delete text within an editable string.
Definition: editstring.c:57

References insert_delete().

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

104  {
105  insert_delete ( string, 1, NULL );
106 }
static void insert_delete(struct edit_string *string, size_t delete_len, const char *insert_text) __attribute__((nonnull(1)))
Insert and/or delete text within an editable string.
Definition: editstring.c:57
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References insert_delete(), and NULL.

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

113  {
114  if ( string->cursor > 0 ) {
115  string->cursor--;
116  delete_character ( string );
117  }
118 }
uint32_t string
Definition: multiboot.h:14
static void delete_character(struct edit_string *string) __nonnull
Delete character at current cursor position.
Definition: editstring.c:104

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

125  {
126  while ( string->cursor &&
127  isspace ( string->buf[ string->cursor - 1 ] ) ) {
128  string->cursor--;
129  }
130  while ( string->cursor &&
131  ( ! isspace ( string->buf[ string->cursor - 1 ] ) ) ) {
132  string->cursor--;
133  }
134 }
uint32_t string
Definition: multiboot.h:14
int isspace(int character)
Check to see if character is a space.
Definition: ctype.c:41

References isspace(), and string.

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

141  {
142  size_t old_cursor = string->cursor;
143  previous_word ( string );
144  insert_delete ( string, ( old_cursor - string->cursor ), NULL );
145 }
uint32_t string
Definition: multiboot.h:14
static void insert_delete(struct edit_string *string, size_t delete_len, const char *insert_text) __attribute__((nonnull(1)))
Insert and/or delete text within an editable string.
Definition: editstring.c:57
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
static void previous_word(struct edit_string *string) __nonnull
Move to start of previous word.
Definition: editstring.c:125

References insert_delete(), NULL, previous_word(), 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 152 of file editstring.c.

152  {
153  size_t old_cursor = string->cursor;
154  string->cursor = 0;
155  insert_delete ( string, old_cursor, NULL );
156 }
static void insert_delete(struct edit_string *string, size_t delete_len, const char *insert_text) __attribute__((nonnull(1)))
Insert and/or delete text within an editable string.
Definition: editstring.c:57
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References insert_delete(), and NULL.

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

163  {
164  insert_delete ( string, ~( ( size_t ) 0 ), NULL );
165 }
static void insert_delete(struct edit_string *string, size_t delete_len, const char *insert_text) __attribute__((nonnull(1)))
Insert and/or delete text within an editable string.
Definition: editstring.c:57
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References insert_delete(), and NULL.

Referenced by edit_string().

◆ replace_string()

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

Replace editable string.

Parameters
stringEditable string
replacementReplacement string

Definition at line 173 of file editstring.c.

173  {
174  string->cursor = 0;
175  insert_delete ( string, ~( ( size_t ) 0 ), replacement );
176 }
static void insert_delete(struct edit_string *string, size_t delete_len, const char *insert_text) __attribute__((nonnull(1)))
Insert and/or delete text within an editable string.
Definition: editstring.c:57

References insert_delete().

Referenced by 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, or zero

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.

This function does not update the display in any way.

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

Definition at line 195 of file editstring.c.

195  {
196  int retval = 0;
197  size_t len = strlen ( string->buf );
198 
199  /* Prepare edit history */
200  string->last_cursor = string->cursor;
201  string->mod_start = string->cursor;
202  string->mod_end = string->cursor;
203 
204  /* Interpret key */
205  if ( ( key >= 0x20 ) && ( key <= 0x7e ) ) {
206  /* Printable character; insert at current position */
207  insert_character ( string, key );
208  } else switch ( key ) {
209  case KEY_BACKSPACE:
210  /* Backspace */
211  backspace ( string );
212  break;
213  case KEY_DC:
214  case CTRL_D:
215  /* Delete character */
216  delete_character ( string );
217  break;
218  case CTRL_W:
219  /* Delete word */
220  kill_word ( string );
221  break;
222  case CTRL_U:
223  /* Delete to start of line */
224  kill_sol ( string );
225  break;
226  case CTRL_K:
227  /* Delete to end of line */
228  kill_eol ( string );
229  break;
230  case KEY_HOME:
231  case CTRL_A:
232  /* Start of line */
233  string->cursor = 0;
234  break;
235  case KEY_END:
236  case CTRL_E:
237  /* End of line */
238  string->cursor = len;
239  break;
240  case KEY_LEFT:
241  case CTRL_B:
242  /* Cursor left */
243  if ( string->cursor > 0 )
244  string->cursor--;
245  break;
246  case KEY_RIGHT:
247  case CTRL_F:
248  /* Cursor right */
249  if ( string->cursor < len )
250  string->cursor++;
251  break;
252  default:
253  retval = key;
254  break;
255  }
256 
257  return retval;
258 }
#define CTRL_B
Definition: keys.h:19
#define CTRL_E
Definition: keys.h:22
uint32_t string
Definition: multiboot.h:14
#define KEY_HOME
Home.
Definition: keys.h:70
static void backspace(struct edit_string *string) __nonnull
Delete character to left of current cursor position.
Definition: editstring.c:113
#define CTRL_K
Definition: keys.h:28
#define CTRL_W
Definition: keys.h:40
#define KEY_END
End.
Definition: keys.h:69
size_t strlen(const char *src)
Get length of string.
Definition: string.c:228
#define CTRL_F
Definition: keys.h:23
static void insert_character(struct edit_string *string, unsigned int character) __nonnull
Insert character at current cursor position.
Definition: editstring.c:93
#define CTRL_A
Definition: keys.h:18
#define CTRL_U
Definition: keys.h:38
static void kill_eol(struct edit_string *string) __nonnull
Delete to end of line.
Definition: editstring.c:163
unsigned long retval
Definition: xen.h:45
#define KEY_DC
Delete.
Definition: keys.h:72
static void kill_sol(struct edit_string *string) __nonnull
Delete to start of line.
Definition: editstring.c:152
uint32_t len
Length.
Definition: ena.h:14
#define KEY_RIGHT
Right arrow.
Definition: keys.h:67
#define CTRL_D
Definition: keys.h:21
#define KEY_BACKSPACE
Definition: keys.h:87
static void kill_word(struct edit_string *string) __nonnull
Delete to end of previous word.
Definition: editstring.c:141
#define KEY_LEFT
Left arrow.
Definition: keys.h:68
union @375 key
Sense key.
Definition: scsi.h:18
static void delete_character(struct edit_string *string) __nonnull
Delete character at current cursor position.
Definition: editstring.c:104

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