Print this page
12212 typos in some section 3tecla man pages


   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.
  12 .\"
  13 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14 .\" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
  16 .\" OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  17 .\" HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  18 .\" INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  19 .\" FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  20 .\" NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  21 .\" WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22 .\"
  23 .\" Except as contained in this notice, the name of a copyright holder
  24 .\" shall not be used in advertising or otherwise to promote the sale, use
  25 .\" or other dealings in this Software without prior written authorization
  26 .\" of the copyright holder.
  27 .\" Portions Copyright (c) 2007, Sun Microsystems, Inc. All Rights Reserved.
  28 .TH GL_GET_LINE 3TECLA "April 9, 2016"
  29 .SH NAME
  30 gl_get_line, new_GetLine, del_GetLine, gl_customize_completion,
  31 gl_change_terminal, gl_configure_getline, gl_load_history, gl_save_history,
  32 gl_group_history, gl_show_history, gl_watch_fd, gl_inactivity_timeout,
  33 gl_terminal_size, gl_set_term_size, gl_resize_history, gl_limit_history,
  34 gl_clear_history, gl_toggle_history, gl_lookup_history, gl_state_of_history,
  35 gl_range_of_history, gl_size_of_history, gl_echo_mode, gl_replace_prompt,
  36 gl_prompt_style, gl_ignore_signal, gl_trap_signal, gl_last_signal,
  37 gl_completion_action, gl_register_action, gl_display_text, gl_return_status,
  38 gl_error_message, gl_catch_blocked, gl_list_signals, gl_bind_keyseq,
  39 gl_erase_terminal, gl_automatic_history, gl_append_history, gl_query_char,
  40 gl_read_char \- allow the user to compose an input line
  41 .SH SYNOPSIS
  42 .LP
  43 .nf
  44 cc [ \fIflag\fR\&.\|.\|. ] \fIfile\fR\&.\|.\|. \fB-ltecla\fR [ \fIlibrary\fR\&.\|.\|. ]
  45 #include <stdio.h>
  46 #include <libtecla.h>
  47 
  48 \fBGetLine *\fR\fBnew_GetLine\fR(\fBsize_t\fR \fIlinelen\fR, \fBsize_t\fR \fIhistlen\fR);
  49 .fi
  50 
  51 .LP
  52 .nf
  53 \fBGetLine *\fR\fBdel_GetLine\fR(\fBGetLine *\fR\fIgl\fR);
  54 .fi
  55 
  56 .LP
  57 .nf
  58 \fBchar *\fR\fBgl_get_line\fR(\fBGetLine *\fR\fIgl\fR, \fBconst char *\fR\fIprompt\fR,
  59      \fBconst char *\fR\fIstart_line\fR, \fBint\fR \fIstart_pos\fR);
  60 .fi
  61 
  62 .LP


 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
 270 .LP
 271 The \fBgl_get_line()\fR function is part of the \fBlibtecla\fR(3LIB) library.
 272 If the user is typing at a terminal, each call prompts them for a line of
 273 input, then provides interactive editing facilities, similar to those of the
 274 UNIX \fBtcsh\fR shell. In addition to simple command-line editing, it supports
 275 recall of previously entered command lines, TAB completion of file names, and
 276 in-line wild-card expansion of filenames. Documentation of both the user-level
 277 command-line editing features and all user configuration options can be found
 278 on the \fBtecla\fR(5) manual page.
 279 .SS "An Example"
 280 .LP
 281 The following shows a complete example of how to use the \fBgl_get_line()\fR
 282 function to get input from the user:
 283 .sp
 284 .in +2
 285 .nf
 286 #include <stdio.h>
 287 #include <locale.h>
 288 #include <libtecla.h>
 289 
 290 int main(int argc, char *argv[])
 291 {
 292   char *line;    /* The line that the user typed */
 293   GetLine *gl;   /* The gl_get_line() resource object */
 294 
 295   setlocale(LC_CTYPE, ""); /* Adopt the user's choice */
 296                            /* of character set. */
 297 
 298   gl = new_GetLine(1024, 2048);
 299   if(!gl)
 300     return 1;


 305   gl = del_GetLine(gl);
 306   return 0;
 307 }
 308 .fi
 309 .in -2
 310 
 311 .sp
 312 .LP
 313 In the example, first the resources needed by the \fBgl_get_line()\fR function
 314 are created by calling \fBnew_GetLine()\fR. This allocates the memory used in
 315 subsequent calls to the \fBgl_get_line()\fR function, including the history
 316 buffer for recording previously entered lines. Then one or more lines are read
 317 from the user, until either an error occurs, or the user types exit. Then
 318 finally the resources that were allocated by \fBnew_GetLine()\fR, are returned
 319 to the system by calling \fBdel_GetLine()\fR. Note the use of the \fINULL\fR
 320 return value of \fBdel_GetLine()\fR to make \fIgl\fR \fINULL\fR. This is a
 321 safety precaution. If the program subsequently attempts to pass \fIgl\fR to
 322 \fBgl_get_line()\fR, said function will complain, and return an error, instead
 323 of attempting to use the deleted resource object.
 324 .SS "The Functions Used In The Example"
 325 .LP
 326 The \fBnew_GetLine()\fR function creates the resources used by the
 327 \fBgl_get_line()\fR function and returns an opaque pointer to the object that
 328 contains them. The maximum length of an input line is specified by the
 329 \fIlinelen\fR argument, and the number of bytes to allocate for storing history
 330 lines is set by the \fIhistlen\fR argument. History lines are stored
 331 back-to-back in a single buffer of this size. Note that this means that the
 332 number of history lines that can be stored at any given time, depends on the
 333 lengths of the individual lines. If you want to place an upper limit on the
 334 number of lines that can be stored, see the description of the
 335 \fBgl_limit_history()\fR function. If you do not want history at all, specify
 336 \fIhistlen\fR as zero, and no history buffer will be allocated.
 337 .sp
 338 .LP
 339 On error, a message is printed to \fBstderr\fR and \fINULL\fR is returned.
 340 .sp
 341 .LP
 342 The \fBdel_GetLine()\fR function deletes the resources that were returned by a
 343 previous call to \fBnew_GetLine()\fR. It always returns \fINULL\fR (for
 344 example, a deleted object). It does nothing if the \fIgl\fR argument is
 345 \fINULL\fR.


 358 pass the desired string with the \fIstart_line\fR argument. You can then
 359 specify which character of this line the cursor is initially positioned over by
 360 using the \fIstart_pos\fR argument. This should be -1 if you want the cursor to
 361 follow the last character of the start line. If you do not want to preload the
 362 line in this manner, send \fIstart_line\fR as \fINULL\fR, and set
 363 \fIstart_pos\fR to -1.
 364 .sp
 365 .LP
 366 The \fBgl_get_line()\fR function returns a pointer to the line entered by the
 367 user, or \fINULL\fR on error or at the end of the input. The returned pointer
 368 is part of the specified \fIgl\fR resource object, and thus should not be freed
 369 by the caller, or assumed to be unchanging from one call to the next. When
 370 reading from a user at a terminal, there will always be a newline character at
 371 the end of the returned line. When standard input is being taken from a pipe or
 372 a file, there will similarly be a newline unless the input line was too long to
 373 store in the internal buffer. In the latter case you should call
 374 \fBgl_get_line()\fR again to read the rest of the line. Note that this behavior
 375 makes \fBgl_get_line()\fR similar to \fBfgets\fR(3C). When \fBstdin\fR is not
 376 connected to a terminal, \fBgl_get_line()\fR simply calls \fBfgets()\fR.
 377 .SS "The Return Status Of \fBgl_get_line()\fR"
 378 .LP
 379 The \fBgl_get_line()\fR function has two possible return values: a pointer to
 380 the completed input line, or \fINULL\fR. Additional information about what
 381 caused \fBgl_get_line()\fR to return is available both by inspecting
 382 \fBerrno\fR and by calling the \fBgl_return_status()\fR function.
 383 .sp
 384 .LP
 385 The following are the possible enumerated values returned by
 386 \fBgl_return_status()\fR:
 387 .sp
 388 .ne 2
 389 .na
 390 \fB\fBGLR_NEWLINE\fR\fR
 391 .ad
 392 .RS 15n
 393 The last call to \fBgl_get_line()\fR successfully returned a completed input
 394 line.
 395 .RE
 396 
 397 .sp
 398 .ne 2


 416 of \fBGLS_ABORT\fR. See \fBgl_trap_signal()\fR.
 417 .RE
 418 
 419 .sp
 420 .ne 2
 421 .na
 422 \fB\fBGLR_TIMEOUT\fR\fR
 423 .ad
 424 .RS 15n
 425 The inactivity timer expired while \fBgl_get_line()\fR was waiting for input,
 426 and the timeout callback function returned \fBGLTO_ABORT\fR. See
 427 \fBgl_inactivity_timeout()\fR for information about timeouts.
 428 .RE
 429 
 430 .sp
 431 .ne 2
 432 .na
 433 \fB\fBGLR_FDABORT\fR\fR
 434 .ad
 435 .RS 15n
 436 An application I/O callback returned \fBGLFD_ABORT\fR. Ssee
 437 \fBgl_watch_fd()\fR.
 438 .RE
 439 
 440 .sp
 441 .ne 2
 442 .na
 443 \fB\fBGLR_EOF\fR\fR
 444 .ad
 445 .RS 15n
 446 End of file reached. This can happen when input is coming from a file or a
 447 pipe, instead of the terminal. It also occurs if the user invokes the
 448 list-or-eof or del-char-or-list-or-eof actions at the start of a new line.
 449 .RE
 450 
 451 .sp
 452 .ne 2
 453 .na
 454 \fB\fBGLR_ERROR\fR\fR
 455 .ad
 456 .RS 15n


 458 and/or \fBgl_error_message()\fR for details.
 459 .RE
 460 
 461 .sp
 462 .LP
 463 When \fBgl_return_status()\fR returns \fBGLR_ERROR\fR and the value of
 464 \fBerrno\fR is not sufficient to explain what happened, you can use the
 465 \fBgl_error_message()\fR function to request a description of the last error
 466 that occurred.
 467 .sp
 468 .LP
 469 The return value of \fBgl_error_message()\fR is a pointer to the message that
 470 occurred. If the \fIbuff\fR argument is \fINULL\fR, this will be a pointer to a
 471 buffer within \fIgl\fR whose value will probably change on the next call to any
 472 function associated with \fBgl_get_line()\fR. Otherwise, if a non-null
 473 \fIbuff\fR argument is provided, the error message, including a '\e0'
 474 terminator, will be written within the first \fIn\fR elements of this buffer,
 475 and the return value will be a pointer to the first element of this buffer. If
 476 the message will not fit in the provided buffer, it will be truncated to fit.
 477 .SS "Optional Prompt Formatting"
 478 .LP
 479 Whereas by default the prompt string that you specify is displayed literally
 480 without any special interpretation of the characters within it, the
 481 \fBgl_prompt_style()\fR function can be used to enable optional formatting
 482 directives within the prompt.
 483 .sp
 484 .LP
 485 The \fIstyle\fR argument, which specifies the formatting style, can take any of
 486 the following values:
 487 .sp
 488 .ne 2
 489 .na
 490 \fB\fBGL_FORMAT_PROMPT\fR\fR
 491 .ad
 492 .RS 21n
 493 In this style, the formatting directives described below, when included in
 494 prompt strings, are interpreted as follows:
 495 .sp
 496 .ne 2
 497 .na
 498 \fB\fB%B\fR\fR


 615 Note that although a pair of characters that starts with a % character, but
 616 does not match any of the above directives is displayed literally, if a new
 617 directive is subsequently introduced which does match, the displayed prompt
 618 will change, so it is better to always use %% to display a literal %.
 619 .sp
 620 Also note that not all terminals support all of these text attributes, and that
 621 some substitute a different attribute for missing ones.
 622 .RE
 623 
 624 .sp
 625 .ne 2
 626 .na
 627 \fB\fBGL_LITERAL_PROMPT\fR\fR
 628 .ad
 629 .RS 21n
 630 In this style, the prompt string is printed literally. This is the default
 631 style.
 632 .RE
 633 
 634 .SS "Alternate Configuration Sources"
 635 .LP
 636 By default users have the option of configuring the behavior of
 637 \fBgl_get_line()\fR with a configuration file called \fB\&.teclarc\fR in their
 638 home directories. The fact that all applications share this same configuration
 639 file is both an advantage and a disadvantage. In most cases it is an advantage,
 640 since it encourages uniformity, and frees the user from having to configure
 641 each application separately. In some applications, however, this single means
 642 of configuration is a problem. This is particularly true of embedded software,
 643 where there's no filesystem to read a configuration file from, and also in
 644 applications where a radically different choice of keybindings is needed to
 645 emulate a legacy keyboard interface. To cater for such cases, the
 646 \fBgl_configure_getline()\fR function allows the application to control where
 647 configuration information is read from.
 648 .sp
 649 .LP
 650 The \fBgl_configure_getline()\fR function allows the configuration commands
 651 that would normally be read from a user's \fB~/.teclarc\fR file, to be read
 652 from any or none of, a string, an application specific configuration file,
 653 and/or a user-specific configuration file. If this function is called before
 654 the first call to \fBgl_get_line()\fR, the default behavior of reading
 655 \fB~/.teclarc\fR on the first call to \fBgl_get_line()\fR is disabled, so all


 664 file. If user_file != \fINULL\fR then it is interpreted as the full path name
 665 of a user-specific configuration file, such as \fB~/.teclarc\fR. For example,
 666 in the call
 667 .sp
 668 .in +2
 669 .nf
 670 gl_configure_getline(gl, "edit-mode vi \en nobeep",
 671                      "/usr/share/myapp/teclarc", "~/.teclarc");
 672 .fi
 673 .in -2
 674 
 675 .sp
 676 .LP
 677 The \fIapp_string\fR argument causes the calling application to start in
 678 \fBvi\fR(1) edit-mode, instead of the default \fBemacs\fR mode, and turns off
 679 the use of the terminal bell by the library. It then attempts to read
 680 system-wide configuration commands from an optional file called
 681 \fB/usr/share/myapp/teclarc\fR, then finally reads user-specific configuration
 682 commands from an optional \fB\&.teclarc\fR file in the user's home directory.
 683 Note that the arguments are listed in ascending order of priority, with the
 684 contents of \fIapp_string\fR being potentially over riden by commands in
 685 \fIapp_file\fR, and commands in \fIapp_file\fR potentially being overridden by
 686 commands in \fIuser_file\fR.
 687 .sp
 688 .LP
 689 You can call this function as many times as needed, the results being
 690 cumulative, but note that copies of any file names specified with the
 691 \fIapp_file\fR and \fIuser_file\fR arguments are recorded internally for
 692 subsequent use by the read-init-files key-binding function, so if you plan to
 693 call this function multiple times, be sure that the last call specifies the
 694 filenames that you want re-read when the user requests that the configuration
 695 files be re-read.
 696 .sp
 697 .LP
 698 Individual key sequences can also be bound and unbound using the
 699 \fBgl_bind_keyseq()\fR function. The \fIorigin\fR argument specifies the
 700 priority of the binding, according to whom it is being established for, and
 701 must be one of the following two values.
 702 .sp
 703 .ne 2
 704 .na


 714 \fB\fBGL_APP_KEY\fR\fR
 715 .ad
 716 .RS 15n
 717 This is a default binding set by the application.
 718 .RE
 719 
 720 .sp
 721 .LP
 722 When both user and application bindings for a given key sequence have been
 723 specified, the user binding takes precedence. The application's binding is
 724 subsequently reinstated if the user's binding is later unbound with either
 725 another call to this function, or a call to \fBgl_configure_getline()\fR.
 726 .sp
 727 .LP
 728 The \fIkeyseq\fR argument specifies the key sequence to be bound or unbound,
 729 and is expressed in the same way as in a \fB~/.teclarc\fR configuration file.
 730 The \fIaction\fR argument must either be a string containing the name of the
 731 action to bind the key sequence to, or it must be \fINULL\fR or \fB""\fR to
 732 unbind the key sequence.
 733 .SS "Customized Word Completion"
 734 .LP
 735 If in your application you would like to have TAB completion complete other
 736 things in addition to or instead of filenames, you can arrange this by
 737 registering an alternate completion callback function with a call to the
 738 \fBgl_customize_completion()\fR function.
 739 .sp
 740 .LP
 741 The \fIdata\fR argument provides a way for your application to pass arbitrary,
 742 application-specific information to the callback function. This is passed to
 743 the callback every time that it is called. It might for example point to the
 744 symbol table from which possible completions are to be sought. The
 745 \fImatch_fn\fR argument specifies the callback function to be called. The
 746 \fICplMatchFn\fR function type is defined in <\fBlibtecla.h\fR>, as is a
 747 \fBCPL_MATCH_FN()\fR macro that you can use to declare and prototype callback
 748 functions. The declaration and responsibilities of callback functions are
 749 described in depth on the \fBcpl_complete_word\fR(3TECLA) manual page.
 750 .sp
 751 .LP
 752 The callback function is responsible for looking backwards in the input line
 753 from the point at which the user pressed TAB, to find the start of the word
 754 being completed. It then must lookup possible completions of this word, and
 755 record them one by one in the \fBWordCompletion\fR object that is passed to it
 756 as an argument, by calling the \fBcpl_add_completion()\fR function. If the
 757 callback function wants to provide filename completion in addition to its own
 758 specific completions, it has the option of itself calling the builtin filename
 759 completion callback. This also is documented on the
 760 \fBcpl_complete_word\fR(3TECLA) manual page.
 761 .sp
 762 .LP
 763 If you would like \fBgl_get_line()\fR to return the current input line when a
 764 successful completion is been made, you can arrange this when you call
 765 \fBcpl_add_completion()\fR by making the last character of the continuation
 766 suffix a newline character. The input line will be updated to display the
 767 completion, together with any contiuation suffix up to the newline character,
 768 and \fBgl_get_line()\fR will return this input line.
 769 .sp
 770 .LP
 771 If your callback function needs to write something to the terminal, it must
 772 call \fBgl_normal_io()\fR before doing so. This will start a new line after the
 773 input line that is currently being edited, reinstate normal terminal I/O, and
 774 notify \fBgl_get_line()\fR that the input line will need to be redrawn when the
 775 callback returns.
 776 .SS "Adding Completion Actions"
 777 .LP
 778 In the previous section the ability to customize the behavior of the only
 779 default completion action, complete-word, was described. In this section the
 780 ability to install additional action functions, so that different types of word
 781 completion can be bound to different key sequences, is described. This is
 782 achieved by using the \fBgl_completion_action()\fR function.
 783 .sp
 784 .LP
 785 The \fIdata\fR and \fImatch_fn\fR arguments are as described on the
 786 \fBcpl_complete_word\fR(3TECLA) manual page, and specify the callback function
 787 that should be invoked to identify possible completions. The \fIlist_only\fR
 788 argument determines whether the action that is being defined should attempt to
 789 complete the word as far as possible in the input line before displaying any
 790 possible ambiguous completions, or whether it should simply display the list of
 791 possible completions without touching the input line. The former option is
 792 selected by specifying a value of 0, and the latter by specifying a value of 1.
 793 The \fIname\fR argument specifies the name by which configuration files and
 794 future invocations of this function should refer to the action. This must
 795 either be the name of an existing completion action to be changed, or be a new
 796 unused name for a new action. Finally, the \fIkeyseq\fR argument specifies the
 797 default key sequence to bind the action to. If this is \fINULL\fR, no new key
 798 sequence will be bound to the action.
 799 .sp
 800 .LP
 801 Beware that in order for the user to be able to change the key sequence that is
 802 bound to actions that are installed in this manner, you shouldcall
 803 \fBgl_completion_action()\fR to install a given action for the first time
 804 between calling \fBnew_GetLine()\fR and the first call to \fBgl_get_line()\fR.
 805 Otherwise, when the user's configuration file is read on the first call to
 806 \fBgl_get_line()\fR, the name of the your additional action will not be known,
 807 and any reference to it in the configuration file will generate an error.
 808 .sp
 809 .LP
 810 As discussed for \fBgl_customize_completion()\fR, if your callback function
 811 needs to write anything to the terminal, it must call \fBgl_normal_io()\fR
 812 before doing so.
 813 .SS "Defining Custom Actions"
 814 .LP
 815 Although the built-in key-binding actions are sufficient for the needs of most
 816 applications, occasionally a specialized application may need to define one or
 817 more custom actions, bound to application-specific key sequences. For example,
 818 a sales application would benefit from having a key sequence that displayed the
 819 part name that corresponded to a part number preceding the cursor. Such a
 820 feature is clearly beyond the scope of the built-in action functions. So for
 821 such special cases, the \fBgl_register_action()\fR function is provided.
 822 .sp
 823 .LP
 824 The \fBgl_register_action()\fR function lets the application register an
 825 external function, \fIfn\fR, that will thereafter be called whenever either the
 826 specified key sequence, \fIkeyseq\fR, is entered by the user, or the user
 827 enters any other key sequence that the user subsequently binds to the specified
 828 action name, \fIname\fR, in their configuration file. The \fIdata\fR argument
 829 can be a pointer to anything that the application wants to have passed to the
 830 action function, \fIfn\fR, whenever that function is invoked.
 831 .sp
 832 .LP
 833 The action function, \fIfn\fR, should be declared using the
 834 \fBGL_ACTION_FN()\fR macro, which is defined in <\fBlibtecla.h\fR>.


 920 Cause \fBgl_get_line()\fR to return the completed input line
 921 .RE
 922 
 923 .sp
 924 .ne 2
 925 .na
 926 \fB\fBGLA_CONTINUE\fR\fR
 927 .ad
 928 .RS 16n
 929 Resume command-line editing.
 930 .RE
 931 
 932 .sp
 933 .LP
 934 Note that the \fIname\fR argument of \fBgl_register_action()\fR specifies the
 935 name by which a user can refer to the action in their configuration file. This
 936 allows them to re-bind the action to an alternate key-sequence. In order for
 937 this to work, it is necessary to call \fBgl_register_action()\fR between
 938 calling \fBnew_GetLine()\fR and the first call to \fBgl_get_line()\fR.
 939 .SS "History Files"
 940 .LP
 941 To save the contents of the history buffer before quitting your application and
 942 subsequently restore them when you next start the application, the
 943 \fBgl_save_history()\fR and \fBgl_load_history()\fR functions are provided.
 944 .sp
 945 .LP
 946 The \fIfilename\fR argument specifies the name to give the history file when
 947 saving, or the name of an existing history file, when loading. This may contain
 948 home directory and environment variable expressions, such as
 949 \fB~/.myapp_history\fR or \fB$HOME/.myapp_history\fR.
 950 .sp
 951 .LP
 952 Along with each history line, additional information about it, such as its
 953 nesting level and when it was entered by the user, is recorded as a comment
 954 preceding the line in the history file. Writing this as a comment allows the
 955 history file to double as a command file, just in case you wish to replay a
 956 whole session using it. Since comment prefixes differ in different languages,
 957 the comment argument is provided for specifying the comment prefix. For
 958 example, if your application were a UNIX shell, such as the Bourne shell, you
 959 would specify "#" here. Whatever you choose for the comment character, you must
 960 specify the same prefix to \fBgl_load_history()\fR that you used when you
 961 called \fBgl_save_history()\fR to write the history file.
 962 .sp
 963 .LP
 964 The \fImax_lines\fR argument must be either -1 to specify that all lines in the
 965 history list be saved, or a positive number specifying a ceiling on how many of
 966 the most recent lines should be saved.
 967 .sp
 968 .LP
 969 Both fuctions return non-zero on error, after writing an error message to
 970 \fBstderr\fR. Note that \fBgl_load_history()\fR does not consider the
 971 non-existence of a file to be an error.
 972 .SS "Multiple History Lists"
 973 .LP
 974 If your application uses a single \fBGetLine\fR object for entering many
 975 different types of input lines, you might want \fBgl_get_line()\fR to
 976 distinguish the different types of lines in the history list, and only recall
 977 lines that match the current type of line. To support this requirement,
 978 \fBgl_get_line()\fR marks lines being recorded in the history list with an
 979 integer identifier chosen by the application. Initially this identifier is set
 980 to 0 by \fBnew_GetLine()\fR, but it can be changed subsequently by calling
 981 \fBgl_group_history()\fR.
 982 .sp
 983 .LP
 984 The integer identifier ID can be any number chosen by the application, but note
 985 that \fBgl_save_history()\fR and \fBgl_load_history()\fR preserve the
 986 association between identifiers and historical input lines between program
 987 invocations, so you should choose fixed identifiers for the different types of
 988 input line used by your application.
 989 .sp
 990 .LP
 991 Whenever \fBgl_get_line()\fR appends a new input line to the history list, the
 992 current history identifier is recorded with it, and when it is asked to recall
 993 a historical input line, it only recalls lines that are marked with the current
 994 identifier.
 995 .SS "Displaying History"
 996 .LP
 997 The history list can be displayed by calling \fBgl_show_history()\fR. This
 998 function displays the current contents of the history list to the \fBstdio\fR
 999 output stream \fIfp\fR. If the \fImax_lines\fR argument is greater than or
