/*
* Copyright (c) 1981, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
int
_cursesi_setterm(const char *type, SCREEN *screen)
{
int unknown, r;
char *p;
if (type[0] == '\0')
type = "xx";
unknown = 0;
if (screen->term)
del_curterm(screen->term);
(void)ti_setupterm(&screen->term, type, fileno(screen->outfd), &r);
if (screen->term == NULL) {
unknown++;
(void)ti_setupterm(&screen->term, "dumb",
fileno(screen->outfd), &r);
/* No dumb term? We can't continue */
if (screen->term == NULL)
return ERR;
}
__CTRACE(__CTRACE_INIT, "setterm: tty = %s\n", type);
/* lines and cols will have been setup correctly by ti_setupterm(3). */
screen->LINES = t_lines(screen->term);
screen->COLS = t_columns(screen->term);
if (screen->filtered) {
/* Disable use of clear, cud, cud1, cup, cuu1 and vpa. */
screen->term->strs[TICODE_clear] = NULL;
screen->term->strs[TICODE_cud] = NULL;
screen->term->strs[TICODE_cud1] = NULL;
screen->term->strs[TICODE_cup] = NULL;
screen->term->strs[TICODE_cuu] = NULL;
screen->term->strs[TICODE_cuu1] = NULL;
screen->term->strs[TICODE_vpa] = NULL;
/* Set the value of the home string to the value of
* the cr string. */
screen->term->strs[TICODE_home] = screen->term->strs[TICODE_cr];
/* Set lines equal to 1. */
screen->LINES = 1;
t_lines(screen->term) = 1;
}
__CTRACE(__CTRACE_INIT, "setterm: filtered %d\n", screen->filtered);
if ((p = getenv("ESCDELAY")) != NULL)
screen->ESCDELAY = (int)strtol(p, NULL, 0);
else
screen->ESCDELAY = ESCDELAY_DEFAULT;
if ((p = getenv("TABSIZE")) != NULL)
screen->TABSIZE = (int)strtol(p, NULL, 0);
else if (t_init_tabs(screen->term) >= 0)
screen->TABSIZE = (int)t_init_tabs(screen->term);
else
screen->TABSIZE = TABSIZE_DEFAULT;
/*
* Want cols > 4, otherwise things will fail.
*/
if (screen->COLS <= 4)
return ERR;
/*
* set the pad char, only take the first char of the pc capability
* as this is all we can use.
*/
screen->padchar = t_pad_char(screen->term) ?
t_pad_char(screen->term)[0] : 0;
/*
* _cursesi_resetterm --
* Copy the terminal instance data for the given screen to the global
* variables.
*/
void
_cursesi_resetterm(SCREEN *screen)
{
/*
* does_esc_m --
* A hack for xterm-like terminals where "\E[m" or "\E[0m" will turn off
* other attributes, so we check for this in the capability passed to us.
* Note that we can't just do simple string comparison, as the capability
* may contain multiple, ';' separated sequence parts.
*/
static int
does_esc_m(const char *cap)
{
#define WAITING 0
#define PARSING 1
#define NUMBER 2
#define FOUND 4
const char *capptr;
int seq;
__CTRACE(__CTRACE_INIT, "does_esc_m: Checking %s\n", cap);
/* Is it just "\E[m" or "\E[0m"? */
if (!strcmp(cap, "\x1b[m") || !strcmp(cap, "\x1b[0m"))
return 1;
/* Does it contain a "\E[...m" sequence? */
if (strlen(cap) < 4)
return 0;
capptr = cap;
seq = WAITING;
while (*capptr != 0) {
switch (seq) {
/* Start of sequence */
case WAITING:
if (!strncmp(capptr, "\x1b[", 2)) {
capptr+=2;
if (*capptr == 'm')
return 1;
else {
seq=PARSING;
continue;
}
}
break;
/* Looking for '0' */
case PARSING:
if (*capptr == '0')
seq=FOUND;
else if (*capptr > '0' && *capptr <= '9')
seq=NUMBER;
else if (*capptr != ';')
seq=WAITING;
break;
/* Some other number */
case NUMBER:
if (*capptr == ';')
seq=PARSING;
else if (!(*capptr >= '0' && *capptr <= '9'))
seq=WAITING;
break;
/* Found a '0' */
case FOUND:
if (*capptr == 'm')
return 1;
else if (!((*capptr >= '0' && *capptr <= '9') ||
*capptr == ';'))
seq=WAITING;
break;
default:
break;
}
capptr++;
}
return 0;
}
/*
* capdup_nodelay --
* A helper for does_ctrl_o below that creates a copy of the given
* capability with delay specifications dropped.
*/
static char *
capdup_nodelay(const char *src)
{
char *clean, *dst;
while (*src != '\0') {
if (src[0] == '$' && src[1] == '<') {
const char *end = strchr(src + 2, '>');
if (__predict_true(end != NULL)) {
src = end + 1;
continue;
}
}
*dst++ = *src++;
}
*dst = '\0';
return clean;
}
/*
* does_ctrl_o --
* A hack for vt100/xterm-like terminals where the "me" capability also
* unsets acs.
*/
static int
does_ctrl_o(const char *exit_cap, const char *acs_cap)
{
char *eptr, *aptr;
int res;
__CTRACE(__CTRACE_INIT, "does_ctrl_o: Testing %s for %s\n",
exit_cap, acs_cap);
eptr = capdup_nodelay(exit_cap);
if (__predict_false(eptr == NULL))
return 0;