xref: /minix/sys/arch/i386/stand/lib/menuutils.c (revision 0a6a1f1d)
1 /*	$NetBSD: menuutils.c,v 1.4 2014/04/06 19:11:26 jakllsch Exp $	*/
2 
3 /*
4  * Copyright (c) 1996, 1997
5  * 	Matthias Drochner.  All rights reserved.
6  * Copyright (c) 1996, 1997
7  * 	Perry E. Metzger.  All rights reserved.
8  * Copyright (c) 1997
9  *	Jason R. Thorpe.  All rights reserved
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgements:
21  *	This product includes software developed for the NetBSD Project
22  *	by Matthias Drochner.
23  *	This product includes software developed for the NetBSD Project
24  *	by Perry E. Metzger.
25  * 4. The names of the authors may not be used to endorse or promote products
26  *    derived from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <lib/libkern/libkern.h>
41 #include <lib/libsa/stand.h>
42 
43 #include "libi386.h"
44 
45 void
docommand(char * arg)46 docommand(char *arg)
47 {
48 	char *options;
49 	int i;
50 
51 	options = gettrailer(arg);
52 
53 	for (i = 0; commands[i].c_name != NULL; i++) {
54 		if (strcmp(arg, commands[i].c_name) == 0) {
55 			(*commands[i].c_fn)(options);
56 			return;
57 		}
58 	}
59 
60 	printf("unknown command\n");
61 	command_help(NULL);
62 }
63 
64 __dead void
bootmenu(void)65 bootmenu(void)
66 #if defined(__minix)
67 {
68 	prompt(0);
69 	while(1); /* This should never return. */
70 }
71 
72 /* Derived from libsa gets(). */
73 void
editline(char * buf,size_t size,char * input)74 editline(char *buf, size_t size, char *input)
75 {
76 	int c, i, pos, len = 0;
77 
78 	/* If an initial input has been given, copy and print this first. */
79 	if (input != NULL) {
80 		while (*input && len < size - 1)
81 			putchar(buf[len++] = *input++);
82 	}
83 	pos = len;
84 
85 	for (;;) {
86 		c = getchar_ex();
87 		switch (c & 0177) {
88 		case '\0':
89 			switch (c) {
90 			case 0x4b00: /* Left arrow: move cursor to left. */
91 				if (pos > 0) {
92 					putchar('\b');
93 					pos--;
94 				}
95 				break;
96 			case 0x4d00: /* Right arrow: move cursor to right. */
97 				if (pos < len) putchar(buf[pos++]);
98 				break;
99 			}
100 			break;
101 		case 'b' & 037: /* Ctrl+B: move cursor to left. */
102 			if (pos > 0) {
103 				putchar('\b');
104 				pos--;
105 			}
106 			break;
107 		case 'f' & 037: /* Ctrl+F: move cursor to right. */
108 			if (pos < len) putchar(buf[pos++]);
109 			break;
110 		case 'a' & 037: /* Ctrl+A: move cursor to start of line. */
111 			for ( ; pos > 0; pos--) putchar('\b');
112 			break;
113 		case 'e' & 037: /* Ctrl+E: move cursor to end of line. */
114 			for ( ; pos < len; pos++) putchar(buf[pos]);
115 			break;
116 		case '\n': /* Enter: return line. */
117 		case '\r':
118 			for ( ; pos < len; pos++) putchar(buf[pos]);
119 			buf[len] = '\0';
120 			putchar('\n');
121 			return;
122 #if HASH_ERASE
123 		case '#':
124 #endif
125 		case '\b': /* Backspace: erase character before cursor. */
126 		case '\177':
127 			if (pos > 0) {
128 				pos--;
129 				len--;
130 				putchar('\b');
131 				for (i = pos; i < len; i++)
132 					putchar(buf[i] = buf[i + 1]);
133 				putchar(' ');
134 				for (i = pos; i < len; i++) putchar('\b');
135 				putchar('\b');
136 			}
137 			break;
138 		case 'r' & 037: /* Ctrl+R: reprint line. */
139 			putchar('\n');
140 			for (i = 0; i < len; i++) putchar(buf[i]);
141 			for (i = len; i > pos; i--) putchar('\b');
142 			break;
143 #if AT_ERASE
144 		case '@':
145 #endif
146 		case 'u' & 037: /* Ctrl+U: clear entire line. */
147 		case 'w' & 037:
148 			for ( ; pos > 0; pos--) putchar('\b');
149 			for ( ; pos < len; pos++) putchar(' ');
150 			for ( ; pos > 0; pos--) putchar('\b');
151 			len = 0;
152 			break;
153 		case '\a': /* Ctrl+G: sound bell but do not store character. */
154 			putchar(c);
155 			break;
156 		case '\t': /* Tab: convert to single space. */
157 			c = ' ';
158 			/*FALLTHROUGH*/
159 		default: /* Insert character at cursor position. */
160 			if (len < size - 1) {
161 				for (i = len; i > pos; i--)
162 					buf[i] = buf[i - 1];
163 				buf[pos] = c;
164 				pos++;
165 				len++;
166 				putchar(c);
167 				for (i = pos; i < len; i++) putchar(buf[i]);
168 				for (i = pos; i < len; i++) putchar('\b');
169 			} else {
170 				putchar('\a');
171 			}
172 			break;
173 		}
174 	}
175 	/*NOTREACHED*/
176 }
177 
178 void
prompt(int allowreturn)179 prompt(int allowreturn)
180 #endif /* defined(__minix) */
181 {
182 	char input[80];
183 
184 	for (;;) {
185 		char *c = input;
186 
187 		input[0] = '\0';
188 		printf("> ");
189 #if !defined(__minix)
190 		gets(input);
191 #else
192 		editline(input, sizeof(input), NULL);
193 #endif /* !defined(__minix) */
194 
195 		/*
196 		 * Skip leading whitespace.
197 		 */
198 		while (*c == ' ')
199 			c++;
200 #if defined(__minix)
201 		if (allowreturn && !strcmp(c, "menu"))
202 			break;
203 #endif /* defined(__minix) */
204 		if (*c)
205 			docommand(c);
206 	}
207 }
208