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