Sync with HEAD.
[dragonfly.git] / lib / libedit / term.c
blobbd4b68c3866e2a0a462fe99b3db66e9a8b5961d0
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * @(#)term.c 8.2 (Berkeley) 4/30/95
33 * $NetBSD: term.c,v 1.46 2006/11/24 00:01:17 christos Exp $
34 * $DragonFly: src/lib/libedit/term.c,v 1.5 2007/05/05 00:27:39 pavalos Exp $
37 #include "config.h"
40 * term.c: Editor/termcap-curses interface
41 * We have to declare a static variable here, since the
42 * termcap putchar routine does not take an argument!
44 #include <stdio.h>
45 #include <signal.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #ifdef HAVE_TERMCAP_H
50 #include <termcap.h>
51 #endif
52 #ifdef HAVE_CURSES_H
53 #include <curses.h>
54 #endif
55 #ifdef HAVE_NCURSES_H
56 #include <ncurses.h>
57 #endif
58 /* Solaris's term.h does horrid things. */
59 #if (defined(HAVE_TERM_H) && !defined(SUNOS))
60 #include <term.h>
61 #endif
62 #include <sys/types.h>
63 #include <sys/ioctl.h>
65 #include "el.h"
68 * IMPORTANT NOTE: these routines are allowed to look at the current screen
69 * and the current possition assuming that it is correct. If this is not
70 * true, then the update will be WRONG! This is (should be) a valid
71 * assumption...
74 #define TC_BUFSIZE 2048
76 #define GoodStr(a) (el->el_term.t_str[a] != NULL && \
77 el->el_term.t_str[a][0] != '\0')
78 #define Str(a) el->el_term.t_str[a]
79 #define Val(a) el->el_term.t_val[a]
81 #ifdef notdef
82 private const struct {
83 const char *b_name;
84 int b_rate;
85 } baud_rate[] = {
86 #ifdef B0
87 { "0", B0 },
88 #endif
89 #ifdef B50
90 { "50", B50 },
91 #endif
92 #ifdef B75
93 { "75", B75 },
94 #endif
95 #ifdef B110
96 { "110", B110 },
97 #endif
98 #ifdef B134
99 { "134", B134 },
100 #endif
101 #ifdef B150
102 { "150", B150 },
103 #endif
104 #ifdef B200
105 { "200", B200 },
106 #endif
107 #ifdef B300
108 { "300", B300 },
109 #endif
110 #ifdef B600
111 { "600", B600 },
112 #endif
113 #ifdef B900
114 { "900", B900 },
115 #endif
116 #ifdef B1200
117 { "1200", B1200 },
118 #endif
119 #ifdef B1800
120 { "1800", B1800 },
121 #endif
122 #ifdef B2400
123 { "2400", B2400 },
124 #endif
125 #ifdef B3600
126 { "3600", B3600 },
127 #endif
128 #ifdef B4800
129 { "4800", B4800 },
130 #endif
131 #ifdef B7200
132 { "7200", B7200 },
133 #endif
134 #ifdef B9600
135 { "9600", B9600 },
136 #endif
137 #ifdef EXTA
138 { "19200", EXTA },
139 #endif
140 #ifdef B19200
141 { "19200", B19200 },
142 #endif
143 #ifdef EXTB
144 { "38400", EXTB },
145 #endif
146 #ifdef B38400
147 { "38400", B38400 },
148 #endif
149 { NULL, 0 }
151 #endif
153 private const struct termcapstr {
154 const char *name;
155 const char *long_name;
156 } tstr[] = {
157 #define T_al 0
158 { "al", "add new blank line" },
159 #define T_bl 1
160 { "bl", "audible bell" },
161 #define T_cd 2
162 { "cd", "clear to bottom" },
163 #define T_ce 3
164 { "ce", "clear to end of line" },
165 #define T_ch 4
166 { "ch", "cursor to horiz pos" },
167 #define T_cl 5
168 { "cl", "clear screen" },
169 #define T_dc 6
170 { "dc", "delete a character" },
171 #define T_dl 7
172 { "dl", "delete a line" },
173 #define T_dm 8
174 { "dm", "start delete mode" },
175 #define T_ed 9
176 { "ed", "end delete mode" },
177 #define T_ei 10
178 { "ei", "end insert mode" },
179 #define T_fs 11
180 { "fs", "cursor from status line" },
181 #define T_ho 12
182 { "ho", "home cursor" },
183 #define T_ic 13
184 { "ic", "insert character" },
185 #define T_im 14
186 { "im", "start insert mode" },
187 #define T_ip 15
188 { "ip", "insert padding" },
189 #define T_kd 16
190 { "kd", "sends cursor down" },
191 #define T_kl 17
192 { "kl", "sends cursor left" },
193 #define T_kr 18
194 { "kr", "sends cursor right" },
195 #define T_ku 19
196 { "ku", "sends cursor up" },
197 #define T_md 20
198 { "md", "begin bold" },
199 #define T_me 21
200 { "me", "end attributes" },
201 #define T_nd 22
202 { "nd", "non destructive space" },
203 #define T_se 23
204 { "se", "end standout" },
205 #define T_so 24
206 { "so", "begin standout" },
207 #define T_ts 25
208 { "ts", "cursor to status line" },
209 #define T_up 26
210 { "up", "cursor up one" },
211 #define T_us 27
212 { "us", "begin underline" },
213 #define T_ue 28
214 { "ue", "end underline" },
215 #define T_vb 29
216 { "vb", "visible bell" },
217 #define T_DC 30
218 { "DC", "delete multiple chars" },
219 #define T_DO 31
220 { "DO", "cursor down multiple" },
221 #define T_IC 32
222 { "IC", "insert multiple chars" },
223 #define T_LE 33
224 { "LE", "cursor left multiple" },
225 #define T_RI 34
226 { "RI", "cursor right multiple" },
227 #define T_UP 35
228 { "UP", "cursor up multiple" },
229 #define T_kh 36
230 { "kh", "send cursor home" },
231 #define T_at7 37
232 { "@7", "send cursor end" },
233 #define T_str 38
234 { NULL, NULL }
237 private const struct termcapval {
238 const char *name;
239 const char *long_name;
240 } tval[] = {
241 #define T_am 0
242 { "am", "has automatic margins" },
243 #define T_pt 1
244 { "pt", "has physical tabs" },
245 #define T_li 2
246 { "li", "Number of lines" },
247 #define T_co 3
248 { "co", "Number of columns" },
249 #define T_km 4
250 { "km", "Has meta key" },
251 #define T_xt 5
252 { "xt", "Tab chars destructive" },
253 #define T_xn 6
254 { "xn", "newline ignored at right margin" },
255 #define T_MT 7
256 { "MT", "Has meta key" }, /* XXX? */
257 #define T_val 8
258 { NULL, NULL, }
260 /* do two or more of the attributes use me */
262 private void term_setflags(EditLine *);
263 private int term_rebuffer_display(EditLine *);
264 private void term_free_display(EditLine *);
265 private int term_alloc_display(EditLine *);
266 private void term_alloc(EditLine *, const struct termcapstr *, const char *);
267 private void term_init_arrow(EditLine *);
268 private void term_reset_arrow(EditLine *);
271 private FILE *term_outfile = NULL; /* XXX: How do we fix that? */
274 /* term_setflags():
275 * Set the terminal capability flags
277 private void
278 term_setflags(EditLine *el)
280 EL_FLAGS = 0;
281 if (el->el_tty.t_tabs)
282 EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
284 EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
285 EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
286 EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
287 EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
288 TERM_CAN_INSERT : 0;
289 EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
290 EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0;
291 EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0;
293 if (GoodStr(T_me) && GoodStr(T_ue))
294 EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ?
295 TERM_CAN_ME : 0;
296 else
297 EL_FLAGS &= ~TERM_CAN_ME;
298 if (GoodStr(T_me) && GoodStr(T_se))
299 EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ?
300 TERM_CAN_ME : 0;
303 #ifdef DEBUG_SCREEN
304 if (!EL_CAN_UP) {
305 (void) fprintf(el->el_errfile,
306 "WARNING: Your terminal cannot move up.\n");
307 (void) fprintf(el->el_errfile,
308 "Editing may be odd for long lines.\n");
310 if (!EL_CAN_CEOL)
311 (void) fprintf(el->el_errfile, "no clear EOL capability.\n");
312 if (!EL_CAN_DELETE)
313 (void) fprintf(el->el_errfile, "no delete char capability.\n");
314 if (!EL_CAN_INSERT)
315 (void) fprintf(el->el_errfile, "no insert char capability.\n");
316 #endif /* DEBUG_SCREEN */
320 /* term_init():
321 * Initialize the terminal stuff
323 protected int
324 term_init(EditLine *el)
327 el->el_term.t_buf = (char *) el_malloc(TC_BUFSIZE);
328 if (el->el_term.t_buf == NULL)
329 return (-1);
330 el->el_term.t_cap = (char *) el_malloc(TC_BUFSIZE);
331 if (el->el_term.t_cap == NULL)
332 return (-1);
333 el->el_term.t_fkey = (fkey_t *) el_malloc(A_K_NKEYS * sizeof(fkey_t));
334 if (el->el_term.t_fkey == NULL)
335 return (-1);
336 el->el_term.t_loc = 0;
337 el->el_term.t_str = (char **) el_malloc(T_str * sizeof(char *));
338 if (el->el_term.t_str == NULL)
339 return (-1);
340 (void) memset(el->el_term.t_str, 0, T_str * sizeof(char *));
341 el->el_term.t_val = (int *) el_malloc(T_val * sizeof(int));
342 if (el->el_term.t_val == NULL)
343 return (-1);
344 (void) memset(el->el_term.t_val, 0, T_val * sizeof(int));
345 term_outfile = el->el_outfile;
346 (void) term_set(el, NULL);
347 term_init_arrow(el);
348 return (0);
351 /* term_end():
352 * Clean up the terminal stuff
354 protected void
355 term_end(EditLine *el)
358 el_free((ptr_t) el->el_term.t_buf);
359 el->el_term.t_buf = NULL;
360 el_free((ptr_t) el->el_term.t_cap);
361 el->el_term.t_cap = NULL;
362 el->el_term.t_loc = 0;
363 el_free((ptr_t) el->el_term.t_str);
364 el->el_term.t_str = NULL;
365 el_free((ptr_t) el->el_term.t_val);
366 el->el_term.t_val = NULL;
367 el_free((ptr_t) el->el_term.t_fkey);
368 el->el_term.t_fkey = NULL;
369 term_free_display(el);
373 /* term_alloc():
374 * Maintain a string pool for termcap strings
376 private void
377 term_alloc(EditLine *el, const struct termcapstr *t, const char *cap)
379 char termbuf[TC_BUFSIZE];
380 int tlen, clen;
381 char **tlist = el->el_term.t_str;
382 char **tmp, **str = &tlist[t - tstr];
384 if (cap == NULL || *cap == '\0') {
385 *str = NULL;
386 return;
387 } else
388 clen = strlen(cap);
390 tlen = *str == NULL ? 0 : strlen(*str);
393 * New string is shorter; no need to allocate space
395 if (clen <= tlen) {
396 if (*str)
397 (void) strcpy(*str, cap); /* XXX strcpy is safe */
398 return;
401 * New string is longer; see if we have enough space to append
403 if (el->el_term.t_loc + 3 < TC_BUFSIZE) {
404 /* XXX strcpy is safe */
405 (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc],
406 cap);
407 el->el_term.t_loc += clen + 1; /* one for \0 */
408 return;
411 * Compact our buffer; no need to check compaction, cause we know it
412 * fits...
414 tlen = 0;
415 for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
416 if (*tmp != NULL && *tmp != '\0' && *tmp != *str) {
417 char *ptr;
419 for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
420 continue;
421 termbuf[tlen++] = '\0';
423 memcpy(el->el_term.t_buf, termbuf, TC_BUFSIZE);
424 el->el_term.t_loc = tlen;
425 if (el->el_term.t_loc + 3 >= TC_BUFSIZE) {
426 (void) fprintf(el->el_errfile,
427 "Out of termcap string space.\n");
428 return;
430 /* XXX strcpy is safe */
431 (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap);
432 el->el_term.t_loc += clen + 1; /* one for \0 */
433 return;
437 /* term_rebuffer_display():
438 * Rebuffer the display after the screen changed size
440 private int
441 term_rebuffer_display(EditLine *el)
443 coord_t *c = &el->el_term.t_size;
445 term_free_display(el);
447 c->h = Val(T_co);
448 c->v = Val(T_li);
450 if (term_alloc_display(el) == -1)
451 return (-1);
452 return (0);
456 /* term_alloc_display():
457 * Allocate a new display.
459 private int
460 term_alloc_display(EditLine *el)
462 int i;
463 char **b;
464 coord_t *c = &el->el_term.t_size;
466 b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
467 if (b == NULL)
468 return (-1);
469 for (i = 0; i < c->v; i++) {
470 b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
471 if (b[i] == NULL) {
472 while (--i >= 0)
473 el_free((ptr_t) b[i]);
474 el_free((ptr_t) b);
475 return (-1);
478 b[c->v] = NULL;
479 el->el_display = b;
481 b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
482 if (b == NULL)
483 return (-1);
484 for (i = 0; i < c->v; i++) {
485 b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
486 if (b[i] == NULL) {
487 while (--i >= 0)
488 el_free((ptr_t) b[i]);
489 el_free((ptr_t) b);
490 return (-1);
493 b[c->v] = NULL;
494 el->el_vdisplay = b;
495 return (0);
499 /* term_free_display():
500 * Free the display buffers
502 private void
503 term_free_display(EditLine *el)
505 char **b;
506 char **bufp;
508 b = el->el_display;
509 el->el_display = NULL;
510 if (b != NULL) {
511 for (bufp = b; *bufp != NULL; bufp++)
512 el_free((ptr_t) * bufp);
513 el_free((ptr_t) b);
515 b = el->el_vdisplay;
516 el->el_vdisplay = NULL;
517 if (b != NULL) {
518 for (bufp = b; *bufp != NULL; bufp++)
519 el_free((ptr_t) * bufp);
520 el_free((ptr_t) b);
525 /* term_move_to_line():
526 * move to line <where> (first line == 0)
527 * as efficiently as possible
529 protected void
530 term_move_to_line(EditLine *el, int where)
532 int del;
534 if (where == el->el_cursor.v)
535 return;
537 if (where > el->el_term.t_size.v) {
538 #ifdef DEBUG_SCREEN
539 (void) fprintf(el->el_errfile,
540 "term_move_to_line: where is ridiculous: %d\r\n", where);
541 #endif /* DEBUG_SCREEN */
542 return;
544 if ((del = where - el->el_cursor.v) > 0) {
545 while (del > 0) {
546 if (EL_HAS_AUTO_MARGINS &&
547 el->el_display[el->el_cursor.v][0] != '\0') {
548 /* move without newline */
549 term_move_to_char(el, el->el_term.t_size.h - 1);
550 term_overwrite(el,
551 &el->el_display[el->el_cursor.v][el->el_cursor.h],
553 /* updates Cursor */
554 del--;
555 } else {
556 if ((del > 1) && GoodStr(T_DO)) {
557 (void) tputs(tgoto(Str(T_DO), del, del),
558 del, term__putc);
559 del = 0;
560 } else {
561 for (; del > 0; del--)
562 term__putc('\n');
563 /* because the \n will become \r\n */
564 el->el_cursor.h = 0;
568 } else { /* del < 0 */
569 if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
570 (void) tputs(tgoto(Str(T_UP), -del, -del), -del,
571 term__putc);
572 else {
573 if (GoodStr(T_up))
574 for (; del < 0; del++)
575 (void) tputs(Str(T_up), 1, term__putc);
578 el->el_cursor.v = where;/* now where is here */
582 /* term_move_to_char():
583 * Move to the character position specified
585 protected void
586 term_move_to_char(EditLine *el, int where)
588 int del, i;
590 mc_again:
591 if (where == el->el_cursor.h)
592 return;
594 if (where > el->el_term.t_size.h) {
595 #ifdef DEBUG_SCREEN
596 (void) fprintf(el->el_errfile,
597 "term_move_to_char: where is riduculous: %d\r\n", where);
598 #endif /* DEBUG_SCREEN */
599 return;
601 if (!where) { /* if where is first column */
602 term__putc('\r'); /* do a CR */
603 el->el_cursor.h = 0;
604 return;
606 del = where - el->el_cursor.h;
608 if ((del < -4 || del > 4) && GoodStr(T_ch))
609 /* go there directly */
610 (void) tputs(tgoto(Str(T_ch), where, where), where, term__putc);
611 else {
612 if (del > 0) { /* moving forward */
613 if ((del > 4) && GoodStr(T_RI))
614 (void) tputs(tgoto(Str(T_RI), del, del),
615 del, term__putc);
616 else {
617 /* if I can do tabs, use them */
618 if (EL_CAN_TAB) {
619 if ((el->el_cursor.h & 0370) !=
620 (where & 0370)) {
621 /* if not within tab stop */
622 for (i =
623 (el->el_cursor.h & 0370);
624 i < (where & 0370);
625 i += 8)
626 term__putc('\t');
627 /* then tab over */
628 el->el_cursor.h = where & 0370;
632 * it's usually cheaper to just write the
633 * chars, so we do.
636 * NOTE THAT term_overwrite() WILL CHANGE
637 * el->el_cursor.h!!!
639 term_overwrite(el,
640 &el->el_display[el->el_cursor.v][el->el_cursor.h],
641 where - el->el_cursor.h);
644 } else { /* del < 0 := moving backward */
645 if ((-del > 4) && GoodStr(T_LE))
646 (void) tputs(tgoto(Str(T_LE), -del, -del),
647 -del, term__putc);
648 else { /* can't go directly there */
650 * if the "cost" is greater than the "cost"
651 * from col 0
653 if (EL_CAN_TAB ?
654 ((unsigned int)-del >
655 (((unsigned int) where >> 3) +
656 (where & 07)))
657 : (-del > where)) {
658 term__putc('\r'); /* do a CR */
659 el->el_cursor.h = 0;
660 goto mc_again; /* and try again */
662 for (i = 0; i < -del; i++)
663 term__putc('\b');
667 el->el_cursor.h = where; /* now where is here */
671 /* term_overwrite():
672 * Overstrike num characters
674 protected void
675 term_overwrite(EditLine *el, const char *cp, int n)
677 if (n <= 0)
678 return; /* catch bugs */
680 if (n > el->el_term.t_size.h) {
681 #ifdef DEBUG_SCREEN
682 (void) fprintf(el->el_errfile,
683 "term_overwrite: n is riduculous: %d\r\n", n);
684 #endif /* DEBUG_SCREEN */
685 return;
687 do {
688 term__putc(*cp++);
689 el->el_cursor.h++;
690 } while (--n);
692 if (el->el_cursor.h >= el->el_term.t_size.h) { /* wrap? */
693 if (EL_HAS_AUTO_MARGINS) { /* yes */
694 el->el_cursor.h = 0;
695 el->el_cursor.v++;
696 if (EL_HAS_MAGIC_MARGINS) {
697 /* force the wrap to avoid the "magic"
698 * situation */
699 char c;
700 if ((c = el->el_display[el->el_cursor.v][el->el_cursor.h])
701 != '\0')
702 term_overwrite(el, &c, 1);
703 else
704 term__putc(' ');
705 el->el_cursor.h = 1;
707 } else /* no wrap, but cursor stays on screen */
708 el->el_cursor.h = el->el_term.t_size.h;
713 /* term_deletechars():
714 * Delete num characters
716 protected void
717 term_deletechars(EditLine *el, int num)
719 if (num <= 0)
720 return;
722 if (!EL_CAN_DELETE) {
723 #ifdef DEBUG_EDIT
724 (void) fprintf(el->el_errfile, " ERROR: cannot delete \n");
725 #endif /* DEBUG_EDIT */
726 return;
728 if (num > el->el_term.t_size.h) {
729 #ifdef DEBUG_SCREEN
730 (void) fprintf(el->el_errfile,
731 "term_deletechars: num is riduculous: %d\r\n", num);
732 #endif /* DEBUG_SCREEN */
733 return;
735 if (GoodStr(T_DC)) /* if I have multiple delete */
736 if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more
737 * expen. */
738 (void) tputs(tgoto(Str(T_DC), num, num),
739 num, term__putc);
740 return;
742 if (GoodStr(T_dm)) /* if I have delete mode */
743 (void) tputs(Str(T_dm), 1, term__putc);
745 if (GoodStr(T_dc)) /* else do one at a time */
746 while (num--)
747 (void) tputs(Str(T_dc), 1, term__putc);
749 if (GoodStr(T_ed)) /* if I have delete mode */
750 (void) tputs(Str(T_ed), 1, term__putc);
754 /* term_insertwrite():
755 * Puts terminal in insert character mode or inserts num
756 * characters in the line
758 protected void
759 term_insertwrite(EditLine *el, char *cp, int num)
761 if (num <= 0)
762 return;
763 if (!EL_CAN_INSERT) {
764 #ifdef DEBUG_EDIT
765 (void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
766 #endif /* DEBUG_EDIT */
767 return;
769 if (num > el->el_term.t_size.h) {
770 #ifdef DEBUG_SCREEN
771 (void) fprintf(el->el_errfile,
772 "StartInsert: num is riduculous: %d\r\n", num);
773 #endif /* DEBUG_SCREEN */
774 return;
776 if (GoodStr(T_IC)) /* if I have multiple insert */
777 if ((num > 1) || !GoodStr(T_ic)) {
778 /* if ic would be more expensive */
779 (void) tputs(tgoto(Str(T_IC), num, num),
780 num, term__putc);
781 term_overwrite(el, cp, num);
782 /* this updates el_cursor.h */
783 return;
785 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
786 (void) tputs(Str(T_im), 1, term__putc);
788 el->el_cursor.h += num;
790 term__putc(*cp++);
791 while (--num);
793 if (GoodStr(T_ip)) /* have to make num chars insert */
794 (void) tputs(Str(T_ip), 1, term__putc);
796 (void) tputs(Str(T_ei), 1, term__putc);
797 return;
799 do {
800 if (GoodStr(T_ic)) /* have to make num chars insert */
801 (void) tputs(Str(T_ic), 1, term__putc);
802 /* insert a char */
804 term__putc(*cp++);
806 el->el_cursor.h++;
808 if (GoodStr(T_ip)) /* have to make num chars insert */
809 (void) tputs(Str(T_ip), 1, term__putc);
810 /* pad the inserted char */
812 } while (--num);
816 /* term_clear_EOL():
817 * clear to end of line. There are num characters to clear
819 protected void
820 term_clear_EOL(EditLine *el, int num)
822 int i;
824 if (EL_CAN_CEOL && GoodStr(T_ce))
825 (void) tputs(Str(T_ce), 1, term__putc);
826 else {
827 for (i = 0; i < num; i++)
828 term__putc(' ');
829 el->el_cursor.h += num; /* have written num spaces */
834 /* term_clear_screen():
835 * Clear the screen
837 protected void
838 term_clear_screen(EditLine *el)
839 { /* clear the whole screen and home */
841 if (GoodStr(T_cl))
842 /* send the clear screen code */
843 (void) tputs(Str(T_cl), Val(T_li), term__putc);
844 else if (GoodStr(T_ho) && GoodStr(T_cd)) {
845 (void) tputs(Str(T_ho), Val(T_li), term__putc); /* home */
846 /* clear to bottom of screen */
847 (void) tputs(Str(T_cd), Val(T_li), term__putc);
848 } else {
849 term__putc('\r');
850 term__putc('\n');
855 /* term_beep():
856 * Beep the way the terminal wants us
858 protected void
859 term_beep(EditLine *el)
861 if (GoodStr(T_bl))
862 /* what termcap says we should use */
863 (void) tputs(Str(T_bl), 1, term__putc);
864 else
865 term__putc('\007'); /* an ASCII bell; ^G */
869 #ifdef notdef
870 /* term_clear_to_bottom():
871 * Clear to the bottom of the screen
873 protected void
874 term_clear_to_bottom(EditLine *el)
876 if (GoodStr(T_cd))
877 (void) tputs(Str(T_cd), Val(T_li), term__putc);
878 else if (GoodStr(T_ce))
879 (void) tputs(Str(T_ce), Val(T_li), term__putc);
881 #endif
883 protected void
884 term_get(EditLine *el, const char **term)
886 *term = el->el_term.t_name;
890 /* term_set():
891 * Read in the terminal capabilities from the requested terminal
893 protected int
894 term_set(EditLine *el, const char *term)
896 int i;
897 char buf[TC_BUFSIZE];
898 char *area;
899 const struct termcapstr *t;
900 sigset_t oset, nset;
901 int lins, cols;
903 (void) sigemptyset(&nset);
904 (void) sigaddset(&nset, SIGWINCH);
905 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
907 area = buf;
910 if (term == NULL)
911 term = getenv("TERM");
913 if (!term || !term[0])
914 term = "dumb";
916 if (strcmp(term, "emacs") == 0)
917 el->el_flags |= EDIT_DISABLED;
919 memset(el->el_term.t_cap, 0, TC_BUFSIZE);
921 i = tgetent(el->el_term.t_cap, term);
923 if (i <= 0) {
924 if (i == -1)
925 (void) fprintf(el->el_errfile,
926 "Cannot read termcap database;\n");
927 else if (i == 0)
928 (void) fprintf(el->el_errfile,
929 "No entry for terminal type \"%s\";\n", term);
930 (void) fprintf(el->el_errfile,
931 "using dumb terminal settings.\n");
932 Val(T_co) = 80; /* do a dumb terminal */
933 Val(T_pt) = Val(T_km) = Val(T_li) = 0;
934 Val(T_xt) = Val(T_MT);
935 for (t = tstr; t->name != NULL; t++)
936 term_alloc(el, t, NULL);
937 } else {
938 /* auto/magic margins */
939 Val(T_am) = tgetflag("am");
940 Val(T_xn) = tgetflag("xn");
941 /* Can we tab */
942 Val(T_pt) = tgetflag("pt");
943 Val(T_xt) = tgetflag("xt");
944 /* do we have a meta? */
945 Val(T_km) = tgetflag("km");
946 Val(T_MT) = tgetflag("MT");
947 /* Get the size */
948 Val(T_co) = tgetnum("co");
949 Val(T_li) = tgetnum("li");
950 for (t = tstr; t->name != NULL; t++) {
951 /* XXX: some systems' tgetstr needs non const */
952 term_alloc(el, t, tgetstr(strchr(t->name, *t->name),
953 &area));
957 if (Val(T_co) < 2)
958 Val(T_co) = 80; /* just in case */
959 if (Val(T_li) < 1)
960 Val(T_li) = 24;
962 el->el_term.t_size.v = Val(T_co);
963 el->el_term.t_size.h = Val(T_li);
965 term_setflags(el);
967 /* get the correct window size */
968 (void) term_get_size(el, &lins, &cols);
969 if (term_change_size(el, lins, cols) == -1)
970 return (-1);
971 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
972 term_bind_arrow(el);
973 el->el_term.t_name = term;
974 return (i <= 0 ? -1 : 0);
978 /* term_get_size():
979 * Return the new window size in lines and cols, and
980 * true if the size was changed.
982 protected int
983 term_get_size(EditLine *el, int *lins, int *cols)
986 *cols = Val(T_co);
987 *lins = Val(T_li);
989 #ifdef TIOCGWINSZ
991 struct winsize ws;
992 if (ioctl(el->el_infd, TIOCGWINSZ, (ioctl_t) & ws) != -1) {
993 if (ws.ws_col)
994 *cols = ws.ws_col;
995 if (ws.ws_row)
996 *lins = ws.ws_row;
999 #endif
1000 #ifdef TIOCGSIZE
1002 struct ttysize ts;
1003 if (ioctl(el->el_infd, TIOCGSIZE, (ioctl_t) & ts) != -1) {
1004 if (ts.ts_cols)
1005 *cols = ts.ts_cols;
1006 if (ts.ts_lines)
1007 *lins = ts.ts_lines;
1010 #endif
1011 return (Val(T_co) != *cols || Val(T_li) != *lins);
1015 /* term_change_size():
1016 * Change the size of the terminal
1018 protected int
1019 term_change_size(EditLine *el, int lins, int cols)
1022 * Just in case
1024 Val(T_co) = (cols < 2) ? 80 : cols;
1025 Val(T_li) = (lins < 1) ? 24 : lins;
1027 /* re-make display buffers */
1028 if (term_rebuffer_display(el) == -1)
1029 return (-1);
1030 re_clear_display(el);
1031 return (0);
1035 /* term_init_arrow():
1036 * Initialize the arrow key bindings from termcap
1038 private void
1039 term_init_arrow(EditLine *el)
1041 fkey_t *arrow = el->el_term.t_fkey;
1043 arrow[A_K_DN].name = "down";
1044 arrow[A_K_DN].key = T_kd;
1045 arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
1046 arrow[A_K_DN].type = XK_CMD;
1048 arrow[A_K_UP].name = "up";
1049 arrow[A_K_UP].key = T_ku;
1050 arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
1051 arrow[A_K_UP].type = XK_CMD;
1053 arrow[A_K_LT].name = "left";
1054 arrow[A_K_LT].key = T_kl;
1055 arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
1056 arrow[A_K_LT].type = XK_CMD;
1058 arrow[A_K_RT].name = "right";
1059 arrow[A_K_RT].key = T_kr;
1060 arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
1061 arrow[A_K_RT].type = XK_CMD;
1063 arrow[A_K_HO].name = "home";
1064 arrow[A_K_HO].key = T_kh;
1065 arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
1066 arrow[A_K_HO].type = XK_CMD;
1068 arrow[A_K_EN].name = "end";
1069 arrow[A_K_EN].key = T_at7;
1070 arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
1071 arrow[A_K_EN].type = XK_CMD;
1075 /* term_reset_arrow():
1076 * Reset arrow key bindings
1078 private void
1079 term_reset_arrow(EditLine *el)
1081 fkey_t *arrow = el->el_term.t_fkey;
1082 static const char strA[] = {033, '[', 'A', '\0'};
1083 static const char strB[] = {033, '[', 'B', '\0'};
1084 static const char strC[] = {033, '[', 'C', '\0'};
1085 static const char strD[] = {033, '[', 'D', '\0'};
1086 static const char strH[] = {033, '[', 'H', '\0'};
1087 static const char strF[] = {033, '[', 'F', '\0'};
1088 static const char stOA[] = {033, 'O', 'A', '\0'};
1089 static const char stOB[] = {033, 'O', 'B', '\0'};
1090 static const char stOC[] = {033, 'O', 'C', '\0'};
1091 static const char stOD[] = {033, 'O', 'D', '\0'};
1092 static const char stOH[] = {033, 'O', 'H', '\0'};
1093 static const char stOF[] = {033, 'O', 'F', '\0'};
1095 key_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1096 key_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1097 key_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1098 key_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1099 key_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1100 key_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1101 key_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1102 key_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1103 key_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1104 key_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1105 key_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1106 key_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1108 if (el->el_map.type == MAP_VI) {
1109 key_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1110 key_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1111 key_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1112 key_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1113 key_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1114 key_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1115 key_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1116 key_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1117 key_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1118 key_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1119 key_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1120 key_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1125 /* term_set_arrow():
1126 * Set an arrow key binding
1128 protected int
1129 term_set_arrow(EditLine *el, const char *name, key_value_t *fun, int type)
1131 fkey_t *arrow = el->el_term.t_fkey;
1132 int i;
1134 for (i = 0; i < A_K_NKEYS; i++)
1135 if (strcmp(name, arrow[i].name) == 0) {
1136 arrow[i].fun = *fun;
1137 arrow[i].type = type;
1138 return (0);
1140 return (-1);
1144 /* term_clear_arrow():
1145 * Clear an arrow key binding
1147 protected int
1148 term_clear_arrow(EditLine *el, const char *name)
1150 fkey_t *arrow = el->el_term.t_fkey;
1151 int i;
1153 for (i = 0; i < A_K_NKEYS; i++)
1154 if (strcmp(name, arrow[i].name) == 0) {
1155 arrow[i].type = XK_NOD;
1156 return (0);
1158 return (-1);
1162 /* term_print_arrow():
1163 * Print the arrow key bindings
1165 protected void
1166 term_print_arrow(EditLine *el, const char *name)
1168 int i;
1169 fkey_t *arrow = el->el_term.t_fkey;
1171 for (i = 0; i < A_K_NKEYS; i++)
1172 if (*name == '\0' || strcmp(name, arrow[i].name) == 0)
1173 if (arrow[i].type != XK_NOD)
1174 key_kprint(el, arrow[i].name, &arrow[i].fun,
1175 arrow[i].type);
1179 /* term_bind_arrow():
1180 * Bind the arrow keys
1182 protected void
1183 term_bind_arrow(EditLine *el)
1185 el_action_t *map;
1186 const el_action_t *dmap;
1187 int i, j;
1188 char *p;
1189 fkey_t *arrow = el->el_term.t_fkey;
1191 /* Check if the components needed are initialized */
1192 if (el->el_term.t_buf == NULL || el->el_map.key == NULL)
1193 return;
1195 map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
1196 dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
1198 term_reset_arrow(el);
1200 for (i = 0; i < A_K_NKEYS; i++) {
1201 p = el->el_term.t_str[arrow[i].key];
1202 if (p && *p) {
1203 j = (unsigned char) *p;
1205 * Assign the arrow keys only if:
1207 * 1. They are multi-character arrow keys and the user
1208 * has not re-assigned the leading character, or
1209 * has re-assigned the leading character to be
1210 * ED_SEQUENCE_LEAD_IN
1211 * 2. They are single arrow keys pointing to an
1212 * unassigned key.
1214 if (arrow[i].type == XK_NOD)
1215 key_clear(el, map, p);
1216 else {
1217 if (p[1] && (dmap[j] == map[j] ||
1218 map[j] == ED_SEQUENCE_LEAD_IN)) {
1219 key_add(el, p, &arrow[i].fun,
1220 arrow[i].type);
1221 map[j] = ED_SEQUENCE_LEAD_IN;
1222 } else if (map[j] == ED_UNASSIGNED) {
1223 key_clear(el, map, p);
1224 if (arrow[i].type == XK_CMD)
1225 map[j] = arrow[i].fun.cmd;
1226 else
1227 key_add(el, p, &arrow[i].fun,
1228 arrow[i].type);
1236 /* term__putc():
1237 * Add a character
1239 protected int
1240 term__putc(int c)
1243 return (fputc(c, term_outfile));
1247 /* term__flush():
1248 * Flush output
1250 protected void
1251 term__flush(void)
1254 (void) fflush(term_outfile);
1257 /* term_writec():
1258 * Write the given character out, in a human readable form
1260 protected void
1261 term_writec(EditLine *el, int c)
1263 char buf[8];
1264 int cnt = key__decode_char(buf, sizeof(buf), 0, c);
1265 buf[cnt] = '\0';
1266 term_overwrite(el, buf, cnt);
1267 term__flush();
1271 /* term_telltc():
1272 * Print the current termcap characteristics
1274 protected int
1275 /*ARGSUSED*/
1276 term_telltc(EditLine *el, int argc __attribute__((__unused__)),
1277 const char **argv __attribute__((__unused__)))
1279 const struct termcapstr *t;
1280 char **ts;
1281 char upbuf[EL_BUFSIZ];
1283 (void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
1284 (void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
1285 (void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
1286 Val(T_co), Val(T_li));
1287 (void) fprintf(el->el_outfile,
1288 "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
1289 (void) fprintf(el->el_outfile,
1290 "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
1291 (void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
1292 EL_HAS_AUTO_MARGINS ? "has" : "does not have");
1293 if (EL_HAS_AUTO_MARGINS)
1294 (void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
1295 EL_HAS_MAGIC_MARGINS ? "has" : "does not have");
1297 for (t = tstr, ts = el->el_term.t_str; t->name != NULL; t++, ts++) {
1298 const char *ub;
1299 if (*ts && **ts) {
1300 (void) key__decode_str(*ts, upbuf, sizeof(upbuf), "");
1301 ub = upbuf;
1302 } else {
1303 ub = "(empty)";
1305 (void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n",
1306 t->long_name, t->name, ub);
1308 (void) fputc('\n', el->el_outfile);
1309 return (0);
1313 /* term_settc():
1314 * Change the current terminal characteristics
1316 protected int
1317 /*ARGSUSED*/
1318 term_settc(EditLine *el, int argc __attribute__((__unused__)),
1319 const char **argv)
1321 const struct termcapstr *ts;
1322 const struct termcapval *tv;
1323 const char *what, *how;
1325 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1326 return -1;
1328 what = argv[1];
1329 how = argv[2];
1332 * Do the strings first
1334 for (ts = tstr; ts->name != NULL; ts++)
1335 if (strcmp(ts->name, what) == 0)
1336 break;
1338 if (ts->name != NULL) {
1339 term_alloc(el, ts, how);
1340 term_setflags(el);
1341 return 0;
1344 * Do the numeric ones second
1346 for (tv = tval; tv->name != NULL; tv++)
1347 if (strcmp(tv->name, what) == 0)
1348 break;
1350 if (tv->name != NULL)
1351 return -1;
1353 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1354 tv == &tval[T_am] || tv == &tval[T_xn]) {
1355 if (strcmp(how, "yes") == 0)
1356 el->el_term.t_val[tv - tval] = 1;
1357 else if (strcmp(how, "no") == 0)
1358 el->el_term.t_val[tv - tval] = 0;
1359 else {
1360 (void) fprintf(el->el_errfile,
1361 "%s: Bad value `%s'.\n", argv[0], how);
1362 return -1;
1364 term_setflags(el);
1365 if (term_change_size(el, Val(T_li), Val(T_co)) == -1)
1366 return -1;
1367 return 0;
1368 } else {
1369 long i;
1370 char *ep;
1372 i = strtol(how, &ep, 10);
1373 if (*ep != '\0') {
1374 (void) fprintf(el->el_errfile,
1375 "%s: Bad value `%s'.\n", argv[0], how);
1376 return -1;
1378 el->el_term.t_val[tv - tval] = (int) i;
1379 el->el_term.t_size.v = Val(T_co);
1380 el->el_term.t_size.h = Val(T_li);
1381 if (tv == &tval[T_co] || tv == &tval[T_li])
1382 if (term_change_size(el, Val(T_li), Val(T_co))
1383 == -1)
1384 return -1;
1385 return 0;
1390 /* term_gettc():
1391 * Get the current terminal characteristics
1393 protected int
1394 /*ARGSUSED*/
1395 term_gettc(EditLine *el, int argc __attribute__((__unused__)), char **argv)
1397 const struct termcapstr *ts;
1398 const struct termcapval *tv;
1399 char *what;
1400 void *how;
1402 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1403 return (-1);
1405 what = argv[1];
1406 how = argv[2];
1409 * Do the strings first
1411 for (ts = tstr; ts->name != NULL; ts++)
1412 if (strcmp(ts->name, what) == 0)
1413 break;
1415 if (ts->name != NULL) {
1416 *(char **)how = el->el_term.t_str[ts - tstr];
1417 return 0;
1420 * Do the numeric ones second
1422 for (tv = tval; tv->name != NULL; tv++)
1423 if (strcmp(tv->name, what) == 0)
1424 break;
1426 if (tv->name == NULL)
1427 return -1;
1429 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1430 tv == &tval[T_am] || tv == &tval[T_xn]) {
1431 static char yes[] = "yes";
1432 static char no[] = "no";
1433 if (el->el_term.t_val[tv - tval])
1434 *(char **)how = yes;
1435 else
1436 *(char **)how = no;
1437 return 0;
1438 } else {
1439 *(int *)how = el->el_term.t_val[tv - tval];
1440 return 0;
1444 /* term_echotc():
1445 * Print the termcap string out with variable substitution
1447 protected int
1448 /*ARGSUSED*/
1449 term_echotc(EditLine *el, int argc __attribute__((__unused__)),
1450 const char **argv)
1452 char *cap, *scap, *ep;
1453 int arg_need, arg_cols, arg_rows;
1454 int verbose = 0, silent = 0;
1455 char *area;
1456 static const char fmts[] = "%s\n", fmtd[] = "%d\n";
1457 const struct termcapstr *t;
1458 char buf[TC_BUFSIZE];
1459 long i;
1461 area = buf;
1463 if (argv == NULL || argv[1] == NULL)
1464 return (-1);
1465 argv++;
1467 if (argv[0][0] == '-') {
1468 switch (argv[0][1]) {
1469 case 'v':
1470 verbose = 1;
1471 break;
1472 case 's':
1473 silent = 1;
1474 break;
1475 default:
1476 /* stderror(ERR_NAME | ERR_TCUSAGE); */
1477 break;
1479 argv++;
1481 if (!*argv || *argv[0] == '\0')
1482 return (0);
1483 if (strcmp(*argv, "tabs") == 0) {
1484 (void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
1485 return (0);
1486 } else if (strcmp(*argv, "meta") == 0) {
1487 (void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
1488 return (0);
1489 } else if (strcmp(*argv, "xn") == 0) {
1490 (void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ?
1491 "yes" : "no");
1492 return (0);
1493 } else if (strcmp(*argv, "am") == 0) {
1494 (void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ?
1495 "yes" : "no");
1496 return (0);
1497 } else if (strcmp(*argv, "baud") == 0) {
1498 #ifdef notdef
1499 int i;
1501 for (i = 0; baud_rate[i].b_name != NULL; i++)
1502 if (el->el_tty.t_speed == baud_rate[i].b_rate) {
1503 (void) fprintf(el->el_outfile, fmts,
1504 baud_rate[i].b_name);
1505 return (0);
1507 (void) fprintf(el->el_outfile, fmtd, 0);
1508 #else
1509 (void) fprintf(el->el_outfile, fmtd, (int)el->el_tty.t_speed);
1510 #endif
1511 return (0);
1512 } else if (strcmp(*argv, "rows") == 0 || strcmp(*argv, "lines") == 0) {
1513 (void) fprintf(el->el_outfile, fmtd, Val(T_li));
1514 return (0);
1515 } else if (strcmp(*argv, "cols") == 0) {
1516 (void) fprintf(el->el_outfile, fmtd, Val(T_co));
1517 return (0);
1520 * Try to use our local definition first
1522 scap = NULL;
1523 for (t = tstr; t->name != NULL; t++)
1524 if (strcmp(t->name, *argv) == 0) {
1525 scap = el->el_term.t_str[t - tstr];
1526 break;
1528 if (t->name == NULL) {
1529 /* XXX: some systems' tgetstr needs non const */
1530 scap = tgetstr(strchr(*argv, **argv), &area);
1532 if (!scap || scap[0] == '\0') {
1533 if (!silent)
1534 (void) fprintf(el->el_errfile,
1535 "echotc: Termcap parameter `%s' not found.\n",
1536 *argv);
1537 return (-1);
1540 * Count home many values we need for this capability.
1542 for (cap = scap, arg_need = 0; *cap; cap++)
1543 if (*cap == '%')
1544 switch (*++cap) {
1545 case 'd':
1546 case '2':
1547 case '3':
1548 case '.':
1549 case '+':
1550 arg_need++;
1551 break;
1552 case '%':
1553 case '>':
1554 case 'i':
1555 case 'r':
1556 case 'n':
1557 case 'B':
1558 case 'D':
1559 break;
1560 default:
1562 * hpux has lot's of them...
1564 if (verbose)
1565 (void) fprintf(el->el_errfile,
1566 "echotc: Warning: unknown termcap %% `%c'.\n",
1567 *cap);
1568 /* This is bad, but I won't complain */
1569 break;
1572 switch (arg_need) {
1573 case 0:
1574 argv++;
1575 if (*argv && *argv[0]) {
1576 if (!silent)
1577 (void) fprintf(el->el_errfile,
1578 "echotc: Warning: Extra argument `%s'.\n",
1579 *argv);
1580 return (-1);
1582 (void) tputs(scap, 1, term__putc);
1583 break;
1584 case 1:
1585 argv++;
1586 if (!*argv || *argv[0] == '\0') {
1587 if (!silent)
1588 (void) fprintf(el->el_errfile,
1589 "echotc: Warning: Missing argument.\n");
1590 return (-1);
1592 arg_cols = 0;
1593 i = strtol(*argv, &ep, 10);
1594 if (*ep != '\0' || i < 0) {
1595 if (!silent)
1596 (void) fprintf(el->el_errfile,
1597 "echotc: Bad value `%s' for rows.\n",
1598 *argv);
1599 return (-1);
1601 arg_rows = (int) i;
1602 argv++;
1603 if (*argv && *argv[0]) {
1604 if (!silent)
1605 (void) fprintf(el->el_errfile,
1606 "echotc: Warning: Extra argument `%s'.\n",
1607 *argv);
1608 return (-1);
1610 (void) tputs(tgoto(scap, arg_cols, arg_rows), 1, term__putc);
1611 break;
1612 default:
1613 /* This is wrong, but I will ignore it... */
1614 if (verbose)
1615 (void) fprintf(el->el_errfile,
1616 "echotc: Warning: Too many required arguments (%d).\n",
1617 arg_need);
1618 /* FALLTHROUGH */
1619 case 2:
1620 argv++;
1621 if (!*argv || *argv[0] == '\0') {
1622 if (!silent)
1623 (void) fprintf(el->el_errfile,
1624 "echotc: Warning: Missing argument.\n");
1625 return (-1);
1627 i = strtol(*argv, &ep, 10);
1628 if (*ep != '\0' || i < 0) {
1629 if (!silent)
1630 (void) fprintf(el->el_errfile,
1631 "echotc: Bad value `%s' for cols.\n",
1632 *argv);
1633 return (-1);
1635 arg_cols = (int) i;
1636 argv++;
1637 if (!*argv || *argv[0] == '\0') {
1638 if (!silent)
1639 (void) fprintf(el->el_errfile,
1640 "echotc: Warning: Missing argument.\n");
1641 return (-1);
1643 i = strtol(*argv, &ep, 10);
1644 if (*ep != '\0' || i < 0) {
1645 if (!silent)
1646 (void) fprintf(el->el_errfile,
1647 "echotc: Bad value `%s' for rows.\n",
1648 *argv);
1649 return (-1);
1651 arg_rows = (int) i;
1652 if (*ep != '\0') {
1653 if (!silent)
1654 (void) fprintf(el->el_errfile,
1655 "echotc: Bad value `%s'.\n", *argv);
1656 return (-1);
1658 argv++;
1659 if (*argv && *argv[0]) {
1660 if (!silent)
1661 (void) fprintf(el->el_errfile,
1662 "echotc: Warning: Extra argument `%s'.\n",
1663 *argv);
1664 return (-1);
1666 (void) tputs(tgoto(scap, arg_cols, arg_rows), arg_rows,
1667 term__putc);
1668 break;
1670 return (0);