#include <sys/cdefs.h>
#include <limits.h>
#include <stdlib.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)cr_put.c 8.3 (Berkeley) 5/4/94";
#else
__RCSID("$NetBSD: cr_put.c,v 1.40 2022/10/19 06:09:27 blymn Exp $");
#endif
#endif
#include <string.h>
#include "curses.h"
#include "curses_private.h"
#define HARDTABS 8
int
mvcur(int ly, int lx, int y, int x)
{
return (__mvcur(ly, lx, y, x, 0));
}
static void fgoto(int);
static int plod(int, int);
static int plodput(int);
static int tabcol(int, int);
static int outcol, outline, destcol, destline;
int
__mvcur(int ly, int lx, int y, int x, int in_refresh)
{
__CTRACE(__CTRACE_OUTPUT,
"mvcur: moving cursor from (%d, %d) to (%d, %d) in refresh %d\n",
ly, lx, y, x, in_refresh);
destcol = x;
destline = y;
outcol = lx;
outline = ly;
fgoto(in_refresh);
return (OK);
}
static void
fgoto(int in_refresh)
{
int c, l;
char *cgp;
__CTRACE(__CTRACE_OUTPUT, "fgoto: in_refresh=%d\n", in_refresh);
__CTRACE(__CTRACE_OUTPUT,
"fgoto: outcol=%d, outline=%d, destcol=%d, destline=%d\n",
outcol, outline, destcol, destline);
if (destcol >= COLS) {
destline += destcol / COLS;
destcol %= COLS;
}
if (outcol >= COLS) {
l = (outcol + 1) / COLS;
outline += l;
outcol %= COLS;
if (auto_left_margin == 0) {
while (l > 0) {
if (__pfast) {
if (carriage_return)
tputs(carriage_return,
0, __cputchar);
else
__cputchar('\r');
}
if (cursor_down)
tputs(cursor_down, 0, __cputchar);
else
__cputchar('\n');
l--;
}
outcol = 0;
}
if (outline > LINES - 1) {
destline -= outline - (LINES - 1);
outline = LINES - 1;
}
}
if (destline >= LINES) {
l = destline;
destline = LINES - 1;
if (outline < LINES - 1) {
c = destcol;
if (__pfast == 0 && !cursor_address)
destcol = 0;
fgoto(in_refresh);
destcol = c;
}
while (l >= LINES) {
if (cursor_down && __pfast)
tputs(cursor_down, 0, __cputchar);
else
__cputchar('\n');
l--;
if (__pfast == 0)
outcol = 0;
}
}
if (destline < outline && !(cursor_address || cursor_up))
destline = outline;
if (cursor_address &&
(cgp = tiparm(cursor_address, destline, destcol)))
{
__CTRACE(__CTRACE_OUTPUT, "fgoto: cgp=%s\n", cgp);
if (outcol != COLS - 1 &&
plod((int) strlen(cgp), in_refresh) > 0)
plod(0, in_refresh);
else
tputs(cgp, 0, __cputchar);
} else
plod(0, in_refresh);
outline = destline;
outcol = destcol;
}
static int plodcnt, plodflg;
#ifdef HAVE_WCHAR
static char s[MB_LEN_MAX];
#endif
static int
plodput(int c)
{
if (plodflg) {
int cw;
#ifdef HAVE_WCHAR
cw = wctomb(s, c);
if (cw < 0)
cw = 1;
#else
cw = 1;
#endif
plodcnt -= cw;
} else
__cputchar(c);
return 0;
}
static int
plod(int cnt, int in_refresh)
{
int i, j, k, soutcol, soutline;
__LDATA *csp;
__CTRACE(__CTRACE_OUTPUT, "plod: cnt=%d, in_refresh=%d\n",
cnt, in_refresh);
__CTRACE(__CTRACE_OUTPUT,
"plod: plodding from col %d, row %d to col %d, row %d\n",
outcol, outline, destcol, destline);
plodcnt = plodflg = cnt;
soutcol = outcol;
soutline = outline;
if (cursor_home) {
if (__GT)
i = (destcol / HARDTABS) + (destcol % HARDTABS);
else
i = destcol;
if (destcol >= outcol) {
j = destcol / HARDTABS - outcol / HARDTABS;
if (__GT && j)
j += destcol % HARDTABS;
else
j = destcol - outcol;
} else
if (outcol - destcol <= i)
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 (-1);
if (__GT)
i = destcol % HARDTABS + destcol / HARDTABS;
else
i = destcol;
#ifdef notdef
if (back_tab && outcol > destcol &&
(j = (((outcol + 7) & ~7) - destcol - 1) >> 3)) {
j *= (k = strlen(back_tab));
if ((k += (destcol & 7)) > 4)
j += 8 - (destcol & 7);
else
j += k;
} else
#endif
j = outcol - destcol;
if ((__NONL || !__pfast) && outline < destline)
goto dontcr;
if (!carriage_return && outline >= destline)
goto dontcr;
if (j > i + 1 || outcol > destcol) {
if (carriage_return)
tputs(carriage_return, 0, plodput);
else
plodput('\r');
if (!carriage_return) {
if (cursor_down)
tputs(cursor_down, 0, plodput);
else
plodput('\n');
outline++;
}
outcol = 0;
}
dontcr:while (outline < destline) {
outline++;
if (cursor_down)
tputs(cursor_down, 0, plodput);
else
plodput('\n');
if (plodcnt < 0)
goto out;
if ((__NONL || __pfast == 0) &&
(!cursor_down || (*cursor_down == '\n')))
outcol = 0;
}
#ifdef notdef
if (back_tab)
k = (int) strlen(back_tab);
#endif
while (outcol > destcol) {
if (plodcnt < 0)
goto out;
#ifdef notdef
if (back_tab && outcol - destcol > k + 4) {
tputs(back_tab, 0, plodput);
outcol--;
outcol &= ~7;
continue;
}
#endif
outcol--;
if (cursor_left)
tputs(cursor_left, 0, plodput);
else
plodput('\b');
}
while (outline > destline) {
outline--;
tputs(cursor_up, 0, plodput);
if (plodcnt < 0)
goto out;
}
if (__GT && destcol - outcol > 1) {
for (;;) {
i = tabcol(outcol, HARDTABS);
if (i > destcol)
break;
if (tab)
tputs(tab, 0, plodput);
else
plodput('\t');
outcol = i;
}
if (destcol - outcol > 4 && i < COLS) {
if (tab)
tputs(tab, 0, plodput);
else
plodput('\t');
outcol = i;
while (outcol > destcol) {
outcol--;
if (cursor_left)
tputs(cursor_left, 0, plodput);
else
plodput('\b');
}
}
}
#ifdef HAVE_WCHAR
if (curscr->alines[outline]->line[outcol].cflags & CA_CONTINUATION) {
plodcnt = -1;
goto out;
}
#endif
while (outcol < destcol) {
int chw;
csp = &curscr->alines[outline]->line[outcol];
#ifdef HAVE_WCHAR
chw = csp->wcols;
#else
chw = 1;
#endif
if (in_refresh)
if (plodflg)
plodcnt--;
else {
#ifndef HAVE_WCHAR
i = csp->ch & __CHARTEXT;
if (csp->attr == curscr->wattr)
__cputchar(i);
#else
if ((csp->attr & WA_ATTRIBUTES)
== curscr->wattr) {
if (csp->cflags & CA_CONTINUATION)
goto nondes;
if (csp->wcols >= 1) {
__cputwchar(csp->ch);
__cursesi_putnsp(csp->nsp,
outline,
outcol);
__CTRACE(__CTRACE_OUTPUT,
"plod: (%d,%d)wcols(%d), "
"putwchar(%x)\n",
outline, outcol,
csp->wcols, csp->ch);
}
if (csp->wcols == 0)
break;
}
#endif
else
goto nondes;
}
else {
nondes: if (cursor_right)
tputs(cursor_right, 0, plodput);
else
plodput(' ');
}
outcol += chw;
if (plodcnt < 0)
goto out;
}
out: if (plodflg) {
outcol = soutcol;
outline = soutline;
}
__CTRACE(__CTRACE_OUTPUT, "plod: returns %d\n", plodcnt);
return plodcnt;
}
static int
tabcol(int col, int ts)
{
int offset;
if (col >= COLS) {
offset = COLS * (col / COLS);
col -= offset;
} else
offset = 0;
return (col + ts - (col % ts) + offset);
}