xref: /illumos-gate/usr/src/ucblib/libcurses/cr_tty.c (revision 8c0b080c)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
7*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
8*7c478bd9Sstevel@tonic-gate 
9*7c478bd9Sstevel@tonic-gate /*
10*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
11*7c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
12*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
13*7c478bd9Sstevel@tonic-gate  */
14*7c478bd9Sstevel@tonic-gate 
15*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
16*7c478bd9Sstevel@tonic-gate 
17*7c478bd9Sstevel@tonic-gate #ifndef lint
18*7c478bd9Sstevel@tonic-gate static char
19*7c478bd9Sstevel@tonic-gate sccsid[] = "@(#)cr_tty.c 1.7 88/02/08 SMI"; /* from UCB 5.2 85/11/08 */
20*7c478bd9Sstevel@tonic-gate #endif /* not lint */
21*7c478bd9Sstevel@tonic-gate 
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Terminal initialization routines.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #include	<unistd.h>
27*7c478bd9Sstevel@tonic-gate #include	<string.h>
28*7c478bd9Sstevel@tonic-gate #include	<sgtty.h>
29*7c478bd9Sstevel@tonic-gate #include	"curses.ext"
30*7c478bd9Sstevel@tonic-gate #include	<term.h>
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate /* forward declaration */
33*7c478bd9Sstevel@tonic-gate void zap(void);
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate static bool	*sflags[] = {
36*7c478bd9Sstevel@tonic-gate 			&AM, &BS, &DA, &DB, &EO, &HC, &HZ, &IN, &MI,
37*7c478bd9Sstevel@tonic-gate 			&MS, &NC, &NS, &OS, &UL, &XB, &XN, &XT, &XS,
38*7c478bd9Sstevel@tonic-gate 			&XX
39*7c478bd9Sstevel@tonic-gate 		};
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate static char	*_PC,
42*7c478bd9Sstevel@tonic-gate 		**sstrs[] = {
43*7c478bd9Sstevel@tonic-gate 			&AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS,
44*7c478bd9Sstevel@tonic-gate 			&DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2,
45*7c478bd9Sstevel@tonic-gate 			&K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC,
46*7c478bd9Sstevel@tonic-gate 			&IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU,
47*7c478bd9Sstevel@tonic-gate 			&LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF,
48*7c478bd9Sstevel@tonic-gate 			&SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US,
49*7c478bd9Sstevel@tonic-gate 			&VB, &VS, &VE, &AL_PARM, &DL_PARM, &UP_PARM,
50*7c478bd9Sstevel@tonic-gate 			&DOWN_PARM, &LEFT_PARM, &RIGHT_PARM,
51*7c478bd9Sstevel@tonic-gate 		};
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate char		_tspace[2048];		/* Space for capability strings */
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate static char	*aoftspace;		/* Address of _tspace for relocation */
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate static int	destcol, destline;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /*
60*7c478bd9Sstevel@tonic-gate  *	This routine does terminal type initialization routines, and
61*7c478bd9Sstevel@tonic-gate  * calculation of flags at entry.  It is almost entirely stolen from
62*7c478bd9Sstevel@tonic-gate  * Bill Joy's ex version 2.6.
63*7c478bd9Sstevel@tonic-gate  */
64*7c478bd9Sstevel@tonic-gate short	ospeed = -1;
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate int
gettmode(void)67*7c478bd9Sstevel@tonic-gate gettmode(void)
68*7c478bd9Sstevel@tonic-gate {
69*7c478bd9Sstevel@tonic-gate 	if (gtty(_tty_ch, &_tty) < 0)
70*7c478bd9Sstevel@tonic-gate 		return (ERR);
71*7c478bd9Sstevel@tonic-gate 	savetty();
72*7c478bd9Sstevel@tonic-gate 	if (stty(_tty_ch, &_tty) < 0)
73*7c478bd9Sstevel@tonic-gate 		_tty.sg_flags = _res_flg;
74*7c478bd9Sstevel@tonic-gate 	ospeed = _tty.sg_ospeed;
75*7c478bd9Sstevel@tonic-gate 	_res_flg = _tty.sg_flags;
76*7c478bd9Sstevel@tonic-gate 	UPPERCASE = (_tty.sg_flags & LCASE) != 0;
77*7c478bd9Sstevel@tonic-gate 	GT = ((_tty.sg_flags & XTABS) == 0);
78*7c478bd9Sstevel@tonic-gate 	NONL = ((_tty.sg_flags & CRMOD) == 0);
79*7c478bd9Sstevel@tonic-gate 	_tty.sg_flags &= ~XTABS;
80*7c478bd9Sstevel@tonic-gate 	(void) stty(_tty_ch, &_tty);
81*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
82*7c478bd9Sstevel@tonic-gate 	fprintf(outf, "GETTMODE: UPPERCASE = %s\n", UPPERCASE ? "TRUE":"FALSE");
83*7c478bd9Sstevel@tonic-gate 	fprintf(outf, "GETTMODE: GT = %s\n", GT ? "TRUE" : "FALSE");
84*7c478bd9Sstevel@tonic-gate 	fprintf(outf, "GETTMODE: NONL = %s\n", NONL ? "TRUE" : "FALSE");
85*7c478bd9Sstevel@tonic-gate 	fprintf(outf, "GETTMODE: ospeed = %d\n", ospeed);
86*7c478bd9Sstevel@tonic-gate #endif
87*7c478bd9Sstevel@tonic-gate 	return (OK);
88*7c478bd9Sstevel@tonic-gate }
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate int
setterm(char * type)91*7c478bd9Sstevel@tonic-gate setterm(char *type)
92*7c478bd9Sstevel@tonic-gate {
93*7c478bd9Sstevel@tonic-gate 	int		unknown;
94*7c478bd9Sstevel@tonic-gate 	static char	genbuf[1024];
95*7c478bd9Sstevel@tonic-gate #ifdef TIOCGWINSZ
96*7c478bd9Sstevel@tonic-gate 	struct winsize win;
97*7c478bd9Sstevel@tonic-gate #endif
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
100*7c478bd9Sstevel@tonic-gate 	fprintf(outf, "SETTERM(\"%s\")\n", type);
101*7c478bd9Sstevel@tonic-gate 	fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
102*7c478bd9Sstevel@tonic-gate #endif
103*7c478bd9Sstevel@tonic-gate 	if (type[0] == '\0')
104*7c478bd9Sstevel@tonic-gate 		type = "xx";
105*7c478bd9Sstevel@tonic-gate 	unknown = FALSE;
106*7c478bd9Sstevel@tonic-gate 	if (tgetent(genbuf, type) != 1) {
107*7c478bd9Sstevel@tonic-gate 		unknown++;
108*7c478bd9Sstevel@tonic-gate 		(void) strcpy(genbuf, "xx|dumb:");
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
111*7c478bd9Sstevel@tonic-gate 	fprintf(outf, "SETTERM: tty = %s\n", type);
112*7c478bd9Sstevel@tonic-gate #endif
113*7c478bd9Sstevel@tonic-gate #ifdef TIOCGWINSZ
114*7c478bd9Sstevel@tonic-gate 	if (ioctl(_tty_ch, TIOCGWINSZ, &win) >= 0) {
115*7c478bd9Sstevel@tonic-gate 		if (LINES == 0)
116*7c478bd9Sstevel@tonic-gate 			LINES = win.ws_row;
117*7c478bd9Sstevel@tonic-gate 		if (COLS == 0)
118*7c478bd9Sstevel@tonic-gate 			COLS = win.ws_col;
119*7c478bd9Sstevel@tonic-gate 	}
120*7c478bd9Sstevel@tonic-gate #endif
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	if (LINES == 0)
123*7c478bd9Sstevel@tonic-gate 		LINES = tgetnum("li");
124*7c478bd9Sstevel@tonic-gate 	if (LINES <= 5)
125*7c478bd9Sstevel@tonic-gate 		LINES = 24;
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	if (COLS == 0)
128*7c478bd9Sstevel@tonic-gate 		COLS = tgetnum("co");
129*7c478bd9Sstevel@tonic-gate 	if (COLS <= 4)
130*7c478bd9Sstevel@tonic-gate 		COLS = 80;
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
133*7c478bd9Sstevel@tonic-gate 	fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
134*7c478bd9Sstevel@tonic-gate #endif
135*7c478bd9Sstevel@tonic-gate 	aoftspace = _tspace;
136*7c478bd9Sstevel@tonic-gate 	zap();			/* get terminal description		*/
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	/*
139*7c478bd9Sstevel@tonic-gate 	 * Handle funny termcap capabilities
140*7c478bd9Sstevel@tonic-gate 	 */
141*7c478bd9Sstevel@tonic-gate 	if (CS && SC && RC) AL = DL = "";
142*7c478bd9Sstevel@tonic-gate 	if (AL_PARM && AL == NULL) AL = "";
143*7c478bd9Sstevel@tonic-gate 	if (DL_PARM && DL == NULL) DL = "";
144*7c478bd9Sstevel@tonic-gate 	if (IC && IM == NULL) IM = "";
145*7c478bd9Sstevel@tonic-gate 	if (IC && EI == NULL) EI = "";
146*7c478bd9Sstevel@tonic-gate 	if (!GT) BT = NULL;	/* If we can't tab, we can't backtab either */
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	if (tgoto(CM, destcol, destline)[0] == 'O')
149*7c478bd9Sstevel@tonic-gate 		CA = FALSE, CM = 0;
150*7c478bd9Sstevel@tonic-gate 	else
151*7c478bd9Sstevel@tonic-gate 		CA = TRUE;
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	PC = _PC ? _PC[0] : FALSE;
154*7c478bd9Sstevel@tonic-gate 	aoftspace = _tspace;
155*7c478bd9Sstevel@tonic-gate 	(void) strncpy(ttytype, longname(genbuf, type), sizeof (ttytype) - 1);
156*7c478bd9Sstevel@tonic-gate 	ttytype[sizeof (ttytype) - 1] = '\0';
157*7c478bd9Sstevel@tonic-gate 	if (unknown)
158*7c478bd9Sstevel@tonic-gate 		return (ERR);
159*7c478bd9Sstevel@tonic-gate 	return (OK);
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate /*
163*7c478bd9Sstevel@tonic-gate  *	This routine gets all the terminal flags from the termcap database
164*7c478bd9Sstevel@tonic-gate  */
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate void
zap(void)167*7c478bd9Sstevel@tonic-gate zap(void)
168*7c478bd9Sstevel@tonic-gate {
169*7c478bd9Sstevel@tonic-gate 	char	*namp;
170*7c478bd9Sstevel@tonic-gate 	bool	**fp;
171*7c478bd9Sstevel@tonic-gate 	char	***sp;
172*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
173*7c478bd9Sstevel@tonic-gate 	char	*cp;
174*7c478bd9Sstevel@tonic-gate #endif
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	namp = "ambsdadbeohchzinmimsncnsosulxbxnxtxsxx";
177*7c478bd9Sstevel@tonic-gate 	fp = sflags;
178*7c478bd9Sstevel@tonic-gate 	do {
179*7c478bd9Sstevel@tonic-gate 		*(*fp++) = tgetflag(namp);
180*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
181*7c478bd9Sstevel@tonic-gate 		fprintf(outf, "%2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE");
182*7c478bd9Sstevel@tonic-gate #endif
183*7c478bd9Sstevel@tonic-gate 		namp += 2;
184*7c478bd9Sstevel@tonic-gate 	} while (*namp);
185*7c478bd9Sstevel@tonic-gate 	namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9"
186*7c478bd9Sstevel@tonic-gate 		"hoicimipkdkekhklkrkskullmandnlpcrcscsesfsosrtatet"
187*7c478bd9Sstevel@tonic-gate 		"iucueupusvbvsveALDLUPDOLERI";
188*7c478bd9Sstevel@tonic-gate 	sp = sstrs;
189*7c478bd9Sstevel@tonic-gate 	do {
190*7c478bd9Sstevel@tonic-gate 		*(*sp++) = tgetstr(namp, &aoftspace);
191*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
192*7c478bd9Sstevel@tonic-gate 		fprintf(outf, "%2.2s = %s",
193*7c478bd9Sstevel@tonic-gate 			namp, *sp[-1] == NULL ? "NULL\n" : "\"");
194*7c478bd9Sstevel@tonic-gate 		if (*sp[-1] != NULL) {
195*7c478bd9Sstevel@tonic-gate 			for (cp = *sp[-1]; *cp; cp++)
196*7c478bd9Sstevel@tonic-gate 				fprintf(outf, "%s", unctrl(*cp));
197*7c478bd9Sstevel@tonic-gate 			fprintf(outf, "\"\n");
198*7c478bd9Sstevel@tonic-gate 		}
199*7c478bd9Sstevel@tonic-gate #endif
200*7c478bd9Sstevel@tonic-gate 		namp += 2;
201*7c478bd9Sstevel@tonic-gate 	} while (*namp);
202*7c478bd9Sstevel@tonic-gate 	if (XS)
203*7c478bd9Sstevel@tonic-gate 		SO = SE = NULL;
204*7c478bd9Sstevel@tonic-gate 	else {
205*7c478bd9Sstevel@tonic-gate 		if (tgetnum("sg") > 0)
206*7c478bd9Sstevel@tonic-gate 			SO = NULL;
207*7c478bd9Sstevel@tonic-gate 		if (tgetnum("ug") > 0)
208*7c478bd9Sstevel@tonic-gate 			US = NULL;
209*7c478bd9Sstevel@tonic-gate 		if (!SO && US) {
210*7c478bd9Sstevel@tonic-gate 			SO = US;
211*7c478bd9Sstevel@tonic-gate 			SE = UE;
212*7c478bd9Sstevel@tonic-gate 		}
213*7c478bd9Sstevel@tonic-gate 	}
214*7c478bd9Sstevel@tonic-gate }
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate /*
217*7c478bd9Sstevel@tonic-gate  * return a capability from termcap
218*7c478bd9Sstevel@tonic-gate  */
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate char *
getcap(char * name)221*7c478bd9Sstevel@tonic-gate getcap(char *name)
222*7c478bd9Sstevel@tonic-gate {
223*7c478bd9Sstevel@tonic-gate 	return (tgetstr(name, &aoftspace));
224*7c478bd9Sstevel@tonic-gate }
225