xref: /illumos-gate/usr/src/lib/libcurses/screen/winsch.c (revision bb9b6b3f59b8820022416cea99b49c50fef6e391)
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 1997 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*LINTLIBRARY*/
43 
44 #include	<sys/types.h>
45 #include	"curses_inc.h"
46 #include	<ctype.h>
47 
48 /* Insert a character at (curx, cury). */
49 
50 int
51 winsch(WINDOW *win, chtype c)
52 {
53 	short	curx = win->_curx;
54 	int	n, cury = win->_cury;
55 	chtype	*wcp, a;
56 	int		rv;
57 
58 	a = _ATTR(c);
59 	c &= A_CHARTEXT;
60 
61 	rv = OK;
62 	win->_insmode = TRUE;
63 	if (_scrmax > 1 && (rv = _mbvalid(win)) == ERR)
64 		goto done;
65 	/* take care of multi-byte characters */
66 	if (_mbtrue && ISMBIT(c)) {
67 		rv = _mbaddch(win, A_NORMAL, RBYTE(c));
68 		goto done;
69 	}
70 	win->_nbyte = -1;
71 	curx = win->_curx;
72 
73 	/* let waddch() worry about these */
74 	if (c == '\r' || c == '\b')
75 		return (waddch(win, c));
76 
77 	/* with \n, in contrast to waddch, we don't clear-to-eol */
78 	if (c == '\n') {
79 		if (cury >= (win->_maxy-1) || cury == win->_bmarg)
80 			return (wscrl(win, 1));
81 		else {
82 			win->_cury++;
83 			win->_curx = 0;
84 			return (OK);
85 		}
86 	}
87 
88 	/* with tabs or control characters, we have to do more */
89 	if (c == '\t') {
90 		n = (TABSIZE - (curx % TABSIZE));
91 		if ((curx + n) >= win->_maxx)
92 			n = win->_maxx - curx;
93 		c = ' ';
94 	} else {
95 		if (iscntrl((int) c) != 0) {
96 			if (curx >= win->_maxx-1)
97 				return (ERR);
98 			n = 2;
99 		} else
100 			n = 1;
101 	}
102 
103 	/* shift right */
104 	wcp = win->_y[cury] + curx;
105 	if ((rv = _mbinsshift(win, n)) == ERR)
106 		goto done;
107 
108 	/* insert new control character */
109 	if (c < ' ' || c == _CTRL('?')) {
110 		*wcp++ = '^' | win->_attrs | a;
111 		*wcp = _UNCTRL(c) | win->_attrs | a;
112 	} else {
113 		/* normal characters */
114 		c = _WCHAR(win, c) | a;
115 		for (; n > 0; --n)
116 			*wcp++ = c;
117 	}
118 
119 done:
120 	if (curx < win->_firstch[cury])
121 		win->_firstch[cury] = curx;
122 	win->_lastch[cury] = win->_maxx-1;
123 
124 	win->_flags |= _WINCHANGED;
125 
126 	if (win->_sync)
127 		wsyncup(win);
128 
129 	return ((rv == OK && win->_immed) ? wrefresh(win) : rv);
130 }
131