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