1 /*
2  * Copyright (c) 1981, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char sccsid[] = "@(#)setterm.c	8.8 (Berkeley) 10/25/94";
36 #endif /* not lint */
37 
38 #include <sys/ioctl.h>		/* TIOCGWINSZ on old systems. */
39 
40 #include <stdlib.h>
41 #include <string.h>
42 #include <termios.h>
43 #include <unistd.h>
44 
45 #include "curses.h"
46 
47 static void zap();
48 
49 static char	*sflags[] = {
50 		/*       am   bs   da   eo   hc   in   mi   ms  */
51 			&AM, &BS, &DA, &EO, &HC, &IN, &MI, &MS,
52 		/*	 nc   ns   os   ul   xb   xn   xt   xs   xx  */
53 			&NC, &NS, &OS, &UL, &XB, &XN, &XT, &XS, &XX
54 		};
55 
56 static char	*_PC,
57 		**sstrs[] = {
58 		/*	 AL   bc   bt   cd   ce   cl   cm   cr   cs  */
59 			&AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS,
60 		/*	 dc   DL   dm   do   ed   ei   k0   k1   k2  */
61 			&DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2,
62 		/*	 k3   k4   k5   k6   k7   k8   k9   ho   ic  */
63 			&K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC,
64 		/*	 im   ip   kd   ke   kh   kl   kr   ks   ku  */
65 			&IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU,
66 		/*	 ll   ma   nd   nl    pc   rc   sc   se   SF */
67 			&LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF,
68 		/*	 so   SR   ta   te   ti   uc   ue   up   us  */
69 			&SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US,
70 		/*	 vb   vs   ve   al   dl   sf   sr   AL	     */
71 			&VB, &VS, &VE, &al, &dl, &sf, &sr, &AL_PARM,
72 		/*	 DL	   UP	     DO		 LE	     */
73 			&DL_PARM, &UP_PARM, &DOWN_PARM, &LEFT_PARM,
74 		/*	 RI					     */
75 			&RIGHT_PARM,
76 		};
77 
78 static char	*aoftspace;		/* Address of _tspace for relocation */
79 static char	tspace[2048];		/* Space for capability strings */
80 
81 int
setterm(type)82 setterm(type)
83 	register char *type;
84 {
85 	static char genbuf[1024];
86 	register int unknown;
87 #ifdef TIOCGWINSZ
88 	struct winsize win;
89 #endif
90 	char *p;
91 
92 #ifdef CURSES_DEBUG
93 	__CTRACE("setterm: (\"%s\")\nLINES = %d, COLS = %d\n",
94 	    type, LINES, COLS);
95 #endif
96 	if (type[0] == '\0')
97 		type = "xx";
98 	unknown = 0;
99 	if (tgetent(genbuf, type) != 1) {
100 		unknown++;
101 		strcpy(genbuf, "xx|dumb:");
102 	}
103 #ifdef CURSES_DEBUG
104 	__CTRACE("setterm: tty = %s\n", type);
105 #endif
106 
107 #ifdef TIOCGWINSZ
108 	/* Try TIOCGWINSZ, and, if it fails, the termcap entry. */
109 	if (ioctl(STDERR_FILENO, TIOCGWINSZ, &win) != -1 &&
110 	    win.ws_row != 0 && win.ws_col != 0) {
111 		LINES = win.ws_row;
112 		COLS = win.ws_col;
113 	}  else {
114 #endif
115 		LINES = tgetnum("li");
116 		COLS = tgetnum("co");
117 #ifdef TIOCGWINSZ
118 	}
119 #endif
120 
121 	/* POSIX 1003.2 requires that the environment override. */
122 	if ((p = getenv("LINES")) != NULL)
123 		LINES = strtol(p, NULL, 10);
124 	if ((p = getenv("COLUMNS")) != NULL)
125 		COLS = strtol(p, NULL, 10);
126 
127 	/*
128 	 * Want cols > 4, otherwise things will fail.
129 	 */
130 	if (COLS <= 4)
131 		return (ERR);
132 
133 #ifdef CURSES_DEBUG
134 	__CTRACE("setterm: LINES = %d, COLS = %d\n", LINES, COLS);
135 #endif
136 	aoftspace = tspace;
137 	zap();			/* Get terminal description. */
138 
139 	/* If we can't tab, we can't backtab, either. */
140 	if (!GT)
141 		BT = NULL;
142 
143 	/*
144 	 * Test for cursor motion capbility.
145 	 *
146 	 * XXX
147 	 * This is truly stupid -- historically, tgoto returns "OOPS" if it
148 	 * can't do cursor motions.  Some systems have been fixed to return
149 	 * a NULL pointer.
150 	 */
151 	if ((p = tgoto(CM, 0, 0)) == NULL || *p == 'O') {
152 		CA = 0;
153 		CM = 0;
154 	} else
155 		CA = 1;
156 
157 	PC = _PC ? _PC[0] : 0;
158 	aoftspace = tspace;
159 
160 	/* If no scrolling commands, no quick change. */
161 	__noqch =
162 	    (CS == NULL || HO == NULL ||
163 	    SF == NULL && sf == NULL || SR == NULL && sr == NULL) &&
164 	    (AL == NULL && al == NULL || DL == NULL && dl == NULL);
165 
166 	return (unknown ? ERR : OK);
167 }
168 
169 /*
170  * zap --
171  *	Gets all the terminal flags from the termcap database.
172  */
173 static void
zap()174 zap()
175 {
176 	register char *namp, ***sp;
177 	register char **fp;
178 	char tmp[3];
179 #ifdef CURSES_DEBUG
180 	register char	*cp;
181 #endif
182 	tmp[2] = '\0';
183 
184 	namp = "ambsdaeohcinmimsncnsosulxbxnxtxsxx";
185 	fp = sflags;
186 	do {
187 		*tmp = *namp;
188 		*(tmp + 1) = *(namp + 1);
189 		*(*fp++) = tgetflag(tmp);
190 #ifdef CURSES_DEBUG
191 		__CTRACE("2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE");
192 #endif
193 		namp += 2;
194 
195 	} while (*namp);
196 	namp = "ALbcbtcdceclcmcrcsdcDLdmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullmandnlpcrcscseSFsoSRtatetiucueupusvbvsvealdlsfsrALDLUPDOLERI";
197 	sp = sstrs;
198 	do {
199 		*tmp = *namp;
200 		*(tmp + 1) = *(namp + 1);
201 		*(*sp++) = tgetstr(tmp, &aoftspace);
202 #ifdef CURSES_DEBUG
203 		__CTRACE("2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\"");
204 		if (*sp[-1] != NULL) {
205 			for (cp = *sp[-1]; *cp; cp++)
206 				__CTRACE("%s", unctrl(*cp));
207 			__CTRACE("\"\n");
208 		}
209 #endif
210 		namp += 2;
211 	} while (*namp);
212 	if (XS)
213 		SO = SE = NULL;
214 	else {
215 		if (tgetnum("sg") > 0)
216 			SO = NULL;
217 		if (tgetnum("ug") > 0)
218 			US = NULL;
219 		if (!SO && US) {
220 			SO = US;
221 			SE = UE;
222 		}
223 	}
224 }
225 
226 /*
227  * getcap --
228  *	Return a capability from termcap.
229  */
230 char *
getcap(name)231 getcap(name)
232 	char *name;
233 {
234 	return (tgetstr(name, &aoftspace));
235 }
236