xref: /csrg-svn/lib/libcurses/setterm.c (revision 38371)
12243Sarnold /*
234677Sbostic  * Copyright (c) 1981 Regents of the University of California.
334677Sbostic  * All rights reserved.
434677Sbostic  *
534677Sbostic  * Redistribution and use in source and binary forms are permitted
634924Sbostic  * provided that the above copyright notice and this paragraph are
734924Sbostic  * duplicated in all such forms and that any documentation,
834924Sbostic  * advertising materials, and other materials related to such
934924Sbostic  * distribution and use acknowledge that the software was developed
1034924Sbostic  * by the University of California, Berkeley.  The name of the
1134924Sbostic  * University may not be used to endorse or promote products derived
1234924Sbostic  * from this software without specific prior written permission.
1334924Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434924Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534924Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1622656Sdist  */
1722656Sdist 
1822656Sdist #ifndef lint
19*38371Sbostic static char sccsid[] = "@(#)setterm.c	5.6 (Berkeley) 06/29/89";
2034677Sbostic #endif /* not lint */
2122656Sdist 
2222656Sdist /*
232243Sarnold  * Terminal initialization routines.
242243Sarnold  *
252243Sarnold  */
262243Sarnold 
272243Sarnold # include	"curses.ext"
282243Sarnold 
2919875Sbloom static bool	*sflags[] = {
3019875Sbloom 			&AM, &BS, &DA, &DB, &EO, &HC, &HZ, &IN, &MI,
3121040Sbloom 			&MS, &NC, &NS, &OS, &UL, &XB, &XN, &XT, &XS,
3221040Sbloom 			&XX
332243Sarnold 		};
342243Sarnold 
3519875Sbloom static char	*_PC,
3619875Sbloom 		**sstrs[] = {
3719875Sbloom 			&AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS,
3819875Sbloom 			&DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2,
3919875Sbloom 			&K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC,
4019875Sbloom 			&IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU,
4119875Sbloom 			&LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF,
4219875Sbloom 			&SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US,
4319875Sbloom 			&VB, &VS, &VE, &AL_PARM, &DL_PARM, &UP_PARM,
4419875Sbloom 			&DOWN_PARM, &LEFT_PARM, &RIGHT_PARM,
452243Sarnold 		},
462465Sarnold 		*tgoto();
472243Sarnold 
4819875Sbloom char		_tspace[2048];		/* Space for capability strings */
492243Sarnold 
5019875Sbloom static char	*aoftspace;		/* Address of _tspace for relocation */
5119875Sbloom 
522243Sarnold static int	destcol, destline;
532243Sarnold 
542243Sarnold /*
552243Sarnold  *	This routine does terminal type initialization routines, and
562243Sarnold  * calculation of flags at entry.  It is almost entirely stolen from
572243Sarnold  * Bill Joy's ex version 2.6.
582243Sarnold  */
592243Sarnold short	ospeed = -1;
602243Sarnold 
612243Sarnold gettmode() {
622243Sarnold 
63*38371Sbostic 	if (ioctl(_tty_ch, TIOCGETP, &_tty) < 0)
642243Sarnold 		return;
652243Sarnold 	savetty();
66*38371Sbostic 	if (ioctl(_tty_ch, TIOCSETP, &_tty) < 0)
672243Sarnold 		_tty.sg_flags = _res_flg;
682243Sarnold 	ospeed = _tty.sg_ospeed;
692243Sarnold 	_res_flg = _tty.sg_flags;
702243Sarnold 	UPPERCASE = (_tty.sg_flags & LCASE) != 0;
712243Sarnold 	GT = ((_tty.sg_flags & XTABS) == 0);
722243Sarnold 	NONL = ((_tty.sg_flags & CRMOD) == 0);
733631Sarnold 	_tty.sg_flags &= ~XTABS;
74*38371Sbostic 	ioctl(_tty_ch, TIOCSETP, &_tty);
752243Sarnold # ifdef DEBUG
762243Sarnold 	fprintf(outf, "GETTMODE: UPPERCASE = %s\n", UPPERCASE ? "TRUE":"FALSE");
772243Sarnold 	fprintf(outf, "GETTMODE: GT = %s\n", GT ? "TRUE" : "FALSE");
782243Sarnold 	fprintf(outf, "GETTMODE: NONL = %s\n", NONL ? "TRUE" : "FALSE");
792243Sarnold 	fprintf(outf, "GETTMODE: ospeed = %d\n", ospeed);
802243Sarnold # endif
812243Sarnold }
822243Sarnold 
832243Sarnold setterm(type)
842243Sarnold reg char	*type; {
852243Sarnold 
8611736Sarnold 	reg int		unknown;
8711736Sarnold 	static char	genbuf[1024];
8819875Sbloom # ifdef TIOCGWINSZ
8918032Sbloom 	struct winsize win;
9019875Sbloom # endif
912243Sarnold 
922243Sarnold # ifdef DEBUG
932243Sarnold 	fprintf(outf, "SETTERM(\"%s\")\n", type);
942243Sarnold 	fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
952243Sarnold # endif
962243Sarnold 	if (type[0] == '\0')
972243Sarnold 		type = "xx";
982243Sarnold 	unknown = FALSE;
992243Sarnold 	if (tgetent(genbuf, type) != 1) {
1002243Sarnold 		unknown++;
1012243Sarnold 		strcpy(genbuf, "xx|dumb:");
1022243Sarnold 	}
1032243Sarnold # ifdef DEBUG
1042243Sarnold 	fprintf(outf, "SETTERM: tty = %s\n", type);
1052243Sarnold # endif
10619875Sbloom # ifdef TIOCGWINSZ
10718032Sbloom 	if (ioctl(_tty_ch, TIOCGWINSZ, &win) >= 0) {
10818032Sbloom 		if (LINES == 0)
10918032Sbloom 			LINES = win.ws_row;
11018032Sbloom 		if (COLS == 0)
11118032Sbloom 			COLS = win.ws_col;
11218032Sbloom 	}
11319875Sbloom # endif
11418032Sbloom 
1152243Sarnold 	if (LINES == 0)
1162243Sarnold 		LINES = tgetnum("li");
1172243Sarnold 	if (LINES <= 5)
1182243Sarnold 		LINES = 24;
1192243Sarnold 
1202243Sarnold 	if (COLS == 0)
1212243Sarnold 		COLS = tgetnum("co");
1222243Sarnold 	if (COLS <= 4)
1232243Sarnold 		COLS = 80;
12412570Sarnold 
1252243Sarnold # ifdef DEBUG
1262243Sarnold 	fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
1272243Sarnold # endif
12819875Sbloom 	aoftspace = _tspace;
1292243Sarnold 	zap();			/* get terminal description		*/
13019875Sbloom 
13119875Sbloom 	/*
13219875Sbloom 	 * Handle funny termcap capabilities
13319875Sbloom 	 */
13419875Sbloom 	if (CS && SC && RC) AL=DL="";
13519875Sbloom 	if (AL_PARM && AL==NULL) AL="";
13619875Sbloom 	if (DL_PARM && DL==NULL) DL="";
13719875Sbloom 	if (IC && IM==NULL) IM="";
13819875Sbloom 	if (IC && EI==NULL) EI="";
13919875Sbloom 	if (!GT) BT=NULL;	/* If we can't tab, we can't backtab either */
14019875Sbloom 
1412243Sarnold 	if (tgoto(CM, destcol, destline)[0] == 'O')
1422243Sarnold 		CA = FALSE, CM = 0;
1432243Sarnold 	else
1442243Sarnold 		CA = TRUE;
14519875Sbloom 
14619875Sbloom 	PC = _PC ? _PC[0] : FALSE;
14719875Sbloom 	aoftspace = _tspace;
14837420Sbostic 	{
14937420Sbostic 		/* xtype should be the same size as genbuf for longname(). */
15037420Sbostic 		static char xtype[1024];
15137420Sbostic 
15237420Sbostic 		(void)strcpy(xtype,type);
15337420Sbostic 		strncpy(ttytype, longname(genbuf, xtype), sizeof(ttytype) - 1);
15437420Sbostic 	}
15525427Sbloom 	ttytype[sizeof(ttytype) - 1] = '\0';
1562243Sarnold 	if (unknown)
1572243Sarnold 		return ERR;
1582243Sarnold 	return OK;
1592243Sarnold }
16019875Sbloom 
1612243Sarnold /*
1624616Sarnold  *	This routine gets all the terminal flags from the termcap database
1632243Sarnold  */
1642243Sarnold 
16519875Sbloom zap()
16619875Sbloom {
16719875Sbloom 	register char	*namp;
16819875Sbloom 	register bool	**fp;
16919875Sbloom 	register char	***sp;
17019875Sbloom #ifdef	DEBUG
17119875Sbloom 	register char	*cp;
17219875Sbloom #endif
1732243Sarnold 	extern char	*tgetstr();
1742243Sarnold 
17521040Sbloom 	namp = "ambsdadbeohchzinmimsncnsosulxbxnxtxsxx";
1762243Sarnold 	fp = sflags;
1772243Sarnold 	do {
1782243Sarnold 		*(*fp++) = tgetflag(namp);
17919875Sbloom #ifdef DEBUG
18019875Sbloom 		fprintf(outf, "%2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE");
18119875Sbloom #endif
1822243Sarnold 		namp += 2;
1832243Sarnold 	} while (*namp);
18419875Sbloom 	namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullmandnlpcrcscsesfsosrtatetiucueupusvbvsveALDLUPDOLERI";
1852243Sarnold 	sp = sstrs;
1862243Sarnold 	do {
1872243Sarnold 		*(*sp++) = tgetstr(namp, &aoftspace);
18819875Sbloom #ifdef DEBUG
18919875Sbloom 		fprintf(outf, "%2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\"");
19019875Sbloom 		if (*sp[-1] != NULL) {
19119875Sbloom 			for (cp = *sp[-1]; *cp; cp++)
19219875Sbloom 				fprintf(outf, "%s", unctrl(*cp));
19319875Sbloom 			fprintf(outf, "\"\n");
19419875Sbloom 		}
19519875Sbloom #endif
1962243Sarnold 		namp += 2;
1972243Sarnold 	} while (*namp);
19821040Sbloom 	if (XS)
19921040Sbloom 		SO = SE = NULL;
20021040Sbloom 	else {
20121040Sbloom 		if (tgetnum("sg") > 0)
20221040Sbloom 			SO = NULL;
20321040Sbloom 		if (tgetnum("ug") > 0)
20421040Sbloom 			US = NULL;
20521040Sbloom 		if (!SO && US) {
20621040Sbloom 			SO = US;
20721040Sbloom 			SE = UE;
20821040Sbloom 		}
2092243Sarnold 	}
2102243Sarnold }
2113786Sarnold 
2123786Sarnold /*
2133786Sarnold  * return a capability from termcap
2143786Sarnold  */
2153786Sarnold char *
2163786Sarnold getcap(name)
2173786Sarnold char *name;
2183786Sarnold {
2194616Sarnold 	char *tgetstr();
2204615Sarnold 
2214616Sarnold 	return tgetstr(name, &aoftspace);
2223786Sarnold }
223