#include <sys/cdefs.h>
__RCSID("$NetBSD: curterm.c,v 1.14 2020/05/30 16:03:58 roy Exp $");
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <term_private.h>
#include <term.h>
#include <termios.h>
#include <stdio.h>
TERMINAL *cur_term;
#define NAMESIZE 256
char ttytype[NAMESIZE];
static const speed_t bauds[] = {
0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 2400, 4800, 9600,
19200, 38400, 57600, 115200, 230400, 460800, 921600
};
void
_ti_setospeed(TERMINAL *term)
{
struct termios termios;
speed_t os;
size_t i;
_DIAGASSERT(term != NULL);
term->_ospeed = 0;
if (tcgetattr(term->fildes, &termios) == 0) {
os = cfgetospeed(&termios);
for (i = 0; i < __arraycount(bauds); i++)
if (bauds[i] == os) {
term->_ospeed = (short)i;
break;
}
}
}
TERMINAL *
set_curterm(TERMINAL *nterm)
{
TERMINAL *oterm;
size_t l, n;
char *p;
oterm = cur_term;
cur_term = nterm;
ospeed = 0;
if (cur_term == NULL)
PC = '\0';
else {
if (pad_char == NULL)
PC = '\0';
else
PC = *pad_char;
_ti_setospeed(nterm);
ospeed = nterm->_ospeed;
p = ttytype;
l = sizeof(ttytype);
if ((n = strlcpy(p, nterm->name, l)) < l) {
p += n;
l -= n;
*p++ = '|';
l--;
if (nterm->_alias != NULL &&
(n = strlcpy(p, nterm->_alias, l)) < l)
{
p += n;
l -= n;
*p++ = '|';
l--;
}
if (nterm->desc != NULL &&
(n = strlcpy(p, nterm->desc, l)) < l)
{
p += n;
l -= n;
*p++ = '|';
l--;
}
p--;
}
*p = '\0';
}
return oterm;
}
int
del_curterm(TERMINAL *oterm)
{
if (oterm == NULL)
return ERR;
free(oterm->_area);
free(oterm->strs);
free(oterm->nums);
free(oterm->flags);
free(oterm->_userdefs);
free(oterm->_buf);
free(oterm);
if (oterm == cur_term)
cur_term = NULL;
return OK;
}
char *
termname(void)
{
_DIAGASSERT(cur_term != NULL);
return __UNCONST(cur_term->name);
}
static const char * nullname = "";
char *
longname(void)
{
_DIAGASSERT(cur_term != NULL);
if (cur_term->desc == NULL)
return __UNCONST(nullname);
return __UNCONST(cur_term->desc);
}