1*7a0279c0Schristos /* $NetBSD: terminal.c,v 1.46 2023/02/04 14:34:28 christos Exp $ */
298c7cbebSchristos
398c7cbebSchristos /*-
498c7cbebSchristos * Copyright (c) 1992, 1993
598c7cbebSchristos * The Regents of the University of California. All rights reserved.
698c7cbebSchristos *
798c7cbebSchristos * This code is derived from software contributed to Berkeley by
898c7cbebSchristos * Christos Zoulas of Cornell University.
998c7cbebSchristos *
1098c7cbebSchristos * Redistribution and use in source and binary forms, with or without
1198c7cbebSchristos * modification, are permitted provided that the following conditions
1298c7cbebSchristos * are met:
1398c7cbebSchristos * 1. Redistributions of source code must retain the above copyright
1498c7cbebSchristos * notice, this list of conditions and the following disclaimer.
1598c7cbebSchristos * 2. Redistributions in binary form must reproduce the above copyright
1698c7cbebSchristos * notice, this list of conditions and the following disclaimer in the
1798c7cbebSchristos * documentation and/or other materials provided with the distribution.
1898c7cbebSchristos * 3. Neither the name of the University nor the names of its contributors
1998c7cbebSchristos * may be used to endorse or promote products derived from this software
2098c7cbebSchristos * without specific prior written permission.
2198c7cbebSchristos *
2298c7cbebSchristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2398c7cbebSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2498c7cbebSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2598c7cbebSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2698c7cbebSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2798c7cbebSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2898c7cbebSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2998c7cbebSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3098c7cbebSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3198c7cbebSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3298c7cbebSchristos * SUCH DAMAGE.
3398c7cbebSchristos */
3498c7cbebSchristos
3598c7cbebSchristos #include "config.h"
3698c7cbebSchristos #if !defined(lint) && !defined(SCCSID)
3798c7cbebSchristos #if 0
3898c7cbebSchristos static char sccsid[] = "@(#)term.c 8.2 (Berkeley) 4/30/95";
3998c7cbebSchristos #else
40*7a0279c0Schristos __RCSID("$NetBSD: terminal.c,v 1.46 2023/02/04 14:34:28 christos Exp $");
4198c7cbebSchristos #endif
4298c7cbebSchristos #endif /* not lint && not SCCSID */
4398c7cbebSchristos
4498c7cbebSchristos /*
457da8639eSchristos * terminal.c: Editor/termcap-curses interface
4698c7cbebSchristos * We have to declare a static variable here, since the
4798c7cbebSchristos * termcap putchar routine does not take an argument!
4898c7cbebSchristos */
4922383670Schristos #include <sys/types.h>
5022383670Schristos #include <sys/ioctl.h>
5198c7cbebSchristos #include <limits.h>
5222383670Schristos #include <signal.h>
5322383670Schristos #include <stdio.h>
5422383670Schristos #include <stdlib.h>
5522383670Schristos #include <string.h>
5622383670Schristos #include <unistd.h>
5798c7cbebSchristos #ifdef HAVE_TERMCAP_H
5898c7cbebSchristos #include <termcap.h>
5998c7cbebSchristos #endif
6098c7cbebSchristos #ifdef HAVE_CURSES_H
6198c7cbebSchristos #include <curses.h>
6298c7cbebSchristos #elif HAVE_NCURSES_H
6398c7cbebSchristos #include <ncurses.h>
6498c7cbebSchristos #endif
6598c7cbebSchristos
6698c7cbebSchristos /* Solaris's term.h does horrid things. */
67d22248f8Schristos #if defined(HAVE_TERM_H) && !defined(__sun) && !defined(HAVE_TERMCAP_H)
6898c7cbebSchristos #include <term.h>
6998c7cbebSchristos #endif
7098c7cbebSchristos
71*7a0279c0Schristos #if defined(__sun)
72*7a0279c0Schristos extern int tgetent(char *, const char *);
73*7a0279c0Schristos extern int tgetflag(char *);
74*7a0279c0Schristos extern int tgetnum(char *);
75*7a0279c0Schristos extern int tputs(const char *, int, int (*)(int));
76*7a0279c0Schristos extern char* tgoto(const char*, int, int);
77*7a0279c0Schristos extern char* tgetstr(char*, char**);
78*7a0279c0Schristos #endif
79*7a0279c0Schristos
8098c7cbebSchristos #ifdef _REENTRANT
8198c7cbebSchristos #include <pthread.h>
8298c7cbebSchristos #endif
8398c7cbebSchristos
8498c7cbebSchristos #include "el.h"
854fc1f47dSchristos #include "fcns.h"
8698c7cbebSchristos
8798c7cbebSchristos /*
8898c7cbebSchristos * IMPORTANT NOTE: these routines are allowed to look at the current screen
8998c7cbebSchristos * and the current position assuming that it is correct. If this is not
9098c7cbebSchristos * true, then the update will be WRONG! This is (should be) a valid
9198c7cbebSchristos * assumption...
9298c7cbebSchristos */
9398c7cbebSchristos
94c11bd863Schristos #define TC_BUFSIZE ((size_t)2048)
9598c7cbebSchristos
9698c7cbebSchristos #define GoodStr(a) (el->el_terminal.t_str[a] != NULL && \
9798c7cbebSchristos el->el_terminal.t_str[a][0] != '\0')
9898c7cbebSchristos #define Str(a) el->el_terminal.t_str[a]
9998c7cbebSchristos #define Val(a) el->el_terminal.t_val[a]
10098c7cbebSchristos
101469d44f8Schristos static const struct termcapstr {
10298c7cbebSchristos const char *name;
10398c7cbebSchristos const char *long_name;
10498c7cbebSchristos } tstr[] = {
10598c7cbebSchristos #define T_al 0
10698c7cbebSchristos { "al", "add new blank line" },
10798c7cbebSchristos #define T_bl 1
10898c7cbebSchristos { "bl", "audible bell" },
10998c7cbebSchristos #define T_cd 2
11098c7cbebSchristos { "cd", "clear to bottom" },
11198c7cbebSchristos #define T_ce 3
11298c7cbebSchristos { "ce", "clear to end of line" },
11398c7cbebSchristos #define T_ch 4
11498c7cbebSchristos { "ch", "cursor to horiz pos" },
11598c7cbebSchristos #define T_cl 5
11698c7cbebSchristos { "cl", "clear screen" },
11798c7cbebSchristos #define T_dc 6
11898c7cbebSchristos { "dc", "delete a character" },
11998c7cbebSchristos #define T_dl 7
12098c7cbebSchristos { "dl", "delete a line" },
12198c7cbebSchristos #define T_dm 8
12298c7cbebSchristos { "dm", "start delete mode" },
12398c7cbebSchristos #define T_ed 9
12498c7cbebSchristos { "ed", "end delete mode" },
12598c7cbebSchristos #define T_ei 10
12698c7cbebSchristos { "ei", "end insert mode" },
12798c7cbebSchristos #define T_fs 11
12898c7cbebSchristos { "fs", "cursor from status line" },
12998c7cbebSchristos #define T_ho 12
13098c7cbebSchristos { "ho", "home cursor" },
13198c7cbebSchristos #define T_ic 13
13298c7cbebSchristos { "ic", "insert character" },
13398c7cbebSchristos #define T_im 14
13498c7cbebSchristos { "im", "start insert mode" },
13598c7cbebSchristos #define T_ip 15
13698c7cbebSchristos { "ip", "insert padding" },
13798c7cbebSchristos #define T_kd 16
13898c7cbebSchristos { "kd", "sends cursor down" },
13998c7cbebSchristos #define T_kl 17
14098c7cbebSchristos { "kl", "sends cursor left" },
14198c7cbebSchristos #define T_kr 18
14298c7cbebSchristos { "kr", "sends cursor right" },
14398c7cbebSchristos #define T_ku 19
14498c7cbebSchristos { "ku", "sends cursor up" },
14598c7cbebSchristos #define T_md 20
14698c7cbebSchristos { "md", "begin bold" },
14798c7cbebSchristos #define T_me 21
14898c7cbebSchristos { "me", "end attributes" },
14998c7cbebSchristos #define T_nd 22
15098c7cbebSchristos { "nd", "non destructive space" },
15198c7cbebSchristos #define T_se 23
15298c7cbebSchristos { "se", "end standout" },
15398c7cbebSchristos #define T_so 24
15498c7cbebSchristos { "so", "begin standout" },
15598c7cbebSchristos #define T_ts 25
15698c7cbebSchristos { "ts", "cursor to status line" },
15798c7cbebSchristos #define T_up 26
15898c7cbebSchristos { "up", "cursor up one" },
15998c7cbebSchristos #define T_us 27
16098c7cbebSchristos { "us", "begin underline" },
16198c7cbebSchristos #define T_ue 28
16298c7cbebSchristos { "ue", "end underline" },
16398c7cbebSchristos #define T_vb 29
16498c7cbebSchristos { "vb", "visible bell" },
16598c7cbebSchristos #define T_DC 30
16698c7cbebSchristos { "DC", "delete multiple chars" },
16798c7cbebSchristos #define T_DO 31
16898c7cbebSchristos { "DO", "cursor down multiple" },
16998c7cbebSchristos #define T_IC 32
17098c7cbebSchristos { "IC", "insert multiple chars" },
17198c7cbebSchristos #define T_LE 33
17298c7cbebSchristos { "LE", "cursor left multiple" },
17398c7cbebSchristos #define T_RI 34
17498c7cbebSchristos { "RI", "cursor right multiple" },
17598c7cbebSchristos #define T_UP 35
17698c7cbebSchristos { "UP", "cursor up multiple" },
17798c7cbebSchristos #define T_kh 36
17898c7cbebSchristos { "kh", "send cursor home" },
17998c7cbebSchristos #define T_at7 37
18098c7cbebSchristos { "@7", "send cursor end" },
181fac360ffSchristos #define T_kD 38
182fac360ffSchristos { "kD", "send cursor delete" },
183fac360ffSchristos #define T_str 39
18498c7cbebSchristos { NULL, NULL }
18598c7cbebSchristos };
18698c7cbebSchristos
187469d44f8Schristos static const struct termcapval {
18898c7cbebSchristos const char *name;
18998c7cbebSchristos const char *long_name;
19098c7cbebSchristos } tval[] = {
19198c7cbebSchristos #define T_am 0
19298c7cbebSchristos { "am", "has automatic margins" },
19398c7cbebSchristos #define T_pt 1
19498c7cbebSchristos { "pt", "has physical tabs" },
19598c7cbebSchristos #define T_li 2
19698c7cbebSchristos { "li", "Number of lines" },
19798c7cbebSchristos #define T_co 3
19898c7cbebSchristos { "co", "Number of columns" },
19998c7cbebSchristos #define T_km 4
20098c7cbebSchristos { "km", "Has meta key" },
20198c7cbebSchristos #define T_xt 5
20298c7cbebSchristos { "xt", "Tab chars destructive" },
20398c7cbebSchristos #define T_xn 6
20498c7cbebSchristos { "xn", "newline ignored at right margin" },
20598c7cbebSchristos #define T_MT 7
20698c7cbebSchristos { "MT", "Has meta key" }, /* XXX? */
20798c7cbebSchristos #define T_val 8
20898c7cbebSchristos { NULL, NULL, }
20998c7cbebSchristos };
21098c7cbebSchristos /* do two or more of the attributes use me */
21198c7cbebSchristos
212469d44f8Schristos static void terminal_setflags(EditLine *);
213469d44f8Schristos static int terminal_rebuffer_display(EditLine *);
214469d44f8Schristos static void terminal_free_display(EditLine *);
215469d44f8Schristos static int terminal_alloc_display(EditLine *);
216469d44f8Schristos static void terminal_alloc(EditLine *, const struct termcapstr *,
2177da8639eSchristos const char *);
218469d44f8Schristos static void terminal_init_arrow(EditLine *);
219469d44f8Schristos static void terminal_reset_arrow(EditLine *);
220469d44f8Schristos static int terminal_putc(int);
221469d44f8Schristos static void terminal_tputs(EditLine *, const char *, int);
22298c7cbebSchristos
22398c7cbebSchristos #ifdef _REENTRANT
224469d44f8Schristos static pthread_mutex_t terminal_mutex = PTHREAD_MUTEX_INITIALIZER;
22598c7cbebSchristos #endif
226469d44f8Schristos static FILE *terminal_outfile = NULL;
22798c7cbebSchristos
22898c7cbebSchristos
22998c7cbebSchristos /* terminal_setflags():
23098c7cbebSchristos * Set the terminal capability flags
23198c7cbebSchristos */
232469d44f8Schristos static void
terminal_setflags(EditLine * el)23398c7cbebSchristos terminal_setflags(EditLine *el)
23498c7cbebSchristos {
23598c7cbebSchristos EL_FLAGS = 0;
23698c7cbebSchristos if (el->el_tty.t_tabs)
23798c7cbebSchristos EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
23898c7cbebSchristos
23998c7cbebSchristos EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
24098c7cbebSchristos EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
24198c7cbebSchristos EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
24298c7cbebSchristos EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
24398c7cbebSchristos TERM_CAN_INSERT : 0;
24498c7cbebSchristos EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
24598c7cbebSchristos EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0;
24698c7cbebSchristos EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0;
24798c7cbebSchristos
24898c7cbebSchristos if (GoodStr(T_me) && GoodStr(T_ue))
24998c7cbebSchristos EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ?
25098c7cbebSchristos TERM_CAN_ME : 0;
25198c7cbebSchristos else
25298c7cbebSchristos EL_FLAGS &= ~TERM_CAN_ME;
25398c7cbebSchristos if (GoodStr(T_me) && GoodStr(T_se))
25498c7cbebSchristos EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ?
25598c7cbebSchristos TERM_CAN_ME : 0;
25698c7cbebSchristos
25798c7cbebSchristos
25898c7cbebSchristos #ifdef DEBUG_SCREEN
25998c7cbebSchristos if (!EL_CAN_UP) {
26098c7cbebSchristos (void) fprintf(el->el_errfile,
26198c7cbebSchristos "WARNING: Your terminal cannot move up.\n");
26298c7cbebSchristos (void) fprintf(el->el_errfile,
26398c7cbebSchristos "Editing may be odd for long lines.\n");
26498c7cbebSchristos }
26598c7cbebSchristos if (!EL_CAN_CEOL)
26698c7cbebSchristos (void) fprintf(el->el_errfile, "no clear EOL capability.\n");
26798c7cbebSchristos if (!EL_CAN_DELETE)
26898c7cbebSchristos (void) fprintf(el->el_errfile, "no delete char capability.\n");
26998c7cbebSchristos if (!EL_CAN_INSERT)
27098c7cbebSchristos (void) fprintf(el->el_errfile, "no insert char capability.\n");
27198c7cbebSchristos #endif /* DEBUG_SCREEN */
27298c7cbebSchristos }
27398c7cbebSchristos
27498c7cbebSchristos /* terminal_init():
27598c7cbebSchristos * Initialize the terminal stuff
27698c7cbebSchristos */
277a2d6b270Schristos libedit_private int
terminal_init(EditLine * el)27898c7cbebSchristos terminal_init(EditLine *el)
27998c7cbebSchristos {
28098c7cbebSchristos
281113f06a3Schristos el->el_terminal.t_buf = el_calloc(TC_BUFSIZE,
282a13cd756Schristos sizeof(*el->el_terminal.t_buf));
28398c7cbebSchristos if (el->el_terminal.t_buf == NULL)
28421a4b1ccSchristos return -1;
285113f06a3Schristos el->el_terminal.t_cap = el_calloc(TC_BUFSIZE,
286a13cd756Schristos sizeof(*el->el_terminal.t_cap));
28798c7cbebSchristos if (el->el_terminal.t_cap == NULL)
28821a4b1ccSchristos goto out;
289113f06a3Schristos el->el_terminal.t_fkey = el_calloc(A_K_NKEYS,
290a13cd756Schristos sizeof(*el->el_terminal.t_fkey));
29198c7cbebSchristos if (el->el_terminal.t_fkey == NULL)
29221a4b1ccSchristos goto out;
29398c7cbebSchristos el->el_terminal.t_loc = 0;
294113f06a3Schristos el->el_terminal.t_str = el_calloc(T_str,
295a13cd756Schristos sizeof(*el->el_terminal.t_str));
29698c7cbebSchristos if (el->el_terminal.t_str == NULL)
29721a4b1ccSchristos goto out;
298113f06a3Schristos el->el_terminal.t_val = el_calloc(T_val,
299a13cd756Schristos sizeof(*el->el_terminal.t_val));
30098c7cbebSchristos if (el->el_terminal.t_val == NULL)
30121a4b1ccSchristos goto out;
30298c7cbebSchristos (void) terminal_set(el, NULL);
30398c7cbebSchristos terminal_init_arrow(el);
304b71bed95Schristos return 0;
30521a4b1ccSchristos out:
30621a4b1ccSchristos terminal_end(el);
307efeef4e5Schristos return -1;
30898c7cbebSchristos }
30998c7cbebSchristos
31098c7cbebSchristos /* terminal_end():
31198c7cbebSchristos * Clean up the terminal stuff
31298c7cbebSchristos */
313a2d6b270Schristos libedit_private void
terminal_end(EditLine * el)31498c7cbebSchristos terminal_end(EditLine *el)
31598c7cbebSchristos {
31698c7cbebSchristos
317a13cd756Schristos el_free(el->el_terminal.t_buf);
31898c7cbebSchristos el->el_terminal.t_buf = NULL;
319a13cd756Schristos el_free(el->el_terminal.t_cap);
32098c7cbebSchristos el->el_terminal.t_cap = NULL;
32198c7cbebSchristos el->el_terminal.t_loc = 0;
322a13cd756Schristos el_free(el->el_terminal.t_str);
32398c7cbebSchristos el->el_terminal.t_str = NULL;
324a13cd756Schristos el_free(el->el_terminal.t_val);
32598c7cbebSchristos el->el_terminal.t_val = NULL;
326a13cd756Schristos el_free(el->el_terminal.t_fkey);
32798c7cbebSchristos el->el_terminal.t_fkey = NULL;
32898c7cbebSchristos terminal_free_display(el);
32998c7cbebSchristos }
33098c7cbebSchristos
33198c7cbebSchristos
33298c7cbebSchristos /* terminal_alloc():
33398c7cbebSchristos * Maintain a string pool for termcap strings
33498c7cbebSchristos */
335469d44f8Schristos static void
terminal_alloc(EditLine * el,const struct termcapstr * t,const char * cap)33698c7cbebSchristos terminal_alloc(EditLine *el, const struct termcapstr *t, const char *cap)
33798c7cbebSchristos {
33898c7cbebSchristos char termbuf[TC_BUFSIZE];
33998c7cbebSchristos size_t tlen, clen;
34098c7cbebSchristos char **tlist = el->el_terminal.t_str;
34198c7cbebSchristos char **tmp, **str = &tlist[t - tstr];
34298c7cbebSchristos
34336e7768bSchristos (void) memset(termbuf, 0, sizeof(termbuf));
34498c7cbebSchristos if (cap == NULL || *cap == '\0') {
34598c7cbebSchristos *str = NULL;
34698c7cbebSchristos return;
34798c7cbebSchristos } else
34898c7cbebSchristos clen = strlen(cap);
34998c7cbebSchristos
35098c7cbebSchristos tlen = *str == NULL ? 0 : strlen(*str);
35198c7cbebSchristos
35298c7cbebSchristos /*
35398c7cbebSchristos * New string is shorter; no need to allocate space
35498c7cbebSchristos */
35598c7cbebSchristos if (clen <= tlen) {
35698c7cbebSchristos if (*str)
35798c7cbebSchristos (void) strcpy(*str, cap); /* XXX strcpy is safe */
35898c7cbebSchristos return;
35998c7cbebSchristos }
36098c7cbebSchristos /*
36198c7cbebSchristos * New string is longer; see if we have enough space to append
36298c7cbebSchristos */
36398c7cbebSchristos if (el->el_terminal.t_loc + 3 < TC_BUFSIZE) {
36498c7cbebSchristos /* XXX strcpy is safe */
3657da8639eSchristos (void) strcpy(*str = &el->el_terminal.t_buf[
3667da8639eSchristos el->el_terminal.t_loc], cap);
367c11bd863Schristos el->el_terminal.t_loc += clen + 1; /* one for \0 */
36898c7cbebSchristos return;
36998c7cbebSchristos }
37098c7cbebSchristos /*
37198c7cbebSchristos * Compact our buffer; no need to check compaction, cause we know it
37298c7cbebSchristos * fits...
37398c7cbebSchristos */
37498c7cbebSchristos tlen = 0;
37598c7cbebSchristos for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
37662666ab4Schristos if (*tmp != NULL && **tmp != '\0' && *tmp != *str) {
37798c7cbebSchristos char *ptr;
37898c7cbebSchristos
37998c7cbebSchristos for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
38098c7cbebSchristos continue;
38198c7cbebSchristos termbuf[tlen++] = '\0';
38298c7cbebSchristos }
38398c7cbebSchristos memcpy(el->el_terminal.t_buf, termbuf, TC_BUFSIZE);
384c11bd863Schristos el->el_terminal.t_loc = tlen;
38598c7cbebSchristos if (el->el_terminal.t_loc + 3 >= TC_BUFSIZE) {
38698c7cbebSchristos (void) fprintf(el->el_errfile,
38798c7cbebSchristos "Out of termcap string space.\n");
38898c7cbebSchristos return;
38998c7cbebSchristos }
39098c7cbebSchristos /* XXX strcpy is safe */
3917da8639eSchristos (void) strcpy(*str = &el->el_terminal.t_buf[el->el_terminal.t_loc],
3927da8639eSchristos cap);
3933d802cf5Schristos el->el_terminal.t_loc += (size_t)clen + 1; /* one for \0 */
39498c7cbebSchristos return;
39598c7cbebSchristos }
39698c7cbebSchristos
39798c7cbebSchristos
39898c7cbebSchristos /* terminal_rebuffer_display():
39998c7cbebSchristos * Rebuffer the display after the screen changed size
40098c7cbebSchristos */
401469d44f8Schristos static int
terminal_rebuffer_display(EditLine * el)40298c7cbebSchristos terminal_rebuffer_display(EditLine *el)
40398c7cbebSchristos {
40498c7cbebSchristos coord_t *c = &el->el_terminal.t_size;
40598c7cbebSchristos
40698c7cbebSchristos terminal_free_display(el);
40798c7cbebSchristos
40898c7cbebSchristos c->h = Val(T_co);
40998c7cbebSchristos c->v = Val(T_li);
41098c7cbebSchristos
41198c7cbebSchristos if (terminal_alloc_display(el) == -1)
412b71bed95Schristos return -1;
413b71bed95Schristos return 0;
41498c7cbebSchristos }
41598c7cbebSchristos
41652b10dfdSchristos static wint_t **
terminal_alloc_buffer(EditLine * el)41706d596a8Schristos terminal_alloc_buffer(EditLine *el)
41806d596a8Schristos {
41906d596a8Schristos wint_t **b;
42006d596a8Schristos coord_t *c = &el->el_terminal.t_size;
42106d596a8Schristos int i;
42206d596a8Schristos
423113f06a3Schristos b = el_calloc((size_t)(c->v + 1), sizeof(*b));
42406d596a8Schristos if (b == NULL)
42506d596a8Schristos return NULL;
42606d596a8Schristos for (i = 0; i < c->v; i++) {
427113f06a3Schristos b[i] = el_calloc((size_t)(c->h + 1), sizeof(**b));
42806d596a8Schristos if (b[i] == NULL) {
42906d596a8Schristos while (--i >= 0)
43006d596a8Schristos el_free(b[i]);
43106d596a8Schristos el_free(b);
43206d596a8Schristos return NULL;
43306d596a8Schristos }
43406d596a8Schristos }
43506d596a8Schristos b[c->v] = NULL;
43606d596a8Schristos return b;
43706d596a8Schristos }
43806d596a8Schristos
43906d596a8Schristos static void
terminal_free_buffer(wint_t *** bp)44006d596a8Schristos terminal_free_buffer(wint_t ***bp)
44106d596a8Schristos {
44206d596a8Schristos wint_t **b;
44306d596a8Schristos wint_t **bufp;
44406d596a8Schristos
44506d596a8Schristos if (*bp == NULL)
44606d596a8Schristos return;
44706d596a8Schristos
44806d596a8Schristos b = *bp;
44906d596a8Schristos *bp = NULL;
45006d596a8Schristos
45106d596a8Schristos for (bufp = b; *bufp != NULL; bufp++)
45206d596a8Schristos el_free(*bufp);
45306d596a8Schristos el_free(b);
45406d596a8Schristos }
45598c7cbebSchristos
45698c7cbebSchristos /* terminal_alloc_display():
45798c7cbebSchristos * Allocate a new display.
45898c7cbebSchristos */
459469d44f8Schristos static int
terminal_alloc_display(EditLine * el)46098c7cbebSchristos terminal_alloc_display(EditLine *el)
46198c7cbebSchristos {
46206d596a8Schristos el->el_display = terminal_alloc_buffer(el);
46306d596a8Schristos if (el->el_display == NULL)
464efeef4e5Schristos goto done;
46506d596a8Schristos el->el_vdisplay = terminal_alloc_buffer(el);
46606d596a8Schristos if (el->el_vdisplay == NULL)
467efeef4e5Schristos goto done;
468b71bed95Schristos return 0;
469efeef4e5Schristos done:
470efeef4e5Schristos terminal_free_display(el);
471efeef4e5Schristos return -1;
47298c7cbebSchristos }
47398c7cbebSchristos
47498c7cbebSchristos
47598c7cbebSchristos /* terminal_free_display():
47698c7cbebSchristos * Free the display buffers
47798c7cbebSchristos */
478469d44f8Schristos static void
terminal_free_display(EditLine * el)47998c7cbebSchristos terminal_free_display(EditLine *el)
48098c7cbebSchristos {
48106d596a8Schristos terminal_free_buffer(&el->el_display);
48206d596a8Schristos terminal_free_buffer(&el->el_vdisplay);
48398c7cbebSchristos }
48498c7cbebSchristos
48598c7cbebSchristos
48698c7cbebSchristos /* terminal_move_to_line():
48798c7cbebSchristos * move to line <where> (first line == 0)
48898c7cbebSchristos * as efficiently as possible
48998c7cbebSchristos */
490a2d6b270Schristos libedit_private void
terminal_move_to_line(EditLine * el,int where)49198c7cbebSchristos terminal_move_to_line(EditLine *el, int where)
49298c7cbebSchristos {
49398c7cbebSchristos int del;
49498c7cbebSchristos
49598c7cbebSchristos if (where == el->el_cursor.v)
49698c7cbebSchristos return;
49798c7cbebSchristos
498b9ecc063Schristos if (where >= el->el_terminal.t_size.v) {
49998c7cbebSchristos #ifdef DEBUG_SCREEN
50098c7cbebSchristos (void) fprintf(el->el_errfile,
50167b10d3eSchristos "%s: where is ridiculous: %d\r\n", __func__, where);
50298c7cbebSchristos #endif /* DEBUG_SCREEN */
50398c7cbebSchristos return;
50498c7cbebSchristos }
50598c7cbebSchristos if ((del = where - el->el_cursor.v) > 0) {
506ac94b724Schristos /*
507ac94b724Schristos * We don't use DO here because some terminals are buggy
508ac94b724Schristos * if the destination is beyond bottom of the screen.
509ac94b724Schristos */
51098c7cbebSchristos for (; del > 0; del--)
51198c7cbebSchristos terminal__putc(el, '\n');
51298c7cbebSchristos /* because the \n will become \r\n */
51398c7cbebSchristos el->el_cursor.h = 0;
51498c7cbebSchristos } else { /* del < 0 */
51598c7cbebSchristos if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
51698c7cbebSchristos terminal_tputs(el, tgoto(Str(T_UP), -del, -del), -del);
51798c7cbebSchristos else {
51898c7cbebSchristos if (GoodStr(T_up))
51998c7cbebSchristos for (; del < 0; del++)
52098c7cbebSchristos terminal_tputs(el, Str(T_up), 1);
52198c7cbebSchristos }
52298c7cbebSchristos }
52398c7cbebSchristos el->el_cursor.v = where;/* now where is here */
52498c7cbebSchristos }
52598c7cbebSchristos
52698c7cbebSchristos
52798c7cbebSchristos /* terminal_move_to_char():
52898c7cbebSchristos * Move to the character position specified
52998c7cbebSchristos */
530a2d6b270Schristos libedit_private void
terminal_move_to_char(EditLine * el,int where)53198c7cbebSchristos terminal_move_to_char(EditLine *el, int where)
53298c7cbebSchristos {
53398c7cbebSchristos int del, i;
53498c7cbebSchristos
53598c7cbebSchristos mc_again:
53698c7cbebSchristos if (where == el->el_cursor.h)
53798c7cbebSchristos return;
53898c7cbebSchristos
53998c7cbebSchristos if (where > el->el_terminal.t_size.h) {
54098c7cbebSchristos #ifdef DEBUG_SCREEN
54198c7cbebSchristos (void) fprintf(el->el_errfile,
54267b10d3eSchristos "%s: where is ridiculous: %d\r\n", __func__, where);
54398c7cbebSchristos #endif /* DEBUG_SCREEN */
54498c7cbebSchristos return;
54598c7cbebSchristos }
54698c7cbebSchristos if (!where) { /* if where is first column */
54798c7cbebSchristos terminal__putc(el, '\r'); /* do a CR */
54898c7cbebSchristos el->el_cursor.h = 0;
54998c7cbebSchristos return;
55098c7cbebSchristos }
55198c7cbebSchristos del = where - el->el_cursor.h;
55298c7cbebSchristos
55398c7cbebSchristos if ((del < -4 || del > 4) && GoodStr(T_ch))
55498c7cbebSchristos /* go there directly */
55598c7cbebSchristos terminal_tputs(el, tgoto(Str(T_ch), where, where), where);
55698c7cbebSchristos else {
55798c7cbebSchristos if (del > 0) { /* moving forward */
55898c7cbebSchristos if ((del > 4) && GoodStr(T_RI))
5597da8639eSchristos terminal_tputs(el, tgoto(Str(T_RI), del, del),
5607da8639eSchristos del);
56198c7cbebSchristos else {
56298c7cbebSchristos /* if I can do tabs, use them */
56398c7cbebSchristos if (EL_CAN_TAB) {
56498c7cbebSchristos if ((el->el_cursor.h & 0370) !=
56598c7cbebSchristos (where & ~0x7)
56698c7cbebSchristos && (el->el_display[
56798c7cbebSchristos el->el_cursor.v][where & 0370] !=
56898c7cbebSchristos MB_FILL_CHAR)
56998c7cbebSchristos ) {
57098c7cbebSchristos /* if not within tab stop */
57198c7cbebSchristos for (i =
57298c7cbebSchristos (el->el_cursor.h & 0370);
57398c7cbebSchristos i < (where & ~0x7);
57498c7cbebSchristos i += 8)
5757da8639eSchristos terminal__putc(el,
5767da8639eSchristos '\t');
57798c7cbebSchristos /* then tab over */
57898c7cbebSchristos el->el_cursor.h = where & ~0x7;
57998c7cbebSchristos }
58098c7cbebSchristos }
58198c7cbebSchristos /*
58298c7cbebSchristos * it's usually cheaper to just write the
58398c7cbebSchristos * chars, so we do.
58498c7cbebSchristos */
58598c7cbebSchristos /*
58698c7cbebSchristos * NOTE THAT terminal_overwrite() WILL CHANGE
58798c7cbebSchristos * el->el_cursor.h!!!
58898c7cbebSchristos */
589e2b95d2cSchristos terminal_overwrite(el,
590e2b95d2cSchristos (wchar_t *)&el->el_display[
59198c7cbebSchristos el->el_cursor.v][el->el_cursor.h],
59298c7cbebSchristos (size_t)(where - el->el_cursor.h));
59398c7cbebSchristos
59498c7cbebSchristos }
59598c7cbebSchristos } else { /* del < 0 := moving backward */
59698c7cbebSchristos if ((-del > 4) && GoodStr(T_LE))
59798c7cbebSchristos terminal_tputs(el, tgoto(Str(T_LE), -del, -del),
59898c7cbebSchristos -del);
59998c7cbebSchristos else { /* can't go directly there */
60098c7cbebSchristos /*
60198c7cbebSchristos * if the "cost" is greater than the "cost"
60298c7cbebSchristos * from col 0
60398c7cbebSchristos */
60498c7cbebSchristos if (EL_CAN_TAB ?
60598c7cbebSchristos ((unsigned int)-del >
60698c7cbebSchristos (((unsigned int) where >> 3) +
60798c7cbebSchristos (where & 07)))
60898c7cbebSchristos : (-del > where)) {
60998c7cbebSchristos terminal__putc(el, '\r');/* do a CR */
61098c7cbebSchristos el->el_cursor.h = 0;
61198c7cbebSchristos goto mc_again; /* and try again */
61298c7cbebSchristos }
61398c7cbebSchristos for (i = 0; i < -del; i++)
61498c7cbebSchristos terminal__putc(el, '\b');
61598c7cbebSchristos }
61698c7cbebSchristos }
61798c7cbebSchristos }
61898c7cbebSchristos el->el_cursor.h = where; /* now where is here */
61998c7cbebSchristos }
62098c7cbebSchristos
62198c7cbebSchristos
62298c7cbebSchristos /* terminal_overwrite():
62398c7cbebSchristos * Overstrike num characters
62498c7cbebSchristos * Assumes MB_FILL_CHARs are present to keep the column count correct
62598c7cbebSchristos */
626a2d6b270Schristos libedit_private void
terminal_overwrite(EditLine * el,const wchar_t * cp,size_t n)6270594af80Schristos terminal_overwrite(EditLine *el, const wchar_t *cp, size_t n)
62898c7cbebSchristos {
62998c7cbebSchristos if (n == 0)
63098c7cbebSchristos return;
63198c7cbebSchristos
63298c7cbebSchristos if (n > (size_t)el->el_terminal.t_size.h) {
63398c7cbebSchristos #ifdef DEBUG_SCREEN
63498c7cbebSchristos (void) fprintf(el->el_errfile,
635d784c575Schristos "%s: n is ridiculous: %zu\r\n", __func__, n);
63698c7cbebSchristos #endif /* DEBUG_SCREEN */
63798c7cbebSchristos return;
63898c7cbebSchristos }
63998c7cbebSchristos
64098c7cbebSchristos do {
64198c7cbebSchristos /* terminal__putc() ignores any MB_FILL_CHARs */
64298c7cbebSchristos terminal__putc(el, *cp++);
64398c7cbebSchristos el->el_cursor.h++;
64498c7cbebSchristos } while (--n);
64598c7cbebSchristos
64698c7cbebSchristos if (el->el_cursor.h >= el->el_terminal.t_size.h) { /* wrap? */
64798c7cbebSchristos if (EL_HAS_AUTO_MARGINS) { /* yes */
64898c7cbebSchristos el->el_cursor.h = 0;
649b9ecc063Schristos if (el->el_cursor.v + 1 < el->el_terminal.t_size.v)
65098c7cbebSchristos el->el_cursor.v++;
65198c7cbebSchristos if (EL_HAS_MAGIC_MARGINS) {
65298c7cbebSchristos /* force the wrap to avoid the "magic"
65398c7cbebSchristos * situation */
6540594af80Schristos wchar_t c;
65598c7cbebSchristos if ((c = el->el_display[el->el_cursor.v]
65698c7cbebSchristos [el->el_cursor.h]) != '\0') {
657c11bd863Schristos terminal_overwrite(el, &c, (size_t)1);
65898c7cbebSchristos while (el->el_display[el->el_cursor.v]
65998c7cbebSchristos [el->el_cursor.h] == MB_FILL_CHAR)
66098c7cbebSchristos el->el_cursor.h++;
66198c7cbebSchristos } else {
66298c7cbebSchristos terminal__putc(el, ' ');
66398c7cbebSchristos el->el_cursor.h = 1;
66498c7cbebSchristos }
66598c7cbebSchristos }
66698c7cbebSchristos } else /* no wrap, but cursor stays on screen */
66798c7cbebSchristos el->el_cursor.h = el->el_terminal.t_size.h - 1;
66898c7cbebSchristos }
66998c7cbebSchristos }
67098c7cbebSchristos
67198c7cbebSchristos
67298c7cbebSchristos /* terminal_deletechars():
67398c7cbebSchristos * Delete num characters
67498c7cbebSchristos */
675a2d6b270Schristos libedit_private void
terminal_deletechars(EditLine * el,int num)67698c7cbebSchristos terminal_deletechars(EditLine *el, int num)
67798c7cbebSchristos {
67898c7cbebSchristos if (num <= 0)
67998c7cbebSchristos return;
68098c7cbebSchristos
68198c7cbebSchristos if (!EL_CAN_DELETE) {
68298c7cbebSchristos #ifdef DEBUG_EDIT
68398c7cbebSchristos (void) fprintf(el->el_errfile, " ERROR: cannot delete \n");
68498c7cbebSchristos #endif /* DEBUG_EDIT */
68598c7cbebSchristos return;
68698c7cbebSchristos }
68798c7cbebSchristos if (num > el->el_terminal.t_size.h) {
68898c7cbebSchristos #ifdef DEBUG_SCREEN
68998c7cbebSchristos (void) fprintf(el->el_errfile,
69067b10d3eSchristos "%s: num is ridiculous: %d\r\n", __func__, num);
69198c7cbebSchristos #endif /* DEBUG_SCREEN */
69298c7cbebSchristos return;
69398c7cbebSchristos }
69498c7cbebSchristos if (GoodStr(T_DC)) /* if I have multiple delete */
69598c7cbebSchristos if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more
69698c7cbebSchristos * expen. */
69798c7cbebSchristos terminal_tputs(el, tgoto(Str(T_DC), num, num), num);
69898c7cbebSchristos return;
69998c7cbebSchristos }
70098c7cbebSchristos if (GoodStr(T_dm)) /* if I have delete mode */
70198c7cbebSchristos terminal_tputs(el, Str(T_dm), 1);
70298c7cbebSchristos
70398c7cbebSchristos if (GoodStr(T_dc)) /* else do one at a time */
70498c7cbebSchristos while (num--)
70598c7cbebSchristos terminal_tputs(el, Str(T_dc), 1);
70698c7cbebSchristos
70798c7cbebSchristos if (GoodStr(T_ed)) /* if I have delete mode */
70898c7cbebSchristos terminal_tputs(el, Str(T_ed), 1);
70998c7cbebSchristos }
71098c7cbebSchristos
71198c7cbebSchristos
71298c7cbebSchristos /* terminal_insertwrite():
71398c7cbebSchristos * Puts terminal in insert character mode or inserts num
71498c7cbebSchristos * characters in the line
71598c7cbebSchristos * Assumes MB_FILL_CHARs are present to keep column count correct
71698c7cbebSchristos */
717a2d6b270Schristos libedit_private void
terminal_insertwrite(EditLine * el,wchar_t * cp,int num)7180594af80Schristos terminal_insertwrite(EditLine *el, wchar_t *cp, int num)
71998c7cbebSchristos {
72098c7cbebSchristos if (num <= 0)
72198c7cbebSchristos return;
72298c7cbebSchristos if (!EL_CAN_INSERT) {
72398c7cbebSchristos #ifdef DEBUG_EDIT
72498c7cbebSchristos (void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
72598c7cbebSchristos #endif /* DEBUG_EDIT */
72698c7cbebSchristos return;
72798c7cbebSchristos }
72898c7cbebSchristos if (num > el->el_terminal.t_size.h) {
72998c7cbebSchristos #ifdef DEBUG_SCREEN
73098c7cbebSchristos (void) fprintf(el->el_errfile,
73167b10d3eSchristos "%s: num is ridiculous: %d\r\n", __func__, num);
73298c7cbebSchristos #endif /* DEBUG_SCREEN */
73398c7cbebSchristos return;
73498c7cbebSchristos }
73598c7cbebSchristos if (GoodStr(T_IC)) /* if I have multiple insert */
73698c7cbebSchristos if ((num > 1) || !GoodStr(T_ic)) {
73798c7cbebSchristos /* if ic would be more expensive */
73898c7cbebSchristos terminal_tputs(el, tgoto(Str(T_IC), num, num), num);
73998c7cbebSchristos terminal_overwrite(el, cp, (size_t)num);
74098c7cbebSchristos /* this updates el_cursor.h */
74198c7cbebSchristos return;
74298c7cbebSchristos }
74398c7cbebSchristos if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
74498c7cbebSchristos terminal_tputs(el, Str(T_im), 1);
74598c7cbebSchristos
74698c7cbebSchristos el->el_cursor.h += num;
74798c7cbebSchristos do
74898c7cbebSchristos terminal__putc(el, *cp++);
74998c7cbebSchristos while (--num);
75098c7cbebSchristos
75198c7cbebSchristos if (GoodStr(T_ip)) /* have to make num chars insert */
75298c7cbebSchristos terminal_tputs(el, Str(T_ip), 1);
75398c7cbebSchristos
75498c7cbebSchristos terminal_tputs(el, Str(T_ei), 1);
75598c7cbebSchristos return;
75698c7cbebSchristos }
75798c7cbebSchristos do {
75898c7cbebSchristos if (GoodStr(T_ic)) /* have to make num chars insert */
75998c7cbebSchristos terminal_tputs(el, Str(T_ic), 1);
76098c7cbebSchristos
76198c7cbebSchristos terminal__putc(el, *cp++);
76298c7cbebSchristos
76398c7cbebSchristos el->el_cursor.h++;
76498c7cbebSchristos
76598c7cbebSchristos if (GoodStr(T_ip)) /* have to make num chars insert */
76698c7cbebSchristos terminal_tputs(el, Str(T_ip), 1);
76798c7cbebSchristos /* pad the inserted char */
76898c7cbebSchristos
76998c7cbebSchristos } while (--num);
77098c7cbebSchristos }
77198c7cbebSchristos
77298c7cbebSchristos
77398c7cbebSchristos /* terminal_clear_EOL():
77498c7cbebSchristos * clear to end of line. There are num characters to clear
77598c7cbebSchristos */
776a2d6b270Schristos libedit_private void
terminal_clear_EOL(EditLine * el,int num)77798c7cbebSchristos terminal_clear_EOL(EditLine *el, int num)
77898c7cbebSchristos {
77998c7cbebSchristos int i;
78098c7cbebSchristos
78198c7cbebSchristos if (EL_CAN_CEOL && GoodStr(T_ce))
78298c7cbebSchristos terminal_tputs(el, Str(T_ce), 1);
78398c7cbebSchristos else {
78498c7cbebSchristos for (i = 0; i < num; i++)
78598c7cbebSchristos terminal__putc(el, ' ');
78698c7cbebSchristos el->el_cursor.h += num; /* have written num spaces */
78798c7cbebSchristos }
78898c7cbebSchristos }
78998c7cbebSchristos
79098c7cbebSchristos
79198c7cbebSchristos /* terminal_clear_screen():
79298c7cbebSchristos * Clear the screen
79398c7cbebSchristos */
794a2d6b270Schristos libedit_private void
terminal_clear_screen(EditLine * el)79598c7cbebSchristos terminal_clear_screen(EditLine *el)
79698c7cbebSchristos { /* clear the whole screen and home */
79798c7cbebSchristos
79898c7cbebSchristos if (GoodStr(T_cl))
79998c7cbebSchristos /* send the clear screen code */
80098c7cbebSchristos terminal_tputs(el, Str(T_cl), Val(T_li));
80198c7cbebSchristos else if (GoodStr(T_ho) && GoodStr(T_cd)) {
80298c7cbebSchristos terminal_tputs(el, Str(T_ho), Val(T_li)); /* home */
80398c7cbebSchristos /* clear to bottom of screen */
80498c7cbebSchristos terminal_tputs(el, Str(T_cd), Val(T_li));
80598c7cbebSchristos } else {
80698c7cbebSchristos terminal__putc(el, '\r');
80798c7cbebSchristos terminal__putc(el, '\n');
80898c7cbebSchristos }
80998c7cbebSchristos }
81098c7cbebSchristos
81198c7cbebSchristos
81298c7cbebSchristos /* terminal_beep():
81398c7cbebSchristos * Beep the way the terminal wants us
81498c7cbebSchristos */
815a2d6b270Schristos libedit_private void
terminal_beep(EditLine * el)81698c7cbebSchristos terminal_beep(EditLine *el)
81798c7cbebSchristos {
81898c7cbebSchristos if (GoodStr(T_bl))
81998c7cbebSchristos /* what termcap says we should use */
82098c7cbebSchristos terminal_tputs(el, Str(T_bl), 1);
82198c7cbebSchristos else
82298c7cbebSchristos terminal__putc(el, '\007'); /* an ASCII bell; ^G */
82398c7cbebSchristos }
82498c7cbebSchristos
82598c7cbebSchristos
826a2d6b270Schristos libedit_private void
terminal_get(EditLine * el,const char ** term)82798c7cbebSchristos terminal_get(EditLine *el, const char **term)
82898c7cbebSchristos {
82998c7cbebSchristos *term = el->el_terminal.t_name;
83098c7cbebSchristos }
83198c7cbebSchristos
83298c7cbebSchristos
83398c7cbebSchristos /* terminal_set():
83498c7cbebSchristos * Read in the terminal capabilities from the requested terminal
83598c7cbebSchristos */
836a2d6b270Schristos libedit_private int
terminal_set(EditLine * el,const char * term)83798c7cbebSchristos terminal_set(EditLine *el, const char *term)
83898c7cbebSchristos {
83998c7cbebSchristos int i;
84098c7cbebSchristos char buf[TC_BUFSIZE];
84198c7cbebSchristos char *area;
84298c7cbebSchristos const struct termcapstr *t;
84398c7cbebSchristos sigset_t oset, nset;
84498c7cbebSchristos int lins, cols;
84598c7cbebSchristos
84698c7cbebSchristos (void) sigemptyset(&nset);
84798c7cbebSchristos (void) sigaddset(&nset, SIGWINCH);
84898c7cbebSchristos (void) sigprocmask(SIG_BLOCK, &nset, &oset);
84998c7cbebSchristos
85098c7cbebSchristos area = buf;
85198c7cbebSchristos
85298c7cbebSchristos
85398c7cbebSchristos if (term == NULL)
85498c7cbebSchristos term = getenv("TERM");
85598c7cbebSchristos
85698c7cbebSchristos if (!term || !term[0])
85798c7cbebSchristos term = "dumb";
85898c7cbebSchristos
85998c7cbebSchristos if (strcmp(term, "emacs") == 0)
86098c7cbebSchristos el->el_flags |= EDIT_DISABLED;
86198c7cbebSchristos
86236e7768bSchristos (void) memset(el->el_terminal.t_cap, 0, TC_BUFSIZE);
86398c7cbebSchristos
86498c7cbebSchristos i = tgetent(el->el_terminal.t_cap, term);
86598c7cbebSchristos
86698c7cbebSchristos if (i <= 0) {
86798c7cbebSchristos if (i == -1)
86898c7cbebSchristos (void) fprintf(el->el_errfile,
86998c7cbebSchristos "Cannot read termcap database;\n");
87098c7cbebSchristos else if (i == 0)
87198c7cbebSchristos (void) fprintf(el->el_errfile,
87298c7cbebSchristos "No entry for terminal type \"%s\";\n", term);
87398c7cbebSchristos (void) fprintf(el->el_errfile,
87498c7cbebSchristos "using dumb terminal settings.\n");
87598c7cbebSchristos Val(T_co) = 80; /* do a dumb terminal */
87698c7cbebSchristos Val(T_pt) = Val(T_km) = Val(T_li) = 0;
87798c7cbebSchristos Val(T_xt) = Val(T_MT);
87898c7cbebSchristos for (t = tstr; t->name != NULL; t++)
87998c7cbebSchristos terminal_alloc(el, t, NULL);
88098c7cbebSchristos } else {
88198c7cbebSchristos /* auto/magic margins */
88298c7cbebSchristos Val(T_am) = tgetflag("am");
88398c7cbebSchristos Val(T_xn) = tgetflag("xn");
88498c7cbebSchristos /* Can we tab */
88598c7cbebSchristos Val(T_pt) = tgetflag("pt");
88698c7cbebSchristos Val(T_xt) = tgetflag("xt");
88798c7cbebSchristos /* do we have a meta? */
88898c7cbebSchristos Val(T_km) = tgetflag("km");
88998c7cbebSchristos Val(T_MT) = tgetflag("MT");
89098c7cbebSchristos /* Get the size */
89198c7cbebSchristos Val(T_co) = tgetnum("co");
89298c7cbebSchristos Val(T_li) = tgetnum("li");
89398c7cbebSchristos for (t = tstr; t->name != NULL; t++) {
89498c7cbebSchristos /* XXX: some systems' tgetstr needs non const */
89598c7cbebSchristos terminal_alloc(el, t, tgetstr(strchr(t->name, *t->name),
89698c7cbebSchristos &area));
89798c7cbebSchristos }
89898c7cbebSchristos }
89998c7cbebSchristos
90098c7cbebSchristos if (Val(T_co) < 2)
90198c7cbebSchristos Val(T_co) = 80; /* just in case */
90298c7cbebSchristos if (Val(T_li) < 1)
90398c7cbebSchristos Val(T_li) = 24;
90498c7cbebSchristos
90598c7cbebSchristos el->el_terminal.t_size.v = Val(T_co);
90698c7cbebSchristos el->el_terminal.t_size.h = Val(T_li);
90798c7cbebSchristos
90898c7cbebSchristos terminal_setflags(el);
90998c7cbebSchristos
91098c7cbebSchristos /* get the correct window size */
91198c7cbebSchristos (void) terminal_get_size(el, &lins, &cols);
91298c7cbebSchristos if (terminal_change_size(el, lins, cols) == -1)
913b71bed95Schristos return -1;
91498c7cbebSchristos (void) sigprocmask(SIG_SETMASK, &oset, NULL);
91598c7cbebSchristos terminal_bind_arrow(el);
91698c7cbebSchristos el->el_terminal.t_name = term;
917b71bed95Schristos return i <= 0 ? -1 : 0;
91898c7cbebSchristos }
91998c7cbebSchristos
92098c7cbebSchristos
92198c7cbebSchristos /* terminal_get_size():
92298c7cbebSchristos * Return the new window size in lines and cols, and
92398c7cbebSchristos * true if the size was changed.
92498c7cbebSchristos */
925a2d6b270Schristos libedit_private int
terminal_get_size(EditLine * el,int * lins,int * cols)92698c7cbebSchristos terminal_get_size(EditLine *el, int *lins, int *cols)
92798c7cbebSchristos {
92898c7cbebSchristos
92998c7cbebSchristos *cols = Val(T_co);
93098c7cbebSchristos *lins = Val(T_li);
93198c7cbebSchristos
93298c7cbebSchristos #ifdef TIOCGWINSZ
93398c7cbebSchristos {
93498c7cbebSchristos struct winsize ws;
935a13cd756Schristos if (ioctl(el->el_infd, TIOCGWINSZ, &ws) != -1) {
93698c7cbebSchristos if (ws.ws_col)
93798c7cbebSchristos *cols = ws.ws_col;
93898c7cbebSchristos if (ws.ws_row)
93998c7cbebSchristos *lins = ws.ws_row;
94098c7cbebSchristos }
94198c7cbebSchristos }
94298c7cbebSchristos #endif
94398c7cbebSchristos #ifdef TIOCGSIZE
94498c7cbebSchristos {
94598c7cbebSchristos struct ttysize ts;
946a13cd756Schristos if (ioctl(el->el_infd, TIOCGSIZE, &ts) != -1) {
94798c7cbebSchristos if (ts.ts_cols)
94898c7cbebSchristos *cols = ts.ts_cols;
94998c7cbebSchristos if (ts.ts_lines)
95098c7cbebSchristos *lins = ts.ts_lines;
95198c7cbebSchristos }
95298c7cbebSchristos }
95398c7cbebSchristos #endif
9548c9fc883Schristos return Val(T_co) != *cols || Val(T_li) != *lins;
95598c7cbebSchristos }
95698c7cbebSchristos
95798c7cbebSchristos
95898c7cbebSchristos /* terminal_change_size():
95998c7cbebSchristos * Change the size of the terminal
96098c7cbebSchristos */
961a2d6b270Schristos libedit_private int
terminal_change_size(EditLine * el,int lins,int cols)96298c7cbebSchristos terminal_change_size(EditLine *el, int lins, int cols)
96398c7cbebSchristos {
9642f37aad7Schristos coord_t cur = el->el_cursor;
96598c7cbebSchristos /*
96698c7cbebSchristos * Just in case
96798c7cbebSchristos */
96898c7cbebSchristos Val(T_co) = (cols < 2) ? 80 : cols;
96998c7cbebSchristos Val(T_li) = (lins < 1) ? 24 : lins;
97098c7cbebSchristos
97198c7cbebSchristos /* re-make display buffers */
97298c7cbebSchristos if (terminal_rebuffer_display(el) == -1)
973b71bed95Schristos return -1;
97498c7cbebSchristos re_clear_display(el);
9752f37aad7Schristos el->el_cursor = cur;
976b71bed95Schristos return 0;
97798c7cbebSchristos }
97898c7cbebSchristos
97998c7cbebSchristos
98098c7cbebSchristos /* terminal_init_arrow():
98198c7cbebSchristos * Initialize the arrow key bindings from termcap
98298c7cbebSchristos */
983469d44f8Schristos static void
terminal_init_arrow(EditLine * el)98498c7cbebSchristos terminal_init_arrow(EditLine *el)
98598c7cbebSchristos {
986d47f9584Schristos funckey_t *arrow = el->el_terminal.t_fkey;
98798c7cbebSchristos
9880aefc7f9Schristos arrow[A_K_DN].name = L"down";
98998c7cbebSchristos arrow[A_K_DN].key = T_kd;
99098c7cbebSchristos arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
99198c7cbebSchristos arrow[A_K_DN].type = XK_CMD;
99298c7cbebSchristos
9930aefc7f9Schristos arrow[A_K_UP].name = L"up";
99498c7cbebSchristos arrow[A_K_UP].key = T_ku;
99598c7cbebSchristos arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
99698c7cbebSchristos arrow[A_K_UP].type = XK_CMD;
99798c7cbebSchristos
9980aefc7f9Schristos arrow[A_K_LT].name = L"left";
99998c7cbebSchristos arrow[A_K_LT].key = T_kl;
100098c7cbebSchristos arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
100198c7cbebSchristos arrow[A_K_LT].type = XK_CMD;
100298c7cbebSchristos
10030aefc7f9Schristos arrow[A_K_RT].name = L"right";
100498c7cbebSchristos arrow[A_K_RT].key = T_kr;
100598c7cbebSchristos arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
100698c7cbebSchristos arrow[A_K_RT].type = XK_CMD;
100798c7cbebSchristos
10080aefc7f9Schristos arrow[A_K_HO].name = L"home";
100998c7cbebSchristos arrow[A_K_HO].key = T_kh;
101098c7cbebSchristos arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
101198c7cbebSchristos arrow[A_K_HO].type = XK_CMD;
101298c7cbebSchristos
10130aefc7f9Schristos arrow[A_K_EN].name = L"end";
101498c7cbebSchristos arrow[A_K_EN].key = T_at7;
101598c7cbebSchristos arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
101698c7cbebSchristos arrow[A_K_EN].type = XK_CMD;
1017fac360ffSchristos
10180aefc7f9Schristos arrow[A_K_DE].name = L"delete";
1019fac360ffSchristos arrow[A_K_DE].key = T_kD;
1020fac360ffSchristos arrow[A_K_DE].fun.cmd = ED_DELETE_NEXT_CHAR;
1021fac360ffSchristos arrow[A_K_DE].type = XK_CMD;
102298c7cbebSchristos }
102398c7cbebSchristos
102498c7cbebSchristos
102598c7cbebSchristos /* terminal_reset_arrow():
102698c7cbebSchristos * Reset arrow key bindings
102798c7cbebSchristos */
1028469d44f8Schristos static void
terminal_reset_arrow(EditLine * el)102998c7cbebSchristos terminal_reset_arrow(EditLine *el)
103098c7cbebSchristos {
1031d47f9584Schristos funckey_t *arrow = el->el_terminal.t_fkey;
10320594af80Schristos static const wchar_t strA[] = L"\033[A";
10330594af80Schristos static const wchar_t strB[] = L"\033[B";
10340594af80Schristos static const wchar_t strC[] = L"\033[C";
10350594af80Schristos static const wchar_t strD[] = L"\033[D";
10360594af80Schristos static const wchar_t strH[] = L"\033[H";
10370594af80Schristos static const wchar_t strF[] = L"\033[F";
10380594af80Schristos static const wchar_t stOA[] = L"\033OA";
10390594af80Schristos static const wchar_t stOB[] = L"\033OB";
10400594af80Schristos static const wchar_t stOC[] = L"\033OC";
10410594af80Schristos static const wchar_t stOD[] = L"\033OD";
10420594af80Schristos static const wchar_t stOH[] = L"\033OH";
10430594af80Schristos static const wchar_t stOF[] = L"\033OF";
104498c7cbebSchristos
1045d47f9584Schristos keymacro_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1046d47f9584Schristos keymacro_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1047d47f9584Schristos keymacro_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1048d47f9584Schristos keymacro_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1049d47f9584Schristos keymacro_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1050d47f9584Schristos keymacro_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1051d47f9584Schristos keymacro_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1052d47f9584Schristos keymacro_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1053d47f9584Schristos keymacro_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1054d47f9584Schristos keymacro_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1055d47f9584Schristos keymacro_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1056d47f9584Schristos keymacro_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
105798c7cbebSchristos
10587da8639eSchristos if (el->el_map.type != MAP_VI)
10597da8639eSchristos return;
1060d47f9584Schristos keymacro_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1061d47f9584Schristos keymacro_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1062d47f9584Schristos keymacro_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1063d47f9584Schristos keymacro_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1064d47f9584Schristos keymacro_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1065d47f9584Schristos keymacro_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1066d47f9584Schristos keymacro_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1067d47f9584Schristos keymacro_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1068d47f9584Schristos keymacro_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1069d47f9584Schristos keymacro_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1070d47f9584Schristos keymacro_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1071d47f9584Schristos keymacro_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
107298c7cbebSchristos }
107398c7cbebSchristos
107498c7cbebSchristos
107598c7cbebSchristos /* terminal_set_arrow():
107698c7cbebSchristos * Set an arrow key binding
107798c7cbebSchristos */
1078a2d6b270Schristos libedit_private int
terminal_set_arrow(EditLine * el,const wchar_t * name,keymacro_value_t * fun,int type)10790594af80Schristos terminal_set_arrow(EditLine *el, const wchar_t *name, keymacro_value_t *fun,
10807da8639eSchristos int type)
108198c7cbebSchristos {
1082d47f9584Schristos funckey_t *arrow = el->el_terminal.t_fkey;
108398c7cbebSchristos int i;
108498c7cbebSchristos
108598c7cbebSchristos for (i = 0; i < A_K_NKEYS; i++)
10860aefc7f9Schristos if (wcscmp(name, arrow[i].name) == 0) {
108798c7cbebSchristos arrow[i].fun = *fun;
108898c7cbebSchristos arrow[i].type = type;
1089b71bed95Schristos return 0;
109098c7cbebSchristos }
1091b71bed95Schristos return -1;
109298c7cbebSchristos }
109398c7cbebSchristos
109498c7cbebSchristos
109598c7cbebSchristos /* terminal_clear_arrow():
109698c7cbebSchristos * Clear an arrow key binding
109798c7cbebSchristos */
1098a2d6b270Schristos libedit_private int
terminal_clear_arrow(EditLine * el,const wchar_t * name)10990594af80Schristos terminal_clear_arrow(EditLine *el, const wchar_t *name)
110098c7cbebSchristos {
1101d47f9584Schristos funckey_t *arrow = el->el_terminal.t_fkey;
110298c7cbebSchristos int i;
110398c7cbebSchristos
110498c7cbebSchristos for (i = 0; i < A_K_NKEYS; i++)
11050aefc7f9Schristos if (wcscmp(name, arrow[i].name) == 0) {
110698c7cbebSchristos arrow[i].type = XK_NOD;
1107b71bed95Schristos return 0;
110898c7cbebSchristos }
1109b71bed95Schristos return -1;
111098c7cbebSchristos }
111198c7cbebSchristos
111298c7cbebSchristos
111398c7cbebSchristos /* terminal_print_arrow():
111498c7cbebSchristos * Print the arrow key bindings
111598c7cbebSchristos */
1116a2d6b270Schristos libedit_private void
terminal_print_arrow(EditLine * el,const wchar_t * name)11170594af80Schristos terminal_print_arrow(EditLine *el, const wchar_t *name)
111898c7cbebSchristos {
111998c7cbebSchristos int i;
1120d47f9584Schristos funckey_t *arrow = el->el_terminal.t_fkey;
112198c7cbebSchristos
112298c7cbebSchristos for (i = 0; i < A_K_NKEYS; i++)
11230aefc7f9Schristos if (*name == '\0' || wcscmp(name, arrow[i].name) == 0)
112498c7cbebSchristos if (arrow[i].type != XK_NOD)
11257da8639eSchristos keymacro_kprint(el, arrow[i].name,
11267da8639eSchristos &arrow[i].fun, arrow[i].type);
112798c7cbebSchristos }
112898c7cbebSchristos
112998c7cbebSchristos
113098c7cbebSchristos /* terminal_bind_arrow():
113198c7cbebSchristos * Bind the arrow keys
113298c7cbebSchristos */
1133a2d6b270Schristos libedit_private void
terminal_bind_arrow(EditLine * el)113498c7cbebSchristos terminal_bind_arrow(EditLine *el)
113598c7cbebSchristos {
113698c7cbebSchristos el_action_t *map;
113798c7cbebSchristos const el_action_t *dmap;
113898c7cbebSchristos int i, j;
113998c7cbebSchristos char *p;
1140d47f9584Schristos funckey_t *arrow = el->el_terminal.t_fkey;
114198c7cbebSchristos
114298c7cbebSchristos /* Check if the components needed are initialized */
114398c7cbebSchristos if (el->el_terminal.t_buf == NULL || el->el_map.key == NULL)
114498c7cbebSchristos return;
114598c7cbebSchristos
114698c7cbebSchristos map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
114798c7cbebSchristos dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
114898c7cbebSchristos
114998c7cbebSchristos terminal_reset_arrow(el);
115098c7cbebSchristos
115198c7cbebSchristos for (i = 0; i < A_K_NKEYS; i++) {
11520594af80Schristos wchar_t wt_str[VISUAL_WIDTH_MAX];
11530594af80Schristos wchar_t *px;
115498c7cbebSchristos size_t n;
115598c7cbebSchristos
115698c7cbebSchristos p = el->el_terminal.t_str[arrow[i].key];
115798c7cbebSchristos if (!p || !*p)
115898c7cbebSchristos continue;
115998c7cbebSchristos for (n = 0; n < VISUAL_WIDTH_MAX && p[n]; ++n)
116098c7cbebSchristos wt_str[n] = p[n];
116198c7cbebSchristos while (n < VISUAL_WIDTH_MAX)
116298c7cbebSchristos wt_str[n++] = '\0';
116398c7cbebSchristos px = wt_str;
116498c7cbebSchristos j = (unsigned char) *p;
116598c7cbebSchristos /*
116698c7cbebSchristos * Assign the arrow keys only if:
116798c7cbebSchristos *
116898c7cbebSchristos * 1. They are multi-character arrow keys and the user
116998c7cbebSchristos * has not re-assigned the leading character, or
117098c7cbebSchristos * has re-assigned the leading character to be
117198c7cbebSchristos * ED_SEQUENCE_LEAD_IN
117298c7cbebSchristos * 2. They are single arrow keys pointing to an
117398c7cbebSchristos * unassigned key.
117498c7cbebSchristos */
117598c7cbebSchristos if (arrow[i].type == XK_NOD)
1176d47f9584Schristos keymacro_clear(el, map, px);
117798c7cbebSchristos else {
117898c7cbebSchristos if (p[1] && (dmap[j] == map[j] ||
117998c7cbebSchristos map[j] == ED_SEQUENCE_LEAD_IN)) {
1180d47f9584Schristos keymacro_add(el, px, &arrow[i].fun,
118198c7cbebSchristos arrow[i].type);
118298c7cbebSchristos map[j] = ED_SEQUENCE_LEAD_IN;
118398c7cbebSchristos } else if (map[j] == ED_UNASSIGNED) {
1184d47f9584Schristos keymacro_clear(el, map, px);
118598c7cbebSchristos if (arrow[i].type == XK_CMD)
118698c7cbebSchristos map[j] = arrow[i].fun.cmd;
118798c7cbebSchristos else
1188d47f9584Schristos keymacro_add(el, px, &arrow[i].fun,
118998c7cbebSchristos arrow[i].type);
119098c7cbebSchristos }
119198c7cbebSchristos }
119298c7cbebSchristos }
119398c7cbebSchristos }
119498c7cbebSchristos
119598c7cbebSchristos /* terminal_putc():
119698c7cbebSchristos * Add a character
119798c7cbebSchristos */
1198469d44f8Schristos static int
terminal_putc(int c)119998c7cbebSchristos terminal_putc(int c)
120098c7cbebSchristos {
120198c7cbebSchristos if (terminal_outfile == NULL)
120298c7cbebSchristos return -1;
120398c7cbebSchristos return fputc(c, terminal_outfile);
120498c7cbebSchristos }
120598c7cbebSchristos
1206469d44f8Schristos static void
terminal_tputs(EditLine * el,const char * cap,int affcnt)120798c7cbebSchristos terminal_tputs(EditLine *el, const char *cap, int affcnt)
120898c7cbebSchristos {
120998c7cbebSchristos #ifdef _REENTRANT
121098c7cbebSchristos pthread_mutex_lock(&terminal_mutex);
121198c7cbebSchristos #endif
121298c7cbebSchristos terminal_outfile = el->el_outfile;
121398c7cbebSchristos (void)tputs(cap, affcnt, terminal_putc);
121498c7cbebSchristos #ifdef _REENTRANT
121598c7cbebSchristos pthread_mutex_unlock(&terminal_mutex);
121698c7cbebSchristos #endif
121798c7cbebSchristos }
121898c7cbebSchristos
121998c7cbebSchristos /* terminal__putc():
122098c7cbebSchristos * Add a character
122198c7cbebSchristos */
1222a2d6b270Schristos libedit_private int
terminal__putc(EditLine * el,wint_t c)1223f54e4f97Schristos terminal__putc(EditLine *el, wint_t c)
122498c7cbebSchristos {
122598c7cbebSchristos char buf[MB_LEN_MAX +1];
122698c7cbebSchristos ssize_t i;
1227a5a5a01fSchristos if (c == MB_FILL_CHAR)
122898c7cbebSchristos return 0;
122906d596a8Schristos if (c & EL_LITERAL)
123006d596a8Schristos return fputs(literal_get(el, c), el->el_outfile);
12310594af80Schristos i = ct_encode_char(buf, (size_t)MB_LEN_MAX, c);
123298c7cbebSchristos if (i <= 0)
123398c7cbebSchristos return (int)i;
123498c7cbebSchristos buf[i] = '\0';
123598c7cbebSchristos return fputs(buf, el->el_outfile);
123698c7cbebSchristos }
123798c7cbebSchristos
123898c7cbebSchristos /* terminal__flush():
123998c7cbebSchristos * Flush output
124098c7cbebSchristos */
1241a2d6b270Schristos libedit_private void
terminal__flush(EditLine * el)124298c7cbebSchristos terminal__flush(EditLine *el)
124398c7cbebSchristos {
124498c7cbebSchristos
124598c7cbebSchristos (void) fflush(el->el_outfile);
124698c7cbebSchristos }
124798c7cbebSchristos
124898c7cbebSchristos /* terminal_writec():
124998c7cbebSchristos * Write the given character out, in a human readable form
125098c7cbebSchristos */
1251a2d6b270Schristos libedit_private void
terminal_writec(EditLine * el,wint_t c)1252f54e4f97Schristos terminal_writec(EditLine *el, wint_t c)
125398c7cbebSchristos {
12540594af80Schristos wchar_t visbuf[VISUAL_WIDTH_MAX +1];
12550594af80Schristos ssize_t vcnt = ct_visual_char(visbuf, VISUAL_WIDTH_MAX, c);
1256d24c721eSchristos if (vcnt < 0)
1257d24c721eSchristos vcnt = 0;
125898c7cbebSchristos visbuf[vcnt] = '\0';
125998c7cbebSchristos terminal_overwrite(el, visbuf, (size_t)vcnt);
126098c7cbebSchristos terminal__flush(el);
126198c7cbebSchristos }
126298c7cbebSchristos
126398c7cbebSchristos
126498c7cbebSchristos /* terminal_telltc():
126598c7cbebSchristos * Print the current termcap characteristics
126698c7cbebSchristos */
1267a2d6b270Schristos libedit_private int
126898c7cbebSchristos /*ARGSUSED*/
terminal_telltc(EditLine * el,int argc,const wchar_t ** argv)126998c7cbebSchristos terminal_telltc(EditLine *el, int argc __attribute__((__unused__)),
12700594af80Schristos const wchar_t **argv __attribute__((__unused__)))
127198c7cbebSchristos {
127298c7cbebSchristos const struct termcapstr *t;
127398c7cbebSchristos char **ts;
127498c7cbebSchristos
127598c7cbebSchristos (void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
127698c7cbebSchristos (void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
127798c7cbebSchristos (void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
127898c7cbebSchristos Val(T_co), Val(T_li));
127998c7cbebSchristos (void) fprintf(el->el_outfile,
128098c7cbebSchristos "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
128198c7cbebSchristos (void) fprintf(el->el_outfile,
128298c7cbebSchristos "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
128398c7cbebSchristos (void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
128498c7cbebSchristos EL_HAS_AUTO_MARGINS ? "has" : "does not have");
128598c7cbebSchristos if (EL_HAS_AUTO_MARGINS)
128698c7cbebSchristos (void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
128798c7cbebSchristos EL_HAS_MAGIC_MARGINS ? "has" : "does not have");
128898c7cbebSchristos
128998c7cbebSchristos for (t = tstr, ts = el->el_terminal.t_str; t->name != NULL; t++, ts++) {
129098c7cbebSchristos const char *ub;
129198c7cbebSchristos if (*ts && **ts) {
129298c7cbebSchristos ub = ct_encode_string(ct_visual_string(
1293300e2ca4Schristos ct_decode_string(*ts, &el->el_scratch),
1294300e2ca4Schristos &el->el_visual), &el->el_scratch);
129598c7cbebSchristos } else {
129698c7cbebSchristos ub = "(empty)";
129798c7cbebSchristos }
129898c7cbebSchristos (void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n",
129998c7cbebSchristos t->long_name, t->name, ub);
130098c7cbebSchristos }
130198c7cbebSchristos (void) fputc('\n', el->el_outfile);
1302b71bed95Schristos return 0;
130398c7cbebSchristos }
130498c7cbebSchristos
130598c7cbebSchristos
130698c7cbebSchristos /* terminal_settc():
130798c7cbebSchristos * Change the current terminal characteristics
130898c7cbebSchristos */
1309a2d6b270Schristos libedit_private int
131098c7cbebSchristos /*ARGSUSED*/
terminal_settc(EditLine * el,int argc,const wchar_t ** argv)131198c7cbebSchristos terminal_settc(EditLine *el, int argc __attribute__((__unused__)),
13120594af80Schristos const wchar_t **argv)
131398c7cbebSchristos {
131498c7cbebSchristos const struct termcapstr *ts;
131598c7cbebSchristos const struct termcapval *tv;
131698c7cbebSchristos char what[8], how[8];
13176953eca8Schristos long i;
13186953eca8Schristos char *ep;
131998c7cbebSchristos
132098c7cbebSchristos if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
132198c7cbebSchristos return -1;
132298c7cbebSchristos
132358b288d8Schristos strlcpy(what, ct_encode_string(argv[1], &el->el_scratch), sizeof(what));
132458b288d8Schristos strlcpy(how, ct_encode_string(argv[2], &el->el_scratch), sizeof(how));
132598c7cbebSchristos
132698c7cbebSchristos /*
132798c7cbebSchristos * Do the strings first
132898c7cbebSchristos */
132998c7cbebSchristos for (ts = tstr; ts->name != NULL; ts++)
133098c7cbebSchristos if (strcmp(ts->name, what) == 0)
133198c7cbebSchristos break;
133298c7cbebSchristos
133398c7cbebSchristos if (ts->name != NULL) {
133498c7cbebSchristos terminal_alloc(el, ts, how);
133598c7cbebSchristos terminal_setflags(el);
133698c7cbebSchristos return 0;
133798c7cbebSchristos }
133898c7cbebSchristos /*
133998c7cbebSchristos * Do the numeric ones second
134098c7cbebSchristos */
134198c7cbebSchristos for (tv = tval; tv->name != NULL; tv++)
134298c7cbebSchristos if (strcmp(tv->name, what) == 0)
134398c7cbebSchristos break;
134498c7cbebSchristos
13456953eca8Schristos if (tv->name == NULL) {
13466953eca8Schristos (void) fprintf(el->el_errfile,
13476953eca8Schristos "%ls: Bad capability `%s'.\n", argv[0], what);
134898c7cbebSchristos return -1;
13496953eca8Schristos }
135098c7cbebSchristos
135198c7cbebSchristos if (tv == &tval[T_pt] || tv == &tval[T_km] ||
135298c7cbebSchristos tv == &tval[T_am] || tv == &tval[T_xn]) {
13536953eca8Schristos /*
13546953eca8Schristos * Booleans
13556953eca8Schristos */
135698c7cbebSchristos if (strcmp(how, "yes") == 0)
135798c7cbebSchristos el->el_terminal.t_val[tv - tval] = 1;
135898c7cbebSchristos else if (strcmp(how, "no") == 0)
135998c7cbebSchristos el->el_terminal.t_val[tv - tval] = 0;
136098c7cbebSchristos else {
136198c7cbebSchristos (void) fprintf(el->el_errfile,
1362fcf85103Schristos "%ls: Bad value `%s'.\n", argv[0], how);
136398c7cbebSchristos return -1;
136498c7cbebSchristos }
136598c7cbebSchristos terminal_setflags(el);
136698c7cbebSchristos return 0;
13676953eca8Schristos }
136898c7cbebSchristos
13696953eca8Schristos /*
13706953eca8Schristos * Numerics
13716953eca8Schristos */
137298c7cbebSchristos i = strtol(how, &ep, 10);
137398c7cbebSchristos if (*ep != '\0') {
137498c7cbebSchristos (void) fprintf(el->el_errfile,
1375fcf85103Schristos "%ls: Bad value `%s'.\n", argv[0], how);
137698c7cbebSchristos return -1;
137798c7cbebSchristos }
137898c7cbebSchristos el->el_terminal.t_val[tv - tval] = (int) i;
13796953eca8Schristos i = 0;
13806953eca8Schristos if (tv == &tval[T_co]) {
138198c7cbebSchristos el->el_terminal.t_size.v = Val(T_co);
13826953eca8Schristos i++;
13836953eca8Schristos } else if (tv == &tval[T_li]) {
138498c7cbebSchristos el->el_terminal.t_size.h = Val(T_li);
13856953eca8Schristos i++;
13866953eca8Schristos }
13876953eca8Schristos if (i && terminal_change_size(el, Val(T_li), Val(T_co)) == -1)
138898c7cbebSchristos return -1;
138998c7cbebSchristos return 0;
139098c7cbebSchristos }
139198c7cbebSchristos
139298c7cbebSchristos
139398c7cbebSchristos /* terminal_gettc():
139498c7cbebSchristos * Get the current terminal characteristics
139598c7cbebSchristos */
1396a2d6b270Schristos libedit_private int
139798c7cbebSchristos /*ARGSUSED*/
terminal_gettc(EditLine * el,int argc,char ** argv)139898c7cbebSchristos terminal_gettc(EditLine *el, int argc __attribute__((__unused__)), char **argv)
139998c7cbebSchristos {
140098c7cbebSchristos const struct termcapstr *ts;
140198c7cbebSchristos const struct termcapval *tv;
140298c7cbebSchristos char *what;
140398c7cbebSchristos void *how;
140498c7cbebSchristos
140598c7cbebSchristos if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1406b71bed95Schristos return -1;
140798c7cbebSchristos
140898c7cbebSchristos what = argv[1];
140998c7cbebSchristos how = argv[2];
141098c7cbebSchristos
141198c7cbebSchristos /*
141298c7cbebSchristos * Do the strings first
141398c7cbebSchristos */
141498c7cbebSchristos for (ts = tstr; ts->name != NULL; ts++)
141598c7cbebSchristos if (strcmp(ts->name, what) == 0)
141698c7cbebSchristos break;
141798c7cbebSchristos
141898c7cbebSchristos if (ts->name != NULL) {
141998c7cbebSchristos *(char **)how = el->el_terminal.t_str[ts - tstr];
142098c7cbebSchristos return 0;
142198c7cbebSchristos }
142298c7cbebSchristos /*
142398c7cbebSchristos * Do the numeric ones second
142498c7cbebSchristos */
142598c7cbebSchristos for (tv = tval; tv->name != NULL; tv++)
142698c7cbebSchristos if (strcmp(tv->name, what) == 0)
142798c7cbebSchristos break;
142898c7cbebSchristos
142998c7cbebSchristos if (tv->name == NULL)
143098c7cbebSchristos return -1;
143198c7cbebSchristos
143298c7cbebSchristos if (tv == &tval[T_pt] || tv == &tval[T_km] ||
143398c7cbebSchristos tv == &tval[T_am] || tv == &tval[T_xn]) {
143498c7cbebSchristos static char yes[] = "yes";
143598c7cbebSchristos static char no[] = "no";
143698c7cbebSchristos if (el->el_terminal.t_val[tv - tval])
143798c7cbebSchristos *(char **)how = yes;
143898c7cbebSchristos else
143998c7cbebSchristos *(char **)how = no;
144098c7cbebSchristos return 0;
144198c7cbebSchristos } else {
144298c7cbebSchristos *(int *)how = el->el_terminal.t_val[tv - tval];
144398c7cbebSchristos return 0;
144498c7cbebSchristos }
144598c7cbebSchristos }
144698c7cbebSchristos
144798c7cbebSchristos /* terminal_echotc():
144898c7cbebSchristos * Print the termcap string out with variable substitution
144998c7cbebSchristos */
1450a2d6b270Schristos libedit_private int
145198c7cbebSchristos /*ARGSUSED*/
terminal_echotc(EditLine * el,int argc,const wchar_t ** argv)145298c7cbebSchristos terminal_echotc(EditLine *el, int argc __attribute__((__unused__)),
14530594af80Schristos const wchar_t **argv)
145498c7cbebSchristos {
145598c7cbebSchristos char *cap, *scap;
14560594af80Schristos wchar_t *ep;
145798c7cbebSchristos int arg_need, arg_cols, arg_rows;
145898c7cbebSchristos int verbose = 0, silent = 0;
145998c7cbebSchristos char *area;
146098c7cbebSchristos static const char fmts[] = "%s\n", fmtd[] = "%d\n";
146198c7cbebSchristos const struct termcapstr *t;
146298c7cbebSchristos char buf[TC_BUFSIZE];
146398c7cbebSchristos long i;
146498c7cbebSchristos
146598c7cbebSchristos area = buf;
146698c7cbebSchristos
146798c7cbebSchristos if (argv == NULL || argv[1] == NULL)
1468b71bed95Schristos return -1;
146998c7cbebSchristos argv++;
147098c7cbebSchristos
147198c7cbebSchristos if (argv[0][0] == '-') {
147298c7cbebSchristos switch (argv[0][1]) {
147398c7cbebSchristos case 'v':
147498c7cbebSchristos verbose = 1;
147598c7cbebSchristos break;
147698c7cbebSchristos case 's':
147798c7cbebSchristos silent = 1;
147898c7cbebSchristos break;
147998c7cbebSchristos default:
148098c7cbebSchristos /* stderror(ERR_NAME | ERR_TCUSAGE); */
148198c7cbebSchristos break;
148298c7cbebSchristos }
148398c7cbebSchristos argv++;
148498c7cbebSchristos }
148598c7cbebSchristos if (!*argv || *argv[0] == '\0')
1486b71bed95Schristos return 0;
14870aefc7f9Schristos if (wcscmp(*argv, L"tabs") == 0) {
148898c7cbebSchristos (void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
1489b71bed95Schristos return 0;
14900aefc7f9Schristos } else if (wcscmp(*argv, L"meta") == 0) {
149198c7cbebSchristos (void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
1492b71bed95Schristos return 0;
14930aefc7f9Schristos } else if (wcscmp(*argv, L"xn") == 0) {
149498c7cbebSchristos (void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ?
149598c7cbebSchristos "yes" : "no");
1496b71bed95Schristos return 0;
14970aefc7f9Schristos } else if (wcscmp(*argv, L"am") == 0) {
149898c7cbebSchristos (void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ?
149998c7cbebSchristos "yes" : "no");
1500b71bed95Schristos return 0;
15010aefc7f9Schristos } else if (wcscmp(*argv, L"baud") == 0) {
150298c7cbebSchristos (void) fprintf(el->el_outfile, fmtd, (int)el->el_tty.t_speed);
1503b71bed95Schristos return 0;
15040aefc7f9Schristos } else if (wcscmp(*argv, L"rows") == 0 ||
15050aefc7f9Schristos wcscmp(*argv, L"lines") == 0) {
150698c7cbebSchristos (void) fprintf(el->el_outfile, fmtd, Val(T_li));
1507b71bed95Schristos return 0;
15080aefc7f9Schristos } else if (wcscmp(*argv, L"cols") == 0) {
150998c7cbebSchristos (void) fprintf(el->el_outfile, fmtd, Val(T_co));
1510b71bed95Schristos return 0;
151198c7cbebSchristos }
151298c7cbebSchristos /*
151398c7cbebSchristos * Try to use our local definition first
151498c7cbebSchristos */
151598c7cbebSchristos scap = NULL;
151698c7cbebSchristos for (t = tstr; t->name != NULL; t++)
151798c7cbebSchristos if (strcmp(t->name,
151898c7cbebSchristos ct_encode_string(*argv, &el->el_scratch)) == 0) {
151998c7cbebSchristos scap = el->el_terminal.t_str[t - tstr];
152098c7cbebSchristos break;
152198c7cbebSchristos }
152298c7cbebSchristos if (t->name == NULL) {
152398c7cbebSchristos /* XXX: some systems' tgetstr needs non const */
152498c7cbebSchristos scap = tgetstr(ct_encode_string(*argv, &el->el_scratch), &area);
152598c7cbebSchristos }
152698c7cbebSchristos if (!scap || scap[0] == '\0') {
152798c7cbebSchristos if (!silent)
152898c7cbebSchristos (void) fprintf(el->el_errfile,
1529fcf85103Schristos "echotc: Termcap parameter `%ls' not found.\n",
153098c7cbebSchristos *argv);
1531b71bed95Schristos return -1;
153298c7cbebSchristos }
153398c7cbebSchristos /*
153498c7cbebSchristos * Count home many values we need for this capability.
153598c7cbebSchristos */
153698c7cbebSchristos for (cap = scap, arg_need = 0; *cap; cap++)
153798c7cbebSchristos if (*cap == '%')
153898c7cbebSchristos switch (*++cap) {
153998c7cbebSchristos case 'd':
154098c7cbebSchristos case '2':
154198c7cbebSchristos case '3':
154298c7cbebSchristos case '.':
154398c7cbebSchristos case '+':
154498c7cbebSchristos arg_need++;
154598c7cbebSchristos break;
154698c7cbebSchristos case '%':
154798c7cbebSchristos case '>':
154898c7cbebSchristos case 'i':
154998c7cbebSchristos case 'r':
155098c7cbebSchristos case 'n':
155198c7cbebSchristos case 'B':
155298c7cbebSchristos case 'D':
155398c7cbebSchristos break;
155498c7cbebSchristos default:
155598c7cbebSchristos /*
155698c7cbebSchristos * hpux has lot's of them...
155798c7cbebSchristos */
155898c7cbebSchristos if (verbose)
155998c7cbebSchristos (void) fprintf(el->el_errfile,
156098c7cbebSchristos "echotc: Warning: unknown termcap %% `%c'.\n",
156198c7cbebSchristos *cap);
156298c7cbebSchristos /* This is bad, but I won't complain */
156398c7cbebSchristos break;
156498c7cbebSchristos }
156598c7cbebSchristos
156698c7cbebSchristos switch (arg_need) {
156798c7cbebSchristos case 0:
156898c7cbebSchristos argv++;
156998c7cbebSchristos if (*argv && *argv[0]) {
157098c7cbebSchristos if (!silent)
157198c7cbebSchristos (void) fprintf(el->el_errfile,
1572fcf85103Schristos "echotc: Warning: Extra argument `%ls'.\n",
157398c7cbebSchristos *argv);
1574b71bed95Schristos return -1;
157598c7cbebSchristos }
157698c7cbebSchristos terminal_tputs(el, scap, 1);
157798c7cbebSchristos break;
157898c7cbebSchristos case 1:
157998c7cbebSchristos argv++;
158098c7cbebSchristos if (!*argv || *argv[0] == '\0') {
158198c7cbebSchristos if (!silent)
158298c7cbebSchristos (void) fprintf(el->el_errfile,
158398c7cbebSchristos "echotc: Warning: Missing argument.\n");
1584b71bed95Schristos return -1;
158598c7cbebSchristos }
158698c7cbebSchristos arg_cols = 0;
1587fcf85103Schristos i = wcstol(*argv, &ep, 10);
158898c7cbebSchristos if (*ep != '\0' || i < 0) {
158998c7cbebSchristos if (!silent)
159098c7cbebSchristos (void) fprintf(el->el_errfile,
1591fcf85103Schristos "echotc: Bad value `%ls' for rows.\n",
159298c7cbebSchristos *argv);
1593b71bed95Schristos return -1;
159498c7cbebSchristos }
159598c7cbebSchristos arg_rows = (int) i;
159698c7cbebSchristos argv++;
159798c7cbebSchristos if (*argv && *argv[0]) {
159898c7cbebSchristos if (!silent)
159998c7cbebSchristos (void) fprintf(el->el_errfile,
1600fcf85103Schristos "echotc: Warning: Extra argument `%ls"
16017da8639eSchristos "'.\n", *argv);
1602b71bed95Schristos return -1;
160398c7cbebSchristos }
160498c7cbebSchristos terminal_tputs(el, tgoto(scap, arg_cols, arg_rows), 1);
160598c7cbebSchristos break;
160698c7cbebSchristos default:
160798c7cbebSchristos /* This is wrong, but I will ignore it... */
160898c7cbebSchristos if (verbose)
160998c7cbebSchristos (void) fprintf(el->el_errfile,
161098c7cbebSchristos "echotc: Warning: Too many required arguments (%d).\n",
161198c7cbebSchristos arg_need);
161298c7cbebSchristos /* FALLTHROUGH */
161398c7cbebSchristos case 2:
161498c7cbebSchristos argv++;
161598c7cbebSchristos if (!*argv || *argv[0] == '\0') {
161698c7cbebSchristos if (!silent)
161798c7cbebSchristos (void) fprintf(el->el_errfile,
161898c7cbebSchristos "echotc: Warning: Missing argument.\n");
1619b71bed95Schristos return -1;
162098c7cbebSchristos }
1621fcf85103Schristos i = wcstol(*argv, &ep, 10);
162298c7cbebSchristos if (*ep != '\0' || i < 0) {
162398c7cbebSchristos if (!silent)
162498c7cbebSchristos (void) fprintf(el->el_errfile,
1625fcf85103Schristos "echotc: Bad value `%ls' for cols.\n",
162698c7cbebSchristos *argv);
1627b71bed95Schristos return -1;
162898c7cbebSchristos }
162998c7cbebSchristos arg_cols = (int) i;
163098c7cbebSchristos argv++;
163198c7cbebSchristos if (!*argv || *argv[0] == '\0') {
163298c7cbebSchristos if (!silent)
163398c7cbebSchristos (void) fprintf(el->el_errfile,
163498c7cbebSchristos "echotc: Warning: Missing argument.\n");
1635b71bed95Schristos return -1;
163698c7cbebSchristos }
1637fcf85103Schristos i = wcstol(*argv, &ep, 10);
163898c7cbebSchristos if (*ep != '\0' || i < 0) {
163998c7cbebSchristos if (!silent)
164098c7cbebSchristos (void) fprintf(el->el_errfile,
1641fcf85103Schristos "echotc: Bad value `%ls' for rows.\n",
164298c7cbebSchristos *argv);
1643b71bed95Schristos return -1;
164498c7cbebSchristos }
164598c7cbebSchristos arg_rows = (int) i;
164698c7cbebSchristos if (*ep != '\0') {
164798c7cbebSchristos if (!silent)
164898c7cbebSchristos (void) fprintf(el->el_errfile,
1649fcf85103Schristos "echotc: Bad value `%ls'.\n", *argv);
1650b71bed95Schristos return -1;
165198c7cbebSchristos }
165298c7cbebSchristos argv++;
165398c7cbebSchristos if (*argv && *argv[0]) {
165498c7cbebSchristos if (!silent)
165598c7cbebSchristos (void) fprintf(el->el_errfile,
1656fcf85103Schristos "echotc: Warning: Extra argument `%ls"
16577da8639eSchristos "'.\n", *argv);
1658b71bed95Schristos return -1;
165998c7cbebSchristos }
166098c7cbebSchristos terminal_tputs(el, tgoto(scap, arg_cols, arg_rows), arg_rows);
166198c7cbebSchristos break;
166298c7cbebSchristos }
1663b71bed95Schristos return 0;
166498c7cbebSchristos }
1665