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.
16 .\" 3. Neither the name of The NetBSD Foundation nor the names of its
17 .\" contributors may be used to endorse or promote products derived
18 .\" from this software without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 .\" POSSIBILITY OF SUCH DAMAGE.
61 .Nd line editor, history and tokenization functions
67 .Fn el_init "const char *prog" "FILE *fin" "FILE *fout" "FILE *ferr"
69 .Fn el_end "EditLine *e"
71 .Fn el_reset "EditLine *e"
73 .Fn el_gets "EditLine *e" "int *count"
75 .Fn el_getc "EditLine *e" "char *ch"
77 .Fn el_push "EditLine *e" "char *str"
79 .Fn el_parse "EditLine *e" "int argc" "const char *argv[]"
81 .Fn el_set "EditLine *e" "int op" "..."
83 .Fn el_get "EditLine *e" "int op" "..."
85 .Fn el_source "EditLine *e" "const char *file"
87 .Fn el_resize "EditLine *e"
89 .Fn el_line "EditLine *e"
91 .Fn el_insertstr "EditLine *e" "const char *str"
93 .Fn el_deletestr "EditLine *e" "int count"
97 .Fn history_end "History *h"
99 .Fn history "History *h" "HistEvent *ev" "int op" "..."
101 .Fn tok_init "const char *IFS"
103 .Fn tok_end "Tokenizer *t"
105 .Fn tok_reset "Tokenizer *t"
108 .Fa "Tokenizer *t" "const LineInfo *li" "int *argc" "const char **argv[]"
109 .Fa "int *cursorc" "int *cursoro"
112 .Fn tok_str "Tokenizer *t" "const char *str" "int *argc" "const char **argv[]"
116 library provides generic line editing, history and tokenization functions,
117 similar to those found in
120 These functions are available in the
122 library (which needs the
125 Programs should be linked with
127 .Sh LINE EDITING FUNCTIONS
128 The line editing functions use a common data structure,
135 The following functions are available:
138 Initialise the line editor, and return a data structure
139 to be used by all other line editing functions.
141 is the name of the invoking program, used when reading the
143 file to determine which settings to use.
148 are the input, output, and error streams (respectively) to use.
149 In this documentation, references to
151 are actually to this input/output stream combination.
153 Clean up and finish with
155 assumed to have been created with
158 Reset the tty and the parser.
159 This should be called after an error which may have upset the tty's
162 Read a line from the tty.
164 is modified to contain the number of characters read.
165 Returns the line read if successful, or
167 if no characters were read or if an error occurred.
168 If an error occurred,
172 contains the error code that caused it.
173 The return value may not remain valid across calls to
175 and must be copied if the data is to be retained.
177 Read a character from the tty.
179 is modified to contain the character read.
180 Returns the number of characters read if successful, \-1 otherwise.
184 back onto the input stream.
185 This is used by the macro expansion mechanism.
186 Refer to the description of
191 for more information.
201 If the command is prefixed with
205 will only execute the command if
212 \-1 if the command is unknown,
213 0 if there was no error or
216 1 if the command returned an error.
219 for more information.
225 determines which parameter to set, and each operation has its
228 The following values for
230 are supported, along with the required argument list:
232 .It Dv EL_PROMPT , Fa "char *(*f)(EditLine *)"
233 Define prompt printing function as
235 which is to return a string that contains the prompt.
236 .It Dv EL_PROMPT_ESC , Fa "char *(*f)(EditLine *)" , Fa "char c"
241 argument indicates the start/stop literal prompt character.
243 If a start/stop literal character is found in the prompt, the
245 is not printed, but characters after it are printed directly to the
246 terminal without affecting the state of the current line.
247 A subsequent second start/stop literal character ends this behavior.
248 This is typically used to embed literal escape sequences that change the
249 color/style of the terminal in the prompt.
253 Re-display the current line on the next terminal line.
254 .It Dv EL_RPROMPT , Fa "char *(*f)(EditLine *)"
255 Define right side prompt printing function as
257 which is to return a string that contains the prompt.
258 .It Dv EL_RPROMPT_ESC , Fa "char *(*f)(EditLine *)" , Fa "char c"
259 Define the right prompt printing function but with a literal escape character.
260 .It Dv EL_TERMINAL , Fa "const char *type"
261 Define terminal type of the tty to be
269 .It Dv EL_EDITOR , Fa "const char *mode"
276 .It Dv EL_SIGNAL , Fa "int flag"
281 will install its own signal handler for the following signals when
282 reading command input:
292 Otherwise, the current signal handlers will be used.
293 .It Dv EL_BIND , Fa "const char *" , Fa "..." , Dv NULL
299 for more information.
300 .It Dv EL_ECHOTC , Fa "const char *" , Fa "..." , Dv NULL
306 for more information.
307 .It Dv EL_SETTC , Fa "const char *" , Fa "..." , Dv NULL
313 for more information.
314 .It Dv EL_SETTY , Fa "const char *" , Fa "..." , Dv NULL
320 for more information.
321 .It Dv EL_TELLTC , Fa "const char *" , Fa "..." , Dv NULL
327 for more information.
328 .It Dv EL_ADDFN , Fa "const char *name" , Fa "const char *help" , \
329 Fa "unsigned char (*func)(EditLine *e, int ch)"
330 Add a user defined function,
334 which is invoked when a key which is bound to
342 is the key which caused the invocation.
346 .Bl -tag -width "CC_REDISPLAY"
348 Add a normal character.
350 End of line was entered.
354 Expecting further command input as arguments, do nothing visually.
357 .It Dv CC_REFRESH_BEEP
358 Refresh display, and beep.
360 Cursor moved, so update and perform
363 Redisplay entire input line.
364 This is useful if a key binding outputs extra information.
369 Fatal error, reset tty to known state.
371 .It Dv EL_HIST , Fa "History *(*func)(History *, int op, ...)" , \
373 Defines which history function to use, which is usually
376 should be the value returned by
378 .It Dv EL_EDITMODE , Fa "int flag"
382 editing is enabled (the default).
383 Note that this is only an indication, and does not
384 affect the operation of
386 At this time, it is the caller's responsibility to
390 to determine if editing should be enabled or not.
391 .It Dv EL_GETCFN , Fa "int (*f)(EditLine *, char *c)"
392 Define the character reading function as
394 which is to return the number of characters read and store them in
396 This function is called internally by
400 The builtin function can be set or restored with the special function
402 .Dv EL_BUILTIN_GETCFN .
403 .It Dv EL_CLIENTDATA , Fa "void *data"
406 to be associated with this EditLine structure.
407 It can be retrieved with the corresponding
410 .It Dv EL_SETFP , Fa "int fd" , Fa "FILE *fp"
435 determines which parameter to retrieve into
437 Returns 0 if successful, \-1 otherwise.
439 The following values for
441 are supported, along with actual type of
444 .It Dv EL_PROMPT , Fa "char *(*f)(EditLine *)" , Fa "char *c"
445 Return a pointer to the function that displays the prompt in
451 return the start/stop literal prompt character in it.
452 .It Dv EL_RPROMPT , Fa "char *(*f)(EditLine *)" , Fa "char *c"
453 Return a pointer to the function that displays the prompt in
459 return the start/stop literal prompt character in it.
460 .It Dv EL_EDITOR , Fa "const char *"
461 Return the name of the editor, which will be one of
465 .It Dv EL_GETTC , Fa "const char *name" , Fa "void *value"
473 to the current value of that capability.
474 .It Dv EL_SIGNAL , Fa "int *"
477 has installed private signal handlers (see
480 .It Dv EL_EDITMODE , Fa "int *"
481 Return non-zero if editing is enabled.
482 .It Dv EL_GETCFN , Fa "int (**f)(EditLine *, char *)"
483 Return a pointer to the function that read characters, which is equal to
484 .Dv EL_BUILTIN_GETCFN
485 in the case of the default builtin function.
486 .It Dv EL_CLIENTDATA , Fa "void **data"
489 previously registered with the corresponding
492 .It Dv EL_UNBUFFERED , Fa "int"
493 Sets or clears unbuffered mode.
496 will return immediately after processing a single character.
497 .It Dv EL_PREP_TERM , Fa "int"
498 Sets or clears terminal editing mode.
499 .It Dv EL_GETFP , Fa "int fd", Fa "FILE **fp"
522 by reading the contents of
525 is called for each line in
537 for details on the format of
540 Must be called if the terminal size changes.
545 then this is done automatically.
546 Otherwise, it is the responsibility of the application to call
548 on the appropriate occasions.
550 Return the editing information for the current line in a
552 structure, which is defined as follows:
554 typedef struct lineinfo {
555 const char *buffer; /* address of buffer */
556 const char *cursor; /* address of cursor */
557 const char *lastchar; /* address of last character */
562 is not NUL terminated.
563 This function may be called after
567 structure pertaining to line returned by that function,
568 and from within user defined functions added with
573 into the line at the cursor.
576 is empty or will not fit, and 0 otherwise.
580 characters before the cursor.
582 .Sh HISTORY LIST FUNCTIONS
583 The history functions use a common data structure,
590 The following functions are available:
593 Initialise the history list, and return a data structure
594 to be used by all other history list functions.
596 Clean up and finish with
598 assumed to have been created with
603 on the history list, with optional arguments as needed by the
606 is changed accordingly to operation.
607 The following values for
609 are supported, along with the required argument list:
611 .It Dv H_SETSIZE , Fa "int size"
612 Set size of history to
616 Get number of events currently in history.
618 Cleans up and finishes with
620 assumed to be created with
624 .It Dv H_FUNC , Fa "void *ptr" , Fa "history_gfun_t first" , \
625 Fa "history_gfun_t next" , Fa "history_gfun_t last" , \
626 Fa "history_gfun_t prev" , Fa "history_gfun_t curr" , \
627 Fa "history_sfun_t set" , Fa "history_vfun_t clear" , \
628 Fa "history_efun_t enter" , Fa "history_efun_t add"
629 Define functions to perform various history operations.
631 is the argument given to a function when it is invoked.
633 Return the first element in the history.
635 Return the last element in the history.
637 Return the previous element in the history.
639 Return the next element in the history.
641 Return the current element in the history.
643 Set the cursor to point to the requested element.
644 .It Dv H_ADD , Fa "const char *str"
647 to the current element of the history, or perform the
649 operation with argument
651 if there is no current element.
652 .It Dv H_APPEND , Fa "const char *str"
655 to the last new element of the history.
656 .It Dv H_ENTER , Fa "const char *str"
659 as a new element to the history, and, if necessary,
660 removing the oldest entry to keep the list to the created size.
663 was has been called with a non-zero arguments, the element
664 will not be entered into the history if its contents match
665 the ones of the current history element.
666 If the element is entered
668 returns 1, if it is ignored as a duplicate returns 0.
671 returns \-1 if an error occurred.
672 .It Dv H_PREV_STR , Fa "const char *str"
673 Return the closest previous event that starts with
675 .It Dv H_NEXT_STR , Fa "const char *str"
676 Return the closest next event that starts with
678 .It Dv H_PREV_EVENT , Fa "int e"
679 Return the previous event numbered
681 .It Dv H_NEXT_EVENT , Fa "int e"
682 Return the next event numbered
684 .It Dv H_LOAD , Fa "const char *file"
685 Load the history list stored in
687 .It Dv H_SAVE , Fa "const char *file"
688 Save the history list to
690 .It Dv H_SETUNIQUE , Fa "int unique"
691 Set flag that adjacent identical event strings should not be entered
694 Retrieve the current setting if adjacent identical elements should
695 be entered into the history.
696 .It Dv H_DEL , Fa "int e"
697 Delete the event numbered
699 This function is only provided for
702 The caller is responsible for free'ing the string in the returned
708 function returns \*[Ge] 0 if the operation
711 Otherwise, \-1 is returned and
713 is updated to contain more details about the error.
715 .Sh TOKENIZATION FUNCTIONS
716 The tokenization functions use a common data structure,
723 The following functions are available:
726 Initialise the tokenizer, and return a data structure
727 to be used by all other tokenizer functions.
729 contains the Input Field Separators, which defaults to
737 Clean up and finish with
739 assumed to have been created with
742 Reset the tokenizer state.
743 Use after a line has been successfully tokenized
748 and before a new line is to be tokenized.
752 If successful, modify:
754 to contain the words,
756 to contain the number of words,
760 to contain the index of the word containing the cursor,
765 to contain the offset within
771 \-1 for an internal error,
772 1 for an unmatched single quote,
773 2 for an unmatched double quote,
775 3 for a backslash quoted
777 A positive exit code indicates that another line should be read
778 and tokenization attempted again.
783 is a NUL terminated string to tokenize.
786 .\"XXX: provide some examples
796 library first appeared in
813 library was written by
814 .An Christos Zoulas .
816 wrote this manual and implemented
818 .Dv CC_REFRESH_BEEP ,
823 At this time, it is the responsibility of the caller to
824 check the result of the
834 should be used for further input.
837 is purely an indication of the result of the most recent