1 .\" $NetBSD: editline.3,v 1.77 2012/09/11 20:29:58 christos Exp $
3 .\" Copyright (c) 1997-2003 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.
29 .Dd September 11, 2012
73 .Nd line editor, history and tokenization functions
79 .Fn el_init "const char *prog" "FILE *fin" "FILE *fout" "FILE *ferr"
81 .Fn el_end "EditLine *e"
83 .Fn el_reset "EditLine *e"
85 .Fn el_gets "EditLine *e" "int *count"
87 .Fn el_wgets "EditLine *e" "int *count"
89 .Fn el_getc "EditLine *e" "char *ch"
91 .Fn el_wgetc "EditLine *e" "wchar_t *ch"
93 .Fn el_push "EditLine *e" "const char *str"
95 .Fn el_wpush "EditLine *e" "const wchar_t *str"
97 .Fn el_parse "EditLine *e" "int argc" "const char *argv[]"
99 .Fn el_wparse "EditLine *e" "int argc" "const wchar_t *argv[]"
101 .Fn el_set "EditLine *e" "int op" "..."
103 .Fn el_wset "EditLine *e" "int op" "..."
105 .Fn el_get "EditLine *e" "int op" "..."
107 .Fn el_wget "EditLine *e" "int op" "..."
109 .Fn el_source "EditLine *e" "const char *file"
111 .Fn el_resize "EditLine *e"
113 .Fn el_line "EditLine *e"
115 .Fn el_insertstr "EditLine *e" "const char *str"
117 .Fn el_winsertstr "EditLine *e" "const wchar_t *str"
119 .Fn el_deletestr "EditLine *e" "int count"
121 .Fn el_wdeletestr "EditLine *e" "int count"
127 .Fn history_end "History *h"
129 .Fn history_wend "HistoryW *h"
131 .Fn history "History *h" "HistEvent *ev" "int op" "..."
133 .Fn history_w "HistoryW *h" "HistEventW *ev" "int op" "..."
135 .Fn tok_init "const char *IFS"
137 .Fn tok_winit "const wchar_t *IFS"
139 .Fn tok_end "Tokenizer *t"
141 .Fn tok_wend "TokenizerW *t"
143 .Fn tok_reset "Tokenizer *t"
145 .Fn tok_wreset "TokenizerW *t"
147 .Fn tok_line "Tokenizer *t" "const LineInfo *li" "int *argc" "const char **argv[]" "int *cursorc" "int *cursoro"
149 .Fn tok_wline "TokenizerW *t" "const LineInfoW *li" "int *argc" "const wchar_t **argv[]" "int *cursorc" "int *cursoro"
151 .Fn tok_str "Tokenizer *t" "const char *str" "int *argc" "const char **argv[]"
153 .Fn tok_wstr "TokenizerW *t" "const wchar_t *str" "int *argc" "const wchar_t **argv[]"
157 library provides generic line editing, history and tokenization functions,
158 similar to those found in
161 These functions are available in the
163 library (which needs the
166 Programs should be linked with
168 .Sh LINE EDITING FUNCTIONS
169 The line editing functions use a common data structure,
176 The wide-character functions behave the same way as their narrow
179 The following functions are available:
182 Initialise the line editor, and return a data structure
183 to be used by all other line editing functions.
185 is the name of the invoking program, used when reading the
187 file to determine which settings to use.
192 are the input, output, and error streams (respectively) to use.
193 In this documentation, references to
195 are actually to this input/output stream combination.
197 Clean up and finish with
199 assumed to have been created with
202 Reset the tty and the parser.
203 This should be called after an error which may have upset the tty's
206 Read a line from the tty.
208 is modified to contain the number of characters read.
209 Returns the line read if successful, or
211 if no characters were read or if an error occurred.
212 If an error occurred,
216 contains the error code that caused it.
217 The return value may not remain valid across calls to
219 and must be copied if the data is to be retained.
221 Read a character from the tty.
223 is modified to contain the character read.
224 Returns the number of characters read if successful, \-1 otherwise,
227 can be inspected for the cause.
231 back onto the input stream.
232 This is used by the macro expansion mechanism.
233 Refer to the description of
238 for more information.
248 If the command is prefixed with
252 will only execute the command if
259 \-1 if the command is unknown,
260 0 if there was no error or
263 1 if the command returned an error.
266 for more information.
272 determines which parameter to set, and each operation has its
275 The following values for
277 are supported, along with the required argument list:
279 .It Dv EL_PROMPT , Fa "char *(*f)(EditLine *)"
280 Define prompt printing function as
282 which is to return a string that contains the prompt.
283 .It Dv EL_PROMPT_ESC , Fa "char *(*f)(EditLine *)" , Fa "char c"
288 argument indicates the start/stop literal prompt character.
290 If a start/stop literal character is found in the prompt, the
292 is not printed, but characters after it are printed directly to the
293 terminal without affecting the state of the current line.
294 A subsequent second start/stop literal character ends this behavior.
295 This is typically used to embed literal escape sequences that change the
296 color/style of the terminal in the prompt.
300 Re-display the current line on the next terminal line.
301 .It Dv EL_RPROMPT , Fa "char *(*f)(EditLine *)"
302 Define right side prompt printing function as
304 which is to return a string that contains the prompt.
305 .It Dv EL_RPROMPT_ESC , Fa "char *(*f)(EditLine *)" , Fa "char c"
306 Define the right prompt printing function but with a literal escape character.
307 .It Dv EL_TERMINAL , Fa "const char *type"
308 Define terminal type of the tty to be
316 .It Dv EL_EDITOR , Fa "const char *mode"
323 .It Dv EL_SIGNAL , Fa "int flag"
328 will install its own signal handler for the following signals when
329 reading command input:
339 Otherwise, the current signal handlers will be used.
340 .It Dv EL_BIND , Fa "const char *" , Fa "..." , Dv NULL
346 for more information.
347 .It Dv EL_ECHOTC , Fa "const char *" , Fa "..." , Dv NULL
353 for more information.
354 .It Dv EL_SETTC , Fa "const char *" , Fa "..." , Dv NULL
360 for more information.
361 .It Dv EL_SETTY , Fa "const char *" , Fa "..." , Dv NULL
367 for more information.
368 .It Dv EL_TELLTC , Fa "const char *" , Fa "..." , Dv NULL
374 for more information.
375 .It Dv EL_ADDFN , Fa "const char *name" , Fa "const char *help" , \
376 Fa "unsigned char (*func)(EditLine *e, int ch)"
377 Add a user defined function,
381 which is invoked when a key which is bound to
389 is the key which caused the invocation.
393 .Bl -tag -width "CC_REDISPLAY"
395 Add a normal character.
397 End of line was entered.
401 Expecting further command input as arguments, do nothing visually.
404 .It Dv CC_REFRESH_BEEP
405 Refresh display, and beep.
407 Cursor moved, so update and perform
410 Redisplay entire input line.
411 This is useful if a key binding outputs extra information.
416 Fatal error, reset tty to known state.
418 .It Dv EL_HIST , Fa "History *(*func)(History *, int op, ...)" , \
420 Defines which history function to use, which is usually
423 should be the value returned by
425 .It Dv EL_EDITMODE , Fa "int flag"
429 editing is enabled (the default).
430 Note that this is only an indication, and does not
431 affect the operation of
433 At this time, it is the caller's responsibility to
437 to determine if editing should be enabled or not.
438 .It Dv EL_UNBUFFERED , Fa "int flag"
442 unbuffered mode is disabled (the default).
445 will return immediately after processing a single character.
446 .It Dv EL_GETCFN , Fa "int (*f)(EditLine *, char *c)"
447 Define the character reading function as
449 which is to return the number of characters read and store them in
451 This function is called internally by
455 The builtin function can be set or restored with the special function
457 .Dq Dv EL_BUILTIN_GETCFN .
458 .It Dv EL_CLIENTDATA , Fa "void *data"
461 to be associated with this EditLine structure.
462 It can be retrieved with the corresponding
465 .It Dv EL_SETFP , Fa "int fd" , Fa "FILE *fp"
490 determines which parameter to retrieve into
492 Returns 0 if successful, \-1 otherwise.
494 The following values for
496 are supported, along with actual type of
499 .It Dv EL_PROMPT , Fa "char *(*f)(EditLine *)" , Fa "char *c"
500 Return a pointer to the function that displays the prompt in
506 return the start/stop literal prompt character in it.
507 .It Dv EL_RPROMPT , Fa "char *(*f)(EditLine *)" , Fa "char *c"
508 Return a pointer to the function that displays the prompt in
514 return the start/stop literal prompt character in it.
515 .It Dv EL_EDITOR , Fa "const char **"
516 Return the name of the editor, which will be one of
520 .It Dv EL_GETTC , Fa "const char *name" , Fa "void *value"
528 to the current value of that capability.
529 .It Dv EL_SIGNAL , Fa "int *"
532 has installed private signal handlers (see
535 .It Dv EL_EDITMODE , Fa "int *"
536 Return non-zero if editing is enabled.
537 .It Dv EL_GETCFN , Fa "int (**f)(EditLine *, char *)"
538 Return a pointer to the function that read characters, which is equal to
539 .Dq Dv EL_BUILTIN_GETCFN
540 in the case of the default builtin function.
541 .It Dv EL_CLIENTDATA , Fa "void **data"
544 previously registered with the corresponding
547 .It Dv EL_UNBUFFERED , Fa "int"
548 Return non-zero if unbuffered mode is enabled.
549 .It Dv EL_PREP_TERM , Fa "int"
550 Sets or clears terminal editing mode.
551 .It Dv EL_GETFP , Fa "int fd", Fa "FILE **fp"
574 by reading the contents of
577 is called for each line in
589 for details on the format of
592 Must be called if the terminal size changes.
597 then this is done automatically.
598 Otherwise, it's the responsibility of the application to call
600 on the appropriate occasions.
602 Return the editing information for the current line in a
604 structure, which is defined as follows:
606 typedef struct lineinfo {
607 const char *buffer; /* address of buffer */
608 const char *cursor; /* address of cursor */
609 const char *lastchar; /* address of last character */
614 is not NUL terminated.
615 This function may be called after
619 structure pertaining to line returned by that function,
620 and from within user defined functions added with
625 into the line at the cursor.
628 is empty or won't fit, and 0 otherwise.
632 characters before the cursor.
634 .Sh HISTORY LIST FUNCTIONS
635 The history functions use a common data structure,
642 The following functions are available:
645 Initialise the history list, and return a data structure
646 to be used by all other history list functions.
648 Clean up and finish with
650 assumed to have been created with
655 on the history list, with optional arguments as needed by the
658 is changed accordingly to operation.
659 The following values for
661 are supported, along with the required argument list:
663 .It Dv H_SETSIZE , Fa "int size"
664 Set size of history to
668 Get number of events currently in history.
670 Cleans up and finishes with
672 assumed to be created with
676 .It Dv H_FUNC , Fa "void *ptr" , Fa "history_gfun_t first" , \
677 Fa "history_gfun_t next" , Fa "history_gfun_t last" , \
678 Fa "history_gfun_t prev" , Fa "history_gfun_t curr" , \
679 Fa "history_sfun_t set" , Fa "history_vfun_t clear" , \
680 Fa "history_efun_t enter" , Fa "history_efun_t add"
681 Define functions to perform various history operations.
683 is the argument given to a function when it's invoked.
685 Return the first element in the history.
687 Return the last element in the history.
689 Return the previous element in the history.
691 Return the next element in the history.
693 Return the current element in the history.
695 Set the cursor to point to the requested element.
696 .It Dv H_ADD , Fa "const char *str"
699 to the current element of the history, or perform the
701 operation with argument
703 if there is no current element.
704 .It Dv H_APPEND , Fa "const char *str"
707 to the last new element of the history.
708 .It Dv H_ENTER , Fa "const char *str"
711 as a new element to the history, and, if necessary,
712 removing the oldest entry to keep the list to the created size.
715 was has been called with a non-zero arguments, the element
716 will not be entered into the history if its contents match
717 the ones of the current history element.
718 If the element is entered
720 returns 1, if it is ignored as a duplicate returns 0.
723 returns \-1 if an error occurred.
724 .It Dv H_PREV_STR , Fa "const char *str"
725 Return the closest previous event that starts with
727 .It Dv H_NEXT_STR , Fa "const char *str"
728 Return the closest next event that starts with
730 .It Dv H_PREV_EVENT , Fa "int e"
731 Return the previous event numbered
733 .It Dv H_NEXT_EVENT , Fa "int e"
734 Return the next event numbered
736 .It Dv H_LOAD , Fa "const char *file"
737 Load the history list stored in
739 .It Dv H_SAVE , Fa "const char *file"
740 Save the history list to
742 .It Dv H_SETUNIQUE , Fa "int unique"
743 Set flag that adjacent identical event strings should not be entered
746 Retrieve the current setting if adjacent identical elements should
747 be entered into the history.
748 .It Dv H_DEL , Fa "int e"
749 Delete the event numbered
751 This function is only provided for
754 The caller is responsible for free'ing the string in the returned
759 returns \*[Gt]= 0 if the operation
762 Otherwise, \-1 is returned and
764 is updated to contain more details about the error.
766 .Sh TOKENIZATION FUNCTIONS
767 The tokenization functions use a common data structure,
774 The following functions are available:
777 Initialise the tokenizer, and return a data structure
778 to be used by all other tokenizer functions.
780 contains the Input Field Separators, which defaults to
788 Clean up and finish with
790 assumed to have been created with
793 Reset the tokenizer state.
794 Use after a line has been successfully tokenized
799 and before a new line is to be tokenized.
803 If successful, modify:
805 to contain the words,
807 to contain the number of words,
811 to contain the index of the word containing the cursor,
816 to contain the offset within
822 \-1 for an internal error,
823 1 for an unmatched single quote,
824 2 for an unmatched double quote,
826 3 for a backslash quoted
828 A positive exit code indicates that another line should be read
829 and tokenization attempted again.
835 is a NUL terminated string to tokenize.
839 .\"XXX: provide some examples
849 library first appeared in
854 .Dv CC_REFRESH_BEEP ,
856 and the readline emulation appeared in
864 library was written by Christos Zoulas.
865 Luke Mewburn wrote this manual and implemented
867 .Dv CC_REFRESH_BEEP ,
871 Jaromir Dolecek implemented the readline emulation.
872 Johny Mattsson implemented wide-character support.
874 At this time, it is the responsibility of the caller to
875 check the result of the
885 should be used for further input.
888 is purely an indication of the result of the most recent