addbytes.c revision 1.39.8.1 1 1.39.8.1 tls /* $NetBSD: addbytes.c,v 1.39.8.1 2014/08/20 00:02:17 tls 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.28 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.11 mikel #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.11 mikel #if 0
35 1.10 cgd static char sccsid[] = "@(#)addbytes.c 8.4 (Berkeley) 5/4/94";
36 1.11 mikel #else
37 1.39.8.1 tls __RCSID("$NetBSD: addbytes.c,v 1.39.8.1 2014/08/20 00:02:17 tls Exp $");
38 1.11 mikel #endif
39 1.13 mrg #endif /* not lint */
40 1.1 cgd
41 1.32 blymn #include <stdlib.h>
42 1.37 tnozaki #include <string.h>
43 1.10 cgd #include "curses.h"
44 1.15 blymn #include "curses_private.h"
45 1.23 blymn #ifdef DEBUG
46 1.23 blymn #include <assert.h>
47 1.23 blymn #endif
48 1.1 cgd
49 1.7 cgd #define SYNCH_IN {y = win->cury; x = win->curx;}
50 1.7 cgd #define SYNCH_OUT {win->cury = y; win->curx = x;}
51 1.32 blymn #define PSYNCH_IN {*y = win->cury; *x = win->curx;}
52 1.32 blymn #define PSYNCH_OUT {win->cury = *y; win->curx = *x;}
53 1.3 alm
54 1.17 blymn #ifndef _CURSES_USE_MACROS
55 1.17 blymn
56 1.17 blymn /*
57 1.17 blymn * addbytes --
58 1.17 blymn * Add the character to the current position in stdscr.
59 1.17 blymn */
60 1.17 blymn int
61 1.17 blymn addbytes(const char *bytes, int count)
62 1.17 blymn {
63 1.39.8.1 tls return _cursesi_waddbytes(stdscr, bytes, count, 0, 1);
64 1.17 blymn }
65 1.17 blymn
66 1.17 blymn /*
67 1.17 blymn * waddbytes --
68 1.17 blymn * Add the character to the current position in the given window.
69 1.17 blymn */
70 1.17 blymn int
71 1.17 blymn waddbytes(WINDOW *win, const char *bytes, int count)
72 1.17 blymn {
73 1.39.8.1 tls return _cursesi_waddbytes(win, bytes, count, 0, 1);
74 1.17 blymn }
75 1.17 blymn
76 1.17 blymn /*
77 1.17 blymn * mvaddbytes --
78 1.17 blymn * Add the characters to stdscr at the location given.
79 1.17 blymn */
80 1.17 blymn int
81 1.17 blymn mvaddbytes(int y, int x, const char *bytes, int count)
82 1.17 blymn {
83 1.17 blymn return mvwaddbytes(stdscr, y, x, bytes, count);
84 1.17 blymn }
85 1.17 blymn
86 1.17 blymn /*
87 1.17 blymn * mvwaddbytes --
88 1.17 blymn * Add the characters to the given window at the location given.
89 1.17 blymn */
90 1.17 blymn int
91 1.17 blymn mvwaddbytes(WINDOW *win, int y, int x, const char *bytes, int count)
92 1.17 blymn {
93 1.17 blymn if (wmove(win, y, x) == ERR)
94 1.17 blymn return ERR;
95 1.17 blymn
96 1.39.8.1 tls return _cursesi_waddbytes(win, bytes, count, 0, 1);
97 1.17 blymn }
98 1.17 blymn
99 1.17 blymn #endif
100 1.17 blymn
101 1.39.8.1 tls int
102 1.39.8.1 tls __waddbytes(WINDOW *win, const char *bytes, int count, attr_t attr)
103 1.39.8.1 tls {
104 1.39.8.1 tls return _cursesi_waddbytes(win, bytes, count, attr, 1);
105 1.39.8.1 tls }
106 1.39.8.1 tls
107 1.1 cgd /*
108 1.39.8.1 tls * _cursesi_waddbytes --
109 1.5 mycroft * Add the character to the current position in the given window.
110 1.39.8.1 tls * if char_interp is non-zero then character interpretation is done on
111 1.39.8.1 tls * the byte (i.e. \n to newline, \r to carriage return, \b to backspace
112 1.39.8.1 tls * and so on).
113 1.1 cgd */
114 1.5 mycroft int
115 1.39.8.1 tls _cursesi_waddbytes(WINDOW *win, const char *bytes, int count, attr_t attr,
116 1.39.8.1 tls int char_interp)
117 1.1 cgd {
118 1.39.8.1 tls int x, y, err;
119 1.15 blymn __LINE *lp;
120 1.32 blymn #ifdef HAVE_WCHAR
121 1.32 blymn int n;
122 1.32 blymn cchar_t cc;
123 1.32 blymn wchar_t wc;
124 1.34 tnozaki mbstate_t st;
125 1.32 blymn #else
126 1.32 blymn int c;
127 1.32 blymn #endif
128 1.23 blymn #ifdef DEBUG
129 1.23 blymn int i;
130 1.5 mycroft
131 1.23 blymn for (i = 0; i < win->maxy; i++) {
132 1.35 roy assert(win->alines[i]->sentinel == SENTINEL_VALUE);
133 1.23 blymn }
134 1.32 blymn
135 1.32 blymn __CTRACE(__CTRACE_INPUT, "ADDBYTES: add %d bytes\n", count);
136 1.23 blymn #endif
137 1.32 blymn
138 1.32 blymn err = OK;
139 1.5 mycroft SYNCH_IN;
140 1.35 roy lp = win->alines[y];
141 1.1 cgd
142 1.34 tnozaki #ifdef HAVE_WCHAR
143 1.37 tnozaki (void)memset(&st, 0, sizeof(st));
144 1.34 tnozaki #endif
145 1.32 blymn while (count > 0) {
146 1.32 blymn #ifndef HAVE_WCHAR
147 1.11 mikel c = *bytes++;
148 1.5 mycroft #ifdef DEBUG
149 1.31 jdc __CTRACE(__CTRACE_INPUT, "ADDBYTES('%c', %x) at (%d, %d)\n",
150 1.31 jdc c, attr, y, x);
151 1.5 mycroft #endif
152 1.39.8.1 tls err = _cursesi_addbyte(win, &lp, &y, &x, c, attr, char_interp);
153 1.32 blymn count--;
154 1.32 blymn #else
155 1.32 blymn /*
156 1.38 wiz * For wide-character support only, try and convert the
157 1.32 blymn * given string into a wide character - we do this because
158 1.32 blymn * this is how ncurses behaves (not that I think this is
159 1.32 blymn * actually the correct thing to do but if we don't do it
160 1.32 blymn * a lot of things that rely on this behaviour will break
161 1.32 blymn * and we will be blamed). If the conversion succeeds
162 1.32 blymn * then we eat the n characters used to make the wide char
163 1.32 blymn * from the string.
164 1.32 blymn */
165 1.34 tnozaki n = (int)mbrtowc(&wc, bytes, (size_t)count, &st);
166 1.34 tnozaki if (n < 0) {
167 1.34 tnozaki /* not a valid conversion just eat a char */
168 1.32 blymn wc = *bytes;
169 1.32 blymn n = 1;
170 1.39 joerg (void)memset(&st, 0, sizeof(st));
171 1.34 tnozaki } else if (wc == 0) {
172 1.34 tnozaki break;
173 1.32 blymn }
174 1.32 blymn #ifdef DEBUG
175 1.32 blymn __CTRACE(__CTRACE_INPUT,
176 1.32 blymn "ADDBYTES WIDE(0x%x [%s], %x) at (%d, %d), ate %d bytes\n",
177 1.32 blymn (unsigned) wc, unctrl((unsigned) wc), attr, y, x, n);
178 1.32 blymn #endif
179 1.32 blymn cc.vals[0] = wc;
180 1.32 blymn cc.elements = 1;
181 1.32 blymn cc.attributes = attr;
182 1.39.8.1 tls err = _cursesi_addwchar(win, &lp, &y, &x, &cc, char_interp);
183 1.32 blymn bytes += n;
184 1.32 blymn count -= n;
185 1.32 blymn #endif
186 1.32 blymn }
187 1.32 blymn
188 1.32 blymn SYNCH_OUT;
189 1.32 blymn
190 1.32 blymn #ifdef DEBUG
191 1.32 blymn for (i = 0; i < win->maxy; i++) {
192 1.35 roy assert(win->alines[i]->sentinel == SENTINEL_VALUE);
193 1.32 blymn }
194 1.32 blymn #endif
195 1.32 blymn
196 1.32 blymn return (err);
197 1.32 blymn }
198 1.5 mycroft
199 1.32 blymn /*
200 1.32 blymn * _cursesi_addbyte -
201 1.32 blymn * Internal function to add a byte and update the row and column
202 1.32 blymn * positions as appropriate. This function is only used in the narrow
203 1.39.8.1 tls * character version of curses. If update_cursor is non-zero then character
204 1.39.8.1 tls * interpretation.
205 1.32 blymn */
206 1.32 blymn int
207 1.32 blymn _cursesi_addbyte(WINDOW *win, __LINE **lp, int *y, int *x, int c,
208 1.39.8.1 tls attr_t attr, int char_interp)
209 1.32 blymn {
210 1.39.8.1 tls static char blank[] = " ";
211 1.39.8.1 tls int tabsize;
212 1.39.8.1 tls int newx, i;
213 1.32 blymn attr_t attributes;
214 1.32 blymn
215 1.39.8.1 tls if (char_interp) {
216 1.39.8.1 tls switch (c) {
217 1.39.8.1 tls case '\t':
218 1.39.8.1 tls tabsize = win->screen->TABSIZE;
219 1.39.8.1 tls PSYNCH_OUT;
220 1.39.8.1 tls for (i = 0; i < (tabsize - (*x % tabsize)); i++) {
221 1.39.8.1 tls if (waddbytes(win, blank, 1) == ERR)
222 1.39.8.1 tls return (ERR);
223 1.39.8.1 tls }
224 1.39.8.1 tls PSYNCH_IN;
225 1.39.8.1 tls return (OK);
226 1.39.8.1 tls
227 1.39.8.1 tls case '\n':
228 1.39.8.1 tls PSYNCH_OUT;
229 1.39.8.1 tls wclrtoeol(win);
230 1.39.8.1 tls PSYNCH_IN;
231 1.39.8.1 tls (*lp)->flags |= __ISPASTEOL;
232 1.39.8.1 tls break;
233 1.39.8.1 tls
234 1.39.8.1 tls case '\r':
235 1.39.8.1 tls *x = 0;
236 1.39.8.1 tls win->curx = *x;
237 1.39.8.1 tls return (OK);
238 1.39.8.1 tls
239 1.39.8.1 tls case '\b':
240 1.39.8.1 tls if (--(*x) < 0)
241 1.39.8.1 tls *x = 0;
242 1.39.8.1 tls win->curx = *x;
243 1.39.8.1 tls return (OK);
244 1.39.8.1 tls }
245 1.39.8.1 tls }
246 1.32 blymn
247 1.5 mycroft #ifdef DEBUG
248 1.39.8.1 tls __CTRACE(__CTRACE_INPUT, "ADDBYTES(%p, %d, %d)\n", win, *y, *x);
249 1.5 mycroft #endif
250 1.14 simonb
251 1.39.8.1 tls if (char_interp && ((*lp)->flags & __ISPASTEOL)) {
252 1.39.8.1 tls *x = 0;
253 1.39.8.1 tls (*lp)->flags &= ~__ISPASTEOL;
254 1.39.8.1 tls if (*y == win->scr_b) {
255 1.32 blymn #ifdef DEBUG
256 1.39.8.1 tls __CTRACE(__CTRACE_INPUT,
257 1.39.8.1 tls "ADDBYTES - on bottom "
258 1.39.8.1 tls "of scrolling region\n");
259 1.32 blymn #endif
260 1.39.8.1 tls if (!(win->flags & __SCROLLOK))
261 1.39.8.1 tls return ERR;
262 1.39.8.1 tls PSYNCH_OUT;
263 1.39.8.1 tls scroll(win);
264 1.39.8.1 tls PSYNCH_IN;
265 1.39.8.1 tls } else {
266 1.39.8.1 tls (*y)++;
267 1.32 blymn }
268 1.39.8.1 tls *lp = win->alines[*y];
269 1.39.8.1 tls if (c == '\n')
270 1.39.8.1 tls return (OK);
271 1.39.8.1 tls }
272 1.14 simonb
273 1.32 blymn #ifdef DEBUG
274 1.39.8.1 tls __CTRACE(__CTRACE_INPUT,
275 1.39.8.1 tls "ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n",
276 1.39.8.1 tls *y, *x, *win->alines[*y]->firstchp,
277 1.39.8.1 tls *win->alines[*y]->lastchp);
278 1.32 blymn #endif
279 1.32 blymn
280 1.39.8.1 tls attributes = (win->wattr | attr) & (__ATTRIBUTES & ~__COLOR);
281 1.39.8.1 tls if (attr & __COLOR)
282 1.39.8.1 tls attributes |= attr & __COLOR;
283 1.39.8.1 tls else if (win->wattr & __COLOR)
284 1.39.8.1 tls attributes |= win->wattr & __COLOR;
285 1.39.8.1 tls
286 1.39.8.1 tls /*
287 1.39.8.1 tls * Always update the change pointers. Otherwise,
288 1.39.8.1 tls * we could end up not displaying 'blank' characters
289 1.39.8.1 tls * when overlapping windows are displayed.
290 1.39.8.1 tls */
291 1.39.8.1 tls newx = *x + win->ch_off;
292 1.39.8.1 tls (*lp)->flags |= __ISDIRTY;
293 1.39.8.1 tls /*
294 1.39.8.1 tls * firstchp/lastchp are shared between
295 1.39.8.1 tls * parent window and sub-window.
296 1.39.8.1 tls */
297 1.39.8.1 tls if (newx < *(*lp)->firstchp)
298 1.39.8.1 tls *(*lp)->firstchp = newx;
299 1.39.8.1 tls if (newx > *(*lp)->lastchp)
300 1.39.8.1 tls *(*lp)->lastchp = newx;
301 1.39.8.1 tls #ifdef DEBUG
302 1.39.8.1 tls __CTRACE(__CTRACE_INPUT, "ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
303 1.39.8.1 tls *(*lp)->firstchp, *(*lp)->lastchp,
304 1.39.8.1 tls *(*lp)->firstchp - win->ch_off,
305 1.39.8.1 tls *(*lp)->lastchp - win->ch_off);
306 1.39.8.1 tls #endif
307 1.39.8.1 tls if (win->bch != ' ' && c == ' ')
308 1.39.8.1 tls (*lp)->line[*x].ch = win->bch;
309 1.39.8.1 tls else
310 1.39.8.1 tls (*lp)->line[*x].ch = c;
311 1.39.8.1 tls
312 1.39.8.1 tls if (attributes & __COLOR)
313 1.39.8.1 tls (*lp)->line[*x].attr =
314 1.39.8.1 tls attributes | (win->battr & ~__COLOR);
315 1.39.8.1 tls else
316 1.39.8.1 tls (*lp)->line[*x].attr = attributes | win->battr;
317 1.39.8.1 tls
318 1.39.8.1 tls if (*x == win->maxx - 1)
319 1.39.8.1 tls (*lp)->flags |= __ISPASTEOL;
320 1.39.8.1 tls else
321 1.39.8.1 tls (*x)++;
322 1.32 blymn
323 1.39.8.1 tls #ifdef DEBUG
324 1.39.8.1 tls __CTRACE(__CTRACE_INPUT,
325 1.39.8.1 tls "ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n",
326 1.39.8.1 tls *y, *x, *win->alines[*y]->firstchp,
327 1.39.8.1 tls *win->alines[*y]->lastchp);
328 1.39.8.1 tls #endif
329 1.32 blymn return (OK);
330 1.32 blymn }
331 1.32 blymn
332 1.32 blymn /*
333 1.32 blymn * _cursesi_addwchar -
334 1.32 blymn * Internal function to add a wide character and update the row
335 1.32 blymn * and column positions.
336 1.32 blymn */
337 1.32 blymn int
338 1.32 blymn _cursesi_addwchar(WINDOW *win, __LINE **lnp, int *y, int *x,
339 1.39.8.1 tls const cchar_t *wch, int char_interp)
340 1.32 blymn {
341 1.32 blymn #ifndef HAVE_WCHAR
342 1.32 blymn return (ERR);
343 1.32 blymn #else
344 1.39.8.1 tls int sx = 0, ex = 0, cw = 0, i = 0, newx = 0, tabsize;
345 1.35 roy __LDATA *lp = &win->alines[*y]->line[*x], *tp = NULL;
346 1.32 blymn nschar_t *np = NULL;
347 1.32 blymn cchar_t cc;
348 1.32 blymn attr_t attributes;
349 1.32 blymn
350 1.39.8.1 tls if (char_interp) {
351 1.39.8.1 tls /* special characters handling */
352 1.39.8.1 tls switch (wch->vals[0]) {
353 1.39.8.1 tls case L'\b':
354 1.39.8.1 tls if (--*x < 0)
355 1.39.8.1 tls *x = 0;
356 1.39.8.1 tls win->curx = *x;
357 1.39.8.1 tls return OK;
358 1.39.8.1 tls case L'\r':
359 1.32 blymn *x = 0;
360 1.39.8.1 tls win->curx = *x;
361 1.39.8.1 tls return OK;
362 1.39.8.1 tls case L'\n':
363 1.39.8.1 tls wclrtoeol(win);
364 1.32 blymn PSYNCH_IN;
365 1.39.8.1 tls *x = 0;
366 1.39.8.1 tls (*lnp)->flags &= ~__ISPASTEOL;
367 1.39.8.1 tls if (*y == win->scr_b) {
368 1.39.8.1 tls if (!(win->flags & __SCROLLOK))
369 1.39.8.1 tls return ERR;
370 1.39.8.1 tls PSYNCH_OUT;
371 1.39.8.1 tls scroll(win);
372 1.39.8.1 tls PSYNCH_IN;
373 1.39.8.1 tls } else {
374 1.39.8.1 tls (*y)++;
375 1.39.8.1 tls }
376 1.39.8.1 tls PSYNCH_OUT;
377 1.39.8.1 tls return OK;
378 1.39.8.1 tls case L'\t':
379 1.39.8.1 tls cc.vals[0] = L' ';
380 1.39.8.1 tls cc.elements = 1;
381 1.39.8.1 tls cc.attributes = win->wattr;
382 1.39.8.1 tls tabsize = win->screen->TABSIZE;
383 1.39.8.1 tls for (i = 0; i < tabsize - (*x % tabsize); i++) {
384 1.39.8.1 tls if (wadd_wch(win, &cc) == ERR)
385 1.39.8.1 tls return ERR;
386 1.39.8.1 tls }
387 1.39.8.1 tls return OK;
388 1.32 blymn }
389 1.32 blymn }
390 1.32 blymn
391 1.32 blymn /* check for non-spacing character */
392 1.32 blymn if (!wcwidth(wch->vals[0])) {
393 1.32 blymn #ifdef DEBUG
394 1.32 blymn __CTRACE(__CTRACE_INPUT,
395 1.32 blymn "_cursesi_addwchar: char '%c' is non-spacing\n",
396 1.32 blymn wch->vals[0]);
397 1.32 blymn #endif /* DEBUG */
398 1.32 blymn cw = WCOL(*lp);
399 1.32 blymn if (cw < 0) {
400 1.32 blymn lp += cw;
401 1.32 blymn *x += cw;
402 1.32 blymn }
403 1.32 blymn for (i = 0; i < wch->elements; i++) {
404 1.32 blymn if (!(np = (nschar_t *) malloc(sizeof(nschar_t))))
405 1.32 blymn return ERR;;
406 1.32 blymn np->ch = wch->vals[i];
407 1.32 blymn np->next = lp->nsp;
408 1.32 blymn lp->nsp = np;
409 1.32 blymn }
410 1.32 blymn (*lnp)->flags |= __ISDIRTY;
411 1.32 blymn newx = *x + win->ch_off;
412 1.32 blymn if (newx < *(*lnp)->firstchp)
413 1.32 blymn *(*lnp)->firstchp = newx;
414 1.32 blymn if (newx > *(*lnp)->lastchp)
415 1.32 blymn *(*lnp)->lastchp = newx;
416 1.32 blymn __touchline(win, *y, *x, *x);
417 1.32 blymn return OK;
418 1.32 blymn }
419 1.32 blymn /* check for new line first */
420 1.39.8.1 tls if (char_interp && ((*lnp)->flags & __ISPASTEOL)) {
421 1.32 blymn *x = 0;
422 1.32 blymn (*lnp)->flags &= ~__ISPASTEOL;
423 1.32 blymn if (*y == win->scr_b) {
424 1.32 blymn if (!(win->flags & __SCROLLOK))
425 1.32 blymn return ERR;
426 1.32 blymn PSYNCH_OUT;
427 1.32 blymn scroll(win);
428 1.32 blymn PSYNCH_IN;
429 1.32 blymn } else {
430 1.32 blymn (*y)++;
431 1.32 blymn }
432 1.35 roy (*lnp) = win->alines[*y];
433 1.35 roy lp = &win->alines[*y]->line[*x];
434 1.32 blymn }
435 1.32 blymn /* clear out the current character */
436 1.32 blymn cw = WCOL(*lp);
437 1.32 blymn if (cw >= 0) {
438 1.32 blymn sx = *x;
439 1.32 blymn } else {
440 1.32 blymn for (sx = *x - 1; sx >= max(*x + cw, 0); sx--) {
441 1.5 mycroft #ifdef DEBUG
442 1.31 jdc __CTRACE(__CTRACE_INPUT,
443 1.32 blymn "_cursesi_addwchar: clear current char (%d,%d)\n",
444 1.32 blymn *y, sx);
445 1.32 blymn #endif /* DEBUG */
446 1.35 roy tp = &win->alines[*y]->line[sx];
447 1.32 blymn tp->ch = (wchar_t) btowc((int) win->bch);
448 1.32 blymn if (_cursesi_copy_nsp(win->bnsp, tp) == ERR)
449 1.32 blymn return ERR;
450 1.32 blymn
451 1.32 blymn tp->attr = win->battr;
452 1.32 blymn SET_WCOL(*tp, 1);
453 1.32 blymn }
454 1.32 blymn sx = *x + cw;
455 1.32 blymn (*lnp)->flags |= __ISDIRTY;
456 1.32 blymn newx = sx + win->ch_off;
457 1.32 blymn if (newx < *(*lnp)->firstchp)
458 1.32 blymn *(*lnp)->firstchp = newx;
459 1.32 blymn }
460 1.32 blymn
461 1.32 blymn /* check for enough space before the end of line */
462 1.32 blymn cw = wcwidth(wch->vals[0]);
463 1.36 drochner if (cw < 0)
464 1.36 drochner cw = 1;
465 1.39.8.1 tls
466 1.32 blymn if (cw > win->maxx - *x) {
467 1.32 blymn #ifdef DEBUG
468 1.32 blymn __CTRACE(__CTRACE_INPUT,
469 1.32 blymn "_cursesi_addwchar: clear EOL (%d,%d)\n",
470 1.32 blymn *y, *x);
471 1.32 blymn #endif /* DEBUG */
472 1.32 blymn (*lnp)->flags |= __ISDIRTY;
473 1.32 blymn newx = *x + win->ch_off;
474 1.32 blymn if (newx < *(*lnp)->firstchp)
475 1.32 blymn *(*lnp)->firstchp = newx;
476 1.32 blymn for (tp = lp; *x < win->maxx; tp++, (*x)++) {
477 1.32 blymn tp->ch = (wchar_t) btowc((int) win->bch);
478 1.32 blymn if (_cursesi_copy_nsp(win->bnsp, tp) == ERR)
479 1.32 blymn return ERR;
480 1.32 blymn tp->attr = win->battr;
481 1.32 blymn SET_WCOL(*tp, 1);
482 1.32 blymn }
483 1.32 blymn newx = win->maxx - 1 + win->ch_off;
484 1.32 blymn if (newx > *(*lnp)->lastchp)
485 1.32 blymn *(*lnp)->lastchp = newx;
486 1.32 blymn __touchline(win, *y, sx, (int) win->maxx - 1);
487 1.32 blymn sx = *x = 0;
488 1.32 blymn if (*y == win->scr_b) {
489 1.32 blymn if (!(win->flags & __SCROLLOK))
490 1.32 blymn return ERR;
491 1.32 blymn PSYNCH_OUT;
492 1.32 blymn scroll(win);
493 1.32 blymn PSYNCH_IN;
494 1.32 blymn } else {
495 1.32 blymn (*y)++;
496 1.32 blymn }
497 1.35 roy lp = &win->alines[*y]->line[0];
498 1.35 roy (*lnp) = win->alines[*y];
499 1.32 blymn }
500 1.32 blymn win->cury = *y;
501 1.32 blymn
502 1.32 blymn /* add spacing character */
503 1.32 blymn #ifdef DEBUG
504 1.32 blymn __CTRACE(__CTRACE_INPUT,
505 1.32 blymn "_cursesi_addwchar: add character (%d,%d) 0x%x\n",
506 1.32 blymn *y, *x, wch->vals[0]);
507 1.32 blymn #endif /* DEBUG */
508 1.32 blymn (*lnp)->flags |= __ISDIRTY;
509 1.32 blymn newx = *x + win->ch_off;
510 1.32 blymn if (newx < *(*lnp)->firstchp)
511 1.32 blymn *(*lnp)->firstchp = newx;
512 1.32 blymn if (lp->nsp) {
513 1.32 blymn __cursesi_free_nsp(lp->nsp);
514 1.32 blymn lp->nsp = NULL;
515 1.32 blymn }
516 1.32 blymn
517 1.32 blymn lp->ch = wch->vals[0];
518 1.32 blymn
519 1.32 blymn attributes = (win->wattr | wch->attributes)
520 1.32 blymn & (WA_ATTRIBUTES & ~__COLOR);
521 1.32 blymn if (wch->attributes & __COLOR)
522 1.32 blymn attributes |= wch->attributes & __COLOR;
523 1.32 blymn else if (win->wattr & __COLOR)
524 1.32 blymn attributes |= win->wattr & __COLOR;
525 1.32 blymn if (attributes & __COLOR)
526 1.32 blymn lp->attr = attributes | (win->battr & ~__COLOR);
527 1.32 blymn else
528 1.32 blymn lp->attr = attributes | win->battr;
529 1.32 blymn
530 1.32 blymn SET_WCOL(*lp, cw);
531 1.32 blymn
532 1.32 blymn #ifdef DEBUG
533 1.32 blymn __CTRACE(__CTRACE_INPUT,
534 1.32 blymn "_cursesi_addwchar: add spacing char 0x%x, attr 0x%x\n",
535 1.32 blymn lp->ch, lp->attr);
536 1.32 blymn #endif /* DEBUG */
537 1.32 blymn
538 1.32 blymn if (wch->elements > 1) {
539 1.32 blymn for (i = 1; i < wch->elements; i++) {
540 1.32 blymn np = (nschar_t *)malloc(sizeof(nschar_t));
541 1.32 blymn if (!np)
542 1.32 blymn return ERR;;
543 1.32 blymn np->ch = wch->vals[i];
544 1.32 blymn np->next = lp->nsp;
545 1.24 jdc #ifdef DEBUG
546 1.31 jdc __CTRACE(__CTRACE_INPUT,
547 1.32 blymn "_cursesi_addwchar: add non-spacing char 0x%x\n", np->ch);
548 1.32 blymn #endif /* DEBUG */
549 1.32 blymn lp->nsp = np;
550 1.32 blymn }
551 1.32 blymn }
552 1.5 mycroft #ifdef DEBUG
553 1.32 blymn __CTRACE(__CTRACE_INPUT, "_cursesi_addwchar: non-spacing list header: %p\n",
554 1.32 blymn lp->nsp);
555 1.32 blymn __CTRACE(__CTRACE_INPUT, "_cursesi_addwchar: add rest columns (%d:%d)\n",
556 1.32 blymn sx + 1, sx + cw - 1);
557 1.32 blymn #endif /* DEBUG */
558 1.32 blymn for (tp = lp + 1, *x = sx + 1; *x - sx <= cw - 1; tp++, (*x)++) {
559 1.32 blymn if (tp->nsp) {
560 1.32 blymn __cursesi_free_nsp(tp->nsp);
561 1.32 blymn tp->nsp = NULL;
562 1.5 mycroft }
563 1.32 blymn tp->ch = wch->vals[0];
564 1.32 blymn tp->attr = lp->attr & WA_ATTRIBUTES;
565 1.32 blymn /* Mark as "continuation" cell */
566 1.32 blymn tp->attr |= __WCWIDTH;
567 1.5 mycroft }
568 1.39.8.1 tls
569 1.32 blymn if (*x == win->maxx) {
570 1.32 blymn (*lnp)->flags |= __ISPASTEOL;
571 1.32 blymn newx = win->maxx - 1 + win->ch_off;
572 1.32 blymn if (newx > *(*lnp)->lastchp)
573 1.32 blymn *(*lnp)->lastchp = newx;
574 1.32 blymn __touchline(win, *y, sx, (int) win->maxx - 1);
575 1.32 blymn win->curx = sx;
576 1.32 blymn } else {
577 1.32 blymn win->curx = *x;
578 1.32 blymn
579 1.32 blymn /* clear the remining of the current characer */
580 1.32 blymn if (*x && *x < win->maxx) {
581 1.32 blymn ex = sx + cw;
582 1.35 roy tp = &win->alines[*y]->line[ex];
583 1.32 blymn while (ex < win->maxx && WCOL(*tp) < 0) {
584 1.23 blymn #ifdef DEBUG
585 1.32 blymn __CTRACE(__CTRACE_INPUT,
586 1.39.8.1 tls "_cursesi_addwchar: clear "
587 1.39.8.1 tls "remaining of current char (%d,%d)nn",
588 1.39.8.1 tls *y, ex);
589 1.32 blymn #endif /* DEBUG */
590 1.32 blymn tp->ch = (wchar_t) btowc((int) win->bch);
591 1.32 blymn if (_cursesi_copy_nsp(win->bnsp, tp) == ERR)
592 1.32 blymn return ERR;
593 1.32 blymn tp->attr = win->battr;
594 1.32 blymn SET_WCOL(*tp, 1);
595 1.32 blymn tp++, ex++;
596 1.32 blymn }
597 1.32 blymn newx = ex - 1 + win->ch_off;
598 1.32 blymn if (newx > *(*lnp)->lastchp)
599 1.32 blymn *(*lnp)->lastchp = newx;
600 1.32 blymn __touchline(win, *y, sx, ex - 1);
601 1.32 blymn }
602 1.23 blymn }
603 1.32 blymn
604 1.32 blymn #ifdef DEBUG
605 1.32 blymn __CTRACE(__CTRACE_INPUT, "add_wch: %d : 0x%x\n", lp->ch, lp->attr);
606 1.32 blymn #endif /* DEBUG */
607 1.32 blymn return OK;
608 1.23 blymn #endif
609 1.1 cgd }
610