console.c revision 1.1 1 1.1 cherry /* $NetBSD: console.c,v 1.1 2006/04/07 14:21:29 cherry Exp $ */
2 1.1 cherry
3 1.1 cherry /*-
4 1.1 cherry * Copyright (c) 1998 Michael Smith <msmith (at) freebsd.org>
5 1.1 cherry * All rights reserved.
6 1.1 cherry *
7 1.1 cherry * Redistribution and use in source and binary forms, with or without
8 1.1 cherry * modification, are permitted provided that the following conditions
9 1.1 cherry * are met:
10 1.1 cherry * 1. Redistributions of source code must retain the above copyright
11 1.1 cherry * notice, this list of conditions and the following disclaimer.
12 1.1 cherry * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cherry * notice, this list of conditions and the following disclaimer in the
14 1.1 cherry * documentation and/or other materials provided with the distribution.
15 1.1 cherry *
16 1.1 cherry * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 cherry * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 cherry * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 cherry * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 cherry * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 cherry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 cherry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 cherry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 cherry * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 cherry * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 cherry * SUCH DAMAGE.
27 1.1 cherry */
28 1.1 cherry
29 1.1 cherry #include <sys/cdefs.h>
30 1.1 cherry
31 1.1 cherry #include <lib/libsa/stand.h>
32 1.1 cherry #include <lib/libkern/libkern.h>
33 1.1 cherry
34 1.1 cherry #include "bootstrap.h"
35 1.1 cherry /*
36 1.1 cherry * Core console support
37 1.1 cherry */
38 1.1 cherry
39 1.1 cherry static int cons_set(struct env_var *ev, int flags, void *value);
40 1.1 cherry static int cons_find(char *name);
41 1.1 cherry
42 1.1 cherry /*
43 1.1 cherry * Detect possible console(s) to use. The first probed console
44 1.1 cherry * is marked active. Also create the console variable.
45 1.1 cherry *
46 1.1 cherry * XXX Add logic for multiple console support.
47 1.1 cherry */
48 1.1 cherry void
49 1.1 cherry cons_probe(void)
50 1.1 cherry {
51 1.1 cherry int cons;
52 1.1 cherry int active;
53 1.1 cherry char *prefconsole;
54 1.1 cherry
55 1.1 cherry /* Do all console probes */
56 1.1 cherry for (cons = 0; consoles[cons] != NULL; cons++) {
57 1.1 cherry consoles[cons]->c_flags = 0;
58 1.1 cherry consoles[cons]->c_probe(consoles[cons]);
59 1.1 cherry }
60 1.1 cherry /* Now find the first working one */
61 1.1 cherry active = -1;
62 1.1 cherry for (cons = 0; consoles[cons] != NULL && active == -1; cons++) {
63 1.1 cherry consoles[cons]->c_flags = 0;
64 1.1 cherry consoles[cons]->c_probe(consoles[cons]);
65 1.1 cherry if (consoles[cons]->c_flags == (C_PRESENTIN | C_PRESENTOUT))
66 1.1 cherry active = cons;
67 1.1 cherry }
68 1.1 cherry
69 1.1 cherry /* Check to see if a console preference has already been registered */
70 1.1 cherry prefconsole = getenv("console");
71 1.1 cherry if (prefconsole != NULL)
72 1.1 cherry prefconsole = strdup(prefconsole);
73 1.1 cherry if (prefconsole != NULL) {
74 1.1 cherry unsetenv("console"); /* we want to replace this */
75 1.1 cherry for (cons = 0; consoles[cons] != NULL; cons++)
76 1.1 cherry /* look for the nominated console, use it if it's functional */
77 1.1 cherry if (!strcmp(prefconsole, consoles[cons]->c_name) &&
78 1.1 cherry (consoles[cons]->c_flags == (C_PRESENTIN | C_PRESENTOUT)))
79 1.1 cherry active = cons;
80 1.1 cherry free(prefconsole);
81 1.1 cherry }
82 1.1 cherry if (active == -1)
83 1.1 cherry active = 0;
84 1.1 cherry consoles[active]->c_flags |= (C_ACTIVEIN | C_ACTIVEOUT);
85 1.1 cherry consoles[active]->c_init(0);
86 1.1 cherry
87 1.1 cherry printf("Console: %s\n", consoles[active]->c_desc);
88 1.1 cherry env_setenv("console", EV_VOLATILE, consoles[active]->c_name, (ev_sethook_t *) cons_set,
89 1.1 cherry env_nounset);
90 1.1 cherry }
91 1.1 cherry
92 1.1 cherry int
93 1.1 cherry getchar(void)
94 1.1 cherry {
95 1.1 cherry int cons;
96 1.1 cherry int rv;
97 1.1 cherry
98 1.1 cherry /* Loop forever polling all active consoles */
99 1.1 cherry for(;;)
100 1.1 cherry for (cons = 0; consoles[cons] != NULL; cons++)
101 1.1 cherry if ((consoles[cons]->c_flags & C_ACTIVEIN) &&
102 1.1 cherry ((rv = consoles[cons]->c_in()) != -1))
103 1.1 cherry return(rv);
104 1.1 cherry }
105 1.1 cherry
106 1.1 cherry int
107 1.1 cherry ischar(void)
108 1.1 cherry {
109 1.1 cherry int cons;
110 1.1 cherry
111 1.1 cherry for (cons = 0; consoles[cons] != NULL; cons++)
112 1.1 cherry if ((consoles[cons]->c_flags & C_ACTIVEIN) &&
113 1.1 cherry (consoles[cons]->c_ready() != 0))
114 1.1 cherry return(1);
115 1.1 cherry return(0);
116 1.1 cherry }
117 1.1 cherry
118 1.1 cherry void
119 1.1 cherry putchar(int c)
120 1.1 cherry {
121 1.1 cherry int cons;
122 1.1 cherry
123 1.1 cherry /* Expand newlines */
124 1.1 cherry if (c == '\n')
125 1.1 cherry putchar('\r');
126 1.1 cherry
127 1.1 cherry for (cons = 0; consoles[cons] != NULL; cons++)
128 1.1 cherry if (consoles[cons]->c_flags & C_ACTIVEOUT)
129 1.1 cherry consoles[cons]->c_out(c);
130 1.1 cherry }
131 1.1 cherry
132 1.1 cherry static int
133 1.1 cherry cons_find(char *name)
134 1.1 cherry {
135 1.1 cherry int cons;
136 1.1 cherry
137 1.1 cherry for (cons = 0; consoles[cons] != NULL; cons++)
138 1.1 cherry if (!strcmp(consoles[cons]->c_name, name))
139 1.1 cherry return(cons);
140 1.1 cherry return(-1);
141 1.1 cherry }
142 1.1 cherry
143 1.1 cherry
144 1.1 cherry /*
145 1.1 cherry * Select a console.
146 1.1 cherry *
147 1.1 cherry * XXX Note that the console system design allows for some extension
148 1.1 cherry * here (eg. multiple consoles, input/output only, etc.)
149 1.1 cherry */
150 1.1 cherry static int
151 1.1 cherry cons_set(struct env_var *ev, int flags, void *value)
152 1.1 cherry {
153 1.1 cherry int cons, active;
154 1.1 cherry
155 1.1 cherry if ((value == NULL) || ((active = cons_find(value)) == -1)) {
156 1.1 cherry if (value != NULL)
157 1.1 cherry printf("no such console '%s'\n", (char *)value);
158 1.1 cherry printf("Available consoles:\n");
159 1.1 cherry for (cons = 0; consoles[cons] != NULL; cons++)
160 1.1 cherry printf(" %s\n", consoles[cons]->c_name);
161 1.1 cherry return(CMD_ERROR);
162 1.1 cherry }
163 1.1 cherry
164 1.1 cherry /* disable all current consoles */
165 1.1 cherry for (cons = 0; consoles[cons] != NULL; cons++)
166 1.1 cherry consoles[cons]->c_flags &= ~(C_ACTIVEIN | C_ACTIVEOUT);
167 1.1 cherry
168 1.1 cherry /* enable selected console */
169 1.1 cherry consoles[active]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
170 1.1 cherry consoles[active]->c_init(0);
171 1.1 cherry
172 1.1 cherry env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
173 1.1 cherry return(CMD_OK);
174 1.1 cherry }
175