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