1 /* $NetBSD: add_wchstr.c,v 1.15 2024/12/23 02:58:03 blymn Exp $ */ 2 3 /* 4 * Copyright (c) 2005 The NetBSD Foundation Inc. 5 * All rights reserved. 6 * 7 * This code is derived from code donated to the NetBSD Foundation 8 * by Ruibiao Qiu <ruibiao@arl.wustl.edu,ruibiao@gmail.com>. 9 * 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the NetBSD Foundation nor the names of its 20 * contributors may be used to endorse or promote products derived 21 * from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 24 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #include <sys/cdefs.h> 38 #ifndef lint 39 __RCSID("$NetBSD: add_wchstr.c,v 1.15 2024/12/23 02:58:03 blymn Exp $"); 40 #endif /* not lint */ 41 42 #include <stdlib.h> 43 44 #include "curses.h" 45 #include "curses_private.h" 46 47 /* 48 * add_wchstr -- 49 * Add a wide string to stdscr starting at (_cury, _curx). 50 */ 51 int 52 add_wchstr(const cchar_t *wchstr) 53 { 54 return wadd_wchnstr(stdscr, wchstr, -1); 55 } 56 57 58 /* 59 * wadd_wchstr -- 60 * Add a string to the given window starting at (_cury, _curx). 61 */ 62 int 63 wadd_wchstr(WINDOW *win, const cchar_t *wchstr) 64 { 65 return wadd_wchnstr(win, wchstr, -1); 66 } 67 68 69 /* 70 * add_wchnstr -- 71 * Add a string (at most n characters) to stdscr starting 72 * at (_cury, _curx). If n is negative, add the entire string. 73 */ 74 int 75 add_wchnstr(const cchar_t *wchstr, int n) 76 { 77 return wadd_wchnstr(stdscr, wchstr, n); 78 } 79 80 81 /* 82 * mvadd_wchstr -- 83 * Add a string to stdscr starting at (y, x) 84 */ 85 int 86 mvadd_wchstr(int y, int x, const cchar_t *wchstr) 87 { 88 return mvwadd_wchnstr(stdscr, y, x, wchstr, -1); 89 } 90 91 92 /* 93 * mvwadd_wchstr -- 94 * Add a string to the given window starting at (y, x) 95 */ 96 int 97 mvwadd_wchstr(WINDOW *win, int y, int x, const cchar_t *wchstr) 98 { 99 return mvwadd_wchnstr(win, y, x, wchstr, -1); 100 } 101 102 103 /* 104 * mvadd_wchnstr -- 105 * Add a string of at most n characters to stdscr 106 * starting at (y, x). 107 */ 108 int 109 mvadd_wchnstr(int y, int x, const cchar_t *wchstr, int n) 110 { 111 return mvwadd_wchnstr(stdscr, y, x, wchstr, n); 112 } 113 114 115 /* 116 * mvwadd_wchnstr -- 117 * Add a string of at most n characters to the given window 118 * starting at (y, x). 119 */ 120 int 121 mvwadd_wchnstr(WINDOW *win, int y, int x, const cchar_t *wchstr, int n) 122 { 123 if (wmove(win, y, x) == ERR) 124 return ERR; 125 126 return wadd_wchnstr(win, wchstr, n); 127 } 128 129 130 /* 131 * wadd_wchnstr -- 132 * Add a string (at most n wide characters) to the given window 133 * starting at (_cury, _curx). If n is -1, add the entire string. 134 */ 135 int 136 wadd_wchnstr(WINDOW *win, const cchar_t *wchstr, int n) 137 { 138 const cchar_t *chp; 139 wchar_t wc; 140 int cw, x, y, sx, ex, newx, i, cnt; 141 __LDATA *lp, *tp; 142 nschar_t *np, *tnp; 143 __LINE *lnp; 144 145 __CTRACE(__CTRACE_INPUT, 146 "wadd_wchnstr: win = %p, wchstr = %p, n = %d\n", win, wchstr, n); 147 148 if (__predict_false(win == NULL)) 149 return ERR; 150 151 if (!wchstr) 152 return OK; 153 154 /* compute length of the cchar string */ 155 if (n < -1) 156 return ERR; 157 if (n >= 0) 158 for (chp = wchstr, cnt = 0; n && chp->vals[0]; 159 n--, chp++, ++cnt); 160 else 161 for (chp = wchstr, cnt = 0; chp->vals[0]; chp++, ++cnt); 162 __CTRACE(__CTRACE_INPUT, "wadd_wchnstr: len=%d\n", cnt); 163 chp = wchstr; 164 x = win->curx; 165 y = win->cury; 166 lp = &win->alines[y]->line[x]; 167 lnp = win->alines[y]; 168 169 cw = (*lp).wcols; 170 if (cw >= 0) { 171 sx = x; 172 } else { 173 if (wcwidth(chp->vals[0])) { 174 /* clear the partial character before cursor */ 175 for (tp = lp + cw; tp < lp; tp++) { 176 tp->ch = win->bch; 177 if (_cursesi_copy_nsp(win->bnsp, tp) == ERR) 178 return ERR; 179 tp->attr = win->battr; 180 tp->wcols = 1; 181 tp->cflags = CA_BACKGROUND; 182 np = tp->nsp; 183 } 184 } else { 185 /* move to the start of current char */ 186 lp += cw; 187 x += cw; 188 } 189 sx = x + cw; 190 } 191 lnp->flags |= __ISDIRTY; 192 newx = sx + win->ch_off; 193 if (newx < *lnp->firstchp) 194 *lnp->firstchp = newx; 195 196 /* add characters in the string */ 197 ex = x; 198 while (cnt) { 199 x = ex; 200 wc = chp->vals[0]; 201 __CTRACE(__CTRACE_INPUT, "wadd_wchnstr: adding %x", wc); 202 cw = wcwidth(wc); 203 if (cw < 0) 204 cw = 1; 205 if (cw) { 206 /* spacing character */ 207 __CTRACE(__CTRACE_INPUT, 208 " as a spacing char(width=%d)\n", cw); 209 if (cw > win->maxx - ex) { 210 /* clear to EOL */ 211 while (ex < win->maxx) { 212 lp->ch = win->bch; 213 if (_cursesi_copy_nsp(win->bnsp, lp) 214 == ERR) 215 return ERR; 216 lp->attr = win->battr; 217 lp->cflags = CA_BACKGROUND; 218 (*lp).wcols = 1; 219 lp++, ex++; 220 } 221 ex = win->maxx - 1; 222 break; 223 } 224 /* this could combine with the insertion of 225 * non-spacing char */ 226 np = lp->nsp; 227 if (np) { 228 while (np) { 229 tnp = np->next; 230 free(np); 231 np = tnp; 232 } 233 lp->nsp = NULL; 234 } 235 lp->ch = chp->vals[0]; 236 lp->attr = chp->attributes & WA_ATTRIBUTES; 237 lp->cflags &= ~CA_BACKGROUND; 238 (*lp).wcols = cw; 239 if (chp->elements > 1) { 240 for (i = 1; i < chp->elements; i++) { 241 np = (nschar_t *) 242 malloc(sizeof(nschar_t)); 243 if (!np) 244 return ERR; 245 np->ch = chp->vals[i]; 246 np->next = lp->nsp; 247 lp->nsp = np; 248 } 249 } 250 lp++, ex++; 251 __CTRACE(__CTRACE_INPUT, 252 "wadd_wchnstr: ex = %d, x = %d, cw = %d\n", 253 ex, x, cw); 254 while (ex - x <= cw - 1) { 255 np = lp->nsp; 256 if (np) { 257 while (np) { 258 tnp = np->next; 259 free(np); 260 np = tnp; 261 } 262 lp->nsp = NULL; 263 } 264 lp->ch = chp->vals[0]; 265 lp->attr = chp->attributes & WA_ATTRIBUTES; 266 lp->cflags &= ~CA_BACKGROUND; 267 lp->cflags |= CA_CONTINUATION; 268 (*lp).wcols = x - ex; 269 lp++, ex++; 270 } 271 } else { 272 /* non-spacing character */ 273 __CTRACE(__CTRACE_INPUT, 274 "wadd_wchnstr: as non-spacing char"); 275 for (i = 0; i < chp->elements; i++) { 276 np = malloc(sizeof(nschar_t)); 277 if (!np) 278 return ERR; 279 np->ch = chp->vals[i]; 280 np->next = lp->nsp; 281 lp->nsp = np; 282 } 283 } 284 cnt--, chp++; 285 } 286 #ifdef DEBUG 287 for (i = sx; i < ex; i++) { 288 __CTRACE(__CTRACE_INPUT, "wadd_wchnstr: (%d,%d)=(%x,%x,%p)\n", 289 win->cury, i, win->alines[win->cury]->line[i].ch, 290 win->alines[win->cury]->line[i].attr, 291 win->alines[win->cury]->line[i].nsp); 292 } 293 #endif /* DEBUG */ 294 lnp->flags |= __ISDIRTY; 295 newx = ex + win->ch_off; 296 if (newx > *lnp->lastchp) 297 *lnp->lastchp = newx; 298 __touchline(win, y, sx, ex); 299 300 return OK; 301 } 302