curs_getstr.3x - Man Page

accept character strings from curses terminal keyboard

Synopsis

#include <curses.h>

int getstr(char * str);
int wgetstr(WINDOW * win, char * str);
int mvgetstr(int y, int x, char * str);
int mvwgetstr(WINDOW * win, int y, int x, char * str);

int getnstr(char * str, int n);
int wgetnstr(WINDOW * win, char * str, int n);
int mvgetnstr(int y, int x, char * str, int n);
int mvwgetnstr(WINDOW * win, int y, int x, char * str, int n);

Description

wgetstr populates a user-supplied string buffer str by repeatedly calling wgetch(3X) with the win argument until a line feed or carriage return character is input. The function

The erase character replaces the character at the end of the buffer with a null character, while the kill character does the same for the entire buffer.

If the screen's echo option is enabled (see echo(3X)), wgetstr updates win with wechochar(3X). Further,

wgetnstr is similar, but reads at most n characters, aiding the application to avoid overrunning the buffer to which str points. An attempt to input more than n characters (other than the terminating line feed or carriage return) is ignored with a beep.

ncurses(3X) describes the variants of these functions.

Return Value

These functions return OK on success and ERR on failure.

In ncurses, they return ERR if

Further, in ncurses, these functions return KEY_RESIZE if a SIGWINCH event interrupts the function.

Functions prefixed with “mv” first perform cursor movement and fail if the position (y, x) is outside the window boundaries.

Notes

All of these functions except wgetnstr may be implemented as macros.

Use of getstr, mvgetstr, mvwgetstr, or wgetstr to read input that overruns the buffer pointed to by str causes undefined results. Use their n-infixed counterpart functions instead.

While wgetnstr is conceptually a series of calls to wgetch, it also temporarily changes properties of the curses screen to permit simple editing of the input buffer. It saves the screen's state and then calls nl(3X) and, if the screen was in normal (“cooked”) mode, cbreak(3X). Before returning, it restores the saved screen state. Other implementations differ in detail, affecting which control characters they can accept in the buffer; see section “Portability” below.

Extensions

The return value KEY_RESIZE is an ncurses extension.

Portability

Applications employing ncurses extensions should condition their use on the visibility of the NCURSES_VERSION preprocessor macro.

X/Open Curses Issue 4 describes these functions. It specifies no error conditions for them, but indicates that wgetnstr and its variants read “the entire multi-byte sequence associated with a character” and “fail” if n and str together do not describe a buffer “large enough to contain any complete characters”. In ncurses, however, wgetch reads only single-byte characters, so this scenario does not arise.

SVr4 curses describes a successful return value only as “an integer value other than ERR”.

SVr3 and early SVr4 curses implementations did not reject function keys; the SVr4 documentation asserted that, like the screen's erase and kill characters, they were

interpreted, as well as any special keys (such as function keys, “home” key, “clear” key, etc.)

without further detail. 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 code's KEY_ constant value. (The same language, unchanged except for styling, survived into X/Open Curses Issue 4,  but disappeared from Issue 7.)

X/Open Curses Issue 5 (2007) stated that these functions “read at most n bytes” but did not state whether the terminating null character counted toward that limit. X/Open Curses Issue 7 (2009) changed that to say they “read at most n-1 bytes” to allow for the terminating null character. As of 2018, some implementations count it, some do not.

In SVr4 curses, a negative n tells wgetnstr to assume that the caller's buffer is large enough to hold the result; that is, the function then acts like wgetstr. X/Open Curses does not mention this behavior (or anything related to nonpositive n values), however most curses libraries implement it. Most implementations nevertheless enforce an upper limit on the count of bytes they write to the destination buffer str.

Implementations vary in their handling of input control characters.

History

4BSD (1980) curses introduced wgetstr along with its variants.

SVr3.1 (1987) added wgetnstr, but none of its variants.

X/Open Curses Issue 4 (1995) specified getnstr, mvwgetnstr, and mvgetnstr.

See Also

curs_get_wstr(3X) describes comparable functions of the ncurses library in its wide-character configuration (ncursesw).

curses(3X), curs_addch(3X), curs_getch(3X), curs_inopts(3X),  curs_termattrs(3X),

Referenced By

The man pages getnstr.3x(3), getstr.3x(3), mvgetnstr.3x(3), mvgetstr.3x(3), mvwgetnstr.3x(3), mvwgetstr.3x(3), wgetnstr.3x(3) and wgetstr.3x(3) are aliases of curs_getstr.3x(3).

2024-06-22 ncurses 6.5 Library calls