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