]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - editline.3
Import libedit 2016-01-16
[FreeBSD/FreeBSD.git] / editline.3
1 .\"     $NetBSD: editline.3,v 1.85 2015/11/03 21:36:59 christos Exp $
2 .\"
3 .\" Copyright (c) 1997-2014 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
5 .\"
6 .\" This file was contributed to The NetBSD Foundation by Luke Mewburn.
7 .\"
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
10 .\" are met:
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 .\"
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.
28 .\"
29 .Dd November 3, 2015
30 .Dt EDITLINE 3
31 .Os
32 .Sh NAME
33 .Nm editline ,
34 .Nm el_init ,
35 .Nm el_init_fd ,
36 .Nm el_end ,
37 .Nm el_reset ,
38 .Nm el_gets ,
39 .Nm el_wgets ,
40 .Nm el_getc ,
41 .Nm el_wgetc ,
42 .Nm el_push ,
43 .Nm el_wpush ,
44 .Nm el_parse ,
45 .Nm el_wparse ,
46 .Nm el_set ,
47 .Nm el_wset ,
48 .Nm el_get ,
49 .Nm el_wget ,
50 .Nm el_source ,
51 .Nm el_resize ,
52 .Nm el_cursor ,
53 .Nm el_line ,
54 .Nm el_wline ,
55 .Nm el_insertstr ,
56 .Nm el_winsertstr ,
57 .Nm el_deletestr ,
58 .Nm el_wdeletestr ,
59 .Nm history_init ,
60 .Nm history_winit ,
61 .Nm history_end ,
62 .Nm history_wend ,
63 .Nm history ,
64 .Nm history_w ,
65 .Nm tok_init ,
66 .Nm tok_winit ,
67 .Nm tok_end ,
68 .Nm tok_wend ,
69 .Nm tok_reset ,
70 .Nm tok_wreset ,
71 .Nm tok_line ,
72 .Nm tok_wline ,
73 .Nm tok_str
74 .Nm tok_wstr
75 .Nd line editor, history and tokenization functions
76 .Sh LIBRARY
77 .Lb libedit
78 .Sh SYNOPSIS
79 .In histedit.h
80 .Ft EditLine *
81 .Fn el_init "const char *prog" "FILE *fin" "FILE *fout" "FILE *ferr"
82 .Ft EditLine *
83 .Fn el_init_fd "const char *prog" "FILE *fin" "FILE *fout" "FILE *ferr" "int fdin" "int fdout" "int fderr"
84 .Ft void
85 .Fn el_end "EditLine *e"
86 .Ft void
87 .Fn el_reset "EditLine *e"
88 .Ft const char *
89 .Fn el_gets "EditLine *e" "int *count"
90 .Ft const wchar_t *
91 .Fn el_wgets "EditLine *e" "int *count"
92 .Ft int
93 .Fn el_getc "EditLine *e" "char *ch"
94 .Ft int
95 .Fn el_wgetc "EditLine *e" "wchar_t *ch"
96 .Ft void
97 .Fn el_push "EditLine *e" "const char *str"
98 .Ft void
99 .Fn el_wpush "EditLine *e" "const wchar_t *str"
100 .Ft int
101 .Fn el_parse "EditLine *e" "int argc" "const char *argv[]"
102 .Ft int
103 .Fn el_wparse "EditLine *e" "int argc" "const wchar_t *argv[]"
104 .Ft int
105 .Fn el_set "EditLine *e" "int op" "..."
106 .Ft int
107 .Fn el_wset "EditLine *e" "int op" "..."
108 .Ft int
109 .Fn el_get "EditLine *e" "int op" "..."
110 .Ft int
111 .Fn el_wget "EditLine *e" "int op" "..."
112 .Ft int
113 .Fn el_source "EditLine *e" "const char *file"
114 .Ft void
115 .Fn el_resize "EditLine *e"
116 .Fn int
117 .Fn el_cursor "EditLine *e" "int count"
118 .Ft const LineInfo *
119 .Fn el_line "EditLine *e"
120 .Ft const LineInfoW *
121 .Fn el_wline "EditLine *e"
122 .Ft int
123 .Fn el_insertstr "EditLine *e" "const char *str"
124 .Ft int
125 .Fn el_winsertstr "EditLine *e" "const wchar_t *str"
126 .Ft void
127 .Fn el_deletestr "EditLine *e" "int count"
128 .Ft void
129 .Fn el_wdeletestr "EditLine *e" "int count"
130 .Ft History *
131 .Fn history_init
132 .Ft HistoryW *
133 .Fn history_winit
134 .Ft void
135 .Fn history_end "History *h"
136 .Ft void
137 .Fn history_wend "HistoryW *h"
138 .Ft int
139 .Fn history "History *h" "HistEvent *ev" "int op" "..."
140 .Ft int
141 .Fn history_w "HistoryW *h" "HistEventW *ev" "int op" "..."
142 .Ft Tokenizer *
143 .Fn tok_init "const char *IFS"
144 .Ft TokenizerW *
145 .Fn tok_winit "const wchar_t *IFS"
146 .Ft void
147 .Fn tok_end "Tokenizer *t"
148 .Ft void
149 .Fn tok_wend "TokenizerW *t"
150 .Ft void
151 .Fn tok_reset "Tokenizer *t"
152 .Ft void
153 .Fn tok_wreset "TokenizerW *t"
154 .Ft int
155 .Fn tok_line "Tokenizer *t" "const LineInfo *li" "int *argc" "const char **argv[]" "int *cursorc" "int *cursoro"
156 .Ft int
157 .Fn tok_wline "TokenizerW *t" "const LineInfoW *li" "int *argc" "const wchar_t **argv[]" "int *cursorc" "int *cursoro"
158 .Ft int
159 .Fn tok_str "Tokenizer *t" "const char *str" "int *argc" "const char **argv[]"
160 .Ft int
161 .Fn tok_wstr "TokenizerW *t" "const wchar_t *str" "int *argc" "const wchar_t **argv[]"
162 .Sh DESCRIPTION
163 The
164 .Nm
165 library provides generic line editing, history and tokenization functions,
166 similar to those found in
167 .Xr sh 1 .
168 .Pp
169 These functions are available in the
170 .Nm libedit
171 library (which needs the
172 .Nm libtermcap
173 library).
174 Programs should be linked with
175 .Fl ledit ltermcap .
176 .Sh LINE EDITING FUNCTIONS
177 The line editing functions use a common data structure,
178 .Fa EditLine ,
179 which is created by
180 .Fn el_init
181 or
182 .Fn el_init_fd
183 and freed by
184 .Fn el_end .
185 .Pp
186 The wide-character functions behave the same way as their narrow
187 counterparts.
188 .Pp
189 The following functions are available:
190 .Bl -tag -width 4n
191 .It Fn el_init
192 Initialize the line editor, and return a data structure
193 to be used by all other line editing functions, or
194 .Dv NULL
195 on failure.
196 .Fa prog
197 is the name of the invoking program, used when reading the
198 .Xr editrc 5
199 file to determine which settings to use.
200 .Fa fin ,
201 .Fa fout
202 and
203 .Fa ferr
204 are the input, output, and error streams (respectively) to use.
205 In this documentation, references to
206 .Dq the tty
207 are actually to this input/output stream combination.
208 .It Fn el_init_fd
209 Like
210 .Fn el_init
211 but allows specifying file descriptors for the
212 .Xr stdio 3
213 corresponding streams, in case those were created with
214 .Xr funopen 3 .
215 .It Fn el_end
216 Clean up and finish with
217 .Fa e ,
218 assumed to have been created with
219 .Fn el_init
220 or
221 .Fn el_init_fd .
222 .It Fn el_reset
223 Reset the tty and the parser.
224 This should be called after an error which may have upset the tty's
225 state.
226 .It Fn el_gets
227 Read a line from the tty.
228 .Fa count
229 is modified to contain the number of characters read.
230 Returns the line read if successful, or
231 .Dv NULL
232 if no characters were read or if an error occurred.
233 If an error occurred,
234 .Fa count
235 is set to \-1 and
236 .Dv errno
237 contains the error code that caused it.
238 The return value may not remain valid across calls to
239 .Fn el_gets
240 and must be copied if the data is to be retained.
241 .It Fn el_getc
242 Read a character from the tty.
243 .Fa ch
244 is modified to contain the character read.
245 Returns the number of characters read if successful, \-1 otherwise,
246 in which case
247 .Dv errno
248 can be inspected for the cause.
249 .It Fn el_push
250 Pushes
251 .Fa str
252 back onto the input stream.
253 This is used by the macro expansion mechanism.
254 Refer to the description of
255 .Ic bind
256 .Fl s
257 in
258 .Xr editrc 5
259 for more information.
260 .It Fn el_parse
261 Parses the
262 .Fa argv
263 array (which is
264 .Fa argc
265 elements in size)
266 to execute builtin
267 .Nm
268 commands.
269 If the command is prefixed with
270 .Dq prog :
271 then
272 .Fn el_parse
273 will only execute the command if
274 .Dq prog
275 matches the
276 .Fa prog
277 argument supplied to
278 .Fn el_init .
279 The return value is
280 \-1 if the command is unknown,
281 0 if there was no error or
282 .Dq prog
283 didn't match, or
284 1 if the command returned an error.
285 Refer to
286 .Xr editrc 5
287 for more information.
288 .It Fn el_set
289 Set
290 .Nm
291 parameters.
292 .Fa op
293 determines which parameter to set, and each operation has its
294 own parameter list.
295 Returns 0 on success, \-1 on failure.
296 .Pp
297 The following values for
298 .Fa op
299 are supported, along with the required argument list:
300 .Bl -tag -width 4n
301 .It Dv EL_PROMPT , Fa "char *(*f)(EditLine *)"
302 Define prompt printing function as
303 .Fa f ,
304 which is to return a string that contains the prompt.
305 .It Dv EL_PROMPT_ESC , Fa "char *(*f)(EditLine *)" , Fa "char c"
306 Same as
307 .Dv EL_PROMPT ,
308 but the
309 .Fa c
310 argument indicates the start/stop literal prompt character.
311 .Pp
312 If a start/stop literal character is found in the prompt, the
313 character itself
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.
319 .Dv 0
320 unsets it.
321 .It Dv EL_REFRESH
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
325 .Fa f ,
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
331 .Fa type ,
332 or to
333 .Ev TERM
334 if
335 .Fa type
336 is
337 .Dv NULL .
338 .It Dv EL_EDITOR , Fa "const char *mode"
339 Set editing mode to
340 .Fa mode ,
341 which must be one of
342 .Dq emacs
343 or
344 .Dq vi .
345 .It Dv EL_SIGNAL , Fa "int flag"
346 If
347 .Fa flag
348 is non-zero,
349 .Nm
350 will install its own signal handler for the following signals when
351 reading command input:
352 .Dv SIGCONT ,
353 .Dv SIGHUP ,
354 .Dv SIGINT ,
355 .Dv SIGQUIT ,
356 .Dv SIGSTOP ,
357 .Dv SIGTERM ,
358 .Dv SIGTSTP ,
359 and
360 .Dv SIGWINCH .
361 Otherwise, the current signal handlers will be used.
362 .It Dv EL_BIND , Fa "const char *" , Fa "..." , Dv NULL
363 Perform the
364 .Ic bind
365 builtin command.
366 Refer to
367 .Xr editrc 5
368 for more information.
369 .It Dv EL_ECHOTC , Fa "const char *" , Fa "..." , Dv NULL
370 Perform the
371 .Ic echotc
372 builtin command.
373 Refer to
374 .Xr editrc 5
375 for more information.
376 .It Dv EL_SETTC , Fa "const char *" , Fa "..." , Dv NULL
377 Perform the
378 .Ic settc
379 builtin command.
380 Refer to
381 .Xr editrc 5
382 for more information.
383 .It Dv EL_SETTY , Fa "const char *" , Fa "..." , Dv NULL
384 Perform the
385 .Ic setty
386 builtin command.
387 Refer to
388 .Xr editrc 5
389 for more information.
390 .It Dv EL_TELLTC , Fa "const char *" , Fa "..." , Dv NULL
391 Perform the
392 .Ic telltc
393 builtin command.
394 Refer to
395 .Xr editrc 5
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,
400 .Fn func ,
401 referred to as
402 .Fa name
403 which is invoked when a key which is bound to
404 .Fa name
405 is entered.
406 .Fa help
407 is a description of
408 .Fa name .
409 At invocation time,
410 .Fa ch
411 is the key which caused the invocation.
412 The return value of
413 .Fn func
414 should be one of:
415 .Bl -tag -width "CC_REDISPLAY"
416 .It Dv CC_NORM
417 Add a normal character.
418 .It Dv CC_NEWLINE
419 End of line was entered.
420 .It Dv CC_EOF
421 EOF was entered.
422 .It Dv CC_ARGHACK
423 Expecting further command input as arguments, do nothing visually.
424 .It Dv CC_REFRESH
425 Refresh display.
426 .It Dv CC_REFRESH_BEEP
427 Refresh display, and beep.
428 .It Dv CC_CURSOR
429 Cursor moved, so update and perform
430 .Dv CC_REFRESH .
431 .It Dv CC_REDISPLAY
432 Redisplay entire input line.
433 This is useful if a key binding outputs extra information.
434 .It Dv CC_ERROR
435 An error occurred.
436 Beep, and flush tty.
437 .It Dv CC_FATAL
438 Fatal error, reset tty to known state.
439 .El
440 .It Dv EL_HIST , Fa "History *(*func)(History *, int op, ...)" , \
441 Fa "const char *ptr"
442 Defines which history function to use, which is usually
443 .Fn history .
444 .Fa ptr
445 should be the value returned by
446 .Fn history_init .
447 .It Dv EL_EDITMODE , Fa "int flag"
448 If
449 .Fa flag
450 is non-zero,
451 editing is enabled (the default).
452 Note that this is only an indication, and does not
453 affect the operation of
454 .Nm .
455 At this time, it is the caller's responsibility to
456 check this
457 (using
458 .Fn el_get )
459 to determine if editing should be enabled or not.
460 .It Dv EL_UNBUFFERED , Fa "int flag"
461 If
462 .Fa flag
463 is zero,
464 unbuffered mode is disabled (the default).
465 In unbuffered mode,
466 .Fn el_gets
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
470 .Fa f ,
471 which is to return the number of characters read and store them in
472 .Fa c .
473 This function is called internally by
474 .Fn el_gets
475 and
476 .Fn el_getc .
477 The builtin function can be set or restored with the special function
478 name
479 .Dq Dv EL_BUILTIN_GETCFN .
480 .It Dv EL_CLIENTDATA , Fa "void *data"
481 Register
482 .Fa data
483 to be associated with this EditLine structure.
484 It can be retrieved with the corresponding
485 .Fn el_get
486 call.
487 .It Dv EL_SETFP , Fa "int fd" , Fa "FILE *fp"
488 Set the current
489 .Nm editline
490 file pointer for
491 .Dq input
492 .Fa fd
493 =
494 .Dv 0 ,
495 .Dq output
496 .Fa fd
497 =
498 .Dv 1 ,
499 or
500 .Dq error
501 .Fa fd
502 =
503 .Dv 2
504 from
505 .Fa fp .
506 .El
507 .It Fn el_get
508 Get
509 .Nm
510 parameters.
511 .Fa op
512 determines which parameter to retrieve into
513 .Fa result .
514 Returns 0 if successful, \-1 otherwise.
515 .Pp
516 The following values for
517 .Fa op
518 are supported, along with actual type of
519 .Fa result :
520 .Bl -tag -width 4n
521 .It Dv EL_PROMPT , Fa "char *(*f)(EditLine *)" , Fa "char *c"
522 Set
523 .Fa f .
524 to a pointer to the function that displays the prompt.
525 If
526 .Fa c
527 is not
528 .Dv NULL ,
529 set it to the start/stop literal prompt character.
530 .It Dv EL_RPROMPT , Fa "char *(*f)(EditLine *)" , Fa "char *c"
531 Set
532 .Fa f .
533 to a pointer to the function that displays the prompt.
534 If
535 .Fa c
536 is not
537 .Dv NULL ,
538 set it to the start/stop literal prompt character.
539 .It Dv EL_EDITOR , Fa "const char **n"
540 Set the name of the editor in
541 .Fa n ,
542 which will be one of
543 .Dq emacs
544 or
545 .Dq vi .
546 .It Dv EL_GETTC , Fa "const char *name" , Fa "void *value"
547 If
548 .Fa name
549 is a valid
550 .Xr termcap 5
551 capability set
552 .Fa value
553 to the current value of that capability.
554 .It Dv EL_SIGNAL , Fa "int *s"
555 Set
556 .Fa s
557 to non zero if
558 .Nm
559 has installed private signal handlers (see
560 .Fn el_get
561 above).
562 .It Dv EL_EDITMODE , Fa "int *c"
563 Set
564 .Fa c
565 to non-zero if editing is enabled.
566 .It Dv EL_GETCFN , Fa "int (**f)(EditLine *, char *)"
567 Return a pointer to the function that read characters, which is equal to
568 .Dq Dv EL_BUILTIN_GETCFN
569 in the case of the default builtin function.
570 .It Dv EL_CLIENTDATA , Fa "void **data"
571 Set
572 .Fa data
573 to the previously registered client data set by an
574 .Fn el_set
575 call.
576 .It Dv EL_UNBUFFERED , Fa "int *c"
577 Set
578 .Fa c
579 to non-zero if unbuffered mode is enabled.
580 .It Dv EL_GETFP , Fa "int fd", Fa "FILE **fp"
581 Set
582 .Fa fp
583 to the current
584 .Nm editline
585 file pointer for
586 .Dq input
587 .Fa fd
588 =
589 .Dv 0 ,
590 .Dq output
591 .Fa fd
592 =
593 .Dv 1 ,
594 or
595 .Dq error
596 .Fa fd
597 =
598 .Dv 2 .
599 .El
600 .It Fn el_source
601 Initialize
602 .Nm
603 by reading the contents of
604 .Fa file .
605 .Fn el_parse
606 is called for each line in
607 .Fa file .
608 If
609 .Fa file
610 is
611 .Dv NULL ,
612 try
613 .Pa $HOME/.editrc .
614 Refer to
615 .Xr editrc 5
616 for details on the format of
617 .Fa file .
618 .Fn el_source
619 returns 0 on success and \-1 on error.
620 .It Fn el_resize
621 Must be called if the terminal size changes.
622 If
623 .Dv EL_SIGNAL
624 has been set with
625 .Fn el_set ,
626 then this is done automatically.
627 Otherwise, it's the responsibility of the application to call
628 .Fn el_resize
629 on the appropriate occasions.
630 .It Fn el_cursor
631 Move the cursor to the right (if positive) or to the left (if negative)
632 .Fa count
633 characters.
634 Returns the resulting offset of the cursor from the beginning of the line.
635 .It Fn el_line
636 Return the editing information for the current line in a
637 .Fa LineInfo
638 structure, which is defined as follows:
639 .Bd -literal
640 typedef struct lineinfo {
641     const char *buffer;    /* address of buffer */
642     const char *cursor;    /* address of cursor */
643     const char *lastchar;  /* address of last character */
644 } LineInfo;
645 .Ed
646 .Pp
647 .Fa buffer
648 is not NUL terminated.
649 This function may be called after
650 .Fn el_gets
651 to obtain the
652 .Fa LineInfo
653 structure pertaining to line returned by that function,
654 and from within user defined functions added with
655 .Dv EL_ADDFN .
656 .It Fn el_insertstr
657 Insert
658 .Fa str
659 into the line at the cursor.
660 Returns \-1 if
661 .Fa str
662 is empty or won't fit, and 0 otherwise.
663 .It Fn el_deletestr
664 Delete
665 .Fa count
666 characters before the cursor.
667 .El
668 .Sh HISTORY LIST FUNCTIONS
669 The history functions use a common data structure,
670 .Fa History ,
671 which is created by
672 .Fn history_init
673 and freed by
674 .Fn history_end .
675 .Pp
676 The following functions are available:
677 .Bl -tag -width 4n
678 .It Fn history_init
679 Initialize the history list, and return a data structure
680 to be used by all other history list functions, or
681 .Dv NULL
682 on failure.
683 .It Fn history_end
684 Clean up and finish with
685 .Fa h ,
686 assumed to have been created with
687 .Fn history_init .
688 .It Fn history
689 Perform operation
690 .Fa op
691 on the history list, with optional arguments as needed by the
692 operation.
693 .Fa ev
694 is changed accordingly to operation.
695 The following values for
696 .Fa op
697 are supported, along with the required argument list:
698 .Bl -tag -width 4n
699 .It Dv H_SETSIZE , Fa "int size"
700 Set size of history to
701 .Fa size
702 elements.
703 .It Dv H_GETSIZE
704 Get number of events currently in history.
705 .It Dv H_END
706 Cleans up and finishes with
707 .Fa h ,
708 assumed to be created with
709 .Fn history_init .
710 .It Dv H_CLEAR
711 Clear the history.
712 .It Dv H_FUNC , Fa "void *ptr" , Fa "history_gfun_t first" , \
713 Fa "history_gfun_t next" , Fa "history_gfun_t last" , \
714 Fa "history_gfun_t prev" , Fa "history_gfun_t curr" , \
715 Fa "history_sfun_t set" , Fa "history_vfun_t clear" , \
716 Fa "history_efun_t enter" , Fa "history_efun_t add"
717 Define functions to perform various history operations.
718 .Fa ptr
719 is the argument given to a function when it's invoked.
720 .It Dv H_FIRST
721 Return the first element in the history.
722 .It Dv H_LAST
723 Return the last element in the history.
724 .It Dv H_PREV
725 Return the previous element in the history.
726 .It Dv H_NEXT
727 Return the next element in the history.
728 .It Dv H_CURR
729 Return the current element in the history.
730 .It Dv H_SET
731 Set the cursor to point to the requested element.
732 .It Dv H_ADD , Fa "const char *str"
733 Append
734 .Fa str
735 to the current element of the history, or perform the
736 .Dv H_ENTER
737 operation with argument
738 .Fa str
739 if there is no current element.
740 .It Dv H_APPEND , Fa "const char *str"
741 Append
742 .Fa str
743 to the last new element of the history.
744 .It Dv H_ENTER , Fa "const char *str"
745 Add
746 .Fa str
747 as a new element to the history, and, if necessary,
748 removing the oldest entry to keep the list to the created size.
749 If
750 .Dv H_SETUNIQUE
751 has been called with a non-zero argument, the element
752 will not be entered into the history if its contents match
753 the ones of the current history element.
754 If the element is entered
755 .Fn history
756 returns 1; if it is ignored as a duplicate returns 0.
757 Finally
758 .Fn history
759 returns \-1 if an error occurred.
760 .It Dv H_PREV_STR , Fa "const char *str"
761 Return the closest previous event that starts with
762 .Fa str .
763 .It Dv H_NEXT_STR , Fa "const char *str"
764 Return the closest next event that starts with
765 .Fa str .
766 .It Dv H_PREV_EVENT , Fa "int e"
767 Return the previous event numbered
768 .Fa e .
769 .It Dv H_NEXT_EVENT , Fa "int e"
770 Return the next event numbered
771 .Fa e .
772 .It Dv H_LOAD , Fa "const char *file"
773 Load the history list stored in
774 .Fa file .
775 .It Dv H_SAVE , Fa "const char *file"
776 Save the history list to
777 .Fa file .
778 .It Dv H_SAVE_FP , Fa "FILE *fp"
779 Save the history list to the opened
780 .Ft FILE
781 pointer
782 .Fa fp .
783 .It Dv H_SETUNIQUE , Fa "int unique"
784 Set flag that adjacent identical event strings should not be entered
785 into the history.
786 .It Dv H_GETUNIQUE
787 Retrieve the current setting if adjacent identical elements should
788 be entered into the history.
789 .It Dv H_DEL , Fa "int e"
790 Delete the event numbered
791 .Fa e .
792 This function is only provided for
793 .Xr readline 3
794 compatibility.
795 The caller is responsible for free'ing the string in the returned
796 .Fa HistEvent .
797 .El
798 .Pp
799 .Fn history
800 returns \*[Gt]= 0 if the operation
801 .Fa op
802 succeeds.
803 Otherwise, \-1 is returned and
804 .Fa ev
805 is updated to contain more details about the error.
806 .El
807 .Sh TOKENIZATION FUNCTIONS
808 The tokenization functions use a common data structure,
809 .Fa Tokenizer ,
810 which is created by
811 .Fn tok_init
812 and freed by
813 .Fn tok_end .
814 .Pp
815 The following functions are available:
816 .Bl -tag -width 4n
817 .It Fn tok_init
818 Initialize the tokenizer, and return a data structure
819 to be used by all other tokenizer functions.
820 .Fa IFS
821 contains the Input Field Separators, which defaults to
822 .Aq space ,
823 .Aq tab ,
824 and
825 .Aq newline
826 if
827 .Dv NULL .
828 .It Fn tok_end
829 Clean up and finish with
830 .Fa t ,
831 assumed to have been created with
832 .Fn tok_init .
833 .It Fn tok_reset
834 Reset the tokenizer state.
835 Use after a line has been successfully tokenized
836 by
837 .Fn tok_line
838 or
839 .Fn tok_str
840 and before a new line is to be tokenized.
841 .It Fn tok_line
842 Tokenize
843 .Fa li ,
844 If successful, modify:
845 .Fa argv
846 to contain the words,
847 .Fa argc
848 to contain the number of words,
849 .Fa cursorc
850 (if not
851 .Dv NULL )
852 to contain the index of the word containing the cursor,
853 and
854 .Fa cursoro
855 (if not
856 .Dv NULL )
857 to contain the offset within
858 .Fa argv[cursorc]
859 of the cursor.
860 .Pp
861 Returns
862 0 if successful,
863 \-1 for an internal error,
864 1 for an unmatched single quote,
865 2 for an unmatched double quote,
866 and
867 3 for a backslash quoted
868 .Aq newline .
869 A positive exit code indicates that another line should be read
870 and tokenization attempted again.
871 .
872 .It Fn tok_str
873 A simpler form of
874 .Fn tok_line ;
875 .Fa str
876 is a NUL terminated string to tokenize.
877 .El
878 .
879 .\"XXX.Sh EXAMPLES
880 .\"XXX: provide some examples
881 .Sh SEE ALSO
882 .Xr sh 1 ,
883 .Xr signal 3 ,
884 .Xr termcap 3 ,
885 .Xr editrc 5 ,
886 .Xr termcap 5
887 .Sh HISTORY
888 The
889 .Nm
890 library first appeared in
891 .Bx 4.4 .
892 .Dv CC_REDISPLAY
893 appeared in
894 .Nx 1.3 .
895 .Dv CC_REFRESH_BEEP ,
896 .Dv EL_EDITMODE
897 and the readline emulation appeared in
898 .Nx 1.4 .
899 .Dv EL_RPROMPT
900 appeared in
901 .Nx 1.5 .
902 .Sh AUTHORS
903 .An -nosplit
904 The
905 .Nm
906 library was written by
907 .An Christos Zoulas .
908 .An Luke Mewburn
909 wrote this manual and implemented
910 .Dv CC_REDISPLAY ,
911 .Dv CC_REFRESH_BEEP ,
912 .Dv EL_EDITMODE ,
913 and
914 .Dv EL_RPROMPT .
915 .An Jaromir Dolecek
916 implemented the readline emulation.
917 .An Johny Mattsson
918 implemented wide-character support.
919 .Sh BUGS
920 At this time, it is the responsibility of the caller to
921 check the result of the
922 .Dv EL_EDITMODE
923 operation of
924 .Fn el_get
925 (after an
926 .Fn el_source
927 or
928 .Fn el_parse )
929 to determine if
930 .Nm
931 should be used for further input.
932 I.e.,
933 .Dv EL_EDITMODE
934 is purely an indication of the result of the most recent
935 .Xr editrc 5
936 .Ic edit
937 command.