xref: /netbsd-src/lib/libcurses/addbytes.c (revision 75de25bb937aed7d7fbdbd3a822fbc03d8bf6a08)
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
3561f28255Scgd static char sccsid[] = "@(#)addbytes.c	5.4 (Berkeley) 6/1/90";
3661f28255Scgd #endif /* not lint */
3761f28255Scgd 
3861f28255Scgd # include	"curses.ext"
3961f28255Scgd 
40*75de25bbSalm waddbytes(win, bytes, count)
41*75de25bbSalm reg WINDOW	*win;
42*75de25bbSalm reg char        *bytes;
43*75de25bbSalm int         count;
44*75de25bbSalm {
45*75de25bbSalm 	chtype c;
46*75de25bbSalm 	reg int i;
47*75de25bbSalm 
48*75de25bbSalm 	for (i = 0; i < count; i++) {
49*75de25bbSalm 		c = (unsigned char) *bytes++;
50*75de25bbSalm 		if (_waddbytes(win, &c, 1) == ERR)
51*75de25bbSalm 			return ERR;
52*75de25bbSalm 	}
53*75de25bbSalm 	return OK;
54*75de25bbSalm }
55*75de25bbSalm 
5661f28255Scgd /*
5761f28255Scgd  *	This routine adds the character to the current position
5861f28255Scgd  *
5961f28255Scgd  */
60*75de25bbSalm _waddbytes(win, bytes, count)
6161f28255Scgd reg WINDOW	*win;
62*75de25bbSalm reg chtype      *bytes;
6361f28255Scgd reg int		count;
6461f28255Scgd {
6561f28255Scgd #define	SYNCH_OUT()	{win->_cury = y; win->_curx = x;}
6661f28255Scgd #define	SYNCH_IN()	{y = win->_cury; x = win->_curx;}
6761f28255Scgd 	reg int		x, y;
6861f28255Scgd 	reg int		newx;
6961f28255Scgd 
7061f28255Scgd 	SYNCH_IN();
7161f28255Scgd 	while (count--) {
72*75de25bbSalm 	    register chtype c;
73*75de25bbSalm 	    static chtype blanks[] = {' ',' ',' ',' ',' ',' ',' ',' '};
7461f28255Scgd 
7561f28255Scgd 	    c = *bytes++;
7661f28255Scgd 	    switch (c) {
7761f28255Scgd 	      case '\t':
78c2ef5bd1Smycroft 		    SYNCH_OUT();
79*75de25bbSalm 		    if (_waddbytes(win, blanks, 8-(x%8)) == ERR) {
8061f28255Scgd 			return ERR;
8161f28255Scgd 		    }
82c2ef5bd1Smycroft 		    SYNCH_IN();
8361f28255Scgd 		    break;
8461f28255Scgd 
8561f28255Scgd 	      default:
8661f28255Scgd # ifdef FULLDEBUG
8761f28255Scgd 		    fprintf(outf, "ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n", y, x, win->_firstch[y], win->_lastch[y]);
8861f28255Scgd # endif
8961f28255Scgd 		    if (win->_flags & _STANDOUT)
9061f28255Scgd 			    c |= _STANDOUT;
9161f28255Scgd 		    {
9261f28255Scgd # ifdef	FULLDEBUG
9361f28255Scgd 			    fprintf(outf, "ADDBYTES(%0.2o, %d, %d)\n", win, y, x);
9461f28255Scgd # endif
9561f28255Scgd 			    if (win->_y[y][x] != c) {
9661f28255Scgd 				    newx = x + win->_ch_off;
9761f28255Scgd 				    if (win->_firstch[y] == _NOCHANGE) {
9861f28255Scgd 					    win->_firstch[y] =
9961f28255Scgd 							    win->_lastch[y] = newx;
10061f28255Scgd 				    } else if (newx < win->_firstch[y])
10161f28255Scgd 					    win->_firstch[y] = newx;
10261f28255Scgd 				    else if (newx > win->_lastch[y])
10361f28255Scgd 					    win->_lastch[y] = newx;
10461f28255Scgd # ifdef FULLDEBUG
10561f28255Scgd 				    fprintf(outf, "ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
10661f28255Scgd 					    win->_firstch[y], win->_lastch[y],
10761f28255Scgd 					    win->_firstch[y] - win->_ch_off,
10861f28255Scgd 					    win->_lastch[y] - win->_ch_off);
10961f28255Scgd # endif
11061f28255Scgd 			    }
11161f28255Scgd 		    }
11261f28255Scgd 		    win->_y[y][x++] = c;
11361f28255Scgd 		    if (x >= win->_maxx) {
11461f28255Scgd 			    x = 0;
11561f28255Scgd     newline:
11661f28255Scgd 			    if (++y >= win->_maxy)
11761f28255Scgd 				    if (win->_scroll) {
118*75de25bbSalm 					    --y;
11961f28255Scgd 					    SYNCH_OUT();
12061f28255Scgd 					    scroll(win);
12161f28255Scgd 					    SYNCH_IN();
12261f28255Scgd 				    }
12361f28255Scgd 				    else
12461f28255Scgd 					    return ERR;
12561f28255Scgd 		    }
12661f28255Scgd # ifdef FULLDEBUG
12761f28255Scgd 		    fprintf(outf, "ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n", y, x, win->_firstch[y], win->_lastch[y]);
12861f28255Scgd # endif
12961f28255Scgd 		    break;
13061f28255Scgd 	      case '\n':
13161f28255Scgd 		    SYNCH_OUT();
13261f28255Scgd 		    wclrtoeol(win);
13361f28255Scgd 		    SYNCH_IN();
13461f28255Scgd 		    if (!NONL)
13561f28255Scgd 			    x = 0;
13661f28255Scgd 		    goto newline;
13761f28255Scgd 	      case '\r':
13861f28255Scgd 		    x = 0;
13961f28255Scgd 		    break;
14061f28255Scgd 	      case '\b':
14161f28255Scgd 		    if (--x < 0)
14261f28255Scgd 			    x = 0;
14361f28255Scgd 		    break;
14461f28255Scgd 	    }
14561f28255Scgd     }
14661f28255Scgd     SYNCH_OUT();
14761f28255Scgd     return OK;
14861f28255Scgd }
149