2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/*
2N/A * mvcur.c
2N/A *
2N/A * XCurses Library
2N/A *
2N/A * Copyright 1990, 1995 by Mortice Kern Systems Inc. All rights reserved.
2N/A *
2N/A */
2N/A
2N/A#ifdef M_RCSID
2N/A#ifndef lint
2N/Astatic char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/mvcur.c 1.4 1995/06/15 18:56:03 ant Exp $";
2N/A#endif
2N/A#endif
2N/A
2N/A#include <private.h>
2N/A#include <string.h>
2N/A#include <stdarg.h>
2N/A
2N/A#define VECTOR_SIZE 128 /* size of strategy buffer */
2N/A
2N/A/*
2N/A * #define
2N/A * Make_seq_best(s1, s2)
2N/A *
2N/A * Make_seq_best() swaps the values of the pointers if s1->cost > s2->cost.
2N/A */
2N/A#define Make_seq_best(s1, s2) \
2N/A if (s1->cost > s2->cost) { \
2N/A struct Sequence* temp = s1; \
2N/A s1 = s2; \
2N/A s2 = temp; \
2N/A }
2N/A
2N/A#define zero_seq(seq) ((seq)->end = (seq)->vec, (seq)->cost = 0)
2N/A
2N/Astruct Sequence {
2N/A int vec[VECTOR_SIZE]; /* vector of operations */
2N/A int *end; /* end of vector */
2N/A int cost; /* cost of vector */
2N/A};
2N/A
2N/Astatic bool relative; /* set if we really know where we are */
2N/A
2N/A/*f
2N/A * Add sequence 2 to sequence 1.
2N/A */
2N/ASTATIC void
2N/Aadd_seq(seq1, seq2)
2N/Astruct Sequence *seq1, *seq2;
2N/A{
2N/A if (seq1->cost >= __MOVE_INFINITY || seq2->cost >= __MOVE_INFINITY)
2N/A seq1->cost = __MOVE_INFINITY;
2N/A else {
2N/A int* vptr = seq2->vec;
2N/A while (vptr != seq2->end)
2N/A *(seq1->end++) = *(vptr++);
2N/A seq1->cost += seq2->cost;
2N/A }
2N/A}
2N/A
2N/A/*f
2N/A * add_op() adds the operator op and the appropriate
2N/A * number of parameters to seq. It also increases the
2N/A * cost appropriately.
2N/A *
2N/A * If op takes no parameters then p0 is taken to be a count.
2N/A */
2N/ASTATIC void
2N/Aadd_op(seq, op, p1, p2)
2N/Astruct Sequence *seq;
2N/Aint op, p1, p2;
2N/A{
2N/A *(seq->end++) = op;
2N/A *(seq->end++) = p1;
2N/A *(seq->end++) = p2;
2N/A
2N/A if (cur_term->_move[op]._seq == (char *) 0) {
2N/A seq->cost = __MOVE_INFINITY;
2N/A } else if (op < __MOVE_MAX_RELATIVE) {
2N/A /* No parameters, total is cost * p1. */
2N/A seq->cost += cur_term->_move[op]._cost * p1;
2N/A } else {
2N/A /* Cursor motion using parameters have fixed cost. */
2N/A seq->cost = cur_term->_move[op]._cost;
2N/A }
2N/A}
2N/A
2N/A/*f
2N/A * row() adds the best sequence for moving the cursor from orow
2N/A * to nrow to seq.
2N/A *
2N/A * row() considers row_address, parm_up/down_cursor and cursor_up/down.
2N/A */
2N/ASTATIC void
2N/Arow(outseq, orow, nrow)
2N/Astruct Sequence *outseq;
2N/Aint orow, nrow;
2N/A{
2N/A struct Sequence seqA, seqB;
2N/A struct Sequence* best = &seqA;
2N/A struct Sequence* try = &seqB;
2N/A int parm_cursor, one_step, dist;
2N/A
2N/A if (nrow == orow)
2N/A return;
2N/A
2N/A if (nrow < orow) {
2N/A parm_cursor = __MOVE_N_UP;
2N/A one_step = __MOVE_UP;
2N/A dist = orow - nrow;
2N/A } else {
2N/A parm_cursor = __MOVE_N_DOWN;
2N/A one_step = __MOVE_DOWN;
2N/A dist = nrow - orow;
2N/A }
2N/A
2N/A /* try out direct row addressing */
2N/A zero_seq(best);
2N/A add_op(best, __MOVE_ROW, nrow, 0);
2N/A
2N/A /* try out parameterized up or down motion */
2N/A zero_seq(try);
2N/A add_op(try, parm_cursor, dist, 0);
2N/A Make_seq_best(best, try);
2N/A
2N/A /* try getting there one step at a time... */
2N/A zero_seq(try);
2N/A add_op(try, one_step, dist, 0);
2N/A Make_seq_best(best, try);
2N/A
2N/A add_seq(outseq, best);
2N/A}
2N/A
2N/A/*
2N/A * Motion indexes used in simp_col().
2N/A */
2N/Atypedef struct {
2N/A int _tab; /* Tab index. */
2N/A int _one; /* Single-step index, same direction as tab. */
2N/A int _opp; /* Single-step index, opposite direction to tab. */
2N/A} t_steps;
2N/A
2N/A/*f
2N/A * simp_col(outseq, oldcol, newcol)
2N/A *
2N/A * simp_col() adds the best simple sequence for getting from oldcol
2N/A * to newcol to outseq. simp_col() considers (back_)tab and
2N/A * cursor_left/right.
2N/A */
2N/ASTATIC void
2N/Asimp_col(outseq, oc, nc)
2N/Astruct Sequence *outseq;
2N/Aint oc, nc;
2N/A{
2N/A t_steps *dir;
2N/A int dist, tabs, tabstop;
2N/A struct Sequence seqA, seqB, *best, *try;
2N/A static t_steps right = { __MOVE_TAB, __MOVE_RIGHT, __MOVE_LEFT };
2N/A static t_steps left = { __MOVE_BACK_TAB, __MOVE_LEFT, __MOVE_RIGHT };
2N/A
2N/A if (oc == nc)
2N/A return;
2N/A
2N/A tabs = tabstop = dist = 0;
2N/A best = &seqA;
2N/A try = &seqB;
2N/A
2N/A if (oc < nc) {
2N/A dir = &right;
2N/A
2N/A if (0 < init_tabs) {
2N/A /* Tabstop preceding nc. */
2N/A tabstop = nc / init_tabs;
2N/A
2N/A tabs = tabstop - oc / init_tabs;
2N/A if (0 < tabs)
2N/A /* Set oc to tabstop before nc : oc <= nc. */
2N/A oc = tabstop * init_tabs;
2N/A
2N/A /* Distance from next tabstop to nc in columns. */
2N/A tabstop = init_tabs - nc % init_tabs;
2N/A }
2N/A
2N/A dist = nc - oc;
2N/A } else {
2N/A dir = &left;
2N/A
2N/A if (0 < init_tabs) {
2N/A /* Tabstop preceding nc. */
2N/A tabstop = nc / init_tabs;
2N/A
2N/A tabs = (oc - 1) / init_tabs - tabstop;
2N/A if (0 < tabs)
2N/A /* Set oc to tabstop after nc : nc <= oc. */
2N/A oc = (tabstop + 1) * init_tabs;
2N/A
2N/A /* Distance from tabstop preceding nc in columns. */
2N/A tabstop = nc % init_tabs;
2N/A }
2N/A
2N/A dist = oc - nc;
2N/A }
2N/A
2N/A if (0 < tabs) {
2N/A /* Tab as close as possible to nc. */
2N/A zero_seq(best);
2N/A add_op(best, dir->_tab, tabs, 0);
2N/A add_seq(outseq, best);
2N/A
2N/A /* If tabs alone get us there, then stop. */
2N/A if (oc == nc)
2N/A return;
2N/A }
2N/A
2N/A /* We're not exactly positioned yet. Compare the worth of
2N/A * two sequences :
2N/A * 1. single-step to location;
2N/A * 2. over tab by one tabstop, then single-step back to location.
2N/A */
2N/A
2N/A /* 1. Single-step to location. */
2N/A zero_seq(best);
2N/A add_op(best, dir->_one, dist, 0);
2N/A
2N/A /* 2. Over tab by one tabstop, then single-step back to location. */
2N/A if (0 < tabstop
2N/A && (nc < columns-init_tabs || auto_left_margin || eat_newline_glitch)) {
2N/A zero_seq(try);
2N/A add_op(try, dir->_tab, 1, 0);
2N/A
2N/A /* vt100 terminals only wrap the cursor when a spacing
2N/A * character is written. Control characters like <tab>
2N/A * will not cause a line wrap. Adjust the number of
2N/A * columns to backup by to reflect the cursor having been
2N/A * placed in the last column. See O'Reilly Termcap &
2N/A * Terminfo book.
2N/A */
2N/A if (eat_newline_glitch && columns <= nc + tabstop)
2N/A tabstop = columns - nc - 1;
2N/A
2N/A add_op(try, dir->_opp, tabstop, 0);
2N/A Make_seq_best(best, try);
2N/A }
2N/A
2N/A add_seq(outseq, best);
2N/A}
2N/A
2N/A/*f
2N/A * column() adds the best sequence for moving the cursor from oldcol
2N/A * to newcol to outseq.
2N/A *
2N/A * column() considers column_address, parm_left/right_cursor,
2N/A * simp_col() and carriage_return + simp_col().
2N/A */
2N/ASTATIC void
2N/Acolumn(outseq, ocol, ncol)
2N/Astruct Sequence* outseq;
2N/Aint ocol, ncol;
2N/A{
2N/A struct Sequence seqA, seqB;
2N/A struct Sequence* best = &seqA;
2N/A struct Sequence* try = &seqB;
2N/A int parm_cursor, dist;
2N/A
2N/A if (ncol == ocol)
2N/A return;
2N/A
2N/A /* try out direct column addressing */
2N/A zero_seq(best);
2N/A add_op(best, __MOVE_COLUMN, ncol, 0);
2N/A
2N/A /* try out parameterized left or right motion */
2N/A if (ncol < ocol){
2N/A parm_cursor = __MOVE_N_LEFT;
2N/A dist = ocol - ncol;
2N/A } else {
2N/A parm_cursor = __MOVE_N_RIGHT;
2N/A dist = ncol - ocol;
2N/A }
2N/A zero_seq(try);
2N/A add_op(try, parm_cursor, dist, 0);
2N/A Make_seq_best(best, try);
2N/A
2N/A if (ncol < ocol || !relative) {
2N/A /* try carriage_return then simp_col() */
2N/A zero_seq(try);
2N/A add_op(try, __MOVE_RETURN, 1, 0);
2N/A simp_col(try, 0, ncol);
2N/A Make_seq_best(best, try);
2N/A }
2N/A
2N/A /* try getting there by simpl_col() */
2N/A zero_seq(try);
2N/A simp_col(try, ocol, ncol);
2N/A Make_seq_best(best, try);
2N/A
2N/A add_seq(outseq, best);
2N/A}
2N/A
2N/A/*f
2N/A * send relevant terminal sequences to the screen
2N/A */
2N/ASTATIC int
2N/Aout_seq(seq, putout)
2N/Astruct Sequence *seq;
2N/Aint (*putout) ANSI((int));
2N/A{
2N/A long p1, p2;
2N/A int *ptr, op;
2N/A
2N/A if (__MOVE_INFINITY <= seq->cost)
2N/A return ERR;
2N/A
2N/A for (ptr = seq->vec; ptr < seq->end; ) {
2N/A op = *ptr++;
2N/A p1 = *ptr++;
2N/A p2 = *ptr++;
2N/A
2N/A if (op < __MOVE_MAX_RELATIVE) {
2N/A while (0 < p1--)
2N/A (void) tputs(
2N/A cur_term->_move[op]._seq, 1, putout
2N/A );
2N/A } else {
2N/A (void) tputs(
2N/A tparm(
2N/A cur_term->_move[op]._seq, p1, p2,
2N/A 0, 0, 0, 0, 0, 0, 0
2N/A ), 1, putout
2N/A );
2N/A }
2N/A }
2N/A
2N/A return OK;
2N/A}
2N/A
2N/A/*f
2N/A * Low-level relative cursor motion. __m_mvcur() looks for the optimal
2N/A * way to move the cursor from point A to point B. If either of the
2N/A * coordinates for point A are -1 then only absolute addressing is used.
2N/A * If the coordinates are out-of-bounds then they are MODed into bounds.
2N/A *
2N/A * Since __m_mvcur() must perform output to various terminals, an API
2N/A * similar to tputs() and vidputs() was adopted.
2N/A */
2N/Aint
2N/A__m_mvcur(oldrow, oldcol, newrow, newcol, putout)
2N/Aint oldrow, oldcol, newrow, newcol, (*putout)(int);
2N/A{
2N/A struct Sequence seqA, seqB; /* allocate work structures */
2N/A struct Sequence col0seq; /* sequence to get from col0 to nc */
2N/A struct Sequence* best = &seqA; /* best sequence so far */
2N/A struct Sequence* try = &seqB; /* next try */
2N/A
2N/A#ifdef M_CURSES_TRACE
2N/A __m_trace(
2N/A "__m_mvcur(%d, %d, %d, %d, %p)",
2N/A oldrow, oldcol, newrow, newcol, putout
2N/A );
2N/A#endif
2N/A
2N/A newrow %= lines;
2N/A newcol %= columns;
2N/A
2N/A zero_seq(best);
2N/A
2N/A /* try out direct cursor addressing */
2N/A add_op(best, __MOVE_ROW_COLUMN, newrow, newcol);
2N/A
2N/A if((relative = 0 <= oldrow && 0 <= oldcol)){
2N/A oldrow %= lines;
2N/A oldcol %= columns;
2N/A
2N/A /* try out independent row/column addressing */
2N/A zero_seq(try);
2N/A row(try, oldrow, newrow);
2N/A column(try, oldcol, newcol);
2N/A Make_seq_best(best, try);
2N/A }
2N/A if (newcol < oldcol || !relative){
2N/A zero_seq(&col0seq);
2N/A column(&col0seq, 0, newcol);
2N/A if (col0seq.cost < __MOVE_INFINITY) {
2N/A /* try out homing and then row/column */
2N/A if (newrow < oldrow || !relative) {
2N/A zero_seq(try);
2N/A add_op(try, __MOVE_HOME, 1, 0);
2N/A row(try, 0, newrow);
2N/A add_seq(try, &col0seq);
2N/A Make_seq_best(best, try);
2N/A }
2N/A
2N/A /* try out homing to last line and then row/column */
2N/A if (newrow > oldrow || !relative) {
2N/A zero_seq(try);
2N/A add_op(try, __MOVE_LAST_LINE, 1, 0);
2N/A row(try, lines - 1, newrow);
2N/A add_seq(try, &col0seq);
2N/A Make_seq_best(best, try);
2N/A }
2N/A }
2N/A }
2N/A
2N/A return __m_return_code("__m_mvcur", out_seq(best, putout));
2N/A}
2N/A
2N/A/*
2N/A * A do nothing output function for tputs().
2N/A */
2N/ASTATIC int
2N/Anilout(ch)
2N/Aint ch;
2N/A{
2N/A return ch;
2N/A}
2N/A
2N/A/*
2N/A * Initialize an entry in cur_term->_move[] with parameters p1 and p2.
2N/A * Note that some capabilities will ignore their parameters.
2N/A */
2N/ASTATIC void
2N/Acost(cap, index, p1, p2)
2N/Achar *cap;
2N/Aint index, p1, p2;
2N/A{
2N/A cur_term->_move[index]._seq = cap;
2N/A
2N/A if (cap == (char *) 0 || cap[0] == '\0') {
2N/A cur_term->_move[index]._cost = __MOVE_INFINITY;
2N/A } else {
2N/A cur_term->_move[index]._cost = tputs(
2N/A tparm(cap, (long) p1, (long) p2, 0, 0, 0, 0, 0, 0, 0),
2N/A 1, nilout
2N/A );
2N/A
2N/A if (cap == cursor_down && strchr(cap, '\n') != (char *) 0)
2N/A cur_term->_move[index]._cost = __MOVE_INFINITY;
2N/A }
2N/A}
2N/A
2N/Avoid
2N/A__m_mvcur_cost()
2N/A{
2N/A /* Relative cursor motion that will be costed on a per
2N/A * character basis in __m_mvcur().
2N/A */
2N/A cost(cursor_up, __MOVE_UP, 0, 0);
2N/A cost(cursor_down, __MOVE_DOWN, 0, 0);
2N/A cost(cursor_left, __MOVE_LEFT, 0, 0);
2N/A cost(cursor_right, __MOVE_RIGHT, 0, 0);
2N/A cost(dest_tabs_magic_smso ? (char *) 0 : tab, __MOVE_TAB, 0, 0);
2N/A cost(
2N/A dest_tabs_magic_smso ? (char *) 0
2N/A : back_tab, __MOVE_BACK_TAB, 0, 0
2N/A );
2N/A
2N/A /* Absolute cursor motion with fixed cost. */
2N/A cost(cursor_home, __MOVE_HOME, 0, 0);
2N/A cost(cursor_to_ll, __MOVE_LAST_LINE, 0, 0);
2N/A cost(carriage_return, __MOVE_RETURN, 0, 0);
2N/A
2N/A /* Parameter cursor motion with worst case cost. */
2N/A cost(row_address, __MOVE_ROW, lines-1, 0);
2N/A cost(parm_up_cursor, __MOVE_N_UP, lines-1, 0);
2N/A cost(parm_down_cursor, __MOVE_N_DOWN, lines-1, 0);
2N/A cost(column_address, __MOVE_COLUMN, columns-1, 0);
2N/A cost(parm_left_cursor, __MOVE_N_LEFT, columns-1, 0);
2N/A cost(parm_right_cursor, __MOVE_N_RIGHT, columns-1, 0);
2N/A cost(cursor_address, __MOVE_ROW_COLUMN, lines-1, columns-1);
2N/A}
2N/A
2N/Aint
2N/A(mvcur)(oy, ox, ny, nx)
2N/Aint oy, ox, ny, nx;
2N/A{
2N/A#ifdef M_CURSES_TRACE
2N/A __m_trace("mvcur(%d, %d, %d, %d)", oy, ox, ny, nx);
2N/A#endif
2N/A
2N/A return __m_return_code("mvcur", __m_mvcur(oy, ox, ny, nx, __m_outc));
2N/A}
2N/A