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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 static char sccsid[] = "@(#)setterm.c	8.7 (Berkeley) 7/27/94";
32 #endif /* not lint */
33 
34 #include <sys/ioctl.h>
35 
36 #include <stdlib.h>
37 #include <string.h>
38 #include <termios.h>
39 #include <unistd.h>
40 #include <limits.h>
41 
42 #include "curses.h"
43 
44 static void zap(void);
45 
46 static char	*sflags[] = {
47 		/*       am   bs   da   eo   hc   in   mi   ms  */
48 			&AM, &BS, &DA, &EO, &HC, &IN, &MI, &MS,
49 		/*	 nc   ns   os   ul   xb   xn   xt   xs   xx  */
50 			&NC, &NS, &OS, &UL, &XB, &XN, &XT, &XS, &XX
51 		};
52 
53 static char	*_PC,
54 		**sstrs[] = {
55 		/*	 AL   bc   bt   cd   ce   cl   cm   cr   cs  */
56 			&AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS,
57 		/*	 dc   DL   dm   do   ed   ei   k0   k1   k2  */
58 			&DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2,
59 		/*	 k3   k4   k5   k6   k7   k8   k9   ho   ic  */
60 			&K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC,
61 		/*	 im   ip   kd   ke   kh   kl   kr   ks   ku  */
62 			&IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU,
63 		/*	 ll   ma   nd   nl    pc   rc   sc   se   SF */
64 			&LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF,
65 		/*	 so   SR   ta   te   ti   uc   ue   up   us  */
66 			&SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US,
67 		/*	 vb   vs   ve   al   dl   sf   sr   AL	     */
68 			&VB, &VS, &VE, &al, &dl, &sf, &sr, &AL_PARM,
69 		/*	 DL	   UP	     DO		 LE	     */
70 			&DL_PARM, &UP_PARM, &DOWN_PARM, &LEFT_PARM,
71 		/*	 RI					     */
72 			&RIGHT_PARM,
73 		};
74 
75 static char	*aoftspace;		/* Address of _tspace for relocation */
76 static char	tspace[2048];		/* Space for capability strings */
77 
78 char *ttytype;
79 
80 int
setterm(type)81 setterm(type)
82 	register char *type;
83 {
84 	static char genbuf[1024];
85 	static char __ttytype[1024];
86 	register int unknown;
87 	struct ttysize win;
88 	char *p;
89 
90 #ifdef DEBUG
91 	__CTRACE("setterm: (\"%s\")\nLINES = %d, COLS = %d\n",
92 	    type, LINES, COLS);
93 #endif
94 	if (type[0] == '\0')
95 		type = "xx";
96 	unknown = 0;
97 	if (tgetent(genbuf, type) != 1) {
98 		unknown++;
99 		strlcpy(genbuf, "xx|dumb:", sizeof(genbuf));
100 	}
101 #ifdef DEBUG
102 	__CTRACE("setterm: tty = %s\n", type);
103 #endif
104 
105 	/* Try TIOCGSIZE, and, if it fails, the termcap entry. */
106 	if (ioctl(STDERR_FILENO, TIOCGSIZE, &win) != -1 &&
107 	    win.ts_lines != 0 && win.ts_cols != 0) {
108 		LINES = win.ts_lines;
109 		COLS = win.ts_cols;
110 	}  else {
111 		LINES = tgetnum("li");
112 		COLS = tgetnum("co");
113 	}
114 
115 	/* POSIX 1003.2 requires that the environment override. */
116 	if ((p = getenv("LINES")) != NULL) {
117 		long l = strtol(p, &p, 10);
118 		if (l > 0 && l < INT_MAX && *p == '\0')
119 			LINES = (int)l;
120 	}
121 	if ((p = getenv("COLUMNS")) != NULL) {
122 		long l = strtol(p, &p, 10);
123 		if (l > 0 && l < INT_MAX && *p == '\0')
124 			COLS = (int)l;
125 	}
126 
127 	/*
128 	 * Want cols > 4, otherwise things will fail.
129 	 */
130 	if (COLS <= 4)
131 		return (ERR);
132 
133 #ifdef 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 -- tgoto returns "OOPS" if it can't
148 	 * do cursor motions.
149 	 */
150 	if (tgoto(CM, 0, 0)[0] == 'O') {
151 		CA = 0;
152 		CM = 0;
153 	} else
154 		CA = 1;
155 
156 	PC = _PC ? _PC[0] : 0;
157 	aoftspace = tspace;
158 	ttytype = longname(genbuf, __ttytype);
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 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 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 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