jcs's openbsd hax
openbsd
at jcs 411 lines 16 kB view raw
1'\" t 2.\" $OpenBSD: curs_util.3,v 1.7 2023/10/17 09:52:08 nicm Exp $ 3.\" 4.\"*************************************************************************** 5.\" Copyright 2018-2022,2023 Thomas E. Dickey * 6.\" Copyright 1998-2015,2017 Free Software Foundation, Inc. * 7.\" * 8.\" Permission is hereby granted, free of charge, to any person obtaining a * 9.\" copy of this software and associated documentation files (the * 10.\" "Software"), to deal in the Software without restriction, including * 11.\" without limitation the rights to use, copy, modify, merge, publish, * 12.\" distribute, distribute with modifications, sublicense, and/or sell * 13.\" copies of the Software, and to permit persons to whom the Software is * 14.\" furnished to do so, subject to the following conditions: * 15.\" * 16.\" The above copyright notice and this permission notice shall be included * 17.\" in all copies or substantial portions of the Software. * 18.\" * 19.\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 20.\" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 21.\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 22.\" IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 23.\" DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 24.\" OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 25.\" THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 26.\" * 27.\" Except as contained in this notice, the name(s) of the above copyright * 28.\" holders shall not be used in advertising or otherwise to promote the * 29.\" sale, use or other dealings in this Software without prior written * 30.\" authorization. * 31.\"*************************************************************************** 32.\" 33.\" $Id: curs_util.3,v 1.7 2023/10/17 09:52:08 nicm Exp $ 34.TH curs_util 3 2023-08-19 "ncurses 6.4" "Library calls" 35.ie \n(.g .ds `` \(lq 36.el .ds `` `` 37.ie \n(.g .ds '' \(rq 38.el .ds '' '' 39.de bP 40.ie n .IP \(bu 4 41.el .IP \(bu 2 42.. 43.na 44.hy 0 45.SH NAME 46\fBdelay_output\fP, 47\fBfilter\fP, 48\fBflushinp\fP, 49\fBgetwin\fP, 50\fBkey_name\fP, 51\fBkeyname\fP, 52\fBnofilter\fP, 53\fBputwin\fP, 54\fBunctrl\fP, 55\fBuse_env\fP, 56\fBuse_tioctl\fP, 57\fBwunctrl\fP \- miscellaneous \fBcurses\fP utility routines 58.ad 59.hy 60.SH SYNOPSIS 61\fB#include <curses.h>\fP 62.sp 63\fBconst char *unctrl(chtype \fIc\fB);\fR 64.br 65\fBwchar_t *wunctrl(cchar_t *\fIc\fB);\fR 66.sp 67\fBconst char *keyname(int \fIc\fB);\fR 68.br 69\fBconst char *key_name(wchar_t \fIw\fB);\fR 70.sp 71\fBvoid filter(void);\fP 72.sp 73\fBvoid use_env(bool \fIf\fB);\fR 74.sp 75\fBint putwin(WINDOW *\fIwin\fB, FILE *\fIfilep\fB);\fR 76.br 77\fBWINDOW *getwin(FILE *\fIfilep\fB);\fR 78.sp 79\fBint delay_output(int \fIms\fB);\fR 80.br 81\fBint flushinp(void);\fP 82.sp 83/* extensions */ 84.br 85\fBvoid nofilter(void);\fP 86.br 87\fBvoid use_tioctl(bool \fIf\fB);\fR 88.SH DESCRIPTION 89.SS unctrl 90The \fBunctrl\fP routine returns a character string which is a printable 91representation of the character \fIc\fP: 92.bP 93Printable characters are displayed as themselves, 94e.g., a one-character string containing the key. 95.bP 96Control characters are displayed in the \fB^\fIX\fR notation. 97.bP 98Printing characters are displayed as is. 99.bP 100DEL (character 127) is displayed as \fB^?\fP. 101.bP 102Values above 128 are either meta characters 103(if the screen has not been initialized, 104or if \fBmeta\fP(3) has been called with a \fBTRUE\fP parameter), 105shown in the \fBM\-\fIX\fR notation, 106or are displayed as themselves. 107In the latter case, the values may not be printable; 108this follows the X/Open specification. 109.PP 110The corresponding \fBwunctrl\fP returns a printable representation of 111a complex character \fIc\fP. 112.PP 113In both \fBunctrl\fP and \fBwunctrl\fP the attributes and color associated 114with the character parameter are ignored. 115.SS keyname/key_name 116The \fBkeyname\fP routine returns a character string 117corresponding to the key \fIc\fP. 118Key codes are different from character codes. 119.bP 120Key codes below 256 are characters. 121They are displayed using \fBunctrl\fP. 122.bP 123Values above 256 may be the codes for function keys. 124The function key name is displayed. 125.bP 126Otherwise (if there is no corresponding name and the key is not a character) 127the function returns null, to denote an error. 128X/Open also lists an \*(``UNKNOWN KEY\*('' return value, 129which some implementations return rather than null. 130.LP 131The corresponding \fBkey_name\fP returns 132a multibyte character string corresponding 133to the wide-character value \fIw\fP. 134The two functions (\fBkeyname\fP and \fBkey_name\fP) 135do not return the same set of strings: 136.bP 137\fBkeyname\fP returns null where \fBkey_name\fP would display a meta character. 138.bP 139\fBkey_name\fP does not return the name of a function key. 140.SS filter/nofilter 141The \fBfilter\fP routine, if used, must be called before \fBinitscr\fP or 142\fBnewterm\fP are called. 143Calling \fBfilter\fP causes these changes in initialization: 144.bP 145\fBLINES\fP is set to 1; 146.bP 147the capabilities 148\fBclear\fP, 149\fBcud1\fP, 150\fBcud\fP, 151\fBcup\fP, 152\fBcuu1\fP, 153\fBcuu\fP, 154\fBvpa\fP 155are disabled; 156.bP 157the capability \fBed\fP is disabled if \fBbce\fP is set; 158.bP 159and the \fBhome\fP string is set to the value of \fBcr\fP. 160.PP 161The \fBnofilter\fP routine cancels the effect of a preceding \fBfilter\fP 162call. 163That allows the caller to initialize a screen on a different device, 164using a different value of \fB$TERM\fP. 165The limitation arises because the \fBfilter\fP routine modifies the 166in-memory copy of the terminal information. 167.SS use_env 168The \fBuse_env\fP routine, if used, 169should be called before \fBinitscr\fP or 170\fBnewterm\fP are called 171(because those compute the screen size). 172It modifies the way \fBncurses\fP treats environment variables 173when determining the screen size. 174.bP 175Normally \fBncurses\fP looks first at the terminal database for the screen size. 176.IP 177If \fBuse_env\fP was called with \fBFALSE\fP for parameter, 178it stops here unless 179\fBuse_tioctl\fP was also called with \fBTRUE\fP for parameter. 180.bP 181Then it asks for the screen size via operating system calls. 182If successful, 183it overrides the values from the terminal database. 184.bP 185Finally (unless \fBuse_env\fP was called with \fBFALSE\fP parameter), 186\fBncurses\fP examines the \fBLINES\fP or \fBCOLUMNS\fP environment variables, 187using a value in those to override the results 188from the operating system or terminal database. 189.IP 190\fBNcurses\fP also updates the screen size in response to \fBSIGWINCH\fP, 191unless overridden by the \fBLINES\fP or \fBCOLUMNS\fP environment variables, 192.SS use_tioctl 193The \fBuse_tioctl\fP routine, if used, 194should be called before \fBinitscr\fP or \fBnewterm\fP are called 195(because those compute the screen size). 196After \fBuse_tioctl\fP is called with \fBTRUE\fP as an argument, 197\fBncurses\fP modifies the last step in its computation 198of screen size as follows: 199.bP 200checks if the \fBLINES\fP and \fBCOLUMNS\fP environment variables 201are set to a number greater than zero. 202.bP 203for each, \fBncurses\fP updates the corresponding environment variable 204with the value that it has obtained via operating system call 205or from the terminal database. 206.bP 207\fBncurses\fP re-fetches the value of the environment variables so that 208it is still the environment variables which set the screen size. 209.PP 210The \fBuse_env\fP and \fBuse_tioctl\fP routines combine as 211summarized here: 212.PP 213.TS 214center tab(/); 215l l l 216_ _ _ 217lw7 lw7 lw40. 218\fBuse_env\fP/\fBuse_tioctl\fP/\fBSummary\fP 219TRUE/FALSE/T{ 220This is the default behavior. 221\fBncurses\fP uses operating system calls 222unless overridden by $LINES or $COLUMNS environment variables. 223T} 224TRUE/TRUE/T{ 225\fBncurses\fP updates $LINES and $COLUMNS based on operating system calls. 226T} 227FALSE/TRUE/T{ 228\fBncurses\fP ignores $LINES and $COLUMNS, 229uses operating system calls to obtain size. 230T} 231FALSE/FALSE/T{ 232\fBncurses\fP relies on the terminal database to determine size. 233T} 234.TE 235.SS putwin/getwin 236The \fBputwin\fP routine writes all data associated 237with window (or pad) \fIwin\fP into 238the file to which \fIfilep\fP points. 239This information can be later retrieved 240using the \fBgetwin\fP function. 241.PP 242The \fBgetwin\fP routine reads window related data stored in the file by 243\fBputwin\fP. 244The routine then creates and initializes a new window using that 245data. 246It returns a pointer to the new window. 247There are a few caveats: 248.bP 249the data written is a copy of the \fBWINDOW\fP structure, 250and its associated character cells. 251The format differs between the wide-character (\fBncursesw\fP) and 252non-wide (\fBncurses\fP) libraries. 253You can transfer data between the two, however. 254.bP 255the retrieved window is always created as a top-level window (or pad), 256rather than a subwindow. 257.bP 258the window's character cells contain the color pair \fIvalue\fP, 259but not the actual color \fInumbers\fP. 260If cells in the retrieved window use color pairs which have not been 261created in the application using \fBinit_pair\fP, 262they will not be colored when the window is refreshed. 263.SS delay_output 264The \fBdelay_output\fP routine inserts an \fIms\fP millisecond pause 265in output. 266This routine should not be used extensively because 267padding characters are used rather than a CPU pause. 268If no padding character is specified, 269this uses \fBnapms\fP to perform the delay. 270.SS flushinp 271The \fBflushinp\fP routine throws away any typeahead that has been typed by the 272user and has not yet been read by the program. 273.SH RETURN VALUE 274Except for \fBflushinp\fP, routines that return an integer return \fBERR\fP 275upon failure and \fBOK\fP (SVr4 specifies only "an integer value other than 276\fBERR\fP") upon successful completion. 277.PP 278Routines that return pointers return \fBNULL\fP on error. 279.PP 280X/Open does not define any error conditions. 281In this implementation 282.RS 3 283.TP 5 284\fBflushinp\fP 285returns an error if the terminal was not initialized. 286.TP 5 287\fBputwin\fP 288returns an error if the associated \fBfwrite\fP calls return an error. 289.RE 290.SH PORTABILITY 291.SS filter 292The SVr4 documentation describes the action of \fBfilter\fP only in the vaguest 293terms. 294The description here is adapted from the XSI Curses standard (which 295erroneously fails to describe the disabling of \fBcuu\fP). 296.SS keyname 297The \fBkeyname\fP function may return the names of user-defined 298string capabilities which are defined in the terminfo entry via the \fB\-x\fP 299option of \fBtic\fP. 300This implementation automatically assigns at run-time keycodes to 301user-defined strings which begin with \*(``k\*(''. 302The keycodes start at KEY_MAX, but are not guaranteed to be 303the same value for different runs because user-defined codes are 304merged from all terminal descriptions which have been loaded. 305The \fBuse_extended_names\fP(3) function controls whether this data is 306loaded when the terminal description is read by the library. 307.SS nofilter/use_tioctl 308The \fBnofilter\fP and \fBuse_tioctl\fP routines are specific to \fBncurses\fP. 309They were not supported on Version 7, BSD or System V implementations. 310It is recommended that any code depending on \fBncurses\fP extensions 311be conditioned using NCURSES_VERSION. 312.SS putwin/getwin file-format 313The \fBputwin\fP and \fBgetwin\fP functions have several issues with 314portability: 315.bP 316The files written and read by these functions 317use an implementation-specific format. 318Although the format is an obvious target for standardization, 319it has been overlooked. 320.IP 321Interestingly enough, according to the copyright dates in Solaris source, 322the functions (along with \fBscr_init\fP, etc.) originated with 323the University of California, Berkeley (in 1982) 324and were later (in 1988) incorporated into SVr4. 325Oddly, there are no such functions in the 4.3BSD curses sources. 326.bP 327Most implementations simply dump the binary \fBWINDOW\fP structure to the file. 328These include SVr4 curses, NetBSD and PDCurses, 329as well as older \fBncurses\fP versions. 330This implementation 331(as well as the X/Open variant of Solaris curses, dated 1995) 332uses textual dumps. 333.IP 334The implementations which use binary dumps use block-I/O 335(the \fBfwrite\fP and \fBfread\fP functions). 336Those that use textual dumps use buffered-I/O. 337A few applications may happen to write extra data in the file using 338these functions. 339Doing that can run into problems mixing block- and buffered-I/O. 340This implementation reduces the problem on writes by flushing the output. 341However, reading from a file written using mixed schemes may not be successful. 342.SS unctrl/wunctrl 343The XSI Curses standard, Issue 4 describes these functions. 344It states that \fBunctrl\fP and \fBwunctrl\fP will return a null pointer if 345unsuccessful, but does not define any error conditions. 346This implementation checks for three cases: 347.bP 348the parameter is a 7-bit US\-ASCII code. 349This is the case that X/Open Curses documented. 350.bP 351the parameter is in the range 128\-159, i.e., a C1 control code. 352If \fBuse_legacy_coding\fP(3) has been called with a \fB2\fP parameter, 353\fBunctrl\fP returns the parameter, i.e., a one-character string with 354the parameter as the first character. 355Otherwise, it returns \*(``~@\*('', \*(``~A\*('', etc., 356analogous to \*(``^@\*('', \*(``^A\*('', C0 controls. 357.IP 358X/Open Curses does not document whether \fBunctrl\fP can be called before 359initializing curses. 360This implementation permits that, 361and returns the \*(``~@\*('', etc., values in that case. 362.bP 363parameter values outside the 0 to 255 range. 364\fBunctrl\fP returns a null pointer. 365.PP 366The strings returned by \fBunctrl\fP in this implementation are determined 367at compile time, 368showing C1 controls from the upper-128 codes 369with a \*(``~\*('' prefix rather than \*(``^\*(''. 370Other implementations have different conventions. 371For example, they may show both sets of control characters with \*(``^\*('', 372and strip the parameter to 7 bits. 373Or they may ignore C1 controls and treat all of the upper-128 codes as 374printable. 375This implementation uses 8 bits but does not modify the string to reflect 376locale. 377The \fBuse_legacy_coding\fP(3) function allows the caller to 378change the output of \fBunctrl\fP. 379.PP 380Likewise, the \fBmeta\fP(3) function allows the caller to change the 381output of \fBkeyname\fP, i.e., 382it determines whether to use the \*(``M\-\*('' prefix 383for \*(``meta\*('' keys (codes in the range 128 to 255). 384Both \fBuse_legacy_coding\fP(3) and \fBmeta\fP(3) succeed only after 385curses is initialized. 386X/Open Curses does not document the treatment of codes 128 to 159. 387When treating them as \*(``meta\*('' keys 388(or if \fBkeyname\fP is called before initializing curses), 389this implementation returns strings \*(``M\-^@\*('', \*(``M\-^A\*('', etc. 390.PP 391X/Open Curses documents \fBunctrl\fP as declared in \fB<unctrl.h>\fP, 392which \fBncurses\fP does. 393However, \fBncurses\fP' \fB<curses.h>\fP includes \fB<unctrl.h>\fP, 394matching the behavior of SVr4 curses. 395Other implementations may not do that. 396.SS use_env/use_tioctl 397If \fBncurses\fP is configured to provide the sp-functions extension, 398the state of \fBuse_env\fP and \fBuse_tioctl\fP may be updated before 399creating each \fIscreen\fP rather than once only 400(\fBcurs_sp_funcs\fP(3)). 401This feature of \fBuse_env\fP 402is not provided by other implementations of curses. 403.SH SEE ALSO 404\fBcurses\fP(3), 405\fBcurs_initscr\fP(3), 406\fBcurs_inopts\fP(3), 407\fBcurs_kernel\fP(3), 408\fBcurs_scr_dump\fP(3), 409\fBcurs_sp_funcs\fP(3), 410\fBcurs_variables\fP(3), 411\fBlegacy_coding\fP(3).