1 .\" $OpenBSD: editline.3,v 1.46 2016/05/22 22:08:42 schwarze Exp $
2 .\" $NetBSD: editline.3,v 1.88 2016/02/25 14:59:22 wiz Exp $
4 .\" Copyright (c) 1997-2003 The NetBSD Foundation, Inc.
5 .\" All rights reserved.
7 .\" This file was contributed to The NetBSD Foundation by Luke Mewburn.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
30 .Dd $Mdocdate: May 22 2016 $
74 .Nd line editor, history and tokenization functions
78 .Fn el_init "const char *prog" "FILE *fin" "FILE *fout" "FILE *ferr"
80 .Fn el_end "EditLine *e"
82 .Fn el_reset "EditLine *e"
84 .Fn el_gets "EditLine *e" "int *count"
86 .Fn el_wgets "EditLine *e" "int *count"
88 .Fn el_getc "EditLine *e" "char *ch"
90 .Fn el_wgetc "EditLine *e" "wchar_t *wc"
92 .Fn el_push "EditLine *e" "const char *mbs"
94 .Fn el_wpush "EditLine *e" "const wchar_t *wcs"
96 .Fn el_parse "EditLine *e" "int argc" "const char *argv[]"
98 .Fn el_wparse "EditLine *e" "int argc" "const wchar_t *argv[]"
100 .Fn el_set "EditLine *e" "int op" "..."
102 .Fn el_wset "EditLine *e" "int op" "..."
104 .Fn el_get "EditLine *e" "int op" "..."
106 .Fn el_wget "EditLine *e" "int op" "..."
108 .Fn el_source "EditLine *e" "const char *file"
110 .Fn el_resize "EditLine *e"
112 .Fn el_line "EditLine *e"
113 .Ft const LineInfoW *
114 .Fn el_wline "EditLine *e"
116 .Fn el_insertstr "EditLine *e" "const char *str"
118 .Fn el_winsertstr "EditLine *e" "const wchar_t *str"
120 .Fn el_deletestr "EditLine *e" "int count"
122 .Fn el_wdeletestr "EditLine *e" "int count"
124 .Fn history_init void
126 .Fn history_winit void
128 .Fn history_end "History *h"
130 .Fn history_wend "HistoryW *h"
132 .Fn history "History *h" "HistEvent *ev" "int op" "..."
134 .Fn history_w "HistoryW *h" "HistEventW *ev" "int op" "..."
136 .Fn tok_init "const char *IFS"
138 .Fn tok_winit "const wchar_t *IFS"
140 .Fn tok_end "Tokenizer *t"
142 .Fn tok_wend "TokenizerW *t"
144 .Fn tok_reset "Tokenizer *t"
146 .Fn tok_wreset "TokenizerW *t"
148 .Fn tok_line "Tokenizer *t" "const LineInfo *li" "int *argc" "const char **argv[]" "int *cursorc" "int *cursoro"
150 .Fn tok_wline "TokenizerW *t" "const LineInfoW *li" "int *argc" "const wchar_t **argv[]" "int *cursorc" "int *cursoro"
152 .Fn tok_str "Tokenizer *t" "const char *str" "int *argc" "const char **argv[]"
154 .Fn tok_wstr "TokenizerW *t" "const wchar_t *str" "int *argc" "const wchar_t **argv[]"
158 library provides generic line editing, history and tokenization functions,
159 similar to those found in
162 These functions are available in the
164 library (which needs the
167 Programs should be linked with
174 locale set by the application program and never uses
176 to change the locale.
177 The only locales supported are UTF-8 and the default C or POSIX locale.
178 If any other locale is set, behaviour is undefined.
179 .Sh LINE EDITING FUNCTIONS
180 The line editing functions use a common data structure,
187 The wide-character functions behave the same way as their narrow
190 The following functions are available:
193 Initialize the line editor, and return a data structure
194 to be used by all other line editing functions, or
198 is the name of the invoking program, used when reading the
200 file to determine which settings to use.
205 are the input, output, and error streams (respectively) to use.
206 In this documentation, references to
208 are actually to this input/output stream combination.
210 Clean up and finish with
212 assumed to have been created with
215 Reset the tty and the parser.
216 This should be called after an error which may have upset the tty's
219 Read a line from the tty.
221 is modified to contain the number of characters read.
222 Returns the line read if successful, or
224 if no characters were read or if an error occurred.
225 If an error occurred,
229 contains the error code that caused it.
230 The return value may not remain valid across calls to
232 and must be copied if the data is to be retained.
234 Read a wide character from the tty, respecting the current locale,
235 or from the input queue described in
237 if that is not empty, and store it in
239 If an invalid or incomplete character is found, it is discarded,
243 and the next character is read and stored in
245 Returns 1 if a valid character was read, 0 on end of file, or \-1 on
250 is set to indicate the error.
252 Read a wide character as described for
254 and return 0 on end of file or \-1 on failure.
255 If the wide character can be represented as a single-byte character,
260 and return 1; otherwise, set
265 In the C or POSIX locale, this simply reads a byte, but for any other
266 locale, including UTF-8, this is rarely useful.
268 Push the wide character string
270 back onto the input queue described in
272 If the queue overflows, for example due to a recursive macro,
273 or if an error occurs, for example because
277 or memory allocation fails, the function beeps at the user,
278 but does not report the problem to the caller.
280 Use the current locale to convert the multibyte string
282 to a wide character string, and pass the result to
293 If the command is prefixed with
297 will only execute the command if
304 \-1 if the command is unknown,
305 0 if there was no error or
308 1 if the command returned an error.
311 for more information.
317 determines which parameter to set, and each operation has its
319 Returns 0 on success, \-1 on failure.
321 The following values for
323 are supported, along with the required argument list:
325 .It Dv EL_PROMPT , Fa "char *(*f)(EditLine *)"
326 Define prompt printing function as
328 which is to return a string that contains the prompt.
329 .It Dv EL_PROMPT_ESC , Fa "char *(*f)(EditLine *)" , Fa "char c"
334 argument indicates the start/stop literal prompt character.
336 If a start/stop literal character is found in the prompt, the
338 is not printed, but characters after it are printed directly to the
339 terminal without affecting the state of the current line.
340 A subsequent second start/stop literal character ends this behavior.
341 This is typically used to embed literal escape sequences that change the
342 color/style of the terminal in the prompt.
346 Re-display the current line on the next terminal line.
347 .It Dv EL_RPROMPT , Fa "char *(*f)(EditLine *)"
348 Define right side prompt printing function as
350 which is to return a string that contains the prompt.
351 .It Dv EL_RPROMPT_ESC , Fa "char *(*f)(EditLine *)" , Fa "char c"
352 Define the right prompt printing function but with a literal escape character.
353 .It Dv EL_TERMINAL , Fa "const char *type"
354 Define terminal type of the tty to be
362 .It Dv EL_EDITOR , Fa "const char *mode"
369 .It Dv EL_SIGNAL , Fa "int flag"
374 will install its own signal handler for the following signals when
375 reading command input:
385 Otherwise, the current signal handlers will be used.
386 .It Dv EL_BIND , Fa "const char *" , Fa "..." , Dv NULL
392 for more information.
393 .It Dv EL_ECHOTC , Fa "const char *" , Fa "..." , Dv NULL
399 for more information.
400 .It Dv EL_SETTC , Fa "const char *" , Fa "..." , Dv NULL
406 for more information.
407 .It Dv EL_SETTY , Fa "const char *" , Fa "..." , Dv NULL
413 for more information.
414 .It Dv EL_TELLTC , Fa "const char *" , Fa "..." , Dv NULL
420 for more information.
421 .It Dv EL_ADDFN , Fa "const char *name" , Fa "const char *help" , \
422 Fa "unsigned char (*func)(EditLine *e, int ch)"
423 Add a user defined function,
427 which is invoked when a key which is bound to
435 is the key which caused the invocation.
439 .Bl -tag -width "CC_REDISPLAY"
441 Add a normal character.
443 End of line was entered.
447 Expecting further command input as arguments, do nothing visually.
450 .It Dv CC_REFRESH_BEEP
451 Refresh display, and beep.
453 Cursor moved, so update and perform
456 Redisplay entire input line.
457 This is useful if a key binding outputs extra information.
462 Fatal error, reset tty to known state.
464 .It Dv EL_HIST , Fa "History *(*func)(History *, int op, ...)" , \
466 Defines which history function to use, which is usually
469 should be the value returned by
471 .It Dv EL_EDITMODE , Fa "int flag"
475 editing is enabled (the default).
476 Note that this is only an indication, and does not
477 affect the operation of
479 At this time, it is the caller's responsibility to
483 to determine if editing should be enabled or not.
484 .It Dv EL_UNBUFFERED , Fa "int flag"
488 unbuffered mode is disabled (the default).
491 will return immediately after processing a single character.
492 .It Dv EL_GETCFN , Fa "el_rfunc_t f"
493 Whenever reading a character, use the function
494 .Bd -ragged -offset indent -compact
501 which stores the character in
503 and returns 1 on success, 0 on end of file, or \-1 on I/O or encoding
505 Functions internally using it include
511 Initially, a builtin function is installed, and replacing it
512 is discouraged because writing such a function is very error prone.
513 The builtin function can be restored at any time by passing the
515 .Dv EL_BUILTIN_GETCFN
516 instead of a function pointer.
517 .It Dv EL_CLIENTDATA , Fa "void *data"
520 to be associated with this EditLine structure.
521 It can be retrieved with the corresponding
524 .It Dv EL_SETFP , Fa "int fd" , Fa "FILE *fp"
549 determines which parameter to retrieve into
551 Returns 0 if successful, \-1 otherwise.
553 The following values for
555 are supported, along with actual type of
558 .It Dv EL_PROMPT , Fa "char *(*f)(EditLine *)" , Fa "char *c"
561 to a pointer to the function that displays the prompt.
566 set it to the start/stop literal prompt character.
567 .It Dv EL_RPROMPT , Fa "char *(*f)(EditLine *)" , Fa "char *c"
570 to a pointer to the function that displays the prompt.
575 set it to the start/stop literal prompt character.
576 .It Dv EL_EDITOR , Fa "const char **n"
577 Set the name of the editor in
583 .It Dv EL_GETTC , Fa "const char *name" , Fa "void *value"
590 to the current value of that capability.
591 .It Dv EL_SIGNAL , Fa "int *s"
596 has installed private signal handlers (see
599 .It Dv EL_EDITMODE , Fa "int *c"
602 to non-zero if editing is enabled.
603 .It Dv EL_GETCFN , Fa "el_rfunc_t *f"
606 to a pointer to the function that reads characters, or to
607 .Dv EL_BUILTIN_GETCFN
608 if the builtin function is in use.
609 .It Dv EL_CLIENTDATA , Fa "void **data"
612 to the previously registered client data set by an
615 .It Dv EL_UNBUFFERED , Fa "int *c"
618 to non-zero if unbuffered mode is enabled.
619 .It Dv EL_PREP_TERM , Fa "int"
620 Sets or clears terminal editing mode.
621 .It Dv EL_GETFP , Fa "int fd", Fa "FILE **fp"
644 by reading the contents of
647 is called for each line in
657 for details on the format of
660 returns 0 on success and \-1 on error.
662 Must be called if the terminal size changes.
667 then this is done automatically.
668 Otherwise, it's the responsibility of the application to call
670 on the appropriate occasions.
672 Return the editing information for the current line in a
674 structure, which is defined as follows:
676 typedef struct lineinfo {
677 const char *buffer; /* address of buffer */
678 const char *cursor; /* address of cursor */
679 const char *lastchar; /* address of last character */
684 is not NUL terminated.
685 This function may be called after
689 structure pertaining to line returned by that function,
690 and from within user defined functions added with
695 into the line at the cursor.
698 is empty or won't fit, and 0 otherwise.
702 characters before the cursor.
704 .Sh HISTORY LIST FUNCTIONS
705 The history functions use a common data structure,
712 The following functions are available:
715 Initialize the history list, and return a data structure
716 to be used by all other history list functions, or
720 Clean up and finish with
722 assumed to have been created with
727 on the history list, with optional arguments as needed by the
730 is changed accordingly to operation.
731 The following values for
733 are supported, along with the required argument list:
735 .It Dv H_SETSIZE , Fa "int size"
736 Set size of history to
740 Get number of events currently in history.
742 Cleans up and finishes with
744 assumed to be created with
748 .It Dv H_FUNC , Fa "void *ptr" , Fa "history_gfun_t first" , \
749 Fa "history_gfun_t next" , Fa "history_gfun_t last" , \
750 Fa "history_gfun_t prev" , Fa "history_gfun_t curr" , \
751 Fa "history_sfun_t set" , Fa "history_vfun_t clear" , \
752 Fa "history_efun_t enter" , Fa "history_efun_t add"
753 Define functions to perform various history operations.
755 is the argument given to a function when it's invoked.
757 Return the first element in the history.
759 Return the last element in the history.
761 Return the previous element in the history.
762 It is newer than the current one.
764 Return the next element in the history.
765 It is older than the current one.
767 Return the current element in the history.
769 Set the cursor to point to the requested element.
770 .It Dv H_ADD , Fa "const char *str"
773 to the current element of the history, or perform the
775 operation with argument
777 if there is no current element.
778 .It Dv H_APPEND , Fa "const char *str"
781 to the last new element of the history.
782 .It Dv H_ENTER , Fa "const char *str"
785 as a new element to the history and, if necessary,
786 removing the oldest entry to keep the list to the created size.
789 has been called with a non-zero argument, the element
790 will not be entered into the history if its contents match
791 the ones of the current history element.
792 If the element is entered
794 returns 1; if it is ignored as a duplicate returns 0.
797 returns \-1 if an error occurred.
798 .It Dv H_PREV_STR , Fa "const char *str"
799 Return the closest previous event that starts with
801 .It Dv H_NEXT_STR , Fa "const char *str"
802 Return the closest next event that starts with
804 .It Dv H_PREV_EVENT , Fa "int e"
805 Return the previous event numbered
807 .It Dv H_NEXT_EVENT , Fa "int e"
808 Return the next event numbered
810 .It Dv H_LOAD , Fa "const char *file"
811 Load the history list stored in
813 .It Dv H_SAVE , Fa "const char *file"
814 Save the history list to
816 .It Dv H_SAVE_FP , Fa "FILE *fp"
817 Save the history list to the opened
821 .It Dv H_SETUNIQUE , Fa "int unique"
822 Set flag that adjacent identical event strings should not be entered
825 Retrieve the current setting if adjacent identical elements should
826 be entered into the history.
827 .It Dv H_DEL , Fa "int e"
828 Delete the event numbered
830 This function is only provided for
833 The caller is responsible for free'ing the string in the returned
838 returns \*[Gt]= 0 if the operation
841 Otherwise, \-1 is returned and
843 is updated to contain more details about the error.
845 .Sh TOKENIZATION FUNCTIONS
846 The tokenization functions use a common data structure,
853 The following functions are available:
856 Initialize the tokenizer, and return a data structure
857 to be used by all other tokenizer functions.
859 contains the Input Field Separators, which defaults to
867 Clean up and finish with
869 assumed to have been created with
872 Reset the tokenizer state.
873 Use after a line has been successfully tokenized
878 and before a new line is to be tokenized.
882 If successful, modify:
884 to contain the words,
886 to contain the number of words,
890 to contain the index of the word containing the cursor,
895 to contain the offset within
901 \-1 for an internal error,
902 1 for an unmatched single quote,
903 2 for an unmatched double quote,
905 3 for a backslash quoted
907 A positive exit code indicates that another line should be read
908 and tokenization attempted again.
914 is a NUL terminated string to tokenize.
918 .\"XXX: provide some examples
929 library first appeared in
934 .Dv CC_REFRESH_BEEP ,
936 and the readline emulation appeared in
945 library was written by
946 .An Christos Zoulas .
948 wrote this manual and implemented
950 .Dv CC_REFRESH_BEEP ,
955 implemented the readline emulation.
957 implemented wide-character support.
959 At this time, it is the responsibility of the caller to
960 check the result of the
970 should be used for further input.
973 is purely an indication of the result of the most recent