xref: /netbsd/lib/libcurses/insstr.c (revision fc7239c2)
1 /*   $NetBSD: insstr.c,v 1.8 2020/07/06 22:46:50 uwe 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: insstr.c,v 1.8 2020/07/06 22:46:50 uwe 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 #ifndef _CURSES_USE_MACROS
49 
50 /*
51  * insstr --
52  *	insert a multi-byte character string into the current window
53  */
54 int
55 insstr(const char *str)
56 {
57 
58 	return winsstr(stdscr, str);
59 }
60 
61 /*
62  * insnstr --
63  *	insert a multi-byte character string into the current window
64  *	with at most n characters
65  */
66 int
67 insnstr(const char *str, int n)
68 {
69 
70 	return winsnstr(stdscr, str, n);
71 }
72 
73 /*
74  * mvinsstr --
75  *	  Do an insert-string on the line at (y, x).
76  */
77 int
78 mvinsstr(int y, int x, const char *str)
79 {
80 
81 	return mvwinsstr(stdscr, y, x, str);
82 }
83 
84 /*
85  * mvinsnstr --
86  *	  Do an insert-n-string on the line at (y, x).
87  */
88 int
89 mvinsnstr(int y, int x, const char *str, int n)
90 {
91 
92 	return mvwinsnstr(stdscr, y, x, str, n);
93 }
94 
95 /*
96  * mvwinsstr --
97  *	  Do an insert-string on the line at (y, x) in the given window.
98  */
99 int
100 mvwinsstr(WINDOW *win, int y, int x, const char *str)
101 {
102 
103 	if (wmove(win, y, x) == ERR)
104 		return ERR;
105 
106 	return winsstr(win, str);
107 }
108 
109 /*
110  * mvwinsnstr --
111  *	  Do an insert-n-string on the line at (y, x) in the given window.
112  */
113 int
114 mvwinsnstr(WINDOW *win, int y, int x, const char *str, int n)
115 {
116 
117 	if (wmove(win, y, x) == ERR)
118 		return ERR;
119 
120 	return winsnstr(win, str, n);
121 }
122 
123 #endif
124 
125 /*
126  * winsstr --
127  *	Do an insert-string on the line, leaving (cury, curx) unchanged.
128  *	No wrapping.
129  */
130 int
131 winsstr(WINDOW *win, const char *str)
132 {
133 
134 	return winsnstr(win, str, -1);
135 }
136 
137 /*
138  * winsnstr --
139  *	Do an insert-n-string on the line, leaving (cury, curx) unchanged.
140  *	Performs wrapping.
141  */
142 int
143 winsnstr(WINDOW *win, const char *str, int n)
144 {
145 	__LDATA	*end, *temp1, *temp2;
146 	const char *scp;
147 	int len, x;
148 	__LINE *lnp;
149 #ifdef HAVE_WCHAR
150 	nschar_t *np, *tnp;
151 #endif /* HAVE_WCHAR */
152 
153 	/* find string length */
154 	if (n > 0)
155 		for (scp = str, len = 0; n-- && *scp++; ++len);
156 	else
157 		for (scp = str, len = 0; *scp++; ++len);
158 #ifdef DEBUG
159 	__CTRACE(__CTRACE_INPUT, "winsnstr: len = %d\n", len);
160 #endif /* DEBUG */
161 
162 	/* move string */
163 	end = &win->alines[win->cury]->line[win->curx];
164 	if (len < win->maxx - win->curx) {
165 #ifdef DEBUG
166 		__CTRACE(__CTRACE_INPUT, "winsnstr: shift %d cells\n", len);
167 #endif /* DEBUG */
168 		temp1 = &win->alines[win->cury]->line[win->maxx - 1];
169 		temp2 = temp1 - len;
170 		while (temp2 >= end) {
171 #ifdef HAVE_WCHAR
172 			np = temp1->nsp;
173 			if (np){
174 				while (np) {
175 					tnp = np->next;
176 					free(np);
177 					np = tnp;
178 				}
179 				temp1->nsp = NULL;
180 			}
181 #endif /* HAVE_WCHAR */
182 			(void)memcpy(temp1, temp2, sizeof(__LDATA));
183 			temp1--, temp2--;
184 		}
185 	}
186 
187 	for (scp = str, temp1 = end, x = win->curx;
188 	     *scp && x < len + win->curx && x < win->maxx;
189 	     scp++, temp1++, x++)
190 	{
191 		temp1->ch = (wchar_t)*scp & __CHARTEXT;
192 		temp1->attr = win->wattr;
193 #ifdef HAVE_WCHAR
194 		SET_WCOL(*temp1, 1);
195 #endif /* HAVE_WCHAR */
196 	}
197 #ifdef DEBUG
198 	{
199 		int i;
200 
201 		for (i = win->curx; i < win->curx + len; i++) {
202 			__CTRACE(__CTRACE_INPUT,
203 			    "winsnstr: (%d,%d)=('%c',%x)\n", win->cury, i,
204 			    win->alines[win->cury]->line[i].ch,
205 			    win->alines[win->cury]->line[i].attr);
206 		}
207 	}
208 #endif /* DEBUG */
209 	lnp = win->alines[win->cury];
210 	lnp->flags |= __ISDIRTY;
211 	if (win->ch_off < *lnp->firstchp)
212 		*lnp->firstchp = win->ch_off;
213 	if (win->ch_off + win->maxx - 1 > *lnp->lastchp)
214 		*lnp->lastchp = win->ch_off + win->maxx - 1;
215 	__touchline(win, (int)win->cury, (int)win->curx, (int)win->maxx - 1);
216 	__sync(win);
217 	return OK;
218 }
219