2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
33 #include "bootstrap.h"
35 * Core console support
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);
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.
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]);
60 /* Now find the first working one */
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))
68 /* Force a console even if all probes failed */
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);
80 consoles[active]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
81 consoles[active]->c_init(0);
82 prefconsole = strdup(consoles[active]->c_name);
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);
91 if (prefconsole != NULL) {
92 env_setenv("console", EV_VOLATILE, prefconsole, cons_set,
104 /* Loop forever polling all active consoles */
106 for (cons = 0; consoles[cons] != NULL; cons++)
107 if ((consoles[cons]->c_flags & (C_PRESENTIN | C_ACTIVEIN)) ==
108 (C_PRESENTIN | C_ACTIVEIN) &&
109 ((rv = consoles[cons]->c_in()) != -1))
118 for (cons = 0; consoles[cons] != NULL; cons++)
119 if ((consoles[cons]->c_flags & (C_PRESENTIN | C_ACTIVEIN)) ==
120 (C_PRESENTIN | C_ACTIVEIN) &&
121 (consoles[cons]->c_ready() != 0))
131 /* Expand newlines */
135 for (cons = 0; consoles[cons] != NULL; cons++)
136 if ((consoles[cons]->c_flags & (C_PRESENTOUT | C_ACTIVEOUT)) ==
137 (C_PRESENTOUT | C_ACTIVEOUT))
138 consoles[cons]->c_out(c);
142 * Find the console with the specified name.
145 cons_find(const char *name)
149 for (cons = 0; consoles[cons] != NULL; cons++)
150 if (!strcmp(consoles[cons]->c_name, name))
156 * Select one or more consoles.
159 cons_set(struct env_var *ev, int flags, const void *value)
163 if ((value == NULL) || (cons_check(value) == -1)) {
165 printf("no such console!\n");
166 printf("Available consoles:\n");
167 for (cons = 0; consoles[cons] != NULL; cons++)
168 printf(" %s\n", consoles[cons]->c_name);
174 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
179 * Check that all of the consoles listed in *string are valid consoles
182 cons_check(const char *string)
185 char *curpos, *dup, *next;
187 dup = next = strdup(string);
189 while (next != NULL) {
190 curpos = strsep(&next, " ,");
191 if (*curpos != '\0') {
192 cons = cons_find(curpos);
203 * Activate all of the consoles listed in *string and disable all the others.
206 cons_change(const char *string)
209 char *curpos, *dup, *next;
211 /* Disable all consoles */
212 for (cons = 0; consoles[cons] != NULL; cons++) {
213 consoles[cons]->c_flags &= ~(C_ACTIVEIN | C_ACTIVEOUT);
216 /* Enable selected consoles */
217 dup = next = strdup(string);
218 while (next != NULL) {
219 curpos = strsep(&next, " ,");
222 cons = cons_find(curpos);
224 consoles[cons]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
225 consoles[cons]->c_init(0);
226 if ((consoles[cons]->c_flags & (C_PRESENTIN | C_PRESENTOUT)) !=
227 (C_PRESENTIN | C_PRESENTOUT))
228 printf("console %s failed to initialize\n",
229 consoles[cons]->c_name);