Home | History | Annotate | Line # | Download | only in libterminfo
curterm.c revision 1.7
      1  1.7  roy /* $NetBSD: curterm.c,v 1.7 2011/10/04 11:01:14 roy Exp $ */
      2  1.1  roy 
      3  1.1  roy /*
      4  1.5  roy  * Copyright (c) 2009, 2011 The NetBSD Foundation, Inc.
      5  1.1  roy  *
      6  1.1  roy  * This code is derived from software contributed to The NetBSD Foundation
      7  1.1  roy  * by Roy Marples.
      8  1.1  roy  *
      9  1.1  roy  * Redistribution and use in source and binary forms, with or without
     10  1.1  roy  * modification, are permitted provided that the following conditions
     11  1.1  roy  * are met:
     12  1.1  roy  * 1. Redistributions of source code must retain the above copyright
     13  1.1  roy  *    notice, this list of conditions and the following disclaimer.
     14  1.1  roy  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  roy  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  roy  *    documentation and/or other materials provided with the distribution.
     17  1.1  roy  *
     18  1.1  roy  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1  roy  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1  roy  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1  roy  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1  roy  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  1.1  roy  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  1.1  roy  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  1.1  roy  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  1.1  roy  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  1.1  roy  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1  roy  */
     29  1.1  roy 
     30  1.1  roy #include <sys/cdefs.h>
     31  1.7  roy __RCSID("$NetBSD: curterm.c,v 1.7 2011/10/04 11:01:14 roy Exp $");
     32  1.1  roy 
     33  1.1  roy #include <assert.h>
     34  1.1  roy #include <stdlib.h>
     35  1.1  roy #include <term_private.h>
     36  1.1  roy #include <term.h>
     37  1.1  roy #include <termios.h>
     38  1.2  roy #include <stdio.h>
     39  1.1  roy TERMINAL *cur_term;
     40  1.1  roy 
     41  1.1  roy static const speed_t bauds[] = {
     42  1.1  roy 	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 2400, 4800, 9600,
     43  1.1  roy 	19200, 38400, 57600, 115200, 230400, 460800, 921600
     44  1.1  roy };
     45  1.1  roy 
     46  1.1  roy void
     47  1.1  roy _ti_setospeed(TERMINAL *term)
     48  1.1  roy {
     49  1.1  roy 	struct termios termios;
     50  1.1  roy 	speed_t os;
     51  1.1  roy 	size_t i;
     52  1.1  roy 
     53  1.1  roy 	_DIAGASSERT(term != NULL);
     54  1.1  roy 
     55  1.1  roy 	term->_ospeed = 0;
     56  1.1  roy 	if (tcgetattr(term->fildes, &termios) == 0) {
     57  1.1  roy 		os = cfgetospeed(&termios);
     58  1.1  roy 		for (i = 0; i < __arraycount(bauds); i++)
     59  1.1  roy 			if (bauds[i] == os) {
     60  1.1  roy 				term->_ospeed = i;
     61  1.1  roy 				break;
     62  1.1  roy 			}
     63  1.1  roy 	}
     64  1.1  roy }
     65  1.1  roy 
     66  1.1  roy TERMINAL *
     67  1.1  roy set_curterm(TERMINAL *nterm)
     68  1.1  roy {
     69  1.1  roy 	TERMINAL *oterm;
     70  1.1  roy 
     71  1.1  roy 	oterm = cur_term;
     72  1.1  roy 	cur_term = nterm;
     73  1.1  roy 
     74  1.1  roy 	ospeed = 0;
     75  1.1  roy 	if (cur_term == NULL)
     76  1.1  roy 		PC = '\0';
     77  1.1  roy 	else {
     78  1.1  roy 		if (pad_char == NULL)
     79  1.1  roy 			PC = '\0';
     80  1.1  roy 		else
     81  1.1  roy 			PC = *pad_char;
     82  1.1  roy 		_ti_setospeed(nterm);
     83  1.1  roy 		ospeed = nterm->_ospeed;
     84  1.1  roy 	}
     85  1.1  roy 
     86  1.1  roy 	return oterm;
     87  1.1  roy }
     88  1.1  roy 
     89  1.1  roy int
     90  1.1  roy del_curterm(TERMINAL *oterm)
     91  1.1  roy {
     92  1.4  roy 
     93  1.6  roy 	if (oterm == NULL)
     94  1.6  roy 		return ERR;
     95  1.6  roy 	free(oterm->_area);
     96  1.6  roy 	free(oterm->strs);
     97  1.6  roy 	free(oterm->nums);
     98  1.6  roy 	free(oterm->flags);
     99  1.6  roy 	free(oterm->_userdefs);
    100  1.6  roy 	free(oterm);
    101  1.6  roy 	return OK;
    102  1.1  roy }
    103  1.5  roy 
    104  1.5  roy char *
    105  1.5  roy termname(void)
    106  1.5  roy {
    107  1.5  roy 
    108  1.5  roy         _DIAGASSERT(cur_term != NULL);
    109  1.5  roy 	return __UNCONST(cur_term->name);
    110  1.5  roy }
    111  1.7  roy 
    112  1.7  roy static const char * nullname = '\0';
    113  1.7  roy 
    114  1.7  roy char *
    115  1.7  roy longname(void)
    116  1.7  roy {
    117  1.7  roy 
    118  1.7  roy 	_DIAGASSERT(cur_term != NULL);
    119  1.7  roy 	if (cur_term->desc == NULL)
    120  1.7  roy 		return __UNCONST(nullname);
    121  1.7  roy 	return __UNCONST(cur_term->desc);
    122  1.7  roy }
    123