xref: /netbsd-src/lib/libcurses/inwstr.c (revision 6348e3f32a4431b45d07e1da16fd7b12725a45e7)
1*6348e3f3Sblymn /*   $NetBSD: inwstr.c,v 1.11 2024/12/23 02:58:03 blymn Exp $ */
2fa0b432bSblymn 
3fa0b432bSblymn /*
4fa0b432bSblymn  * Copyright (c) 2005 The NetBSD Foundation Inc.
5fa0b432bSblymn  * All rights reserved.
6fa0b432bSblymn  *
7fa0b432bSblymn  * This code is derived from code donated to the NetBSD Foundation
8fa0b432bSblymn  * by Ruibiao Qiu <ruibiao@arl.wustl.edu,ruibiao@gmail.com>.
9fa0b432bSblymn  *
10fa0b432bSblymn  *
11fa0b432bSblymn  * Redistribution and use in source and binary forms, with or without
12fa0b432bSblymn  * modification, are permitted provided that the following conditions
13fa0b432bSblymn  * are met:
14fa0b432bSblymn  * 1. Redistributions of source code must retain the above copyright
15fa0b432bSblymn  *	notice, this list of conditions and the following disclaimer.
16fa0b432bSblymn  * 2. Redistributions in binary form must reproduce the above copyright
17fa0b432bSblymn  *	notice, this list of conditions and the following disclaimer in the
18fa0b432bSblymn  *	documentation and/or other materials provided with the distribution.
19fa0b432bSblymn  * 3. Neither the name of the NetBSD Foundation nor the names of its
20fa0b432bSblymn  *	contributors may be used to endorse or promote products derived
21fa0b432bSblymn  *	from this software without specific prior written permission.
22fa0b432bSblymn  *
23fa0b432bSblymn  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
24fa0b432bSblymn  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25fa0b432bSblymn  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26fa0b432bSblymn  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27fa0b432bSblymn  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28fa0b432bSblymn  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29fa0b432bSblymn  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30fa0b432bSblymn  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31fa0b432bSblymn  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32fa0b432bSblymn  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33fa0b432bSblymn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34fa0b432bSblymn  * SUCH DAMAGE.
35fa0b432bSblymn  */
36fa0b432bSblymn 
37fa0b432bSblymn #include <sys/cdefs.h>
38fa0b432bSblymn #ifndef lint
39*6348e3f3Sblymn __RCSID("$NetBSD: inwstr.c,v 1.11 2024/12/23 02:58:03 blymn Exp $");
40fa0b432bSblymn #endif						  /* not lint */
41fa0b432bSblymn 
42fa0b432bSblymn #include "curses.h"
43fa0b432bSblymn #include "curses_private.h"
44fa0b432bSblymn 
45fa0b432bSblymn /*
46fa0b432bSblymn  * inwstr, innwstr --
47fa0b432bSblymn  *	Return a string of wide characters at cursor position from stdscr.
48fa0b432bSblymn  */
49fa0b432bSblymn __warn_references(inwstr,
5054391ee9Srillig 	"warning: this program uses inwstr(), which is unsafe.")
51fa0b432bSblymn int
52fa0b432bSblymn inwstr(wchar_t *wstr)
53fa0b432bSblymn {
54fa0b432bSblymn 	return winwstr(stdscr, wstr);
55fa0b432bSblymn }
56fa0b432bSblymn 
57fa0b432bSblymn int
58fa0b432bSblymn innwstr(wchar_t *wstr, int n)
59fa0b432bSblymn {
60fa0b432bSblymn 	return winnwstr(stdscr, wstr, n);
61fa0b432bSblymn }
62fa0b432bSblymn 
63fa0b432bSblymn /*
64fa0b432bSblymn  * mvinwstr, mvinnwstr --
65fa0b432bSblymn  *  Return a string of wide characters at position (y, x) from stdscr.
66fa0b432bSblymn  */
67fa0b432bSblymn __warn_references(mvinwstr,
6854391ee9Srillig 	"warning: this program uses mvinwstr(), which is unsafe.")
69fa0b432bSblymn int
70fa0b432bSblymn mvinwstr(int y, int x, wchar_t *wstr)
71fa0b432bSblymn {
72fa0b432bSblymn 	return mvwinwstr(stdscr, y, x, wstr);
73fa0b432bSblymn }
74fa0b432bSblymn 
75fa0b432bSblymn int
76fa0b432bSblymn mvinnwstr(int y, int x, wchar_t *wstr, int n)
77fa0b432bSblymn {
78fa0b432bSblymn 	return mvwinnwstr(stdscr, y, x, wstr, n);
79fa0b432bSblymn }
80fa0b432bSblymn 
81fa0b432bSblymn /*
82fa0b432bSblymn  * mvwinwstr, mvwinnwstr --
83fa0b432bSblymn  *  Return an array wide characters at position (y, x) from the given window.
84fa0b432bSblymn  */
85fa0b432bSblymn __warn_references(mvwinwstr,
8654391ee9Srillig 	"warning: this program uses mvwinwstr(), which is unsafe.")
87fa0b432bSblymn int
88fa0b432bSblymn mvwinwstr(WINDOW *win, int y, int x, wchar_t *wstr)
89fa0b432bSblymn {
902a780e62Sblymn 	if (wmove(win, y, x) == ERR)
91fa0b432bSblymn 		return ERR;
92fa0b432bSblymn 
93fa0b432bSblymn 	return winwstr(win, wstr);
94fa0b432bSblymn }
95fa0b432bSblymn 
96fa0b432bSblymn int
97fa0b432bSblymn mvwinnwstr(WINDOW *win, int y, int x, wchar_t *wstr, int n)
98fa0b432bSblymn {
992a780e62Sblymn 	if (wmove(win, y, x) == ERR)
100fa0b432bSblymn 		return ERR;
101fa0b432bSblymn 
102fa0b432bSblymn 	return winnwstr(win, wstr, n);
103fa0b432bSblymn }
104fa0b432bSblymn 
105fa0b432bSblymn /*
106fa0b432bSblymn  * winwstr, winnwstr --
107fa0b432bSblymn  *	Return a string of wide characters at cursor position.
108fa0b432bSblymn  */
109fa0b432bSblymn __warn_references(winwstr,
11054391ee9Srillig 	"warning: this program uses winwstr(), which is unsafe.")
111fa0b432bSblymn int
112fa0b432bSblymn winwstr(WINDOW *win, wchar_t *wstr)
113fa0b432bSblymn {
114fa0b432bSblymn 
115fa0b432bSblymn 	return winnwstr(win, wstr, -1);
116fa0b432bSblymn }
117fa0b432bSblymn 
118fa0b432bSblymn /*
119fa0b432bSblymn  * - winnwstr() returns the number of characters copied only of if it is
120fa0b432bSblymn  *   called with n >= 0 (ie, as in_wchnstr(), mvin_wchnstr(), mvwin_wchnstr()
121fa0b432bSblymn  *   or win_wchnstr()).  If N < 0, it returns `OK'.
122fa0b432bSblymn  * - SUSv2/xcurses doesn't document whether the trailing NUL is included
123fa0b432bSblymn  *   in the length count or not.  For safety's sake it _is_ included.
124fa0b432bSblymn  * - This implementation does not (yet) support multi-byte characters
125fa0b432bSblymn  *   strings.
126fa0b432bSblymn  */
127fa0b432bSblymn int
128fa0b432bSblymn winnwstr(WINDOW *win, wchar_t *wstr, int n)
129fa0b432bSblymn {
130fa0b432bSblymn 	__LDATA	*start;
131fa0b432bSblymn 	int x, cw, cnt;
132fa0b432bSblymn 	wchar_t *wcp;
133fa0b432bSblymn 
134*6348e3f3Sblymn 	if (__predict_false(win == NULL))
135*6348e3f3Sblymn 		return ERR;
136*6348e3f3Sblymn 
137fa0b432bSblymn 	if (wstr == NULL)
138fa0b432bSblymn 		return ERR;
139fa0b432bSblymn 
14043d5eb45Sroy 	start = &win->alines[win->cury]->line[win->curx];
141fa0b432bSblymn 	x = win->curx;
142f1942931Sblymn 	cw = start->wcols;
143fa0b432bSblymn 	if (cw < 0) {
144fa0b432bSblymn 		start += cw;
145fa0b432bSblymn 		x += cw;
146fa0b432bSblymn 	}
147fa0b432bSblymn     cnt = 0;
148fa0b432bSblymn 	wcp = wstr;
149fa0b432bSblymn 	/* (n - 1) to leave room for the trailing 0 element */
150fa0b432bSblymn 	while ((x < win->maxx) && ((n < 0) || ((n > 1) && (cnt < n - 1)))) {
151f1942931Sblymn 		cw = start->wcols;
152fa0b432bSblymn 		*wcp = start->ch;
153fa0b432bSblymn 		wcp++;
154fa0b432bSblymn 		cnt++;
155fa0b432bSblymn 		x += cw;
156fa0b432bSblymn 		if (x < win->maxx)
157fa0b432bSblymn 			start += cw;
158fa0b432bSblymn 	}
159fa0b432bSblymn 	*wcp = L'\0';
160fa0b432bSblymn 
161fa0b432bSblymn 	if (n < 0)
162fa0b432bSblymn 		return OK;
163fa0b432bSblymn 	else
164fa0b432bSblymn 		return cnt;
165fa0b432bSblymn }
166