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