1 /* $NetBSD: add_wchstr.c,v 1.14 2022/10/19 06:09:27 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.14 2022/10/19 06:09:27 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 (!wchstr) 149 return OK; 150 151 /* compute length of the cchar string */ 152 if (n < -1) 153 return ERR; 154 if (n >= 0) 155 for (chp = wchstr, cnt = 0; n && chp->vals[0]; 156 n--, chp++, ++cnt); 157 else 158 for (chp = wchstr, cnt = 0; chp->vals[0]; chp++, ++cnt); 159 __CTRACE(__CTRACE_INPUT, "wadd_wchnstr: len=%d\n", cnt); 160 chp = wchstr; 161 x = win->curx; 162 y = win->cury; 163 lp = &win->alines[y]->line[x]; 164 lnp = win->alines[y]; 165 166 cw = (*lp).wcols; 167 if (cw >= 0) { 168 sx = x; 169 } else { 170 if (wcwidth(chp->vals[0])) { 171 /* clear the partial character before cursor */ 172 for (tp = lp + cw; tp < lp; tp++) { 173 tp->ch = win->bch; 174 if (_cursesi_copy_nsp(win->bnsp, tp) == ERR) 175 return ERR; 176 tp->attr = win->battr; 177 tp->wcols = 1; 178 tp->cflags = CA_BACKGROUND; 179 np = tp->nsp; 180 } 181 } else { 182 /* move to the start of current char */ 183 lp += cw; 184 x += cw; 185 } 186 sx = x + cw; 187 } 188 lnp->flags |= __ISDIRTY; 189 newx = sx + win->ch_off; 190 if (newx < *lnp->firstchp) 191 *lnp->firstchp = newx; 192 193 /* add characters in the string */ 194 ex = x; 195 while (cnt) { 196 x = ex; 197 wc = chp->vals[0]; 198 __CTRACE(__CTRACE_INPUT, "wadd_wchnstr: adding %x", wc); 199 cw = wcwidth(wc); 200 if (cw < 0) 201 cw = 1; 202 if (cw) { 203 /* spacing character */ 204 __CTRACE(__CTRACE_INPUT, 205 " as a spacing char(width=%d)\n", cw); 206 if (cw > win->maxx - ex) { 207 /* clear to EOL */ 208 while (ex < win->maxx) { 209 lp->ch = win->bch; 210 if (_cursesi_copy_nsp(win->bnsp, lp) 211 == ERR) 212 return ERR; 213 lp->attr = win->battr; 214 lp->cflags = CA_BACKGROUND; 215 (*lp).wcols = 1; 216 lp++, ex++; 217 } 218 ex = win->maxx - 1; 219 break; 220 } 221 /* this could combine with the insertion of 222 * non-spacing char */ 223 np = lp->nsp; 224 if (np) { 225 while (np) { 226 tnp = np->next; 227 free(np); 228 np = tnp; 229 } 230 lp->nsp = NULL; 231 } 232 lp->ch = chp->vals[0]; 233 lp->attr = chp->attributes & WA_ATTRIBUTES; 234 lp->cflags &= ~CA_BACKGROUND; 235 (*lp).wcols = cw; 236 if (chp->elements > 1) { 237 for (i = 1; i < chp->elements; i++) { 238 np = (nschar_t *) 239 malloc(sizeof(nschar_t)); 240 if (!np) 241 return ERR; 242 np->ch = chp->vals[i]; 243 np->next = lp->nsp; 244 lp->nsp = np; 245 } 246 } 247 lp++, ex++; 248 __CTRACE(__CTRACE_INPUT, 249 "wadd_wchnstr: ex = %d, x = %d, cw = %d\n", 250 ex, x, cw); 251 while (ex - x <= cw - 1) { 252 np = lp->nsp; 253 if (np) { 254 while (np) { 255 tnp = np->next; 256 free(np); 257 np = tnp; 258 } 259 lp->nsp = NULL; 260 } 261 lp->ch = chp->vals[0]; 262 lp->attr = chp->attributes & WA_ATTRIBUTES; 263 lp->cflags &= ~CA_BACKGROUND; 264 lp->cflags |= CA_CONTINUATION; 265 (*lp).wcols = x - ex; 266 lp++, ex++; 267 } 268 } else { 269 /* non-spacing character */ 270 __CTRACE(__CTRACE_INPUT, 271 "wadd_wchnstr: as non-spacing char"); 272 for (i = 0; i < chp->elements; i++) { 273 np = malloc(sizeof(nschar_t)); 274 if (!np) 275 return ERR; 276 np->ch = chp->vals[i]; 277 np->next = lp->nsp; 278 lp->nsp = np; 279 } 280 } 281 cnt--, chp++; 282 } 283 #ifdef DEBUG 284 for (i = sx; i < ex; i++) { 285 __CTRACE(__CTRACE_INPUT, "wadd_wchnstr: (%d,%d)=(%x,%x,%p)\n", 286 win->cury, i, win->alines[win->cury]->line[i].ch, 287 win->alines[win->cury]->line[i].attr, 288 win->alines[win->cury]->line[i].nsp); 289 } 290 #endif /* DEBUG */ 291 lnp->flags |= __ISDIRTY; 292 newx = ex + win->ch_off; 293 if (newx > *lnp->lastchp) 294 *lnp->lastchp = newx; 295 __touchline(win, y, sx, ex); 296 297 return OK; 298 } 299