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