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