xref: /original-bsd/lib/libcurses/setterm.c (revision e9b82df0)
1 /*
2  * Copyright (c) 1981 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)setterm.c	5.8 (Berkeley) 06/01/90";
10 #endif /* not lint */
11 
12 /*
13  * Terminal initialization routines.
14  *
15  */
16 
17 # include	"curses.ext"
18 
19 static bool	*sflags[] = {
20 			&AM, &BS, &DA, &DB, &EO, &HC, &HZ, &IN, &MI,
21 			&MS, &NC, &NS, &OS, &UL, &XB, &XN, &XT, &XS,
22 			&XX
23 		};
24 
25 static char	*_PC,
26 		**sstrs[] = {
27 			&AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS,
28 			&DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2,
29 			&K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC,
30 			&IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU,
31 			&LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF,
32 			&SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US,
33 			&VB, &VS, &VE, &AL_PARM, &DL_PARM, &UP_PARM,
34 			&DOWN_PARM, &LEFT_PARM, &RIGHT_PARM,
35 		};
36 
37 extern char	*tgoto();
38 
39 char		_tspace[2048];		/* Space for capability strings */
40 
41 static char	*aoftspace;		/* Address of _tspace for relocation */
42 
43 static int	destcol, destline;
44 
45 /*
46  *	This routine does terminal type initialization routines, and
47  * calculation of flags at entry.  It is almost entirely stolen from
48  * Bill Joy's ex version 2.6.
49  */
50 short	ospeed = -1;
51 
52 gettmode() {
53 
54 	if (ioctl(_tty_ch, TIOCGETP, &_tty) < 0)
55 		return;
56 	savetty();
57 	if (ioctl(_tty_ch, TIOCSETP, &_tty) < 0)
58 		_tty.sg_flags = _res_flg;
59 	ospeed = _tty.sg_ospeed;
60 	_res_flg = _tty.sg_flags;
61 	UPPERCASE = (_tty.sg_flags & LCASE) != 0;
62 	GT = ((_tty.sg_flags & XTABS) == 0);
63 	NONL = ((_tty.sg_flags & CRMOD) == 0);
64 	_tty.sg_flags &= ~XTABS;
65 	ioctl(_tty_ch, TIOCSETP, &_tty);
66 # ifdef DEBUG
67 	fprintf(outf, "GETTMODE: UPPERCASE = %s\n", UPPERCASE ? "TRUE":"FALSE");
68 	fprintf(outf, "GETTMODE: GT = %s\n", GT ? "TRUE" : "FALSE");
69 	fprintf(outf, "GETTMODE: NONL = %s\n", NONL ? "TRUE" : "FALSE");
70 	fprintf(outf, "GETTMODE: ospeed = %d\n", ospeed);
71 # endif
72 }
73 
74 setterm(type)
75 reg char	*type; {
76 
77 	reg int		unknown;
78 	static char	genbuf[1024];
79 # ifdef TIOCGWINSZ
80 	struct winsize win;
81 # endif
82 
83 # ifdef DEBUG
84 	fprintf(outf, "SETTERM(\"%s\")\n", type);
85 	fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
86 # endif
87 	if (type[0] == '\0')
88 		type = "xx";
89 	unknown = FALSE;
90 	if (tgetent(genbuf, type) != 1) {
91 		unknown++;
92 		strcpy(genbuf, "xx|dumb:");
93 	}
94 # ifdef DEBUG
95 	fprintf(outf, "SETTERM: tty = %s\n", type);
96 # endif
97 # ifdef TIOCGWINSZ
98 	if (ioctl(_tty_ch, TIOCGWINSZ, &win) >= 0) {
99 		if (LINES == 0)
100 			LINES = win.ws_row;
101 		if (COLS == 0)
102 			COLS = win.ws_col;
103 	}
104 # endif
105 
106 	if (LINES == 0)
107 		LINES = tgetnum("li");
108 	if (LINES <= 5)
109 		LINES = 24;
110 
111 	if (COLS == 0)
112 		COLS = tgetnum("co");
113 	if (COLS <= 4)
114 		COLS = 80;
115 
116 # ifdef DEBUG
117 	fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
118 # endif
119 	aoftspace = _tspace;
120 	zap();			/* get terminal description		*/
121 
122 	/*
123 	 * Handle funny termcap capabilities
124 	 */
125 	if (CS && SC && RC) AL=DL="";
126 	if (AL_PARM && AL==NULL) AL="";
127 	if (DL_PARM && DL==NULL) DL="";
128 	if (IC && IM==NULL) IM="";
129 	if (IC && EI==NULL) EI="";
130 	if (!GT) BT=NULL;	/* If we can't tab, we can't backtab either */
131 
132 	if (tgoto(CM, destcol, destline)[0] == 'O')
133 		CA = FALSE, CM = 0;
134 	else
135 		CA = TRUE;
136 
137 	PC = _PC ? _PC[0] : FALSE;
138 	aoftspace = _tspace;
139 	{
140 		/* xtype should be the same size as genbuf for longname(). */
141 		static char xtype[1024];
142 
143 		(void)strcpy(xtype,type);
144 		strncpy(ttytype, longname(genbuf, xtype), sizeof(ttytype) - 1);
145 	}
146 	ttytype[sizeof(ttytype) - 1] = '\0';
147 	if (unknown)
148 		return ERR;
149 	return OK;
150 }
151 
152 /*
153  *	This routine gets all the terminal flags from the termcap database
154  */
155 
156 zap()
157 {
158 	register char	*namp;
159 	register bool	**fp;
160 	register char	***sp;
161 #ifdef	DEBUG
162 	register char	*cp;
163 #endif
164 	extern char	*tgetstr();
165 
166 	namp = "ambsdadbeohchzinmimsncnsosulxbxnxtxsxx";
167 	fp = sflags;
168 	do {
169 		*(*fp++) = tgetflag(namp);
170 #ifdef DEBUG
171 		fprintf(outf, "%2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE");
172 #endif
173 		namp += 2;
174 	} while (*namp);
175 	namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullmandnlpcrcscsesfsosrtatetiucueupusvbvsveALDLUPDOLERI";
176 	sp = sstrs;
177 	do {
178 		*(*sp++) = tgetstr(namp, &aoftspace);
179 #ifdef DEBUG
180 		fprintf(outf, "%2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\"");
181 		if (*sp[-1] != NULL) {
182 			for (cp = *sp[-1]; *cp; cp++)
183 				fprintf(outf, "%s", unctrl(*cp));
184 			fprintf(outf, "\"\n");
185 		}
186 #endif
187 		namp += 2;
188 	} while (*namp);
189 	if (XS)
190 		SO = SE = NULL;
191 	else {
192 		if (tgetnum("sg") > 0)
193 			SO = NULL;
194 		if (tgetnum("ug") > 0)
195 			US = NULL;
196 		if (!SO && US) {
197 			SO = US;
198 			SE = UE;
199 		}
200 	}
201 }
202 
203 /*
204  * return a capability from termcap
205  */
206 char *
207 getcap(name)
208 char *name;
209 {
210 	char *tgetstr();
211 
212 	return tgetstr(name, &aoftspace);
213 }
214