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