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 FILE_SECBOOT ( PERMITTED );
26 
27 #include <assert.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <ipxe/keys.h>
33 #include <ipxe/editstring.h>
34 
35 /** @file
36  *
37  * Editable strings
38  *
39  */
40 
41 static __attribute__ (( nonnull ( 1 ) )) int
42 insert_delete ( struct edit_string *string, size_t delete_len,
43  const char *insert_text );
44 static __nonnull int insert_character ( struct edit_string *string,
45  unsigned int character );
46 static __nonnull void delete_character ( struct edit_string *string );
47 static __nonnull void backspace ( struct edit_string *string );
48 static __nonnull void previous_word ( struct edit_string *string );
49 static __nonnull void kill_word ( struct edit_string *string );
50 static __nonnull void kill_sol ( struct edit_string *string );
51 static __nonnull void kill_eol ( struct edit_string *string );
52 
53 /**
54  * Insert and/or delete text within an editable string
55  *
56  * @v string Editable string
57  * @v delete_len Length of text to delete from current cursor position
58  * @v insert_text Text to insert at current cursor position, or NULL
59  * @ret rc Return status code
60  */
61 static int insert_delete ( struct edit_string *string, size_t delete_len,
62  const char *insert_text ) {
63  size_t old_len, max_delete_len, move_len, insert_len, new_len;
64  char *buf;
65  char *tmp;
66 
67  /* Prepare edit history */
68  string->mod_start = string->cursor;
69  string->mod_end = string->cursor;
70 
71  /* Calculate lengths */
72  buf = *(string->buf);
73  old_len = ( buf ? strlen ( buf ) : 0 );
74  assert ( string->cursor <= old_len );
75  max_delete_len = ( old_len - string->cursor );
76  if ( delete_len > max_delete_len )
77  delete_len = max_delete_len;
78  move_len = ( max_delete_len - delete_len );
79  insert_len = ( insert_text ? strlen ( insert_text ) : 0 );
80  new_len = ( old_len - delete_len + insert_len );
81 
82  /* Delete existing text */
83  memmove ( ( buf + string->cursor ),
84  ( buf + string->cursor + delete_len ), move_len );
85 
86  /* Reallocate string, ignoring failures if shrinking */
87  tmp = realloc ( buf, ( new_len + 1 /* NUL */ ) );
88  if ( tmp ) {
89  buf = tmp;
90  *(string->buf) = buf;
91  } else if ( ( new_len > old_len ) || ( ! buf ) ) {
92  return -ENOMEM;
93  }
94 
95  /* Create space for inserted text */
96  memmove ( ( buf + string->cursor + insert_len ),
97  ( buf + string->cursor ), move_len );
98 
99  /* Copy inserted text to cursor position */
100  memcpy ( ( buf + string->cursor ), insert_text, insert_len );
101  string->cursor += insert_len;
102 
103  /* Terminate string */
104  buf[new_len] = '\0';
105 
106  /* Update edit history */
107  string->mod_end = ( ( new_len > old_len ) ? new_len : old_len );
108 
109  return 0;
110 }
111 
112 /**
113  * Insert character at current cursor position
114  *
115  * @v string Editable string
116  * @v character Character to insert
117  * @ret rc Return status code
118  */
119 static int insert_character ( struct edit_string *string,
120  unsigned int character ) {
121  char insert_text[2] = { character, '\0' };
122 
123  return insert_delete ( string, 0, insert_text );
124 }
125 
126 /**
127  * Delete character at current cursor position
128  *
129  * @v string Editable string
130  */
131 static void delete_character ( struct edit_string *string ) {
132  int rc;
133 
134  rc = insert_delete ( string, 1, NULL );
135  assert ( ( rc == 0 ) || ( *(string->buf) == NULL ) );
136 }
137 
138 /**
139  * Delete character to left of current cursor position
140  *
141  * @v string Editable string
142  */
143 static void backspace ( struct edit_string *string ) {
144 
145  if ( string->cursor > 0 ) {
146  string->cursor--;
147  delete_character ( string );
148  }
149 }
150 
151 /**
152  * Move to start of previous word
153  *
154  * @v string Editable string
155  */
156 static void previous_word ( struct edit_string *string ) {
157  const char *buf = *(string->buf);
158  size_t cursor = string->cursor;
159 
160  while ( cursor && isspace ( buf[ cursor - 1 ] ) ) {
161  cursor--;
162  }
163  while ( cursor && ( ! isspace ( buf[ cursor - 1 ] ) ) ) {
164  cursor--;
165  }
166  string->cursor = cursor;
167 }
168 
169 /**
170  * Delete to end of previous word
171  *
172  * @v string Editable string
173  */
174 static void kill_word ( struct edit_string *string ) {
175  size_t old_cursor = string->cursor;
176  int rc;
177 
178  previous_word ( string );
179  rc = insert_delete ( string, ( old_cursor - string->cursor ), NULL );
180  assert ( ( rc == 0 ) || ( *(string->buf) == NULL ) );
181 }
182 
183 /**
184  * Delete to start of line
185  *
186  * @v string Editable string
187  */
188 static void kill_sol ( struct edit_string *string ) {
189  size_t old_cursor = string->cursor;
190  int rc;
191 
192  string->cursor = 0;
193  rc = insert_delete ( string, old_cursor, NULL );
194  assert ( ( rc == 0 ) || ( *(string->buf) == NULL ) );
195 }
196 
197 /**
198  * Delete to end of line
199  *
200  * @v string Editable string
201  */
202 static void kill_eol ( struct edit_string *string ) {
203  int rc;
204 
205  rc = insert_delete ( string, ~( ( size_t ) 0 ), NULL );
206  assert ( ( rc == 0 ) || ( *(string->buf) == NULL ) );
207 }
208 
209 /**
210  * Replace editable string
211  *
212  * @v string Editable string
213  * @v replacement Replacement string, or NULL to empty the string
214  * @ret rc Return status code
215  *
216  * Replace the entire content of the editable string and update the
217  * edit history to allow the caller to bring the display into sync
218  * with the string content.
219  *
220  * This function does not itself update the display in any way.
221  *
222  * Upon success, the string buffer is guaranteed to be non-NULL (even
223  * if the replacement string is NULL or empty).
224  *
225  * Errors may safely be ignored if it is deemed that subsequently
226  * failing to update the display will provide sufficient feedback to
227  * the user.
228  */
229 int replace_string ( struct edit_string *string, const char *replacement ) {
230 
231  string->cursor = 0;
232  return insert_delete ( string, ~( ( size_t ) 0 ), replacement );
233 }
234 
235 /**
236  * Edit editable string
237  *
238  * @v string Editable string
239  * @v key Key pressed by user
240  * @ret key Key returned to application, zero, or negative error
241  *
242  * Handles keypresses and updates the content of the editable string.
243  * Basic line editing facilities (delete/insert/cursor) are supported.
244  * If edit_string() understands and uses the keypress it will return
245  * zero, otherwise it will return the original key.
246  *
247  * The string's edit history will be updated to allow the caller to
248  * efficiently bring the display into sync with the string content.
249  *
250  * This function does not itself update the display in any way.
251  *
252  * Errors may safely be ignored if it is deemed that subsequently
253  * failing to update the display will provide sufficient feedback to
254  * the user.
255  */
256 int edit_string ( struct edit_string *string, int key ) {
257  const char *buf = *(string->buf);
258  size_t len = ( buf ? strlen ( buf ) : 0 );
259  int retval = 0;
260 
261  /* Prepare edit history */
262  string->last_cursor = string->cursor;
263  string->mod_start = string->cursor;
264  string->mod_end = string->cursor;
265 
266  /* Interpret key */
267  if ( ( key >= 0x20 ) && ( key <= 0x7e ) ) {
268  /* Printable character; insert at current position */
269  retval = insert_character ( string, key );
270  } else switch ( key ) {
271  case KEY_BACKSPACE:
272  /* Backspace */
273  backspace ( string );
274  break;
275  case KEY_DC:
276  case CTRL_D:
277  /* Delete character */
278  delete_character ( string );
279  break;
280  case CTRL_W:
281  /* Delete word */
282  kill_word ( string );
283  break;
284  case CTRL_U:
285  /* Delete to start of line */
286  kill_sol ( string );
287  break;
288  case CTRL_K:
289  /* Delete to end of line */
290  kill_eol ( string );
291  break;
292  case KEY_HOME:
293  case CTRL_A:
294  /* Start of line */
295  string->cursor = 0;
296  break;
297  case KEY_END:
298  case CTRL_E:
299  /* End of line */
300  string->cursor = len;
301  break;
302  case KEY_LEFT:
303  case CTRL_B:
304  /* Cursor left */
305  if ( string->cursor > 0 )
306  string->cursor--;
307  break;
308  case KEY_RIGHT:
309  case CTRL_F:
310  /* Cursor right */
311  if ( string->cursor < len )
312  string->cursor++;
313  break;
314  default:
315  retval = key;
316  break;
317  }
318 
319  return retval;
320 }
#define CTRL_B
Definition: keys.h:20
Editable strings.
#define CTRL_E
Definition: keys.h:23
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:229
static __attribute__((nonnull(1)))
Definition: editstring.c:41
Error codes.
int edit_string(struct edit_string *string, int key)
Edit editable string.
Definition: editstring.c:256
static int insert_character(struct edit_string *string, unsigned int character)
Insert character at current cursor position.
Definition: editstring.c:119
uint32_t string
Definition: multiboot.h:14
Character types.
#define KEY_HOME
Home.
Definition: keys.h:111
unsigned long tmp
Definition: linux_pci.h:65
static void backspace(struct edit_string *string)
Delete character to left of current cursor position.
Definition: editstring.c:143
#define ENOMEM
Not enough space.
Definition: errno.h:535
#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:29
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
const char * replacement
Definition: editstring.h:54
ring len
Length.
Definition: dwmac.h:231
FILE_SECBOOT(PERMITTED)
int isspace(int character)
Check to see if character is a space.
Definition: ctype.c:42
#define CTRL_W
Definition: keys.h:41
#define KEY_END
End.
Definition: keys.h:110
size_t strlen(const char *src)
Get length of string.
Definition: string.c:244
#define CTRL_F
Definition: keys.h:24
void * memmove(void *dest, const void *src, size_t len) __nonnull
#define CTRL_A
Definition: keys.h:19
#define CTRL_U
Definition: keys.h:39
static void kill_eol(struct edit_string *string)
Delete to end of line.
Definition: editstring.c:202
unsigned long retval
Definition: xen.h:46
#define KEY_DC
Delete.
Definition: keys.h:113
static void kill_sol(struct edit_string *string)
Delete to start of line.
Definition: editstring.c:188
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
Key definitions.
An editable string.
Definition: editstring.h:14
#define KEY_RIGHT
Right arrow.
Definition: keys.h:108
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:607
#define CTRL_D
Definition: keys.h:22
#define KEY_BACKSPACE
Definition: keys.h:128
static void kill_word(struct edit_string *string)
Delete to end of previous word.
Definition: editstring.c:174
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
String functions.
static void previous_word(struct edit_string *string)
Move to start of previous word.
Definition: editstring.c:156
#define KEY_LEFT
Left arrow.
Definition: keys.h:109
union @391 key
Sense key.
Definition: scsi.h:18
static void delete_character(struct edit_string *string)
Delete character at current cursor position.
Definition: editstring.c:131