1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1995-1998 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /* LINTLIBRARY */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * mvcur.c
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * XCurses Library
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  * Copyright 1990, 1995 by Mortice Kern Systems Inc. All rights reserved.
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  */
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #ifdef M_RCSID
39*7c478bd9Sstevel@tonic-gate #ifndef lint
40*7c478bd9Sstevel@tonic-gate static char rcsID[] =
41*7c478bd9Sstevel@tonic-gate "$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/"
42*7c478bd9Sstevel@tonic-gate "libxcurses/src/libc/xcurses/rcs/mvcur.c 1.4 1998/05/29 18:09:09 "
43*7c478bd9Sstevel@tonic-gate "cbates Exp $";
44*7c478bd9Sstevel@tonic-gate #endif
45*7c478bd9Sstevel@tonic-gate #endif
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #include <private.h>
48*7c478bd9Sstevel@tonic-gate #include <string.h>
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate #define	VECTOR_SIZE		128	/* size of strategy buffer */
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate /*
53*7c478bd9Sstevel@tonic-gate  * #define
54*7c478bd9Sstevel@tonic-gate  * Make_seq_best(s1, s2)
55*7c478bd9Sstevel@tonic-gate  *
56*7c478bd9Sstevel@tonic-gate  * Make_seq_best() swaps the values of the pointers if s1->cost > s2->cost.
57*7c478bd9Sstevel@tonic-gate  */
58*7c478bd9Sstevel@tonic-gate #define	Make_seq_best(s1, s2)	\
59*7c478bd9Sstevel@tonic-gate 	if (s1->cost > s2->cost) {	\
60*7c478bd9Sstevel@tonic-gate 	    struct Sequence	*temp = s1; \
61*7c478bd9Sstevel@tonic-gate 	    s1 = s2;			\
62*7c478bd9Sstevel@tonic-gate 	    s2 = temp;			\
63*7c478bd9Sstevel@tonic-gate 	}
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate #define	zero_seq(seq)	((seq)->end = (seq)->vec, (seq)->cost = 0)
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate struct Sequence {
68*7c478bd9Sstevel@tonic-gate 	int	vec[VECTOR_SIZE];	/* vector of operations */
69*7c478bd9Sstevel@tonic-gate 	int	*end;		/* end of vector */
70*7c478bd9Sstevel@tonic-gate 	int	cost;		/* cost of vector */
71*7c478bd9Sstevel@tonic-gate };
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate static bool	relative;		/* set if we really know where we are */
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate /*
76*7c478bd9Sstevel@tonic-gate  * Add sequence 2 to sequence 1.
77*7c478bd9Sstevel@tonic-gate  */
78*7c478bd9Sstevel@tonic-gate static void
add_seq(struct Sequence * seq1,struct Sequence * seq2)79*7c478bd9Sstevel@tonic-gate add_seq(struct Sequence *seq1, struct Sequence *seq2)
80*7c478bd9Sstevel@tonic-gate {
81*7c478bd9Sstevel@tonic-gate 	if (seq1->cost >= __MOVE_INFINITY || seq2->cost >= __MOVE_INFINITY)
82*7c478bd9Sstevel@tonic-gate 		seq1->cost = __MOVE_INFINITY;
83*7c478bd9Sstevel@tonic-gate 	else {
84*7c478bd9Sstevel@tonic-gate 		int	*vptr = seq2->vec;
85*7c478bd9Sstevel@tonic-gate 		while (vptr != seq2->end)
86*7c478bd9Sstevel@tonic-gate 			*(seq1->end++) = *(vptr++);
87*7c478bd9Sstevel@tonic-gate 		seq1->cost += seq2->cost;
88*7c478bd9Sstevel@tonic-gate 	}
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate /*
92*7c478bd9Sstevel@tonic-gate  * add_op() adds the operator op and the appropriate
93*7c478bd9Sstevel@tonic-gate  * number of paramaters to seq.  It also increases the
94*7c478bd9Sstevel@tonic-gate  * cost appropriately.
95*7c478bd9Sstevel@tonic-gate  *
96*7c478bd9Sstevel@tonic-gate  * If op takes no parameters then p0 is taken to be a count.
97*7c478bd9Sstevel@tonic-gate  */
98*7c478bd9Sstevel@tonic-gate static void
add_op(struct Sequence * seq,int op,int p1,int p2)99*7c478bd9Sstevel@tonic-gate add_op(struct Sequence *seq, int op, int p1, int p2)
100*7c478bd9Sstevel@tonic-gate {
101*7c478bd9Sstevel@tonic-gate 	*(seq->end++) = op;
102*7c478bd9Sstevel@tonic-gate 	*(seq->end++) = p1;
103*7c478bd9Sstevel@tonic-gate 	*(seq->end++) = p2;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	if (cur_term->_move[op]._seq == NULL) {
106*7c478bd9Sstevel@tonic-gate 		seq->cost = __MOVE_INFINITY;
107*7c478bd9Sstevel@tonic-gate 	} else if (op < __MOVE_MAX_RELATIVE) {
108*7c478bd9Sstevel@tonic-gate 		/* No parameters, total is cost * p1. */
109*7c478bd9Sstevel@tonic-gate 		seq->cost += cur_term->_move[op]._cost * p1;
110*7c478bd9Sstevel@tonic-gate 	} else {
111*7c478bd9Sstevel@tonic-gate 		/* Cursor motion using parameters have fixed cost. */
112*7c478bd9Sstevel@tonic-gate 		seq->cost = cur_term->_move[op]._cost;
113*7c478bd9Sstevel@tonic-gate 	}
114*7c478bd9Sstevel@tonic-gate }
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate /*
117*7c478bd9Sstevel@tonic-gate  * row() adds the best sequence for moving the cursor from orow
118*7c478bd9Sstevel@tonic-gate  * to nrow to seq.
119*7c478bd9Sstevel@tonic-gate  *
120*7c478bd9Sstevel@tonic-gate  * row() considers row_address, parm_up/down_cursor and cursor_up/down.
121*7c478bd9Sstevel@tonic-gate  */
122*7c478bd9Sstevel@tonic-gate static void
row(struct Sequence * outseq,int orow,int nrow)123*7c478bd9Sstevel@tonic-gate row(struct Sequence *outseq, int orow, int nrow)
124*7c478bd9Sstevel@tonic-gate {
125*7c478bd9Sstevel@tonic-gate 	struct Sequence	seqA, seqB;
126*7c478bd9Sstevel@tonic-gate 	struct Sequence	*best = &seqA;
127*7c478bd9Sstevel@tonic-gate 	struct Sequence	*try = &seqB;
128*7c478bd9Sstevel@tonic-gate 	int	parm_cursor, one_step, dist;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if (nrow == orow)
131*7c478bd9Sstevel@tonic-gate 		return;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	if (nrow < orow) {
134*7c478bd9Sstevel@tonic-gate 		parm_cursor = __MOVE_N_UP;
135*7c478bd9Sstevel@tonic-gate 		one_step = __MOVE_UP;
136*7c478bd9Sstevel@tonic-gate 		dist = orow - nrow;
137*7c478bd9Sstevel@tonic-gate 	} else {
138*7c478bd9Sstevel@tonic-gate 		parm_cursor = __MOVE_N_DOWN;
139*7c478bd9Sstevel@tonic-gate 		one_step = __MOVE_DOWN;
140*7c478bd9Sstevel@tonic-gate 		dist = nrow - orow;
141*7c478bd9Sstevel@tonic-gate 	}
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	/* try out direct row addressing */
144*7c478bd9Sstevel@tonic-gate 	zero_seq(best);
145*7c478bd9Sstevel@tonic-gate 	add_op(best, __MOVE_ROW, nrow, 0);
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	/* try out paramaterized up or down motion */
148*7c478bd9Sstevel@tonic-gate 	zero_seq(try);
149*7c478bd9Sstevel@tonic-gate 	add_op(try, parm_cursor, dist, 0);
150*7c478bd9Sstevel@tonic-gate 	Make_seq_best(best, try);
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	/* try getting there one step at a time... */
153*7c478bd9Sstevel@tonic-gate 	zero_seq(try);
154*7c478bd9Sstevel@tonic-gate 	add_op(try, one_step, dist, 0);
155*7c478bd9Sstevel@tonic-gate 	Make_seq_best(best, try);
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	add_seq(outseq, best);
158*7c478bd9Sstevel@tonic-gate }
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate /*
161*7c478bd9Sstevel@tonic-gate  * Motion indexes used in simp_col().
162*7c478bd9Sstevel@tonic-gate  */
163*7c478bd9Sstevel@tonic-gate typedef struct {
164*7c478bd9Sstevel@tonic-gate 	int	_tab;	/* Tab index. */
165*7c478bd9Sstevel@tonic-gate 	int	_one;	/* Single-step index, same direction as tab. */
166*7c478bd9Sstevel@tonic-gate 	int	_opp;	/* Single-step index, opposite direction to tab. */
167*7c478bd9Sstevel@tonic-gate } t_steps;
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate /*
170*7c478bd9Sstevel@tonic-gate  * simp_col(outseq, oldcol, newcol)
171*7c478bd9Sstevel@tonic-gate  *
172*7c478bd9Sstevel@tonic-gate  * simp_col() adds the best simple sequence for getting from oldcol
173*7c478bd9Sstevel@tonic-gate  * to newcol to outseq. simp_col() considers (back_)tab and
174*7c478bd9Sstevel@tonic-gate  * cursor_left/right.
175*7c478bd9Sstevel@tonic-gate  */
176*7c478bd9Sstevel@tonic-gate static void
simp_col(struct Sequence * outseq,int oc,int nc)177*7c478bd9Sstevel@tonic-gate simp_col(struct Sequence *outseq, int oc, int nc)
178*7c478bd9Sstevel@tonic-gate {
179*7c478bd9Sstevel@tonic-gate 	t_steps	*dir;
180*7c478bd9Sstevel@tonic-gate 	int	dist, tabs, tabstop;
181*7c478bd9Sstevel@tonic-gate 	struct Sequence	seqA, seqB, *best, *try;
182*7c478bd9Sstevel@tonic-gate 	static t_steps	right = { __MOVE_TAB, __MOVE_RIGHT, __MOVE_LEFT };
183*7c478bd9Sstevel@tonic-gate 	static t_steps	left = { __MOVE_BACK_TAB, __MOVE_LEFT, __MOVE_RIGHT };
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	if (oc == nc)
186*7c478bd9Sstevel@tonic-gate 		return;
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	tabs = tabstop = 0;
189*7c478bd9Sstevel@tonic-gate 	best = &seqA;
190*7c478bd9Sstevel@tonic-gate 	try = &seqB;
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	if (oc < nc) {
193*7c478bd9Sstevel@tonic-gate 		dir = &right;
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 		if (0 < init_tabs) {
196*7c478bd9Sstevel@tonic-gate 			/* Tabstop preceeding nc. */
197*7c478bd9Sstevel@tonic-gate 			tabstop = nc / init_tabs;
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 			tabs = tabstop - oc / init_tabs;
200*7c478bd9Sstevel@tonic-gate 			if (0 < tabs)
201*7c478bd9Sstevel@tonic-gate 				/* Set oc to tabstop before nc : oc <= nc. */
202*7c478bd9Sstevel@tonic-gate 				oc = tabstop * init_tabs;
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 			/* Distance from next tabstop to nc in columns. */
205*7c478bd9Sstevel@tonic-gate 			tabstop = init_tabs - nc % init_tabs;
206*7c478bd9Sstevel@tonic-gate 		}
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 		dist = nc - oc;
209*7c478bd9Sstevel@tonic-gate 	} else {
210*7c478bd9Sstevel@tonic-gate 		dir = &left;
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 		if (0 < init_tabs) {
213*7c478bd9Sstevel@tonic-gate 			/* Tabstop preceeding nc. */
214*7c478bd9Sstevel@tonic-gate 			tabstop = nc / init_tabs;
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 			tabs = (oc - 1) / init_tabs - tabstop;
217*7c478bd9Sstevel@tonic-gate 			if (0 < tabs)
218*7c478bd9Sstevel@tonic-gate 				/* Set oc to tabstop after nc : nc <= oc. */
219*7c478bd9Sstevel@tonic-gate 				oc = (tabstop + 1) * init_tabs;
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 			/* Distance from tabstop preceeding nc in columns. */
222*7c478bd9Sstevel@tonic-gate 			tabstop = nc % init_tabs;
223*7c478bd9Sstevel@tonic-gate 		}
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 		dist = oc - nc;
226*7c478bd9Sstevel@tonic-gate 	}
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 	if (0 < tabs) {
229*7c478bd9Sstevel@tonic-gate 		/* Tab as close as possible to nc. */
230*7c478bd9Sstevel@tonic-gate 		zero_seq(best);
231*7c478bd9Sstevel@tonic-gate 		add_op(best, dir->_tab, tabs, 0);
232*7c478bd9Sstevel@tonic-gate 		add_seq(outseq, best);
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 		/* If tabs alone get us there, then stop. */
235*7c478bd9Sstevel@tonic-gate 		if (oc == nc)
236*7c478bd9Sstevel@tonic-gate 			return;
237*7c478bd9Sstevel@tonic-gate 	}
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	/*
240*7c478bd9Sstevel@tonic-gate 	 * We're not exactly positioned yet.  Compare the worth of
241*7c478bd9Sstevel@tonic-gate 	 * two sequences :
242*7c478bd9Sstevel@tonic-gate 	 *   1.	single-step to location;
243*7c478bd9Sstevel@tonic-gate 	 *   2.	over tab by one tabstop, then single-step back to location.
244*7c478bd9Sstevel@tonic-gate 	 */
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	/* 1. Single-step to location. */
247*7c478bd9Sstevel@tonic-gate 	zero_seq(best);
248*7c478bd9Sstevel@tonic-gate 	add_op(best, dir->_one, dist, 0);
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	/* 2. Over tab by one tabstop, then single-step back to location. */
251*7c478bd9Sstevel@tonic-gate 	if (0 < tabstop &&
252*7c478bd9Sstevel@tonic-gate 		(nc < columns-init_tabs || auto_left_margin ||
253*7c478bd9Sstevel@tonic-gate 		eat_newline_glitch)) {
254*7c478bd9Sstevel@tonic-gate 		zero_seq(try);
255*7c478bd9Sstevel@tonic-gate 		add_op(try, dir->_tab, 1, 0);
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 		/*
258*7c478bd9Sstevel@tonic-gate 		 * vt100 terminals only wrap the cursor when a spacing
259*7c478bd9Sstevel@tonic-gate 		 * character is written.  Control characters like <tab>
260*7c478bd9Sstevel@tonic-gate 		 * will not cause a line wrap.  Adjust the number of
261*7c478bd9Sstevel@tonic-gate 		 * columns to backup by to reflect the cursor having been
262*7c478bd9Sstevel@tonic-gate 		 * placed in the last column.  See O'Reilly Termcap &
263*7c478bd9Sstevel@tonic-gate 		 * Terminfo book.
264*7c478bd9Sstevel@tonic-gate 		 */
265*7c478bd9Sstevel@tonic-gate 		if (eat_newline_glitch && columns <= nc + tabstop)
266*7c478bd9Sstevel@tonic-gate 			tabstop = columns - nc - 1;
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 		add_op(try, dir->_opp, tabstop, 0);
269*7c478bd9Sstevel@tonic-gate 		Make_seq_best(best, try);
270*7c478bd9Sstevel@tonic-gate 	}
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 	add_seq(outseq, best);
273*7c478bd9Sstevel@tonic-gate }
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate /*
276*7c478bd9Sstevel@tonic-gate  * column() adds the best sequence for moving the cursor from oldcol
277*7c478bd9Sstevel@tonic-gate  * to newcol to outseq.
278*7c478bd9Sstevel@tonic-gate  *
279*7c478bd9Sstevel@tonic-gate  * column() considers column_address, parm_left/right_cursor,
280*7c478bd9Sstevel@tonic-gate  * simp_col() and carriage_return + simp_col().
281*7c478bd9Sstevel@tonic-gate  */
282*7c478bd9Sstevel@tonic-gate static void
column(struct Sequence * outseq,int ocol,int ncol)283*7c478bd9Sstevel@tonic-gate column(struct Sequence *outseq, int ocol, int ncol)
284*7c478bd9Sstevel@tonic-gate {
285*7c478bd9Sstevel@tonic-gate 	struct Sequence	seqA, seqB;
286*7c478bd9Sstevel@tonic-gate 	struct Sequence	*best = &seqA;
287*7c478bd9Sstevel@tonic-gate 	struct Sequence	*try = &seqB;
288*7c478bd9Sstevel@tonic-gate 	int	parm_cursor, dist;
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 	if (ncol == ocol)
291*7c478bd9Sstevel@tonic-gate 		return;
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 	/* try out direct column addressing */
294*7c478bd9Sstevel@tonic-gate 	zero_seq(best);
295*7c478bd9Sstevel@tonic-gate 	add_op(best, __MOVE_COLUMN, ncol, 0);
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate 	/* try out paramaterized left or right motion */
298*7c478bd9Sstevel@tonic-gate 	if (ncol < ocol) {
299*7c478bd9Sstevel@tonic-gate 		parm_cursor = __MOVE_N_LEFT;
300*7c478bd9Sstevel@tonic-gate 		dist = ocol - ncol;
301*7c478bd9Sstevel@tonic-gate 	} else {
302*7c478bd9Sstevel@tonic-gate 		parm_cursor = __MOVE_N_RIGHT;
303*7c478bd9Sstevel@tonic-gate 		dist = ncol - ocol;
304*7c478bd9Sstevel@tonic-gate 	}
305*7c478bd9Sstevel@tonic-gate 	zero_seq(try);
306*7c478bd9Sstevel@tonic-gate 	add_op(try, parm_cursor, dist, 0);
307*7c478bd9Sstevel@tonic-gate 	Make_seq_best(best, try);
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 	if (ncol < ocol || !relative) {
310*7c478bd9Sstevel@tonic-gate 		/* try carriage_return then simp_col() */
311*7c478bd9Sstevel@tonic-gate 		zero_seq(try);
312*7c478bd9Sstevel@tonic-gate 		add_op(try, __MOVE_RETURN, 1, 0);
313*7c478bd9Sstevel@tonic-gate 		simp_col(try, 0, ncol);
314*7c478bd9Sstevel@tonic-gate 		Make_seq_best(best, try);
315*7c478bd9Sstevel@tonic-gate 	}
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate 	/* try getting there by simpl_col() */
318*7c478bd9Sstevel@tonic-gate 	zero_seq(try);
319*7c478bd9Sstevel@tonic-gate 	simp_col(try, ocol, ncol);
320*7c478bd9Sstevel@tonic-gate 	Make_seq_best(best, try);
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate 	add_seq(outseq, best);
323*7c478bd9Sstevel@tonic-gate }
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate /*
326*7c478bd9Sstevel@tonic-gate  * send relevant terminal sequences to the screen
327*7c478bd9Sstevel@tonic-gate  */
328*7c478bd9Sstevel@tonic-gate static int
out_seq(struct Sequence * seq,int (* putout)(int))329*7c478bd9Sstevel@tonic-gate out_seq(struct Sequence *seq, int (*putout)(int))
330*7c478bd9Sstevel@tonic-gate {
331*7c478bd9Sstevel@tonic-gate 	long	p1, p2;
332*7c478bd9Sstevel@tonic-gate 	int	*ptr, op;
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate 	if (__MOVE_INFINITY <= seq->cost)
335*7c478bd9Sstevel@tonic-gate 		return (ERR);
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate 	for (ptr = seq->vec; ptr < seq->end; ) {
338*7c478bd9Sstevel@tonic-gate 		op = *ptr++;
339*7c478bd9Sstevel@tonic-gate 		p1 = *ptr++;
340*7c478bd9Sstevel@tonic-gate 		p2 = *ptr++;
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate 		if (op < __MOVE_MAX_RELATIVE) {
343*7c478bd9Sstevel@tonic-gate 			while (0 < p1--)
344*7c478bd9Sstevel@tonic-gate 				(void) TPUTS(cur_term->_move[op]._seq, 1,
345*7c478bd9Sstevel@tonic-gate 					putout);
346*7c478bd9Sstevel@tonic-gate 		} else {
347*7c478bd9Sstevel@tonic-gate 			(void) TPUTS(tparm(cur_term->_move[op]._seq, p1, p2,
348*7c478bd9Sstevel@tonic-gate 				0, 0, 0, 0, 0, 0, 0), 1, putout);
349*7c478bd9Sstevel@tonic-gate 		}
350*7c478bd9Sstevel@tonic-gate 	}
351*7c478bd9Sstevel@tonic-gate 
352*7c478bd9Sstevel@tonic-gate 	return (OK);
353*7c478bd9Sstevel@tonic-gate }
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate /*
356*7c478bd9Sstevel@tonic-gate  * Low-level relative cursor motion.  __m_mvcur() looks for the optimal
357*7c478bd9Sstevel@tonic-gate  * way to move the cursor from point A to point B.  If either of the
358*7c478bd9Sstevel@tonic-gate  * coordinates for point A are -1 then only absolute addressing is used.
359*7c478bd9Sstevel@tonic-gate  * If the coordinates are out-of-bounds then they are MODed into bounds.
360*7c478bd9Sstevel@tonic-gate  *
361*7c478bd9Sstevel@tonic-gate  * Since __m_mvcur() must perform output to various terminals, an API
362*7c478bd9Sstevel@tonic-gate  * similar to tputs() and vidputs() was adopted.
363*7c478bd9Sstevel@tonic-gate  */
364*7c478bd9Sstevel@tonic-gate int
__m_mvcur(int oldrow,int oldcol,int newrow,int newcol,int (* putout)(int))365*7c478bd9Sstevel@tonic-gate __m_mvcur(int oldrow, int oldcol, int newrow, int newcol, int (*putout)(int))
366*7c478bd9Sstevel@tonic-gate {
367*7c478bd9Sstevel@tonic-gate 	struct Sequence	seqA, seqB;	/* allocate work structures */
368*7c478bd9Sstevel@tonic-gate 	struct Sequence	col0seq;	/* sequence to get from col0 to nc */
369*7c478bd9Sstevel@tonic-gate 	struct Sequence	*best = &seqA;	/* best sequence so far */
370*7c478bd9Sstevel@tonic-gate 	struct Sequence	*try = &seqB;	/* next try */
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 	newrow %= lines;
373*7c478bd9Sstevel@tonic-gate 	newcol %= columns;
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 	zero_seq(best);
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 	/* try out direct cursor addressing */
378*7c478bd9Sstevel@tonic-gate 	add_op(best, __MOVE_ROW_COLUMN, newrow, newcol);
379*7c478bd9Sstevel@tonic-gate 
380*7c478bd9Sstevel@tonic-gate 	if (newrow == lines-1 && newcol == columns-1) {
381*7c478bd9Sstevel@tonic-gate 		/* Force absolute position at bottom right because we	*/
382*7c478bd9Sstevel@tonic-gate 		/* don't know where the terminal thinks it is...	*/
383*7c478bd9Sstevel@tonic-gate 		return (out_seq(best, putout));
384*7c478bd9Sstevel@tonic-gate 	}
385*7c478bd9Sstevel@tonic-gate 	if ((relative = (0 <= oldrow && 0 <= oldcol)) != 0) {
386*7c478bd9Sstevel@tonic-gate 		oldrow %= lines;
387*7c478bd9Sstevel@tonic-gate 		oldcol %= columns;
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 		/* try out independent row/column addressing */
390*7c478bd9Sstevel@tonic-gate 		zero_seq(try);
391*7c478bd9Sstevel@tonic-gate 		row(try, oldrow, newrow);
392*7c478bd9Sstevel@tonic-gate 		column(try, oldcol, newcol);
393*7c478bd9Sstevel@tonic-gate 		Make_seq_best(best, try);
394*7c478bd9Sstevel@tonic-gate 	}
395*7c478bd9Sstevel@tonic-gate 	if (newcol < oldcol || !relative) {
396*7c478bd9Sstevel@tonic-gate 		zero_seq(&col0seq);
397*7c478bd9Sstevel@tonic-gate 		column(&col0seq, 0, newcol);
398*7c478bd9Sstevel@tonic-gate 		if (col0seq.cost < __MOVE_INFINITY) {
399*7c478bd9Sstevel@tonic-gate 			/* try out homing and then row/column */
400*7c478bd9Sstevel@tonic-gate 			if (newrow < oldrow || !relative) {
401*7c478bd9Sstevel@tonic-gate 				zero_seq(try);
402*7c478bd9Sstevel@tonic-gate 				add_op(try, __MOVE_HOME, 1, 0);
403*7c478bd9Sstevel@tonic-gate 				row(try, 0, newrow);
404*7c478bd9Sstevel@tonic-gate 				add_seq(try, &col0seq);
405*7c478bd9Sstevel@tonic-gate 				Make_seq_best(best, try);
406*7c478bd9Sstevel@tonic-gate 			}
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 			/* try out homing to last line  and then row/column */
409*7c478bd9Sstevel@tonic-gate 			if (newrow > oldrow || !relative) {
410*7c478bd9Sstevel@tonic-gate 				zero_seq(try);
411*7c478bd9Sstevel@tonic-gate 				add_op(try, __MOVE_LAST_LINE, 1, 0);
412*7c478bd9Sstevel@tonic-gate 				row(try, lines - 1, newrow);
413*7c478bd9Sstevel@tonic-gate 				add_seq(try, &col0seq);
414*7c478bd9Sstevel@tonic-gate 				Make_seq_best(best, try);
415*7c478bd9Sstevel@tonic-gate 			}
416*7c478bd9Sstevel@tonic-gate 		}
417*7c478bd9Sstevel@tonic-gate 	}
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 	return (out_seq(best, putout));
420*7c478bd9Sstevel@tonic-gate }
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate /*
423*7c478bd9Sstevel@tonic-gate  * A do nothing output function for tputs().
424*7c478bd9Sstevel@tonic-gate  */
425*7c478bd9Sstevel@tonic-gate static int
nilout(int ch)426*7c478bd9Sstevel@tonic-gate nilout(int ch)
427*7c478bd9Sstevel@tonic-gate {
428*7c478bd9Sstevel@tonic-gate 	return (ch);
429*7c478bd9Sstevel@tonic-gate }
430*7c478bd9Sstevel@tonic-gate 
431*7c478bd9Sstevel@tonic-gate /*
432*7c478bd9Sstevel@tonic-gate  * Initialize an entry in cur_term->_move[] with parameters p1 and p2.
433*7c478bd9Sstevel@tonic-gate  * Note that some capabilities will ignore their parameters.
434*7c478bd9Sstevel@tonic-gate  */
435*7c478bd9Sstevel@tonic-gate static void
cost(char * cap,int index,int p1,int p2)436*7c478bd9Sstevel@tonic-gate cost(char *cap, int index, int p1, int p2)
437*7c478bd9Sstevel@tonic-gate {
438*7c478bd9Sstevel@tonic-gate 	cur_term->_move[index]._seq = cap;
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate 	if (cap == (char *) 0 || cap[0] == '\0') {
441*7c478bd9Sstevel@tonic-gate 		cur_term->_move[index]._cost = __MOVE_INFINITY;
442*7c478bd9Sstevel@tonic-gate 	} else {
443*7c478bd9Sstevel@tonic-gate 		cur_term->_move[index]._cost = __m_tputs(
444*7c478bd9Sstevel@tonic-gate 			tparm(cap, (long) p1, (long) p2, 0, 0, 0, 0, 0, 0, 0),
445*7c478bd9Sstevel@tonic-gate 			1, nilout);
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate 		if (cap == cursor_down && strchr(cap, '\n') != (char *) 0)
448*7c478bd9Sstevel@tonic-gate 			cur_term->_move[index]._cost = __MOVE_INFINITY;
449*7c478bd9Sstevel@tonic-gate 	}
450*7c478bd9Sstevel@tonic-gate }
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate void
__m_mvcur_cost(void)453*7c478bd9Sstevel@tonic-gate __m_mvcur_cost(void)
454*7c478bd9Sstevel@tonic-gate {
455*7c478bd9Sstevel@tonic-gate 	/*
456*7c478bd9Sstevel@tonic-gate 	 * Relative cursor motion that will be costed on a per
457*7c478bd9Sstevel@tonic-gate 	 * character basis in __m_mvcur().
458*7c478bd9Sstevel@tonic-gate 	 */
459*7c478bd9Sstevel@tonic-gate 	cost(cursor_up, __MOVE_UP, 0, 0);
460*7c478bd9Sstevel@tonic-gate 	cost(cursor_down, __MOVE_DOWN, 0, 0);
461*7c478bd9Sstevel@tonic-gate 	cost(cursor_left, __MOVE_LEFT, 0, 0);
462*7c478bd9Sstevel@tonic-gate 	cost(cursor_right, __MOVE_RIGHT, 0, 0);
463*7c478bd9Sstevel@tonic-gate 	cost(dest_tabs_magic_smso ? NULL : tab, __MOVE_TAB, 0, 0);
464*7c478bd9Sstevel@tonic-gate 	cost(dest_tabs_magic_smso ? NULL : back_tab,
465*7c478bd9Sstevel@tonic-gate 		__MOVE_BACK_TAB, 0, 0);
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 	/* Absolute cursor motion with fixed cost. */
468*7c478bd9Sstevel@tonic-gate 	cost(cursor_home, __MOVE_HOME, 0, 0);
469*7c478bd9Sstevel@tonic-gate 	cost(cursor_to_ll, __MOVE_LAST_LINE, 0, 0);
470*7c478bd9Sstevel@tonic-gate 	cost(carriage_return, __MOVE_RETURN, 0, 0);
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate 	/* Parameter cursor motion with worst case cost. */
473*7c478bd9Sstevel@tonic-gate 	cost(row_address, __MOVE_ROW, lines-1, 0);
474*7c478bd9Sstevel@tonic-gate 	cost(parm_up_cursor, __MOVE_N_UP, lines-1, 0);
475*7c478bd9Sstevel@tonic-gate 	cost(parm_down_cursor, __MOVE_N_DOWN, lines-1, 0);
476*7c478bd9Sstevel@tonic-gate 	cost(column_address, __MOVE_COLUMN, columns-1, 0);
477*7c478bd9Sstevel@tonic-gate 	cost(parm_left_cursor, __MOVE_N_LEFT, columns-1, 0);
478*7c478bd9Sstevel@tonic-gate 	cost(parm_right_cursor, __MOVE_N_RIGHT, columns-1, 0);
479*7c478bd9Sstevel@tonic-gate 	cost(cursor_address, __MOVE_ROW_COLUMN, lines-1, columns-1);
480*7c478bd9Sstevel@tonic-gate }
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate #undef mvcur
483*7c478bd9Sstevel@tonic-gate 
484*7c478bd9Sstevel@tonic-gate int
mvcur(int oy,int ox,int ny,int nx)485*7c478bd9Sstevel@tonic-gate mvcur(int oy, int ox, int ny, int nx)
486*7c478bd9Sstevel@tonic-gate {
487*7c478bd9Sstevel@tonic-gate 	return (__m_mvcur(oy, ox, ny, nx, __m_outc));
488*7c478bd9Sstevel@tonic-gate }
489