xref: /csrg-svn/lib/libcurses/addbytes.c (revision 55944)
131681Sminshall /*
231681Sminshall  * Copyright (c) 1987 Regents of the University of California.
334677Sbostic  * All rights reserved.
434677Sbostic  *
542651Sbostic  * %sccs.include.redist.c%
631681Sminshall  */
731681Sminshall 
831681Sminshall #ifndef lint
9*55944Sbostic static char sccsid[] = "@(#)addbytes.c	5.5 (Berkeley) 08/23/92";
10*55944Sbostic #endif	/* not lint */
1131681Sminshall 
12*55944Sbostic #include <curses.h>
1331681Sminshall 
14*55944Sbostic #define	SYNCH_IN	{y = win->_cury; x = win->_curx;}
15*55944Sbostic #define	SYNCH_OUT	{win->_cury = y; win->_curx = x;}
16*55944Sbostic 
1731681Sminshall /*
18*55944Sbostic  * waddbytes --
19*55944Sbostic  *	Add the character to the current position in the given window.
2031681Sminshall  */
2131681Sminshall waddbytes(win, bytes, count)
22*55944Sbostic 	register WINDOW *win;
23*55944Sbostic 	register char *bytes;
24*55944Sbostic 	register int count;
2531681Sminshall {
26*55944Sbostic 	static char blanks[] = "        ";
27*55944Sbostic 	register int c, newx, x, y;
2831681Sminshall 
29*55944Sbostic 	SYNCH_IN;
30*55944Sbostic #ifdef DEBUG
31*55944Sbostic 	__TRACE("ADDBYTES('%c') at (%d, %d)\n", c, y, x);
32*55944Sbostic #endif
3331681Sminshall 	while (count--) {
34*55944Sbostic 		c = *bytes++;
35*55944Sbostic 		switch (c) {
36*55944Sbostic 		case '\t':
37*55944Sbostic 			SYNCH_IN;
38*55944Sbostic 			if (waddbytes(win, blanks, 8 - (x % 8)) == ERR)
39*55944Sbostic 				return (ERR);
40*55944Sbostic 			SYNCH_OUT;
41*55944Sbostic 			break;
4231681Sminshall 
43*55944Sbostic 		default:
44*55944Sbostic #ifdef DEBUG
45*55944Sbostic 	__TRACE("ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n",
46*55944Sbostic 	    y, x, win->_firstch[y], win->_lastch[y]);
47*55944Sbostic #endif
48*55944Sbostic 			if (win->_flags & _STANDOUT)
49*55944Sbostic 				c |= _STANDOUT;
50*55944Sbostic #ifdef DEBUG
51*55944Sbostic 	__TRACE("ADDBYTES(%0.2o, %d, %d)\n", win, y, x);
52*55944Sbostic #endif
53*55944Sbostic 			if (win->_y[y][x] != c) {
54*55944Sbostic 				newx = x + win->_ch_off;
55*55944Sbostic 				if (win->_firstch[y] == _NOCHANGE)
56*55944Sbostic 					win->_firstch[y] =
5731681Sminshall 					    win->_lastch[y] = newx;
58*55944Sbostic 				else if (newx < win->_firstch[y])
59*55944Sbostic 					win->_firstch[y] = newx;
60*55944Sbostic 				else if (newx > win->_lastch[y])
61*55944Sbostic 					win->_lastch[y] = newx;
62*55944Sbostic #ifdef __TRACE
63*55944Sbostic 	__TRACE("ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
64*55944Sbostic 	    win->_firstch[y], win->_lastch[y], win->_firstch[y] - win->_ch_off,
65*55944Sbostic 	    win->_lastch[y] - win->_ch_off);
66*55944Sbostic #endif
67*55944Sbostic 			}
68*55944Sbostic 			win->_y[y][x++] = c;
69*55944Sbostic 			if (x >= win->_maxx) {
70*55944Sbostic 				x = 0;
71*55944Sbostic newline:			if (++y >= win->_maxy)
72*55944Sbostic 					if (win->_scroll) {
73*55944Sbostic 						SYNCH_OUT;
74*55944Sbostic 						scroll(win);
75*55944Sbostic 						SYNCH_IN;
76*55944Sbostic 						--y;
77*55944Sbostic 					} else
78*55944Sbostic 						return (ERR);
79*55944Sbostic 			}
80*55944Sbostic #ifdef DEBUG
81*55944Sbostic 	__TRACE("ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n",
82*55944Sbostic 	    y, x, win->_firstch[y], win->_lastch[y]);
83*55944Sbostic #endif
84*55944Sbostic 			break;
85*55944Sbostic 		case '\n':
86*55944Sbostic 			SYNCH_OUT;
87*55944Sbostic 			wclrtoeol(win);
88*55944Sbostic 			SYNCH_IN;
89*55944Sbostic 			if (!NONL)
90*55944Sbostic 				x = 0;
91*55944Sbostic 			goto newline;
92*55944Sbostic 		case '\r':
93*55944Sbostic 			x = 0;
94*55944Sbostic 			break;
95*55944Sbostic 		case '\b':
96*55944Sbostic 			if (--x < 0)
97*55944Sbostic 				x = 0;
98*55944Sbostic 			break;
99*55944Sbostic 		}
100*55944Sbostic 	}
101*55944Sbostic 	SYNCH_OUT;
102*55944Sbostic 	return (OK);
10331681Sminshall }
104