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