xref: /csrg-svn/lib/libcurses/setterm.c (revision 19875)
12243Sarnold /*
22243Sarnold  * Terminal initialization routines.
32243Sarnold  *
4*19875Sbloom  * @(#)setterm.c	1.16 (Berkeley) 05/01/85
52243Sarnold  */
62243Sarnold 
72243Sarnold # include	"curses.ext"
82243Sarnold 
9*19875Sbloom static bool	*sflags[] = {
10*19875Sbloom 			&AM, &BS, &DA, &DB, &EO, &HC, &HZ, &IN, &MI,
11*19875Sbloom 			&MS, &NC, &NS, &OS, &UL, &XB, &XN, &XT, &XX
122243Sarnold 		};
132243Sarnold 
14*19875Sbloom static char	*_PC,
15*19875Sbloom 		**sstrs[] = {
16*19875Sbloom 			&AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS,
17*19875Sbloom 			&DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2,
18*19875Sbloom 			&K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC,
19*19875Sbloom 			&IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU,
20*19875Sbloom 			&LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF,
21*19875Sbloom 			&SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US,
22*19875Sbloom 			&VB, &VS, &VE, &AL_PARM, &DL_PARM, &UP_PARM,
23*19875Sbloom 			&DOWN_PARM, &LEFT_PARM, &RIGHT_PARM,
242243Sarnold 		},
252465Sarnold 		*tgoto();
262243Sarnold 
27*19875Sbloom char		_tspace[2048];		/* Space for capability strings */
282243Sarnold 
29*19875Sbloom static char	*aoftspace;		/* Address of _tspace for relocation */
30*19875Sbloom 
312243Sarnold static int	destcol, destline;
322243Sarnold 
332243Sarnold /*
342243Sarnold  *	This routine does terminal type initialization routines, and
352243Sarnold  * calculation of flags at entry.  It is almost entirely stolen from
362243Sarnold  * Bill Joy's ex version 2.6.
372243Sarnold  */
382243Sarnold short	ospeed = -1;
392243Sarnold 
402243Sarnold gettmode() {
412243Sarnold 
422243Sarnold 	if (gtty(_tty_ch, &_tty) < 0)
432243Sarnold 		return;
442243Sarnold 	savetty();
452243Sarnold 	if (stty(_tty_ch, &_tty) < 0)
462243Sarnold 		_tty.sg_flags = _res_flg;
472243Sarnold 	ospeed = _tty.sg_ospeed;
482243Sarnold 	_res_flg = _tty.sg_flags;
492243Sarnold 	UPPERCASE = (_tty.sg_flags & LCASE) != 0;
502243Sarnold 	GT = ((_tty.sg_flags & XTABS) == 0);
512243Sarnold 	NONL = ((_tty.sg_flags & CRMOD) == 0);
523631Sarnold 	_tty.sg_flags &= ~XTABS;
534612Sarnold 	stty(_tty_ch, &_tty);
542243Sarnold # ifdef DEBUG
552243Sarnold 	fprintf(outf, "GETTMODE: UPPERCASE = %s\n", UPPERCASE ? "TRUE":"FALSE");
562243Sarnold 	fprintf(outf, "GETTMODE: GT = %s\n", GT ? "TRUE" : "FALSE");
572243Sarnold 	fprintf(outf, "GETTMODE: NONL = %s\n", NONL ? "TRUE" : "FALSE");
582243Sarnold 	fprintf(outf, "GETTMODE: ospeed = %d\n", ospeed);
592243Sarnold # endif
602243Sarnold }
612243Sarnold 
622243Sarnold setterm(type)
632243Sarnold reg char	*type; {
642243Sarnold 
6511736Sarnold 	reg int		unknown;
6611736Sarnold 	static char	genbuf[1024];
67*19875Sbloom # ifdef TIOCGWINSZ
6818032Sbloom 	struct winsize win;
69*19875Sbloom # endif
702243Sarnold 
712243Sarnold # ifdef DEBUG
722243Sarnold 	fprintf(outf, "SETTERM(\"%s\")\n", type);
732243Sarnold 	fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
742243Sarnold # endif
752243Sarnold 	if (type[0] == '\0')
762243Sarnold 		type = "xx";
772243Sarnold 	unknown = FALSE;
782243Sarnold 	if (tgetent(genbuf, type) != 1) {
792243Sarnold 		unknown++;
802243Sarnold 		strcpy(genbuf, "xx|dumb:");
812243Sarnold 	}
822243Sarnold # ifdef DEBUG
832243Sarnold 	fprintf(outf, "SETTERM: tty = %s\n", type);
842243Sarnold # endif
85*19875Sbloom # ifdef TIOCGWINSZ
8618032Sbloom 	if (ioctl(_tty_ch, TIOCGWINSZ, &win) >= 0) {
8718032Sbloom 		if (LINES == 0)
8818032Sbloom 			LINES = win.ws_row;
8918032Sbloom 		if (COLS == 0)
9018032Sbloom 			COLS = win.ws_col;
9118032Sbloom 	}
92*19875Sbloom # endif
9318032Sbloom 
942243Sarnold 	if (LINES == 0)
952243Sarnold 		LINES = tgetnum("li");
962243Sarnold 	if (LINES <= 5)
972243Sarnold 		LINES = 24;
982243Sarnold 
992243Sarnold 	if (COLS == 0)
1002243Sarnold 		COLS = tgetnum("co");
1012243Sarnold 	if (COLS <= 4)
1022243Sarnold 		COLS = 80;
10312570Sarnold 
1042243Sarnold # ifdef DEBUG
1052243Sarnold 	fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
1062243Sarnold # endif
107*19875Sbloom 	aoftspace = _tspace;
1082243Sarnold 	zap();			/* get terminal description		*/
109*19875Sbloom 
110*19875Sbloom 	/*
111*19875Sbloom 	 * Handle funny termcap capabilities
112*19875Sbloom 	 */
113*19875Sbloom 	if (CS && SC && RC) AL=DL="";
114*19875Sbloom 	if (AL_PARM && AL==NULL) AL="";
115*19875Sbloom 	if (DL_PARM && DL==NULL) DL="";
116*19875Sbloom 	if (IC && IM==NULL) IM="";
117*19875Sbloom 	if (IC && EI==NULL) EI="";
118*19875Sbloom 	if (!GT) BT=NULL;	/* If we can't tab, we can't backtab either */
119*19875Sbloom 
1202243Sarnold 	if (tgoto(CM, destcol, destline)[0] == 'O')
1212243Sarnold 		CA = FALSE, CM = 0;
1222243Sarnold 	else
1232243Sarnold 		CA = TRUE;
124*19875Sbloom 
125*19875Sbloom 	PC = _PC ? _PC[0] : FALSE;
126*19875Sbloom 	aoftspace = _tspace;
1272243Sarnold 	strcpy(ttytype, longname(genbuf, type));
1282243Sarnold 	if (unknown)
1292243Sarnold 		return ERR;
1302243Sarnold 	return OK;
1312243Sarnold }
132*19875Sbloom 
1332243Sarnold /*
1344616Sarnold  *	This routine gets all the terminal flags from the termcap database
1352243Sarnold  */
1362243Sarnold 
137*19875Sbloom zap()
138*19875Sbloom {
139*19875Sbloom 	register char	*namp;
140*19875Sbloom 	register bool	**fp;
141*19875Sbloom 	register char	***sp;
142*19875Sbloom #ifdef	DEBUG
143*19875Sbloom 	register char	*cp;
144*19875Sbloom #endif
1452243Sarnold 	extern char	*tgetstr();
1462243Sarnold 
147*19875Sbloom 	namp = "ambsdadbeohchzinmimsncnsosulxbxnxtxx";
1482243Sarnold 	fp = sflags;
1492243Sarnold 	do {
1502243Sarnold 		*(*fp++) = tgetflag(namp);
151*19875Sbloom #ifdef DEBUG
152*19875Sbloom 		fprintf(outf, "%2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE");
153*19875Sbloom #endif
1542243Sarnold 		namp += 2;
1552243Sarnold 	} while (*namp);
156*19875Sbloom 	namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullmandnlpcrcscsesfsosrtatetiucueupusvbvsveALDLUPDOLERI";
1572243Sarnold 	sp = sstrs;
1582243Sarnold 	do {
1592243Sarnold 		*(*sp++) = tgetstr(namp, &aoftspace);
160*19875Sbloom #ifdef DEBUG
161*19875Sbloom 		fprintf(outf, "%2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\"");
162*19875Sbloom 		if (*sp[-1] != NULL) {
163*19875Sbloom 			for (cp = *sp[-1]; *cp; cp++)
164*19875Sbloom 				fprintf(outf, "%s", unctrl(*cp));
165*19875Sbloom 			fprintf(outf, "\"\n");
166*19875Sbloom 		}
167*19875Sbloom #endif
1682243Sarnold 		namp += 2;
1692243Sarnold 	} while (*namp);
17012864Sarnold 	if (tgetnum("sg") > 0)
17112864Sarnold 		SO = NULL;
17212864Sarnold 	if (tgetnum("ug") > 0)
17312864Sarnold 		US = NULL;
17412864Sarnold 	if (!SO && US) {
1752243Sarnold 		SO = US;
1762243Sarnold 		SE = UE;
1772243Sarnold 	}
1782243Sarnold }
1793786Sarnold 
1803786Sarnold /*
1813786Sarnold  * return a capability from termcap
1823786Sarnold  */
1833786Sarnold char *
1843786Sarnold getcap(name)
1853786Sarnold char *name;
1863786Sarnold {
1874616Sarnold 	char *tgetstr();
1884615Sarnold 
1894616Sarnold 	return tgetstr(name, &aoftspace);
1903786Sarnold }
191