#include "config.h"
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <bitstring.h>
#include <curses.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include "../common/common.h"
#include "cl.h"
static int cl_pfmap(SCR *, seq_t, CHAR_T *, size_t, CHAR_T *, size_t);
typedef struct _tklist {
char *ts;
char *output;
char *name;
} TKLIST;
static TKLIST const c_tklist[] = {
{"kil1", "O", "insert line"},
{"kdch1", "x", "delete character"},
{"kcud1", "j", "cursor down"},
{"kel", "D", "delete to eol"},
{"kind", "\004", "scroll down"},
{"kll", "$", "go to eol"},
{"khome", "^", "go to sol"},
{"kich1", "i", "insert at cursor"},
{"kdl1", "dd", "delete line"},
{"kcub1", "h", "cursor left"},
{"knp", "\006", "page down"},
{"kpp", "\002", "page up"},
{"kri", "\025", "scroll up"},
{"ked", "dG", "delete to end of screen"},
{"kcuf1", "l", "cursor right"},
{"kcuu1", "k", "cursor up"},
{NULL, NULL, NULL},
};
static TKLIST const m1_tklist[] = {
{"kcud1", "\033ja", "cursor down"},
{"kcub1", "\033ha", "cursor left"},
{"kcuu1", "\033ka", "cursor up"},
{"kcuf1", "\033la", "cursor right"},
{NULL, NULL, NULL},
};
int
cl_term_init(SCR *sp)
{
SEQ *qp;
TKLIST const *tkp;
size_t output_len;
char *t;
for (tkp = c_tklist; tkp->name != NULL; ++tkp) {
if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
continue;
output_len = 0;
if (tkp->output != NULL)
output_len = strlen(tkp->output);
if (seq_set(sp, tkp->name, strlen(tkp->name), t, strlen(t),
tkp->output, output_len, SEQ_COMMAND,
SEQ_NOOVERWRITE | SEQ_SCREEN))
return (1);
}
for (tkp = m1_tklist; tkp->name != NULL; ++tkp) {
if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
continue;
if (!strcmp(t, "\b"))
continue;
output_len = 0;
if (tkp->output != NULL)
output_len = strlen(tkp->output);
if (seq_set(sp, tkp->name, strlen(tkp->name), t, strlen(t),
tkp->output, output_len, SEQ_INPUT,
SEQ_NOOVERWRITE | SEQ_SCREEN))
return (1);
}
LIST_FOREACH(qp, & sp->gp->seqq, q)
if (F_ISSET(qp, SEQ_FUNCMAP))
(void)cl_pfmap(sp, qp->stype,
qp->input, qp->ilen, qp->output, qp->olen);
return (0);
}
int
cl_term_end(GS *gp)
{
SEQ *qp, *nqp;
for (qp = LIST_FIRST(&gp->seqq); qp != NULL; qp = nqp) {
nqp = LIST_NEXT(qp, q);
if (F_ISSET(qp, SEQ_SCREEN))
(void)seq_mdel(qp);
}
return (0);
}
int
cl_fmap(SCR *sp, seq_t stype, CHAR_T *from, size_t flen, CHAR_T *to,
size_t tlen)
{
if (F_ISSET(sp, SC_VI) && !F_ISSET(sp, SC_SCR_VI))
return (0);
if (F_ISSET(sp, SC_EX) && !F_ISSET(sp, SC_SCR_EX))
return (0);
return (cl_pfmap(sp, stype, from, flen, to, tlen));
}
static int
cl_pfmap(SCR *sp, seq_t stype, CHAR_T *from, size_t flen, CHAR_T *to,
size_t tlen)
{
size_t nlen;
char *p, key_name[64];
(void)snprintf(key_name, sizeof(key_name), "kf%d", atoi(from + 1));
if ((p = tigetstr(key_name)) == NULL ||
p == (char *)-1 || strlen(p) == 0)
p = NULL;
if (p == NULL) {
msgq_str(sp, M_ERR, from, "This terminal has no %s key");
return (1);
}
nlen = snprintf(key_name,
sizeof(key_name), "function key %d", atoi(from + 1));
if (nlen >= sizeof(key_name))
nlen = sizeof(key_name) - 1;
return (seq_set(sp, key_name, nlen,
p, strlen(p), to, tlen, stype, SEQ_NOOVERWRITE | SEQ_SCREEN));
}
int
cl_optchange(SCR *sp, int opt, char *str, u_long *valp)
{
CL_PRIVATE *clp;
clp = CLP(sp);
switch (opt) {
case O_TERM:
F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
case O_COLUMNS:
case O_LINES:
F_SET(sp->gp, G_SRESTART);
break;
case O_MESG:
(void)cl_omesg(sp, clp, !*valp);
break;
case O_WINDOWNAME:
if (*valp) {
F_CLR(clp, CL_RENAME_OK);
(void)cl_rename(sp, NULL, 0);
} else {
F_SET(clp, CL_RENAME_OK);
if (sp->frp != NULL && sp->frp->name != NULL)
(void)cl_rename(sp, sp->frp->name, 1);
}
break;
}
return (0);
}
int
cl_omesg(SCR *sp, CL_PRIVATE *clp, int on)
{
struct stat sb;
char *tty;
if ((tty = ttyname(STDERR_FILENO)) == NULL) {
if (sp != NULL)
msgq(sp, M_SYSERR, "stderr");
return (1);
}
if (stat(tty, &sb) < 0) {
if (sp != NULL)
msgq(sp, M_SYSERR, "%s", tty);
return (1);
}
sb.st_mode &= ACCESSPERMS;
if (clp->tgw == TGW_UNKNOWN)
clp->tgw = sb.st_mode & S_IWGRP ? TGW_SET : TGW_UNSET;
if (on) {
if (chmod(tty, sb.st_mode | S_IWGRP) < 0) {
if (sp != NULL)
msgq(sp, M_SYSERR,
"messages not turned on: %s", tty);
return (1);
}
} else
if (chmod(tty, sb.st_mode & ~S_IWGRP) < 0) {
if (sp != NULL)
msgq(sp, M_SYSERR,
"messages not turned off: %s", tty);
return (1);
}
return (0);
}
int
cl_ssize(SCR *sp, int sigwinch, size_t *rowp, size_t *colp, int *changedp)
{
struct winsize win;
size_t col, row;
int rval;
char *p;
if (changedp != NULL)
*changedp = 1;
row = col = 0;
if (ioctl(STDERR_FILENO, TIOCGWINSZ, &win) != -1) {
row = win.ws_row;
col = win.ws_col;
}
if (sigwinch) {
if (row == 0 || col == 0) {
if (changedp != NULL)
*changedp = 0;
return (0);
}
if (sp != NULL &&
row == O_VAL(sp, O_LINES) && col == O_VAL(sp, O_COLUMNS)) {
if (changedp != NULL)
*changedp = 0;
return (0);
}
if (rowp != NULL)
*rowp = row;
if (colp != NULL)
*colp = col;
return (0);
}
if (row == 0 || col == 0) {
if ((p = getenv("TERM")) == NULL)
goto noterm;
if (row == 0) {
if ((rval = tigetnum("lines")) < 0)
msgq(sp, M_SYSERR, "tigetnum: lines");
else
row = rval;
}
if (col == 0) {
if ((rval = tigetnum("cols")) < 0)
msgq(sp, M_SYSERR, "tigetnum: cols");
else
col = rval;
}
}
noterm: if (row == 0)
row = 24;
if (col == 0)
col = 80;
if ((p = getenv("LINES")) != NULL &&
(rval = strtonum(p, 1, INT_MAX, NULL)) > 0)
row = rval;
if ((p = getenv("COLUMNS")) != NULL &&
(rval = strtonum(p, 1, INT_MAX, NULL)) > 0)
col = rval;
if (rowp != NULL)
*rowp = row;
if (colp != NULL)
*colp = col;
return (0);
}
int
cl_putchar(int ch)
{
return (putchar(ch));
}