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