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