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