xref: /netbsd-src/lib/libcurses/ins_wstr.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*   $NetBSD: ins_wstr.c,v 1.20 2021/11/16 23:23:02 kre 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.20 2021/11/16 23:23:02 kre 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(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
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
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 	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 					break;
258 
259 				case L'\t':
260 					cw = wcwidth(ws[0]);
261 					if (cw < 0)
262 						cw = 1;
263 					w = cw * (TABSIZE - (x % TABSIZE));
264 					width += w;
265 					x += w;
266 					len += w;
267 					scp++;
268 					*lstr = *ws;
269 					lstr++;
270 					continue;
271 			}
272 			w = wcwidth(*scp);
273 			if (w < 0)
274 				w = 1;
275 			*lstr = *scp;
276 			n--, len++, width += w;
277 			scp++, lstr++;
278 		}
279 
280 loopdone:
281 		start = &win->alines[y]->line[x];
282 		sx = x;
283 		lnp = win->alines[y];
284 		pcw = WCOL(*start);
285 		if (pcw < 0) {
286 			sx += pcw;
287 			start += pcw;
288 		}
289 		__CTRACE(__CTRACE_INPUT, "wins_nwstr: start@(%d)\n", sx);
290 		pcw = WCOL(*start);
291 		lnp->flags |= __ISDIRTY;
292 		newx = sx + win->ch_off;
293 		if (newx < *lnp->firstchp)
294 			*lnp->firstchp = newx;
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,%p)\n",
303 				    y, i, win->alines[y]->line[i].ch,
304 				    win->alines[y]->line[i].attr,
305 				    win->alines[y]->line[i].nsp);
306 		}
307 #endif /* DEBUG */
308 
309 		/* shift all complete characters */
310 		if (sx + width + pcw <= win->maxx) {
311 			__CTRACE(__CTRACE_INPUT, "wins_nwstr: shift all characters by %d\n", width);
312 			temp1 = &win->alines[y]->line[win->maxx - 1];
313 			temp2 = temp1 - width;
314 			pcw = WCOL(*(temp2 + 1));
315 			if (pcw < 0) {
316 				__CTRACE(__CTRACE_INPUT,
317 				    "wins_nwstr: clear from %d to EOL(%d)\n",
318 				    win->maxx + pcw, win->maxx - 1);
319 				temp2 += pcw;
320 				while (temp1 > temp2 + width) {
321 					temp1->ch = (wchar_t)btowc((int) win->bch);
322 					if (_cursesi_copy_nsp(win->bnsp, temp1) == ERR) {
323 						free(lstr);
324 						return ERR;
325 					}
326 					temp1->attr = win->battr;
327 					SET_WCOL(*temp1, 1);
328 					__CTRACE(__CTRACE_INPUT,
329 					    "wins_nwstr: empty cell(%p)\n", temp1);
330 					temp1--;
331 				}
332 			}
333 			while (temp2 >= start) {
334 				(void)memcpy(temp1, temp2, sizeof(__LDATA));
335 				temp1--, temp2--;
336 			}
337 #ifdef DEBUG
338 			{
339 				int i;
340 
341 				__CTRACE(__CTRACE_INPUT, "=====after shift====\n");
342 				for (i = 0; i < win->maxx; i++)
343 					__CTRACE(__CTRACE_INPUT,
344 					    "wins_nwstr: (%d,%d)=(%x,%x,%p)\n",
345 					    y, i,
346 					    win->alines[y]->line[i].ch,
347 					    win->alines[y]->line[i].attr,
348 					    win->alines[y]->line[i].nsp);
349 				__CTRACE(__CTRACE_INPUT, "=====lstr====\n");
350 				for (i = 0; i < len; i++)
351 					__CTRACE(__CTRACE_INPUT,
352 					    "wins_nwstr: lstr[%d]= %x,\n",
353 					    i, (unsigned) slstr[i]);
354 			}
355 #endif /* DEBUG */
356 		}
357 
358 		/* update string columns */
359 		x = lx;
360 		for (lstr = slstr, temp1 = start; len; len--, lstr++) {
361 			lnp = win->alines[y];
362 			cc.vals[0] = *lstr;
363 			cc.elements = 1;
364 			cc.attributes = win->wattr;
365 			_cursesi_addwchar(win, &lnp, &y, &x, &cc, 0);
366 		}
367 
368 #ifdef DEBUG
369 		{
370 			int i;
371 
372 			__CTRACE(__CTRACE_INPUT, "lx = %d, x = %x\n", lx, x);
373 			__CTRACE(__CTRACE_INPUT, "========after=======\n");
374 			for (i = 0; i < win->maxx; i++)
375 				__CTRACE(__CTRACE_INPUT,
376 				    "wins_nwstr: (%d,%d)=(%x,%x,%p)\n",
377 				    y, i,
378 				    win->alines[y]->line[i].ch,
379 				    win->alines[y]->line[i].attr,
380 				    win->alines[y]->line[i].nsp);
381 		}
382 #endif /* DEBUG */
383 
384 		__touchline(win, (int) y, lx, (int) win->maxx - 1);
385 
386 		/*
387 		 * Handle the newline here - we don't need to check
388 		 * if we are allowed to scroll because this was checked
389 		 * already.
390 		 */
391 		if (*scp == '\n') {
392 			tx = win->curx;
393 			ty = win->cury;
394 			win->curx = x;
395 			win->cury = y;
396 			wclrtoeol(win);
397 			win->curx = tx;
398 			win->cury = ty;
399 			if (y == win->scr_b)
400 				scroll(win);
401 			else
402 				y++;
403 			scp++;
404 
405 		}
406 
407 		newx = win->maxx - 1 + win->ch_off;
408 		if (newx > *lnp->lastchp)
409 			*lnp->lastchp = newx;
410 	}
411 	free(lstr);
412 	__sync(win);
413 	return OK;
414 }
415