date and time created 88/11/14 09:47:10 by bostic
[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 *
5 * Redistribution and use in source and binary forms are permitted
a399f6c8
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
6e1c93d0
DF
16 */
17
18#ifndef lint
a399f6c8 19static char sccsid[] = "@(#)setterm.c 5.4 (Berkeley) %G%";
2f14f200 20#endif /* not lint */
6e1c93d0 21
658815ab
KA
22/*
23 * Terminal initialization routines.
24 *
658815ab
KA
25 */
26
658815ab 27# include "curses.ext"
658815ab 28
d45d8bdb
JB
29static bool *sflags[] = {
30 &AM, &BS, &DA, &DB, &EO, &HC, &HZ, &IN, &MI,
4b8e1c40
JB
31 &MS, &NC, &NS, &OS, &UL, &XB, &XN, &XT, &XS,
32 &XX
658815ab
KA
33 };
34
d45d8bdb
JB
35static char *_PC,
36 **sstrs[] = {
37 &AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS,
38 &DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2,
39 &K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC,
40 &IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU,
41 &LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF,
42 &SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US,
43 &VB, &VS, &VE, &AL_PARM, &DL_PARM, &UP_PARM,
44 &DOWN_PARM, &LEFT_PARM, &RIGHT_PARM,
658815ab 45 },
0d0dfbce 46 *tgoto();
658815ab 47
d45d8bdb
JB
48char _tspace[2048]; /* Space for capability strings */
49
50static char *aoftspace; /* Address of _tspace for relocation */
658815ab
KA
51
52static int destcol, destline;
53
54/*
55 * This routine does terminal type initialization routines, and
56 * calculation of flags at entry. It is almost entirely stolen from
57 * Bill Joy's ex version 2.6.
58 */
59short ospeed = -1;
60
61gettmode() {
62
63 if (gtty(_tty_ch, &_tty) < 0)
64 return;
65 savetty();
66 if (stty(_tty_ch, &_tty) < 0)
67 _tty.sg_flags = _res_flg;
68 ospeed = _tty.sg_ospeed;
69 _res_flg = _tty.sg_flags;
70 UPPERCASE = (_tty.sg_flags & LCASE) != 0;
71 GT = ((_tty.sg_flags & XTABS) == 0);
72 NONL = ((_tty.sg_flags & CRMOD) == 0);
d581dac5 73 _tty.sg_flags &= ~XTABS;
8ec7ff8b 74 stty(_tty_ch, &_tty);
658815ab
KA
75# ifdef DEBUG
76 fprintf(outf, "GETTMODE: UPPERCASE = %s\n", UPPERCASE ? "TRUE":"FALSE");
77 fprintf(outf, "GETTMODE: GT = %s\n", GT ? "TRUE" : "FALSE");
78 fprintf(outf, "GETTMODE: NONL = %s\n", NONL ? "TRUE" : "FALSE");
79 fprintf(outf, "GETTMODE: ospeed = %d\n", ospeed);
80# endif
81}
82
83setterm(type)
84reg char *type; {
85
1fe7814b
KA
86 reg int unknown;
87 static char genbuf[1024];
d45d8bdb 88# ifdef TIOCGWINSZ
6edfd2db 89 struct winsize win;
d45d8bdb 90# endif
658815ab
KA
91
92# ifdef DEBUG
93 fprintf(outf, "SETTERM(\"%s\")\n", type);
94 fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
95# endif
96 if (type[0] == '\0')
97 type = "xx";
98 unknown = FALSE;
99 if (tgetent(genbuf, type) != 1) {
100 unknown++;
101 strcpy(genbuf, "xx|dumb:");
102 }
103# ifdef DEBUG
104 fprintf(outf, "SETTERM: tty = %s\n", type);
105# endif
d45d8bdb 106# ifdef TIOCGWINSZ
6edfd2db
JB
107 if (ioctl(_tty_ch, TIOCGWINSZ, &win) >= 0) {
108 if (LINES == 0)
109 LINES = win.ws_row;
110 if (COLS == 0)
111 COLS = win.ws_col;
112 }
d45d8bdb 113# endif
6edfd2db 114
658815ab
KA
115 if (LINES == 0)
116 LINES = tgetnum("li");
117 if (LINES <= 5)
118 LINES = 24;
658815ab
KA
119
120 if (COLS == 0)
121 COLS = tgetnum("co");
122 if (COLS <= 4)
123 COLS = 80;
623c4d86 124
658815ab
KA
125# ifdef DEBUG
126 fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
127# endif
d45d8bdb 128 aoftspace = _tspace;
658815ab 129 zap(); /* get terminal description */
d45d8bdb
JB
130
131 /*
132 * Handle funny termcap capabilities
133 */
134 if (CS && SC && RC) AL=DL="";
135 if (AL_PARM && AL==NULL) AL="";
136 if (DL_PARM && DL==NULL) DL="";
137 if (IC && IM==NULL) IM="";
138 if (IC && EI==NULL) EI="";
139 if (!GT) BT=NULL; /* If we can't tab, we can't backtab either */
140
658815ab
KA
141 if (tgoto(CM, destcol, destline)[0] == 'O')
142 CA = FALSE, CM = 0;
143 else
144 CA = TRUE;
d45d8bdb
JB
145
146 PC = _PC ? _PC[0] : FALSE;
147 aoftspace = _tspace;
a9674233
JB
148 strncpy(ttytype, longname(genbuf, type), sizeof(ttytype) - 1);
149 ttytype[sizeof(ttytype) - 1] = '\0';
658815ab
KA
150 if (unknown)
151 return ERR;
152 return OK;
153}
d45d8bdb 154
658815ab 155/*
0c1e0ce7 156 * This routine gets all the terminal flags from the termcap database
658815ab 157 */
658815ab 158
d45d8bdb
JB
159zap()
160{
161 register char *namp;
162 register bool **fp;
163 register char ***sp;
164#ifdef DEBUG
165 register char *cp;
166#endif
658815ab
KA
167 extern char *tgetstr();
168
4b8e1c40 169 namp = "ambsdadbeohchzinmimsncnsosulxbxnxtxsxx";
658815ab
KA
170 fp = sflags;
171 do {
172 *(*fp++) = tgetflag(namp);
d45d8bdb
JB
173#ifdef DEBUG
174 fprintf(outf, "%2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE");
175#endif
658815ab
KA
176 namp += 2;
177 } while (*namp);
d45d8bdb 178 namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullmandnlpcrcscsesfsosrtatetiucueupusvbvsveALDLUPDOLERI";
658815ab
KA
179 sp = sstrs;
180 do {
181 *(*sp++) = tgetstr(namp, &aoftspace);
d45d8bdb
JB
182#ifdef DEBUG
183 fprintf(outf, "%2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\"");
184 if (*sp[-1] != NULL) {
185 for (cp = *sp[-1]; *cp; cp++)
186 fprintf(outf, "%s", unctrl(*cp));
187 fprintf(outf, "\"\n");
188 }
189#endif
658815ab
KA
190 namp += 2;
191 } while (*namp);
4b8e1c40
JB
192 if (XS)
193 SO = SE = NULL;
194 else {
195 if (tgetnum("sg") > 0)
196 SO = NULL;
197 if (tgetnum("ug") > 0)
198 US = NULL;
199 if (!SO && US) {
200 SO = US;
201 SE = UE;
202 }
658815ab
KA
203 }
204}
f81f846b
KA
205
206/*
207 * return a capability from termcap
208 */
209char *
210getcap(name)
211char *name;
212{
0c1e0ce7 213 char *tgetstr();
6cc9d15d 214
0c1e0ce7 215 return tgetstr(name, &aoftspace);
f81f846b 216}