xref: /netbsd-src/lib/libcurses/addbytes.c (revision c2ef5bd11468ee9ed8810fa5f7b2708c69e54742)
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 
4061f28255Scgd /*
4161f28255Scgd  *	This routine adds the character to the current position
4261f28255Scgd  *
4361f28255Scgd  */
4461f28255Scgd waddbytes(win, bytes, count)
4561f28255Scgd reg WINDOW	*win;
4661f28255Scgd reg char	*bytes;
4761f28255Scgd reg int		count;
4861f28255Scgd {
4961f28255Scgd #define	SYNCH_OUT()	{win->_cury = y; win->_curx = x;}
5061f28255Scgd #define	SYNCH_IN()	{y = win->_cury; x = win->_curx;}
5161f28255Scgd 	reg int		x, y;
5261f28255Scgd 	reg int		newx;
5361f28255Scgd 
5461f28255Scgd 	SYNCH_IN();
5561f28255Scgd # ifdef FULLDEBUG
5661f28255Scgd 	fprintf(outf, "ADDBYTES('%c') at (%d, %d)\n", c, y, x);
5761f28255Scgd # endif
5861f28255Scgd 	while (count--) {
5961f28255Scgd 	    register int c;
6061f28255Scgd 	    static char blanks[] = "        ";
6161f28255Scgd 
6261f28255Scgd 	    c = *bytes++;
6361f28255Scgd 	    switch (c) {
6461f28255Scgd 	      case '\t':
65*c2ef5bd1Smycroft 		    SYNCH_OUT();
6661f28255Scgd 		    if (waddbytes(win, blanks, 8-(x%8)) == ERR) {
6761f28255Scgd 			return ERR;
6861f28255Scgd 		    }
69*c2ef5bd1Smycroft 		    SYNCH_IN();
7061f28255Scgd 		    break;
7161f28255Scgd 
7261f28255Scgd 	      default:
7361f28255Scgd # ifdef FULLDEBUG
7461f28255Scgd 		    fprintf(outf, "ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n", y, x, win->_firstch[y], win->_lastch[y]);
7561f28255Scgd # endif
7661f28255Scgd 		    if (win->_flags & _STANDOUT)
7761f28255Scgd 			    c |= _STANDOUT;
7861f28255Scgd 		    {
7961f28255Scgd # ifdef	FULLDEBUG
8061f28255Scgd 			    fprintf(outf, "ADDBYTES(%0.2o, %d, %d)\n", win, y, x);
8161f28255Scgd # endif
8261f28255Scgd 			    if (win->_y[y][x] != c) {
8361f28255Scgd 				    newx = x + win->_ch_off;
8461f28255Scgd 				    if (win->_firstch[y] == _NOCHANGE) {
8561f28255Scgd 					    win->_firstch[y] =
8661f28255Scgd 							    win->_lastch[y] = newx;
8761f28255Scgd 				    } else if (newx < win->_firstch[y])
8861f28255Scgd 					    win->_firstch[y] = newx;
8961f28255Scgd 				    else if (newx > win->_lastch[y])
9061f28255Scgd 					    win->_lastch[y] = newx;
9161f28255Scgd # ifdef FULLDEBUG
9261f28255Scgd 				    fprintf(outf, "ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
9361f28255Scgd 					    win->_firstch[y], win->_lastch[y],
9461f28255Scgd 					    win->_firstch[y] - win->_ch_off,
9561f28255Scgd 					    win->_lastch[y] - win->_ch_off);
9661f28255Scgd # endif
9761f28255Scgd 			    }
9861f28255Scgd 		    }
9961f28255Scgd 		    win->_y[y][x++] = c;
10061f28255Scgd 		    if (x >= win->_maxx) {
10161f28255Scgd 			    x = 0;
10261f28255Scgd     newline:
10361f28255Scgd 			    if (++y >= win->_maxy)
10461f28255Scgd 				    if (win->_scroll) {
10561f28255Scgd 					    SYNCH_OUT();
10661f28255Scgd 					    scroll(win);
10761f28255Scgd 					    SYNCH_IN();
10861f28255Scgd 					    --y;
10961f28255Scgd 				    }
11061f28255Scgd 				    else
11161f28255Scgd 					    return ERR;
11261f28255Scgd 		    }
11361f28255Scgd # ifdef FULLDEBUG
11461f28255Scgd 		    fprintf(outf, "ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n", y, x, win->_firstch[y], win->_lastch[y]);
11561f28255Scgd # endif
11661f28255Scgd 		    break;
11761f28255Scgd 	      case '\n':
11861f28255Scgd 		    SYNCH_OUT();
11961f28255Scgd 		    wclrtoeol(win);
12061f28255Scgd 		    SYNCH_IN();
12161f28255Scgd 		    if (!NONL)
12261f28255Scgd 			    x = 0;
12361f28255Scgd 		    goto newline;
12461f28255Scgd 	      case '\r':
12561f28255Scgd 		    x = 0;
12661f28255Scgd 		    break;
12761f28255Scgd 	      case '\b':
12861f28255Scgd 		    if (--x < 0)
12961f28255Scgd 			    x = 0;
13061f28255Scgd 		    break;
13161f28255Scgd 	    }
13261f28255Scgd     }
13361f28255Scgd     SYNCH_OUT();
13461f28255Scgd     return OK;
13561f28255Scgd }
136