addbytes.c revision 1.30.6.4 1 /* $NetBSD: addbytes.c,v 1.30.6.4 2007/01/22 10:43:28 blymn 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)addbytes.c 8.4 (Berkeley) 5/4/94";
36 #else
37 __RCSID("$NetBSD: addbytes.c,v 1.30.6.4 2007/01/22 10:43:28 blymn Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <stdlib.h>
42 #include <assert.h>
43 #include "curses.h"
44 #include "curses_private.h"
45 #ifdef DEBUG
46 #include <assert.h>
47 #endif
48
49 #define SYNCH_IN {y = win->cury; x = win->curx;}
50 #define SYNCH_OUT {win->cury = y; win->curx = x;}
51
52 #ifndef _CURSES_USE_MACROS
53
54 /*
55 * addbytes --
56 * Add the character to the current position in stdscr.
57 */
58 int
59 addbytes(const char *bytes, int count)
60 {
61 return __waddbytes(stdscr, bytes, count, 0);
62 }
63
64 /*
65 * waddbytes --
66 * Add the character to the current position in the given window.
67 */
68 int
69 waddbytes(WINDOW *win, const char *bytes, int count)
70 {
71 return __waddbytes(win, bytes, count, 0);
72 }
73
74 /*
75 * mvaddbytes --
76 * Add the characters to stdscr at the location given.
77 */
78 int
79 mvaddbytes(int y, int x, const char *bytes, int count)
80 {
81 return mvwaddbytes(stdscr, y, x, bytes, count);
82 }
83
84 /*
85 * mvwaddbytes --
86 * Add the characters to the given window at the location given.
87 */
88 int
89 mvwaddbytes(WINDOW *win, int y, int x, const char *bytes, int count)
90 {
91 if (wmove(win, y, x) == ERR)
92 return ERR;
93
94 return __waddbytes(win, bytes, count, 0);
95 }
96
97 #endif
98
99 /*
100 * waddbytes --
101 * Add the character to the current position in the given window.
102 */
103 int
104 __waddbytes(WINDOW *win, const char *bytes, int count, attr_t attr)
105 {
106 static char blanks[] = " ";
107 int c, newx, x, y;
108 attr_t attributes;
109 __LINE *lp;
110 #ifdef DEBUG
111 int i;
112
113 for (i = 0; i < win->maxy; i++) {
114 assert(win->lines[i]->sentinel == SENTINEL_VALUE);
115 }
116 #endif
117
118 SYNCH_IN;
119 lp = win->lines[y];
120
121 while (count--) {
122 c = *bytes++;
123 #ifdef DEBUG
124 __CTRACE(__CTRACE_INPUT, "ADDBYTES('%c', %x) at (%d, %d)\n",
125 c, attr, y, x);
126 #endif
127 switch (c) {
128 case '\t':
129 SYNCH_OUT;
130 if (waddbytes(win, blanks, 8 - (x % 8)) == ERR)
131 return (ERR);
132 SYNCH_IN;
133 break;
134
135 default:
136 #ifdef DEBUG
137 __CTRACE(__CTRACE_INPUT, "ADDBYTES(%p, %d, %d)\n",
138 win, y, x);
139 #endif
140
141 if (lp->flags & __ISPASTEOL) {
142 newline:
143 x = 0;
144 lp->flags &= ~__ISPASTEOL;
145 if (y == win->scr_b) {
146 #ifdef DEBUG
147 __CTRACE(__CTRACE_INPUT,
148 "ADDBYTES - on bottom "
149 "of scrolling region\n");
150 #endif
151 if (!(win->flags & __SCROLLOK))
152 return ERR;
153 SYNCH_OUT;
154 scroll(win);
155 SYNCH_IN;
156 } else {
157 y++;
158 }
159 lp = win->lines[y];
160 if (c == '\n')
161 break;
162 }
163
164 attributes = (win->wattr | attr) &
165 (__ATTRIBUTES & ~__COLOR);
166 if (attr & __COLOR)
167 attributes |= attr & __COLOR;
168 else if (win->wattr & __COLOR)
169 attributes |= win->wattr & __COLOR;
170 #ifdef DEBUG
171 __CTRACE(__CTRACE_INPUT,
172 "ADDBYTES: 1: y = %d, x = %d, firstch = %d, "
173 "lastch = %d\n",
174 y, x, *win->lines[y]->firstchp,
175 *win->lines[y]->lastchp);
176 #endif
177 /*
178 * Always update the change pointers. Otherwise,
179 * we could end up not displaying 'blank' characters
180 * when overlapping windows are displayed.
181 */
182 newx = x + win->ch_off;
183 lp->flags |= __ISDIRTY;
184 /*
185 * firstchp/lastchp are shared between
186 * parent window and sub-window.
187 */
188 if (newx < *lp->firstchp)
189 *lp->firstchp = newx;
190 if (newx > *lp->lastchp)
191 *lp->lastchp = newx;
192 #ifdef DEBUG
193 __CTRACE(__CTRACE_INPUT,
194 "ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
195 *lp->firstchp, *lp->lastchp,
196 *lp->firstchp - win->ch_off,
197 *lp->lastchp - win->ch_off);
198 #endif
199 if (win->bch != ' ' && c == ' ')
200 lp->line[x].ch = win->bch;
201 else
202 lp->line[x].ch = c;
203
204 #ifdef HAVE_WCHAR
205 if (_cursesi_copy_nsp(win->bnsp, &lp->line[x]) == ERR)
206 return ERR;
207 SET_WCOL(lp->line[x], 1);
208 #endif /* HAVE_WCHAR */
209
210 if (attributes & __COLOR)
211 lp->line[x].attr =
212 attributes | (win->battr & ~__COLOR);
213 else
214 lp->line[x].attr = attributes | win->battr;
215 #ifdef HAVE_WCHAR
216 SET_WCOL(lp->line[x], 1);
217 #endif /* HAVE_WCHAR */
218
219 if (x == win->maxx - 1)
220 lp->flags |= __ISPASTEOL;
221 else
222 x++;
223 #ifdef DEBUG
224 __CTRACE(__CTRACE_INPUT,
225 "ADDBYTES: 2: y = %d, x = %d, firstch = %d, "
226 "lastch = %d\n",
227 y, x, *win->lines[y]->firstchp,
228 *win->lines[y]->lastchp);
229 #endif
230 break;
231 case '\n':
232 SYNCH_OUT;
233 wclrtoeol(win);
234 SYNCH_IN;
235 goto newline;
236 case '\r':
237 x = 0;
238 continue;
239 case '\b':
240 if (--x < 0)
241 x = 0;
242 break;
243 }
244 }
245 SYNCH_OUT;
246
247 #ifdef DEBUG
248 for (i = 0; i < win->maxy; i++) {
249 assert(win->lines[i]->sentinel == SENTINEL_VALUE);
250 }
251 #endif
252
253 return (OK);
254 }
255