1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 /*LINTLIBRARY*/
41 
42 #include	<stdlib.h>
43 #include	<sys/types.h>
44 #include	"curses_inc.h"
45 
46 /*
47  * Figure out (roughly) how much each of these capabilities costs.
48  * In the parameterized cases, we just take a typical case and
49  * use that value.  This is done only once at startup, since it
50  * would be too expensive for intensive use.
51  */
52 
53 static  int 	_cost_fn(char *, int);
54 
55 static	short	offsets[] = {
56 		    52,		/* insert_character, */
57 		    21,		/* delete_character, */
58 		    12,		/* cursor_home, */
59 		    18,		/* cursor_to_ll, */
60 		    14,		/* cursor_left, */
61 		    17,		/* cursor_right, */
62 		    11,		/* cursor_down, */
63 		    19,		/* cursor_up, */
64 		    2,		/* carriage_return, */
65 		    134,	/* tab, */
66 		    0,		/* back_tab, */
67 		    6,		/* clr_eol, */
68 		    269,	/* clr_bol, */
69 #define	FIRST_LOOP	13
70 		    108,	/* parm_ich, */
71 		    105,	/* parm_dch, */
72 		    111,	/* parm_left_cursor, */
73 		    114,	/* parm_up_cursor, */
74 		    107,	/* parm_down_cursor, */
75 		    112,	/* parm_right_cursor, */
76 #define	SECOND_LOOP	19
77 		};
78 
79 void
_init_costs(void)80 _init_costs(void)
81 {
82 	short	*costptr = &(SP->term_costs.icfixed);
83 	char	**str_array = (char **) cur_strs;
84 	int	i = 0;
85 	char	save_xflag = xon_xoff;
86 
87 	xon_xoff = 0;
88 /*
89  * This next block of code is actually correct in that it takes into
90  * account many things that wrefresh has to keep figuring in the function
91  * _useidch.  Wrefresh MUST be changed (in the words of Tony Hansen) !!!
92  *
93  * Wrefresh has been changed (in my words -Phong Vo) !!!!
94  */
95 	*costptr++ = ((enter_insert_mode) && (exit_insert_mode)) ?
96 	    _cost_fn(enter_insert_mode, 0) + _cost_fn(exit_insert_mode, 0) : 0;
97 
98 	*costptr++ = ((enter_delete_mode) && (exit_delete_mode)) ?
99 	    _cost_fn(enter_delete_mode, 0) + _cost_fn(exit_delete_mode, 0) : 0;
100 
101 	while (i < FIRST_LOOP)
102 		*costptr++ = _cost_fn(str_array[offsets[i++]], 1);
103 
104 	while (i < SECOND_LOOP)
105 		*costptr++ = _cost_fn(tparm_p1(str_array[offsets[i++]], 10), 1);
106 
107 	*costptr++ = _cost_fn(tparm_p2(cursor_address, 8, 10), 1);
108 	*costptr++ = _cost_fn(tparm_p1(row_address, 8), 1);
109 
110 	xon_xoff = save_xflag;
111 #ifdef	DEBUG
112 	if (outf) {
113 		fprintf(outf, "icfixed %d=%d+%d\n", _COST(icfixed),
114 		    _cost_fn(enter_insert_mode, 0),
115 		    _cost_fn(exit_insert_mode, 0));
116 		fprintf(outf, "from ich1 %x '%s' %d\n", insert_character,
117 		    insert_character, _cost_fn(insert_character, 1));
118 		fprintf(outf, "ip %x '%s' %d\n", insert_padding,
119 		    insert_padding, _cost_fn(insert_padding, 1));
120 		fprintf(outf, "dcfixed %d\n", _COST(dcfixed));
121 	}
122 #endif	/* DEBUG */
123 /*FALLTHROUGH*/
124 }
125 
126 static int counter = 0;
127 int
128 /* ARGSUSED */
_countchar(char dummy)129 _countchar(char dummy)
130 {
131 	counter++;
132 	return (0);
133 }
134 
135 /*
136  * Figure out the _COST in characters to print this string.
137  * Due to padding, we can't just use strlen, so instead we
138  * feed it through tputs and trap the results.
139  * Even if the terminal uses xon/xoff handshaking, count the
140  * pad chars here since they estimate the real time to do the
141  * operation, useful in calculating costs.
142  */
143 
144 static int
_cost_fn(char * str,int affcnt)145 _cost_fn(char *str, int affcnt)
146 {
147 	if (str == NULL)
148 		return (LARGECOST);
149 	counter = 0;
150 	(void) tputs(str, affcnt, _countchar);
151 	return (counter);
152 }
153