xref: /csrg-svn/lib/libcurses/setterm.c (revision 42653)
12243Sarnold /*
234677Sbostic  * Copyright (c) 1981 Regents of the University of California.
334677Sbostic  * All rights reserved.
434677Sbostic  *
5*42653Sbostic  * %sccs.include.redist.c%
622656Sdist  */
722656Sdist 
822656Sdist #ifndef lint
9*42653Sbostic static char sccsid[] = "@(#)setterm.c	5.8 (Berkeley) 06/01/90";
1034677Sbostic #endif /* not lint */
1122656Sdist 
1222656Sdist /*
132243Sarnold  * Terminal initialization routines.
142243Sarnold  *
152243Sarnold  */
162243Sarnold 
172243Sarnold # include	"curses.ext"
182243Sarnold 
1919875Sbloom static bool	*sflags[] = {
2019875Sbloom 			&AM, &BS, &DA, &DB, &EO, &HC, &HZ, &IN, &MI,
2121040Sbloom 			&MS, &NC, &NS, &OS, &UL, &XB, &XN, &XT, &XS,
2221040Sbloom 			&XX
232243Sarnold 		};
242243Sarnold 
2519875Sbloom static char	*_PC,
2619875Sbloom 		**sstrs[] = {
2719875Sbloom 			&AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS,
2819875Sbloom 			&DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2,
2919875Sbloom 			&K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC,
3019875Sbloom 			&IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU,
3119875Sbloom 			&LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF,
3219875Sbloom 			&SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US,
3319875Sbloom 			&VB, &VS, &VE, &AL_PARM, &DL_PARM, &UP_PARM,
3419875Sbloom 			&DOWN_PARM, &LEFT_PARM, &RIGHT_PARM,
3539140Sbostic 		};
362243Sarnold 
3739140Sbostic extern char	*tgoto();
3839140Sbostic 
3919875Sbloom char		_tspace[2048];		/* Space for capability strings */
402243Sarnold 
4119875Sbloom static char	*aoftspace;		/* Address of _tspace for relocation */
4219875Sbloom 
432243Sarnold static int	destcol, destline;
442243Sarnold 
452243Sarnold /*
462243Sarnold  *	This routine does terminal type initialization routines, and
472243Sarnold  * calculation of flags at entry.  It is almost entirely stolen from
482243Sarnold  * Bill Joy's ex version 2.6.
492243Sarnold  */
502243Sarnold short	ospeed = -1;
512243Sarnold 
522243Sarnold gettmode() {
532243Sarnold 
5438371Sbostic 	if (ioctl(_tty_ch, TIOCGETP, &_tty) < 0)
552243Sarnold 		return;
562243Sarnold 	savetty();
5738371Sbostic 	if (ioctl(_tty_ch, TIOCSETP, &_tty) < 0)
582243Sarnold 		_tty.sg_flags = _res_flg;
592243Sarnold 	ospeed = _tty.sg_ospeed;
602243Sarnold 	_res_flg = _tty.sg_flags;
612243Sarnold 	UPPERCASE = (_tty.sg_flags & LCASE) != 0;
622243Sarnold 	GT = ((_tty.sg_flags & XTABS) == 0);
632243Sarnold 	NONL = ((_tty.sg_flags & CRMOD) == 0);
643631Sarnold 	_tty.sg_flags &= ~XTABS;
6538371Sbostic 	ioctl(_tty_ch, TIOCSETP, &_tty);
662243Sarnold # ifdef DEBUG
672243Sarnold 	fprintf(outf, "GETTMODE: UPPERCASE = %s\n", UPPERCASE ? "TRUE":"FALSE");
682243Sarnold 	fprintf(outf, "GETTMODE: GT = %s\n", GT ? "TRUE" : "FALSE");
692243Sarnold 	fprintf(outf, "GETTMODE: NONL = %s\n", NONL ? "TRUE" : "FALSE");
702243Sarnold 	fprintf(outf, "GETTMODE: ospeed = %d\n", ospeed);
712243Sarnold # endif
722243Sarnold }
732243Sarnold 
742243Sarnold setterm(type)
752243Sarnold reg char	*type; {
762243Sarnold 
7711736Sarnold 	reg int		unknown;
7811736Sarnold 	static char	genbuf[1024];
7919875Sbloom # ifdef TIOCGWINSZ
8018032Sbloom 	struct winsize win;
8119875Sbloom # endif
822243Sarnold 
832243Sarnold # ifdef DEBUG
842243Sarnold 	fprintf(outf, "SETTERM(\"%s\")\n", type);
852243Sarnold 	fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
862243Sarnold # endif
872243Sarnold 	if (type[0] == '\0')
882243Sarnold 		type = "xx";
892243Sarnold 	unknown = FALSE;
902243Sarnold 	if (tgetent(genbuf, type) != 1) {
912243Sarnold 		unknown++;
922243Sarnold 		strcpy(genbuf, "xx|dumb:");
932243Sarnold 	}
942243Sarnold # ifdef DEBUG
952243Sarnold 	fprintf(outf, "SETTERM: tty = %s\n", type);
962243Sarnold # endif
9719875Sbloom # ifdef TIOCGWINSZ
9818032Sbloom 	if (ioctl(_tty_ch, TIOCGWINSZ, &win) >= 0) {
9918032Sbloom 		if (LINES == 0)
10018032Sbloom 			LINES = win.ws_row;
10118032Sbloom 		if (COLS == 0)
10218032Sbloom 			COLS = win.ws_col;
10318032Sbloom 	}
10419875Sbloom # endif
10518032Sbloom 
1062243Sarnold 	if (LINES == 0)
1072243Sarnold 		LINES = tgetnum("li");
1082243Sarnold 	if (LINES <= 5)
1092243Sarnold 		LINES = 24;
1102243Sarnold 
1112243Sarnold 	if (COLS == 0)
1122243Sarnold 		COLS = tgetnum("co");
1132243Sarnold 	if (COLS <= 4)
1142243Sarnold 		COLS = 80;
11512570Sarnold 
1162243Sarnold # ifdef DEBUG
1172243Sarnold 	fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
1182243Sarnold # endif
11919875Sbloom 	aoftspace = _tspace;
1202243Sarnold 	zap();			/* get terminal description		*/
12119875Sbloom 
12219875Sbloom 	/*
12319875Sbloom 	 * Handle funny termcap capabilities
12419875Sbloom 	 */
12519875Sbloom 	if (CS && SC && RC) AL=DL="";
12619875Sbloom 	if (AL_PARM && AL==NULL) AL="";
12719875Sbloom 	if (DL_PARM && DL==NULL) DL="";
12819875Sbloom 	if (IC && IM==NULL) IM="";
12919875Sbloom 	if (IC && EI==NULL) EI="";
13019875Sbloom 	if (!GT) BT=NULL;	/* If we can't tab, we can't backtab either */
13119875Sbloom 
1322243Sarnold 	if (tgoto(CM, destcol, destline)[0] == 'O')
1332243Sarnold 		CA = FALSE, CM = 0;
1342243Sarnold 	else
1352243Sarnold 		CA = TRUE;
13619875Sbloom 
13719875Sbloom 	PC = _PC ? _PC[0] : FALSE;
13819875Sbloom 	aoftspace = _tspace;
13937420Sbostic 	{
14037420Sbostic 		/* xtype should be the same size as genbuf for longname(). */
14137420Sbostic 		static char xtype[1024];
14237420Sbostic 
14337420Sbostic 		(void)strcpy(xtype,type);
14437420Sbostic 		strncpy(ttytype, longname(genbuf, xtype), sizeof(ttytype) - 1);
14537420Sbostic 	}
14625427Sbloom 	ttytype[sizeof(ttytype) - 1] = '\0';
1472243Sarnold 	if (unknown)
1482243Sarnold 		return ERR;
1492243Sarnold 	return OK;
1502243Sarnold }
15119875Sbloom 
1522243Sarnold /*
1534616Sarnold  *	This routine gets all the terminal flags from the termcap database
1542243Sarnold  */
1552243Sarnold 
15619875Sbloom zap()
15719875Sbloom {
15819875Sbloom 	register char	*namp;
15919875Sbloom 	register bool	**fp;
16019875Sbloom 	register char	***sp;
16119875Sbloom #ifdef	DEBUG
16219875Sbloom 	register char	*cp;
16319875Sbloom #endif
1642243Sarnold 	extern char	*tgetstr();
1652243Sarnold 
16621040Sbloom 	namp = "ambsdadbeohchzinmimsncnsosulxbxnxtxsxx";
1672243Sarnold 	fp = sflags;
1682243Sarnold 	do {
1692243Sarnold 		*(*fp++) = tgetflag(namp);
17019875Sbloom #ifdef DEBUG
17119875Sbloom 		fprintf(outf, "%2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE");
17219875Sbloom #endif
1732243Sarnold 		namp += 2;
1742243Sarnold 	} while (*namp);
17519875Sbloom 	namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullmandnlpcrcscsesfsosrtatetiucueupusvbvsveALDLUPDOLERI";
1762243Sarnold 	sp = sstrs;
1772243Sarnold 	do {
1782243Sarnold 		*(*sp++) = tgetstr(namp, &aoftspace);
17919875Sbloom #ifdef DEBUG
18019875Sbloom 		fprintf(outf, "%2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\"");
18119875Sbloom 		if (*sp[-1] != NULL) {
18219875Sbloom 			for (cp = *sp[-1]; *cp; cp++)
18319875Sbloom 				fprintf(outf, "%s", unctrl(*cp));
18419875Sbloom 			fprintf(outf, "\"\n");
18519875Sbloom 		}
18619875Sbloom #endif
1872243Sarnold 		namp += 2;
1882243Sarnold 	} while (*namp);
18921040Sbloom 	if (XS)
19021040Sbloom 		SO = SE = NULL;
19121040Sbloom 	else {
19221040Sbloom 		if (tgetnum("sg") > 0)
19321040Sbloom 			SO = NULL;
19421040Sbloom 		if (tgetnum("ug") > 0)
19521040Sbloom 			US = NULL;
19621040Sbloom 		if (!SO && US) {
19721040Sbloom 			SO = US;
19821040Sbloom 			SE = UE;
19921040Sbloom 		}
2002243Sarnold 	}
2012243Sarnold }
2023786Sarnold 
2033786Sarnold /*
2043786Sarnold  * return a capability from termcap
2053786Sarnold  */
2063786Sarnold char *
2073786Sarnold getcap(name)
2083786Sarnold char *name;
2093786Sarnold {
2104616Sarnold 	char *tgetstr();
2114615Sarnold 
2124616Sarnold 	return tgetstr(name, &aoftspace);
2133786Sarnold }
214