xref: /netbsd/lib/libcurses/inchstr.c (revision 165f4767)
1 /*	$NetBSD: inchstr.c,v 1.11 2021/08/15 15:12:36 rillig Exp $	*/
2 
3 /*
4  * Copyright 2001 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Simon Burge for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed for the NetBSD Project by
20  *      Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND ANY
26  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC. BE
29  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35  * THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 #ifndef lint
40 __RCSID("$NetBSD: inchstr.c,v 1.11 2021/08/15 15:12:36 rillig Exp $");
41 #endif				/* not lint */
42 
43 #include "curses.h"
44 #include "curses_private.h"
45 
46 #ifndef _CURSES_USE_MACROS
47 
48 /*
49  * inchstr, inchnstr --
50  *	Return an array of characters at cursor position from stdscr.
51  */
52 __warn_references(inchstr,
53     "warning: this program uses inchstr(), which is unsafe.")
54 int
inchstr(chtype * chstr)55 inchstr(chtype *chstr)
56 {
57 
58 	return winchstr(stdscr, chstr);
59 }
60 
61 int
inchnstr(chtype * chstr,int n)62 inchnstr(chtype *chstr, int n)
63 {
64 
65 	return winchnstr(stdscr, chstr, n);
66 }
67 
68 /*
69  * mvinchstr, mvinchnstr --
70  *      Return an array of characters at position (y, x) from stdscr.
71  */
72 __warn_references(mvinchstr,
73     "warning: this program uses mvinchstr(), which is unsafe.")
74 int
mvinchstr(int y,int x,chtype * chstr)75 mvinchstr(int y, int x, chtype *chstr)
76 {
77 
78 	return mvwinchstr(stdscr, y, x, chstr);
79 }
80 
81 int
mvinchnstr(int y,int x,chtype * chstr,int n)82 mvinchnstr(int y, int x, chtype *chstr, int n)
83 {
84 
85 	return mvwinchnstr(stdscr, y, x, chstr, n);
86 }
87 
88 /*
89  * mvwinchstr, mvwinchnstr --
90  *      Return an array characters at position (y, x) from the given window.
91  */
92 __warn_references(mvwinchstr,
93     "warning: this program uses mvwinchstr(), which is unsafe.")
94 int
mvwinchstr(WINDOW * win,int y,int x,chtype * chstr)95 mvwinchstr(WINDOW *win, int y, int x, chtype *chstr)
96 {
97 
98 	if (wmove(win, y, x) == ERR)
99 		return ERR;
100 
101 	return winchstr(win, chstr);
102 }
103 
104 int
mvwinchnstr(WINDOW * win,int y,int x,chtype * chstr,int n)105 mvwinchnstr(WINDOW *win, int y, int x, chtype *chstr, int n)
106 {
107 
108 	if (wmove(win, y, x) == ERR)
109 		return ERR;
110 
111 	return winchnstr(win, chstr, n);
112 }
113 
114 #endif	/* _CURSES_USE_MACROS */
115 
116 /*
117  * winchstr, winchnstr --
118  *	Return an array of characters at cursor position.
119  */
120 __warn_references(winchstr,
121     "warning: this program uses winchstr(), which is unsafe.")
122 int
winchstr(WINDOW * win,chtype * chstr)123 winchstr(WINDOW *win, chtype *chstr)
124 {
125 
126 	return winchnstr(win, chstr, -1);
127 }
128 
129 /*
130  * XXX: This should go in a manpage!
131  * - SUSv2/xcurses doesn't document whether the trailing 0 is included
132  *   in the length count or not.  For safety's sake it _is_ included.
133  */
134 int
winchnstr(WINDOW * win,chtype * chstr,int n)135 winchnstr(WINDOW *win, chtype *chstr, int n)
136 {
137 	__LDATA	*end, *start;
138 	int epos;
139 
140 	if (chstr == NULL)
141 		return ERR;
142 
143 	start = &win->alines[win->cury]->line[win->curx];
144 	/* (n - 1) to leave room for the trailing 0 element */
145 	if (n < 0 || (n - 1) > win->maxx - win->curx - 1)
146 		epos = win->maxx - 1;
147 	else
148 		/* extra -1 for trailing NUL */
149 		epos = win->curx + n -1 - 1;
150 	end = &win->alines[win->cury]->line[epos];
151 
152 	while (start <= end) {
153 		/* or in the attributes but strip out internal flags */
154 #ifdef HAVE_WCHAR
155 		*chstr = start->ch | (start->attr & ~__ACS_IS_WACS);
156 #else
157 		*chstr = start->ch | start->attr;
158 #endif
159 		chstr++;
160 		start++;
161 	}
162 	*chstr = 0;
163 
164 	return OK;
165 }
166