#include "ex.h"
#include "ex_tty.h"
#include "ex_vis.h"
#ifndef PRESUNEUC
#include <wctype.h>
#ifdef putchar
# undef putchar
#endif
#ifdef getchar
# undef getchar
#endif
#endif
int (*Outchar)() = termchar;
int (*Putchar)() = normchar;
int (*Pline)() = normline;
static unsigned char multic[MULTI_BYTE_MAX];
bool putoctal;
int termiosflag = -1;
int (*
setlist(t))()
bool t;
{
int (*P)();
listf = t;
P = Putchar;
Putchar = t ? listchar : normchar;
return (P);
}
int (*
setnumb(t))()
bool t;
{
int (*P)();
numberf = t;
P = Pline;
Pline = t ? (int (*)())numbline : normline;
return (P);
}
int
listchar(wchar_t c)
{
c &= (int)(TRIM|QUOTE);
switch (c) {
case '\t':
case '\b':
outchar('^');
c = ctlof(c);
break;
case '\n':
break;
case (int)('\n' | QUOTE):
outchar('$');
break;
default:
if((int)(c & QUOTE))
break;
if (c < ' ' && c != '\n' || c == DELETE)
outchar('^'), c = ctlof(c);
}
(void) normchar(c);
return (0);
}
int
normchar(wchar_t c)
{
char *colp;
c &= (int)(TRIM|QUOTE);
if (c == '~' && tilde_glitch) {
(void) normchar('\\');
c = '^';
}
if ((int)(c & QUOTE))
switch (c) {
case (int)(' ' | QUOTE):
case (int)('\b' | QUOTE):
break;
case (int)QUOTE:
return (0);
default:
c &= (int)TRIM;
}
else if (c < ' ' && (c != '\b' || !over_strike) && c != '\n' && c != '\t' || c == DELETE)
putchar('^'), c = ctlof(c);
else if (c >= 0200 && (putoctal || !iswprint(c))) {
outchar('\\');
outchar(((c >> 6) & 07) + '0');
outchar(((c >> 3) & 07) + '0');
outchar((c & 07) + '0');
return (0);
} else if (UPPERCASE)
if (isupper(c)) {
outchar('\\');
c = tolower(c);
} else {
colp = "({)}!|^~'`";
while (*colp++)
if (c == *colp++) {
outchar('\\');
c = colp[-2];
break;
}
}
outchar(c);
return (0);
}
int
numbline(int i)
{
if (shudclob)
slobber(' ');
viprintf("%6d ", i);
(void) normline();
return (0);
}
int
normline(void)
{
unsigned char *cp;
int n;
wchar_t wchar;
if (shudclob)
slobber(linebuf[0]);
for (cp = linebuf; *cp;)
if((n = mbtowc(&wchar, (char *)cp, MULTI_BYTE_MAX)) < 0) {
putoctal = 1;
putchar(*cp++);
putoctal = 0;
} else {
cp += n;
putchar(wchar);
}
if (!inopen)
putchar((int)('\n' | QUOTE));
return (0);
}
void
slobber(int c)
{
shudclob = 0;
switch (c) {
case '\t':
if (Putchar == listchar)
return;
break;
default:
return;
case ' ':
case 0:
break;
}
if (over_strike)
return;
flush();
(void) putch(' ');
tputs(cursor_left, 0, putch);
}
static wchar_t linb[66];
wchar_t *linp = linb;
static bool phadnl;
int
putchar(int c)
{
return ((*Putchar)((wchar_t)c));
}
int
termchar(wchar_t c)
{
if (pfast == 0 && phadnl)
pstart();
if (c == '\n')
phadnl = 1;
else if (linp >= &linb[63])
flush1();
*linp++ = c;
if (linp >= &linb[63]) {
fgoto();
flush1();
}
return (0);
}
void
flush(void)
{
flush1();
flush2();
}
void
flush1(void)
{
wchar_t *lp;
wchar_t c;
#ifdef PRESUNEUC
int splitcnt = 0;
#else
int fillercnt = 0;
#endif
*linp = 0;
lp = linb;
while (*lp)
switch (c = *lp++) {
case '\r':
destline += destcol / columns;
destcol = 0;
continue;
case '\b':
if (destcol)
destcol--;
continue;
case ' ':
destcol++;
continue;
case '\t':
destcol += value(vi_TABSTOP) - destcol % value(vi_TABSTOP);
continue;
case '\n':
destline += destcol / columns + 1;
if (destcol != 0 && destcol % columns == 0)
destline--;
destcol = 0;
continue;
default:
fgoto();
for (;;) {
int length, length2;
unsigned char *p;
c &= TRIM;
if ((length = wcwidth(c)) < 0)
length = 0;
if (auto_right_margin == 0 && outcol >= columns)
fgoto();
if((destcol % columns) + length - 1 >= columns) {
#ifdef PRESUNEUC
splitcnt = length - 1;
c = '>';
#else
if(mc_wrap) {
fillercnt = columns -
(destcol % columns);
while(fillercnt) {
(void) putch(mc_filler);
outcol++;
destcol++;
fillercnt--;
}
} else {
fillercnt = length - 1;
c = mc_filler;
}
#endif
continue;
}
length2 = wctomb((char *)multic, c);
p = multic;
while(length2--)
(void) putch(*p++);
if (c == '\b') {
outcol--;
destcol--;
} else if (c >= ' ' && c != DELETE) {
outcol += length;
destcol += length;
if (eat_newline_glitch && outcol % columns == 0)
(void) putch('\r'),
(void) putch('\n');
}
#ifdef PRESUNEUC
if(splitcnt) {
splitcnt--;
c = '>';
} else
c = *lp++;
#else
if(fillercnt) {
fillercnt--;
c = mc_filler;
if(c == ' ')
continue;
} else
c = *lp++;
#endif
if (c <= ' ')
break;
}
--lp;
continue;
}
linp = linb;
}
void
flush2(void)
{
fgoto();
flusho();
pstop();
}
void
fgoto(void)
{
int l, c;
if (destcol > columns - 1) {
destline += destcol / columns;
destcol %= columns;
}
if (outcol > columns - 1) {
l = (outcol + 1) / columns;
outline += l;
outcol %= columns;
if (auto_right_margin == 0) {
while (l > 0) {
if (pfast)
tputs(carriage_return, 0, putch);
tputs(cursor_down, 0, putch);
l--;
}
outcol = 0;
}
if (outline > lines - 1) {
destline -= outline - (lines - 1);
outline = lines - 1;
}
}
if (destline > lines - 1) {
l = destline;
destline = lines - 1;
if (outline < lines - 1) {
c = destcol;
if (pfast == 0 && (!cursor_address || holdcm))
destcol = 0;
fgoto();
destcol = c;
}
while (l > lines - 1) {
if (scroll_forward && pfast)
tputs(scroll_forward, 0, putch);
else
(void) putch('\n');
l--;
if (pfast == 0)
outcol = 0;
}
}
if (destline < outline && !(cursor_address && !holdcm || cursor_up || cursor_home))
destline = outline;
if (cursor_address && !holdcm)
if (plod(costCM) > 0)
plod(0);
else
tputs(tparm(cursor_address, destline, destcol), 0, putch);
else
plod(0);
outline = destline;
outcol = destcol;
}
void
gotab(int col)
{
flush1();
destcol = col;
}
static int plodcnt, plodflg;
int
#ifdef __STDC__
plodput(char c)
#else
plodput(c)
char c;
#endif
{
if (plodflg)
plodcnt--;
else
(void) putch(c);
return (0);
}
int
plod(int cnt)
{
int i, j, k;
int soutcol, soutline;
plodcnt = plodflg = cnt;
soutcol = outcol;
soutline = outline;
if (cursor_home) {
if (tab && value(vi_HARDTABS))
i = (destcol / value(vi_HARDTABS)) + (destcol % value(vi_HARDTABS));
else
i = destcol;
if (destcol >= outcol) {
if (value(vi_HARDTABS)) {
j = destcol / value(vi_HARDTABS) - outcol / value(vi_HARDTABS);
if (tab && j)
j += destcol % value(vi_HARDTABS);
else
j = destcol - outcol;
} else
j = destcol - outcol;
} else
if (outcol - destcol <= i && (cursor_left))
i = j = outcol - destcol;
else
j = i + 1;
k = outline - destline;
if (k < 0)
k = -k;
j += k;
if (i + destline < j || (!cursor_up && destline < outline)) {
tputs(cursor_home, 0, plodput);
outcol = outline = 0;
} else if (cursor_to_ll) {
k = (lines - 1) - destline;
if (i + k + 2 < j && (k<=0 || cursor_up)) {
tputs(cursor_to_ll, 0, plodput);
outcol = 0;
outline = lines - 1;
}
}
} else
if (!cursor_up && destline < outline)
return (500);
if (tab && value(vi_HARDTABS))
i = destcol % value(vi_HARDTABS)
+ destcol / value(vi_HARDTABS);
else
i = destcol;
j = outcol - destcol;
if ((NONL || !pfast) && outline < destline)
goto dontcr;
if (!carriage_return && outline >= destline)
goto dontcr;
if (j > i + 1 || outcol > destcol && !cursor_left) {
if (carriage_return) {
tputs(carriage_return, 0, plodput);
outcol = 0;
} else if (newline) {
tputs(newline, 0, plodput);
outline++;
outcol = 0;
}
}
dontcr:
while (outline < destline) {
j = destline - outline;
if (j > costDP && parm_down_cursor) {
tputs(tparm(parm_down_cursor, j), j, plodput);
outline += j;
}
else {
outline++;
if (cursor_down && pfast)
tputs(cursor_down, 0, plodput);
else
(void) plodput('\n');
}
if (plodcnt < 0)
goto out;
if (NONL || pfast == 0)
outcol = 0;
}
if (back_tab)
k = strlen(back_tab);
while (outcol > destcol) {
if (plodcnt < 0)
goto out;
if (back_tab && !insmode && outcol - destcol > 4+k) {
tputs(back_tab, 0, plodput);
outcol--;
if (value(vi_HARDTABS))
outcol -= outcol % value(vi_HARDTABS);
continue;
}
j = outcol - destcol;
if (j > costLP && parm_left_cursor) {
tputs(tparm(parm_left_cursor, j), j, plodput);
outcol -= j;
}
else {
outcol--;
tputs(cursor_left, 0, plodput);
}
}
while (outline > destline) {
j = outline - destline;
if (parm_up_cursor && j > 1) {
tputs(tparm(parm_up_cursor, j), j, plodput);
outline -= j;
}
else {
outline--;
tputs(cursor_up, 0, plodput);
}
if (plodcnt < 0)
goto out;
}
if (value(vi_HARDTABS) && tab && !insmode && destcol - outcol > 1) {
for (;;) {
i = tabcol(outcol, value(vi_HARDTABS));
if (i > destcol)
break;
if (tab)
tputs(tab, 0, plodput);
else
(void) plodput('\t');
outcol = i;
}
if (destcol - outcol > 4 && i < columns && cursor_left) {
tputs(tab, 0, plodput);
outcol = i;
while (outcol > destcol) {
outcol--;
tputs(cursor_left, 0, plodput);
}
}
}
while (outcol < destcol) {
j = destcol - outcol;
if (j > costRP && parm_right_cursor) {
tputs(tparm(parm_right_cursor, j), j, plodput);
outcol += j;
}
else {
wchar_t wchar;
int length, scrlength;
unsigned char multic[MB_LEN_MAX];
if (!inopen || vtube[outline]==NULL ||
(wchar=vtube[outline][outcol]) < ' ')
wchar = ' ';
if((int)(wchar & QUOTE))
wchar = ' ';
length = wctomb((char *)multic, wchar);
if ((scrlength = wcwidth(wchar)) < 0)
scrlength = 0;
if (insmode && cursor_right || length > 1 || wchar == FILLER) {
int diff = destcol - outcol;
j = (wchar == FILLER ? 1 : scrlength > diff ? diff : scrlength);
while(j--) {
outcol++;
tputs(cursor_right, 0, plodput);
}
} else {
(void) plodput((char)multic[0]);
outcol++;
}
}
if (plodcnt < 0)
goto out;
}
out:
if(plodflg) {
outcol = soutcol;
outline = soutline;
}
return(plodcnt);
}
void
noteinp(void)
{
outline++;
if (outline > lines - 1)
outline = lines - 1;
destline = outline;
destcol = outcol = 0;
}
void
termreset(void)
{
endim();
if (enter_ca_mode)
putpad((unsigned char *)enter_ca_mode);
destcol = 0;
destline = lines - 1;
if (cursor_address) {
outcol = UKCOL;
outline = UKCOL;
} else {
outcol = destcol;
outline = destline;
}
}
unsigned char *obp = obuf;
void
draino(void)
{
obp = obuf;
}
void
flusho(void)
{
if (obp != obuf) {
write(1, obuf, obp - obuf);
#ifdef TRACE
if (trace)
fwrite(obuf, 1, obp-obuf, trace);
#endif
obp = obuf;
}
}
void
putnl(void)
{
putchar('\n');
}
void
putS(unsigned char *cp)
{
if (cp == NULL)
return;
while (*cp)
(void) putch(*cp++);
}
int
putch(char c)
{
#ifdef OLD3BTTY
if(c == '\n')
(void) putch('\r');
#endif
*obp++ = c;
if (obp >= &obuf[sizeof obuf])
flusho();
return (0);
}
void
putpad(unsigned char *cp)
{
flush();
tputs((char *)cp, 0, putch);
}
void
setoutt(void)
{
Outchar = termchar;
}
void
lprintf(unsigned char *cp, unsigned char *dp, ...)
{
int (*P)();
P = setlist(1);
#ifdef PRESUNEUC
viprintf(cp, dp);
#else
viprintf((char *)cp, (char *)dp);
#endif
Putchar = P;
}
void
putNFL()
{
putnl();
flush();
}
void
pstart(void)
{
if (NONL)
return;
if (!value(vi_OPTIMIZE))
return;
if (ruptible == 0 || pfast)
return;
fgoto();
flusho();
pfast = 1;
normtty++;
tty = normf;
tty.c_oflag &= ~(ONLCR|TAB3);
tty.c_lflag &= ~ECHO;
saveterm();
sTTY(2);
}
void
pstop(void)
{
if (inopen)
return;
phadnl = 0;
linp = linb;
draino();
normal(normf);
pfast &= ~1;
}
ttymode
ostart()
{
ttymode f;
(void) gTTY(2);
normtty++;
f = tty;
tty = normf;
tty.c_iflag &= ~ICRNL;
tty.c_lflag &= ~(ECHO|ICANON);
tty.c_oflag &= ~(TAB3|ONLCR);
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 1;
ttcharoff();
sTTY(2);
tostart();
pfast |= 2;
saveterm();
return (f);
}
void
tostart(void)
{
putpad((unsigned char *)cursor_visible);
putpad((unsigned char *)keypad_xmit);
if (!value(vi_MESG)) {
if (ttynbuf[0] == 0) {
char *tn;
if ((tn=ttyname(2)) == NULL &&
(tn=ttyname(1)) == NULL &&
(tn=ttyname(0)) == NULL)
ttynbuf[0] = 1;
else
strcpy(ttynbuf, tn);
}
if (ttynbuf[0] != 1) {
struct stat64 sbuf;
stat64((char *)ttynbuf, &sbuf);
ttymesg = FMODE(sbuf) & 0777;
chmod((char *)ttynbuf, 0600);
}
}
}
void
ttcharoff(void)
{
tty.c_cc[VQUIT] = termiosflag ? _POSIX_VDISABLE : '\200';
if (tty.c_cc[VSTART] != CTRL('q'))
tty.c_cc[VSTART] = _POSIX_VDISABLE;
if (tty.c_cc[VSTOP] != CTRL('s'))
tty.c_cc[VSTOP] = _POSIX_VDISABLE;
tty.c_cc[VSUSP] = _POSIX_VDISABLE;
tty.c_cc[VDSUSP] = _POSIX_VDISABLE;
tty.c_cc[VREPRINT] = _POSIX_VDISABLE;
tty.c_cc[VDISCARD] = _POSIX_VDISABLE;
tty.c_cc[VWERASE] = _POSIX_VDISABLE;
tty.c_cc[VLNEXT] = _POSIX_VDISABLE;
}
void
ostop(ttymode f)
{
pfast = (f.c_oflag & ONLCR) == 0;
termreset(), fgoto(), flusho();
normal(f);
tostop();
}
void
tostop(void)
{
putpad((unsigned char *)clr_eos);
putpad((unsigned char *)cursor_normal);
putpad((unsigned char *)keypad_local);
if (!value(vi_MESG) && ttynbuf[0]>1)
chmod((char *)ttynbuf, ttymesg);
}
#ifndef CBREAK
vcook()
{
tty.sg_flags &= ~RAW;
sTTY(2);
}
vraw()
{
tty.sg_flags |= RAW;
sTTY(2);
}
#endif
void
normal(ttymode f)
{
if (normtty > 0) {
setty(f);
normtty--;
}
}
ttymode
setty(f)
ttymode f;
{
int isnorm = 0;
ttymode ot;
ot = tty;
if (tty.c_lflag & ICANON)
ttcharoff();
else
isnorm = 1;
tty = f;
sTTY(2);
if (!isnorm)
saveterm();
return (ot);
}
static struct termio termio;
int
gTTY(int i)
{
if(termiosflag < 0) {
if(ioctl(i, TCGETS, &tty) == 0)
termiosflag = 1;
else {
termiosflag = 0;
if(ioctl(i, TCGETA, &termio) < 0)
return (-1);
tty.c_iflag = termio.c_iflag;
tty.c_oflag = termio.c_oflag;
tty.c_cflag = termio.c_cflag;
tty.c_lflag = termio.c_lflag;
for(i = 0; i < NCC; i++)
tty.c_cc[i] = termio.c_cc[i];
}
return (0);
}
if(termiosflag)
return (ioctl(i, TCGETS, &tty));
if(ioctl(i, TCGETA, &termio) < 0)
return (-1);
tty.c_iflag = termio.c_iflag;
tty.c_oflag = termio.c_oflag;
tty.c_cflag = termio.c_cflag;
tty.c_lflag = termio.c_lflag;
for(i = 0; i < NCC; i++)
tty.c_cc[i] = termio.c_cc[i];
return (0);
}
void
sTTY(int i)
{
int j;
if(termiosflag)
ioctl(i, TCSETSW, &tty);
else {
termio.c_iflag = tty.c_iflag;
termio.c_oflag = tty.c_oflag;
termio.c_cflag = tty.c_cflag;
termio.c_lflag = tty.c_lflag;
for(j = 0; j < NCC; j++)
termio.c_cc[j] = tty.c_cc[j];
ioctl(i, TCSETAW, &termio);
}
}
void
noonl(void)
{
putchar(Outchar != termchar ? ' ' : '\n');
}