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 (c) 1995, by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 /*
28  * vid_puts.c
29  *
30  * XCurses Library
31  *
32  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
33  *
34  */
35 
36 #ifdef M_RCSID
37 #ifndef lint
38 static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/vid_puts.c 1.5 1995/07/19 16:38:26 ant Exp $";
39 #endif
40 #endif
41 
42 #include <private.h>
43 #include <limits.h>
44 
45 STATIC attr_t turn_off(int (*)(int), attr_t);
46 STATIC attr_t turn_on(int (*)(int), attr_t);
47 
48 /*
49  * Return true if attribute X a member of the attribute set A.
50  * no_color_video is the set of attributes that cannot be combined
51  * with colours.
52  */
53 #define ISATTR(a,x)	(((a) & ~no_color_video & (x)) == (x))
54 
55 /*f
56  * Set the desired attribute state for a terminal screen.
57  *
58  * Using set_attributes is the prefered method but requires some care
59  * in writing the proper terminfo string.  Using exit_attribute_mode and
60  * the assorted enter_ attribute mode capabilities is the next best method.
61  * Finally using the assorted exit_ and enter_ attribute mode capabilities
62  * is the last method available and is not necessarily efficent (or smart
63  * because of the needs of ceol_standout_glitch support).
64  */
65 int
vid_puts(attr_t attr,short pair,void * opts,int (* putout)(int))66 vid_puts(attr_t attr, short pair, void *opts, int (*putout)(int))
67 {
68 #ifdef M_CURSES_TRACE
69 	__m_trace("vid_puts(%x, %d, %p, %p)", attr, pair, opts, putout);
70 #endif
71 
72 	if (set_attributes != (char *) 0 && ATTR_STATE != attr) {
73 		/* Assume that <set_attributes> disables attributes
74 		 * then re-enables attributes that are to be on.
75 		 */
76 		(void) tputs(
77 			tparm(
78 				set_attributes,
79 				(long) ISATTR(attr, WA_STANDOUT),
80 				(long) ISATTR(attr, WA_UNDERLINE),
81 				(long) ISATTR(attr, WA_REVERSE),
82 				(long) ISATTR(attr, WA_BLINK),
83 				(long) ISATTR(attr, WA_DIM),
84 				(long) ISATTR(attr, WA_BOLD),
85 				(long) ISATTR(attr, WA_INVIS),
86 				(long) ISATTR(attr, WA_PROTECT),
87 				(long) ISATTR(attr, WA_ALTCHARSET)
88 			),
89 			1, putout
90 		);
91 
92 		ATTR_STATE &= ~WA_SGR_MASK;
93 		ATTR_STATE |= attr & WA_SGR_MASK;
94 
95 		/* Only use <set_a_attributes> when <set_attributes>
96 		 * is defined.  <set_a_attributes> should not disable
97 		 * attributes, as this will have been handled by
98 		 * <set_attributes>.
99 		 */
100 		if (set_a_attributes != (char *) 0 && (attr & WA_SGR1_MASK)) {
101 			(void) tputs(
102 				tparm(
103 					set_a_attributes,
104 					(long) ISATTR(attr, WA_HORIZONTAL),
105 					(long) ISATTR(attr, WA_LEFT),
106 					(long) ISATTR(attr, WA_LOW),
107 					(long) ISATTR(attr, WA_RIGHT),
108 					(long) ISATTR(attr, WA_TOP),
109 					(long) ISATTR(attr, WA_VERTICAL),
110 					0L, 0L, 0L
111 				),
112 				1, putout
113 			);
114 
115 			ATTR_STATE &= ~WA_SGR1_MASK;
116 			ATTR_STATE |= attr & WA_SGR1_MASK;
117 		}
118 	} else if (ATTR_STATE != attr) {
119 		/* Turn off only those attributes that are on. */
120 		(void) turn_off(putout, ATTR_STATE);
121 
122 		/* Turn on attributes regardless if they are already
123 		 * on, because terminals with ceol_standout_glitch, like
124 		 * HP terminals, will have to re-enforce the current
125 		 * attributes in order to change existing attribute
126 		 * cookies on the screen.
127 		 */
128 		ATTR_STATE = turn_on(putout, attr);
129 	}
130 
131 	/* A_NORMAL equals 0, which is all attributes off and
132 	 * COLOR_PAIR(0).  This implies that colour pair 0 is
133 	 * the orig_pair.
134 	 */
135 	if (attr == WA_NORMAL) {
136 		if (orig_pair != (char *) 0)
137 			(void) tputs(orig_pair, 1, putout);
138 
139 		pair = 0;
140 	} else if (pair != cur_term->_co && 0 < max_colors) {
141 		short fg, bg;
142 
143 		if (set_color_pair != (char *) 0) {
144 			(void) tputs(
145 				tparm(
146 					set_color_pair, (long) pair,
147 					0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
148 				),
149 				1, putout
150 			);
151 		} else if (pair_content(pair, &fg, &bg) == OK) {
152 			if (set_foreground != (char *) 0) {
153 				(void) tputs(
154 					tparm(set_foreground, (long) fg,
155 						0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
156 					),
157 					1, putout
158 				);
159 			} else if (set_a_foreground != (char *) 0) {
160 				(void) tputs(
161 					tparm(
162 						set_a_foreground, (long) fg,
163 						0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
164 					),
165 					1, putout
166 				);
167 			}
168 
169 			if (set_background != (char *) 0) {
170 				(void) tputs(
171 					tparm(set_background, (long) bg,
172 						0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
173 					),
174 					1, putout
175 				);
176 			} else if (set_a_background != (char *) 0) {
177 				(void) tputs(
178 					tparm(
179 						set_a_background, (long) bg,
180 						0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
181 					),
182 					1, putout
183 				);
184 			}
185 		}
186 	}
187 
188 	/* Remember the current attribute state for the terminal. */
189 	ATTR_STATE = attr;
190 	cur_term->_co = pair;
191 
192 	return __m_return_code("vid_puts", OK);
193 }
194 
195 STATIC attr_t
turn_off(int (* putout)(int),attr_t attr)196 turn_off(int (*putout)(int), attr_t attr)
197 {
198 	attr_t new = attr;
199 
200 	if (exit_attribute_mode != (char *) 0) {
201 		(void) tputs(exit_attribute_mode, 1, putout);
202 		new = WA_NORMAL;
203 	} else {
204 		if (ISATTR(attr, WA_UNDERLINE)
205 		&& exit_underline_mode != (char *) 0) {
206 			(void) tputs(exit_underline_mode, 1, putout);
207 			new &= ~WA_UNDERLINE;
208 		}
209 
210 		if (ISATTR(attr, WA_STANDOUT)
211 		&& exit_standout_mode != (char *) 0) {
212 			(void) tputs(exit_standout_mode, 1, putout);
213 			new &= ~WA_STANDOUT;
214 		}
215 
216 		if (ISATTR(attr, WA_ALTCHARSET)
217 		&& exit_alt_charset_mode != (char *) 0) {
218 			(void) tputs(exit_alt_charset_mode, 1, putout);
219 			new &= ~WA_ALTCHARSET;
220 		}
221 	}
222 
223 	return new;
224 }
225 
226 STATIC attr_t
turn_on(int (* putout)(int),attr_t attr)227 turn_on(int (*putout)(int), attr_t attr)
228 {
229 	attr_t new = attr;
230 
231 	if (ISATTR(attr, WA_ALTCHARSET)
232 	&& enter_alt_charset_mode != (char *) 0) {
233 		(void) tputs(enter_alt_charset_mode, 1, putout);
234 		new |= WA_ALTCHARSET;
235 	}
236 
237 	if (ISATTR(attr, WA_BLINK) && enter_blink_mode != (char *) 0) {
238 		(void) tputs(enter_blink_mode, 1, putout);
239 		new |= WA_BLINK;
240 	}
241 
242 	if (ISATTR(attr, WA_BOLD) && enter_bold_mode != (char *) 0) {
243 		(void) tputs(enter_bold_mode, 1, putout);
244 		new |= WA_BOLD;
245 	}
246 
247 	if (ISATTR(attr, WA_INVIS) && enter_secure_mode != (char *) 0) {
248 		(void) tputs(enter_secure_mode, 1, putout);
249 		new |= WA_INVIS;
250 	}
251 
252 	if (ISATTR(attr, WA_DIM) && enter_dim_mode != (char *) 0) {
253 		(void) tputs(enter_dim_mode, 1, putout);
254 		new |= WA_DIM;
255 	}
256 
257 	if (ISATTR(attr, WA_PROTECT) && enter_protected_mode != (char *) 0) {
258 		(void) tputs(enter_protected_mode, 1, putout);
259 		new |= WA_PROTECT;
260 	}
261 
262 	if (ISATTR(attr, WA_REVERSE) && enter_reverse_mode != (char *) 0) {
263 		(void) tputs(enter_reverse_mode, 1, putout);
264 		new |= WA_REVERSE;
265 	}
266 
267 	if (ISATTR(attr, WA_STANDOUT) && enter_standout_mode != (char *) 0) {
268 		(void) tputs(enter_standout_mode, 1, putout);
269 		new |= WA_STANDOUT;
270 	}
271 
272 	if (ISATTR(attr, WA_UNDERLINE) && enter_underline_mode != (char *) 0) {
273 		(void) tputs(enter_underline_mode, 1, putout);
274 		new |= WA_UNDERLINE;
275 	}
276 
277 	if (ISATTR(attr, WA_HORIZONTAL)
278 	&& enter_horizontal_hl_mode != (char *) 0) {
279 		(void) tputs(enter_horizontal_hl_mode, 1, putout);
280 		new |= WA_HORIZONTAL;
281 	}
282 
283 	if (ISATTR(attr, WA_LEFT) && enter_left_hl_mode != (char *) 0) {
284 		(void) tputs(enter_left_hl_mode, 1, putout);
285 		new |= WA_LEFT;
286 	}
287 
288 	if (ISATTR(attr, WA_LOW) && enter_low_hl_mode != (char *) 0) {
289 		(void) tputs(enter_low_hl_mode, 1, putout);
290 		new |= WA_LOW;
291 	}
292 
293 	if (ISATTR(attr, WA_RIGHT) && enter_right_hl_mode != (char *) 0) {
294 		(void) tputs(enter_right_hl_mode, 1, putout);
295 		new |= WA_RIGHT;
296 	}
297 
298 	if (ISATTR(attr, WA_TOP) && enter_top_hl_mode != (char *) 0) {
299 		(void) tputs(enter_top_hl_mode, 1, putout);
300 		new |= WA_TOP;
301 	}
302 
303 	if (ISATTR(attr, WA_VERTICAL) && enter_vertical_hl_mode != (char *) 0) {
304 		(void) tputs(enter_vertical_hl_mode, 1, putout);
305 		new |= WA_VERTICAL;
306 	}
307 
308 	return new;
309 }
310 
311