prompt.c revision 1.6 1 1.6 jmcneill /* $NetBSD: prompt.c,v 1.6 2020/01/25 10:09:46 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*
4 1.1 jmcneill * Copyright (c) 1996, 1997
5 1.1 jmcneill * Matthias Drochner. All rights reserved.
6 1.1 jmcneill * Copyright (c) 1996, 1997
7 1.1 jmcneill * Perry E. Metzger. All rights reserved.
8 1.1 jmcneill * Copyright (c) 1997
9 1.1 jmcneill * Jason R. Thorpe. All rights reserved
10 1.1 jmcneill *
11 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
12 1.1 jmcneill * modification, are permitted provided that the following conditions
13 1.1 jmcneill * are met:
14 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
15 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
16 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
18 1.1 jmcneill * documentation and/or other materials provided with the distribution.
19 1.1 jmcneill * 3. All advertising materials mentioning features or use of this software
20 1.1 jmcneill * must display the following acknowledgements:
21 1.1 jmcneill * This product includes software developed for the NetBSD Project
22 1.1 jmcneill * by Matthias Drochner.
23 1.1 jmcneill * This product includes software developed for the NetBSD Project
24 1.1 jmcneill * by Perry E. Metzger.
25 1.1 jmcneill * 4. The names of the authors may not be used to endorse or promote products
26 1.1 jmcneill * derived from this software without specific prior written permission.
27 1.1 jmcneill *
28 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 1.1 jmcneill * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 1.1 jmcneill * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 1.1 jmcneill * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 1.1 jmcneill * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 1.1 jmcneill * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 1.1 jmcneill */
39 1.1 jmcneill
40 1.1 jmcneill #include "efiboot.h"
41 1.1 jmcneill
42 1.1 jmcneill #include <lib/libsa/net.h>
43 1.5 jakllsch #include <sys/syslimits.h>
44 1.1 jmcneill
45 1.1 jmcneill #define POLL_FREQ 10
46 1.1 jmcneill
47 1.1 jmcneill char *
48 1.1 jmcneill gettrailer(char *arg)
49 1.1 jmcneill {
50 1.1 jmcneill char *options;
51 1.1 jmcneill
52 1.1 jmcneill for (options = arg; *options; options++) {
53 1.1 jmcneill switch (*options) {
54 1.1 jmcneill case ' ':
55 1.1 jmcneill case '\t':
56 1.1 jmcneill *options++ = '\0';
57 1.1 jmcneill break;
58 1.1 jmcneill default:
59 1.1 jmcneill continue;
60 1.1 jmcneill }
61 1.1 jmcneill break;
62 1.1 jmcneill }
63 1.1 jmcneill if (*options == '\0')
64 1.1 jmcneill return options;
65 1.1 jmcneill
66 1.1 jmcneill /* trim leading blanks/tabs */
67 1.1 jmcneill while (*options == ' ' || *options == '\t')
68 1.1 jmcneill options++;
69 1.1 jmcneill
70 1.1 jmcneill return options;
71 1.1 jmcneill }
72 1.1 jmcneill
73 1.1 jmcneill char
74 1.1 jmcneill awaitkey(int timeout, int tell)
75 1.1 jmcneill {
76 1.1 jmcneill int i = timeout * POLL_FREQ;
77 1.4 jmcneill int last_secs = -1, secs;
78 1.6 jmcneill int last_len = -1, n;
79 1.6 jmcneill char buf[32];
80 1.1 jmcneill char c = 0;
81 1.1 jmcneill
82 1.1 jmcneill for (;;) {
83 1.1 jmcneill if (tell) {
84 1.1 jmcneill int len;
85 1.1 jmcneill
86 1.4 jmcneill secs = (i + POLL_FREQ - 1) / POLL_FREQ;
87 1.4 jmcneill if (secs != last_secs) {
88 1.6 jmcneill if (last_len != -1) {
89 1.4 jmcneill char *p = buf;
90 1.6 jmcneill for (n = 0; n < last_len; n++)
91 1.4 jmcneill *p++ = '\b';
92 1.6 jmcneill *p = '\0';
93 1.4 jmcneill printf("%s", buf);
94 1.4 jmcneill }
95 1.6 jmcneill len = snprintf(buf, sizeof(buf), "%d seconds. ", (i + POLL_FREQ - 1) / POLL_FREQ);
96 1.6 jmcneill if (len > 0 && len < sizeof(buf))
97 1.6 jmcneill printf("%s", buf);
98 1.6 jmcneill last_len = len;
99 1.4 jmcneill last_secs = secs;
100 1.1 jmcneill }
101 1.1 jmcneill }
102 1.1 jmcneill if (ischar()) {
103 1.3 jmcneill c = getchar();
104 1.1 jmcneill if (c == 0)
105 1.1 jmcneill c = -1;
106 1.1 jmcneill goto out;
107 1.1 jmcneill }
108 1.1 jmcneill if (--i > 0) {
109 1.1 jmcneill efi_delay(1000000 / POLL_FREQ);
110 1.1 jmcneill } else {
111 1.1 jmcneill break;
112 1.1 jmcneill }
113 1.1 jmcneill }
114 1.1 jmcneill
115 1.1 jmcneill out:
116 1.6 jmcneill if (tell) {
117 1.6 jmcneill if (last_len != -1) {
118 1.6 jmcneill char *p = buf;
119 1.6 jmcneill for (n = 0; n < last_len; n++)
120 1.6 jmcneill *p++ = '\b';
121 1.6 jmcneill *p = '\0';
122 1.6 jmcneill printf("%s", buf);
123 1.6 jmcneill }
124 1.1 jmcneill printf("0 seconds. \n");
125 1.6 jmcneill }
126 1.1 jmcneill
127 1.1 jmcneill return c;
128 1.1 jmcneill }
129 1.1 jmcneill
130 1.1 jmcneill void
131 1.1 jmcneill docommand(char *arg)
132 1.1 jmcneill {
133 1.1 jmcneill char *options;
134 1.1 jmcneill int i;
135 1.1 jmcneill
136 1.1 jmcneill options = gettrailer(arg);
137 1.1 jmcneill
138 1.1 jmcneill for (i = 0; commands[i].c_name != NULL; i++) {
139 1.1 jmcneill if (strcmp(arg, commands[i].c_name) == 0) {
140 1.1 jmcneill (*commands[i].c_fn)(options);
141 1.1 jmcneill return;
142 1.1 jmcneill }
143 1.1 jmcneill }
144 1.1 jmcneill
145 1.1 jmcneill printf("unknown command\n");
146 1.1 jmcneill command_help(NULL);
147 1.1 jmcneill }
148 1.1 jmcneill
149 1.1 jmcneill __dead void
150 1.1 jmcneill bootprompt(void)
151 1.1 jmcneill {
152 1.5 jakllsch char input[LINE_MAX];
153 1.1 jmcneill
154 1.1 jmcneill for (;;) {
155 1.1 jmcneill char *c = input;
156 1.1 jmcneill
157 1.1 jmcneill input[0] = '\0';
158 1.1 jmcneill printf("> ");
159 1.1 jmcneill kgets(input, sizeof(input));
160 1.1 jmcneill
161 1.1 jmcneill /*
162 1.1 jmcneill * Skip leading whitespace.
163 1.1 jmcneill */
164 1.1 jmcneill while (*c == ' ')
165 1.1 jmcneill c++;
166 1.1 jmcneill if (*c)
167 1.1 jmcneill docommand(c);
168 1.1 jmcneill }
169 1.1 jmcneill }
170