1April 5, 2008() April 5, 2008()
2
3
4
6 editline, el_init, el_end, el_reset, el_gets, el_getc, el_push,
7 el_parse, el_set, el_get, el_source, el_resize, el_line, el_insertstr,
8 el_deletestr, history_init, history_end, history, tok_init, tok_end,
9 tok_reset, tok_line, tok_str - line editor, history and tokenization
10 functions
11
13 Command Line Editor Library (libedit, -ledit)
14
16 #include <histedit.h>
17
18 EditLine *
19 el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr);
20
21 void
22 el_end(EditLine *e);
23
24 void
25 el_reset(EditLine *e);
26
27 const char *
28 el_gets(EditLine *e, int *count);
29
30 int
31 el_getc(EditLine *e, char *ch);
32
33 void
34 el_push(EditLine *e, const char *str);
35
36 int
37 el_parse(EditLine *e, int argc, const char *argv[]);
38
39 int
40 el_set(EditLine *e, int op, ...);
41
42 int
43 el_get(EditLine *e, int op, ...);
44
45 int
46 el_source(EditLine *e, const char *file);
47
48 void
49 el_resize(EditLine *e);
50
51 const LineInfo *
52 el_line(EditLine *e);
53
54 int
55 el_insertstr(EditLine *e, const char *str);
56
57 void
58 el_deletestr(EditLine *e, int count);
59
60 History *
61 history_init();
62
63 void
64 history_end(History *h);
65
66 int
67 history(History *h, HistEvent *ev, int op, ...);
68
69 Tokenizer *
70 tok_init(const char *IFS);
71
72 void
73 tok_end(Tokenizer *t);
74
75 void
76 tok_reset(Tokenizer *t);
77
78 int
79 tok_line(Tokenizer *t, const LineInfo *li, int *argc, const char
80 **argv[], int *cursorc, int *cursoro);
81
82 int
83 tok_str(Tokenizer *t, const char *str, int *argc, const char **argv[]);
84
86 The editline library provides generic line editing, history and tok‐
87 enization functions, similar to those found in sh(1).
88
89 These functions are available in the libedit library (which needs the
90 libcurses library). Programs should be linked with -ledit -lcurses.
91
93 The line editing functions use a common data structure, EditLine, which
94 is created by el_init() and freed by el_end().
95
96 The following functions are available:
97
98 el_init()
99 Initialise the line editor, and return a data structure to be
100 used by all other line editing functions. prog is the name of
101 the invoking program, used when reading the editrc(5) file to
102 determine which settings to use. fin, fout and ferr are the
103 input, output, and error streams (respectively) to use. In this
104 documentation, references to ``the tty'' are actually to this
105 input/output stream combination.
106
107 el_end()
108 Clean up and finish with e, assumed to have been created with
109 el_init().
110
111 el_reset()
112 Reset the tty and the parser. This should be called after an
113 error which may have upset the tty's state.
114
115 el_gets()
116 Read a line from the tty. count is modified to contain the num‐
117 ber of characters read. Returns the line read if successful, or
118 NULL if no characters were read or if an error occurred.
119
120 el_getc()
121 Read a character from the tty. ch is modified to contain the
122 character read. Returns the number of characters read if suc‐
123 cessful, -1 otherwise.
124
125 el_push()
126 Pushes str back onto the input stream. This is used by the
127 macro expansion mechanism. Refer to the description of bind -s
128 in editrc(5) for more information.
129
130 el_parse()
131 Parses the argv array (which is argc elements in size) to exe‐
132 cute builtin editline commands. If the command is prefixed with
133 ``prog :'' then el_parse() will only execute the command if
134 ``prog'' matches the prog argument supplied to el_init(). The
135 return value is -1 if the command is unknown, 0 if there was no
136 error or ``prog'' didn't match, or 1 if the command returned an
137 error. Refer to editrc(5) for more information.
138
139 el_set()
140 Set editline parameters. op determines which parameter to set,
141 and each operation has its own parameter list.
142
143 The following values for op are supported, along with the
144 required argument list:
145
146 EL_PROMPT , char *(*f)(EditLine *)
147 Define prompt printing function as f, which is to return
148 a string that contains the prompt.
149
150 EL_REFRESH
151 Re-display the current line on the next terminal line.
152
153 EL_RPROMPT , char *(*f)(EditLine *)
154 Define right side prompt printing function as f, which is
155 to return a string that contains the prompt.
156
157 EL_TERMINAL , const char *type
158 Define terminal type of the tty to be type, or to TERM if
159 type is NULL .
160
161 EL_EDITOR , const char *mode
162 Set editing mode to mode, which must be one of ``emacs''
163 or ``vi''.
164
165 EL_SIGNAL , int flag
166 If flag is non-zero, editline will install its own signal
167 handler for the following signals when reading command
168 input: SIGCONT , SIGHUP , SIGINT , SIGQUIT , SIGSTOP ,
169 SIGTERM , SIGTSTP , and SIGWINCH . Otherwise, the cur‐
170 rent signal handlers will be used.
171
172 EL_BIND , const char *, ..., NULL
173 Perform the bind builtin command. Refer to editrc(5) for
174 more information.
175
176 EL_ECHOTC , const char *, ..., NULL
177 Perform the echotc builtin command. Refer to editrc(5)
178 for more information.
179
180 EL_SETTC , const char *, ..., NULL
181 Perform the settc builtin command. Refer to editrc(5)
182 for more information.
183
184 EL_SETTY , const char *, ..., NULL
185 Perform the setty builtin command. Refer to editrc(5)
186 for more information.
187
188 EL_TELLTC , const char *, ..., NULL
189 Perform the telltc builtin command. Refer to editrc(5)
190 for more information.
191
192 EL_ADDFN , const char *name, const char *help, unsigned char
193 (*func)(EditLine *e, int ch)
194 Add a user defined function, func(), referred to as name
195 which is invoked when a key which is bound to name is
196 entered. help is a description of name. At invocation
197 time, ch is the key which caused the invocation. The
198 return value of func() should be one of:
199
200 CC_NORM
201 Add a normal character.
202
203 CC_NEWLINE
204 End of line was entered.
205
206 CC_EOF EOF was entered.
207
208 CC_ARGHACK
209 Expecting further command input as arguments, do
210 nothing visually.
211
212 CC_REFRESH
213 Refresh display.
214
215 CC_REFRESH_BEEP
216 Refresh display, and beep.
217
218 CC_CURSOR
219 Cursor moved, so update and perform CC_REFRESH .
220
221 CC_REDISPLAY
222 Redisplay entire input line. This is useful if a
223 key binding outputs extra information.
224
225 CC_ERROR
226 An error occurred. Beep, and flush tty.
227
228 CC_FATAL
229 Fatal error, reset tty to known state.
230
231 EL_HIST , History *(*func)(History *, int op, ...), const char
232 *ptr
233 Defines which history function to use, which is usually
234 history(). ptr should be the value returned by his‐
235 tory_init().
236
237 EL_EDITMODE , int flag
238 If flag is non-zero, editing is enabled (the default).
239 Note that this is only an indication, and does not affect
240 the operation of . At this time, it is the caller's
241 responsibility to check this (using el_get() ) to deter‐
242 mine if editing should be enabled or not.
243
244 EL_GETCFN , int (*f)(EditLine *, char *c)
245 Define the character reading function as f, which is to
246 return the number of characters read and store them in c.
247 This function is called internally by el_gets() and
248 el_getc(). The builtin function can be set or restored
249 with the special function name ``EL_BUILTIN_GETCFN''.
250
251 EL_CLIENTDATA , void *data
252 Register data to be associated with this EditLine struc‐
253 ture. It can be retrieved with the corresponding
254 el_get() call.
255
256 EL_SETFP , int fd, Fa FILE *fp
257 Set the current editline file pointer for ``input'' fd =
258 0 , ``output'' fd = 1 , or ``error'' fd = 2 from fp.
259
260 el_get()
261 Get editline parameters. op determines which parameter to
262 retrieve into result. Returns 0 if successful, -1 otherwise.
263
264 The following values for op are supported, along with actual
265 type of result :
266
267 EL_PROMPT , char *(*f)(EditLine *)
268 Return a pointer to the function that displays the
269 prompt.
270
271 EL_RPROMPT , char *(*f)(EditLine *)
272 Return a pointer to the function that displays the right‐
273 side prompt.
274
275 EL_EDITOR , const char *
276 Return the name of the editor, which will be one of
277 ``emacs'' or ``vi''.
278
279 EL_GETTC , const char *name, Fa void *value
280 Return non-zero if name is a valid termcap(5) capability
281 and set value to the current value of that capability.
282
283 EL_SIGNAL , int *
284 Return non-zero if editline has installed private signal
285 handlers (see el_get() above).
286
287 EL_EDITMODE , int *
288 Return non-zero if editing is enabled.
289
290 EL_GETCFN , int (**f)(EditLine *, char *)
291 Return a pointer to the function that read characters,
292 which is equal to ``EL_BUILTIN_GETCFN'' in the case of
293 the default builtin function.
294
295 EL_CLIENTDATA , void **data
296 Retrieve data previously registered with the correspond‐
297 ing el_set() call.
298
299 EL_UNBUFFERED , int
300 Sets or clears unbuffered mode. In this mode, el_gets()
301 will return immediately after processing a single charac‐
302 ter.
303
304 EL_PREP_TERM , int
305 Sets or clears terminal editing mode.
306
307 EL_GETFP , int fd", Fa FILE **fp
308 Return in fp the current editline file pointer for
309 ``input'' fd = 0 , ``output'' fd = 1 , or ``error'' fd =
310 2 .
311
312 el_source()
313 Initialise editline by reading the contents of file. el_parse()
314 is called for each line in file. If file is NULL , try
315 $PWD/.editrc then $HOME/.editrc. Refer to editrc(5) for details
316 on the format of file.
317
318 el_resize()
319 Must be called if the terminal size changes. If EL_SIGNAL has
320 been set with el_set(), then this is done automatically. Other‐
321 wise, it's the responsibility of the application to call
322 el_resize() on the appropriate occasions.
323
324 el_line()
325 Return the editing information for the current line in a Line‐
326 Info structure, which is defined as follows:
327
328 typedef struct lineinfo {
329 const char *buffer; /* address of buffer */
330 const char *cursor; /* address of cursor */
331 const char *lastchar; /* address of last character */
332 } LineInfo;
333
334 buffer is not NUL terminated. This function may be called after
335 el_gets() to obtain the LineInfo structure pertaining to line
336 returned by that function, and from within user defined func‐
337 tions added with EL_ADDFN .
338
339 el_insertstr()
340 Insert str into the line at the cursor. Returns -1 if str is
341 empty or won't fit, and 0 otherwise.
342
343 el_deletestr()
344 Delete count characters before the cursor.
345
347 The history functions use a common data structure, History, which is
348 created by history_init() and freed by history_end().
349
350 The following functions are available:
351
352 history_init()
353 Initialise the history list, and return a data structure to be
354 used by all other history list functions.
355
356 history_end()
357 Clean up and finish with h, assumed to have been created with
358 history_init().
359
360 history()
361 Perform operation op on the history list, with optional argu‐
362 ments as needed by the operation. ev is changed accordingly to
363 operation. The following values for op are supported, along
364 with the required argument list:
365
366 H_SETSIZE , int size
367 Set size of history to size elements.
368
369 H_GETSIZE
370 Get number of events currently in history.
371
372 H_END Cleans up and finishes with h, assumed to be created with
373 history_init().
374
375 H_CLEAR
376 Clear the history.
377
378 H_FUNC , void *ptr, history_gfun_t first, history_gfun_t next,
379 history_gfun_t last, history_gfun_t prev, history_gfun_t curr,
380 history_sfun_t set, history_vfun_t clear, history_efun_t enter,
381 history_efun_t add
382 Define functions to perform various history operations.
383 ptr is the argument given to a function when it's
384 invoked.
385
386 H_FIRST
387 Return the first element in the history.
388
389 H_LAST Return the last element in the history.
390
391 H_PREV Return the previous element in the history.
392
393 H_NEXT Return the next element in the history.
394
395 H_CURR Return the current element in the history.
396
397 H_SET Set the cursor to point to the requested element.
398
399 H_ADD , const char *str
400 Append str to the current element of the history, or per‐
401 form the H_ENTER operation with argument str if there is
402 no current element.
403
404 H_APPEND , const char *str
405 Append str to the last new element of the history.
406
407 H_ENTER , const char *str
408 Add str as a new element to the history, and, if neces‐
409 sary, removing the oldest entry to keep the list to the
410 created size. If H_SETUNIQUE was has been called with a
411 non-zero arguments, the element will not be entered into
412 the history if its contents match the ones of the current
413 history element. If the element is entered history()
414 returns 1, if it is ignored as a duplicate returns 0.
415 Finally history() returns -1 if an error occurred.
416
417 H_PREV_STR , const char *str
418 Return the closest previous event that starts with str.
419
420 H_NEXT_STR , const char *str
421 Return the closest next event that starts with str.
422
423 H_PREV_EVENT , int e
424 Return the previous event numbered e.
425
426 H_NEXT_EVENT , int e
427 Return the next event numbered e.
428
429 H_LOAD , const char *file
430 Load the history list stored in file.
431
432 H_SAVE , const char *file
433 Save the history list to file.
434
435 H_SETUNIQUE , int unique
436 Set flag that adjacent identical event strings should not
437 be entered into the history.
438
439 H_GETUNIQUE
440 Retrieve the current setting if adjacent identical ele‐
441 ments should be entered into the history.
442
443 H_DEL , int e
444 Delete the event numbered e. This function is only pro‐
445 vided for readline(3) compatibility. The caller is
446 responsible for free'ing the string in the returned His‐
447 tEvent.
448
449 history() returns = 0 if the operation op succeeds. Otherwise, -1 is
450 returned and ev is updated to contain more details about the error.
451
453 The tokenization functions use a common data structure, Tokenizer,
454 which is created by tok_init() and freed by tok_end().
455
456 The following functions are available:
457
458 tok_init()
459 Initialise the tokenizer, and return a data structure to be used
460 by all other tokenizer functions. IFS contains the Input Field
461 Separators, which defaults to <space> , <tab> , and <newline> if
462 NULL .
463
464 tok_end()
465 Clean up and finish with t, assumed to have been created with
466 tok_init().
467
468 tok_reset()
469 Reset the tokenizer state. Use after a line has been success‐
470 fully tokenized by tok_line() or tok_str() and before a new line
471 is to be tokenized.
472
473 tok_line()
474 Tokenize li, If successful, modify: argv to contain the words,
475 argc to contain the number of words, cursorc (if not NULL ) to
476 contain the index of the word containing the cursor, and cursoro
477 (if not NULL ) to contain the offset within argv[cursorc] of the
478 cursor.
479
480 Returns 0 if successful, -1 for an internal error, 1 for an
481 unmatched single quote, 2 for an unmatched double quote, and 3
482 for a backslash quoted <newline .> A positive exit code indi‐
483 cates that another line should be read and tokenization
484 attempted again.
485
486 tok_str()
487 A simpler form of tok_line(; ) str is a NUL terminated string to
488 tokenize.
489
491 sh(1), signal(3), termcap(3), editrc(5), termcap(5)
492
494 The editline library first appeared in Bx 4.4 . CC_REDISPLAY appeared
495 in Nx 1.3 . CC_REFRESH_BEEP , EL_EDITMODE and the readline emulation
496 appeared in Nx 1.4 . EL_RPROMPT appeared in Nx 1.5 .
497
499 The editline library was written by Christos Zoulas. Luke Mewburn
500 wrote this manual and implemented CC_REDISPLAY , CC_REFRESH_BEEP ,
501 EL_EDITMODE , and EL_RPROMPT . Jaromir Dolecek implemented the read‐
502 line emulation.
503
505 At this time, it is the responsibility of the caller to check the
506 result of the EL_EDITMODE operation of el_get() (after an el_source()
507 or el_parse() ) to determine if editline should be used for further
508 input. I.e., EL_EDITMODE is purely an indication of the result of the
509 most recent editrc(5) edit command.
510
511
512
513 April 5, 2008()