]> ncurses.scripts.mit.edu Git - ncurses.git/blob - man/curs_getstr.3x
33563ab68436573807ccd5af8d8a8be74c9cd762
[ncurses.git] / man / curs_getstr.3x
1 .\"***************************************************************************
2 .\" Copyright 2018-2023,2024 Thomas E. Dickey                                *
3 .\" Copyright 1998-2010,2017 Free Software Foundation, Inc.                  *
4 .\"                                                                          *
5 .\" Permission is hereby granted, free of charge, to any person obtaining a  *
6 .\" copy of this software and associated documentation files (the            *
7 .\" "Software"), to deal in the Software without restriction, including      *
8 .\" without limitation the rights to use, copy, modify, merge, publish,      *
9 .\" distribute, distribute with modifications, sublicense, and/or sell       *
10 .\" copies of the Software, and to permit persons to whom the Software is    *
11 .\" furnished to do so, subject to the following conditions:                 *
12 .\"                                                                          *
13 .\" The above copyright notice and this permission notice shall be included  *
14 .\" in all copies or substantial portions of the Software.                   *
15 .\"                                                                          *
16 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17 .\" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19 .\" IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20 .\" DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21 .\" OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22 .\" THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23 .\"                                                                          *
24 .\" Except as contained in this notice, the name(s) of the above copyright   *
25 .\" holders shall not be used in advertising or otherwise to promote the     *
26 .\" sale, use or other dealings in this Software without prior written       *
27 .\" authorization.                                                           *
28 .\"***************************************************************************
29 .\"
30 .\" $Id: curs_getstr.3x,v 1.54 2024/03/16 15:35:01 tom Exp $
31 .TH curs_getstr 3X 2024-03-16 "ncurses @NCURSES_MAJOR@.@NCURSES_MINOR@" "Library calls"
32 .ie \n(.g \{\
33 .ds `` \(lq
34 .ds '' \(rq
35 .\}
36 .el \{\
37 .ie t .ds `` ``
38 .el   .ds `` ""
39 .ie t .ds '' ''
40 .el   .ds '' ""
41 .\}
42 .
43 .de bP
44 .ie n  .IP \(bu 4
45 .el    .IP \(bu 2
46 ..
47 .SH NAME
48 \fB\%getstr\fP,
49 \fB\%getnstr\fP,
50 \fB\%wgetstr\fP,
51 \fB\%wgetnstr\fP,
52 \fB\%mvgetstr\fP,
53 \fB\%mvgetnstr\fP,
54 \fB\%mvwgetstr\fP,
55 \fB\%mvwgetnstr\fP \-
56 accept character strings from \fIcurses\fR terminal keyboard
57 .SH SYNOPSIS
58 .nf
59 \fB#include <curses.h>
60 .PP
61 \fBint getstr(char *\fIstr\fP);
62 \fBint getnstr(char *\fIstr\fP, int \fIn\fP);
63 \fBint wgetstr(WINDOW *\fIwin\fP, char *\fIstr\fP);
64 \fBint wgetnstr(WINDOW *\fIwin\fP, char *\fIstr\fP, int \fIn\fP);
65 .PP
66 \fBint mvgetstr(int \fIy\fP, int \fIx\fP, char *\fIstr\fP);
67 \fBint mvwgetstr(WINDOW *\fIwin\fP, int \fIy\fP, int \fIx\fP, char *\fIstr\fP);
68 \fBint mvgetnstr(int \fIy\fP, int \fIx\fP, char *\fIstr\fP, int \fIn\fP);
69 \fBint mvwgetnstr(WINDOW *\fIwin\fP, int \fIy\fP, int \fIx\fP, char *\fIstr\fP, int \fIn\fP);
70 .fi
71 .SH DESCRIPTION
72 The function
73 \fBwgetnstr\fP
74 is equivalent to a series of calls to
75 \fBwgetch\fP(3X),
76 until a newline or carriage return terminates the series:
77 .bP
78 The terminating character is not included in the returned string.
79 .bP
80 In all instances, the end of the string is terminated
81 by a NUL.
82 .bP
83 The function stores the result in the area pointed to
84 by the \fIstr\fP parameter.
85 .bP
86 The function reads at most \fIn\fP characters,
87 thus preventing a possible overflow of the input buffer.
88 .IP
89 Any attempt to enter more characters
90 (other than the terminating newline or carriage return)
91 causes a beep.
92 .IP
93 Function keys also cause a beep and are ignored.
94 .PP
95 The user's \fIerase\fP and \fIkill\fP characters are interpreted:
96 .bP
97 The \fIerase\fP character (e.g., \fB^H\fP) erases the character
98 at the end of the buffer, moving the cursor to the left.
99 .IP
100 If \fIkeypad\fP mode is on for the window,
101 \fBKEY_LEFT\fP and \fBKEY_BACKSPACE\fP
102 are both considered equivalent to the user's \fIerase\fP character.
103 .bP
104 The \fIkill\fP character (e.g., \fB^U\fP) erases the entire buffer,
105 leaving the cursor at the beginning of the buffer.
106 .PP
107 Characters input are echoed only if \fBecho\fP is currently on.
108 In that case,
109 backspace is echoed as deletion of the previous character
110 (typically a left motion).
111 .PP
112 The
113 \fBgetnstr\fP,
114 \fBmvgetnstr\fP,
115 \fBmvwgetnstr\fP, and
116 \fBwgetnstr\fP
117 functions are identical
118 to the
119 \fBgetstr\fP,
120 \fBmvgetstr\fP,
121 \fBmvwgetstr\fP, and
122 \fBwgetstr\fP
123 functions, respectively,
124 except that the
125 \fB*n*\fP
126 versions read at most
127 \fIn\fP
128 characters, letting the application prevent overflow of the
129 input buffer.
130 .SH RETURN VALUE
131 All of these functions return the integer \fBOK\fP upon successful completion.
132 (SVr4 specifies only \*(``an integer value other than \fBERR\fP\*('')
133 If unsuccessful, they return \fBERR\fP.
134 .PP
135 X/Open defines no error conditions.
136 .PP
137 In this implementation,
138 these functions return an error
139 .bP
140 if the window pointer is null,
141 .bP
142 if its timeout expires without having any data, or
143 .bP
144 if the associated call to
145 \fBwgetch\fP
146 failed.
147 .PP
148 This implementation provides an extension as well.
149 If a \fBSIGWINCH\fP interrupts the function, it will return \fBKEY_RESIZE\fP
150 rather than \fBOK\fP or \fBERR\fP.
151 .PP
152 Functions with a \*(``mv\*('' prefix first perform a cursor movement using
153 \fBwmove\fP, and return an error if the position is outside the window,
154 or if the window pointer is null.
155 .SH NOTES
156 Any of these functions other than
157 \fBwgetnstr\fP
158 may be macros.
159 .PP
160 Using
161 \fBgetstr\fP,
162 \fBmvgetstr\fP,
163 \fBmvwgetstr\fP, or
164 \fBwgetstr\fP
165 to read a line that
166 overflows the array pointed to by
167 \fBstr\fP
168 causes undefined
169 results.
170 The use of
171 \fBgetnstr\fP,
172 \fBmvgetnstr\fP,
173 \fBmvwgetnstr\fP, or
174 \fBwgetnstr\fP,
175 respectively, is recommended.
176 .SH PORTABILITY
177 These functions are described in The Single Unix Specification, Version 2.
178 No error conditions are defined.
179 .PP
180 This implementation returns \fBERR\fP if the window pointer is null,
181 or if the lower-level \fBwgetch\fP(3X) call returns an \fBERR\fP.
182 .PP
183 SVr3 and early SVr4 curses implementations did not reject function keys;
184 the SVr4.0 documentation claimed that \*(``special keys\*(''
185 (such as function keys,
186 \*(``home\*('' key,
187 \*(``clear\*('' key,
188 \fIetc\fP.) are \*(``interpreted\*('',
189 without giving details.
190 It lied.
191 In fact, the \*(``character\*('' value appended to the
192 string by those implementations was predictable but not useful
193 (being, in fact, the low-order eight bits of the key's KEY_ value).
194 .PP
195 The functions \fBgetnstr\fP, \fBmvgetnstr\fP, and \fBmvwgetnstr\fP were
196 present but not documented in SVr4.
197 .PP
198 X/Open Curses, Issue 5 (2007) stated that these functions
199 \*(``read at most \fIn\fP bytes\*(''
200 but did not state whether the terminating NUL is counted in that limit.
201 X/Open Curses, Issue 7 (2009) changed that to say they
202 \*(``read at most \fIn\fP\-1 bytes\*(''
203 to allow for the terminating NUL.
204 As of 2018, some implementations count it, some do not:
205 .bP
206 \fI\%ncurses\fP 6.1 and PDCurses do not count the NUL in the given limit, while
207 .bP
208 Solaris SVr4 and NetBSD curses count the NUL as part of the limit.
209 .bP
210 Solaris xcurses provides both:
211 its wide-character \fBwget_nstr\fP reserves a NUL,
212 but its \fBwgetnstr\fP does not count the NUL consistently.
213 .PP
214 In SVr4 curses,
215 a negative value of \fIn\fP tells \fBwgetnstr\fP to assume that the
216 caller's buffer is large enough to hold the result,
217 i.e., to act like \fBwgetstr\fP.
218 X/Open Curses does not mention this
219 (or anything related to negative or zero values of \fIn\fP),
220 however most implementations
221 use the feature, with different limits:
222 .bP
223 Solaris SVr4 curses and PDCurses limit the result to 255 bytes.
224 Other Unix systems than Solaris are likely to use the same limit.
225 .bP
226 Solaris xcurses limits the result to \fBLINE_MAX\fP bytes.
227 .bP
228 NetBSD 7 assumes no particular limit for the result from \fBwgetstr\fP.
229 However, it limits the \fBwgetnstr\fP parameter \fIn\fP to ensure
230 that it is greater than zero.
231 .IP
232 A comment in NetBSD's source code states that this is specified in SUSv2.
233 .bP
234 \fI\%ncurses\fP (before 6.2) assumes no particular limit for the result
235 from \fBwgetstr\fP, and treats the \fIn\fP parameter of \fBwgetnstr\fP
236 like SVr4 curses.
237 .bP
238 \fI\%ncurses\fP 6.2 uses \fBLINE_MAX\fP,
239 or a larger (system-dependent) value
240 which the \fBsysconf\fP function may provide.
241 If neither \fBLINE_MAX\fP or \fBsysconf\fP is available,
242 \fI\%ncurses\fP uses the POSIX value for \fBLINE_MAX\fP (a 2048 byte limit).
243 In either case, it reserves a byte for the terminating NUL.
244 .PP
245 Although \fBgetnstr\fP is equivalent to a series of calls to \fBgetch\fP,
246 it also makes changes to the curses modes to allow simple editing of
247 the input buffer:
248 .bP
249 \fBgetnstr\fP saves the current value of the \fBnl\fP, \fBecho\fP,
250 \fBraw\fP and \fBcbreak\fP modes, and sets
251 \fBnl\fP,
252 \fBnoecho\fP,
253 \fBnoraw\fP, and
254 \fBcbreak\fP.
255 .IP
256 \fBgetnstr\fP handles the echoing of characters,
257 rather than relying on the caller to set an appropriate mode.
258 .bP
259 It also obtains the \fIerase\fP and \fIkill\fP characters
260 from \fBerasechar\fP and \fBkillchar\fP, respectively.
261 .bP
262 On return, \fBgetnstr\fP restores the modes to their previous values.
263 .PP
264 Other implementations differ in their treatment of special characters:
265 .bP
266 While they may set the \fIecho\fP mode,
267 other implementations do not modify the \fIraw\fP mode,
268 They may take the \fIcbreak\fP
269 mode set by the caller into account when deciding whether to handle
270 echoing within \fBgetnstr\fP or as a side-effect of the \fBgetch\fP calls.
271 .bP
272 The original \fI\%ncurses\fP
273 (as \fIpcurses\fP in 1986)
274 set \fBnoraw\fP and \fBcbreak\fP when accepting input for \fBgetnstr\fP.
275 That may have been done to make function- and cursor-keys work;
276 it is not necessary with \fI\%ncurses\fP.
277 .IP
278 Since 1995,
279 \fI\%ncurses\fP has provided signal handlers for INTR and QUIT
280 (e.g., \fB^C\fP or \fB^\e\fP).
281 With the \fBnoraw\fP and \fBcbreak\fP settings,
282 those may catch a signal and stop the program,
283 where other implementations allow one to enter those characters in the buffer.
284 .bP
285 Starting in 2021
286 (\fI\%ncurses\fP 6.3),
287 \fBgetnstr\fP sets \fBraw\fP,
288 rather than \fBnoraw\fP and \fBcbreak\fP for better compatibility with
289 SVr4-curses, e.g., allowing one to enter a \fB^C\fP into the buffer.
290 .SH SEE ALSO
291 \fB\%curses\fP(3X),
292 \fB\%curs_getch\fP(3X),
293 \fB\%curs_termattrs\fP(3X),
294 \fB\%curs_variables\fP(3X)