addbytes.c revision 1.67 1 /* $NetBSD: addbytes.c,v 1.67 2022/12/12 21:14:15 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.67 2022/12/12 21:14:15 blymn Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <stdlib.h>
42 #include <string.h>
43 #include "curses.h"
44 #include "curses_private.h"
45 #ifdef DEBUG
46 #include <assert.h>
47 #endif
48
49 #ifndef _CURSES_USE_MACROS
50
51 /*
52 * addbytes --
53 * Add the characters to the current position in stdscr.
54 */
55 int
56 addbytes(const char *bytes, int count)
57 {
58
59 return _cursesi_waddbytes(stdscr, bytes, count, 0, 1);
60 }
61
62 /*
63 * waddbytes --
64 * Add the characters to the current position in the given window.
65 */
66 int
67 waddbytes(WINDOW *win, const char *bytes, int count)
68 {
69
70 return _cursesi_waddbytes(win, bytes, count, 0, 1);
71 }
72
73 /*
74 * mvaddbytes --
75 * Add the characters to stdscr at the location given.
76 */
77 int
78 mvaddbytes(int y, int x, const char *bytes, int count)
79 {
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
92 if (wmove(win, y, x) == ERR)
93 return ERR;
94
95 return _cursesi_waddbytes(win, bytes, count, 0, 1);
96 }
97
98 #endif
99
100 int
101 __waddbytes(WINDOW *win, const char *bytes, int count, attr_t attr)
102 {
103
104 return _cursesi_waddbytes(win, bytes, count, attr, 1);
105 }
106
107 /*
108 * _cursesi_waddbytes --
109 * Add the characters 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
112 * and so on).
113 */
114 int
115 _cursesi_waddbytes(WINDOW *win, const char *bytes, int count, attr_t attr,
116 int char_interp)
117 {
118 int *py = &win->cury, *px = &win->curx, err;
119 __LINE *lp;
120 #ifdef HAVE_WCHAR
121 int n, width;
122 cchar_t cc;
123 wchar_t wc;
124 mbstate_t st;
125 #else
126 int c;
127 #endif
128 #ifdef DEBUG
129 int i;
130
131 for (i = 0; i < win->maxy; i++) {
132 assert(win->alines[i]->sentinel == SENTINEL_VALUE);
133 }
134
135 __CTRACE(__CTRACE_INPUT, "ADDBYTES: add %d bytes\n", count);
136 #endif
137
138 err = OK;
139 lp = win->alines[*py];
140
141 #ifdef HAVE_WCHAR
142 (void)memset(&st, 0, sizeof(st));
143 #endif
144 while (count > 0) {
145 #ifndef HAVE_WCHAR
146 c = *bytes++;
147 __CTRACE(__CTRACE_INPUT, "ADDBYTES('%c', %x) at (%d, %d)\n",
148 c, attr, *py, *px);
149 err = _cursesi_addbyte(win, &lp, py, px, c, attr, char_interp);
150 count--;
151 #else
152 /*
153 * For wide-character support only, try to convert the
154 * given string into a wide character - we do this because
155 * this is how ncurses behaves (not that I think this is
156 * actually the correct thing to do but if we don't do it
157 * a lot of things that rely on this behaviour will break
158 * and we will be blamed). If the conversion succeeds
159 * then we eat the n characters used to make the wide char
160 * from the string.
161 */
162 n = (int)mbrtowc(&wc, bytes, (size_t)count, &st);
163 if (n < 0) {
164 /* not a valid conversion just eat a char */
165 wc = *bytes;
166 n = 1;
167 (void)memset(&st, 0, sizeof(st));
168 } else if (wc == 0) {
169 break;
170 }
171
172 __CTRACE(__CTRACE_INPUT,
173 "ADDBYTES WIDE(0x%x [%s], %x) at (%d, %d), ate %d bytes\n",
174 (unsigned)wc, unctrl((unsigned)wc), attr, *py, *px, n);
175 cc.vals[0] = wc;
176 cc.elements = 1;
177 cc.attributes = attr;
178 err = _cursesi_addwchar(win, &lp, py, px, &cc, char_interp);
179 bytes += n;
180
181 width = wcwidth(wc);
182 if (width < 0)
183 width = 1;
184 count -= width;
185 #endif
186 }
187
188 #ifdef DEBUG
189 for (i = 0; i < win->maxy; i++) {
190 assert(win->alines[i]->sentinel == SENTINEL_VALUE);
191 }
192 #endif
193
194 return (err);
195 }
196
197 /*
198 * _cursesi_addbyte -
199 * Internal function to add a byte and update the row and column
200 * positions as appropriate. If char_interp is non-zero then
201 * character interpretation is done on the byte. This function is
202 * only used in the narrow character version of curses.
203 */
204 int
205 _cursesi_addbyte(WINDOW *win, __LINE **lp, int *y, int *x, int c,
206 attr_t attr, int char_interp)
207 {
208 static char blank[] = " ";
209 int tabsize;
210 int newx, i, wcols;
211 attr_t attributes;
212
213 if (char_interp) {
214 switch (c) {
215 case '\t':
216 tabsize = win->screen->TABSIZE;
217 newx = tabsize - (*x % tabsize);
218 /* if at the bottom of the window and
219 not allowed to scroll then just do
220 what we can */
221 if ((*y == win->scr_b) &&
222 !(win->flags & __SCROLLOK)) {
223 if ((*lp)->flags & __ISPASTEOL) {
224 return OK;
225 }
226
227 if (*x + newx > win->maxx - 1)
228 newx = win->maxx - *x - 1;
229 }
230
231 for (i = 0; i < newx; i++) {
232 if (waddbytes(win, blank, 1) == ERR)
233 return ERR;
234 }
235 return OK;
236
237 case '\n':
238 wclrtoeol(win);
239 (*lp)->flags |= __ISPASTEOL;
240 break;
241
242 case '\r':
243 *x = 0;
244 return OK;
245
246 case '\b':
247 if (--(*x) < 0)
248 *x = 0;
249 return OK;
250 }
251 }
252
253 __CTRACE(__CTRACE_INPUT, "ADDBYTES(%p, %d, %d)\n", win, *y, *x);
254
255 if (char_interp && ((*lp)->flags & __ISPASTEOL)) {
256 *x = 0;
257 (*lp)->flags &= ~__ISPASTEOL;
258 if (*y == win->scr_b) {
259 __CTRACE(__CTRACE_INPUT,
260 "ADDBYTES - on bottom of scrolling region\n");
261 if (!(win->flags & __SCROLLOK))
262 return ERR;
263 scroll(win);
264 } else {
265 (*y)++;
266 }
267 *lp = win->alines[*y];
268 if (c == '\n')
269 return OK;
270 }
271
272 __CTRACE(__CTRACE_INPUT,
273 "ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n",
274 *y, *x, *win->alines[*y]->firstchp, *win->alines[*y]->lastchp);
275
276 attributes = (win->wattr | attr) & (__ATTRIBUTES & ~__COLOR);
277 if (attr & __COLOR)
278 attributes |= attr & __COLOR;
279 else if (win->wattr & __COLOR)
280 attributes |= win->wattr & __COLOR;
281
282
283 wcols = wcwidth(c);
284 if (wcols < 0)
285 wcols = 1;
286
287 /*
288 * Always update the change pointers. Otherwise,
289 * we could end up not displaying 'blank' characters
290 * when overlapping windows are displayed.
291 */
292 newx = *x + win->ch_off;
293 (*lp)->flags |= __ISDIRTY;
294 /*
295 * firstchp/lastchp are shared between
296 * parent window and sub-window.
297 */
298 if (newx < *(*lp)->firstchp)
299 *(*lp)->firstchp = newx;
300
301 if (newx > *(*lp)->lastchp)
302 *(*lp)->lastchp = newx;
303 __CTRACE(__CTRACE_INPUT, "ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
304 *(*lp)->firstchp, *(*lp)->lastchp,
305 *(*lp)->firstchp - win->ch_off,
306 *(*lp)->lastchp - win->ch_off);
307 if (win->bch != ' ' && c == ' ') {
308 (*lp)->line[*x].ch = win->bch;
309 #ifdef HAVE_CHAR
310 (*lp)->line[*x].wcols = win->wcols;
311 #endif
312 } else {
313 (*lp)->line[*x].ch = c;
314 #ifdef HAVE_CHAR
315 (*lp)->line[*x].wcols = wcols;
316 #endif
317 }
318
319 (*lp)->line[*x].cflags &= ~ (CA_BACKGROUND | CA_CONTINUATION);
320
321 if (attributes & __COLOR)
322 (*lp)->line[*x].attr =
323 attributes | (win->battr & ~__COLOR);
324 else
325 (*lp)->line[*x].attr = attributes | win->battr;
326
327 if (*x == win->maxx - 1)
328 (*lp)->flags |= __ISPASTEOL;
329 else
330 (*x)++;
331
332 __CTRACE(__CTRACE_INPUT,
333 "ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n",
334 *y, *x, *win->alines[*y]->firstchp, *win->alines[*y]->lastchp);
335 __sync(win);
336 return OK;
337 }
338
339 /*
340 * _cursesi_addwchar -
341 * Internal function to add a wide character and update the row
342 * and column positions.
343 */
344 int
345 _cursesi_addwchar(WINDOW *win, __LINE **lnp, int *y, int *x,
346 const cchar_t *wch, int char_interp)
347 {
348 #ifndef HAVE_WCHAR
349 return ERR;
350 #else
351 int sx = 0, ex = 0, cw = 0, i = 0, newx = 0, tabsize;
352 __LDATA *lp = &win->alines[*y]->line[*x], *tp = NULL;
353 nschar_t *np = NULL;
354 cchar_t cc;
355 attr_t attributes;
356
357 if (char_interp) {
358 /* special characters handling */
359 switch (wch->vals[0]) {
360 case L'\b':
361 if (--*x < 0)
362 *x = 0;
363 return OK;
364 case L'\r':
365 *x = 0;
366 return OK;
367 case L'\n':
368 if (*y == win->scr_b) {
369 if (!(win->flags & __SCROLLOK))
370 return ERR;
371 wclrtoeol(win);
372 scroll(win);
373 } else {
374 wclrtoeol(win);
375 (*y)++;
376 }
377 *x = 0;
378 (*lnp)->flags &= ~__ISPASTEOL;
379 return OK;
380 case L'\t':
381 cc.vals[0] = L' ';
382 cc.elements = 1;
383 cc.attributes = win->wattr;
384 tabsize = win->screen->TABSIZE;
385 newx = tabsize - (*x % tabsize);
386
387 /* if at the bottom of the window and
388 not allowed to scroll then just do
389 what we can */
390 if ((*y == win->scr_b) &&
391 !(win->flags & __SCROLLOK)) {
392 if ((*lnp)->flags & __ISPASTEOL) {
393 return ERR;
394 }
395
396 if (*x + newx > win->maxx - 1)
397 newx = win->maxx - *x - 1;
398 }
399
400 for (i = 0; i < newx; i++) {
401 if (wadd_wch(win, &cc) == ERR)
402 return ERR;
403 }
404 return OK;
405 }
406 }
407
408 /* check for non-spacing character */
409 if (!wcwidth(wch->vals[0])) {
410 __CTRACE(__CTRACE_INPUT,
411 "_cursesi_addwchar: char '%c' is non-spacing\n",
412 wch->vals[0]);
413 cw = (*lp).wcols;
414 if (cw < 0) {
415 lp += cw;
416 *x += cw;
417 }
418 for (i = 0; i < wch->elements; i++) {
419 if (!(np = (nschar_t *) malloc(sizeof(nschar_t))))
420 return ERR;
421 np->ch = wch->vals[i];
422 np->next = lp->nsp;
423 lp->nsp = np;
424 }
425 (*lnp)->flags |= __ISDIRTY;
426 newx = *x + win->ch_off;
427 if (newx < *(*lnp)->firstchp)
428 *(*lnp)->firstchp = newx;
429
430 if (newx > *(*lnp)->lastchp)
431 *(*lnp)->lastchp = newx;
432 __touchline(win, *y, *x, *x);
433 return OK;
434 }
435 /* check for new line first */
436 if (char_interp && ((*lnp)->flags & __ISPASTEOL)) {
437 *x = 0;
438 (*lnp)->flags &= ~__ISPASTEOL;
439 if (*y == win->scr_b) {
440 if (!(win->flags & __SCROLLOK))
441 return ERR;
442 scroll(win);
443 } else {
444 (*y)++;
445 }
446 (*lnp) = win->alines[*y];
447 lp = &win->alines[*y]->line[*x];
448 }
449 /* clear out the current character */
450 cw = (*lp).wcols;
451 if (cw >= 0) {
452 sx = *x;
453 } else {
454 for (sx = *x - 1; sx >= max(*x + cw, 0); sx--) {
455 __CTRACE(__CTRACE_INPUT,
456 "_cursesi_addwchar: clear current char (%d,%d)\n",
457 *y, sx);
458 tp = &win->alines[*y]->line[sx];
459 tp->ch = win->bch;
460 tp->cflags = CA_BACKGROUND;
461 if (_cursesi_copy_nsp(win->bnsp, tp) == ERR)
462 return ERR;
463
464 tp->attr = win->battr;
465 tp->wcols = win->wcols;
466 }
467 sx = *x + cw;
468 (*lnp)->flags |= __ISDIRTY;
469 newx = sx + win->ch_off;
470 if (newx < *(*lnp)->firstchp)
471 *(*lnp)->firstchp = newx;
472 }
473
474 /* check for enough space before the end of line */
475 cw = wcwidth(wch->vals[0]);
476 if (cw < 0)
477 cw = 1;
478
479 if (cw > win->maxx - *x) {
480 __CTRACE(__CTRACE_INPUT,
481 "_cursesi_addwchar: clear EOL (%d,%d)\n", *y, *x);
482 if (*y == win->scr_b) {
483 if (!(win->flags & __SCROLLOK))
484 return ERR;
485 scroll(win);
486 }
487
488 (*lnp)->flags |= __ISDIRTY;
489 newx = *x + win->ch_off;
490 if (newx < *(*lnp)->firstchp)
491 *(*lnp)->firstchp = newx;
492
493 for (tp = lp; *x < win->maxx; tp++, (*x)++) {
494 tp->ch = win->bch;
495 if (_cursesi_copy_nsp(win->bnsp, tp) == ERR)
496 return ERR;
497 tp->attr = win->battr;
498 tp->wcols = win->wcols;
499 tp->cflags = CA_BACKGROUND;
500 }
501 newx = win->maxx - 1 + win->ch_off;
502 if (newx > *(*lnp)->lastchp)
503 *(*lnp)->lastchp = newx;
504 __touchline(win, *y, sx, (int) win->maxx - 1);
505 sx = *x = 0;
506 if (*y != win->scr_b) {
507 (*y)++;
508 }
509 lp = &win->alines[*y]->line[0];
510 (*lnp) = win->alines[*y];
511 }
512
513 /* add spacing character */
514 __CTRACE(__CTRACE_INPUT,
515 "_cursesi_addwchar: add character (%d,%d) 0x%x\n",
516 *y, *x, wch->vals[0]);
517 (*lnp)->flags |= __ISDIRTY;
518 newx = *x + win->ch_off;
519 if (newx < *(*lnp)->firstchp)
520 *(*lnp)->firstchp = newx;
521
522 if (lp->nsp) {
523 __cursesi_free_nsp(lp->nsp);
524 lp->nsp = NULL;
525 }
526
527 lp->ch = wch->vals[0];
528 lp->cflags &= ~ (CA_BACKGROUND | CA_CONTINUATION);
529
530 attributes = (win->wattr | wch->attributes)
531 & (WA_ATTRIBUTES & ~__COLOR);
532 if (wch->attributes & __COLOR)
533 attributes |= wch->attributes & __COLOR;
534 else if (win->wattr & __COLOR)
535 attributes |= win->wattr & __COLOR;
536 if (attributes & __COLOR)
537 lp->attr = attributes | (win->battr & ~__COLOR);
538 else
539 lp->attr = attributes | win->battr;
540
541 lp->wcols = cw;
542
543 __CTRACE(__CTRACE_INPUT,
544 "_cursesi_addwchar: add spacing char 0x%x, attr 0x%x, width %d\n",
545 lp->ch, lp->attr, lp->wcols);
546
547 if (wch->elements > 1) {
548 for (i = 1; i < wch->elements; i++) {
549 np = malloc(sizeof(nschar_t));
550 if (!np)
551 return ERR;
552 np->ch = wch->vals[i];
553 np->next = lp->nsp;
554 __CTRACE(__CTRACE_INPUT,
555 "_cursesi_addwchar: add non-spacing char 0x%x\n",
556 np->ch);
557 lp->nsp = np;
558 }
559 }
560 __CTRACE(__CTRACE_INPUT,
561 "_cursesi_addwchar: non-spacing list header: %p\n", lp->nsp);
562 __CTRACE(__CTRACE_INPUT,
563 "_cursesi_addwchar: add rest columns (%d:%d)\n",
564 sx + 1, sx + cw - 1);
565 __CTRACE(__CTRACE_INPUT, "_cursesi_addwchar: *x = %d, win->maxx = %d\n",
566 *x, win->maxx);
567 for (tp = lp + 1, *x = sx + 1, i = cw - 1; i > 0; tp++, (*x)++, i--) {
568 __CTRACE(__CTRACE_INPUT,
569 "_cursesi_addwchar: setting continuation at x %d\n", *x);
570 if (tp->nsp) {
571 __cursesi_free_nsp(tp->nsp);
572 tp->nsp = NULL;
573 }
574 tp->ch = wch->vals[0];
575 tp->attr = lp->attr & WA_ATTRIBUTES;
576 /* Mark as "continuation" cell */
577 tp->cflags |= CA_CONTINUATION;
578 tp->cflags &= ~ CA_BACKGROUND;
579 tp->wcols = i;
580 }
581
582 if (*x >= win->maxx) {
583 __CTRACE(__CTRACE_INPUT, "_cursesi_addwchar: do line wrap\n");
584 if (*y == win->scr_b) {
585 __CTRACE(__CTRACE_INPUT,
586 "_cursesi_addwchar: at bottom of screen\n");
587 if (!(win->flags & __SCROLLOK))
588 return ERR;
589 __CTRACE(__CTRACE_INPUT,
590 "_cursesi_addwchar: do a scroll\n");
591 scroll(win);
592 }
593 newx = win->maxx - 1 + win->ch_off;
594 if (newx > *(*lnp)->lastchp)
595 *(*lnp)->lastchp = newx;
596 __touchline(win, *y, sx, (int) win->maxx - 1);
597 *x = sx = 0;
598 if (*y != win->scr_b) {
599 (*y)++;
600 }
601 lp = &win->alines[*y]->line[0];
602 (*lnp) = win->alines[*y];
603 *(*lnp)->lastchp = win->ch_off + win->maxx - 1;
604 } else {
605 /* clear the remaining of the current character */
606 if (*x && *x < win->maxx) {
607 ex = sx + cw;
608 tp = &win->alines[*y]->line[ex];
609 while (ex < win->maxx && tp->wcols < 0) {
610 __CTRACE(__CTRACE_INPUT,
611 "_cursesi_addwchar: clear "
612 "remaining of current char (%d,%d)nn",
613 *y, ex);
614 tp->ch = win->bch;
615 tp->cflags = CA_BACKGROUND;
616 if (_cursesi_copy_nsp(win->bnsp, tp) == ERR)
617 return ERR;
618 tp->attr = win->battr;
619 tp->wcols = win->wcols;
620 tp++, ex++;
621 }
622 newx = ex - 1 + win->ch_off;
623 if (newx > *(*lnp)->lastchp)
624 *(*lnp)->lastchp = newx;
625 __touchline(win, *y, sx, ex - 1);
626 }
627 }
628
629 __CTRACE(__CTRACE_INPUT, "_cursesi_addwchar: %d : 0x%x\n",
630 lp->ch, lp->attr);
631 __CTRACE(__CTRACE_INPUT,
632 "_cursesi_addwchar: *x = %d, *y = %d, win->maxx = %d\n",
633 *x, *y, win->maxx);
634 __sync(win);
635 return OK;
636 #endif
637 }
638