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