xref: /netbsd-src/lib/libcurses/setterm.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /*	$NetBSD: setterm.c,v 1.33 2002/09/11 15:23:24 jdc 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.33 2002/09/11 15:23:24 jdc 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 const char *__attroff = "\x1b[m";
118 
119 int
120 setterm(char *type)
121 {
122 	return _cursesi_setterm(type, _cursesi_screen);
123 }
124 
125 int
126 _cursesi_setterm(char *type, SCREEN *screen)
127 {
128 	int unknown;
129 	struct winsize win;
130 	char *p;
131 	char cm_buff[64];
132 
133 #ifdef DEBUG
134 	__CTRACE("setterm: (\"%s\")\nLINES = %d, COLS = %d\n",
135 	    type, LINES, COLS);
136 #endif
137 	if (type[0] == '\0')
138 		type = "xx";
139 	unknown = 0;
140 	if (t_getent(&screen->cursesi_genbuf, type) != 1) {
141 		unknown++;
142 	}
143 #ifdef DEBUG
144 	__CTRACE("setterm: tty = %s\n", type);
145 #endif
146 
147 	/* Try TIOCGWINSZ, and, if it fails, the termcap entry. */
148 	if (ioctl(fileno(screen->outfd), TIOCGWINSZ, &win) != -1 &&
149 	    win.ws_row != 0 && win.ws_col != 0) {
150 		screen->LINES = win.ws_row;
151 		screen->COLS = win.ws_col;
152 	}  else {
153 		if (unknown) {
154 			screen->LINES = -1;
155 			screen->COLS = -1;
156 		} else {
157 			screen->LINES = t_getnum(screen->cursesi_genbuf, "li");
158 			screen->COLS = t_getnum(screen->cursesi_genbuf, "co");
159 		}
160 
161 	}
162 
163 	/* POSIX 1003.2 requires that the environment override. */
164 	if ((p = getenv("LINES")) != NULL)
165 		screen->LINES = (int) strtol(p, NULL, 10);
166 	if ((p = getenv("COLUMNS")) != NULL)
167 		screen->COLS = (int) strtol(p, NULL, 10);
168 
169 	/*
170 	 * Want cols > 4, otherwise things will fail.
171 	 */
172 	if (screen->COLS <= 4)
173 		return (ERR);
174 
175 #ifdef DEBUG
176 	__CTRACE("setterm: LINES = %d, COLS = %d\n", LINES, COLS);
177 #endif
178 	if (!unknown) {
179 		if (zap(screen) == ERR) /* Get terminal description.*/
180 			return ERR;
181 	}
182 
183 	/* If we can't tab, we can't backtab, either. */
184 	if (!screen->GT)
185 		screen->tc_bt = NULL;
186 
187 	/*
188 	 * Test for cursor motion capability.
189 	 *
190 	 */
191 	if (t_goto(NULL, screen->tc_cm, 0, 0, cm_buff, sizeof(cm_buff) - 1) < 0) {
192 		screen->CA = 0;
193 		screen->tc_cm = 0;
194 	} else
195 		screen->CA = 1;
196 
197         /*
198 	 * set the pad char, only take the first char of the pc capability
199 	 * as this is all we can use.
200 	 */
201 	screen->pad_char = screen->tc_pc ? screen->tc_pc[0] : 0;
202 
203 	/* Get full name of terminal */
204 	if (unknown) {
205 		strcpy(screen->ttytype, "dumb");
206 	} else {
207 		char *tcptr;
208 		size_t limit = 0;
209 		if (t_getterm(screen->cursesi_genbuf, 0, &limit))
210 			return ERR;
211 		if ((tcptr = malloc(limit + 1)) == NULL)
212 			return ERR;
213 		if (t_getterm(screen->cursesi_genbuf, &tcptr, 0) < 0)
214  			return ERR;
215 		__longname(tcptr, screen->ttytype);
216 		free(tcptr);
217 	}
218 
219 	/* If no scrolling commands, no quick change. */
220 	screen->noqch =
221   	    (screen->tc_cs == NULL || screen->tc_ho == NULL ||
222 	    (screen->tc_SF == NULL && screen->tc_sf == NULL) ||
223 	     (screen->tc_SR == NULL && screen->tc_sr == NULL)) &&
224 	    ((screen->tc_AL == NULL && screen->tc_al == NULL) ||
225 	     (screen->tc_DL == NULL && screen->tc_dl == NULL));
226 
227 	/*
228 	 * Precalculate conflict info for color/attribute end commands.
229 	 * Add a hack for xterm-like terminals where "\E[m" will turn off
230 	 * other attributes.
231 	 */
232 	screen->mask_op = __ATTRIBUTES & ~__COLOR;
233 	if (screen->tc_op != NULL) {
234 		if (screen->tc_se != NULL &&
235 		    (!strcmp(screen->tc_op, screen->tc_se) ||
236 		    !strcmp(screen->tc_op, __attroff)))
237 			screen->mask_op &= ~__STANDOUT;
238 		if (screen->tc_ue != NULL &&
239 		    (!strcmp(screen->tc_op, screen->tc_ue) ||
240 		    !strcmp(screen->tc_op, __attroff)))
241 			screen->mask_op &= ~__UNDERSCORE;
242 		if (screen->tc_me != NULL &&
243 		    (!strcmp(screen->tc_op, screen->tc_me) ||
244 		    !strcmp(screen->tc_op, __attroff)))
245 			screen->mask_op &= ~__TERMATTR;
246 	}
247 	screen->mask_me = __ATTRIBUTES & ~__TERMATTR;
248 	if (screen->tc_me != NULL) {
249 		if (screen->tc_se != NULL &&
250 		    (!strcmp(screen->tc_me, screen->tc_se) ||
251 		    !strcmp(screen->tc_me, __attroff)))
252 			screen->mask_me &= ~__STANDOUT;
253 		if (screen->tc_ue != NULL &&
254 		    (!strcmp(screen->tc_me, screen->tc_ue) ||
255 		    !strcmp(screen->tc_me, __attroff)))
256 			screen->mask_me &= ~__UNDERSCORE;
257 		if (screen->tc_op != NULL &&
258 		    (!strcmp(screen->tc_me, screen->tc_op) ||
259 		    !strcmp(screen->tc_me, __attroff)))
260 			screen->mask_me &= ~__COLOR;
261 	}
262 	screen->mask_ue = __ATTRIBUTES & ~__UNDERSCORE;
263 	if (screen->tc_ue != NULL) {
264 		if (screen->tc_se != NULL &&
265 		    (!strcmp(screen->tc_ue, screen->tc_se) ||
266 		    !strcmp(screen->tc_ue, __attroff)))
267 			screen->mask_ue &= ~__STANDOUT;
268 		if (screen->tc_me != NULL &&
269 		    (!strcmp(screen->tc_ue, screen->tc_me) ||
270 		    !strcmp(screen->tc_ue, __attroff)))
271 			screen->mask_ue &= ~__TERMATTR;
272 		if (screen->tc_op != NULL &&
273 		    (!strcmp(screen->tc_ue, screen->tc_op) ||
274 		    !strcmp(screen->tc_ue, __attroff)))
275 			screen->mask_ue &= ~__COLOR;
276 	}
277 	screen->mask_se = __ATTRIBUTES & ~__STANDOUT;
278 	if (screen->tc_se != NULL) {
279 		if (screen->tc_ue != NULL &&
280 		    (!strcmp(screen->tc_se, screen->tc_ue) ||
281 		    !strcmp(screen->tc_se, __attroff)))
282 			screen->mask_se &= ~__UNDERSCORE;
283 		if (screen->tc_me != NULL &&
284 		    (!strcmp(screen->tc_se, screen->tc_me) ||
285 		    !strcmp(screen->tc_se, __attroff)))
286 			screen->mask_se &= ~__TERMATTR;
287 		if (screen->tc_op != NULL &&
288 		    (!strcmp(screen->tc_se, screen->tc_op) ||
289 		    !strcmp(screen->tc_se, __attroff)))
290 			screen->mask_se &= ~__COLOR;
291 	}
292 
293 	return (unknown ? ERR : OK);
294 }
295 
296 /*
297  * _cursesi_resetterm --
298  *  Copy the terminal instance data for the given screen to the global
299  *  variables.
300  */
301 void
302 _cursesi_resetterm(SCREEN *screen)
303 {
304 	char ***sp, **scr_sp;
305 	int  **vp, *scr_vp, i;
306 	char **fp, *scr_fp;
307 
308 	LINES = screen->LINES;
309 	COLS = screen->COLS;
310 
311 	  /* reset terminal capabilities */
312 	fp = sflags;
313 	scr_fp = &screen->tc_am;
314 	for (i = 0; i < screen->flag_count; i++)
315 		*(*fp++) = *scr_fp++;
316 
317 	vp = svals;
318 	scr_vp = &screen->tc_pa;
319 	for (i = 0; i < screen->int_count; i++)
320 		*(*vp++) = *scr_vp++;
321 
322 	sp = sstrs;
323 	scr_sp = &screen->tc_AB;
324 	for (i = 0; i < screen->str_count; i++)
325 		*(*sp++) = *scr_sp++;
326 
327 	__GT = screen->GT;
328 	__CA = screen->CA;
329 
330 	PC = screen->pad_char;
331 
332 	__noqch = screen->noqch;
333 	__mask_op = screen->mask_op;
334 	__mask_me = screen->mask_me;
335 	__mask_ue = screen->mask_ue;
336 	__mask_se = screen->mask_se;
337 }
338 
339 /*
340  * zap --
341  *	Gets all the terminal flags from the termcap database.
342  */
343 static int
344 zap(SCREEN *screen)
345 {
346 	const char *nampstr, *namp;
347         char **sp;
348 	int  *vp;
349 	char *fp;
350 	char tmp[3];
351 #ifdef DEBUG
352 	char	*cp;
353 #endif
354 	tmp[2] = '\0';
355 
356 	namp = "ambsccdaeohchlinmimsncnsosulutxbxnxtxsxx";
357 	fp = &screen->tc_am;
358 	screen->flag_count = 0;
359 	do {
360 		*tmp = *namp;
361 		*(tmp + 1) = *(namp + 1);
362 		*fp++ = t_getflag(screen->cursesi_genbuf, tmp);
363 #ifdef DEBUG
364 		__CTRACE("%2.2s = %s\n", namp, fp[-1] ? "TRUE" : "FALSE");
365 #endif
366 		namp += 2;
367 		screen->flag_count++;
368 	} while (*namp);
369 	namp = "paCoNC";
370 	vp = &screen->tc_pa;
371 	screen->int_count = 0;
372 	do {
373 		*tmp = *namp;
374 		*(tmp + 1) = *(namp + 1);
375 		*vp++ = t_getnum(screen->cursesi_genbuf, tmp);
376 #ifdef DEBUG
377 		__CTRACE("%2.2s = %d\n", namp, vp[-1]);
378 #endif
379 		namp += 2;
380 		screen->int_count++;
381 	} while (*namp);
382 
383   	nampstr = "ABacaeAFALalasbcblbtcdceclcmcrcsdcDLdldmDOdoeAedeihoIcicimIpipk0k1k2k3k4k5k6k7k8k9kdkekhklkrkskuLEllmambmdmemhmkmmmompmrndnlocoppcrcRISbscseSFSfsfsospSRsrtatetiucueUPupusvbvevivs";
384 
385 	namp = nampstr;
386 	sp = &screen->tc_AB;
387 	screen->str_count = 0;
388 	do {
389 		*tmp = *namp;
390 		*(tmp + 1) = *(namp + 1);
391 		*sp++ = t_agetstr(screen->cursesi_genbuf, tmp);
392 #ifdef DEBUG
393 		__CTRACE("%2.2s = %s", namp, sp[-1] == NULL ? "NULL\n" : "\"");
394 		if (sp[-1] != NULL) {
395 			for (cp = sp[-1]; *cp; cp++)
396 				__CTRACE("%s", unctrl(*cp));
397 			__CTRACE("\"\n");
398 		}
399 #endif
400 		namp += 2;
401 		screen->str_count++;
402 	} while (*namp);
403 	if (screen->tc_xs)
404 		screen->tc_so = screen->tc_se = NULL;
405 	else {
406 		if (t_getnum(screen->cursesi_genbuf, "sg") > 0)
407 			screen->tc_so = NULL;
408 		if (t_getnum(screen->cursesi_genbuf, "ug") > 0)
409 			screen->tc_us = NULL;
410 		if (!screen->tc_so && screen->tc_us) {
411 			screen->tc_so = screen->tc_us;
412 			screen->tc_se = screen->tc_ue;
413 		}
414 	}
415 
416 	return OK;
417 }
418 
419 /*
420  * getcap --
421  *	Return a capability from termcap.
422  */
423 char	*
424 getcap(char *name)
425 {
426 	return (t_agetstr(_cursesi_genbuf, name));
427 }
428