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