xref: /csrg-svn/lib/libcurses/setterm.c (revision 21040)
12243Sarnold /*
22243Sarnold  * Terminal initialization routines.
32243Sarnold  *
4*21040Sbloom  * @(#)setterm.c	1.17 (Berkeley) 05/23/85
52243Sarnold  */
62243Sarnold 
72243Sarnold # include	"curses.ext"
82243Sarnold 
919875Sbloom static bool	*sflags[] = {
1019875Sbloom 			&AM, &BS, &DA, &DB, &EO, &HC, &HZ, &IN, &MI,
11*21040Sbloom 			&MS, &NC, &NS, &OS, &UL, &XB, &XN, &XT, &XS,
12*21040Sbloom 			&XX
132243Sarnold 		};
142243Sarnold 
1519875Sbloom static char	*_PC,
1619875Sbloom 		**sstrs[] = {
1719875Sbloom 			&AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS,
1819875Sbloom 			&DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2,
1919875Sbloom 			&K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC,
2019875Sbloom 			&IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU,
2119875Sbloom 			&LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF,
2219875Sbloom 			&SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US,
2319875Sbloom 			&VB, &VS, &VE, &AL_PARM, &DL_PARM, &UP_PARM,
2419875Sbloom 			&DOWN_PARM, &LEFT_PARM, &RIGHT_PARM,
252243Sarnold 		},
262465Sarnold 		*tgoto();
272243Sarnold 
2819875Sbloom char		_tspace[2048];		/* Space for capability strings */
292243Sarnold 
3019875Sbloom static char	*aoftspace;		/* Address of _tspace for relocation */
3119875Sbloom 
322243Sarnold static int	destcol, destline;
332243Sarnold 
342243Sarnold /*
352243Sarnold  *	This routine does terminal type initialization routines, and
362243Sarnold  * calculation of flags at entry.  It is almost entirely stolen from
372243Sarnold  * Bill Joy's ex version 2.6.
382243Sarnold  */
392243Sarnold short	ospeed = -1;
402243Sarnold 
412243Sarnold gettmode() {
422243Sarnold 
432243Sarnold 	if (gtty(_tty_ch, &_tty) < 0)
442243Sarnold 		return;
452243Sarnold 	savetty();
462243Sarnold 	if (stty(_tty_ch, &_tty) < 0)
472243Sarnold 		_tty.sg_flags = _res_flg;
482243Sarnold 	ospeed = _tty.sg_ospeed;
492243Sarnold 	_res_flg = _tty.sg_flags;
502243Sarnold 	UPPERCASE = (_tty.sg_flags & LCASE) != 0;
512243Sarnold 	GT = ((_tty.sg_flags & XTABS) == 0);
522243Sarnold 	NONL = ((_tty.sg_flags & CRMOD) == 0);
533631Sarnold 	_tty.sg_flags &= ~XTABS;
544612Sarnold 	stty(_tty_ch, &_tty);
552243Sarnold # ifdef DEBUG
562243Sarnold 	fprintf(outf, "GETTMODE: UPPERCASE = %s\n", UPPERCASE ? "TRUE":"FALSE");
572243Sarnold 	fprintf(outf, "GETTMODE: GT = %s\n", GT ? "TRUE" : "FALSE");
582243Sarnold 	fprintf(outf, "GETTMODE: NONL = %s\n", NONL ? "TRUE" : "FALSE");
592243Sarnold 	fprintf(outf, "GETTMODE: ospeed = %d\n", ospeed);
602243Sarnold # endif
612243Sarnold }
622243Sarnold 
632243Sarnold setterm(type)
642243Sarnold reg char	*type; {
652243Sarnold 
6611736Sarnold 	reg int		unknown;
6711736Sarnold 	static char	genbuf[1024];
6819875Sbloom # ifdef TIOCGWINSZ
6918032Sbloom 	struct winsize win;
7019875Sbloom # endif
712243Sarnold 
722243Sarnold # ifdef DEBUG
732243Sarnold 	fprintf(outf, "SETTERM(\"%s\")\n", type);
742243Sarnold 	fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
752243Sarnold # endif
762243Sarnold 	if (type[0] == '\0')
772243Sarnold 		type = "xx";
782243Sarnold 	unknown = FALSE;
792243Sarnold 	if (tgetent(genbuf, type) != 1) {
802243Sarnold 		unknown++;
812243Sarnold 		strcpy(genbuf, "xx|dumb:");
822243Sarnold 	}
832243Sarnold # ifdef DEBUG
842243Sarnold 	fprintf(outf, "SETTERM: tty = %s\n", type);
852243Sarnold # endif
8619875Sbloom # ifdef TIOCGWINSZ
8718032Sbloom 	if (ioctl(_tty_ch, TIOCGWINSZ, &win) >= 0) {
8818032Sbloom 		if (LINES == 0)
8918032Sbloom 			LINES = win.ws_row;
9018032Sbloom 		if (COLS == 0)
9118032Sbloom 			COLS = win.ws_col;
9218032Sbloom 	}
9319875Sbloom # endif
9418032Sbloom 
952243Sarnold 	if (LINES == 0)
962243Sarnold 		LINES = tgetnum("li");
972243Sarnold 	if (LINES <= 5)
982243Sarnold 		LINES = 24;
992243Sarnold 
1002243Sarnold 	if (COLS == 0)
1012243Sarnold 		COLS = tgetnum("co");
1022243Sarnold 	if (COLS <= 4)
1032243Sarnold 		COLS = 80;
10412570Sarnold 
1052243Sarnold # ifdef DEBUG
1062243Sarnold 	fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
1072243Sarnold # endif
10819875Sbloom 	aoftspace = _tspace;
1092243Sarnold 	zap();			/* get terminal description		*/
11019875Sbloom 
11119875Sbloom 	/*
11219875Sbloom 	 * Handle funny termcap capabilities
11319875Sbloom 	 */
11419875Sbloom 	if (CS && SC && RC) AL=DL="";
11519875Sbloom 	if (AL_PARM && AL==NULL) AL="";
11619875Sbloom 	if (DL_PARM && DL==NULL) DL="";
11719875Sbloom 	if (IC && IM==NULL) IM="";
11819875Sbloom 	if (IC && EI==NULL) EI="";
11919875Sbloom 	if (!GT) BT=NULL;	/* If we can't tab, we can't backtab either */
12019875Sbloom 
1212243Sarnold 	if (tgoto(CM, destcol, destline)[0] == 'O')
1222243Sarnold 		CA = FALSE, CM = 0;
1232243Sarnold 	else
1242243Sarnold 		CA = TRUE;
12519875Sbloom 
12619875Sbloom 	PC = _PC ? _PC[0] : FALSE;
12719875Sbloom 	aoftspace = _tspace;
1282243Sarnold 	strcpy(ttytype, longname(genbuf, type));
1292243Sarnold 	if (unknown)
1302243Sarnold 		return ERR;
1312243Sarnold 	return OK;
1322243Sarnold }
13319875Sbloom 
1342243Sarnold /*
1354616Sarnold  *	This routine gets all the terminal flags from the termcap database
1362243Sarnold  */
1372243Sarnold 
13819875Sbloom zap()
13919875Sbloom {
14019875Sbloom 	register char	*namp;
14119875Sbloom 	register bool	**fp;
14219875Sbloom 	register char	***sp;
14319875Sbloom #ifdef	DEBUG
14419875Sbloom 	register char	*cp;
14519875Sbloom #endif
1462243Sarnold 	extern char	*tgetstr();
1472243Sarnold 
148*21040Sbloom 	namp = "ambsdadbeohchzinmimsncnsosulxbxnxtxsxx";
1492243Sarnold 	fp = sflags;
1502243Sarnold 	do {
1512243Sarnold 		*(*fp++) = tgetflag(namp);
15219875Sbloom #ifdef DEBUG
15319875Sbloom 		fprintf(outf, "%2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE");
15419875Sbloom #endif
1552243Sarnold 		namp += 2;
1562243Sarnold 	} while (*namp);
15719875Sbloom 	namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullmandnlpcrcscsesfsosrtatetiucueupusvbvsveALDLUPDOLERI";
1582243Sarnold 	sp = sstrs;
1592243Sarnold 	do {
1602243Sarnold 		*(*sp++) = tgetstr(namp, &aoftspace);
16119875Sbloom #ifdef DEBUG
16219875Sbloom 		fprintf(outf, "%2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\"");
16319875Sbloom 		if (*sp[-1] != NULL) {
16419875Sbloom 			for (cp = *sp[-1]; *cp; cp++)
16519875Sbloom 				fprintf(outf, "%s", unctrl(*cp));
16619875Sbloom 			fprintf(outf, "\"\n");
16719875Sbloom 		}
16819875Sbloom #endif
1692243Sarnold 		namp += 2;
1702243Sarnold 	} while (*namp);
171*21040Sbloom 	if (XS)
172*21040Sbloom 		SO = SE = NULL;
173*21040Sbloom 	else {
174*21040Sbloom 		if (tgetnum("sg") > 0)
175*21040Sbloom 			SO = NULL;
176*21040Sbloom 		if (tgetnum("ug") > 0)
177*21040Sbloom 			US = NULL;
178*21040Sbloom 		if (!SO && US) {
179*21040Sbloom 			SO = US;
180*21040Sbloom 			SE = UE;
181*21040Sbloom 		}
1822243Sarnold 	}
1832243Sarnold }
1843786Sarnold 
1853786Sarnold /*
1863786Sarnold  * return a capability from termcap
1873786Sarnold  */
1883786Sarnold char *
1893786Sarnold getcap(name)
1903786Sarnold char *name;
1913786Sarnold {
1924616Sarnold 	char *tgetstr();
1934615Sarnold 
1944616Sarnold 	return tgetstr(name, &aoftspace);
1953786Sarnold }
196