prompt.c revision 1.1 1 1.1 jmcneill /* $NetBSD: prompt.c,v 1.1 2018/08/24 02:01:06 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.1 jmcneill char c = 0;
77 1.1 jmcneill
78 1.1 jmcneill for (;;) {
79 1.1 jmcneill if (tell) {
80 1.1 jmcneill char buf[32];
81 1.1 jmcneill int len;
82 1.1 jmcneill
83 1.1 jmcneill len = snprintf(buf, sizeof(buf), "%d seconds. ", i / POLL_FREQ);
84 1.1 jmcneill if (len > 0 && len < sizeof(buf)) {
85 1.1 jmcneill char *p = buf;
86 1.1 jmcneill printf("%s", buf);
87 1.1 jmcneill while (*p)
88 1.1 jmcneill *p++ = '\b';
89 1.1 jmcneill printf("%s", buf);
90 1.1 jmcneill }
91 1.1 jmcneill }
92 1.1 jmcneill if (ischar()) {
93 1.1 jmcneill while (ischar())
94 1.1 jmcneill c = getchar();
95 1.1 jmcneill if (c == 0)
96 1.1 jmcneill c = -1;
97 1.1 jmcneill goto out;
98 1.1 jmcneill }
99 1.1 jmcneill if (--i > 0) {
100 1.1 jmcneill efi_delay(1000000 / POLL_FREQ);
101 1.1 jmcneill } else {
102 1.1 jmcneill break;
103 1.1 jmcneill }
104 1.1 jmcneill }
105 1.1 jmcneill
106 1.1 jmcneill out:
107 1.1 jmcneill if (tell)
108 1.1 jmcneill printf("0 seconds. \n");
109 1.1 jmcneill
110 1.1 jmcneill return c;
111 1.1 jmcneill }
112 1.1 jmcneill
113 1.1 jmcneill void
114 1.1 jmcneill docommand(char *arg)
115 1.1 jmcneill {
116 1.1 jmcneill char *options;
117 1.1 jmcneill int i;
118 1.1 jmcneill
119 1.1 jmcneill options = gettrailer(arg);
120 1.1 jmcneill
121 1.1 jmcneill for (i = 0; commands[i].c_name != NULL; i++) {
122 1.1 jmcneill if (strcmp(arg, commands[i].c_name) == 0) {
123 1.1 jmcneill (*commands[i].c_fn)(options);
124 1.1 jmcneill return;
125 1.1 jmcneill }
126 1.1 jmcneill }
127 1.1 jmcneill
128 1.1 jmcneill printf("unknown command\n");
129 1.1 jmcneill command_help(NULL);
130 1.1 jmcneill }
131 1.1 jmcneill
132 1.1 jmcneill __dead void
133 1.1 jmcneill bootprompt(void)
134 1.1 jmcneill {
135 1.1 jmcneill char input[80];
136 1.1 jmcneill
137 1.1 jmcneill for (;;) {
138 1.1 jmcneill char *c = input;
139 1.1 jmcneill
140 1.1 jmcneill input[0] = '\0';
141 1.1 jmcneill printf("> ");
142 1.1 jmcneill kgets(input, sizeof(input));
143 1.1 jmcneill
144 1.1 jmcneill /*
145 1.1 jmcneill * Skip leading whitespace.
146 1.1 jmcneill */
147 1.1 jmcneill while (*c == ' ')
148 1.1 jmcneill c++;
149 1.1 jmcneill if (*c)
150 1.1 jmcneill docommand(c);
151 1.1 jmcneill }
152 1.1 jmcneill }
153