1 /* $NetBSD: getstr.c,v 1.19 2007/01/21 13:25:36 jdc Exp $ */
4 * Copyright (c) 1981, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
36 static char sccsid
[] = "@(#)getstr.c 8.2 (Berkeley) 5/4/94";
38 __RCSID("$NetBSD: getstr.c,v 1.19 2007/01/21 13:25:36 jdc Exp $");
43 #include "curses_private.h"
45 #ifndef _CURSES_USE_MACROS
49 * Get a string (of maximum n) characters from stdscr starting at
53 getnstr(char *str
, int n
)
55 return wgetnstr(stdscr
, str
, n
);
60 * Get a string from stdscr starting at (cury, curx).
62 __warn_references(getstr
,
63 "warning: this program uses getstr(), which is unsafe.")
67 return wgetstr(stdscr
, str
);
72 * Get a string (of maximum n) characters from stdscr starting at (y, x).
75 mvgetnstr(int y
, int x
, char *str
, int n
)
77 return mvwgetnstr(stdscr
, y
, x
, str
, n
);
82 * Get a string from stdscr starting at (y, x).
84 __warn_references(mvgetstr
,
85 "warning: this program uses mvgetstr(), which is unsafe.")
87 mvgetstr(int y
, int x
, char *str
)
89 return mvwgetstr(stdscr
, y
, x
, str
);
94 * Get a string (of maximum n) characters from the given window starting
98 mvwgetnstr(WINDOW
*win
, int y
, int x
, char *str
, int n
)
100 if (wmove(win
, y
, x
) == ERR
)
103 return wgetnstr(win
, str
, n
);
108 * Get a string from the given window starting at (y, x).
110 __warn_references(mvgetstr
,
111 "warning: this program uses mvgetstr(), which is unsafe.")
113 mvwgetstr(WINDOW
*win
, int y
, int x
, char *str
)
115 if (wmove(win
, y
, x
) == ERR
)
118 return wgetstr(win
, str
);
125 * Get a string starting at (cury, curx).
127 __warn_references(wgetstr
,
128 "warning: this program uses wgetstr(), which is unsafe.")
130 wgetstr(WINDOW
*win
, char *str
)
132 return __wgetnstr(win
, str
, -1);
137 * Get a string starting at (cury, curx).
138 * Note that n < 2 means that we return ERR (SUSv2 specification).
141 wgetnstr(WINDOW
*win
, char *str
, int n
)
149 return __wgetnstr(win
, str
, n
);
154 * The actual implementation.
155 * Note that we include a trailing '\0' for safety, so str will contain
156 * at most n - 1 other characters.
157 * XXX: character deletion from screen is based on how the characters
158 * are displayed by wgetch().
161 __wgetnstr(WINDOW
*win
, char *str
, int n
)
170 _DIAGASSERT(n
== -1 || n
> 1);
173 while ((c
= wgetch(win
)) != ERR
&& c
!= '\n' && c
!= '\r') {
175 __CTRACE(__CTRACE_INPUT
,
176 "__wgetnstr: win %p, char 0x%x, remain %d\n",
180 touchline(win
, win
->cury
, 1);
181 if (c
== ec
|| c
== KEY_BACKSPACE
|| c
== KEY_LEFT
) {
184 if ((char) c
== ec
) {
185 mvwaddch(win
, win
->cury
, win
->curx
,
187 wmove(win
, win
->cury
, win
->curx
- 1);
189 if (c
== KEY_BACKSPACE
|| c
== KEY_LEFT
) {
190 /* getch() displays the key sequence */
191 mvwaddch(win
, win
->cury
, win
->curx
- 1,
193 mvwaddch(win
, win
->cury
, win
->curx
- 2,
195 wmove(win
, win
->cury
, win
->curx
- 1);
199 /* We're counting chars */
202 } else { /* str == ostr */
203 if (c
== KEY_BACKSPACE
|| c
== KEY_LEFT
)
204 /* getch() displays the other keys */
205 mvwaddch(win
, win
->cury
, win
->curx
- 1,
207 wmove(win
, win
->cury
, oldx
);
209 } else if (c
== kc
) {
212 /* getch() displays the kill character */
213 mvwaddch(win
, win
->cury
, win
->curx
- 1, ' ');
214 /* Clear the characters from screen and str */
215 while (str
!= ostr
) {
216 mvwaddch(win
, win
->cury
, win
->curx
- 1,
218 wmove(win
, win
->cury
, win
->curx
- 1);
221 /* We're counting chars */
224 mvwaddch(win
, win
->cury
, win
->curx
- 1, ' ');
225 wmove(win
, win
->cury
, win
->curx
- 1);
227 /* getch() displays the kill character */
228 mvwaddch(win
, win
->cury
, oldx
, ' ');
229 wmove(win
, win
->cury
, oldx
);
230 } else if (c
>= KEY_MIN
&& c
<= KEY_MAX
) {
231 /* getch() displays these characters */
232 mvwaddch(win
, win
->cury
, win
->curx
- 1, ' ');
233 wmove(win
, win
->cury
, win
->curx
- 1);
239 mvwaddch(win
, win
->cury
, win
->curx
- 1, ' ');
240 wmove(win
, win
->cury
, win
->curx
- 1);