1 /* $NetBSD: addbytes.c,v 1.42 2013/11/10 03:14:16 christos 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.42 2013/11/10 03:14:16 christos 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 _cursesi_waddbytes(stdscr
, bytes
, count
, 0, 1);
68 * Add the character to the current position in the given window.
71 waddbytes(WINDOW
*win
, const char *bytes
, int count
)
73 return _cursesi_waddbytes(win
, bytes
, count
, 0, 1);
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 _cursesi_waddbytes(win
, bytes
, count
, 0, 1);
102 __waddbytes(WINDOW
*win
, const char *bytes
, int count
, attr_t attr
)
104 return _cursesi_waddbytes(win
, bytes
, count
, attr
, 1);
108 * _cursesi_waddbytes --
109 * Add the character to the current position in the given window.
110 * if char_interp is non-zero then character interpretation is done on
111 * the byte (i.e. \n to newline, \r to carriage return, \b to backspace
115 _cursesi_waddbytes(WINDOW
*win
, const char *bytes
, int count
, attr_t attr
,
131 for (i
= 0; i
< win
->maxy
; i
++) {
132 assert(win
->alines
[i
]->sentinel
== SENTINEL_VALUE
);
135 __CTRACE(__CTRACE_INPUT
, "ADDBYTES: add %d bytes\n", count
);
143 (void)memset(&st
, 0, sizeof(st
));
149 __CTRACE(__CTRACE_INPUT
, "ADDBYTES('%c', %x) at (%d, %d)\n",
152 err
= _cursesi_addbyte(win
, &lp
, &y
, &x
, c
, attr
, char_interp
);
156 * For wide-character support only, try and convert the
157 * given string into a wide character - we do this because
158 * this is how ncurses behaves (not that I think this is
159 * actually the correct thing to do but if we don't do it
160 * a lot of things that rely on this behaviour will break
161 * and we will be blamed). If the conversion succeeds
162 * then we eat the n characters used to make the wide char
165 n
= (int)mbrtowc(&wc
, bytes
, (size_t)count
, &st
);
167 /* not a valid conversion just eat a char */
170 (void)memset(&st
, 0, sizeof(st
));
171 } else if (wc
== 0) {
175 __CTRACE(__CTRACE_INPUT
,
176 "ADDBYTES WIDE(0x%x [%s], %x) at (%d, %d), ate %d bytes\n",
177 (unsigned) wc
, unctrl((unsigned) wc
), attr
, y
, x
, n
);
181 cc
.attributes
= attr
;
182 err
= _cursesi_addwchar(win
, &lp
, &y
, &x
, &cc
, char_interp
);
191 for (i
= 0; i
< win
->maxy
; i
++) {
192 assert(win
->alines
[i
]->sentinel
== SENTINEL_VALUE
);
201 * Internal function to add a byte and update the row and column
202 * positions as appropriate. This function is only used in the narrow
203 * character version of curses. If update_cursor is non-zero then character
207 _cursesi_addbyte(WINDOW
*win
, __LINE
**lp
, int *y
, int *x
, int c
,
208 attr_t attr
, int char_interp
)
210 static char blank
[] = " ";
218 tabsize
= win
->screen
->TABSIZE
;
220 for (i
= 0; i
< (tabsize
- (*x
% tabsize
)); i
++) {
221 if (waddbytes(win
, blank
, 1) == ERR
)
231 (*lp
)->flags
|= __ISPASTEOL
;
248 __CTRACE(__CTRACE_INPUT
, "ADDBYTES(%p, %d, %d)\n", win
, *y
, *x
);
251 if (char_interp
&& ((*lp
)->flags
& __ISPASTEOL
)) {
253 (*lp
)->flags
&= ~__ISPASTEOL
;
254 if (*y
== win
->scr_b
) {
256 __CTRACE(__CTRACE_INPUT
,
257 "ADDBYTES - on bottom "
258 "of scrolling region\n");
260 if (!(win
->flags
& __SCROLLOK
))
268 *lp
= win
->alines
[*y
];
274 __CTRACE(__CTRACE_INPUT
,
275 "ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n",
276 *y
, *x
, *win
->alines
[*y
]->firstchp
,
277 *win
->alines
[*y
]->lastchp
);
280 attributes
= (win
->wattr
| attr
) & (__ATTRIBUTES
& ~__COLOR
);
282 attributes
|= attr
& __COLOR
;
283 else if (win
->wattr
& __COLOR
)
284 attributes
|= win
->wattr
& __COLOR
;
287 * Always update the change pointers. Otherwise,
288 * we could end up not displaying 'blank' characters
289 * when overlapping windows are displayed.
291 newx
= *x
+ win
->ch_off
;
292 (*lp
)->flags
|= __ISDIRTY
;
294 * firstchp/lastchp are shared between
295 * parent window and sub-window.
297 if (newx
< *(*lp
)->firstchp
)
298 *(*lp
)->firstchp
= newx
;
299 if (newx
> *(*lp
)->lastchp
)
300 *(*lp
)->lastchp
= newx
;
302 __CTRACE(__CTRACE_INPUT
, "ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
303 *(*lp
)->firstchp
, *(*lp
)->lastchp
,
304 *(*lp
)->firstchp
- win
->ch_off
,
305 *(*lp
)->lastchp
- win
->ch_off
);
307 if (win
->bch
!= ' ' && c
== ' ')
308 (*lp
)->line
[*x
].ch
= win
->bch
;
310 (*lp
)->line
[*x
].ch
= c
;
312 if (attributes
& __COLOR
)
313 (*lp
)->line
[*x
].attr
=
314 attributes
| (win
->battr
& ~__COLOR
);
316 (*lp
)->line
[*x
].attr
= attributes
| win
->battr
;
318 if (*x
== win
->maxx
- 1)
319 (*lp
)->flags
|= __ISPASTEOL
;
324 __CTRACE(__CTRACE_INPUT
,
325 "ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n",
326 *y
, *x
, *win
->alines
[*y
]->firstchp
,
327 *win
->alines
[*y
]->lastchp
);
333 * _cursesi_addwchar -
334 * Internal function to add a wide character and update the row
335 * and column positions.
338 _cursesi_addwchar(WINDOW
*win
, __LINE
**lnp
, int *y
, int *x
,
339 const cchar_t
*wch
, int char_interp
)
344 int sx
= 0, ex
= 0, cw
= 0, i
= 0, newx
= 0, tabsize
;
345 __LDATA
*lp
= &win
->alines
[*y
]->line
[*x
], *tp
= NULL
;
351 /* special characters handling */
352 switch (wch
->vals
[0]) {
366 (*lnp
)->flags
&= ~__ISPASTEOL
;
367 if (*y
== win
->scr_b
) {
368 if (!(win
->flags
& __SCROLLOK
))
381 cc
.attributes
= win
->wattr
;
382 tabsize
= win
->screen
->TABSIZE
;
383 for (i
= 0; i
< tabsize
- (*x
% tabsize
); i
++) {
384 if (wadd_wch(win
, &cc
) == ERR
)
391 /* check for non-spacing character */
392 if (!wcwidth(wch
->vals
[0])) {
394 __CTRACE(__CTRACE_INPUT
,
395 "_cursesi_addwchar: char '%c' is non-spacing\n",
403 for (i
= 0; i
< wch
->elements
; i
++) {
404 if (!(np
= (nschar_t
*) malloc(sizeof(nschar_t
))))
406 np
->ch
= wch
->vals
[i
];
410 (*lnp
)->flags
|= __ISDIRTY
;
411 newx
= *x
+ win
->ch_off
;
412 if (newx
< *(*lnp
)->firstchp
)
413 *(*lnp
)->firstchp
= newx
;
414 if (newx
> *(*lnp
)->lastchp
)
415 *(*lnp
)->lastchp
= newx
;
416 __touchline(win
, *y
, *x
, *x
);
419 /* check for new line first */
420 if (char_interp
&& ((*lnp
)->flags
& __ISPASTEOL
)) {
422 (*lnp
)->flags
&= ~__ISPASTEOL
;
423 if (*y
== win
->scr_b
) {
424 if (!(win
->flags
& __SCROLLOK
))
432 (*lnp
) = win
->alines
[*y
];
433 lp
= &win
->alines
[*y
]->line
[*x
];
435 /* clear out the current character */
440 for (sx
= *x
- 1; sx
>= max(*x
+ cw
, 0); sx
--) {
442 __CTRACE(__CTRACE_INPUT
,
443 "_cursesi_addwchar: clear current char (%d,%d)\n",
446 tp
= &win
->alines
[*y
]->line
[sx
];
447 tp
->ch
= (wchar_t) btowc((int) win
->bch
);
448 if (_cursesi_copy_nsp(win
->bnsp
, tp
) == ERR
)
451 tp
->attr
= win
->battr
;
455 (*lnp
)->flags
|= __ISDIRTY
;
456 newx
= sx
+ win
->ch_off
;
457 if (newx
< *(*lnp
)->firstchp
)
458 *(*lnp
)->firstchp
= newx
;
461 /* check for enough space before the end of line */
462 cw
= wcwidth(wch
->vals
[0]);
466 if (cw
> win
->maxx
- *x
) {
468 __CTRACE(__CTRACE_INPUT
,
469 "_cursesi_addwchar: clear EOL (%d,%d)\n",
472 (*lnp
)->flags
|= __ISDIRTY
;
473 newx
= *x
+ win
->ch_off
;
474 if (newx
< *(*lnp
)->firstchp
)
475 *(*lnp
)->firstchp
= newx
;
476 for (tp
= lp
; *x
< win
->maxx
; tp
++, (*x
)++) {
477 tp
->ch
= (wchar_t) btowc((int) win
->bch
);
478 if (_cursesi_copy_nsp(win
->bnsp
, tp
) == ERR
)
480 tp
->attr
= win
->battr
;
483 newx
= win
->maxx
- 1 + win
->ch_off
;
484 if (newx
> *(*lnp
)->lastchp
)
485 *(*lnp
)->lastchp
= newx
;
486 __touchline(win
, *y
, sx
, (int) win
->maxx
- 1);
488 if (*y
== win
->scr_b
) {
489 if (!(win
->flags
& __SCROLLOK
))
497 lp
= &win
->alines
[*y
]->line
[0];
498 (*lnp
) = win
->alines
[*y
];
502 /* add spacing character */
504 __CTRACE(__CTRACE_INPUT
,
505 "_cursesi_addwchar: add character (%d,%d) 0x%x\n",
506 *y
, *x
, wch
->vals
[0]);
508 (*lnp
)->flags
|= __ISDIRTY
;
509 newx
= *x
+ win
->ch_off
;
510 if (newx
< *(*lnp
)->firstchp
)
511 *(*lnp
)->firstchp
= newx
;
513 __cursesi_free_nsp(lp
->nsp
);
517 lp
->ch
= wch
->vals
[0];
519 attributes
= (win
->wattr
| wch
->attributes
)
520 & (WA_ATTRIBUTES
& ~__COLOR
);
521 if (wch
->attributes
& __COLOR
)
522 attributes
|= wch
->attributes
& __COLOR
;
523 else if (win
->wattr
& __COLOR
)
524 attributes
|= win
->wattr
& __COLOR
;
525 if (attributes
& __COLOR
)
526 lp
->attr
= attributes
| (win
->battr
& ~__COLOR
);
528 lp
->attr
= attributes
| win
->battr
;
533 __CTRACE(__CTRACE_INPUT
,
534 "_cursesi_addwchar: add spacing char 0x%x, attr 0x%x\n",
538 if (wch
->elements
> 1) {
539 for (i
= 1; i
< wch
->elements
; i
++) {
540 np
= (nschar_t
*)malloc(sizeof(nschar_t
));
543 np
->ch
= wch
->vals
[i
];
546 __CTRACE(__CTRACE_INPUT
,
547 "_cursesi_addwchar: add non-spacing char 0x%x\n", np
->ch
);
553 __CTRACE(__CTRACE_INPUT
, "_cursesi_addwchar: non-spacing list header: %p\n",
555 __CTRACE(__CTRACE_INPUT
, "_cursesi_addwchar: add rest columns (%d:%d)\n",
556 sx
+ 1, sx
+ cw
- 1);
558 for (tp
= lp
+ 1, *x
= sx
+ 1; *x
- sx
<= cw
- 1; tp
++, (*x
)++) {
560 __cursesi_free_nsp(tp
->nsp
);
563 tp
->ch
= wch
->vals
[0];
564 tp
->attr
= lp
->attr
& WA_ATTRIBUTES
;
565 /* Mark as "continuation" cell */
566 tp
->attr
|= __WCWIDTH
;
569 if (*x
== win
->maxx
) {
570 (*lnp
)->flags
|= __ISPASTEOL
;
571 newx
= win
->maxx
- 1 + win
->ch_off
;
572 if (newx
> *(*lnp
)->lastchp
)
573 *(*lnp
)->lastchp
= newx
;
574 __touchline(win
, *y
, sx
, (int) win
->maxx
- 1);
579 /* clear the remining of the current characer */
580 if (*x
&& *x
< win
->maxx
) {
582 tp
= &win
->alines
[*y
]->line
[ex
];
583 while (ex
< win
->maxx
&& WCOL(*tp
) < 0) {
585 __CTRACE(__CTRACE_INPUT
,
586 "_cursesi_addwchar: clear "
587 "remaining of current char (%d,%d)nn",
590 tp
->ch
= (wchar_t) btowc((int) win
->bch
);
591 if (_cursesi_copy_nsp(win
->bnsp
, tp
) == ERR
)
593 tp
->attr
= win
->battr
;
597 newx
= ex
- 1 + win
->ch_off
;
598 if (newx
> *(*lnp
)->lastchp
)
599 *(*lnp
)->lastchp
= newx
;
600 __touchline(win
, *y
, sx
, ex
- 1);
605 __CTRACE(__CTRACE_INPUT
, "add_wch: %d : 0x%x\n", lp
->ch
, lp
->attr
);