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