xref: /original-bsd/lib/libcurses/addbytes.c (revision 1b61d772)
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.16 (Berkeley) 01/11/93";
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 int
19 waddbytes(win, bytes, count)
20 	WINDOW *win;
21 	char *bytes;
22 	int count;
23 {
24 	__waddbytes(win, bytes, count, 0);
25 }
26 
27 /*
28  * waddbytes --
29  *	Add the character to the current position in the given window.
30  */
31 int
32 __waddbytes(win, bytes, count, so)
33 	register WINDOW *win;
34 	register char *bytes;
35 	register int count;
36 	int so;
37 {
38 	static char blanks[] = "        ";
39 	register int c, newx, x, y;
40 	char stand;
41 	__LINE *lp;
42 
43 	SYNCH_IN;
44 
45 #ifdef DEBUG
46 	__TRACE("ADDBYTES('%c') at (%d, %d)\n", c, y, x);
47 #endif
48 	while (count--) {
49 		c = *bytes++;
50 		switch (c) {
51 		case '\t':
52 			SYNCH_OUT;
53 			if (waddbytes(win, blanks, 8 - (x % 8)) == ERR)
54 				return (ERR);
55 			SYNCH_IN;
56 			break;
57 
58 		default:
59 #ifdef DEBUG
60 	__TRACE("ADDBYTES(%0.2o, %d, %d)\n", win, y, x);
61 #endif
62 
63 			lp = win->lines[y];
64 			if (lp->flags & __ISPASTEOL) {
65 				lp->flags &= ~__ISPASTEOL;
66 newline:			if (y == win->maxy - 1) {
67 					if (win->flags & __SCROLLOK) {
68 						SYNCH_OUT;
69 						scroll(win);
70 						SYNCH_IN;
71 						lp = win->lines[y];
72 					        x = 0;
73 					}
74 				} else {
75 					y++;
76 					lp = win->lines[y];
77 					x = 0;
78 				}
79 			}
80 
81 			stand = '\0';
82 			if (win->flags & __WSTANDOUT || so)
83 				stand |= __STANDOUT;
84 #ifdef DEBUG
85 	__TRACE("ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n",
86 	    y, x, *win->lines[y]->firstchp, *win->lines[y]->lastchp);
87 #endif
88 			if (lp->line[x].ch != c ||
89 			    !(lp->line[x].attr & stand)) {
90 				newx = x + win->ch_off;
91 				if (!(lp->flags & __ISDIRTY)) {
92 					lp->flags |= __ISDIRTY;
93 					*lp->firstchp = *lp->lastchp = newx;
94 				}
95 				else if (newx < *lp->firstchp)
96 					*lp->firstchp = newx;
97 				else if (newx > *lp->lastchp)
98 					*lp->lastchp = newx;
99 #ifdef DEBUG
100 	__TRACE("ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
101 	    *lp->firstchp, *lp->lastchp,
102 	    *lp->firstchp - win->ch_off,
103 	    *lp->lastchp - win->ch_off);
104 #endif
105 			}
106 			lp->line[x].ch = c;
107 			if (stand)
108 				lp->line[x].attr |= __STANDOUT;
109 			else
110 				lp->line[x].attr &= ~__STANDOUT;
111 			if (x == win->maxx - 1)
112 				lp->flags |= __ISPASTEOL;
113 			else
114 				x++;
115 #ifdef DEBUG
116 	__TRACE("ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n",
117 	    y, x, *win->lines[y]->firstchp, *win->lines[y]->lastchp);
118 #endif
119 			break;
120 		case '\n':
121 			SYNCH_OUT;
122 			wclrtoeol(win);
123 			SYNCH_IN;
124 			if (origtermio.c_oflag & ONLCR)
125 				x = 0;
126 			goto newline;
127 		case '\r':
128 			x = 0;
129 			break;
130 		case '\b':
131 			if (--x < 0)
132 				x = 0;
133 			break;
134 		}
135 	}
136 	SYNCH_OUT;
137 	return (OK);
138 }
139