xref: /csrg-svn/lib/libcurses/addbytes.c (revision 34677)
131681Sminshall /*
231681Sminshall  * Copyright (c) 1987 Regents of the University of California.
3*34677Sbostic  * All rights reserved.
4*34677Sbostic  *
5*34677Sbostic  * Redistribution and use in source and binary forms are permitted
6*34677Sbostic  * provided that this notice is preserved and that due credit is given
7*34677Sbostic  * to the University of California at Berkeley. The name of the University
8*34677Sbostic  * may not be used to endorse or promote products derived from this
9*34677Sbostic  * software without specific prior written permission. This software
10*34677Sbostic  * is provided ``as is'' without express or implied warranty.
1131681Sminshall  */
1231681Sminshall 
1331681Sminshall #ifndef lint
14*34677Sbostic static char sccsid[] = "@(#)addbytes.c	5.2 (Berkeley) 06/08/88";
15*34677Sbostic #endif /* not lint */
1631681Sminshall 
1731681Sminshall # include	"curses.ext"
1831681Sminshall 
1931681Sminshall /*
2031681Sminshall  *	This routine adds the character to the current position
2131681Sminshall  *
2231681Sminshall  */
2331681Sminshall waddbytes(win, bytes, count)
2431681Sminshall reg WINDOW	*win;
2531681Sminshall reg char	*bytes;
2631681Sminshall reg int		count;
2731681Sminshall {
2831681Sminshall #define	SYNCH_OUT()	{win->_cury = y; win->_curx = x;}
2931681Sminshall #define	SYNCH_IN()	{y = win->_cury; x = win->_curx;}
3031681Sminshall 	reg int		x, y;
3131681Sminshall 	reg int		newx;
3231681Sminshall 
3331681Sminshall 	SYNCH_IN();
3431681Sminshall # ifdef FULLDEBUG
3531681Sminshall 	fprintf(outf, "ADDBYTES('%c') at (%d, %d)\n", c, y, x);
3631681Sminshall # endif
3731681Sminshall 	while (count--) {
3831681Sminshall 	    register int c;
3931681Sminshall 	    static char blanks[] = "        ";
4031681Sminshall 
4131681Sminshall 	    c = *bytes++;
4231681Sminshall 	    switch (c) {
4331681Sminshall 	      case '\t':
4431681Sminshall 		    SYNCH_IN();
4531681Sminshall 		    if (waddbytes(win, blanks, 8-(x%8)) == ERR) {
4631681Sminshall 			return ERR;
4731681Sminshall 		    }
4831681Sminshall 		    SYNCH_OUT();
4931681Sminshall 		    break;
5031681Sminshall 
5131681Sminshall 	      default:
5231681Sminshall # ifdef FULLDEBUG
5331681Sminshall 		    fprintf(outf, "ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n", y, x, win->_firstch[y], win->_lastch[y]);
5431681Sminshall # endif
5531681Sminshall 		    if (win->_flags & _STANDOUT)
5631681Sminshall 			    c |= _STANDOUT;
5731681Sminshall 		    {
5831681Sminshall # ifdef	FULLDEBUG
5931681Sminshall 			    fprintf(outf, "ADDBYTES(%0.2o, %d, %d)\n", win, y, x);
6031681Sminshall # endif
6131681Sminshall 			    if (win->_y[y][x] != c) {
6231681Sminshall 				    newx = x + win->_ch_off;
6331681Sminshall 				    if (win->_firstch[y] == _NOCHANGE) {
6431681Sminshall 					    win->_firstch[y] =
6531681Sminshall 							    win->_lastch[y] = newx;
6631681Sminshall 				    } else if (newx < win->_firstch[y])
6731681Sminshall 					    win->_firstch[y] = newx;
6831681Sminshall 				    else if (newx > win->_lastch[y])
6931681Sminshall 					    win->_lastch[y] = newx;
7031681Sminshall # ifdef FULLDEBUG
7131681Sminshall 				    fprintf(outf, "ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
7231681Sminshall 					    win->_firstch[y], win->_lastch[y],
7331681Sminshall 					    win->_firstch[y] - win->_ch_off,
7431681Sminshall 					    win->_lastch[y] - win->_ch_off);
7531681Sminshall # endif
7631681Sminshall 			    }
7731681Sminshall 		    }
7831681Sminshall 		    win->_y[y][x++] = c;
7931681Sminshall 		    if (x >= win->_maxx) {
8031681Sminshall 			    x = 0;
8131681Sminshall     newline:
8231681Sminshall 			    if (++y >= win->_maxy)
8331681Sminshall 				    if (win->_scroll) {
8431681Sminshall 					    SYNCH_OUT();
8531681Sminshall 					    scroll(win);
8631681Sminshall 					    SYNCH_IN();
8731681Sminshall 					    --y;
8831681Sminshall 				    }
8931681Sminshall 				    else
9031681Sminshall 					    return ERR;
9131681Sminshall 		    }
9231681Sminshall # ifdef FULLDEBUG
9331681Sminshall 		    fprintf(outf, "ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n", y, x, win->_firstch[y], win->_lastch[y]);
9431681Sminshall # endif
9531681Sminshall 		    break;
9631681Sminshall 	      case '\n':
9731681Sminshall 		    SYNCH_OUT();
9831681Sminshall 		    wclrtoeol(win);
9931681Sminshall 		    SYNCH_IN();
10031681Sminshall 		    if (!NONL)
10131681Sminshall 			    x = 0;
10231681Sminshall 		    goto newline;
10331681Sminshall 	      case '\r':
10431681Sminshall 		    x = 0;
10531681Sminshall 		    break;
10631681Sminshall 	      case '\b':
10731681Sminshall 		    if (--x < 0)
10831681Sminshall 			    x = 0;
10931681Sminshall 		    break;
11031681Sminshall 	    }
11131681Sminshall     }
11231681Sminshall     SYNCH_OUT();
11331681Sminshall     return OK;
11431681Sminshall }
115