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