aboutsummaryrefslogtreecommitdiff
path: root/src/hci/editstring.c
blob: b83916c6ec357a170133b55dfab9efb05db62ca3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

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

/** @file
 *
 * Editable strings
 *
 */

static __attribute__ (( nonnull ( 1 ) )) int
insert_delete ( struct edit_string *string, size_t delete_len,
		const char *insert_text );
static __nonnull int insert_character ( struct edit_string *string,
					unsigned int character );
static __nonnull void delete_character ( struct edit_string *string );
static __nonnull void backspace ( struct edit_string *string );
static __nonnull void previous_word ( struct edit_string *string );
static __nonnull void kill_word ( struct edit_string *string );
static __nonnull void kill_sol ( struct edit_string *string );
static __nonnull void kill_eol ( struct edit_string *string );

/**
 * Insert and/or delete text within an editable string
 *
 * @v string		Editable string
 * @v delete_len	Length of text to delete from current cursor position
 * @v insert_text	Text to insert at current cursor position, or NULL
 * @ret rc		Return status code
 */
static int insert_delete ( struct edit_string *string, size_t delete_len,
			   const char *insert_text ) {
	size_t old_len, max_delete_len, insert_len, new_len;
	char *buf;
	char *tmp;

	/* Calculate lengths */
	buf = *(string->buf);
	old_len = ( buf ? strlen ( buf ) : 0 );
	assert ( string->cursor <= old_len );
	max_delete_len = ( old_len - string->cursor );
	if ( delete_len > max_delete_len )
		delete_len = max_delete_len;
	insert_len = ( insert_text ? strlen ( insert_text ) : 0 );
	new_len = ( old_len - delete_len + insert_len );

	/* Reallocate string, ignoring failures if shrinking */
	tmp = realloc ( buf, ( new_len + 1 /* NUL */ ) );
	if ( tmp ) {
		buf = tmp;
		*(string->buf) = buf;
	} else if ( new_len > old_len ) {
		return -ENOMEM;
	}

	/* Fill in edit history */
	string->mod_start = string->cursor;
	string->mod_end = ( ( new_len > old_len ) ? new_len : old_len );

	/* Move data following the cursor */
	memmove ( ( buf + string->cursor + insert_len ),
		  ( buf + string->cursor + delete_len ),
		  ( max_delete_len - delete_len ) );

	/* Copy inserted text to cursor position */
	memcpy ( ( buf + string->cursor ), insert_text, insert_len );
	string->cursor += insert_len;

	/* Terminate string (if not NULL) */
	if ( buf ) {
		buf[new_len] = '\0';
	} else {
		assert ( old_len == 0 );
		assert ( insert_len == 0 );
		assert ( new_len == 0 );
	}

	return 0;
}

/**
 * Insert character at current cursor position
 *
 * @v string		Editable string
 * @v character		Character to insert
 * @ret rc		Return status code
 */
static int insert_character ( struct edit_string *string,
			      unsigned int character ) {
	char insert_text[2] = { character, '\0' };

	return insert_delete ( string, 0, insert_text );
}

/**
 * Delete character at current cursor position
 *
 * @v string		Editable string
 */
static void delete_character ( struct edit_string *string ) {
	int rc;

	rc = insert_delete ( string, 1, NULL );
	assert ( ( rc == 0 ) || ( *(string->buf) == NULL ) );
}

/**
 * Delete character to left of current cursor position
 *
 * @v string		Editable string
 */
static void backspace ( struct edit_string *string ) {

	if ( string->cursor > 0 ) {
		string->cursor--;
		delete_character ( string );
	}
}

/**
 * Move to start of previous word
 *
 * @v string		Editable string
 */
static void previous_word ( struct edit_string *string ) {
	const char *buf = *(string->buf);
	size_t cursor = string->cursor;

	while ( cursor && isspace ( buf[ cursor - 1 ] ) ) {
		cursor--;
	}
	while ( cursor && ( ! isspace ( buf[ cursor - 1 ] ) ) ) {
		cursor--;
	}
	string->cursor = cursor;
}

/**
 * Delete to end of previous word
 *
 * @v string		Editable string
 */
static void kill_word ( struct edit_string *string ) {
	size_t old_cursor = string->cursor;
	int rc;

	previous_word ( string );
	rc = insert_delete ( string, ( old_cursor - string->cursor ), NULL );
	assert ( ( rc == 0 ) || ( *(string->buf) == NULL ) );
}

/**
 * Delete to start of line
 *
 * @v string		Editable string
 */
static void kill_sol ( struct edit_string *string ) {
	size_t old_cursor = string->cursor;
	int rc;

	string->cursor = 0;
	rc = insert_delete ( string, old_cursor, NULL );
	assert ( ( rc == 0 ) || ( *(string->buf) == NULL ) );
}

/**
 * Delete to end of line
 *
 * @v string		Editable string
 */
static void kill_eol ( struct edit_string *string ) {
	int rc;

	rc = insert_delete ( string, ~( ( size_t ) 0 ), NULL );
	assert ( ( rc == 0 ) || ( *(string->buf) == NULL ) );
}

/**
 * Replace editable string
 *
 * @v string		Editable string
 * @v replacement	Replacement string, or NULL to empty the string
 * @ret rc		Return 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.
 */
int replace_string ( struct edit_string *string, const char *replacement ) {

	string->cursor = 0;
	return insert_delete ( string, ~( ( size_t ) 0 ), replacement );
}

/**
 * Edit editable string
 *
 * @v string		Editable string
 * @v key		Key pressed by user
 * @ret key		Key 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.
 */
int edit_string ( struct edit_string *string, int key ) {
	const char *buf = *(string->buf);
	size_t len = ( buf ? strlen ( buf ) : 0 );
	int retval = 0;

	/* Prepare edit history */
	string->last_cursor = string->cursor;
	string->mod_start = string->cursor;
	string->mod_end = string->cursor;

	/* Interpret key */
	if ( ( key >= 0x20 ) && ( key <= 0x7e ) ) {
		/* Printable character; insert at current position */
		retval = insert_character ( string, key );
	} else switch ( key ) {
	case KEY_BACKSPACE:
		/* Backspace */
		backspace ( string );
		break;
	case KEY_DC:
	case CTRL_D:
		/* Delete character */
		delete_character ( string );
		break;
	case CTRL_W:
		/* Delete word */
		kill_word ( string );
		break;
	case CTRL_U:
		/* Delete to start of line */
		kill_sol ( string );
		break;
	case CTRL_K:
		/* Delete to end of line */
		kill_eol ( string );
		break;
	case KEY_HOME:
	case CTRL_A:
		/* Start of line */
		string->cursor = 0;
		break;
	case KEY_END:
	case CTRL_E:
		/* End of line */
		string->cursor = len;
		break;
	case KEY_LEFT:
	case CTRL_B:
		/* Cursor left */
		if ( string->cursor > 0 )
			string->cursor--;
		break;
	case KEY_RIGHT:
	case CTRL_F:
		/* Cursor right */
		if ( string->cursor < len )
			string->cursor++;
		break;
	default:
		retval = key;
		break;
	}

	return retval;
}