2 ****************************************************************************
3 * Copyright 2018-2023,2024 Thomas E. Dickey *
4 * Copyright 1998-2010,2017 Free Software Foundation, Inc. *
6 * Permission is hereby granted, free of charge, to any person obtaining a *
7 * copy of this software and associated documentation files (the *
8 * "Software"), to deal in the Software without restriction, including *
9 * without limitation the rights to use, copy, modify, merge, publish, *
10 * distribute, distribute with modifications, sublicense, and/or sell *
11 * copies of the Software, and to permit persons to whom the Software is *
12 * furnished to do so, subject to the following conditions: *
14 * The above copyright notice and this permission notice shall be included *
15 * in all copies or substantial portions of the Software. *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
25 * Except as contained in this notice, the name(s) of the above copyright *
26 * holders shall not be used in advertising or otherwise to promote the *
27 * sale, use or other dealings in this Software without prior written *
29 ****************************************************************************
30 * @Id: curs_getstr.3x,v 1.54 2024/03/16 15:35:01 tom Exp @
32 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
35 <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
36 <meta name="generator" content="Manpage converted by man2html - see https://invisible-island.net/scripts/readme.html#others_scripts">
37 <TITLE>curs_getstr 3x 2024-03-16 ncurses 6.4 Library calls</TITLE>
38 <link rel="author" href="mailto:bug-ncurses@gnu.org">
42 <H1 class="no-header">curs_getstr 3x 2024-03-16 ncurses 6.4 Library calls</H1>
44 <STRONG><A HREF="curs_getstr.3x.html">curs_getstr(3x)</A></STRONG> Library calls <STRONG><A HREF="curs_getstr.3x.html">curs_getstr(3x)</A></STRONG>
49 </PRE><H2><a name="h2-NAME">NAME</a></H2><PRE>
50 <STRONG>getstr</STRONG>, <STRONG>getnstr</STRONG>, <STRONG>wgetstr</STRONG>, <STRONG>wgetnstr</STRONG>, <STRONG>mvgetstr</STRONG>, <STRONG>mvgetnstr</STRONG>, <STRONG>mvwgetstr</STRONG>,
51 <STRONG>mvwgetnstr</STRONG> - accept character strings from <EM>curses</EM> terminal keyboard
54 </PRE><H2><a name="h2-SYNOPSIS">SYNOPSIS</a></H2><PRE>
55 <STRONG>#include</STRONG> <STRONG><curses.h></STRONG>
57 <STRONG>int</STRONG> <STRONG>getstr(char</STRONG> <STRONG>*</STRONG><EM>str</EM><STRONG>);</STRONG>
58 <STRONG>int</STRONG> <STRONG>getnstr(char</STRONG> <STRONG>*</STRONG><EM>str</EM><STRONG>,</STRONG> <STRONG>int</STRONG> <EM>n</EM><STRONG>);</STRONG>
59 <STRONG>int</STRONG> <STRONG>wgetstr(WINDOW</STRONG> <STRONG>*</STRONG><EM>win</EM><STRONG>,</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>str</EM><STRONG>);</STRONG>
60 <STRONG>int</STRONG> <STRONG>wgetnstr(WINDOW</STRONG> <STRONG>*</STRONG><EM>win</EM><STRONG>,</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>str</EM><STRONG>,</STRONG> <STRONG>int</STRONG> <EM>n</EM><STRONG>);</STRONG>
62 <STRONG>int</STRONG> <STRONG>mvgetstr(int</STRONG> <EM>y</EM><STRONG>,</STRONG> <STRONG>int</STRONG> <EM>x</EM><STRONG>,</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>str</EM><STRONG>);</STRONG>
63 <STRONG>int</STRONG> <STRONG>mvwgetstr(WINDOW</STRONG> <STRONG>*</STRONG><EM>win</EM><STRONG>,</STRONG> <STRONG>int</STRONG> <EM>y</EM><STRONG>,</STRONG> <STRONG>int</STRONG> <EM>x</EM><STRONG>,</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>str</EM><STRONG>);</STRONG>
64 <STRONG>int</STRONG> <STRONG>mvgetnstr(int</STRONG> <EM>y</EM><STRONG>,</STRONG> <STRONG>int</STRONG> <EM>x</EM><STRONG>,</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>str</EM><STRONG>,</STRONG> <STRONG>int</STRONG> <EM>n</EM><STRONG>);</STRONG>
65 <STRONG>int</STRONG> <STRONG>mvwgetnstr(WINDOW</STRONG> <STRONG>*</STRONG><EM>win</EM><STRONG>,</STRONG> <STRONG>int</STRONG> <EM>y</EM><STRONG>,</STRONG> <STRONG>int</STRONG> <EM>x</EM><STRONG>,</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>str</EM><STRONG>,</STRONG> <STRONG>int</STRONG> <EM>n</EM><STRONG>);</STRONG>
68 </PRE><H2><a name="h2-DESCRIPTION">DESCRIPTION</a></H2><PRE>
69 The function <STRONG>wgetnstr</STRONG> is equivalent to a series of calls to <STRONG><A HREF="curs_getch.3x.html">wgetch(3x)</A></STRONG>,
70 until a newline or carriage return terminates the series:
72 <STRONG>o</STRONG> The terminating character is not included in the returned string.
74 <STRONG>o</STRONG> In all instances, the end of the string is terminated by a NUL.
76 <STRONG>o</STRONG> The function stores the result in the area pointed to by the <EM>str</EM>
79 <STRONG>o</STRONG> The function reads at most <EM>n</EM> characters, thus preventing a possible
80 overflow of the input buffer.
82 Any attempt to enter more characters (other than the terminating
83 newline or carriage return) causes a beep.
85 Function keys also cause a beep and are ignored.
87 The user's <EM>erase</EM> and <EM>kill</EM> characters are interpreted:
89 <STRONG>o</STRONG> The <EM>erase</EM> character (e.g., <STRONG>^H</STRONG>) erases the character at the end of
90 the buffer, moving the cursor to the left.
92 If <EM>keypad</EM> mode is on for the window, <STRONG>KEY_LEFT</STRONG> and <STRONG>KEY_BACKSPACE</STRONG> are
93 both considered equivalent to the user's <EM>erase</EM> character.
95 <STRONG>o</STRONG> The <EM>kill</EM> character (e.g., <STRONG>^U</STRONG>) erases the entire buffer, leaving the
96 cursor at the beginning of the buffer.
98 Characters input are echoed only if <STRONG>echo</STRONG> is currently on. In that
99 case, backspace is echoed as deletion of the previous character
100 (typically a left motion).
102 The <STRONG>getnstr</STRONG>, <STRONG>mvgetnstr</STRONG>, <STRONG>mvwgetnstr</STRONG>, and <STRONG>wgetnstr</STRONG> functions are
103 identical to the <STRONG>getstr</STRONG>, <STRONG>mvgetstr</STRONG>, <STRONG>mvwgetstr</STRONG>, and <STRONG>wgetstr</STRONG> functions,
104 respectively, except that the <STRONG>*n*</STRONG> versions read at most <EM>n</EM> characters,
105 letting the application prevent overflow of the input buffer.
108 </PRE><H2><a name="h2-RETURN-VALUE">RETURN VALUE</a></H2><PRE>
109 All of these functions return the integer <STRONG>OK</STRONG> upon successful
110 completion. (SVr4 specifies only "an integer value other than <STRONG>ERR</STRONG>") If
111 unsuccessful, they return <STRONG>ERR</STRONG>.
113 X/Open defines no error conditions.
115 In this implementation, these functions return an error
117 <STRONG>o</STRONG> if the window pointer is null,
119 <STRONG>o</STRONG> if its timeout expires without having any data, or
121 <STRONG>o</STRONG> if the associated call to <STRONG>wgetch</STRONG> failed.
123 This implementation provides an extension as well. If a <STRONG>SIGWINCH</STRONG>
124 interrupts the function, it will return <STRONG>KEY_RESIZE</STRONG> rather than <STRONG>OK</STRONG> or
125 <STRONG>ERR</STRONG>.
127 Functions with a "mv" prefix first perform a cursor movement using
128 <STRONG>wmove</STRONG>, and return an error if the position is outside the window, or if
129 the window pointer is null.
132 </PRE><H2><a name="h2-NOTES">NOTES</a></H2><PRE>
133 Any of these functions other than <STRONG>wgetnstr</STRONG> may be macros.
135 Using <STRONG>getstr</STRONG>, <STRONG>mvgetstr</STRONG>, <STRONG>mvwgetstr</STRONG>, or <STRONG>wgetstr</STRONG> to read a line that
136 overflows the array pointed to by <STRONG>str</STRONG> causes undefined results. The
137 use of <STRONG>getnstr</STRONG>, <STRONG>mvgetnstr</STRONG>, <STRONG>mvwgetnstr</STRONG>, or <STRONG>wgetnstr</STRONG>, respectively, is
141 </PRE><H2><a name="h2-PORTABILITY">PORTABILITY</a></H2><PRE>
142 These functions are described in The Single Unix Specification, Version
143 2. No error conditions are defined.
145 This implementation returns <STRONG>ERR</STRONG> if the window pointer is null, or if
146 the lower-level <STRONG><A HREF="curs_getch.3x.html">wgetch(3x)</A></STRONG> call returns an <STRONG>ERR</STRONG>.
148 SVr3 and early SVr4 curses implementations did not reject function
149 keys; the SVr4.0 documentation claimed that "special keys" (such as
150 function keys, "home" key, "clear" key, <EM>etc</EM>.) are "interpreted",
151 without giving details. It lied. In fact, the "character" value
152 appended to the string by those implementations was predictable but not
153 useful (being, in fact, the low-order eight bits of the key's KEY_
156 The functions <STRONG>getnstr</STRONG>, <STRONG>mvgetnstr</STRONG>, and <STRONG>mvwgetnstr</STRONG> were present but not
159 X/Open Curses, Issue 5 (2007) stated that these functions "read at most
160 <EM>n</EM> bytes" but did not state whether the terminating NUL is counted in
161 that limit. X/Open Curses, Issue 7 (2009) changed that to say they
162 "read at most <EM>n</EM>-1 bytes" to allow for the terminating NUL. As of 2018,
163 some implementations count it, some do not:
165 <STRONG>o</STRONG> <EM>ncurses</EM> 6.1 and PDCurses do not count the NUL in the given limit,
168 <STRONG>o</STRONG> Solaris SVr4 and NetBSD curses count the NUL as part of the limit.
170 <STRONG>o</STRONG> Solaris xcurses provides both: its wide-character <STRONG>wget_nstr</STRONG>
171 reserves a NUL, but its <STRONG>wgetnstr</STRONG> does not count the NUL
174 In SVr4 curses, a negative value of <EM>n</EM> tells <STRONG>wgetnstr</STRONG> to assume that the
175 caller's buffer is large enough to hold the result, i.e., to act like
176 <STRONG>wgetstr</STRONG>. X/Open Curses does not mention this (or anything related to
177 negative or zero values of <EM>n</EM>), however most implementations use the
178 feature, with different limits:
180 <STRONG>o</STRONG> Solaris SVr4 curses and PDCurses limit the result to 255 bytes.
181 Other Unix systems than Solaris are likely to use the same limit.
183 <STRONG>o</STRONG> Solaris xcurses limits the result to <STRONG>LINE_MAX</STRONG> bytes.
185 <STRONG>o</STRONG> NetBSD 7 assumes no particular limit for the result from <STRONG>wgetstr</STRONG>.
186 However, it limits the <STRONG>wgetnstr</STRONG> parameter <EM>n</EM> to ensure that it is
189 A comment in NetBSD's source code states that this is specified in
192 <STRONG>o</STRONG> <EM>ncurses</EM> (before 6.2) assumes no particular limit for the result
193 from <STRONG>wgetstr</STRONG>, and treats the <EM>n</EM> parameter of <STRONG>wgetnstr</STRONG> like SVr4
196 <STRONG>o</STRONG> <EM>ncurses</EM> 6.2 uses <STRONG>LINE_MAX</STRONG>, or a larger (system-dependent) value
197 which the <STRONG>sysconf</STRONG> function may provide. If neither <STRONG>LINE_MAX</STRONG> or
198 <STRONG>sysconf</STRONG> is available, <EM>ncurses</EM> uses the POSIX value for <STRONG>LINE_MAX</STRONG> (a
199 2048 byte limit). In either case, it reserves a byte for the
202 Although <STRONG>getnstr</STRONG> is equivalent to a series of calls to <STRONG>getch</STRONG>, it also
203 makes changes to the curses modes to allow simple editing of the input
206 <STRONG>o</STRONG> <STRONG>getnstr</STRONG> saves the current value of the <STRONG>nl</STRONG>, <STRONG>echo</STRONG>, <STRONG>raw</STRONG> and <STRONG>cbreak</STRONG>
207 modes, and sets <STRONG>nl</STRONG>, <STRONG>noecho</STRONG>, <STRONG>noraw</STRONG>, and <STRONG>cbreak</STRONG>.
209 <STRONG>getnstr</STRONG> handles the echoing of characters, rather than relying on
210 the caller to set an appropriate mode.
212 <STRONG>o</STRONG> It also obtains the <EM>erase</EM> and <EM>kill</EM> characters from <STRONG>erasechar</STRONG> and
213 <STRONG>killchar</STRONG>, respectively.
215 <STRONG>o</STRONG> On return, <STRONG>getnstr</STRONG> restores the modes to their previous values.
217 Other implementations differ in their treatment of special characters:
219 <STRONG>o</STRONG> While they may set the <EM>echo</EM> mode, other implementations do not
220 modify the <EM>raw</EM> mode, They may take the <EM>cbreak</EM> mode set by the
221 caller into account when deciding whether to handle echoing within
222 <STRONG>getnstr</STRONG> or as a side-effect of the <STRONG>getch</STRONG> calls.
224 <STRONG>o</STRONG> The original <EM>ncurses</EM> (as <EM>pcurses</EM> in 1986) set <STRONG>noraw</STRONG> and <STRONG>cbreak</STRONG> when
225 accepting input for <STRONG>getnstr</STRONG>. That may have been done to make
226 function- and cursor-keys work; it is not necessary with <EM>ncurses</EM>.
228 Since 1995, <EM>ncurses</EM> has provided signal handlers for INTR and QUIT
229 (e.g., <STRONG>^C</STRONG> or <STRONG>^\</STRONG>). With the <STRONG>noraw</STRONG> and <STRONG>cbreak</STRONG> settings, those may
230 catch a signal and stop the program, where other implementations
231 allow one to enter those characters in the buffer.
233 <STRONG>o</STRONG> Starting in 2021 (<EM>ncurses</EM> 6.3), <STRONG>getnstr</STRONG> sets <STRONG>raw</STRONG>, rather than <STRONG>noraw</STRONG>
234 and <STRONG>cbreak</STRONG> for better compatibility with SVr4-curses, e.g.,
235 allowing one to enter a <STRONG>^C</STRONG> into the buffer.
238 </PRE><H2><a name="h2-SEE-ALSO">SEE ALSO</a></H2><PRE>
239 <STRONG><A HREF="ncurses.3x.html">curses(3x)</A></STRONG>, <STRONG><A HREF="curs_getch.3x.html">curs_getch(3x)</A></STRONG>, <STRONG><A HREF="curs_termattrs.3x.html">curs_termattrs(3x)</A></STRONG>, <STRONG><A HREF="curs_variables.3x.html">curs_variables(3x)</A></STRONG>
243 ncurses 6.4 2024-03-16 <STRONG><A HREF="curs_getstr.3x.html">curs_getstr(3x)</A></STRONG>
247 <li><a href="#h2-NAME">NAME</a></li>
248 <li><a href="#h2-SYNOPSIS">SYNOPSIS</a></li>
249 <li><a href="#h2-DESCRIPTION">DESCRIPTION</a></li>
250 <li><a href="#h2-RETURN-VALUE">RETURN VALUE</a></li>
251 <li><a href="#h2-NOTES">NOTES</a></li>
252 <li><a href="#h2-PORTABILITY">PORTABILITY</a></li>
253 <li><a href="#h2-SEE-ALSO">SEE ALSO</a></li>