blob: 4a49352a146cdf6de9b3b1ca4ce1bee33f1def33 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301.\"***************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002.\" Copyright 2018-2023,2024 Thomas E. Dickey *
3.\" Copyright 1998-2010,2017 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304.\" *
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.\"
micky3879b9f5e72025-07-08 18:04:53 -040030.\" $Id: curs_getstr.3x,v 1.58 2024/04/20 19:18:18 tom Exp $
31.TH curs_getstr 3X 2024-04-20 "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..
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053047.SH NAME
micky3879b9f5e72025-07-08 18:04:53 -040048\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 \-
56accept character strings from \fIcurses\fR terminal keyboard
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053057.SH SYNOPSIS
micky3879b9f5e72025-07-08 18:04:53 -040058.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
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053071.SH DESCRIPTION
micky3879b9f5e72025-07-08 18:04:53 -040072The function
73\fBwgetnstr\fP
74is equivalent to a series of calls to
75\fBwgetch\fP(3X),
76until a newline or carriage return terminates the series:
77.bP
78The terminating character is not included in the returned string.
79.bP
80In all instances, the end of the string is terminated
81by a NUL.
82.bP
83The function stores the result in the area pointed to
84by the \fIstr\fP parameter.
85.bP
86The function reads at most \fIn\fP characters,
87thus preventing a possible overflow of the input buffer.
88.IP
89Any attempt to enter more characters
90(other than the terminating newline or carriage return)
91causes a beep.
92.IP
93Function keys also cause a beep and are ignored.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053094.PP
micky3879b9f5e72025-07-08 18:04:53 -040095The user's \fIerase\fP and \fIkill\fP characters are interpreted:
96.bP
97The \fIerase\fP character (e.g., \fB^H\fP) erases the character
98at the end of the buffer, moving the cursor to the left.
99.IP
100If \fIkeypad\fP mode is on for the window,
101\fBKEY_LEFT\fP and \fBKEY_BACKSPACE\fP
102are both considered equivalent to the user's \fIerase\fP character.
103.bP
104The \fIkill\fP character (e.g., \fB^U\fP) erases the entire buffer,
105leaving the cursor at the beginning of the buffer.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530106.PP
micky3879b9f5e72025-07-08 18:04:53 -0400107Characters input are echoed only if \fBecho\fP is currently on.
108In that case,
109backspace is echoed as deletion of the previous character
110(typically a left motion).
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530111.PP
micky3879b9f5e72025-07-08 18:04:53 -0400112The
113\fBgetnstr\fP,
114\fBmvgetnstr\fP,
115\fBmvwgetnstr\fP, and
116\fBwgetnstr\fP
117functions are identical
118to the
119\fBgetstr\fP,
120\fBmvgetstr\fP,
121\fBmvwgetstr\fP, and
122\fBwgetstr\fP
123functions, respectively,
124except that the
125\fB*n*\fP
126versions read at most
127\fIn\fP
128characters, letting the application prevent overflow of the
129input buffer.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530130.SH RETURN VALUE
micky3879b9f5e72025-07-08 18:04:53 -0400131All of these functions return the integer \fBOK\fP upon successful completion.
132(SVr4 specifies only \*(``an integer value other than \fBERR\fP\*('')
133If unsuccessful, they return \fBERR\fP.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530134.PP
135X/Open defines no error conditions.
136.PP
137In this implementation,
138these functions return an error
micky3879b9f5e72025-07-08 18:04:53 -0400139.bP
140if the window pointer is null,
141.bP
142if its timeout expires without having any data, or
143.bP
144if the associated call to
145\fBwgetch\fP
146failed.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530147.PP
148This implementation provides an extension as well.
micky3879b9f5e72025-07-08 18:04:53 -0400149If a \fBSIGWINCH\fP interrupts the function, it will return \fBKEY_RESIZE\fP
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530150rather than \fBOK\fP or \fBERR\fP.
Steve Kondikae271bc2015-11-15 02:50:53 +0100151.PP
micky3879b9f5e72025-07-08 18:04:53 -0400152Functions prefixed with \*(``mv\*('' first perform cursor movement and
153fail if the position
154.RI ( y ,
155.IR x )
156is outside the window boundaries.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530157.SH NOTES
micky3879b9f5e72025-07-08 18:04:53 -0400158Any of these functions other than
159\fBwgetnstr\fP
160may be macros.
161.PP
162Using
163\fBgetstr\fP,
164\fBmvgetstr\fP,
165\fBmvwgetstr\fP, or
166\fBwgetstr\fP
167to read a line that
168overflows the array pointed to by
169\fBstr\fP
170causes undefined
171results.
172The use of
173\fBgetnstr\fP,
174\fBmvgetnstr\fP,
175\fBmvwgetnstr\fP, or
176\fBwgetnstr\fP,
177respectively, is recommended.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530178.SH PORTABILITY
micky3879b9f5e72025-07-08 18:04:53 -0400179These functions are described in The Single Unix Specification, Version 2.
180No error conditions are defined.
181.PP
182This implementation returns \fBERR\fP if the window pointer is null,
183or if the lower-level \fBwgetch\fP(3X) call returns an \fBERR\fP.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530184.PP
185SVr3 and early SVr4 curses implementations did not reject function keys;
micky3879b9f5e72025-07-08 18:04:53 -0400186the SVr4.0 documentation claimed that \*(``special keys\*(''
187(such as function keys,
188\*(``home\*('' key,
189\*(``clear\*('' key,
190\fIetc\fP.) are \*(``interpreted\*('',
191without giving details.
192It lied.
193In fact, the \*(``character\*('' value appended to the
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530194string by those implementations was predictable but not useful
195(being, in fact, the low-order eight bits of the key's KEY_ value).
196.PP
micky3879b9f5e72025-07-08 18:04:53 -0400197The functions \fBgetnstr\fP, \fBmvgetnstr\fP, and \fBmvwgetnstr\fP were
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530198present but not documented in SVr4.
micky3879b9f5e72025-07-08 18:04:53 -0400199.PP
200X/Open Curses, Issue 5 (2007) stated that these functions
201\*(``read at most \fIn\fP bytes\*(''
202but did not state whether the terminating NUL is counted in that limit.
203X/Open Curses, Issue 7 (2009) changed that to say they
204\*(``read at most \fIn\fP\-1 bytes\*(''
205to allow for the terminating NUL.
206As of 2018, some implementations count it, some do not:
207.bP
208\fI\%ncurses\fP 6.1 and PDCurses do not count the NUL in the given limit, while
209.bP
210Solaris SVr4 and NetBSD curses count the NUL as part of the limit.
211.bP
212Solaris xcurses provides both:
213its wide-character \fBwget_nstr\fP reserves a NUL,
214but its \fBwgetnstr\fP does not count the NUL consistently.
215.PP
216In SVr4 curses,
217a negative value of \fIn\fP tells \fBwgetnstr\fP to assume that the
218caller's buffer is large enough to hold the result,
219i.e., to act like \fBwgetstr\fP.
220X/Open Curses does not mention this
221(or anything related to negative or zero values of \fIn\fP),
222however most implementations
223use the feature, with different limits:
224.bP
225Solaris SVr4 curses and PDCurses limit the result to 255 bytes.
226Other Unix systems than Solaris are likely to use the same limit.
227.bP
228Solaris xcurses limits the result to \fBLINE_MAX\fP bytes.
229.bP
230NetBSD 7 assumes no particular limit for the result from \fBwgetstr\fP.
231However, it limits the \fBwgetnstr\fP parameter \fIn\fP to ensure
232that it is greater than zero.
233.IP
234A comment in NetBSD's source code states that this is specified in SUSv2.
235.bP
236\fI\%ncurses\fP (before 6.2) assumes no particular limit for the result
237from \fBwgetstr\fP, and treats the \fIn\fP parameter of \fBwgetnstr\fP
238like SVr4 curses.
239.bP
240\fI\%ncurses\fP 6.2 uses \fBLINE_MAX\fP,
241or a larger (system-dependent) value
242which the \fBsysconf\fP function may provide.
243If neither \fBLINE_MAX\fP or \fBsysconf\fP is available,
244\fI\%ncurses\fP uses the POSIX value for \fBLINE_MAX\fP (a 2048 byte limit).
245In either case, it reserves a byte for the terminating NUL.
246.PP
247Although \fBgetnstr\fP is equivalent to a series of calls to \fBgetch\fP,
248it also makes changes to the curses modes to allow simple editing of
249the input buffer:
250.bP
251\fBgetnstr\fP saves the current value of the \fBnl\fP, \fBecho\fP,
252\fBraw\fP and \fBcbreak\fP modes, and sets
253\fBnl\fP,
254\fBnoecho\fP,
255\fBnoraw\fP, and
256\fBcbreak\fP.
257.IP
258\fBgetnstr\fP handles the echoing of characters,
259rather than relying on the caller to set an appropriate mode.
260.bP
261It also obtains the \fIerase\fP and \fIkill\fP characters
262from \fBerasechar\fP and \fBkillchar\fP, respectively.
263.bP
264On return, \fBgetnstr\fP restores the modes to their previous values.
265.PP
266Other implementations differ in their treatment of special characters:
267.bP
268While they may set the \fIecho\fP mode,
269other implementations do not modify the \fIraw\fP mode,
270They may take the \fIcbreak\fP
271mode set by the caller into account when deciding whether to handle
272echoing within \fBgetnstr\fP or as a side-effect of the \fBgetch\fP calls.
273.bP
274The original \fI\%ncurses\fP
275(as \fIpcurses\fP in 1986)
276set \fBnoraw\fP and \fBcbreak\fP when accepting input for \fBgetnstr\fP.
277That may have been done to make function- and cursor-keys work;
278it is not necessary with \fI\%ncurses\fP.
279.IP
280Since 1995,
281\fI\%ncurses\fP has provided signal handlers for INTR and QUIT
282(e.g., \fB^C\fP or \fB^\e\fP).
283With the \fBnoraw\fP and \fBcbreak\fP settings,
284those may catch a signal and stop the program,
285where other implementations allow one to enter those characters in the buffer.
286.bP
287Starting in 2021
288(\fI\%ncurses\fP 6.3),
289\fBgetnstr\fP sets \fBraw\fP,
290rather than \fBnoraw\fP and \fBcbreak\fP for better compatibility with
291SVr4-curses, e.g., allowing one to enter a \fB^C\fP into the buffer.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530292.SH SEE ALSO
micky3879b9f5e72025-07-08 18:04:53 -0400293\fB\%curs_get_wstr\fP(3X) describes comparable functions of the
294.I \%ncurses
295library in its wide-character configuration
296.RI ( \%ncursesw ).
297.PP
298\fB\%curses\fP(3X),
299\fB\%curs_getch\fP(3X),
300\fB\%curs_termattrs\fP(3X),
301\fB\%curs_variables\fP(3X)