NetBSD/lib/libcurses/setterm.c

215 lines
5.5 KiB
C

/*
* Copyright (c) 1981 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
#ifndef lint
/*static char sccsid[] = "from: @(#)setterm.c 5.9 (Berkeley) 8/23/92";*/
static char rcsid[] = "$Id: setterm.c,v 1.1 1993/08/07 05:51:11 mycroft Exp $";
#endif /* not lint */
#include <sys/ioctl.h>
#include <curses.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static void zap __P((void));
static char *sflags[] = {
&AM, &BS, &DA, &EO, &HC, &HZ, &IN, &MI, &MS,
&NC, &NS, &OS, &UL, &XB, &XN, &XT, &XS, &XX
};
static char *_PC,
**sstrs[] = {
&AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS,
&DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2,
&K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC,
&IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU,
&LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF,
&SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US,
&VB, &VS, &VE, &AL_PARM, &DL_PARM, &UP_PARM,
&DOWN_PARM, &LEFT_PARM, &RIGHT_PARM,
};
static char *aoftspace; /* Address of _tspace for relocation */
static char tspace[2048]; /* Space for capability strings */
static int destcol, destline;
char *ttytype;
int
setterm(type)
register char *type;
{
static char genbuf[1024];
static char __ttytype[1024];
register int unknown;
struct winsize win;
char *p;
#ifdef DEBUG
__TRACE("setterm: (\"%s\")\nLINES = %d, COLS = %d\n",
type, LINES, COLS);
#endif
if (type[0] == '\0')
type = "xx";
unknown = 0;
if (tgetent(genbuf, type) != 1) {
unknown++;
strcpy(genbuf, "xx|dumb:");
}
#ifdef DEBUG
__TRACE("setterm: tty = %s\n", type);
#endif
/* Try TIOCGWINSZ, and, if it fails, the termcap entry. */
if (ioctl(STDERR_FILENO, TIOCGWINSZ, &win) != -1 &&
win.ws_row != 0 && win.ws_col != 0) {
LINES = win.ws_row;
COLS = win.ws_col;
} else {
LINES = tgetnum("li");
COLS = tgetnum("co");
}
/* POSIX 1003.2 requires that the environment override. */
if ((p = getenv("ROWS")) != NULL)
LINES = strtol(p, NULL, 10);
if ((p = getenv("COLUMNS")) != NULL)
COLS = strtol(p, NULL, 10);
/*
* XXX
* Historically, curses fails if rows <= 5, cols <= 4.
*/
if (LINES <= 5 || COLS <= 4)
return (ERR);
#ifdef DEBUG
__TRACE("setterm: LINES = %d, COLS = %d\n", LINES, COLS);
#endif
aoftspace = tspace;
zap(); /* Get terminal description. */
/* Handle funny termcap capabilities. */
if (CS && SC && RC)
AL = DL = "";
if (AL_PARM && AL == NULL)
AL = "";
if (DL_PARM && DL == NULL)
DL = "";
if (IC) {
if (IM == NULL)
IM = "";
if (EI == NULL)
EI = "";
}
if (!GT) /* If we can't tab, we can't backtab either. */
BT = NULL;
if (tgoto(CM, destcol, destline)[0] == 'O') {
CA = 0;
CM = 0;
} else
CA = 1;
PC = _PC ? _PC[0] : 0;
aoftspace = tspace;
ttytype = longname(genbuf, __ttytype);
return (unknown ? ERR : OK);
}
/*
* zap --
* Gets all the terminal flags from the termcap database.
*/
static void
zap()
{
register char *namp, ***sp;
register char **fp;
#ifdef DEBUG
register char *cp;
#endif
namp = "ambsdadbeohchzinmimsncnsosulxbxnxtxsxx";
fp = sflags;
do {
*(*fp++) = tgetflag(namp);
#ifdef DEBUG
__TRACE("2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE");
#endif
namp += 2;
} while (*namp);
namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullmandnlpcrcscsesfsosrtatetiucueupusvbvsveALDLUPDOLERI";
sp = sstrs;
do {
*(*sp++) = tgetstr(namp, &aoftspace);
#ifdef DEBUG
__TRACE("2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\"");
if (*sp[-1] != NULL) {
for (cp = *sp[-1]; *cp; cp++)
__TRACE("%s", unctrl(*cp));
__TRACE("\"\n");
}
#endif
namp += 2;
} while (*namp);
if (XS)
SO = SE = NULL;
else {
if (tgetnum("sg") > 0)
SO = NULL;
if (tgetnum("ug") > 0)
US = NULL;
if (!SO && US) {
SO = US;
SE = UE;
}
}
}
/*
* getcap --
* Return a capability from termcap.
*/
char *
getcap(name)
char *name;
{
return (tgetstr(name, &aoftspace));
}