xref: /minix3/lib/libcurses/insstr.c (revision 51ffecc181005cb45a40108612ee28d1daaeeb86)
1*51ffecc1SBen Gras /*   $NetBSD: insstr.c,v 1.3 2009/07/22 16:57:15 roy Exp $ */
2*51ffecc1SBen Gras 
3*51ffecc1SBen Gras /*
4*51ffecc1SBen Gras  * Copyright (c) 2005 The NetBSD Foundation Inc.
5*51ffecc1SBen Gras  * All rights reserved.
6*51ffecc1SBen Gras  *
7*51ffecc1SBen Gras  * This code is derived from code donated to the NetBSD Foundation
8*51ffecc1SBen Gras  * by Ruibiao Qiu <ruibiao@arl.wustl.edu,ruibiao@gmail.com>.
9*51ffecc1SBen Gras  *
10*51ffecc1SBen Gras  *
11*51ffecc1SBen Gras  * Redistribution and use in source and binary forms, with or without
12*51ffecc1SBen Gras  * modification, are permitted provided that the following conditions
13*51ffecc1SBen Gras  * are met:
14*51ffecc1SBen Gras  * 1. Redistributions of source code must retain the above copyright
15*51ffecc1SBen Gras  *	notice, this list of conditions and the following disclaimer.
16*51ffecc1SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
17*51ffecc1SBen Gras  *	notice, this list of conditions and the following disclaimer in the
18*51ffecc1SBen Gras  *	documentation and/or other materials provided with the distribution.
19*51ffecc1SBen Gras  * 3. Neither the name of the NetBSD Foundation nor the names of its
20*51ffecc1SBen Gras  *	contributors may be used to endorse or promote products derived
21*51ffecc1SBen Gras  *	from this software without specific prior written permission.
22*51ffecc1SBen Gras  *
23*51ffecc1SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
24*51ffecc1SBen Gras  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25*51ffecc1SBen Gras  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26*51ffecc1SBen Gras  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27*51ffecc1SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28*51ffecc1SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29*51ffecc1SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30*51ffecc1SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31*51ffecc1SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32*51ffecc1SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33*51ffecc1SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34*51ffecc1SBen Gras  * SUCH DAMAGE.
35*51ffecc1SBen Gras  */
36*51ffecc1SBen Gras 
37*51ffecc1SBen Gras #include <sys/cdefs.h>
38*51ffecc1SBen Gras #ifndef lint
39*51ffecc1SBen Gras __RCSID("$NetBSD: insstr.c,v 1.3 2009/07/22 16:57:15 roy Exp $");
40*51ffecc1SBen Gras #endif						  /* not lint */
41*51ffecc1SBen Gras 
42*51ffecc1SBen Gras #include <string.h>
43*51ffecc1SBen Gras #include <stdlib.h>
44*51ffecc1SBen Gras 
45*51ffecc1SBen Gras #include "curses.h"
46*51ffecc1SBen Gras #include "curses_private.h"
47*51ffecc1SBen Gras 
48*51ffecc1SBen Gras #ifndef _CURSES_USE_MACROS
49*51ffecc1SBen Gras 
50*51ffecc1SBen Gras /*
51*51ffecc1SBen Gras  * insstr --
52*51ffecc1SBen Gras  *	insert a multi-byte character string into the current window
53*51ffecc1SBen Gras  */
54*51ffecc1SBen Gras int
insstr(const char * str)55*51ffecc1SBen Gras insstr(const char *str)
56*51ffecc1SBen Gras {
57*51ffecc1SBen Gras 	return winsstr(stdscr, str);
58*51ffecc1SBen Gras }
59*51ffecc1SBen Gras 
60*51ffecc1SBen Gras /*
61*51ffecc1SBen Gras  * insnstr --
62*51ffecc1SBen Gras  *	insert a multi-byte character string into the current window
63*51ffecc1SBen Gras  *	with at most n characters
64*51ffecc1SBen Gras  */
65*51ffecc1SBen Gras int
insnstr(const char * str,int n)66*51ffecc1SBen Gras insnstr(const char *str, int n)
67*51ffecc1SBen Gras {
68*51ffecc1SBen Gras 	return winsnstr(stdscr, str, n);
69*51ffecc1SBen Gras }
70*51ffecc1SBen Gras 
71*51ffecc1SBen Gras /*
72*51ffecc1SBen Gras  * mvinsstr --
73*51ffecc1SBen Gras  *	  Do an insert-string on the line at (y, x).
74*51ffecc1SBen Gras  */
75*51ffecc1SBen Gras int
mvinsstr(int y,int x,const char * str)76*51ffecc1SBen Gras mvinsstr(int y, int x, const char *str)
77*51ffecc1SBen Gras {
78*51ffecc1SBen Gras 	return mvwinsstr(stdscr, y, x, str);
79*51ffecc1SBen Gras }
80*51ffecc1SBen Gras 
81*51ffecc1SBen Gras /*
82*51ffecc1SBen Gras  * mvinsnstr --
83*51ffecc1SBen Gras  *	  Do an insert-n-string on the line at (y, x).
84*51ffecc1SBen Gras  */
85*51ffecc1SBen Gras int
mvinsnstr(int y,int x,const char * str,int n)86*51ffecc1SBen Gras mvinsnstr(int y, int x, const char *str, int n)
87*51ffecc1SBen Gras {
88*51ffecc1SBen Gras 	return mvwinsnstr(stdscr, y, x, str, n);
89*51ffecc1SBen Gras }
90*51ffecc1SBen Gras 
91*51ffecc1SBen Gras /*
92*51ffecc1SBen Gras  * mvwinsstr --
93*51ffecc1SBen Gras  *	  Do an insert-string on the line at (y, x) in the given window.
94*51ffecc1SBen Gras  */
95*51ffecc1SBen Gras int
mvwinsstr(WINDOW * win,int y,int x,const char * str)96*51ffecc1SBen Gras mvwinsstr(WINDOW *win, int y, int x, const char *str)
97*51ffecc1SBen Gras {
98*51ffecc1SBen Gras 	if (wmove(win, y, x) == ERR)
99*51ffecc1SBen Gras 		return ERR;
100*51ffecc1SBen Gras 
101*51ffecc1SBen Gras 	return winsstr(stdscr, str);
102*51ffecc1SBen Gras }
103*51ffecc1SBen Gras 
104*51ffecc1SBen Gras /*
105*51ffecc1SBen Gras  * mvwinsnstr --
106*51ffecc1SBen Gras  *	  Do an insert-n-string on the line at (y, x) in the given window.
107*51ffecc1SBen Gras  */
108*51ffecc1SBen Gras int
mvwinsnstr(WINDOW * win,int y,int x,const char * str,int n)109*51ffecc1SBen Gras mvwinsnstr(WINDOW *win, int y, int x, const char *str, int n)
110*51ffecc1SBen Gras {
111*51ffecc1SBen Gras 	if (wmove(win, y, x) == ERR)
112*51ffecc1SBen Gras 		return ERR;
113*51ffecc1SBen Gras 
114*51ffecc1SBen Gras 	return winsnstr(stdscr, str, n);
115*51ffecc1SBen Gras }
116*51ffecc1SBen Gras 
117*51ffecc1SBen Gras #endif
118*51ffecc1SBen Gras 
119*51ffecc1SBen Gras /*
120*51ffecc1SBen Gras  * winsstr --
121*51ffecc1SBen Gras  *	Do an insert-string on the line, leaving (cury, curx) unchanged.
122*51ffecc1SBen Gras  *	No wrapping.
123*51ffecc1SBen Gras  */
124*51ffecc1SBen Gras int
winsstr(WINDOW * win,const char * str)125*51ffecc1SBen Gras winsstr(WINDOW *win, const char *str)
126*51ffecc1SBen Gras {
127*51ffecc1SBen Gras 	return winsnstr( win, str, -1 );
128*51ffecc1SBen Gras }
129*51ffecc1SBen Gras 
130*51ffecc1SBen Gras /*
131*51ffecc1SBen Gras  * winsnstr --
132*51ffecc1SBen Gras  *	Do an insert-n-string on the line, leaving (cury, curx) unchanged.
133*51ffecc1SBen Gras  *	Performs wrapping.
134*51ffecc1SBen Gras  */
135*51ffecc1SBen Gras int
winsnstr(WINDOW * win,const char * str,int n)136*51ffecc1SBen Gras winsnstr(WINDOW *win, const char *str, int n)
137*51ffecc1SBen Gras {
138*51ffecc1SBen Gras 	__LDATA	*end, *temp1, *temp2;
139*51ffecc1SBen Gras 	const char *scp;
140*51ffecc1SBen Gras 	int len, x;
141*51ffecc1SBen Gras 	__LINE *lnp;
142*51ffecc1SBen Gras #ifdef HAVE_WCHAR
143*51ffecc1SBen Gras 	nschar_t *np, *tnp;
144*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
145*51ffecc1SBen Gras 
146*51ffecc1SBen Gras 	/* find string length */
147*51ffecc1SBen Gras 	if ( n > 0 )
148*51ffecc1SBen Gras 		for ( scp = str, len = 0; n-- && *scp++; ++len );
149*51ffecc1SBen Gras 	else
150*51ffecc1SBen Gras 		for ( scp = str, len = 0; *scp++; ++len );
151*51ffecc1SBen Gras #ifdef DEBUG
152*51ffecc1SBen Gras 	__CTRACE(__CTRACE_INPUT, "winsnstr: len = %d\n", len);
153*51ffecc1SBen Gras #endif /* DEBUG */
154*51ffecc1SBen Gras 
155*51ffecc1SBen Gras 	/* move string */
156*51ffecc1SBen Gras 	end = &win->alines[win->cury]->line[win->curx];
157*51ffecc1SBen Gras 	if ( len < win->maxx - win->curx ) {
158*51ffecc1SBen Gras #ifdef DEBUG
159*51ffecc1SBen Gras 		__CTRACE(__CTRACE_INPUT, "winsnstr: shift %d cells\n", len);
160*51ffecc1SBen Gras #endif /* DEBUG */
161*51ffecc1SBen Gras 		temp1 = &win->alines[win->cury]->line[win->maxx - 1];
162*51ffecc1SBen Gras 		temp2 = temp1 - len;
163*51ffecc1SBen Gras 		while (temp2 >= end) {
164*51ffecc1SBen Gras #ifdef HAVE_WCHAR
165*51ffecc1SBen Gras 			np = temp1->nsp;
166*51ffecc1SBen Gras 			if (np){
167*51ffecc1SBen Gras 				while ( np ) {
168*51ffecc1SBen Gras 					tnp = np->next;
169*51ffecc1SBen Gras 					free( np );
170*51ffecc1SBen Gras 					np = tnp;
171*51ffecc1SBen Gras 				}
172*51ffecc1SBen Gras 				temp1->nsp = NULL;
173*51ffecc1SBen Gras 			}
174*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
175*51ffecc1SBen Gras 			(void) memcpy(temp1, temp2, sizeof(__LDATA));
176*51ffecc1SBen Gras 			temp1--, temp2--;
177*51ffecc1SBen Gras 		}
178*51ffecc1SBen Gras 	}
179*51ffecc1SBen Gras 
180*51ffecc1SBen Gras 	for ( scp = str, temp1 = end, x = win->curx;
181*51ffecc1SBen Gras 			*scp && x < len + win->curx && x < win->maxx;
182*51ffecc1SBen Gras 			scp++, temp1++, x++ ) {
183*51ffecc1SBen Gras 		temp1->ch = (wchar_t)*scp & __CHARTEXT;
184*51ffecc1SBen Gras 		temp1->attr = win->wattr;
185*51ffecc1SBen Gras #ifdef HAVE_WCHAR
186*51ffecc1SBen Gras 		SET_WCOL( *temp1, 1 );
187*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
188*51ffecc1SBen Gras 	}
189*51ffecc1SBen Gras #ifdef DEBUG
190*51ffecc1SBen Gras 	{
191*51ffecc1SBen Gras 		int i;
192*51ffecc1SBen Gras 
193*51ffecc1SBen Gras 		for ( i = win->curx; i < win->curx + len; i++ ) {
194*51ffecc1SBen Gras 			__CTRACE(__CTRACE_INPUT,
195*51ffecc1SBen Gras 			    "winsnstr: (%d,%d)=('%c',%x)\n", win->cury, i,
196*51ffecc1SBen Gras 			    win->alines[win->cury]->line[i].ch,
197*51ffecc1SBen Gras 			    win->alines[win->cury]->line[i].attr);
198*51ffecc1SBen Gras 		}
199*51ffecc1SBen Gras 	}
200*51ffecc1SBen Gras #endif /* DEBUG */
201*51ffecc1SBen Gras 	lnp = win->alines[ win->cury ];
202*51ffecc1SBen Gras 	lnp->flags |= __ISDIRTY;
203*51ffecc1SBen Gras 	if ( win->ch_off < *lnp->firstchp )
204*51ffecc1SBen Gras 		*lnp->firstchp = win->ch_off;
205*51ffecc1SBen Gras 	if ( win->ch_off + win->maxx - 1 > *lnp->lastchp )
206*51ffecc1SBen Gras 		*lnp->lastchp = win->ch_off + win->maxx - 1;
207*51ffecc1SBen Gras 	__touchline(win, (int)win->cury, (int)win->curx, (int)win->maxx - 1);
208*51ffecc1SBen Gras 	return OK;
209*51ffecc1SBen Gras }
210