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