Browse Source

Skeletal (non-echoing) version of readline()

tags/v0.9.3
Michael Brown 18 years ago
parent
commit
b3c535e550
2 changed files with 80 additions and 0 deletions
  1. 68
    0
      src/hci/readline.c
  2. 12
    0
      src/include/readline/readline.h

+ 68
- 0
src/hci/readline.c View File

@@ -0,0 +1,68 @@
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 <string.h>
20
+#include <malloc.h>
21
+#include <console.h>
22
+#include <gpxe/editstring.h>
23
+#include <readline/readline.h>
24
+
25
+/** @file
26
+ *
27
+ * Minimal readline
28
+ *
29
+ */
30
+
31
+#define READLINE_MAX 256
32
+
33
+/**
34
+ * Read line from console
35
+ *
36
+ * @v prompt		Prompt string
37
+ * @ret line		Line read from console (excluding terminating newline)
38
+ *
39
+ * The returned line is allocated with malloc(); the caller must
40
+ * eventually call free() to release the storage.
41
+ */
42
+char * readline ( const char *prompt ) {
43
+	char buf[READLINE_MAX];
44
+	struct edit_string string = {
45
+		.buf = buf,
46
+		.len = sizeof ( buf ),
47
+		.cursor = 0,
48
+	};
49
+	int key;
50
+
51
+	if ( prompt )
52
+		printf ( "%s", prompt );
53
+
54
+	buf[0] = '\0';
55
+	while ( 1 ) {
56
+		key = edit_string ( &string, getchar() );
57
+		switch ( key ) {
58
+		case 0x0d: /* Carriage return */
59
+		case 0x0a: /* Line feed */
60
+			return ( strdup ( buf ) );
61
+		case 0x03: /* Ctrl-C */
62
+			return NULL;
63
+		default:
64
+			/* Do nothing */
65
+			break;
66
+		}
67
+	}
68
+}

+ 12
- 0
src/include/readline/readline.h View File

@@ -0,0 +1,12 @@
1
+#ifndef _READLINE_H
2
+#define _READLINE_H
3
+
4
+/** @file
5
+ *
6
+ * Minmal readline
7
+ *
8
+ */
9
+
10
+extern char * readline ( const char *prompt );
11
+
12
+#endif /* _READLINE_H */

Loading…
Cancel
Save