Debian Codegraph @

termcap.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 #include "jove.h"
9
10 #ifdef TERMCAP /* the body is the rest of the file */
11
12 #include "term.h"
13
14 #include "disp.h"
15 #include "fmt.h"
16 #include "fp.h"
17 #include "jctype.h"
18 #include "screen.h"
19
20 extern int (tgetent)(char */*buf*/, const char */*name*/);
21 extern int (tgetflag)(const char */*id*/);
22 extern int (tgetnum)(const char */*id*/);
23 extern char *(tgetstr)(const char */*id*/, char **/*area*/);
24 extern void (tputs)(const char *, int, void (*)(char));
25
26 /* Termcap definitions */
27
28 const char
29 *CS, /* change scrolling region */
30 *SO, /* Start standout */
31 *SE, /* End standout */
32 *US, /* Start underlining */
33 *UE, /* End underlining */
34 *CM, /* The cursor motion string */
35 *CL, /* Clear screen */
36 *CE, /* Clear to end of line */
37 *HO, /* Home cursor */
38 *AL, /* Addline (insert line) */
39 *DL, /* Delete line */
40 *VS, /* Visual start */
41 *VE, /* Visual end */
42 *KS, /* Keypad mode start */
43 *KE, /* Keypad mode end */
44 *TI, /* Cursor addressing start */
45 *TE, /* Cursor addressing end */
46 *LL, /* Last line, first column */
47 *SF = "\n", /* Scroll forward (defaults to \n) */
48 *SR, /* Scroll reverse */
49 *VB, /* visible bell */
50 *BL = "\007", /* audible bell (defaults to BEL) */
51 *lPC, /* pad character (as a string!) */
52 *NL = "\n", /* newline character (defaults to \n) */
53 *DO = "\n", /* down one line (defaults to \n capability) */
54 *M_AL, /* Insert line with arg */
55 *M_DL, /* Delete line with arg */
56 *M_SF, /* Scroll forward with arg */
57 *M_SR; /* Scroll back with arg */
58
59 int
60 UPlen = INFINITY, /* length of the UP string */
61 HOlen = INFINITY, /* length of Home string */
62 LLlen = INFINITY, /* length of last line string */
63
64 phystab = 8, /* ("it") terminal's tabstop settings */
65 UG; /* number of magic cookies left by US and UE */
66
67 jbool
68 Hazeltine, /* Hazeltine tilde kludge */
69 UL, /* underscores don't replace chars already on screen */
70 NP; /* there is No Pad character */
71
72 # ifdef DEFINE_PC_BC_UP_OSPEED
73 /* This is needed for HP-UX, possibly for other SYSVR2 systems */
74 char
75 PC; /* pad character, as a char (set from lPC; defaults to NUL) */
76
77 const char
78 *BC, /* back space (defaults to BS) */
79 *UP; /* Scroll reverse, or up */
80
81 short ospeed;
82 # endif /* DEFINE_PC_BC_UP_OSPEED */
83
84 jbool CanScroll; /* can this terminal scroll? */
85
86 # ifdef ID_CHAR
87
88 const char
89 *IC, /* Insert char */
90 *DC, /* Delete char */
91 *IM, /* Insert mode */
92 *EI, /* End insert mode */
93 *IP, /* insert pad after character inserted */
94 *M_IC, /* Insert char with arg */
95 *M_DC; /* Delete char with arg */
96
97 jbool UseIC = NO; /* VAR: whether or not to use i/d char processesing */
98
99 int
100 # ifdef NCURSES_BUG
101 IMEIlen = INFINITY, /* length of insert mode + end insert mode strings */
102 # else
103 IMEIlen = 0, /* length of insert mode + end insert mode strings */
104 # endif
105
106 IClen = INFINITY, /* length of insert char */
107 MIClen = INFINITY, /* length of insert char with arg */
108 DClen = INFINITY, /* length of delete char */
109 MDClen = INFINITY, /* length of delete char with arg */
110 CElen = INFINITY; /* length of clear to end of line */
111
112 jbool
113 MI; /* okay to move while in insert mode */
114
115 # endif /* ID_CHAR */
116
117 /* as of 20200104, xterm-new is 254 bytes and guru-nctxt is 289 bytes! */
118 private char tspace[320]; /* space for termcap strings */
119
120 /* The ordering of ts and meas must agree !! */
121 private const char ts[] =
122 # ifdef ID_CHAR
123 /* | | | | */
124 "vsvealdlcssoseusuecmclcehoupbcllsfsrvbksketitepcblnldoALDLSFSRicimdceiipICDC";
125 # else
126 "vsvealdlcssoseusuecmclcehoupbcllsfsrvbksketitepcblnldoALDLSFSR";
127 # endif
128
129 private const char **const meas[] = {
130 &VS, &VE, &AL, &DL, &CS, &SO, &SE, &US, &UE, &CM,
131 &CL, &CE, &HO, &UP, &BC, &LL, &SF, &SR, &VB, &KS,
132 &KE, &TI, &TE, &lPC, &BL, &NL, &DO, &M_AL, &M_DL, &M_SF,
133 &M_SR,
134 # ifdef ID_CHAR
135 &IC, &IM, &DC, &EI, &IP, &M_IC, &M_DC,
136 # endif
137 NULL
138 };
139
140 struct CapLen {
141 const char **cap_var;
142 int *len_var;
143 };
144
145 private const struct CapLen CapLenTab[] = {
146 { &HO, &HOlen },
147 { &LL, &LLlen },
148 { &UP, &UPlen },
149 # ifdef ID_CHAR
150 { &IC, &IClen },
151 { &DC, &DClen },
152 { &M_IC, &MIClen },
153 { &M_DC, &MDClen },
154 { &CE, &CElen },
155 # endif
156 { NULL, NULL }
157 };
158
159 private void
160 tcbad(const char *termname, const char *why)
161 {
162 writef("You can't run JOVE on a %s terminal: %s\n", termname, why);
163 flushscreen();
164 _exit(1);
165 }
166
167 void
168 getTERM(void)
169 {
170 char termnmbuf[13],
171 *termname = getenv("TERM"),
172 *termp = tspace,
173 tbuff[2048]; /* Good grief! */
174
175 if (termname == NULL || *termname == '\0'
176 || strcmp(termname, "dumb") == 0
177 || strcmp(termname, "unknown") == 0
178 || strcmp(termname, "network") == 0)
179 {
180 int len;
181 putstr("Enter terminal type (e.g, vt100): ");
182 flushscreen();
183 len = read(0, termnmbuf, sizeof termnmbuf);
184 termnmbuf[len > 1? len - 1 : 0] = '\0';
185 termname = termnmbuf;
186 }
187
188 if (tgetent(tbuff, termname) < 1)
189 tcbad(termname, "type unknown");
190
191 /* get numeric capabilities */
192
193 if ((CO = tgetnum("co")) == -1)
194 tcbad(termname, "co unknown (width)");
195
196 if (CO > MAXCOLS)
197 CO = MAXCOLS;
198
199 if ((LI = tgetnum("li")) == -1)
200 tcbad(termname, "li unknown (height)");
201
202 if ((phystab = tgetnum("it")) == -1 || phystab <= 0)
203 phystab = 8;
204
205 if ((SG = tgetnum("sg")) == -1)
206 SG = 0; /* Used for mode line only */
207
208 if ((UG = tgetnum("ug")) == -1)
209 UG = 0; /* We shan't bother if != 0 */
210
211 /* get string capabilities */
212 {
213 const char *tsp = ts;
214 const char **const *measp;
215
216 for (measp = meas; *measp != NULL; measp++) {
217 static char nm[3] = "xx";
218 char *val;
219
220 nm[0] = *tsp++;
221 nm[1] = *tsp++;
222 val = tgetstr(nm, &termp);
223 if (val != NULL)
224 **measp = val;
225 if (termp > tspace + sizeof(tspace))
226 tcbad(termname, "too many bytes of termcap strings");
227 }
228 }
229 if (lPC)
230 PC = *lPC; /* convert lPC string attribute to char PC */
231
232 /* get boolean capabilities */
233
234 Hazeltine = tgetflag("hz")==YES; /* Hazeltine tilde kludge */
235 NP = tgetflag("NP")==YES; /* there is No Pad character */
236 UL = tgetflag("ul")==YES; /* underscores don't replace chars already on screen */
237
238 /* adjust capabilities */
239
240 if (tgetflag("km") == YES
241 # if defined(USE_CTYPE) && !defined(NO_SETLOCALE)
242 && strcmp(LcCtype, "C") == 0
243 # endif
244 )
245 MetaKey = YES; /* has meta-key and default locale */
246
247 if (tgetflag("xs") == YES) {
248 SO = SE = NULL; /* don't use braindamaged standout mode */
249 SG = 0;
250 }
251
252 if (SR == NULL && M_SR == NULL)
253 CS = NULL; /* don't use scrolling region without way of reverse scrolling */
254
255 /* Note: the way termcap/terminfo is defined, we must use *both*
256 * IC and IM to insert, but normally only one will be defined.
257 * See terminfo(5), under the heading "Insert/Delete Character".
258 * Because of this, IM might be defined as a null string.
259 *
260 * The freely redistributable termcap/terminfo database associated
261 * with ncurses breaks this rule. This is unfortunate, but we
262 * cannot seem to get them to fix this bug. Apparently no currently
263 * supported terminal needed this feature anyway, so we might
264 * as well go with the flow.
265 */
266 # ifdef ID_CHAR
267 if (IM == NULL || *IM == '\0' || EI == NULL || *EI == '\0')
268 IM = EI = NULL; /* If IM or EI is empty, supress both. */
269
270 # ifndef NCURSES_BUG
271 if (IC == NULL || *IC == '\0')
272 M_IC = NULL; /* don't know how to use this */
273 # endif
274
275 UseIC = (IC != NULL || IM != NULL || M_IC != NULL);
276 MI = tgetflag("mi")==YES; /* okay to move while in insert mode */
277 # endif /* ID_CHAR */
278
279 /* strip stupid padding information */
280 while ('0' <= *NL && *NL <= '9')
281 NL += 1;
282 if (*NL == '*')
283 NL += 1;
284
285 if (BC == NULL)
286 BC = "\b"; /* default back space to BS */
287
288 CanScroll = (AL != NULL && DL != NULL) || CS != NULL;
289
290 /* calculate lengths */
291 {
292 static const struct CapLen *p;
293
294 for (p = CapLenTab; p->cap_var != NULL; p++)
295 if (*p->cap_var != NULL)
296 *p->len_var = strlen(*p->cap_var);
297 #ifdef ID_CHAR
298 if (IM != NULL)
299 IMEIlen = strlen(IM) + strlen(EI);
300 #endif
301 }
302 if (!(CM != NULL || HO != NULL))
303 tcbad(termname, "JOVE needs either cm or ho termcap/terminfo capability");
304 }
305
306 /* Put multi-unit or multiple single-unit strings, as appropriate. */
307
308 private void
309 tputc(char c)
310 {
311 scr_putchar(c);
312 }
313
314 void
315 putmulti(const char *ss, const char *ms, int num, int lines)
316 {
317 if (ms && (num > 1 || !ss)) {
318 /* use the multi string */
319 tputs(targ1(ms, num), lines, tputc);
320 } else {
321 /* repeatedly use single string */
322 while (num--)
323 putpad(ss, lines);
324 }
325 }
326
327 /* put a string with padding */
328
329 void
330 putpad(const char *str, int lines)
331 {
332 if (str != NULL)
333 tputs(str, lines, tputc);
334 }
335
336 void
337 dobell(int n) /* declared in term.h */
338 {
339 while (--n >= 0) {
340 if (VisBell && VB)
341 putpad(VB, 1);
342 else
343 putpad(BL, 1);
344 }
345 flushscreen();
346 }
347
348 void
349 clr_page(void)
350 {
351 putpad(CL, LI);
352 }
353
354 #endif /* TERMCAP */