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