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