xref: /netbsd-src/lib/libcurses/addbytes.c (revision e4d7c2e329d54c97e0c0bd3016bbe74f550c3d5e)
1 /*	$NetBSD: addbytes.c,v 1.14 1999/06/28 13:32:43 simonb Exp $	*/
2 
3 /*
4  * Copyright (c) 1987, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)addbytes.c	8.4 (Berkeley) 5/4/94";
40 #else
41 __RCSID("$NetBSD: addbytes.c,v 1.14 1999/06/28 13:32:43 simonb Exp $");
42 #endif
43 #endif				/* not lint */
44 
45 #include "curses.h"
46 
47 #define	SYNCH_IN	{y = win->cury; x = win->curx;}
48 #define	SYNCH_OUT	{win->cury = y; win->curx = x;}
49 
50 /*
51  * waddbytes --
52  *	Add the character to the current position in the given window.
53  */
54 int
55 __waddbytes(win, bytes, count, attr)
56 	WINDOW	*win;
57 	const char	*bytes;
58 	int	count;
59 	int	attr;
60 {
61 	static char	blanks[] = "        ";
62 	int	c, newx, x, y;
63 	char	attributes;
64 	__LINE	*lp;
65 
66 	SYNCH_IN;
67 
68 	while (count--) {
69 		c = *bytes++;
70 #ifdef DEBUG
71 		__CTRACE("ADDBYTES('%c') at (%d, %d)\n", c, y, x);
72 #endif
73 		switch (c) {
74 		case '\t':
75 			SYNCH_OUT;
76 			if (waddbytes(win, blanks, 8 - (x % 8)) == ERR)
77 				return (ERR);
78 			SYNCH_IN;
79 			break;
80 
81 		default:
82 #ifdef DEBUG
83 			__CTRACE("ADDBYTES(%0.2o, %d, %d)\n", win, y, x);
84 #endif
85 
86 			lp = win->lines[y];
87 			if (lp->flags & __ISPASTEOL) {
88 				lp->flags &= ~__ISPASTEOL;
89 		newline:	if (y == win->maxy - 1) {
90 					if (win->flags & __SCROLLOK) {
91 						SYNCH_OUT;
92 						scroll(win);
93 						SYNCH_IN;
94 						lp = win->lines[y];
95 						x = 0;
96 					} else
97 						return (ERR);
98 				} else {
99 					y++;
100 					lp = win->lines[y];
101 					x = 0;
102 				}
103 				if (c == '\n')
104 					break;
105 			}
106 
107 			attributes = '\0';
108 			if (win->flags & __WSTANDOUT || attr & __STANDOUT)
109 				attributes |= __STANDOUT;
110 			if (win->flags & __WUNDERSCORE || attr & __UNDERSCORE)
111 				attributes |= __UNDERSCORE;
112 			if (win->flags & __WREVERSE || attr & __REVERSE)
113 				attributes |= __REVERSE;
114 			if (win->flags & __WBLINK || attr & __BLINK)
115 				attributes |= __BLINK;
116 			if (win->flags & __WDIM || attr & __DIM)
117 				attributes |= __DIM;
118 			if (win->flags & __WBOLD || attr & __BOLD)
119 				attributes |= __BOLD;
120 			if (win->flags & __WBLANK || attr & __BLANK)
121 				attributes |= __BLANK;
122 			if (win->flags & __WPROTECT || attr & __PROTECT)
123 				attributes |= __PROTECT;
124 #ifdef DEBUG
125 			__CTRACE("ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n",
126 			    y, x, *win->lines[y]->firstchp,
127 			    *win->lines[y]->lastchp);
128 #endif
129 			if (lp->line[x].ch != c ||
130 			    !(lp->line[x].attr & attributes)) {
131 				newx = x + win->ch_off;
132 				if (!(lp->flags & __ISDIRTY)) {
133 					lp->flags |= __ISDIRTY;
134 					*lp->firstchp = *lp->lastchp = newx;
135 				} else
136 					if (newx < *lp->firstchp)
137 						*lp->firstchp = newx;
138 					else
139 						if (newx > *lp->lastchp)
140 							*lp->lastchp = newx;
141 #ifdef DEBUG
142 				__CTRACE("ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
143 				    *lp->firstchp, *lp->lastchp,
144 				    *lp->firstchp - win->ch_off,
145 				    *lp->lastchp - win->ch_off);
146 #endif
147 			}
148 			lp->line[x].ch = c;
149 			if (attributes & __STANDOUT)
150 				lp->line[x].attr |= __STANDOUT;
151 			else
152 				lp->line[x].attr &= ~__STANDOUT;
153 			if (attributes & __UNDERSCORE)
154 				lp->line[x].attr |= __UNDERSCORE;
155 			else
156 				lp->line[x].attr &= ~__UNDERSCORE;
157 			if (attributes & __REVERSE)
158 				lp->line[x].attr |= __REVERSE;
159 			else
160 				lp->line[x].attr &= ~__REVERSE;
161 			if (attributes & __BLINK)
162 				lp->line[x].attr |= __BLINK;
163 			else
164 				lp->line[x].attr &= ~__BLINK;
165 			if (attributes & __DIM)
166 				lp->line[x].attr |= __DIM;
167 			else
168 				lp->line[x].attr &= ~__DIM;
169 			if (attributes & __BOLD)
170 				lp->line[x].attr |= __BOLD;
171 			else
172 				lp->line[x].attr &= ~__BOLD;
173 			if (attributes & __BLANK)
174 				lp->line[x].attr |= __BLANK;
175 			else
176 				lp->line[x].attr &= ~__BLANK;
177 			if (attributes & __PROTECT)
178 				lp->line[x].attr |= __PROTECT;
179 			else
180 				lp->line[x].attr &= ~__PROTECT;
181 			if (x == win->maxx - 1)
182 				lp->flags |= __ISPASTEOL;
183 			else
184 				x++;
185 #ifdef DEBUG
186 			__CTRACE("ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n",
187 			    y, x, *win->lines[y]->firstchp,
188 			    *win->lines[y]->lastchp);
189 #endif
190 			break;
191 		case '\n':
192 			SYNCH_OUT;
193 			wclrtoeol(win);
194 			SYNCH_IN;
195 			if (!NONL)
196 				x = 0;
197 			goto newline;
198 		case '\r':
199 			x = 0;
200 			break;
201 		case '\b':
202 			if (--x < 0)
203 				x = 0;
204 			break;
205 		}
206 	}
207 	SYNCH_OUT;
208 	return (OK);
209 }
210