1 .\" $NetBSD: editline.3,v 1.88 2016/02/25 14:59:22 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.
77 .Nd line editor, history and tokenization functions
83 .Fn el_init "const char *prog" "FILE *fin" "FILE *fout" "FILE *ferr"
85 .Fn el_init_fd "const char *prog" "FILE *fin" "FILE *fout" "FILE *ferr" "int fdin" "int fdout" "int fderr"
87 .Fn el_end "EditLine *e"
89 .Fn el_reset "EditLine *e"
91 .Fn el_gets "EditLine *e" "int *count"
93 .Fn el_wgets "EditLine *e" "int *count"
95 .Fn el_getc "EditLine *e" "char *ch"
97 .Fn el_wgetc "EditLine *e" "wchar_t *ch"
99 .Fn el_push "EditLine *e" "const char *str"
101 .Fn el_wpush "EditLine *e" "const wchar_t *str"
103 .Fn el_parse "EditLine *e" "int argc" "const char *argv[]"
105 .Fn el_wparse "EditLine *e" "int argc" "const wchar_t *argv[]"
107 .Fn el_set "EditLine *e" "int op" "..."
109 .Fn el_wset "EditLine *e" "int op" "..."
111 .Fn el_get "EditLine *e" "int op" "..."
113 .Fn el_wget "EditLine *e" "int op" "..."
115 .Fn el_source "EditLine *e" "const char *file"
117 .Fn el_resize "EditLine *e"
119 .Fn el_cursor "EditLine *e" "int count"
121 .Fn el_line "EditLine *e"
122 .Ft const LineInfoW *
123 .Fn el_wline "EditLine *e"
125 .Fn el_insertstr "EditLine *e" "const char *str"
127 .Fn el_winsertstr "EditLine *e" "const wchar_t *str"
129 .Fn el_deletestr "EditLine *e" "int count"
131 .Fn el_wdeletestr "EditLine *e" "int count"
133 .Fn history_init void
135 .Fn history_winit void
137 .Fn history_end "History *h"
139 .Fn history_wend "HistoryW *h"
141 .Fn history "History *h" "HistEvent *ev" "int op" "..."
143 .Fn history_w "HistoryW *h" "HistEventW *ev" "int op" "..."
145 .Fn tok_init "const char *IFS"
147 .Fn tok_winit "const wchar_t *IFS"
149 .Fn tok_end "Tokenizer *t"
151 .Fn tok_wend "TokenizerW *t"
153 .Fn tok_reset "Tokenizer *t"
155 .Fn tok_wreset "TokenizerW *t"
157 .Fn tok_line "Tokenizer *t" "const LineInfo *li" "int *argc" "const char **argv[]" "int *cursorc" "int *cursoro"
159 .Fn tok_wline "TokenizerW *t" "const LineInfoW *li" "int *argc" "const wchar_t **argv[]" "int *cursorc" "int *cursoro"
161 .Fn tok_str "Tokenizer *t" "const char *str" "int *argc" "const char **argv[]"
163 .Fn tok_wstr "TokenizerW *t" "const wchar_t *str" "int *argc" "const wchar_t **argv[]"
167 library provides generic line editing, history and tokenization functions,
168 similar to those found in
171 These functions are available in the
173 library (which needs the
176 Programs should be linked with
183 locale set by the application program and never uses
185 to change the locale.
186 The only locales supported are UTF-8 and the default C or POSIX locale.
187 If any other locale is set, behaviour is undefined.
188 .Sh LINE EDITING FUNCTIONS
189 The line editing functions use a common data structure,
198 The wide-character functions behave the same way as their narrow
201 The following functions are available:
204 Initialize the line editor, and return a data structure
205 to be used by all other line editing functions, or
209 is the name of the invoking program, used when reading the
211 file to determine which settings to use.
216 are the input, output, and error streams (respectively) to use.
217 In this documentation, references to
219 are actually to this input/output stream combination.
223 but allows specifying file descriptors for the
225 corresponding streams, in case those were created with
228 Clean up and finish with
230 assumed to have been created with
235 Reset the tty and the parser.
236 This should be called after an error which may have upset the tty's
239 Read a line from the tty.
241 is modified to contain the number of characters read.
242 Returns the line read if successful, or
244 if no characters were read or if an error occurred.
245 If an error occurred,
249 contains the error code that caused it.
250 The return value may not remain valid across calls to
252 and must be copied if the data is to be retained.
254 Read a wide character from the tty, respecting the current locale,
255 or from the input stream written by
259 if that is not empty, and store it in
261 If an invalid or incomplete character is found, it is discarded,
265 and the next character is read and stored in
267 Returns 1 if a valid character was read, 0 on end of file, or \-1 on
272 is set to indicate the error.
274 Read a wide character as described for
276 and return 0 on end of file or \-1 on failure.
277 If the wide character can be represented as a single-byte character,
282 and return 1; otherwise, set
287 In the C or POSIX locale, this simply reads a byte, but for any other
288 locale, including UTF-8, this is rarely useful.
292 back onto the input stream.
293 This is used by the macro expansion mechanism.
294 Refer to the description of
299 for more information.
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.
362 Re-display the current line on the next terminal line.
363 .It Dv EL_RPROMPT , Fa "char *(*f)(EditLine *)"
364 Define right side prompt printing function as
366 which is to return a string that contains the prompt.
367 .It Dv EL_RPROMPT_ESC , Fa "char *(*f)(EditLine *)" , Fa "char c"
368 Define the right prompt printing function but with a literal escape character.
369 .It Dv EL_TERMINAL , Fa "const char *type"
370 Define terminal type of the tty to be
378 .It Dv EL_EDITOR , Fa "const char *mode"
385 .It Dv EL_SIGNAL , Fa "int flag"
390 will install its own signal handler for the following signals when
391 reading command input:
401 Otherwise, the current signal handlers will be used.
402 .It Dv EL_BIND , Fa "const char *" , Fa "..." , Dv NULL
408 for more information.
409 .It Dv EL_ECHOTC , Fa "const char *" , Fa "..." , Dv NULL
415 for more information.
416 .It Dv EL_SETTC , Fa "const char *" , Fa "..." , Dv NULL
422 for more information.
423 .It Dv EL_SETTY , Fa "const char *" , Fa "..." , Dv NULL
429 for more information.
430 .It Dv EL_TELLTC , Fa "const char *" , Fa "..." , Dv NULL
436 for more information.
437 .It Dv EL_ADDFN , Fa "const char *name" , Fa "const char *help" , \
438 Fa "unsigned char (*func)(EditLine *e, int ch)"
439 Add a user defined function,
443 which is invoked when a key which is bound to
451 is the key which caused the invocation.
455 .Bl -tag -width "CC_REDISPLAY"
457 Add a normal character.
459 End of line was entered.
463 Expecting further command input as arguments, do nothing visually.
466 .It Dv CC_REFRESH_BEEP
467 Refresh display, and beep.
469 Cursor moved, so update and perform
472 Redisplay entire input line.
473 This is useful if a key binding outputs extra information.
478 Fatal error, reset tty to known state.
480 .It Dv EL_HIST , Fa "History *(*func)(History *, int op, ...)" , \
482 Defines which history function to use, which is usually
485 should be the value returned by
487 .It Dv EL_EDITMODE , Fa "int flag"
491 editing is enabled (the default).
492 Note that this is only an indication, and does not
493 affect the operation of
495 At this time, it is the caller's responsibility to
499 to determine if editing should be enabled or not.
500 .It Dv EL_UNBUFFERED , Fa "int flag"
504 unbuffered mode is disabled (the default).
507 will return immediately after processing a single character.
508 .It Dv EL_GETCFN , Fa "int (*f)(EditLine *, char *c)"
509 Define the character reading function as
511 which is to return the number of characters read and store them in
513 This function is called internally by
517 The builtin function can be set or restored with the special function
519 .Dq Dv EL_BUILTIN_GETCFN .
520 .It Dv EL_CLIENTDATA , Fa "void *data"
523 to be associated with this EditLine structure.
524 It can be retrieved with the corresponding
527 .It Dv EL_SETFP , Fa "int fd" , Fa "FILE *fp"
552 determines which parameter to retrieve into
554 Returns 0 if successful, \-1 otherwise.
556 The following values for
558 are supported, along with actual type of
561 .It Dv EL_PROMPT , Fa "char *(*f)(EditLine *)" , Fa "char *c"
564 to a pointer to the function that displays the prompt.
569 set it to the start/stop literal prompt character.
570 .It Dv EL_RPROMPT , Fa "char *(*f)(EditLine *)" , Fa "char *c"
573 to a pointer to the function that displays the prompt.
578 set it to the start/stop literal prompt character.
579 .It Dv EL_EDITOR , Fa "const char **n"
580 Set the name of the editor in
586 .It Dv EL_GETTC , Fa "const char *name" , Fa "void *value"
593 to the current value of that capability.
594 .It Dv EL_SIGNAL , Fa "int *s"
599 has installed private signal handlers (see
602 .It Dv EL_EDITMODE , Fa "int *c"
605 to non-zero if editing is enabled.
606 .It Dv EL_GETCFN , Fa "int (**f)(EditLine *, char *)"
607 Return a pointer to the function that read characters, which is equal to
608 .Dq Dv EL_BUILTIN_GETCFN
609 in the case of the default builtin function.
610 .It Dv EL_CLIENTDATA , Fa "void **data"
613 to the previously registered client data set by an
616 .It Dv EL_UNBUFFERED , Fa "int *c"
619 to non-zero if unbuffered mode is enabled.
620 .It Dv EL_GETFP , Fa "int fd", Fa "FILE **fp"
643 by reading the contents of
646 is called for each line in
656 for details on the format of
659 returns 0 on success and \-1 on error.
661 Must be called if the terminal size changes.
666 then this is done automatically.
667 Otherwise, it's the responsibility of the application to call
669 on the appropriate occasions.
671 Move the cursor to the right (if positive) or to the left (if negative)
674 Returns the resulting offset of the cursor from the beginning of the line.
676 Return the editing information for the current line in a
678 structure, which is defined as follows:
680 typedef struct lineinfo {
681 const char *buffer; /* address of buffer */
682 const char *cursor; /* address of cursor */
683 const char *lastchar; /* address of last character */
688 is not NUL terminated.
689 This function may be called after
693 structure pertaining to line returned by that function,
694 and from within user defined functions added with
699 into the line at the cursor.
702 is empty or won't fit, and 0 otherwise.
706 characters before the cursor.
708 .Sh HISTORY LIST FUNCTIONS
709 The history functions use a common data structure,
716 The following functions are available:
719 Initialize the history list, and return a data structure
720 to be used by all other history list functions, or
724 Clean up and finish with
726 assumed to have been created with
731 on the history list, with optional arguments as needed by the
734 is changed accordingly to operation.
735 The following values for
737 are supported, along with the required argument list:
739 .It Dv H_SETSIZE , Fa "int size"
740 Set size of history to
744 Get number of events currently in history.
746 Cleans up and finishes with
748 assumed to be created with
752 .It Dv H_FUNC , Fa "void *ptr" , Fa "history_gfun_t first" , \
753 Fa "history_gfun_t next" , Fa "history_gfun_t last" , \
754 Fa "history_gfun_t prev" , Fa "history_gfun_t curr" , \
755 Fa "history_sfun_t set" , Fa "history_vfun_t clear" , \
756 Fa "history_efun_t enter" , Fa "history_efun_t add"
757 Define functions to perform various history operations.
759 is the argument given to a function when it's invoked.
761 Return the first element in the history.
763 Return the last element in the history.
765 Return the previous element in the history.
767 Return the next element in the history.
769 Return the current element in the history.
770 .It Dv H_SET , Fa "int position"
771 Set the cursor to point to the requested element.
772 .It Dv H_ADD , Fa "const char *str"
775 to the current element of the history, or perform the
777 operation with argument
779 if there is no current element.
780 .It Dv H_APPEND , Fa "const char *str"
783 to the last new element of the history.
784 .It Dv H_ENTER , Fa "const char *str"
787 as a new element to the history and, if necessary,
788 removing the oldest entry to keep the list to the created size.
791 has been called with a non-zero argument, the element
792 will not be entered into the history if its contents match
793 the ones of the current history element.
794 If the element is entered
796 returns 1; if it is ignored as a duplicate returns 0.
799 returns \-1 if an error occurred.
800 .It Dv H_PREV_STR , Fa "const char *str"
801 Return the closest previous event that starts with
803 .It Dv H_NEXT_STR , Fa "const char *str"
804 Return the closest next event that starts with
806 .It Dv H_PREV_EVENT , Fa "int e"
807 Return the previous event numbered
809 .It Dv H_NEXT_EVENT , Fa "int e"
810 Return the next event numbered
812 .It Dv H_LOAD , Fa "const char *file"
813 Load the history list stored in
815 .It Dv H_SAVE , Fa "const char *file"
816 Save the history list to
818 .It Dv H_SAVE_FP , Fa "FILE *fp"
819 Save the history list to the opened
823 .It Dv H_SETUNIQUE , Fa "int unique"
824 Set flag that adjacent identical event strings should not be entered
827 Retrieve the current setting if adjacent identical elements should
828 be entered into the history.
829 .It Dv H_DEL , Fa "int e"
830 Delete the event numbered
832 This function is only provided for
835 The caller is responsible for free'ing the string in the returned
840 returns \*[Gt]= 0 if the operation
843 Otherwise, \-1 is returned and
845 is updated to contain more details about the error.
847 .Sh TOKENIZATION FUNCTIONS
848 The tokenization functions use a common data structure,
855 The following functions are available:
858 Initialize the tokenizer, and return a data structure
859 to be used by all other tokenizer functions.
861 contains the Input Field Separators, which defaults to
869 Clean up and finish with
871 assumed to have been created with
874 Reset the tokenizer state.
875 Use after a line has been successfully tokenized
880 and before a new line is to be tokenized.
884 If successful, modify:
886 to contain the words,
888 to contain the number of words,
892 to contain the index of the word containing the cursor,
897 to contain the offset within
903 \-1 for an internal error,
904 1 for an unmatched single quote,
905 2 for an unmatched double quote,
907 3 for a backslash quoted
909 A positive exit code indicates that another line should be read
910 and tokenization attempted again.
916 is a NUL terminated string to tokenize.
920 .\"XXX: provide some examples
930 library first appeared in
935 .Dv CC_REFRESH_BEEP ,
937 and the readline emulation appeared in
946 library was written by
947 .An Christos Zoulas .
949 wrote this manual and implemented
951 .Dv CC_REFRESH_BEEP ,
956 implemented the readline emulation.
958 implemented wide-character support.
960 At this time, it is the responsibility of the caller to
961 check the result of the
971 should be used for further input.
974 is purely an indication of the result of the most recent