1 .\" $NetBSD: editline.3,v 1.101 2021/08/15 10:12:54 wiz Exp $
3 .\" Copyright (c) 1997-2014 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This file was contributed to The NetBSD Foundation by Luke Mewburn.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
17 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 .\" POSSIBILITY OF SUCH DAMAGE.
75 .Nd line editor, history and tokenization functions
81 .Fn el_init "const char *prog" "FILE *fin" "FILE *fout" "FILE *ferr"
83 .Fn el_init_fd "const char *prog" "FILE *fin" "FILE *fout" "FILE *ferr" "int fdin" "int fdout" "int fderr"
85 .Fn el_end "EditLine *e"
87 .Fn el_reset "EditLine *e"
89 .Fn el_gets "EditLine *e" "int *count"
91 .Fn el_wgets "EditLine *e" "int *count"
93 .Fn el_getc "EditLine *e" "char *ch"
95 .Fn el_wgetc "EditLine *e" "wchar_t *wc"
97 .Fn el_push "EditLine *e" "const char *mbs"
99 .Fn el_wpush "EditLine *e" "const wchar_t *wcs"
101 .Fn el_parse "EditLine *e" "int argc" "const char *argv[]"
103 .Fn el_wparse "EditLine *e" "int argc" "const wchar_t *argv[]"
105 .Fn el_set "EditLine *e" "int op" "..."
107 .Fn el_wset "EditLine *e" "int op" "..."
109 .Fn el_get "EditLine *e" "int op" "..."
111 .Fn el_wget "EditLine *e" "int op" "..."
113 .Fn el_source "EditLine *e" "const char *file"
115 .Fn el_resize "EditLine *e"
117 .Fn el_cursor "EditLine *e" "int count"
119 .Fn el_line "EditLine *e"
120 .Ft const LineInfoW *
121 .Fn el_wline "EditLine *e"
123 .Fn el_insertstr "EditLine *e" "const char *str"
125 .Fn el_winsertstr "EditLine *e" "const wchar_t *str"
127 .Fn el_deletestr "EditLine *e" "int count"
129 .Fn el_wdeletestr "EditLine *e" "int count"
131 .Fn history_init void
133 .Fn history_winit void
135 .Fn history_end "History *h"
137 .Fn history_wend "HistoryW *h"
139 .Fn history "History *h" "HistEvent *ev" "int op" "..."
141 .Fn history_w "HistoryW *h" "HistEventW *ev" "int op" "..."
143 .Fn tok_init "const char *IFS"
145 .Fn tok_winit "const wchar_t *IFS"
147 .Fn tok_end "Tokenizer *t"
149 .Fn tok_wend "TokenizerW *t"
151 .Fn tok_reset "Tokenizer *t"
153 .Fn tok_wreset "TokenizerW *t"
155 .Fn tok_line "Tokenizer *t" "const LineInfo *li" "int *argc" "const char **argv[]" "int *cursorc" "int *cursoro"
157 .Fn tok_wline "TokenizerW *t" "const LineInfoW *li" "int *argc" "const wchar_t **argv[]" "int *cursorc" "int *cursoro"
159 .Fn tok_str "Tokenizer *t" "const char *str" "int *argc" "const char **argv[]"
161 .Fn tok_wstr "TokenizerW *t" "const wchar_t *str" "int *argc" "const wchar_t **argv[]"
165 library provides generic line editing, history and tokenization functions,
166 similar to those found in
169 These functions are available in the
171 library (which needs the
174 Programs should be linked with
181 locale set by the application program and never uses
183 to change the locale.
184 .Sh LINE EDITING FUNCTIONS
185 The line editing functions use a common data structure,
194 The wide-character functions behave the same way as their narrow
197 The following functions are available:
200 Initialize the line editor, and return a data structure
201 to be used by all other line editing functions, or
205 is the name of the invoking program, used when reading the
207 file to determine which settings to use.
212 are the input, output, and error streams (respectively) to use.
213 In this documentation, references to
215 are actually to this input/output stream combination.
219 but allows specifying file descriptors for the
221 corresponding streams, in case those were created with
224 Clean up and finish with
226 assumed to have been created with
231 Reset the tty and the parser.
232 This should be called after an error which may have upset the tty's
235 Read a line from the tty.
237 is modified to contain the number of characters read.
238 Returns the line read if successful, or
240 if no characters were read or if an error occurred.
241 If an error occurred,
245 contains the error code that caused it.
246 The return value may not remain valid across calls to
248 and must be copied if the data is to be retained.
250 Read a wide character from the tty, respecting the current locale,
251 or from the input queue described in
253 if that is not empty, and store it in
255 If an invalid or incomplete character is found, it is discarded,
259 and the next character is read and stored in
261 Returns 1 if a valid character was read, 0 on end of file, or \-1 on
266 is set to indicate the error.
268 Read a wide character as described for
270 and return 0 on end of file or \-1 on failure.
271 If the wide character can be represented as a single-byte character,
276 and return 1; otherwise, set
281 In the C or POSIX locale, this simply reads a byte, but for any other
282 locale, including UTF-8, this is rarely useful.
284 Push the wide character string
286 back onto the input queue described in
288 If the queue overflows, for example due to a recursive macro,
289 or if an error occurs, for example because
293 or memory allocation fails, the function beeps at the user,
294 but does not report the problem to the caller.
296 Use the current locale to convert the multibyte string
298 to a wide character string, and pass the result to
309 If the command is prefixed with
313 will only execute the command if
320 \-1 if the command is unknown,
321 0 if there was no error or
324 1 if the command returned an error.
327 for more information.
333 determines which parameter to set, and each operation has its
335 Returns 0 on success, \-1 on failure.
337 The following values for
339 are supported, along with the required argument list:
341 .It Dv EL_PROMPT , Fa "char *(*f)(EditLine *)"
342 Define prompt printing function as
344 which is to return a string that contains the prompt.
345 .It Dv EL_PROMPT_ESC , Fa "char *(*f)(EditLine *)" , Fa "char c"
350 argument indicates the start/stop literal prompt character.
352 If a start/stop literal character is found in the prompt, the
354 is not printed, but characters after it are printed directly to the
355 terminal without affecting the state of the current line.
356 A subsequent second start/stop literal character ends this behavior.
357 This is typically used to embed literal escape sequences that change the
358 color/style of the terminal in the prompt.
359 Note that the literal escape character cannot be the last character in the
360 prompt, as the escape sequence is attached to the next character in the prompt.
364 Re-display the current line on the next terminal line.
365 .It Dv EL_RPROMPT , Fa "char *(*f)(EditLine *)"
366 Define right side prompt printing function as
368 which is to return a string that contains the prompt.
369 .It Dv EL_RPROMPT_ESC , Fa "char *(*f)(EditLine *)" , Fa "char c"
370 Define the right prompt printing function but with a literal escape character.
371 .It Dv EL_TERMINAL , Fa "const char *type"
372 Define terminal type of the tty to be
380 .It Dv EL_EDITOR , Fa "const char *mode"
387 .It Dv EL_SIGNAL , Fa "int flag"
392 will install its own signal handler for the following signals when
393 reading command input:
403 Otherwise, the current signal handlers will be used.
404 .It Dv EL_BIND , Fa "const char *" , Fa "..." , Dv NULL
410 for more information.
411 .It Dv EL_ECHOTC , Fa "const char *" , Fa "..." , Dv NULL
417 for more information.
418 .It Dv EL_SETTC , Fa "const char *" , Fa "..." , Dv NULL
424 for more information.
425 .It Dv EL_SETTY , Fa "const char *" , Fa "..." , Dv NULL
431 for more information.
432 .It Dv EL_TELLTC , Fa "const char *" , Fa "..." , Dv NULL
438 for more information.
439 .It Dv EL_ADDFN , Fa "const char *name" , Fa "const char *help" , \
440 Fa "unsigned char (*func)(EditLine *e, int ch)"
441 Add a user defined function,
445 which is invoked when a key which is bound to
453 is the key which caused the invocation.
457 .Bl -tag -width "CC_REDISPLAY"
459 Add a normal character.
461 End of line was entered.
465 Expecting further command input as arguments, do nothing visually.
468 .It Dv CC_REFRESH_BEEP
469 Refresh display, and beep.
471 Cursor moved, so update and perform
474 Redisplay entire input line.
475 This is useful if a key binding outputs extra information.
480 Fatal error, reset tty to known state.
482 .It Dv EL_HIST , Fa "History *(*func)(History *, int op, ...)" , \
484 Defines which history function to use, which is usually
487 should be the value returned by
489 .It Dv EL_EDITMODE , Fa "int flag"
493 editing is enabled (the default).
494 Note that this is only an indication, and does not
495 affect the operation of
497 At this time, it is the caller's responsibility to
501 to determine if editing should be enabled or not.
502 .It Dv EL_UNBUFFERED , Fa "int flag"
506 unbuffered mode is disabled (the default).
509 will return immediately after processing a single character.
510 .It Dv EL_SAFEREAD , Fa "int flag"
513 argument is non-zero, then
515 attempts to recover from read errors, ignoring the first interrrupted
516 error, and trying to reset the input file descriptor to reset non-blocking I/O.
517 This is disabled by default, and desirable only when
519 is used in shell-like applications.
520 .It Dv EL_GETCFN , Fa "el_rfunc_t f"
521 Whenever reading a character, use the function
522 .Bd -ragged -offset indent -compact
529 which stores the character in
531 and returns 1 on success, 0 on end of file, or \-1 on I/O or encoding
533 Functions internally using it include
539 Initially, a builtin function is installed, and replacing it
540 is discouraged because writing such a function is very error prone.
541 The builtin function can be restored at any time by passing the
543 .Dv EL_BUILTIN_GETCFN
544 instead of a function pointer.
545 .It Dv EL_CLIENTDATA , Fa "void *data"
548 to be associated with this EditLine structure.
549 It can be retrieved with the corresponding
552 .It Dv EL_SETFP , Fa "int fd" , Fa "FILE *fp"
577 determines which parameter to retrieve into
579 Returns 0 if successful, \-1 otherwise.
581 The following values for
583 are supported, along with actual type of
586 .It Dv EL_PROMPT , Fa "char *(*f)(EditLine *)" , Fa "char *c"
589 to a pointer to the function that displays the prompt.
594 set it to the start/stop literal prompt character.
595 .It Dv EL_RPROMPT , Fa "char *(*f)(EditLine *)" , Fa "char *c"
598 to a pointer to the function that displays the prompt.
603 set it to the start/stop literal prompt character.
604 .It Dv EL_EDITOR , Fa "const char **n"
605 Set the name of the editor in
611 .It Dv EL_GETTC , Fa "const char *name" , Fa "void *value"
618 to the current value of that capability.
619 .It Dv EL_SIGNAL , Fa "int *s"
624 has installed private signal handlers (see
627 .It Dv EL_EDITMODE , Fa "int *c"
630 to non-zero if editing is enabled.
631 .It Dv EL_GETCFN , Fa "el_rfunc_t *f"
634 to a pointer to the function that reads characters, or to
635 .Dv EL_BUILTIN_GETCFN
636 if the builtin function is in use.
637 .It Dv EL_CLIENTDATA , Fa "void **data"
640 to the previously registered client data set by an
643 .It Dv EL_UNBUFFERED , Fa "int *c"
646 to non-zero if unbuffered mode is enabled.
647 .It Dv EL_SAFEREAD , Fa "int *c"
650 to non-zero if safe read is set.
651 .It Dv EL_GETFP , Fa "int fd", Fa "FILE **fp"
674 by reading the contents of
677 is called for each line in
685 and if that is not set
689 for details on the format of
692 returns 0 on success and \-1 on error.
694 Must be called if the terminal size changes.
699 then this is done automatically.
700 Otherwise, it's the responsibility of the application to call
702 on the appropriate occasions.
704 Move the cursor to the right (if positive) or to the left (if negative)
707 Returns the resulting offset of the cursor from the beginning of the line.
709 Return the editing information for the current line in a
711 structure, which is defined as follows:
713 typedef struct lineinfo {
714 const char *buffer; /* address of buffer */
715 const char *cursor; /* address of cursor */
716 const char *lastchar; /* address of last character */
721 is not NUL terminated.
722 This function may be called after
726 structure pertaining to line returned by that function,
727 and from within user defined functions added with
732 into the line at the cursor.
735 is empty or won't fit, and 0 otherwise.
739 characters before the cursor.
741 .Sh HISTORY LIST FUNCTIONS
742 The history functions use a common data structure,
749 The following functions are available:
752 Initialize the history list, and return a data structure
753 to be used by all other history list functions, or
757 Clean up and finish with
759 assumed to have been created with
764 on the history list, with optional arguments as needed by the
767 is changed accordingly to operation.
768 The following values for
770 are supported, along with the required argument list:
772 .It Dv H_SETSIZE , Fa "int size"
773 Set size of history to
777 Get number of events currently in history.
779 Cleans up and finishes with
781 assumed to be created with
785 .It Dv H_FUNC , Fa "void *ptr" , Fa "history_gfun_t first" , \
786 Fa "history_gfun_t next" , Fa "history_gfun_t last" , \
787 Fa "history_gfun_t prev" , Fa "history_gfun_t curr" , \
788 Fa "history_sfun_t set" , Fa "history_vfun_t clear" , \
789 Fa "history_efun_t enter" , Fa "history_efun_t add"
790 Define functions to perform various history operations.
792 is the argument given to a function when it's invoked.
794 Return the first element in the history.
796 Return the last element in the history.
798 Return the previous element in the history.
799 It is newer than the current one.
801 Return the next element in the history.
802 It is older than the current one.
804 Return the current element in the history.
805 .It Dv H_SET , Fa "int position"
806 Set the cursor to point to the requested element.
807 .It Dv H_ADD , Fa "const char *str"
810 to the current element of the history, or perform the
812 operation with argument
814 if there is no current element.
815 .It Dv H_APPEND , Fa "const char *str"
818 to the last new element of the history.
819 .It Dv H_ENTER , Fa "const char *str"
822 as a new element to the history and, if necessary,
823 removing the oldest entry to keep the list to the created size.
826 has been called with a non-zero argument, the element
827 will not be entered into the history if its contents match
828 the ones of the current history element.
829 If the element is entered
831 returns 1; if it is ignored as a duplicate returns 0.
834 returns \-1 if an error occurred.
835 .It Dv H_PREV_STR , Fa "const char *str"
836 Return the closest previous event that starts with
838 .It Dv H_NEXT_STR , Fa "const char *str"
839 Return the closest next event that starts with
841 .It Dv H_PREV_EVENT , Fa "int e"
842 Return the previous event numbered
844 .It Dv H_NEXT_EVENT , Fa "int e"
845 Return the next event numbered
847 .It Dv H_LOAD , Fa "const char *file"
848 Load the history list stored in
850 .It Dv H_SAVE , Fa "const char *file"
851 Save the history list to
853 .It Dv H_SAVE_FP , Fa "FILE *fp"
854 Save the history list to the opened
858 .It Dv H_NSAVE_FP , Fa "size_t n" , Fa "FILE *fp"
861 history entries to the opened
865 .It Dv H_SETUNIQUE , Fa "int unique"
866 Set flag that adjacent identical event strings should not be entered
869 Retrieve the current setting if adjacent identical elements should
870 be entered into the history.
871 .It Dv H_DEL , Fa "int e"
872 Delete the event numbered
874 This function is only provided for
877 The caller is responsible for free'ing the string in the returned
882 returns >= 0 if the operation
885 Otherwise, \-1 is returned and
887 is updated to contain more details about the error.
889 .Sh TOKENIZATION FUNCTIONS
890 The tokenization functions use a common data structure,
897 The following functions are available:
900 Initialize the tokenizer, and return a data structure
901 to be used by all other tokenizer functions.
903 contains the Input Field Separators, which defaults to
911 Clean up and finish with
913 assumed to have been created with
916 Reset the tokenizer state.
917 Use after a line has been successfully tokenized
922 and before a new line is to be tokenized.
926 If successful, modify:
928 to contain the words,
930 to contain the number of words,
934 to contain the index of the word containing the cursor,
939 to contain the offset within
945 \-1 for an internal error,
946 1 for an unmatched single quote,
947 2 for an unmatched double quote,
949 3 for a backslash quoted
951 A positive exit code indicates that another line should be read
952 and tokenization attempted again.
958 is a NUL terminated string to tokenize.
962 .\"XXX: provide some examples
973 library first appeared in
978 .Dv CC_REFRESH_BEEP ,
980 and the readline emulation appeared in
989 library was written by
990 .An Christos Zoulas .
992 wrote this manual and implemented
994 .Dv CC_REFRESH_BEEP ,
999 implemented the readline emulation.
1001 implemented wide-character support.
1003 At this time, it is the responsibility of the caller to
1004 check the result of the
1014 should be used for further input.
1017 is purely an indication of the result of the most recent