EDITLINE(3edit)
β¨ EDITLINE(3edit) β¨
okay bestie, welcome to the editline page π
. This is the manual for the editline library β line editor, history and tokenization functions. We're gonna go through everything, no skips, periodt. π
π NAME π
editline, el_init, el_init_fd, el_end, el_reset, el_gets, el_wgets, el_getc, el_wgetc, el_push, el_wpush, el_parse, el_wparse, el_set, el_wset, el_get, el_wget, el_source, el_resize, el_cursor, el_line, el_wline, el_insertstr, el_winsertstr, el_deletestr, el_wdeletestr, history_init, history_winit, history_end, history_wend, history, history_w, tok_init, tok_winit, tok_end, tok_wend, tok_reset, tok_wreset, tok_line, tok_wline, tok_str, tok_wstr β line editor, history and tokenization functions
π LIBRARY π
Command Line Editor Library (libedit, -ledit)
π SYNOPSIS π
#include <histedit.h>
EditLine *
el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr);
EditLine *
el_init_fd(const char *prog, FILE *fin, FILE *fout, FILE *ferr,
int fdin, int fdout, int fderr);
void
el_end(EditLine *e);
void
el_reset(EditLine *e);
const char *
el_gets(EditLine *e, int *count);
const wchar_t *
el_wgets(EditLine *e, int *count);
int
el_getc(EditLine *e, char *ch);
int
el_wgetc(EditLine *e, wchar_t *wc);
void
el_push(EditLine *e, const char *mbs);
void
el_wpush(EditLine *e, const wchar_t *wcs);
int
el_parse(EditLine *e, int argc, const char *argv[]);
int
el_wparse(EditLine *e, int argc, const wchar_t *argv[]);
int
el_set(EditLine *e, int op, ...);
int
el_wset(EditLine *e, int op, ...);
int
el_get(EditLine *e, int op, ...);
int
el_wget(EditLine *e, int op, ...);
int
el_source(EditLine *e, const char *file);
void
el_resize(EditLine *e);
int
el_cursor(EditLine *e, int count);
const LineInfo *
el_line(EditLine *e);
const LineInfoW *
el_wline(EditLine *e);
int
el_insertstr(EditLine *e, const char *str);
int
el_winsertstr(EditLine *e, const wchar_t *str);
void
el_deletestr(EditLine *e, int count);
void
el_wdeletestr(EditLine *e, int count);
History *
history_init(void);
HistoryW *
history_winit(void);
void
history_end(History *h);
void
history_wend(HistoryW *h);
int
history(History *h, HistEvent *ev, int op, ...);
int
history_w(HistoryW *h, HistEventW *ev, int op, ...);
Tokenizer *
tok_init(const char *IFS);
TokenizerW *
tok_winit(const wchar_t *IFS);
void
tok_end(Tokenizer *t);
void
tok_wend(TokenizerW *t);
void
tok_reset(Tokenizer *t);
void
tok_wreset(TokenizerW *t);
int
tok_line(Tokenizer *t, const LineInfo *li, int *argc,
const char **argv[], int *cursorc, int *cursoro);
int
tok_wline(TokenizerW *t, const LineInfoW *li, int *argc,
const wchar_t **argv[], int *cursorc, int *cursoro);
int
tok_str(Tokenizer *t, const char *str, int *argc, const char **argv[]);
int
tok_wstr(TokenizerW *t, const wchar_t *str, int *argc,
const wchar_t **argv[]);
πΈ DESCRIPTION πΈ
The editline library provides generic line editing, history and tokenization functions, similar to those found in sh(1). iykyk π
These functions are available in the libedit library (which needs the libtermcap library). Programs should be linked with -ledit -ltermcap.
The editline library respects the LC_CTYPE locale set by the application program and never uses setlocale(3) to change the locale. no cap β¨
β¨ LINE EDITING FUNCTIONS β¨
The line editing functions use a common data structure, EditLine, which is created by el_init() or el_init_fd() and freed by el_end().
The wide-character functions behave the same way as their narrow counterparts. fr fr
The following functions are available:
el_init()β okay so this is the main character init: it sets up the line editor and gives you back a data structure that all the other line editing functions are gonna use, orNULLon failure.progis the name of the invoking program, used when reading theeditrc(5)file to determine which settings to use.fin,foutandferrare the input, output, and error streams (respectively) to use. In this documentation, references to βthe ttyβ are actually to this input/output stream combination. πel_init_fd()β Likeel_init()but allows specifying file descriptors for thestdio(3)corresponding streams, in case those were created withfunopen(3). lowkey usefulel_end()β Clean up and finish withe, assumed to have been created withel_init()orel_init_fd(). bye bestie πel_reset()β Reset the tty and the parser. This should be called after an error which may have upset the tty's state. important β οΈel_gets()β Read a line from the tty.countis modified to contain the number of characters read. Returns the line read if successful, orNULLif no characters were read or if an error occurred. If an error occurred,countis set to-1anderrnocontains the error code that caused it. The return value may not remain valid across calls toel_gets()and must be copied if the data is to be retained. copy it if you need it πel_wgetc()β Read a wide character from the tty, respecting the current locale, or from the input queue described ineditline(7)if that is not empty, and store it inwc. If an invalid or incomplete character is found, it is discarded,errnois set toEILSEQ, and the next character is read and stored inwc. Returns1if a valid character was read,0on end of file, or-1onread(2)failure. In the latter case,errnois set to indicate the error. wide char energy πel_getc()β Read a wide character as described forel_wgetc()and return0on end of file or-1on failure. If the wide character can be represented as a single-byte character, convert it withwctob(3), store the result inch, and return1; otherwise, seterrnotoERANGEand return-1. In the C or POSIX locale, this simply reads a byte, but for any other locale, including UTF-8, this is rarely useful. ngl, not the vibe for UTF-8 πel_wpush()β Push the wide character stringwcsback onto the input queue described ineditline(7). If the queue overflows, for example due to a recursive macro, or if an error occurs, for example becausewcsisNULLor memory allocation fails, the function beeps at the user, but does not report the problem to the caller. beep beep π’el_push()β Use the current locale to convert the multibyte stringmbsto a wide character string, and pass the result toel_wpush(). convert and pushel_parse()β Parses theargvarray (which isargcelements in size) to execute builtin editline commands. If the command is prefixed with βprogβ: thenel_parse()will only execute the command if βprogβ matches theprogargument supplied toel_init(). The return value is-1if the command is unknown,0if there was no error or βprogβ didn't match, or1if the command returned an error. Refer toeditrc(5)for more information. parse it, bestie π§el_set()β Set editline parameters.opdetermines which parameter to set, and each operation has its own parameter list. Returns0on success,-1on failure. the big one π
The following values for op are supported, along with the required argument list:
EL_PROMPT,char *(*f)(EditLine *)β Define prompt printing function asf, which is to return a string that contains the prompt. prompt vibesEL_PROMPT_ESC,char *(*f)(EditLine *),char cβ Same asEL_PROMPT, but thecargument indicates the start/stop literal prompt character.
If a start/stop literal character is found in the prompt, the character itself is not printed, but characters after it are printed directly to the terminal without affecting the state of the current line. A subsequent second start/stop literal character ends this behavior. This is typically used to embed literal escape sequences that change the color/style of the terminal in the prompt. Note that the literal escape character cannot be the last character in the prompt, as the escape sequence is attached to the next character in the prompt. 0 unsets it. escape character realness β¨
EL_REFRESHβ Re-display the current line on the next terminal line. refresh πEL_RPROMPT,char *(*f)(EditLine *)β Define right side prompt printing function asf, which is to return a string that contains the prompt. right sideEL_RPROMPT_ESC,char *(*f)(EditLine *),char cβ Define the right prompt printing function but with a literal escape character. escape againEL_TERMINAL,const char *typeβ Define terminal type of the tty to betype, or toTERMiftypeisNULL. terminal typeEL_EDITOR,const char *modeβ Set editing mode tomode, which must be one of βemacsβ or βviβ. emacs or vi, pick your fighter βοΈEL_SIGNAL,int flagβ Ifflagis non-zero, editline will install its own signal handler for the following signals when reading command input:SIGCONT,SIGHUP,SIGINT,SIGQUIT,SIGSTOP,SIGTERM,SIGTSTP, andSIGWINCH. Otherwise, the current signal handlers will be used. signals, handle with care π¨EL_BIND,const char *, ..., NULLβ Perform thebindbuiltin command. Refer toeditrc(5)for more information. bind itEL_ECHOTC,const char *, ..., NULLβ Perform theechotcbuiltin command. Refer toeditrc(5)for more information. echotcEL_SETTC,const char *, ..., NULLβ Perform thesettcbuiltin command. Refer toeditrc(5)for more information. settcEL_SETTY,const char *, ..., NULLβ Perform thesettybuiltin command. Refer toeditrc(5)for more information. settyEL_TELLTC,const char *, ..., NULLβ Perform thetelltcbuiltin command. Refer toeditrc(5)for more information. telltcEL_ADDFN,const char *name,const char *help,unsigned char (*func)(EditLine *e, int ch)β Add a user defined function,func(), referred to asnamewhich is invoked when a key which is bound tonameis entered.helpis a description ofname. At invocation time,chis the key which caused the invocation. The return value offunc()should be one of: add your own function, main character energy πCC_NORMβ Add a normal character. normalCC_NEWLINEβ End of line was entered. newlineCC_EOFβ EOF was entered. EOFCC_ARGHACKβ Expecting further command input as arguments, do nothing visually. arghack
CC_REFRESHβ refresh the display, bestie. that's it, that's the whole job πCC_REFRESH_BEEPβ refresh the display and beep. lil refresh, lil noise, iconic combo β¨CC_CURSORβ the cursor moved! so update stuff and then performCC_REFRESH. two for one π«ΆCC_REDISPLAYβ redisplay the entire input line. this one's your bestie when a key binding outputs extra info and everything gets scrambled πCC_ERRORβ an error happened π beep, and flush the tty.CC_FATALβ fatal error, no cap. reset the tty to a known state. this is the emergency-exit vibe πEL_HIST,History *(*func)(History *, int op, ...),const char *ptrβ decides which history function gets used, which is usually justhistory().ptrshould be the value thathistory_init()handed back to you.EL_EDITMODE,int flagβ ifflagis non-zero, editing is enabled (that's the default β¨). lowkey important caveat though: this is only an indication, and it does not affect how editline actually operates. at this time, it's the caller's job to check this (viael_get()) to figure out whether editing should be enabled or not. so likeβ¦ you gotta do the vibe check yourself πEL_UNBUFFERED,int flagβ ifflagis zero, unbuffered mode is off (the default). in unbuffered mode,el_gets()returns immediately after processing a single character. no waiting, no chill, just one char and go πββοΈEL_SAFEREAD,int flagβ if theflagargument is non-zero, editline tries to recover from read errors: it ignores the first interrupted error, and it tries to reset the input file descriptor to reset non-blocking I/O. this is disabled by default, and it's only desirable when editline is used in shell-like applications. fr fr, only turn this on if you know why π¨EL_GETCFN,el_rfunc_t fβ whenever a character is read, use the function
int f(EditLine *e, wchar_t *wc)
which stores the character in wc and returns 1 on success, 0 on end of file, or -1 on I/O or encoding errors. the functions that use it internally include el_wgets(), el_wgetc(), el_gets(), and el_getc(). initially a builtin function is installed, and replacing it is discouraged because writing such a function is very error prone π. the builtin function can be restored at any time by passing the special value EL_BUILTIN_GETCFN instead of a function pointer. keep that one in your pocket π
EL_CLIENTDATA,void *dataβ registerdataso it's associated with thisEditLinestructure. you can grab it back later with the correspondingel_get()call.EL_SETFP,int fd,FILE *fpβ set the current editline file pointer forβinputβfd = 0,βoutputβfd = 1, orβerrorβfd = 2fromfp.
π« el_get() β the receiving end π«
Get editline parameters. op decides which parameter gets retrieved into result. returns 0 if it worked, -1 otherwise.
The following values for op are supported, along with the actual type of result:
EL_PROMPT,char *(*f)(EditLine *),char *cβ setfto a pointer to the function that displays the prompt. ifcis notNULL, set it to the start/stop literal prompt character.EL_RPROMPT,char *(*f)(EditLine *),char *cβ setfto a pointer to the function that displays the prompt. ifcis notNULL, set it to the start/stop literal prompt character. (yes bestie, it's the same description asEL_PROMPTβ that's just how the page do π )EL_EDITOR,const char **nβ set the name of the editor inn, which will be one ofβemacsβorβviβ. choose your fighter βοΈEL_GETTC,const char *name,void *valueβ ifnameis a validtermcap(5)capability, setvalueto the current value of that capability.EL_SIGNAL,int *sβ setsto non-zero if editline has installed private signal handlers (seeel_get()above).EL_EDITMODE,int *cβ setcto non-zero if editing is enabled.EL_GETCFN,el_rfunc_t *fβ setfto a pointer to the function that reads characters, or toEL_BUILTIN_GETCFNif the builtin function is the one in use.EL_CLIENTDATA,void **dataβ setdatato the previously registered client data that was set by anel_set()call.EL_UNBUFFERED,int *cβ setcto non-zero if unbuffered mode is enabled.EL_SAFEREAD,int *cβ setcto non-zero if safe read is set.EL_GETFP,int fd,FILE **fpβ setfpto the current editline file pointer forβinputβfd = 0,βoutputβfd = 1, orβerrorβfd = 2.
πΈ el_source() πΈ
Initialize editline by reading the contents of file. el_parse() gets called for each line in file. if file is NULL, it tries $EDITRC, and if that isn't set, $HOME/.editrc. peek at editrc(5) for the details on the format of file. el_source() returns 0 on success and -1 on error. the duality of girlhood π
π¦ el_resize() π¦
Must be called if the terminal size changes. if EL_SIGNAL has been set with el_set(), then this happens automatically for you, slay. otherwise it's the application's responsibility to call el_resize() on the appropriate occasions. no one else is gonna do it for you, bestie π«Ά
π el_cursor() π
Move the cursor to the right (if positive) or to the left (if negative) count characters. returns the resulting offset of the cursor from the beginning of the line.
β el_line() β
Return the editing information for the current line in a LineInfo structure, which is defined like this:
typedef struct lineinfo {
const char *buffer; /* address of buffer */
const char *cursor; /* address of cursor */
const char *lastchar; /* address of last character */
} LineInfo;
buffer is not NUL terminated, jsyk π. you can call this function after el_gets() to obtain the LineInfo structure pertaining to the line that function returned, and also from inside user defined functions added with EL_ADDFN.
π el_insertstr() π
Insert str into the line at the cursor. returns -1 if str is empty or won't fit, and 0 otherwise. very "make room for me" energy π
π el_deletestr() π
Delete count characters before the cursor. bye bye, character era π¦
π HISTORY LIST FUNCTIONS π
The history functions all share one common data structure, History, which is created by history_init() and freed by history_end(). found family π«Ά
The following functions are available:
β¨ history_init() β¨
Initialize the history list, and return a data structure to be used by all the other history list functions β or NULL on failure π.
π history_end() π
Clean up and finish with h, which is assumed to have been created with history_init().
π₯ history() π₯
Perform operation op on the history list, with optional arguments as needed by the operation. ev gets changed accordingly to the operation. The following values for op are supported, along with the required argument list:
H_SETSIZE,int sizeβ set the size of the history tosizeelements.H_GETSIZEβ get the number of events currently in the history.H_ENDβ cleans up and finishes withh, assumed to have been created withhistory_init().H_CLEARβ clear the history. poof, gone π¨H_FUNC,void *ptr,history_gfun_t first,history_gfun_t next,history_gfun_t last,history_gfun_t prev,history_gfun_t curr,history_sfun_t set,history_vfun_t clear,history_efun_t enter,history_efun_t addβ define functions to perform the various history operations.ptris the argument given to a function when it's invoked.H_FIRSTβ return the first element in the history.H_LASTβ return the last element in the history.H_PREVβ return the previous element in the history. it's newer than the current one.H_NEXTβ return the next element in the history. it's older than the current one.H_CURRβ return the current element in the history.H_SET,int positionβ set the cursor to point to the requested element.H_ADD,const char *strβ appendstrto the current element of the history, or perform theH_ENTERoperation with argumentstrif there's no current element.H_APPEND,const char *strβ appendstrto the last new element of the history.H_ENTER,const char *strβ addstras a new element to the history and, if necessary, remove the oldest entry to keep the list at the size it was created with. ifH_SETUNIQUEhas been called with a non-zero argument, the element will not be entered into the history if its contents match the ones of the current history element. if the element is entered,history()returns1; if it's ignored as a duplicate, it returns0. finally,history()returns-1if an error occurred. three possible outcomes, main character range πH_PREV_STR,const char *strβ return the closest previous event that starts withstr.H_NEXT_STR,const char *strβ return the closest next event that starts withstr.H_PREV_EVENT,int eβ return the previous event numberede.H_NEXT_EVENT,int eβ return the next event numberede.H_LOAD,const char *fileβ load the history list stored infile.H_SAVE,const char *fileβ save the history list tofile.H_SAVE_FP,FILE *fpβ save the history list to the openedFILEpointerfp.H_NSAVE_FP,size_t n,FILE *fpβ save the lastnhistory entries to the openedFILEpointerfp.H_SETUNIQUE,int uniqueβ set the flag that adjacent identical event strings should not be entered into the history. no repeats, we don't do reruns here πH_GETUNIQUEβ retrieve the current setting for whether adjacent identical elements should be entered into the history.H_DEL,int eβ delete the event numberede. heads up: this function is only provided for readline compatibility. the caller is responsible forfree'ing the string in the returnedHistEventπ¨
history() returns >= 0 if the operation op succeeds. otherwise -1 is returned and ev is updated to contain more details about the error.
π TOKENIZATION FUNCTIONS π
okay so bestie, these functions all share one common data structure called a Tokenizer, and here's the vibe: it gets created by tok_init() and freed by tok_end(). that's it, that's the lifecycle, no cap π
The following functions are available:
tok_init()β sets up the tokenizer and hands you back a data structure that literally every other tokenizer function is gonna need.IFSholds the Input Field Separators, and if it'sNULLit just defaults to β¨spaceβ©, β¨tabβ©, and β¨newlineβ©. so cozy, so default β¨tok_end()β cleans everything up and says bye tot, which we're assuming you made withtok_init()in the first place. no orphaned tokenizers in this house π«Άtok_reset()β resets the tokenizer state. use this after a line has been successfully tokenized bytok_line()ortok_str(), and before you go tokenizing a whole new line. it's giving "fresh start, clean slate" π¦tok_line()β tokenizesli. If it succeeds, it modifies:argvto contain the words,argcto contain the number of words,cursorc(if it's notNULL) to contain the index of the word containing the cursor, andcursoro(if it's notNULL) to contain the offset withinargv[cursorc]of the cursor. she's thorough, she's detail-oriented, we love her π
Returns 0 if successful, -1 for an internal error, 1 for an unmatched single quote, 2 for an unmatched double quote, and 3 for a backslash quoted β¨newlineβ©. and listen β a positive exit code means you should read another line and try the tokenization again. don't skip that part, i'm serious π
tok_str()β the simpler, chiller version oftok_line().stris a NUL terminated string to tokenize. lowkey the easy mode, and that's valid πΈ
π SEE ALSO π
sh(1), signal(3), termcap(3), editrc(5edit), termcap(5), editline(7edit)
go read those too, bestie, it's called β¨enrichmentβ¨ π
π HISTORY π
The editline library first appeared in 4.4BSD. CC_REDISPLAY appeared in NetBSD 1.3. CC_REFRESH_BEEP, EL_EDITMODE and the readline emulation appeared in NetBSD 1.4. EL_RPROMPT appeared in NetBSD 1.5.
a whole glow-up timeline fr fr, we love an evolution arc π¦
π AUTHORS π
The editline library was written by Christos Zoulas. Luke Mewburn wrote this manual and implemented CC_REDISPLAY, CC_REFRESH_BEEP, EL_EDITMODE, and EL_RPROMPT. Jaromir Dolecek implemented the readline emulation. Johny Mattsson implemented wide-character support.
icons, every single one of them, no notes πβ¨
π BUGS π
At this time, it is the responsibility of the caller to check the result of the EL_EDITMODE operation of el_get() (after an el_source() or el_parse()) to figure out if editline should be used for further input. I.e., EL_EDITMODE is purely an indication of the result of the most recent editrc(5) edit command.
so yeah bestie,EL_EDITMODEis not gonna do the thinking for you β she's just reporting back what the lasteditrc(5)edit command said. you gotta check that yourself. the call is coming from inside the house ππ
and that's the whole page, we made it π this one's from the Debian crew, dated August 15, 2021 β thanks for reading the EDITLINE(3edit) manual with me, mwah ππ
π¦ where this page comes from
- debian bookworm: libedit-dev 3.1-20221030-2
- ubuntu jammy: libedit-dev 3.1-20210910-1build1
- ubuntu noble: libedit-dev 3.1-20230828-1build1
- Copyright (c) 1997-2014 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms