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