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