ins_wstr.c revision 1.14 1 /* $NetBSD: ins_wstr.c,v 1.14 2019/06/09 07:40:14 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 (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: ins_wstr.c,v 1.14 2019/06/09 07:40:14 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_wstr --
50 * insert a multi-character wide-character string into the current window
51 */
52 int
53 ins_wstr(const wchar_t *wstr)
54 {
55 return wins_wstr(stdscr, wstr);
56 }
57
58 /*
59 * ins_nwstr --
60 * insert a multi-character wide-character string into the current window
61 * with at most n characters
62 */
63 int
64 ins_nwstr(const wchar_t *wstr, int n)
65 {
66 return wins_nwstr(stdscr, wstr, n);
67 }
68
69 /*
70 * mvins_wstr --
71 * Do an insert-string on the line at (y, x).
72 */
73 int
74 mvins_wstr(int y, int x, const wchar_t *wstr)
75 {
76 return mvwins_wstr(stdscr, y, x, wstr);
77 }
78
79 /*
80 * mvins_nwstr --
81 * Do an insert-n-string on the line at (y, x).
82 */
83 int
84 mvins_nwstr(int y, int x, const wchar_t *wstr, int n)
85 {
86 return mvwins_nwstr(stdscr, y, x, wstr, n);
87 }
88
89 /*
90 * mvwins_wstr --
91 * Do an insert-string on the line at (y, x) in the given window.
92 */
93 int
94 mvwins_wstr(WINDOW *win, int y, int x, const wchar_t *wstr)
95 {
96 if (wmove(win, y, x) == ERR)
97 return ERR;
98
99 return wins_wstr(stdscr, wstr);
100 }
101
102 /*
103 * mvwins_nwstr --
104 * Do an insert-n-string on the line at (y, x) in the given window.
105 */
106 int
107 mvwins_nwstr(WINDOW *win, int y, int x, const wchar_t *wstr, int n)
108 {
109 if (wmove(win, y, x) == ERR)
110 return ERR;
111
112 return wins_nwstr(stdscr, wstr, n);
113 }
114
115
116 /*
117 * wins_wstr --
118 * Do an insert-string on the line, leaving (cury, curx) unchanged.
119 */
120 int
121 wins_wstr(WINDOW *win, const wchar_t *wstr)
122 {
123 return wins_nwstr(win, wstr, -1);
124 }
125
126 /*
127 * wins_nwstr --
128 * Do an insert-n-string on the line, leaving (cury, curx) unchanged.
129 */
130 int
131 wins_nwstr(WINDOW *win, const wchar_t *wstr, int n)
132 {
133 __LDATA *start, *temp1, *temp2;
134 __LINE *lnp;
135 const wchar_t *scp;
136 int width, len, sx, x, y, cw, pcw, newx;
137 nschar_t *np;
138 wchar_t ws[] = L" ";
139
140 /* check for leading non-spacing character */
141 if (!wstr)
142 return OK;
143 cw = wcwidth(*wstr);
144 if (cw < 0)
145 cw = 1;
146 if (!cw)
147 return ERR;
148
149 scp = wstr + 1;
150 width = cw;
151 len = 1;
152 n--;
153 while (*scp) {
154 int w;
155 if (!n)
156 break;
157 w = wcwidth(*scp);
158 if (w < 0)
159 w = 1;
160 n--, len++, width += w;
161 scp++;
162 }
163 #ifdef DEBUG
164 __CTRACE(__CTRACE_INPUT, "wins_nwstr: width=%d,len=%d\n", width, len);
165 #endif /* DEBUG */
166
167 if (cw > win->maxx - win->curx + 1)
168 return ERR;
169 start = &win->alines[win->cury]->line[win->curx];
170 sx = win->curx;
171 lnp = win->alines[win->cury];
172 pcw = WCOL(*start);
173 if (pcw < 0) {
174 sx += pcw;
175 start += pcw;
176 }
177 #ifdef DEBUG
178 __CTRACE(__CTRACE_INPUT, "wins_nwstr: start@(%d)\n", sx);
179 #endif /* DEBUG */
180 pcw = WCOL(*start);
181 lnp->flags |= __ISDIRTY;
182 newx = sx + win->ch_off;
183 if (newx < *lnp->firstchp)
184 *lnp->firstchp = newx;
185 #ifdef DEBUG
186 {
187 __CTRACE(__CTRACE_INPUT, "========before=======\n");
188 for (x = 0; x < win->maxx; x++)
189 __CTRACE(__CTRACE_INPUT,
190 "wins_nwstr: (%d,%d)=(%x,%x,%p)\n",
191 (int) win->cury, x,
192 win->alines[win->cury]->line[x].ch,
193 win->alines[win->cury]->line[x].attr,
194 win->alines[win->cury]->line[x].nsp);
195 }
196 #endif /* DEBUG */
197
198 /* shift all complete characters */
199 if (sx + width + pcw <= win->maxx) {
200 #ifdef DEBUG
201 __CTRACE(__CTRACE_INPUT, "wins_nwstr: shift all characters\n");
202 #endif /* DEBUG */
203 temp1 = &win->alines[win->cury]->line[win->maxx - 1];
204 temp2 = temp1 - width;
205 pcw = WCOL(*(temp2 + 1));
206 if (pcw < 0) {
207 #ifdef DEBUG
208 __CTRACE(__CTRACE_INPUT,
209 "wins_nwstr: clear from %d to EOL(%d)\n",
210 win->maxx + pcw, win->maxx - 1);
211 #endif /* DEBUG */
212 temp2 += pcw;
213 while (temp1 > temp2 + width) {
214 temp1->ch = (wchar_t)btowc((int) win->bch);
215 if (_cursesi_copy_nsp(win->bnsp, temp1) == ERR)
216 return ERR;
217 temp1->attr = win->battr;
218 SET_WCOL(*temp1, 1);
219 #ifdef DEBUG
220 __CTRACE(__CTRACE_INPUT,
221 "wins_nwstr: empty cell(%p)\n", temp1);
222 #endif /* DEBUG */
223 temp1--;
224 }
225 }
226 while (temp2 >= start) {
227 (void)memcpy(temp1, temp2, sizeof(__LDATA));
228 temp1--, temp2--;
229 }
230 #ifdef DEBUG
231 {
232 __CTRACE(__CTRACE_INPUT, "=====after shift====\n");
233 for (x = 0; x < win->maxx; x++)
234 __CTRACE(__CTRACE_INPUT,
235 "wins_nwstr: (%d,%d)=(%x,%x,%p)\n",
236 (int) win->cury, x,
237 win->alines[win->cury]->line[x].ch,
238 win->alines[win->cury]->line[x].attr,
239 win->alines[win->cury]->line[x].nsp);
240 }
241 #endif /* DEBUG */
242 }
243
244 /* update string columns */
245 x = win->curx;
246 y = win->cury;
247 for (scp = wstr, temp1 = start; len; len--, scp++) {
248 switch (*scp) {
249 case L'\b':
250 if (--x < 0)
251 x = 0;
252 win->curx = x;
253 continue;;
254 case L'\r':
255 win->curx = 0;
256 continue;
257 case L'\n':
258 wclrtoeol(win);
259 if (y == win->scr_b) {
260 if (!(win->flags & __SCROLLOK))
261 return ERR;
262 scroll(win);
263 }
264 continue;
265 case L'\t':
266 if (wins_nwstr(win, ws,
267 min(win->maxx - x, TABSIZE - (x % TABSIZE)))
268 == ERR)
269 return ERR;
270 continue;
271 }
272 cw = wcwidth(*scp);
273 if (cw < 0)
274 cw = 1;
275 if (cw) {
276 /* 1st column */
277 temp1->ch = (wchar_t)*scp;
278 temp1->attr = win->wattr;
279 SET_WCOL(*temp1, cw);
280 temp1->nsp = NULL;
281 #ifdef DEBUG
282 __CTRACE(__CTRACE_INPUT,
283 "wins_nwstr: add spacing char(%x)\n", temp1->ch);
284 #endif /* DEBUG */
285 temp2 = temp1++;
286 if (cw > 1) {
287 x = -1;
288 while (temp1 < temp2 + cw) {
289 /* the rest columns */
290 temp1->ch = (wchar_t)*scp;
291 temp1->attr = win->wattr;
292 temp1->nsp = NULL;
293 SET_WCOL(*temp1, x);
294 temp1++, x--;
295 }
296 temp1--;
297 }
298 } else {
299 /* non-spacing character */
300 np = malloc(sizeof(nschar_t));
301 if (!np)
302 return ERR;
303 np->ch = *scp;
304 np->next = temp1->nsp;
305 temp1->nsp = np;
306 #ifdef DEBUG
307 __CTRACE(__CTRACE_INPUT,
308 "wins_nstr: add non-spacing char(%x)\n", np->ch);
309 #endif /* DEBUG */
310 }
311 }
312 #ifdef DEBUG
313 {
314 __CTRACE(__CTRACE_INPUT, "========after=======\n");
315 for (x = 0; x < win->maxx; x++)
316 __CTRACE(__CTRACE_INPUT,
317 "wins_nwstr: (%d,%d)=(%x,%x,%p)\n",
318 (int) win->cury, x,
319 win->alines[win->cury]->line[x].ch,
320 win->alines[win->cury]->line[x].attr,
321 win->alines[win->cury]->line[x].nsp);
322 }
323 #endif /* DEBUG */
324 newx = win->maxx - 1 + win->ch_off;
325 if (newx > *lnp->lastchp)
326 *lnp->lastchp = newx;
327 __touchline(win, (int) win->cury, sx, (int) win->maxx - 1);
328 __sync(win);
329 return OK;
330 }
331