1*84d9c625SLionel Sambuc /* $NetBSD: curterm.c,v 1.10 2013/11/18 20:51:03 joerg Exp $ */
251e66a47SVivek Prakash
351e66a47SVivek Prakash /*
40c3ae37fSLionel Sambuc * Copyright (c) 2009, 2011 The NetBSD Foundation, Inc.
551e66a47SVivek Prakash *
651e66a47SVivek Prakash * This code is derived from software contributed to The NetBSD Foundation
751e66a47SVivek Prakash * by Roy Marples.
851e66a47SVivek Prakash *
951e66a47SVivek Prakash * Redistribution and use in source and binary forms, with or without
1051e66a47SVivek Prakash * modification, are permitted provided that the following conditions
1151e66a47SVivek Prakash * are met:
1251e66a47SVivek Prakash * 1. Redistributions of source code must retain the above copyright
1351e66a47SVivek Prakash * notice, this list of conditions and the following disclaimer.
1451e66a47SVivek Prakash * 2. Redistributions in binary form must reproduce the above copyright
1551e66a47SVivek Prakash * notice, this list of conditions and the following disclaimer in the
1651e66a47SVivek Prakash * documentation and/or other materials provided with the distribution.
1751e66a47SVivek Prakash *
1851e66a47SVivek Prakash * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1951e66a47SVivek Prakash * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2051e66a47SVivek Prakash * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2151e66a47SVivek Prakash * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2251e66a47SVivek Prakash * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2351e66a47SVivek Prakash * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2451e66a47SVivek Prakash * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2551e66a47SVivek Prakash * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2651e66a47SVivek Prakash * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2751e66a47SVivek Prakash * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2851e66a47SVivek Prakash */
2951e66a47SVivek Prakash
3051e66a47SVivek Prakash #include <sys/cdefs.h>
31*84d9c625SLionel Sambuc __RCSID("$NetBSD: curterm.c,v 1.10 2013/11/18 20:51:03 joerg Exp $");
3251e66a47SVivek Prakash
3351e66a47SVivek Prakash #include <assert.h>
3451e66a47SVivek Prakash #include <stdlib.h>
350c3ae37fSLionel Sambuc #include <string.h>
3651e66a47SVivek Prakash #include <term_private.h>
3751e66a47SVivek Prakash #include <term.h>
3851e66a47SVivek Prakash #include <termios.h>
3951e66a47SVivek Prakash #include <stdio.h>
400c3ae37fSLionel Sambuc
4151e66a47SVivek Prakash TERMINAL *cur_term;
4251e66a47SVivek Prakash
430c3ae37fSLionel Sambuc /*
440c3ae37fSLionel Sambuc * There is no standard way of getting a list of aliases for the
450c3ae37fSLionel Sambuc * terminal. However, some applications such as telnet want to know this.
460c3ae37fSLionel Sambuc * ncurses dumps the terminfo header into an undefined variable ttytype
470c3ae37fSLionel Sambuc * and these applications then parse it to work out the aliases.
480c3ae37fSLionel Sambuc * We should do the same for now, until a standard mechanism for getting
490c3ae37fSLionel Sambuc * the information is available or the need for it goes away.
500c3ae37fSLionel Sambuc */
510c3ae37fSLionel Sambuc #define NAMESIZE 256
520c3ae37fSLionel Sambuc char ttytype[NAMESIZE];
530c3ae37fSLionel Sambuc
5451e66a47SVivek Prakash static const speed_t bauds[] = {
5551e66a47SVivek Prakash 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 2400, 4800, 9600,
5651e66a47SVivek Prakash 19200, 38400, 57600, 115200, 230400, 460800, 921600
5751e66a47SVivek Prakash };
5851e66a47SVivek Prakash
5951e66a47SVivek Prakash void
_ti_setospeed(TERMINAL * term)6051e66a47SVivek Prakash _ti_setospeed(TERMINAL *term)
6151e66a47SVivek Prakash {
6251e66a47SVivek Prakash struct termios termios;
6351e66a47SVivek Prakash speed_t os;
6451e66a47SVivek Prakash size_t i;
6551e66a47SVivek Prakash
6651e66a47SVivek Prakash _DIAGASSERT(term != NULL);
6751e66a47SVivek Prakash
6851e66a47SVivek Prakash term->_ospeed = 0;
6951e66a47SVivek Prakash if (tcgetattr(term->fildes, &termios) == 0) {
7051e66a47SVivek Prakash os = cfgetospeed(&termios);
7151e66a47SVivek Prakash for (i = 0; i < __arraycount(bauds); i++)
7251e66a47SVivek Prakash if (bauds[i] == os) {
7351e66a47SVivek Prakash term->_ospeed = i;
7451e66a47SVivek Prakash break;
7551e66a47SVivek Prakash }
7651e66a47SVivek Prakash }
7751e66a47SVivek Prakash }
7851e66a47SVivek Prakash
7951e66a47SVivek Prakash TERMINAL *
set_curterm(TERMINAL * nterm)8051e66a47SVivek Prakash set_curterm(TERMINAL *nterm)
8151e66a47SVivek Prakash {
8251e66a47SVivek Prakash TERMINAL *oterm;
830c3ae37fSLionel Sambuc size_t l, n;
840c3ae37fSLionel Sambuc char *p;
8551e66a47SVivek Prakash
8651e66a47SVivek Prakash oterm = cur_term;
8751e66a47SVivek Prakash cur_term = nterm;
8851e66a47SVivek Prakash
8951e66a47SVivek Prakash ospeed = 0;
9051e66a47SVivek Prakash if (cur_term == NULL)
9151e66a47SVivek Prakash PC = '\0';
9251e66a47SVivek Prakash else {
9351e66a47SVivek Prakash if (pad_char == NULL)
9451e66a47SVivek Prakash PC = '\0';
9551e66a47SVivek Prakash else
9651e66a47SVivek Prakash PC = *pad_char;
9751e66a47SVivek Prakash _ti_setospeed(nterm);
9851e66a47SVivek Prakash ospeed = nterm->_ospeed;
990c3ae37fSLionel Sambuc
1000c3ae37fSLionel Sambuc p = ttytype;
1010c3ae37fSLionel Sambuc l = sizeof(ttytype);
1020c3ae37fSLionel Sambuc if ((n = strlcpy(p, nterm->name, l)) == strlen(p)) {
1030c3ae37fSLionel Sambuc p += n;
1040c3ae37fSLionel Sambuc l -= n;
1050c3ae37fSLionel Sambuc *p++ = '|';
1060c3ae37fSLionel Sambuc l--;
1070c3ae37fSLionel Sambuc if (nterm->_alias &&
1080c3ae37fSLionel Sambuc (n = strlcpy(p, nterm->_alias, l)) == strlen(p))
1090c3ae37fSLionel Sambuc {
1100c3ae37fSLionel Sambuc p += n;
1110c3ae37fSLionel Sambuc l -= n;
1120c3ae37fSLionel Sambuc *p++ = '|';
1130c3ae37fSLionel Sambuc l--;
1140c3ae37fSLionel Sambuc }
1150c3ae37fSLionel Sambuc if (nterm->desc &&
1160c3ae37fSLionel Sambuc (n = strlcpy(p, nterm->desc, l)) == strlen(p))
1170c3ae37fSLionel Sambuc {
1180c3ae37fSLionel Sambuc p += n;
1190c3ae37fSLionel Sambuc l -= n;
1200c3ae37fSLionel Sambuc *p++ = '|';
1210c3ae37fSLionel Sambuc l--;
1220c3ae37fSLionel Sambuc }
1230c3ae37fSLionel Sambuc p--;
1240c3ae37fSLionel Sambuc }
1250c3ae37fSLionel Sambuc *p = '\0';
12651e66a47SVivek Prakash }
12751e66a47SVivek Prakash
12851e66a47SVivek Prakash return oterm;
12951e66a47SVivek Prakash }
13051e66a47SVivek Prakash
13151e66a47SVivek Prakash int
del_curterm(TERMINAL * oterm)13251e66a47SVivek Prakash del_curterm(TERMINAL *oterm)
13351e66a47SVivek Prakash {
13451e66a47SVivek Prakash
1350c3ae37fSLionel Sambuc if (oterm == NULL)
1360c3ae37fSLionel Sambuc return ERR;
1370c3ae37fSLionel Sambuc free(oterm->_area);
1380c3ae37fSLionel Sambuc free(oterm->strs);
1390c3ae37fSLionel Sambuc free(oterm->nums);
1400c3ae37fSLionel Sambuc free(oterm->flags);
1410c3ae37fSLionel Sambuc free(oterm->_userdefs);
1420c3ae37fSLionel Sambuc free(oterm);
1430c3ae37fSLionel Sambuc return OK;
1440c3ae37fSLionel Sambuc }
1450c3ae37fSLionel Sambuc
1460c3ae37fSLionel Sambuc char *
termname(void)1470c3ae37fSLionel Sambuc termname(void)
1480c3ae37fSLionel Sambuc {
1490c3ae37fSLionel Sambuc
1500c3ae37fSLionel Sambuc _DIAGASSERT(cur_term != NULL);
1510c3ae37fSLionel Sambuc return __UNCONST(cur_term->name);
1520c3ae37fSLionel Sambuc }
1530c3ae37fSLionel Sambuc
154*84d9c625SLionel Sambuc static const char * nullname = "";
1550c3ae37fSLionel Sambuc
1560c3ae37fSLionel Sambuc char *
longname(void)1570c3ae37fSLionel Sambuc longname(void)
1580c3ae37fSLionel Sambuc {
1590c3ae37fSLionel Sambuc
1600c3ae37fSLionel Sambuc _DIAGASSERT(cur_term != NULL);
1610c3ae37fSLionel Sambuc if (cur_term->desc == NULL)
1620c3ae37fSLionel Sambuc return __UNCONST(nullname);
1630c3ae37fSLionel Sambuc return __UNCONST(cur_term->desc);
16451e66a47SVivek Prakash }
165