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