1 /* $NetBSD: menuutils.c,v 1.3 2008/12/14 18:46:33 christos Exp $ */
4 * Copyright (c) 1996, 1997
5 * Matthias Drochner. All rights reserved.
6 * Copyright (c) 1996, 1997
7 * Perry E. Metzger. All rights reserved.
9 * Jason R. Thorpe. All rights reserved
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
40 #include <lib/libkern/libkern.h>
41 #include <lib/libsa/stand.h>
51 options
= gettrailer(arg
);
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
);
60 printf("unknown command\n");
65 prompt(int allowreturn
)
73 editline(input
, sizeof(input
), NULL
);
76 * Skip leading whitespace.
80 if (allowreturn
&& !strcmp(c
, "menu"))
93 /* Derived from libsa gets(). */
95 editline(char *buf
, size_t size
, char *input
)
97 int c
, i
, pos
, len
= 0;
99 /* If an initial input has been given, copy and print this first. */
101 while (*input
&& len
< size
- 1)
102 putchar(buf
[len
++] = *input
++);
111 case 0x4b00: /* Left arrow: move cursor to left. */
117 case 0x4d00: /* Right arrow: move cursor to right. */
118 if (pos
< len
) putchar(buf
[pos
++]);
122 case 'b' & 037: /* Ctrl+B: move cursor to left. */
128 case 'f' & 037: /* Ctrl+F: move cursor to right. */
129 if (pos
< len
) putchar(buf
[pos
++]);
131 case 'a' & 037: /* Ctrl+A: move cursor to start of line. */
132 for ( ; pos
> 0; pos
--) putchar('\b');
134 case 'e' & 037: /* Ctrl+E: move cursor to end of line. */
135 for ( ; pos
< len
; pos
++) putchar(buf
[pos
]);
137 case '\n': /* Enter: return line. */
139 for ( ; pos
< len
; pos
++) putchar(buf
[pos
]);
146 case '\b': /* Backspace: erase character before cursor. */
152 for (i
= pos
; i
< len
; i
++)
153 putchar(buf
[i
] = buf
[i
+ 1]);
155 for (i
= pos
; i
< len
; i
++) putchar('\b');
159 case 'r' & 037: /* Ctrl+R: reprint line. */
161 for (i
= 0; i
< len
; i
++) putchar(buf
[i
]);
162 for (i
= len
; i
> pos
; i
--) putchar('\b');
167 case 'u' & 037: /* Ctrl+U: clear entire line. */
169 for ( ; pos
> 0; pos
--) putchar('\b');
170 for ( ; pos
< len
; pos
++) putchar(' ');
171 for ( ; pos
> 0; pos
--) putchar('\b');
174 case '\a': /* Ctrl+G: sound bell but do not store character. */
177 case '\t': /* Tab: convert to single space. */
180 default: /* Insert character at cursor position. */
181 if (len
< size
- 1) {
182 for (i
= len
; i
> pos
; i
--)
188 for (i
= pos
; i
< len
; i
++) putchar(buf
[i
]);
189 for (i
= pos
; i
< len
; i
++) putchar('\b');