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