iPXE
editstring.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <assert.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <stdlib.h>
31 #include <ipxe/keys.h>
32 #include <ipxe/editstring.h>
33 
34 /** @file
35  *
36  * Editable strings
37  *
38  */
39 
40 static __attribute__ (( nonnull ( 1 ) )) int
41 insert_delete ( struct edit_string *string, size_t delete_len,
42  const char *insert_text );
43 static __nonnull int insert_character ( struct edit_string *string,
44  unsigned int character );
45 static __nonnull void delete_character ( struct edit_string *string );
46 static __nonnull void backspace ( struct edit_string *string );
47 static __nonnull void previous_word ( struct edit_string *string );
48 static __nonnull void kill_word ( struct edit_string *string );
49 static __nonnull void kill_sol ( struct edit_string *string );
50 static __nonnull void kill_eol ( struct edit_string *string );
51 
52 /**
53  * Insert and/or delete text within an editable string
54  *
55  * @v string Editable string
56  * @v delete_len Length of text to delete from current cursor position
57  * @v insert_text Text to insert at current cursor position, or NULL
58  * @ret rc Return status code
59  */
60 static int insert_delete ( struct edit_string *string, size_t delete_len,
61  const char *insert_text ) {
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 }
110 
111 /**
112  * Insert character at current cursor position
113  *
114  * @v string Editable string
115  * @v character Character to insert
116  * @ret rc Return status code
117  */
118 static int insert_character ( struct edit_string *string,
119  unsigned int character ) {
120  char insert_text[2] = { character, '\0' };
121 
122  return insert_delete ( string, 0, insert_text );
123 }
124 
125 /**
126  * Delete character at current cursor position
127  *
128  * @v string Editable string
129  */
130 static void delete_character ( struct edit_string *string ) {
131  int rc;
132 
133  rc = insert_delete ( string, 1, NULL );
134  assert ( ( rc == 0 ) || ( *(string->buf) == NULL ) );
135 }
136 
137 /**
138  * Delete character to left of current cursor position
139  *
140  * @v string Editable string
141  */
142 static void backspace ( struct edit_string *string ) {
143 
144  if ( string->cursor > 0 ) {
145  string->cursor--;
146  delete_character ( string );
147  }
148 }
149 
150 /**
151  * Move to start of previous word
152  *
153  * @v string Editable string
154  */
155 static void previous_word ( struct edit_string *string ) {
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 }
167 
168 /**
169  * Delete to end of previous word
170  *
171  * @v string Editable string
172  */
173 static void kill_word ( struct edit_string *string ) {
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 }
181 
182 /**
183  * Delete to start of line
184  *
185  * @v string Editable string
186  */
187 static void kill_sol ( struct edit_string *string ) {
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 }
195 
196 /**
197  * Delete to end of line
198  *
199  * @v string Editable string
200  */
201 static void kill_eol ( struct edit_string *string ) {
202  int rc;
203 
204  rc = insert_delete ( string, ~( ( size_t ) 0 ), NULL );
205  assert ( ( rc == 0 ) || ( *(string->buf) == NULL ) );
206 }
207 
208 /**
209  * Replace editable string
210  *
211  * @v string Editable string
212  * @v replacement Replacement string, or NULL to empty the string
213  * @ret rc Return status code
214  *
215  * Replace the entire content of the editable string and update the
216  * edit history to allow the caller to bring the display into sync
217  * with the string content.
218  *
219  * This function does not itself update the display in any way.
220  *
221  * Upon success, the string buffer is guaranteed to be non-NULL (even
222  * if the replacement string is NULL or empty).
223  *
224  * Errors may safely be ignored if it is deemed that subsequently
225  * failing to update the display will provide sufficient feedback to
226  * the user.
227  */
228 int replace_string ( struct edit_string *string, const char *replacement ) {
229 
230  string->cursor = 0;
231  return insert_delete ( string, ~( ( size_t ) 0 ), replacement );
232 }
233 
234 /**
235  * Edit editable string
236  *
237  * @v string Editable string
238  * @v key Key pressed by user
239  * @ret key Key returned to application, zero, or negative error
240  *
241  * Handles keypresses and updates the content of the editable string.
242  * Basic line editing facilities (delete/insert/cursor) are supported.
243  * If edit_string() understands and uses the keypress it will return
244  * zero, otherwise it will return the original key.
245  *
246  * The string's edit history will be updated to allow the caller to
247  * efficiently bring the display into sync with the string content.
248  *
249  * This function does not itself update the display in any way.
250  *
251  * Errors may safely be ignored if it is deemed that subsequently
252  * failing to update the display will provide sufficient feedback to
253  * the user.
254  */
255 int edit_string ( struct edit_string *string, int key ) {
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
Editable strings.
#define CTRL_E
Definition: keys.h:22
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int replace_string(struct edit_string *string, const char *replacement)
Replace editable string.
Definition: editstring.c:228
static __attribute__((nonnull(1)))
Definition: editstring.c:40
Error codes.
int edit_string(struct edit_string *string, int key)
Edit editable string.
Definition: editstring.c:255
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
Character types.
#define KEY_HOME
Home.
Definition: keys.h:109
unsigned long tmp
Definition: linux_pci.h:53
static void backspace(struct edit_string *string)
Delete character to left of current cursor position.
Definition: editstring.c:142
#define ENOMEM
Not enough space.
Definition: errno.h:534
#define __nonnull
Declare a function's pointer parameters as non-null - i.e.
Definition: compiler.h:592
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define CTRL_K
Definition: keys.h:28
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
const char * replacement
Definition: editstring.h:53
int isspace(int character)
Check to see if character is a space.
Definition: ctype.c:41
#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
void * memmove(void *dest, const void *src, size_t len) __nonnull
#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
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
uint32_t len
Length.
Definition: ena.h:14
Key definitions.
An editable string.
Definition: editstring.h:13
#define KEY_RIGHT
Right arrow.
Definition: keys.h:106
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:521
#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 NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
static void previous_word(struct edit_string *string)
Move to start of previous word.
Definition: editstring.c:155
#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