xref: /netbsd-src/lib/libcurses/addbytes.c (revision 62a3457d0ebe6378ca3ac69f937f612fae886cec)
161f28255Scgd /*
261f28255Scgd  * Copyright (c) 1987 Regents of the University of California.
361f28255Scgd  * All rights reserved.
461f28255Scgd  *
561f28255Scgd  * Redistribution and use in source and binary forms, with or without
661f28255Scgd  * modification, are permitted provided that the following conditions
761f28255Scgd  * are met:
861f28255Scgd  * 1. Redistributions of source code must retain the above copyright
961f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1061f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1261f28255Scgd  *    documentation and/or other materials provided with the distribution.
1361f28255Scgd  * 3. All advertising materials mentioning features or use of this software
1461f28255Scgd  *    must display the following acknowledgement:
1561f28255Scgd  *	This product includes software developed by the University of
1661f28255Scgd  *	California, Berkeley and its contributors.
1761f28255Scgd  * 4. Neither the name of the University nor the names of its contributors
1861f28255Scgd  *    may be used to endorse or promote products derived from this software
1961f28255Scgd  *    without specific prior written permission.
2061f28255Scgd  *
2161f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2261f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2361f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2461f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2561f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2661f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2761f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2861f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2961f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3061f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3161f28255Scgd  * SUCH DAMAGE.
3261f28255Scgd  */
3361f28255Scgd 
3461f28255Scgd #ifndef lint
35*62a3457dSmycroft /*static char sccsid[] = "from: @(#)addbytes.c	5.8 (Berkeley) 8/28/92";*/
36*62a3457dSmycroft static char rcsid[] = "$Id: addbytes.c,v 1.5 1993/08/07 05:48:38 mycroft Exp $";
3761f28255Scgd #endif	/* not lint */
3861f28255Scgd 
39*62a3457dSmycroft #include <curses.h>
40*62a3457dSmycroft #include <termios.h>
4161f28255Scgd 
42*62a3457dSmycroft #define	SYNCH_IN	{y = win->_cury; x = win->_curx;}
43*62a3457dSmycroft #define	SYNCH_OUT	{win->_cury = y; win->_curx = x;}
4475de25bbSalm 
4561f28255Scgd /*
46*62a3457dSmycroft  * waddbytes --
47*62a3457dSmycroft  *	Add the character to the current position in the given window.
4861f28255Scgd  */
49*62a3457dSmycroft int
50*62a3457dSmycroft waddbytes(win, bytes, count)
51*62a3457dSmycroft 	register WINDOW *win;
52*62a3457dSmycroft 	register char *bytes;
53*62a3457dSmycroft 	register int count;
5461f28255Scgd {
55*62a3457dSmycroft 	static char blanks[] = "        ";
56*62a3457dSmycroft 	register int c, newx, x, y;
5761f28255Scgd 
58*62a3457dSmycroft 	SYNCH_IN;
59*62a3457dSmycroft 
60*62a3457dSmycroft #ifdef DEBUG
61*62a3457dSmycroft 	__TRACE("ADDBYTES('%c') at (%d, %d)\n", c, y, x);
62*62a3457dSmycroft #endif
6361f28255Scgd 	while (count--) {
6461f28255Scgd 		c = *bytes++;
6561f28255Scgd 		switch (c) {
6661f28255Scgd 		case '\t':
67*62a3457dSmycroft 			SYNCH_OUT;
68*62a3457dSmycroft 			if (waddbytes(win, blanks, 8 - (x % 8)) == ERR)
69*62a3457dSmycroft 				return (ERR);
70*62a3457dSmycroft 			SYNCH_IN;
7161f28255Scgd 			break;
7261f28255Scgd 
7361f28255Scgd 		default:
74*62a3457dSmycroft #ifdef DEBUG
75*62a3457dSmycroft 	__TRACE("ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n",
76*62a3457dSmycroft 	    y, x, win->_firstch[y], win->_lastch[y]);
7761f28255Scgd #endif
7861f28255Scgd 			if (win->_flags & _STANDOUT)
7961f28255Scgd 				c |= _STANDOUT;
80*62a3457dSmycroft #ifdef DEBUG
81*62a3457dSmycroft 	__TRACE("ADDBYTES(%0.2o, %d, %d)\n", win, y, x);
8261f28255Scgd #endif
8361f28255Scgd 			if (win->_y[y][x] != c) {
8461f28255Scgd 				newx = x + win->_ch_off;
85*62a3457dSmycroft 				if (win->_firstch[y] == _NOCHANGE)
8661f28255Scgd 					win->_firstch[y] =
8761f28255Scgd 					    win->_lastch[y] = newx;
88*62a3457dSmycroft 				else if (newx < win->_firstch[y])
8961f28255Scgd 					win->_firstch[y] = newx;
9061f28255Scgd 				else if (newx > win->_lastch[y])
9161f28255Scgd 					win->_lastch[y] = newx;
92*62a3457dSmycroft #ifdef DEBUG
93*62a3457dSmycroft 	__TRACE("ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
9461f28255Scgd 	    win->_firstch[y], win->_lastch[y],
9561f28255Scgd 	    win->_firstch[y] - win->_ch_off,
9661f28255Scgd 	    win->_lastch[y] - win->_ch_off);
9761f28255Scgd #endif
9861f28255Scgd 			}
99*62a3457dSmycroft 			win->_y[y][x] = c;
100*62a3457dSmycroft 			if (++x >= win->_maxx) {
10161f28255Scgd 				x = 0;
102*62a3457dSmycroft newline:			if (++y >= win->_maxy)
10361f28255Scgd 					if (win->_scroll) {
104*62a3457dSmycroft 						SYNCH_OUT;
10561f28255Scgd 						scroll(win);
106*62a3457dSmycroft 						SYNCH_IN;
107*62a3457dSmycroft 						--y;
108*62a3457dSmycroft 					} else
109*62a3457dSmycroft 						return (ERR);
11061f28255Scgd 			}
111*62a3457dSmycroft #ifdef DEBUG
112*62a3457dSmycroft 	__TRACE("ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n",
113*62a3457dSmycroft 	    y, x, win->_firstch[y], win->_lastch[y]);
11461f28255Scgd #endif
11561f28255Scgd 			break;
11661f28255Scgd 		case '\n':
117*62a3457dSmycroft 			SYNCH_OUT;
11861f28255Scgd 			wclrtoeol(win);
119*62a3457dSmycroft 			SYNCH_IN;
120*62a3457dSmycroft 			if (origtermio.c_oflag & ONLCR)
12161f28255Scgd 				x = 0;
12261f28255Scgd 			goto newline;
12361f28255Scgd 		case '\r':
12461f28255Scgd 			x = 0;
12561f28255Scgd 			break;
12661f28255Scgd 		case '\b':
12761f28255Scgd 			if (--x < 0)
12861f28255Scgd 				x = 0;
12961f28255Scgd 			break;
13061f28255Scgd 		}
13161f28255Scgd 	}
132*62a3457dSmycroft 	SYNCH_OUT;
133*62a3457dSmycroft 	return (OK);
13461f28255Scgd }
135