sync
[bitrig.git] / lib / libedit / term.c
blobe6d6cadd3552c361b262657109e061dd94ce0489
1 /* $OpenBSD: term.c,v 1.15 2011/07/07 05:40:42 okan Exp $ */
2 /* $NetBSD: term.c,v 1.57 2009/12/30 22:37:40 christos Exp $ */
4 /*-
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Christos Zoulas of Cornell University.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #include "config.h"
39 * term.c: Editor/termcap-curses interface
40 * We have to declare a static variable here, since the
41 * termcap putchar routine does not take an argument!
43 #include <stdio.h>
44 #include <signal.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <limits.h>
49 #ifdef HAVE_TERMCAP_H
50 #include <termcap.h>
51 #endif
52 #ifdef HAVE_CURSES_H
53 #include <curses.h>
54 #elif HAVE_NCURSES_H
55 #include <ncurses.h>
56 #endif
57 /* Solaris's term.h does horrid things. */
59 #if defined(HAVE_TERM_H) && !defined(__sun)
60 #include <term.h>
61 #endif
62 #include <sys/types.h>
63 #include <sys/ioctl.h>
65 #ifdef _REENTRANT
66 #include <pthread.h>
67 #endif
69 #include "el.h"
72 * IMPORTANT NOTE: these routines are allowed to look at the current screen
73 * and the current position assuming that it is correct. If this is not
74 * true, then the update will be WRONG! This is (should be) a valid
75 * assumption...
78 #define TC_BUFSIZE 2048
80 #define GoodStr(a) (el->el_term.t_str[a] != NULL && \
81 el->el_term.t_str[a][0] != '\0')
82 #define Str(a) el->el_term.t_str[a]
83 #define Val(a) el->el_term.t_val[a]
85 #ifdef notdef
86 private const struct {
87 const char *b_name;
88 int b_rate;
89 } baud_rate[] = {
90 #ifdef B0
91 { "0", B0 },
92 #endif
93 #ifdef B50
94 { "50", B50 },
95 #endif
96 #ifdef B75
97 { "75", B75 },
98 #endif
99 #ifdef B110
100 { "110", B110 },
101 #endif
102 #ifdef B134
103 { "134", B134 },
104 #endif
105 #ifdef B150
106 { "150", B150 },
107 #endif
108 #ifdef B200
109 { "200", B200 },
110 #endif
111 #ifdef B300
112 { "300", B300 },
113 #endif
114 #ifdef B600
115 { "600", B600 },
116 #endif
117 #ifdef B900
118 { "900", B900 },
119 #endif
120 #ifdef B1200
121 { "1200", B1200 },
122 #endif
123 #ifdef B1800
124 { "1800", B1800 },
125 #endif
126 #ifdef B2400
127 { "2400", B2400 },
128 #endif
129 #ifdef B3600
130 { "3600", B3600 },
131 #endif
132 #ifdef B4800
133 { "4800", B4800 },
134 #endif
135 #ifdef B7200
136 { "7200", B7200 },
137 #endif
138 #ifdef B9600
139 { "9600", B9600 },
140 #endif
141 #ifdef EXTA
142 { "19200", EXTA },
143 #endif
144 #ifdef B19200
145 { "19200", B19200 },
146 #endif
147 #ifdef EXTB
148 { "38400", EXTB },
149 #endif
150 #ifdef B38400
151 { "38400", B38400 },
152 #endif
153 { NULL, 0 }
155 #endif
157 private const struct termcapstr {
158 const char *name;
159 const char *long_name;
160 } tstr[] = {
161 #define T_al 0
162 { "al", "add new blank line" },
163 #define T_bl 1
164 { "bl", "audible bell" },
165 #define T_cd 2
166 { "cd", "clear to bottom" },
167 #define T_ce 3
168 { "ce", "clear to end of line" },
169 #define T_ch 4
170 { "ch", "cursor to horiz pos" },
171 #define T_cl 5
172 { "cl", "clear screen" },
173 #define T_dc 6
174 { "dc", "delete a character" },
175 #define T_dl 7
176 { "dl", "delete a line" },
177 #define T_dm 8
178 { "dm", "start delete mode" },
179 #define T_ed 9
180 { "ed", "end delete mode" },
181 #define T_ei 10
182 { "ei", "end insert mode" },
183 #define T_fs 11
184 { "fs", "cursor from status line" },
185 #define T_ho 12
186 { "ho", "home cursor" },
187 #define T_ic 13
188 { "ic", "insert character" },
189 #define T_im 14
190 { "im", "start insert mode" },
191 #define T_ip 15
192 { "ip", "insert padding" },
193 #define T_kd 16
194 { "kd", "sends cursor down" },
195 #define T_kl 17
196 { "kl", "sends cursor left" },
197 #define T_kr 18
198 { "kr", "sends cursor right" },
199 #define T_ku 19
200 { "ku", "sends cursor up" },
201 #define T_md 20
202 { "md", "begin bold" },
203 #define T_me 21
204 { "me", "end attributes" },
205 #define T_nd 22
206 { "nd", "non destructive space" },
207 #define T_se 23
208 { "se", "end standout" },
209 #define T_so 24
210 { "so", "begin standout" },
211 #define T_ts 25
212 { "ts", "cursor to status line" },
213 #define T_up 26
214 { "up", "cursor up one" },
215 #define T_us 27
216 { "us", "begin underline" },
217 #define T_ue 28
218 { "ue", "end underline" },
219 #define T_vb 29
220 { "vb", "visible bell" },
221 #define T_DC 30
222 { "DC", "delete multiple chars" },
223 #define T_DO 31
224 { "DO", "cursor down multiple" },
225 #define T_IC 32
226 { "IC", "insert multiple chars" },
227 #define T_LE 33
228 { "LE", "cursor left multiple" },
229 #define T_RI 34
230 { "RI", "cursor right multiple" },
231 #define T_UP 35
232 { "UP", "cursor up multiple" },
233 #define T_kh 36
234 { "kh", "send cursor home" },
235 #define T_at7 37
236 { "@7", "send cursor end" },
237 #define T_str 38
238 { NULL, NULL }
241 private const struct termcapval {
242 const char *name;
243 const char *long_name;
244 } tval[] = {
245 #define T_am 0
246 { "am", "has automatic margins" },
247 #define T_pt 1
248 { "pt", "has physical tabs" },
249 #define T_li 2
250 { "li", "Number of lines" },
251 #define T_co 3
252 { "co", "Number of columns" },
253 #define T_km 4
254 { "km", "Has meta key" },
255 #define T_xt 5
256 { "xt", "Tab chars destructive" },
257 #define T_xn 6
258 { "xn", "newline ignored at right margin" },
259 #define T_MT 7
260 { "MT", "Has meta key" }, /* XXX? */
261 #define T_val 8
262 { NULL, NULL, }
264 /* do two or more of the attributes use me */
266 private void term_setflags(EditLine *);
267 private int term_rebuffer_display(EditLine *);
268 private void term_free_display(EditLine *);
269 private int term_alloc_display(EditLine *);
270 private void term_alloc(EditLine *, const struct termcapstr *, const char *);
271 private void term_init_arrow(EditLine *);
272 private void term_reset_arrow(EditLine *);
273 private int term_putc(int);
274 private void term_tputs(EditLine *, const char *, int);
276 #ifdef _REENTRANT
277 private pthread_mutex_t term_mutex = PTHREAD_MUTEX_INITIALIZER;
278 #endif
279 private FILE *term_outfile = NULL;
282 /* term_setflags():
283 * Set the terminal capability flags
285 private void
286 term_setflags(EditLine *el)
288 EL_FLAGS = 0;
289 if (el->el_tty.t_tabs)
290 EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
292 EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
293 EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
294 EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
295 EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
296 TERM_CAN_INSERT : 0;
297 EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
298 EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0;
299 EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0;
301 if (GoodStr(T_me) && GoodStr(T_ue))
302 EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ?
303 TERM_CAN_ME : 0;
304 else
305 EL_FLAGS &= ~TERM_CAN_ME;
306 if (GoodStr(T_me) && GoodStr(T_se))
307 EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ?
308 TERM_CAN_ME : 0;
311 #ifdef DEBUG_SCREEN
312 if (!EL_CAN_UP) {
313 (void) fprintf(el->el_errfile,
314 "WARNING: Your terminal cannot move up.\n");
315 (void) fprintf(el->el_errfile,
316 "Editing may be odd for long lines.\n");
318 if (!EL_CAN_CEOL)
319 (void) fprintf(el->el_errfile, "no clear EOL capability.\n");
320 if (!EL_CAN_DELETE)
321 (void) fprintf(el->el_errfile, "no delete char capability.\n");
322 if (!EL_CAN_INSERT)
323 (void) fprintf(el->el_errfile, "no insert char capability.\n");
324 #endif /* DEBUG_SCREEN */
327 /* term_init():
328 * Initialize the terminal stuff
330 protected int
331 term_init(EditLine *el)
334 el->el_term.t_buf = (char *) el_malloc(TC_BUFSIZE);
335 if (el->el_term.t_buf == NULL)
336 return (-1);
337 el->el_term.t_cap = (char *) el_malloc(TC_BUFSIZE);
338 if (el->el_term.t_cap == NULL)
339 return (-1);
340 el->el_term.t_fkey = (fkey_t *) el_malloc(A_K_NKEYS * sizeof(fkey_t));
341 if (el->el_term.t_fkey == NULL)
342 return (-1);
343 el->el_term.t_loc = 0;
344 el->el_term.t_str = (char **) el_malloc(T_str * sizeof(char *));
345 if (el->el_term.t_str == NULL)
346 return (-1);
347 (void) memset(el->el_term.t_str, 0, T_str * sizeof(char *));
348 el->el_term.t_val = (int *) el_malloc(T_val * sizeof(int));
349 if (el->el_term.t_val == NULL)
350 return (-1);
351 (void) memset(el->el_term.t_val, 0, T_val * sizeof(int));
352 (void) term_set(el, NULL);
353 term_init_arrow(el);
354 return (0);
357 /* term_end():
358 * Clean up the terminal stuff
360 protected void
361 term_end(EditLine *el)
364 el_free((ptr_t) el->el_term.t_buf);
365 el->el_term.t_buf = NULL;
366 el_free((ptr_t) el->el_term.t_cap);
367 el->el_term.t_cap = NULL;
368 el->el_term.t_loc = 0;
369 el_free((ptr_t) el->el_term.t_str);
370 el->el_term.t_str = NULL;
371 el_free((ptr_t) el->el_term.t_val);
372 el->el_term.t_val = NULL;
373 el_free((ptr_t) el->el_term.t_fkey);
374 el->el_term.t_fkey = NULL;
375 term_free_display(el);
379 /* term_alloc():
380 * Maintain a string pool for termcap strings
382 private void
383 term_alloc(EditLine *el, const struct termcapstr *t, const char *cap)
385 char termbuf[TC_BUFSIZE];
386 size_t tlen, clen;
387 char **tlist = el->el_term.t_str;
388 char **tmp, **str = &tlist[t - tstr];
390 if (cap == NULL || *cap == '\0') {
391 *str = NULL;
392 return;
393 } else
394 clen = strlen(cap);
396 tlen = *str == NULL ? 0 : strlen(*str);
399 * New string is shorter; no need to allocate space
401 if (clen <= tlen) {
402 if (*str)
403 (void) strlcpy(*str, cap, tlen + 1);
404 return;
407 * New string is longer; see if we have enough space to append
409 if (el->el_term.t_loc + 3 < TC_BUFSIZE) {
410 tlen = TC_BUFSIZE - el->el_term.t_loc;
411 (void) strlcpy(*str = &el->el_term.t_buf[el->el_term.t_loc],
412 cap, tlen);
413 el->el_term.t_loc += (int)clen + 1; /* one for \0 */
414 return;
417 * Compact our buffer; no need to check compaction, cause we know it
418 * fits...
420 tlen = 0;
421 for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
422 if (*tmp != NULL && *tmp != '\0' && *tmp != *str) {
423 char *ptr;
425 for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
426 continue;
427 termbuf[tlen++] = '\0';
429 memcpy(el->el_term.t_buf, termbuf, TC_BUFSIZE);
430 el->el_term.t_loc = (int)tlen;
431 if (el->el_term.t_loc + 3 >= TC_BUFSIZE) {
432 (void) fprintf(el->el_errfile,
433 "Out of termcap string space.\n");
434 return;
436 tlen = TC_BUFSIZE - el->el_term.t_loc;
437 (void) strlcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap, tlen);
438 el->el_term.t_loc += (int)clen + 1; /* one for \0 */
439 return;
443 /* term_rebuffer_display():
444 * Rebuffer the display after the screen changed size
446 private int
447 term_rebuffer_display(EditLine *el)
449 coord_t *c = &el->el_term.t_size;
451 term_free_display(el);
453 c->h = Val(T_co);
454 c->v = Val(T_li);
456 if (term_alloc_display(el) == -1)
457 return (-1);
458 return (0);
462 /* term_alloc_display():
463 * Allocate a new display.
465 private int
466 term_alloc_display(EditLine *el)
468 int i;
469 Char **b;
470 coord_t *c = &el->el_term.t_size;
472 b = el_malloc(sizeof(*b) * (c->v + 1));
473 if (b == NULL)
474 return (-1);
475 for (i = 0; i < c->v; i++) {
476 b[i] = el_malloc(sizeof(**b) * (c->h + 1));
477 if (b[i] == NULL) {
478 while (--i >= 0)
479 el_free((ptr_t) b[i]);
480 el_free((ptr_t) b);
481 return (-1);
484 b[c->v] = NULL;
485 el->el_display = b;
487 b = el_malloc(sizeof(*b) * (c->v + 1));
488 if (b == NULL)
489 return (-1);
490 for (i = 0; i < c->v; i++) {
491 b[i] = el_malloc(sizeof(**b) * (c->h + 1));
492 if (b[i] == NULL) {
493 while (--i >= 0)
494 el_free((ptr_t) b[i]);
495 el_free((ptr_t) b);
496 return (-1);
499 b[c->v] = NULL;
500 el->el_vdisplay = b;
501 return (0);
505 /* term_free_display():
506 * Free the display buffers
508 private void
509 term_free_display(EditLine *el)
511 Char **b;
512 Char **bufp;
514 b = el->el_display;
515 el->el_display = NULL;
516 if (b != NULL) {
517 for (bufp = b; *bufp != NULL; bufp++)
518 el_free((ptr_t) *bufp);
519 el_free((ptr_t) b);
521 b = el->el_vdisplay;
522 el->el_vdisplay = NULL;
523 if (b != NULL) {
524 for (bufp = b; *bufp != NULL; bufp++)
525 el_free((ptr_t) *bufp);
526 el_free((ptr_t) b);
531 /* term_move_to_line():
532 * move to line <where> (first line == 0)
533 * as efficiently as possible
535 protected void
536 term_move_to_line(EditLine *el, int where)
538 int del;
540 if (where == el->el_cursor.v)
541 return;
543 if (where > el->el_term.t_size.v) {
544 #ifdef DEBUG_SCREEN
545 (void) fprintf(el->el_errfile,
546 "term_move_to_line: where is ridiculous: %d\r\n", where);
547 #endif /* DEBUG_SCREEN */
548 return;
550 if ((del = where - el->el_cursor.v) > 0) {
551 while (del > 0) {
552 if (EL_HAS_AUTO_MARGINS &&
553 el->el_display[el->el_cursor.v][0] != '\0') {
554 size_t h = el->el_term.t_size.h - 1;
555 #ifdef WIDECHAR
556 for (; h > 0 &&
557 el->el_display[el->el_cursor.v][h] ==
558 MB_FILL_CHAR;
559 h--)
560 continue;
561 #endif
562 /* move without newline */
563 term_move_to_char(el, (int)h);
564 term_overwrite(el, &el->el_display
565 [el->el_cursor.v][el->el_cursor.h],
566 (size_t)(el->el_term.t_size.h -
567 el->el_cursor.h));
568 /* updates Cursor */
569 del--;
570 } else {
571 if ((del > 1) && GoodStr(T_DO)) {
572 term_tputs(el, tgoto(Str(T_DO), del,
573 del), del);
574 del = 0;
575 } else {
576 for (; del > 0; del--)
577 term__putc(el, '\n');
578 /* because the \n will become \r\n */
579 el->el_cursor.h = 0;
583 } else { /* del < 0 */
584 if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
585 term_tputs(el, tgoto(Str(T_UP), -del, -del), -del);
586 else {
587 if (GoodStr(T_up))
588 for (; del < 0; del++)
589 term_tputs(el, Str(T_up), 1);
592 el->el_cursor.v = where;/* now where is here */
596 /* term_move_to_char():
597 * Move to the character position specified
599 protected void
600 term_move_to_char(EditLine *el, int where)
602 int del, i;
604 mc_again:
605 if (where == el->el_cursor.h)
606 return;
608 if (where > el->el_term.t_size.h) {
609 #ifdef DEBUG_SCREEN
610 (void) fprintf(el->el_errfile,
611 "term_move_to_char: where is riduculous: %d\r\n", where);
612 #endif /* DEBUG_SCREEN */
613 return;
615 if (!where) { /* if where is first column */
616 term__putc(el, '\r'); /* do a CR */
617 el->el_cursor.h = 0;
618 return;
620 del = where - el->el_cursor.h;
622 if ((del < -4 || del > 4) && GoodStr(T_ch))
623 /* go there directly */
624 term_tputs(el, tgoto(Str(T_ch), where, where), where);
625 else {
626 if (del > 0) { /* moving forward */
627 if ((del > 4) && GoodStr(T_RI))
628 term_tputs(el, tgoto(Str(T_RI), del, del), del);
629 else {
630 /* if I can do tabs, use them */
631 if (EL_CAN_TAB) {
632 if ((el->el_cursor.h & 0370) !=
633 (where & ~0x7)
634 #ifdef WIDECHAR
635 && (el->el_display[
636 el->el_cursor.v][where & 0370] !=
637 MB_FILL_CHAR)
638 #endif
640 /* if not within tab stop */
641 for (i =
642 (el->el_cursor.h & 0370);
643 i < (where & ~0x7);
644 i += 8)
645 term__putc(el, '\t');
646 /* then tab over */
647 el->el_cursor.h = where & ~0x7;
651 * it's usually cheaper to just write the
652 * chars, so we do.
655 * NOTE THAT term_overwrite() WILL CHANGE
656 * el->el_cursor.h!!!
658 term_overwrite(el, &el->el_display[
659 el->el_cursor.v][el->el_cursor.h],
660 (size_t)(where - el->el_cursor.h));
663 } else { /* del < 0 := moving backward */
664 if ((-del > 4) && GoodStr(T_LE))
665 term_tputs(el, tgoto(Str(T_LE), -del, -del),
666 -del);
667 else { /* can't go directly there */
669 * if the "cost" is greater than the "cost"
670 * from col 0
672 if (EL_CAN_TAB ?
673 ((unsigned int)-del >
674 (((unsigned int) where >> 3) +
675 (where & 07)))
676 : (-del > where)) {
677 term__putc(el, '\r'); /* do a CR */
678 el->el_cursor.h = 0;
679 goto mc_again; /* and try again */
681 for (i = 0; i < -del; i++)
682 term__putc(el, '\b');
686 el->el_cursor.h = where; /* now where is here */
690 /* term_overwrite():
691 * Overstrike num characters
692 * Assumes MB_FILL_CHARs are present to keep the column count correct
694 protected void
695 term_overwrite(EditLine *el, const Char *cp, size_t n)
697 if (n == 0)
698 return;
700 if (n > (size_t)el->el_term.t_size.h) {
701 #ifdef DEBUG_SCREEN
702 (void) fprintf(el->el_errfile,
703 "term_overwrite: n is riduculous: %d\r\n", n);
704 #endif /* DEBUG_SCREEN */
705 return;
708 do {
709 /* term__putc() ignores any MB_FILL_CHARs */
710 term__putc(el, *cp++);
711 el->el_cursor.h++;
712 } while (--n);
714 if (el->el_cursor.h >= el->el_term.t_size.h) { /* wrap? */
715 if (EL_HAS_AUTO_MARGINS) { /* yes */
716 el->el_cursor.h = 0;
717 el->el_cursor.v++;
718 if (EL_HAS_MAGIC_MARGINS) {
719 /* force the wrap to avoid the "magic"
720 * situation */
721 Char c;
722 if ((c = el->el_display[el->el_cursor.v]
723 [el->el_cursor.h]) != '\0') {
724 term_overwrite(el, &c, 1);
725 #ifdef WIDECHAR
726 while (el->el_display[el->el_cursor.v]
727 [el->el_cursor.h] == MB_FILL_CHAR)
728 el->el_cursor.h++;
729 #endif
730 } else {
731 term__putc(el, ' ');
732 el->el_cursor.h = 1;
735 } else /* no wrap, but cursor stays on screen */
736 el->el_cursor.h = el->el_term.t_size.h - 1;
741 /* term_deletechars():
742 * Delete num characters
744 protected void
745 term_deletechars(EditLine *el, int num)
747 if (num <= 0)
748 return;
750 if (!EL_CAN_DELETE) {
751 #ifdef DEBUG_EDIT
752 (void) fprintf(el->el_errfile, " ERROR: cannot delete \n");
753 #endif /* DEBUG_EDIT */
754 return;
756 if (num > el->el_term.t_size.h) {
757 #ifdef DEBUG_SCREEN
758 (void) fprintf(el->el_errfile,
759 "term_deletechars: num is riduculous: %d\r\n", num);
760 #endif /* DEBUG_SCREEN */
761 return;
763 if (GoodStr(T_DC)) /* if I have multiple delete */
764 if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more
765 * expen. */
766 term_tputs(el, tgoto(Str(T_DC), num, num), num);
767 return;
769 if (GoodStr(T_dm)) /* if I have delete mode */
770 term_tputs(el, Str(T_dm), 1);
772 if (GoodStr(T_dc)) /* else do one at a time */
773 while (num--)
774 term_tputs(el, Str(T_dc), 1);
776 if (GoodStr(T_ed)) /* if I have delete mode */
777 term_tputs(el, Str(T_ed), 1);
781 /* term_insertwrite():
782 * Puts terminal in insert character mode or inserts num
783 * characters in the line
784 * Assumes MB_FILL_CHARs are present to keep column count correct
786 protected void
787 term_insertwrite(EditLine *el, Char *cp, int num)
789 if (num <= 0)
790 return;
791 if (!EL_CAN_INSERT) {
792 #ifdef DEBUG_EDIT
793 (void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
794 #endif /* DEBUG_EDIT */
795 return;
797 if (num > el->el_term.t_size.h) {
798 #ifdef DEBUG_SCREEN
799 (void) fprintf(el->el_errfile,
800 "StartInsert: num is riduculous: %d\r\n", num);
801 #endif /* DEBUG_SCREEN */
802 return;
804 if (GoodStr(T_IC)) /* if I have multiple insert */
805 if ((num > 1) || !GoodStr(T_ic)) {
806 /* if ic would be more expensive */
807 term_tputs(el, tgoto(Str(T_IC), num, num), num);
808 term_overwrite(el, cp, (size_t)num);
809 /* this updates el_cursor.h */
810 return;
812 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
813 term_tputs(el, Str(T_im), 1);
815 el->el_cursor.h += num;
817 term__putc(el, *cp++);
818 while (--num);
820 if (GoodStr(T_ip)) /* have to make num chars insert */
821 term_tputs(el, Str(T_ip), 1);
823 term_tputs(el, Str(T_ei), 1);
824 return;
826 do {
827 if (GoodStr(T_ic)) /* have to make num chars insert */
828 term_tputs(el, Str(T_ic), 1);
830 term__putc(el, *cp++);
832 el->el_cursor.h++;
834 if (GoodStr(T_ip)) /* have to make num chars insert */
835 term_tputs(el, Str(T_ip), 1);
836 /* pad the inserted char */
838 } while (--num);
842 /* term_clear_EOL():
843 * clear to end of line. There are num characters to clear
845 protected void
846 term_clear_EOL(EditLine *el, int num)
848 int i;
850 if (EL_CAN_CEOL && GoodStr(T_ce))
851 term_tputs(el, Str(T_ce), 1);
852 else {
853 for (i = 0; i < num; i++)
854 term__putc(el, ' ');
855 el->el_cursor.h += num; /* have written num spaces */
860 /* term_clear_screen():
861 * Clear the screen
863 protected void
864 term_clear_screen(EditLine *el)
865 { /* clear the whole screen and home */
867 if (GoodStr(T_cl))
868 /* send the clear screen code */
869 term_tputs(el, Str(T_cl), Val(T_li));
870 else if (GoodStr(T_ho) && GoodStr(T_cd)) {
871 term_tputs(el, Str(T_ho), Val(T_li)); /* home */
872 /* clear to bottom of screen */
873 term_tputs(el, Str(T_cd), Val(T_li));
874 } else {
875 term__putc(el, '\r');
876 term__putc(el, '\n');
881 /* term_beep():
882 * Beep the way the terminal wants us
884 protected void
885 term_beep(EditLine *el)
887 if (GoodStr(T_bl))
888 /* what termcap says we should use */
889 term_tputs(el, Str(T_bl), 1);
890 else
891 term__putc(el, '\007'); /* an ASCII bell; ^G */
895 #ifdef notdef
896 /* term_clear_to_bottom():
897 * Clear to the bottom of the screen
899 protected void
900 term_clear_to_bottom(EditLine *el)
902 if (GoodStr(T_cd))
903 term_tputs(el, Str(T_cd), Val(T_li));
904 else if (GoodStr(T_ce))
905 term_tputs(el, Str(T_ce), Val(T_li));
907 #endif
909 protected void
910 term_get(EditLine *el, const char **term)
912 *term = el->el_term.t_name;
916 /* term_set():
917 * Read in the terminal capabilities from the requested terminal
919 protected int
920 term_set(EditLine *el, const char *term)
922 int i;
923 char buf[TC_BUFSIZE];
924 char *area;
925 const struct termcapstr *t;
926 sigset_t oset, nset;
927 int lins, cols;
929 (void) sigemptyset(&nset);
930 (void) sigaddset(&nset, SIGWINCH);
931 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
933 area = buf;
936 if (term == NULL)
937 term = getenv("TERM");
939 if (!term || !term[0])
940 term = "dumb";
942 if (strcmp(term, "emacs") == 0)
943 el->el_flags |= EDIT_DISABLED;
945 memset(el->el_term.t_cap, 0, TC_BUFSIZE);
947 i = tgetent(el->el_term.t_cap, term);
949 if (i <= 0) {
950 if (i == -1)
951 (void) fprintf(el->el_errfile,
952 "Cannot read termcap database;\n");
953 else if (i == 0)
954 (void) fprintf(el->el_errfile,
955 "No entry for terminal type \"%s\";\n", term);
956 (void) fprintf(el->el_errfile,
957 "using dumb terminal settings.\n");
958 Val(T_co) = 80; /* do a dumb terminal */
959 Val(T_pt) = Val(T_km) = Val(T_li) = 0;
960 Val(T_xt) = Val(T_MT);
961 for (t = tstr; t->name != NULL; t++)
962 term_alloc(el, t, NULL);
963 } else {
964 /* auto/magic margins */
965 Val(T_am) = tgetflag("am");
966 Val(T_xn) = tgetflag("xn");
967 /* Can we tab */
968 Val(T_pt) = tgetflag("pt");
969 Val(T_xt) = tgetflag("xt");
970 /* do we have a meta? */
971 Val(T_km) = tgetflag("km");
972 Val(T_MT) = tgetflag("MT");
973 /* Get the size */
974 Val(T_co) = tgetnum("co");
975 Val(T_li) = tgetnum("li");
976 for (t = tstr; t->name != NULL; t++) {
977 /* XXX: some systems' tgetstr needs non const */
978 term_alloc(el, t, tgetstr(strchr(t->name, *t->name),
979 &area));
983 if (Val(T_co) < 2)
984 Val(T_co) = 80; /* just in case */
985 if (Val(T_li) < 1)
986 Val(T_li) = 24;
988 el->el_term.t_size.v = Val(T_co);
989 el->el_term.t_size.h = Val(T_li);
991 term_setflags(el);
993 /* get the correct window size */
994 (void) term_get_size(el, &lins, &cols);
995 if (term_change_size(el, lins, cols) == -1)
996 return (-1);
997 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
998 term_bind_arrow(el);
999 el->el_term.t_name = term;
1000 return (i <= 0 ? -1 : 0);
1004 /* term_get_size():
1005 * Return the new window size in lines and cols, and
1006 * true if the size was changed.
1008 protected int
1009 term_get_size(EditLine *el, int *lins, int *cols)
1012 *cols = Val(T_co);
1013 *lins = Val(T_li);
1015 #ifdef TIOCGWINSZ
1017 struct winsize ws;
1018 if (ioctl(el->el_infd, TIOCGWINSZ, (ioctl_t) & ws) != -1) {
1019 if (ws.ws_col)
1020 *cols = ws.ws_col;
1021 if (ws.ws_row)
1022 *lins = ws.ws_row;
1025 #endif
1026 #ifdef TIOCGSIZE
1028 struct ttysize ts;
1029 if (ioctl(el->el_infd, TIOCGSIZE, (ioctl_t) & ts) != -1) {
1030 if (ts.ts_cols)
1031 *cols = ts.ts_cols;
1032 if (ts.ts_lines)
1033 *lins = ts.ts_lines;
1036 #endif
1037 return (Val(T_co) != *cols || Val(T_li) != *lins);
1041 /* term_change_size():
1042 * Change the size of the terminal
1044 protected int
1045 term_change_size(EditLine *el, int lins, int cols)
1048 * Just in case
1050 Val(T_co) = (cols < 2) ? 80 : cols;
1051 Val(T_li) = (lins < 1) ? 24 : lins;
1053 /* re-make display buffers */
1054 if (term_rebuffer_display(el) == -1)
1055 return (-1);
1056 re_clear_display(el);
1057 return (0);
1061 /* term_init_arrow():
1062 * Initialize the arrow key bindings from termcap
1064 private void
1065 term_init_arrow(EditLine *el)
1067 fkey_t *arrow = el->el_term.t_fkey;
1069 arrow[A_K_DN].name = STR("down");
1070 arrow[A_K_DN].key = T_kd;
1071 arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
1072 arrow[A_K_DN].type = XK_CMD;
1074 arrow[A_K_UP].name = STR("up");
1075 arrow[A_K_UP].key = T_ku;
1076 arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
1077 arrow[A_K_UP].type = XK_CMD;
1079 arrow[A_K_LT].name = STR("left");
1080 arrow[A_K_LT].key = T_kl;
1081 arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
1082 arrow[A_K_LT].type = XK_CMD;
1084 arrow[A_K_RT].name = STR("right");
1085 arrow[A_K_RT].key = T_kr;
1086 arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
1087 arrow[A_K_RT].type = XK_CMD;
1089 arrow[A_K_HO].name = STR("home");
1090 arrow[A_K_HO].key = T_kh;
1091 arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
1092 arrow[A_K_HO].type = XK_CMD;
1094 arrow[A_K_EN].name = STR("end");
1095 arrow[A_K_EN].key = T_at7;
1096 arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
1097 arrow[A_K_EN].type = XK_CMD;
1101 /* term_reset_arrow():
1102 * Reset arrow key bindings
1104 private void
1105 term_reset_arrow(EditLine *el)
1107 fkey_t *arrow = el->el_term.t_fkey;
1108 static const Char strA[] = {033, '[', 'A', '\0'};
1109 static const Char strB[] = {033, '[', 'B', '\0'};
1110 static const Char strC[] = {033, '[', 'C', '\0'};
1111 static const Char strD[] = {033, '[', 'D', '\0'};
1112 static const Char strH[] = {033, '[', 'H', '\0'};
1113 static const Char strF[] = {033, '[', 'F', '\0'};
1114 static const Char stOA[] = {033, 'O', 'A', '\0'};
1115 static const Char stOB[] = {033, 'O', 'B', '\0'};
1116 static const Char stOC[] = {033, 'O', 'C', '\0'};
1117 static const Char stOD[] = {033, 'O', 'D', '\0'};
1118 static const Char stOH[] = {033, 'O', 'H', '\0'};
1119 static const Char stOF[] = {033, 'O', 'F', '\0'};
1121 key_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1122 key_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1123 key_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1124 key_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1125 key_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1126 key_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1127 key_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1128 key_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1129 key_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1130 key_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1131 key_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1132 key_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1134 if (el->el_map.type == MAP_VI) {
1135 key_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1136 key_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1137 key_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1138 key_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1139 key_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1140 key_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1141 key_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1142 key_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1143 key_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1144 key_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1145 key_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1146 key_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1151 /* term_set_arrow():
1152 * Set an arrow key binding
1154 protected int
1155 term_set_arrow(EditLine *el, const Char *name, key_value_t *fun, int type)
1157 fkey_t *arrow = el->el_term.t_fkey;
1158 int i;
1160 for (i = 0; i < A_K_NKEYS; i++)
1161 if (Strcmp(name, arrow[i].name) == 0) {
1162 arrow[i].fun = *fun;
1163 arrow[i].type = type;
1164 return (0);
1166 return (-1);
1170 /* term_clear_arrow():
1171 * Clear an arrow key binding
1173 protected int
1174 term_clear_arrow(EditLine *el, const Char *name)
1176 fkey_t *arrow = el->el_term.t_fkey;
1177 int i;
1179 for (i = 0; i < A_K_NKEYS; i++)
1180 if (Strcmp(name, arrow[i].name) == 0) {
1181 arrow[i].type = XK_NOD;
1182 return (0);
1184 return (-1);
1188 /* term_print_arrow():
1189 * Print the arrow key bindings
1191 protected void
1192 term_print_arrow(EditLine *el, const Char *name)
1194 int i;
1195 fkey_t *arrow = el->el_term.t_fkey;
1197 for (i = 0; i < A_K_NKEYS; i++)
1198 if (*name == '\0' || Strcmp(name, arrow[i].name) == 0)
1199 if (arrow[i].type != XK_NOD)
1200 key_kprint(el, arrow[i].name, &arrow[i].fun,
1201 arrow[i].type);
1205 /* term_bind_arrow():
1206 * Bind the arrow keys
1208 protected void
1209 term_bind_arrow(EditLine *el)
1211 el_action_t *map;
1212 const el_action_t *dmap;
1213 int i, j;
1214 char *p;
1215 fkey_t *arrow = el->el_term.t_fkey;
1217 /* Check if the components needed are initialized */
1218 if (el->el_term.t_buf == NULL || el->el_map.key == NULL)
1219 return;
1221 map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
1222 dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
1224 term_reset_arrow(el);
1226 for (i = 0; i < A_K_NKEYS; i++) {
1227 Char wt_str[VISUAL_WIDTH_MAX];
1228 Char *px;
1229 size_t n;
1231 p = el->el_term.t_str[arrow[i].key];
1232 if (!p || !*p)
1233 continue;
1234 for (n = 0; n < VISUAL_WIDTH_MAX && p[n]; ++n)
1235 wt_str[n] = p[n];
1236 while (n < VISUAL_WIDTH_MAX)
1237 wt_str[n++] = '\0';
1238 px = wt_str;
1239 j = (unsigned char) *p;
1241 * Assign the arrow keys only if:
1243 * 1. They are multi-character arrow keys and the user
1244 * has not re-assigned the leading character, or
1245 * has re-assigned the leading character to be
1246 * ED_SEQUENCE_LEAD_IN
1247 * 2. They are single arrow keys pointing to an
1248 * unassigned key.
1250 if (arrow[i].type == XK_NOD)
1251 key_clear(el, map, px);
1252 else {
1253 if (p[1] && (dmap[j] == map[j] ||
1254 map[j] == ED_SEQUENCE_LEAD_IN)) {
1255 key_add(el, px, &arrow[i].fun,
1256 arrow[i].type);
1257 map[j] = ED_SEQUENCE_LEAD_IN;
1258 } else if (map[j] == ED_UNASSIGNED) {
1259 key_clear(el, map, px);
1260 if (arrow[i].type == XK_CMD)
1261 map[j] = arrow[i].fun.cmd;
1262 else
1263 key_add(el, px, &arrow[i].fun,
1264 arrow[i].type);
1270 /* term_putc():
1271 * Add a character
1273 private int
1274 term_putc(int c)
1276 if (term_outfile == NULL)
1277 return -1;
1278 return fputc(c, term_outfile);
1281 private void
1282 term_tputs(EditLine *el, const char *cap, int affcnt)
1284 #ifdef _REENTRANT
1285 pthread_mutex_lock(&term_mutex);
1286 #endif
1287 term_outfile = el->el_outfile;
1288 (void)tputs(cap, affcnt, term_putc);
1289 #ifdef _REENTRANT
1290 pthread_mutex_unlock(&term_mutex);
1291 #endif
1294 /* term__putc():
1295 * Add a character
1297 protected int
1298 term__putc(EditLine *el, Int c)
1300 char buf[MB_LEN_MAX +1];
1301 ssize_t i;
1302 if (c == MB_FILL_CHAR)
1303 return 0;
1304 i = ct_encode_char(buf, MB_LEN_MAX, c);
1305 if (i <= 0)
1306 return (int)i;
1307 buf[i] = '\0';
1308 return fputs(buf, el->el_outfile);
1311 /* term__flush():
1312 * Flush output
1314 protected void
1315 term__flush(EditLine *el)
1318 (void) fflush(el->el_outfile);
1321 /* term_writec():
1322 * Write the given character out, in a human readable form
1324 protected void
1325 term_writec(EditLine *el, Int c)
1327 Char visbuf[VISUAL_WIDTH_MAX +1];
1328 ssize_t vcnt = ct_visual_char(visbuf, VISUAL_WIDTH_MAX, c);
1329 visbuf[vcnt] = '\0';
1330 term_overwrite(el, visbuf, (size_t)vcnt);
1331 term__flush(el);
1335 /* term_telltc():
1336 * Print the current termcap characteristics
1338 protected int
1339 /*ARGSUSED*/
1340 term_telltc(EditLine *el, int argc __attribute__((__unused__)),
1341 const Char **argv __attribute__((__unused__)))
1343 const struct termcapstr *t;
1344 char **ts;
1346 (void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
1347 (void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
1348 (void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
1349 Val(T_co), Val(T_li));
1350 (void) fprintf(el->el_outfile,
1351 "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
1352 (void) fprintf(el->el_outfile,
1353 "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
1354 (void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
1355 EL_HAS_AUTO_MARGINS ? "has" : "does not have");
1356 if (EL_HAS_AUTO_MARGINS)
1357 (void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
1358 EL_HAS_MAGIC_MARGINS ? "has" : "does not have");
1360 for (t = tstr, ts = el->el_term.t_str; t->name != NULL; t++, ts++) {
1361 const char *ub;
1362 if (*ts && **ts) {
1363 ub = ct_encode_string(ct_visual_string(
1364 ct_decode_string(*ts, &el->el_scratch)),
1365 &el->el_scratch);
1366 } else {
1367 ub = "(empty)";
1369 (void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n",
1370 t->long_name, t->name, ub);
1372 (void) fputc('\n', el->el_outfile);
1373 return (0);
1377 /* term_settc():
1378 * Change the current terminal characteristics
1380 protected int
1381 /*ARGSUSED*/
1382 term_settc(EditLine *el, int argc __attribute__((__unused__)),
1383 const Char **argv)
1385 const struct termcapstr *ts;
1386 const struct termcapval *tv;
1387 char what[8], how[8];
1389 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1390 return -1;
1392 strncpy(what, ct_encode_string(argv[1], &el->el_scratch), sizeof(what));
1393 what[sizeof(what) - 1] = '\0';
1394 strncpy(how, ct_encode_string(argv[2], &el->el_scratch), sizeof(how));
1395 how[sizeof(how) - 1] = '\0';
1398 * Do the strings first
1400 for (ts = tstr; ts->name != NULL; ts++)
1401 if (strcmp(ts->name, what) == 0)
1402 break;
1404 if (ts->name != NULL) {
1405 term_alloc(el, ts, how);
1406 term_setflags(el);
1407 return 0;
1410 * Do the numeric ones second
1412 for (tv = tval; tv->name != NULL; tv++)
1413 if (strcmp(tv->name, what) == 0)
1414 break;
1416 if (tv->name != NULL)
1417 return -1;
1419 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1420 tv == &tval[T_am] || tv == &tval[T_xn]) {
1421 if (strcmp(how, "yes") == 0)
1422 el->el_term.t_val[tv - tval] = 1;
1423 else if (strcmp(how, "no") == 0)
1424 el->el_term.t_val[tv - tval] = 0;
1425 else {
1426 (void) fprintf(el->el_errfile,
1427 "" FSTR ": Bad value `%s'.\n", argv[0], how);
1428 return -1;
1430 term_setflags(el);
1431 if (term_change_size(el, Val(T_li), Val(T_co)) == -1)
1432 return -1;
1433 return 0;
1434 } else {
1435 long i;
1436 char *ep;
1438 i = strtol(how, &ep, 10);
1439 if (*ep != '\0') {
1440 (void) fprintf(el->el_errfile,
1441 "" FSTR ": Bad value `%s'.\n", argv[0], how);
1442 return -1;
1444 el->el_term.t_val[tv - tval] = (int) i;
1445 el->el_term.t_size.v = Val(T_co);
1446 el->el_term.t_size.h = Val(T_li);
1447 if (tv == &tval[T_co] || tv == &tval[T_li])
1448 if (term_change_size(el, Val(T_li), Val(T_co))
1449 == -1)
1450 return -1;
1451 return 0;
1456 /* term_gettc():
1457 * Get the current terminal characteristics
1459 protected int
1460 /*ARGSUSED*/
1461 term_gettc(EditLine *el, int argc __attribute__((__unused__)), char **argv)
1463 const struct termcapstr *ts;
1464 const struct termcapval *tv;
1465 char *what;
1466 void *how;
1468 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1469 return (-1);
1471 what = argv[1];
1472 how = argv[2];
1475 * Do the strings first
1477 for (ts = tstr; ts->name != NULL; ts++)
1478 if (strcmp(ts->name, what) == 0)
1479 break;
1481 if (ts->name != NULL) {
1482 *(char **)how = el->el_term.t_str[ts - tstr];
1483 return 0;
1486 * Do the numeric ones second
1488 for (tv = tval; tv->name != NULL; tv++)
1489 if (strcmp(tv->name, what) == 0)
1490 break;
1492 if (tv->name == NULL)
1493 return -1;
1495 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1496 tv == &tval[T_am] || tv == &tval[T_xn]) {
1497 static char yes[] = "yes";
1498 static char no[] = "no";
1499 if (el->el_term.t_val[tv - tval])
1500 *(char **)how = yes;
1501 else
1502 *(char **)how = no;
1503 return 0;
1504 } else {
1505 *(int *)how = el->el_term.t_val[tv - tval];
1506 return 0;
1510 /* term_echotc():
1511 * Print the termcap string out with variable substitution
1513 protected int
1514 /*ARGSUSED*/
1515 term_echotc(EditLine *el, int argc __attribute__((__unused__)),
1516 const Char **argv)
1518 char *cap, *scap;
1519 Char *ep;
1520 int arg_need, arg_cols, arg_rows;
1521 int verbose = 0, silent = 0;
1522 char *area;
1523 static const char fmts[] = "%s\n", fmtd[] = "%d\n";
1524 const struct termcapstr *t;
1525 char buf[TC_BUFSIZE];
1526 long i;
1528 area = buf;
1530 if (argv == NULL || argv[1] == NULL)
1531 return (-1);
1532 argv++;
1534 if (argv[0][0] == '-') {
1535 switch (argv[0][1]) {
1536 case 'v':
1537 verbose = 1;
1538 break;
1539 case 's':
1540 silent = 1;
1541 break;
1542 default:
1543 /* stderror(ERR_NAME | ERR_TCUSAGE); */
1544 break;
1546 argv++;
1548 if (!*argv || *argv[0] == '\0')
1549 return (0);
1550 if (Strcmp(*argv, STR("tabs")) == 0) {
1551 (void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
1552 return (0);
1553 } else if (Strcmp(*argv, STR("meta")) == 0) {
1554 (void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
1555 return (0);
1556 } else if (Strcmp(*argv, STR("xn")) == 0) {
1557 (void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ?
1558 "yes" : "no");
1559 return (0);
1560 } else if (Strcmp(*argv, STR("am")) == 0) {
1561 (void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ?
1562 "yes" : "no");
1563 return (0);
1564 } else if (Strcmp(*argv, STR("baud")) == 0) {
1565 #ifdef notdef
1566 int i;
1568 for (i = 0; baud_rate[i].b_name != NULL; i++)
1569 if (el->el_tty.t_speed == baud_rate[i].b_rate) {
1570 (void) fprintf(el->el_outfile, fmts,
1571 baud_rate[i].b_name);
1572 return (0);
1574 (void) fprintf(el->el_outfile, fmtd, 0);
1575 #else
1576 (void) fprintf(el->el_outfile, fmtd, (int)el->el_tty.t_speed);
1577 #endif
1578 return (0);
1579 } else if (Strcmp(*argv, STR("rows")) == 0 ||
1580 Strcmp(*argv, STR("lines")) == 0) {
1581 (void) fprintf(el->el_outfile, fmtd, Val(T_li));
1582 return (0);
1583 } else if (Strcmp(*argv, STR("cols")) == 0) {
1584 (void) fprintf(el->el_outfile, fmtd, Val(T_co));
1585 return (0);
1588 * Try to use our local definition first
1590 scap = NULL;
1591 for (t = tstr; t->name != NULL; t++)
1592 if (strcmp(t->name,
1593 ct_encode_string(*argv, &el->el_scratch)) == 0) {
1594 scap = el->el_term.t_str[t - tstr];
1595 break;
1597 if (t->name == NULL) {
1598 /* XXX: some systems' tgetstr needs non const */
1599 scap = tgetstr(ct_encode_string(*argv, &el->el_scratch), &area);
1601 if (!scap || scap[0] == '\0') {
1602 if (!silent)
1603 (void) fprintf(el->el_errfile,
1604 "echotc: Termcap parameter `" FSTR "' not found.\n",
1605 *argv);
1606 return (-1);
1609 * Count home many values we need for this capability.
1611 for (cap = scap, arg_need = 0; *cap; cap++)
1612 if (*cap == '%')
1613 switch (*++cap) {
1614 case 'd':
1615 case '2':
1616 case '3':
1617 case '.':
1618 case '+':
1619 arg_need++;
1620 break;
1621 case '%':
1622 case '>':
1623 case 'i':
1624 case 'r':
1625 case 'n':
1626 case 'B':
1627 case 'D':
1628 break;
1629 default:
1631 * hpux has lot's of them...
1633 if (verbose)
1634 (void) fprintf(el->el_errfile,
1635 "echotc: Warning: unknown termcap %% `%c'.\n",
1636 *cap);
1637 /* This is bad, but I won't complain */
1638 break;
1641 switch (arg_need) {
1642 case 0:
1643 argv++;
1644 if (*argv && *argv[0]) {
1645 if (!silent)
1646 (void) fprintf(el->el_errfile,
1647 "echotc: Warning: Extra argument `" FSTR "'.\n",
1648 *argv);
1649 return (-1);
1651 term_tputs(el, scap, 1);
1652 break;
1653 case 1:
1654 argv++;
1655 if (!*argv || *argv[0] == '\0') {
1656 if (!silent)
1657 (void) fprintf(el->el_errfile,
1658 "echotc: Warning: Missing argument.\n");
1659 return (-1);
1661 arg_cols = 0;
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);
1670 arg_rows = (int) i;
1671 argv++;
1672 if (*argv && *argv[0]) {
1673 if (!silent)
1674 (void) fprintf(el->el_errfile,
1675 "echotc: Warning: Extra argument `" FSTR "'.\n",
1676 *argv);
1677 return (-1);
1679 term_tputs(el, tgoto(scap, arg_cols, arg_rows), 1);
1680 break;
1681 default:
1682 /* This is wrong, but I will ignore it... */
1683 if (verbose)
1684 (void) fprintf(el->el_errfile,
1685 "echotc: Warning: Too many required arguments (%d).\n",
1686 arg_need);
1687 /* FALLTHROUGH */
1688 case 2:
1689 argv++;
1690 if (!*argv || *argv[0] == '\0') {
1691 if (!silent)
1692 (void) fprintf(el->el_errfile,
1693 "echotc: Warning: Missing argument.\n");
1694 return (-1);
1696 i = Strtol(*argv, &ep, 10);
1697 if (*ep != '\0' || i < 0) {
1698 if (!silent)
1699 (void) fprintf(el->el_errfile,
1700 "echotc: Bad value `" FSTR "' for cols.\n",
1701 *argv);
1702 return (-1);
1704 arg_cols = (int) i;
1705 argv++;
1706 if (!*argv || *argv[0] == '\0') {
1707 if (!silent)
1708 (void) fprintf(el->el_errfile,
1709 "echotc: Warning: Missing argument.\n");
1710 return (-1);
1712 i = Strtol(*argv, &ep, 10);
1713 if (*ep != '\0' || i < 0) {
1714 if (!silent)
1715 (void) fprintf(el->el_errfile,
1716 "echotc: Bad value `" FSTR "' for rows.\n",
1717 *argv);
1718 return (-1);
1720 arg_rows = (int) i;
1721 if (*ep != '\0') {
1722 if (!silent)
1723 (void) fprintf(el->el_errfile,
1724 "echotc: Bad value `" FSTR "'.\n", *argv);
1725 return (-1);
1727 argv++;
1728 if (*argv && *argv[0]) {
1729 if (!silent)
1730 (void) fprintf(el->el_errfile,
1731 "echotc: Warning: Extra argument `" FSTR "'.\n",
1732 *argv);
1733 return (-1);
1735 term_tputs(el, tgoto(scap, arg_cols, arg_rows), arg_rows);
1736 break;
1738 return (0);