xref: /minix3/lib/libcurses/instr.c (revision 0c3ae37f525eceade8dc047e551f5c9cb33faeb1)
1*0c3ae37fSLionel Sambuc /*	$NetBSD: instr.c,v 1.4 2011/08/07 10:54:53 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: instr.c,v 1.4 2011/08/07 10:54:53 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  * instr, innstr --
5051ffecc1SBen Gras  *	Return a string of characters at cursor position from stdscr.
5151ffecc1SBen Gras  */
5251ffecc1SBen Gras __warn_references(instr,
5351ffecc1SBen Gras     "warning: this program uses instr(), which is unsafe.")
5451ffecc1SBen Gras int
instr(char * str)5551ffecc1SBen Gras instr(char *str)
5651ffecc1SBen Gras {
5751ffecc1SBen Gras 	return winstr(stdscr, str);
5851ffecc1SBen Gras }
5951ffecc1SBen Gras 
6051ffecc1SBen Gras int
innstr(char * str,int n)6151ffecc1SBen Gras innstr(char *str, int n)
6251ffecc1SBen Gras {
6351ffecc1SBen Gras 	return winnstr(stdscr, str, n);
6451ffecc1SBen Gras }
6551ffecc1SBen Gras 
6651ffecc1SBen Gras /*
6751ffecc1SBen Gras  * mvinstr, mvinnstr --
6851ffecc1SBen Gras  *      Return a string of characters at position (y, x) from stdscr.
6951ffecc1SBen Gras  *	XXX: should be multi-byte characters for SUSv2.
7051ffecc1SBen Gras  */
7151ffecc1SBen Gras __warn_references(mvinstr,
7251ffecc1SBen Gras     "warning: this program uses mvinstr(), which is unsafe.")
7351ffecc1SBen Gras int
mvinstr(int y,int x,char * str)7451ffecc1SBen Gras mvinstr(int y, int x, char *str)
7551ffecc1SBen Gras {
7651ffecc1SBen Gras 	return mvwinstr(stdscr, y, x, str);
7751ffecc1SBen Gras }
7851ffecc1SBen Gras 
7951ffecc1SBen Gras int
mvinnstr(int y,int x,char * str,int n)8051ffecc1SBen Gras mvinnstr(int y, int x, char *str, int n)
8151ffecc1SBen Gras {
8251ffecc1SBen Gras 	return mvwinnstr(stdscr, y, x, str, n);
8351ffecc1SBen Gras }
8451ffecc1SBen Gras 
8551ffecc1SBen Gras /*
8651ffecc1SBen Gras  * mvwinstr, mvwinnstr --
8751ffecc1SBen Gras  *      Return an array characters at position (y, x) from the given window.
8851ffecc1SBen Gras  *	XXX: should be multi-byte characters for SUSv2.
8951ffecc1SBen Gras  */
9051ffecc1SBen Gras __warn_references(mvwinstr,
9151ffecc1SBen Gras     "warning: this program uses mvwinstr(), which is unsafe.")
9251ffecc1SBen Gras int
mvwinstr(WINDOW * win,int y,int x,char * str)9351ffecc1SBen Gras mvwinstr(WINDOW *win, int y, int x, char *str)
9451ffecc1SBen Gras {
9551ffecc1SBen Gras 	if (wmove(win, y, x) == ERR)
9651ffecc1SBen Gras 		return ERR;
9751ffecc1SBen Gras 
9851ffecc1SBen Gras 	return winstr(win, str);
9951ffecc1SBen Gras }
10051ffecc1SBen Gras 
10151ffecc1SBen Gras int
mvwinnstr(WINDOW * win,int y,int x,char * str,int n)10251ffecc1SBen Gras mvwinnstr(WINDOW *win, int y, int x, char *str, int n)
10351ffecc1SBen Gras {
10451ffecc1SBen Gras 	if (wmove(win, y, x) == ERR)
10551ffecc1SBen Gras 		return ERR;
10651ffecc1SBen Gras 
10751ffecc1SBen Gras 	return winnstr(win, str, n);
10851ffecc1SBen Gras }
10951ffecc1SBen Gras 
11051ffecc1SBen Gras #endif	/* _CURSES_USE_MACROS */
11151ffecc1SBen Gras 
11251ffecc1SBen Gras /*
11351ffecc1SBen Gras  * winstr, winnstr --
11451ffecc1SBen Gras  *	Return a string of characters at cursor position.
11551ffecc1SBen Gras  *	XXX: should be multi-byte characters for SUSv2.
11651ffecc1SBen Gras  */
11751ffecc1SBen Gras __warn_references(winstr,
11851ffecc1SBen Gras     "warning: this program uses winstr(), which is unsafe.")
11951ffecc1SBen Gras int
winstr(WINDOW * win,char * str)12051ffecc1SBen Gras winstr(WINDOW *win, char *str)
12151ffecc1SBen Gras {
12251ffecc1SBen Gras 
12351ffecc1SBen Gras 	return winnstr(win, str, -1);
12451ffecc1SBen Gras }
12551ffecc1SBen Gras 
12651ffecc1SBen Gras /*
12751ffecc1SBen Gras  * XXX: This should go in a manpage!
12851ffecc1SBen Gras  * - winnstr() returns the number of characters copied only of if it is
12951ffecc1SBen Gras  *   called with n >= 0 (ie, as inchnstr(), mvinchnstr(), mvwinchnstr()
13051ffecc1SBen Gras  *   or winchnstr()).  If N < 0, it returns `OK'.
13151ffecc1SBen Gras  * - SUSv2/xcurses doesn't document whether the trailing NUL is included
13251ffecc1SBen Gras  *   in the length count or not.  For safety's sake it _is_ included.
13351ffecc1SBen Gras  * - This implementation does not (yet) support multi-byte characters
13451ffecc1SBen Gras  *   strings.
13551ffecc1SBen Gras  */
13651ffecc1SBen Gras int
winnstr(WINDOW * win,char * str,int n)13751ffecc1SBen Gras winnstr(WINDOW *win, char *str, int n)
13851ffecc1SBen Gras {
13951ffecc1SBen Gras 	__LDATA	*end, *start;
140*0c3ae37fSLionel Sambuc 	int epos, sn;
14151ffecc1SBen Gras 
14251ffecc1SBen Gras 	if (str == NULL)
14351ffecc1SBen Gras 		return ERR;
14451ffecc1SBen Gras 
145*0c3ae37fSLionel Sambuc 	sn = n;
14651ffecc1SBen Gras 	start = &win->alines[win->cury]->line[win->curx];
14751ffecc1SBen Gras 	/* (n - 1) to leave room for the trailing NUL */
14851ffecc1SBen Gras 	if (n < 0 || (n - 1) > win->maxx - win->curx - 1) {
14951ffecc1SBen Gras 		epos = win->maxx - 1;
15051ffecc1SBen Gras 		n = win->maxx - win->curx;
15151ffecc1SBen Gras 	} else {
15251ffecc1SBen Gras 		/* extra -1 for trailing NUL */
15351ffecc1SBen Gras 		epos = win->curx + n - 1 - 1;
15451ffecc1SBen Gras 		n--;
15551ffecc1SBen Gras 	}
15651ffecc1SBen Gras 	end = &win->alines[win->cury]->line[epos];
15751ffecc1SBen Gras 
15851ffecc1SBen Gras 	while (start <= end) {
15951ffecc1SBen Gras 		*str = start->ch & __CHARTEXT;
16051ffecc1SBen Gras 		str++;
16151ffecc1SBen Gras 		start++;
16251ffecc1SBen Gras 	}
16351ffecc1SBen Gras 	*str = '\0';
16451ffecc1SBen Gras 
165*0c3ae37fSLionel Sambuc 	if (sn < 0)
16651ffecc1SBen Gras 		return OK;
16751ffecc1SBen Gras 	else
16851ffecc1SBen Gras 		return n;
16951ffecc1SBen Gras }
170