xref: /minix3/lib/libcurses/inchstr.c (revision 0c3ae37f525eceade8dc047e551f5c9cb33faeb1)
1*0c3ae37fSLionel Sambuc /*	$NetBSD: inchstr.c,v 1.6 2012/04/21 11:33:16 blymn Exp $	*/
251ffecc1SBen Gras 
351ffecc1SBen Gras /*
451ffecc1SBen Gras  * Copyright 2001 Wasabi Systems, Inc.
551ffecc1SBen Gras  * All rights reserved.
651ffecc1SBen Gras  *
751ffecc1SBen Gras  * Written by Simon Burge for Wasabi Systems, Inc.
851ffecc1SBen Gras  *
951ffecc1SBen Gras  * Redistribution and use in source and binary forms, with or without
1051ffecc1SBen Gras  * modification, are permitted provided that the following conditions
1151ffecc1SBen Gras  * are met:
1251ffecc1SBen Gras  * 1. Redistributions of source code must retain the above copyright
1351ffecc1SBen Gras  *    notice, this list of conditions and the following disclaimer.
1451ffecc1SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
1551ffecc1SBen Gras  *    notice, this list of conditions and the following disclaimer in the
1651ffecc1SBen Gras  *    documentation and/or other materials provided with the distribution.
1751ffecc1SBen Gras  * 3. All advertising materials mentioning features or use of this software
1851ffecc1SBen Gras  *    must display the following acknowledgement:
1951ffecc1SBen Gras  *      This product includes software developed for the NetBSD Project by
2051ffecc1SBen Gras  *      Wasabi Systems, Inc.
2151ffecc1SBen Gras  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
2251ffecc1SBen Gras  *    or promote products derived from this software without specific prior
2351ffecc1SBen Gras  *    written permission.
2451ffecc1SBen Gras  *
2551ffecc1SBen Gras  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND ANY
2651ffecc1SBen Gras  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2751ffecc1SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2851ffecc1SBen Gras  * ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC. BE
2951ffecc1SBen Gras  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3051ffecc1SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3151ffecc1SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3251ffecc1SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3351ffecc1SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3451ffecc1SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3551ffecc1SBen Gras  * THE POSSIBILITY OF SUCH DAMAGE.
3651ffecc1SBen Gras  */
3751ffecc1SBen Gras 
3851ffecc1SBen Gras #include <sys/cdefs.h>
3951ffecc1SBen Gras #ifndef lint
40*0c3ae37fSLionel Sambuc __RCSID("$NetBSD: inchstr.c,v 1.6 2012/04/21 11:33:16 blymn Exp $");
4151ffecc1SBen Gras #endif				/* not lint */
4251ffecc1SBen Gras 
4351ffecc1SBen Gras #include "curses.h"
4451ffecc1SBen Gras #include "curses_private.h"
4551ffecc1SBen Gras 
4651ffecc1SBen Gras #ifndef _CURSES_USE_MACROS
4751ffecc1SBen Gras 
4851ffecc1SBen Gras /*
4951ffecc1SBen Gras  * inchstr, inchnstr --
5051ffecc1SBen Gras  *	Return an array of characters at cursor position from stdscr.
5151ffecc1SBen Gras  */
5251ffecc1SBen Gras __warn_references(inchstr,
5351ffecc1SBen Gras     "warning: this program uses inchstr(), which is unsafe.")
5451ffecc1SBen Gras int
inchstr(chtype * chstr)5551ffecc1SBen Gras inchstr(chtype *chstr)
5651ffecc1SBen Gras {
5751ffecc1SBen Gras 	return winchstr(stdscr, chstr);
5851ffecc1SBen Gras }
5951ffecc1SBen Gras 
6051ffecc1SBen Gras int
inchnstr(chtype * chstr,int n)6151ffecc1SBen Gras inchnstr(chtype *chstr, int n)
6251ffecc1SBen Gras {
6351ffecc1SBen Gras 	return winchnstr(stdscr, chstr, n);
6451ffecc1SBen Gras }
6551ffecc1SBen Gras 
6651ffecc1SBen Gras /*
6751ffecc1SBen Gras  * mvinchstr, mvinchnstr --
6851ffecc1SBen Gras  *      Return an array of characters at position (y, x) from stdscr.
6951ffecc1SBen Gras  */
7051ffecc1SBen Gras __warn_references(mvinchstr,
7151ffecc1SBen Gras     "warning: this program uses mvinchstr(), which is unsafe.")
7251ffecc1SBen Gras int
mvinchstr(int y,int x,chtype * chstr)7351ffecc1SBen Gras mvinchstr(int y, int x, chtype *chstr)
7451ffecc1SBen Gras {
7551ffecc1SBen Gras 	return mvwinchstr(stdscr, y, x, chstr);
7651ffecc1SBen Gras }
7751ffecc1SBen Gras 
7851ffecc1SBen Gras int
mvinchnstr(int y,int x,chtype * chstr,int n)7951ffecc1SBen Gras mvinchnstr(int y, int x, chtype *chstr, int n)
8051ffecc1SBen Gras {
8151ffecc1SBen Gras 	return mvwinchnstr(stdscr, y, x, chstr, n);
8251ffecc1SBen Gras }
8351ffecc1SBen Gras 
8451ffecc1SBen Gras /*
8551ffecc1SBen Gras  * mvwinchstr, mvwinchnstr --
8651ffecc1SBen Gras  *      Return an array characters at position (y, x) from the given window.
8751ffecc1SBen Gras  */
8851ffecc1SBen Gras __warn_references(mvwinchstr,
8951ffecc1SBen Gras     "warning: this program uses mvwinchstr(), which is unsafe.")
9051ffecc1SBen Gras int
mvwinchstr(WINDOW * win,int y,int x,chtype * chstr)9151ffecc1SBen Gras mvwinchstr(WINDOW *win, int y, int x, chtype *chstr)
9251ffecc1SBen Gras {
9351ffecc1SBen Gras 	if (wmove(win, y, x) == ERR)
9451ffecc1SBen Gras 		return ERR;
9551ffecc1SBen Gras 
9651ffecc1SBen Gras 	return winchstr(win, chstr);
9751ffecc1SBen Gras }
9851ffecc1SBen Gras 
9951ffecc1SBen Gras int
mvwinchnstr(WINDOW * win,int y,int x,chtype * chstr,int n)10051ffecc1SBen Gras mvwinchnstr(WINDOW *win, int y, int x, chtype *chstr, int n)
10151ffecc1SBen Gras {
10251ffecc1SBen Gras 	if (wmove(win, y, x) == ERR)
10351ffecc1SBen Gras 		return ERR;
10451ffecc1SBen Gras 
10551ffecc1SBen Gras 	return winchnstr(win, chstr, n);
10651ffecc1SBen Gras }
10751ffecc1SBen Gras 
10851ffecc1SBen Gras #endif	/* _CURSES_USE_MACROS */
10951ffecc1SBen Gras 
11051ffecc1SBen Gras /*
11151ffecc1SBen Gras  * winchstr, winchnstr --
11251ffecc1SBen Gras  *	Return an array of characters at cursor position.
11351ffecc1SBen Gras  */
11451ffecc1SBen Gras __warn_references(winchstr,
11551ffecc1SBen Gras     "warning: this program uses winchstr(), which is unsafe.")
11651ffecc1SBen Gras int
winchstr(WINDOW * win,chtype * chstr)11751ffecc1SBen Gras winchstr(WINDOW *win, chtype *chstr)
11851ffecc1SBen Gras {
11951ffecc1SBen Gras 
12051ffecc1SBen Gras 	return winchnstr(win, chstr, -1);
12151ffecc1SBen Gras }
12251ffecc1SBen Gras 
12351ffecc1SBen Gras /*
12451ffecc1SBen Gras  * XXX: This should go in a manpage!
12551ffecc1SBen Gras  * - SUSv2/xcurses doesn't document whether the trailing 0 is included
12651ffecc1SBen Gras  *   in the length count or not.  For safety's sake it _is_ included.
12751ffecc1SBen Gras  */
12851ffecc1SBen Gras int
winchnstr(WINDOW * win,chtype * chstr,int n)12951ffecc1SBen Gras winchnstr(WINDOW *win, chtype *chstr, int n)
13051ffecc1SBen Gras {
13151ffecc1SBen Gras 	__LDATA	*end, *start;
13251ffecc1SBen Gras 	int epos;
13351ffecc1SBen Gras 
13451ffecc1SBen Gras 	if (chstr == NULL)
13551ffecc1SBen Gras 		return ERR;
13651ffecc1SBen Gras 
13751ffecc1SBen Gras 	start = &win->alines[win->cury]->line[win->curx];
13851ffecc1SBen Gras 	/* (n - 1) to leave room for the trailing 0 element */
13951ffecc1SBen Gras 	if (n < 0 || (n - 1) > win->maxx - win->curx - 1)
14051ffecc1SBen Gras 		epos = win->maxx - 1;
14151ffecc1SBen Gras 	else
14251ffecc1SBen Gras 		/* extra -1 for trailing NUL */
14351ffecc1SBen Gras 		epos = win->curx + n -1 - 1;
14451ffecc1SBen Gras 	end = &win->alines[win->cury]->line[epos];
14551ffecc1SBen Gras 
14651ffecc1SBen Gras 	while (start <= end) {
147*0c3ae37fSLionel Sambuc 		/* or in the attributes but strip out internal flags */
148*0c3ae37fSLionel Sambuc #ifdef HAVE_WCHAR
149*0c3ae37fSLionel Sambuc 		*chstr = start->ch | (start->attr & ~__ACS_IS_WACS);
150*0c3ae37fSLionel Sambuc #else
151*0c3ae37fSLionel Sambuc 		*chstr = start->ch | start->attr;
152*0c3ae37fSLionel Sambuc #endif
15351ffecc1SBen Gras 		chstr++;
15451ffecc1SBen Gras 		start++;
15551ffecc1SBen Gras 	}
15651ffecc1SBen Gras 	*chstr = 0;
15751ffecc1SBen Gras 
15851ffecc1SBen Gras 	return OK;
15951ffecc1SBen Gras }
160