add_wchstr.c revision 1.10 1 /* $NetBSD: add_wchstr.c,v 1.10 2021/09/06 07:45:48 rin 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 (at) arl.wustl.edu,ruibiao (at) 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.10 2021/09/06 07:45:48 rin 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 = WCOL(*lp);
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 = (wchar_t) btowc((int) win->bch);
174 if (_cursesi_copy_nsp(win->bnsp, tp) == ERR)
175 return ERR;
176 tp->attr = win->battr;
177 SET_WCOL(*tp, 1);
178 np = tp->nsp;
179 }
180 } else {
181 /* move to the start of current char */
182 lp += cw;
183 x += cw;
184 }
185 sx = x + cw;
186 }
187 lnp->flags |= __ISDIRTY;
188 newx = sx + win->ch_off;
189 if (newx < *lnp->firstchp)
190 *lnp->firstchp = newx;
191
192 /* add characters in the string */
193 ex = x;
194 while (cnt) {
195 x = ex;
196 wc = chp->vals[0];
197 __CTRACE(__CTRACE_INPUT, "wadd_wchnstr: adding %x", wc);
198 cw = wcwidth(wc);
199 if (cw < 0)
200 cw = 1;
201 if (cw) {
202 /* spacing character */
203 __CTRACE(__CTRACE_INPUT,
204 " as a spacing char(width=%d)\n", cw);
205 if (cw > win->maxx - ex) {
206 /* clear to EOL */
207 while (ex < win->maxx) {
208 lp->ch = (wchar_t)
209 btowc((int) win->bch);
210 if (_cursesi_copy_nsp(win->bnsp, lp)
211 == ERR)
212 return ERR;
213 lp->attr = win->battr;
214 SET_WCOL(*lp, 1);
215 lp++, ex++;
216 }
217 ex = win->maxx - 1;
218 break;
219 }
220 /* this could combine with the insertion of
221 * non-spacing char */
222 np = lp->nsp;
223 if (np) {
224 while (np) {
225 tnp = np->next;
226 free(np);
227 np = tnp;
228 }
229 lp->nsp = NULL;
230 }
231 lp->ch = chp->vals[0];
232 lp->attr = chp->attributes & WA_ATTRIBUTES;
233 SET_WCOL(*lp, cw);
234 if (chp->elements > 1) {
235 for (i = 1; i < chp->elements; i++) {
236 np = (nschar_t *)
237 malloc(sizeof(nschar_t));
238 if (!np)
239 return ERR;
240 np->ch = chp->vals[i];
241 np->next = lp->nsp;
242 lp->nsp = np;
243 }
244 }
245 lp++, ex++;
246 __CTRACE(__CTRACE_INPUT,
247 "wadd_wchnstr: ex = %d, x = %d, cw = %d\n",
248 ex, x, cw);
249 while (ex - x <= cw - 1) {
250 np = lp->nsp;
251 if (np) {
252 while (np) {
253 tnp = np->next;
254 free(np);
255 np = tnp;
256 }
257 lp->nsp = NULL;
258 }
259 lp->ch = chp->vals[0];
260 lp->attr = chp->attributes & WA_ATTRIBUTES;
261 SET_WCOL(*lp, x - ex);
262 lp++, ex++;
263 }
264 } else {
265 /* non-spacing character */
266 __CTRACE(__CTRACE_INPUT,
267 "wadd_wchnstr: as non-spacing char");
268 for (i = 0; i < chp->elements; i++) {
269 np = malloc(sizeof(nschar_t));
270 if (!np)
271 return ERR;
272 np->ch = chp->vals[i];
273 np->next = lp->nsp;
274 lp->nsp = np;
275 }
276 }
277 cnt--, chp++;
278 }
279 #ifdef DEBUG
280 for (i = sx; i < ex; i++) {
281 __CTRACE(__CTRACE_INPUT, "wadd_wchnstr: (%d,%d)=(%x,%x,%p)\n",
282 win->cury, i, win->alines[win->cury]->line[i].ch,
283 win->alines[win->cury]->line[i].attr,
284 win->alines[win->cury]->line[i].nsp);
285 }
286 #endif /* DEBUG */
287 lnp->flags |= __ISDIRTY;
288 newx = ex + win->ch_off;
289 if (newx > *lnp->lastchp)
290 *lnp->lastchp = newx;
291 __touchline(win, y, sx, ex);
292
293 return OK;
294 }
295