1 /* $NetBSD: ins_wch.c,v 1.21 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: ins_wch.c,v 1.21 2024/12/23 02:58:03 blymn Exp $"); 40 #endif /* not lint */ 41 42 #include <string.h> 43 #include <stdlib.h> 44 45 #include "curses.h" 46 #include "curses_private.h" 47 48 /* 49 * ins_wch -- 50 * Do an insert-char on the line, leaving (cury, curx) unchanged. 51 */ 52 int 53 ins_wch(const cchar_t *wch) 54 { 55 return wins_wch(stdscr, wch); 56 } 57 58 /* 59 * mvins_wch -- 60 * Do an insert-char on the line at (y, x). 61 */ 62 int 63 mvins_wch(int y, int x, const cchar_t *wch) 64 { 65 return mvwins_wch(stdscr, y, x, wch); 66 } 67 68 /* 69 * mvwins_wch -- 70 * Do an insert-char on the line at (y, x) in the given window. 71 */ 72 int 73 mvwins_wch(WINDOW *win, int y, int x, const cchar_t *wch) 74 { 75 if (wmove(win, y, x) == ERR) 76 return ERR; 77 78 return wins_wch(win, wch); 79 } 80 81 /* 82 * wins_wch -- 83 * Do an insert-char on the line, leaving (cury, curx) unchanged. 84 */ 85 int 86 wins_wch(WINDOW *win, const cchar_t *wch) 87 { 88 __LDATA *start, *temp1, *temp2; 89 __LINE *lnp; 90 int cw, pcw, x, y, sx, ex, newx, i; 91 nschar_t *np, *tnp; 92 wchar_t ws[] = L" "; 93 94 if (__predict_false(win == NULL)) 95 return ERR; 96 97 /* check for non-spacing characters */ 98 if (!wch) 99 return OK; 100 cw = wcwidth(wch->vals[0]); 101 __CTRACE(__CTRACE_INPUT, "wins_wch: wcwidth %d\n", cw); 102 if (cw < 0) 103 cw = 1; 104 if (!cw) 105 return wadd_wch( win, wch ); 106 107 x = win->curx; 108 y = win->cury; 109 __CTRACE(__CTRACE_INPUT, "wins_wch: (%d,%d)\n", y, x); 110 switch (wch->vals[0]) { 111 case L'\b': 112 if (--x < 0) 113 x = 0; 114 win->curx = x; 115 return OK; 116 case L'\r': 117 win->curx = 0; 118 return OK; 119 case L'\n': 120 wclrtoeol(win); 121 if (y == win->scr_b) { 122 if (!(win->flags & __SCROLLOK)) 123 return ERR; 124 scroll(win); 125 } 126 return OK; 127 case L'\t': 128 if (wins_nwstr(win, ws, min(win->maxx - x, 129 TABSIZE - (x % TABSIZE))) == ERR) 130 return ERR; 131 return OK; 132 } 133 134 /* locate current cell */ 135 x = win->curx; 136 y = win->cury; 137 lnp = win->alines[y]; 138 start = &win->alines[y]->line[x]; 139 sx = x; 140 pcw = start->wcols; 141 if (pcw < 0) { 142 start += pcw; 143 sx += pcw; 144 } 145 if (cw > win->maxx - sx) 146 return ERR; 147 lnp->flags |= __ISDIRTY; 148 newx = sx + win->ch_off; 149 if (newx < *lnp->firstchp) 150 *lnp->firstchp = newx; 151 152 /* shift all complete characters */ 153 __CTRACE(__CTRACE_INPUT, "wins_wch: shift all characters\n"); 154 temp1 = &win->alines[y]->line[win->maxx - 1]; 155 temp2 = temp1 - cw; 156 pcw = (temp2 + 1)->wcols; 157 if (pcw < 0) { 158 __CTRACE(__CTRACE_INPUT, "wins_wch: clear EOL\n"); 159 temp2 += pcw; 160 while (temp1 > temp2 + cw) { 161 np = temp1->nsp; 162 if (np) { 163 while (np) { 164 tnp = np->next; 165 free(np); 166 np = tnp; 167 } 168 temp1->nsp = NULL; 169 } 170 temp1->ch = win->bch; 171 if (_cursesi_copy_nsp(win->bnsp, temp1) == ERR) 172 return ERR; 173 temp1->attr = win->battr; 174 temp1->wcols = 1; 175 temp1--; 176 } 177 } 178 while (temp2 >= start) { 179 (void)memcpy(temp1, temp2, sizeof(__LDATA)); 180 temp1--, temp2--; 181 } 182 183 /* update character under cursor */ 184 start->nsp = NULL; 185 start->ch = wch->vals[0]; 186 start->attr = wch->attributes & WA_ATTRIBUTES; 187 start->wcols = cw; 188 if (wch->elements > 1) { 189 for (i = 1; i < wch->elements; i++) { 190 np = malloc(sizeof(nschar_t)); 191 if (!np) 192 return ERR; 193 np->ch = wch->vals[i]; 194 np->next = start->nsp; 195 start->nsp = np; 196 } 197 } 198 __CTRACE(__CTRACE_INPUT, "wins_wch: insert (%x,%x,%p)\n", 199 start->ch, start->attr, start->nsp); 200 temp1 = start + 1; 201 ex = x + 1; 202 while (ex - x < cw) { 203 temp1->ch = wch->vals[0]; 204 temp1->wcols = x - ex; 205 temp1->nsp = NULL; 206 temp1->cflags |= CA_CONTINUATION; 207 ex++, temp1++; 208 } 209 210 newx = win->maxx - 1 + win->ch_off; 211 if (newx > *lnp->lastchp) 212 *lnp->lastchp = newx; 213 __touchline(win, y, sx, (int)win->maxx - 1); 214 __sync(win); 215 return OK; 216 } 217