1 /*   $NetBSD: get_wstr.c,v 1.12 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: get_wstr.c,v 1.12 2024/12/23 02:58:03 blymn Exp $");
40 #endif                                                        /* not lint */
41 
42 #include "curses.h"
43 #include "curses_private.h"
44 
45 /* prototypes for private functions */
46 static int __wgetn_wstr(WINDOW *, wchar_t *, int);
47 
48 /*
49  * getn_wstr --
50  *        Get a string (of maximum n) characters from stdscr starting at
51  *        (cury, curx).
52  */
53 int
getn_wstr(wchar_t * wstr,int n)54 getn_wstr(wchar_t *wstr, int n)
55 {
56           return wgetn_wstr(stdscr, wstr, n);
57 }
58 
59 /*
60  * get_wstr --
61  *        Get a string from stdscr starting at (cury, curx).
62  */
63 __warn_references(get_wstr,
64           "warning: this program uses get_wstr(), which is unsafe.")
65 int
get_wstr(wchar_t * wstr)66 get_wstr(wchar_t *wstr)
67 {
68           return wget_wstr(stdscr, wstr);
69 }
70 
71 /*
72  * mvgetn_wstr --
73  *  Get a string (of maximum n) characters from stdscr starting at (y, x).
74  */
75 int
mvgetn_wstr(int y,int x,wchar_t * wstr,int n)76 mvgetn_wstr(int y, int x, wchar_t *wstr, int n)
77 {
78           return mvwgetn_wstr(stdscr, y, x, wstr, n);
79 }
80 
81 /*
82  * mvget_wstr --
83  *          Get a string from stdscr starting at (y, x).
84  */
85 __warn_references(mvget_wstr,
86           "warning: this program uses mvget_wstr(), which is unsafe.")
87 int
mvget_wstr(int y,int x,wchar_t * wstr)88 mvget_wstr(int y, int x, wchar_t *wstr)
89 {
90           return mvwget_wstr(stdscr, y, x, wstr);
91 }
92 
93 /*
94  * mvwgetn_wstr --
95  *  Get a string (of maximum n) characters from the given window starting
96  *        at (y, x).
97  */
98 int
mvwgetn_wstr(WINDOW * win,int y,int x,wchar_t * wstr,int n)99 mvwgetn_wstr(WINDOW *win, int y, int x, wchar_t *wstr, int n)
100 {
101           if (wmove(win, y, x) == ERR)
102                     return ERR;
103 
104           return wgetn_wstr(win, wstr, n);
105 }
106 
107 /*
108  * mvwget_wstr --
109  *          Get a string from the given window starting at (y, x).
110  */
111 __warn_references(mvget_wstr,
112           "warning: this program uses mvget_wstr(), which is unsafe.")
113 int
mvwget_wstr(WINDOW * win,int y,int x,wchar_t * wstr)114 mvwget_wstr(WINDOW *win, int y, int x, wchar_t *wstr)
115 {
116           if (wmove(win, y, x) == ERR)
117                     return ERR;
118 
119           return wget_wstr(win, wstr);
120 }
121 
122 /*
123  * wget_wstr --
124  *        Get a string starting at (cury, curx).
125  */
126 __warn_references(wget_wstr,
127           "warning: this program uses wget_wstr(), which is unsafe.")
128 int
wget_wstr(WINDOW * win,wchar_t * wstr)129 wget_wstr(WINDOW *win, wchar_t *wstr)
130 {
131           return __wgetn_wstr(win, wstr, -1);
132 }
133 
134 /*
135  * wgetn_wstr --
136  *        Get a string starting at (cury, curx).
137  *        Note that n <  2 means that we return ERR (SUSv2 specification).
138  */
139 int
wgetn_wstr(WINDOW * win,wchar_t * wstr,int n)140 wgetn_wstr(WINDOW *win, wchar_t *wstr, int n)
141 {
142           if (n < 1)
143                     return ERR;
144           if (n == 1) {
145                     wstr[0] = L'\0';
146                     return ERR;
147           }
148           return __wgetn_wstr(win, wstr, n);
149 }
150 
151 /*
152  * __wgetn_wstr --
153  *        The actual implementation.
154  *        Note that we include a trailing L'\0' for safety, so str will contain
155  *        at most n - 1 other characters.
156  */
157 int
__wgetn_wstr(WINDOW * win,wchar_t * wstr,int n)158 __wgetn_wstr(WINDOW *win, wchar_t *wstr, int n)
159 {
160           wchar_t *ostr, ec, kc, sc[ 2 ];
161           int oldx, remain;
162           wint_t wc;
163           cchar_t cc;
164 
165           if (__predict_false(win == NULL))
166                     return ERR;
167 
168           ostr = wstr;
169           if (erasewchar(&ec) == ERR)
170                     return ERR;
171           if (killwchar(&kc) == ERR)
172                     return ERR;
173           sc[0] = win->bch;
174           sc[1] = L'\0';
175           setcchar(&cc, sc, win->wattr, 0, NULL);
176           oldx = win->curx;
177           remain = n - 1;
178 
179           while (wget_wch(win, &wc) != ERR
180                  && wc != L'\n' && wc != L'\r') {
181                     __CTRACE(__CTRACE_INPUT,
182                         "__wgetn_wstr: win %p, char 0x%x, remain %d\n",
183                         win, wc, remain);
184                     *wstr = wc;
185                     touchline(win, win->cury, 1);
186                     if (wc == ec || wc == KEY_BACKSPACE || wc == KEY_LEFT) {
187                               *wstr = L'\0';
188                               if (wstr != ostr) {
189                                         if ((wchar_t)wc == ec) {
190                                                   mvwadd_wch(win, win->cury,
191                                                             win->curx, &cc);
192                                                   wmove(win, win->cury, win->curx - 1);
193                                         }
194                                         if (wc == KEY_BACKSPACE || wc == KEY_LEFT) {
195                                                   /* getch() displays the key sequence */
196                                                   mvwadd_wch(win, win->cury,
197                                                             win->curx - 1, &cc);
198                                                   mvwadd_wch(win, win->cury,
199                                                             win->curx - 2, &cc);
200                                                   wmove(win, win->cury, win->curx - 1);
201                                         }
202                                         wstr--;
203                                         if (n != -1) {
204                                                   /* We're counting chars */
205                                                   remain++;
206                                         }
207                               } else { /* str == ostr */
208                                         if (wc == KEY_BACKSPACE || wc == KEY_LEFT)
209                                                   /* getch() displays the other keys */
210                                                   mvwadd_wch(win, win->cury,
211                                                             win->curx - 1, &cc);
212                                         wmove(win, win->cury, oldx);
213                               }
214                     } else if (wc == kc) {
215                               *wstr = L'\0';
216                               if (wstr != ostr) {
217                                         /* getch() displays the kill character */
218                                         mvwadd_wch(win, win->cury, win->curx - 1, &cc);
219                                         /* Clear the characters from screen and str */
220                                         while (wstr != ostr) {
221                                                   mvwadd_wch(win, win->cury,
222                                                             win->curx - 1, &cc);
223                                                   wmove(win, win->cury, win->curx - 1);
224                                                   wstr--;
225                                                   if (n != -1)
226                                                             /* We're counting chars */
227                                                             remain++;
228                                         }
229                                         mvwadd_wch(win, win->cury, win->curx - 1, &cc);
230                                         wmove(win, win->cury, win->curx - 1);
231                               } else
232                                         /* getch() displays the kill character */
233                                         mvwadd_wch( win, win->cury, oldx, &cc );
234                               wmove(win, win->cury, oldx);
235                     } else if (wc >= KEY_MIN && wc <= KEY_MAX) {
236                               /* get_wch() displays these characters */
237                               mvwadd_wch( win, win->cury, win->curx - 1, &cc );
238                               wmove(win, win->cury, win->curx - 1);
239                     } else {
240                               if (remain) {
241                                         wstr++;
242                                         remain--;
243                               } else {
244                                         mvwadd_wch(win, win->cury, win->curx - 1, &cc);
245                                         wmove(win, win->cury, win->curx - 1);
246                               }
247                     }
248           }
249 
250           if (wc == ERR) {
251                     *wstr = L'\0';
252                     return ERR;
253           }
254           *wstr = L'\0';
255           return OK;
256 }
257