rewrite tstp as __stop_signal_handler: new implementation
[unix-history] / usr / src / lib / libcurses / setterm.c
CommitLineData
6e1c93d0 1/*
2f14f200
KB
2 * Copyright (c) 1981 Regents of the University of California.
3 * All rights reserved.
4 *
c07973a2 5 * %sccs.include.redist.c%
6e1c93d0
DF
6 */
7
8#ifndef lint
79ec5a07 9static char sccsid[] = "@(#)setterm.c 5.14 (Berkeley) %G%";
2f14f200 10#endif /* not lint */
6e1c93d0 11
f826011f
KB
12#include <sys/ioctl.h>
13
14#include <curses.h>
15#include <stdlib.h>
16#include <string.h>
17#include <unistd.h>
658815ab 18
f826011f 19static void zap __P((void));
658815ab 20
f826011f
KB
21static char *sflags[] = {
22 &AM, &BS, &DA, &EO, &HC, &HZ, &IN, &MI, &MS,
23 &NC, &NS, &OS, &UL, &XB, &XN, &XT, &XS, &XX
658815ab
KA
24 };
25
d45d8bdb
JB
26static char *_PC,
27 **sstrs[] = {
28 &AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS,
29 &DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2,
30 &K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC,
31 &IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU,
32 &LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF,
33 &SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US,
f007057e
EA
34 &VB, &VS, &VE, &al, &dl, &sf, &sr, &AL_PARM,
35 &DL_PARM, &UP_PARM, &DOWN_PARM, &LEFT_PARM,
36 &RIGHT_PARM,
e3c13f11
KB
37 };
38
d45d8bdb 39static char *aoftspace; /* Address of _tspace for relocation */
f826011f 40static char tspace[2048]; /* Space for capability strings */
658815ab 41
f826011f 42char *ttytype;
658815ab 43
f826011f 44int
658815ab 45setterm(type)
f826011f
KB
46 register char *type;
47{
48 static char genbuf[1024];
49 static char __ttytype[1024];
50 register int unknown;
cd9a3cf2 51 int destcol, destline;
6edfd2db 52 struct winsize win;
f826011f 53 char *p;
658815ab 54
f826011f
KB
55#ifdef DEBUG
56 __TRACE("setterm: (\"%s\")\nLINES = %d, COLS = %d\n",
57 type, LINES, COLS);
58#endif
658815ab
KA
59 if (type[0] == '\0')
60 type = "xx";
f826011f 61 unknown = 0;
658815ab
KA
62 if (tgetent(genbuf, type) != 1) {
63 unknown++;
64 strcpy(genbuf, "xx|dumb:");
65 }
f826011f
KB
66#ifdef DEBUG
67 __TRACE("setterm: tty = %s\n", type);
68#endif
6edfd2db 69
f826011f
KB
70 /* Try TIOCGWINSZ, and, if it fails, the termcap entry. */
71 if (ioctl(STDERR_FILENO, TIOCGWINSZ, &win) != -1 &&
72 win.ws_row != 0 && win.ws_col != 0) {
73 LINES = win.ws_row;
74 COLS = win.ws_col;
75 } else {
658815ab 76 LINES = tgetnum("li");
658815ab 77 COLS = tgetnum("co");
f826011f 78 }
623c4d86 79
f826011f
KB
80 /* POSIX 1003.2 requires that the environment override. */
81 if ((p = getenv("ROWS")) != NULL)
82 LINES = strtol(p, NULL, 10);
83 if ((p = getenv("COLUMNS")) != NULL)
84 COLS = strtol(p, NULL, 10);
d45d8bdb
JB
85
86 /*
f826011f
KB
87 * XXX
88 * Historically, curses fails if rows <= 5, cols <= 4.
d45d8bdb 89 */
f826011f 90 if (LINES <= 5 || COLS <= 4)
79ec5a07 91 return (ERR);
f826011f
KB
92
93#ifdef DEBUG
94 __TRACE("setterm: LINES = %d, COLS = %d\n", LINES, COLS);
95#endif
96 aoftspace = tspace;
97 zap(); /* Get terminal description. */
98
99 /* Handle funny termcap capabilities. */
100 if (CS && SC && RC)
101 AL = DL = "";
102 if (AL_PARM && AL == NULL)
103 AL = "";
104 if (DL_PARM && DL == NULL)
105 DL = "";
106 if (IC) {
107 if (IM == NULL)
108 IM = "";
109 if (EI == NULL)
110 EI = "";
c44ef71b 111 }
f826011f
KB
112 if (!GT) /* If we can't tab, we can't backtab either. */
113 BT = NULL;
114
115 if (tgoto(CM, destcol, destline)[0] == 'O') {
116 CA = 0;
117 CM = 0;
118 } else
119 CA = 1;
120
121 PC = _PC ? _PC[0] : 0;
122 aoftspace = tspace;
123 ttytype = longname(genbuf, __ttytype);
124
f007057e
EA
125 if ((!AL && !al) || (!DL && !dl))
126 __noqch = 1;
127
79ec5a07 128 return (unknown ? ERR : OK);
658815ab 129}
d45d8bdb 130
658815ab 131/*
f826011f
KB
132 * zap --
133 * Gets all the terminal flags from the termcap database.
658815ab 134 */
f826011f 135static void
d45d8bdb
JB
136zap()
137{
f826011f
KB
138 register char *namp, ***sp;
139 register char **fp;
0c5c5188 140 char tmp[3];
f826011f 141#ifdef DEBUG
d45d8bdb
JB
142 register char *cp;
143#endif
0c5c5188 144 tmp[2] = '\0';
658815ab 145
4b8e1c40 146 namp = "ambsdadbeohchzinmimsncnsosulxbxnxtxsxx";
658815ab
KA
147 fp = sflags;
148 do {
0c5c5188
EA
149 *tmp = *namp;
150 *(tmp + 1) = *(namp + 1);
151 *(*fp++) = tgetflag(tmp);
d45d8bdb 152#ifdef DEBUG
f826011f 153 __TRACE("2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE");
d45d8bdb 154#endif
658815ab 155 namp += 2;
0c5c5188 156
658815ab 157 } while (*namp);
f007057e 158 namp = "ALbcbtcdceclcmcrcsdcDLdmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullmandnlpcrcscseSFsoSRtatetiucueupusvbvsvealdlsfsrALDLUPDOLERI";
658815ab
KA
159 sp = sstrs;
160 do {
0c5c5188
EA
161 *tmp = *namp;
162 *(tmp + 1) = *(namp + 1);
163 *(*sp++) = tgetstr(tmp, &aoftspace);
d45d8bdb 164#ifdef DEBUG
f826011f 165 __TRACE("2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\"");
d45d8bdb
JB
166 if (*sp[-1] != NULL) {
167 for (cp = *sp[-1]; *cp; cp++)
f826011f
KB
168 __TRACE("%s", unctrl(*cp));
169 __TRACE("\"\n");
d45d8bdb
JB
170 }
171#endif
658815ab
KA
172 namp += 2;
173 } while (*namp);
4b8e1c40
JB
174 if (XS)
175 SO = SE = NULL;
176 else {
177 if (tgetnum("sg") > 0)
178 SO = NULL;
179 if (tgetnum("ug") > 0)
180 US = NULL;
181 if (!SO && US) {
182 SO = US;
183 SE = UE;
184 }
658815ab
KA
185 }
186}
f81f846b
KA
187
188/*
f826011f
KB
189 * getcap --
190 * Return a capability from termcap.
f81f846b
KA
191 */
192char *
193getcap(name)
f826011f 194 char *name;
f81f846b 195{
f826011f 196 return (tgetstr(name, &aoftspace));
f81f846b 197}