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