#ifdef M_RCSID
#ifndef lint
static char rcsID[] =
"$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/"
"libxcurses/src/libc/xcurses/rcs/mvcur.c 1.4 1998/05/29 18:09:09 "
"cbates Exp $";
#endif
#endif
#include <private.h>
#include <string.h>
#define VECTOR_SIZE 128
#define Make_seq_best(s1, s2) \
if (s1->cost > s2->cost) { \
struct Sequence *temp = s1; \
s1 = s2; \
s2 = temp; \
}
#define zero_seq(seq) ((seq)->end = (seq)->vec, (seq)->cost = 0)
struct Sequence {
int vec[VECTOR_SIZE];
int *end;
int cost;
};
static bool relative;
static void
add_seq(struct Sequence *seq1, struct Sequence *seq2)
{
if (seq1->cost >= __MOVE_INFINITY || seq2->cost >= __MOVE_INFINITY)
seq1->cost = __MOVE_INFINITY;
else {
int *vptr = seq2->vec;
while (vptr != seq2->end)
*(seq1->end++) = *(vptr++);
seq1->cost += seq2->cost;
}
}
static void
add_op(struct Sequence *seq, int op, int p1, int p2)
{
*(seq->end++) = op;
*(seq->end++) = p1;
*(seq->end++) = p2;
if (cur_term->_move[op]._seq == NULL) {
seq->cost = __MOVE_INFINITY;
} else if (op < __MOVE_MAX_RELATIVE) {
seq->cost += cur_term->_move[op]._cost * p1;
} else {
seq->cost = cur_term->_move[op]._cost;
}
}
static void
row(struct Sequence *outseq, int orow, int nrow)
{
struct Sequence seqA, seqB;
struct Sequence *best = &seqA;
struct Sequence *try = &seqB;
int parm_cursor, one_step, dist;
if (nrow == orow)
return;
if (nrow < orow) {
parm_cursor = __MOVE_N_UP;
one_step = __MOVE_UP;
dist = orow - nrow;
} else {
parm_cursor = __MOVE_N_DOWN;
one_step = __MOVE_DOWN;
dist = nrow - orow;
}
zero_seq(best);
add_op(best, __MOVE_ROW, nrow, 0);
zero_seq(try);
add_op(try, parm_cursor, dist, 0);
Make_seq_best(best, try);
zero_seq(try);
add_op(try, one_step, dist, 0);
Make_seq_best(best, try);
add_seq(outseq, best);
}
typedef struct {
int _tab;
int _one;
int _opp;
} t_steps;
static void
simp_col(struct Sequence *outseq, int oc, int nc)
{
t_steps *dir;
int dist, tabs, tabstop;
struct Sequence seqA, seqB, *best, *try;
static t_steps right = { __MOVE_TAB, __MOVE_RIGHT, __MOVE_LEFT };
static t_steps left = { __MOVE_BACK_TAB, __MOVE_LEFT, __MOVE_RIGHT };
if (oc == nc)
return;
tabs = tabstop = 0;
best = &seqA;
try = &seqB;
if (oc < nc) {
dir = &right;
if (0 < init_tabs) {
tabstop = nc / init_tabs;
tabs = tabstop - oc / init_tabs;
if (0 < tabs)
oc = tabstop * init_tabs;
tabstop = init_tabs - nc % init_tabs;
}
dist = nc - oc;
} else {
dir = &left;
if (0 < init_tabs) {
tabstop = nc / init_tabs;
tabs = (oc - 1) / init_tabs - tabstop;
if (0 < tabs)
oc = (tabstop + 1) * init_tabs;
tabstop = nc % init_tabs;
}
dist = oc - nc;
}
if (0 < tabs) {
zero_seq(best);
add_op(best, dir->_tab, tabs, 0);
add_seq(outseq, best);
if (oc == nc)
return;
}
zero_seq(best);
add_op(best, dir->_one, dist, 0);
if (0 < tabstop &&
(nc < columns-init_tabs || auto_left_margin ||
eat_newline_glitch)) {
zero_seq(try);
add_op(try, dir->_tab, 1, 0);
if (eat_newline_glitch && columns <= nc + tabstop)
tabstop = columns - nc - 1;
add_op(try, dir->_opp, tabstop, 0);
Make_seq_best(best, try);
}
add_seq(outseq, best);
}
static void
column(struct Sequence *outseq, int ocol, int ncol)
{
struct Sequence seqA, seqB;
struct Sequence *best = &seqA;
struct Sequence *try = &seqB;
int parm_cursor, dist;
if (ncol == ocol)
return;
zero_seq(best);
add_op(best, __MOVE_COLUMN, ncol, 0);
if (ncol < ocol) {
parm_cursor = __MOVE_N_LEFT;
dist = ocol - ncol;
} else {
parm_cursor = __MOVE_N_RIGHT;
dist = ncol - ocol;
}
zero_seq(try);
add_op(try, parm_cursor, dist, 0);
Make_seq_best(best, try);
if (ncol < ocol || !relative) {
zero_seq(try);
add_op(try, __MOVE_RETURN, 1, 0);
simp_col(try, 0, ncol);
Make_seq_best(best, try);
}
zero_seq(try);
simp_col(try, ocol, ncol);
Make_seq_best(best, try);
add_seq(outseq, best);
}
static int
out_seq(struct Sequence *seq, int (*putout)(int))
{
long p1, p2;
int *ptr, op;
if (__MOVE_INFINITY <= seq->cost)
return (ERR);
for (ptr = seq->vec; ptr < seq->end; ) {
op = *ptr++;
p1 = *ptr++;
p2 = *ptr++;
if (op < __MOVE_MAX_RELATIVE) {
while (0 < p1--)
(void) TPUTS(cur_term->_move[op]._seq, 1,
putout);
} else {
(void) TPUTS(tparm(cur_term->_move[op]._seq, p1, p2,
0, 0, 0, 0, 0, 0, 0), 1, putout);
}
}
return (OK);
}
int
__m_mvcur(int oldrow, int oldcol, int newrow, int newcol, int (*putout)(int))
{
struct Sequence seqA, seqB;
struct Sequence col0seq;
struct Sequence *best = &seqA;
struct Sequence *try = &seqB;
newrow %= lines;
newcol %= columns;
zero_seq(best);
add_op(best, __MOVE_ROW_COLUMN, newrow, newcol);
if (newrow == lines-1 && newcol == columns-1) {
return (out_seq(best, putout));
}
if ((relative = (0 <= oldrow && 0 <= oldcol)) != 0) {
oldrow %= lines;
oldcol %= columns;
zero_seq(try);
row(try, oldrow, newrow);
column(try, oldcol, newcol);
Make_seq_best(best, try);
}
if (newcol < oldcol || !relative) {
zero_seq(&col0seq);
column(&col0seq, 0, newcol);
if (col0seq.cost < __MOVE_INFINITY) {
if (newrow < oldrow || !relative) {
zero_seq(try);
add_op(try, __MOVE_HOME, 1, 0);
row(try, 0, newrow);
add_seq(try, &col0seq);
Make_seq_best(best, try);
}
if (newrow > oldrow || !relative) {
zero_seq(try);
add_op(try, __MOVE_LAST_LINE, 1, 0);
row(try, lines - 1, newrow);
add_seq(try, &col0seq);
Make_seq_best(best, try);
}
}
}
return (out_seq(best, putout));
}
static int
nilout(int ch)
{
return (ch);
}
static void
cost(char *cap, int index, int p1, int p2)
{
cur_term->_move[index]._seq = cap;
if (cap == (char *) 0 || cap[0] == '\0') {
cur_term->_move[index]._cost = __MOVE_INFINITY;
} else {
cur_term->_move[index]._cost = __m_tputs(
tparm(cap, (long) p1, (long) p2, 0, 0, 0, 0, 0, 0, 0),
1, nilout);
if (cap == cursor_down && strchr(cap, '\n') != (char *) 0)
cur_term->_move[index]._cost = __MOVE_INFINITY;
}
}
void
__m_mvcur_cost(void)
{
cost(cursor_up, __MOVE_UP, 0, 0);
cost(cursor_down, __MOVE_DOWN, 0, 0);
cost(cursor_left, __MOVE_LEFT, 0, 0);
cost(cursor_right, __MOVE_RIGHT, 0, 0);
cost(dest_tabs_magic_smso ? NULL : tab, __MOVE_TAB, 0, 0);
cost(dest_tabs_magic_smso ? NULL : back_tab,
__MOVE_BACK_TAB, 0, 0);
cost(cursor_home, __MOVE_HOME, 0, 0);
cost(cursor_to_ll, __MOVE_LAST_LINE, 0, 0);
cost(carriage_return, __MOVE_RETURN, 0, 0);
cost(row_address, __MOVE_ROW, lines-1, 0);
cost(parm_up_cursor, __MOVE_N_UP, lines-1, 0);
cost(parm_down_cursor, __MOVE_N_DOWN, lines-1, 0);
cost(column_address, __MOVE_COLUMN, columns-1, 0);
cost(parm_left_cursor, __MOVE_N_LEFT, columns-1, 0);
cost(parm_right_cursor, __MOVE_N_RIGHT, columns-1, 0);
cost(cursor_address, __MOVE_ROW_COLUMN, lines-1, columns-1);
}
#undef mvcur
int
mvcur(int oy, int ox, int ny, int nx)
{
return (__m_mvcur(oy, ox, ny, nx, __m_outc));
}