Commit | Line | Data |
---|---|---|
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 | |
8 | static char sccsid[] = "@(#)setterm.c 5.1 (Berkeley) %G%"; | |
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 |
18 | static 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 |
24 | static 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 |
37 | char _tspace[2048]; /* Space for capability strings */ |
38 | ||
39 | static char *aoftspace; /* Address of _tspace for relocation */ | |
658815ab KA |
40 | |
41 | static 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 | */ | |
48 | short ospeed = -1; | |
49 | ||
50 | gettmode() { | |
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 | ||
72 | setterm(type) | |
73 | reg 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; | |
658815ab KA |
137 | strcpy(ttytype, longname(genbuf, type)); |
138 | if (unknown) | |
139 | return ERR; | |
140 | return OK; | |
141 | } | |
d45d8bdb | 142 | |
658815ab | 143 | /* |
0c1e0ce7 | 144 | * This routine gets all the terminal flags from the termcap database |
658815ab | 145 | */ |
658815ab | 146 | |
d45d8bdb JB |
147 | zap() |
148 | { | |
149 | register char *namp; | |
150 | register bool **fp; | |
151 | register char ***sp; | |
152 | #ifdef DEBUG | |
153 | register char *cp; | |
154 | #endif | |
658815ab KA |
155 | extern char *tgetstr(); |
156 | ||
4b8e1c40 | 157 | namp = "ambsdadbeohchzinmimsncnsosulxbxnxtxsxx"; |
658815ab KA |
158 | fp = sflags; |
159 | do { | |
160 | *(*fp++) = tgetflag(namp); | |
d45d8bdb JB |
161 | #ifdef DEBUG |
162 | fprintf(outf, "%2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE"); | |
163 | #endif | |
658815ab KA |
164 | namp += 2; |
165 | } while (*namp); | |
d45d8bdb | 166 | namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullmandnlpcrcscsesfsosrtatetiucueupusvbvsveALDLUPDOLERI"; |
658815ab KA |
167 | sp = sstrs; |
168 | do { | |
169 | *(*sp++) = tgetstr(namp, &aoftspace); | |
d45d8bdb JB |
170 | #ifdef DEBUG |
171 | fprintf(outf, "%2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\""); | |
172 | if (*sp[-1] != NULL) { | |
173 | for (cp = *sp[-1]; *cp; cp++) | |
174 | fprintf(outf, "%s", unctrl(*cp)); | |
175 | fprintf(outf, "\"\n"); | |
176 | } | |
177 | #endif | |
658815ab KA |
178 | namp += 2; |
179 | } while (*namp); | |
4b8e1c40 JB |
180 | if (XS) |
181 | SO = SE = NULL; | |
182 | else { | |
183 | if (tgetnum("sg") > 0) | |
184 | SO = NULL; | |
185 | if (tgetnum("ug") > 0) | |
186 | US = NULL; | |
187 | if (!SO && US) { | |
188 | SO = US; | |
189 | SE = UE; | |
190 | } | |
658815ab KA |
191 | } |
192 | } | |
f81f846b KA |
193 | |
194 | /* | |
195 | * return a capability from termcap | |
196 | */ | |
197 | char * | |
198 | getcap(name) | |
199 | char *name; | |
200 | { | |
0c1e0ce7 | 201 | char *tgetstr(); |
6cc9d15d | 202 | |
0c1e0ce7 | 203 | return tgetstr(name, &aoftspace); |
f81f846b | 204 | } |