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