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