1*87798259Sroy /* $NetBSD: curterm.c,v 1.14 2020/05/30 16:03:58 roy Exp $ */
24ca00e00Sroy
34ca00e00Sroy /*
4fd2d10c6Sroy * Copyright (c) 2009, 2011 The NetBSD Foundation, Inc.
54ca00e00Sroy *
64ca00e00Sroy * This code is derived from software contributed to The NetBSD Foundation
74ca00e00Sroy * by Roy Marples.
84ca00e00Sroy *
94ca00e00Sroy * Redistribution and use in source and binary forms, with or without
104ca00e00Sroy * modification, are permitted provided that the following conditions
114ca00e00Sroy * are met:
124ca00e00Sroy * 1. Redistributions of source code must retain the above copyright
134ca00e00Sroy * notice, this list of conditions and the following disclaimer.
144ca00e00Sroy * 2. Redistributions in binary form must reproduce the above copyright
154ca00e00Sroy * notice, this list of conditions and the following disclaimer in the
164ca00e00Sroy * documentation and/or other materials provided with the distribution.
174ca00e00Sroy *
184ca00e00Sroy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
194ca00e00Sroy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
204ca00e00Sroy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
214ca00e00Sroy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
224ca00e00Sroy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
234ca00e00Sroy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
244ca00e00Sroy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
254ca00e00Sroy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
264ca00e00Sroy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
274ca00e00Sroy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
284ca00e00Sroy */
294ca00e00Sroy
304ca00e00Sroy #include <sys/cdefs.h>
31*87798259Sroy __RCSID("$NetBSD: curterm.c,v 1.14 2020/05/30 16:03:58 roy Exp $");
324ca00e00Sroy
334ca00e00Sroy #include <assert.h>
344ca00e00Sroy #include <stdlib.h>
3562480e1cSroy #include <string.h>
364ca00e00Sroy #include <term_private.h>
374ca00e00Sroy #include <term.h>
384ca00e00Sroy #include <termios.h>
3990cead5eSroy #include <stdio.h>
4062480e1cSroy
414ca00e00Sroy TERMINAL *cur_term;
424ca00e00Sroy
4362480e1cSroy /*
4462480e1cSroy * There is no standard way of getting a list of aliases for the
4562480e1cSroy * terminal. However, some applications such as telnet want to know this.
4662480e1cSroy * ncurses dumps the terminfo header into an undefined variable ttytype
4762480e1cSroy * and these applications then parse it to work out the aliases.
4862480e1cSroy * We should do the same for now, until a standard mechanism for getting
4962480e1cSroy * the information is available or the need for it goes away.
5062480e1cSroy */
5162480e1cSroy #define NAMESIZE 256
5262480e1cSroy char ttytype[NAMESIZE];
5362480e1cSroy
544ca00e00Sroy static const speed_t bauds[] = {
554ca00e00Sroy 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 2400, 4800, 9600,
564ca00e00Sroy 19200, 38400, 57600, 115200, 230400, 460800, 921600
574ca00e00Sroy };
584ca00e00Sroy
594ca00e00Sroy void
_ti_setospeed(TERMINAL * term)604ca00e00Sroy _ti_setospeed(TERMINAL *term)
614ca00e00Sroy {
624ca00e00Sroy struct termios termios;
634ca00e00Sroy speed_t os;
644ca00e00Sroy size_t i;
654ca00e00Sroy
664ca00e00Sroy _DIAGASSERT(term != NULL);
674ca00e00Sroy
684ca00e00Sroy term->_ospeed = 0;
694ca00e00Sroy if (tcgetattr(term->fildes, &termios) == 0) {
704ca00e00Sroy os = cfgetospeed(&termios);
714ca00e00Sroy for (i = 0; i < __arraycount(bauds); i++)
724ca00e00Sroy if (bauds[i] == os) {
7304f58b48Sroy term->_ospeed = (short)i;
744ca00e00Sroy break;
754ca00e00Sroy }
764ca00e00Sroy }
774ca00e00Sroy }
784ca00e00Sroy
794ca00e00Sroy TERMINAL *
set_curterm(TERMINAL * nterm)804ca00e00Sroy set_curterm(TERMINAL *nterm)
814ca00e00Sroy {
824ca00e00Sroy TERMINAL *oterm;
8362480e1cSroy size_t l, n;
8462480e1cSroy char *p;
854ca00e00Sroy
864ca00e00Sroy oterm = cur_term;
874ca00e00Sroy cur_term = nterm;
884ca00e00Sroy
894ca00e00Sroy ospeed = 0;
904ca00e00Sroy if (cur_term == NULL)
914ca00e00Sroy PC = '\0';
924ca00e00Sroy else {
934ca00e00Sroy if (pad_char == NULL)
944ca00e00Sroy PC = '\0';
954ca00e00Sroy else
964ca00e00Sroy PC = *pad_char;
974ca00e00Sroy _ti_setospeed(nterm);
984ca00e00Sroy ospeed = nterm->_ospeed;
9962480e1cSroy
10062480e1cSroy p = ttytype;
10162480e1cSroy l = sizeof(ttytype);
102*87798259Sroy if ((n = strlcpy(p, nterm->name, l)) < l) {
10362480e1cSroy p += n;
10462480e1cSroy l -= n;
10562480e1cSroy *p++ = '|';
10662480e1cSroy l--;
107*87798259Sroy if (nterm->_alias != NULL &&
108*87798259Sroy (n = strlcpy(p, nterm->_alias, l)) < l)
10962480e1cSroy {
11062480e1cSroy p += n;
11162480e1cSroy l -= n;
11262480e1cSroy *p++ = '|';
11362480e1cSroy l--;
11462480e1cSroy }
115*87798259Sroy if (nterm->desc != NULL &&
116*87798259Sroy (n = strlcpy(p, nterm->desc, l)) < l)
11762480e1cSroy {
11862480e1cSroy p += n;
11962480e1cSroy l -= n;
12062480e1cSroy *p++ = '|';
12162480e1cSroy l--;
12262480e1cSroy }
12362480e1cSroy p--;
12462480e1cSroy }
12562480e1cSroy *p = '\0';
1264ca00e00Sroy }
1274ca00e00Sroy
1284ca00e00Sroy return oterm;
1294ca00e00Sroy }
1304ca00e00Sroy
1314ca00e00Sroy int
del_curterm(TERMINAL * oterm)1324ca00e00Sroy del_curterm(TERMINAL *oterm)
1334ca00e00Sroy {
134fde317d2Sroy
13591ab69b0Sroy if (oterm == NULL)
13691ab69b0Sroy return ERR;
13791ab69b0Sroy free(oterm->_area);
13891ab69b0Sroy free(oterm->strs);
13991ab69b0Sroy free(oterm->nums);
14091ab69b0Sroy free(oterm->flags);
14191ab69b0Sroy free(oterm->_userdefs);
1427ddef863Schristos free(oterm->_buf);
14391ab69b0Sroy free(oterm);
1448b22ec00Schristos if (oterm == cur_term)
1458b22ec00Schristos cur_term = NULL;
14691ab69b0Sroy return OK;
1474ca00e00Sroy }
148fd2d10c6Sroy
149fd2d10c6Sroy char *
termname(void)150fd2d10c6Sroy termname(void)
151fd2d10c6Sroy {
152fd2d10c6Sroy
153fd2d10c6Sroy _DIAGASSERT(cur_term != NULL);
154fd2d10c6Sroy return __UNCONST(cur_term->name);
155fd2d10c6Sroy }
15664afaaccSroy
15727861734Sjoerg static const char * nullname = "";
15864afaaccSroy
15964afaaccSroy char *
longname(void)16064afaaccSroy longname(void)
16164afaaccSroy {
16264afaaccSroy
16364afaaccSroy _DIAGASSERT(cur_term != NULL);
16464afaaccSroy if (cur_term->desc == NULL)
16564afaaccSroy return __UNCONST(nullname);
16664afaaccSroy return __UNCONST(cur_term->desc);
16764afaaccSroy }
168