1000 equal to zero, then no more than this number of  the most recent lines will be
1001 displayed. If the \fIall_groups\fR argument is non-zero, lines from all history
1002 groups are displayed. Otherwise only those of the currently selected history
1003 group are displayed. The format string argument, \fIfmt\fR, determines how the
1004 line is displayed. This can contain arbitrary characters which are written
1005 verbatim, interleaved with any of the following format directives:
1006 .sp
1007 .ne 2
1008 .na
1009 \fB\fB%D\fR\fR
1010 .ad
1011 .RS 6n
1012 The date on which the line was originally entered, formatted like 2001-11-20.
1013 .RE
1014 
1015 .sp
1016 .ne 2


1054 \fB\fB%H\fR\fR
1055 .ad
1056 .RS 6n
1057 The history line itself.
1058 .RE
1059 
1060 .sp
1061 .LP
1062 Thus a format string like "%D %T %H0" would output something like:
1063 .sp
1064 .in +2
1065 .nf
1066 2001-11-20 10:23:34  Hello world
1067 .fi
1068 .in -2
1069 
1070 .sp
1071 .LP
1072 Note the inclusion of an explicit newline character in the format string.
1073 .SS "Looking Up History"
1074 .LP
1075 The \fBgl_lookup_history()\fR function allows the calling application to look
1076 up lines in the history list.
1077 .sp
1078 .LP
1079 The \fIid\fR argument indicates which line to look up, where the first line
1080 that was entered in the history list after \fBnew_GetLine()\fR was called is
1081 denoted by 0, and subsequently entered lines are denoted with successively
1082 higher numbers. Note that the range of lines currently preserved in the history
1083 list can be queried by calling the \fBgl_range_of_history()\fR function. If the
1084 requested line is in the history list, the details of the line are recorded in
1085 the variable pointed to by the \fIhline\fR argument, and 1 is returned.
1086 Otherwise 0 is returned, and the variable pointed to by \fIhline\fR is left
1087 unchanged.
1088 .sp
1089 .LP
1090 Beware that the string returned in \fIhline\fR->\fIline\fR is part of the
1091 history buffer, so it must not be modified by the caller, and will be recycled
1092 on the next call to any function that takes \fIgl\fR as its argument. Therefore
1093 you should make a private copy of this string if you need to keep it.
1094 .SS "Manual History Archival"
1095 .LP
1096 By default, whenever a line is entered by the user, it is automatically
1097 appended to the history list, just before \fBgl_get_line()\fR returns the line
1098 to the caller. This is convenient for the majority of applications, but there
1099 are also applications that need finer-grained control over what gets added to
1100 the history list. In such cases, the automatic addition of entered lines to the
1101 history list can be turned off by calling the \fBgl_automatic_history()\fR
1102 function.
1103 .sp
1104 .LP
1105 If this function is called with its \fIenable\fR argument set to 0,
1106 \fBgl_get_line()\fR will not automatically archive subsequently entered lines.
1107 Automatic archiving can be reenabled at a later time by calling this function
1108 again, with its \fIenable\fR argument set to 1. While automatic history
1109 archiving is disabled, the calling application can use the
1110 \fBgl_append_history()\fR to append lines to the history list as needed.
1111 .sp
1112 .LP
1113 The \fIline\fR argument specifies the line to be added to the history list.
1114 This must be a normal '\e0 ' terminated string. If this string contains any
1115 newline characters, the line that gets archived in the history list will be


