Debian Codegraph @

upstream/opensuse-leap-16-1/man3/getstr.3ncurses

raw
1 .\"***************************************************************************
2 .\" Copyright 2018-2024,2025 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.89 2025/04/05 22:07:21 tom Exp $
31 .TH getstr 3NCURSES 2025-04-05 "ncurses 6.5" "Library calls"
32 .ie \n(.g \{\
33 .ds `` \(lq
34 .ds '' \(rq
35 .ds ^ \(ha
36 .\}
37 .el \{\
38 .ie t .ds `` ``
39 .el .ds `` ""
40 .ie t .ds '' ''
41 .el .ds '' ""
42 .ds ^ ^
43 .\}
44 .
45 .de bP
46 .ie n .IP \(bu 4
47 .el .IP \(bu 2
48 ..
49 .SH NAME
50 \fB\%getstr\fP,
51 \fB\%getnstr\fP,
52 \fB\%wgetstr\fP,
53 \fB\%wgetnstr\fP,
54 \fB\%mvgetstr\fP,
55 \fB\%mvgetnstr\fP,
56 \fB\%mvwgetstr\fP,
57 \fB\%mvwgetnstr\fP \-
58 read a character string from \fIcurses\fR terminal keyboard
59 .SH SYNOPSIS
60 .nf
61 \fB#include <ncursesw/curses.h>
62 .PP
63 \fBint getstr(char * \fIstr\fP);
64 \fBint wgetstr(WINDOW * \fIwin\fP, char * \fIstr\fP);
65 \fBint mvgetstr(int \fIy\fP, int \fIx\fP, char * \fIstr\fP);
66 \fBint mvwgetstr(WINDOW * \fIwin\fP, int \fIy\fP, int \fIx\fP, char * \fIstr\fP);
67 .PP
68 \fBint getnstr(char * \fIstr\fP, int \fIn\fP);
69 \fBint wgetnstr(WINDOW * \fIwin\fP, char * \fIstr\fP, int \fIn\fP);
70 \fBint mvgetnstr(int \fIy\fP, int \fIx\fP, char * \fIstr\fP, int \fIn\fP);
71 \fBint mvwgetnstr(WINDOW * \fIwin\fP, int \fIy\fP, int \fIx\fP, char * \fIstr\fP,
72 int \fIn\fP);
73 .fi
74 .SH DESCRIPTION
75 .B \%wgetstr
76 populates a user-supplied string buffer
77 .I str
78 by repeatedly calling \fBwgetch\fP(3NCURSES)
79 with the
80 .I win
81 argument
82 until a line feed or carriage return character is input.
83 .\" Of the two, because wgetnstr() calls nl(), only a line feed (\n)
84 .\" will ever be returned by wgetch().
85 The function
86 .bP
87 does not copy the terminating character to
88 .IR str ";"
89 .bP
90 always terminates
91 .I str
92 with a null character;
93 .bP
94 interprets the screen's erase and kill characters
95 (see \fB\%erasechar\fP(3NCURSES) and \fB\%killchar\fP(3NCURSES));
96 .bP
97 recognizes function keys only if the screen's keypad option is enabled
98 (see \fB\%keypad\fP(3NCURSES));
99 .bP
100 treats the function keys
101 .B \%KEY_LEFT
102 and
103 .B \%KEY_BACKSPACE
104 the same as the erase character;
105 and
106 .bP
107 discards function key inputs other than those
108 treated as the erase or kill characters,
109 calling \fBbeep\fP(3NCURSES).
110 .PP
111 The erase character replaces the character at the end of the buffer with
112 a null character,
113 while the kill character does the same for the entire buffer.
114 .PP
115 If the screen's echo option is enabled
116 (see \fBecho\fP(3NCURSES)),
117 .B \%wgetstr
118 updates
119 .I win
120 with \fB\%waddch\fP(3NCURSES).
121 Further,
122 .bP
123 the erase character
124 and its function key synonyms
125 move the cursor to the left,
126 and
127 .bP
128 the kill character returns the cursor to where it was located when
129 .B \%wgetstr
130 was called.
131 .PP
132 .B \%wgetnstr
133 is similar,
134 but reads at most
135 .I n
136 characters,
137 aiding the application to avoid overrunning the buffer to which
138 .I str
139 points.
140 .I curses
141 ignores an attempt to input more than
142 .I n
143 characters
144 (other than the terminating line feed or carriage return),
145 calling \fBbeep\fP(3NCURSES).
146 If
147 .I n
148 is negative,
149 .B \%wgetn_wstr
150 reads up to
151 .I LINE_MAX
152 characters
153 (see
154 .IR sysconf (3)).
155 .PP
156 \fB\%ncurses\fP(3NCURSES) describes the variants of these functions.
157 .SH RETURN VALUE
158 These functions return
159 .B OK
160 on success and
161 .B ERR
162 on failure.
163 .PP
164 In
165 .IR \%ncurses ,
166 these functions fail if
167 .bP
168 the
169 .I curses
170 screen has not been initialized,
171 .bP
172 (for functions taking a
173 .I \%WINDOW
174 pointer argument)
175 .I win
176 is a null pointer,
177 .bP
178 .I str
179 is a null pointer,
180 or
181 .bP
182 an internal \fB\%wgetch\fP(3NCURSES) call fails.
183 .PP
184 Further,
185 in
186 .IR \%ncurses ","
187 these functions return
188 .B \%KEY_RESIZE
189 if a
190 .I \%SIGWINCH
191 event interrupts the function.
192 .PP
193 Functions prefixed with \*(``mv\*('' first perform cursor movement and
194 fail if the position
195 .RI ( y ,
196 .IR x )
197 is outside the window boundaries.
198 .SH NOTES
199 All of these functions except
200 .B \%wgetnstr
201 may be implemented as macros.
202 .PP
203 Reading input that overruns the buffer pointed to by
204 .I str
205 causes undefined results.
206 Use the
207 .BR n -infixed
208 functions,
209 and allocate sufficient storage for
210 .I str
211 \(em at least
212 .IR n +1
213 times
214 .BR sizeof(char) "."
215 .PP
216 While these functions conceptually implement
217 a series of calls to
218 .BR \%wgetch ","
219 they also temporarily change properties of the
220 .I curses
221 screen to permit simple editing of the input buffer.
222 Each function saves the screen's state,
223 calls \fBnl\fP(3NCURSES),
224 and,
225 if the screen was in canonical (\*(``cooked\*('') mode,
226 \fB\%cbreak\fP(3NCURSES).
227 Before returning,
228 it restores the saved screen state.
229 Other implementations differ in detail,
230 affecting which control characters they can accept in the buffer;
231 see section \*(``PORTABILITY\*('' below.
232 .SH EXTENSIONS
233 .BR \%getnstr ","
234 .BR \%wgetnstr ","
235 .BR \%mvgetnstr ","
236 and
237 .BR \%mvwgetnstr "'s"
238 handing of negative
239 .I n
240 values is an
241 .I \%ncurses
242 extension.
243 .PP
244 The return value
245 .B \%KEY_RESIZE
246 is an
247 .I \%ncurses
248 extension.
249 .SH PORTABILITY
250 Applications employing
251 .I \%ncurses
252 extensions should condition their use on the visibility of the
253 .B \%NCURSES_VERSION
254 preprocessor macro.
255 .PP
256 X/Open Curses Issue\ 4 describes these functions.
257 It specifies no error conditions for them,
258 but indicates that
259 .I \%wgetnstr
260 and its variants read
261 \*(``the entire multi-byte sequence associated with a character\*(''
262 and \*(``fail\*('' if
263 .I n
264 and
265 .I str
266 together do not describe a buffer
267 \*(``large enough to contain any complete characters\*(''.
268 In
269 .IR \%ncurses ","
270 however,
271 .I \%wgetch
272 reads only single-byte characters,
273 so this scenario does not arise.
274 .\" You can pass ncurses wgetnstr n=0 and it will beep at you with each
275 .\" key stroke.
276 .PP
277 SVr4 describes a successful return value only as
278 \*(``an integer value other than
279 .IR ERR \*(''. \" Courier roman in source; SVID 4, vol. 3, p. 495
280 .PP
281 SVr3 and early SVr4
282 .I curses
283 implementations did not reject function keys;
284 the SVr4 documentation asserted that,
285 like the screen's erase and kill characters,
286 they were
287 .PP
288 .RS
289 interpreted,
290 as well as any special keys
291 (such as function keys,
292 \*(``home\*('' key,
293 \*(``clear\*('' key,
294 .IR etc. )
295 .\" SVID 4, vol. 3, p. 495
296 .RE
297 .PP
298 without further detail.
299 It lied.
300 The \*(``character\*('' value
301 appended to the string
302 by those implementations
303 was predictable but not useful \(em
304 being,
305 in fact,
306 the low-order eight bits of the key code's
307 .I KEY_
308 constant value.
309 (The same language,
310 unchanged except for styling,
311 survived into X/Open Curses Issue\ 4,
312 Version\ 2 \" p. 94 (PDF 114)
313 but disappeared from Issue\ 7.) \" p. 105 (PDF 119)
314 .PP
315 A draft of X/Open Curses Issue\ 5
316 (which never saw final release)
317 stated that these functions
318 \*(``read at most
319 .I n
320 bytes\*(''
321 but did not state whether the terminating null character
322 counted toward that limit.
323 X/Open Curses Issue\ 7 changed that to say they
324 \*(``read at most
325 .IR n \-1
326 bytes\*(''
327 to allow for the terminating null character.
328 As of 2018,
329 some implementations count it,
330 some do not.
331 .bP
332 .I \%ncurses
333 6.1 and
334 .I \%PDCurses
335 do not count the null character toward the limit,
336 while Solaris and NetBSD
337 .I curses
338 do.
339 .bP
340 Solaris
341 .I xcurses
342 offers both behaviors:
343 its wide-character
344 .I \%wgetn_wstr
345 reserves room for a wide null character,
346 but its non-wide
347 .I \%wgetnstr
348 does not consistently count a null character toward the limit.
349 .PP
350 X/Open Curses does not specify what happens if the length
351 .I n
352 is negative.
353 .bP
354 .I \%ncurses
355 6.2 uses
356 .I LINE_MAX
357 or a larger (system-dependent) value
358 provided by \fI\%sysconf\fP(3).
359 If neither
360 .I LINE_MAX
361 nor
362 .I \%sysconf
363 is available,
364 .I \%ncurses
365 uses the POSIX minimum value for
366 .I LINE_MAX
367 (2048). \" _POSIX2_LINE_MAX
368 In either case,
369 it reserves a byte for the terminating null character.
370 .bP
371 In SVr4
372 .IR curses ","
373 a negative
374 .I n
375 tells
376 .I \%wgetnstr
377 to assume that the caller's buffer
378 is large enough to hold the result;
379 that is,
380 the function then acts like
381 .IR \%wgetstr "."
382 X/Open Curses does not mention this behavior
383 (or anything related to nonpositive
384 .I n
385 values),
386 however most
387 .I curses
388 libraries implement it.
389 Most implementations nevertheless enforce an upper limit
390 on the count of bytes they write to the destination buffer
391 .IR str "."
392 .bP
393 BSD
394 .I curses
395 lacked
396 .IR \%wgetnstr ","
397 and its
398 .I \%wgetstr
399 wrote to
400 .I str
401 unboundedly,
402 .\" https://minnie.tuhs.org/cgi-bin/utree.pl?file=4BSD/usr/src/lib/\
403 .\" libcurses/getstr.c
404 .\" https://minnie.tuhs.org/cgi-bin/utree.pl?file=4.4BSD/usr/src/lib/\
405 .\" libcurses/getstr.c
406 as did that in SVr2.
407 .\" https://github.com/ryanwoodsmall/oldsysv/blob/master/sysvr2-vax/\
408 .\" src/lib/libcurses/screen/getstr.c
409 .bP
410 .IR PDCurses ","
411 and
412 SVr3 and later,
413 .\" https://github.com/ryanwoodsmall/oldsysv/blob/master/\
414 .\" sysvr3/301/usr/src/lib/libcurses/screen/getstr.c#L21
415 .\" sysvr3/31/usr/src/lib/libcurses/screen/wgetstr.c#L10
416 .\" sysvr4/svr4/lib/xlibcurses/screen/wgetstr.c#L12
417 and
418 Solaris
419 .I curses
420 limit both functions to writing 256 bytes.
421 Other System\ V-based platforms likely use the same limit.
422 .bP
423 Solaris
424 .I xcurses
425 limits the write to
426 .I LINE_MAX
427 bytes
428 (see
429 .IR sysconf (3)).
430 .bP
431 NetBSD 7
432 .I curses
433 imposes no particular limit on the length of the write,
434 but does validate
435 .I n
436 to ensure that it is greater than zero.
437 A comment in NetBSD's source code asserts that SUSv2 specifies this.
438 .PP
439 Implementations vary in their handling of input control characters.
440 .bP
441 While they may enable the screen's echo option,
442 some do not take it out of raw mode,
443 and may take cbreak mode into account
444 when deciding whether to handle echoing within
445 .I \%wgetnstr
446 or to rely on it as a side effect of calling
447 .IR \%wgetch "."
448 .bP
449 Originally,
450 .IR \%ncurses ","
451 like its progenitor
452 .IR \%pcurses ","
453 had its
454 .I \%wgetnstr
455 call
456 .I \%noraw
457 and
458 .I \%cbreak
459 before accepting input.
460 That may have been done to make function keys work;
461 it is not necessary with modern
462 .IR \%ncurses "."
463 .IP
464 Since 1995,
465 .I \%ncurses
466 has provided handlers for
467 .I SIGINTR
468 and
469 .I SIGQUIT
470 events,
471 which are typically generated at the keyboard with
472 .B \*^C
473 and
474 .B \*^\e
475 respectively.
476 In cbreak mode,
477 those handlers catch a signal and stop the program,
478 whereas other implementations write those characters into the buffer.
479 .bP
480 Starting with
481 .I \%ncurses
482 6.3 (2021),
483 .I \%wgetnstr
484 preserves raw mode if the screen was already in that state,
485 allowing one to enter the characters the terminal interprets
486 as interrupt and quit events
487 into the buffer,
488 for better compatibility with SVr4
489 .IR curses "."
490 .SH HISTORY
491 4BSD (1980)
492 introduced
493 .I \%wgetstr
494 along with its variants.
495 .PP
496 SVr3.1 (1987)
497 added
498 .IR \%wgetnstr ","
499 but none of its variants.
500 .PP
501 X/Open Curses Issue\ 4 (1995)
502 specified
503 .IR \%getnstr ","
504 .IR \%mvgetnstr ","
505 and
506 .IR \%mvwgetnstr "."
507 .SH SEE ALSO
508 \fB\%get_wstr\fP(3NCURSES) describes comparable functions of the
509 .I \%ncurses
510 library in its wide-character configuration
511 .RI \%( ncursesw ).
512 .PP
513 \fB\%ncurses\fP(3NCURSES),
514 \fB\%addch\fP(3NCURSES),
515 \fB\%getch\fP(3NCURSES),
516 \fB\%inopts\fP(3NCURSES), \" echo(), keypad()
517 \fB\%termattrs\fP(3NCURSES), \" erasechar(), killchar()