Ver código fonte

Start of generic editable string support

tags/v0.9.3
Michael Brown 17 anos atrás
pai
commit
1bc3514bd8
3 arquivos alterados com 182 adições e 1 exclusões
  1. 1
    1
      src/Makefile
  2. 159
    0
      src/hci/editstring.c
  3. 22
    0
      src/include/gpxe/editstring.h

+ 1
- 1
src/Makefile Ver arquivo

@@ -144,7 +144,7 @@ SRCDIRS		+= drivers/bitbash
144 144
 SRCDIRS		+= interface/pxe
145 145
 SRCDIRS		+= tests
146 146
 SRCDIRS		+= crypto
147
-SRCDIRS		+= hci/mucurses
147
+SRCDIRS		+= hci hci/mucurses
148 148
 SRCDIRS		+= commandline
149 149
 SRCDIRS		+= commandline/commands
150 150
 

+ 159
- 0
src/hci/editstring.c Ver arquivo

@@ -0,0 +1,159 @@
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+#include <assert.h>
20
+#include <string.h>
21
+#include <gpxe/editstring.h>
22
+
23
+/** @file
24
+ *
25
+ * Editable strings
26
+ *
27
+ */
28
+
29
+/**
30
+ * Insert and/or delete text within an editable string
31
+ *
32
+ * @v string		Editable string
33
+ * @v delete_len	Length of text to delete from current cursor position
34
+ * @v insert_text	Text to insert at current cursor position, or NULL
35
+ */
36
+static void insert_delete ( struct edit_string *string, size_t delete_len,
37
+			    const char *insert_text ) {
38
+	size_t old_len, max_delete_len, insert_len, max_insert_len;
39
+
40
+	/* Calculate lengths */
41
+	old_len = strlen ( string->buf );
42
+	assert ( string->cursor <= old_len );
43
+	max_delete_len = ( old_len - string->cursor );
44
+	if ( delete_len > max_delete_len )
45
+		delete_len = max_delete_len;
46
+	insert_len = ( insert_text ? strlen ( insert_text ) : 0 );
47
+	max_insert_len = ( ( string->len - 1 ) - ( old_len - delete_len ) );
48
+	if ( insert_len > max_insert_len )
49
+		insert_len = max_insert_len;
50
+
51
+	/* Move data following the cursor */
52
+	memmove ( ( string->buf + string->cursor + insert_len ),
53
+		  ( string->buf + string->cursor + delete_len ),
54
+		  ( max_delete_len + 1 - delete_len ) );
55
+
56
+	/* Copy inserted text to cursor position */
57
+	memcpy ( ( string->buf + string->cursor ), insert_text, insert_len );
58
+	string->cursor += insert_len;
59
+}
60
+
61
+/**
62
+ * Insert character at current cursor position
63
+ *
64
+ * @v string		Editable string
65
+ * @v character		Character to insert
66
+ */
67
+static void insert_character ( struct edit_string *string,
68
+			      unsigned int character ) {
69
+	char insert_text[2] = { character, '\0' };
70
+	insert_delete ( string, 0, insert_text );
71
+}
72
+
73
+/**
74
+ * Delete character at current cursor position
75
+ *
76
+ * @v string		Editable string
77
+ */
78
+static void delete_character ( struct edit_string *string ) {
79
+	insert_delete ( string, 1, NULL );
80
+}
81
+
82
+/**
83
+ * Delete character to left of current cursor position
84
+ *
85
+ * @v string		Editable string
86
+ */
87
+static void backspace ( struct edit_string *string ) {
88
+	if ( string->cursor > 0 ) {
89
+		string->cursor--;
90
+		delete_character ( string );
91
+	}
92
+}
93
+
94
+/**
95
+ * Delete to end of line
96
+ *
97
+ * @v string		Editable string
98
+ */
99
+static void kill_eol ( struct edit_string *string ) {
100
+	insert_delete ( string, ~( ( size_t ) 0 ), NULL );
101
+}
102
+
103
+/**
104
+ * Edit editable string
105
+ *
106
+ * @v string		Editable string
107
+ * @v key		Key pressed by user
108
+ * @ret key		Key returned to application, or zero
109
+ *
110
+ * Handles keypresses and updates the content of the editable string.
111
+ * Basic line editing facilities (delete/insert/cursor) are supported.
112
+ * If edit_string() understands and uses the keypress it will return
113
+ * zero, otherwise it will return the original key.
114
+ *
115
+ * This function does not update the display in any way.
116
+ */
117
+int edit_string ( struct edit_string *string, int key ) {
118
+	int retval = 0;
119
+
120
+	if ( ( key >= 0x20 ) && ( key <= 0x7e ) ) {
121
+		/* Printable character; insert at current position */
122
+		insert_character ( string, key );
123
+	} else switch ( key ) {
124
+	case 0x08: /* Ctrl-H */
125
+		/* Backspace */
126
+		backspace ( string );
127
+		break;
128
+	case 0x04: /* Ctrl-D */
129
+		/* Delete character */
130
+		delete_character ( string );
131
+		break;
132
+	case 0x0b: /* Ctrl-K */
133
+		kill_eol ( string );
134
+		break;
135
+	case 0x01: /* Ctrl-A */
136
+		/* Start of line */
137
+		string->cursor = 0;
138
+		break;
139
+	case 0x05: /* Ctrl-E */
140
+		/* End of line */
141
+		string->cursor = strlen ( string->buf );
142
+		break;
143
+	case 0x02: /* Ctrl-B */
144
+		/* Cursor left */
145
+		if ( string->cursor > 0 )
146
+			string->cursor--;
147
+		break;
148
+	case 0x06: /* Ctrl-F */
149
+		/* Cursor right */
150
+		if ( string->cursor < ( string->len - 1 ) )
151
+			string->cursor++;
152
+		break;
153
+	default:
154
+		retval = key;
155
+		break;
156
+	}
157
+
158
+	return retval;
159
+}

+ 22
- 0
src/include/gpxe/editstring.h Ver arquivo

@@ -0,0 +1,22 @@
1
+#ifndef _EDITSTRING_H
2
+#define _EDITSTRING_H
3
+
4
+/** @file
5
+ *
6
+ * Editable strings
7
+ *
8
+ */
9
+
10
+/** An editable string */
11
+struct edit_string {
12
+	/** Buffer for string */
13
+	char *buf;
14
+	/** Size of buffer (including terminating NUL) */
15
+	size_t len;
16
+	/** Cursor position */
17
+	unsigned int cursor;
18
+};
19
+
20
+extern int edit_string ( struct edit_string *string, int key );
21
+
22
+#endif /* _EDITSTRING_H */

Carregando…
Cancelar
Salvar