in_wchstr.c revision 1.4.14.1 1 /* $NetBSD: in_wchstr.c,v 1.4.14.1 2019/06/10 22:05:22 christos 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 (at) arl.wustl.edu,ruibiao (at) 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.4.14.1 2019/06/10 22:05:22 christos 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 (wchstr == NULL)
131 return ERR;
132
133 start = &win->alines[win->cury]->line[win->curx];
134 x = win->curx;
135 cw = WCOL(*start);
136 if (cw < 0) {
137 start += cw;
138 x += cw;
139 }
140 wcp = wchstr;
141 /* (n - 1) to leave room for the trailing 0 element */
142 while ((x < win->maxx) && ((n < 0) || ((n > 1) && (cnt < n - 1)))) {
143 cw = WCOL(*start);
144 wcp->vals[0] = start->ch;
145 wcp->attributes = start->attr;
146 wcp->elements = 1;
147 np = start->nsp;
148 if (np) {
149 do {
150 wcp->vals[wcp->elements++] = np->ch;
151 np = np->next;
152 } while (np);
153 }
154 wcp++;
155 cnt++;
156 x += cw;
157 if (x < win->maxx)
158 start += cw;
159 }
160 wcp->vals[0] = L'\0';
161 wcp->elements = 1;
162 wcp->attributes = win->wattr;
163
164 return OK;
165 }
166