Make Dt match installed file name.
[netbsd-mini2440.git] / lib / libedit / term.c
blobc4ea6dd38ea886faa2ca0708001c67539e344bed
1 /* $NetBSD: term.c,v 1.54 2009/08/30 15:41:05 christos Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
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.
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.
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: term.c,v 1.54 2009/08/30 15:41:05 christos Exp $");
41 #endif
42 #endif /* not lint && not SCCSID */
45 * term.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!
49 #include <stdio.h>
50 #include <signal.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #ifdef HAVE_TERMCAP_H
55 #include <termcap.h>
56 #endif
57 #ifdef HAVE_CURSES_H
58 #include <curses.h>
59 #elif HAVE_NCURSES_H
60 #include <ncurses.h>
61 #endif
62 /* Solaris's term.h does horrid things. */
64 #if defined(HAVE_TERM_H) && !defined(__sun)
65 #include <term.h>
66 #endif
67 #include <sys/types.h>
68 #include <sys/ioctl.h>
70 #ifdef _REENTRANT
71 #include <pthread.h>
72 #endif
74 #include "el.h"
77 * IMPORTANT NOTE: these routines are allowed to look at the current screen
78 * and the current possition assuming that it is correct. If this is not
79 * true, then the update will be WRONG! This is (should be) a valid
80 * assumption...
83 #define TC_BUFSIZE 2048
85 #define GoodStr(a) (el->el_term.t_str[a] != NULL && \
86 el->el_term.t_str[a][0] != '\0')
87 #define Str(a) el->el_term.t_str[a]
88 #define Val(a) el->el_term.t_val[a]
90 #ifdef notdef
91 private const struct {
92 const char *b_name;
93 int b_rate;
94 } baud_rate[] = {
95 #ifdef B0
96 { "0", B0 },
97 #endif
98 #ifdef B50
99 { "50", B50 },
100 #endif
101 #ifdef B75
102 { "75", B75 },
103 #endif
104 #ifdef B110
105 { "110", B110 },
106 #endif
107 #ifdef B134
108 { "134", B134 },
109 #endif
110 #ifdef B150
111 { "150", B150 },
112 #endif
113 #ifdef B200
114 { "200", B200 },
115 #endif
116 #ifdef B300
117 { "300", B300 },
118 #endif
119 #ifdef B600
120 { "600", B600 },
121 #endif
122 #ifdef B900
123 { "900", B900 },
124 #endif
125 #ifdef B1200
126 { "1200", B1200 },
127 #endif
128 #ifdef B1800
129 { "1800", B1800 },
130 #endif
131 #ifdef B2400
132 { "2400", B2400 },
133 #endif
134 #ifdef B3600
135 { "3600", B3600 },
136 #endif
137 #ifdef B4800
138 { "4800", B4800 },
139 #endif
140 #ifdef B7200
141 { "7200", B7200 },
142 #endif
143 #ifdef B9600
144 { "9600", B9600 },
145 #endif
146 #ifdef EXTA
147 { "19200", EXTA },
148 #endif
149 #ifdef B19200
150 { "19200", B19200 },
151 #endif
152 #ifdef EXTB
153 { "38400", EXTB },
154 #endif
155 #ifdef B38400
156 { "38400", B38400 },
157 #endif
158 { NULL, 0 }
160 #endif
162 private const struct termcapstr {
163 const char *name;
164 const char *long_name;
165 } tstr[] = {
166 #define T_al 0
167 { "al", "add new blank line" },
168 #define T_bl 1
169 { "bl", "audible bell" },
170 #define T_cd 2
171 { "cd", "clear to bottom" },
172 #define T_ce 3
173 { "ce", "clear to end of line" },
174 #define T_ch 4
175 { "ch", "cursor to horiz pos" },
176 #define T_cl 5
177 { "cl", "clear screen" },
178 #define T_dc 6
179 { "dc", "delete a character" },
180 #define T_dl 7
181 { "dl", "delete a line" },
182 #define T_dm 8
183 { "dm", "start delete mode" },
184 #define T_ed 9
185 { "ed", "end delete mode" },
186 #define T_ei 10
187 { "ei", "end insert mode" },
188 #define T_fs 11
189 { "fs", "cursor from status line" },
190 #define T_ho 12
191 { "ho", "home cursor" },
192 #define T_ic 13
193 { "ic", "insert character" },
194 #define T_im 14
195 { "im", "start insert mode" },
196 #define T_ip 15
197 { "ip", "insert padding" },
198 #define T_kd 16
199 { "kd", "sends cursor down" },
200 #define T_kl 17
201 { "kl", "sends cursor left" },
202 #define T_kr 18
203 { "kr", "sends cursor right" },
204 #define T_ku 19
205 { "ku", "sends cursor up" },
206 #define T_md 20
207 { "md", "begin bold" },
208 #define T_me 21
209 { "me", "end attributes" },
210 #define T_nd 22
211 { "nd", "non destructive space" },
212 #define T_se 23
213 { "se", "end standout" },
214 #define T_so 24
215 { "so", "begin standout" },
216 #define T_ts 25
217 { "ts", "cursor to status line" },
218 #define T_up 26
219 { "up", "cursor up one" },
220 #define T_us 27
221 { "us", "begin underline" },
222 #define T_ue 28
223 { "ue", "end underline" },
224 #define T_vb 29
225 { "vb", "visible bell" },
226 #define T_DC 30
227 { "DC", "delete multiple chars" },
228 #define T_DO 31
229 { "DO", "cursor down multiple" },
230 #define T_IC 32
231 { "IC", "insert multiple chars" },
232 #define T_LE 33
233 { "LE", "cursor left multiple" },
234 #define T_RI 34
235 { "RI", "cursor right multiple" },
236 #define T_UP 35
237 { "UP", "cursor up multiple" },
238 #define T_kh 36
239 { "kh", "send cursor home" },
240 #define T_at7 37
241 { "@7", "send cursor end" },
242 #define T_str 38
243 { NULL, NULL }
246 private const struct termcapval {
247 const char *name;
248 const char *long_name;
249 } tval[] = {
250 #define T_am 0
251 { "am", "has automatic margins" },
252 #define T_pt 1
253 { "pt", "has physical tabs" },
254 #define T_li 2
255 { "li", "Number of lines" },
256 #define T_co 3
257 { "co", "Number of columns" },
258 #define T_km 4
259 { "km", "Has meta key" },
260 #define T_xt 5
261 { "xt", "Tab chars destructive" },
262 #define T_xn 6
263 { "xn", "newline ignored at right margin" },
264 #define T_MT 7
265 { "MT", "Has meta key" }, /* XXX? */
266 #define T_val 8
267 { NULL, NULL, }
269 /* do two or more of the attributes use me */
271 private void term_setflags(EditLine *);
272 private int term_rebuffer_display(EditLine *);
273 private void term_free_display(EditLine *);
274 private int term_alloc_display(EditLine *);
275 private void term_alloc(EditLine *, const struct termcapstr *, const char *);
276 private void term_init_arrow(EditLine *);
277 private void term_reset_arrow(EditLine *);
278 private int term_putc(int);
279 private void term_tputs(EditLine *, const char *, int);
281 #ifdef _REENTRANT
282 private pthread_mutex_t term_mutex = PTHREAD_MUTEX_INITIALIZER;
283 #endif
284 private FILE *term_outfile = NULL;
287 /* term_setflags():
288 * Set the terminal capability flags
290 private void
291 term_setflags(EditLine *el)
293 EL_FLAGS = 0;
294 if (el->el_tty.t_tabs)
295 EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
297 EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
298 EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
299 EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
300 EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
301 TERM_CAN_INSERT : 0;
302 EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
303 EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0;
304 EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0;
306 if (GoodStr(T_me) && GoodStr(T_ue))
307 EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ?
308 TERM_CAN_ME : 0;
309 else
310 EL_FLAGS &= ~TERM_CAN_ME;
311 if (GoodStr(T_me) && GoodStr(T_se))
312 EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ?
313 TERM_CAN_ME : 0;
316 #ifdef DEBUG_SCREEN
317 if (!EL_CAN_UP) {
318 (void) fprintf(el->el_errfile,
319 "WARNING: Your terminal cannot move up.\n");
320 (void) fprintf(el->el_errfile,
321 "Editing may be odd for long lines.\n");
323 if (!EL_CAN_CEOL)
324 (void) fprintf(el->el_errfile, "no clear EOL capability.\n");
325 if (!EL_CAN_DELETE)
326 (void) fprintf(el->el_errfile, "no delete char capability.\n");
327 if (!EL_CAN_INSERT)
328 (void) fprintf(el->el_errfile, "no insert char capability.\n");
329 #endif /* DEBUG_SCREEN */
332 /* term_init():
333 * Initialize the terminal stuff
335 protected int
336 term_init(EditLine *el)
339 el->el_term.t_buf = (char *) el_malloc(TC_BUFSIZE);
340 if (el->el_term.t_buf == NULL)
341 return (-1);
342 el->el_term.t_cap = (char *) el_malloc(TC_BUFSIZE);
343 if (el->el_term.t_cap == NULL)
344 return (-1);
345 el->el_term.t_fkey = (fkey_t *) el_malloc(A_K_NKEYS * sizeof(fkey_t));
346 if (el->el_term.t_fkey == NULL)
347 return (-1);
348 el->el_term.t_loc = 0;
349 el->el_term.t_str = (char **) el_malloc(T_str * sizeof(char *));
350 if (el->el_term.t_str == NULL)
351 return (-1);
352 (void) memset(el->el_term.t_str, 0, T_str * sizeof(char *));
353 el->el_term.t_val = (int *) el_malloc(T_val * sizeof(int));
354 if (el->el_term.t_val == NULL)
355 return (-1);
356 (void) memset(el->el_term.t_val, 0, T_val * sizeof(int));
357 (void) term_set(el, NULL);
358 term_init_arrow(el);
359 return (0);
362 /* term_end():
363 * Clean up the terminal stuff
365 protected void
366 term_end(EditLine *el)
369 el_free((ptr_t) el->el_term.t_buf);
370 el->el_term.t_buf = NULL;
371 el_free((ptr_t) el->el_term.t_cap);
372 el->el_term.t_cap = NULL;
373 el->el_term.t_loc = 0;
374 el_free((ptr_t) el->el_term.t_str);
375 el->el_term.t_str = NULL;
376 el_free((ptr_t) el->el_term.t_val);
377 el->el_term.t_val = NULL;
378 el_free((ptr_t) el->el_term.t_fkey);
379 el->el_term.t_fkey = NULL;
380 term_free_display(el);
384 /* term_alloc():
385 * Maintain a string pool for termcap strings
387 private void
388 term_alloc(EditLine *el, const struct termcapstr *t, const char *cap)
390 char termbuf[TC_BUFSIZE];
391 size_t tlen, clen;
392 char **tlist = el->el_term.t_str;
393 char **tmp, **str = &tlist[t - tstr];
395 if (cap == NULL || *cap == '\0') {
396 *str = NULL;
397 return;
398 } else
399 clen = strlen(cap);
401 tlen = *str == NULL ? 0 : strlen(*str);
404 * New string is shorter; no need to allocate space
406 if (clen <= tlen) {
407 if (*str)
408 (void) strcpy(*str, cap); /* XXX strcpy is safe */
409 return;
412 * New string is longer; see if we have enough space to append
414 if (el->el_term.t_loc + 3 < TC_BUFSIZE) {
415 /* XXX strcpy is safe */
416 (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc],
417 cap);
418 el->el_term.t_loc += (int)clen + 1; /* one for \0 */
419 return;
422 * Compact our buffer; no need to check compaction, cause we know it
423 * fits...
425 tlen = 0;
426 for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
427 if (*tmp != NULL && *tmp != '\0' && *tmp != *str) {
428 char *ptr;
430 for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
431 continue;
432 termbuf[tlen++] = '\0';
434 memcpy(el->el_term.t_buf, termbuf, TC_BUFSIZE);
435 el->el_term.t_loc = (int)tlen;
436 if (el->el_term.t_loc + 3 >= TC_BUFSIZE) {
437 (void) fprintf(el->el_errfile,
438 "Out of termcap string space.\n");
439 return;
441 /* XXX strcpy is safe */
442 (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap);
443 el->el_term.t_loc += (int)clen + 1; /* one for \0 */
444 return;
448 /* term_rebuffer_display():
449 * Rebuffer the display after the screen changed size
451 private int
452 term_rebuffer_display(EditLine *el)
454 coord_t *c = &el->el_term.t_size;
456 term_free_display(el);
458 c->h = Val(T_co);
459 c->v = Val(T_li);
461 if (term_alloc_display(el) == -1)
462 return (-1);
463 return (0);
467 /* term_alloc_display():
468 * Allocate a new display.
470 private int
471 term_alloc_display(EditLine *el)
473 int i;
474 char **b;
475 coord_t *c = &el->el_term.t_size;
477 b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
478 if (b == NULL)
479 return (-1);
480 for (i = 0; i < c->v; i++) {
481 b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
482 if (b[i] == NULL) {
483 while (--i >= 0)
484 el_free((ptr_t) b[i]);
485 el_free((ptr_t) b);
486 return (-1);
489 b[c->v] = NULL;
490 el->el_display = b;
492 b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
493 if (b == NULL)
494 return (-1);
495 for (i = 0; i < c->v; i++) {
496 b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
497 if (b[i] == NULL) {
498 while (--i >= 0)
499 el_free((ptr_t) b[i]);
500 el_free((ptr_t) b);
501 return (-1);
504 b[c->v] = NULL;
505 el->el_vdisplay = b;
506 return (0);
510 /* term_free_display():
511 * Free the display buffers
513 private void
514 term_free_display(EditLine *el)
516 char **b;
517 char **bufp;
519 b = el->el_display;
520 el->el_display = NULL;
521 if (b != NULL) {
522 for (bufp = b; *bufp != NULL; bufp++)
523 el_free((ptr_t) * bufp);
524 el_free((ptr_t) b);
526 b = el->el_vdisplay;
527 el->el_vdisplay = NULL;
528 if (b != NULL) {
529 for (bufp = b; *bufp != NULL; bufp++)
530 el_free((ptr_t) * bufp);
531 el_free((ptr_t) b);
536 /* term_move_to_line():
537 * move to line <where> (first line == 0)
538 * as efficiently as possible
540 protected void
541 term_move_to_line(EditLine *el, int where)
543 int del;
545 if (where == el->el_cursor.v)
546 return;
548 if (where > el->el_term.t_size.v) {
549 #ifdef DEBUG_SCREEN
550 (void) fprintf(el->el_errfile,
551 "term_move_to_line: where is ridiculous: %d\r\n", where);
552 #endif /* DEBUG_SCREEN */
553 return;
555 if ((del = where - el->el_cursor.v) > 0) {
556 while (del > 0) {
557 if (EL_HAS_AUTO_MARGINS &&
558 el->el_display[el->el_cursor.v][0] != '\0') {
559 /* move without newline */
560 term_move_to_char(el, el->el_term.t_size.h - 1);
561 term_overwrite(el, &el->el_display
562 [el->el_cursor.v][el->el_cursor.h],
563 (size_t)(el->el_term.t_size.h -
564 el->el_cursor.h));
565 /* updates Cursor */
566 del--;
567 } else {
568 if ((del > 1) && GoodStr(T_DO)) {
569 term_tputs(el, tgoto(Str(T_DO), del,
570 del), del);
571 del = 0;
572 } else {
573 for (; del > 0; del--)
574 term__putc(el, '\n');
575 /* because the \n will become \r\n */
576 el->el_cursor.h = 0;
580 } else { /* del < 0 */
581 if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
582 term_tputs(el, tgoto(Str(T_UP), -del, -del), -del);
583 else {
584 if (GoodStr(T_up))
585 for (; del < 0; del++)
586 term_tputs(el, Str(T_up), 1);
589 el->el_cursor.v = where;/* now where is here */
593 /* term_move_to_char():
594 * Move to the character position specified
596 protected void
597 term_move_to_char(EditLine *el, int where)
599 int del, i;
601 mc_again:
602 if (where == el->el_cursor.h)
603 return;
605 if (where > el->el_term.t_size.h) {
606 #ifdef DEBUG_SCREEN
607 (void) fprintf(el->el_errfile,
608 "term_move_to_char: where is riduculous: %d\r\n", where);
609 #endif /* DEBUG_SCREEN */
610 return;
612 if (!where) { /* if where is first column */
613 term__putc(el, '\r'); /* do a CR */
614 el->el_cursor.h = 0;
615 return;
617 del = where - el->el_cursor.h;
619 if ((del < -4 || del > 4) && GoodStr(T_ch))
620 /* go there directly */
621 term_tputs(el, tgoto(Str(T_ch), where, where), where);
622 else {
623 if (del > 0) { /* moving forward */
624 if ((del > 4) && GoodStr(T_RI))
625 term_tputs(el, tgoto(Str(T_RI), del, del), del);
626 else {
627 /* if I can do tabs, use them */
628 if (EL_CAN_TAB) {
629 if ((el->el_cursor.h & 0370) !=
630 (where & 0370)) {
631 /* if not within tab stop */
632 for (i =
633 (el->el_cursor.h & 0370);
634 i < (where & 0370);
635 i += 8)
636 term__putc(el, '\t');
637 /* then tab over */
638 el->el_cursor.h = where & 0370;
642 * it's usually cheaper to just write the
643 * chars, so we do.
646 * NOTE THAT term_overwrite() WILL CHANGE
647 * el->el_cursor.h!!!
649 term_overwrite(el,
650 &el->el_display[el->el_cursor.v][el->el_cursor.h],
651 (size_t)(where - el->el_cursor.h));
654 } else { /* del < 0 := moving backward */
655 if ((-del > 4) && GoodStr(T_LE))
656 term_tputs(el, tgoto(Str(T_LE), -del, -del),
657 -del);
658 else { /* can't go directly there */
660 * if the "cost" is greater than the "cost"
661 * from col 0
663 if (EL_CAN_TAB ?
664 ((unsigned int)-del >
665 (((unsigned int) where >> 3) +
666 (where & 07)))
667 : (-del > where)) {
668 term__putc(el, '\r'); /* do a CR */
669 el->el_cursor.h = 0;
670 goto mc_again; /* and try again */
672 for (i = 0; i < -del; i++)
673 term__putc(el, '\b');
677 el->el_cursor.h = where; /* now where is here */
681 /* term_overwrite():
682 * Overstrike num characters
684 protected void
685 term_overwrite(EditLine *el, const char *cp, size_t n)
687 if (n == 0)
688 return;
690 if (n > (size_t)el->el_term.t_size.h) {
691 #ifdef DEBUG_SCREEN
692 (void) fprintf(el->el_errfile,
693 "term_overwrite: n is riduculous: %d\r\n", n);
694 #endif /* DEBUG_SCREEN */
695 return;
698 do {
699 term__putc(el, *cp++);
700 el->el_cursor.h++;
701 } while (--n);
703 if (el->el_cursor.h >= el->el_term.t_size.h) { /* wrap? */
704 if (EL_HAS_AUTO_MARGINS) { /* yes */
705 el->el_cursor.h = 0;
706 el->el_cursor.v++;
707 if (EL_HAS_MAGIC_MARGINS) {
708 /* force the wrap to avoid the "magic"
709 * situation */
710 char c;
711 if ((c = el->el_display[el->el_cursor.v][el->el_cursor.h])
712 != '\0')
713 term_overwrite(el, &c, 1);
714 else
715 term__putc(el, ' ');
716 el->el_cursor.h = 1;
718 } else /* no wrap, but cursor stays on screen */
719 el->el_cursor.h = el->el_term.t_size.h - 1;
724 /* term_deletechars():
725 * Delete num characters
727 protected void
728 term_deletechars(EditLine *el, int num)
730 if (num <= 0)
731 return;
733 if (!EL_CAN_DELETE) {
734 #ifdef DEBUG_EDIT
735 (void) fprintf(el->el_errfile, " ERROR: cannot delete \n");
736 #endif /* DEBUG_EDIT */
737 return;
739 if (num > el->el_term.t_size.h) {
740 #ifdef DEBUG_SCREEN
741 (void) fprintf(el->el_errfile,
742 "term_deletechars: num is riduculous: %d\r\n", num);
743 #endif /* DEBUG_SCREEN */
744 return;
746 if (GoodStr(T_DC)) /* if I have multiple delete */
747 if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more
748 * expen. */
749 term_tputs(el, tgoto(Str(T_DC), num, num), num);
750 return;
752 if (GoodStr(T_dm)) /* if I have delete mode */
753 term_tputs(el, Str(T_dm), 1);
755 if (GoodStr(T_dc)) /* else do one at a time */
756 while (num--)
757 term_tputs(el, Str(T_dc), 1);
759 if (GoodStr(T_ed)) /* if I have delete mode */
760 term_tputs(el, Str(T_ed), 1);
764 /* term_insertwrite():
765 * Puts terminal in insert character mode or inserts num
766 * characters in the line
768 protected void
769 term_insertwrite(EditLine *el, char *cp, int num)
771 if (num <= 0)
772 return;
773 if (!EL_CAN_INSERT) {
774 #ifdef DEBUG_EDIT
775 (void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
776 #endif /* DEBUG_EDIT */
777 return;
779 if (num > el->el_term.t_size.h) {
780 #ifdef DEBUG_SCREEN
781 (void) fprintf(el->el_errfile,
782 "StartInsert: num is riduculous: %d\r\n", num);
783 #endif /* DEBUG_SCREEN */
784 return;
786 if (GoodStr(T_IC)) /* if I have multiple insert */
787 if ((num > 1) || !GoodStr(T_ic)) {
788 /* if ic would be more expensive */
789 term_tputs(el, tgoto(Str(T_IC), num, num), num);
790 term_overwrite(el, cp, (size_t)num);
791 /* this updates el_cursor.h */
792 return;
794 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
795 term_tputs(el, Str(T_im), 1);
797 el->el_cursor.h += num;
799 term__putc(el, *cp++);
800 while (--num);
802 if (GoodStr(T_ip)) /* have to make num chars insert */
803 term_tputs(el, Str(T_ip), 1);
805 term_tputs(el, Str(T_ei), 1);
806 return;
808 do {
809 if (GoodStr(T_ic)) /* have to make num chars insert */
810 term_tputs(el, Str(T_ic), 1);
812 term__putc(el, *cp++);
814 el->el_cursor.h++;
816 if (GoodStr(T_ip)) /* have to make num chars insert */
817 term_tputs(el, Str(T_ip), 1);
818 /* pad the inserted char */
820 } while (--num);
824 /* term_clear_EOL():
825 * clear to end of line. There are num characters to clear
827 protected void
828 term_clear_EOL(EditLine *el, int num)
830 int i;
832 if (EL_CAN_CEOL && GoodStr(T_ce))
833 term_tputs(el, Str(T_ce), 1);
834 else {
835 for (i = 0; i < num; i++)
836 term__putc(el, ' ');
837 el->el_cursor.h += num; /* have written num spaces */
842 /* term_clear_screen():
843 * Clear the screen
845 protected void
846 term_clear_screen(EditLine *el)
847 { /* clear the whole screen and home */
849 if (GoodStr(T_cl))
850 /* send the clear screen code */
851 term_tputs(el, Str(T_cl), Val(T_li));
852 else if (GoodStr(T_ho) && GoodStr(T_cd)) {
853 term_tputs(el, Str(T_ho), Val(T_li)); /* home */
854 /* clear to bottom of screen */
855 term_tputs(el, Str(T_cd), Val(T_li));
856 } else {
857 term__putc(el, '\r');
858 term__putc(el, '\n');
863 /* term_beep():
864 * Beep the way the terminal wants us
866 protected void
867 term_beep(EditLine *el)
869 if (GoodStr(T_bl))
870 /* what termcap says we should use */
871 term_tputs(el, Str(T_bl), 1);
872 else
873 term__putc(el, '\007'); /* an ASCII bell; ^G */
877 #ifdef notdef
878 /* term_clear_to_bottom():
879 * Clear to the bottom of the screen
881 protected void
882 term_clear_to_bottom(EditLine *el)
884 if (GoodStr(T_cd))
885 term_tputs(el, Str(T_cd), Val(T_li));
886 else if (GoodStr(T_ce))
887 term_tputs(el, Str(T_ce), Val(T_li));
889 #endif
891 protected void
892 term_get(EditLine *el, const char **term)
894 *term = el->el_term.t_name;
898 /* term_set():
899 * Read in the terminal capabilities from the requested terminal
901 protected int
902 term_set(EditLine *el, const char *term)
904 int i;
905 char buf[TC_BUFSIZE];
906 char *area;
907 const struct termcapstr *t;
908 sigset_t oset, nset;
909 int lins, cols;
911 (void) sigemptyset(&nset);
912 (void) sigaddset(&nset, SIGWINCH);
913 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
915 area = buf;
918 if (term == NULL)
919 term = getenv("TERM");
921 if (!term || !term[0])
922 term = "dumb";
924 if (strcmp(term, "emacs") == 0)
925 el->el_flags |= EDIT_DISABLED;
927 memset(el->el_term.t_cap, 0, TC_BUFSIZE);
929 i = tgetent(el->el_term.t_cap, term);
931 if (i <= 0) {
932 if (i == -1)
933 (void) fprintf(el->el_errfile,
934 "Cannot read termcap database;\n");
935 else if (i == 0)
936 (void) fprintf(el->el_errfile,
937 "No entry for terminal type \"%s\";\n", term);
938 (void) fprintf(el->el_errfile,
939 "using dumb terminal settings.\n");
940 Val(T_co) = 80; /* do a dumb terminal */
941 Val(T_pt) = Val(T_km) = Val(T_li) = 0;
942 Val(T_xt) = Val(T_MT);
943 for (t = tstr; t->name != NULL; t++)
944 term_alloc(el, t, NULL);
945 } else {
946 /* auto/magic margins */
947 Val(T_am) = tgetflag("am");
948 Val(T_xn) = tgetflag("xn");
949 /* Can we tab */
950 Val(T_pt) = tgetflag("pt");
951 Val(T_xt) = tgetflag("xt");
952 /* do we have a meta? */
953 Val(T_km) = tgetflag("km");
954 Val(T_MT) = tgetflag("MT");
955 /* Get the size */
956 Val(T_co) = tgetnum("co");
957 Val(T_li) = tgetnum("li");
958 for (t = tstr; t->name != NULL; t++) {
959 /* XXX: some systems' tgetstr needs non const */
960 term_alloc(el, t, tgetstr(strchr(t->name, *t->name),
961 &area));
965 if (Val(T_co) < 2)
966 Val(T_co) = 80; /* just in case */
967 if (Val(T_li) < 1)
968 Val(T_li) = 24;
970 el->el_term.t_size.v = Val(T_co);
971 el->el_term.t_size.h = Val(T_li);
973 term_setflags(el);
975 /* get the correct window size */
976 (void) term_get_size(el, &lins, &cols);
977 if (term_change_size(el, lins, cols) == -1)
978 return (-1);
979 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
980 term_bind_arrow(el);
981 el->el_term.t_name = term;
982 return (i <= 0 ? -1 : 0);
986 /* term_get_size():
987 * Return the new window size in lines and cols, and
988 * true if the size was changed.
990 protected int
991 term_get_size(EditLine *el, int *lins, int *cols)
994 *cols = Val(T_co);
995 *lins = Val(T_li);
997 #ifdef TIOCGWINSZ
999 struct winsize ws;
1000 if (ioctl(el->el_infd, TIOCGWINSZ, (ioctl_t) & ws) != -1) {
1001 if (ws.ws_col)
1002 *cols = ws.ws_col;
1003 if (ws.ws_row)
1004 *lins = ws.ws_row;
1007 #endif
1008 #ifdef TIOCGSIZE
1010 struct ttysize ts;
1011 if (ioctl(el->el_infd, TIOCGSIZE, (ioctl_t) & ts) != -1) {
1012 if (ts.ts_cols)
1013 *cols = ts.ts_cols;
1014 if (ts.ts_lines)
1015 *lins = ts.ts_lines;
1018 #endif
1019 return (Val(T_co) != *cols || Val(T_li) != *lins);
1023 /* term_change_size():
1024 * Change the size of the terminal
1026 protected int
1027 term_change_size(EditLine *el, int lins, int cols)
1030 * Just in case
1032 Val(T_co) = (cols < 2) ? 80 : cols;
1033 Val(T_li) = (lins < 1) ? 24 : lins;
1035 /* re-make display buffers */
1036 if (term_rebuffer_display(el) == -1)
1037 return (-1);
1038 re_clear_display(el);
1039 return (0);
1043 /* term_init_arrow():
1044 * Initialize the arrow key bindings from termcap
1046 private void
1047 term_init_arrow(EditLine *el)
1049 fkey_t *arrow = el->el_term.t_fkey;
1051 arrow[A_K_DN].name = "down";
1052 arrow[A_K_DN].key = T_kd;
1053 arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
1054 arrow[A_K_DN].type = XK_CMD;
1056 arrow[A_K_UP].name = "up";
1057 arrow[A_K_UP].key = T_ku;
1058 arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
1059 arrow[A_K_UP].type = XK_CMD;
1061 arrow[A_K_LT].name = "left";
1062 arrow[A_K_LT].key = T_kl;
1063 arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
1064 arrow[A_K_LT].type = XK_CMD;
1066 arrow[A_K_RT].name = "right";
1067 arrow[A_K_RT].key = T_kr;
1068 arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
1069 arrow[A_K_RT].type = XK_CMD;
1071 arrow[A_K_HO].name = "home";
1072 arrow[A_K_HO].key = T_kh;
1073 arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
1074 arrow[A_K_HO].type = XK_CMD;
1076 arrow[A_K_EN].name = "end";
1077 arrow[A_K_EN].key = T_at7;
1078 arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
1079 arrow[A_K_EN].type = XK_CMD;
1083 /* term_reset_arrow():
1084 * Reset arrow key bindings
1086 private void
1087 term_reset_arrow(EditLine *el)
1089 fkey_t *arrow = el->el_term.t_fkey;
1090 static const char strA[] = {033, '[', 'A', '\0'};
1091 static const char strB[] = {033, '[', 'B', '\0'};
1092 static const char strC[] = {033, '[', 'C', '\0'};
1093 static const char strD[] = {033, '[', 'D', '\0'};
1094 static const char strH[] = {033, '[', 'H', '\0'};
1095 static const char strF[] = {033, '[', 'F', '\0'};
1096 static const char stOA[] = {033, 'O', 'A', '\0'};
1097 static const char stOB[] = {033, 'O', 'B', '\0'};
1098 static const char stOC[] = {033, 'O', 'C', '\0'};
1099 static const char stOD[] = {033, 'O', 'D', '\0'};
1100 static const char stOH[] = {033, 'O', 'H', '\0'};
1101 static const char stOF[] = {033, 'O', 'F', '\0'};
1103 key_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1104 key_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1105 key_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1106 key_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1107 key_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1108 key_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1109 key_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1110 key_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1111 key_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1112 key_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1113 key_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1114 key_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1116 if (el->el_map.type == MAP_VI) {
1117 key_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1118 key_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1119 key_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1120 key_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1121 key_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1122 key_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1123 key_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1124 key_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1125 key_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1126 key_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1127 key_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1128 key_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1133 /* term_set_arrow():
1134 * Set an arrow key binding
1136 protected int
1137 term_set_arrow(EditLine *el, const char *name, key_value_t *fun, int type)
1139 fkey_t *arrow = el->el_term.t_fkey;
1140 int i;
1142 for (i = 0; i < A_K_NKEYS; i++)
1143 if (strcmp(name, arrow[i].name) == 0) {
1144 arrow[i].fun = *fun;
1145 arrow[i].type = type;
1146 return (0);
1148 return (-1);
1152 /* term_clear_arrow():
1153 * Clear an arrow key binding
1155 protected int
1156 term_clear_arrow(EditLine *el, const char *name)
1158 fkey_t *arrow = el->el_term.t_fkey;
1159 int i;
1161 for (i = 0; i < A_K_NKEYS; i++)
1162 if (strcmp(name, arrow[i].name) == 0) {
1163 arrow[i].type = XK_NOD;
1164 return (0);
1166 return (-1);
1170 /* term_print_arrow():
1171 * Print the arrow key bindings
1173 protected void
1174 term_print_arrow(EditLine *el, const char *name)
1176 int i;
1177 fkey_t *arrow = el->el_term.t_fkey;
1179 for (i = 0; i < A_K_NKEYS; i++)
1180 if (*name == '\0' || strcmp(name, arrow[i].name) == 0)
1181 if (arrow[i].type != XK_NOD)
1182 key_kprint(el, arrow[i].name, &arrow[i].fun,
1183 arrow[i].type);
1187 /* term_bind_arrow():
1188 * Bind the arrow keys
1190 protected void
1191 term_bind_arrow(EditLine *el)
1193 el_action_t *map;
1194 const el_action_t *dmap;
1195 int i, j;
1196 char *p;
1197 fkey_t *arrow = el->el_term.t_fkey;
1199 /* Check if the components needed are initialized */
1200 if (el->el_term.t_buf == NULL || el->el_map.key == NULL)
1201 return;
1203 map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
1204 dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
1206 term_reset_arrow(el);
1208 for (i = 0; i < A_K_NKEYS; i++) {
1209 p = el->el_term.t_str[arrow[i].key];
1210 if (p && *p) {
1211 j = (unsigned char) *p;
1213 * Assign the arrow keys only if:
1215 * 1. They are multi-character arrow keys and the user
1216 * has not re-assigned the leading character, or
1217 * has re-assigned the leading character to be
1218 * ED_SEQUENCE_LEAD_IN
1219 * 2. They are single arrow keys pointing to an
1220 * unassigned key.
1222 if (arrow[i].type == XK_NOD)
1223 key_clear(el, map, p);
1224 else {
1225 if (p[1] && (dmap[j] == map[j] ||
1226 map[j] == ED_SEQUENCE_LEAD_IN)) {
1227 key_add(el, p, &arrow[i].fun,
1228 arrow[i].type);
1229 map[j] = ED_SEQUENCE_LEAD_IN;
1230 } else if (map[j] == ED_UNASSIGNED) {
1231 key_clear(el, map, p);
1232 if (arrow[i].type == XK_CMD)
1233 map[j] = arrow[i].fun.cmd;
1234 else
1235 key_add(el, p, &arrow[i].fun,
1236 arrow[i].type);
1243 /* term_putc():
1244 * Add a character
1246 private int
1247 term_putc(int c)
1250 if (term_outfile == NULL)
1251 return -1;
1252 return fputc(c, term_outfile);
1255 private void
1256 term_tputs(EditLine *el, const char *cap, int affcnt)
1258 #ifdef _REENTRANT
1259 pthread_mutex_lock(&term_mutex);
1260 #endif
1261 term_outfile = el->el_outfile;
1262 (void)tputs(cap, affcnt, term_putc);
1263 #ifdef _REENTRANT
1264 pthread_mutex_unlock(&term_mutex);
1265 #endif
1268 /* term__putc():
1269 * Add a character
1271 protected int
1272 term__putc(EditLine *el, int c)
1275 return fputc(c, el->el_outfile);
1278 /* term__flush():
1279 * Flush output
1281 protected void
1282 term__flush(EditLine *el)
1285 (void) fflush(el->el_outfile);
1288 /* term_writec():
1289 * Write the given character out, in a human readable form
1291 protected void
1292 term_writec(EditLine *el, int c)
1294 char buf[8];
1295 size_t cnt = key__decode_char(buf, sizeof(buf), 0, c);
1296 buf[cnt] = '\0';
1297 term_overwrite(el, buf, (size_t)cnt);
1298 term__flush(el);
1302 /* term_telltc():
1303 * Print the current termcap characteristics
1305 protected int
1306 /*ARGSUSED*/
1307 term_telltc(EditLine *el, int argc __attribute__((__unused__)),
1308 const char **argv __attribute__((__unused__)))
1310 const struct termcapstr *t;
1311 char **ts;
1312 char upbuf[EL_BUFSIZ];
1314 (void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
1315 (void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
1316 (void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
1317 Val(T_co), Val(T_li));
1318 (void) fprintf(el->el_outfile,
1319 "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
1320 (void) fprintf(el->el_outfile,
1321 "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
1322 (void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
1323 EL_HAS_AUTO_MARGINS ? "has" : "does not have");
1324 if (EL_HAS_AUTO_MARGINS)
1325 (void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
1326 EL_HAS_MAGIC_MARGINS ? "has" : "does not have");
1328 for (t = tstr, ts = el->el_term.t_str; t->name != NULL; t++, ts++) {
1329 const char *ub;
1330 if (*ts && **ts) {
1331 (void) key__decode_str(*ts, upbuf, sizeof(upbuf), "");
1332 ub = upbuf;
1333 } else {
1334 ub = "(empty)";
1336 (void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n",
1337 t->long_name, t->name, ub);
1339 (void) fputc('\n', el->el_outfile);
1340 return (0);
1344 /* term_settc():
1345 * Change the current terminal characteristics
1347 protected int
1348 /*ARGSUSED*/
1349 term_settc(EditLine *el, int argc __attribute__((__unused__)),
1350 const char **argv)
1352 const struct termcapstr *ts;
1353 const struct termcapval *tv;
1354 const char *what, *how;
1356 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1357 return -1;
1359 what = argv[1];
1360 how = argv[2];
1363 * Do the strings first
1365 for (ts = tstr; ts->name != NULL; ts++)
1366 if (strcmp(ts->name, what) == 0)
1367 break;
1369 if (ts->name != NULL) {
1370 term_alloc(el, ts, how);
1371 term_setflags(el);
1372 return 0;
1375 * Do the numeric ones second
1377 for (tv = tval; tv->name != NULL; tv++)
1378 if (strcmp(tv->name, what) == 0)
1379 break;
1381 if (tv->name != NULL)
1382 return -1;
1384 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1385 tv == &tval[T_am] || tv == &tval[T_xn]) {
1386 if (strcmp(how, "yes") == 0)
1387 el->el_term.t_val[tv - tval] = 1;
1388 else if (strcmp(how, "no") == 0)
1389 el->el_term.t_val[tv - tval] = 0;
1390 else {
1391 (void) fprintf(el->el_errfile,
1392 "%s: Bad value `%s'.\n", argv[0], how);
1393 return -1;
1395 term_setflags(el);
1396 if (term_change_size(el, Val(T_li), Val(T_co)) == -1)
1397 return -1;
1398 return 0;
1399 } else {
1400 long i;
1401 char *ep;
1403 i = strtol(how, &ep, 10);
1404 if (*ep != '\0') {
1405 (void) fprintf(el->el_errfile,
1406 "%s: Bad value `%s'.\n", argv[0], how);
1407 return -1;
1409 el->el_term.t_val[tv - tval] = (int) i;
1410 el->el_term.t_size.v = Val(T_co);
1411 el->el_term.t_size.h = Val(T_li);
1412 if (tv == &tval[T_co] || tv == &tval[T_li])
1413 if (term_change_size(el, Val(T_li), Val(T_co))
1414 == -1)
1415 return -1;
1416 return 0;
1421 /* term_gettc():
1422 * Get the current terminal characteristics
1424 protected int
1425 /*ARGSUSED*/
1426 term_gettc(EditLine *el, int argc __attribute__((__unused__)), char **argv)
1428 const struct termcapstr *ts;
1429 const struct termcapval *tv;
1430 char *what;
1431 void *how;
1433 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1434 return (-1);
1436 what = argv[1];
1437 how = argv[2];
1440 * Do the strings first
1442 for (ts = tstr; ts->name != NULL; ts++)
1443 if (strcmp(ts->name, what) == 0)
1444 break;
1446 if (ts->name != NULL) {
1447 *(char **)how = el->el_term.t_str[ts - tstr];
1448 return 0;
1451 * Do the numeric ones second
1453 for (tv = tval; tv->name != NULL; tv++)
1454 if (strcmp(tv->name, what) == 0)
1455 break;
1457 if (tv->name == NULL)
1458 return -1;
1460 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1461 tv == &tval[T_am] || tv == &tval[T_xn]) {
1462 static char yes[] = "yes";
1463 static char no[] = "no";
1464 if (el->el_term.t_val[tv - tval])
1465 *(char **)how = yes;
1466 else
1467 *(char **)how = no;
1468 return 0;
1469 } else {
1470 *(int *)how = el->el_term.t_val[tv - tval];
1471 return 0;
1475 /* term_echotc():
1476 * Print the termcap string out with variable substitution
1478 protected int
1479 /*ARGSUSED*/
1480 term_echotc(EditLine *el, int argc __attribute__((__unused__)),
1481 const char **argv)
1483 char *cap, *scap, *ep;
1484 int arg_need, arg_cols, arg_rows;
1485 int verbose = 0, silent = 0;
1486 char *area;
1487 static const char fmts[] = "%s\n", fmtd[] = "%d\n";
1488 const struct termcapstr *t;
1489 char buf[TC_BUFSIZE];
1490 long i;
1492 area = buf;
1494 if (argv == NULL || argv[1] == NULL)
1495 return (-1);
1496 argv++;
1498 if (argv[0][0] == '-') {
1499 switch (argv[0][1]) {
1500 case 'v':
1501 verbose = 1;
1502 break;
1503 case 's':
1504 silent = 1;
1505 break;
1506 default:
1507 /* stderror(ERR_NAME | ERR_TCUSAGE); */
1508 break;
1510 argv++;
1512 if (!*argv || *argv[0] == '\0')
1513 return (0);
1514 if (strcmp(*argv, "tabs") == 0) {
1515 (void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
1516 return (0);
1517 } else if (strcmp(*argv, "meta") == 0) {
1518 (void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
1519 return (0);
1520 } else if (strcmp(*argv, "xn") == 0) {
1521 (void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ?
1522 "yes" : "no");
1523 return (0);
1524 } else if (strcmp(*argv, "am") == 0) {
1525 (void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ?
1526 "yes" : "no");
1527 return (0);
1528 } else if (strcmp(*argv, "baud") == 0) {
1529 #ifdef notdef
1530 int i;
1532 for (i = 0; baud_rate[i].b_name != NULL; i++)
1533 if (el->el_tty.t_speed == baud_rate[i].b_rate) {
1534 (void) fprintf(el->el_outfile, fmts,
1535 baud_rate[i].b_name);
1536 return (0);
1538 (void) fprintf(el->el_outfile, fmtd, 0);
1539 #else
1540 (void) fprintf(el->el_outfile, fmtd, (int)el->el_tty.t_speed);
1541 #endif
1542 return (0);
1543 } else if (strcmp(*argv, "rows") == 0 || strcmp(*argv, "lines") == 0) {
1544 (void) fprintf(el->el_outfile, fmtd, Val(T_li));
1545 return (0);
1546 } else if (strcmp(*argv, "cols") == 0) {
1547 (void) fprintf(el->el_outfile, fmtd, Val(T_co));
1548 return (0);
1551 * Try to use our local definition first
1553 scap = NULL;
1554 for (t = tstr; t->name != NULL; t++)
1555 if (strcmp(t->name, *argv) == 0) {
1556 scap = el->el_term.t_str[t - tstr];
1557 break;
1559 if (t->name == NULL) {
1560 /* XXX: some systems' tgetstr needs non const */
1561 scap = tgetstr(strchr(*argv, **argv), &area);
1563 if (!scap || scap[0] == '\0') {
1564 if (!silent)
1565 (void) fprintf(el->el_errfile,
1566 "echotc: Termcap parameter `%s' not found.\n",
1567 *argv);
1568 return (-1);
1571 * Count home many values we need for this capability.
1573 for (cap = scap, arg_need = 0; *cap; cap++)
1574 if (*cap == '%')
1575 switch (*++cap) {
1576 case 'd':
1577 case '2':
1578 case '3':
1579 case '.':
1580 case '+':
1581 arg_need++;
1582 break;
1583 case '%':
1584 case '>':
1585 case 'i':
1586 case 'r':
1587 case 'n':
1588 case 'B':
1589 case 'D':
1590 break;
1591 default:
1593 * hpux has lot's of them...
1595 if (verbose)
1596 (void) fprintf(el->el_errfile,
1597 "echotc: Warning: unknown termcap %% `%c'.\n",
1598 *cap);
1599 /* This is bad, but I won't complain */
1600 break;
1603 switch (arg_need) {
1604 case 0:
1605 argv++;
1606 if (*argv && *argv[0]) {
1607 if (!silent)
1608 (void) fprintf(el->el_errfile,
1609 "echotc: Warning: Extra argument `%s'.\n",
1610 *argv);
1611 return (-1);
1613 term_tputs(el, scap, 1);
1614 break;
1615 case 1:
1616 argv++;
1617 if (!*argv || *argv[0] == '\0') {
1618 if (!silent)
1619 (void) fprintf(el->el_errfile,
1620 "echotc: Warning: Missing argument.\n");
1621 return (-1);
1623 arg_cols = 0;
1624 i = strtol(*argv, &ep, 10);
1625 if (*ep != '\0' || i < 0) {
1626 if (!silent)
1627 (void) fprintf(el->el_errfile,
1628 "echotc: Bad value `%s' for rows.\n",
1629 *argv);
1630 return (-1);
1632 arg_rows = (int) i;
1633 argv++;
1634 if (*argv && *argv[0]) {
1635 if (!silent)
1636 (void) fprintf(el->el_errfile,
1637 "echotc: Warning: Extra argument `%s'.\n",
1638 *argv);
1639 return (-1);
1641 term_tputs(el, tgoto(scap, arg_cols, arg_rows), 1);
1642 break;
1643 default:
1644 /* This is wrong, but I will ignore it... */
1645 if (verbose)
1646 (void) fprintf(el->el_errfile,
1647 "echotc: Warning: Too many required arguments (%d).\n",
1648 arg_need);
1649 /* FALLTHROUGH */
1650 case 2:
1651 argv++;
1652 if (!*argv || *argv[0] == '\0') {
1653 if (!silent)
1654 (void) fprintf(el->el_errfile,
1655 "echotc: Warning: Missing argument.\n");
1656 return (-1);
1658 i = strtol(*argv, &ep, 10);
1659 if (*ep != '\0' || i < 0) {
1660 if (!silent)
1661 (void) fprintf(el->el_errfile,
1662 "echotc: Bad value `%s' for cols.\n",
1663 *argv);
1664 return (-1);
1666 arg_cols = (int) i;
1667 argv++;
1668 if (!*argv || *argv[0] == '\0') {
1669 if (!silent)
1670 (void) fprintf(el->el_errfile,
1671 "echotc: Warning: Missing argument.\n");
1672 return (-1);
1674 i = strtol(*argv, &ep, 10);
1675 if (*ep != '\0' || i < 0) {
1676 if (!silent)
1677 (void) fprintf(el->el_errfile,
1678 "echotc: Bad value `%s' for rows.\n",
1679 *argv);
1680 return (-1);
1682 arg_rows = (int) i;
1683 if (*ep != '\0') {
1684 if (!silent)
1685 (void) fprintf(el->el_errfile,
1686 "echotc: Bad value `%s'.\n", *argv);
1687 return (-1);
1689 argv++;
1690 if (*argv && *argv[0]) {
1691 if (!silent)
1692 (void) fprintf(el->el_errfile,
1693 "echotc: Warning: Extra argument `%s'.\n",
1694 *argv);
1695 return (-1);
1697 term_tputs(el, tgoto(scap, arg_cols, arg_rows), arg_rows);
1698 break;
1700 return (0);