Initial import from FreeBSD RELENG_4:
[games.git] / sys / boot / common / console.c
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  * $FreeBSD: src/sys/boot/common/console.c,v 1.5 1999/08/28 00:39:46 peter Exp $
27  */
28
29 #include <stand.h>
30 #include <string.h>
31
32 #include "bootstrap.h"
33 /*
34  * Core console support
35  */
36
37 static int      cons_set(struct env_var *ev, int flags, void *value);
38 static int      cons_find(char *name);
39
40 /*
41  * Detect possible console(s) to use.  The first probed console
42  * is marked active.  Also create the console variable.
43  *      
44  * XXX Add logic for multiple console support.
45  */
46 void
47 cons_probe(void) 
48 {
49     int                 cons;
50     int                 active;
51     char                *prefconsole;
52     
53     /* Do all console probes */
54     for (cons = 0; consoles[cons] != NULL; cons++) {
55         consoles[cons]->c_flags = 0;
56         consoles[cons]->c_probe(consoles[cons]);
57     }
58     /* Now find the first working one */
59     active = -1;
60     for (cons = 0; consoles[cons] != NULL && active == -1; cons++) {
61         consoles[cons]->c_flags = 0;
62         consoles[cons]->c_probe(consoles[cons]);
63         if (consoles[cons]->c_flags == (C_PRESENTIN | C_PRESENTOUT))
64             active = cons;
65     }
66
67     /* Check to see if a console preference has already been registered */
68     prefconsole = getenv("console");
69     if (prefconsole != NULL)
70         prefconsole = strdup(prefconsole);
71     if (prefconsole != NULL) {
72         unsetenv("console");            /* we want to replace this */
73         for (cons = 0; consoles[cons] != NULL; cons++)
74             /* look for the nominated console, use it if it's functional */
75             if (!strcmp(prefconsole, consoles[cons]->c_name) &&
76                 (consoles[cons]->c_flags == (C_PRESENTIN | C_PRESENTOUT)))
77                 active = cons;
78         free(prefconsole);
79     }
80     if (active == -1)
81         active = 0;
82     consoles[active]->c_flags |= (C_ACTIVEIN | C_ACTIVEOUT);
83     consoles[active]->c_init(0);
84
85     printf("Console: %s\n", consoles[active]->c_desc);
86     env_setenv("console", EV_VOLATILE, consoles[active]->c_name, cons_set,
87         env_nounset);
88 }
89
90 int
91 getchar(void)
92 {
93     int         cons;
94     int         rv;
95     
96     /* Loop forever polling all active consoles */
97     for(;;)
98         for (cons = 0; consoles[cons] != NULL; cons++)
99             if ((consoles[cons]->c_flags & C_ACTIVEIN) && 
100                 ((rv = consoles[cons]->c_in()) != -1))
101                 return(rv);
102 }
103
104 int
105 ischar(void)
106 {
107     int         cons;
108
109     for (cons = 0; consoles[cons] != NULL; cons++)
110         if ((consoles[cons]->c_flags & C_ACTIVEIN) && 
111             (consoles[cons]->c_ready() != 0))
112                 return(1);
113     return(0);
114 }
115
116 void
117 putchar(int c)
118 {
119     int         cons;
120     
121     /* Expand newlines */
122     if (c == '\n')
123         putchar('\r');
124     
125     for (cons = 0; consoles[cons] != NULL; cons++)
126         if (consoles[cons]->c_flags & C_ACTIVEOUT)
127             consoles[cons]->c_out(c);
128 }
129
130 static int
131 cons_find(char *name)
132 {
133     int         cons;
134     
135     for (cons = 0; consoles[cons] != NULL; cons++)
136         if (!strcmp(consoles[cons]->c_name, name))
137             return(cons);
138     return(-1);
139 }
140     
141
142 /*
143  * Select a console.
144  *
145  * XXX Note that the console system design allows for some extension
146  *     here (eg. multiple consoles, input/output only, etc.)
147  */
148 static int
149 cons_set(struct env_var *ev, int flags, void *value)
150 {
151     int         cons, active;
152
153     if ((value == NULL) || ((active = cons_find(value)) == -1)) {
154         if (value != NULL) 
155             printf("no such console '%s'\n", (char *)value);
156         printf("Available consoles:\n");
157         for (cons = 0; consoles[cons] != NULL; cons++)
158             printf("    %s\n", consoles[cons]->c_name);
159         return(CMD_ERROR);
160     }
161
162     /* disable all current consoles */
163     for (cons = 0; consoles[cons] != NULL; cons++)
164         consoles[cons]->c_flags &= ~(C_ACTIVEIN | C_ACTIVEOUT);
165     
166     /* enable selected console */
167     consoles[active]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
168     consoles[active]->c_init(0);
169
170     env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
171     return(CMD_OK);
172 }