1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
7 *
8 * %sccs.include.redist.c%
9 */
10
11 #if !defined(lint) && !defined(SCCSID)
12 static char sccsid[] = "@(#)term.c 8.2 (Berkeley) 04/30/95";
13 #endif /* not lint && not SCCSID */
14
15 /*
16 * term.c: Editor/termcap-curses interface
17 * We have to declare a static variable here, since the
18 * termcap putchar routine does not take an argument!
19 */
20 #include "sys.h"
21 #include <stdio.h>
22 #include <signal.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include "termcap.h" /* XXX: should be <termcap.h> */
27 #include <sys/types.h>
28
29 #include "el.h"
30
31 /*
32 * IMPORTANT NOTE: these routines are allowed to look at the current screen
33 * and the current possition assuming that it is correct. If this is not
34 * true, then the update will be WRONG! This is (should be) a valid
35 * assumption...
36 */
37
38 #define TC_BUFSIZE 2048
39
40 #define GoodStr(a) (el->el_term.t_str[a] != NULL && \
41 el->el_term.t_str[a][0] != '\0')
42 #define Str(a) el->el_term.t_str[a]
43 #define Val(a) el->el_term.t_val[a]
44
45 private struct {
46 char *b_name;
47 int b_rate;
48 } baud_rate[] = {
49 #ifdef B0
50 { "0", B0 },
51 #endif
52 #ifdef B50
53 { "50", B50 },
54 #endif
55 #ifdef B75
56 { "75", B75 },
57 #endif
58 #ifdef B110
59 { "110", B110 },
60 #endif
61 #ifdef B134
62 { "134", B134 },
63 #endif
64 #ifdef B150
65 { "150", B150 },
66 #endif
67 #ifdef B200
68 { "200", B200 },
69 #endif
70 #ifdef B300
71 { "300", B300 },
72 #endif
73 #ifdef B600
74 { "600", B600 },
75 #endif
76 #ifdef B900
77 { "900", B900 },
78 #endif
79 #ifdef B1200
80 { "1200", B1200 },
81 #endif
82 #ifdef B1800
83 { "1800", B1800 },
84 #endif
85 #ifdef B2400
86 { "2400", B2400 },
87 #endif
88 #ifdef B3600
89 { "3600", B3600 },
90 #endif
91 #ifdef B4800
92 { "4800", B4800 },
93 #endif
94 #ifdef B7200
95 { "7200", B7200 },
96 #endif
97 #ifdef B9600
98 { "9600", B9600 },
99 #endif
100 #ifdef EXTA
101 { "19200", EXTA },
102 #endif
103 #ifdef B19200
104 { "19200", B19200 },
105 #endif
106 #ifdef EXTB
107 { "38400", EXTB },
108 #endif
109 #ifdef B38400
110 { "38400", B38400 },
111 #endif
112 { NULL, 0 }
113 };
114
115 private struct termcapstr {
116 char *name;
117 char *long_name;
118 } tstr[] = {
119
120 #define T_al 0
121 { "al", "add new blank line" },
122 #define T_bl 1
123 { "bl", "audible bell" },
124 #define T_cd 2
125 { "cd", "clear to bottom" },
126 #define T_ce 3
127 { "ce", "clear to end of line" },
128 #define T_ch 4
129 { "ch", "cursor to horiz pos" },
130 #define T_cl 5
131 { "cl", "clear screen" },
132 #define T_dc 6
133 { "dc", "delete a character" },
134 #define T_dl 7
135 { "dl", "delete a line" },
136 #define T_dm 8
137 { "dm", "start delete mode" },
138 #define T_ed 9
139 { "ed", "end delete mode" },
140 #define T_ei 10
141 { "ei", "end insert mode" },
142 #define T_fs 11
143 { "fs", "cursor from status line" },
144 #define T_ho 12
145 { "ho", "home cursor" },
146 #define T_ic 13
147 { "ic", "insert character" },
148 #define T_im 14
149 { "im", "start insert mode" },
150 #define T_ip 15
151 { "ip", "insert padding" },
152 #define T_kd 16
153 { "kd", "sends cursor down" },
154 #define T_kl 17
155 { "kl", "sends cursor left" },
156 #define T_kr 18
157 { "kr", "sends cursor right" },
158 #define T_ku 19
159 { "ku", "sends cursor up" },
160 #define T_md 20
161 { "md", "begin bold" },
162 #define T_me 21
163 { "me", "end attributes" },
164 #define T_nd 22
165 { "nd", "non destructive space" },
166 #define T_se 23
167 { "se", "end standout" },
168 #define T_so 24
169 { "so", "begin standout" },
170 #define T_ts 25
171 { "ts", "cursor to status line" },
172 #define T_up 26
173 { "up", "cursor up one" },
174 #define T_us 27
175 { "us", "begin underline" },
176 #define T_ue 28
177 { "ue", "end underline" },
178 #define T_vb 29
179 { "vb", "visible bell" },
180 #define T_DC 30
181 { "DC", "delete multiple chars" },
182 #define T_DO 31
183 { "DO", "cursor down multiple" },
184 #define T_IC 32
185 { "IC", "insert multiple chars" },
186 #define T_LE 33
187 { "LE", "cursor left multiple" },
188 #define T_RI 34
189 { "RI", "cursor right multiple" },
190 #define T_UP 35
191 { "UP", "cursor up multiple" },
192 #define T_str 36
193 { NULL, NULL }
194 };
195
196 private struct termcapval {
197 char *name;
198 char *long_name;
199 } tval[] = {
200 #define T_pt 0
201 { "pt", "has physical tabs" },
202 #define T_li 1
203 { "li", "Number of lines" },
204 #define T_co 2
205 { "co", "Number of columns" },
206 #define T_km 3
207 { "km", "Has meta key" },
208 #define T_xt 4
209 { "xt", "Tab chars destructive" },
210 #define T_MT 5
211 { "MT", "Has meta key" }, /* XXX? */
212 #define T_val 6
213 { NULL, NULL, }
214 };
215
216 /* do two or more of the attributes use me */
217
218 private void term_rebuffer_display __P((EditLine *));
219 private void term_free_display __P((EditLine *));
220 private void term_alloc_display __P((EditLine *));
221 private void term_alloc __P((EditLine *,
222 struct termcapstr *, char *));
223 private void term_init_arrow __P((EditLine *));
224 private void term_reset_arrow __P((EditLine *));
225
226
227 private FILE *term_outfile = NULL; /* XXX: How do we fix that? */
228
229
230 /* term_setflags():
231 * Set the terminal capability flags
232 */
233 private void
term_setflags(el)234 term_setflags(el)
235 EditLine *el;
236 {
237 EL_FLAGS = 0;
238 if (el->el_tty.t_tabs)
239 EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
240
241 EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
242 EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
243 EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
244 EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
245 TERM_CAN_INSERT : 0;
246 EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
247
248 if (GoodStr(T_me) && GoodStr(T_ue))
249 EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ? TERM_CAN_ME : 0;
250 else
251 EL_FLAGS &= ~TERM_CAN_ME;
252 if (GoodStr(T_me) && GoodStr(T_se))
253 EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ? TERM_CAN_ME : 0;
254
255
256 #ifdef DEBUG_SCREEN
257 if (!EL_CAN_UP) {
258 (void) fprintf(el->el_errfile, "WARNING: Your terminal cannot move up.\n");
259 (void) fprintf(el->el_errfile, "Editing may be odd for long lines.\n");
260 }
261 if (!EL_CAN_CEOL)
262 (void) fprintf(el->el_errfile, "no clear EOL capability.\n");
263 if (!EL_CAN_DELETE)
264 (void) fprintf(el->el_errfile, "no delete char capability.\n");
265 if (!EL_CAN_INSERT)
266 (void) fprintf(el->el_errfile, "no insert char capability.\n");
267 #endif /* DEBUG_SCREEN */
268 }
269
270
271 /* term_init():
272 * Initialize the terminal stuff
273 */
274 protected int
term_init(el)275 term_init(el)
276 EditLine *el;
277 {
278 el->el_term.t_buf = (char *) el_malloc(TC_BUFSIZE);
279 el->el_term.t_cap = (char *) el_malloc(TC_BUFSIZE);
280 el->el_term.t_fkey = (fkey_t *) el_malloc(4 * sizeof(fkey_t));
281 el->el_term.t_loc = 0;
282 el->el_term.t_str = (char **) el_malloc(T_str * sizeof(char*));
283 (void) memset(el->el_term.t_str, 0, T_str * sizeof(char*));
284 el->el_term.t_val = (int *) el_malloc(T_val * sizeof(int));
285 (void) memset(el->el_term.t_val, 0, T_val * sizeof(char*));
286 term_outfile = el->el_outfile;
287 (void) term_set(el, NULL);
288 term_init_arrow(el);
289 return 0;
290 }
291
292 /* term_end():
293 * Clean up the terminal stuff
294 */
295 protected void
term_end(el)296 term_end(el)
297 EditLine *el;
298 {
299 el_free((ptr_t) el->el_term.t_buf);
300 el->el_term.t_buf = NULL;
301 el_free((ptr_t) el->el_term.t_cap);
302 el->el_term.t_cap = NULL;
303 el->el_term.t_loc = 0;
304 el_free((ptr_t) el->el_term.t_str);
305 el->el_term.t_str = NULL;
306 el_free((ptr_t) el->el_term.t_val);
307 el->el_term.t_val = NULL;
308 term_free_display(el);
309 }
310
311
312 /* term_alloc():
313 * Maintain a string pool for termcap strings
314 */
315 private void
term_alloc(el,t,cap)316 term_alloc(el, t, cap)
317 EditLine *el;
318 struct termcapstr *t;
319 char *cap;
320 {
321 char termbuf[TC_BUFSIZE];
322 int tlen, clen;
323 char **tlist = el->el_term.t_str;
324 char **tmp, **str = &tlist[t - tstr];
325
326 if (cap == NULL || *cap == '\0') {
327 *str = NULL;
328 return;
329 }
330 else
331 clen = strlen(cap);
332
333 tlen = *str == NULL ? 0 : strlen(*str);
334
335 /*
336 * New string is shorter; no need to allocate space
337 */
338 if (clen <= tlen) {
339 (void) strcpy(*str, cap);
340 return;
341 }
342
343 /*
344 * New string is longer; see if we have enough space to append
345 */
346 if (el->el_term.t_loc + 3 < TC_BUFSIZE) {
347 (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap);
348 el->el_term.t_loc += clen + 1; /* one for \0 */
349 return;
350 }
351
352 /*
353 * Compact our buffer; no need to check compaction, cause we know it
354 * fits...
355 */
356 tlen = 0;
357 for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
358 if (*tmp != NULL && *tmp != '\0' && *tmp != *str) {
359 char *ptr;
360
361 for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
362 continue;
363 termbuf[tlen++] = '\0';
364 }
365 memcpy(el->el_term.t_buf, termbuf, TC_BUFSIZE);
366 el->el_term.t_loc = tlen;
367 if (el->el_term.t_loc + 3 >= TC_BUFSIZE) {
368 (void) fprintf(el->el_errfile, "Out of termcap string space.\n");
369 return;
370 }
371 (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap);
372 el->el_term.t_loc += clen + 1; /* one for \0 */
373 return;
374 } /* end term_alloc */
375
376
377 /* term_rebuffer_display():
378 * Rebuffer the display after the screen changed size
379 */
380 private void
term_rebuffer_display(el)381 term_rebuffer_display(el)
382 EditLine *el;
383 {
384 coord_t *c = &el->el_term.t_size;
385
386 term_free_display(el);
387
388 /* make this public, -1 to avoid wraps */
389 c->h = Val(T_co) - 1;
390 c->v = (EL_BUFSIZ * 4) / c->h + 1;
391
392 term_alloc_display(el);
393 } /* end term_rebuffer_display */
394
395
396 /* term_alloc_display():
397 * Allocate a new display.
398 */
399 private void
term_alloc_display(el)400 term_alloc_display(el)
401 EditLine *el;
402 {
403 int i;
404 char **b;
405 coord_t *c = &el->el_term.t_size;
406
407 b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
408 for (i = 0; i < c->v; i++)
409 b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
410 b[c->v] = NULL;
411 el->el_display = b;
412
413 b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
414 for (i = 0; i < c->v; i++)
415 b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
416 b[c->v] = NULL;
417 el->el_vdisplay = b;
418
419 } /* end term_alloc_display */
420
421
422 /* term_free_display():
423 * Free the display buffers
424 */
425 private void
term_free_display(el)426 term_free_display(el)
427 EditLine *el;
428 {
429 char **b;
430 char **bufp;
431
432 b = el->el_display;
433 el->el_display = NULL;
434 if (b != NULL) {
435 for (bufp = b; *bufp != NULL; bufp++)
436 el_free((ptr_t) *bufp);
437 el_free((ptr_t) b);
438 }
439 b = el->el_vdisplay;
440 el->el_vdisplay = NULL;
441 if (b != NULL) {
442 for (bufp = b; *bufp != NULL; bufp++)
443 el_free((ptr_t) * bufp);
444 el_free((ptr_t) b);
445 }
446 } /* end term_free_display */
447
448
449 /* term_move_to_line():
450 * move to line <where> (first line == 0)
451 * as efficiently as possible
452 */
453 protected void
term_move_to_line(el,where)454 term_move_to_line(el, where)
455 EditLine *el;
456 int where;
457 {
458 int del, i;
459
460 if (where == el->el_cursor.v)
461 return;
462
463 if (where > el->el_term.t_size.v) {
464 #ifdef DEBUG_SCREEN
465 (void) fprintf(el->el_errfile,
466 "term_move_to_line: where is ridiculous: %d\r\n", where);
467 #endif /* DEBUG_SCREEN */
468 return;
469 }
470
471 if ((del = where - el->el_cursor.v) > 0) {
472 if ((del > 1) && GoodStr(T_DO))
473 (void) tputs(tgoto(Str(T_DO), del, del), del, term__putc);
474 else {
475 for (i = 0; i < del; i++)
476 term__putc('\n');
477 el->el_cursor.h = 0; /* because the \n will become \r\n */
478 }
479 }
480 else { /* del < 0 */
481 if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
482 (void) tputs(tgoto(Str(T_UP), -del, -del), -del, term__putc);
483 else {
484 if (GoodStr(T_up))
485 for (i = 0; i < -del; i++)
486 (void) tputs(Str(T_up), 1, term__putc);
487 }
488 }
489 el->el_cursor.v = where; /* now where is here */
490 } /* end term_move_to_line */
491
492
493 /* term_move_to_char():
494 * Move to the character position specified
495 */
496 protected void
term_move_to_char(el,where)497 term_move_to_char(el, where)
498 EditLine *el;
499 int where;
500 {
501 int del, i;
502
503 mc_again:
504 if (where == el->el_cursor.h)
505 return;
506
507 if (where > (el->el_term.t_size.h + 1)) {
508 #ifdef DEBUG_SCREEN
509 (void) fprintf(el->el_errfile,
510 "term_move_to_char: where is riduculous: %d\r\n", where);
511 #endif /* DEBUG_SCREEN */
512 return;
513 }
514
515 if (!where) { /* if where is first column */
516 term__putc('\r'); /* do a CR */
517 el->el_cursor.h = 0;
518 return;
519 }
520
521 del = where - el->el_cursor.h;
522
523 if ((del < -4 || del > 4) && GoodStr(T_ch))
524 /* go there directly */
525 (void) tputs(tgoto(Str(T_ch), where, where), where, term__putc);
526 else {
527 if (del > 0) { /* moving forward */
528 if ((del > 4) && GoodStr(T_RI))
529 (void) tputs(tgoto(Str(T_RI), del, del), del, term__putc);
530 else {
531 if (EL_CAN_TAB) { /* if I can do tabs, use them */
532 if ((el->el_cursor.h & 0370) != (where & 0370)) {
533 /* if not within tab stop */
534 for (i = (el->el_cursor.h & 0370);
535 i < (where & 0370); i += 8)
536 term__putc('\t'); /* then tab over */
537 el->el_cursor.h = where & 0370;
538 }
539 }
540 /* it's usually cheaper to just write the chars, so we do. */
541
542 /* NOTE THAT term_overwrite() WILL CHANGE el->el_cursor.h!!! */
543 term_overwrite(el,
544 &el->el_display[el->el_cursor.v][el->el_cursor.h],
545 where - el->el_cursor.h);
546
547 }
548 }
549 else { /* del < 0 := moving backward */
550 if ((-del > 4) && GoodStr(T_LE))
551 (void) tputs(tgoto(Str(T_LE), -del, -del), -del, term__putc);
552 else { /* can't go directly there */
553 /* if the "cost" is greater than the "cost" from col 0 */
554 if (EL_CAN_TAB ? (-del > ((where >> 3) + (where & 07)))
555 : (-del > where)) {
556 term__putc('\r'); /* do a CR */
557 el->el_cursor.h = 0;
558 goto mc_again; /* and try again */
559 }
560 for (i = 0; i < -del; i++)
561 term__putc('\b');
562 }
563 }
564 }
565 el->el_cursor.h = where; /* now where is here */
566 } /* end term_move_to_char */
567
568
569 /* term_overwrite():
570 * Overstrike num characters
571 */
572 protected void
term_overwrite(el,cp,n)573 term_overwrite(el, cp, n)
574 EditLine *el;
575 char *cp;
576 int n;
577 {
578 if (n <= 0)
579 return; /* catch bugs */
580
581 if (n > (el->el_term.t_size.h + 1)) {
582 #ifdef DEBUG_SCREEN
583 (void) fprintf(el->el_errfile, "term_overwrite: n is riduculous: %d\r\n", n);
584 #endif /* DEBUG_SCREEN */
585 return;
586 }
587
588 do {
589 term__putc(*cp++);
590 el->el_cursor.h++;
591 } while (--n);
592 } /* end term_overwrite */
593
594
595 /* term_deletechars():
596 * Delete num characters
597 */
598 protected void
term_deletechars(el,num)599 term_deletechars(el, num)
600 EditLine *el;
601 int num;
602 {
603 if (num <= 0)
604 return;
605
606 if (!EL_CAN_DELETE) {
607 #ifdef DEBUG_EDIT
608 (void) fprintf(el->el_errfile, " ERROR: cannot delete \n");
609 #endif /* DEBUG_EDIT */
610 return;
611 }
612
613 if (num > el->el_term.t_size.h) {
614 #ifdef DEBUG_SCREEN
615 (void) fprintf(el->el_errfile,
616 "term_deletechars: num is riduculous: %d\r\n", num);
617 #endif /* DEBUG_SCREEN */
618 return;
619 }
620
621 if (GoodStr(T_DC)) /* if I have multiple delete */
622 if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more expen. */
623 (void) tputs(tgoto(Str(T_DC), num, num), num, term__putc);
624 return;
625 }
626
627 if (GoodStr(T_dm)) /* if I have delete mode */
628 (void) tputs(Str(T_dm), 1, term__putc);
629
630 if (GoodStr(T_dc)) /* else do one at a time */
631 while (num--)
632 (void) tputs(Str(T_dc), 1, term__putc);
633
634 if (GoodStr(T_ed)) /* if I have delete mode */
635 (void) tputs(Str(T_ed), 1, term__putc);
636 } /* end term_deletechars */
637
638
639 /* term_insertwrite():
640 * Puts terminal in insert character mode or inserts num
641 * characters in the line
642 */
643 protected void
term_insertwrite(el,cp,num)644 term_insertwrite(el, cp, num)
645 EditLine *el;
646 char *cp;
647 int num;
648 {
649 if (num <= 0)
650 return;
651 if (!EL_CAN_INSERT) {
652 #ifdef DEBUG_EDIT
653 (void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
654 #endif /* DEBUG_EDIT */
655 return;
656 }
657
658 if (num > el->el_term.t_size.h) {
659 #ifdef DEBUG_SCREEN
660 (void) fprintf(el->el_errfile, "StartInsert: num is riduculous: %d\r\n", num);
661 #endif /* DEBUG_SCREEN */
662 return;
663 }
664
665 if (GoodStr(T_IC)) /* if I have multiple insert */
666 if ((num > 1) || !GoodStr(T_ic)) { /* if ic would be more expen. */
667 (void) tputs(tgoto(Str(T_IC), num, num), num, term__putc);
668 term_overwrite(el, cp, num); /* this updates el_cursor.h */
669 return;
670 }
671
672 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
673 (void) tputs(Str(T_im), 1, term__putc);
674
675 el->el_cursor.h += num;
676 do
677 term__putc(*cp++);
678 while (--num);
679
680 if (GoodStr(T_ip)) /* have to make num chars insert */
681 (void) tputs(Str(T_ip), 1, term__putc);
682
683 (void) tputs(Str(T_ei), 1, term__putc);
684 return;
685 }
686
687 do {
688 if (GoodStr(T_ic)) /* have to make num chars insert */
689 (void) tputs(Str(T_ic), 1, term__putc); /* insert a char */
690
691 term__putc(*cp++);
692
693 el->el_cursor.h++;
694
695 if (GoodStr(T_ip)) /* have to make num chars insert */
696 (void) tputs(Str(T_ip), 1, term__putc);/* pad the inserted char */
697
698 } while (--num);
699 } /* end term_insertwrite */
700
701
702 /* term_clear_EOL():
703 * clear to end of line. There are num characters to clear
704 */
705 protected void
term_clear_EOL(el,num)706 term_clear_EOL(el, num)
707 EditLine *el;
708 int num;
709 {
710 int i;
711
712 if (EL_CAN_CEOL && GoodStr(T_ce))
713 (void) tputs(Str(T_ce), 1, term__putc);
714 else {
715 for (i = 0; i < num; i++)
716 term__putc(' ');
717 el->el_cursor.h += num; /* have written num spaces */
718 }
719 } /* end term_clear_EOL */
720
721
722 /* term_clear_screen():
723 * Clear the screen
724 */
725 protected void
term_clear_screen(el)726 term_clear_screen(el)
727 EditLine *el;
728 { /* clear the whole screen and home */
729 if (GoodStr(T_cl))
730 /* send the clear screen code */
731 (void) tputs(Str(T_cl), Val(T_li), term__putc);
732 else if (GoodStr(T_ho) && GoodStr(T_cd)) {
733 (void) tputs(Str(T_ho), Val(T_li), term__putc); /* home */
734 /* clear to bottom of screen */
735 (void) tputs(Str(T_cd), Val(T_li), term__putc);
736 }
737 else {
738 term__putc('\r');
739 term__putc('\n');
740 }
741 } /* end term_clear_screen */
742
743
744 /* term_beep():
745 * Beep the way the terminal wants us
746 */
747 protected void
term_beep(el)748 term_beep(el)
749 EditLine *el;
750 {
751 if (GoodStr(T_vb))
752 (void) tputs(Str(T_vb), 1, term__putc); /* visible bell */
753 else if (GoodStr(T_bl))
754 /* what termcap says we should use */
755 (void) tputs(Str(T_bl), 1, term__putc);
756 else
757 term__putc('\007'); /* an ASCII bell; ^G */
758 } /* end term_beep */
759
760
761 #ifdef notdef
762 /* term_clear_to_bottom():
763 * Clear to the bottom of the screen
764 */
765 protected void
term_clear_to_bottom(el)766 term_clear_to_bottom(el)
767 EditLine *el;
768 {
769 if (GoodStr(T_cd))
770 (void) tputs(Str(T_cd), Val(T_li), term__putc);
771 else if (GoodStr(T_ce))
772 (void) tputs(Str(T_ce), Val(T_li), term__putc);
773 } /* end term_clear_to_bottom */
774 #endif
775
776
777 /* term_set():
778 * Read in the terminal capabilities from the requested terminal
779 */
780 protected int
term_set(el,term)781 term_set(el, term)
782 EditLine *el;
783 char *term;
784 {
785 int i;
786 char buf[TC_BUFSIZE];
787 char *area;
788 struct termcapstr *t;
789 sigset_t oset, nset;
790 int lins, cols;
791
792 (void) sigemptyset(&nset);
793 (void) sigaddset(&nset, SIGWINCH);
794 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
795
796 area = buf;
797
798
799 if (term == NULL)
800 term = getenv("TERM");
801
802 if (!term || !term[0])
803 term = "dumb";
804
805 memset(el->el_term.t_cap, 0, TC_BUFSIZE);
806
807 i = tgetent(el->el_term.t_cap, term);
808
809 if (i <= 0) {
810 if (i == -1)
811 (void) fprintf(el->el_errfile, "Cannot open /etc/termcap.\n");
812 else if (i == 0)
813 (void) fprintf(el->el_errfile,
814 "No entry for terminal type \"%s\"\n", term);
815 (void) fprintf(el->el_errfile, "using dumb terminal settings.\n");
816 Val(T_co) = 80; /* do a dumb terminal */
817 Val(T_pt) = Val(T_km) = Val(T_li) = 0;
818 Val(T_xt) = Val(T_MT);
819 for (t = tstr; t->name != NULL; t++)
820 term_alloc(el, t, NULL);
821 }
822 else {
823 /* Can we tab */
824 Val(T_pt) = tgetflag("pt");
825 Val(T_xt) = tgetflag("xt");
826 /* do we have a meta? */
827 Val(T_km) = tgetflag("km");
828 Val(T_MT) = tgetflag("MT");
829 /* Get the size */
830 Val(T_co) = tgetnum("co");
831 Val(T_li) = tgetnum("li");
832 for (t = tstr; t->name != NULL; t++)
833 term_alloc(el, t, tgetstr(t->name, &area));
834 }
835
836 if (Val(T_co) < 2)
837 Val(T_co) = 80; /* just in case */
838 if (Val(T_li) < 1)
839 Val(T_li) = 24;
840
841 el->el_term.t_size.v = Val(T_co);
842 el->el_term.t_size.h = Val(T_li);
843
844 term_setflags(el);
845
846 (void) term_get_size(el, &lins, &cols);/* get the correct window size */
847 term_change_size(el, lins, cols);
848 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
849 term_bind_arrow(el);
850 return 0;
851 } /* end term_set */
852
853
854 /* term_get_size():
855 * Return the new window size in lines and cols, and
856 * true if the size was changed.
857 */
858 protected int
term_get_size(el,lins,cols)859 term_get_size(el, lins, cols)
860 EditLine *el;
861 int *lins, *cols;
862 {
863
864 *cols = Val(T_co);
865 *lins = Val(T_li);
866
867 #ifdef TIOCGWINSZ
868 {
869 struct winsize ws;
870 if (ioctl(el->el_infd, TIOCGWINSZ, (ioctl_t) &ws) != -1) {
871 if (ws.ws_col)
872 *cols = ws.ws_col;
873 if (ws.ws_row)
874 *lins = ws.ws_row;
875 }
876 }
877 #endif
878 #ifdef TIOCGSIZE
879 {
880 struct ttysize ts;
881 if (ioctl(el->el_infd, TIOCGSIZE, (ioctl_t) &ts) != -1) {
882 if (ts.ts_cols)
883 *cols = ts.ts_cols;
884 if (ts.ts_lines)
885 *lins = ts.ts_lines;
886 }
887 }
888 #endif
889 return (Val(T_co) != *cols || Val(T_li) != *lins);
890 } /* end term_get_size */
891
892
893 /* term_change_size():
894 * Change the size of the terminal
895 */
896 protected void
term_change_size(el,lins,cols)897 term_change_size(el, lins, cols)
898 EditLine *el;
899 int lins, cols;
900 {
901 /*
902 * Just in case
903 */
904 Val(T_co) = (cols < 2) ? 80 : cols;
905 Val(T_li) = (lins < 1) ? 24 : lins;
906
907 term_rebuffer_display(el); /* re-make display buffers */
908 re_clear_display(el);
909 } /* end term_change_size */
910
911
912 /* term_init_arrow():
913 * Initialize the arrow key bindings from termcap
914 */
915 private void
term_init_arrow(el)916 term_init_arrow(el)
917 EditLine *el;
918 {
919 fkey_t *arrow = el->el_term.t_fkey;
920
921 arrow[A_K_DN].name = "down";
922 arrow[A_K_DN].key = T_kd;
923 arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
924 arrow[A_K_DN].type = XK_CMD;
925
926 arrow[A_K_UP].name = "up";
927 arrow[A_K_UP].key = T_ku;
928 arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
929 arrow[A_K_UP].type = XK_CMD;
930
931 arrow[A_K_LT].name = "left";
932 arrow[A_K_LT].key = T_kl;
933 arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
934 arrow[A_K_LT].type = XK_CMD;
935
936 arrow[A_K_RT].name = "right";
937 arrow[A_K_RT].key = T_kr;
938 arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
939 arrow[A_K_RT].type = XK_CMD;
940
941 }
942
943
944 /* term_reset_arrow():
945 * Reset arrow key bindings
946 */
947 private void
term_reset_arrow(el)948 term_reset_arrow(el)
949 EditLine *el;
950 {
951 fkey_t *arrow = el->el_term.t_fkey;
952 static char strA[] = {033, '[', 'A', '\0'};
953 static char strB[] = {033, '[', 'B', '\0'};
954 static char strC[] = {033, '[', 'C', '\0'};
955 static char strD[] = {033, '[', 'D', '\0'};
956 static char stOA[] = {033, 'O', 'A', '\0'};
957 static char stOB[] = {033, 'O', 'B', '\0'};
958 static char stOC[] = {033, 'O', 'C', '\0'};
959 static char stOD[] = {033, 'O', 'D', '\0'};
960
961 key_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
962 key_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
963 key_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
964 key_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
965 key_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
966 key_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
967 key_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
968 key_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
969
970 if (el->el_map.type == MAP_VI) {
971 key_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
972 key_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
973 key_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
974 key_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
975 key_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
976 key_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
977 key_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
978 key_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
979 }
980 }
981
982
983 /* term_set_arrow():
984 * Set an arrow key binding
985 */
986 protected int
term_set_arrow(el,name,fun,type)987 term_set_arrow(el, name, fun, type)
988 EditLine *el;
989 char *name;
990 key_value_t *fun;
991 int type;
992 {
993 fkey_t *arrow = el->el_term.t_fkey;
994 int i;
995
996 for (i = 0; i < A_K_NKEYS; i++)
997 if (strcmp(name, arrow[i].name) == 0) {
998 arrow[i].fun = *fun;
999 arrow[i].type = type;
1000 return 0;
1001 }
1002 return -1;
1003 }
1004
1005
1006 /* term_clear_arrow():
1007 * Clear an arrow key binding
1008 */
1009 protected int
term_clear_arrow(el,name)1010 term_clear_arrow(el, name)
1011 EditLine *el;
1012 char *name;
1013 {
1014 fkey_t *arrow = el->el_term.t_fkey;
1015 int i;
1016
1017 for (i = 0; i < A_K_NKEYS; i++)
1018 if (strcmp(name, arrow[i].name) == 0) {
1019 arrow[i].type = XK_NOD;
1020 return 0;
1021 }
1022 return -1;
1023 }
1024
1025
1026 /* term_print_arrow():
1027 * Print the arrow key bindings
1028 */
1029 protected void
term_print_arrow(el,name)1030 term_print_arrow(el, name)
1031 EditLine *el;
1032 char *name;
1033 {
1034 int i;
1035 fkey_t *arrow = el->el_term.t_fkey;
1036
1037 for (i = 0; i < A_K_NKEYS; i++)
1038 if (*name == '\0' || strcmp(name, arrow[i].name) == 0)
1039 if (arrow[i].type != XK_NOD)
1040 key_kprint(el, arrow[i].name, &arrow[i].fun, arrow[i].type);
1041 }
1042
1043
1044 /* term_bind_arrow():
1045 * Bind the arrow keys
1046 */
1047 protected void
term_bind_arrow(el)1048 term_bind_arrow(el)
1049 EditLine *el;
1050 {
1051 el_action_t *map, *dmap;
1052 int i, j;
1053 char *p;
1054 fkey_t *arrow = el->el_term.t_fkey;
1055
1056 /* Check if the components needed are initialized */
1057 if (el->el_term.t_buf == NULL || el->el_map.key == NULL)
1058 return;
1059
1060 map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
1061 dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
1062
1063 term_reset_arrow(el);
1064
1065 for (i = 0; i < 4; i++) {
1066 p = el->el_term.t_str[arrow[i].key];
1067 if (p && *p) {
1068 j = (unsigned char) *p;
1069 /*
1070 * Assign the arrow keys only if:
1071 *
1072 * 1. They are multi-character arrow keys and the user
1073 * has not re-assigned the leading character, or
1074 * has re-assigned the leading character to be
1075 * ED_SEQUENCE_LEAD_IN
1076 * 2. They are single arrow keys pointing to an unassigned key.
1077 */
1078 if (arrow[i].type == XK_NOD)
1079 key_clear(el, map, p);
1080 else {
1081 if (p[1] && (dmap[j] == map[j] ||
1082 map[j] == ED_SEQUENCE_LEAD_IN)) {
1083 key_add(el, p, &arrow[i].fun, arrow[i].type);
1084 map[j] = ED_SEQUENCE_LEAD_IN;
1085 }
1086 else if (map[j] == ED_UNASSIGNED) {
1087 key_clear(el, map, p);
1088 if (arrow[i].type == XK_CMD)
1089 map[j] = arrow[i].fun.cmd;
1090 else
1091 key_add(el, p, &arrow[i].fun, arrow[i].type);
1092 }
1093 }
1094 }
1095 }
1096 }
1097
1098
1099 /* term__putc():
1100 * Add a character
1101 */
1102 protected void
term__putc(c)1103 term__putc(c)
1104 int c;
1105 {
1106 (void) fputc(c, term_outfile);
1107 } /* end term__putc */
1108
1109
1110 /* term__flush():
1111 * Flush output
1112 */
1113 protected void
term__flush()1114 term__flush()
1115 {
1116 (void) fflush(term_outfile);
1117 } /* end term__flush */
1118
1119
1120 /* term_telltc():
1121 * Print the current termcap characteristics
1122 */
1123 protected int
1124 /*ARGSUSED*/
term_telltc(el,argc,argv)1125 term_telltc(el, argc, argv)
1126 EditLine *el;
1127 int argc;
1128 char **argv;
1129 {
1130 struct termcapstr *t;
1131 char **ts;
1132 char upbuf[EL_BUFSIZ];
1133
1134 (void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
1135 (void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
1136 (void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
1137 Val(T_co), Val(T_li));
1138 (void) fprintf(el->el_outfile,
1139 "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
1140 (void) fprintf(el->el_outfile,
1141 "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
1142 #ifdef notyet
1143 (void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
1144 (T_Margin&MARGIN_AUTO)? "has": "does not have");
1145 if (T_Margin & MARGIN_AUTO)
1146 (void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
1147 (T_Margin&MARGIN_MAGIC)?"has":"does not have");
1148 #endif
1149
1150 for (t = tstr, ts = el->el_term.t_str; t->name != NULL; t++, ts++)
1151 (void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n", t->long_name,
1152 t->name, *ts && **ts ?
1153 key__decode_str(*ts, upbuf, "") : "(empty)");
1154 (void) fputc('\n', el->el_outfile);
1155 return 0;
1156 }
1157
1158
1159 /* term_settc():
1160 * Change the current terminal characteristics
1161 */
1162 protected int
1163 /*ARGSUSED*/
term_settc(el,argc,argv)1164 term_settc(el, argc, argv)
1165 EditLine *el;
1166 int argc;
1167 char **argv;
1168 {
1169 struct termcapstr *ts;
1170 struct termcapval *tv;
1171 char *what, *how;
1172
1173 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1174 return -1;
1175
1176 what = argv[1];
1177 how = argv[2];
1178
1179 /*
1180 * Do the strings first
1181 */
1182 for (ts = tstr; ts->name != NULL; ts++)
1183 if (strcmp(ts->name, what) == 0)
1184 break;
1185
1186 if (ts->name != NULL) {
1187 term_alloc(el, ts, how);
1188 term_setflags(el);
1189 return 0;
1190 }
1191
1192 /*
1193 * Do the numeric ones second
1194 */
1195 for (tv = tval; tv->name != NULL; tv++)
1196 if (strcmp(tv->name, what) == 0)
1197 break;
1198
1199 if (tv->name != NULL) {
1200 if (tv == &tval[T_pt] || tv == &tval[T_km]
1201 #ifdef notyet
1202 || tv == &tval[T_am] || tv == &tval[T_xn]
1203 #endif
1204 ) {
1205 if (strcmp(how, "yes") == 0)
1206 el->el_term.t_val[tv - tval] = 1;
1207 else if (strcmp(how, "no") == 0)
1208 el->el_term.t_val[tv - tval] = 0;
1209 else {
1210 (void) fprintf(el->el_errfile, "settc: Bad value `%s'.\n", how);
1211 return -1;
1212 }
1213 term_setflags(el);
1214 term_change_size(el, Val(T_li), Val(T_co));
1215 return 0;
1216 }
1217 else {
1218 el->el_term.t_val[tv - tval] = atoi(how);
1219 el->el_term.t_size.v = Val(T_co);
1220 el->el_term.t_size.h = Val(T_li);
1221 if (tv == &tval[T_co] || tv == &tval[T_li])
1222 term_change_size(el, Val(T_li), Val(T_co));
1223 return 0;
1224 }
1225 }
1226 return -1;
1227 }
1228
1229
1230 /* term_echotc():
1231 * Print the termcap string out with variable substitution
1232 */
1233 protected int
1234 /*ARGSUSED*/
term_echotc(el,argc,argv)1235 term_echotc(el, argc, argv)
1236 EditLine *el;
1237 int argc;
1238 char **argv;
1239 {
1240 char *cap, *scap;
1241 int arg_need, arg_cols, arg_rows;
1242 int verbose = 0, silent = 0;
1243 char *area;
1244 static char *fmts = "%s\n", *fmtd = "%d\n";
1245 struct termcapstr *t;
1246 char buf[TC_BUFSIZE];
1247
1248 area = buf;
1249
1250 if (argv == NULL || argv[1] == NULL)
1251 return -1;
1252 argv++;
1253
1254 if (argv[0][0] == '-') {
1255 switch (argv[0][1]) {
1256 case 'v':
1257 verbose = 1;
1258 break;
1259 case 's':
1260 silent = 1;
1261 break;
1262 default:
1263 /* stderror(ERR_NAME | ERR_TCUSAGE); */
1264 break;
1265 }
1266 argv++;
1267 }
1268 if (!*argv || *argv[0] == '\0')
1269 return 0;
1270 if (strcmp(*argv, "tabs") == 0) {
1271 (void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
1272 return 0;
1273 }
1274 else if (strcmp(*argv, "meta") == 0) {
1275 (void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
1276 return 0;
1277 }
1278 #ifdef notyet
1279 else if (strcmp(*argv, "xn") == 0) {
1280 (void) fprintf(el->el_outfile, fmts, T_Margin & MARGIN_MAGIC ?
1281 "yes" : "no");
1282 return 0;
1283 }
1284 else if (strcmp(*argv, "am") == 0) {
1285 (void) fprintf(el->el_outfile, fmts, T_Margin & MARGIN_AUTO ?
1286 "yes" : "no");
1287 return 0;
1288 }
1289 #endif
1290 else if (strcmp(*argv, "baud") == 0) {
1291 int i;
1292
1293 for (i = 0; baud_rate[i].b_name != NULL; i++)
1294 if (el->el_tty.t_speed == baud_rate[i].b_rate) {
1295 (void) fprintf(el->el_outfile, fmts, baud_rate[i].b_name);
1296 return 0;
1297 }
1298 (void) fprintf(el->el_outfile, fmtd, 0);
1299 return 0;
1300 }
1301 else if (strcmp(*argv, "rows") == 0 || strcmp(*argv, "lines") == 0) {
1302 (void) fprintf(el->el_outfile, fmtd, Val(T_li));
1303 return 0;
1304 }
1305 else if (strcmp(*argv, "cols") == 0) {
1306 (void) fprintf(el->el_outfile, fmtd, Val(T_co));
1307 return 0;
1308 }
1309
1310 /*
1311 * Try to use our local definition first
1312 */
1313 scap = NULL;
1314 for (t = tstr; t->name != NULL; t++)
1315 if (strcmp(t->name, *argv) == 0) {
1316 scap = el->el_term.t_str[t - tstr];
1317 break;
1318 }
1319 if (t->name == NULL)
1320 scap = tgetstr(*argv, &area);
1321 if (!scap || scap[0] == '\0') {
1322 if (!silent)
1323 (void) fprintf(el->el_errfile,
1324 "echotc: Termcap parameter `%s' not found.\n", *argv);
1325 return -1;
1326 }
1327
1328 /*
1329 * Count home many values we need for this capability.
1330 */
1331 for (cap = scap, arg_need = 0; *cap; cap++)
1332 if (*cap == '%')
1333 switch (*++cap) {
1334 case 'd':
1335 case '2':
1336 case '3':
1337 case '.':
1338 case '+':
1339 arg_need++;
1340 break;
1341 case '%':
1342 case '>':
1343 case 'i':
1344 case 'r':
1345 case 'n':
1346 case 'B':
1347 case 'D':
1348 break;
1349 default:
1350 /*
1351 * hpux has lot's of them...
1352 */
1353 if (verbose)
1354 (void) fprintf(el->el_errfile,
1355 "echotc: Warning: unknown termcap %% `%c'.\n", *cap);
1356 /* This is bad, but I won't complain */
1357 break;
1358 }
1359
1360 switch (arg_need) {
1361 case 0:
1362 argv++;
1363 if (*argv && *argv[0]) {
1364 if (!silent)
1365 (void) fprintf(el->el_errfile,
1366 "echotc: Warning: Extra argument `%s'.\n", *argv);
1367 return -1;
1368 }
1369 (void) tputs(scap, 1, term__putc);
1370 break;
1371 case 1:
1372 argv++;
1373 if (!*argv || *argv[0] == '\0') {
1374 if (!silent)
1375 (void) fprintf(el->el_errfile,
1376 "echotc: Warning: Missing argument.\n");
1377 return -1;
1378 }
1379 arg_cols = 0;
1380 arg_rows = atoi(*argv);
1381 argv++;
1382 if (*argv && *argv[0]) {
1383 if (!silent)
1384 (void) fprintf(el->el_errfile,
1385 "echotc: Warning: Extra argument `%s'.\n", *argv);
1386 return -1;
1387 }
1388 (void) tputs(tgoto(scap, arg_cols, arg_rows), 1, term__putc);
1389 break;
1390 default:
1391 /* This is wrong, but I will ignore it... */
1392 if (verbose)
1393 (void) fprintf(el->el_errfile,
1394 "echotc: Warning: Too many required arguments (%d).\n",
1395 arg_need);
1396 /*FALLTHROUGH*/
1397 case 2:
1398 argv++;
1399 if (!*argv || *argv[0] == '\0') {
1400 if (!silent)
1401 (void) fprintf(el->el_errfile,
1402 "echotc: Warning: Missing argument.\n");
1403 return -1;
1404 }
1405 arg_cols = atoi(*argv);
1406 argv++;
1407 if (!*argv || *argv[0] == '\0') {
1408 if (!silent)
1409 (void) fprintf(el->el_errfile,
1410 "echotc: Warning: Missing argument.\n");
1411 return -1;
1412 }
1413 arg_rows = atoi(*argv);
1414 argv++;
1415 if (*argv && *argv[0]) {
1416 if (!silent)
1417 (void) fprintf(el->el_errfile,
1418 "echotc: Warning: Extra argument `%s'.\n", *argv);
1419 return -1;
1420 }
1421 (void) tputs(tgoto(scap, arg_cols, arg_rows), arg_rows, term__putc);
1422 break;
1423 }
1424 return 0;
1425 }
1426