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