]> ncurses.scripts.mit.edu Git - ncurses.git/blob - man/curs_getstr.3x
ncurses 6.5 - patch 20240601
[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.63 2024/06/01 22:29:08 tom Exp $
31 .TH curs_getstr 3X 2024-06-01 "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
139 .B ERR
140 .bP
141 if the window pointer is null,
142 .bP
143 if its timeout expires without having any data, or
144 .bP
145 if the associated call to
146 \fBwgetch\fP
147 failed.
148 .PP
149 This implementation provides an extension as well.
150 If a \fBSIGWINCH\fP interrupts the function, it will return \fBKEY_RESIZE\fP
151 rather than \fBOK\fP or \fBERR\fP.
152 .PP
153 Functions prefixed with \*(``mv\*('' first perform cursor movement and
154 fail if the position
155 .RI ( y ,
156 .IR x )
157 is outside the window boundaries.
158 .SH NOTES
159 Any of these functions other than
160 \fBwgetnstr\fP
161 may be macros.
162 .PP
163 Using
164 \fBgetstr\fP,
165 \fBmvgetstr\fP,
166 \fBmvwgetstr\fP, or
167 \fBwgetstr\fP
168 to read a line that
169 overflows the array pointed to by
170 \fBstr\fP
171 causes undefined
172 results.
173 The use of
174 \fBgetnstr\fP,
175 \fBmvgetnstr\fP,
176 \fBmvwgetnstr\fP, or
177 \fBwgetnstr\fP,
178 respectively, is recommended.
179 .SH PORTABILITY
180 These functions are described in The Single Unix Specification, Version 2.
181 No error conditions are defined.
182 .PP
183 This implementation returns \fBERR\fP if the window pointer is null,
184 or if the lower-level \fBwgetch\fP(3X) call returns an \fBERR\fP.
185 .PP
186 SVr3 and early SVr4 curses implementations did not reject function keys;
187 the SVr4.0 documentation claimed that \*(``special keys\*(''
188 (such as function keys,
189 \*(``home\*('' key,
190 \*(``clear\*('' key,
191 \fIetc\fP.) are \*(``interpreted\*('',
192 without giving details.
193 It lied.
194 In fact, the \*(``character\*('' value appended to the
195 string by those implementations was predictable but not useful
196 (being, in fact, the low-order eight bits of the key's KEY_ value).
197 .PP
198 The functions \fBgetnstr\fP, \fBmvgetnstr\fP, and \fBmvwgetnstr\fP were
199 present but not documented in SVr4.
200 .PP
201 X/Open Curses, Issue 5 (2007) stated that these functions
202 \*(``read at most \fIn\fP bytes\*(''
203 but did not state whether the terminating NUL counted toward that limit.
204 X/Open Curses, Issue 7 (2009) changed that to say they
205 \*(``read at most \fIn\fP\-1 bytes\*(''
206 to allow for the terminating NUL.
207 As of 2018, some implementations count it, some do not:
208 .bP
209 \fI\%ncurses\fP 6.1 and PDCurses do not count the NUL in the given limit, while
210 .bP
211 Solaris SVr4 and NetBSD curses count the NUL as part of the limit.
212 .bP
213 Solaris xcurses provides both:
214 its wide-character \fBwget_nstr\fP reserves a NUL,
215 but its \fBwgetnstr\fP does not count the NUL consistently.
216 .PP
217 In SVr4 curses,
218 a negative value of \fIn\fP tells \fBwgetnstr\fP to assume that the
219 caller's buffer is large enough to hold the result,
220 i.e., to act like \fBwgetstr\fP.
221 X/Open Curses does not mention this
222 (or anything related to negative or zero values of \fIn\fP),
223 however most implementations
224 use the feature, with different limits:
225 .bP
226 Solaris SVr4 curses and PDCurses limit the result to 255 bytes.
227 Other Unix systems than Solaris are likely to use the same limit.
228 .bP
229 Solaris xcurses limits the result to \fBLINE_MAX\fP bytes.
230 .bP
231 NetBSD 7 assumes no particular limit for the result from \fBwgetstr\fP.
232 However, it limits the \fBwgetnstr\fP parameter \fIn\fP to ensure
233 that it is greater than zero.
234 .IP
235 A comment in NetBSD's source code states that this is specified in SUSv2.
236 .bP
237 \fI\%ncurses\fP (before 6.2) assumes no particular limit for the result
238 from \fBwgetstr\fP, and treats the \fIn\fP parameter of \fBwgetnstr\fP
239 like SVr4 curses.
240 .bP
241 \fI\%ncurses\fP 6.2 uses \fBLINE_MAX\fP,
242 or a larger (system-dependent) value
243 which the \fBsysconf\fP function may provide.
244 If neither \fBLINE_MAX\fP or \fBsysconf\fP is available,
245 \fI\%ncurses\fP uses the POSIX value for \fBLINE_MAX\fP (a 2048 byte limit).
246 In either case, it reserves a byte for the terminating NUL.
247 .PP
248 Although \fBgetnstr\fP is equivalent to a series of calls to \fBgetch\fP,
249 it also makes changes to the curses modes to allow simple editing of
250 the input buffer:
251 .bP
252 \fBgetnstr\fP saves the current value of the \fBnl\fP, \fBecho\fP,
253 \fBraw\fP and \fBcbreak\fP modes, and sets
254 \fBnl\fP,
255 \fBnoecho\fP,
256 \fBnoraw\fP, and
257 \fBcbreak\fP.
258 .IP
259 \fBgetnstr\fP handles the echoing of characters,
260 rather than relying on the caller to set an appropriate mode.
261 .bP
262 It also obtains the \fIerase\fP and \fIkill\fP characters
263 from \fBerasechar\fP and \fBkillchar\fP, respectively.
264 .bP
265 On return, \fBgetnstr\fP restores the modes to their previous values.
266 .PP
267 Other implementations differ in their treatment of special characters:
268 .bP
269 While they may set the \fIecho\fP mode,
270 other implementations do not modify the \fIraw\fP mode,
271 They may take the \fIcbreak\fP
272 mode set by the caller into account when deciding whether to handle
273 echoing within \fBgetnstr\fP or as a side-effect of the \fBgetch\fP calls.
274 .bP
275 The original \fI\%ncurses\fP
276 (as \fIpcurses\fP in 1986)
277 set \fBnoraw\fP and \fBcbreak\fP when accepting input for \fBgetnstr\fP.
278 That may have been done to make function- and cursor-keys work;
279 it is not necessary with \fI\%ncurses\fP.
280 .IP
281 Since 1995,
282 \fI\%ncurses\fP has provided signal handlers for INTR and QUIT
283 (e.g., \fB^C\fP or \fB^\e\fP).
284 With the \fBnoraw\fP and \fBcbreak\fP settings,
285 those may catch a signal and stop the program,
286 where other implementations allow one to enter those characters in the buffer.
287 .bP
288 Starting in 2021
289 (\fI\%ncurses\fP 6.3),
290 \fBgetnstr\fP sets \fBraw\fP,
291 rather than \fBnoraw\fP and \fBcbreak\fP for better compatibility with
292 SVr4-curses, e.g., allowing one to enter a \fB^C\fP into the buffer.
293 .SH SEE ALSO
294 \fB\%curs_get_wstr\fP(3X) describes comparable functions of the
295 .I \%ncurses
296 library in its wide-character configuration
297 .RI \%( ncursesw ).
298 .PP
299 \fB\%curses\fP(3X),
300 \fB\%curs_getch\fP(3X),
301 \fB\%curs_termattrs\fP(3X),
302 \fB\%curs_variables\fP(3X)