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