xref: /netbsd-src/lib/libcurses/in_wchstr.c (revision 6348e3f32a4431b45d07e1da16fd7b12725a45e7)
1 /*   $NetBSD: in_wchstr.c,v 1.11 2024/12/23 02:58:03 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: in_wchstr.c,v 1.11 2024/12/23 02:58:03 blymn Exp $");
40 #endif						  /* not lint */
41 
42 #include "curses.h"
43 #include "curses_private.h"
44 
45 /*
46  * in_wchstr, in_wchnstr --
47  *	Return an array of wide characters at cursor position from stdscr.
48  */
49 __warn_references(in_wchstr,
50 	"warning: this program uses in_wchstr(), which is unsafe.")
51 int
52 in_wchstr(cchar_t *wchstr)
53 {
54 	return win_wchstr(stdscr, wchstr);
55 }
56 
57 int
58 in_wchnstr(cchar_t *wchstr, int n)
59 {
60 	return win_wchnstr(stdscr, wchstr, n);
61 }
62 
63 /*
64  * mvin_wchstr, mv_winchnstr --
65  *  Return an array of wide characters at position (y, x) from stdscr.
66  */
67 __warn_references(mvin_wchstr,
68 	"warning: this program uses mvin_wchstr(), which is unsafe.")
69 int
70 mvin_wchstr(int y, int x, cchar_t *wchstr)
71 {
72 	return mvwin_wchstr(stdscr, y, x, wchstr);
73 }
74 
75 int
76 mvin_wchnstr(int y, int x, cchar_t *wchstr, int n)
77 {
78 	return mvwin_wchnstr(stdscr, y, x, wchstr, n);
79 }
80 
81 /*
82  * mvwin_wchstr, mvwin_wchnstr --
83  *  Return an array wide characters at position (y, x) from the given window.
84  */
85 __warn_references(mvwin_wchstr,
86 	"warning: this program uses mvwin_wchstr(), which is unsafe.")
87 int
88 mvwin_wchstr(WINDOW *win, int y, int x, cchar_t *wchstr)
89 {
90 	if (wmove(win, y, x) == ERR)
91 		return ERR;
92 
93 	return win_wchstr(win, wchstr);
94 }
95 
96 int
97 mvwin_wchnstr(WINDOW *win, int y, int x, cchar_t *wchstr, int n)
98 {
99 	if (wmove(win, y, x) == ERR)
100 		return ERR;
101 
102 	return win_wchnstr(win, wchstr, n);
103 }
104 
105 /*
106  * win_wchstr, win_wchnstr --
107  *	Return an array of characters at cursor position.
108  */
109 __warn_references(win_wchstr,
110 	"warning: this program uses win_wchstr(), which is unsafe.")
111 int
112 win_wchstr(WINDOW *win, cchar_t *wchstr)
113 {
114 
115 	return win_wchnstr(win, wchstr, -1);
116 }
117 
118 /*
119  * - SUSv2/xcurses doesn't document whether the trailing 0 is included
120  *   in the length count or not.  For safety's sake it _is_ included.
121  */
122 int
123 win_wchnstr(WINDOW *win, cchar_t *wchstr, int n)
124 {
125 	__LDATA	*start;
126 	int x = 0, cw = 0, cnt = 0;
127 	cchar_t *wcp;
128 	nschar_t *np;
129 
130 	if (__predict_false(win == NULL))
131 		return ERR;
132 
133 	if (wchstr == NULL)
134 		return ERR;
135 
136 	start = &win->alines[win->cury]->line[win->curx];
137 	x = win->curx;
138 	cw = start->wcols;
139 	if (cw < 0) {
140 		start += cw;
141 		x += cw;
142 	}
143 	wcp = wchstr;
144 	/* (n - 1) to leave room for the trailing 0 element */
145 	while ((x < win->maxx) && ((n < 0) || ((n > 1) && (cnt < n - 1)))) {
146 		cw = start->wcols;
147 		wcp->vals[0] = start->ch;
148 		wcp->attributes = start->attr;
149 		wcp->elements = 1;
150 		np = start->nsp;
151 		if (np) {
152 			do {
153 				wcp->vals[wcp->elements++] = np->ch;
154 				np = np->next;
155 			} while (np);
156 		}
157 		wcp++;
158 		cnt++;
159 		x += cw;
160 		if (x < win->maxx)
161 			start += cw;
162 	}
163 	wcp->vals[0] = L'\0';
164 	wcp->elements = 1;
165 	wcp->attributes = win->wattr;
166 
167 	return OK;
168 }
169