Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1 | .\"*************************************************************************** |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 2 | .\" Copyright 2018-2023,2024 Thomas E. Dickey * |
| 3 | .\" Copyright 1998-2010,2017 Free Software Foundation, Inc. * |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 | .\" |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 30 | .\" $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 Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 47 | .SH NAME |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 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 |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 57 | .SH SYNOPSIS |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 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 |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 71 | .SH DESCRIPTION |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 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. |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 94 | .PP |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 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. |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 106 | .PP |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 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). |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 111 | .PP |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 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. |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 130 | .SH RETURN VALUE |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 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. |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 134 | .PP |
| 135 | X/Open defines no error conditions. |
| 136 | .PP |
| 137 | In this implementation, |
| 138 | these functions return an error |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 139 | .bP |
| 140 | if the window pointer is null, |
| 141 | .bP |
| 142 | if its timeout expires without having any data, or |
| 143 | .bP |
| 144 | if the associated call to |
| 145 | \fBwgetch\fP |
| 146 | failed. |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 147 | .PP |
| 148 | This implementation provides an extension as well. |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 149 | If a \fBSIGWINCH\fP interrupts the function, it will return \fBKEY_RESIZE\fP |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 150 | rather than \fBOK\fP or \fBERR\fP. |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 151 | .PP |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 152 | Functions prefixed with \*(``mv\*('' first perform cursor movement and |
| 153 | fail if the position |
| 154 | .RI ( y , |
| 155 | .IR x ) |
| 156 | is outside the window boundaries. |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 157 | .SH NOTES |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 158 | Any of these functions other than |
| 159 | \fBwgetnstr\fP |
| 160 | may be macros. |
| 161 | .PP |
| 162 | Using |
| 163 | \fBgetstr\fP, |
| 164 | \fBmvgetstr\fP, |
| 165 | \fBmvwgetstr\fP, or |
| 166 | \fBwgetstr\fP |
| 167 | to read a line that |
| 168 | overflows the array pointed to by |
| 169 | \fBstr\fP |
| 170 | causes undefined |
| 171 | results. |
| 172 | The use of |
| 173 | \fBgetnstr\fP, |
| 174 | \fBmvgetnstr\fP, |
| 175 | \fBmvwgetnstr\fP, or |
| 176 | \fBwgetnstr\fP, |
| 177 | respectively, is recommended. |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 178 | .SH PORTABILITY |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 179 | These functions are described in The Single Unix Specification, Version 2. |
| 180 | No error conditions are defined. |
| 181 | .PP |
| 182 | This implementation returns \fBERR\fP if the window pointer is null, |
| 183 | or if the lower-level \fBwgetch\fP(3X) call returns an \fBERR\fP. |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 184 | .PP |
| 185 | SVr3 and early SVr4 curses implementations did not reject function keys; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 186 | the SVr4.0 documentation claimed that \*(``special keys\*('' |
| 187 | (such as function keys, |
| 188 | \*(``home\*('' key, |
| 189 | \*(``clear\*('' key, |
| 190 | \fIetc\fP.) are \*(``interpreted\*('', |
| 191 | without giving details. |
| 192 | It lied. |
| 193 | In fact, the \*(``character\*('' value appended to the |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 194 | string 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 |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 197 | The functions \fBgetnstr\fP, \fBmvgetnstr\fP, and \fBmvwgetnstr\fP were |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 198 | present but not documented in SVr4. |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 199 | .PP |
| 200 | X/Open Curses, Issue 5 (2007) stated that these functions |
| 201 | \*(``read at most \fIn\fP bytes\*('' |
| 202 | but did not state whether the terminating NUL is counted in that limit. |
| 203 | X/Open Curses, Issue 7 (2009) changed that to say they |
| 204 | \*(``read at most \fIn\fP\-1 bytes\*('' |
| 205 | to allow for the terminating NUL. |
| 206 | As 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 |
| 210 | Solaris SVr4 and NetBSD curses count the NUL as part of the limit. |
| 211 | .bP |
| 212 | Solaris xcurses provides both: |
| 213 | its wide-character \fBwget_nstr\fP reserves a NUL, |
| 214 | but its \fBwgetnstr\fP does not count the NUL consistently. |
| 215 | .PP |
| 216 | In SVr4 curses, |
| 217 | a negative value of \fIn\fP tells \fBwgetnstr\fP to assume that the |
| 218 | caller's buffer is large enough to hold the result, |
| 219 | i.e., to act like \fBwgetstr\fP. |
| 220 | X/Open Curses does not mention this |
| 221 | (or anything related to negative or zero values of \fIn\fP), |
| 222 | however most implementations |
| 223 | use the feature, with different limits: |
| 224 | .bP |
| 225 | Solaris SVr4 curses and PDCurses limit the result to 255 bytes. |
| 226 | Other Unix systems than Solaris are likely to use the same limit. |
| 227 | .bP |
| 228 | Solaris xcurses limits the result to \fBLINE_MAX\fP bytes. |
| 229 | .bP |
| 230 | NetBSD 7 assumes no particular limit for the result from \fBwgetstr\fP. |
| 231 | However, it limits the \fBwgetnstr\fP parameter \fIn\fP to ensure |
| 232 | that it is greater than zero. |
| 233 | .IP |
| 234 | A 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 |
| 237 | from \fBwgetstr\fP, and treats the \fIn\fP parameter of \fBwgetnstr\fP |
| 238 | like SVr4 curses. |
| 239 | .bP |
| 240 | \fI\%ncurses\fP 6.2 uses \fBLINE_MAX\fP, |
| 241 | or a larger (system-dependent) value |
| 242 | which the \fBsysconf\fP function may provide. |
| 243 | If 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). |
| 245 | In either case, it reserves a byte for the terminating NUL. |
| 246 | .PP |
| 247 | Although \fBgetnstr\fP is equivalent to a series of calls to \fBgetch\fP, |
| 248 | it also makes changes to the curses modes to allow simple editing of |
| 249 | the 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, |
| 259 | rather than relying on the caller to set an appropriate mode. |
| 260 | .bP |
| 261 | It also obtains the \fIerase\fP and \fIkill\fP characters |
| 262 | from \fBerasechar\fP and \fBkillchar\fP, respectively. |
| 263 | .bP |
| 264 | On return, \fBgetnstr\fP restores the modes to their previous values. |
| 265 | .PP |
| 266 | Other implementations differ in their treatment of special characters: |
| 267 | .bP |
| 268 | While they may set the \fIecho\fP mode, |
| 269 | other implementations do not modify the \fIraw\fP mode, |
| 270 | They may take the \fIcbreak\fP |
| 271 | mode set by the caller into account when deciding whether to handle |
| 272 | echoing within \fBgetnstr\fP or as a side-effect of the \fBgetch\fP calls. |
| 273 | .bP |
| 274 | The original \fI\%ncurses\fP |
| 275 | (as \fIpcurses\fP in 1986) |
| 276 | set \fBnoraw\fP and \fBcbreak\fP when accepting input for \fBgetnstr\fP. |
| 277 | That may have been done to make function- and cursor-keys work; |
| 278 | it is not necessary with \fI\%ncurses\fP. |
| 279 | .IP |
| 280 | Since 1995, |
| 281 | \fI\%ncurses\fP has provided signal handlers for INTR and QUIT |
| 282 | (e.g., \fB^C\fP or \fB^\e\fP). |
| 283 | With the \fBnoraw\fP and \fBcbreak\fP settings, |
| 284 | those may catch a signal and stop the program, |
| 285 | where other implementations allow one to enter those characters in the buffer. |
| 286 | .bP |
| 287 | Starting in 2021 |
| 288 | (\fI\%ncurses\fP 6.3), |
| 289 | \fBgetnstr\fP sets \fBraw\fP, |
| 290 | rather than \fBnoraw\fP and \fBcbreak\fP for better compatibility with |
| 291 | SVr4-curses, e.g., allowing one to enter a \fB^C\fP into the buffer. |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 292 | .SH SEE ALSO |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 293 | \fB\%curs_get_wstr\fP(3X) describes comparable functions of the |
| 294 | .I \%ncurses |
| 295 | library 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) |