prompt.c revision 1.5 1 /* $NetBSD: prompt.c,v 1.5 2019/09/29 00:52: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 "efiboot.h"
41
42 #include <lib/libsa/net.h>
43 #include <sys/syslimits.h>
44
45 #define POLL_FREQ 10
46
47 char *
48 gettrailer(char *arg)
49 {
50 char *options;
51
52 for (options = arg; *options; options++) {
53 switch (*options) {
54 case ' ':
55 case '\t':
56 *options++ = '\0';
57 break;
58 default:
59 continue;
60 }
61 break;
62 }
63 if (*options == '\0')
64 return options;
65
66 /* trim leading blanks/tabs */
67 while (*options == ' ' || *options == '\t')
68 options++;
69
70 return options;
71 }
72
73 char
74 awaitkey(int timeout, int tell)
75 {
76 int i = timeout * POLL_FREQ;
77 int last_secs = -1, secs;
78 char c = 0;
79
80 for (;;) {
81 if (tell) {
82 char buf[32];
83 int len;
84
85 secs = (i + POLL_FREQ - 1) / POLL_FREQ;
86 if (secs != last_secs) {
87 len = snprintf(buf, sizeof(buf), "%d seconds. ", (i + POLL_FREQ - 1) / POLL_FREQ);
88 if (len > 0 && len < sizeof(buf)) {
89 char *p = buf;
90 printf("%s", buf);
91 while (*p)
92 *p++ = '\b';
93 printf("%s", buf);
94 }
95 last_secs = secs;
96 }
97 }
98 if (ischar()) {
99 c = getchar();
100 if (c == 0)
101 c = -1;
102 goto out;
103 }
104 if (--i > 0) {
105 efi_delay(1000000 / POLL_FREQ);
106 } else {
107 break;
108 }
109 }
110
111 out:
112 if (tell)
113 printf("0 seconds. \n");
114
115 return c;
116 }
117
118 void
119 docommand(char *arg)
120 {
121 char *options;
122 int i;
123
124 options = gettrailer(arg);
125
126 for (i = 0; commands[i].c_name != NULL; i++) {
127 if (strcmp(arg, commands[i].c_name) == 0) {
128 (*commands[i].c_fn)(options);
129 return;
130 }
131 }
132
133 printf("unknown command\n");
134 command_help(NULL);
135 }
136
137 __dead void
138 bootprompt(void)
139 {
140 char input[LINE_MAX];
141
142 for (;;) {
143 char *c = input;
144
145 input[0] = '\0';
146 printf("> ");
147 kgets(input, sizeof(input));
148
149 /*
150 * Skip leading whitespace.
151 */
152 while (*c == ' ')
153 c++;
154 if (*c)
155 docommand(c);
156 }
157 }
158