xref: /titanic_51/usr/src/lib/libcurses/screen/waddch.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 1997 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
45*7c478bd9Sstevel@tonic-gate #include	"curses_inc.h"
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate /*
48*7c478bd9Sstevel@tonic-gate  * This routine prints the character in the current position.
49*7c478bd9Sstevel@tonic-gate  * Think of it as putc.
50*7c478bd9Sstevel@tonic-gate  */
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate int
53*7c478bd9Sstevel@tonic-gate waddch(WINDOW *win, chtype c)
54*7c478bd9Sstevel@tonic-gate {
55*7c478bd9Sstevel@tonic-gate 	short	x = win->_curx;
56*7c478bd9Sstevel@tonic-gate 	short	y = win->_cury;
57*7c478bd9Sstevel@tonic-gate 	chtype	rawc = _CHAR(c);
58*7c478bd9Sstevel@tonic-gate 	chtype	rawattrs = _ATTR(c);
59*7c478bd9Sstevel@tonic-gate 	int	rv = OK;
60*7c478bd9Sstevel@tonic-gate 	bool	savimmed = win->_immed;
61*7c478bd9Sstevel@tonic-gate 	bool	savsync = win->_sync;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	win->_immed = win->_sync = FALSE;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
66*7c478bd9Sstevel@tonic-gate 	if (outf)
67*7c478bd9Sstevel@tonic-gate 		if (c == rawc)
68*7c478bd9Sstevel@tonic-gate 			fprintf(outf, "'%c'", rawc);
69*7c478bd9Sstevel@tonic-gate 		else
70*7c478bd9Sstevel@tonic-gate 			fprintf(outf, "'%c' %o, raw %o", c, c, rawc);
71*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	win->_insmode = FALSE;
74*7c478bd9Sstevel@tonic-gate 	if (_scrmax > 1 && _mbvalid(win) == ERR)
75*7c478bd9Sstevel@tonic-gate 		goto next;
76*7c478bd9Sstevel@tonic-gate 	if (_mbtrue && ISMBIT(rawc)) {
77*7c478bd9Sstevel@tonic-gate 		rv = _mbaddch(win, rawattrs, RBYTE(rawc));
78*7c478bd9Sstevel@tonic-gate 		win->_immed = savimmed;
79*7c478bd9Sstevel@tonic-gate 		win->_sync = savsync;
80*7c478bd9Sstevel@tonic-gate 		goto nextw;
81*7c478bd9Sstevel@tonic-gate 	}
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	switch (rawc) {
84*7c478bd9Sstevel@tonic-gate 		case '\n':
85*7c478bd9Sstevel@tonic-gate 			(void) wclrtoeol(win);
86*7c478bd9Sstevel@tonic-gate 			goto new_line;
87*7c478bd9Sstevel@tonic-gate 		case '\r':
88*7c478bd9Sstevel@tonic-gate 			goto move_to_begin_line;
89*7c478bd9Sstevel@tonic-gate 		case '\b':
90*7c478bd9Sstevel@tonic-gate 			if (--x < 0)
91*7c478bd9Sstevel@tonic-gate move_to_begin_line:
92*7c478bd9Sstevel@tonic-gate 				x = 0;
93*7c478bd9Sstevel@tonic-gate 			win->_curx = x;
94*7c478bd9Sstevel@tonic-gate 			win->_flags |= _WINMOVED;
95*7c478bd9Sstevel@tonic-gate 			goto out_move_only;
96*7c478bd9Sstevel@tonic-gate 		default:
97*7c478bd9Sstevel@tonic-gate 			if (rawc < ' ' || rawc == _CTRL('?')) {
98*7c478bd9Sstevel@tonic-gate 				if (rawc == '\t') {
99*7c478bd9Sstevel@tonic-gate 					int	newx;
100*7c478bd9Sstevel@tonic-gate 					chtype	space = ' ' | rawattrs;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 					if ((newx = x + (TABSIZE -
103*7c478bd9Sstevel@tonic-gate 					    (x % TABSIZE))) > win->_maxx)
104*7c478bd9Sstevel@tonic-gate 						newx = win->_maxx;
105*7c478bd9Sstevel@tonic-gate 						for (; x < newx; x++)
106*7c478bd9Sstevel@tonic-gate 							if (waddch(win,
107*7c478bd9Sstevel@tonic-gate 							    space) == ERR)
108*7c478bd9Sstevel@tonic-gate 								goto next;
109*7c478bd9Sstevel@tonic-gate 				} else {
110*7c478bd9Sstevel@tonic-gate 					if ((waddch(win, (chtype)
111*7c478bd9Sstevel@tonic-gate 					    '^'|rawattrs) == ERR) ||
112*7c478bd9Sstevel@tonic-gate 					    (waddch(win, (chtype)
113*7c478bd9Sstevel@tonic-gate 					    _UNCTRL(rawc)|rawattrs) == ERR)) {
114*7c478bd9Sstevel@tonic-gate next :
115*7c478bd9Sstevel@tonic-gate 						rv = ERR;
116*7c478bd9Sstevel@tonic-gate 				}
117*7c478bd9Sstevel@tonic-gate 			}
118*7c478bd9Sstevel@tonic-gate 			x = win->_curx;
119*7c478bd9Sstevel@tonic-gate 			y = win->_cury;
120*7c478bd9Sstevel@tonic-gate 			win->_immed = savimmed;
121*7c478bd9Sstevel@tonic-gate 			win->_sync = savsync;
122*7c478bd9Sstevel@tonic-gate 			break;
123*7c478bd9Sstevel@tonic-gate 		}
124*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
125*7c478bd9Sstevel@tonic-gate 		if ((win->_attrs) && outf)
126*7c478bd9Sstevel@tonic-gate 			fprintf(outf, "(attrs %o, %o=>%o)", win->_attrs,
127*7c478bd9Sstevel@tonic-gate 			    c, c | win->_attrs);
128*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 		/* clear any partial multi-column character */
131*7c478bd9Sstevel@tonic-gate 		if (_scrmax > 1 && ISMBIT(win->_y[y][x]) &&
132*7c478bd9Sstevel@tonic-gate 		    (rv = _mbclrch(win, y, x)) == ERR) {
133*7c478bd9Sstevel@tonic-gate 			x = win->_curx;
134*7c478bd9Sstevel@tonic-gate 			y = win->_cury;
135*7c478bd9Sstevel@tonic-gate 			win->_immed = savimmed;
136*7c478bd9Sstevel@tonic-gate 			win->_sync = savsync;
137*7c478bd9Sstevel@tonic-gate 			break;
138*7c478bd9Sstevel@tonic-gate 		}
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 		if ((c = _WCHAR(win, c)|rawattrs) != win->_y[y][x]) {
141*7c478bd9Sstevel@tonic-gate 			if (x < win->_firstch[y])
142*7c478bd9Sstevel@tonic-gate 				win->_firstch[y] = x;
143*7c478bd9Sstevel@tonic-gate 			if (x > win->_lastch[y])
144*7c478bd9Sstevel@tonic-gate 				win->_lastch[y] = x;
145*7c478bd9Sstevel@tonic-gate 			win->_y[y][x] = c;
146*7c478bd9Sstevel@tonic-gate #ifdef	_VR3_COMPAT_CODE
147*7c478bd9Sstevel@tonic-gate 			if (_y16update)
148*7c478bd9Sstevel@tonic-gate 				/* LINTED */
149*7c478bd9Sstevel@tonic-gate 				win->_y16[y][x] = _TO_OCHTYPE(c);
150*7c478bd9Sstevel@tonic-gate #endif	/* _VR3_COMPAT_CODE */
151*7c478bd9Sstevel@tonic-gate 		}
152*7c478bd9Sstevel@tonic-gate 		if (++x == win->_maxx) {
153*7c478bd9Sstevel@tonic-gate new_line:
154*7c478bd9Sstevel@tonic-gate 			if (y == win->_bmarg) {
155*7c478bd9Sstevel@tonic-gate 				if (wscrl(win, 1) == ERR) {
156*7c478bd9Sstevel@tonic-gate 					rv = ERR;
157*7c478bd9Sstevel@tonic-gate 					if (x == win->_maxx)
158*7c478bd9Sstevel@tonic-gate 						--x;
159*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
160*7c478bd9Sstevel@tonic-gate 					if (outf) {
161*7c478bd9Sstevel@tonic-gate 						int	i;
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 						fprintf(outf, "ERR because "
164*7c478bd9Sstevel@tonic-gate 						    "(%d, %d) > (%d, %d)\n",
165*7c478bd9Sstevel@tonic-gate 						    x, y, win->_maxx,
166*7c478bd9Sstevel@tonic-gate 						    win->_maxy);
167*7c478bd9Sstevel@tonic-gate 						fprintf(outf, "line: '");
168*7c478bd9Sstevel@tonic-gate 						for (i = 0; i < win->_maxy;
169*7c478bd9Sstevel@tonic-gate 						    i++)
170*7c478bd9Sstevel@tonic-gate 							fprintf(outf, "%c",
171*7c478bd9Sstevel@tonic-gate 							    win->_y[y-1][i]);
172*7c478bd9Sstevel@tonic-gate 						fprintf(outf, "'\n");
173*7c478bd9Sstevel@tonic-gate 					}
174*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
175*7c478bd9Sstevel@tonic-gate 					break;
176*7c478bd9Sstevel@tonic-gate 				} else
177*7c478bd9Sstevel@tonic-gate 					savimmed = 1;
178*7c478bd9Sstevel@tonic-gate 			} else
179*7c478bd9Sstevel@tonic-gate 				y++;
180*7c478bd9Sstevel@tonic-gate 				x = 0;
181*7c478bd9Sstevel@tonic-gate 		} else
182*7c478bd9Sstevel@tonic-gate 			savimmed += 2;
183*7c478bd9Sstevel@tonic-gate #ifdef	FULLDEBUG
184*7c478bd9Sstevel@tonic-gate 		if (outf)
185*7c478bd9Sstevel@tonic-gate 			fprintf(outf, "ADDCH: 2: y = %d, x = %d, "
186*7c478bd9Sstevel@tonic-gate 			    "firstch = %d, lastch = %d\n", y, x,
187*7c478bd9Sstevel@tonic-gate 			    win->_firstch[y], win->_lastch[y]);
188*7c478bd9Sstevel@tonic-gate #endif	/* FULLDEBUG */
189*7c478bd9Sstevel@tonic-gate 		break;
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate 	win->_cury = y;
192*7c478bd9Sstevel@tonic-gate 	win->_curx = x;
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate nextw:
195*7c478bd9Sstevel@tonic-gate 	/* sync with ancestor structures */
196*7c478bd9Sstevel@tonic-gate 	if (win->_sync)
197*7c478bd9Sstevel@tonic-gate 		wsyncup(win);
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 	if (savimmed == 3)
200*7c478bd9Sstevel@tonic-gate 		return ((*_quick_ptr)(win, c));
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	win->_flags |= _WINCHANGED;
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate out_move_only:
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	return ((savimmed == 1) ? wrefresh(win) : rv);
207*7c478bd9Sstevel@tonic-gate }
208