terminal.c revision 1.10 1 /* $NetBSD: terminal.c,v 1.10 2011/10/04 15:27:04 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include "config.h"
36 #if !defined(lint) && !defined(SCCSID)
37 #if 0
38 static char sccsid[] = "@(#)term.c 8.2 (Berkeley) 4/30/95";
39 #else
40 __RCSID("$NetBSD: terminal.c,v 1.10 2011/10/04 15:27:04 christos Exp $");
41 #endif
42 #endif /* not lint && not SCCSID */
43
44 /*
45 * terminal.c: Editor/termcap-curses interface
46 * We have to declare a static variable here, since the
47 * termcap putchar routine does not take an argument!
48 */
49 #include <stdio.h>
50 #include <signal.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include <limits.h>
55 #ifdef HAVE_TERMCAP_H
56 #include <termcap.h>
57 #endif
58 #ifdef HAVE_CURSES_H
59 #include <curses.h>
60 #elif HAVE_NCURSES_H
61 #include <ncurses.h>
62 #endif
63
64 /* Solaris's term.h does horrid things. */
65 #if defined(HAVE_TERM_H) && !defined(__sun)
66 #include <term.h>
67 #endif
68
69 #include <sys/types.h>
70 #include <sys/ioctl.h>
71
72 #ifdef _REENTRANT
73 #include <pthread.h>
74 #endif
75
76 #include "el.h"
77
78 /*
79 * IMPORTANT NOTE: these routines are allowed to look at the current screen
80 * and the current position assuming that it is correct. If this is not
81 * true, then the update will be WRONG! This is (should be) a valid
82 * assumption...
83 */
84
85 #define TC_BUFSIZE ((size_t)2048)
86
87 #define GoodStr(a) (el->el_terminal.t_str[a] != NULL && \
88 el->el_terminal.t_str[a][0] != '\0')
89 #define Str(a) el->el_terminal.t_str[a]
90 #define Val(a) el->el_terminal.t_val[a]
91
92 private const struct termcapstr {
93 const char *name;
94 const char *long_name;
95 } tstr[] = {
96 #define T_al 0
97 { "al", "add new blank line" },
98 #define T_bl 1
99 { "bl", "audible bell" },
100 #define T_cd 2
101 { "cd", "clear to bottom" },
102 #define T_ce 3
103 { "ce", "clear to end of line" },
104 #define T_ch 4
105 { "ch", "cursor to horiz pos" },
106 #define T_cl 5
107 { "cl", "clear screen" },
108 #define T_dc 6
109 { "dc", "delete a character" },
110 #define T_dl 7
111 { "dl", "delete a line" },
112 #define T_dm 8
113 { "dm", "start delete mode" },
114 #define T_ed 9
115 { "ed", "end delete mode" },
116 #define T_ei 10
117 { "ei", "end insert mode" },
118 #define T_fs 11
119 { "fs", "cursor from status line" },
120 #define T_ho 12
121 { "ho", "home cursor" },
122 #define T_ic 13
123 { "ic", "insert character" },
124 #define T_im 14
125 { "im", "start insert mode" },
126 #define T_ip 15
127 { "ip", "insert padding" },
128 #define T_kd 16
129 { "kd", "sends cursor down" },
130 #define T_kl 17
131 { "kl", "sends cursor left" },
132 #define T_kr 18
133 { "kr", "sends cursor right" },
134 #define T_ku 19
135 { "ku", "sends cursor up" },
136 #define T_md 20
137 { "md", "begin bold" },
138 #define T_me 21
139 { "me", "end attributes" },
140 #define T_nd 22
141 { "nd", "non destructive space" },
142 #define T_se 23
143 { "se", "end standout" },
144 #define T_so 24
145 { "so", "begin standout" },
146 #define T_ts 25
147 { "ts", "cursor to status line" },
148 #define T_up 26
149 { "up", "cursor up one" },
150 #define T_us 27
151 { "us", "begin underline" },
152 #define T_ue 28
153 { "ue", "end underline" },
154 #define T_vb 29
155 { "vb", "visible bell" },
156 #define T_DC 30
157 { "DC", "delete multiple chars" },
158 #define T_DO 31
159 { "DO", "cursor down multiple" },
160 #define T_IC 32
161 { "IC", "insert multiple chars" },
162 #define T_LE 33
163 { "LE", "cursor left multiple" },
164 #define T_RI 34
165 { "RI", "cursor right multiple" },
166 #define T_UP 35
167 { "UP", "cursor up multiple" },
168 #define T_kh 36
169 { "kh", "send cursor home" },
170 #define T_at7 37
171 { "@7", "send cursor end" },
172 #define T_str 38
173 { NULL, NULL }
174 };
175
176 private const struct termcapval {
177 const char *name;
178 const char *long_name;
179 } tval[] = {
180 #define T_am 0
181 { "am", "has automatic margins" },
182 #define T_pt 1
183 { "pt", "has physical tabs" },
184 #define T_li 2
185 { "li", "Number of lines" },
186 #define T_co 3
187 { "co", "Number of columns" },
188 #define T_km 4
189 { "km", "Has meta key" },
190 #define T_xt 5
191 { "xt", "Tab chars destructive" },
192 #define T_xn 6
193 { "xn", "newline ignored at right margin" },
194 #define T_MT 7
195 { "MT", "Has meta key" }, /* XXX? */
196 #define T_val 8
197 { NULL, NULL, }
198 };
199 /* do two or more of the attributes use me */
200
201 private void terminal_setflags(EditLine *);
202 private int terminal_rebuffer_display(EditLine *);
203 private void terminal_free_display(EditLine *);
204 private int terminal_alloc_display(EditLine *);
205 private void terminal_alloc(EditLine *, const struct termcapstr *,
206 const char *);
207 private void terminal_init_arrow(EditLine *);
208 private void terminal_reset_arrow(EditLine *);
209 private int terminal_putc(int);
210 private void terminal_tputs(EditLine *, const char *, int);
211
212 #ifdef _REENTRANT
213 private pthread_mutex_t terminal_mutex = PTHREAD_MUTEX_INITIALIZER;
214 #endif
215 private FILE *terminal_outfile = NULL;
216
217
218 /* terminal_setflags():
219 * Set the terminal capability flags
220 */
221 private void
222 terminal_setflags(EditLine *el)
223 {
224 EL_FLAGS = 0;
225 if (el->el_tty.t_tabs)
226 EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
227
228 EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
229 EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
230 EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
231 EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
232 TERM_CAN_INSERT : 0;
233 EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
234 EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0;
235 EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0;
236
237 if (GoodStr(T_me) && GoodStr(T_ue))
238 EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ?
239 TERM_CAN_ME : 0;
240 else
241 EL_FLAGS &= ~TERM_CAN_ME;
242 if (GoodStr(T_me) && GoodStr(T_se))
243 EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ?
244 TERM_CAN_ME : 0;
245
246
247 #ifdef DEBUG_SCREEN
248 if (!EL_CAN_UP) {
249 (void) fprintf(el->el_errfile,
250 "WARNING: Your terminal cannot move up.\n");
251 (void) fprintf(el->el_errfile,
252 "Editing may be odd for long lines.\n");
253 }
254 if (!EL_CAN_CEOL)
255 (void) fprintf(el->el_errfile, "no clear EOL capability.\n");
256 if (!EL_CAN_DELETE)
257 (void) fprintf(el->el_errfile, "no delete char capability.\n");
258 if (!EL_CAN_INSERT)
259 (void) fprintf(el->el_errfile, "no insert char capability.\n");
260 #endif /* DEBUG_SCREEN */
261 }
262
263 /* terminal_init():
264 * Initialize the terminal stuff
265 */
266 protected int
267 terminal_init(EditLine *el)
268 {
269
270 el->el_terminal.t_buf = el_malloc(TC_BUFSIZE *
271 sizeof(*el->el_terminal.t_buf));
272 if (el->el_terminal.t_buf == NULL)
273 return -1;
274 el->el_terminal.t_cap = el_malloc(TC_BUFSIZE *
275 sizeof(*el->el_terminal.t_cap));
276 if (el->el_terminal.t_cap == NULL)
277 return -1;
278 el->el_terminal.t_fkey = el_malloc(A_K_NKEYS *
279 sizeof(*el->el_terminal.t_fkey));
280 if (el->el_terminal.t_fkey == NULL)
281 return -1;
282 el->el_terminal.t_loc = 0;
283 el->el_terminal.t_str = el_malloc(T_str *
284 sizeof(*el->el_terminal.t_str));
285 if (el->el_terminal.t_str == NULL)
286 return -1;
287 (void) memset(el->el_terminal.t_str, 0, T_str *
288 sizeof(*el->el_terminal.t_str));
289 el->el_terminal.t_val = el_malloc(T_val *
290 sizeof(*el->el_terminal.t_val));
291 if (el->el_terminal.t_val == NULL)
292 return -1;
293 (void) memset(el->el_terminal.t_val, 0, T_val *
294 sizeof(*el->el_terminal.t_val));
295 (void) terminal_set(el, NULL);
296 terminal_init_arrow(el);
297 return 0;
298 }
299
300 /* terminal_end():
301 * Clean up the terminal stuff
302 */
303 protected void
304 terminal_end(EditLine *el)
305 {
306
307 el_free(el->el_terminal.t_buf);
308 el->el_terminal.t_buf = NULL;
309 el_free(el->el_terminal.t_cap);
310 el->el_terminal.t_cap = NULL;
311 el->el_terminal.t_loc = 0;
312 el_free(el->el_terminal.t_str);
313 el->el_terminal.t_str = NULL;
314 el_free(el->el_terminal.t_val);
315 el->el_terminal.t_val = NULL;
316 el_free(el->el_terminal.t_fkey);
317 el->el_terminal.t_fkey = NULL;
318 terminal_free_display(el);
319 }
320
321
322 /* terminal_alloc():
323 * Maintain a string pool for termcap strings
324 */
325 private void
326 terminal_alloc(EditLine *el, const struct termcapstr *t, const char *cap)
327 {
328 char termbuf[TC_BUFSIZE];
329 size_t tlen, clen;
330 char **tlist = el->el_terminal.t_str;
331 char **tmp, **str = &tlist[t - tstr];
332
333 if (cap == NULL || *cap == '\0') {
334 *str = NULL;
335 return;
336 } else
337 clen = strlen(cap);
338
339 tlen = *str == NULL ? 0 : strlen(*str);
340
341 /*
342 * New string is shorter; no need to allocate space
343 */
344 if (clen <= tlen) {
345 if (*str)
346 (void) strcpy(*str, cap); /* XXX strcpy is safe */
347 return;
348 }
349 /*
350 * New string is longer; see if we have enough space to append
351 */
352 if (el->el_terminal.t_loc + 3 < TC_BUFSIZE) {
353 /* XXX strcpy is safe */
354 (void) strcpy(*str = &el->el_terminal.t_buf[
355 el->el_terminal.t_loc], cap);
356 el->el_terminal.t_loc += clen + 1; /* one for \0 */
357 return;
358 }
359 /*
360 * Compact our buffer; no need to check compaction, cause we know it
361 * fits...
362 */
363 tlen = 0;
364 for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
365 if (*tmp != NULL && *tmp != '\0' && *tmp != *str) {
366 char *ptr;
367
368 for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
369 continue;
370 termbuf[tlen++] = '\0';
371 }
372 memcpy(el->el_terminal.t_buf, termbuf, TC_BUFSIZE);
373 el->el_terminal.t_loc = tlen;
374 if (el->el_terminal.t_loc + 3 >= TC_BUFSIZE) {
375 (void) fprintf(el->el_errfile,
376 "Out of termcap string space.\n");
377 return;
378 }
379 /* XXX strcpy is safe */
380 (void) strcpy(*str = &el->el_terminal.t_buf[el->el_terminal.t_loc],
381 cap);
382 el->el_terminal.t_loc += (size_t)clen + 1; /* one for \0 */
383 return;
384 }
385
386
387 /* terminal_rebuffer_display():
388 * Rebuffer the display after the screen changed size
389 */
390 private int
391 terminal_rebuffer_display(EditLine *el)
392 {
393 coord_t *c = &el->el_terminal.t_size;
394
395 terminal_free_display(el);
396
397 c->h = Val(T_co);
398 c->v = Val(T_li);
399
400 if (terminal_alloc_display(el) == -1)
401 return -1;
402 return 0;
403 }
404
405
406 /* terminal_alloc_display():
407 * Allocate a new display.
408 */
409 private int
410 terminal_alloc_display(EditLine *el)
411 {
412 int i;
413 Char **b;
414 coord_t *c = &el->el_terminal.t_size;
415
416 b = el_malloc(sizeof(*b) * (size_t)(c->v + 1));
417 if (b == NULL)
418 return -1;
419 for (i = 0; i < c->v; i++) {
420 b[i] = el_malloc(sizeof(**b) * (size_t)(c->h + 1));
421 if (b[i] == NULL) {
422 while (--i >= 0)
423 el_free(b[i]);
424 el_free(b);
425 return -1;
426 }
427 }
428 b[c->v] = NULL;
429 el->el_display = b;
430
431 b = el_malloc(sizeof(*b) * (size_t)(c->v + 1));
432 if (b == NULL)
433 return -1;
434 for (i = 0; i < c->v; i++) {
435 b[i] = el_malloc(sizeof(**b) * (size_t)(c->h + 1));
436 if (b[i] == NULL) {
437 while (--i >= 0)
438 el_free(b[i]);
439 el_free(b);
440 return -1;
441 }
442 }
443 b[c->v] = NULL;
444 el->el_vdisplay = b;
445 return 0;
446 }
447
448
449 /* terminal_free_display():
450 * Free the display buffers
451 */
452 private void
453 terminal_free_display(EditLine *el)
454 {
455 Char **b;
456 Char **bufp;
457
458 b = el->el_display;
459 el->el_display = NULL;
460 if (b != NULL) {
461 for (bufp = b; *bufp != NULL; bufp++)
462 el_free(*bufp);
463 el_free(b);
464 }
465 b = el->el_vdisplay;
466 el->el_vdisplay = NULL;
467 if (b != NULL) {
468 for (bufp = b; *bufp != NULL; bufp++)
469 el_free(*bufp);
470 el_free(b);
471 }
472 }
473
474
475 /* terminal_move_to_line():
476 * move to line <where> (first line == 0)
477 * as efficiently as possible
478 */
479 protected void
480 terminal_move_to_line(EditLine *el, int where)
481 {
482 int del;
483
484 if (where == el->el_cursor.v)
485 return;
486
487 if (where > el->el_terminal.t_size.v) {
488 #ifdef DEBUG_SCREEN
489 (void) fprintf(el->el_errfile,
490 "terminal_move_to_line: where is ridiculous: %d\r\n",
491 where);
492 #endif /* DEBUG_SCREEN */
493 return;
494 }
495 if ((del = where - el->el_cursor.v) > 0) {
496 while (del > 0) {
497 if (EL_HAS_AUTO_MARGINS &&
498 el->el_display[el->el_cursor.v][0] != '\0') {
499 size_t h = (size_t)
500 (el->el_terminal.t_size.h - 1);
501 #ifdef WIDECHAR
502 for (; h > 0 &&
503 el->el_display[el->el_cursor.v][h] ==
504 MB_FILL_CHAR;
505 h--)
506 continue;
507 #endif
508 /* move without newline */
509 terminal_move_to_char(el, (int)h);
510 terminal_overwrite(el, &el->el_display
511 [el->el_cursor.v][el->el_cursor.h],
512 (size_t)(el->el_terminal.t_size.h -
513 el->el_cursor.h));
514 /* updates Cursor */
515 del--;
516 } else {
517 if ((del > 1) && GoodStr(T_DO)) {
518 terminal_tputs(el, tgoto(Str(T_DO), del,
519 del), del);
520 del = 0;
521 } else {
522 for (; del > 0; del--)
523 terminal__putc(el, '\n');
524 /* because the \n will become \r\n */
525 el->el_cursor.h = 0;
526 }
527 }
528 }
529 } else { /* del < 0 */
530 if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
531 terminal_tputs(el, tgoto(Str(T_UP), -del, -del), -del);
532 else {
533 if (GoodStr(T_up))
534 for (; del < 0; del++)
535 terminal_tputs(el, Str(T_up), 1);
536 }
537 }
538 el->el_cursor.v = where;/* now where is here */
539 }
540
541
542 /* terminal_move_to_char():
543 * Move to the character position specified
544 */
545 protected void
546 terminal_move_to_char(EditLine *el, int where)
547 {
548 int del, i;
549
550 mc_again:
551 if (where == el->el_cursor.h)
552 return;
553
554 if (where > el->el_terminal.t_size.h) {
555 #ifdef DEBUG_SCREEN
556 (void) fprintf(el->el_errfile,
557 "terminal_move_to_char: where is riduculous: %d\r\n",
558 where);
559 #endif /* DEBUG_SCREEN */
560 return;
561 }
562 if (!where) { /* if where is first column */
563 terminal__putc(el, '\r'); /* do a CR */
564 el->el_cursor.h = 0;
565 return;
566 }
567 del = where - el->el_cursor.h;
568
569 if ((del < -4 || del > 4) && GoodStr(T_ch))
570 /* go there directly */
571 terminal_tputs(el, tgoto(Str(T_ch), where, where), where);
572 else {
573 if (del > 0) { /* moving forward */
574 if ((del > 4) && GoodStr(T_RI))
575 terminal_tputs(el, tgoto(Str(T_RI), del, del),
576 del);
577 else {
578 /* if I can do tabs, use them */
579 if (EL_CAN_TAB) {
580 if ((el->el_cursor.h & 0370) !=
581 (where & ~0x7)
582 #ifdef WIDECHAR
583 && (el->el_display[
584 el->el_cursor.v][where & 0370] !=
585 MB_FILL_CHAR)
586 #endif
587 ) {
588 /* if not within tab stop */
589 for (i =
590 (el->el_cursor.h & 0370);
591 i < (where & ~0x7);
592 i += 8)
593 terminal__putc(el,
594 '\t');
595 /* then tab over */
596 el->el_cursor.h = where & ~0x7;
597 }
598 }
599 /*
600 * it's usually cheaper to just write the
601 * chars, so we do.
602 */
603 /*
604 * NOTE THAT terminal_overwrite() WILL CHANGE
605 * el->el_cursor.h!!!
606 */
607 terminal_overwrite(el, &el->el_display[
608 el->el_cursor.v][el->el_cursor.h],
609 (size_t)(where - el->el_cursor.h));
610
611 }
612 } else { /* del < 0 := moving backward */
613 if ((-del > 4) && GoodStr(T_LE))
614 terminal_tputs(el, tgoto(Str(T_LE), -del, -del),
615 -del);
616 else { /* can't go directly there */
617 /*
618 * if the "cost" is greater than the "cost"
619 * from col 0
620 */
621 if (EL_CAN_TAB ?
622 ((unsigned int)-del >
623 (((unsigned int) where >> 3) +
624 (where & 07)))
625 : (-del > where)) {
626 terminal__putc(el, '\r');/* do a CR */
627 el->el_cursor.h = 0;
628 goto mc_again; /* and try again */
629 }
630 for (i = 0; i < -del; i++)
631 terminal__putc(el, '\b');
632 }
633 }
634 }
635 el->el_cursor.h = where; /* now where is here */
636 }
637
638
639 /* terminal_overwrite():
640 * Overstrike num characters
641 * Assumes MB_FILL_CHARs are present to keep the column count correct
642 */
643 protected void
644 terminal_overwrite(EditLine *el, const Char *cp, size_t n)
645 {
646 if (n == 0)
647 return;
648
649 if (n > (size_t)el->el_terminal.t_size.h) {
650 #ifdef DEBUG_SCREEN
651 (void) fprintf(el->el_errfile,
652 "terminal_overwrite: n is riduculous: %d\r\n", n);
653 #endif /* DEBUG_SCREEN */
654 return;
655 }
656
657 do {
658 /* terminal__putc() ignores any MB_FILL_CHARs */
659 terminal__putc(el, *cp++);
660 el->el_cursor.h++;
661 } while (--n);
662
663 if (el->el_cursor.h >= el->el_terminal.t_size.h) { /* wrap? */
664 if (EL_HAS_AUTO_MARGINS) { /* yes */
665 el->el_cursor.h = 0;
666 el->el_cursor.v++;
667 if (EL_HAS_MAGIC_MARGINS) {
668 /* force the wrap to avoid the "magic"
669 * situation */
670 Char c;
671 if ((c = el->el_display[el->el_cursor.v]
672 [el->el_cursor.h]) != '\0') {
673 terminal_overwrite(el, &c, (size_t)1);
674 #ifdef WIDECHAR
675 while (el->el_display[el->el_cursor.v]
676 [el->el_cursor.h] == MB_FILL_CHAR)
677 el->el_cursor.h++;
678 #endif
679 } else {
680 terminal__putc(el, ' ');
681 el->el_cursor.h = 1;
682 }
683 }
684 } else /* no wrap, but cursor stays on screen */
685 el->el_cursor.h = el->el_terminal.t_size.h - 1;
686 }
687 }
688
689
690 /* terminal_deletechars():
691 * Delete num characters
692 */
693 protected void
694 terminal_deletechars(EditLine *el, int num)
695 {
696 if (num <= 0)
697 return;
698
699 if (!EL_CAN_DELETE) {
700 #ifdef DEBUG_EDIT
701 (void) fprintf(el->el_errfile, " ERROR: cannot delete \n");
702 #endif /* DEBUG_EDIT */
703 return;
704 }
705 if (num > el->el_terminal.t_size.h) {
706 #ifdef DEBUG_SCREEN
707 (void) fprintf(el->el_errfile,
708 "terminal_deletechars: num is riduculous: %d\r\n", num);
709 #endif /* DEBUG_SCREEN */
710 return;
711 }
712 if (GoodStr(T_DC)) /* if I have multiple delete */
713 if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more
714 * expen. */
715 terminal_tputs(el, tgoto(Str(T_DC), num, num), num);
716 return;
717 }
718 if (GoodStr(T_dm)) /* if I have delete mode */
719 terminal_tputs(el, Str(T_dm), 1);
720
721 if (GoodStr(T_dc)) /* else do one at a time */
722 while (num--)
723 terminal_tputs(el, Str(T_dc), 1);
724
725 if (GoodStr(T_ed)) /* if I have delete mode */
726 terminal_tputs(el, Str(T_ed), 1);
727 }
728
729
730 /* terminal_insertwrite():
731 * Puts terminal in insert character mode or inserts num
732 * characters in the line
733 * Assumes MB_FILL_CHARs are present to keep column count correct
734 */
735 protected void
736 terminal_insertwrite(EditLine *el, Char *cp, int num)
737 {
738 if (num <= 0)
739 return;
740 if (!EL_CAN_INSERT) {
741 #ifdef DEBUG_EDIT
742 (void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
743 #endif /* DEBUG_EDIT */
744 return;
745 }
746 if (num > el->el_terminal.t_size.h) {
747 #ifdef DEBUG_SCREEN
748 (void) fprintf(el->el_errfile,
749 "StartInsert: num is riduculous: %d\r\n", num);
750 #endif /* DEBUG_SCREEN */
751 return;
752 }
753 if (GoodStr(T_IC)) /* if I have multiple insert */
754 if ((num > 1) || !GoodStr(T_ic)) {
755 /* if ic would be more expensive */
756 terminal_tputs(el, tgoto(Str(T_IC), num, num), num);
757 terminal_overwrite(el, cp, (size_t)num);
758 /* this updates el_cursor.h */
759 return;
760 }
761 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
762 terminal_tputs(el, Str(T_im), 1);
763
764 el->el_cursor.h += num;
765 do
766 terminal__putc(el, *cp++);
767 while (--num);
768
769 if (GoodStr(T_ip)) /* have to make num chars insert */
770 terminal_tputs(el, Str(T_ip), 1);
771
772 terminal_tputs(el, Str(T_ei), 1);
773 return;
774 }
775 do {
776 if (GoodStr(T_ic)) /* have to make num chars insert */
777 terminal_tputs(el, Str(T_ic), 1);
778
779 terminal__putc(el, *cp++);
780
781 el->el_cursor.h++;
782
783 if (GoodStr(T_ip)) /* have to make num chars insert */
784 terminal_tputs(el, Str(T_ip), 1);
785 /* pad the inserted char */
786
787 } while (--num);
788 }
789
790
791 /* terminal_clear_EOL():
792 * clear to end of line. There are num characters to clear
793 */
794 protected void
795 terminal_clear_EOL(EditLine *el, int num)
796 {
797 int i;
798
799 if (EL_CAN_CEOL && GoodStr(T_ce))
800 terminal_tputs(el, Str(T_ce), 1);
801 else {
802 for (i = 0; i < num; i++)
803 terminal__putc(el, ' ');
804 el->el_cursor.h += num; /* have written num spaces */
805 }
806 }
807
808
809 /* terminal_clear_screen():
810 * Clear the screen
811 */
812 protected void
813 terminal_clear_screen(EditLine *el)
814 { /* clear the whole screen and home */
815
816 if (GoodStr(T_cl))
817 /* send the clear screen code */
818 terminal_tputs(el, Str(T_cl), Val(T_li));
819 else if (GoodStr(T_ho) && GoodStr(T_cd)) {
820 terminal_tputs(el, Str(T_ho), Val(T_li)); /* home */
821 /* clear to bottom of screen */
822 terminal_tputs(el, Str(T_cd), Val(T_li));
823 } else {
824 terminal__putc(el, '\r');
825 terminal__putc(el, '\n');
826 }
827 }
828
829
830 /* terminal_beep():
831 * Beep the way the terminal wants us
832 */
833 protected void
834 terminal_beep(EditLine *el)
835 {
836 if (GoodStr(T_bl))
837 /* what termcap says we should use */
838 terminal_tputs(el, Str(T_bl), 1);
839 else
840 terminal__putc(el, '\007'); /* an ASCII bell; ^G */
841 }
842
843
844 protected void
845 terminal_get(EditLine *el, const char **term)
846 {
847 *term = el->el_terminal.t_name;
848 }
849
850
851 /* terminal_set():
852 * Read in the terminal capabilities from the requested terminal
853 */
854 protected int
855 terminal_set(EditLine *el, const char *term)
856 {
857 int i;
858 char buf[TC_BUFSIZE];
859 char *area;
860 const struct termcapstr *t;
861 sigset_t oset, nset;
862 int lins, cols;
863
864 (void) sigemptyset(&nset);
865 (void) sigaddset(&nset, SIGWINCH);
866 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
867
868 area = buf;
869
870
871 if (term == NULL)
872 term = getenv("TERM");
873
874 if (!term || !term[0])
875 term = "dumb";
876
877 if (strcmp(term, "emacs") == 0)
878 el->el_flags |= EDIT_DISABLED;
879
880 memset(el->el_terminal.t_cap, 0, TC_BUFSIZE);
881
882 i = tgetent(el->el_terminal.t_cap, term);
883
884 if (i <= 0) {
885 if (i == -1)
886 (void) fprintf(el->el_errfile,
887 "Cannot read termcap database;\n");
888 else if (i == 0)
889 (void) fprintf(el->el_errfile,
890 "No entry for terminal type \"%s\";\n", term);
891 (void) fprintf(el->el_errfile,
892 "using dumb terminal settings.\n");
893 Val(T_co) = 80; /* do a dumb terminal */
894 Val(T_pt) = Val(T_km) = Val(T_li) = 0;
895 Val(T_xt) = Val(T_MT);
896 for (t = tstr; t->name != NULL; t++)
897 terminal_alloc(el, t, NULL);
898 } else {
899 /* auto/magic margins */
900 Val(T_am) = tgetflag("am");
901 Val(T_xn) = tgetflag("xn");
902 /* Can we tab */
903 Val(T_pt) = tgetflag("pt");
904 Val(T_xt) = tgetflag("xt");
905 /* do we have a meta? */
906 Val(T_km) = tgetflag("km");
907 Val(T_MT) = tgetflag("MT");
908 /* Get the size */
909 Val(T_co) = tgetnum("co");
910 Val(T_li) = tgetnum("li");
911 for (t = tstr; t->name != NULL; t++) {
912 /* XXX: some systems' tgetstr needs non const */
913 terminal_alloc(el, t, tgetstr(strchr(t->name, *t->name),
914 &area));
915 }
916 }
917
918 if (Val(T_co) < 2)
919 Val(T_co) = 80; /* just in case */
920 if (Val(T_li) < 1)
921 Val(T_li) = 24;
922
923 el->el_terminal.t_size.v = Val(T_co);
924 el->el_terminal.t_size.h = Val(T_li);
925
926 terminal_setflags(el);
927
928 /* get the correct window size */
929 (void) terminal_get_size(el, &lins, &cols);
930 if (terminal_change_size(el, lins, cols) == -1)
931 return -1;
932 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
933 terminal_bind_arrow(el);
934 el->el_terminal.t_name = term;
935 return i <= 0 ? -1 : 0;
936 }
937
938
939 /* terminal_get_size():
940 * Return the new window size in lines and cols, and
941 * true if the size was changed.
942 */
943 protected int
944 terminal_get_size(EditLine *el, int *lins, int *cols)
945 {
946
947 *cols = Val(T_co);
948 *lins = Val(T_li);
949
950 #ifdef TIOCGWINSZ
951 {
952 struct winsize ws;
953 if (ioctl(el->el_infd, TIOCGWINSZ, &ws) != -1) {
954 if (ws.ws_col)
955 *cols = ws.ws_col;
956 if (ws.ws_row)
957 *lins = ws.ws_row;
958 }
959 }
960 #endif
961 #ifdef TIOCGSIZE
962 {
963 struct ttysize ts;
964 if (ioctl(el->el_infd, TIOCGSIZE, &ts) != -1) {
965 if (ts.ts_cols)
966 *cols = ts.ts_cols;
967 if (ts.ts_lines)
968 *lins = ts.ts_lines;
969 }
970 }
971 #endif
972 return Val(T_co) != *cols || Val(T_li) != *lins;
973 }
974
975
976 /* terminal_change_size():
977 * Change the size of the terminal
978 */
979 protected int
980 terminal_change_size(EditLine *el, int lins, int cols)
981 {
982 /*
983 * Just in case
984 */
985 Val(T_co) = (cols < 2) ? 80 : cols;
986 Val(T_li) = (lins < 1) ? 24 : lins;
987
988 /* re-make display buffers */
989 if (terminal_rebuffer_display(el) == -1)
990 return -1;
991 re_clear_display(el);
992 return 0;
993 }
994
995
996 /* terminal_init_arrow():
997 * Initialize the arrow key bindings from termcap
998 */
999 private void
1000 terminal_init_arrow(EditLine *el)
1001 {
1002 funckey_t *arrow = el->el_terminal.t_fkey;
1003
1004 arrow[A_K_DN].name = STR("down");
1005 arrow[A_K_DN].key = T_kd;
1006 arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
1007 arrow[A_K_DN].type = XK_CMD;
1008
1009 arrow[A_K_UP].name = STR("up");
1010 arrow[A_K_UP].key = T_ku;
1011 arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
1012 arrow[A_K_UP].type = XK_CMD;
1013
1014 arrow[A_K_LT].name = STR("left");
1015 arrow[A_K_LT].key = T_kl;
1016 arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
1017 arrow[A_K_LT].type = XK_CMD;
1018
1019 arrow[A_K_RT].name = STR("right");
1020 arrow[A_K_RT].key = T_kr;
1021 arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
1022 arrow[A_K_RT].type = XK_CMD;
1023
1024 arrow[A_K_HO].name = STR("home");
1025 arrow[A_K_HO].key = T_kh;
1026 arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
1027 arrow[A_K_HO].type = XK_CMD;
1028
1029 arrow[A_K_EN].name = STR("end");
1030 arrow[A_K_EN].key = T_at7;
1031 arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
1032 arrow[A_K_EN].type = XK_CMD;
1033 }
1034
1035
1036 /* terminal_reset_arrow():
1037 * Reset arrow key bindings
1038 */
1039 private void
1040 terminal_reset_arrow(EditLine *el)
1041 {
1042 funckey_t *arrow = el->el_terminal.t_fkey;
1043 static const Char strA[] = {033, '[', 'A', '\0'};
1044 static const Char strB[] = {033, '[', 'B', '\0'};
1045 static const Char strC[] = {033, '[', 'C', '\0'};
1046 static const Char strD[] = {033, '[', 'D', '\0'};
1047 static const Char strH[] = {033, '[', 'H', '\0'};
1048 static const Char strF[] = {033, '[', 'F', '\0'};
1049 static const Char stOA[] = {033, 'O', 'A', '\0'};
1050 static const Char stOB[] = {033, 'O', 'B', '\0'};
1051 static const Char stOC[] = {033, 'O', 'C', '\0'};
1052 static const Char stOD[] = {033, 'O', 'D', '\0'};
1053 static const Char stOH[] = {033, 'O', 'H', '\0'};
1054 static const Char stOF[] = {033, 'O', 'F', '\0'};
1055
1056 keymacro_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1057 keymacro_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1058 keymacro_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1059 keymacro_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1060 keymacro_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1061 keymacro_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1062 keymacro_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1063 keymacro_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1064 keymacro_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1065 keymacro_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1066 keymacro_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1067 keymacro_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1068
1069 if (el->el_map.type != MAP_VI)
1070 return;
1071 keymacro_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1072 keymacro_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1073 keymacro_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1074 keymacro_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1075 keymacro_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1076 keymacro_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1077 keymacro_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1078 keymacro_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1079 keymacro_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1080 keymacro_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1081 keymacro_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1082 keymacro_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1083 }
1084
1085
1086 /* terminal_set_arrow():
1087 * Set an arrow key binding
1088 */
1089 protected int
1090 terminal_set_arrow(EditLine *el, const Char *name, keymacro_value_t *fun,
1091 int type)
1092 {
1093 funckey_t *arrow = el->el_terminal.t_fkey;
1094 int i;
1095
1096 for (i = 0; i < A_K_NKEYS; i++)
1097 if (Strcmp(name, arrow[i].name) == 0) {
1098 arrow[i].fun = *fun;
1099 arrow[i].type = type;
1100 return 0;
1101 }
1102 return -1;
1103 }
1104
1105
1106 /* terminal_clear_arrow():
1107 * Clear an arrow key binding
1108 */
1109 protected int
1110 terminal_clear_arrow(EditLine *el, const Char *name)
1111 {
1112 funckey_t *arrow = el->el_terminal.t_fkey;
1113 int i;
1114
1115 for (i = 0; i < A_K_NKEYS; i++)
1116 if (Strcmp(name, arrow[i].name) == 0) {
1117 arrow[i].type = XK_NOD;
1118 return 0;
1119 }
1120 return -1;
1121 }
1122
1123
1124 /* terminal_print_arrow():
1125 * Print the arrow key bindings
1126 */
1127 protected void
1128 terminal_print_arrow(EditLine *el, const Char *name)
1129 {
1130 int i;
1131 funckey_t *arrow = el->el_terminal.t_fkey;
1132
1133 for (i = 0; i < A_K_NKEYS; i++)
1134 if (*name == '\0' || Strcmp(name, arrow[i].name) == 0)
1135 if (arrow[i].type != XK_NOD)
1136 keymacro_kprint(el, arrow[i].name,
1137 &arrow[i].fun, arrow[i].type);
1138 }
1139
1140
1141 /* terminal_bind_arrow():
1142 * Bind the arrow keys
1143 */
1144 protected void
1145 terminal_bind_arrow(EditLine *el)
1146 {
1147 el_action_t *map;
1148 const el_action_t *dmap;
1149 int i, j;
1150 char *p;
1151 funckey_t *arrow = el->el_terminal.t_fkey;
1152
1153 /* Check if the components needed are initialized */
1154 if (el->el_terminal.t_buf == NULL || el->el_map.key == NULL)
1155 return;
1156
1157 map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
1158 dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
1159
1160 terminal_reset_arrow(el);
1161
1162 for (i = 0; i < A_K_NKEYS; i++) {
1163 Char wt_str[VISUAL_WIDTH_MAX];
1164 Char *px;
1165 size_t n;
1166
1167 p = el->el_terminal.t_str[arrow[i].key];
1168 if (!p || !*p)
1169 continue;
1170 for (n = 0; n < VISUAL_WIDTH_MAX && p[n]; ++n)
1171 wt_str[n] = p[n];
1172 while (n < VISUAL_WIDTH_MAX)
1173 wt_str[n++] = '\0';
1174 px = wt_str;
1175 j = (unsigned char) *p;
1176 /*
1177 * Assign the arrow keys only if:
1178 *
1179 * 1. They are multi-character arrow keys and the user
1180 * has not re-assigned the leading character, or
1181 * has re-assigned the leading character to be
1182 * ED_SEQUENCE_LEAD_IN
1183 * 2. They are single arrow keys pointing to an
1184 * unassigned key.
1185 */
1186 if (arrow[i].type == XK_NOD)
1187 keymacro_clear(el, map, px);
1188 else {
1189 if (p[1] && (dmap[j] == map[j] ||
1190 map[j] == ED_SEQUENCE_LEAD_IN)) {
1191 keymacro_add(el, px, &arrow[i].fun,
1192 arrow[i].type);
1193 map[j] = ED_SEQUENCE_LEAD_IN;
1194 } else if (map[j] == ED_UNASSIGNED) {
1195 keymacro_clear(el, map, px);
1196 if (arrow[i].type == XK_CMD)
1197 map[j] = arrow[i].fun.cmd;
1198 else
1199 keymacro_add(el, px, &arrow[i].fun,
1200 arrow[i].type);
1201 }
1202 }
1203 }
1204 }
1205
1206 /* terminal_putc():
1207 * Add a character
1208 */
1209 private int
1210 terminal_putc(int c)
1211 {
1212 if (terminal_outfile == NULL)
1213 return -1;
1214 return fputc(c, terminal_outfile);
1215 }
1216
1217 private void
1218 terminal_tputs(EditLine *el, const char *cap, int affcnt)
1219 {
1220 #ifdef _REENTRANT
1221 pthread_mutex_lock(&terminal_mutex);
1222 #endif
1223 terminal_outfile = el->el_outfile;
1224 (void)tputs(cap, affcnt, terminal_putc);
1225 #ifdef _REENTRANT
1226 pthread_mutex_unlock(&terminal_mutex);
1227 #endif
1228 }
1229
1230 /* terminal__putc():
1231 * Add a character
1232 */
1233 protected int
1234 terminal__putc(EditLine *el, Int c)
1235 {
1236 char buf[MB_LEN_MAX +1];
1237 ssize_t i;
1238 if (c == (Int)MB_FILL_CHAR)
1239 return 0;
1240 i = ct_encode_char(buf, (size_t)MB_LEN_MAX, c);
1241 if (i <= 0)
1242 return (int)i;
1243 buf[i] = '\0';
1244 return fputs(buf, el->el_outfile);
1245 }
1246
1247 /* terminal__flush():
1248 * Flush output
1249 */
1250 protected void
1251 terminal__flush(EditLine *el)
1252 {
1253
1254 (void) fflush(el->el_outfile);
1255 }
1256
1257 /* terminal_writec():
1258 * Write the given character out, in a human readable form
1259 */
1260 protected void
1261 terminal_writec(EditLine *el, Int c)
1262 {
1263 Char visbuf[VISUAL_WIDTH_MAX +1];
1264 ssize_t vcnt = ct_visual_char(visbuf, VISUAL_WIDTH_MAX, c);
1265 visbuf[vcnt] = '\0';
1266 terminal_overwrite(el, visbuf, (size_t)vcnt);
1267 terminal__flush(el);
1268 }
1269
1270
1271 /* terminal_telltc():
1272 * Print the current termcap characteristics
1273 */
1274 protected int
1275 /*ARGSUSED*/
1276 terminal_telltc(EditLine *el, int argc __attribute__((__unused__)),
1277 const Char **argv __attribute__((__unused__)))
1278 {
1279 const struct termcapstr *t;
1280 char **ts;
1281
1282 (void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
1283 (void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
1284 (void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
1285 Val(T_co), Val(T_li));
1286 (void) fprintf(el->el_outfile,
1287 "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
1288 (void) fprintf(el->el_outfile,
1289 "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
1290 (void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
1291 EL_HAS_AUTO_MARGINS ? "has" : "does not have");
1292 if (EL_HAS_AUTO_MARGINS)
1293 (void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
1294 EL_HAS_MAGIC_MARGINS ? "has" : "does not have");
1295
1296 for (t = tstr, ts = el->el_terminal.t_str; t->name != NULL; t++, ts++) {
1297 const char *ub;
1298 if (*ts && **ts) {
1299 ub = ct_encode_string(ct_visual_string(
1300 ct_decode_string(*ts, &el->el_scratch)),
1301 &el->el_scratch);
1302 } else {
1303 ub = "(empty)";
1304 }
1305 (void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n",
1306 t->long_name, t->name, ub);
1307 }
1308 (void) fputc('\n', el->el_outfile);
1309 return 0;
1310 }
1311
1312
1313 /* terminal_settc():
1314 * Change the current terminal characteristics
1315 */
1316 protected int
1317 /*ARGSUSED*/
1318 terminal_settc(EditLine *el, int argc __attribute__((__unused__)),
1319 const Char **argv)
1320 {
1321 const struct termcapstr *ts;
1322 const struct termcapval *tv;
1323 char what[8], how[8];
1324
1325 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1326 return -1;
1327
1328 strncpy(what, ct_encode_string(argv[1], &el->el_scratch), sizeof(what));
1329 what[sizeof(what) - 1] = '\0';
1330 strncpy(how, ct_encode_string(argv[2], &el->el_scratch), sizeof(how));
1331 how[sizeof(how) - 1] = '\0';
1332
1333 /*
1334 * Do the strings first
1335 */
1336 for (ts = tstr; ts->name != NULL; ts++)
1337 if (strcmp(ts->name, what) == 0)
1338 break;
1339
1340 if (ts->name != NULL) {
1341 terminal_alloc(el, ts, how);
1342 terminal_setflags(el);
1343 return 0;
1344 }
1345 /*
1346 * Do the numeric ones second
1347 */
1348 for (tv = tval; tv->name != NULL; tv++)
1349 if (strcmp(tv->name, what) == 0)
1350 break;
1351
1352 if (tv->name != NULL)
1353 return -1;
1354
1355 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1356 tv == &tval[T_am] || tv == &tval[T_xn]) {
1357 if (strcmp(how, "yes") == 0)
1358 el->el_terminal.t_val[tv - tval] = 1;
1359 else if (strcmp(how, "no") == 0)
1360 el->el_terminal.t_val[tv - tval] = 0;
1361 else {
1362 (void) fprintf(el->el_errfile,
1363 "" FSTR ": Bad value `%s'.\n", argv[0], how);
1364 return -1;
1365 }
1366 terminal_setflags(el);
1367 if (terminal_change_size(el, Val(T_li), Val(T_co)) == -1)
1368 return -1;
1369 return 0;
1370 } else {
1371 long i;
1372 char *ep;
1373
1374 i = strtol(how, &ep, 10);
1375 if (*ep != '\0') {
1376 (void) fprintf(el->el_errfile,
1377 "" FSTR ": Bad value `%s'.\n", argv[0], how);
1378 return -1;
1379 }
1380 el->el_terminal.t_val[tv - tval] = (int) i;
1381 el->el_terminal.t_size.v = Val(T_co);
1382 el->el_terminal.t_size.h = Val(T_li);
1383 if (tv == &tval[T_co] || tv == &tval[T_li])
1384 if (terminal_change_size(el, Val(T_li), Val(T_co))
1385 == -1)
1386 return -1;
1387 return 0;
1388 }
1389 }
1390
1391
1392 /* terminal_gettc():
1393 * Get the current terminal characteristics
1394 */
1395 protected int
1396 /*ARGSUSED*/
1397 terminal_gettc(EditLine *el, int argc __attribute__((__unused__)), char **argv)
1398 {
1399 const struct termcapstr *ts;
1400 const struct termcapval *tv;
1401 char *what;
1402 void *how;
1403
1404 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1405 return -1;
1406
1407 what = argv[1];
1408 how = argv[2];
1409
1410 /*
1411 * Do the strings first
1412 */
1413 for (ts = tstr; ts->name != NULL; ts++)
1414 if (strcmp(ts->name, what) == 0)
1415 break;
1416
1417 if (ts->name != NULL) {
1418 *(char **)how = el->el_terminal.t_str[ts - tstr];
1419 return 0;
1420 }
1421 /*
1422 * Do the numeric ones second
1423 */
1424 for (tv = tval; tv->name != NULL; tv++)
1425 if (strcmp(tv->name, what) == 0)
1426 break;
1427
1428 if (tv->name == NULL)
1429 return -1;
1430
1431 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1432 tv == &tval[T_am] || tv == &tval[T_xn]) {
1433 static char yes[] = "yes";
1434 static char no[] = "no";
1435 if (el->el_terminal.t_val[tv - tval])
1436 *(char **)how = yes;
1437 else
1438 *(char **)how = no;
1439 return 0;
1440 } else {
1441 *(int *)how = el->el_terminal.t_val[tv - tval];
1442 return 0;
1443 }
1444 }
1445
1446 /* terminal_echotc():
1447 * Print the termcap string out with variable substitution
1448 */
1449 protected int
1450 /*ARGSUSED*/
1451 terminal_echotc(EditLine *el, int argc __attribute__((__unused__)),
1452 const Char **argv)
1453 {
1454 char *cap, *scap;
1455 Char *ep;
1456 int arg_need, arg_cols, arg_rows;
1457 int verbose = 0, silent = 0;
1458 char *area;
1459 static const char fmts[] = "%s\n", fmtd[] = "%d\n";
1460 const struct termcapstr *t;
1461 char buf[TC_BUFSIZE];
1462 long i;
1463
1464 area = buf;
1465
1466 if (argv == NULL || argv[1] == NULL)
1467 return -1;
1468 argv++;
1469
1470 if (argv[0][0] == '-') {
1471 switch (argv[0][1]) {
1472 case 'v':
1473 verbose = 1;
1474 break;
1475 case 's':
1476 silent = 1;
1477 break;
1478 default:
1479 /* stderror(ERR_NAME | ERR_TCUSAGE); */
1480 break;
1481 }
1482 argv++;
1483 }
1484 if (!*argv || *argv[0] == '\0')
1485 return 0;
1486 if (Strcmp(*argv, STR("tabs")) == 0) {
1487 (void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
1488 return 0;
1489 } else if (Strcmp(*argv, STR("meta")) == 0) {
1490 (void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
1491 return 0;
1492 } else if (Strcmp(*argv, STR("xn")) == 0) {
1493 (void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ?
1494 "yes" : "no");
1495 return 0;
1496 } else if (Strcmp(*argv, STR("am")) == 0) {
1497 (void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ?
1498 "yes" : "no");
1499 return 0;
1500 } else if (Strcmp(*argv, STR("baud")) == 0) {
1501 (void) fprintf(el->el_outfile, fmtd, (int)el->el_tty.t_speed);
1502 return 0;
1503 } else if (Strcmp(*argv, STR("rows")) == 0 ||
1504 Strcmp(*argv, STR("lines")) == 0) {
1505 (void) fprintf(el->el_outfile, fmtd, Val(T_li));
1506 return 0;
1507 } else if (Strcmp(*argv, STR("cols")) == 0) {
1508 (void) fprintf(el->el_outfile, fmtd, Val(T_co));
1509 return 0;
1510 }
1511 /*
1512 * Try to use our local definition first
1513 */
1514 scap = NULL;
1515 for (t = tstr; t->name != NULL; t++)
1516 if (strcmp(t->name,
1517 ct_encode_string(*argv, &el->el_scratch)) == 0) {
1518 scap = el->el_terminal.t_str[t - tstr];
1519 break;
1520 }
1521 if (t->name == NULL) {
1522 /* XXX: some systems' tgetstr needs non const */
1523 scap = tgetstr(ct_encode_string(*argv, &el->el_scratch), &area);
1524 }
1525 if (!scap || scap[0] == '\0') {
1526 if (!silent)
1527 (void) fprintf(el->el_errfile,
1528 "echotc: Termcap parameter `" FSTR "' not found.\n",
1529 *argv);
1530 return -1;
1531 }
1532 /*
1533 * Count home many values we need for this capability.
1534 */
1535 for (cap = scap, arg_need = 0; *cap; cap++)
1536 if (*cap == '%')
1537 switch (*++cap) {
1538 case 'd':
1539 case '2':
1540 case '3':
1541 case '.':
1542 case '+':
1543 arg_need++;
1544 break;
1545 case '%':
1546 case '>':
1547 case 'i':
1548 case 'r':
1549 case 'n':
1550 case 'B':
1551 case 'D':
1552 break;
1553 default:
1554 /*
1555 * hpux has lot's of them...
1556 */
1557 if (verbose)
1558 (void) fprintf(el->el_errfile,
1559 "echotc: Warning: unknown termcap %% `%c'.\n",
1560 *cap);
1561 /* This is bad, but I won't complain */
1562 break;
1563 }
1564
1565 switch (arg_need) {
1566 case 0:
1567 argv++;
1568 if (*argv && *argv[0]) {
1569 if (!silent)
1570 (void) fprintf(el->el_errfile,
1571 "echotc: Warning: Extra argument `" FSTR "'.\n",
1572 *argv);
1573 return -1;
1574 }
1575 terminal_tputs(el, scap, 1);
1576 break;
1577 case 1:
1578 argv++;
1579 if (!*argv || *argv[0] == '\0') {
1580 if (!silent)
1581 (void) fprintf(el->el_errfile,
1582 "echotc: Warning: Missing argument.\n");
1583 return -1;
1584 }
1585 arg_cols = 0;
1586 i = Strtol(*argv, &ep, 10);
1587 if (*ep != '\0' || i < 0) {
1588 if (!silent)
1589 (void) fprintf(el->el_errfile,
1590 "echotc: Bad value `" FSTR "' for rows.\n",
1591 *argv);
1592 return -1;
1593 }
1594 arg_rows = (int) i;
1595 argv++;
1596 if (*argv && *argv[0]) {
1597 if (!silent)
1598 (void) fprintf(el->el_errfile,
1599 "echotc: Warning: Extra argument `" FSTR
1600 "'.\n", *argv);
1601 return -1;
1602 }
1603 terminal_tputs(el, tgoto(scap, arg_cols, arg_rows), 1);
1604 break;
1605 default:
1606 /* This is wrong, but I will ignore it... */
1607 if (verbose)
1608 (void) fprintf(el->el_errfile,
1609 "echotc: Warning: Too many required arguments (%d).\n",
1610 arg_need);
1611 /* FALLTHROUGH */
1612 case 2:
1613 argv++;
1614 if (!*argv || *argv[0] == '\0') {
1615 if (!silent)
1616 (void) fprintf(el->el_errfile,
1617 "echotc: Warning: Missing argument.\n");
1618 return -1;
1619 }
1620 i = Strtol(*argv, &ep, 10);
1621 if (*ep != '\0' || i < 0) {
1622 if (!silent)
1623 (void) fprintf(el->el_errfile,
1624 "echotc: Bad value `" FSTR "' for cols.\n",
1625 *argv);
1626 return -1;
1627 }
1628 arg_cols = (int) i;
1629 argv++;
1630 if (!*argv || *argv[0] == '\0') {
1631 if (!silent)
1632 (void) fprintf(el->el_errfile,
1633 "echotc: Warning: Missing argument.\n");
1634 return -1;
1635 }
1636 i = Strtol(*argv, &ep, 10);
1637 if (*ep != '\0' || i < 0) {
1638 if (!silent)
1639 (void) fprintf(el->el_errfile,
1640 "echotc: Bad value `" FSTR "' for rows.\n",
1641 *argv);
1642 return -1;
1643 }
1644 arg_rows = (int) i;
1645 if (*ep != '\0') {
1646 if (!silent)
1647 (void) fprintf(el->el_errfile,
1648 "echotc: Bad value `" FSTR "'.\n", *argv);
1649 return -1;
1650 }
1651 argv++;
1652 if (*argv && *argv[0]) {
1653 if (!silent)
1654 (void) fprintf(el->el_errfile,
1655 "echotc: Warning: Extra argument `" FSTR
1656 "'.\n", *argv);
1657 return -1;
1658 }
1659 terminal_tputs(el, tgoto(scap, arg_cols, arg_rows), arg_rows);
1660 break;
1661 }
1662 return 0;
1663 }
1664