1 .\" $NetBSD: editline.3,v 1.70 2009/07/05 21:55:24 perry 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.
58 .Nd line editor, history and tokenization functions
64 .Fn el_init "const char *prog" "FILE *fin" "FILE *fout" "FILE *ferr"
66 .Fn el_end "EditLine *e"
68 .Fn el_reset "EditLine *e"
70 .Fn el_gets "EditLine *e" "int *count"
72 .Fn el_getc "EditLine *e" "char *ch"
74 .Fn el_push "EditLine *e" "char *str"
76 .Fn el_parse "EditLine *e" "int argc" "const char *argv[]"
78 .Fn el_set "EditLine *e" "int op" "..."
80 .Fn el_get "EditLine *e" "int op" "..."
82 .Fn el_source "EditLine *e" "const char *file"
84 .Fn el_resize "EditLine *e"
86 .Fn el_line "EditLine *e"
88 .Fn el_insertstr "EditLine *e" "const char *str"
90 .Fn el_deletestr "EditLine *e" "int count"
94 .Fn history_end "History *h"
96 .Fn history "History *h" "HistEvent *ev" "int op" "..."
98 .Fn tok_init "const char *IFS"
100 .Fn tok_end "Tokenizer *t"
102 .Fn tok_reset "Tokenizer *t"
105 .Fa "Tokenizer *t" "const LineInfo *li" "int *argc" "const char **argv[]"
106 .Fa "int *cursorc" "int *cursoro"
109 .Fn tok_str "Tokenizer *t" "const char *str" "int *argc" "const char **argv[]"
113 library provides generic line editing, history and tokenization functions,
114 similar to those found in
117 These functions are available in the
119 library (which needs the
122 Programs should be linked with
124 .Sh LINE EDITING FUNCTIONS
125 The line editing functions use a common data structure,
132 The following functions are available:
135 Initialise the line editor, and return a data structure
136 to be used by all other line editing functions.
138 is the name of the invoking program, used when reading the
140 file to determine which settings to use.
145 are the input, output, and error streams (respectively) to use.
146 In this documentation, references to
148 are actually to this input/output stream combination.
150 Clean up and finish with
152 assumed to have been created with
155 Reset the tty and the parser.
156 This should be called after an error which may have upset the tty's
159 Read a line from the tty.
161 is modified to contain the number of characters read.
162 Returns the line read if successful, or
164 if no characters were read or if an error occurred.
165 If an error occurred,
169 contains the error code that caused it.
170 The return value may not remain valid across calls to
172 and must be copied if the data is to be retained.
174 Read a character from the tty.
176 is modified to contain the character read.
177 Returns the number of characters read if successful, \-1 otherwise.
181 back onto the input stream.
182 This is used by the macro expansion mechanism.
183 Refer to the description of
188 for more information.
198 If the command is prefixed with
202 will only execute the command if
209 \-1 if the command is unknown,
210 0 if there was no error or
213 1 if the command returned an error.
216 for more information.
222 determines which parameter to set, and each operation has its
225 The following values for
227 are supported, along with the required argument list:
229 .It Dv EL_PROMPT , Fa "char *(*f)(EditLine *)"
230 Define prompt printing function as
232 which is to return a string that contains the prompt.
233 .It Dv EL_PROMPT_ESC , Fa "char *(*f)(EditLine *)" , Fa "char c"
238 argument indicates the start/stop literal prompt character.
240 If a start/stop literal character is found in the prompt, the
242 is not printed, but characters after it are printed directly to the
243 terminal without affecting the state of the current line.
244 A subsequent second start/stop literal character ends this behavior.
245 This is typically used to embed literal escape sequences that change the
246 color/style of the terminal in the prompt.
250 Re-display the current line on the next terminal line.
251 .It Dv EL_RPROMPT , Fa "char *(*f)(EditLine *)"
252 Define right side prompt printing function as
254 which is to return a string that contains the prompt.
255 .It Dv EL_RPROMPT_ESC , Fa "char *(*f)(EditLine *)" , Fa "char c"
256 Define the right prompt printing function but with a literal escape character.
257 .It Dv EL_TERMINAL , Fa "const char *type"
258 Define terminal type of the tty to be
266 .It Dv EL_EDITOR , Fa "const char *mode"
273 .It Dv EL_SIGNAL , Fa "int flag"
278 will install its own signal handler for the following signals when
279 reading command input:
289 Otherwise, the current signal handlers will be used.
290 .It Dv EL_BIND , Fa "const char *" , Fa "..." , Dv NULL
296 for more information.
297 .It Dv EL_ECHOTC , Fa "const char *" , Fa "..." , Dv NULL
303 for more information.
304 .It Dv EL_SETTC , Fa "const char *" , Fa "..." , Dv NULL
310 for more information.
311 .It Dv EL_SETTY , Fa "const char *" , Fa "..." , Dv NULL
317 for more information.
318 .It Dv EL_TELLTC , Fa "const char *" , Fa "..." , Dv NULL
324 for more information.
325 .It Dv EL_ADDFN , Fa "const char *name" , Fa "const char *help" , \
326 Fa "unsigned char (*func)(EditLine *e, int ch)"
327 Add a user defined function,
331 which is invoked when a key which is bound to
339 is the key which caused the invocation.
343 .Bl -tag -width "CC_REDISPLAY"
345 Add a normal character.
347 End of line was entered.
351 Expecting further command input as arguments, do nothing visually.
354 .It Dv CC_REFRESH_BEEP
355 Refresh display, and beep.
357 Cursor moved, so update and perform
360 Redisplay entire input line.
361 This is useful if a key binding outputs extra information.
366 Fatal error, reset tty to known state.
368 .It Dv EL_HIST , Fa "History *(*func)(History *, int op, ...)" , \
370 Defines which history function to use, which is usually
373 should be the value returned by
375 .It Dv EL_EDITMODE , Fa "int flag"
379 editing is enabled (the default).
380 Note that this is only an indication, and does not
381 affect the operation of
383 At this time, it is the caller's responsibility to
387 to determine if editing should be enabled or not.
388 .It Dv EL_GETCFN , Fa "int (*f)(EditLine *, char *c)"
389 Define the character reading function as
391 which is to return the number of characters read and store them in
393 This function is called internally by
397 The builtin function can be set or restored with the special function
399 .Dv EL_BUILTIN_GETCFN .
400 .It Dv EL_CLIENTDATA , Fa "void *data"
403 to be associated with this EditLine structure.
404 It can be retrieved with the corresponding
407 .It Dv EL_SETFP , Fa "int fd" , Fa "FILE *fp"
432 determines which parameter to retrieve into
434 Returns 0 if successful, \-1 otherwise.
436 The following values for
438 are supported, along with actual type of
441 .It Dv EL_PROMPT , Fa "char *(*f)(EditLine *)" , Fa "char *c"
442 Return a pointer to the function that displays the prompt in
448 return the start/stop literal prompt character in it.
449 .It Dv EL_RPROMPT , Fa "char *(*f)(EditLine *)" , Fa "char *c"
450 Return a pointer to the function that displays the prompt in
456 return the start/stop literal prompt character in it.
457 .It Dv EL_EDITOR , Fa "const char *"
458 Return the name of the editor, which will be one of
462 .It Dv EL_GETTC , Fa "const char *name" , Fa "void *value"
470 to the current value of that capability.
471 .It Dv EL_SIGNAL , Fa "int *"
474 has installed private signal handlers (see
477 .It Dv EL_EDITMODE , Fa "int *"
478 Return non-zero if editing is enabled.
479 .It Dv EL_GETCFN , Fa "int (**f)(EditLine *, char *)"
480 Return a pointer to the function that read characters, which is equal to
481 .Dv EL_BUILTIN_GETCFN
482 in the case of the default builtin function.
483 .It Dv EL_CLIENTDATA , Fa "void **data"
486 previously registered with the corresponding
489 .It Dv EL_UNBUFFERED , Fa "int"
490 Sets or clears unbuffered mode.
493 will return immediately after processing a single character.
494 .It Dv EL_PREP_TERM , Fa "int"
495 Sets or clears terminal editing mode.
496 .It Dv EL_GETFP , Fa "int fd", Fa "FILE **fp"
519 by reading the contents of
522 is called for each line in
532 for details on the format of
535 Must be called if the terminal size changes.
540 then this is done automatically.
541 Otherwise, it is the responsibility of the application to call
543 on the appropriate occasions.
545 Return the editing information for the current line in a
547 structure, which is defined as follows:
549 typedef struct lineinfo {
550 const char *buffer; /* address of buffer */
551 const char *cursor; /* address of cursor */
552 const char *lastchar; /* address of last character */
557 is not NUL terminated.
558 This function may be called after
562 structure pertaining to line returned by that function,
563 and from within user defined functions added with
568 into the line at the cursor.
571 is empty or will not fit, and 0 otherwise.
575 characters before the cursor.
577 .Sh HISTORY LIST FUNCTIONS
578 The history functions use a common data structure,
585 The following functions are available:
588 Initialise the history list, and return a data structure
589 to be used by all other history list functions.
591 Clean up and finish with
593 assumed to have been created with
598 on the history list, with optional arguments as needed by the
601 is changed accordingly to operation.
602 The following values for
604 are supported, along with the required argument list:
606 .It Dv H_SETSIZE , Fa "int size"
607 Set size of history to
611 Get number of events currently in history.
613 Cleans up and finishes with
615 assumed to be created with
619 .It Dv H_FUNC , Fa "void *ptr" , Fa "history_gfun_t first" , \
620 Fa "history_gfun_t next" , Fa "history_gfun_t last" , \
621 Fa "history_gfun_t prev" , Fa "history_gfun_t curr" , \
622 Fa "history_sfun_t set" , Fa "history_vfun_t clear" , \
623 Fa "history_efun_t enter" , Fa "history_efun_t add"
624 Define functions to perform various history operations.
626 is the argument given to a function when it is invoked.
628 Return the first element in the history.
630 Return the last element in the history.
632 Return the previous element in the history.
634 Return the next element in the history.
636 Return the current element in the history.
638 Set the cursor to point to the requested element.
639 .It Dv H_ADD , Fa "const char *str"
642 to the current element of the history, or perform the
644 operation with argument
646 if there is no current element.
647 .It Dv H_APPEND , Fa "const char *str"
650 to the last new element of the history.
651 .It Dv H_ENTER , Fa "const char *str"
654 as a new element to the history, and, if necessary,
655 removing the oldest entry to keep the list to the created size.
658 was has been called with a non-zero arguments, the element
659 will not be entered into the history if its contents match
660 the ones of the current history element.
661 If the element is entered
663 returns 1, if it is ignored as a duplicate returns 0.
666 returns \-1 if an error occurred.
667 .It Dv H_PREV_STR , Fa "const char *str"
668 Return the closest previous event that starts with
670 .It Dv H_NEXT_STR , Fa "const char *str"
671 Return the closest next event that starts with
673 .It Dv H_PREV_EVENT , Fa "int e"
674 Return the previous event numbered
676 .It Dv H_NEXT_EVENT , Fa "int e"
677 Return the next event numbered
679 .It Dv H_LOAD , Fa "const char *file"
680 Load the history list stored in
682 .It Dv H_SAVE , Fa "const char *file"
683 Save the history list to
685 .It Dv H_SAVE_FP , Fa "FILE*"
686 Save the history list to the opened
688 .It Dv H_SETUNIQUE , Fa "int unique"
689 Set flag that adjacent identical event strings should not be entered
692 Retrieve the current setting if adjacent identical elements should
693 be entered into the history.
694 .It Dv H_DEL , Fa "int e"
695 Delete the event numbered
697 This function is only provided for
700 The caller is responsible for free'ing the string in the returned
706 function returns \*[Ge] 0 if the operation
709 Otherwise, \-1 is returned and
711 is updated to contain more details about the error.
713 .Sh TOKENIZATION FUNCTIONS
714 The tokenization functions use a common data structure,
721 The following functions are available:
724 Initialise the tokenizer, and return a data structure
725 to be used by all other tokenizer functions.
727 contains the Input Field Separators, which defaults to
735 Clean up and finish with
737 assumed to have been created with
740 Reset the tokenizer state.
741 Use after a line has been successfully tokenized
746 and before a new line is to be tokenized.
750 if successful, modify
752 to contain the words,
754 to contain the number of words,
758 to contain the index of the word containing the cursor,
763 to contain the offset within
769 \-1 for an internal error,
770 1 for an unmatched single quote,
771 2 for an unmatched double quote,
773 3 for a backslash quoted
775 A positive exit code indicates that another line should be read
776 and tokenization attempted again.
781 is a NUL terminated string to tokenize.
784 .\"XXX: provide some examples
794 library first appeared in
811 library was written by
812 .An Christos Zoulas .
814 wrote this manual and implemented
816 .Dv CC_REFRESH_BEEP ,
821 At this time, it is the responsibility of the caller to
822 check the result of the
832 should be used for further input.
835 is purely an indication of the result of the most recent