xref: /netbsd-src/lib/libcurses/setterm.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
1 /*
2  * Copyright (c) 1981, 1993
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 /* from: static char sccsid[] = "@(#)setterm.c	8.3 (Berkeley) 1/2/94"; */
36 static char *rcsid = "$Id: setterm.c,v 1.5 1994/01/24 08:36:57 cgd Exp $";
37 #endif /* not lint */
38 
39 #include <sys/ioctl.h>
40 
41 #include <curses.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 static void zap __P((void));
47 
48 static char	*sflags[] = {
49 		/*       am   bs   da   eo   hc   in   mi   ms  */
50 			&AM, &BS, &DA, &EO, &HC, &IN, &MI, &MS,
51 		/*	 nc   ns   os   ul   xb   xn   xt   xs   xx  */
52 			&NC, &NS, &OS, &UL, &XB, &XN, &XT, &XS, &XX
53 		};
54 
55 static char	*_PC,
56 		**sstrs[] = {
57 		/*	 AL   bc   bt   cd   ce   cl   cm   cr   cs  */
58 			&AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS,
59 		/*	 dc   DL   dm   do   ed   ei   k0   k1   k2  */
60 			&DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2,
61 		/*	 k3   k4   k5   k6   k7   k8   k9   ho   ic  */
62 			&K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC,
63 		/*	 im   ip   kd   ke   kh   kl   kr   ks   ku  */
64 			&IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU,
65 		/*	 ll   ma   nd   nl    pc   rc   sc   se   SF */
66 			&LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF,
67 		/*	 so   SR   ta   te   ti   uc   ue   up   us  */
68 			&SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US,
69 		/*	 vb   vs   ve   al   dl   sf   sr   AL	     */
70 			&VB, &VS, &VE, &al, &dl, &sf, &sr, &AL_PARM,
71 		/*	 DL	   UP	     DO		 LE	     */
72 			&DL_PARM, &UP_PARM, &DOWN_PARM, &LEFT_PARM,
73 		/*	 RI					     */
74 			&RIGHT_PARM,
75 		};
76 
77 static char	*aoftspace;		/* Address of _tspace for relocation */
78 static char	tspace[2048];		/* Space for capability strings */
79 
80 char *ttytype;
81 
82 int
83 setterm(type)
84 	register char *type;
85 {
86 	static char genbuf[1024];
87 	static char __ttytype[1024];
88 	register int unknown;
89 	struct winsize win;
90 	char *p;
91 
92 #ifdef 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 DEBUG
104 	__CTRACE("setterm: tty = %s\n", type);
105 #endif
106 
107 	/* Try TIOCGWINSZ, and, if it fails, the termcap entry. */
108 	if (ioctl(STDERR_FILENO, TIOCGWINSZ, &win) != -1 &&
109 	    win.ws_row != 0 && win.ws_col != 0) {
110 		LINES = win.ws_row;
111 		COLS = win.ws_col;
112 	}  else {
113 		LINES = tgetnum("li");
114 		COLS = tgetnum("co");
115 	}
116 
117 	/* POSIX 1003.2 requires that the environment override. */
118 	if ((p = getenv("LINES")) != NULL)
119 		LINES = strtol(p, NULL, 10);
120 	if ((p = getenv("COLUMNS")) != NULL)
121 		COLS = strtol(p, NULL, 10);
122 
123 	/*
124 	 * Want cols > 4, otherwise things will fail.
125 	 */
126 	if (COLS <= 4)
127 		return (ERR);
128 
129 #ifdef DEBUG
130 	__CTRACE("setterm: LINES = %d, COLS = %d\n", LINES, COLS);
131 #endif
132 	aoftspace = tspace;
133 	zap();			/* Get terminal description. */
134 
135 	/* If we can't tab, we can't backtab, either. */
136 	if (!GT)
137 		BT = NULL;
138 
139 	/*
140 	 * Test for cursor motion capbility.
141 	 *
142 	 * XXX
143 	 * This is truly stupid -- tgoto returns "OOPS" if it can't
144 	 * do cursor motions.
145 	 */
146 	if (tgoto(CM, 0, 0)[0] == 'O') {
147 		CA = 0;
148 		CM = 0;
149 	} else
150 		CA = 1;
151 
152 	PC = _PC ? _PC[0] : 0;
153 	aoftspace = tspace;
154 	ttytype = longname(genbuf, __ttytype);
155 
156 	if ((!AL && !al) || (!DL && !dl))
157 		__noqch = 1;
158 
159 	return (unknown ? ERR : OK);
160 }
161 
162 /*
163  * zap --
164  *	Gets all the terminal flags from the termcap database.
165  */
166 static void
167 zap()
168 {
169 	register char *namp, ***sp;
170 	register char **fp;
171 	char tmp[3];
172 #ifdef DEBUG
173 	register char	*cp;
174 #endif
175 	tmp[2] = '\0';
176 
177 	namp = "ambsdaeohcinmimsncnsosulxbxnxtxsxx";
178 	fp = sflags;
179 	do {
180 		*tmp = *namp;
181 		*(tmp + 1) = *(namp + 1);
182 		*(*fp++) = tgetflag(tmp);
183 #ifdef DEBUG
184 		__CTRACE("2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE");
185 #endif
186 		namp += 2;
187 
188 	} while (*namp);
189 	namp = "ALbcbtcdceclcmcrcsdcDLdmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullmandnlpcrcscseSFsoSRtatetiucueupusvbvsvealdlsfsrALDLUPDOLERI";
190 	sp = sstrs;
191 	do {
192 		*tmp = *namp;
193 		*(tmp + 1) = *(namp + 1);
194 		*(*sp++) = tgetstr(tmp, &aoftspace);
195 #ifdef DEBUG
196 		__CTRACE("2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\"");
197 		if (*sp[-1] != NULL) {
198 			for (cp = *sp[-1]; *cp; cp++)
199 				__CTRACE("%s", unctrl(*cp));
200 			__CTRACE("\"\n");
201 		}
202 #endif
203 		namp += 2;
204 	} while (*namp);
205 	if (XS)
206 		SO = SE = NULL;
207 	else {
208 		if (tgetnum("sg") > 0)
209 			SO = NULL;
210 		if (tgetnum("ug") > 0)
211 			US = NULL;
212 		if (!SO && US) {
213 			SO = US;
214 			SE = UE;
215 		}
216 	}
217 }
218 
219 /*
220  * getcap --
221  *	Return a capability from termcap.
222  */
223 char *
224 getcap(name)
225 	char *name;
226 {
227 	return (tgetstr(name, &aoftspace));
228 }
229