1130 One of the arguments passed to \fBgl_append_history()\fR was \fINULL\fR.
1131 .RE
1132 
1133 .sp
1134 .ne 2
1135 .na
1136 \fB\fBENOMEM\fR\fR
1137 .ad
1138 .RS 10n
1139 The specified line was longer than the allocated size of the history buffer (as
1140 specified when \fBnew_GetLine()\fR was called), so it could not be archived.
1141 .RE
1142 
1143 .sp
1144 .LP
1145 A textual description of the error can optionally be obtained by calling
1146 \fBgl_error_message()\fR. Note that after such an error, the history list
1147 remains in a valid state to receive new history lines, so there is little harm
1148 in simply ignoring the return status of \fBgl_append_history()\fR.
1149 .SS "Miscellaneous History Configuration"
1150 .LP
1151 If you wish to change the size of the history buffer that was originally
1152 specified in the call to \fBnew_GetLine()\fR, you can do so with the
1153 \fBgl_resize_history()\fR function.
1154 .sp
1155 .LP
1156 The \fIhistlen\fR argument specifies the new size in bytes, and if you specify
1157 this as 0, the buffer will be deleted.
1158 .sp
1159 .LP
1160 As mentioned in the discussion of \fBnew_GetLine()\fR, the number of lines that
1161 can be stored in the history buffer, depends on the lengths of the individual
1162 lines. For example, a 1000 byte buffer could equally store 10 lines of average
1163 length 100 bytes, or 20 lines of average length 50 bytes. Although the buffer
1164 is never expanded when new lines are added, a list of pointers into the buffer
1165 does get expanded when needed to accommodate the number of lines currently
1166 stored in the buffer. To place an upper limit on the number of lines in the
1167 buffer, and thus a ceiling on the amount of memory used in this list, you can
1168 call the \fBgl_limit_history()\fR function.
1169 .sp
1170 .LP


