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