/*-
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Christos Zoulas of Cornell University.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#if defined(__sun)
extern int tgetent(char *, const char *);
extern int tgetflag(char *);
extern int tgetnum(char *);
extern int tputs(const char *, int, int (*)(int));
extern char* tgoto(const char*, int, int);
extern char* tgetstr(char*, char**);
#endif
#ifdef _REENTRANT
#include <pthread.h>
#endif
#include "el.h"
#include "fcns.h"
/*
* IMPORTANT NOTE: these routines are allowed to look at the current screen
* and the current position assuming that it is correct. If this is not
* true, then the update will be WRONG! This is (should be) a valid
* assumption...
*/
#ifdef DEBUG_SCREEN
if (!EL_CAN_UP) {
(void) fprintf(el->el_errfile,
"WARNING: Your terminal cannot move up.\n");
(void) fprintf(el->el_errfile,
"Editing may be odd for long lines.\n");
}
if (!EL_CAN_CEOL)
(void) fprintf(el->el_errfile, "no clear EOL capability.\n");
if (!EL_CAN_DELETE)
(void) fprintf(el->el_errfile, "no delete char capability.\n");
if (!EL_CAN_INSERT)
(void) fprintf(el->el_errfile, "no insert char capability.\n");
#endif /* DEBUG_SCREEN */
}
/* terminal_init():
* Initialize the terminal stuff
*/
libedit_private int
terminal_init(EditLine *el)
{
/*
* New string is shorter; no need to allocate space
*/
if (clen <= tlen) {
if (*str)
(void) strcpy(*str, cap); /* XXX strcpy is safe */
return;
}
/*
* New string is longer; see if we have enough space to append
*/
if (el->el_terminal.t_loc + 3 < TC_BUFSIZE) {
/* XXX strcpy is safe */
(void) strcpy(*str = &el->el_terminal.t_buf[
el->el_terminal.t_loc], cap);
el->el_terminal.t_loc += clen + 1; /* one for \0 */
return;
}
/*
* Compact our buffer; no need to check compaction, cause we know it
* fits...
*/
tlen = 0;
for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
if (*tmp != NULL && **tmp != '\0' && *tmp != *str) {
char *ptr;
for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
continue;
termbuf[tlen++] = '\0';
}
memcpy(el->el_terminal.t_buf, termbuf, TC_BUFSIZE);
el->el_terminal.t_loc = tlen;
if (el->el_terminal.t_loc + 3 >= TC_BUFSIZE) {
(void) fprintf(el->el_errfile,
"Out of termcap string space.\n");
return;
}
/* XXX strcpy is safe */
(void) strcpy(*str = &el->el_terminal.t_buf[el->el_terminal.t_loc],
cap);
el->el_terminal.t_loc += (size_t)clen + 1; /* one for \0 */
return;
}
/* terminal_rebuffer_display():
* Rebuffer the display after the screen changed size
*/
static int
terminal_rebuffer_display(EditLine *el)
{
coord_t *c = &el->el_terminal.t_size;
terminal_free_display(el);
c->h = Val(T_co);
c->v = Val(T_li);
if (terminal_alloc_display(el) == -1)
return -1;
return 0;
}
/* terminal_move_to_line():
* move to line <where> (first line == 0)
* as efficiently as possible
*/
libedit_private void
terminal_move_to_line(EditLine *el, int where)
{
int del;
if (where == el->el_cursor.v)
return;
if (where >= el->el_terminal.t_size.v) {
#ifdef DEBUG_SCREEN
(void) fprintf(el->el_errfile,
"%s: where is ridiculous: %d\r\n", __func__, where);
#endif /* DEBUG_SCREEN */
return;
}
if ((del = where - el->el_cursor.v) > 0) {
/*
* We don't use DO here because some terminals are buggy
* if the destination is beyond bottom of the screen.
*/
for (; del > 0; del--)
terminal__putc(el, '\n');
/* because the \n will become \r\n */
el->el_cursor.h = 0;
} else { /* del < 0 */
if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
terminal_tputs(el, tgoto(Str(T_UP), -del, -del), -del);
else {
if (GoodStr(T_up))
for (; del < 0; del++)
terminal_tputs(el, Str(T_up), 1);
}
}
el->el_cursor.v = where;/* now where is here */
}
/* terminal_move_to_char():
* Move to the character position specified
*/
libedit_private void
terminal_move_to_char(EditLine *el, int where)
{
int del, i;
mc_again:
if (where == el->el_cursor.h)
return;
if (where > el->el_terminal.t_size.h) {
#ifdef DEBUG_SCREEN
(void) fprintf(el->el_errfile,
"%s: where is ridiculous: %d\r\n", __func__, where);
#endif /* DEBUG_SCREEN */
return;
}
if (!where) { /* if where is first column */
terminal__putc(el, '\r'); /* do a CR */
el->el_cursor.h = 0;
return;
}
del = where - el->el_cursor.h;
if ((del < -4 || del > 4) && GoodStr(T_ch))
/* go there directly */
terminal_tputs(el, tgoto(Str(T_ch), where, where), where);
else {
if (del > 0) { /* moving forward */
if ((del > 4) && GoodStr(T_RI))
terminal_tputs(el, tgoto(Str(T_RI), del, del),
del);
else {
/* if I can do tabs, use them */
if (EL_CAN_TAB) {
if ((el->el_cursor.h & 0370) !=
(where & ~0x7)
&& (el->el_display[
el->el_cursor.v][where & 0370] !=
MB_FILL_CHAR)
) {
/* if not within tab stop */
for (i =
(el->el_cursor.h & 0370);
i < (where & ~0x7);
i += 8)
terminal__putc(el,
'\t');
/* then tab over */
el->el_cursor.h = where & ~0x7;
}
}
/*
* it's usually cheaper to just write the
* chars, so we do.
*/
/*
* NOTE THAT terminal_overwrite() WILL CHANGE
* el->el_cursor.h!!!
*/
terminal_overwrite(el,
(wchar_t *)&el->el_display[
el->el_cursor.v][el->el_cursor.h],
(size_t)(where - el->el_cursor.h));
}
} else { /* del < 0 := moving backward */
if ((-del > 4) && GoodStr(T_LE))
terminal_tputs(el, tgoto(Str(T_LE), -del, -del),
-del);
else { /* can't go directly there */
/*
* if the "cost" is greater than the "cost"
* from col 0
*/
if (EL_CAN_TAB ?
((unsigned int)-del >
(((unsigned int) where >> 3) +
(where & 07)))
: (-del > where)) {
terminal__putc(el, '\r');/* do a CR */
el->el_cursor.h = 0;
goto mc_again; /* and try again */
}
for (i = 0; i < -del; i++)
terminal__putc(el, '\b');
}
}
}
el->el_cursor.h = where; /* now where is here */
}
/* terminal_overwrite():
* Overstrike num characters
* Assumes MB_FILL_CHARs are present to keep the column count correct
*/
libedit_private void
terminal_overwrite(EditLine *el, const wchar_t *cp, size_t n)
{
if (n == 0)
return;
if (n > (size_t)el->el_terminal.t_size.h) {
#ifdef DEBUG_SCREEN
(void) fprintf(el->el_errfile,
"%s: n is ridiculous: %zu\r\n", __func__, n);
#endif /* DEBUG_SCREEN */
return;
}
do {
/* terminal__putc() ignores any MB_FILL_CHARs */
terminal__putc(el, *cp++);
el->el_cursor.h++;
} while (--n);
if (el->el_cursor.h >= el->el_terminal.t_size.h) { /* wrap? */
if (EL_HAS_AUTO_MARGINS) { /* yes */
el->el_cursor.h = 0;
if (el->el_cursor.v + 1 < el->el_terminal.t_size.v)
el->el_cursor.v++;
if (EL_HAS_MAGIC_MARGINS) {
/* force the wrap to avoid the "magic"
* situation */
wchar_t c;
if ((c = el->el_display[el->el_cursor.v]
[el->el_cursor.h]) != '\0') {
terminal_overwrite(el, &c, (size_t)1);
while (el->el_display[el->el_cursor.v]
[el->el_cursor.h] == MB_FILL_CHAR)
el->el_cursor.h++;
} else {
terminal__putc(el, ' ');
el->el_cursor.h = 1;
}
}
} else /* no wrap, but cursor stays on screen */
el->el_cursor.h = el->el_terminal.t_size.h - 1;
}
}
/* terminal_deletechars():
* Delete num characters
*/
libedit_private void
terminal_deletechars(EditLine *el, int num)
{
if (num <= 0)
return;
if (!EL_CAN_DELETE) {
#ifdef DEBUG_EDIT
(void) fprintf(el->el_errfile, " ERROR: cannot delete \n");
#endif /* DEBUG_EDIT */
return;
}
if (num > el->el_terminal.t_size.h) {
#ifdef DEBUG_SCREEN
(void) fprintf(el->el_errfile,
"%s: num is ridiculous: %d\r\n", __func__, num);
#endif /* DEBUG_SCREEN */
return;
}
if (GoodStr(T_DC)) /* if I have multiple delete */
if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more
* expen. */
terminal_tputs(el, tgoto(Str(T_DC), num, num), num);
return;
}
if (GoodStr(T_dm)) /* if I have delete mode */
terminal_tputs(el, Str(T_dm), 1);
if (GoodStr(T_dc)) /* else do one at a time */
while (num--)
terminal_tputs(el, Str(T_dc), 1);
if (GoodStr(T_ed)) /* if I have delete mode */
terminal_tputs(el, Str(T_ed), 1);
}
/* terminal_insertwrite():
* Puts terminal in insert character mode or inserts num
* characters in the line
* Assumes MB_FILL_CHARs are present to keep column count correct
*/
libedit_private void
terminal_insertwrite(EditLine *el, wchar_t *cp, int num)
{
if (num <= 0)
return;
if (!EL_CAN_INSERT) {
#ifdef DEBUG_EDIT
(void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
#endif /* DEBUG_EDIT */
return;
}
if (num > el->el_terminal.t_size.h) {
#ifdef DEBUG_SCREEN
(void) fprintf(el->el_errfile,
"%s: num is ridiculous: %d\r\n", __func__, num);
#endif /* DEBUG_SCREEN */
return;
}
if (GoodStr(T_IC)) /* if I have multiple insert */
if ((num > 1) || !GoodStr(T_ic)) {
/* if ic would be more expensive */
terminal_tputs(el, tgoto(Str(T_IC), num, num), num);
terminal_overwrite(el, cp, (size_t)num);
/* this updates el_cursor.h */
return;
}
if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
terminal_tputs(el, Str(T_im), 1);
el->el_cursor.h += num;
do
terminal__putc(el, *cp++);
while (--num);
if (GoodStr(T_ip)) /* have to make num chars insert */
terminal_tputs(el, Str(T_ip), 1);
terminal_tputs(el, Str(T_ei), 1);
return;
}
do {
if (GoodStr(T_ic)) /* have to make num chars insert */
terminal_tputs(el, Str(T_ic), 1);
terminal__putc(el, *cp++);
el->el_cursor.h++;
if (GoodStr(T_ip)) /* have to make num chars insert */
terminal_tputs(el, Str(T_ip), 1);
/* pad the inserted char */
} while (--num);
}
/* terminal_clear_EOL():
* clear to end of line. There are num characters to clear
*/
libedit_private void
terminal_clear_EOL(EditLine *el, int num)
{
int i;
if (EL_CAN_CEOL && GoodStr(T_ce))
terminal_tputs(el, Str(T_ce), 1);
else {
for (i = 0; i < num; i++)
terminal__putc(el, ' ');
el->el_cursor.h += num; /* have written num spaces */
}
}
/* terminal_clear_screen():
* Clear the screen
*/
libedit_private void
terminal_clear_screen(EditLine *el)
{ /* clear the whole screen and home */
if (GoodStr(T_cl))
/* send the clear screen code */
terminal_tputs(el, Str(T_cl), Val(T_li));
else if (GoodStr(T_ho) && GoodStr(T_cd)) {
terminal_tputs(el, Str(T_ho), Val(T_li)); /* home */
/* clear to bottom of screen */
terminal_tputs(el, Str(T_cd), Val(T_li));
} else {
terminal__putc(el, '\r');
terminal__putc(el, '\n');
}
}
/* terminal_beep():
* Beep the way the terminal wants us
*/
libedit_private void
terminal_beep(EditLine *el)
{
if (GoodStr(T_bl))
/* what termcap says we should use */
terminal_tputs(el, Str(T_bl), 1);
else
terminal__putc(el, '\007'); /* an ASCII bell; ^G */
}
/* get the correct window size */
(void) terminal_get_size(el, &lins, &cols);
if (terminal_change_size(el, lins, cols) == -1)
return -1;
(void) sigprocmask(SIG_SETMASK, &oset, NULL);
terminal_bind_arrow(el);
el->el_terminal.t_name = term;
return i <= 0 ? -1 : 0;
}
/* terminal_get_size():
* Return the new window size in lines and cols, and
* true if the size was changed.
*/
libedit_private int
terminal_get_size(EditLine *el, int *lins, int *cols)
{
/* terminal_change_size():
* Change the size of the terminal
*/
libedit_private int
terminal_change_size(EditLine *el, int lins, int cols)
{
coord_t cur = el->el_cursor;
/*
* Just in case
*/
Val(T_co) = (cols < 2) ? 80 : cols;
Val(T_li) = (lins < 1) ? 24 : lins;
for (i = 0; i < A_K_NKEYS; i++) {
wchar_t wt_str[VISUAL_WIDTH_MAX];
wchar_t *px;
size_t n;
p = el->el_terminal.t_str[arrow[i].key];
if (!p || !*p)
continue;
for (n = 0; n < VISUAL_WIDTH_MAX && p[n]; ++n)
wt_str[n] = p[n];
while (n < VISUAL_WIDTH_MAX)
wt_str[n++] = '\0';
px = wt_str;
j = (unsigned char) *p;
/*
* Assign the arrow keys only if:
*
* 1. They are multi-character arrow keys and the user
* has not re-assigned the leading character, or
* has re-assigned the leading character to be
* ED_SEQUENCE_LEAD_IN
* 2. They are single arrow keys pointing to an
* unassigned key.
*/
if (arrow[i].type == XK_NOD)
keymacro_clear(el, map, px);
else {
if (p[1] && (dmap[j] == map[j] ||
map[j] == ED_SEQUENCE_LEAD_IN)) {
keymacro_add(el, px, &arrow[i].fun,
arrow[i].type);
map[j] = ED_SEQUENCE_LEAD_IN;
} else if (map[j] == ED_UNASSIGNED) {
keymacro_clear(el, map, px);
if (arrow[i].type == XK_CMD)
map[j] = arrow[i].fun.cmd;
else
keymacro_add(el, px, &arrow[i].fun,
arrow[i].type);
}
}
}
}
/* terminal_putc():
* Add a character
*/
static int
terminal_putc(int c)
{
if (terminal_outfile == NULL)
return -1;
return fputc(c, terminal_outfile);
}
/* terminal_writec():
* Write the given character out, in a human readable form
*/
libedit_private void
terminal_writec(EditLine *el, wint_t c)
{
wchar_t visbuf[VISUAL_WIDTH_MAX +1];
ssize_t vcnt = ct_visual_char(visbuf, VISUAL_WIDTH_MAX, c);
if (vcnt < 0)
vcnt = 0;
visbuf[vcnt] = '\0';
terminal_overwrite(el, visbuf, (size_t)vcnt);
terminal__flush(el);
}
/* terminal_telltc():
* Print the current termcap characteristics
*/
libedit_private int
/*ARGSUSED*/
terminal_telltc(EditLine *el, int argc __attribute__((__unused__)),
const wchar_t **argv __attribute__((__unused__)))
{
const struct termcapstr *t;
char **ts;
(void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
(void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
(void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
Val(T_co), Val(T_li));
(void) fprintf(el->el_outfile,
"\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
(void) fprintf(el->el_outfile,
"\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
(void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
EL_HAS_AUTO_MARGINS ? "has" : "does not have");
if (EL_HAS_AUTO_MARGINS)
(void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
EL_HAS_MAGIC_MARGINS ? "has" : "does not have");