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_ACTIVEIN) &&
108 ((rv = consoles[cons]->c_in()) != -1))
117 for (cons = 0; consoles[cons] != NULL; cons++)
118 if ((consoles[cons]->c_flags & C_ACTIVEIN) &&
119 (consoles[cons]->c_ready() != 0))
129 /* Expand newlines */
133 for (cons = 0; consoles[cons] != NULL; cons++)
134 if (consoles[cons]->c_flags & C_ACTIVEOUT)
135 consoles[cons]->c_out(c);
139 * Find the console with the specified name.
142 cons_find(const char *name)
146 for (cons = 0; consoles[cons] != NULL; cons++)
147 if (!strcmp(consoles[cons]->c_name, name))
153 * Select one or more consoles.
156 cons_set(struct env_var *ev, int flags, const void *value)
160 if ((value == NULL) || (cons_check(value) == -1)) {
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);
171 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
176 * Check that all of the consoles listed in *string are valid consoles
179 cons_check(const char *string)
182 char *curpos, *dup, *next;
184 dup = next = strdup(string);
186 while (next != NULL) {
187 curpos = strsep(&next, " ,");
188 if (*curpos != '\0') {
189 cons = cons_find(curpos);
200 * Activate all of the consoles listed in *string and disable all the others.
203 cons_change(const char *string)
206 char *curpos, *dup, *next;
208 /* Disable all consoles */
209 for (cons = 0; consoles[cons] != NULL; cons++) {
210 consoles[cons]->c_flags &= ~(C_ACTIVEIN | C_ACTIVEOUT);
213 /* Enable selected consoles */
214 dup = next = strdup(string);
215 while (next != NULL) {
216 curpos = strsep(&next, " ,");
219 cons = cons_find(curpos);
221 consoles[cons]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
222 consoles[cons]->c_init(0);