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