1/*   $NetBSD: inwstr.c,v 1.10 2022/01/25 03:05:06 blymn Exp $ */
2
3/*
4 * Copyright (c) 2005 The NetBSD Foundation Inc.
5 * All rights reserved.
6 *
7 * This code is derived from code donated to the NetBSD Foundation
8 * by Ruibiao Qiu <ruibiao@arl.wustl.edu,ruibiao@gmail.com>.
9 *
10 *
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.
22 *
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.
35 */
36
37#include <sys/cdefs.h>
38#ifndef lint
39__RCSID("$NetBSD: inwstr.c,v 1.10 2022/01/25 03:05:06 blymn Exp $");
40#endif						  /* not lint */
41
42#include "curses.h"
43#include "curses_private.h"
44
45/*
46 * inwstr, innwstr --
47 *	Return a string of wide characters at cursor position from stdscr.
48 */
49__warn_references(inwstr,
50	"warning: this program uses inwstr(), which is unsafe.")
51int
52inwstr(wchar_t *wstr)
53{
54	return winwstr(stdscr, wstr);
55}
56
57int
58innwstr(wchar_t *wstr, int n)
59{
60	return winnwstr(stdscr, wstr, n);
61}
62
63/*
64 * mvinwstr, mvinnwstr --
65 *  Return a string of wide characters at position (y, x) from stdscr.
66 */
67__warn_references(mvinwstr,
68	"warning: this program uses mvinwstr(), which is unsafe.")
69int
70mvinwstr(int y, int x, wchar_t *wstr)
71{
72	return mvwinwstr(stdscr, y, x, wstr);
73}
74
75int
76mvinnwstr(int y, int x, wchar_t *wstr, int n)
77{
78	return mvwinnwstr(stdscr, y, x, wstr, n);
79}
80
81/*
82 * mvwinwstr, mvwinnwstr --
83 *  Return an array wide characters at position (y, x) from the given window.
84 */
85__warn_references(mvwinwstr,
86	"warning: this program uses mvwinwstr(), which is unsafe.")
87int
88mvwinwstr(WINDOW *win, int y, int x, wchar_t *wstr)
89{
90	if (wmove(win, y, x) == ERR)
91		return ERR;
92
93	return winwstr(win, wstr);
94}
95
96int
97mvwinnwstr(WINDOW *win, int y, int x, wchar_t *wstr, int n)
98{
99	if (wmove(win, y, x) == ERR)
100		return ERR;
101
102	return winnwstr(win, wstr, n);
103}
104
105/*
106 * winwstr, winnwstr --
107 *	Return a string of wide characters at cursor position.
108 */
109__warn_references(winwstr,
110	"warning: this program uses winwstr(), which is unsafe.")
111int
112winwstr(WINDOW *win, wchar_t *wstr)
113{
114
115	return winnwstr(win, wstr, -1);
116}
117
118/*
119 * - winnwstr() returns the number of characters copied only of if it is
120 *   called with n >= 0 (ie, as in_wchnstr(), mvin_wchnstr(), mvwin_wchnstr()
121 *   or win_wchnstr()).  If N < 0, it returns `OK'.
122 * - SUSv2/xcurses doesn't document whether the trailing NUL is included
123 *   in the length count or not.  For safety's sake it _is_ included.
124 * - This implementation does not (yet) support multi-byte characters
125 *   strings.
126 */
127int
128winnwstr(WINDOW *win, wchar_t *wstr, int n)
129{
130	__LDATA	*start;
131	int x, cw, cnt;
132	wchar_t *wcp;
133
134	if (wstr == NULL)
135		return ERR;
136
137	start = &win->alines[win->cury]->line[win->curx];
138	x = win->curx;
139	cw = start->wcols;
140	if (cw < 0) {
141		start += cw;
142		x += cw;
143	}
144    cnt = 0;
145	wcp = wstr;
146	/* (n - 1) to leave room for the trailing 0 element */
147	while ((x < win->maxx) && ((n < 0) || ((n > 1) && (cnt < n - 1)))) {
148		cw = start->wcols;
149		*wcp = start->ch;
150		wcp++;
151		cnt++;
152		x += cw;
153		if (x < win->maxx)
154			start += cw;
155	}
156	*wcp = L'\0';
157
158	if (n < 0)
159		return OK;
160	else
161		return cnt;
162}
163