xref: /netbsd-src/lib/libcurses/addbytes.c (revision 0b9f50897e9a9c6709320fafb4c3787fddcc0a45)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 /*static char sccsid[] = "from: @(#)addbytes.c	5.8 (Berkeley) 8/28/92";*/
36 static char rcsid[] = "$Id: addbytes.c,v 1.6 1993/08/15 16:23:57 mycroft Exp $";
37 #endif	/* not lint */
38 
39 #include <curses.h>
40 #include <termios.h>
41 
42 #define	SYNCH_IN	{y = win->_cury; x = win->_curx;}
43 #define	SYNCH_OUT	{win->_cury = y; win->_curx = x;}
44 
45 /*
46  * waddbytes --
47  *	Add the character to the current position in the given window.
48  */
49 int
50 waddbytes(win, bytes, count)
51 	register WINDOW *win;
52 	register const char *bytes;
53 	register int count;
54 {
55 	static char blanks[] = "        ";
56 	register int c, newx, x, y;
57 
58 	SYNCH_IN;
59 
60 #ifdef DEBUG
61 	__TRACE("ADDBYTES('%c') at (%d, %d)\n", c, y, x);
62 #endif
63 	while (count--) {
64 		c = *bytes++;
65 		switch (c) {
66 		case '\t':
67 			SYNCH_OUT;
68 			if (waddbytes(win, blanks, 8 - (x % 8)) == ERR)
69 				return (ERR);
70 			SYNCH_IN;
71 			break;
72 
73 		default:
74 #ifdef DEBUG
75 	__TRACE("ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n",
76 	    y, x, win->_firstch[y], win->_lastch[y]);
77 #endif
78 			if (win->_flags & _STANDOUT)
79 				c |= _STANDOUT;
80 #ifdef DEBUG
81 	__TRACE("ADDBYTES(%0.2o, %d, %d)\n", win, y, x);
82 #endif
83 			if (win->_y[y][x] != c) {
84 				newx = x + win->_ch_off;
85 				if (win->_firstch[y] == _NOCHANGE)
86 					win->_firstch[y] =
87 					    win->_lastch[y] = newx;
88 				else if (newx < win->_firstch[y])
89 					win->_firstch[y] = newx;
90 				else if (newx > win->_lastch[y])
91 					win->_lastch[y] = newx;
92 #ifdef DEBUG
93 	__TRACE("ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
94 	    win->_firstch[y], win->_lastch[y],
95 	    win->_firstch[y] - win->_ch_off,
96 	    win->_lastch[y] - win->_ch_off);
97 #endif
98 			}
99 			win->_y[y][x] = c;
100 			if (++x >= win->_maxx) {
101 				x = 0;
102 newline:			if (++y >= win->_maxy)
103 					if (win->_scroll) {
104 						SYNCH_OUT;
105 						scroll(win);
106 						SYNCH_IN;
107 						--y;
108 					} else
109 						return (ERR);
110 			}
111 #ifdef DEBUG
112 	__TRACE("ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n",
113 	    y, x, win->_firstch[y], win->_lastch[y]);
114 #endif
115 			break;
116 		case '\n':
117 			SYNCH_OUT;
118 			wclrtoeol(win);
119 			SYNCH_IN;
120 			if (origtermio.c_oflag & ONLCR)
121 				x = 0;
122 			goto newline;
123 		case '\r':
124 			x = 0;
125 			break;
126 		case '\b':
127 			if (--x < 0)
128 				x = 0;
129 			break;
130 		}
131 	}
132 	SYNCH_OUT;
133 	return (OK);
134 }
135