xref: /netbsd-src/lib/libcurses/setterm.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: setterm.c,v 1.31 2002/01/02 10:38:29 blymn Exp $	*/
2 
3 /*
4  * Copyright (c) 1981, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)setterm.c	8.8 (Berkeley) 10/25/94";
40 #else
41 __RCSID("$NetBSD: setterm.c,v 1.31 2002/01/02 10:38:29 blymn Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/ioctl.h>		/* TIOCGWINSZ on old systems. */
46 
47 #include <stdlib.h>
48 #include <string.h>
49 #include <termios.h>
50 #include <unistd.h>
51 
52 #include "curses.h"
53 #include "curses_private.h"
54 
55 static int zap(SCREEN *screen);
56 
57 struct tinfo *_cursesi_genbuf;
58 
59 static char	*sflags[] = {
60 /*	      am        bs        cc        da        eo */
61 	 &__tc_am, &__tc_bs, &__tc_cc, &__tc_da, &__tc_eo,
62 /*	      hc        hl        in        mi        ms */
63 	&__tc_hc, &__tc_hl, &__tc_in, &__tc_mi, &__tc_ms,
64 /*	      nc        ns        os        ul        ut */
65 	&__tc_nc, &__tc_ns, &__tc_os, &__tc_ul, &__tc_ut,
66 /*	      xb        xn        xt        xs        xx */
67 	&__tc_xb, &__tc_xn, &__tc_xt, &__tc_xs, &__tc_xx
68 };
69 
70 static int	*svals[] = {
71 /*	      pa        Co        NC */
72 	&__tc_pa, &__tc_Co, &__tc_NC
73 };
74 
75 static char	*_PC,
76 	**sstrs[] = {
77 	/*	      AB        ac        ae        AF        AL */
78 		&__tc_AB, &__tc_ac, &__tc_ae, &__tc_AF, &__tc_AL,
79 	/*	      al        as        bc        bl        bt */
80 		&__tc_al, &__tc_as, &__tc_bc, &__tc_bl, &__tc_bt,
81 	/*	      cd        ce        cl        cm        cr */
82 		&__tc_cd, &__tc_ce, &__tc_cl, &__tc_cm, &__tc_cr,
83 	/*	      cs        dc        DL        dl        dm */
84 		&__tc_cs, &__tc_dc, &__tc_DL, &__tc_dl, &__tc_dm,
85 	/*	      DO        do        eA        ed        ei */
86 		&__tc_DO, &__tc_do, &__tc_eA, &__tc_ed, &__tc_ei,
87 	/*	      ho        Ic        ic        im        Ip */
88 		&__tc_ho, &__tc_Ic, &__tc_ic, &__tc_im, &__tc_Ip,
89 	/*	      ip        k0        k1        k2        k3 */
90 		&__tc_ip, &__tc_k0, &__tc_k1, &__tc_k2, &__tc_k3,
91 	/*	      k4        k5        k6        k7        k8 */
92 		&__tc_k4, &__tc_k5, &__tc_k6, &__tc_k7, &__tc_k8,
93 	/*	      k9        kd        ke        kh        kl */
94 		&__tc_k9, &__tc_kd, &__tc_ke, &__tc_kh, &__tc_kl,
95 	/*	      kr        ks        ku        LE        ll */
96 		&__tc_kr, &__tc_ks, &__tc_ku, &__tc_LE, &__tc_ll,
97 	/*	      ma        mb        md        me        mh */
98 		&__tc_ma, &__tc_mb, &__tc_md, &__tc_me, &__tc_mh,
99 	/*	      mk        mm        mo        mp        mr */
100 		&__tc_mk, &__tc_mm, &__tc_mo, &__tc_mp, &__tc_mr,
101 	/*	      nd        nl        oc        op    pc     */
102 		&__tc_nd, &__tc_nl, &__tc_oc, &__tc_op, &_PC,
103 	/*	      rc        RI        sc        Sb        se */
104 		&__tc_rc, &__tc_RI, &__tc_Sb, &__tc_sc, &__tc_se,
105 	/*	      SF        Sf        sf        so        sp */
106 		&__tc_SF, &__tc_Sf, &__tc_sf, &__tc_so, &__tc_sp,
107 	/*	      SR        sr        ta        te        ti */
108 		&__tc_SR, &__tc_sr, &__tc_ta, &__tc_te, &__tc_ti,
109 	/*	      uc        ue        UP        up        us */
110 		&__tc_uc, &__tc_ue, &__tc_UP, &__tc_up, &__tc_us,
111 	/*	      vb        ve        vi        vs           */
112 		&__tc_vb, &__tc_ve, &__tc_vi, &__tc_vs
113 	};
114 
115 attr_t	 __mask_op, __mask_me, __mask_ue, __mask_se;
116 
117 int
118 setterm(char *type)
119 {
120 	return _cursesi_setterm(type, _cursesi_screen);
121 }
122 
123 int
124 _cursesi_setterm(char *type, SCREEN *screen)
125 {
126 	static char cm_buff[1024], tc[1024], *tcptr;
127 	int unknown;
128 	size_t limit;
129 	struct winsize win;
130 	char *p;
131 
132 #ifdef DEBUG
133 	__CTRACE("setterm: (\"%s\")\nLINES = %d, COLS = %d\n",
134 	    type, LINES, COLS);
135 #endif
136 	if (type[0] == '\0')
137 		type = "xx";
138 	unknown = 0;
139 	if (t_getent(&screen->cursesi_genbuf, type) != 1) {
140 		unknown++;
141 	}
142 #ifdef DEBUG
143 	__CTRACE("setterm: tty = %s\n", type);
144 #endif
145 
146 	/* Try TIOCGWINSZ, and, if it fails, the termcap entry. */
147 	if (ioctl(fileno(screen->outfd), TIOCGWINSZ, &win) != -1 &&
148 	    win.ws_row != 0 && win.ws_col != 0) {
149 		screen->LINES = win.ws_row;
150 		screen->COLS = win.ws_col;
151 	}  else {
152 		if (unknown) {
153 			screen->LINES = -1;
154 			screen->COLS = -1;
155 		} else {
156 			screen->LINES = t_getnum(screen->cursesi_genbuf, "li");
157 			screen->COLS = t_getnum(screen->cursesi_genbuf, "co");
158 		}
159 
160 	}
161 
162 	/* POSIX 1003.2 requires that the environment override. */
163 	if ((p = getenv("LINES")) != NULL)
164 		screen->LINES = (int) strtol(p, NULL, 10);
165 	if ((p = getenv("COLUMNS")) != NULL)
166 		screen->COLS = (int) strtol(p, NULL, 10);
167 
168 	/*
169 	 * Want cols > 4, otherwise things will fail.
170 	 */
171 	if (screen->COLS <= 4)
172 		return (ERR);
173 
174 #ifdef DEBUG
175 	__CTRACE("setterm: LINES = %d, COLS = %d\n", LINES, COLS);
176 #endif
177 	if (!unknown) {
178 		if (zap(screen) == ERR) /* Get terminal description.*/
179 			return ERR;
180 	}
181 
182 	/* If we can't tab, we can't backtab, either. */
183 	if (!screen->GT)
184 		screen->tc_bt = NULL;
185 
186 	/*
187 	 * Test for cursor motion capability.
188 	 *
189 	 */
190 	if (t_goto(NULL, screen->tc_cm, 0, 0, cm_buff, 1023) < 0) {
191 		screen->CA = 0;
192 		screen->tc_cm = 0;
193 	} else
194 		screen->CA = 1;
195 
196         /*
197 	 * set the pad char, only take the first char of the pc capability
198 	 * as this is all we can use.
199 	 */
200 	screen->pad_char = screen->tc_pc ? screen->tc_pc[0] : 0;
201 
202 	if (unknown) {
203 		strcpy(screen->ttytype, "dumb");
204 	} else {
205 		tcptr = tc;
206 		limit = 1023;
207 		if (t_getterm(screen->cursesi_genbuf, &tcptr, &limit) < 0)
208 			return ERR;
209 		__longname(tc, screen->ttytype);
210 	}
211 
212 	/* If no scrolling commands, no quick change. */
213 	screen->noqch =
214   	    (screen->tc_cs == NULL || screen->tc_ho == NULL ||
215 	    (screen->tc_SF == NULL && screen->tc_sf == NULL) ||
216 	     (screen->tc_SR == NULL && screen->tc_sr == NULL)) &&
217 	    ((screen->tc_AL == NULL && screen->tc_al == NULL) ||
218 	     (screen->tc_DL == NULL && screen->tc_dl == NULL));
219 
220 	/* Precalculate conflict info for color/attribute end commands. */
221 	screen->mask_op = __ATTRIBUTES & ~__COLOR;
222 	if (screen->tc_op != NULL) {
223 		if (screen->tc_se != NULL &&
224 		    !strcmp(screen->tc_op, screen->tc_se))
225 			screen->mask_op &= ~__STANDOUT;
226 		if (screen->tc_ue != NULL &&
227 		    !strcmp(screen->tc_op, screen->tc_ue))
228 			screen->mask_op &= ~__UNDERSCORE;
229 		if (screen->tc_me != NULL &&
230 		    !strcmp(screen->tc_op, screen->tc_me))
231 			screen->mask_op &= ~__TERMATTR;
232 	}
233 	screen->mask_me = __ATTRIBUTES & ~__TERMATTR;
234 	if (screen->tc_me != NULL) {
235 		if (screen->tc_se != NULL &&
236 		    !strcmp(screen->tc_me, screen->tc_se))
237 			screen->mask_me &= ~__STANDOUT;
238 		if (screen->tc_ue != NULL &&
239 		    !strcmp(screen->tc_me, screen->tc_ue))
240 			screen->mask_me &= ~__UNDERSCORE;
241 		if (screen->tc_op != NULL &&
242 		    !strcmp(screen->tc_me, screen->tc_op))
243 			screen->mask_me &= ~__COLOR;
244 	}
245 	screen->mask_ue = __ATTRIBUTES & ~__UNDERSCORE;
246 	if (screen->tc_ue != NULL) {
247 		if (screen->tc_se != NULL &&
248 		    !strcmp(screen->tc_ue, screen->tc_se))
249 			screen->mask_ue &= ~__STANDOUT;
250 		if (screen->tc_me != NULL &&
251 		    !strcmp(screen->tc_ue, screen->tc_me))
252 			screen->mask_ue &= ~__TERMATTR;
253 		if (screen->tc_op != NULL &&
254 		    !strcmp(screen->tc_ue, screen->tc_op))
255 			screen->mask_ue &= ~__COLOR;
256 	}
257 	screen->mask_se = __ATTRIBUTES & ~__STANDOUT;
258 	if (screen->tc_se != NULL) {
259 		if (screen->tc_ue != NULL &&
260 		    !strcmp(screen->tc_se, screen->tc_ue))
261 			screen->mask_se &= ~__UNDERSCORE;
262 		if (screen->tc_me != NULL &&
263 		    !strcmp(screen->tc_se, screen->tc_me))
264 			screen->mask_se &= ~__TERMATTR;
265 		if (screen->tc_op != NULL &&
266 		    !strcmp(screen->tc_se, screen->tc_op))
267 			screen->mask_se &= ~__COLOR;
268 	}
269 
270 	return (unknown ? ERR : OK);
271 }
272 
273 /*
274  * _cursesi_resetterm --
275  *  Copy the terminal instance data for the given screen to the global
276  *  variables.
277  */
278 void
279 _cursesi_resetterm(SCREEN *screen)
280 {
281 	char ***sp, **scr_sp;
282 	int  **vp, *scr_vp, i;
283 	char **fp, *scr_fp;
284 
285 	LINES = screen->LINES;
286 	COLS = screen->COLS;
287 
288 	  /* reset terminal capabilities */
289 	fp = sflags;
290 	scr_fp = &screen->tc_am;
291 	for (i = 0; i < screen->flag_count; i++)
292 		*(*fp++) = *scr_fp++;
293 
294 	vp = svals;
295 	scr_vp = &screen->tc_pa;
296 	for (i = 0; i < screen->int_count; i++)
297 		*(*vp++) = *scr_vp++;
298 
299 	sp = sstrs;
300 	scr_sp = &screen->tc_AB;
301 	for (i = 0; i < screen->str_count; i++)
302 		*(*sp++) = *scr_sp++;
303 
304 	__GT = screen->GT;
305 	__CA = screen->CA;
306 
307 	PC = screen->pad_char;
308 
309 	__noqch = screen->noqch;
310 	__mask_op = screen->mask_op;
311 	__mask_me = screen->mask_me;
312 	__mask_ue = screen->mask_ue;
313 	__mask_se = screen->mask_se;
314 }
315 
316 /*
317  * zap --
318  *	Gets all the terminal flags from the termcap database.
319  */
320 static int
321 zap(SCREEN *screen)
322 {
323 	const char *nampstr, *namp;
324         char **sp;
325 	int  *vp;
326 	char *fp;
327 	char tmp[3];
328 #ifdef DEBUG
329 	char	*cp;
330 #endif
331 	tmp[2] = '\0';
332 
333 	namp = "ambsccdaeohchlinmimsncnsosulutxbxnxtxsxx";
334 	fp = &screen->tc_am;
335 	screen->flag_count = 0;
336 	do {
337 		*tmp = *namp;
338 		*(tmp + 1) = *(namp + 1);
339 		*fp++ = t_getflag(screen->cursesi_genbuf, tmp);
340 #ifdef DEBUG
341 		__CTRACE("%2.2s = %s\n", namp, fp[-1] ? "TRUE" : "FALSE");
342 #endif
343 		namp += 2;
344 		screen->flag_count++;
345 	} while (*namp);
346 	namp = "paCoNC";
347 	vp = &screen->tc_pa;
348 	screen->int_count = 0;
349 	do {
350 		*tmp = *namp;
351 		*(tmp + 1) = *(namp + 1);
352 		*vp++ = t_getnum(screen->cursesi_genbuf, tmp);
353 #ifdef DEBUG
354 		__CTRACE("%2.2s = %d\n", namp, *vp[-1]);
355 #endif
356 		namp += 2;
357 		screen->int_count++;
358 	} while (*namp);
359 
360   	nampstr = "ABacaeAFALalasbcblbtcdceclcmcrcsdcDLdldmDOdoeAedeihoIcicimIpipk0k1k2k3k4k5k6k7k8k9kdkekhklkrkskuLEllmambmdmemhmkmmmompmrndnlocoppcrcRISbscseSFSfsfsospSRsrtatetiucueUPupusvbvevivs";
361 
362 	namp = nampstr;
363 	sp = &screen->tc_AB;
364 	screen->str_count = 0;
365 	do {
366 		*tmp = *namp;
367 		*(tmp + 1) = *(namp + 1);
368 		*sp++ = t_agetstr(screen->cursesi_genbuf, tmp);
369 #ifdef DEBUG
370 		__CTRACE("%2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\"");
371 		if (sp[-1] != NULL) {
372 			for (cp = sp[-1]; *cp; cp++)
373 				__CTRACE("%s", unctrl(*cp));
374 			__CTRACE("\"\n");
375 		}
376 #endif
377 		namp += 2;
378 		screen->str_count++;
379 	} while (*namp);
380 	if (screen->tc_xs)
381 		screen->tc_so = screen->tc_se = NULL;
382 	else {
383 		if (t_getnum(screen->cursesi_genbuf, "sg") > 0)
384 			screen->tc_so = NULL;
385 		if (t_getnum(screen->cursesi_genbuf, "ug") > 0)
386 			screen->tc_us = NULL;
387 		if (!screen->tc_so && screen->tc_us) {
388 			screen->tc_so = screen->tc_us;
389 			screen->tc_se = screen->tc_ue;
390 		}
391 	}
392 
393 	return OK;
394 }
395 
396 /*
397  * getcap --
398  *	Return a capability from termcap.
399  */
400 char	*
401 getcap(char *name)
402 {
403 	return (t_agetstr(_cursesi_genbuf, name));
404 }
405