xref: /netbsd/lib/libcurses/ins_wstr.c (revision 33229d3e)
1 /*   $NetBSD: ins_wstr.c,v 1.24 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: ins_wstr.c,v 1.24 2022/10/19 06:09:27 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
ins_wstr(const wchar_t * wstr)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
ins_nwstr(const wchar_t * wstr,int n)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
mvins_wstr(int y,int x,const wchar_t * wstr)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
mvins_nwstr(int y,int x,const wchar_t * wstr,int n)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
mvwins_wstr(WINDOW * win,int y,int x,const wchar_t * wstr)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(win, 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
mvwins_nwstr(WINDOW * win,int y,int x,const wchar_t * wstr,int n)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(win, wstr, n);
113 }
114 
115 
116 /*
117  * wins_wstr --
118  *	Do an insert-string on the line, leaving (cury, curx) unchanged.
119  */
120 int
wins_wstr(WINDOW * win,const wchar_t * wstr)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
wins_nwstr(WINDOW * win,const wchar_t * wstr,int n)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 	cchar_t cc;
137 	wchar_t *lstr, *slstr;
138 	int width, len, lx, sx, x, y, tx, ty, cw, pcw, newx, tn, w;
139 	wchar_t ws[] = L"		";
140 
141 	/* check for leading non-spacing character */
142 	if (!wstr)
143 		return OK;
144 	cw = wcwidth(*wstr);
145 	if (cw < 0)
146 		cw = 1;
147 	if (!cw)
148 		return ERR;
149 
150 	lstr = malloc(sizeof(wchar_t) * win->maxx);
151 	if (lstr == NULL)
152 		return ERR;
153 
154 	scp = wstr + 1;
155 	width = cw;
156 	len = 1;
157 	n--;
158 	y = win->cury;
159 	x = win->curx;
160 	tn = n;
161 
162 	/*
163 	 * Firstly, make sure the string will fit...
164 	 */
165 	while (*scp) {
166 		if (!tn)
167 			break;
168 		switch (*scp) {
169 			case L'\b':
170 				if (--x < 0)
171 					x = 0;
172 				cw = wcwidth(*(scp - 1));
173 				if (cw < 0)
174 					cw = 1;
175 				width -= cw;
176 				scp++;
177 				continue;
178 
179 			case L'\r':
180 				width = 0;
181 				x = 0;
182 				scp++;
183 				continue;
184 
185 			case L'\n':
186 				if (y == win->scr_b) {
187 					if (!(win->flags & __SCROLLOK)) {
188 						free(lstr);
189 						return ERR;
190 					}
191 				}
192 				y++;
193 				scp++;
194 				continue;
195 
196 			case L'\t':
197 				cw = wcwidth(ws[0]);
198 				if (cw < 0)
199 					cw = 1;
200 				w = cw * (TABSIZE - (x % TABSIZE));
201 				width += w;
202 				x += w;
203 				scp++;
204 				continue;
205 		}
206 		w = wcwidth(*scp);
207 		if (w < 0)
208 			w = 1;
209 		tn--, width += w;
210 		scp++;
211 	}
212 	__CTRACE(__CTRACE_INPUT, "wins_nwstr: width=%d,len=%d\n", width, len);
213 
214 	if (width > win->maxx - win->curx + 1) {
215 		free(lstr);
216 		return ERR;
217 	}
218 
219 	scp = wstr;
220 	x = win->curx;
221 	y = win->cury;
222 	len = 0;
223 	width = 0;
224 	slstr = lstr;
225 
226 	while (*scp && n) {
227 		lstr = slstr;
228 		lx = x;
229 		while (*scp) {
230 			if (!n)
231 				break;
232 			switch (*scp) {
233 				case L'\b':
234 					if (--x < 0)
235 						x = 0;
236 					if (--len < 0)
237 						len = 0;
238 					cw = wcwidth(*(scp - 1));
239 					if (cw < 0)
240 						cw = 1;
241 					width -= cw;
242 					scp++;
243 					if (lstr != slstr)
244 						lstr--;
245 					continue;
246 
247 				case L'\r':
248 					width = 0;
249 					len = 0;
250 					x = 0;
251 					scp++;
252 					lstr = slstr;
253 					continue;
254 
255 				case L'\n':
256 					goto loopdone;
257 
258 				case L'\t':
259 					cw = wcwidth(ws[0]);
260 					if (cw < 0)
261 						cw = 1;
262 					w = cw * (TABSIZE - (x % TABSIZE));
263 					width += w;
264 					x += w;
265 					len += w;
266 					scp++;
267 					*lstr = *ws;
268 					lstr++;
269 					continue;
270 			}
271 			w = wcwidth(*scp);
272 			if (w < 0)
273 				w = 1;
274 			*lstr = *scp;
275 			n--, len++, width += w;
276 			scp++, lstr++;
277 		}
278 
279 loopdone:
280 		start = &win->alines[y]->line[x];
281 		sx = x;
282 		lnp = win->alines[y];
283 		pcw = start->wcols;
284 		if (pcw < 0) {
285 			sx += pcw;
286 			start += pcw;
287 		}
288 		__CTRACE(__CTRACE_INPUT, "wins_nwstr: start@(%d)\n", sx);
289 		pcw = start->wcols;
290 		lnp->flags |= __ISDIRTY;
291 		newx = sx + win->ch_off;
292 		if (newx < *lnp->firstchp)
293 			*lnp->firstchp = newx;
294 
295 #ifdef DEBUG
296 		{
297 			int i;
298 
299 			__CTRACE(__CTRACE_INPUT, "========before=======\n");
300 			for (i = 0; i < win->maxx; i++)
301 			__CTRACE(__CTRACE_INPUT,
302 				    "wins_nwstr: (%d,%d)=(%x,%x,%d,%x,%p)\n",
303 				    y, i, win->alines[y]->line[i].ch,
304 				    win->alines[y]->line[i].attr,
305 				    win->alines[y]->line[i].wcols,
306 				    win->alines[y]->line[i].cflags,
307 				    win->alines[y]->line[i].nsp);
308 		}
309 #endif /* DEBUG */
310 
311 		/* shift all complete characters */
312 		if (sx + width + pcw <= win->maxx) {
313 			__CTRACE(__CTRACE_INPUT, "wins_nwstr: shift all characters by %d\n", width);
314 			temp1 = &win->alines[y]->line[win->maxx - 1];
315 			temp2 = temp1 - width;
316 			pcw = (temp2 + 1)->wcols;
317 			if (pcw < 0) {
318 				__CTRACE(__CTRACE_INPUT,
319 				    "wins_nwstr: clear from %d to EOL(%d)\n",
320 				    win->maxx + pcw, win->maxx - 1);
321 				temp2 += pcw;
322 				while (temp1 > temp2 + width) {
323 					temp1->ch = win->bch;
324 					if (_cursesi_copy_nsp(win->bnsp, temp1) == ERR) {
325 						free(lstr);
326 						return ERR;
327 					}
328 					temp1->attr = win->battr;
329 					temp1->cflags |= CA_BACKGROUND;
330 					temp1->cflags &= ~CA_CONTINUATION;
331 					temp1->wcols = 1;
332 					__CTRACE(__CTRACE_INPUT,
333 					    "wins_nwstr: empty cell(%p)\n", temp1);
334 					temp1--;
335 				}
336 			}
337 			while (temp2 >= start) {
338 				(void)memcpy(temp1, temp2, sizeof(__LDATA));
339 				temp1--, temp2--;
340 			}
341 #ifdef DEBUG
342 			{
343 				int i;
344 
345 				__CTRACE(__CTRACE_INPUT, "=====after shift====\n");
346 				for (i = 0; i < win->maxx; i++)
347 					__CTRACE(__CTRACE_INPUT,
348 					    "wins_nwstr: (%d,%d)=(%x,%x,%x,%p)\n",
349 					    y, i,
350 					    win->alines[y]->line[i].ch,
351 					    win->alines[y]->line[i].attr,
352 					    win->alines[y]->line[i].cflags,
353 					    win->alines[y]->line[i].nsp);
354 				__CTRACE(__CTRACE_INPUT, "=====lstr====\n");
355 				for (i = 0; i < len; i++)
356 					__CTRACE(__CTRACE_INPUT,
357 					    "wins_nwstr: lstr[%d]= %x,\n",
358 					    i, (unsigned) slstr[i]);
359 			}
360 #endif /* DEBUG */
361 		}
362 
363 		/* update string columns */
364 		x = lx;
365 		for (lstr = slstr, temp1 = start; len; len--, lstr++) {
366 			lnp = win->alines[y];
367 			cc.vals[0] = *lstr;
368 			cc.elements = 1;
369 			cc.attributes = win->wattr;
370 			_cursesi_addwchar(win, &lnp, &y, &x, &cc, 0);
371 		}
372 
373 #ifdef DEBUG
374 		{
375 			int i;
376 
377 			__CTRACE(__CTRACE_INPUT, "lx = %d, x = %x\n", lx, x);
378 			__CTRACE(__CTRACE_INPUT, "========after=======\n");
379 			for (i = 0; i < win->maxx; i++)
380 				__CTRACE(__CTRACE_INPUT,
381 				    "wins_nwstr: (%d,%d)=(%x,%x,%d,%x,%p)\n",
382 				    y, i,
383 				    win->alines[y]->line[i].ch,
384 				    win->alines[y]->line[i].attr,
385 				    win->alines[y]->line[i].wcols,
386 				    win->alines[y]->line[i].cflags,
387 				    win->alines[y]->line[i].nsp);
388 		}
389 #endif /* DEBUG */
390 
391 		__touchline(win, (int) y, lx, (int) win->maxx - 1);
392 
393 		/*
394 		 * Handle the newline here - we don't need to check
395 		 * if we are allowed to scroll because this was checked
396 		 * already.
397 		 */
398 		if (*scp == '\n') {
399 			tx = win->curx;
400 			ty = win->cury;
401 			win->curx = x;
402 			win->cury = y;
403 			wclrtoeol(win);
404 			win->curx = tx;
405 			win->cury = ty;
406 			if (y == win->scr_b)
407 				scroll(win);
408 			else
409 				y++;
410 			scp++;
411 
412 		}
413 
414 		newx = win->maxx - 1 + win->ch_off;
415 		if (newx > *lnp->lastchp)
416 			*lnp->lastchp = newx;
417 	}
418 	free(lstr);
419 	__sync(win);
420 	return OK;
421 }
422