1 /* $NetBSD: addbytes.c,v 1.34 2008/07/04 16:52:10 tnozaki 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.34 2008/07/04 16:52:10 tnozaki Exp $");
43 #include "curses_private.h"
48 #define SYNCH_IN {y = win->cury; x = win->curx;}
49 #define SYNCH_OUT {win->cury = y; win->curx = x;}
50 #define PSYNCH_IN {*y = win->cury; *x = win->curx;}
51 #define PSYNCH_OUT {win->cury = *y; win->curx = *x;}
53 #ifndef _CURSES_USE_MACROS
57 * Add the character to the current position in stdscr.
60 addbytes(const char *bytes
, int count
)
62 return __waddbytes(stdscr
, bytes
, count
, 0);
67 * Add the character to the current position in the given window.
70 waddbytes(WINDOW
*win
, const char *bytes
, int count
)
72 return __waddbytes(win
, bytes
, count
, 0);
77 * Add the characters to stdscr at the location given.
80 mvaddbytes(int y
, int x
, const char *bytes
, int count
)
82 return mvwaddbytes(stdscr
, y
, x
, bytes
, count
);
87 * Add the characters to the given window at the location given.
90 mvwaddbytes(WINDOW
*win
, int y
, int x
, const char *bytes
, int count
)
92 if (wmove(win
, y
, x
) == ERR
)
95 return __waddbytes(win
, bytes
, count
, 0);
102 * Add the character to the current position in the given window.
105 __waddbytes(WINDOW
*win
, const char *bytes
, int count
, attr_t attr
)
120 for (i
= 0; i
< win
->maxy
; i
++) {
121 assert(win
->alines
[i
]->sentinel
== SENTINEL_VALUE
);
124 __CTRACE(__CTRACE_INPUT
, "ADDBYTES: add %d bytes\n", count
);
132 (void)mbrtowc(NULL
, NULL
, (size_t)0, &st
);
138 __CTRACE(__CTRACE_INPUT
, "ADDBYTES('%c', %x) at (%d, %d)\n",
141 err
= _cursesi_addbyte(win
, &lp
, &y
, &x
, c
, attr
);
145 * For wide character support only, try and convert the
146 * given string into a wide character - we do this because
147 * this is how ncurses behaves (not that I think this is
148 * actually the correct thing to do but if we don't do it
149 * a lot of things that rely on this behaviour will break
150 * and we will be blamed). If the conversion succeeds
151 * then we eat the n characters used to make the wide char
154 n
= (int)mbrtowc(&wc
, bytes
, (size_t)count
, &st
);
156 /* not a valid conversion just eat a char */
159 (void)mbrtowc(NULL
, NULL
, (size_t)0, &st
);
160 } else if (wc
== 0) {
164 __CTRACE(__CTRACE_INPUT
,
165 "ADDBYTES WIDE(0x%x [%s], %x) at (%d, %d), ate %d bytes\n",
166 (unsigned) wc
, unctrl((unsigned) wc
), attr
, y
, x
, n
);
170 cc
.attributes
= attr
;
171 err
= _cursesi_addwchar(win
, &lp
, &y
, &x
, &cc
);
180 for (i
= 0; i
< win
->maxy
; i
++) {
181 assert(win
->alines
[i
]->sentinel
== SENTINEL_VALUE
);
190 * Internal function to add a byte and update the row and column
191 * positions as appropriate. This function is only used in the narrow
192 * character version of curses.
195 _cursesi_addbyte(WINDOW
*win
, __LINE
**lp
, int *y
, int *x
, int c
,
198 static char blanks
[] = " ";
205 if (waddbytes(win
, blanks
, 8 - (*x
% 8)) == ERR
)
212 __CTRACE(__CTRACE_INPUT
, "ADDBYTES(%p, %d, %d)\n",
216 if ((*lp
)->flags
& __ISPASTEOL
) {
219 (*lp
)->flags
&= ~__ISPASTEOL
;
220 if (*y
== win
->scr_b
) {
222 __CTRACE(__CTRACE_INPUT
,
223 "ADDBYTES - on bottom "
224 "of scrolling region\n");
226 if (!(win
->flags
& __SCROLLOK
))
234 *lp
= win
->alines
[*y
];
239 attributes
= (win
->wattr
| attr
) &
240 (__ATTRIBUTES
& ~__COLOR
);
242 attributes
|= attr
& __COLOR
;
243 else if (win
->wattr
& __COLOR
)
244 attributes
|= win
->wattr
& __COLOR
;
246 __CTRACE(__CTRACE_INPUT
,
247 "ADDBYTES: 1: y = %d, x = %d, firstch = %d, "
249 *y
, *x
, *win
->alines
[*y
]->firstchp
,
250 *win
->alines
[*y
]->lastchp
);
253 * Always update the change pointers. Otherwise,
254 * we could end up not displaying 'blank' characters
255 * when overlapping windows are displayed.
257 newx
= *x
+ win
->ch_off
;
258 (*lp
)->flags
|= __ISDIRTY
;
260 * firstchp/lastchp are shared between
261 * parent window and sub-window.
263 if (newx
< *(*lp
)->firstchp
)
264 *(*lp
)->firstchp
= newx
;
265 if (newx
> *(*lp
)->lastchp
)
266 *(*lp
)->lastchp
= newx
;
268 __CTRACE(__CTRACE_INPUT
,
269 "ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
270 *(*lp
)->firstchp
, *(*lp
)->lastchp
,
271 *(*lp
)->firstchp
- win
->ch_off
,
272 *(*lp
)->lastchp
- win
->ch_off
);
274 if (win
->bch
!= ' ' && c
== ' ')
275 (*lp
)->line
[*x
].ch
= win
->bch
;
277 (*lp
)->line
[*x
].ch
= c
;
279 if (attributes
& __COLOR
)
280 (*lp
)->line
[*x
].attr
=
281 attributes
| (win
->battr
& ~__COLOR
);
283 (*lp
)->line
[*x
].attr
= attributes
| win
->battr
;
285 if (*x
== win
->maxx
- 1)
286 (*lp
)->flags
|= __ISPASTEOL
;
290 __CTRACE(__CTRACE_INPUT
,
291 "ADDBYTES: 2: y = %d, x = %d, firstch = %d, "
293 *y
, *x
, *win
->alines
[*y
]->firstchp
,
294 *win
->alines
[*y
]->lastchp
);
315 * _cursesi_addwchar -
316 * Internal function to add a wide character and update the row
317 * and column positions.
320 _cursesi_addwchar(WINDOW
*win
, __LINE
**lnp
, int *y
, int *x
,
326 int sx
= 0, ex
= 0, cw
= 0, i
= 0, newx
= 0;
327 __LDATA
*lp
= &win
->alines
[*y
]->line
[*x
], *tp
= NULL
;
332 /* special characters handling */
333 switch (wch
->vals
[0]) {
346 (*lnp
)->flags
&= ~__ISPASTEOL
;
347 if (*y
== win
->scr_b
) {
348 if (!(win
->flags
& __SCROLLOK
))
361 cc
.attributes
= win
->wattr
;
362 for (i
= 0; i
< 8 - (*x
% 8); i
++) {
363 if (wadd_wch(win
, &cc
) == ERR
)
369 /* check for non-spacing character */
370 if (!wcwidth(wch
->vals
[0])) {
372 __CTRACE(__CTRACE_INPUT
,
373 "_cursesi_addwchar: char '%c' is non-spacing\n",
381 for (i
= 0; i
< wch
->elements
; i
++) {
382 if (!(np
= (nschar_t
*) malloc(sizeof(nschar_t
))))
384 np
->ch
= wch
->vals
[i
];
388 (*lnp
)->flags
|= __ISDIRTY
;
389 newx
= *x
+ win
->ch_off
;
390 if (newx
< *(*lnp
)->firstchp
)
391 *(*lnp
)->firstchp
= newx
;
392 if (newx
> *(*lnp
)->lastchp
)
393 *(*lnp
)->lastchp
= newx
;
394 __touchline(win
, *y
, *x
, *x
);
397 /* check for new line first */
398 if ((*lnp
)->flags
& __ISPASTEOL
) {
400 (*lnp
)->flags
&= ~__ISPASTEOL
;
401 if (*y
== win
->scr_b
) {
402 if (!(win
->flags
& __SCROLLOK
))
410 (*lnp
) = win
->alines
[*y
];
411 lp
= &win
->alines
[*y
]->line
[*x
];
413 /* clear out the current character */
418 for (sx
= *x
- 1; sx
>= max(*x
+ cw
, 0); sx
--) {
420 __CTRACE(__CTRACE_INPUT
,
421 "_cursesi_addwchar: clear current char (%d,%d)\n",
424 tp
= &win
->alines
[*y
]->line
[sx
];
425 tp
->ch
= (wchar_t) btowc((int) win
->bch
);
426 if (_cursesi_copy_nsp(win
->bnsp
, tp
) == ERR
)
429 tp
->attr
= win
->battr
;
433 (*lnp
)->flags
|= __ISDIRTY
;
434 newx
= sx
+ win
->ch_off
;
435 if (newx
< *(*lnp
)->firstchp
)
436 *(*lnp
)->firstchp
= newx
;
439 /* check for enough space before the end of line */
440 cw
= wcwidth(wch
->vals
[0]);
441 if (cw
> win
->maxx
- *x
) {
443 __CTRACE(__CTRACE_INPUT
,
444 "_cursesi_addwchar: clear EOL (%d,%d)\n",
447 (*lnp
)->flags
|= __ISDIRTY
;
448 newx
= *x
+ win
->ch_off
;
449 if (newx
< *(*lnp
)->firstchp
)
450 *(*lnp
)->firstchp
= newx
;
451 for (tp
= lp
; *x
< win
->maxx
; tp
++, (*x
)++) {
452 tp
->ch
= (wchar_t) btowc((int) win
->bch
);
453 if (_cursesi_copy_nsp(win
->bnsp
, tp
) == ERR
)
455 tp
->attr
= win
->battr
;
458 newx
= win
->maxx
- 1 + win
->ch_off
;
459 if (newx
> *(*lnp
)->lastchp
)
460 *(*lnp
)->lastchp
= newx
;
461 __touchline(win
, *y
, sx
, (int) win
->maxx
- 1);
463 if (*y
== win
->scr_b
) {
464 if (!(win
->flags
& __SCROLLOK
))
472 lp
= &win
->alines
[*y
]->line
[0];
473 (*lnp
) = win
->alines
[*y
];
477 /* add spacing character */
479 __CTRACE(__CTRACE_INPUT
,
480 "_cursesi_addwchar: add character (%d,%d) 0x%x\n",
481 *y
, *x
, wch
->vals
[0]);
483 (*lnp
)->flags
|= __ISDIRTY
;
484 newx
= *x
+ win
->ch_off
;
485 if (newx
< *(*lnp
)->firstchp
)
486 *(*lnp
)->firstchp
= newx
;
488 __cursesi_free_nsp(lp
->nsp
);
492 lp
->ch
= wch
->vals
[0];
494 attributes
= (win
->wattr
| wch
->attributes
)
495 & (WA_ATTRIBUTES
& ~__COLOR
);
496 if (wch
->attributes
& __COLOR
)
497 attributes
|= wch
->attributes
& __COLOR
;
498 else if (win
->wattr
& __COLOR
)
499 attributes
|= win
->wattr
& __COLOR
;
500 if (attributes
& __COLOR
)
501 lp
->attr
= attributes
| (win
->battr
& ~__COLOR
);
503 lp
->attr
= attributes
| win
->battr
;
508 __CTRACE(__CTRACE_INPUT
,
509 "_cursesi_addwchar: add spacing char 0x%x, attr 0x%x\n",
513 if (wch
->elements
> 1) {
514 for (i
= 1; i
< wch
->elements
; i
++) {
515 np
= (nschar_t
*)malloc(sizeof(nschar_t
));
518 np
->ch
= wch
->vals
[i
];
521 __CTRACE(__CTRACE_INPUT
,
522 "_cursesi_addwchar: add non-spacing char 0x%x\n", np
->ch
);
528 __CTRACE(__CTRACE_INPUT
, "_cursesi_addwchar: non-spacing list header: %p\n",
530 __CTRACE(__CTRACE_INPUT
, "_cursesi_addwchar: add rest columns (%d:%d)\n",
531 sx
+ 1, sx
+ cw
- 1);
533 for (tp
= lp
+ 1, *x
= sx
+ 1; *x
- sx
<= cw
- 1; tp
++, (*x
)++) {
535 __cursesi_free_nsp(tp
->nsp
);
538 tp
->ch
= wch
->vals
[0];
539 tp
->attr
= lp
->attr
& WA_ATTRIBUTES
;
540 /* Mark as "continuation" cell */
541 tp
->attr
|= __WCWIDTH
;
543 if (*x
== win
->maxx
) {
544 (*lnp
)->flags
|= __ISPASTEOL
;
545 newx
= win
->maxx
- 1 + win
->ch_off
;
546 if (newx
> *(*lnp
)->lastchp
)
547 *(*lnp
)->lastchp
= newx
;
548 __touchline(win
, *y
, sx
, (int) win
->maxx
- 1);
553 /* clear the remining of the current characer */
554 if (*x
&& *x
< win
->maxx
) {
556 tp
= &win
->alines
[*y
]->line
[ex
];
557 while (ex
< win
->maxx
&& WCOL(*tp
) < 0) {
559 __CTRACE(__CTRACE_INPUT
,
560 "_cursesi_addwchar: clear "
561 "remaining of current char (%d,%d)nn",
564 tp
->ch
= (wchar_t) btowc((int) win
->bch
);
565 if (_cursesi_copy_nsp(win
->bnsp
, tp
) == ERR
)
567 tp
->attr
= win
->battr
;
571 newx
= ex
- 1 + win
->ch_off
;
572 if (newx
> *(*lnp
)->lastchp
)
573 *(*lnp
)->lastchp
= newx
;
574 __touchline(win
, *y
, sx
, ex
- 1);
579 __CTRACE(__CTRACE_INPUT
, "add_wch: %d : 0x%x\n", lp
->ch
, lp
->attr
);