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