1 .\" $NetBSD: editline.3,v 1.84 2014/12/25 13:39:41 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 *ch"
97 .Fn el_push "EditLine *e" "const char *str"
99 .Fn el_wpush "EditLine *e" "const wchar_t *str"
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"
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
176 .Sh LINE EDITING FUNCTIONS
177 The line editing functions use a common data structure,
186 The wide-character functions behave the same way as their narrow
189 The following functions are available:
192 Initialise the line editor, and return a data structure
193 to be used by all other line editing functions, or
197 is the name of the invoking program, used when reading the
199 file to determine which settings to use.
204 are the input, output, and error streams (respectively) to use.
205 In this documentation, references to
207 are actually to this input/output stream combination.
211 but allows specifying file descriptors for the
213 corresponding streams, in case those were created with
216 Clean up and finish with
218 assumed to have been created with
223 Reset the tty and the parser.
224 This should be called after an error which may have upset the tty's
227 Read a line from the tty.
229 is modified to contain the number of characters read.
230 Returns the line read if successful, or
232 if no characters were read or if an error occurred.
233 If an error occurred,
237 contains the error code that caused it.
238 The return value may not remain valid across calls to
240 and must be copied if the data is to be retained.
242 Read a character from the tty.
244 is modified to contain the character read.
245 Returns the number of characters read if successful, \-1 otherwise,
248 can be inspected for the cause.
252 back onto the input stream.
253 This is used by the macro expansion mechanism.
254 Refer to the description of
259 for more information.
269 If the command is prefixed with
273 will only execute the command if
280 \-1 if the command is unknown,
281 0 if there was no error or
284 1 if the command returned an error.
287 for more information.
293 determines which parameter to set, and each operation has its
295 Returns 0 on success, \-1 on failure.
297 The following values for
299 are supported, along with the required argument list:
301 .It Dv EL_PROMPT , Fa "char *(*f)(EditLine *)"
302 Define prompt printing function as
304 which is to return a string that contains the prompt.
305 .It Dv EL_PROMPT_ESC , Fa "char *(*f)(EditLine *)" , Fa "char c"
310 argument indicates the start/stop literal prompt character.
312 If a start/stop literal character is found in the prompt, the
314 is not printed, but characters after it are printed directly to the
315 terminal without affecting the state of the current line.
316 A subsequent second start/stop literal character ends this behavior.
317 This is typically used to embed literal escape sequences that change the
318 color/style of the terminal in the prompt.
322 Re-display the current line on the next terminal line.
323 .It Dv EL_RPROMPT , Fa "char *(*f)(EditLine *)"
324 Define right side prompt printing function as
326 which is to return a string that contains the prompt.
327 .It Dv EL_RPROMPT_ESC , Fa "char *(*f)(EditLine *)" , Fa "char c"
328 Define the right prompt printing function but with a literal escape character.
329 .It Dv EL_TERMINAL , Fa "const char *type"
330 Define terminal type of the tty to be
338 .It Dv EL_EDITOR , Fa "const char *mode"
345 .It Dv EL_SIGNAL , Fa "int flag"
350 will install its own signal handler for the following signals when
351 reading command input:
361 Otherwise, the current signal handlers will be used.
362 .It Dv EL_BIND , Fa "const char *" , Fa "..." , Dv NULL
368 for more information.
369 .It Dv EL_ECHOTC , Fa "const char *" , Fa "..." , Dv NULL
375 for more information.
376 .It Dv EL_SETTC , Fa "const char *" , Fa "..." , Dv NULL
382 for more information.
383 .It Dv EL_SETTY , Fa "const char *" , Fa "..." , Dv NULL
389 for more information.
390 .It Dv EL_TELLTC , Fa "const char *" , Fa "..." , Dv NULL
396 for more information.
397 .It Dv EL_ADDFN , Fa "const char *name" , Fa "const char *help" , \
398 Fa "unsigned char (*func)(EditLine *e, int ch)"
399 Add a user defined function,
403 which is invoked when a key which is bound to
411 is the key which caused the invocation.
415 .Bl -tag -width "CC_REDISPLAY"
417 Add a normal character.
419 End of line was entered.
423 Expecting further command input as arguments, do nothing visually.
426 .It Dv CC_REFRESH_BEEP
427 Refresh display, and beep.
429 Cursor moved, so update and perform
432 Redisplay entire input line.
433 This is useful if a key binding outputs extra information.
438 Fatal error, reset tty to known state.
440 .It Dv EL_HIST , Fa "History *(*func)(History *, int op, ...)" , \
442 Defines which history function to use, which is usually
445 should be the value returned by
447 .It Dv EL_EDITMODE , Fa "int flag"
451 editing is enabled (the default).
452 Note that this is only an indication, and does not
453 affect the operation of
455 At this time, it is the caller's responsibility to
459 to determine if editing should be enabled or not.
460 .It Dv EL_UNBUFFERED , Fa "int flag"
464 unbuffered mode is disabled (the default).
467 will return immediately after processing a single character.
468 .It Dv EL_GETCFN , Fa "int (*f)(EditLine *, char *c)"
469 Define the character reading function as
471 which is to return the number of characters read and store them in
473 This function is called internally by
477 The builtin function can be set or restored with the special function
479 .Dq Dv EL_BUILTIN_GETCFN .
480 .It Dv EL_CLIENTDATA , Fa "void *data"
483 to be associated with this EditLine structure.
484 It can be retrieved with the corresponding
487 .It Dv EL_SETFP , Fa "int fd" , Fa "FILE *fp"
512 determines which parameter to retrieve into
514 Returns 0 if successful, \-1 otherwise.
516 The following values for
518 are supported, along with actual type of
521 .It Dv EL_PROMPT , Fa "char *(*f)(EditLine *)" , Fa "char *c"
522 Return a pointer to the function that displays the prompt in
528 return the start/stop literal prompt character in it.
529 .It Dv EL_RPROMPT , Fa "char *(*f)(EditLine *)" , Fa "char *c"
530 Return a pointer to the function that displays the prompt in
536 return the start/stop literal prompt character in it.
537 .It Dv EL_EDITOR , Fa "const char **"
538 Return the name of the editor, which will be one of
542 .It Dv EL_GETTC , Fa "const char *name" , Fa "void *value"
550 to the current value of that capability.
551 .It Dv EL_SIGNAL , Fa "int *"
554 has installed private signal handlers (see
557 .It Dv EL_EDITMODE , Fa "int *"
558 Return non-zero if editing is enabled.
559 .It Dv EL_GETCFN , Fa "int (**f)(EditLine *, char *)"
560 Return a pointer to the function that read characters, which is equal to
561 .Dq Dv EL_BUILTIN_GETCFN
562 in the case of the default builtin function.
563 .It Dv EL_CLIENTDATA , Fa "void **data"
566 previously registered with the corresponding
569 .It Dv EL_UNBUFFERED , Fa "int"
570 Return non-zero if unbuffered mode is enabled.
571 .It Dv EL_PREP_TERM , Fa "int"
572 Sets or clears terminal editing mode.
573 .It Dv EL_GETFP , Fa "int fd", Fa "FILE **fp"
596 by reading the contents of
599 is called for each line in
609 for details on the format of
612 returns 0 on success and \-1 on error.
614 Must be called if the terminal size changes.
619 then this is done automatically.
620 Otherwise, it's the responsibility of the application to call
622 on the appropriate occasions.
624 Move the cursor to the right (if positive) or to the left (if negative)
627 Returns the resulting offset of the cursor from the beginning of the line.
629 Return the editing information for the current line in a
631 structure, which is defined as follows:
633 typedef struct lineinfo {
634 const char *buffer; /* address of buffer */
635 const char *cursor; /* address of cursor */
636 const char *lastchar; /* address of last character */
641 is not NUL terminated.
642 This function may be called after
646 structure pertaining to line returned by that function,
647 and from within user defined functions added with
652 into the line at the cursor.
655 is empty or won't fit, and 0 otherwise.
659 characters before the cursor.
661 .Sh HISTORY LIST FUNCTIONS
662 The history functions use a common data structure,
669 The following functions are available:
672 Initialise the history list, and return a data structure
673 to be used by all other history list functions, or
677 Clean up and finish with
679 assumed to have been created with
684 on the history list, with optional arguments as needed by the
687 is changed accordingly to operation.
688 The following values for
690 are supported, along with the required argument list:
692 .It Dv H_SETSIZE , Fa "int size"
693 Set size of history to
697 Get number of events currently in history.
699 Cleans up and finishes with
701 assumed to be created with
705 .It Dv H_FUNC , Fa "void *ptr" , Fa "history_gfun_t first" , \
706 Fa "history_gfun_t next" , Fa "history_gfun_t last" , \
707 Fa "history_gfun_t prev" , Fa "history_gfun_t curr" , \
708 Fa "history_sfun_t set" , Fa "history_vfun_t clear" , \
709 Fa "history_efun_t enter" , Fa "history_efun_t add"
710 Define functions to perform various history operations.
712 is the argument given to a function when it's invoked.
714 Return the first element in the history.
716 Return the last element in the history.
718 Return the previous element in the history.
720 Return the next element in the history.
722 Return the current element in the history.
724 Set the cursor to point to the requested element.
725 .It Dv H_ADD , Fa "const char *str"
728 to the current element of the history, or perform the
730 operation with argument
732 if there is no current element.
733 .It Dv H_APPEND , Fa "const char *str"
736 to the last new element of the history.
737 .It Dv H_ENTER , Fa "const char *str"
740 as a new element to the history, and, if necessary,
741 removing the oldest entry to keep the list to the created size.
744 has been called with a non-zero argument, the element
745 will not be entered into the history if its contents match
746 the ones of the current history element.
747 If the element is entered
749 returns 1; if it is ignored as a duplicate returns 0.
752 returns \-1 if an error occurred.
753 .It Dv H_PREV_STR , Fa "const char *str"
754 Return the closest previous event that starts with
756 .It Dv H_NEXT_STR , Fa "const char *str"
757 Return the closest next event that starts with
759 .It Dv H_PREV_EVENT , Fa "int e"
760 Return the previous event numbered
762 .It Dv H_NEXT_EVENT , Fa "int e"
763 Return the next event numbered
765 .It Dv H_LOAD , Fa "const char *file"
766 Load the history list stored in
768 .It Dv H_SAVE , Fa "const char *file"
769 Save the history list to
771 .It Dv H_SAVE_FP , Fa "FILE *fp"
772 Save the history list to the opened
776 .It Dv H_SETUNIQUE , Fa "int unique"
777 Set flag that adjacent identical event strings should not be entered
780 Retrieve the current setting if adjacent identical elements should
781 be entered into the history.
782 .It Dv H_DEL , Fa "int e"
783 Delete the event numbered
785 This function is only provided for
788 The caller is responsible for free'ing the string in the returned
793 returns \*[Gt]= 0 if the operation
796 Otherwise, \-1 is returned and
798 is updated to contain more details about the error.
800 .Sh TOKENIZATION FUNCTIONS
801 The tokenization functions use a common data structure,
808 The following functions are available:
811 Initialise the tokenizer, and return a data structure
812 to be used by all other tokenizer functions.
814 contains the Input Field Separators, which defaults to
822 Clean up and finish with
824 assumed to have been created with
827 Reset the tokenizer state.
828 Use after a line has been successfully tokenized
833 and before a new line is to be tokenized.
837 If successful, modify:
839 to contain the words,
841 to contain the number of words,
845 to contain the index of the word containing the cursor,
850 to contain the offset within
856 \-1 for an internal error,
857 1 for an unmatched single quote,
858 2 for an unmatched double quote,
860 3 for a backslash quoted
862 A positive exit code indicates that another line should be read
863 and tokenization attempted again.
869 is a NUL terminated string to tokenize.
873 .\"XXX: provide some examples
883 library first appeared in
888 .Dv CC_REFRESH_BEEP ,
890 and the readline emulation appeared in
899 library was written by
900 .An Christos Zoulas .
902 wrote this manual and implemented
904 .Dv CC_REFRESH_BEEP ,
909 implemented the readline emulation.
911 implemented wide-character support.
913 At this time, it is the responsibility of the caller to
914 check the result of the
924 should be used for further input.
927 is purely an indication of the result of the most recent