vm: fix a null dereference on out-of-memory
[minix.git] / lib / libcurses / insstr.c
blob56c8169f56ca99c286e01e11771eb78ec2628062
1 /* $NetBSD: insstr.c,v 1.3 2009/07/22 16:57:15 roy Exp $ */
3 /*
4 * Copyright (c) 2005 The NetBSD Foundation Inc.
5 * All rights reserved.
7 * This code is derived from code donated to the NetBSD Foundation
8 * by Ruibiao Qiu <ruibiao@arl.wustl.edu,ruibiao@gmail.com>.
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.
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.
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 */
42 #include <string.h>
43 #include <stdlib.h>
45 #include "curses.h"
46 #include "curses_private.h"
48 #ifndef _CURSES_USE_MACROS
51 * insstr --
52 * insert a multi-byte character string into the current window
54 int
55 insstr(const char *str)
57 return winsstr(stdscr, str);
61 * insnstr --
62 * insert a multi-byte character string into the current window
63 * with at most n characters
65 int
66 insnstr(const char *str, int n)
68 return winsnstr(stdscr, str, n);
72 * mvinsstr --
73 * Do an insert-string on the line at (y, x).
75 int
76 mvinsstr(int y, int x, const char *str)
78 return mvwinsstr(stdscr, y, x, str);
82 * mvinsnstr --
83 * Do an insert-n-string on the line at (y, x).
85 int
86 mvinsnstr(int y, int x, const char *str, int n)
88 return mvwinsnstr(stdscr, y, x, str, n);
92 * mvwinsstr --
93 * Do an insert-string on the line at (y, x) in the given window.
95 int
96 mvwinsstr(WINDOW *win, int y, int x, const char *str)
98 if (wmove(win, y, x) == ERR)
99 return ERR;
101 return winsstr(stdscr, str);
105 * mvwinsnstr --
106 * Do an insert-n-string on the line at (y, x) in the given window.
109 mvwinsnstr(WINDOW *win, int y, int x, const char *str, int n)
111 if (wmove(win, y, x) == ERR)
112 return ERR;
114 return winsnstr(stdscr, str, n);
117 #endif
120 * winsstr --
121 * Do an insert-string on the line, leaving (cury, curx) unchanged.
122 * No wrapping.
125 winsstr(WINDOW *win, const char *str)
127 return winsnstr( win, str, -1 );
131 * winsnstr --
132 * Do an insert-n-string on the line, leaving (cury, curx) unchanged.
133 * Performs wrapping.
136 winsnstr(WINDOW *win, const char *str, int n)
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 */
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 */
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;
172 temp1->nsp = NULL;
174 #endif /* HAVE_WCHAR */
175 (void) memcpy(temp1, temp2, sizeof(__LDATA));
176 temp1--, temp2--;
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 */
189 #ifdef DEBUG
191 int i;
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);
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;