1175 .sp
1176 .LP
1177 To discard lines from the history buffer, use the \fBgl_clear_history()\fR
1178 function.
1179 .sp
1180 .LP
1181 The \fIall_groups\fR argument tells the function whether to delete just the
1182 lines associated with the current history group (see \fBgl_group_history()\fR)
1183 or all historical lines in the buffer.
1184 .sp
1185 .LP
1186 The \fBgl_toggle_history()\fR function allows you to toggle history on and off
1187 without losing the current contents of the history list.
1188 .sp
1189 .LP
1190 Setting the \fIenable\fR argument to 0 turns off the history mechanism, and
1191 setting it to 1 turns it back on. When history is turned off, no new lines will
1192 be added to the history list, and history lookup key-bindings will act as
1193 though there is nothing in the history buffer.
1194 .SS "Querying History Information"
1195 .LP
1196 The configured state of the history list can be queried with the
1197 \fBgl_history_state()\fR function. On return, the status information is
1198 recorded in the variable pointed to by the \fIstate\fR argument.
1199 .sp
1200 .LP
1201 The \fBgl_range_of_history()\fR function returns the number and range of lines
1202 in the history list. The return values are recorded in the variable pointed to
1203 by the range argument. If the \fInlines\fR member of this structure is greater
1204 than zero, then the oldest and newest members report the range of lines in the
1205 list, and \fInewest\fR=\fIoldest\fR+\fInlines\fR-1. Otherwise they are both
1206 zero.
1207 .sp
1208 .LP
1209 The \fBgl_size_of_history()\fR function returns the total size of the history
1210 buffer and the amount of the buffer that is currently occupied.
1211 .sp
1212 .LP
1213 On return, the size information is recorded in the variable pointed to by the
1214 \fIsize\fR argument.
1215 .SS "Changing Terminals"
1216 .LP
1217 The \fBnew_GetLine()\fR constructor function assumes that input is to be read
1218 from \fBstdin\fR and output written to \fBstdout\fR. The following function
1219 allows you to switch to different input and output streams.
1220 .sp
1221 .LP
1222 The \fIgl\fR argument is the object that was returned by \fBnew_GetLine()\fR.
1223 The \fIinput_fp\fR argument specifies the stream to read from, and
1224 \fIoutput_fp\fR specifies the stream to be written to. Only if both of these
1225 refer to a terminal, will interactive terminal input be enabled. Otherwise
1226 \fBgl_get_line()\fR will simply call \fBfgets()\fR to read command input. If
1227 both streams refer to a terminal, then they must refer to the same terminal,
1228 and the type of this terminal must be specified with the \fIterm\fR argument.
1229 The value of the \fIterm\fR argument is looked up in the terminal information
1230 database (\fBterminfo\fR or \fBtermcap\fR), in order to determine which special
1231 control sequences are needed to control various aspects of the terminal.
1232 \fBnew_GetLine()\fR for example, passes the return value of
1233 \fBgetenv\fR("TERM") in this argument. Note that if one or both of
1234 \fIinput_fp\fR and \fIoutput_fp\fR do not refer to a terminal, then it is legal
1235 to pass \fINULL\fR instead of a terminal type.
1236 .sp
1237 .LP
1238 Note that if you want to pass file descriptors to \fBgl_change_terminal()\fR,
1239 you can do this by creating \fBstdio\fR stream wrappers using the POSIX
1240 \fBfdopen\fR(3C) function.
1241 .SS "External Event Handling"
1242 .LP
1243 By default, \fBgl_get_line()\fR does not return until either a complete input
1244 line has been entered by the user, or an error occurs. In programs that need to
1245 watch for I/O from other sources than the terminal, there are two options.
1246 .RS +4
1247 .TP
1248 .ie t \(bu
1249 .el o
1250 Use the functions described in the \fBgl_io_mode\fR(3TECLA) manual page to
1251 switch \fBgl_get_line()\fR into non-blocking server mode. In this mode,
1252 \fBgl_get_line()\fR becomes a non-blocking, incremental line-editing function
1253 that can safely be called from an external event loop. Although this is a very
1254 versatile method, it involves taking on some responsibilities that are normally
1255 performed behind the scenes by \fBgl_get_line()\fR.
1256 .RE
1257 .RS +4
1258 .TP
1259 .ie t \(bu
1260 .el o
1261 While \fBgl_get_line()\fR is waiting for keyboard input from the user, you can
1262 ask it to also watch for activity on arbitrary file descriptors, such as


1363 particular signal you will need to both temporarily install your own signal
1364 handler, and unblock the signal. Be sure to re-block the signal (if it was
1365 originally blocked) and reinstate the original signal handler, if any, before
1366 returning.
1367 .sp
1368 .LP
1369 Your callback should not try to read from the terminal, which is left in raw
1370 mode as far as input is concerned. You can write to the terminal as usual,
1371 since features like conversion of newline to carriage-return/linefeed are
1372 re-enabled while the callback is running. If your callback function does write
1373 to the terminal, be sure to output a newline first, and when your callback
1374 returns, tell \fBgl_get_line()\fR that the input line needs to be redrawn, by
1375 returning the \fBGLFD_REFRESH\fR status code.
1376 .sp
1377 .LP
1378 To remove a callback function that you previously registered for a given file
1379 descriptor and event, simply call \fBgl_watch_fd()\fR with the same \fIfd\fR
1380 and \fIevent\fR arguments, but with a \fIcallback\fR argument of 0. The
1381 \fIdata\fR argument is ignored in this case.
1382 .SS "Setting An Inactivity Timeout"
1383 .LP
1384 The \fBgl_inactivity_timeout()\fR function can be used to set or cancel an
1385 inactivity timeout. Inactivity in this case refers both to keyboard input, and
1386 to I/O on any file descriptors registered by prior and subsequent calls to
1387 \fBgl_watch_fd()\fR.
1388 .sp
1389 .LP
1390 The timeout is specified in the form of an integral number of seconds and an
1391 integral number of nanoseconds, specified by the \fIsec\fR and \fInsec\fR
1392 arguments, respectively. Subsequently, whenever no activity is seen for this
1393 time period, the function specified by the \fIcallback\fR argument is called.
1394 The \fIdata\fR argument of \fBgl_inactivity_timeout()\fR is passed to this
1395 callback function whenever it is invoked, and can thus be used to pass
1396 arbitrary application-specific information to the callback. The following macro
1397 is provided in <\fBlibtecla.h\fR> for applications to use to declare and
1398 prototype timeout callback functions.
1399 .sp
1400 .in +2
1401 .nf
1402 #define GL_TIMEOUT_FN(fn) GlAfterTimeout (fn)(GetLine *gl, void *data)
1403 .fi


1457 Your callback should not try to read from the terminal, which is left in raw
1458 mode as far as input is concerned. You can however write to the terminal as
1459 usual, since features like conversion of newline to carriage-return/linefeed
1460 are re-enabled while the callback is running. If your callback function does
1461 write to the terminal, be sure to output a newline first, and when your
1462 callback returns, tell \fBgl_get_line()\fR that the input line needs to be
1463 redrawn, by returning the \fBGLTO_REFRESH\fR status code.
1464 .sp
1465 .LP
1466 Finally, note that although the timeout arguments include a nanosecond
1467 component, few computer clocks presently have resolutions that are finer than a
1468 few milliseconds, so asking for less than a few milliseconds is equivalent to
1469 requesting zero seconds on many systems. If this would be a problem, you should
1470 base your timeout selection on the actual resolution of the host clock (for
1471 example, by calling \fBsysconf\fR(\fB_SC_CLK_TCK\fR)).
1472 .sp
1473 .LP
1474 To turn off timeouts, simply call \fBgl_inactivity_timeout()\fR with a
1475 \fIcallback\fR argument of 0. The \fIdata\fR argument is ignored in this case.
1476 .SS "Signal Handling Defaults"
1477 .LP
1478 By default, the \fBgl_get_line()\fR function intercepts a number of signals.
1479 This is particularly important for signals that would by default terminate the
1480 process, since the terminal needs to be restored to a usable state before this
1481 happens. This section describes the signals that are trapped by default and how
1482 \fBgl_get_line()\fR responds to them. Changing these defaults is the topic of
1483 the following section.
1484 .sp
1485 .LP
1486 When the following subset of signals are caught, \fBgl_get_line()\fR first
1487 restores the terminal settings and signal handling to how they were before
1488 \fBgl_get_line()\fR was called, resends the signal to allow the calling
1489 application's signal handlers to handle it, then, if the process still exists,
1490 returns \fINULL\fR and sets \fBerrno\fR as specified below.
1491 .sp
1492 .ne 2
1493 .na
1494 \fB\fBSIGINT\fR\fR
1495 .ad
1496 .RS 11n
1497 This signal is generated both by the keyboard interrupt key (usually \fB^C\fR),


1695 .RS 13n
1696 This signal is generated if the program attempts to write to the terminal while
1697 the program is running in the background.
1698 .RE
1699 
1700 .sp
1701 .LP
1702 Obviously not all of the above signals are supported on all systems, so code to
1703 support them is conditionally compiled into the tecla library.
1704 .sp
1705 .LP
1706 Note that if \fBSIGKILL\fR or \fBSIGPOLL\fR, which by definition cannot be
1707 caught, or any of the hardware generated exception signals, such as
1708 \fBSIGSEGV\fR, \fBSIGBUS\fR, and \fBSIGFPE\fR, are received and unhandled while
1709 \fBgl_get_line()\fR has the terminal in raw mode, the program will be
1710 terminated without the terminal having been restored to a usable state. In
1711 practice, job-control shells usually reset the terminal settings when a process
1712 relinquishes the controlling terminal, so this is only a problem with older
1713 shells.
1714 .SS "Customized Signal Handling"
1715 .LP
1716 The previous section listed the signals that \fBgl_get_line()\fR traps by
1717 default, and described how it responds to them. This section describes how to
1718 both add and remove signals from the list of trapped signals, and how to
1719 specify how \fBgl_get_line()\fR should respond to a given signal.
1720 .sp
1721 .LP
1722 If you do not need \fBgl_get_line()\fR to do anything in response to a signal
1723 that it normally traps, you can tell to \fBgl_get_line()\fR to ignore that
1724 signal by calling \fBgl_ignore_signal()\fR.
1725 .sp
1726 .LP
1727 The \fIsigno\fR argument is the number of the signal (for example,
1728 \fBSIGINT\fR) that you want to have ignored. If the specified signal is not
1729 currently one of those being trapped, this function does nothing.
1730 .sp
1731 .LP
1732 The \fBgl_trap_signal()\fR function allows you to either add a new signal to
1733 the list that \fBgl_get_line()\fR traps or modify how it responds to a signal
1734 that it already traps.
1735 .sp


1848 .el o
1849 So that you do not need to end each output line with \er\en, instead of just
1850 \en.
1851 .RE
1852 .sp
1853 .LP
1854 The \fBGL_RESTORE_ENV\fR combination is the same as \fBGL_SUSPEND_INPUT\fR,
1855 except that it does not move the cursor. If your signal handler does not read
1856 or write anything to the terminal, the user will not see any visible indication
1857 that a signal was caught. This can be useful if you have a signal handler that
1858 only occasionally writes to the terminal, where using \fBGL_SUSPEND_LINE\fR
1859 would cause the input line to be unnecessarily duplicated when nothing had been
1860 written to the terminal. Such a signal handler, when it does write to the
1861 terminal, should be sure to start a new line at the start of its first write,
1862 by writing a new line before returning. If the signal arrives while the user is
1863 entering a line that only occupies a signal terminal line, or if the cursor is
1864 on the last terminal line of a longer input line, this will have the same
1865 effect as \fBGL_SUSPEND_INPUT\fR. Otherwise it will start writing on a line
1866 that already contains part of the displayed input line. This does not do any
1867 harm, but it looks a bit ugly, which is why the \fBGL_SUSPEND_INPUT\fR
1868 combination is better if you know that you are always going to be writting to
1869 the terminal.
1870 .sp
1871 .LP
1872 The \fIafter\fR argument, which determines what \fBgl_get_line()\fR does after
1873 the application's signal handler returns (if  it returns), can take any one of
1874 the following values:
1875 .sp
1876 .ne 2
1877 .na
1878 \fB\fBGLS_RETURN\fR\fR
1879 .ad
1880 .RS 16n
1881 Return the completed input line, just as though the user had pressed the return
1882 key.
1883 .RE
1884 
1885 .sp
1886 .ne 2
1887 .na
1888 \fB\fBGLS_ABORT\fR\fR


1896 .RE
1897 
1898 .sp
1899 .ne 2
1900 .na
1901 \fB\fBGLS_CONTINUE\fR\fR
1902 .ad
1903 .RS 16n
1904 Resume command line editing.
1905 .RE
1906 
1907 .sp
1908 .LP
1909 The \fIerrno_value\fR argument is intended to be combined with the
1910 \fBGLS_ABORT\fR option, telling \fBgl_get_line()\fR what to set the standard
1911 \fBerrno\fR variable to before returning \fINULL\fR to the calling program. It
1912 can also, however, be used with the \fBGL_RETURN\fR option, in case you want to
1913 have a way to distinguish between an input line that was entered using the
1914 return key, and one that was entered by the receipt of a signal.
1915 .SS "Reliable Signal Handling"
1916 .LP
1917 Signal handling is surprisingly hard to do reliably without race conditions. In
1918 \fBgl_get_line()\fR a lot of care has been taken to allow applications to
1919 perform reliable signal handling around \fBgl_get_line()\fR. This section
1920 explains how to make use of this.
1921 .sp
1922 .LP
1923 As an example of the problems that can arise if the application is not written
1924 correctly, imagine that one's application has a \fBSIGINT\fR signal handler
1925 that sets a global flag. Now suppose that the application tests this flag just
1926 before invoking \fBgl_get_line()\fR. If a \fBSIGINT\fR signal happens to be
1927 received in the small window of time between the statement that tests the value
1928 of this flag, and the statement that calls \fBgl_get_line()\fR, then
1929 \fBgl_get_line()\fR will not see the signal, and will not be interrupted. As a
1930 result, the application will not be able to respond to the signal until the
1931 user gets around to finishing entering the input line and \fBgl_get_line()\fR
1932 returns. Depending on the application, this might or might not be a disaster,
1933 but at the very least it would puzzle the user.
1934 .sp
1935 .LP
1936 The way to avoid such problems is to do the following.


2110 \fBgl_get_line()\fR blocks delivery of the above signal again.
2111 .RE
2112 .RS +4
2113 .TP
2114 6.
2115 It then undoes any actions performed in the first of the above steps and
2116 redisplays the line, if the signal configuration calls for this.
2117 .RE
2118 .RS +4
2119 .TP
2120 7.
2121 \fBgl_get_line()\fR then either resumes trying to read a character, or
2122 aborts, depending on the configuration of the signal that was caught.
2123 .RE
2124 .sp
2125 .LP
2126 What the above steps do in essence is to take asynchronously delivered signals
2127 and handle them synchronously, one at a time, at a point in the code where
2128 \fBgl_get_line()\fR has complete control over its environment.
2129 .SS "The Terminal Size"
2130 .LP
2131 On most systems the combination of the \fBTIOCGWINSZ\fR ioctl and the
2132 \fBSIGWINCH\fR signal is used to maintain an accurate idea of the terminal
2133 size. The terminal size is newly queried every time that \fBgl_get_line()\fR is
2134 called and whenever a \fBSIGWINCH\fR signal is received.
2135 .sp
2136 .LP
2137 On the few systems where this mechanism is not available, at startup
2138 \fBnew_GetLine()\fR first looks for the \fBLINES\fR and \fBCOLUMNS\fR
2139 environment variables. If these are not found, or they contain unusable values,
2140 then if a terminal information database like \fBterminfo\fR or \fBtermcap\fR is
2141 available, the default size of the terminal is looked up in this database. If
2142 this too fails to provide the terminal size, a default size of 80 columns by 24
2143 lines is used.
2144 .sp
2145 .LP
2146 Even on systems that do support ioctl(\fBTIOCGWINSZ\fR), if the terminal is on
2147 the other end of a serial line, the terminal driver generally has no way of
2148 detecting when a resize occurs or of querying what the current size is. In such
2149 cases no \fBSIGWINCH\fR is sent to the process, and the dimensions returned by
2150 ioctl(\fBTIOCGWINSZ\fR) are not correct. The only way to handle such instances


2161 In non-blocking server-I/O mode, if a line is currently being input, the input
2162 line is then redrawn to accommodate the changed size. Finally the new values are
2163 recorded in \fIgl\fR for future use by \fBgl_get_line()\fR.
2164 .sp
2165 .LP
2166 The \fBgl_terminal_size()\fR function allows you to query the current size of
2167 the terminal, and install an alternate fallback size for cases where the size
2168 is not available. Beware that the terminal size will not be available if
2169 reading from a pipe or a file, so the default values can be important even on
2170 systems that do support ways of finding out the terminal size.
2171 .sp
2172 .LP
2173 This function first updates \fBgl_get_line()\fR's fallback terminal dimensions,
2174 then records its findings in the return value.
2175 .sp
2176 .LP
2177 The \fIdef_ncolumn\fR and \fIdef_nline\fR arguments specify the default number
2178 of terminal columns and lines to use if the terminal size cannot be determined
2179 by ioctl(\fBTIOCGWINSZ\fR) or environment variables.
2180 .SS "Hiding What You Type"
2181 .LP
2182 When entering sensitive information, such as passwords, it is best not to have
2183 the text that you are entering echoed on the terminal. Furthermore, such text
2184 should not be recorded in the history list, since somebody finding your
2185 terminal unattended could then recall it, or somebody snooping through your
2186 directories could see it in your history file. With this in mind, the
2187 \fBgl_echo_mode()\fR function allows you to toggle on and off the display and
2188 archival of any text that is subsequently entered in calls to
2189 \fBgl_get_line()\fR.
2190 .sp
2191 .LP
2192 The \fIenable\fR argument specifies whether entered text should be visible or
2193 not. If it is 0, then subsequently entered lines will not be visible on the
2194 terminal, and will not be recorded in the history list. If it is 1, then
2195 subsequent input lines will be displayed as they are entered, and provided that
2196 history has not been turned off with a call to \fBgl_toggle_history()\fR, then
2197 they will also be archived in the history list. Finally, if the enable argument
2198 is -1, then the echoing mode is left unchanged, which allows you to
2199 non-destructively query the current setting through the return value. In all
2200 cases, the return value of the function is 0 if echoing was disabled before the
2201 function was called, and 1 if it was enabled.
2202 .sp
2203 .LP
2204 When echoing is turned off, note that although tab completion will invisibly
2205 complete your prefix as far as possible, ambiguous completions will not be
2206 displayed.
2207 .SS "Single Character Queries"
2208 .LP
2209 Using \fBgl_get_line()\fR to query the user for a single character reply, is
2210 inconvenient for the user, since they must hit the enter or return key before
2211 the character that they typed is returned to the program. Thus the
2212 \fBgl_query_char()\fR function has been provided for single character queries
2213 like this.
2214 .sp
2215 .LP
2216 This function displays the specified prompt at the start of a new line, and
2217 waits for the user to type a character. When the user types a character,
2218 \fBgl_query_char()\fR displays it to the right of the prompt, starts a newline,
2219 then returns the character to the calling program. The return value of the
2220 function is the character that was typed. If the read had to be aborted for
2221 some reason, EOF is returned instead. In the latter case, the application can
2222 call the previously documented \fBgl_return_status()\fR, to find out what went
2223 wrong. This could, for example, have been the reception of a signal, or the
2224 optional inactivity timer going off.
2225 .sp
2226 .LP
2227 If the user simply hits enter, the value of the \fIdefchar\fR argument is
2228 substituted. This means that when the user hits either newline or return, the


2236 symbolically. For example, control-A is displayed as \fB^A\fR, and characters
2237 beyond 127 are displayed in octal, preceded by a backslash.
2238 .sp
2239 .LP
2240 As with \fBgl_get_line()\fR, echoing of the entered character can be disabled
2241 using the \fBgl_echo_mode()\fR function.
2242 .sp
2243 .LP
2244 If the calling process is suspended while waiting for the user to type their
2245 response, the cursor is moved to the line following the prompt line, then when
2246 the process resumes, the prompt is redisplayed, and \fBgl_query_char()\fR
2247 resumes waiting for the user to type a character.
2248 .sp
2249 .LP
2250 Note that in non-blocking server mode, if an incomplete input line is in the
2251 process of being read when \fBgl_query_char()\fR is called, the partial input
2252 line is discarded, and erased from the terminal, before the new prompt is
2253 displayed. The next call to \fBgl_get_line()\fR will thus start editing a new
2254 line.
2255 .SS "Reading Raw Characters"
2256 .LP
2257 Whereas the \fBgl_query_char()\fR function visibly prompts the user for a
2258 character, and displays what they typed, the \fBgl_read_char()\fR function
2259 reads a signal character from the user, without writing anything to the
2260 terminal, or perturbing any incompletely entered input line. This means that it
2261 can be called not only from between calls to \fBgl_get_line()\fR, but also from
2262 callback functions that the application has registered to be called by
2263 \fBgl_get_line()\fR.
2264 .sp
2265 .LP
2266 On success, the return value of \fBgl_read_char()\fR is the character that was
2267 read. On failure, EOF is returned, and the \fBgl_return_status()\fR function
2268 can be called to find out what went wrong. Possibilities include the optional
2269 inactivity timer going off, the receipt of a signal that is configured to abort
2270 \fBgl_get_line()\fR, or terminal I/O blocking, when in non-blocking server-I/O
2271 mode.
2272 .sp
2273 .LP
2274 Beware that certain keyboard keys, such as function keys, and cursor keys,
2275 usually generate at least three characters each, so a single call to
2276 \fBgl_read_char()\fR will not be enough to identify such keystrokes.
2277 .SS "Clearing The Terminal"
2278 .LP
2279 The calling program can clear the terminal by calling
2280 \fBgl_erase_terminal()\fR. In non-blocking server-I/O mode, this function also
2281 arranges for the current input line to be redrawn from scratch when
2282 \fBgl_get_line()\fR is next called.
2283 .SS "Displaying Text Dynamically"
2284 .LP
2285 Between calls to \fBgl_get_line()\fR, the \fBgl_display_text()\fR function
2286 provides a convenient way to display paragraphs of text, left-justified and
2287 split over one or more terminal lines according to the constraints of the
2288 current width of the terminal. Examples of the use of this function may be
2289 found in the demo programs, where it is used to display introductions. In those
2290 examples the advanced use  of optional prefixes, suffixes and filled lines to
2291 draw a box around the text is also illustrated.
2292 .sp
2293 .LP
2294 If \fIgl\fR is not currently connected to a terminal, for example if the output
2295 of a program that uses \fBgl_get_line()\fR is being piped to another program or
2296 redirected to a file, then the value of the \fIdef_width\fR parameter is used
2297 as the terminal width.
2298 .sp
2299 .LP
2300 The \fIindentation\fR argument specifies the number of characters to use to
2301 indent each line of output. The \fIfill_char\fR argument specifies the character
2302 that will be used to perform this indentation.
2303 .sp
2304 .LP


2307 argument can be either \fINULL\fR or a string to place at the end of each line.
2308 The suffix is placed flush against the right edge of the terminal, and any
2309 space between its first character and the last word on that line is filled with
2310 the character specified by the \fIfill_char\fR argument. Normally the
2311 fill-character is a space.
2312 .sp
2313 .LP
2314 The \fIstart\fR argument tells \fBgl_display_text()\fR how many characters have
2315 already been written to the current terminal line, and thus tells it the
2316 starting column index of the cursor. Since the return value of
2317 \fBgl_display_text()\fR is the ending column index of the cursor, by passing
2318 the return value of one call to the start argument of the next call, a
2319 paragraph that is broken between more than one string can be composed by
2320 calling \fBgl_display_text()\fR for each successive portion of the paragraph.
2321 Note that literal newline characters are necessary at the end of each paragraph
2322 to force a new line to be started.
2323 .sp
2324 .LP
2325 On error, \fBgl_display_text()\fR returns -1.
2326 .SS "Callback Function Facilities"
2327 .LP
2328 Unless otherwise stated, callback functions such as tab completion callbacks
2329 and event callbacks should not call any functions in this module. The following
2330 functions, however, are designed specifically to be used by callback functions.
2331 .sp
2332 .LP
2333 Calling the \fBgl_replace_prompt()\fR function from a callback tells
2334 \fBgl_get_line()\fR to display a different prompt when the callback returns.
2335 Except in non-blocking server mode, it has no effect if used between calls to
2336 \fBgl_get_line()\fR. In non-blocking server mode, when used between two calls
2337 to \fBgl_get_line()\fR that are operating on the same input line, the current
2338 input line will be re-drawn with the new prompt on the following call to
2339 \fBgl_get_line()\fR.
2340 .SS "International Character Sets"
2341 .LP
2342 Since \fBlibtecla\fR(3LIB) version 1.4.0, \fBgl_get_line()\fR has been 8-bit
2343 clean. This means that all 8-bit characters that are printable in the user's
2344 current locale are now displayed verbatim and included in the returned input
2345 line. Assuming that the calling program correctly contains a call like the
2346 following,
2347 .sp
2348 .in +2
2349 .nf
2350 setlocale(LC_CTYPE, "")
2351 .fi
2352 .in -2
2353 
2354 .sp
2355 .LP
2356 then the current locale is determined by the first of the environment variables
2357 \fBLC_CTYPE\fR, \fBLC_ALL\fR, and \fBLANG\fR that is found to contain a valid
2358 locale name. If none of these variables are defined, or the program neglects to
2359 call \fBsetlocale\fR(3C), then the default C locale is used, which is US 7-bit
2360 ASCII. On most UNIX-like platforms, you can get a list of valid locales by
2361 typing the command:
2362 .sp
2363 .in +2
2364 .nf
2365 locale -a
2366 .fi
2367 .in -2
2368 .sp
2369 
2370 .sp
2371 .LP
2372 at the shell prompt. Further documentation on how the user can make use of this
2373 to enter international characters can be found in the \fBtecla\fR(5) man page.
2374 .SS "Thread Safety"
2375 .LP
2376 Unfortunately neither \fBterminfo\fR nor \fBtermcap\fR were designed to be
2377 reentrant, so you cannot safely use the functions of the getline module in
2378 multiple threads (you can use the separate file-expansion and word-completion
2379 modules in multiple threads, see the corresponding man pages for details).
2380 However due to the use of POSIX reentrant functions for looking up home
2381 directories, it is safe to use this module from a single thread of a
2382 multi-threaded program, provided that your other threads do not use any
2383 \fBtermcap\fR or \fBterminfo\fR functions.
2384 .SH ATTRIBUTES
2385 .LP
2386 See \fBattributes\fR(5) for descriptions of the following attributes:
2387 .sp
2388 
2389 .sp
2390 .TS
2391 box;
2392 c | c
2393 l | l .
2394 ATTRIBUTE TYPE  ATTRIBUTE VALUE
2395 _
2396 Interface Stability     Committed
2397 _
2398 MT-Level        MT-Safe
2399 .TE
2400 
2401 .SH SEE ALSO
2402 .LP
2403 \fBcpl_complete_word\fR(3TECLA), \fBef_expand_file\fR(3TECLA),
2404 \fBgl_io_mode\fR(3TECLA), \fBlibtecla\fR(3LIB), \fBpca_lookup_file\fR(3TECLA),
2405 \fBattributes\fR(5), \fBtecla\fR(5)


   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.
  12 .\"
  13 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14 .\" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
  16 .\" OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  17 .\" HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  18 .\" INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  19 .\" FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  20 .\" NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  21 .\" WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22 .\"
  23 .\" Except as contained in this notice, the name of a copyright holder
  24 .\" shall not be used in advertising or otherwise to promote the sale, use
  25 .\" or other dealings in this Software without prior written authorization
  26 .\" of the copyright holder.
  27 .\" Portions Copyright (c) 2007, Sun Microsystems, Inc. All Rights Reserved.
  28 .TH GL_GET_LINE 3TECLA "January 18, 2020"
  29 .SH NAME
  30 gl_get_line, new_GetLine, del_GetLine, gl_customize_completion,
  31 gl_change_terminal, gl_configure_getline, gl_load_history, gl_save_history,
  32 gl_group_history, gl_show_history, gl_watch_fd, gl_inactivity_timeout,
  33 gl_terminal_size, gl_set_term_size, gl_resize_history, gl_limit_history,
  34 gl_clear_history, gl_toggle_history, gl_lookup_history, gl_state_of_history,
  35 gl_range_of_history, gl_size_of_history, gl_echo_mode, gl_replace_prompt,
  36 gl_prompt_style, gl_ignore_signal, gl_trap_signal, gl_last_signal,
  37 gl_completion_action, gl_register_action, gl_display_text, gl_return_status,
  38 gl_error_message, gl_catch_blocked, gl_list_signals, gl_bind_keyseq,
  39 gl_erase_terminal, gl_automatic_history, gl_append_history, gl_query_char,
  40 gl_read_char \- allow the user to compose an input line
  41 .SH SYNOPSIS

  42 .nf
  43 cc [ \fIflag\fR\&.\|.\|. ] \fIfile\fR\&.\|.\|. \fB-ltecla\fR [ \fIlibrary\fR\&.\|.\|. ]
  44 #include <stdio.h>
  45 #include <libtecla.h>
  46 
  47 \fBGetLine *\fR\fBnew_GetLine\fR(\fBsize_t\fR \fIlinelen\fR, \fBsize_t\fR \fIhistlen\fR);
  48 .fi
  49 
  50 .LP
  51 .nf
  52 \fBGetLine *\fR\fBdel_GetLine\fR(\fBGetLine *\fR\fIgl\fR);
  53 .fi
  54 
  55 .LP
  56 .nf
  57 \fBchar *\fR\fBgl_get_line\fR(\fBGetLine *\fR\fIgl\fR, \fBconst char *\fR\fIprompt\fR,
  58      \fBconst char *\fR\fIstart_line\fR, \fBint\fR \fIstart_pos\fR);
  59 .fi
  60 
  61 .LP


 249 .nf
 250 \fBint\fR \fBgl_list_signals\fR(\fBGetLine *\fR\fIgl\fR, \fBsigset_t *\fR\fIset\fR);
 251 .fi
 252 
 253 .LP
 254 .nf
 255 \fBint\fR \fBgl_append_history\fR(\fBGetLine *\fR\fIgl\fR, \fBconst char *\fR\fIline\fR);
 256 .fi
 257 
 258 .LP
 259 .nf
 260 \fBint\fR \fBgl_automatic_history\fR(\fBGetLine *\fR\fIgl\fR, \fBint\fR \fIenable\fR);
 261 .fi
 262 
 263 .LP
 264 .nf
 265 \fBint\fR \fBgl_erase_terminal\fR(\fBGetLine *\fR\fIgl\fR);
 266 .fi
 267 
 268 .SH DESCRIPTION

 269 The \fBgl_get_line()\fR function is part of the \fBlibtecla\fR(3LIB) library.
 270 If the user is typing at a terminal, each call prompts them for a line of
 271 input, then provides interactive editing facilities, similar to those of the
 272 UNIX \fBtcsh\fR shell. In addition to simple command-line editing, it supports
 273 recall of previously entered command lines, TAB completion of file names, and
 274 in-line wild-card expansion of filenames. Documentation of both the user-level
 275 command-line editing features and all user configuration options can be found
 276 on the \fBtecla\fR(5) manual page.
 277 .SS "An Example"

 278 The following shows a complete example of how to use the \fBgl_get_line()\fR
 279 function to get input from the user:
 280 .sp
 281 .in +2
 282 .nf
 283 #include <stdio.h>
 284 #include <locale.h>
 285 #include <libtecla.h>
 286 
 287 int main(int argc, char *argv[])
 288 {
 289   char *line;    /* The line that the user typed */
 290   GetLine *gl;   /* The gl_get_line() resource object */
 291 
 292   setlocale(LC_CTYPE, ""); /* Adopt the user's choice */
 293                            /* of character set. */
 294 
 295   gl = new_GetLine(1024, 2048);
 296   if(!gl)
 297     return 1;


 302   gl = del_GetLine(gl);
 303   return 0;
 304 }
 305 .fi
 306 .in -2
 307 
 308 .sp
 309 .LP
 310 In the example, first the resources needed by the \fBgl_get_line()\fR function
 311 are created by calling \fBnew_GetLine()\fR. This allocates the memory used in
 312 subsequent calls to the \fBgl_get_line()\fR function, including the history
 313 buffer for recording previously entered lines. Then one or more lines are read
 314 from the user, until either an error occurs, or the user types exit. Then
 315 finally the resources that were allocated by \fBnew_GetLine()\fR, are returned
 316 to the system by calling \fBdel_GetLine()\fR. Note the use of the \fINULL\fR
 317 return value of \fBdel_GetLine()\fR to make \fIgl\fR \fINULL\fR. This is a
 318 safety precaution. If the program subsequently attempts to pass \fIgl\fR to
 319 \fBgl_get_line()\fR, said function will complain, and return an error, instead
 320 of attempting to use the deleted resource object.
 321 .SS "The Functions Used In The Example"

 322 The \fBnew_GetLine()\fR function creates the resources used by the
 323 \fBgl_get_line()\fR function and returns an opaque pointer to the object that
 324 contains them. The maximum length of an input line is specified by the
 325 \fIlinelen\fR argument, and the number of bytes to allocate for storing history
 326 lines is set by the \fIhistlen\fR argument. History lines are stored
 327 back-to-back in a single buffer of this size. Note that this means that the
 328 number of history lines that can be stored at any given time, depends on the
 329 lengths of the individual lines. If you want to place an upper limit on the
 330 number of lines that can be stored, see the description of the
 331 \fBgl_limit_history()\fR function. If you do not want history at all, specify
 332 \fIhistlen\fR as zero, and no history buffer will be allocated.
 333 .sp
 334 .LP
 335 On error, a message is printed to \fBstderr\fR and \fINULL\fR is returned.
 336 .sp
 337 .LP
 338 The \fBdel_GetLine()\fR function deletes the resources that were returned by a
 339 previous call to \fBnew_GetLine()\fR. It always returns \fINULL\fR (for
 340 example, a deleted object). It does nothing if the \fIgl\fR argument is
 341 \fINULL\fR.


 354 pass the desired string with the \fIstart_line\fR argument. You can then
 355 specify which character of this line the cursor is initially positioned over by
 356 using the \fIstart_pos\fR argument. This should be -1 if you want the cursor to
 357 follow the last character of the start line. If you do not want to preload the
 358 line in this manner, send \fIstart_line\fR as \fINULL\fR, and set
 359 \fIstart_pos\fR to -1.
 360 .sp
 361 .LP
 362 The \fBgl_get_line()\fR function returns a pointer to the line entered by the
 363 user, or \fINULL\fR on error or at the end of the input. The returned pointer
 364 is part of the specified \fIgl\fR resource object, and thus should not be freed
 365 by the caller, or assumed to be unchanging from one call to the next. When
 366 reading from a user at a terminal, there will always be a newline character at
 367 the end of the returned line. When standard input is being taken from a pipe or
 368 a file, there will similarly be a newline unless the input line was too long to
 369 store in the internal buffer. In the latter case you should call
 370 \fBgl_get_line()\fR again to read the rest of the line. Note that this behavior
 371 makes \fBgl_get_line()\fR similar to \fBfgets\fR(3C). When \fBstdin\fR is not
 372 connected to a terminal, \fBgl_get_line()\fR simply calls \fBfgets()\fR.
 373 .SS "The Return Status Of \fBgl_get_line()\fR"

 374 The \fBgl_get_line()\fR function has two possible return values: a pointer to
 375 the completed input line, or \fINULL\fR. Additional information about what
 376 caused \fBgl_get_line()\fR to return is available both by inspecting
 377 \fBerrno\fR and by calling the \fBgl_return_status()\fR function.
 378 .sp
 379 .LP
 380 The following are the possible enumerated values returned by
 381 \fBgl_return_status()\fR:
 382 .sp
 383 .ne 2
 384 .na
 385 \fB\fBGLR_NEWLINE\fR\fR
 386 .ad
 387 .RS 15n
 388 The last call to \fBgl_get_line()\fR successfully returned a completed input
 389 line.
 390 .RE
 391 
 392 .sp
 393 .ne 2


 411 of \fBGLS_ABORT\fR. See \fBgl_trap_signal()\fR.
 412 .RE
 413 
 414 .sp
 415 .ne 2
 416 .na
 417 \fB\fBGLR_TIMEOUT\fR\fR
 418 .ad
 419 .RS 15n
 420 The inactivity timer expired while \fBgl_get_line()\fR was waiting for input,
 421 and the timeout callback function returned \fBGLTO_ABORT\fR. See
 422 \fBgl_inactivity_timeout()\fR for information about timeouts.
 423 .RE
 424 
 425 .sp
 426 .ne 2
 427 .na
 428 \fB\fBGLR_FDABORT\fR\fR
 429 .ad
 430 .RS 15n
 431 An application I/O callback returned \fBGLFD_ABORT\fR. See
 432 \fBgl_watch_fd()\fR.
 433 .RE
 434 
 435 .sp
 436 .ne 2
 437 .na
 438 \fB\fBGLR_EOF\fR\fR
 439 .ad
 440 .RS 15n
 441 End of file reached. This can happen when input is coming from a file or a
 442 pipe, instead of the terminal. It also occurs if the user invokes the
 443 list-or-eof or del-char-or-list-or-eof actions at the start of a new line.
 444 .RE
 445 
 446 .sp
 447 .ne 2
 448 .na
 449 \fB\fBGLR_ERROR\fR\fR
 450 .ad
 451 .RS 15n


 453 and/or \fBgl_error_message()\fR for details.
 454 .RE
 455 
 456 .sp
 457 .LP
 458 When \fBgl_return_status()\fR returns \fBGLR_ERROR\fR and the value of
 459 \fBerrno\fR is not sufficient to explain what happened, you can use the
 460 \fBgl_error_message()\fR function to request a description of the last error
 461 that occurred.
 462 .sp
 463 .LP
 464 The return value of \fBgl_error_message()\fR is a pointer to the message that
 465 occurred. If the \fIbuff\fR argument is \fINULL\fR, this will be a pointer to a
 466 buffer within \fIgl\fR whose value will probably change on the next call to any
 467 function associated with \fBgl_get_line()\fR. Otherwise, if a non-null
 468 \fIbuff\fR argument is provided, the error message, including a '\e0'
 469 terminator, will be written within the first \fIn\fR elements of this buffer,
 470 and the return value will be a pointer to the first element of this buffer. If
 471 the message will not fit in the provided buffer, it will be truncated to fit.
 472 .SS "Optional Prompt Formatting"

 473 Whereas by default the prompt string that you specify is displayed literally
 474 without any special interpretation of the characters within it, the
 475 \fBgl_prompt_style()\fR function can be used to enable optional formatting
 476 directives within the prompt.
 477 .sp
 478 .LP
 479 The \fIstyle\fR argument, which specifies the formatting style, can take any of
 480 the following values:
 481 .sp
 482 .ne 2
 483 .na
 484 \fB\fBGL_FORMAT_PROMPT\fR\fR
 485 .ad
 486 .RS 21n
 487 In this style, the formatting directives described below, when included in
 488 prompt strings, are interpreted as follows:
 489 .sp
 490 .ne 2
 491 .na
 492 \fB\fB%B\fR\fR


 609 Note that although a pair of characters that starts with a % character, but
 610 does not match any of the above directives is displayed literally, if a new
 611 directive is subsequently introduced which does match, the displayed prompt
 612 will change, so it is better to always use %% to display a literal %.
 613 .sp
 614 Also note that not all terminals support all of these text attributes, and that
 615 some substitute a different attribute for missing ones.
 616 .RE
 617 
 618 .sp
 619 .ne 2
 620 .na
 621 \fB\fBGL_LITERAL_PROMPT\fR\fR
 622 .ad
 623 .RS 21n
 624 In this style, the prompt string is printed literally. This is the default
 625 style.
 626 .RE
 627 
 628 .SS "Alternate Configuration Sources"

 629 By default users have the option of configuring the behavior of
 630 \fBgl_get_line()\fR with a configuration file called \fB\&.teclarc\fR in their
 631 home directories. The fact that all applications share this same configuration
 632 file is both an advantage and a disadvantage. In most cases it is an advantage,
 633 since it encourages uniformity, and frees the user from having to configure
 634 each application separately. In some applications, however, this single means
 635 of configuration is a problem. This is particularly true of embedded software,
 636 where there's no filesystem to read a configuration file from, and also in
 637 applications where a radically different choice of keybindings is needed to
 638 emulate a legacy keyboard interface. To cater for such cases, the
 639 \fBgl_configure_getline()\fR function allows the application to control where
 640 configuration information is read from.
 641 .sp
 642 .LP
 643 The \fBgl_configure_getline()\fR function allows the configuration commands
 644 that would normally be read from a user's \fB~/.teclarc\fR file, to be read
 645 from any or none of, a string, an application specific configuration file,
 646 and/or a user-specific configuration file. If this function is called before
 647 the first call to \fBgl_get_line()\fR, the default behavior of reading
 648 \fB~/.teclarc\fR on the first call to \fBgl_get_line()\fR is disabled, so all


 657 file. If user_file != \fINULL\fR then it is interpreted as the full path name
 658 of a user-specific configuration file, such as \fB~/.teclarc\fR. For example,
 659 in the call
 660 .sp
 661 .in +2
 662 .nf
 663 gl_configure_getline(gl, "edit-mode vi \en nobeep",
 664                      "/usr/share/myapp/teclarc", "~/.teclarc");
 665 .fi
 666 .in -2
 667 
 668 .sp
 669 .LP
 670 The \fIapp_string\fR argument causes the calling application to start in
 671 \fBvi\fR(1) edit-mode, instead of the default \fBemacs\fR mode, and turns off
 672 the use of the terminal bell by the library. It then attempts to read
 673 system-wide configuration commands from an optional file called
 674 \fB/usr/share/myapp/teclarc\fR, then finally reads user-specific configuration
 675 commands from an optional \fB\&.teclarc\fR file in the user's home directory.
 676 Note that the arguments are listed in ascending order of priority, with the
 677 contents of \fIapp_string\fR being potentially overridden by commands in
 678 \fIapp_file\fR, and commands in \fIapp_file\fR potentially being overridden by
 679 commands in \fIuser_file\fR.
 680 .sp
 681 .LP
 682 You can call this function as many times as needed, the results being
 683 cumulative, but note that copies of any file names specified with the
 684 \fIapp_file\fR and \fIuser_file\fR arguments are recorded internally for
 685 subsequent use by the read-init-files key-binding function, so if you plan to
 686 call this function multiple times, be sure that the last call specifies the
 687 filenames that you want re-read when the user requests that the configuration
 688 files be re-read.
 689 .sp
 690 .LP
 691 Individual key sequences can also be bound and unbound using the
 692 \fBgl_bind_keyseq()\fR function. The \fIorigin\fR argument specifies the
 693 priority of the binding, according to whom it is being established for, and
 694 must be one of the following two values.
 695 .sp
 696 .ne 2
 697 .na


 707 \fB\fBGL_APP_KEY\fR\fR
 708 .ad
 709 .RS 15n
 710 This is a default binding set by the application.
 711 .RE
 712 
 713 .sp
 714 .LP
 715 When both user and application bindings for a given key sequence have been
 716 specified, the user binding takes precedence. The application's binding is
 717 subsequently reinstated if the user's binding is later unbound with either
 718 another call to this function, or a call to \fBgl_configure_getline()\fR.
 719 .sp
 720 .LP
 721 The \fIkeyseq\fR argument specifies the key sequence to be bound or unbound,
 722 and is expressed in the same way as in a \fB~/.teclarc\fR configuration file.
 723 The \fIaction\fR argument must either be a string containing the name of the
 724 action to bind the key sequence to, or it must be \fINULL\fR or \fB""\fR to
 725 unbind the key sequence.
 726 .SS "Customized Word Completion"

 727 If in your application you would like to have TAB completion complete other
 728 things in addition to or instead of filenames, you can arrange this by
 729 registering an alternate completion callback function with a call to the
 730 \fBgl_customize_completion()\fR function.
 731 .sp
 732 .LP
 733 The \fIdata\fR argument provides a way for your application to pass arbitrary,
 734 application-specific information to the callback function. This is passed to
 735 the callback every time that it is called. It might for example point to the
 736 symbol table from which possible completions are to be sought. The
 737 \fImatch_fn\fR argument specifies the callback function to be called. The
 738 \fICplMatchFn\fR function type is defined in <\fBlibtecla.h\fR>, as is a
 739 \fBCPL_MATCH_FN()\fR macro that you can use to declare and prototype callback
 740 functions. The declaration and responsibilities of callback functions are
 741 described in depth on the \fBcpl_complete_word\fR(3TECLA) manual page.
 742 .sp
 743 .LP
 744 The callback function is responsible for looking backwards in the input line
 745 from the point at which the user pressed TAB, to find the start of the word
 746 being completed. It then must lookup possible completions of this word, and
 747 record them one by one in the \fBWordCompletion\fR object that is passed to it
 748 as an argument, by calling the \fBcpl_add_completion()\fR function. If the
 749 callback function wants to provide filename completion in addition to its own
 750 specific completions, it has the option of itself calling the builtin filename
 751 completion callback. This is also documented in the
 752 \fBcpl_complete_word\fR(3TECLA) manual page.
 753 .sp
 754 .LP
 755 If you would like \fBgl_get_line()\fR to return the current input line when a
 756 successful completion has been made, you can arrange this when you call
 757 \fBcpl_add_completion()\fR by making the last character of the continuation
 758 suffix a newline character. The input line will be updated to display the
 759 completion, together with any continuation suffix up to the newline character,
 760 and \fBgl_get_line()\fR will return this input line.
 761 .sp
 762 .LP
 763 If your callback function needs to write something to the terminal, it must
 764 call \fBgl_normal_io()\fR before doing so. This will start a new line after the
 765 input line that is currently being edited, reinstate normal terminal I/O, and
 766 notify \fBgl_get_line()\fR that the input line will need to be redrawn when the
 767 callback returns.
 768 .SS "Adding Completion Actions"

 769 In the previous section the ability to customize the behavior of the only
 770 default completion action, complete-word, was described. In this section the
 771 ability to install additional action functions, so that different types of word
 772 completion can be bound to different key sequences, is described. This is
 773 achieved by using the \fBgl_completion_action()\fR function.
 774 .sp
 775 .LP
 776 The \fIdata\fR and \fImatch_fn\fR arguments are as described on the
 777 \fBcpl_complete_word\fR(3TECLA) manual page, and specify the callback function
 778 that should be invoked to identify possible completions. The \fIlist_only\fR
 779 argument determines whether the action that is being defined should attempt to
 780 complete the word as far as possible in the input line before displaying any
 781 possible ambiguous completions, or whether it should simply display the list of
 782 possible completions without touching the input line. The former option is
 783 selected by specifying a value of 0, and the latter by specifying a value of 1.
 784 The \fIname\fR argument specifies the name by which configuration files and
 785 future invocations of this function should refer to the action. This must
 786 either be the name of an existing completion action to be changed, or be a new
 787 unused name for a new action. Finally, the \fIkeyseq\fR argument specifies the
 788 default key sequence to bind the action to. If this is \fINULL\fR, no new key
 789 sequence will be bound to the action.
 790 .sp
 791 .LP
 792 Beware that in order for the user to be able to change the key sequence that is
 793 bound to actions that are installed in this manner, you should call
 794 \fBgl_completion_action()\fR to install a given action for the first time
 795 between calling \fBnew_GetLine()\fR and the first call to \fBgl_get_line()\fR.
 796 Otherwise, when the user's configuration file is read on the first call to
 797 \fBgl_get_line()\fR, the name of the your additional action will not be known,
 798 and any reference to it in the configuration file will generate an error.
 799 .sp
 800 .LP
 801 As discussed for \fBgl_customize_completion()\fR, if your callback function
 802 needs to write anything to the terminal, it must call \fBgl_normal_io()\fR
 803 before doing so.
 804 .SS "Defining Custom Actions"

 805 Although the built-in key-binding actions are sufficient for the needs of most
 806 applications, occasionally a specialized application may need to define one or
 807 more custom actions, bound to application-specific key sequences. For example,
 808 a sales application would benefit from having a key sequence that displayed the
 809 part name that corresponded to a part number preceding the cursor. Such a
 810 feature is clearly beyond the scope of the built-in action functions. So for
 811 such special cases, the \fBgl_register_action()\fR function is provided.
 812 .sp
 813 .LP
 814 The \fBgl_register_action()\fR function lets the application register an
 815 external function, \fIfn\fR, that will thereafter be called whenever either the
 816 specified key sequence, \fIkeyseq\fR, is entered by the user, or the user
 817 enters any other key sequence that the user subsequently binds to the specified
 818 action name, \fIname\fR, in their configuration file. The \fIdata\fR argument
 819 can be a pointer to anything that the application wants to have passed to the
 820 action function, \fIfn\fR, whenever that function is invoked.
 821 .sp
 822 .LP
 823 The action function, \fIfn\fR, should be declared using the
 824 \fBGL_ACTION_FN()\fR macro, which is defined in <\fBlibtecla.h\fR>.


 910 Cause \fBgl_get_line()\fR to return the completed input line
 911 .RE
 912 
 913 .sp
 914 .ne 2
 915 .na
 916 \fB\fBGLA_CONTINUE\fR\fR
 917 .ad
 918 .RS 16n
 919 Resume command-line editing.
 920 .RE
 921 
 922 .sp
 923 .LP
 924 Note that the \fIname\fR argument of \fBgl_register_action()\fR specifies the
 925 name by which a user can refer to the action in their configuration file. This
 926 allows them to re-bind the action to an alternate key-sequence. In order for
 927 this to work, it is necessary to call \fBgl_register_action()\fR between
 928 calling \fBnew_GetLine()\fR and the first call to \fBgl_get_line()\fR.
 929 .SS "History Files"

 930 To save the contents of the history buffer before quitting your application and
 931 subsequently restore them when you next start the application, the
 932 \fBgl_save_history()\fR and \fBgl_load_history()\fR functions are provided.
 933 .sp
 934 .LP
 935 The \fIfilename\fR argument specifies the name to give the history file when
 936 saving, or the name of an existing history file, when loading. This may contain
 937 home directory and environment variable expressions, such as
 938 \fB~/.myapp_history\fR or \fB$HOME/.myapp_history\fR.
 939 .sp
 940 .LP
 941 Along with each history line, additional information about it, such as its
 942 nesting level and when it was entered by the user, is recorded as a comment
 943 preceding the line in the history file. Writing this as a comment allows the
 944 history file to double as a command file, just in case you wish to replay a
 945 whole session using it. Since comment prefixes differ in different languages,
 946 the comment argument is provided for specifying the comment prefix. For
 947 example, if your application were a UNIX shell, such as the Bourne shell, you
 948 would specify "#" here. Whatever you choose for the comment character, you must
 949 specify the same prefix to \fBgl_load_history()\fR that you used when you
 950 called \fBgl_save_history()\fR to write the history file.
 951 .sp
 952 .LP
 953 The \fImax_lines\fR argument must be either -1 to specify that all lines in the
 954 history list be saved, or a positive number specifying a ceiling on how many of
 955 the most recent lines should be saved.
 956 .sp
 957 .LP
 958 Both functions return non-zero on error, after writing an error message to
 959 \fBstderr\fR. Note that \fBgl_load_history()\fR does not consider the
 960 non-existence of a file to be an error.
 961 .SS "Multiple History Lists"

 962 If your application uses a single \fBGetLine\fR object for entering many
 963 different types of input lines, you might want \fBgl_get_line()\fR to
 964 distinguish the different types of lines in the history list, and only recall
 965 lines that match the current type of line. To support this requirement,
 966 \fBgl_get_line()\fR marks lines being recorded in the history list with an
 967 integer identifier chosen by the application. Initially this identifier is set
 968 to 0 by \fBnew_GetLine()\fR, but it can be changed subsequently by calling
 969 \fBgl_group_history()\fR.
 970 .sp
 971 .LP
 972 The integer identifier ID can be any number chosen by the application, but note
 973 that \fBgl_save_history()\fR and \fBgl_load_history()\fR preserve the
 974 association between identifiers and historical input lines between program
 975 invocations, so you should choose fixed identifiers for the different types of
 976 input line used by your application.
 977 .sp
 978 .LP
 979 Whenever \fBgl_get_line()\fR appends a new input line to the history list, the
 980 current history identifier is recorded with it, and when it is asked to recall
 981 a historical input line, it only recalls lines that are marked with the current
 982 identifier.
 983 .SS "Displaying History"

 984 The history list can be displayed by calling \fBgl_show_history()\fR. This
 985 function displays the current contents of the history list to the \fBstdio\fR
 986 output stream \fIfp\fR. If the \fImax_lines\fR argument is greater than or
 987 equal to zero, then no more than this number of  the most recent lines will be
 988 displayed. If the \fIall_groups\fR argument is non-zero, lines from all history
 989 groups are displayed. Otherwise only those of the currently selected history
 990 group are displayed. The format string argument, \fIfmt\fR, determines how the
 991 line is displayed. This can contain arbitrary characters which are written
 992 verbatim, interleaved with any of the following format directives:
 993 .sp
 994 .ne 2
 995 .na
 996 \fB\fB%D\fR\fR
 997 .ad
 998 .RS 6n
 999 The date on which the line was originally entered, formatted like 2001-11-20.
1000 .RE
1001 
1002 .sp
1003 .ne 2


1041 \fB\fB%H\fR\fR
1042 .ad
1043 .RS 6n
1044 The history line itself.
1045 .RE
1046 
1047 .sp
1048 .LP
1049 Thus a format string like "%D %T %H0" would output something like:
1050 .sp
1051 .in +2
1052 .nf
1053 2001-11-20 10:23:34  Hello world
1054 .fi
1055 .in -2
1056 
1057 .sp
1058 .LP
1059 Note the inclusion of an explicit newline character in the format string.
1060 .SS "Looking Up History"

1061 The \fBgl_lookup_history()\fR function allows the calling application to look
1062 up lines in the history list.
1063 .sp
1064 .LP
1065 The \fIid\fR argument indicates which line to look up, where the first line
1066 that was entered in the history list after \fBnew_GetLine()\fR was called is
1067 denoted by 0, and subsequently entered lines are denoted with successively
1068 higher numbers. Note that the range of lines currently preserved in the history
1069 list can be queried by calling the \fBgl_range_of_history()\fR function. If the
1070 requested line is in the history list, the details of the line are recorded in
1071 the variable pointed to by the \fIhline\fR argument, and 1 is returned.
1072 Otherwise 0 is returned, and the variable pointed to by \fIhline\fR is left
1073 unchanged.
1074 .sp
1075 .LP
1076 Beware that the string returned in \fIhline\fR->\fIline\fR is part of the
1077 history buffer, so it must not be modified by the caller, and will be recycled
1078 on the next call to any function that takes \fIgl\fR as its argument. Therefore
1079 you should make a private copy of this string if you need to keep it.
1080 .SS "Manual History Archival"

1081 By default, whenever a line is entered by the user, it is automatically
1082 appended to the history list, just before \fBgl_get_line()\fR returns the line
1083 to the caller. This is convenient for the majority of applications, but there
1084 are also applications that need finer-grained control over what gets added to
1085 the history list. In such cases, the automatic addition of entered lines to the
1086 history list can be turned off by calling the \fBgl_automatic_history()\fR
1087 function.
1088 .sp
1089 .LP
1090 If this function is called with its \fIenable\fR argument set to 0,
1091 \fBgl_get_line()\fR will not automatically archive subsequently entered lines.
1092 Automatic archiving can be reenabled at a later time by calling this function
1093 again, with its \fIenable\fR argument set to 1. While automatic history
1094 archiving is disabled, the calling application can use the
1095 \fBgl_append_history()\fR to append lines to the history list as needed.
1096 .sp
1097 .LP
1098 The \fIline\fR argument specifies the line to be added to the history list.
1099 This must be a normal '\e0 ' terminated string. If this string contains any
1100 newline characters, the line that gets archived in the history list will be


1115 One of the arguments passed to \fBgl_append_history()\fR was \fINULL\fR.
1116 .RE
1117 
1118 .sp
1119 .ne 2
1120 .na
1121 \fB\fBENOMEM\fR\fR
1122 .ad
1123 .RS 10n
1124 The specified line was longer than the allocated size of the history buffer (as
1125 specified when \fBnew_GetLine()\fR was called), so it could not be archived.
1126 .RE
1127 
1128 .sp
1129 .LP
1130 A textual description of the error can optionally be obtained by calling
1131 \fBgl_error_message()\fR. Note that after such an error, the history list
1132 remains in a valid state to receive new history lines, so there is little harm
1133 in simply ignoring the return status of \fBgl_append_history()\fR.
1134 .SS "Miscellaneous History Configuration"

1135 If you wish to change the size of the history buffer that was originally
1136 specified in the call to \fBnew_GetLine()\fR, you can do so with the
1137 \fBgl_resize_history()\fR function.
1138 .sp
1139 .LP
1140 The \fIhistlen\fR argument specifies the new size in bytes, and if you specify
1141 this as 0, the buffer will be deleted.
1142 .sp
1143 .LP
1144 As mentioned in the discussion of \fBnew_GetLine()\fR, the number of lines that
1145 can be stored in the history buffer, depends on the lengths of the individual
1146 lines. For example, a 1000 byte buffer could equally store 10 lines of average
1147 length 100 bytes, or 20 lines of average length 50 bytes. Although the buffer
1148 is never expanded when new lines are added, a list of pointers into the buffer
1149 does get expanded when needed to accommodate the number of lines currently
1150 stored in the buffer. To place an upper limit on the number of lines in the
1151 buffer, and thus a ceiling on the amount of memory used in this list, you can
1152 call the \fBgl_limit_history()\fR function.
1153 .sp
1154 .LP


1159 .sp
1160 .LP
1161 To discard lines from the history buffer, use the \fBgl_clear_history()\fR
1162 function.
1163 .sp
1164 .LP
1165 The \fIall_groups\fR argument tells the function whether to delete just the
1166 lines associated with the current history group (see \fBgl_group_history()\fR)
1167 or all historical lines in the buffer.
1168 .sp
1169 .LP
1170 The \fBgl_toggle_history()\fR function allows you to toggle history on and off
1171 without losing the current contents of the history list.
1172 .sp
1173 .LP
1174 Setting the \fIenable\fR argument to 0 turns off the history mechanism, and
1175 setting it to 1 turns it back on. When history is turned off, no new lines will
1176 be added to the history list, and history lookup key-bindings will act as
1177 though there is nothing in the history buffer.
1178 .SS "Querying History Information"

1179 The configured state of the history list can be queried with the
1180 \fBgl_history_state()\fR function. On return, the status information is
1181 recorded in the variable pointed to by the \fIstate\fR argument.
1182 .sp
1183 .LP
1184 The \fBgl_range_of_history()\fR function returns the number and range of lines
1185 in the history list. The return values are recorded in the variable pointed to
1186 by the range argument. If the \fInlines\fR member of this structure is greater
1187 than zero, then the oldest and newest members report the range of lines in the
1188 list, and \fInewest\fR=\fIoldest\fR+\fInlines\fR-1. Otherwise they are both
1189 zero.
1190 .sp
1191 .LP
1192 The \fBgl_size_of_history()\fR function returns the total size of the history
1193 buffer and the amount of the buffer that is currently occupied.
1194 .sp
1195 .LP
1196 On return, the size information is recorded in the variable pointed to by the
1197 \fIsize\fR argument.
1198 .SS "Changing Terminals"

1199 The \fBnew_GetLine()\fR constructor function assumes that input is to be read
1200 from \fBstdin\fR and output written to \fBstdout\fR. The following function
1201 allows you to switch to different input and output streams.
1202 .sp
1203 .LP
1204 The \fIgl\fR argument is the object that was returned by \fBnew_GetLine()\fR.
1205 The \fIinput_fp\fR argument specifies the stream to read from, and
1206 \fIoutput_fp\fR specifies the stream to be written to. Only if both of these
1207 refer to a terminal, will interactive terminal input be enabled. Otherwise
1208 \fBgl_get_line()\fR will simply call \fBfgets()\fR to read command input. If
1209 both streams refer to a terminal, then they must refer to the same terminal,
1210 and the type of this terminal must be specified with the \fIterm\fR argument.
1211 The value of the \fIterm\fR argument is looked up in the terminal information
1212 database (\fBterminfo\fR or \fBtermcap\fR), in order to determine which special
1213 control sequences are needed to control various aspects of the terminal.
1214 \fBnew_GetLine()\fR for example, passes the return value of
1215 \fBgetenv\fR("TERM") in this argument. Note that if one or both of
1216 \fIinput_fp\fR and \fIoutput_fp\fR do not refer to a terminal, then it is legal
1217 to pass \fINULL\fR instead of a terminal type.
1218 .sp
1219 .LP
1220 Note that if you want to pass file descriptors to \fBgl_change_terminal()\fR,
1221 you can do this by creating \fBstdio\fR stream wrappers using the POSIX
1222 \fBfdopen\fR(3C) function.
1223 .SS "External Event Handling"

1224 By default, \fBgl_get_line()\fR does not return until either a complete input
1225 line has been entered by the user, or an error occurs. In programs that need to
1226 watch for I/O from other sources than the terminal, there are two options.
1227 .RS +4
1228 .TP
1229 .ie t \(bu
1230 .el o
1231 Use the functions described in the \fBgl_io_mode\fR(3TECLA) manual page to
1232 switch \fBgl_get_line()\fR into non-blocking server mode. In this mode,
1233 \fBgl_get_line()\fR becomes a non-blocking, incremental line-editing function
1234 that can safely be called from an external event loop. Although this is a very
1235 versatile method, it involves taking on some responsibilities that are normally
1236 performed behind the scenes by \fBgl_get_line()\fR.
1237 .RE
1238 .RS +4
1239 .TP
1240 .ie t \(bu
1241 .el o
1242 While \fBgl_get_line()\fR is waiting for keyboard input from the user, you can
1243 ask it to also watch for activity on arbitrary file descriptors, such as


1344 particular signal you will need to both temporarily install your own signal
1345 handler, and unblock the signal. Be sure to re-block the signal (if it was
1346 originally blocked) and reinstate the original signal handler, if any, before
1347 returning.
1348 .sp
1349 .LP
1350 Your callback should not try to read from the terminal, which is left in raw
1351 mode as far as input is concerned. You can write to the terminal as usual,
1352 since features like conversion of newline to carriage-return/linefeed are
1353 re-enabled while the callback is running. If your callback function does write
1354 to the terminal, be sure to output a newline first, and when your callback
1355 returns, tell \fBgl_get_line()\fR that the input line needs to be redrawn, by
1356 returning the \fBGLFD_REFRESH\fR status code.
1357 .sp
1358 .LP
1359 To remove a callback function that you previously registered for a given file
1360 descriptor and event, simply call \fBgl_watch_fd()\fR with the same \fIfd\fR
1361 and \fIevent\fR arguments, but with a \fIcallback\fR argument of 0. The
1362 \fIdata\fR argument is ignored in this case.
1363 .SS "Setting An Inactivity Timeout"

1364 The \fBgl_inactivity_timeout()\fR function can be used to set or cancel an
1365 inactivity timeout. Inactivity in this case refers both to keyboard input, and
1366 to I/O on any file descriptors registered by prior and subsequent calls to
1367 \fBgl_watch_fd()\fR.
1368 .sp
1369 .LP
1370 The timeout is specified in the form of an integral number of seconds and an
1371 integral number of nanoseconds, specified by the \fIsec\fR and \fInsec\fR
1372 arguments, respectively. Subsequently, whenever no activity is seen for this
1373 time period, the function specified by the \fIcallback\fR argument is called.
1374 The \fIdata\fR argument of \fBgl_inactivity_timeout()\fR is passed to this
1375 callback function whenever it is invoked, and can thus be used to pass
1376 arbitrary application-specific information to the callback. The following macro
1377 is provided in <\fBlibtecla.h\fR> for applications to use to declare and
1378 prototype timeout callback functions.
1379 .sp
1380 .in +2
1381 .nf
1382 #define GL_TIMEOUT_FN(fn) GlAfterTimeout (fn)(GetLine *gl, void *data)
1383 .fi


1437 Your callback should not try to read from the terminal, which is left in raw
1438 mode as far as input is concerned. You can however write to the terminal as
1439 usual, since features like conversion of newline to carriage-return/linefeed
1440 are re-enabled while the callback is running. If your callback function does
1441 write to the terminal, be sure to output a newline first, and when your
1442 callback returns, tell \fBgl_get_line()\fR that the input line needs to be
1443 redrawn, by returning the \fBGLTO_REFRESH\fR status code.
1444 .sp
1445 .LP
1446 Finally, note that although the timeout arguments include a nanosecond
1447 component, few computer clocks presently have resolutions that are finer than a
1448 few milliseconds, so asking for less than a few milliseconds is equivalent to
1449 requesting zero seconds on many systems. If this would be a problem, you should
1450 base your timeout selection on the actual resolution of the host clock (for
1451 example, by calling \fBsysconf\fR(\fB_SC_CLK_TCK\fR)).
1452 .sp
1453 .LP
1454 To turn off timeouts, simply call \fBgl_inactivity_timeout()\fR with a
1455 \fIcallback\fR argument of 0. The \fIdata\fR argument is ignored in this case.
1456 .SS "Signal Handling Defaults"

1457 By default, the \fBgl_get_line()\fR function intercepts a number of signals.
1458 This is particularly important for signals that would by default terminate the
1459 process, since the terminal needs to be restored to a usable state before this
1460 happens. This section describes the signals that are trapped by default and how
1461 \fBgl_get_line()\fR responds to them. Changing these defaults is the topic of
1462 the following section.
1463 .sp
1464 .LP
1465 When the following subset of signals are caught, \fBgl_get_line()\fR first
1466 restores the terminal settings and signal handling to how they were before
1467 \fBgl_get_line()\fR was called, resends the signal to allow the calling
1468 application's signal handlers to handle it, then, if the process still exists,
1469 returns \fINULL\fR and sets \fBerrno\fR as specified below.
1470 .sp
1471 .ne 2
1472 .na
1473 \fB\fBSIGINT\fR\fR
1474 .ad
1475 .RS 11n
1476 This signal is generated both by the keyboard interrupt key (usually \fB^C\fR),


1674 .RS 13n
1675 This signal is generated if the program attempts to write to the terminal while
1676 the program is running in the background.
1677 .RE
1678 
1679 .sp
1680 .LP
1681 Obviously not all of the above signals are supported on all systems, so code to
1682 support them is conditionally compiled into the tecla library.
1683 .sp
1684 .LP
1685 Note that if \fBSIGKILL\fR or \fBSIGPOLL\fR, which by definition cannot be
1686 caught, or any of the hardware generated exception signals, such as
1687 \fBSIGSEGV\fR, \fBSIGBUS\fR, and \fBSIGFPE\fR, are received and unhandled while
1688 \fBgl_get_line()\fR has the terminal in raw mode, the program will be
1689 terminated without the terminal having been restored to a usable state. In
1690 practice, job-control shells usually reset the terminal settings when a process
1691 relinquishes the controlling terminal, so this is only a problem with older
1692 shells.
1693 .SS "Customized Signal Handling"

1694 The previous section listed the signals that \fBgl_get_line()\fR traps by
1695 default, and described how it responds to them. This section describes how to
1696 both add and remove signals from the list of trapped signals, and how to
1697 specify how \fBgl_get_line()\fR should respond to a given signal.
1698 .sp
1699 .LP
1700 If you do not need \fBgl_get_line()\fR to do anything in response to a signal
1701 that it normally traps, you can tell to \fBgl_get_line()\fR to ignore that
1702 signal by calling \fBgl_ignore_signal()\fR.
1703 .sp
1704 .LP
1705 The \fIsigno\fR argument is the number of the signal (for example,
1706 \fBSIGINT\fR) that you want to have ignored. If the specified signal is not
1707 currently one of those being trapped, this function does nothing.
1708 .sp
1709 .LP
1710 The \fBgl_trap_signal()\fR function allows you to either add a new signal to
1711 the list that \fBgl_get_line()\fR traps or modify how it responds to a signal
1712 that it already traps.
1713 .sp


1826 .el o
1827 So that you do not need to end each output line with \er\en, instead of just
1828 \en.
1829 .RE
1830 .sp
1831 .LP
1832 The \fBGL_RESTORE_ENV\fR combination is the same as \fBGL_SUSPEND_INPUT\fR,
1833 except that it does not move the cursor. If your signal handler does not read
1834 or write anything to the terminal, the user will not see any visible indication
1835 that a signal was caught. This can be useful if you have a signal handler that
1836 only occasionally writes to the terminal, where using \fBGL_SUSPEND_LINE\fR
1837 would cause the input line to be unnecessarily duplicated when nothing had been
1838 written to the terminal. Such a signal handler, when it does write to the
1839 terminal, should be sure to start a new line at the start of its first write,
1840 by writing a new line before returning. If the signal arrives while the user is
1841 entering a line that only occupies a signal terminal line, or if the cursor is
1842 on the last terminal line of a longer input line, this will have the same
1843 effect as \fBGL_SUSPEND_INPUT\fR. Otherwise it will start writing on a line
1844 that already contains part of the displayed input line. This does not do any
1845 harm, but it looks a bit ugly, which is why the \fBGL_SUSPEND_INPUT\fR
1846 combination is better if you know that you are always going to be writing to
1847 the terminal.
1848 .sp
1849 .LP
1850 The \fIafter\fR argument, which determines what \fBgl_get_line()\fR does after
1851 the application's signal handler returns (if  it returns), can take any one of
1852 the following values:
1853 .sp
1854 .ne 2
1855 .na
1856 \fB\fBGLS_RETURN\fR\fR
1857 .ad
1858 .RS 16n
1859 Return the completed input line, just as though the user had pressed the return
1860 key.
1861 .RE
1862 
1863 .sp
1864 .ne 2
1865 .na
1866 \fB\fBGLS_ABORT\fR\fR


1874 .RE
1875 
1876 .sp
1877 .ne 2
1878 .na
1879 \fB\fBGLS_CONTINUE\fR\fR
1880 .ad
1881 .RS 16n
1882 Resume command line editing.
1883 .RE
1884 
1885 .sp
1886 .LP
1887 The \fIerrno_value\fR argument is intended to be combined with the
1888 \fBGLS_ABORT\fR option, telling \fBgl_get_line()\fR what to set the standard
1889 \fBerrno\fR variable to before returning \fINULL\fR to the calling program. It
1890 can also, however, be used with the \fBGL_RETURN\fR option, in case you want to
1891 have a way to distinguish between an input line that was entered using the
1892 return key, and one that was entered by the receipt of a signal.
1893 .SS "Reliable Signal Handling"

1894 Signal handling is surprisingly hard to do reliably without race conditions. In
1895 \fBgl_get_line()\fR a lot of care has been taken to allow applications to
1896 perform reliable signal handling around \fBgl_get_line()\fR. This section
1897 explains how to make use of this.
1898 .sp
1899 .LP
1900 As an example of the problems that can arise if the application is not written
1901 correctly, imagine that one's application has a \fBSIGINT\fR signal handler
1902 that sets a global flag. Now suppose that the application tests this flag just
1903 before invoking \fBgl_get_line()\fR. If a \fBSIGINT\fR signal happens to be
1904 received in the small window of time between the statement that tests the value
1905 of this flag, and the statement that calls \fBgl_get_line()\fR, then
1906 \fBgl_get_line()\fR will not see the signal, and will not be interrupted. As a
1907 result, the application will not be able to respond to the signal until the
1908 user gets around to finishing entering the input line and \fBgl_get_line()\fR
1909 returns. Depending on the application, this might or might not be a disaster,
1910 but at the very least it would puzzle the user.
1911 .sp
1912 .LP
1913 The way to avoid such problems is to do the following.


2087 \fBgl_get_line()\fR blocks delivery of the above signal again.
2088 .RE
2089 .RS +4
2090 .TP
2091 6.
2092 It then undoes any actions performed in the first of the above steps and
2093 redisplays the line, if the signal configuration calls for this.
2094 .RE
2095 .RS +4
2096 .TP
2097 7.
2098 \fBgl_get_line()\fR then either resumes trying to read a character, or
2099 aborts, depending on the configuration of the signal that was caught.
2100 .RE
2101 .sp
2102 .LP
2103 What the above steps do in essence is to take asynchronously delivered signals
2104 and handle them synchronously, one at a time, at a point in the code where
2105 \fBgl_get_line()\fR has complete control over its environment.
2106 .SS "The Terminal Size"

2107 On most systems the combination of the \fBTIOCGWINSZ\fR ioctl and the
2108 \fBSIGWINCH\fR signal is used to maintain an accurate idea of the terminal
2109 size. The terminal size is newly queried every time that \fBgl_get_line()\fR is
2110 called and whenever a \fBSIGWINCH\fR signal is received.
2111 .sp
2112 .LP
2113 On the few systems where this mechanism is not available, at startup
2114 \fBnew_GetLine()\fR first looks for the \fBLINES\fR and \fBCOLUMNS\fR
2115 environment variables. If these are not found, or they contain unusable values,
2116 then if a terminal information database like \fBterminfo\fR or \fBtermcap\fR is
2117 available, the default size of the terminal is looked up in this database. If
2118 this too fails to provide the terminal size, a default size of 80 columns by 24
2119 lines is used.
2120 .sp
2121 .LP
2122 Even on systems that do support ioctl(\fBTIOCGWINSZ\fR), if the terminal is on
2123 the other end of a serial line, the terminal driver generally has no way of
2124 detecting when a resize occurs or of querying what the current size is. In such
2125 cases no \fBSIGWINCH\fR is sent to the process, and the dimensions returned by
2126 ioctl(\fBTIOCGWINSZ\fR) are not correct. The only way to handle such instances


2137 In non-blocking server-I/O mode, if a line is currently being input, the input
2138 line is then redrawn to accommodate the changed size. Finally the new values are
2139 recorded in \fIgl\fR for future use by \fBgl_get_line()\fR.
2140 .sp
2141 .LP
2142 The \fBgl_terminal_size()\fR function allows you to query the current size of
2143 the terminal, and install an alternate fallback size for cases where the size
2144 is not available. Beware that the terminal size will not be available if
2145 reading from a pipe or a file, so the default values can be important even on
2146 systems that do support ways of finding out the terminal size.
2147 .sp
2148 .LP
2149 This function first updates \fBgl_get_line()\fR's fallback terminal dimensions,
2150 then records its findings in the return value.
2151 .sp
2152 .LP
2153 The \fIdef_ncolumn\fR and \fIdef_nline\fR arguments specify the default number
2154 of terminal columns and lines to use if the terminal size cannot be determined
2155 by ioctl(\fBTIOCGWINSZ\fR) or environment variables.
2156 .SS "Hiding What You Type"

2157 When entering sensitive information, such as passwords, it is best not to have
2158 the text that you are entering echoed on the terminal. Furthermore, such text
2159 should not be recorded in the history list, since somebody finding your
2160 terminal unattended could then recall it, or somebody snooping through your
2161 directories could see it in your history file. With this in mind, the
2162 \fBgl_echo_mode()\fR function allows you to toggle on and off the display and
2163 archival of any text that is subsequently entered in calls to
2164 \fBgl_get_line()\fR.
2165 .sp
2166 .LP
2167 The \fIenable\fR argument specifies whether entered text should be visible or
2168 not. If it is 0, then subsequently entered lines will not be visible on the
2169 terminal, and will not be recorded in the history list. If it is 1, then
2170 subsequent input lines will be displayed as they are entered, and provided that
2171 history has not been turned off with a call to \fBgl_toggle_history()\fR, then
2172 they will also be archived in the history list. Finally, if the enable argument
2173 is -1, then the echoing mode is left unchanged, which allows you to
2174 non-destructively query the current setting through the return value. In all
2175 cases, the return value of the function is 0 if echoing was disabled before the
2176 function was called, and 1 if it was enabled.
2177 .sp
2178 .LP
2179 When echoing is turned off, note that although tab completion will invisibly
2180 complete your prefix as far as possible, ambiguous completions will not be
2181 displayed.
2182 .SS "Single Character Queries"

2183 Using \fBgl_get_line()\fR to query the user for a single character reply, is
2184 inconvenient for the user, since they must hit the enter or return key before
2185 the character that they typed is returned to the program. Thus the
2186 \fBgl_query_char()\fR function has been provided for single character queries
2187 like this.
2188 .sp
2189 .LP
2190 This function displays the specified prompt at the start of a new line, and
2191 waits for the user to type a character. When the user types a character,
2192 \fBgl_query_char()\fR displays it to the right of the prompt, starts a newline,
2193 then returns the character to the calling program. The return value of the
2194 function is the character that was typed. If the read had to be aborted for
2195 some reason, EOF is returned instead. In the latter case, the application can
2196 call the previously documented \fBgl_return_status()\fR, to find out what went
2197 wrong. This could, for example, have been the reception of a signal, or the
2198 optional inactivity timer going off.
2199 .sp
2200 .LP
2201 If the user simply hits enter, the value of the \fIdefchar\fR argument is
2202 substituted. This means that when the user hits either newline or return, the


2210 symbolically. For example, control-A is displayed as \fB^A\fR, and characters
2211 beyond 127 are displayed in octal, preceded by a backslash.
2212 .sp
2213 .LP
2214 As with \fBgl_get_line()\fR, echoing of the entered character can be disabled
2215 using the \fBgl_echo_mode()\fR function.
2216 .sp
2217 .LP
2218 If the calling process is suspended while waiting for the user to type their
2219 response, the cursor is moved to the line following the prompt line, then when
2220 the process resumes, the prompt is redisplayed, and \fBgl_query_char()\fR
2221 resumes waiting for the user to type a character.
2222 .sp
2223 .LP
2224 Note that in non-blocking server mode, if an incomplete input line is in the
2225 process of being read when \fBgl_query_char()\fR is called, the partial input
2226 line is discarded, and erased from the terminal, before the new prompt is
2227 displayed. The next call to \fBgl_get_line()\fR will thus start editing a new
2228 line.
2229 .SS "Reading Raw Characters"

2230 Whereas the \fBgl_query_char()\fR function visibly prompts the user for a
2231 character, and displays what they typed, the \fBgl_read_char()\fR function
2232 reads a signal character from the user, without writing anything to the
2233 terminal, or perturbing any incompletely entered input line. This means that it
2234 can be called not only from between calls to \fBgl_get_line()\fR, but also from
2235 callback functions that the application has registered to be called by
2236 \fBgl_get_line()\fR.
2237 .sp
2238 .LP
2239 On success, the return value of \fBgl_read_char()\fR is the character that was
2240 read. On failure, EOF is returned, and the \fBgl_return_status()\fR function
2241 can be called to find out what went wrong. Possibilities include the optional
2242 inactivity timer going off, the receipt of a signal that is configured to abort
2243 \fBgl_get_line()\fR, or terminal I/O blocking, when in non-blocking server-I/O
2244 mode.
2245 .sp
2246 .LP
2247 Beware that certain keyboard keys, such as function keys, and cursor keys,
2248 usually generate at least three characters each, so a single call to
2249 \fBgl_read_char()\fR will not be enough to identify such keystrokes.
2250 .SS "Clearing The Terminal"

2251 The calling program can clear the terminal by calling
2252 \fBgl_erase_terminal()\fR. In non-blocking server-I/O mode, this function also
2253 arranges for the current input line to be redrawn from scratch when
2254 \fBgl_get_line()\fR is next called.
2255 .SS "Displaying Text Dynamically"

2256 Between calls to \fBgl_get_line()\fR, the \fBgl_display_text()\fR function
2257 provides a convenient way to display paragraphs of text, left-justified and
2258 split over one or more terminal lines according to the constraints of the
2259 current width of the terminal. Examples of the use of this function may be
2260 found in the demo programs, where it is used to display introductions. In those
2261 examples the advanced use  of optional prefixes, suffixes and filled lines to
2262 draw a box around the text is also illustrated.
2263 .sp
2264 .LP
2265 If \fIgl\fR is not currently connected to a terminal, for example if the output
2266 of a program that uses \fBgl_get_line()\fR is being piped to another program or
2267 redirected to a file, then the value of the \fIdef_width\fR parameter is used
2268 as the terminal width.
2269 .sp
2270 .LP
2271 The \fIindentation\fR argument specifies the number of characters to use to
2272 indent each line of output. The \fIfill_char\fR argument specifies the character
2273 that will be used to perform this indentation.
2274 .sp
2275 .LP


2278 argument can be either \fINULL\fR or a string to place at the end of each line.
2279 The suffix is placed flush against the right edge of the terminal, and any
2280 space between its first character and the last word on that line is filled with
2281 the character specified by the \fIfill_char\fR argument. Normally the
2282 fill-character is a space.
2283 .sp
2284 .LP
2285 The \fIstart\fR argument tells \fBgl_display_text()\fR how many characters have
2286 already been written to the current terminal line, and thus tells it the
2287 starting column index of the cursor. Since the return value of
2288 \fBgl_display_text()\fR is the ending column index of the cursor, by passing
2289 the return value of one call to the start argument of the next call, a
2290 paragraph that is broken between more than one string can be composed by
2291 calling \fBgl_display_text()\fR for each successive portion of the paragraph.
2292 Note that literal newline characters are necessary at the end of each paragraph
2293 to force a new line to be started.
2294 .sp
2295 .LP
2296 On error, \fBgl_display_text()\fR returns -1.
2297 .SS "Callback Function Facilities"

2298 Unless otherwise stated, callback functions such as tab completion callbacks
2299 and event callbacks should not call any functions in this module. The following
2300 functions, however, are designed specifically to be used by callback functions.
2301 .sp
2302 .LP
2303 Calling the \fBgl_replace_prompt()\fR function from a callback tells
2304 \fBgl_get_line()\fR to display a different prompt when the callback returns.
2305 Except in non-blocking server mode, it has no effect if used between calls to
2306 \fBgl_get_line()\fR. In non-blocking server mode, when used between two calls
2307 to \fBgl_get_line()\fR that are operating on the same input line, the current
2308 input line will be re-drawn with the new prompt on the following call to
2309 \fBgl_get_line()\fR.
2310 .SS "International Character Sets"

2311 Since \fBlibtecla\fR(3LIB) version 1.4.0, \fBgl_get_line()\fR has been 8-bit
2312 clean. This means that all 8-bit characters that are printable in the user's
2313 current locale are now displayed verbatim and included in the returned input
2314 line. Assuming that the calling program correctly contains a call like the
2315 following,
2316 .sp
2317 .in +2
2318 .nf
2319 setlocale(LC_CTYPE, "")
2320 .fi
2321 .in -2
2322 
2323 .sp
2324 .LP
2325 then the current locale is determined by the first of the environment variables
2326 \fBLC_CTYPE\fR, \fBLC_ALL\fR, and \fBLANG\fR that is found to contain a valid
2327 locale name. If none of these variables are defined, or the program neglects to
2328 call \fBsetlocale\fR(3C), then the default C locale is used, which is US 7-bit
2329 ASCII. On most UNIX-like platforms, you can get a list of valid locales by
2330 typing the command:
2331 .sp
2332 .in +2
2333 .nf
2334 locale -a
2335 .fi
2336 .in -2
2337 .sp
2338 
2339 .sp
2340 .LP
2341 at the shell prompt. Further documentation on how the user can make use of this
2342 to enter international characters can be found in the \fBtecla\fR(5) man page.
2343 .SS "Thread Safety"

2344 Unfortunately neither \fBterminfo\fR nor \fBtermcap\fR were designed to be
2345 reentrant, so you cannot safely use the functions of the getline module in
2346 multiple threads (you can use the separate file-expansion and word-completion
2347 modules in multiple threads, see the corresponding man pages for details).
2348 However due to the use of POSIX reentrant functions for looking up home
2349 directories, it is safe to use this module from a single thread of a
2350 multi-threaded program, provided that your other threads do not use any
2351 \fBtermcap\fR or \fBterminfo\fR functions.
2352 .SH ATTRIBUTES

2353 See \fBattributes\fR(5) for descriptions of the following attributes:
2354 .sp
2355 
2356 .sp
2357 .TS
2358 box;
2359 c | c
2360 l | l .
2361 ATTRIBUTE TYPE  ATTRIBUTE VALUE
2362 _
2363 Interface Stability     Committed
2364 _
2365 MT-Level        MT-Safe
2366 .TE
2367 
2368 .SH SEE ALSO

2369 \fBcpl_complete_word\fR(3TECLA), \fBef_expand_file\fR(3TECLA),
2370 \fBgl_io_mode\fR(3TECLA), \fBlibtecla\fR(3LIB), \fBpca_lookup_file\fR(3TECLA),
2371 \fBattributes\fR(5), \fBtecla\fR(5)