1 .\" $NetBSD: editline.3,v 1.85 2015/11/03 21:36:59 christos 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"
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
178 .Sh LINE EDITING FUNCTIONS
179 The line editing functions use a common data structure,
188 The wide-character functions behave the same way as their narrow
191 The following functions are available:
194 Initialize the line editor, and return a data structure
195 to be used by all other line editing functions, or
199 is the name of the invoking program, used when reading the
201 file to determine which settings to use.
206 are the input, output, and error streams (respectively) to use.
207 In this documentation, references to
209 are actually to this input/output stream combination.
213 but allows specifying file descriptors for the
215 corresponding streams, in case those were created with
218 Clean up and finish with
220 assumed to have been created with
225 Reset the tty and the parser.
226 This should be called after an error which may have upset the tty's
229 Read a line from the tty.
231 is modified to contain the number of characters read.
232 Returns the line read if successful, or
234 if no characters were read or if an error occurred.
235 If an error occurred,
239 contains the error code that caused it.
240 The return value may not remain valid across calls to
242 and must be copied if the data is to be retained.
244 Read a character from the tty.
246 is modified to contain the character read.
247 Returns the number of characters read if successful, \-1 otherwise,
250 can be inspected for the cause.
254 back onto the input stream.
255 This is used by the macro expansion mechanism.
256 Refer to the description of
261 for more information.
271 If the command is prefixed with
275 will only execute the command if
282 \-1 if the command is unknown,
283 0 if there was no error or
286 1 if the command returned an error.
289 for more information.
295 determines which parameter to set, and each operation has its
297 Returns 0 on success, \-1 on failure.
299 The following values for
301 are supported, along with the required argument list:
303 .It Dv EL_PROMPT , Fa "char *(*f)(EditLine *)"
304 Define prompt printing function as
306 which is to return a string that contains the prompt.
307 .It Dv EL_PROMPT_ESC , Fa "char *(*f)(EditLine *)" , Fa "char c"
312 argument indicates the start/stop literal prompt character.
314 If a start/stop literal character is found in the prompt, the
316 is not printed, but characters after it are printed directly to the
317 terminal without affecting the state of the current line.
318 A subsequent second start/stop literal character ends this behavior.
319 This is typically used to embed literal escape sequences that change the
320 color/style of the terminal in the prompt.
324 Re-display the current line on the next terminal line.
325 .It Dv EL_RPROMPT , Fa "char *(*f)(EditLine *)"
326 Define right side prompt printing function as
328 which is to return a string that contains the prompt.
329 .It Dv EL_RPROMPT_ESC , Fa "char *(*f)(EditLine *)" , Fa "char c"
330 Define the right prompt printing function but with a literal escape character.
331 .It Dv EL_TERMINAL , Fa "const char *type"
332 Define terminal type of the tty to be
340 .It Dv EL_EDITOR , Fa "const char *mode"
347 .It Dv EL_SIGNAL , Fa "int flag"
352 will install its own signal handler for the following signals when
353 reading command input:
363 Otherwise, the current signal handlers will be used.
364 .It Dv EL_BIND , Fa "const char *" , Fa "..." , Dv NULL
370 for more information.
371 .It Dv EL_ECHOTC , Fa "const char *" , Fa "..." , Dv NULL
377 for more information.
378 .It Dv EL_SETTC , Fa "const char *" , Fa "..." , Dv NULL
384 for more information.
385 .It Dv EL_SETTY , Fa "const char *" , Fa "..." , Dv NULL
391 for more information.
392 .It Dv EL_TELLTC , Fa "const char *" , Fa "..." , Dv NULL
398 for more information.
399 .It Dv EL_ADDFN , Fa "const char *name" , Fa "const char *help" , \
400 Fa "unsigned char (*func)(EditLine *e, int ch)"
401 Add a user defined function,
405 which is invoked when a key which is bound to
413 is the key which caused the invocation.
417 .Bl -tag -width "CC_REDISPLAY"
419 Add a normal character.
421 End of line was entered.
425 Expecting further command input as arguments, do nothing visually.
428 .It Dv CC_REFRESH_BEEP
429 Refresh display, and beep.
431 Cursor moved, so update and perform
434 Redisplay entire input line.
435 This is useful if a key binding outputs extra information.
440 Fatal error, reset tty to known state.
442 .It Dv EL_HIST , Fa "History *(*func)(History *, int op, ...)" , \
444 Defines which history function to use, which is usually
447 should be the value returned by
449 .It Dv EL_EDITMODE , Fa "int flag"
453 editing is enabled (the default).
454 Note that this is only an indication, and does not
455 affect the operation of
457 At this time, it is the caller's responsibility to
461 to determine if editing should be enabled or not.
462 .It Dv EL_UNBUFFERED , Fa "int flag"
466 unbuffered mode is disabled (the default).
469 will return immediately after processing a single character.
470 .It Dv EL_GETCFN , Fa "int (*f)(EditLine *, char *c)"
471 Define the character reading function as
473 which is to return the number of characters read and store them in
475 This function is called internally by
479 The builtin function can be set or restored with the special function
481 .Dq Dv EL_BUILTIN_GETCFN .
482 .It Dv EL_CLIENTDATA , Fa "void *data"
485 to be associated with this EditLine structure.
486 It can be retrieved with the corresponding
489 .It Dv EL_SETFP , Fa "int fd" , Fa "FILE *fp"
514 determines which parameter to retrieve into
516 Returns 0 if successful, \-1 otherwise.
518 The following values for
520 are supported, along with actual type of
523 .It Dv EL_PROMPT , Fa "char *(*f)(EditLine *)" , Fa "char *c"
526 to a pointer to the function that displays the prompt.
531 set it to the start/stop literal prompt character.
532 .It Dv EL_RPROMPT , Fa "char *(*f)(EditLine *)" , Fa "char *c"
535 to a pointer to the function that displays the prompt.
540 set it to the start/stop literal prompt character.
541 .It Dv EL_EDITOR , Fa "const char **n"
542 Set the name of the editor in
548 .It Dv EL_GETTC , Fa "const char *name" , Fa "void *value"
555 to the current value of that capability.
556 .It Dv EL_SIGNAL , Fa "int *s"
561 has installed private signal handlers (see
564 .It Dv EL_EDITMODE , Fa "int *c"
567 to non-zero if editing is enabled.
568 .It Dv EL_GETCFN , Fa "int (**f)(EditLine *, char *)"
569 Return a pointer to the function that read characters, which is equal to
570 .Dq Dv EL_BUILTIN_GETCFN
571 in the case of the default builtin function.
572 .It Dv EL_CLIENTDATA , Fa "void **data"
575 to the previously registered client data set by an
578 .It Dv EL_UNBUFFERED , Fa "int *c"
581 to non-zero if unbuffered mode is enabled.
582 .It Dv EL_GETFP , Fa "int fd", Fa "FILE **fp"
605 by reading the contents of
608 is called for each line in
618 for details on the format of
621 returns 0 on success and \-1 on error.
623 Must be called if the terminal size changes.
628 then this is done automatically.
629 Otherwise, it's the responsibility of the application to call
631 on the appropriate occasions.
633 Move the cursor to the right (if positive) or to the left (if negative)
636 Returns the resulting offset of the cursor from the beginning of the line.
638 Return the editing information for the current line in a
640 structure, which is defined as follows:
642 typedef struct lineinfo {
643 const char *buffer; /* address of buffer */
644 const char *cursor; /* address of cursor */
645 const char *lastchar; /* address of last character */
650 is not NUL terminated.
651 This function may be called after
655 structure pertaining to line returned by that function,
656 and from within user defined functions added with
661 into the line at the cursor.
664 is empty or won't fit, and 0 otherwise.
668 characters before the cursor.
670 .Sh HISTORY LIST FUNCTIONS
671 The history functions use a common data structure,
678 The following functions are available:
681 Initialize the history list, and return a data structure
682 to be used by all other history list functions, or
686 Clean up and finish with
688 assumed to have been created with
693 on the history list, with optional arguments as needed by the
696 is changed accordingly to operation.
697 The following values for
699 are supported, along with the required argument list:
701 .It Dv H_SETSIZE , Fa "int size"
702 Set size of history to
706 Get number of events currently in history.
708 Cleans up and finishes with
710 assumed to be created with
714 .It Dv H_FUNC , Fa "void *ptr" , Fa "history_gfun_t first" , \
715 Fa "history_gfun_t next" , Fa "history_gfun_t last" , \
716 Fa "history_gfun_t prev" , Fa "history_gfun_t curr" , \
717 Fa "history_sfun_t set" , Fa "history_vfun_t clear" , \
718 Fa "history_efun_t enter" , Fa "history_efun_t add"
719 Define functions to perform various history operations.
721 is the argument given to a function when it's invoked.
723 Return the first element in the history.
725 Return the last element in the history.
727 Return the previous element in the history.
729 Return the next element in the history.
731 Return the current element in the history.
733 Set the cursor to point to the requested element.
734 .It Dv H_ADD , Fa "const char *str"
737 to the current element of the history, or perform the
739 operation with argument
741 if there is no current element.
742 .It Dv H_APPEND , Fa "const char *str"
745 to the last new element of the history.
746 .It Dv H_ENTER , Fa "const char *str"
749 as a new element to the history, and, if necessary,
750 removing the oldest entry to keep the list to the created size.
753 has been called with a non-zero argument, the element
754 will not be entered into the history if its contents match
755 the ones of the current history element.
756 If the element is entered
758 returns 1; if it is ignored as a duplicate returns 0.
761 returns \-1 if an error occurred.
762 .It Dv H_PREV_STR , Fa "const char *str"
763 Return the closest previous event that starts with
765 .It Dv H_NEXT_STR , Fa "const char *str"
766 Return the closest next event that starts with
768 .It Dv H_PREV_EVENT , Fa "int e"
769 Return the previous event numbered
771 .It Dv H_NEXT_EVENT , Fa "int e"
772 Return the next event numbered
774 .It Dv H_LOAD , Fa "const char *file"
775 Load the history list stored in
777 .It Dv H_SAVE , Fa "const char *file"
778 Save the history list to
780 .It Dv H_SAVE_FP , Fa "FILE *fp"
781 Save the history list to the opened
785 .It Dv H_SETUNIQUE , Fa "int unique"
786 Set flag that adjacent identical event strings should not be entered
789 Retrieve the current setting if adjacent identical elements should
790 be entered into the history.
791 .It Dv H_DEL , Fa "int e"
792 Delete the event numbered
794 This function is only provided for
797 The caller is responsible for free'ing the string in the returned
802 returns \*[Gt]= 0 if the operation
805 Otherwise, \-1 is returned and
807 is updated to contain more details about the error.
809 .Sh TOKENIZATION FUNCTIONS
810 The tokenization functions use a common data structure,
817 The following functions are available:
820 Initialize the tokenizer, and return a data structure
821 to be used by all other tokenizer functions.
823 contains the Input Field Separators, which defaults to
831 Clean up and finish with
833 assumed to have been created with
836 Reset the tokenizer state.
837 Use after a line has been successfully tokenized
842 and before a new line is to be tokenized.
846 If successful, modify:
848 to contain the words,
850 to contain the number of words,
854 to contain the index of the word containing the cursor,
859 to contain the offset within
865 \-1 for an internal error,
866 1 for an unmatched single quote,
867 2 for an unmatched double quote,
869 3 for a backslash quoted
871 A positive exit code indicates that another line should be read
872 and tokenization attempted again.
878 is a NUL terminated string to tokenize.
882 .\"XXX: provide some examples
892 library first appeared in
897 .Dv CC_REFRESH_BEEP ,
899 and the readline emulation appeared in
908 library was written by
909 .An Christos Zoulas .
911 wrote this manual and implemented
913 .Dv CC_REFRESH_BEEP ,
918 implemented the readline emulation.
920 implemented wide-character support.
922 At this time, it is the responsibility of the caller to
923 check the result of the
933 should be used for further input.
936 is purely an indication of the result of the most recent