Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1 | '\" te |
| 2 | .\" Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd. |
| 3 | .\" All Rights Reserved. |
| 4 | .\" Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the |
| 5 | .\" "Software"), to deal in the Software without restriction, including |
| 6 | .\" without limitation the rights to use, copy, modify, merge, publish, |
| 7 | .\" distribute, and/or sell copies of the Software, and to permit persons |
| 8 | .\" to whom the Software is furnished to do so, provided that the above |
| 9 | .\" copyright notice(s) and this permission notice appear in all copies of |
| 10 | .\" the Software and that both the above copyright notice(s) and this |
| 11 | .\" permission notice appear in supporting documentation. |
Yuri Pankov | ed22c71 | 2011-10-03 04:36:40 -0700 | [diff] [blame] | 12 | .\" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 13 | .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 14 | .\" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 15 | .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT |
| 16 | .\" OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 17 | .\" HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL |
| 18 | .\" INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING |
| 19 | .\" FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | .\" NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION |
| 21 | .\" WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Yuri Pankov | ed22c71 | 2011-10-03 04:36:40 -0700 | [diff] [blame] | 22 | .\" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 23 | .\" Except as contained in this notice, the name of a copyright holder |
| 24 | .\" shall not be used in advertising or otherwise to promote the sale, use |
| 25 | .\" or other dealings in this Software without prior written authorization |
| 26 | .\" of the copyright holder. |
| 27 | .\" Portions Copyright (c) 2007, Sun Microsystems, Inc. All Rights Reserved. |
Cody Peter Mello | 843c398 | 2016-04-09 23:18:13 +0000 | [diff] [blame] | 28 | .TH GL_GET_LINE 3TECLA "April 9, 2016" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 29 | .SH NAME |
| 30 | gl_get_line, new_GetLine, del_GetLine, gl_customize_completion, |
| 31 | gl_change_terminal, gl_configure_getline, gl_load_history, gl_save_history, |
| 32 | gl_group_history, gl_show_history, gl_watch_fd, gl_inactivity_timeout, |
| 33 | gl_terminal_size, gl_set_term_size, gl_resize_history, gl_limit_history, |
| 34 | gl_clear_history, gl_toggle_history, gl_lookup_history, gl_state_of_history, |
| 35 | gl_range_of_history, gl_size_of_history, gl_echo_mode, gl_replace_prompt, |
| 36 | gl_prompt_style, gl_ignore_signal, gl_trap_signal, gl_last_signal, |
| 37 | gl_completion_action, gl_register_action, gl_display_text, gl_return_status, |
| 38 | gl_error_message, gl_catch_blocked, gl_list_signals, gl_bind_keyseq, |
| 39 | gl_erase_terminal, gl_automatic_history, gl_append_history, gl_query_char, |
| 40 | gl_read_char \- allow the user to compose an input line |
| 41 | .SH SYNOPSIS |
| 42 | .LP |
| 43 | .nf |
Yuri Pankov | ed22c71 | 2011-10-03 04:36:40 -0700 | [diff] [blame] | 44 | cc [ \fIflag\fR\&.\|.\|. ] \fIfile\fR\&.\|.\|. \fB-ltecla\fR [ \fIlibrary\fR\&.\|.\|. ] |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 45 | #include <stdio.h> |
| 46 | #include <libtecla.h> |
| 47 | |
| 48 | \fBGetLine *\fR\fBnew_GetLine\fR(\fBsize_t\fR \fIlinelen\fR, \fBsize_t\fR \fIhistlen\fR); |
| 49 | .fi |
| 50 | |
| 51 | .LP |
| 52 | .nf |
| 53 | \fBGetLine *\fR\fBdel_GetLine\fR(\fBGetLine *\fR\fIgl\fR); |
| 54 | .fi |
| 55 | |
| 56 | .LP |
| 57 | .nf |
| 58 | \fBchar *\fR\fBgl_get_line\fR(\fBGetLine *\fR\fIgl\fR, \fBconst char *\fR\fIprompt\fR, |
| 59 | \fBconst char *\fR\fIstart_line\fR, \fBint\fR \fIstart_pos\fR); |
| 60 | .fi |
| 61 | |
| 62 | .LP |
| 63 | .nf |
| 64 | \fBint\fR \fBgl_query_char\fR(\fBGetLine *\fR\fIgl\fR, \fBconst char *\fR\fIprompt\fR, \fBchar\fR \fIdefchar\fR); |
| 65 | .fi |
| 66 | |
| 67 | .LP |
| 68 | .nf |
| 69 | \fBint\fR \fBgl_read_char\fR(\fBGetLine *\fR\fIgl\fR); |
| 70 | .fi |
| 71 | |
| 72 | .LP |
| 73 | .nf |
| 74 | \fBint\fR \fBgl_customize_completion\fR(\fBGetLine *\fR\fIgl\fR, \fBvoid *\fR\fIdata\fR, |
| 75 | \fBCplMatchFn *\fR\fImatch_fn\fR); |
| 76 | .fi |
| 77 | |
| 78 | .LP |
| 79 | .nf |
| 80 | \fBint\fR \fBgl_change_terminal\fR(\fBGetLine *\fR\fIgl\fR, \fBFILE *\fR\fIinput_fp\fR, |
| 81 | \fBFILE *\fR\fIoutput_fp\fR, \fBconst char *\fR\fIterm\fR); |
| 82 | .fi |
| 83 | |
| 84 | .LP |
| 85 | .nf |
| 86 | \fBint\fR \fBgl_configure_getline\fR(\fBGetLine *\fR\fIgl\fR, \fBconst char *\fR\fIapp_string\fR, |
| 87 | \fBconst char *\fR\fIapp_file\fR,\ \fBconst char *\fR\fIuser_file\fR); |
| 88 | .fi |
| 89 | |
| 90 | .LP |
| 91 | .nf |
| 92 | \fBint\fR \fBgl_bind_keyseq\fR(\fBGetLine *\fR\fIgl\fR, \fBGlKeyOrigin\fR \fIorigin\fR, |
| 93 | \fBconst char *\fR\fIkeyseq\fR, \fBconst char *\fR\fIaction\fR); |
| 94 | .fi |
| 95 | |
| 96 | .LP |
| 97 | .nf |
| 98 | \fBint\fR \fBgl_save_history\fR(\fBGetLine *\fR\fIgl\fR, \fBconst char *\fR\fIfilename\fR, |
| 99 | \fBconst char *\fR\fIcomment\fR, \fBint\fR \fImax_lines\fR); |
| 100 | .fi |
| 101 | |
| 102 | .LP |
| 103 | .nf |
| 104 | \fBint\fR \fBgl_load_history\fR(\fBGetLine *\fR\fIgl\fR, \fBconst char *\fR\fIfilename\fR, |
| 105 | \fBconst char *\fR\fIcomment\fR); |
| 106 | .fi |
| 107 | |
| 108 | .LP |
| 109 | .nf |
| 110 | \fBint\fR \fBgl_watch_fd\fR(\fBGetLine *\fR\fIgl\fR, \fBint\fR \fIfd\fR, \fBGlFdEvent\fR \fIevent\fR, |
| 111 | \fBGlFdEventFn *\fR\fIcallback\fR, \fBvoid *\fR\fIdata\fR); |
| 112 | .fi |
| 113 | |
| 114 | .LP |
| 115 | .nf |
| 116 | \fBint\fR \fBgl_inactivity_timeout\fR(\fBGetLine *\fR\fIgl\fR, \fBGlTimeoutFn *\fR\fIcallback\fR, |
| 117 | \fBvoid *\fR\fIdata\fR, \fBunsigned long\fR \fIsec\fR, \fBunsigned long\fR \fInsec\fR); |
| 118 | .fi |
| 119 | |
| 120 | .LP |
| 121 | .nf |
| 122 | \fBint\fR \fBgl_group_history\fR(\fBGetLine *\fR\fIgl\fR, \fBunsigned\fR \fIstream\fR); |
| 123 | .fi |
| 124 | |
| 125 | .LP |
| 126 | .nf |
| 127 | \fBint\fR \fBgl_show_history\fR(\fBGetLine *\fR\fIgl\fR, \fBFILE *\fR\fIfp\fR, \fBconst char *\fR\fIfmt\fR, |
| 128 | \fBint\fR \fIall_groups\fR, \fBint\fR \fImax_lines\fR); |
| 129 | .fi |
| 130 | |
| 131 | .LP |
| 132 | .nf |
| 133 | \fBint\fR \fBgl_resize_history\fR(\fBGetLine *\fR\fIgl\fR, \fBsize_t\fR \fIbufsize\fR); |
| 134 | .fi |
| 135 | |
| 136 | .LP |
| 137 | .nf |
| 138 | \fBvoid\fR \fBgl_limit_history\fR(\fBGetLine *\fR\fIgl\fR, \fBint\fR \fImax_lines\fR); |
| 139 | .fi |
| 140 | |
| 141 | .LP |
| 142 | .nf |
| 143 | \fBvoid\fR \fBgl_clear_history\fR(\fBGetLine *\fR\fIgl\fR, \fBint\fR \fIall_groups\fR); |
| 144 | .fi |
| 145 | |
| 146 | .LP |
| 147 | .nf |
| 148 | \fBvoid\fR \fBgl_toggle_history\fR(\fBGetLine *\fR\fIgl\fR, \fBint\fR \fIenable\fR); |
| 149 | .fi |
| 150 | |
| 151 | .LP |
| 152 | .nf |
| 153 | \fBGlTerminalSize\fR \fBgl_terminal_size\fR(\fBGetLine *\fR\fIgl\fR, \fBint\fR \fIdef_ncolumn\fR, |
| 154 | \fBint\fR \fIdef_nline\fR); |
| 155 | .fi |
| 156 | |
| 157 | .LP |
| 158 | .nf |
| 159 | \fBint\fR \fBgl_set_term_size\fR(\fBGetLine *\fR\fIgl\fR, \fBint\fR \fIncolumn\fR, \fBint\fR \fInline\fR); |
| 160 | .fi |
| 161 | |
| 162 | .LP |
| 163 | .nf |
| 164 | \fBint\fR \fBgl_lookup_history\fR(\fBGetLine *\fR\fIgl\fR, \fBunsigned long\fR \fIid\fR, |
| 165 | \fBGlHistoryLine *\fR\fIhline\fR); |
| 166 | .fi |
| 167 | |
| 168 | .LP |
| 169 | .nf |
| 170 | \fBvoid\fR \fBgl_state_of_history\fR(\fBGetLine *\fR\fIgl\fR, \fBGlHistoryState *\fR\fIstate\fR); |
| 171 | .fi |
| 172 | |
| 173 | .LP |
| 174 | .nf |
| 175 | \fBvoid\fR \fBgl_range_of_history\fR(\fBGetLine *\fR\fIgl\fR, \fBGlHistoryRange *\fR\fIrange\fR); |
| 176 | .fi |
| 177 | |
| 178 | .LP |
| 179 | .nf |
| 180 | \fBvoid\fR \fBgl_size_of_history\fR(\fBGetLine *\fR\fIgl\fR, \fBGlHistorySize *\fR\fIsize\fR); |
| 181 | .fi |
| 182 | |
| 183 | .LP |
| 184 | .nf |
| 185 | \fBvoid\fR \fBgl_echo_mode\fR(\fBGetLine *\fR\fIgl\fR, \fBint\fR \fIenable\fR); |
| 186 | .fi |
| 187 | |
| 188 | .LP |
| 189 | .nf |
| 190 | \fBvoid\fR \fBgl_replace_prompt\fR(\fBGetLine *\fR\fIgl\fR, \fBconst char *\fR\fIprompt\fR); |
| 191 | .fi |
| 192 | |
| 193 | .LP |
| 194 | .nf |
| 195 | \fBvoid\fR \fBgl_prompt_style\fR(\fBGetLine *\fR\fIgl\fR, \fBGlPromptStyle\fR \fIstyle\fR); |
| 196 | .fi |
| 197 | |
| 198 | .LP |
| 199 | .nf |
| 200 | \fBint\fR \fBgl_ignore_signal\fR(\fBGetLine *\fR\fIgl\fR, \fBint\fR \fIsigno\fR); |
| 201 | .fi |
| 202 | |
| 203 | .LP |
| 204 | .nf |
| 205 | \fBint\fR \fBgl_trap_signal\fR(\fBGetLine *\fR\fIgl\fR, \fBint\fR \fIsigno\fR, \fBunsigned\fR \fIflags\fR, |
| 206 | \fBGlAfterSignal\fR \fIafter\fR, \fBint\fR \fIerrno_value\fR); |
| 207 | .fi |
| 208 | |
| 209 | .LP |
| 210 | .nf |
| 211 | \fBint\fR \fBgl_last_signal\fR(\fBGetLine *\fR\fIgl\fR); |
| 212 | .fi |
| 213 | |
| 214 | .LP |
| 215 | .nf |
| 216 | \fBint\fR \fBgl_completion_action\fR(\fBGetLine *\fR\fIgl\fR, \fBvoid *\fR\fIdata\fR, |
| 217 | \fBCplMatchFn *\fR\fImatch_fn\fR, \fBint\fR \fIlist_only\fR, \fBconst char *\fR\fIname\fR, |
| 218 | \fBconst char *\fR\fIkeyseq\fR); |
| 219 | .fi |
| 220 | |
| 221 | .LP |
| 222 | .nf |
| 223 | \fBint\fR \fBgl_register_action\fR(\fBGetLine *\fR\fIgl\fR, \fBvoid *\fR\fIdata\fR, \fBGlActionFn *\fR\fIfn\fR, |
| 224 | \fBconst char *\fR\fIname\fR, \fBconst char *\fR\fIkeyseq\fR); |
| 225 | .fi |
| 226 | |
| 227 | .LP |
| 228 | .nf |
| 229 | \fBint\fR \fBgl_display_text\fR(\fBGetLine *\fR\fIgl\fR, \fBint\fR \fIindentation\fR, |
| 230 | \fBconst char *\fR\fIprefix\fR, \fBconst char *\fR\fIsuffix\fR, \fBint\fR \fIfill_char\fR, |
| 231 | \fBint\fR \fIdef_width\fR, \fBint\fR \fIstart\fR, \fBconst char *\fR\fIstring\fR); |
| 232 | .fi |
| 233 | |
| 234 | .LP |
| 235 | .nf |
| 236 | \fBGlReturnStatus\fR \fBgl_return_status\fR(\fBGetLine *\fR\fIgl\fR); |
| 237 | .fi |
| 238 | |
| 239 | .LP |
| 240 | .nf |
| 241 | \fBconst char *\fR\fBgl_error_message\fR(\fBGetLine *\fR\fIgl\fR, \fBchar *\fR\fIbuff\fR, \fBsize_t\fR \fIn\fR); |
| 242 | .fi |
| 243 | |
| 244 | .LP |
| 245 | .nf |
| 246 | \fBvoid\fR \fBgl_catch_blocked\fR(\fBGetLine *\fR\fIgl\fR); |
| 247 | .fi |
| 248 | |
| 249 | .LP |
| 250 | .nf |
| 251 | \fBint\fR \fBgl_list_signals\fR(\fBGetLine *\fR\fIgl\fR, \fBsigset_t *\fR\fIset\fR); |
| 252 | .fi |
| 253 | |
| 254 | .LP |
| 255 | .nf |
| 256 | \fBint\fR \fBgl_append_history\fR(\fBGetLine *\fR\fIgl\fR, \fBconst char *\fR\fIline\fR); |
| 257 | .fi |
| 258 | |
| 259 | .LP |
| 260 | .nf |
| 261 | \fBint\fR \fBgl_automatic_history\fR(\fBGetLine *\fR\fIgl\fR, \fBint\fR \fIenable\fR); |
| 262 | .fi |
| 263 | |
| 264 | .LP |
| 265 | .nf |
| 266 | \fBint\fR \fBgl_erase_terminal\fR(\fBGetLine *\fR\fIgl\fR); |
| 267 | .fi |
| 268 | |
| 269 | .SH DESCRIPTION |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 270 | .LP |
| 271 | The \fBgl_get_line()\fR function is part of the \fBlibtecla\fR(3LIB) library. |
Cody Peter Mello | 843c398 | 2016-04-09 23:18:13 +0000 | [diff] [blame] | 272 | If the user is typing at a terminal, each call prompts them for a line of |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 273 | input, then provides interactive editing facilities, similar to those of the |
| 274 | UNIX \fBtcsh\fR shell. In addition to simple command-line editing, it supports |
| 275 | recall of previously entered command lines, TAB completion of file names, and |
| 276 | in-line wild-card expansion of filenames. Documentation of both the user-level |
| 277 | command-line editing features and all user configuration options can be found |
| 278 | on the \fBtecla\fR(5) manual page. |
| 279 | .SS "An Example" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 280 | .LP |
| 281 | The following shows a complete example of how to use the \fBgl_get_line()\fR |
| 282 | function to get input from the user: |
| 283 | .sp |
| 284 | .in +2 |
| 285 | .nf |
| 286 | #include <stdio.h> |
| 287 | #include <locale.h> |
| 288 | #include <libtecla.h> |
| 289 | |
| 290 | int main(int argc, char *argv[]) |
| 291 | { |
| 292 | char *line; /* The line that the user typed */ |
| 293 | GetLine *gl; /* The gl_get_line() resource object */ |
| 294 | |
| 295 | setlocale(LC_CTYPE, ""); /* Adopt the user's choice */ |
| 296 | /* of character set. */ |
| 297 | |
| 298 | gl = new_GetLine(1024, 2048); |
| 299 | if(!gl) |
| 300 | return 1; |
| 301 | while((line=gl_get_line(gl, "$ ", NULL, -1)) != NULL && |
| 302 | strcmp(line, "exit\en") != 0) |
| 303 | printf("You typed: %s\en", line); |
| 304 | |
| 305 | gl = del_GetLine(gl); |
| 306 | return 0; |
| 307 | } |
| 308 | .fi |
| 309 | .in -2 |
| 310 | |
| 311 | .sp |
| 312 | .LP |
| 313 | In the example, first the resources needed by the \fBgl_get_line()\fR function |
| 314 | are created by calling \fBnew_GetLine()\fR. This allocates the memory used in |
| 315 | subsequent calls to the \fBgl_get_line()\fR function, including the history |
| 316 | buffer for recording previously entered lines. Then one or more lines are read |
| 317 | from the user, until either an error occurs, or the user types exit. Then |
| 318 | finally the resources that were allocated by \fBnew_GetLine()\fR, are returned |
| 319 | to the system by calling \fBdel_GetLine()\fR. Note the use of the \fINULL\fR |
| 320 | return value of \fBdel_GetLine()\fR to make \fIgl\fR \fINULL\fR. This is a |
| 321 | safety precaution. If the program subsequently attempts to pass \fIgl\fR to |
| 322 | \fBgl_get_line()\fR, said function will complain, and return an error, instead |
| 323 | of attempting to use the deleted resource object. |
| 324 | .SS "The Functions Used In The Example" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 325 | .LP |
| 326 | The \fBnew_GetLine()\fR function creates the resources used by the |
| 327 | \fBgl_get_line()\fR function and returns an opaque pointer to the object that |
| 328 | contains them. The maximum length of an input line is specified by the |
| 329 | \fIlinelen\fR argument, and the number of bytes to allocate for storing history |
| 330 | lines is set by the \fIhistlen\fR argument. History lines are stored |
| 331 | back-to-back in a single buffer of this size. Note that this means that the |
| 332 | number of history lines that can be stored at any given time, depends on the |
| 333 | lengths of the individual lines. If you want to place an upper limit on the |
| 334 | number of lines that can be stored, see the description of the |
| 335 | \fBgl_limit_history()\fR function. If you do not want history at all, specify |
| 336 | \fIhistlen\fR as zero, and no history buffer will be allocated. |
| 337 | .sp |
| 338 | .LP |
| 339 | On error, a message is printed to \fBstderr\fR and \fINULL\fR is returned. |
| 340 | .sp |
| 341 | .LP |
| 342 | The \fBdel_GetLine()\fR function deletes the resources that were returned by a |
| 343 | previous call to \fBnew_GetLine()\fR. It always returns \fINULL\fR (for |
| 344 | example, a deleted object). It does nothing if the \fIgl\fR argument is |
| 345 | \fINULL\fR. |
| 346 | .sp |
| 347 | .LP |
| 348 | The \fBgl_get_line()\fR function can be called any number of times to read |
| 349 | input from the user. The gl argument must have been previously returned by a |
| 350 | call to \fBnew_GetLine()\fR. The \fIprompt\fR argument should be a normal |
| 351 | null-terminated string, specifying the prompt to present the user with. By |
| 352 | default prompts are displayed literally, but if enabled with the |
| 353 | \fBgl_prompt_style()\fR function, prompts can contain directives to do |
| 354 | underlining, switch to and from bold fonts, or turn highlighting on and off. |
| 355 | .sp |
| 356 | .LP |
| 357 | If you want to specify the initial contents of the line for the user to edit, |
| 358 | pass the desired string with the \fIstart_line\fR argument. You can then |
| 359 | specify which character of this line the cursor is initially positioned over by |
| 360 | using the \fIstart_pos\fR argument. This should be -1 if you want the cursor to |
| 361 | follow the last character of the start line. If you do not want to preload the |
| 362 | line in this manner, send \fIstart_line\fR as \fINULL\fR, and set |
| 363 | \fIstart_pos\fR to -1. |
| 364 | .sp |
| 365 | .LP |
| 366 | The \fBgl_get_line()\fR function returns a pointer to the line entered by the |
| 367 | user, or \fINULL\fR on error or at the end of the input. The returned pointer |
| 368 | is part of the specified \fIgl\fR resource object, and thus should not be freed |
| 369 | by the caller, or assumed to be unchanging from one call to the next. When |
| 370 | reading from a user at a terminal, there will always be a newline character at |
| 371 | the end of the returned line. When standard input is being taken from a pipe or |
| 372 | a file, there will similarly be a newline unless the input line was too long to |
| 373 | store in the internal buffer. In the latter case you should call |
| 374 | \fBgl_get_line()\fR again to read the rest of the line. Note that this behavior |
| 375 | makes \fBgl_get_line()\fR similar to \fBfgets\fR(3C). When \fBstdin\fR is not |
| 376 | connected to a terminal, \fBgl_get_line()\fR simply calls \fBfgets()\fR. |
| 377 | .SS "The Return Status Of \fBgl_get_line()\fR" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 378 | .LP |
| 379 | The \fBgl_get_line()\fR function has two possible return values: a pointer to |
| 380 | the completed input line, or \fINULL\fR. Additional information about what |
| 381 | caused \fBgl_get_line()\fR to return is available both by inspecting |
| 382 | \fBerrno\fR and by calling the \fBgl_return_status()\fR function. |
| 383 | .sp |
| 384 | .LP |
| 385 | The following are the possible enumerated values returned by |
| 386 | \fBgl_return_status()\fR: |
| 387 | .sp |
| 388 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 389 | .na |
| 390 | \fB\fBGLR_NEWLINE\fR\fR |
| 391 | .ad |
| 392 | .RS 15n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 393 | The last call to \fBgl_get_line()\fR successfully returned a completed input |
| 394 | line. |
| 395 | .RE |
| 396 | |
| 397 | .sp |
| 398 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 399 | .na |
| 400 | \fB\fBGLR_BLOCKED\fR\fR |
| 401 | .ad |
| 402 | .RS 15n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 403 | The \fBgl_get_line()\fR function was in non-blocking server mode, and returned |
| 404 | early to avoid blocking the process while waiting for terminal I/O. The |
| 405 | \fBgl_pending_io()\fR function can be used to see what type of I/O |
| 406 | \fBgl_get_line()\fR was waiting for. See the \fBgl_io_mode\fR(3TECLA). |
| 407 | .RE |
| 408 | |
| 409 | .sp |
| 410 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 411 | .na |
| 412 | \fB\fBGLR_SIGNAL\fR\fR |
| 413 | .ad |
| 414 | .RS 15n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 415 | A signal was caught by \fBgl_get_line()\fR that had an after-signal disposition |
| 416 | of \fBGLS_ABORT\fR. See \fBgl_trap_signal()\fR. |
| 417 | .RE |
| 418 | |
| 419 | .sp |
| 420 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 421 | .na |
| 422 | \fB\fBGLR_TIMEOUT\fR\fR |
| 423 | .ad |
| 424 | .RS 15n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 425 | The inactivity timer expired while \fBgl_get_line()\fR was waiting for input, |
| 426 | and the timeout callback function returned \fBGLTO_ABORT\fR. See |
| 427 | \fBgl_inactivity_timeout()\fR for information about timeouts. |
| 428 | .RE |
| 429 | |
| 430 | .sp |
| 431 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 432 | .na |
| 433 | \fB\fBGLR_FDABORT\fR\fR |
| 434 | .ad |
| 435 | .RS 15n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 436 | An application I/O callback returned \fBGLFD_ABORT\fR. Ssee |
| 437 | \fBgl_watch_fd()\fR. |
| 438 | .RE |
| 439 | |
| 440 | .sp |
| 441 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 442 | .na |
| 443 | \fB\fBGLR_EOF\fR\fR |
| 444 | .ad |
| 445 | .RS 15n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 446 | End of file reached. This can happen when input is coming from a file or a |
| 447 | pipe, instead of the terminal. It also occurs if the user invokes the |
| 448 | list-or-eof or del-char-or-list-or-eof actions at the start of a new line. |
| 449 | .RE |
| 450 | |
| 451 | .sp |
| 452 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 453 | .na |
| 454 | \fB\fBGLR_ERROR\fR\fR |
| 455 | .ad |
| 456 | .RS 15n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 457 | An unexpected error caused \fBgl_get_line()\fR to abort (consult \fBerrno\fR |
| 458 | and/or \fBgl_error_message()\fR for details. |
| 459 | .RE |
| 460 | |
| 461 | .sp |
| 462 | .LP |
| 463 | When \fBgl_return_status()\fR returns \fBGLR_ERROR\fR and the value of |
| 464 | \fBerrno\fR is not sufficient to explain what happened, you can use the |
| 465 | \fBgl_error_message()\fR function to request a description of the last error |
| 466 | that occurred. |
| 467 | .sp |
| 468 | .LP |
| 469 | The return value of \fBgl_error_message()\fR is a pointer to the message that |
| 470 | occurred. If the \fIbuff\fR argument is \fINULL\fR, this will be a pointer to a |
| 471 | buffer within \fIgl\fR whose value will probably change on the next call to any |
| 472 | function associated with \fBgl_get_line()\fR. Otherwise, if a non-null |
| 473 | \fIbuff\fR argument is provided, the error message, including a '\e0' |
| 474 | terminator, will be written within the first \fIn\fR elements of this buffer, |
| 475 | and the return value will be a pointer to the first element of this buffer. If |
| 476 | the message will not fit in the provided buffer, it will be truncated to fit. |
| 477 | .SS "Optional Prompt Formatting" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 478 | .LP |
| 479 | Whereas by default the prompt string that you specify is displayed literally |
| 480 | without any special interpretation of the characters within it, the |
| 481 | \fBgl_prompt_style()\fR function can be used to enable optional formatting |
| 482 | directives within the prompt. |
| 483 | .sp |
| 484 | .LP |
| 485 | The \fIstyle\fR argument, which specifies the formatting style, can take any of |
| 486 | the following values: |
| 487 | .sp |
| 488 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 489 | .na |
| 490 | \fB\fBGL_FORMAT_PROMPT\fR\fR |
| 491 | .ad |
| 492 | .RS 21n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 493 | In this style, the formatting directives described below, when included in |
| 494 | prompt strings, are interpreted as follows: |
| 495 | .sp |
| 496 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 497 | .na |
| 498 | \fB\fB%B\fR\fR |
| 499 | .ad |
| 500 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 501 | Display subsequent characters with a bold font. |
| 502 | .RE |
| 503 | |
| 504 | .sp |
| 505 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 506 | .na |
| 507 | \fB\fB%b\fR\fR |
| 508 | .ad |
| 509 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 510 | Stop displaying characters with the bold font. |
| 511 | .RE |
| 512 | |
| 513 | .sp |
| 514 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 515 | .na |
| 516 | \fB\fB%F\fR\fR |
| 517 | .ad |
| 518 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 519 | Make subsequent characters flash. |
| 520 | .RE |
| 521 | |
| 522 | .sp |
| 523 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 524 | .na |
| 525 | \fB\fB%f\fR\fR |
| 526 | .ad |
| 527 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 528 | Turn off flashing characters. |
| 529 | .RE |
| 530 | |
| 531 | .sp |
| 532 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 533 | .na |
| 534 | \fB\fB%U\fR\fR |
| 535 | .ad |
| 536 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 537 | Underline subsequent characters. |
| 538 | .RE |
| 539 | |
| 540 | .sp |
| 541 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 542 | .na |
| 543 | \fB\fB%u\fR\fR |
| 544 | .ad |
| 545 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 546 | Stop underlining characters. |
| 547 | .RE |
| 548 | |
| 549 | .sp |
| 550 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 551 | .na |
| 552 | \fB\fB%P\fR\fR |
| 553 | .ad |
| 554 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 555 | Switch to a pale (half brightness) font. |
| 556 | .RE |
| 557 | |
| 558 | .sp |
| 559 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 560 | .na |
| 561 | \fB\fB%p\fR\fR |
| 562 | .ad |
| 563 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 564 | Stop using the pale font. |
| 565 | .RE |
| 566 | |
| 567 | .sp |
| 568 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 569 | .na |
| 570 | \fB\fB%S\fR\fR |
| 571 | .ad |
| 572 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 573 | Highlight subsequent characters (also known as standout mode). |
| 574 | .RE |
| 575 | |
| 576 | .sp |
| 577 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 578 | .na |
| 579 | \fB\fB%s\fR\fR |
| 580 | .ad |
| 581 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 582 | Stop highlighting characters. |
| 583 | .RE |
| 584 | |
| 585 | .sp |
| 586 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 587 | .na |
| 588 | \fB\fB%V\fR\fR |
| 589 | .ad |
| 590 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 591 | Turn on reverse video. |
| 592 | .RE |
| 593 | |
| 594 | .sp |
| 595 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 596 | .na |
| 597 | \fB\fB%v\fR\fR |
| 598 | .ad |
| 599 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 600 | Turn off reverse video. |
| 601 | .RE |
| 602 | |
| 603 | .sp |
| 604 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 605 | .na |
| 606 | \fB\fB%%\fR\fR |
| 607 | .ad |
| 608 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 609 | Display a single % character. |
| 610 | .RE |
| 611 | |
| 612 | For example, in this mode, a prompt string like "%UOK%u$" would display the |
| 613 | prompt "OK$", but with the OK part underlined. |
| 614 | .sp |
| 615 | Note that although a pair of characters that starts with a % character, but |
| 616 | does not match any of the above directives is displayed literally, if a new |
| 617 | directive is subsequently introduced which does match, the displayed prompt |
| 618 | will change, so it is better to always use %% to display a literal %. |
| 619 | .sp |
| 620 | Also note that not all terminals support all of these text attributes, and that |
| 621 | some substitute a different attribute for missing ones. |
| 622 | .RE |
| 623 | |
| 624 | .sp |
| 625 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 626 | .na |
| 627 | \fB\fBGL_LITERAL_PROMPT\fR\fR |
| 628 | .ad |
| 629 | .RS 21n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 630 | In this style, the prompt string is printed literally. This is the default |
| 631 | style. |
| 632 | .RE |
| 633 | |
| 634 | .SS "Alternate Configuration Sources" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 635 | .LP |
| 636 | By default users have the option of configuring the behavior of |
| 637 | \fBgl_get_line()\fR with a configuration file called \fB\&.teclarc\fR in their |
| 638 | home directories. The fact that all applications share this same configuration |
| 639 | file is both an advantage and a disadvantage. In most cases it is an advantage, |
| 640 | since it encourages uniformity, and frees the user from having to configure |
| 641 | each application separately. In some applications, however, this single means |
| 642 | of configuration is a problem. This is particularly true of embedded software, |
| 643 | where there's no filesystem to read a configuration file from, and also in |
| 644 | applications where a radically different choice of keybindings is needed to |
| 645 | emulate a legacy keyboard interface. To cater for such cases, the |
| 646 | \fBgl_configure_getline()\fR function allows the application to control where |
| 647 | configuration information is read from. |
| 648 | .sp |
| 649 | .LP |
| 650 | The \fBgl_configure_getline()\fR function allows the configuration commands |
| 651 | that would normally be read from a user's \fB~/.teclarc\fR file, to be read |
| 652 | from any or none of, a string, an application specific configuration file, |
| 653 | and/or a user-specific configuration file. If this function is called before |
| 654 | the first call to \fBgl_get_line()\fR, the default behavior of reading |
| 655 | \fB~/.teclarc\fR on the first call to \fBgl_get_line()\fR is disabled, so all |
| 656 | configurations must be achieved using the configuration sources specified with |
| 657 | this function. |
| 658 | .sp |
| 659 | .LP |
| 660 | If \fIapp_string\fR != \fINULL\fR, then it is interpreted as a string |
| 661 | containing one or more configuration commands, separated from each other in the |
| 662 | string by embedded newline characters. If \fIapp_file\fR != \fINULL\fR then it |
| 663 | is interpreted as the full pathname of an application-specific configuration |
| 664 | file. If user_file != \fINULL\fR then it is interpreted as the full path name |
| 665 | of a user-specific configuration file, such as \fB~/.teclarc\fR. For example, |
| 666 | in the call |
| 667 | .sp |
| 668 | .in +2 |
| 669 | .nf |
Bart Coddens | 98cdf45 | 2013-09-10 20:33:47 +0200 | [diff] [blame] | 670 | gl_configure_getline(gl, "edit-mode vi \en nobeep", |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 671 | "/usr/share/myapp/teclarc", "~/.teclarc"); |
| 672 | .fi |
| 673 | .in -2 |
| 674 | |
| 675 | .sp |
| 676 | .LP |
| 677 | The \fIapp_string\fR argument causes the calling application to start in |
| 678 | \fBvi\fR(1) edit-mode, instead of the default \fBemacs\fR mode, and turns off |
| 679 | the use of the terminal bell by the library. It then attempts to read |
| 680 | system-wide configuration commands from an optional file called |
| 681 | \fB/usr/share/myapp/teclarc\fR, then finally reads user-specific configuration |
| 682 | commands from an optional \fB\&.teclarc\fR file in the user's home directory. |
| 683 | Note that the arguments are listed in ascending order of priority, with the |
| 684 | contents of \fIapp_string\fR being potentially over riden by commands in |
Peter Tribble | 0327063 | 2018-12-10 21:26:43 +0000 | [diff] [blame^] | 685 | \fIapp_file\fR, and commands in \fIapp_file\fR potentially being overridden by |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 686 | commands in \fIuser_file\fR. |
| 687 | .sp |
| 688 | .LP |
| 689 | You can call this function as many times as needed, the results being |
| 690 | cumulative, but note that copies of any file names specified with the |
| 691 | \fIapp_file\fR and \fIuser_file\fR arguments are recorded internally for |
| 692 | subsequent use by the read-init-files key-binding function, so if you plan to |
| 693 | call this function multiple times, be sure that the last call specifies the |
| 694 | filenames that you want re-read when the user requests that the configuration |
| 695 | files be re-read. |
| 696 | .sp |
| 697 | .LP |
| 698 | Individual key sequences can also be bound and unbound using the |
| 699 | \fBgl_bind_keyseq()\fR function. The \fIorigin\fR argument specifies the |
| 700 | priority of the binding, according to whom it is being established for, and |
| 701 | must be one of the following two values. |
| 702 | .sp |
| 703 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 704 | .na |
| 705 | \fB\fBGL_USER_KEY\fR\fR |
| 706 | .ad |
| 707 | .RS 15n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 708 | The user requested this key-binding. |
| 709 | .RE |
| 710 | |
| 711 | .sp |
| 712 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 713 | .na |
| 714 | \fB\fBGL_APP_KEY\fR\fR |
| 715 | .ad |
| 716 | .RS 15n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 717 | This is a default binding set by the application. |
| 718 | .RE |
| 719 | |
| 720 | .sp |
| 721 | .LP |
| 722 | When both user and application bindings for a given key sequence have been |
| 723 | specified, the user binding takes precedence. The application's binding is |
| 724 | subsequently reinstated if the user's binding is later unbound with either |
| 725 | another call to this function, or a call to \fBgl_configure_getline()\fR. |
| 726 | .sp |
| 727 | .LP |
| 728 | The \fIkeyseq\fR argument specifies the key sequence to be bound or unbound, |
| 729 | and is expressed in the same way as in a \fB~/.teclarc\fR configuration file. |
| 730 | The \fIaction\fR argument must either be a string containing the name of the |
| 731 | action to bind the key sequence to, or it must be \fINULL\fR or \fB""\fR to |
| 732 | unbind the key sequence. |
| 733 | .SS "Customized Word Completion" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 734 | .LP |
| 735 | If in your application you would like to have TAB completion complete other |
| 736 | things in addition to or instead of filenames, you can arrange this by |
| 737 | registering an alternate completion callback function with a call to the |
| 738 | \fBgl_customize_completion()\fR function. |
| 739 | .sp |
| 740 | .LP |
| 741 | The \fIdata\fR argument provides a way for your application to pass arbitrary, |
| 742 | application-specific information to the callback function. This is passed to |
| 743 | the callback every time that it is called. It might for example point to the |
| 744 | symbol table from which possible completions are to be sought. The |
| 745 | \fImatch_fn\fR argument specifies the callback function to be called. The |
| 746 | \fICplMatchFn\fR function type is defined in <\fBlibtecla.h\fR>, as is a |
| 747 | \fBCPL_MATCH_FN()\fR macro that you can use to declare and prototype callback |
| 748 | functions. The declaration and responsibilities of callback functions are |
| 749 | described in depth on the \fBcpl_complete_word\fR(3TECLA) manual page. |
| 750 | .sp |
| 751 | .LP |
| 752 | The callback function is responsible for looking backwards in the input line |
| 753 | from the point at which the user pressed TAB, to find the start of the word |
| 754 | being completed. It then must lookup possible completions of this word, and |
| 755 | record them one by one in the \fBWordCompletion\fR object that is passed to it |
| 756 | as an argument, by calling the \fBcpl_add_completion()\fR function. If the |
| 757 | callback function wants to provide filename completion in addition to its own |
| 758 | specific completions, it has the option of itself calling the builtin filename |
| 759 | completion callback. This also is documented on the |
| 760 | \fBcpl_complete_word\fR(3TECLA) manual page. |
| 761 | .sp |
| 762 | .LP |
| 763 | If you would like \fBgl_get_line()\fR to return the current input line when a |
| 764 | successful completion is been made, you can arrange this when you call |
| 765 | \fBcpl_add_completion()\fR by making the last character of the continuation |
| 766 | suffix a newline character. The input line will be updated to display the |
| 767 | completion, together with any contiuation suffix up to the newline character, |
| 768 | and \fBgl_get_line()\fR will return this input line. |
| 769 | .sp |
| 770 | .LP |
| 771 | If your callback function needs to write something to the terminal, it must |
| 772 | call \fBgl_normal_io()\fR before doing so. This will start a new line after the |
| 773 | input line that is currently being edited, reinstate normal terminal I/O, and |
| 774 | notify \fBgl_get_line()\fR that the input line will need to be redrawn when the |
| 775 | callback returns. |
| 776 | .SS "Adding Completion Actions" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 777 | .LP |
| 778 | In the previous section the ability to customize the behavior of the only |
| 779 | default completion action, complete-word, was described. In this section the |
| 780 | ability to install additional action functions, so that different types of word |
| 781 | completion can be bound to different key sequences, is described. This is |
| 782 | achieved by using the \fBgl_completion_action()\fR function. |
| 783 | .sp |
| 784 | .LP |
| 785 | The \fIdata\fR and \fImatch_fn\fR arguments are as described on the |
| 786 | \fBcpl_complete_word\fR(3TECLA) manual page, and specify the callback function |
| 787 | that should be invoked to identify possible completions. The \fIlist_only\fR |
| 788 | argument determines whether the action that is being defined should attempt to |
| 789 | complete the word as far as possible in the input line before displaying any |
| 790 | possible ambiguous completions, or whether it should simply display the list of |
| 791 | possible completions without touching the input line. The former option is |
| 792 | selected by specifying a value of 0, and the latter by specifying a value of 1. |
| 793 | The \fIname\fR argument specifies the name by which configuration files and |
| 794 | future invocations of this function should refer to the action. This must |
| 795 | either be the name of an existing completion action to be changed, or be a new |
| 796 | unused name for a new action. Finally, the \fIkeyseq\fR argument specifies the |
| 797 | default key sequence to bind the action to. If this is \fINULL\fR, no new key |
| 798 | sequence will be bound to the action. |
| 799 | .sp |
| 800 | .LP |
| 801 | Beware that in order for the user to be able to change the key sequence that is |
| 802 | bound to actions that are installed in this manner, you shouldcall |
| 803 | \fBgl_completion_action()\fR to install a given action for the first time |
| 804 | between calling \fBnew_GetLine()\fR and the first call to \fBgl_get_line()\fR. |
| 805 | Otherwise, when the user's configuration file is read on the first call to |
| 806 | \fBgl_get_line()\fR, the name of the your additional action will not be known, |
| 807 | and any reference to it in the configuration file will generate an error. |
| 808 | .sp |
| 809 | .LP |
| 810 | As discussed for \fBgl_customize_completion()\fR, if your callback function |
| 811 | needs to write anything to the terminal, it must call \fBgl_normal_io()\fR |
| 812 | before doing so. |
| 813 | .SS "Defining Custom Actions" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 814 | .LP |
| 815 | Although the built-in key-binding actions are sufficient for the needs of most |
| 816 | applications, occasionally a specialized application may need to define one or |
| 817 | more custom actions, bound to application-specific key sequences. For example, |
| 818 | a sales application would benefit from having a key sequence that displayed the |
| 819 | part name that corresponded to a part number preceding the cursor. Such a |
| 820 | feature is clearly beyond the scope of the built-in action functions. So for |
| 821 | such special cases, the \fBgl_register_action()\fR function is provided. |
| 822 | .sp |
| 823 | .LP |
| 824 | The \fBgl_register_action()\fR function lets the application register an |
| 825 | external function, \fIfn\fR, that will thereafter be called whenever either the |
| 826 | specified key sequence, \fIkeyseq\fR, is entered by the user, or the user |
| 827 | enters any other key sequence that the user subsequently binds to the specified |
| 828 | action name, \fIname\fR, in their configuration file. The \fIdata\fR argument |
| 829 | can be a pointer to anything that the application wants to have passed to the |
| 830 | action function, \fIfn\fR, whenever that function is invoked. |
| 831 | .sp |
| 832 | .LP |
| 833 | The action function, \fIfn\fR, should be declared using the |
| 834 | \fBGL_ACTION_FN()\fR macro, which is defined in <\fBlibtecla.h\fR>. |
| 835 | .sp |
| 836 | .in +2 |
| 837 | .nf |
| 838 | #define GL_ACTION_FN(fn) GlAfterAction (fn)(GetLine *gl, \e |
| 839 | void *data, int count, size_t curpos, \e |
| 840 | const char *line) |
| 841 | .fi |
| 842 | .in -2 |
| 843 | |
| 844 | .sp |
| 845 | .LP |
| 846 | The \fIgl\fR and \fIdata\fR arguments are those that were previously passed to |
| 847 | \fBgl_register_action()\fR when the action function was registered. The |
| 848 | \fIcount\fR argument is a numeric argument which the user has the option of |
| 849 | entering using the digit-argument action, before invoking the action. If the |
| 850 | user does not enter a number, then the \fIcount\fR argument is set to 1. |
| 851 | Nominally this argument is interpreted as a repeat count, meaning that the |
| 852 | action should be repeated that many times. In practice however, for some |
| 853 | actions a repeat count makes little sense. In such cases, actions can either |
| 854 | simply ignore the \fIcount\fR argument, or use its value for a different |
| 855 | purpose. |
| 856 | .sp |
| 857 | .LP |
| 858 | A copy of the current input line is passed in the read-only \fIline\fR |
| 859 | argument. The current cursor position within this string is given by the index |
| 860 | contained in the \fIcurpos\fR argument. Note that direct manipulation of the |
| 861 | input line and the cursor position is not permitted because the rules dictated |
| 862 | by various modes (such as \fBvi\fR mode versus \fBemacs\fR mode, no-echo mode, |
| 863 | and insert mode versus overstrike mode) make it too complex for an application |
| 864 | writer to write a conforming editing action, as well as constrain future |
| 865 | changes to the internals of \fBgl_get_line()\fR. A potential solution to this |
| 866 | dilemma would be to allow the action function to edit the line using the |
| 867 | existing editing actions. This is currently under consideration. |
| 868 | .sp |
| 869 | .LP |
| 870 | If the action function wishes to write text to the terminal without this |
| 871 | getting mixed up with the displayed text of the input line, or read from the |
| 872 | terminal without having to handle raw terminal I/O, then before doing either of |
| 873 | these operations, it must temporarily suspend line editing by calling the |
| 874 | \fBgl_normal_io()\fR function. This function flushes any pending output to the |
| 875 | terminal, moves the cursor to the start of the line that follows the last |
| 876 | terminal line of the input line, then restores the terminal to a state that is |
| 877 | suitable for use with the C \fBstdio\fR facilities. The latter includes such |
| 878 | things as restoring the normal mapping of \en to \er\en, and, when in server |
| 879 | mode, restoring the normal blocking form of terminal I/O. Having called this |
| 880 | function, the action function can read from and write to the terminal without |
| 881 | the fear of creating a mess. It is not necessary for the action function to |
| 882 | restore the original editing environment before it returns. This is done |
| 883 | automatically by \fBgl_get_line()\fR after the action function returns. The |
| 884 | following is a simple example of an action function which writes the sentence |
| 885 | "Hello world" on a new terminal line after the line being edited. When this |
| 886 | function returns, the input line is redrawn on the line that follows the "Hello |
| 887 | world" line, and line editing resumes. |
| 888 | .sp |
| 889 | .in +2 |
| 890 | .nf |
| 891 | static GL_ACTION_FN(say_hello_fn) |
| 892 | { |
| 893 | if(gl_normal_io(gl)) /* Temporarily suspend editing */ |
| 894 | return GLA_ABORT; |
| 895 | printf("Hello world\en"); |
| 896 | return GLA_CONTINUE; |
| 897 | } |
| 898 | .fi |
| 899 | .in -2 |
| 900 | |
| 901 | .sp |
| 902 | .LP |
| 903 | Action functions must return one of the following values, to tell |
| 904 | \fBgl_get_line()\fR how to proceed. |
| 905 | .sp |
| 906 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 907 | .na |
| 908 | \fB\fBGLA_ABORT\fR\fR |
| 909 | .ad |
| 910 | .RS 16n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 911 | Cause \fBgl_get_line()\fR to return \fINULL\fR. |
| 912 | .RE |
| 913 | |
| 914 | .sp |
| 915 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 916 | .na |
| 917 | \fB\fBGLA_RETURN\fR\fR |
| 918 | .ad |
| 919 | .RS 16n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 920 | Cause \fBgl_get_line()\fR to return the completed input line |
| 921 | .RE |
| 922 | |
| 923 | .sp |
| 924 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 925 | .na |
| 926 | \fB\fBGLA_CONTINUE\fR\fR |
| 927 | .ad |
| 928 | .RS 16n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 929 | Resume command-line editing. |
| 930 | .RE |
| 931 | |
| 932 | .sp |
| 933 | .LP |
| 934 | Note that the \fIname\fR argument of \fBgl_register_action()\fR specifies the |
| 935 | name by which a user can refer to the action in their configuration file. This |
Joshua M. Clulow | 8fb237d | 2011-12-30 11:54:52 +1100 | [diff] [blame] | 936 | allows them to re-bind the action to an alternate key-sequence. In order for |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 937 | this to work, it is necessary to call \fBgl_register_action()\fR between |
| 938 | calling \fBnew_GetLine()\fR and the first call to \fBgl_get_line()\fR. |
| 939 | .SS "History Files" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 940 | .LP |
| 941 | To save the contents of the history buffer before quitting your application and |
| 942 | subsequently restore them when you next start the application, the |
| 943 | \fBgl_save_history()\fR and \fBgl_load_history()\fR functions are provided. |
| 944 | .sp |
| 945 | .LP |
| 946 | The \fIfilename\fR argument specifies the name to give the history file when |
| 947 | saving, or the name of an existing history file, when loading. This may contain |
| 948 | home directory and environment variable expressions, such as |
| 949 | \fB~/.myapp_history\fR or \fB$HOME/.myapp_history\fR. |
| 950 | .sp |
| 951 | .LP |
| 952 | Along with each history line, additional information about it, such as its |
| 953 | nesting level and when it was entered by the user, is recorded as a comment |
| 954 | preceding the line in the history file. Writing this as a comment allows the |
| 955 | history file to double as a command file, just in case you wish to replay a |
| 956 | whole session using it. Since comment prefixes differ in different languages, |
| 957 | the comment argument is provided for specifying the comment prefix. For |
Peter Tribble | 0327063 | 2018-12-10 21:26:43 +0000 | [diff] [blame^] | 958 | example, if your application were a UNIX shell, such as the Bourne shell, you |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 959 | would specify "#" here. Whatever you choose for the comment character, you must |
| 960 | specify the same prefix to \fBgl_load_history()\fR that you used when you |
| 961 | called \fBgl_save_history()\fR to write the history file. |
| 962 | .sp |
| 963 | .LP |
| 964 | The \fImax_lines\fR argument must be either -1 to specify that all lines in the |
| 965 | history list be saved, or a positive number specifying a ceiling on how many of |
| 966 | the most recent lines should be saved. |
| 967 | .sp |
| 968 | .LP |
| 969 | Both fuctions return non-zero on error, after writing an error message to |
| 970 | \fBstderr\fR. Note that \fBgl_load_history()\fR does not consider the |
| 971 | non-existence of a file to be an error. |
| 972 | .SS "Multiple History Lists" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 973 | .LP |
| 974 | If your application uses a single \fBGetLine\fR object for entering many |
| 975 | different types of input lines, you might want \fBgl_get_line()\fR to |
| 976 | distinguish the different types of lines in the history list, and only recall |
| 977 | lines that match the current type of line. To support this requirement, |
| 978 | \fBgl_get_line()\fR marks lines being recorded in the history list with an |
| 979 | integer identifier chosen by the application. Initially this identifier is set |
| 980 | to 0 by \fBnew_GetLine()\fR, but it can be changed subsequently by calling |
| 981 | \fBgl_group_history()\fR. |
| 982 | .sp |
| 983 | .LP |
| 984 | The integer identifier ID can be any number chosen by the application, but note |
| 985 | that \fBgl_save_history()\fR and \fBgl_load_history()\fR preserve the |
| 986 | association between identifiers and historical input lines between program |
| 987 | invocations, so you should choose fixed identifiers for the different types of |
| 988 | input line used by your application. |
| 989 | .sp |
| 990 | .LP |
| 991 | Whenever \fBgl_get_line()\fR appends a new input line to the history list, the |
| 992 | current history identifier is recorded with it, and when it is asked to recall |
| 993 | a historical input line, it only recalls lines that are marked with the current |
| 994 | identifier. |
| 995 | .SS "Displaying History" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 996 | .LP |
| 997 | The history list can be displayed by calling \fBgl_show_history()\fR. This |
| 998 | function displays the current contents of the history list to the \fBstdio\fR |
| 999 | output stream \fIfp\fR. If the \fImax_lines\fR argument is greater than or |
| 1000 | equal to zero, then no more than this number of the most recent lines will be |
| 1001 | displayed. If the \fIall_groups\fR argument is non-zero, lines from all history |
| 1002 | groups are displayed. Otherwise only those of the currently selected history |
| 1003 | group are displayed. The format string argument, \fIfmt\fR, determines how the |
| 1004 | line is displayed. This can contain arbitrary characters which are written |
| 1005 | verbatim, interleaved with any of the following format directives: |
| 1006 | .sp |
| 1007 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1008 | .na |
| 1009 | \fB\fB%D\fR\fR |
| 1010 | .ad |
| 1011 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1012 | The date on which the line was originally entered, formatted like 2001-11-20. |
| 1013 | .RE |
| 1014 | |
| 1015 | .sp |
| 1016 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1017 | .na |
| 1018 | \fB\fB%T\fR\fR |
| 1019 | .ad |
| 1020 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1021 | The time of day when the line was entered, formatted like 23:59:59. |
| 1022 | .RE |
| 1023 | |
| 1024 | .sp |
| 1025 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1026 | .na |
| 1027 | \fB\fB%N\fR\fR |
| 1028 | .ad |
| 1029 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1030 | The sequential entry number of the line in the history buffer. |
| 1031 | .RE |
| 1032 | |
| 1033 | .sp |
| 1034 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1035 | .na |
| 1036 | \fB\fB%G\fR\fR |
| 1037 | .ad |
| 1038 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1039 | The number of the history group which the line belongs to. |
| 1040 | .RE |
| 1041 | |
| 1042 | .sp |
| 1043 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1044 | .na |
| 1045 | \fB\fB%%\fR\fR |
| 1046 | .ad |
| 1047 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1048 | A literal % character. |
| 1049 | .RE |
| 1050 | |
| 1051 | .sp |
| 1052 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1053 | .na |
| 1054 | \fB\fB%H\fR\fR |
| 1055 | .ad |
| 1056 | .RS 6n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1057 | The history line itself. |
| 1058 | .RE |
| 1059 | |
| 1060 | .sp |
| 1061 | .LP |
| 1062 | Thus a format string like "%D %T %H0" would output something like: |
| 1063 | .sp |
| 1064 | .in +2 |
| 1065 | .nf |
| 1066 | 2001-11-20 10:23:34 Hello world |
| 1067 | .fi |
| 1068 | .in -2 |
| 1069 | |
| 1070 | .sp |
| 1071 | .LP |
| 1072 | Note the inclusion of an explicit newline character in the format string. |
| 1073 | .SS "Looking Up History" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1074 | .LP |
| 1075 | The \fBgl_lookup_history()\fR function allows the calling application to look |
| 1076 | up lines in the history list. |
| 1077 | .sp |
| 1078 | .LP |
| 1079 | The \fIid\fR argument indicates which line to look up, where the first line |
| 1080 | that was entered in the history list after \fBnew_GetLine()\fR was called is |
| 1081 | denoted by 0, and subsequently entered lines are denoted with successively |
| 1082 | higher numbers. Note that the range of lines currently preserved in the history |
| 1083 | list can be queried by calling the \fBgl_range_of_history()\fR function. If the |
| 1084 | requested line is in the history list, the details of the line are recorded in |
| 1085 | the variable pointed to by the \fIhline\fR argument, and 1 is returned. |
| 1086 | Otherwise 0 is returned, and the variable pointed to by \fIhline\fR is left |
| 1087 | unchanged. |
| 1088 | .sp |
| 1089 | .LP |
| 1090 | Beware that the string returned in \fIhline\fR->\fIline\fR is part of the |
| 1091 | history buffer, so it must not be modified by the caller, and will be recycled |
| 1092 | on the next call to any function that takes \fIgl\fR as its argument. Therefore |
| 1093 | you should make a private copy of this string if you need to keep it. |
| 1094 | .SS "Manual History Archival" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1095 | .LP |
| 1096 | By default, whenever a line is entered by the user, it is automatically |
| 1097 | appended to the history list, just before \fBgl_get_line()\fR returns the line |
| 1098 | to the caller. This is convenient for the majority of applications, but there |
| 1099 | are also applications that need finer-grained control over what gets added to |
| 1100 | the history list. In such cases, the automatic addition of entered lines to the |
| 1101 | history list can be turned off by calling the \fBgl_automatic_history()\fR |
| 1102 | function. |
| 1103 | .sp |
| 1104 | .LP |
| 1105 | If this function is called with its \fIenable\fR argument set to 0, |
| 1106 | \fBgl_get_line()\fR will not automatically archive subsequently entered lines. |
| 1107 | Automatic archiving can be reenabled at a later time by calling this function |
| 1108 | again, with its \fIenable\fR argument set to 1. While automatic history |
| 1109 | archiving is disabled, the calling application can use the |
| 1110 | \fBgl_append_history()\fR to append lines to the history list as needed. |
| 1111 | .sp |
| 1112 | .LP |
| 1113 | The \fIline\fR argument specifies the line to be added to the history list. |
| 1114 | This must be a normal '\e0 ' terminated string. If this string contains any |
| 1115 | newline characters, the line that gets archived in the history list will be |
| 1116 | terminated by the first of these. Otherwise it will be terminated by the '\e0 ' |
| 1117 | terminator. If the line is longer than the maximum input line length that was |
| 1118 | specified when \fBnew_GetLine()\fR was called, it will be truncated to the |
| 1119 | actual \fBgl_get_line()\fR line length when the line is recalled. |
| 1120 | .sp |
| 1121 | .LP |
| 1122 | If successful, \fBgl_append_history()\fR returns 0. Otherwise it returns |
| 1123 | non-zero and sets \fBerrno\fR to one of the following values. |
| 1124 | .sp |
| 1125 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1126 | .na |
| 1127 | \fB\fBEINVAL\fR\fR |
| 1128 | .ad |
| 1129 | .RS 10n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1130 | One of the arguments passed to \fBgl_append_history()\fR was \fINULL\fR. |
| 1131 | .RE |
| 1132 | |
| 1133 | .sp |
| 1134 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1135 | .na |
| 1136 | \fB\fBENOMEM\fR\fR |
| 1137 | .ad |
| 1138 | .RS 10n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1139 | The specified line was longer than the allocated size of the history buffer (as |
| 1140 | specified when \fBnew_GetLine()\fR was called), so it could not be archived. |
| 1141 | .RE |
| 1142 | |
| 1143 | .sp |
| 1144 | .LP |
| 1145 | A textual description of the error can optionally be obtained by calling |
| 1146 | \fBgl_error_message()\fR. Note that after such an error, the history list |
| 1147 | remains in a valid state to receive new history lines, so there is little harm |
| 1148 | in simply ignoring the return status of \fBgl_append_history()\fR. |
| 1149 | .SS "Miscellaneous History Configuration" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1150 | .LP |
| 1151 | If you wish to change the size of the history buffer that was originally |
| 1152 | specified in the call to \fBnew_GetLine()\fR, you can do so with the |
| 1153 | \fBgl_resize_history()\fR function. |
| 1154 | .sp |
| 1155 | .LP |
| 1156 | The \fIhistlen\fR argument specifies the new size in bytes, and if you specify |
| 1157 | this as 0, the buffer will be deleted. |
| 1158 | .sp |
| 1159 | .LP |
| 1160 | As mentioned in the discussion of \fBnew_GetLine()\fR, the number of lines that |
| 1161 | can be stored in the history buffer, depends on the lengths of the individual |
| 1162 | lines. For example, a 1000 byte buffer could equally store 10 lines of average |
| 1163 | length 100 bytes, or 20 lines of average length 50 bytes. Although the buffer |
| 1164 | is never expanded when new lines are added, a list of pointers into the buffer |
Cody Peter Mello | 843c398 | 2016-04-09 23:18:13 +0000 | [diff] [blame] | 1165 | does get expanded when needed to accommodate the number of lines currently |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1166 | stored in the buffer. To place an upper limit on the number of lines in the |
| 1167 | buffer, and thus a ceiling on the amount of memory used in this list, you can |
| 1168 | call the \fBgl_limit_history()\fR function. |
| 1169 | .sp |
| 1170 | .LP |
| 1171 | The \fImax_lines\fR should either be a positive number >= 0, specifying an |
| 1172 | upper limit on the number of lines in the buffer, or be -1 to cancel any |
| 1173 | previously specified limit. When a limit is in effect, only the \fImax_lines\fR |
| 1174 | most recently appended lines are kept in the buffer. Older lines are discarded. |
| 1175 | .sp |
| 1176 | .LP |
| 1177 | To discard lines from the history buffer, use the \fBgl_clear_history()\fR |
| 1178 | function. |
| 1179 | .sp |
| 1180 | .LP |
| 1181 | The \fIall_groups\fR argument tells the function whether to delete just the |
| 1182 | lines associated with the current history group (see \fBgl_group_history()\fR) |
| 1183 | or all historical lines in the buffer. |
| 1184 | .sp |
| 1185 | .LP |
| 1186 | The \fBgl_toggle_history()\fR function allows you to toggle history on and off |
| 1187 | without losing the current contents of the history list. |
| 1188 | .sp |
| 1189 | .LP |
| 1190 | Setting the \fIenable\fR argument to 0 turns off the history mechanism, and |
| 1191 | setting it to 1 turns it back on. When history is turned off, no new lines will |
| 1192 | be added to the history list, and history lookup key-bindings will act as |
| 1193 | though there is nothing in the history buffer. |
| 1194 | .SS "Querying History Information" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1195 | .LP |
| 1196 | The configured state of the history list can be queried with the |
| 1197 | \fBgl_history_state()\fR function. On return, the status information is |
| 1198 | recorded in the variable pointed to by the \fIstate\fR argument. |
| 1199 | .sp |
| 1200 | .LP |
| 1201 | The \fBgl_range_of_history()\fR function returns the number and range of lines |
| 1202 | in the history list. The return values are recorded in the variable pointed to |
| 1203 | by the range argument. If the \fInlines\fR member of this structure is greater |
| 1204 | than zero, then the oldest and newest members report the range of lines in the |
| 1205 | list, and \fInewest\fR=\fIoldest\fR+\fInlines\fR-1. Otherwise they are both |
| 1206 | zero. |
| 1207 | .sp |
| 1208 | .LP |
| 1209 | The \fBgl_size_of_history()\fR function returns the total size of the history |
| 1210 | buffer and the amount of the buffer that is currently occupied. |
| 1211 | .sp |
| 1212 | .LP |
| 1213 | On return, the size information is recorded in the variable pointed to by the |
| 1214 | \fIsize\fR argument. |
| 1215 | .SS "Changing Terminals" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1216 | .LP |
| 1217 | The \fBnew_GetLine()\fR constructor function assumes that input is to be read |
| 1218 | from \fBstdin\fR and output written to \fBstdout\fR. The following function |
| 1219 | allows you to switch to different input and output streams. |
| 1220 | .sp |
| 1221 | .LP |
| 1222 | The \fIgl\fR argument is the object that was returned by \fBnew_GetLine()\fR. |
| 1223 | The \fIinput_fp\fR argument specifies the stream to read from, and |
| 1224 | \fIoutput_fp\fR specifies the stream to be written to. Only if both of these |
| 1225 | refer to a terminal, will interactive terminal input be enabled. Otherwise |
| 1226 | \fBgl_get_line()\fR will simply call \fBfgets()\fR to read command input. If |
| 1227 | both streams refer to a terminal, then they must refer to the same terminal, |
| 1228 | and the type of this terminal must be specified with the \fIterm\fR argument. |
| 1229 | The value of the \fIterm\fR argument is looked up in the terminal information |
| 1230 | database (\fBterminfo\fR or \fBtermcap\fR), in order to determine which special |
| 1231 | control sequences are needed to control various aspects of the terminal. |
| 1232 | \fBnew_GetLine()\fR for example, passes the return value of |
| 1233 | \fBgetenv\fR("TERM") in this argument. Note that if one or both of |
| 1234 | \fIinput_fp\fR and \fIoutput_fp\fR do not refer to a terminal, then it is legal |
| 1235 | to pass \fINULL\fR instead of a terminal type. |
| 1236 | .sp |
| 1237 | .LP |
| 1238 | Note that if you want to pass file descriptors to \fBgl_change_terminal()\fR, |
| 1239 | you can do this by creating \fBstdio\fR stream wrappers using the POSIX |
| 1240 | \fBfdopen\fR(3C) function. |
| 1241 | .SS "External Event Handling" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1242 | .LP |
| 1243 | By default, \fBgl_get_line()\fR does not return until either a complete input |
| 1244 | line has been entered by the user, or an error occurs. In programs that need to |
| 1245 | watch for I/O from other sources than the terminal, there are two options. |
| 1246 | .RS +4 |
| 1247 | .TP |
| 1248 | .ie t \(bu |
| 1249 | .el o |
| 1250 | Use the functions described in the \fBgl_io_mode\fR(3TECLA) manual page to |
| 1251 | switch \fBgl_get_line()\fR into non-blocking server mode. In this mode, |
| 1252 | \fBgl_get_line()\fR becomes a non-blocking, incremental line-editing function |
| 1253 | that can safely be called from an external event loop. Although this is a very |
| 1254 | versatile method, it involves taking on some responsibilities that are normally |
| 1255 | performed behind the scenes by \fBgl_get_line()\fR. |
| 1256 | .RE |
| 1257 | .RS +4 |
| 1258 | .TP |
| 1259 | .ie t \(bu |
| 1260 | .el o |
| 1261 | While \fBgl_get_line()\fR is waiting for keyboard input from the user, you can |
| 1262 | ask it to also watch for activity on arbitrary file descriptors, such as |
| 1263 | network sockets or pipes, and have it call functions of your choosing when |
| 1264 | activity is seen. This works on any system that has the select system call, |
| 1265 | which is most, if not all flavors of UNIX. |
| 1266 | .RE |
| 1267 | .sp |
| 1268 | .LP |
| 1269 | Registering a file descriptor to be watched by \fBgl_get_line()\fR involves |
| 1270 | calling the \fBgl_watch_fd()\fR function. If this returns non-zero, then it |
| 1271 | means that either your arguments are invalid, or that this facility is not |
| 1272 | supported on the host system. |
| 1273 | .sp |
| 1274 | .LP |
| 1275 | The \fIfd\fR argument is the file descriptor to be watched. The event argument |
| 1276 | specifies what type of activity is of interest, chosen from the following |
| 1277 | enumerated values: |
| 1278 | .sp |
| 1279 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1280 | .na |
| 1281 | \fB\fBGLFD_READ\fR\fR |
| 1282 | .ad |
| 1283 | .RS 15n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1284 | Watch for the arrival of data to be read. |
| 1285 | .RE |
| 1286 | |
| 1287 | .sp |
| 1288 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1289 | .na |
| 1290 | \fB\fBGLFD_WRITE\fR\fR |
| 1291 | .ad |
| 1292 | .RS 15n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1293 | Watch for the ability to write to the file descriptor without blocking. |
| 1294 | .RE |
| 1295 | |
| 1296 | .sp |
| 1297 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1298 | .na |
| 1299 | \fB\fBGLFD_URGENT\fR\fR |
| 1300 | .ad |
| 1301 | .RS 15n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1302 | Watch for the arrival of urgent out-of-band data on the file descriptor. |
| 1303 | .RE |
| 1304 | |
| 1305 | .sp |
| 1306 | .LP |
| 1307 | The \fIcallback\fR argument is the function to call when the selected activity |
| 1308 | is seen. It should be defined with the following macro, which is defined in |
| 1309 | libtecla.h. |
| 1310 | .sp |
| 1311 | .in +2 |
| 1312 | .nf |
| 1313 | #define GL_FD_EVENT_FN(fn) GlFdStatus (fn)(GetLine *gl, \ |
| 1314 | void *data, int fd, GlFdEvent event) |
| 1315 | .fi |
| 1316 | .in -2 |
| 1317 | |
| 1318 | .sp |
| 1319 | .LP |
| 1320 | The data argument of the \fBgl_watch_fd()\fR function is passed to the callback |
| 1321 | function for its own use, and can point to anything you like, including |
| 1322 | \fINULL\fR. The file descriptor and the event argument are also passed to the |
| 1323 | callback function, and this potentially allows the same callback function to be |
| 1324 | registered to more than one type of event and/or more than one file descriptor. |
| 1325 | The return value of the callback function should be one of the following |
| 1326 | values. |
| 1327 | .sp |
| 1328 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1329 | .na |
| 1330 | \fB\fBGLFD_ABORT\fR\fR |
| 1331 | .ad |
| 1332 | .RS 17n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1333 | Tell \fBgl_get_line()\fR to abort. When this happens, \fBgl_get_line()\fR |
| 1334 | returns \fINULL\fR, and a following call to \fBgl_return_status()\fR will |
| 1335 | return \fBGLR_FDABORT\fR. Note that if the application needs \fBerrno\fR always |
| 1336 | to have a meaningful value when \fBgl_get_line()\fR returns \fINULL\fR, the |
| 1337 | callback function should set \fBerrno\fR appropriately. |
| 1338 | .RE |
| 1339 | |
| 1340 | .sp |
| 1341 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1342 | .na |
| 1343 | \fB\fBGLFD_REFRESH\fR\fR |
| 1344 | .ad |
| 1345 | .RS 17n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1346 | Redraw the input line then continue waiting for input. Return this if your |
| 1347 | callback wrote to the terminal. |
| 1348 | .RE |
| 1349 | |
| 1350 | .sp |
| 1351 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1352 | .na |
| 1353 | \fB\fBGLFD_CONTINUE\fR\fR |
| 1354 | .ad |
| 1355 | .RS 17n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1356 | Continue to wait for input, without redrawing the line. |
| 1357 | .RE |
| 1358 | |
| 1359 | .sp |
| 1360 | .LP |
| 1361 | Note that before calling the callback, \fBgl_get_line()\fR blocks most signals |
| 1362 | and leaves its own signal handlers installed, so if you need to catch a |
| 1363 | particular signal you will need to both temporarily install your own signal |
| 1364 | handler, and unblock the signal. Be sure to re-block the signal (if it was |
| 1365 | originally blocked) and reinstate the original signal handler, if any, before |
| 1366 | returning. |
| 1367 | .sp |
| 1368 | .LP |
| 1369 | Your callback should not try to read from the terminal, which is left in raw |
| 1370 | mode as far as input is concerned. You can write to the terminal as usual, |
| 1371 | since features like conversion of newline to carriage-return/linefeed are |
| 1372 | re-enabled while the callback is running. If your callback function does write |
| 1373 | to the terminal, be sure to output a newline first, and when your callback |
| 1374 | returns, tell \fBgl_get_line()\fR that the input line needs to be redrawn, by |
| 1375 | returning the \fBGLFD_REFRESH\fR status code. |
| 1376 | .sp |
| 1377 | .LP |
| 1378 | To remove a callback function that you previously registered for a given file |
| 1379 | descriptor and event, simply call \fBgl_watch_fd()\fR with the same \fIfd\fR |
| 1380 | and \fIevent\fR arguments, but with a \fIcallback\fR argument of 0. The |
| 1381 | \fIdata\fR argument is ignored in this case. |
| 1382 | .SS "Setting An Inactivity Timeout" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1383 | .LP |
| 1384 | The \fBgl_inactivity_timeout()\fR function can be used to set or cancel an |
| 1385 | inactivity timeout. Inactivity in this case refers both to keyboard input, and |
| 1386 | to I/O on any file descriptors registered by prior and subsequent calls to |
| 1387 | \fBgl_watch_fd()\fR. |
| 1388 | .sp |
| 1389 | .LP |
| 1390 | The timeout is specified in the form of an integral number of seconds and an |
| 1391 | integral number of nanoseconds, specified by the \fIsec\fR and \fInsec\fR |
| 1392 | arguments, respectively. Subsequently, whenever no activity is seen for this |
| 1393 | time period, the function specified by the \fIcallback\fR argument is called. |
| 1394 | The \fIdata\fR argument of \fBgl_inactivity_timeout()\fR is passed to this |
| 1395 | callback function whenever it is invoked, and can thus be used to pass |
| 1396 | arbitrary application-specific information to the callback. The following macro |
| 1397 | is provided in <\fBlibtecla.h\fR> for applications to use to declare and |
| 1398 | prototype timeout callback functions. |
| 1399 | .sp |
| 1400 | .in +2 |
| 1401 | .nf |
| 1402 | #define GL_TIMEOUT_FN(fn) GlAfterTimeout (fn)(GetLine *gl, void *data) |
| 1403 | .fi |
| 1404 | .in -2 |
| 1405 | |
| 1406 | .sp |
| 1407 | .LP |
| 1408 | On returning, the application's callback is expected to return one of the |
Cody Peter Mello | 843c398 | 2016-04-09 23:18:13 +0000 | [diff] [blame] | 1409 | following enumerators to tell \fBgl_get_line()\fR how to proceed after the |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1410 | timeout has been handled by the callback. |
| 1411 | .sp |
| 1412 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1413 | .na |
| 1414 | \fB\fBGLTO_ABORT\fR\fR |
| 1415 | .ad |
| 1416 | .RS 17n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1417 | Tell \fBgl_get_line()\fR to abort. When this happens, \fBgl_get_line()\fR will |
| 1418 | return \fINULL\fR, and a following call to \fBgl_return_status()\fR will return |
| 1419 | \fBGLR_TIMEOUT\fR. Note that if the application needs \fBerrno\fR always to |
| 1420 | have a meaningful value when \fBgl_get_line()\fR returns \fINULL\fR, the |
| 1421 | callback function should set \fBerrno\fR appropriately. |
| 1422 | .RE |
| 1423 | |
| 1424 | .sp |
| 1425 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1426 | .na |
| 1427 | \fB\fBGLTO_REFRESH\fR\fR |
| 1428 | .ad |
| 1429 | .RS 17n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1430 | Redraw the input line, then continue waiting for input. You should return this |
| 1431 | value if your callback wrote to the terminal. |
| 1432 | .RE |
| 1433 | |
| 1434 | .sp |
| 1435 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1436 | .na |
| 1437 | \fB\fBGLTO_CONTINUE\fR\fR |
| 1438 | .ad |
| 1439 | .RS 17n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1440 | In normal blocking-I/O mode, continue to wait for input, without redrawing the |
| 1441 | user's input line. In non-blocking server I/O mode (see |
| 1442 | \fBgl_io_mode\fR(3TECLA)), \fBgl_get_line()\fR acts as though I/O blocked. This |
| 1443 | means that \fBgl_get_line()\fR will immediately return \fINULL\fR, and a |
| 1444 | following call to \fBgl_return_status()\fR will return \fBGLR_BLOCKED\fR. |
| 1445 | .RE |
| 1446 | |
| 1447 | .sp |
| 1448 | .LP |
| 1449 | Note that before calling the callback, \fBgl_get_line()\fR blocks most signals |
| 1450 | and leaves its own signal handlers installed, so if you need to catch a |
| 1451 | particular signal you will need to both temporarily install your own signal |
| 1452 | handler and unblock the signal. Be sure to re-block the signal (if it was |
| 1453 | originally blocked) and reinstate the original signal handler, if any, before |
| 1454 | returning. |
| 1455 | .sp |
| 1456 | .LP |
| 1457 | Your callback should not try to read from the terminal, which is left in raw |
| 1458 | mode as far as input is concerned. You can however write to the terminal as |
| 1459 | usual, since features like conversion of newline to carriage-return/linefeed |
| 1460 | are re-enabled while the callback is running. If your callback function does |
| 1461 | write to the terminal, be sure to output a newline first, and when your |
| 1462 | callback returns, tell \fBgl_get_line()\fR that the input line needs to be |
| 1463 | redrawn, by returning the \fBGLTO_REFRESH\fR status code. |
| 1464 | .sp |
| 1465 | .LP |
| 1466 | Finally, note that although the timeout arguments include a nanosecond |
| 1467 | component, few computer clocks presently have resolutions that are finer than a |
| 1468 | few milliseconds, so asking for less than a few milliseconds is equivalent to |
| 1469 | requesting zero seconds on many systems. If this would be a problem, you should |
| 1470 | base your timeout selection on the actual resolution of the host clock (for |
| 1471 | example, by calling \fBsysconf\fR(\fB_SC_CLK_TCK\fR)). |
| 1472 | .sp |
| 1473 | .LP |
| 1474 | To turn off timeouts, simply call \fBgl_inactivity_timeout()\fR with a |
| 1475 | \fIcallback\fR argument of 0. The \fIdata\fR argument is ignored in this case. |
| 1476 | .SS "Signal Handling Defaults" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1477 | .LP |
| 1478 | By default, the \fBgl_get_line()\fR function intercepts a number of signals. |
| 1479 | This is particularly important for signals that would by default terminate the |
| 1480 | process, since the terminal needs to be restored to a usable state before this |
| 1481 | happens. This section describes the signals that are trapped by default and how |
| 1482 | \fBgl_get_line()\fR responds to them. Changing these defaults is the topic of |
| 1483 | the following section. |
| 1484 | .sp |
| 1485 | .LP |
| 1486 | When the following subset of signals are caught, \fBgl_get_line()\fR first |
| 1487 | restores the terminal settings and signal handling to how they were before |
| 1488 | \fBgl_get_line()\fR was called, resends the signal to allow the calling |
| 1489 | application's signal handlers to handle it, then, if the process still exists, |
| 1490 | returns \fINULL\fR and sets \fBerrno\fR as specified below. |
| 1491 | .sp |
| 1492 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1493 | .na |
| 1494 | \fB\fBSIGINT\fR\fR |
| 1495 | .ad |
| 1496 | .RS 11n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1497 | This signal is generated both by the keyboard interrupt key (usually \fB^C\fR), |
| 1498 | and the keyboard break key. The \fBerrno\fR value is \fBEINTR\fR. |
| 1499 | .RE |
| 1500 | |
| 1501 | .sp |
| 1502 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1503 | .na |
| 1504 | \fB\fBSIGHUP\fR\fR |
| 1505 | .ad |
| 1506 | .RS 11n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1507 | This signal is generated when the controlling terminal exits. The \fBerrno\fR |
| 1508 | value is \fBENOTTY\fR. |
| 1509 | .RE |
| 1510 | |
| 1511 | .sp |
| 1512 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1513 | .na |
| 1514 | \fB\fBSIGPIPE\fR\fR |
| 1515 | .ad |
| 1516 | .RS 11n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1517 | This signal is generated when a program attempts to write to a pipe whose |
| 1518 | remote end is not being read by any process. This can happen for example if you |
| 1519 | have called \fBgl_change_terminal()\fR to redirect output to a pipe hidden |
| 1520 | under a pseudo terminal. The \fBerrno\fR value is \fBEPIPE\fR. |
| 1521 | .RE |
| 1522 | |
| 1523 | .sp |
| 1524 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1525 | .na |
| 1526 | \fB\fBSIGQUIT\fR\fR |
| 1527 | .ad |
| 1528 | .RS 11n |
Chris Fraire | d042c5a | 2017-08-25 09:47:09 -0500 | [diff] [blame] | 1529 | This signal is generated by the keyboard quit key (usually \fB^\e\fR). The |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1530 | \fBerrno\fR value is \fBEINTR\fR. |
| 1531 | .RE |
| 1532 | |
| 1533 | .sp |
| 1534 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1535 | .na |
| 1536 | \fB\fBSIGABRT\fR\fR |
| 1537 | .ad |
| 1538 | .RS 11n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1539 | This signal is generated by the standard C, abort function. By default it both |
| 1540 | terminates the process and generates a core dump. The \fBerrno\fR value is |
| 1541 | \fBEINTR\fR. |
| 1542 | .RE |
| 1543 | |
| 1544 | .sp |
| 1545 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1546 | .na |
| 1547 | \fB\fBSIGTERM\fR\fR |
| 1548 | .ad |
| 1549 | .RS 11n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1550 | This is the default signal that the UNIX kill command sends to processes. The |
| 1551 | \fBerrno\fR value is \fBEINTR\fR. |
| 1552 | .RE |
| 1553 | |
| 1554 | .sp |
| 1555 | .LP |
| 1556 | Note that in the case of all of the above signals, POSIX mandates that by |
| 1557 | default the process is terminated, with the addition of a core dump in the case |
| 1558 | of the \fBSIGQUIT\fR signal. In other words, if the calling application does |
| 1559 | not override the default handler by supplying its own signal handler, receipt |
| 1560 | of the corresponding signal will terminate the application before |
| 1561 | \fBgl_get_line()\fR returns. |
| 1562 | .sp |
| 1563 | .LP |
| 1564 | If \fBgl_get_line()\fR aborts with \fBerrno\fR set to \fBEINTR\fR, you can find |
| 1565 | out what signal caused it to abort, by calling the \fBgl_last_signal()\fR |
| 1566 | function. This returns the numeric code (for example, \fBSIGINT\fR) of the last |
| 1567 | signal that was received during the most recent call to \fBgl_get_line()\fR, or |
| 1568 | -1 if no signals were received. |
| 1569 | .sp |
| 1570 | .LP |
| 1571 | On systems that support it, when a \fBSIGWINCH\fR (window change) signal is |
| 1572 | received, \fBgl_get_line()\fR queries the terminal to find out its new size, |
Cody Peter Mello | 843c398 | 2016-04-09 23:18:13 +0000 | [diff] [blame] | 1573 | redraws the current input line to accommodate the new size, then returns to |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1574 | waiting for keyboard input from the user. Unlike other signals, this signal is |
| 1575 | not resent to the application. |
| 1576 | .sp |
| 1577 | .LP |
| 1578 | Finally, the following signals cause \fBgl_get_line()\fR to first restore the |
| 1579 | terminal and signal environment to that which prevailed before |
| 1580 | \fBgl_get_line()\fR was called, then resend the signal to the application. If |
| 1581 | the process still exists after the signal has been delivered, then |
| 1582 | \fBgl_get_line()\fR then re-establishes its own signal handlers, switches the |
| 1583 | terminal back to raw mode, redisplays the input line, and goes back to awaiting |
| 1584 | terminal input from the user. |
| 1585 | .sp |
| 1586 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1587 | .na |
| 1588 | \fB\fBSIGCONT\fR\fR |
| 1589 | .ad |
| 1590 | .RS 13n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1591 | This signal is generated when a suspended process is resumed. |
| 1592 | .RE |
| 1593 | |
| 1594 | .sp |
| 1595 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1596 | .na |
| 1597 | \fB\fBSIGPOLL\fR\fR |
| 1598 | .ad |
| 1599 | .RS 13n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1600 | On SVR4 systems, this signal notifies the process of an asynchronous I/O event. |
| 1601 | Note that under 4.3+BSD, \fBSIGIO\fR and \fBSIGPOLL\fR are the same. On other |
| 1602 | systems, \fBSIGIO\fR is ignored by default, so \fBgl_get_line()\fR does not |
| 1603 | trap it by default. |
| 1604 | .RE |
| 1605 | |
| 1606 | .sp |
| 1607 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1608 | .na |
| 1609 | \fB\fBSIGPWR\fR\fR |
| 1610 | .ad |
| 1611 | .RS 13n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1612 | This signal is generated when a power failure occurs (presumably when the |
| 1613 | system is on a UPS). |
| 1614 | .RE |
| 1615 | |
| 1616 | .sp |
| 1617 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1618 | .na |
| 1619 | \fB\fBSIGALRM\fR\fR |
| 1620 | .ad |
| 1621 | .RS 13n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1622 | This signal is generated when a timer expires. |
| 1623 | .RE |
| 1624 | |
| 1625 | .sp |
| 1626 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1627 | .na |
| 1628 | \fB\fBSIGUSR1\fR\fR |
| 1629 | .ad |
| 1630 | .RS 13n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1631 | An application specific signal. |
| 1632 | .RE |
| 1633 | |
| 1634 | .sp |
| 1635 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1636 | .na |
| 1637 | \fB\fBSIGUSR2\fR\fR |
| 1638 | .ad |
| 1639 | .RS 13n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1640 | Another application specific signal. |
| 1641 | .RE |
| 1642 | |
| 1643 | .sp |
| 1644 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1645 | .na |
| 1646 | \fB\fBSIGVTALRM\fR\fR |
| 1647 | .ad |
| 1648 | .RS 13n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1649 | This signal is generated when a virtual timer expires. See \fBsetitimer\fR(2). |
| 1650 | .RE |
| 1651 | |
| 1652 | .sp |
| 1653 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1654 | .na |
| 1655 | \fB\fBSIGXCPU\fR\fR |
| 1656 | .ad |
| 1657 | .RS 13n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1658 | This signal is generated when a process exceeds its soft CPU time limit. |
| 1659 | .RE |
| 1660 | |
| 1661 | .sp |
| 1662 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1663 | .na |
| 1664 | \fB\fBSIGXFSZ\fR\fR |
| 1665 | .ad |
| 1666 | .RS 13n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1667 | This signal is generated when a process exceeds its soft file-size limit. |
| 1668 | .RE |
| 1669 | |
| 1670 | .sp |
| 1671 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1672 | .na |
| 1673 | \fB\fBSIGTSTP\fR\fR |
| 1674 | .ad |
| 1675 | .RS 13n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1676 | This signal is generated by the terminal suspend key, which is usually |
| 1677 | \fB^Z\fR, or the delayed terminal suspend key, which is usually \fB^Y\fR. |
| 1678 | .RE |
| 1679 | |
| 1680 | .sp |
| 1681 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1682 | .na |
| 1683 | \fB\fBSIGTTIN\fR\fR |
| 1684 | .ad |
| 1685 | .RS 13n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1686 | This signal is generated if the program attempts to read from the terminal |
| 1687 | while the program is running in the background. |
| 1688 | .RE |
| 1689 | |
| 1690 | .sp |
| 1691 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1692 | .na |
| 1693 | \fB\fBSIGTTOU\fR\fR |
| 1694 | .ad |
| 1695 | .RS 13n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1696 | This signal is generated if the program attempts to write to the terminal while |
| 1697 | the program is running in the background. |
| 1698 | .RE |
| 1699 | |
| 1700 | .sp |
| 1701 | .LP |
| 1702 | Obviously not all of the above signals are supported on all systems, so code to |
| 1703 | support them is conditionally compiled into the tecla library. |
| 1704 | .sp |
| 1705 | .LP |
| 1706 | Note that if \fBSIGKILL\fR or \fBSIGPOLL\fR, which by definition cannot be |
| 1707 | caught, or any of the hardware generated exception signals, such as |
| 1708 | \fBSIGSEGV\fR, \fBSIGBUS\fR, and \fBSIGFPE\fR, are received and unhandled while |
| 1709 | \fBgl_get_line()\fR has the terminal in raw mode, the program will be |
| 1710 | terminated without the terminal having been restored to a usable state. In |
| 1711 | practice, job-control shells usually reset the terminal settings when a process |
| 1712 | relinquishes the controlling terminal, so this is only a problem with older |
| 1713 | shells. |
| 1714 | .SS "Customized Signal Handling" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1715 | .LP |
| 1716 | The previous section listed the signals that \fBgl_get_line()\fR traps by |
| 1717 | default, and described how it responds to them. This section describes how to |
| 1718 | both add and remove signals from the list of trapped signals, and how to |
| 1719 | specify how \fBgl_get_line()\fR should respond to a given signal. |
| 1720 | .sp |
| 1721 | .LP |
| 1722 | If you do not need \fBgl_get_line()\fR to do anything in response to a signal |
| 1723 | that it normally traps, you can tell to \fBgl_get_line()\fR to ignore that |
| 1724 | signal by calling \fBgl_ignore_signal()\fR. |
| 1725 | .sp |
| 1726 | .LP |
| 1727 | The \fIsigno\fR argument is the number of the signal (for example, |
| 1728 | \fBSIGINT\fR) that you want to have ignored. If the specified signal is not |
| 1729 | currently one of those being trapped, this function does nothing. |
| 1730 | .sp |
| 1731 | .LP |
| 1732 | The \fBgl_trap_signal()\fR function allows you to either add a new signal to |
| 1733 | the list that \fBgl_get_line()\fR traps or modify how it responds to a signal |
| 1734 | that it already traps. |
| 1735 | .sp |
| 1736 | .LP |
| 1737 | The \fIsigno\fR argument is the number of the signal that you want to have |
| 1738 | trapped. The \fIflags\fR argument is a set of flags that determine the |
| 1739 | environment in which the application's signal handler is invoked. The |
| 1740 | \fIafter\fR argument tells \fBgl_get_line()\fR what to do after the |
| 1741 | application's signal handler returns. The \fIerrno_value\fR tells |
| 1742 | \fBgl_get_line()\fR what to set \fBerrno\fR to if told to abort. |
| 1743 | .sp |
| 1744 | .LP |
| 1745 | The \fIflags\fR argument is a bitwise OR of zero or more of the following |
| 1746 | enumerators: |
| 1747 | .sp |
| 1748 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1749 | .na |
| 1750 | \fB\fBGLS_RESTORE_SIG\fR\fR |
| 1751 | .ad |
| 1752 | .RS 20n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1753 | Restore the caller's signal environment while handling the signal. |
| 1754 | .RE |
| 1755 | |
| 1756 | .sp |
| 1757 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1758 | .na |
| 1759 | \fB\fBGLS_RESTORE_TTY\fR\fR |
| 1760 | .ad |
| 1761 | .RS 20n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1762 | Restore the caller's terminal settings while handling the signal. |
| 1763 | .RE |
| 1764 | |
| 1765 | .sp |
| 1766 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1767 | .na |
| 1768 | \fB\fBGLS_RESTORE_LINE\fR\fR |
| 1769 | .ad |
| 1770 | .RS 20n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1771 | Move the cursor to the start of the line following the input line before |
| 1772 | invoking the application's signal handler. |
| 1773 | .RE |
| 1774 | |
| 1775 | .sp |
| 1776 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1777 | .na |
| 1778 | \fB\fBGLS_REDRAW_LINE\fR\fR |
| 1779 | .ad |
| 1780 | .RS 20n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1781 | Redraw the input line when the application's signal handler returns. |
| 1782 | .RE |
| 1783 | |
| 1784 | .sp |
| 1785 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1786 | .na |
| 1787 | \fB\fBGLS_UNBLOCK_SIG\fR\fR |
| 1788 | .ad |
| 1789 | .RS 20n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1790 | Normally, if the calling program has a signal blocked (see |
| 1791 | \fBsigprocmask\fR(2)), \fBgl_get_line()\fR does not trap that signal. This flag |
| 1792 | tells \fBgl_get_line()\fR to trap the signal and unblock it for the duration of |
| 1793 | the call to \fBgl_get_line()\fR. |
| 1794 | .RE |
| 1795 | |
| 1796 | .sp |
| 1797 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1798 | .na |
| 1799 | \fB\fBGLS_DONT_FORWARD\fR\fR |
| 1800 | .ad |
| 1801 | .RS 20n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1802 | If this flag is included, the signal will not be forwarded to the signal |
| 1803 | handler of the calling program. |
| 1804 | .RE |
| 1805 | |
| 1806 | .sp |
| 1807 | .LP |
| 1808 | Two commonly useful flag combinations are also enumerated as follows: |
| 1809 | .sp |
| 1810 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1811 | .na |
| 1812 | \fB\fBGLS_RESTORE_ENV\fR\fR |
| 1813 | .ad |
| 1814 | .RS 21n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1815 | \fBGLS_RESTORE_SIG\fR | \fBGLS_RESTORE_TTY\fR |\fBGLS_REDRAW_LINE\fR |
| 1816 | .RE |
| 1817 | |
| 1818 | .sp |
| 1819 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1820 | .na |
| 1821 | \fB\fBGLS_SUSPEND_INPUT\fR\fR |
| 1822 | .ad |
| 1823 | .RS 21n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1824 | \fBGLS_RESTORE_ENV\fR | \fBGLS_RESTORE_LINE\fR |
| 1825 | .RE |
| 1826 | |
| 1827 | .sp |
| 1828 | .LP |
| 1829 | If your signal handler, or the default system signal handler for this signal, |
| 1830 | if you have not overridden it, never either writes to the terminal, nor |
| 1831 | suspends or terminates the calling program, then you can safely set the |
| 1832 | \fIflags\fR argument to 0. |
| 1833 | .RS +4 |
| 1834 | .TP |
| 1835 | .ie t \(bu |
| 1836 | .el o |
| 1837 | The cursor does not get left in the middle of the input line. |
| 1838 | .RE |
| 1839 | .RS +4 |
| 1840 | .TP |
| 1841 | .ie t \(bu |
| 1842 | .el o |
| 1843 | So that the user can type in input and have it echoed. |
| 1844 | .RE |
| 1845 | .RS +4 |
| 1846 | .TP |
| 1847 | .ie t \(bu |
| 1848 | .el o |
| 1849 | So that you do not need to end each output line with \er\en, instead of just |
| 1850 | \en. |
| 1851 | .RE |
| 1852 | .sp |
| 1853 | .LP |
| 1854 | The \fBGL_RESTORE_ENV\fR combination is the same as \fBGL_SUSPEND_INPUT\fR, |
| 1855 | except that it does not move the cursor. If your signal handler does not read |
| 1856 | or write anything to the terminal, the user will not see any visible indication |
| 1857 | that a signal was caught. This can be useful if you have a signal handler that |
| 1858 | only occasionally writes to the terminal, where using \fBGL_SUSPEND_LINE\fR |
| 1859 | would cause the input line to be unnecessarily duplicated when nothing had been |
| 1860 | written to the terminal. Such a signal handler, when it does write to the |
| 1861 | terminal, should be sure to start a new line at the start of its first write, |
| 1862 | by writing a new line before returning. If the signal arrives while the user is |
| 1863 | entering a line that only occupies a signal terminal line, or if the cursor is |
| 1864 | on the last terminal line of a longer input line, this will have the same |
| 1865 | effect as \fBGL_SUSPEND_INPUT\fR. Otherwise it will start writing on a line |
| 1866 | that already contains part of the displayed input line. This does not do any |
| 1867 | harm, but it looks a bit ugly, which is why the \fBGL_SUSPEND_INPUT\fR |
| 1868 | combination is better if you know that you are always going to be writting to |
| 1869 | the terminal. |
| 1870 | .sp |
| 1871 | .LP |
| 1872 | The \fIafter\fR argument, which determines what \fBgl_get_line()\fR does after |
| 1873 | the application's signal handler returns (if it returns), can take any one of |
| 1874 | the following values: |
| 1875 | .sp |
| 1876 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1877 | .na |
| 1878 | \fB\fBGLS_RETURN\fR\fR |
| 1879 | .ad |
| 1880 | .RS 16n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1881 | Return the completed input line, just as though the user had pressed the return |
| 1882 | key. |
| 1883 | .RE |
| 1884 | |
| 1885 | .sp |
| 1886 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1887 | .na |
| 1888 | \fB\fBGLS_ABORT\fR\fR |
| 1889 | .ad |
| 1890 | .RS 16n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1891 | Cause \fBgl_get_line()\fR to abort. When this happens, \fBgl_get_line()\fR |
| 1892 | returns \fINULL\fR, and a following call to \fBgl_return_status()\fR will |
| 1893 | return \fBGLR_SIGNAL\fR. Note that if the application needs \fBerrno\fR always |
| 1894 | to have a meaningful value when \fBgl_get_line()\fR returns \fINULL\fR, the |
| 1895 | callback function should set \fBerrno\fR appropriately. |
| 1896 | .RE |
| 1897 | |
| 1898 | .sp |
| 1899 | .ne 2 |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1900 | .na |
| 1901 | \fB\fBGLS_CONTINUE\fR\fR |
| 1902 | .ad |
| 1903 | .RS 16n |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1904 | Resume command line editing. |
| 1905 | .RE |
| 1906 | |
| 1907 | .sp |
| 1908 | .LP |
| 1909 | The \fIerrno_value\fR argument is intended to be combined with the |
| 1910 | \fBGLS_ABORT\fR option, telling \fBgl_get_line()\fR what to set the standard |
| 1911 | \fBerrno\fR variable to before returning \fINULL\fR to the calling program. It |
| 1912 | can also, however, be used with the \fBGL_RETURN\fR option, in case you want to |
| 1913 | have a way to distinguish between an input line that was entered using the |
| 1914 | return key, and one that was entered by the receipt of a signal. |
| 1915 | .SS "Reliable Signal Handling" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1916 | .LP |
Cody Peter Mello | 843c398 | 2016-04-09 23:18:13 +0000 | [diff] [blame] | 1917 | Signal handling is surprisingly hard to do reliably without race conditions. In |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 1918 | \fBgl_get_line()\fR a lot of care has been taken to allow applications to |
| 1919 | perform reliable signal handling around \fBgl_get_line()\fR. This section |
| 1920 | explains how to make use of this. |
| 1921 | .sp |
| 1922 | .LP |
| 1923 | As an example of the problems that can arise if the application is not written |
| 1924 | correctly, imagine that one's application has a \fBSIGINT\fR signal handler |
| 1925 | that sets a global flag. Now suppose that the application tests this flag just |
| 1926 | before invoking \fBgl_get_line()\fR. If a \fBSIGINT\fR signal happens to be |
| 1927 | received in the small window of time between the statement that tests the value |
| 1928 | of this flag, and the statement that calls \fBgl_get_line()\fR, then |
| 1929 | \fBgl_get_line()\fR will not see the signal, and will not be interrupted. As a |
| 1930 | result, the application will not be able to respond to the signal until the |
| 1931 | user gets around to finishing entering the input line and \fBgl_get_line()\fR |
| 1932 | returns. Depending on the application, this might or might not be a disaster, |
| 1933 | but at the very least it would puzzle the user. |
| 1934 | .sp |
| 1935 | .LP |
| 1936 | The way to avoid such problems is to do the following. |
| 1937 | .RS +4 |
| 1938 | .TP |
| 1939 | 1. |
| 1940 | If needed, use the \fBgl_trap_signal()\fR function to configure |
| 1941 | \fBgl_get_line()\fR to abort when important signals are caught. |
| 1942 | .RE |
| 1943 | .RS +4 |
| 1944 | .TP |
| 1945 | 2. |
| 1946 | Configure \fBgl_get_line()\fR such that if any of the signals that it |
| 1947 | catches are blocked when \fBgl_get_line()\fR is called, they will be unblocked |
| 1948 | automatically during times when \fBgl_get_line()\fR is waiting for I/O. This |
| 1949 | can be done either on a per signal basis, by calling the \fBgl_trap_signal()\fR |
| 1950 | function, and specifying the \fBGLS_UNBLOCK\fR attribute of the signal, or |
| 1951 | globally by calling the \fBgl_catch_blocked()\fR function. This function simply |
| 1952 | adds the \fBGLS_UNBLOCK\fR attribute to all of the signals that it is currently |
| 1953 | configured to trap. |
| 1954 | .RE |
| 1955 | .RS +4 |
| 1956 | .TP |
| 1957 | 3. |
| 1958 | Just before calling \fBgl_get_line()\fR, block delivery of all of the |
| 1959 | signals that \fBgl_get_line()\fR is configured to trap. This can be done using |
| 1960 | the POSIX sigprocmask function in conjunction with the \fBgl_list_signals()\fR |
| 1961 | function. This function returns the set of signals that it is currently |
| 1962 | configured to catch in the set argument, which is in the form required by |
| 1963 | \fBsigprocmask\fR(2). |
| 1964 | .RE |
| 1965 | .RS +4 |
| 1966 | .TP |
| 1967 | 4. |
| 1968 | In the example, one would now test the global flag that the signal handler |
| 1969 | sets, knowing that there is now no danger of this flag being set again until |
| 1970 | \fBgl_get_line()\fR unblocks its signals while performing I/O. |
| 1971 | .RE |
| 1972 | .RS +4 |
| 1973 | .TP |
| 1974 | 5. |
| 1975 | Eventually \fBgl_get_line()\fR returns, either because a signal was caught, |
| 1976 | an error occurred, or the user finished entering their input line. |
| 1977 | .RE |
| 1978 | .RS +4 |
| 1979 | .TP |
| 1980 | 6. |
| 1981 | Now one would check the global signal flag again, and if it is set, respond |
| 1982 | to it, and zero the flag. |
| 1983 | .RE |
| 1984 | .RS +4 |
| 1985 | .TP |
| 1986 | 7. |
| 1987 | Use \fBsigprocmask()\fR to unblock the signals that were blocked in step 3. |
| 1988 | .RE |
| 1989 | .sp |
| 1990 | .LP |
| 1991 | The same technique can be used around certain POSIX signal-aware functions, |
| 1992 | such as \fBsigsetjmp\fR(3C) and \fBsigsuspend\fR(2), and in particular, the |
| 1993 | former of these two functions can be used in conjunction with |
| 1994 | \fBsiglongjmp\fR(3C) to implement race-condition free signal handling around |
| 1995 | other long-running system calls. The \fBgl_get_line()\fR function manages to |
| 1996 | reliably trap signals around calls to functions like \fBread\fR(2) and |
| 1997 | \fBselect\fR(3C) without race conditions. |
| 1998 | .sp |
| 1999 | .LP |
| 2000 | The \fBgl_get_line()\fR function first uses the POSIX \fBsigprocmask()\fR |
| 2001 | function to block the delivery of all of the signals that it is currently |
| 2002 | configured to catch. This is redundant if the application has already blocked |
| 2003 | them, but it does no harm. It undoes this step just before returning. |
| 2004 | .sp |
| 2005 | .LP |
| 2006 | Whenever \fBgl_get_line()\fR needs to call read or select to wait for input |
| 2007 | from the user, it first calls the POSIX \fBsigsetjmp()\fR function, being sure |
| 2008 | to specify a non-zero value for its \fIsavemask\fR argument. |
| 2009 | .sp |
| 2010 | .LP |
| 2011 | If \fBsigsetjmp()\fR returns zero, \fBgl_get_line()\fR then does the following. |
| 2012 | .RS +4 |
| 2013 | .TP |
| 2014 | 1. |
| 2015 | It uses the POSIX \fBsigaction\fR(2) function to register a temporary signal |
| 2016 | handler to all of the signals that it is configured to catch. This signal |
| 2017 | handler does two things. |
| 2018 | .RS +4 |
| 2019 | .TP |
| 2020 | a. |
| 2021 | It records the number of the signal that was received in a file-scope |
| 2022 | variable. |
| 2023 | .RE |
| 2024 | .RS +4 |
| 2025 | .TP |
| 2026 | b. |
| 2027 | It then calls the POSIX \fBsiglongjmp()\fR function using the buffer that |
| 2028 | was passed to \fBsigsetjmp()\fR for its first argument and a non-zero value for |
| 2029 | its second argument. |
| 2030 | .RE |
| 2031 | When this signal handler is registered, the \fIsa_mask\fR member of the |
| 2032 | \fBstruct sigaction\fR \fIact\fR argument of the call to \fBsigaction()\fR is |
| 2033 | configured to contain all of the signals that \fBgl_get_line()\fR is catching. |
| 2034 | This ensures that only one signal will be caught at once by our signal handler, |
| 2035 | which in turn ensures that multiple instances of our signal handler do not |
| 2036 | tread on each other's toes. |
| 2037 | .RE |
| 2038 | .RS +4 |
| 2039 | .TP |
| 2040 | 2. |
| 2041 | Now that the signal handler has been set up, \fBgl_get_line()\fR unblocks |
| 2042 | all of the signals that it is configured to catch. |
| 2043 | .RE |
| 2044 | .RS +4 |
| 2045 | .TP |
| 2046 | 3. |
| 2047 | It then calls the \fBread()\fR or \fBselect()\fR function to wait for |
| 2048 | keyboard input. |
| 2049 | .RE |
| 2050 | .RS +4 |
| 2051 | .TP |
| 2052 | 4. |
| 2053 | If this function returns (that is, no signal is received), |
| 2054 | \fBgl_get_line()\fR blocks delivery of the signals of interest again. |
| 2055 | .RE |
| 2056 | .RS +4 |
| 2057 | .TP |
| 2058 | 5. |
| 2059 | It then reinstates the signal handlers that were displaced by the one that |
| 2060 | was just installed. |
| 2061 | .RE |
| 2062 | .sp |
| 2063 | .LP |
| 2064 | Alternatively, if \fBsigsetjmp()\fR returns non-zero, this means that one of |
| 2065 | the signals being trapped was caught while the above steps were executing. When |
| 2066 | this happens, \fBgl_get_line()\fR does the following. |
| 2067 | .sp |
| 2068 | .LP |
| 2069 | First, note that when a call to \fBsiglongjmp()\fR causes \fBsigsetjmp()\fR to |
| 2070 | return, provided that the \fIsavemask\fR argument of \fBsigsetjmp()\fR was |
| 2071 | non-zero, the signal process mask is restored to how it was when |
| 2072 | \fBsigsetjmp()\fR was called. This is the important difference between |
| 2073 | \fBsigsetjmp()\fR and the older problematic \fBsetjmp\fR(3C), and is the |
| 2074 | essential ingredient that makes it possible to avoid signal handling race |
| 2075 | conditions. Because of this we are guaranteed that all of the signals that we |
| 2076 | blocked before calling \fBsigsetjmp()\fR are blocked again as soon as any |
| 2077 | signal is caught. The following statements, which are then executed, are thus |
| 2078 | guaranteed to be executed without any further signals being caught. |
| 2079 | .RS +4 |
| 2080 | .TP |
| 2081 | 1. |
| 2082 | If so instructed by the \fBgl_get_line()\fR configuration attributes of the |
| 2083 | signal that was caught, \fBgl_get_line()\fR restores the terminal attributes to |
| 2084 | the state that they had when \fBgl_get_line()\fR was called. This is |
| 2085 | particularly important for signals that suspend or terminate the process, since |
| 2086 | otherwise the terminal would be left in an unusable state. |
| 2087 | .RE |
| 2088 | .RS +4 |
| 2089 | .TP |
| 2090 | 2. |
| 2091 | It then reinstates the application's signal handlers. |
| 2092 | .RE |
| 2093 | .RS +4 |
| 2094 | .TP |
| 2095 | 3. |
| 2096 | Then it uses the C standard-library \fBraise\fR(3C) function to re-send the |
| 2097 | application the signal that was caught. |
| 2098 | .RE |
| 2099 | .RS +4 |
| 2100 | .TP |
| 2101 | 4. |
| 2102 | Next it unblocks delivery of the signal that we just sent. This results in |
| 2103 | the signal that was just sent by \fBraise()\fR being caught by the |
| 2104 | application's original signal handler, which can now handle it as it sees fit. |
| 2105 | .RE |
| 2106 | .RS +4 |
| 2107 | .TP |
| 2108 | 5. |
| 2109 | If the signal handler returns (that is, it does not terminate the process), |
| 2110 | \fBgl_get_line()\fR blocks delivery of the above signal again. |
| 2111 | .RE |
| 2112 | .RS +4 |
| 2113 | .TP |
| 2114 | 6. |
| 2115 | It then undoes any actions performed in the first of the above steps and |
| 2116 | redisplays the line, if the signal configuration calls for this. |
| 2117 | .RE |
| 2118 | .RS +4 |
| 2119 | .TP |
| 2120 | 7. |
| 2121 | \fBgl_get_line()\fR then either resumes trying to read a character, or |
| 2122 | aborts, depending on the configuration of the signal that was caught. |
| 2123 | .RE |
| 2124 | .sp |
| 2125 | .LP |
| 2126 | What the above steps do in essence is to take asynchronously delivered signals |
| 2127 | and handle them synchronously, one at a time, at a point in the code where |
| 2128 | \fBgl_get_line()\fR has complete control over its environment. |
| 2129 | .SS "The Terminal Size" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 2130 | .LP |
| 2131 | On most systems the combination of the \fBTIOCGWINSZ\fR ioctl and the |
| 2132 | \fBSIGWINCH\fR signal is used to maintain an accurate idea of the terminal |
| 2133 | size. The terminal size is newly queried every time that \fBgl_get_line()\fR is |
| 2134 | called and whenever a \fBSIGWINCH\fR signal is received. |
| 2135 | .sp |
| 2136 | .LP |
| 2137 | On the few systems where this mechanism is not available, at startup |
| 2138 | \fBnew_GetLine()\fR first looks for the \fBLINES\fR and \fBCOLUMNS\fR |
| 2139 | environment variables. If these are not found, or they contain unusable values, |
| 2140 | then if a terminal information database like \fBterminfo\fR or \fBtermcap\fR is |
| 2141 | available, the default size of the terminal is looked up in this database. If |
| 2142 | this too fails to provide the terminal size, a default size of 80 columns by 24 |
| 2143 | lines is used. |
| 2144 | .sp |
| 2145 | .LP |
| 2146 | Even on systems that do support ioctl(\fBTIOCGWINSZ\fR), if the terminal is on |
| 2147 | the other end of a serial line, the terminal driver generally has no way of |
| 2148 | detecting when a resize occurs or of querying what the current size is. In such |
| 2149 | cases no \fBSIGWINCH\fR is sent to the process, and the dimensions returned by |
| 2150 | ioctl(\fBTIOCGWINSZ\fR) are not correct. The only way to handle such instances |
| 2151 | is to provide a way for the user to enter a command that tells the remote |
| 2152 | system what the new size is. This command would then call the |
| 2153 | \fBgl_set_term_size()\fR function to tell \fBgl_get_line()\fR about the change |
| 2154 | in size. |
| 2155 | .sp |
| 2156 | .LP |
| 2157 | The \fIncolumn\fR and \fInline\fR arguments are used to specify the new |
| 2158 | dimensions of the terminal, and must not be less than 1. On systems that do |
| 2159 | support ioctl(\fBTIOCGWINSZ\fR), this function first calls |
| 2160 | ioctl(\fBTIOCSWINSZ\fR) to tell the terminal driver about the change in size. |
| 2161 | In non-blocking server-I/O mode, if a line is currently being input, the input |
Cody Peter Mello | 843c398 | 2016-04-09 23:18:13 +0000 | [diff] [blame] | 2162 | line is then redrawn to accommodate the changed size. Finally the new values are |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 2163 | recorded in \fIgl\fR for future use by \fBgl_get_line()\fR. |
| 2164 | .sp |
| 2165 | .LP |
| 2166 | The \fBgl_terminal_size()\fR function allows you to query the current size of |
| 2167 | the terminal, and install an alternate fallback size for cases where the size |
| 2168 | is not available. Beware that the terminal size will not be available if |
| 2169 | reading from a pipe or a file, so the default values can be important even on |
| 2170 | systems that do support ways of finding out the terminal size. |
| 2171 | .sp |
| 2172 | .LP |
| 2173 | This function first updates \fBgl_get_line()\fR's fallback terminal dimensions, |
| 2174 | then records its findings in the return value. |
| 2175 | .sp |
| 2176 | .LP |
| 2177 | The \fIdef_ncolumn\fR and \fIdef_nline\fR arguments specify the default number |
| 2178 | of terminal columns and lines to use if the terminal size cannot be determined |
| 2179 | by ioctl(\fBTIOCGWINSZ\fR) or environment variables. |
| 2180 | .SS "Hiding What You Type" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 2181 | .LP |
| 2182 | When entering sensitive information, such as passwords, it is best not to have |
| 2183 | the text that you are entering echoed on the terminal. Furthermore, such text |
| 2184 | should not be recorded in the history list, since somebody finding your |
| 2185 | terminal unattended could then recall it, or somebody snooping through your |
| 2186 | directories could see it in your history file. With this in mind, the |
| 2187 | \fBgl_echo_mode()\fR function allows you to toggle on and off the display and |
| 2188 | archival of any text that is subsequently entered in calls to |
| 2189 | \fBgl_get_line()\fR. |
| 2190 | .sp |
| 2191 | .LP |
| 2192 | The \fIenable\fR argument specifies whether entered text should be visible or |
| 2193 | not. If it is 0, then subsequently entered lines will not be visible on the |
| 2194 | terminal, and will not be recorded in the history list. If it is 1, then |
| 2195 | subsequent input lines will be displayed as they are entered, and provided that |
| 2196 | history has not been turned off with a call to \fBgl_toggle_history()\fR, then |
| 2197 | they will also be archived in the history list. Finally, if the enable argument |
| 2198 | is -1, then the echoing mode is left unchanged, which allows you to |
| 2199 | non-destructively query the current setting through the return value. In all |
| 2200 | cases, the return value of the function is 0 if echoing was disabled before the |
| 2201 | function was called, and 1 if it was enabled. |
| 2202 | .sp |
| 2203 | .LP |
| 2204 | When echoing is turned off, note that although tab completion will invisibly |
| 2205 | complete your prefix as far as possible, ambiguous completions will not be |
| 2206 | displayed. |
| 2207 | .SS "Single Character Queries" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 2208 | .LP |
| 2209 | Using \fBgl_get_line()\fR to query the user for a single character reply, is |
| 2210 | inconvenient for the user, since they must hit the enter or return key before |
| 2211 | the character that they typed is returned to the program. Thus the |
| 2212 | \fBgl_query_char()\fR function has been provided for single character queries |
| 2213 | like this. |
| 2214 | .sp |
| 2215 | .LP |
| 2216 | This function displays the specified prompt at the start of a new line, and |
| 2217 | waits for the user to type a character. When the user types a character, |
| 2218 | \fBgl_query_char()\fR displays it to the right of the prompt, starts a newline, |
| 2219 | then returns the character to the calling program. The return value of the |
| 2220 | function is the character that was typed. If the read had to be aborted for |
| 2221 | some reason, EOF is returned instead. In the latter case, the application can |
| 2222 | call the previously documented \fBgl_return_status()\fR, to find out what went |
| 2223 | wrong. This could, for example, have been the reception of a signal, or the |
| 2224 | optional inactivity timer going off. |
| 2225 | .sp |
| 2226 | .LP |
| 2227 | If the user simply hits enter, the value of the \fIdefchar\fR argument is |
| 2228 | substituted. This means that when the user hits either newline or return, the |
| 2229 | character specified in \fIdefchar\fR, is displayed after the prompt, as though |
| 2230 | the user had typed it, as well as being returned to the calling application. If |
| 2231 | such a replacement is not important, simply pass '\en' as the value of |
| 2232 | \fIdefchar\fR. |
| 2233 | .sp |
| 2234 | .LP |
| 2235 | If the entered character is an unprintable character, it is displayed |
| 2236 | symbolically. For example, control-A is displayed as \fB^A\fR, and characters |
| 2237 | beyond 127 are displayed in octal, preceded by a backslash. |
| 2238 | .sp |
| 2239 | .LP |
| 2240 | As with \fBgl_get_line()\fR, echoing of the entered character can be disabled |
| 2241 | using the \fBgl_echo_mode()\fR function. |
| 2242 | .sp |
| 2243 | .LP |
| 2244 | If the calling process is suspended while waiting for the user to type their |
| 2245 | response, the cursor is moved to the line following the prompt line, then when |
| 2246 | the process resumes, the prompt is redisplayed, and \fBgl_query_char()\fR |
| 2247 | resumes waiting for the user to type a character. |
| 2248 | .sp |
| 2249 | .LP |
| 2250 | Note that in non-blocking server mode, if an incomplete input line is in the |
| 2251 | process of being read when \fBgl_query_char()\fR is called, the partial input |
| 2252 | line is discarded, and erased from the terminal, before the new prompt is |
| 2253 | displayed. The next call to \fBgl_get_line()\fR will thus start editing a new |
| 2254 | line. |
| 2255 | .SS "Reading Raw Characters" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 2256 | .LP |
| 2257 | Whereas the \fBgl_query_char()\fR function visibly prompts the user for a |
| 2258 | character, and displays what they typed, the \fBgl_read_char()\fR function |
| 2259 | reads a signal character from the user, without writing anything to the |
| 2260 | terminal, or perturbing any incompletely entered input line. This means that it |
| 2261 | can be called not only from between calls to \fBgl_get_line()\fR, but also from |
| 2262 | callback functions that the application has registered to be called by |
| 2263 | \fBgl_get_line()\fR. |
| 2264 | .sp |
| 2265 | .LP |
| 2266 | On success, the return value of \fBgl_read_char()\fR is the character that was |
| 2267 | read. On failure, EOF is returned, and the \fBgl_return_status()\fR function |
| 2268 | can be called to find out what went wrong. Possibilities include the optional |
| 2269 | inactivity timer going off, the receipt of a signal that is configured to abort |
| 2270 | \fBgl_get_line()\fR, or terminal I/O blocking, when in non-blocking server-I/O |
| 2271 | mode. |
| 2272 | .sp |
| 2273 | .LP |
| 2274 | Beware that certain keyboard keys, such as function keys, and cursor keys, |
| 2275 | usually generate at least three characters each, so a single call to |
| 2276 | \fBgl_read_char()\fR will not be enough to identify such keystrokes. |
| 2277 | .SS "Clearing The Terminal" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 2278 | .LP |
| 2279 | The calling program can clear the terminal by calling |
| 2280 | \fBgl_erase_terminal()\fR. In non-blocking server-I/O mode, this function also |
| 2281 | arranges for the current input line to be redrawn from scratch when |
| 2282 | \fBgl_get_line()\fR is next called. |
| 2283 | .SS "Displaying Text Dynamically" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 2284 | .LP |
| 2285 | Between calls to \fBgl_get_line()\fR, the \fBgl_display_text()\fR function |
| 2286 | provides a convenient way to display paragraphs of text, left-justified and |
| 2287 | split over one or more terminal lines according to the constraints of the |
| 2288 | current width of the terminal. Examples of the use of this function may be |
| 2289 | found in the demo programs, where it is used to display introductions. In those |
| 2290 | examples the advanced use of optional prefixes, suffixes and filled lines to |
| 2291 | draw a box around the text is also illustrated. |
| 2292 | .sp |
| 2293 | .LP |
| 2294 | If \fIgl\fR is not currently connected to a terminal, for example if the output |
| 2295 | of a program that uses \fBgl_get_line()\fR is being piped to another program or |
| 2296 | redirected to a file, then the value of the \fIdef_width\fR parameter is used |
| 2297 | as the terminal width. |
| 2298 | .sp |
| 2299 | .LP |
| 2300 | The \fIindentation\fR argument specifies the number of characters to use to |
Peter Tribble | 0327063 | 2018-12-10 21:26:43 +0000 | [diff] [blame^] | 2301 | indent each line of output. The \fIfill_char\fR argument specifies the character |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 2302 | that will be used to perform this indentation. |
| 2303 | .sp |
| 2304 | .LP |
| 2305 | The \fIprefix\fR argument can be either \fINULL\fR or a string to place at the |
| 2306 | beginning of each new line (after any indentation). Similarly, the \fIsuffix\fR |
| 2307 | argument can be either \fINULL\fR or a string to place at the end of each line. |
| 2308 | The suffix is placed flush against the right edge of the terminal, and any |
| 2309 | space between its first character and the last word on that line is filled with |
| 2310 | the character specified by the \fIfill_char\fR argument. Normally the |
| 2311 | fill-character is a space. |
| 2312 | .sp |
| 2313 | .LP |
| 2314 | The \fIstart\fR argument tells \fBgl_display_text()\fR how many characters have |
| 2315 | already been written to the current terminal line, and thus tells it the |
| 2316 | starting column index of the cursor. Since the return value of |
| 2317 | \fBgl_display_text()\fR is the ending column index of the cursor, by passing |
| 2318 | the return value of one call to the start argument of the next call, a |
| 2319 | paragraph that is broken between more than one string can be composed by |
| 2320 | calling \fBgl_display_text()\fR for each successive portion of the paragraph. |
| 2321 | Note that literal newline characters are necessary at the end of each paragraph |
| 2322 | to force a new line to be started. |
| 2323 | .sp |
| 2324 | .LP |
| 2325 | On error, \fBgl_display_text()\fR returns -1. |
| 2326 | .SS "Callback Function Facilities" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 2327 | .LP |
| 2328 | Unless otherwise stated, callback functions such as tab completion callbacks |
| 2329 | and event callbacks should not call any functions in this module. The following |
| 2330 | functions, however, are designed specifically to be used by callback functions. |
| 2331 | .sp |
| 2332 | .LP |
| 2333 | Calling the \fBgl_replace_prompt()\fR function from a callback tells |
| 2334 | \fBgl_get_line()\fR to display a different prompt when the callback returns. |
| 2335 | Except in non-blocking server mode, it has no effect if used between calls to |
| 2336 | \fBgl_get_line()\fR. In non-blocking server mode, when used between two calls |
| 2337 | to \fBgl_get_line()\fR that are operating on the same input line, the current |
| 2338 | input line will be re-drawn with the new prompt on the following call to |
| 2339 | \fBgl_get_line()\fR. |
| 2340 | .SS "International Character Sets" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 2341 | .LP |
| 2342 | Since \fBlibtecla\fR(3LIB) version 1.4.0, \fBgl_get_line()\fR has been 8-bit |
| 2343 | clean. This means that all 8-bit characters that are printable in the user's |
| 2344 | current locale are now displayed verbatim and included in the returned input |
| 2345 | line. Assuming that the calling program correctly contains a call like the |
| 2346 | following, |
| 2347 | .sp |
| 2348 | .in +2 |
| 2349 | .nf |
| 2350 | setlocale(LC_CTYPE, "") |
| 2351 | .fi |
| 2352 | .in -2 |
| 2353 | |
| 2354 | .sp |
| 2355 | .LP |
| 2356 | then the current locale is determined by the first of the environment variables |
| 2357 | \fBLC_CTYPE\fR, \fBLC_ALL\fR, and \fBLANG\fR that is found to contain a valid |
| 2358 | locale name. If none of these variables are defined, or the program neglects to |
| 2359 | call \fBsetlocale\fR(3C), then the default C locale is used, which is US 7-bit |
| 2360 | ASCII. On most UNIX-like platforms, you can get a list of valid locales by |
| 2361 | typing the command: |
| 2362 | .sp |
| 2363 | .in +2 |
| 2364 | .nf |
| 2365 | locale -a |
| 2366 | .fi |
| 2367 | .in -2 |
| 2368 | .sp |
| 2369 | |
| 2370 | .sp |
| 2371 | .LP |
| 2372 | at the shell prompt. Further documentation on how the user can make use of this |
| 2373 | to enter international characters can be found in the \fBtecla\fR(5) man page. |
| 2374 | .SS "Thread Safety" |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 2375 | .LP |
| 2376 | Unfortunately neither \fBterminfo\fR nor \fBtermcap\fR were designed to be |
| 2377 | reentrant, so you cannot safely use the functions of the getline module in |
| 2378 | multiple threads (you can use the separate file-expansion and word-completion |
| 2379 | modules in multiple threads, see the corresponding man pages for details). |
| 2380 | However due to the use of POSIX reentrant functions for looking up home |
| 2381 | directories, it is safe to use this module from a single thread of a |
| 2382 | multi-threaded program, provided that your other threads do not use any |
| 2383 | \fBtermcap\fR or \fBterminfo\fR functions. |
| 2384 | .SH ATTRIBUTES |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 2385 | .LP |
| 2386 | See \fBattributes\fR(5) for descriptions of the following attributes: |
| 2387 | .sp |
| 2388 | |
| 2389 | .sp |
| 2390 | .TS |
Yuri Pankov | ed22c71 | 2011-10-03 04:36:40 -0700 | [diff] [blame] | 2391 | box; |
| 2392 | c | c |
| 2393 | l | l . |
| 2394 | ATTRIBUTE TYPE ATTRIBUTE VALUE |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 2395 | _ |
Yuri Pankov | ed22c71 | 2011-10-03 04:36:40 -0700 | [diff] [blame] | 2396 | Interface Stability Committed |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 2397 | _ |
Yuri Pankov | ed22c71 | 2011-10-03 04:36:40 -0700 | [diff] [blame] | 2398 | MT-Level MT-Safe |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 2399 | .TE |
| 2400 | |
| 2401 | .SH SEE ALSO |
Richard Lowe | c10c16d | 2011-03-14 14:05:30 -0400 | [diff] [blame] | 2402 | .LP |
| 2403 | \fBcpl_complete_word\fR(3TECLA), \fBef_expand_file\fR(3TECLA), |
| 2404 | \fBgl_io_mode\fR(3TECLA), \fBlibtecla\fR(3LIB), \fBpca_lookup_file\fR(3TECLA), |
| 2405 | \fBattributes\fR(5), \fBtecla\fR(5) |