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