1 /*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <stand.h>
31 #include <string.h>
32
33 #include "bootstrap.h"
34 /*
35 * Core console support
36 */
37
38 static int cons_set(struct env_var *ev, int flags, const void *value);
39 static int cons_find(const char *name);
40 static int cons_check(const char *string);
41 static void cons_change(const char *string);
42
43 /*
44 * Detect possible console(s) to use. If preferred console(s) have been
45 * specified, mark them as active. Else, mark the first probed console
46 * as active. Also create the console variable.
47 */
48 void
49 cons_probe(void)
50 {
51 int cons;
52 int active;
53 char *prefconsole;
54
55 /* Do all console probes */
56 for (cons = 0; consoles[cons] != NULL; cons++) {
57 consoles[cons]->c_flags = 0;
58 consoles[cons]->c_probe(consoles[cons]);
59 }
60 /* Now find the first working one */
61 active = -1;
62 for (cons = 0; consoles[cons] != NULL && active == -1; cons++) {
63 consoles[cons]->c_flags = 0;
64 consoles[cons]->c_probe(consoles[cons]);
65 if (consoles[cons]->c_flags == (C_PRESENTIN | C_PRESENTOUT))
66 active = cons;
67 }
68 /* Force a console even if all probes failed */
69 if (active == -1)
70 active = 0;
71
72 /* Check to see if a console preference has already been registered */
73 prefconsole = getenv("console");
74 if (prefconsole != NULL)
75 prefconsole = strdup(prefconsole);
76 if (prefconsole != NULL) {
77 unsetenv("console"); /* we want to replace this */
78 cons_change(prefconsole);
79 } else {
80 consoles[active]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
81 consoles[active]->c_init(0);
82 prefconsole = strdup(consoles[active]->c_name);
83 }
84
85 printf("Consoles: ");
86 for (cons = 0; consoles[cons] != NULL; cons++)
87 if (consoles[cons]->c_flags & (C_ACTIVEIN | C_ACTIVEOUT))
88 printf("%s ", consoles[cons]->c_desc);
89 printf("\n");
90
91 if (prefconsole != NULL) {
92 env_setenv("console", EV_VOLATILE, prefconsole, cons_set,
93 env_nounset);
94 free(prefconsole);
95 }
96 }
97
98 int
99 getchar(void)
100 {
101 int cons;
102 int rv;
103
104 /* Loop forever polling all active consoles */
105 for(;;)
106 for (cons = 0; consoles[cons] != NULL; cons++)
107 if ((consoles[cons]->c_flags & C_ACTIVEIN) &&
108 ((rv = consoles[cons]->c_in()) != -1))
109 return(rv);
110 }
111
112 int
113 ischar(void)
114 {
115 int cons;
116
117 for (cons = 0; consoles[cons] != NULL; cons++)
118 if ((consoles[cons]->c_flags & C_ACTIVEIN) &&
119 (consoles[cons]->c_ready() != 0))
120 return(1);
121 return(0);
122 }
123
124 void
125 putchar(int c)
126 {
127 int cons;
128
129 /* Expand newlines */
130 if (c == '\n')
131 putchar('\r');
132
133 for (cons = 0; consoles[cons] != NULL; cons++)
134 if (consoles[cons]->c_flags & C_ACTIVEOUT)
135 consoles[cons]->c_out(c);
136 }
137
138 /*
139 * Find the console with the specified name.
140 */
141 static int
142 cons_find(const char *name)
143 {
144 int cons;
145
146 for (cons = 0; consoles[cons] != NULL; cons++)
147 if (!strcmp(consoles[cons]->c_name, name))
148 return (cons);
149 return (-1);
150 }
151
152 /*
153 * Select one or more consoles.
154 */
155 static int
156 cons_set(struct env_var *ev, int flags, const void *value)
157 {
158 int cons;
159
160 if ((value == NULL) || (cons_check(value) == -1)) {
161 if (value != NULL)
162 printf("no such console!\n");
163 printf("Available consoles:\n");
164 for (cons = 0; consoles[cons] != NULL; cons++)
165 printf(" %s\n", consoles[cons]->c_name);
166 return(CMD_ERROR);
167 }
168
169 cons_change(value);
170
171 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
172 return(CMD_OK);
173 }
174
175 /*
176 * Check that all of the consoles listed in *string are valid consoles
177 */
178 static int
179 cons_check(const char *string)
180 {
181 int cons;
182 char *curpos, *dup, *next;
183
184 dup = next = strdup(string);
185 cons = -1;
186 while (next != NULL) {
187 curpos = strsep(&next, " ,");
188 if (*curpos != '\0') {
189 cons = cons_find(curpos);
190 if (cons == -1)
191 break;
192 }
193 }
194
195 free(dup);
196 return (cons);
197 }
198
199 /*
200 * Activate all of the consoles listed in *string and disable all the others.
201 */
202 static void
203 cons_change(const char *string)
204 {
205 int cons;
206 char *curpos, *dup, *next;
207
208 /* Disable all consoles */
209 for (cons = 0; consoles[cons] != NULL; cons++) {
210 consoles[cons]->c_flags &= ~(C_ACTIVEIN | C_ACTIVEOUT);
211 }
212
213 /* Enable selected consoles */
214 dup = next = strdup(string);
215 while (next != NULL) {
216 curpos = strsep(&next, " ,");
217 if (*curpos == '\0')
218 continue;
219 cons = cons_find(curpos);
220 if (cons >= 0) {
221 consoles[cons]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
222 consoles[cons]->c_init(0);
223 }
224 }
225
226 free(dup);
227 }
Cache object: 3a4fe93f08347bef94b8e7da25940ae9
|