1 /* $NetBSD: addbytes.c,v 1.39 2011/07/01 01:19:33 joerg Exp $ */
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)addbytes.c 8.4 (Berkeley) 5/4/94";
37 __RCSID("$NetBSD: addbytes.c,v 1.39 2011/07/01 01:19:33 joerg Exp $");
44 #include "curses_private.h"
49 #define SYNCH_IN {y = win->cury; x = win->curx;}
50 #define SYNCH_OUT {win->cury = y; win->curx = x;}
51 #define PSYNCH_IN {*y = win->cury; *x = win->curx;}
52 #define PSYNCH_OUT {win->cury = *y; win->curx = *x;}
54 #ifndef _CURSES_USE_MACROS
58 * Add the character to the current position in stdscr.
61 addbytes(const char *bytes
, int count
)
63 return __waddbytes(stdscr
, bytes
, count
, 0);
68 * Add the character to the current position in the given window.
71 waddbytes(WINDOW
*win
, const char *bytes
, int count
)
73 return __waddbytes(win
, bytes
, count
, 0);
78 * Add the characters to stdscr at the location given.
81 mvaddbytes(int y
, int x
, const char *bytes
, int count
)
83 return mvwaddbytes(stdscr
, y
, x
, bytes
, count
);
88 * Add the characters to the given window at the location given.
91 mvwaddbytes(WINDOW
*win
, int y
, int x
, const char *bytes
, int count
)
93 if (wmove(win
, y
, x
) == ERR
)
96 return __waddbytes(win
, bytes
, count
, 0);
103 * Add the character to the current position in the given window.
106 __waddbytes(WINDOW
*win
, const char *bytes
, int count
, attr_t attr
)
121 for (i
= 0; i
< win
->maxy
; i
++) {
122 assert(win
->alines
[i
]->sentinel
== SENTINEL_VALUE
);
125 __CTRACE(__CTRACE_INPUT
, "ADDBYTES: add %d bytes\n", count
);
133 (void)memset(&st
, 0, sizeof(st
));
139 __CTRACE(__CTRACE_INPUT
, "ADDBYTES('%c', %x) at (%d, %d)\n",
142 err
= _cursesi_addbyte(win
, &lp
, &y
, &x
, c
, attr
);
146 * For wide-character support only, try and convert the
147 * given string into a wide character - we do this because
148 * this is how ncurses behaves (not that I think this is
149 * actually the correct thing to do but if we don't do it
150 * a lot of things that rely on this behaviour will break
151 * and we will be blamed). If the conversion succeeds
152 * then we eat the n characters used to make the wide char
155 n
= (int)mbrtowc(&wc
, bytes
, (size_t)count
, &st
);
157 /* not a valid conversion just eat a char */
160 (void)memset(&st
, 0, sizeof(st
));
161 } else if (wc
== 0) {
165 __CTRACE(__CTRACE_INPUT
,
166 "ADDBYTES WIDE(0x%x [%s], %x) at (%d, %d), ate %d bytes\n",
167 (unsigned) wc
, unctrl((unsigned) wc
), attr
, y
, x
, n
);
171 cc
.attributes
= attr
;
172 err
= _cursesi_addwchar(win
, &lp
, &y
, &x
, &cc
);
181 for (i
= 0; i
< win
->maxy
; i
++) {
182 assert(win
->alines
[i
]->sentinel
== SENTINEL_VALUE
);
191 * Internal function to add a byte and update the row and column
192 * positions as appropriate. This function is only used in the narrow
193 * character version of curses.
196 _cursesi_addbyte(WINDOW
*win
, __LINE
**lp
, int *y
, int *x
, int c
,
199 static char blanks
[] = " ";
206 if (waddbytes(win
, blanks
, 8 - (*x
% 8)) == ERR
)
213 __CTRACE(__CTRACE_INPUT
, "ADDBYTES(%p, %d, %d)\n",
217 if ((*lp
)->flags
& __ISPASTEOL
) {
220 (*lp
)->flags
&= ~__ISPASTEOL
;
221 if (*y
== win
->scr_b
) {
223 __CTRACE(__CTRACE_INPUT
,
224 "ADDBYTES - on bottom "
225 "of scrolling region\n");
227 if (!(win
->flags
& __SCROLLOK
))
235 *lp
= win
->alines
[*y
];
240 attributes
= (win
->wattr
| attr
) &
241 (__ATTRIBUTES
& ~__COLOR
);
243 attributes
|= attr
& __COLOR
;
244 else if (win
->wattr
& __COLOR
)
245 attributes
|= win
->wattr
& __COLOR
;
247 __CTRACE(__CTRACE_INPUT
,
248 "ADDBYTES: 1: y = %d, x = %d, firstch = %d, "
250 *y
, *x
, *win
->alines
[*y
]->firstchp
,
251 *win
->alines
[*y
]->lastchp
);
254 * Always update the change pointers. Otherwise,
255 * we could end up not displaying 'blank' characters
256 * when overlapping windows are displayed.
258 newx
= *x
+ win
->ch_off
;
259 (*lp
)->flags
|= __ISDIRTY
;
261 * firstchp/lastchp are shared between
262 * parent window and sub-window.
264 if (newx
< *(*lp
)->firstchp
)
265 *(*lp
)->firstchp
= newx
;
266 if (newx
> *(*lp
)->lastchp
)
267 *(*lp
)->lastchp
= newx
;
269 __CTRACE(__CTRACE_INPUT
,
270 "ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
271 *(*lp
)->firstchp
, *(*lp
)->lastchp
,
272 *(*lp
)->firstchp
- win
->ch_off
,
273 *(*lp
)->lastchp
- win
->ch_off
);
275 if (win
->bch
!= ' ' && c
== ' ')
276 (*lp
)->line
[*x
].ch
= win
->bch
;
278 (*lp
)->line
[*x
].ch
= c
;
280 if (attributes
& __COLOR
)
281 (*lp
)->line
[*x
].attr
=
282 attributes
| (win
->battr
& ~__COLOR
);
284 (*lp
)->line
[*x
].attr
= attributes
| win
->battr
;
286 if (*x
== win
->maxx
- 1)
287 (*lp
)->flags
|= __ISPASTEOL
;
291 __CTRACE(__CTRACE_INPUT
,
292 "ADDBYTES: 2: y = %d, x = %d, firstch = %d, "
294 *y
, *x
, *win
->alines
[*y
]->firstchp
,
295 *win
->alines
[*y
]->lastchp
);
316 * _cursesi_addwchar -
317 * Internal function to add a wide character and update the row
318 * and column positions.
321 _cursesi_addwchar(WINDOW
*win
, __LINE
**lnp
, int *y
, int *x
,
327 int sx
= 0, ex
= 0, cw
= 0, i
= 0, newx
= 0;
328 __LDATA
*lp
= &win
->alines
[*y
]->line
[*x
], *tp
= NULL
;
333 /* special characters handling */
334 switch (wch
->vals
[0]) {
347 (*lnp
)->flags
&= ~__ISPASTEOL
;
348 if (*y
== win
->scr_b
) {
349 if (!(win
->flags
& __SCROLLOK
))
362 cc
.attributes
= win
->wattr
;
363 for (i
= 0; i
< 8 - (*x
% 8); i
++) {
364 if (wadd_wch(win
, &cc
) == ERR
)
370 /* check for non-spacing character */
371 if (!wcwidth(wch
->vals
[0])) {
373 __CTRACE(__CTRACE_INPUT
,
374 "_cursesi_addwchar: char '%c' is non-spacing\n",
382 for (i
= 0; i
< wch
->elements
; i
++) {
383 if (!(np
= (nschar_t
*) malloc(sizeof(nschar_t
))))
385 np
->ch
= wch
->vals
[i
];
389 (*lnp
)->flags
|= __ISDIRTY
;
390 newx
= *x
+ win
->ch_off
;
391 if (newx
< *(*lnp
)->firstchp
)
392 *(*lnp
)->firstchp
= newx
;
393 if (newx
> *(*lnp
)->lastchp
)
394 *(*lnp
)->lastchp
= newx
;
395 __touchline(win
, *y
, *x
, *x
);
398 /* check for new line first */
399 if ((*lnp
)->flags
& __ISPASTEOL
) {
401 (*lnp
)->flags
&= ~__ISPASTEOL
;
402 if (*y
== win
->scr_b
) {
403 if (!(win
->flags
& __SCROLLOK
))
411 (*lnp
) = win
->alines
[*y
];
412 lp
= &win
->alines
[*y
]->line
[*x
];
414 /* clear out the current character */
419 for (sx
= *x
- 1; sx
>= max(*x
+ cw
, 0); sx
--) {
421 __CTRACE(__CTRACE_INPUT
,
422 "_cursesi_addwchar: clear current char (%d,%d)\n",
425 tp
= &win
->alines
[*y
]->line
[sx
];
426 tp
->ch
= (wchar_t) btowc((int) win
->bch
);
427 if (_cursesi_copy_nsp(win
->bnsp
, tp
) == ERR
)
430 tp
->attr
= win
->battr
;
434 (*lnp
)->flags
|= __ISDIRTY
;
435 newx
= sx
+ win
->ch_off
;
436 if (newx
< *(*lnp
)->firstchp
)
437 *(*lnp
)->firstchp
= newx
;
440 /* check for enough space before the end of line */
441 cw
= wcwidth(wch
->vals
[0]);
444 if (cw
> win
->maxx
- *x
) {
446 __CTRACE(__CTRACE_INPUT
,
447 "_cursesi_addwchar: clear EOL (%d,%d)\n",
450 (*lnp
)->flags
|= __ISDIRTY
;
451 newx
= *x
+ win
->ch_off
;
452 if (newx
< *(*lnp
)->firstchp
)
453 *(*lnp
)->firstchp
= newx
;
454 for (tp
= lp
; *x
< win
->maxx
; tp
++, (*x
)++) {
455 tp
->ch
= (wchar_t) btowc((int) win
->bch
);
456 if (_cursesi_copy_nsp(win
->bnsp
, tp
) == ERR
)
458 tp
->attr
= win
->battr
;
461 newx
= win
->maxx
- 1 + win
->ch_off
;
462 if (newx
> *(*lnp
)->lastchp
)
463 *(*lnp
)->lastchp
= newx
;
464 __touchline(win
, *y
, sx
, (int) win
->maxx
- 1);
466 if (*y
== win
->scr_b
) {
467 if (!(win
->flags
& __SCROLLOK
))
475 lp
= &win
->alines
[*y
]->line
[0];
476 (*lnp
) = win
->alines
[*y
];
480 /* add spacing character */
482 __CTRACE(__CTRACE_INPUT
,
483 "_cursesi_addwchar: add character (%d,%d) 0x%x\n",
484 *y
, *x
, wch
->vals
[0]);
486 (*lnp
)->flags
|= __ISDIRTY
;
487 newx
= *x
+ win
->ch_off
;
488 if (newx
< *(*lnp
)->firstchp
)
489 *(*lnp
)->firstchp
= newx
;
491 __cursesi_free_nsp(lp
->nsp
);
495 lp
->ch
= wch
->vals
[0];
497 attributes
= (win
->wattr
| wch
->attributes
)
498 & (WA_ATTRIBUTES
& ~__COLOR
);
499 if (wch
->attributes
& __COLOR
)
500 attributes
|= wch
->attributes
& __COLOR
;
501 else if (win
->wattr
& __COLOR
)
502 attributes
|= win
->wattr
& __COLOR
;
503 if (attributes
& __COLOR
)
504 lp
->attr
= attributes
| (win
->battr
& ~__COLOR
);
506 lp
->attr
= attributes
| win
->battr
;
511 __CTRACE(__CTRACE_INPUT
,
512 "_cursesi_addwchar: add spacing char 0x%x, attr 0x%x\n",
516 if (wch
->elements
> 1) {
517 for (i
= 1; i
< wch
->elements
; i
++) {
518 np
= (nschar_t
*)malloc(sizeof(nschar_t
));
521 np
->ch
= wch
->vals
[i
];
524 __CTRACE(__CTRACE_INPUT
,
525 "_cursesi_addwchar: add non-spacing char 0x%x\n", np
->ch
);
531 __CTRACE(__CTRACE_INPUT
, "_cursesi_addwchar: non-spacing list header: %p\n",
533 __CTRACE(__CTRACE_INPUT
, "_cursesi_addwchar: add rest columns (%d:%d)\n",
534 sx
+ 1, sx
+ cw
- 1);
536 for (tp
= lp
+ 1, *x
= sx
+ 1; *x
- sx
<= cw
- 1; tp
++, (*x
)++) {
538 __cursesi_free_nsp(tp
->nsp
);
541 tp
->ch
= wch
->vals
[0];
542 tp
->attr
= lp
->attr
& WA_ATTRIBUTES
;
543 /* Mark as "continuation" cell */
544 tp
->attr
|= __WCWIDTH
;
546 if (*x
== win
->maxx
) {
547 (*lnp
)->flags
|= __ISPASTEOL
;
548 newx
= win
->maxx
- 1 + win
->ch_off
;
549 if (newx
> *(*lnp
)->lastchp
)
550 *(*lnp
)->lastchp
= newx
;
551 __touchline(win
, *y
, sx
, (int) win
->maxx
- 1);
556 /* clear the remining of the current characer */
557 if (*x
&& *x
< win
->maxx
) {
559 tp
= &win
->alines
[*y
]->line
[ex
];
560 while (ex
< win
->maxx
&& WCOL(*tp
) < 0) {
562 __CTRACE(__CTRACE_INPUT
,
563 "_cursesi_addwchar: clear "
564 "remaining of current char (%d,%d)nn",
567 tp
->ch
= (wchar_t) btowc((int) win
->bch
);
568 if (_cursesi_copy_nsp(win
->bnsp
, tp
) == ERR
)
570 tp
->attr
= win
->battr
;
574 newx
= ex
- 1 + win
->ch_off
;
575 if (newx
> *(*lnp
)->lastchp
)
576 *(*lnp
)->lastchp
= newx
;
577 __touchline(win
, *y
, sx
, ex
- 1);
582 __CTRACE(__CTRACE_INPUT
, "add_wch: %d : 0x%x\n", lp
->ch
, lp
->attr
);