1 @comment %**start of header (This is for running Texinfo on a region.)
2 @setfilename rltech.info
3 @comment %**end of header (This is for running Texinfo on a region.)
6 This document describes the GNU Readline Library, a utility for aiding
7 in the consistency of user interface across discrete programs that need
8 to provide a command line interface.
10 Copyright (C) 1988-2007 Free Software Foundation, Inc.
12 Permission is granted to make and distribute verbatim copies of
13 this manual provided the copyright notice and this permission notice
14 pare preserved on all copies.
17 Permission is granted to process this file through TeX and print the
18 results, provided the printed document carries copying permission
19 notice identical to this one except for the removal of this paragraph
20 (this paragraph not being relevant to the printed manual).
23 Permission is granted to copy and distribute modified versions of this
24 manual under the conditions for verbatim copying, provided that the entire
25 resulting derived work is distributed under the terms of a permission
26 notice identical to this one.
28 Permission is granted to copy and distribute translations of this manual
29 into another language, under the above conditions for modified versions,
30 except that this permission notice may be stated in a translation approved
34 @node Programming with GNU Readline
35 @chapter Programming with GNU Readline
37 This chapter describes the interface between the @sc{gnu} Readline Library and
38 other programs. If you are a programmer, and you wish to include the
39 features found in @sc{gnu} Readline
40 such as completion, line editing, and interactive history manipulation
41 in your own programs, this section is for you.
44 * Basic Behavior:: Using the default behavior of Readline.
45 * Custom Functions:: Adding your own functions to Readline.
46 * Readline Variables:: Variables accessible to custom
48 * Readline Convenience Functions:: Functions which Readline supplies to
49 aid in writing your own custom
51 * Readline Signal Handling:: How Readline behaves when it receives signals.
52 * Custom Completers:: Supplanting or supplementing Readline's
57 @section Basic Behavior
59 Many programs provide a command line interface, such as @code{mail},
60 @code{ftp}, and @code{sh}. For such programs, the default behaviour of
61 Readline is sufficient. This section describes how to use Readline in
62 the simplest way possible, perhaps to replace calls in your code to
63 @code{gets()} or @code{fgets()}.
66 @cindex readline, function
68 The function @code{readline()} prints a prompt @var{prompt}
69 and then reads and returns a single line of text from the user.
70 If @var{prompt} is @code{NULL} or the empty string, no prompt is displayed.
71 The line @code{readline} returns is allocated with @code{malloc()};
72 the caller should @code{free()} the line when it has finished with it.
73 The declaration for @code{readline} in ANSI C is
76 @code{char *readline (const char *@var{prompt});}
82 @code{char *line = readline ("Enter a line: ");}
85 in order to read a line of text from the user.
86 The line returned has the final newline removed, so only the
89 If @code{readline} encounters an @code{EOF} while reading the line, and the
90 line is empty at that point, then @code{(char *)NULL} is returned.
91 Otherwise, the line is ended just as if a newline had been typed.
93 If you want the user to be able to get at the line later, (with
94 @key{C-p} for example), you must call @code{add_history()} to save the
95 line away in a @dfn{history} list of such lines.
98 @code{add_history (line)};
102 For full details on the GNU History Library, see the associated manual.
104 It is preferable to avoid saving empty lines on the history list, since
105 users rarely have a burning need to reuse a blank line. Here is
106 a function which usefully replaces the standard @code{gets()} library
107 function, and has the advantage of no static buffer to overflow:
110 /* A static variable for holding the line. */
111 static char *line_read = (char *)NULL;
113 /* Read a string, and return a pointer to it.
114 Returns NULL on EOF. */
118 /* If the buffer has already been allocated,
119 return the memory to the free pool. */
123 line_read = (char *)NULL;
126 /* Get a line from the user. */
127 line_read = readline ("");
129 /* If the line has any text in it,
130 save it on the history. */
131 if (line_read && *line_read)
132 add_history (line_read);
138 This function gives the user the default behaviour of @key{TAB}
139 completion: completion on file names. If you do not want Readline to
140 complete on filenames, you can change the binding of the @key{TAB} key
141 with @code{rl_bind_key()}.
144 @code{int rl_bind_key (int @var{key}, rl_command_func_t *@var{function});}
147 @code{rl_bind_key()} takes two arguments: @var{key} is the character that
148 you want to bind, and @var{function} is the address of the function to
149 call when @var{key} is pressed. Binding @key{TAB} to @code{rl_insert()}
150 makes @key{TAB} insert itself.
151 @code{rl_bind_key()} returns non-zero if @var{key} is not a valid
152 ASCII character code (between 0 and 255).
154 Thus, to disable the default @key{TAB} behavior, the following suffices:
156 @code{rl_bind_key ('\t', rl_insert);}
159 This code should be executed once at the start of your program; you
160 might write a function called @code{initialize_readline()} which
161 performs this and other desired initializations, such as installing
162 custom completers (@pxref{Custom Completers}).
164 @node Custom Functions
165 @section Custom Functions
167 Readline provides many functions for manipulating the text of
168 the line, but it isn't possible to anticipate the needs of all
169 programs. This section describes the various functions and variables
170 defined within the Readline library which allow a user program to add
171 customized functionality to Readline.
173 Before declaring any functions that customize Readline's behavior, or
174 using any functionality Readline provides in other code, an
175 application writer should include the file @code{<readline/readline.h>}
176 in any file that uses Readline's features. Since some of the definitions
177 in @code{readline.h} use the @code{stdio} library, the file
178 @code{<stdio.h>} should be included before @code{readline.h}.
180 @code{readline.h} defines a C preprocessor variable that should
181 be treated as an integer, @code{RL_READLINE_VERSION}, which may
182 be used to conditionally compile application code depending on
183 the installed Readline version. The value is a hexadecimal
184 encoding of the major and minor version numbers of the library,
185 of the form 0x@var{MMmm}. @var{MM} is the two-digit major
186 version number; @var{mm} is the two-digit minor version number.
187 For Readline 4.2, for example, the value of
188 @code{RL_READLINE_VERSION} would be @code{0x0402}.
191 * Readline Typedefs:: C declarations to make code readable.
192 * Function Writing:: Variables and calling conventions.
195 @node Readline Typedefs
196 @subsection Readline Typedefs
198 For readabilty, we declare a number of new object types, all pointers
201 The reason for declaring these new types is to make it easier to write
202 code describing pointers to C functions with appropriately prototyped
203 arguments and return values.
205 For instance, say we want to declare a variable @var{func} as a pointer
206 to a function which takes two @code{int} arguments and returns an
207 @code{int} (this is the type of all of the Readline bindable functions).
208 Instead of the classic C declaration
210 @code{int (*func)();}
213 or the ANSI-C style declaration
215 @code{int (*func)(int, int);}
220 @code{rl_command_func_t *func;}
222 The full list of function pointer types available is
225 @item typedef int rl_command_func_t (int, int);
227 @item typedef char *rl_compentry_func_t (const char *, int);
229 @item typedef char **rl_completion_func_t (const char *, int, int);
231 @item typedef char *rl_quote_func_t (char *, int, char *);
233 @item typedef char *rl_dequote_func_t (char *, int);
235 @item typedef int rl_compignore_func_t (char **);
237 @item typedef void rl_compdisp_func_t (char **, int, int);
239 @item typedef int rl_hook_func_t (void);
241 @item typedef int rl_getc_func_t (FILE *);
243 @item typedef int rl_linebuf_func_t (char *, int);
245 @item typedef int rl_intfunc_t (int);
246 @item #define rl_ivoidfunc_t rl_hook_func_t
247 @item typedef int rl_icpfunc_t (char *);
248 @item typedef int rl_icppfunc_t (char **);
250 @item typedef void rl_voidfunc_t (void);
251 @item typedef void rl_vintfunc_t (int);
252 @item typedef void rl_vcpfunc_t (char *);
253 @item typedef void rl_vcppfunc_t (char **);
257 @node Function Writing
258 @subsection Writing a New Function
260 In order to write new functions for Readline, you need to know the
261 calling conventions for keyboard-invoked functions, and the names of the
262 variables that describe the current state of the line read so far.
264 The calling sequence for a command @code{foo} looks like
267 @code{int foo (int count, int key)}
271 where @var{count} is the numeric argument (or 1 if defaulted) and
272 @var{key} is the key that invoked this function.
274 It is completely up to the function as to what should be done with the
275 numeric argument. Some functions use it as a repeat count, some
276 as a flag, and others to choose alternate behavior (refreshing the current
277 line as opposed to refreshing the screen, for example). Some choose to
278 ignore it. In general, if a
279 function uses the numeric argument as a repeat count, it should be able
280 to do something useful with both negative and positive arguments.
281 At the very least, it should be aware that it can be passed a
284 A command function should return 0 if its action completes successfully,
285 and a non-zero value if some error occurs.
286 This is the convention obeyed by all of the builtin Readline bindable
289 @node Readline Variables
290 @section Readline Variables
292 These variables are available to function writers.
294 @deftypevar {char *} rl_line_buffer
295 This is the line gathered so far. You are welcome to modify the
296 contents of the line, but see @ref{Allowing Undoing}. The
297 function @code{rl_extend_line_buffer} is available to increase
298 the memory allocated to @code{rl_line_buffer}.
301 @deftypevar int rl_point
302 The offset of the current cursor position in @code{rl_line_buffer}
306 @deftypevar int rl_end
307 The number of characters present in @code{rl_line_buffer}. When
308 @code{rl_point} is at the end of the line, @code{rl_point} and
309 @code{rl_end} are equal.
312 @deftypevar int rl_mark
313 The @var{mark} (saved position) in the current line. If set, the mark
314 and point define a @emph{region}.
317 @deftypevar int rl_done
318 Setting this to a non-zero value causes Readline to return the current
322 @deftypevar int rl_num_chars_to_read
323 Setting this to a positive value before calling @code{readline()} causes
324 Readline to return after accepting that many characters, rather
325 than reading up to a character bound to @code{accept-line}.
328 @deftypevar int rl_pending_input
329 Setting this to a value makes it the next keystroke read. This is a
330 way to stuff a single character into the input stream.
333 @deftypevar int rl_dispatching
334 Set to a non-zero value if a function is being called from a key binding;
335 zero otherwise. Application functions can test this to discover whether
336 they were called directly or by Readline's dispatching mechanism.
339 @deftypevar int rl_erase_empty_line
340 Setting this to a non-zero value causes Readline to completely erase
341 the current line, including any prompt, any time a newline is typed as
342 the only character on an otherwise-empty line. The cursor is moved to
343 the beginning of the newly-blank line.
346 @deftypevar {char *} rl_prompt
347 The prompt Readline uses. This is set from the argument to
348 @code{readline()}, and should not be assigned to directly.
349 The @code{rl_set_prompt()} function (@pxref{Redisplay}) may
350 be used to modify the prompt string after calling @code{readline()}.
353 @deftypevar {char *} rl_display_prompt
354 The string displayed as the prompt. This is usually identical to
355 @var{rl_prompt}, but may be changed temporarily by functions that
356 use the prompt string as a message area, such as incremental search.
359 @deftypevar int rl_already_prompted
360 If an application wishes to display the prompt itself, rather than have
361 Readline do it the first time @code{readline()} is called, it should set
362 this variable to a non-zero value after displaying the prompt.
363 The prompt must also be passed as the argument to @code{readline()} so
364 the redisplay functions can update the display properly.
365 The calling application is responsible for managing the value; Readline
369 @deftypevar {const char *} rl_library_version
370 The version number of this revision of the library.
373 @deftypevar int rl_readline_version
374 An integer encoding the current version of the library. The encoding is
375 of the form 0x@var{MMmm}, where @var{MM} is the two-digit major version
376 number, and @var{mm} is the two-digit minor version number.
377 For example, for Readline-4.2, @code{rl_readline_version} would have the
381 @deftypevar {int} rl_gnu_readline_p
382 Always set to 1, denoting that this is @sc{gnu} readline rather than some
386 @deftypevar {const char *} rl_terminal_name
387 The terminal type, used for initialization. If not set by the application,
388 Readline sets this to the value of the @env{TERM} environment variable
389 the first time it is called.
392 @deftypevar {const char *} rl_readline_name
393 This variable is set to a unique name by each application using Readline.
394 The value allows conditional parsing of the inputrc file
395 (@pxref{Conditional Init Constructs}).
398 @deftypevar {FILE *} rl_instream
399 The stdio stream from which Readline reads input.
400 If @code{NULL}, Readline defaults to @var{stdin}.
403 @deftypevar {FILE *} rl_outstream
404 The stdio stream to which Readline performs output.
405 If @code{NULL}, Readline defaults to @var{stdout}.
408 @deftypevar int rl_prefer_env_winsize
409 If non-zero, Readline gives values found in the @env{LINES} and
410 @env{COLUMNS} environment variables greater precedence than values fetched
411 from the kernel when computing the screen dimensions.
414 @deftypevar {rl_command_func_t *} rl_last_func
415 The address of the last command function Readline executed. May be used to
416 test whether or not a function is being executed twice in succession, for
420 @deftypevar {rl_hook_func_t *} rl_startup_hook
421 If non-zero, this is the address of a function to call just
422 before @code{readline} prints the first prompt.
425 @deftypevar {rl_hook_func_t *} rl_pre_input_hook
426 If non-zero, this is the address of a function to call after
427 the first prompt has been printed and just before @code{readline}
428 starts reading input characters.
431 @deftypevar {rl_hook_func_t *} rl_event_hook
432 If non-zero, this is the address of a function to call periodically
433 when Readline is waiting for terminal input.
434 By default, this will be called at most ten times a second if there
435 is no keyboard input.
438 @deftypevar {rl_getc_func_t *} rl_getc_function
439 If non-zero, Readline will call indirectly through this pointer
440 to get a character from the input stream. By default, it is set to
441 @code{rl_getc}, the default Readline character input function
442 (@pxref{Character Input}).
445 @deftypevar {rl_voidfunc_t *} rl_redisplay_function
446 If non-zero, Readline will call indirectly through this pointer
447 to update the display with the current contents of the editing buffer.
448 By default, it is set to @code{rl_redisplay}, the default Readline
449 redisplay function (@pxref{Redisplay}).
452 @deftypevar {rl_vintfunc_t *} rl_prep_term_function
453 If non-zero, Readline will call indirectly through this pointer
454 to initialize the terminal. The function takes a single argument, an
455 @code{int} flag that says whether or not to use eight-bit characters.
456 By default, this is set to @code{rl_prep_terminal}
457 (@pxref{Terminal Management}).
460 @deftypevar {rl_voidfunc_t *} rl_deprep_term_function
461 If non-zero, Readline will call indirectly through this pointer
462 to reset the terminal. This function should undo the effects of
463 @code{rl_prep_term_function}.
464 By default, this is set to @code{rl_deprep_terminal}
465 (@pxref{Terminal Management}).
468 @deftypevar {Keymap} rl_executing_keymap
469 This variable is set to the keymap (@pxref{Keymaps}) in which the
470 currently executing readline function was found.
473 @deftypevar {Keymap} rl_binding_keymap
474 This variable is set to the keymap (@pxref{Keymaps}) in which the
475 last key binding occurred.
478 @deftypevar {char *} rl_executing_macro
479 This variable is set to the text of any currently-executing macro.
482 @deftypevar {int} rl_readline_state
483 A variable with bit values that encapsulate the current Readline state.
484 A bit is set with the @code{RL_SETSTATE} macro, and unset with the
485 @code{RL_UNSETSTATE} macro. Use the @code{RL_ISSTATE} macro to test
486 whether a particular state bit is set. Current state bits include:
490 Readline has not yet been called, nor has it begun to intialize.
491 @item RL_STATE_INITIALIZING
492 Readline is initializing its internal data structures.
493 @item RL_STATE_INITIALIZED
494 Readline has completed its initialization.
495 @item RL_STATE_TERMPREPPED
496 Readline has modified the terminal modes to do its own input and redisplay.
497 @item RL_STATE_READCMD
498 Readline is reading a command from the keyboard.
499 @item RL_STATE_METANEXT
500 Readline is reading more input after reading the meta-prefix character.
501 @item RL_STATE_DISPATCHING
502 Readline is dispatching to a command.
503 @item RL_STATE_MOREINPUT
504 Readline is reading more input while executing an editing command.
505 @item RL_STATE_ISEARCH
506 Readline is performing an incremental history search.
507 @item RL_STATE_NSEARCH
508 Readline is performing a non-incremental history search.
509 @item RL_STATE_SEARCH
510 Readline is searching backward or forward through the history for a string.
511 @item RL_STATE_NUMERICARG
512 Readline is reading a numeric argument.
513 @item RL_STATE_MACROINPUT
514 Readline is currently getting its input from a previously-defined keyboard
516 @item RL_STATE_MACRODEF
517 Readline is currently reading characters defining a keyboard macro.
518 @item RL_STATE_OVERWRITE
519 Readline is in overwrite mode.
520 @item RL_STATE_COMPLETING
521 Readline is performing word completion.
522 @item RL_STATE_SIGHANDLER
523 Readline is currently executing the readline signal handler.
524 @item RL_STATE_UNDOING
525 Readline is performing an undo.
526 @item RL_STATE_INPUTPENDING
527 Readline has input pending due to a call to @code{rl_execute_next()}.
528 @item RL_STATE_TTYCSAVED
529 Readline has saved the values of the terminal's special characters.
530 @item RL_STATE_CALLBACK
531 Readline is currently using the alternate (callback) interface
532 (@pxref{Alternate Interface}).
533 @item RL_STATE_VIMOTION
534 Readline is reading the argument to a vi-mode "motion" command.
535 @item RL_STATE_MULTIKEY
536 Readline is reading a multiple-keystroke command.
537 @item RL_STATE_VICMDONCE
538 Readline has entered vi command (movement) mode at least one time during
539 the current call to @code{readline()}.
541 Readline has read a key sequence bound to @code{accept-line}
542 and is about to return the line to the caller.
547 @deftypevar {int} rl_explicit_arg
548 Set to a non-zero value if an explicit numeric argument was specified by
549 the user. Only valid in a bindable command function.
552 @deftypevar {int} rl_numeric_arg
553 Set to the value of any numeric argument explicitly specified by the user
554 before executing the current Readline function. Only valid in a bindable
558 @deftypevar {int} rl_editing_mode
559 Set to a value denoting Readline's current editing mode. A value of
560 @var{1} means Readline is currently in emacs mode; @var{0}
561 means that vi mode is active.
565 @node Readline Convenience Functions
566 @section Readline Convenience Functions
569 * Function Naming:: How to give a function you write a name.
570 * Keymaps:: Making keymaps.
571 * Binding Keys:: Changing Keymaps.
572 * Associating Function Names and Bindings:: Translate function names to
574 * Allowing Undoing:: How to make your functions undoable.
575 * Redisplay:: Functions to control line display.
576 * Modifying Text:: Functions to modify @code{rl_line_buffer}.
577 * Character Input:: Functions to read keyboard input.
578 * Terminal Management:: Functions to manage terminal settings.
579 * Utility Functions:: Generally useful functions and hooks.
580 * Miscellaneous Functions:: Functions that don't fall into any category.
581 * Alternate Interface:: Using Readline in a `callback' fashion.
582 * A Readline Example:: An example Readline function.
585 @node Function Naming
586 @subsection Naming a Function
588 The user can dynamically change the bindings of keys while using
589 Readline. This is done by representing the function with a descriptive
590 name. The user is able to type the descriptive name when referring to
591 the function. Thus, in an init file, one might find
594 Meta-Rubout: backward-kill-word
597 This binds the keystroke @key{Meta-Rubout} to the function
598 @emph{descriptively} named @code{backward-kill-word}. You, as the
599 programmer, should bind the functions you write to descriptive names as
600 well. Readline provides a function for doing that:
602 @deftypefun int rl_add_defun (const char *name, rl_command_func_t *function, int key)
603 Add @var{name} to the list of named functions. Make @var{function} be
604 the function that gets called. If @var{key} is not -1, then bind it to
605 @var{function} using @code{rl_bind_key()}.
608 Using this function alone is sufficient for most applications.
609 It is the recommended way to add a few functions to the default
610 functions that Readline has built in.
611 If you need to do something other than adding a function to Readline,
612 you may need to use the underlying functions described below.
615 @subsection Selecting a Keymap
617 Key bindings take place on a @dfn{keymap}. The keymap is the
618 association between the keys that the user types and the functions that
619 get run. You can make your own keymaps, copy existing keymaps, and tell
620 Readline which keymap to use.
622 @deftypefun Keymap rl_make_bare_keymap (void)
623 Returns a new, empty keymap. The space for the keymap is allocated with
624 @code{malloc()}; the caller should free it by calling
625 @code{rl_discard_keymap()} when done.
628 @deftypefun Keymap rl_copy_keymap (Keymap map)
629 Return a new keymap which is a copy of @var{map}.
632 @deftypefun Keymap rl_make_keymap (void)
633 Return a new keymap with the printing characters bound to rl_insert,
634 the lowercase Meta characters bound to run their equivalents, and
635 the Meta digits bound to produce numeric arguments.
638 @deftypefun void rl_discard_keymap (Keymap keymap)
639 Free the storage associated with @var{keymap}.
642 Readline has several internal keymaps. These functions allow you to
643 change which keymap is active.
645 @deftypefun Keymap rl_get_keymap (void)
646 Returns the currently active keymap.
649 @deftypefun void rl_set_keymap (Keymap keymap)
650 Makes @var{keymap} the currently active keymap.
653 @deftypefun Keymap rl_get_keymap_by_name (const char *name)
654 Return the keymap matching @var{name}. @var{name} is one which would
655 be supplied in a @code{set keymap} inputrc line (@pxref{Readline Init File}).
658 @deftypefun {char *} rl_get_keymap_name (Keymap keymap)
659 Return the name matching @var{keymap}. @var{name} is one which would
660 be supplied in a @code{set keymap} inputrc line (@pxref{Readline Init File}).
664 @subsection Binding Keys
666 Key sequences are associate with functions through the keymap.
667 Readline has several internal keymaps: @code{emacs_standard_keymap},
668 @code{emacs_meta_keymap}, @code{emacs_ctlx_keymap},
669 @code{vi_movement_keymap}, and @code{vi_insertion_keymap}.
670 @code{emacs_standard_keymap} is the default, and the examples in
671 this manual assume that.
673 Since @code{readline()} installs a set of default key bindings the first
674 time it is called, there is always the danger that a custom binding
675 installed before the first call to @code{readline()} will be overridden.
676 An alternate mechanism is to install custom key bindings in an
677 initialization function assigned to the @code{rl_startup_hook} variable
678 (@pxref{Readline Variables}).
680 These functions manage key bindings.
682 @deftypefun int rl_bind_key (int key, rl_command_func_t *function)
683 Binds @var{key} to @var{function} in the currently active keymap.
684 Returns non-zero in the case of an invalid @var{key}.
687 @deftypefun int rl_bind_key_in_map (int key, rl_command_func_t *function, Keymap map)
688 Bind @var{key} to @var{function} in @var{map}.
689 Returns non-zero in the case of an invalid @var{key}.
692 @deftypefun int rl_bind_key_if_unbound (int key, rl_command_func_t *function)
693 Binds @var{key} to @var{function} if it is not already bound in the
694 currently active keymap.
695 Returns non-zero in the case of an invalid @var{key} or if @var{key} is
699 @deftypefun int rl_bind_key_if_unbound_in_map (int key, rl_command_func_t *function, Keymap map)
700 Binds @var{key} to @var{function} if it is not already bound in @var{map}.
701 Returns non-zero in the case of an invalid @var{key} or if @var{key} is
705 @deftypefun int rl_unbind_key (int key)
706 Bind @var{key} to the null function in the currently active keymap.
707 Returns non-zero in case of error.
710 @deftypefun int rl_unbind_key_in_map (int key, Keymap map)
711 Bind @var{key} to the null function in @var{map}.
712 Returns non-zero in case of error.
715 @deftypefun int rl_unbind_function_in_map (rl_command_func_t *function, Keymap map)
716 Unbind all keys that execute @var{function} in @var{map}.
719 @deftypefun int rl_unbind_command_in_map (const char *command, Keymap map)
720 Unbind all keys that are bound to @var{command} in @var{map}.
723 @deftypefun int rl_bind_keyseq (const char *keyseq, rl_command_func_t *function)
724 Bind the key sequence represented by the string @var{keyseq} to the function
725 @var{function}, beginning in the current keymap.
726 This makes new keymaps as necessary.
727 The return value is non-zero if @var{keyseq} is invalid.
730 @deftypefun int rl_bind_keyseq_in_map (const char *keyseq, rl_command_func_t *function, Keymap map)
731 Bind the key sequence represented by the string @var{keyseq} to the function
732 @var{function}. This makes new keymaps as necessary.
733 Initial bindings are performed in @var{map}.
734 The return value is non-zero if @var{keyseq} is invalid.
737 @deftypefun int rl_set_key (const char *keyseq, rl_command_func_t *function, Keymap map)
738 Equivalent to @code{rl_bind_keyseq_in_map}.
741 @deftypefun int rl_bind_keyseq_if_unbound (const char *keyseq, rl_command_func_t *function)
742 Binds @var{keyseq} to @var{function} if it is not already bound in the
743 currently active keymap.
744 Returns non-zero in the case of an invalid @var{keyseq} or if @var{keyseq} is
748 @deftypefun int rl_bind_keyseq_if_unbound_in_map (const char *keyseq, rl_command_func_t *function, Keymap map)
749 Binds @var{keyseq} to @var{function} if it is not already bound in @var{map}.
750 Returns non-zero in the case of an invalid @var{keyseq} or if @var{keyseq} is
754 @deftypefun int rl_generic_bind (int type, const char *keyseq, char *data, Keymap map)
755 Bind the key sequence represented by the string @var{keyseq} to the arbitrary
756 pointer @var{data}. @var{type} says what kind of data is pointed to by
757 @var{data}; this can be a function (@code{ISFUNC}), a macro
758 (@code{ISMACR}), or a keymap (@code{ISKMAP}). This makes new keymaps as
759 necessary. The initial keymap in which to do bindings is @var{map}.
762 @deftypefun int rl_parse_and_bind (char *line)
763 Parse @var{line} as if it had been read from the @code{inputrc} file and
764 perform any key bindings and variable assignments found
765 (@pxref{Readline Init File}).
768 @deftypefun int rl_read_init_file (const char *filename)
769 Read keybindings and variable assignments from @var{filename}
770 (@pxref{Readline Init File}).
773 @node Associating Function Names and Bindings
774 @subsection Associating Function Names and Bindings
776 These functions allow you to find out what keys invoke named functions
777 and the functions invoked by a particular key sequence. You may also
778 associate a new function name with an arbitrary function.
780 @deftypefun {rl_command_func_t *} rl_named_function (const char *name)
781 Return the function with name @var{name}.
784 @deftypefun {rl_command_func_t *} rl_function_of_keyseq (const char *keyseq, Keymap map, int *type)
785 Return the function invoked by @var{keyseq} in keymap @var{map}.
786 If @var{map} is @code{NULL}, the current keymap is used. If @var{type} is
787 not @code{NULL}, the type of the object is returned in the @code{int} variable
788 it points to (one of @code{ISFUNC}, @code{ISKMAP}, or @code{ISMACR}).
791 @deftypefun {char **} rl_invoking_keyseqs (rl_command_func_t *function)
792 Return an array of strings representing the key sequences used to
793 invoke @var{function} in the current keymap.
796 @deftypefun {char **} rl_invoking_keyseqs_in_map (rl_command_func_t *function, Keymap map)
797 Return an array of strings representing the key sequences used to
798 invoke @var{function} in the keymap @var{map}.
801 @deftypefun void rl_function_dumper (int readable)
802 Print the readline function names and the key sequences currently
803 bound to them to @code{rl_outstream}. If @var{readable} is non-zero,
804 the list is formatted in such a way that it can be made part of an
805 @code{inputrc} file and re-read.
808 @deftypefun void rl_list_funmap_names (void)
809 Print the names of all bindable Readline functions to @code{rl_outstream}.
812 @deftypefun {const char **} rl_funmap_names (void)
813 Return a NULL terminated array of known function names. The array is
814 sorted. The array itself is allocated, but not the strings inside. You
815 should free the array, but not the pointers, using @code{free} or
816 @code{rl_free} when you are done.
819 @deftypefun int rl_add_funmap_entry (const char *name, rl_command_func_t *function)
820 Add @var{name} to the list of bindable Readline command names, and make
821 @var{function} the function to be called when @var{name} is invoked.
824 @node Allowing Undoing
825 @subsection Allowing Undoing
827 Supporting the undo command is a painless thing, and makes your
828 functions much more useful. It is certainly easy to try
829 something if you know you can undo it.
831 If your function simply inserts text once, or deletes text once, and
832 uses @code{rl_insert_text()} or @code{rl_delete_text()} to do it, then
833 undoing is already done for you automatically.
835 If you do multiple insertions or multiple deletions, or any combination
836 of these operations, you should group them together into one operation.
837 This is done with @code{rl_begin_undo_group()} and
838 @code{rl_end_undo_group()}.
840 The types of events that can be undone are:
843 enum undo_code @{ UNDO_DELETE, UNDO_INSERT, UNDO_BEGIN, UNDO_END @};
846 Notice that @code{UNDO_DELETE} means to insert some text, and
847 @code{UNDO_INSERT} means to delete some text. That is, the undo code
848 tells what to undo, not how to undo it. @code{UNDO_BEGIN} and
849 @code{UNDO_END} are tags added by @code{rl_begin_undo_group()} and
850 @code{rl_end_undo_group()}.
852 @deftypefun int rl_begin_undo_group (void)
853 Begins saving undo information in a group construct. The undo
854 information usually comes from calls to @code{rl_insert_text()} and
855 @code{rl_delete_text()}, but could be the result of calls to
856 @code{rl_add_undo()}.
859 @deftypefun int rl_end_undo_group (void)
860 Closes the current undo group started with @code{rl_begin_undo_group
861 ()}. There should be one call to @code{rl_end_undo_group()}
862 for each call to @code{rl_begin_undo_group()}.
865 @deftypefun void rl_add_undo (enum undo_code what, int start, int end, char *text)
866 Remember how to undo an event (according to @var{what}). The affected
867 text runs from @var{start} to @var{end}, and encompasses @var{text}.
870 @deftypefun void rl_free_undo_list (void)
871 Free the existing undo list.
874 @deftypefun int rl_do_undo (void)
875 Undo the first thing on the undo list. Returns @code{0} if there was
876 nothing to undo, non-zero if something was undone.
879 Finally, if you neither insert nor delete text, but directly modify the
880 existing text (e.g., change its case), call @code{rl_modifying()}
881 once, just before you modify the text. You must supply the indices of
882 the text range that you are going to modify.
884 @deftypefun int rl_modifying (int start, int end)
885 Tell Readline to save the text between @var{start} and @var{end} as a
886 single undo unit. It is assumed that you will subsequently modify
891 @subsection Redisplay
893 @deftypefun void rl_redisplay (void)
894 Change what's displayed on the screen to reflect the current contents
895 of @code{rl_line_buffer}.
898 @deftypefun int rl_forced_update_display (void)
899 Force the line to be updated and redisplayed, whether or not
900 Readline thinks the screen display is correct.
903 @deftypefun int rl_on_new_line (void)
904 Tell the update functions that we have moved onto a new (empty) line,
905 usually after ouputting a newline.
908 @deftypefun int rl_on_new_line_with_prompt (void)
909 Tell the update functions that we have moved onto a new line, with
910 @var{rl_prompt} already displayed.
911 This could be used by applications that want to output the prompt string
912 themselves, but still need Readline to know the prompt string length for
914 It should be used after setting @var{rl_already_prompted}.
917 @deftypefun int rl_reset_line_state (void)
918 Reset the display state to a clean state and redisplay the current line
919 starting on a new line.
922 @deftypefun int rl_crlf (void)
923 Move the cursor to the start of the next screen line.
926 @deftypefun int rl_show_char (int c)
927 Display character @var{c} on @code{rl_outstream}.
928 If Readline has not been set to display meta characters directly, this
929 will convert meta characters to a meta-prefixed key sequence.
930 This is intended for use by applications which wish to do their own
934 @deftypefun int rl_message (const char *, @dots{})
935 The arguments are a format string as would be supplied to @code{printf},
936 possibly containing conversion specifications such as @samp{%d}, and
937 any additional arguments necessary to satisfy the conversion specifications.
938 The resulting string is displayed in the @dfn{echo area}. The echo area
939 is also used to display numeric arguments and search strings.
940 You should call @code{rl_save_prompt} to save the prompt information
941 before calling this function.
944 @deftypefun int rl_clear_message (void)
945 Clear the message in the echo area. If the prompt was saved with a call to
946 @code{rl_save_prompt} before the last call to @code{rl_message},
947 call @code{rl_restore_prompt} before calling this function.
950 @deftypefun void rl_save_prompt (void)
951 Save the local Readline prompt display state in preparation for
952 displaying a new message in the message area with @code{rl_message()}.
955 @deftypefun void rl_restore_prompt (void)
956 Restore the local Readline prompt display state saved by the most
957 recent call to @code{rl_save_prompt}.
958 if @code{rl_save_prompt} was called to save the prompt before a call
959 to @code{rl_message}, this function should be called before the
960 corresponding call to @code{rl_clear_message}.
963 @deftypefun int rl_expand_prompt (char *prompt)
964 Expand any special character sequences in @var{prompt} and set up the
965 local Readline prompt redisplay variables.
966 This function is called by @code{readline()}. It may also be called to
967 expand the primary prompt if the @code{rl_on_new_line_with_prompt()}
968 function or @code{rl_already_prompted} variable is used.
969 It returns the number of visible characters on the last line of the
970 (possibly multi-line) prompt.
971 Applications may indicate that the prompt contains characters that take
972 up no physical screen space when displayed by bracketing a sequence of
973 such characters with the special markers @code{RL_PROMPT_START_IGNORE}
974 and @code{RL_PROMPT_END_IGNORE} (declared in @file{readline.h}. This may
975 be used to embed terminal-specific escape sequences in prompts.
978 @deftypefun int rl_set_prompt (const char *prompt)
979 Make Readline use @var{prompt} for subsequent redisplay. This calls
980 @code{rl_expand_prompt()} to expand the prompt and sets @code{rl_prompt}
985 @subsection Modifying Text
987 @deftypefun int rl_insert_text (const char *text)
988 Insert @var{text} into the line at the current cursor position.
989 Returns the number of characters inserted.
992 @deftypefun int rl_delete_text (int start, int end)
993 Delete the text between @var{start} and @var{end} in the current line.
994 Returns the number of characters deleted.
997 @deftypefun {char *} rl_copy_text (int start, int end)
998 Return a copy of the text between @var{start} and @var{end} in
1002 @deftypefun int rl_kill_text (int start, int end)
1003 Copy the text between @var{start} and @var{end} in the current line
1004 to the kill ring, appending or prepending to the last kill if the
1005 last command was a kill command. The text is deleted.
1006 If @var{start} is less than @var{end},
1007 the text is appended, otherwise prepended. If the last command was
1008 not a kill, a new kill ring slot is used.
1011 @deftypefun int rl_push_macro_input (char *macro)
1012 Cause @var{macro} to be inserted into the line, as if it had been invoked
1013 by a key bound to a macro. Not especially useful; use
1014 @code{rl_insert_text()} instead.
1017 @node Character Input
1018 @subsection Character Input
1020 @deftypefun int rl_read_key (void)
1021 Return the next character available from Readline's current input stream.
1022 This handles input inserted into
1023 the input stream via @var{rl_pending_input} (@pxref{Readline Variables})
1024 and @code{rl_stuff_char()}, macros, and characters read from the keyboard.
1025 While waiting for input, this function will call any function assigned to
1026 the @code{rl_event_hook} variable.
1029 @deftypefun int rl_getc (FILE *stream)
1030 Return the next character available from @var{stream}, which is assumed to
1034 @deftypefun int rl_stuff_char (int c)
1035 Insert @var{c} into the Readline input stream. It will be "read"
1036 before Readline attempts to read characters from the terminal with
1037 @code{rl_read_key()}. Up to 512 characters may be pushed back.
1038 @code{rl_stuff_char} returns 1 if the character was successfully inserted;
1042 @deftypefun int rl_execute_next (int c)
1043 Make @var{c} be the next command to be executed when @code{rl_read_key()}
1044 is called. This sets @var{rl_pending_input}.
1047 @deftypefun int rl_clear_pending_input (void)
1048 Unset @var{rl_pending_input}, effectively negating the effect of any
1049 previous call to @code{rl_execute_next()}. This works only if the
1050 pending input has not already been read with @code{rl_read_key()}.
1053 @deftypefun int rl_set_keyboard_input_timeout (int u)
1054 While waiting for keyboard input in @code{rl_read_key()}, Readline will
1055 wait for @var{u} microseconds for input before calling any function
1056 assigned to @code{rl_event_hook}. @var{u} must be greater than or equal
1057 to zero (a zero-length timeout is equivalent to a poll).
1058 The default waiting period is one-tenth of a second.
1059 Returns the old timeout value.
1062 @node Terminal Management
1063 @subsection Terminal Management
1065 @deftypefun void rl_prep_terminal (int meta_flag)
1066 Modify the terminal settings for Readline's use, so @code{readline()}
1067 can read a single character at a time from the keyboard.
1068 The @var{meta_flag} argument should be non-zero if Readline should
1069 read eight-bit input.
1072 @deftypefun void rl_deprep_terminal (void)
1073 Undo the effects of @code{rl_prep_terminal()}, leaving the terminal in
1074 the state in which it was before the most recent call to
1075 @code{rl_prep_terminal()}.
1078 @deftypefun void rl_tty_set_default_bindings (Keymap kmap)
1079 Read the operating system's terminal editing characters (as would be
1080 displayed by @code{stty}) to their Readline equivalents.
1081 The bindings are performed in @var{kmap}.
1084 @deftypefun void rl_tty_unset_default_bindings (Keymap kmap)
1085 Reset the bindings manipulated by @code{rl_tty_set_default_bindings} so
1086 that the terminal editing characters are bound to @code{rl_insert}.
1087 The bindings are performed in @var{kmap}.
1090 @deftypefun int rl_reset_terminal (const char *terminal_name)
1091 Reinitialize Readline's idea of the terminal settings using
1092 @var{terminal_name} as the terminal type (e.g., @code{vt100}).
1093 If @var{terminal_name} is @code{NULL}, the value of the @code{TERM}
1094 environment variable is used.
1097 @node Utility Functions
1098 @subsection Utility Functions
1100 @deftypefun int rl_save_state (struct readline_state *sp)
1101 Save a snapshot of Readline's internal state to @var{sp}.
1102 The contents of the @var{readline_state} structure are documented
1103 in @file{readline.h}.
1104 The caller is responsible for allocating the structure.
1107 @deftypefun int rl_restore_state (struct readline_state *sp)
1108 Restore Readline's internal state to that stored in @var{sp}, which must
1109 have been saved by a call to @code{rl_save_state}.
1110 The contents of the @var{readline_state} structure are documented
1111 in @file{readline.h}.
1112 The caller is responsible for freeing the structure.
1115 @deftypefun void rl_free (void *mem)
1116 Deallocate the memory pointed to by @var{mem}. @var{mem} must have been
1117 allocated by @code{malloc}.
1120 @deftypefun void rl_replace_line (const char *text, int clear_undo)
1121 Replace the contents of @code{rl_line_buffer} with @var{text}.
1122 The point and mark are preserved, if possible.
1123 If @var{clear_undo} is non-zero, the undo list associated with the
1124 current line is cleared.
1127 @deftypefun void rl_extend_line_buffer (int len)
1128 Ensure that @code{rl_line_buffer} has enough space to hold @var{len}
1129 characters, possibly reallocating it if necessary.
1132 @deftypefun int rl_initialize (void)
1133 Initialize or re-initialize Readline's internal state.
1134 It's not strictly necessary to call this; @code{readline()} calls it before
1138 @deftypefun int rl_ding (void)
1139 Ring the terminal bell, obeying the setting of @code{bell-style}.
1142 @deftypefun int rl_alphabetic (int c)
1143 Return 1 if @var{c} is an alphabetic character.
1146 @deftypefun void rl_display_match_list (char **matches, int len, int max)
1147 A convenience function for displaying a list of strings in
1148 columnar format on Readline's output stream. @code{matches} is the list
1149 of strings, in argv format, such as a list of completion matches.
1150 @code{len} is the number of strings in @code{matches}, and @code{max}
1151 is the length of the longest string in @code{matches}. This function uses
1152 the setting of @code{print-completions-horizontally} to select how the
1153 matches are displayed (@pxref{Readline Init File Syntax}).
1156 The following are implemented as macros, defined in @code{chardefs.h}.
1157 Applications should refrain from using them.
1159 @deftypefun int _rl_uppercase_p (int c)
1160 Return 1 if @var{c} is an uppercase alphabetic character.
1163 @deftypefun int _rl_lowercase_p (int c)
1164 Return 1 if @var{c} is a lowercase alphabetic character.
1167 @deftypefun int _rl_digit_p (int c)
1168 Return 1 if @var{c} is a numeric character.
1171 @deftypefun int _rl_to_upper (int c)
1172 If @var{c} is a lowercase alphabetic character, return the corresponding
1173 uppercase character.
1176 @deftypefun int _rl_to_lower (int c)
1177 If @var{c} is an uppercase alphabetic character, return the corresponding
1178 lowercase character.
1181 @deftypefun int _rl_digit_value (int c)
1182 If @var{c} is a number, return the value it represents.
1185 @node Miscellaneous Functions
1186 @subsection Miscellaneous Functions
1188 @deftypefun int rl_macro_bind (const char *keyseq, const char *macro, Keymap map)
1189 Bind the key sequence @var{keyseq} to invoke the macro @var{macro}.
1190 The binding is performed in @var{map}. When @var{keyseq} is invoked, the
1191 @var{macro} will be inserted into the line. This function is deprecated;
1192 use @code{rl_generic_bind()} instead.
1195 @deftypefun void rl_macro_dumper (int readable)
1196 Print the key sequences bound to macros and their values, using
1197 the current keymap, to @code{rl_outstream}.
1198 If @var{readable} is non-zero, the list is formatted in such a way
1199 that it can be made part of an @code{inputrc} file and re-read.
1202 @deftypefun int rl_variable_bind (const char *variable, const char *value)
1203 Make the Readline variable @var{variable} have @var{value}.
1204 This behaves as if the readline command
1205 @samp{set @var{variable} @var{value}} had been executed in an @code{inputrc}
1206 file (@pxref{Readline Init File Syntax}).
1209 @deftypefun {char *} rl_variable_value (const char *variable)
1210 Return a string representing the value of the Readline variable @var{variable}.
1211 For boolean variables, this string is either @samp{on} or @samp{off}.
1214 @deftypefun void rl_variable_dumper (int readable)
1215 Print the readline variable names and their current values
1216 to @code{rl_outstream}.
1217 If @var{readable} is non-zero, the list is formatted in such a way
1218 that it can be made part of an @code{inputrc} file and re-read.
1221 @deftypefun int rl_set_paren_blink_timeout (int u)
1222 Set the time interval (in microseconds) that Readline waits when showing
1223 a balancing character when @code{blink-matching-paren} has been enabled.
1226 @deftypefun {char *} rl_get_termcap (const char *cap)
1227 Retrieve the string value of the termcap capability @var{cap}.
1228 Readline fetches the termcap entry for the current terminal name and
1229 uses those capabilities to move around the screen line and perform other
1230 terminal-specific operations, like erasing a line. Readline does not
1231 use all of a terminal's capabilities, and this function will return
1232 values for only those capabilities Readline uses.
1235 @node Alternate Interface
1236 @subsection Alternate Interface
1238 An alternate interface is available to plain @code{readline()}. Some
1239 applications need to interleave keyboard I/O with file, device, or
1240 window system I/O, typically by using a main loop to @code{select()}
1241 on various file descriptors. To accomodate this need, readline can
1242 also be invoked as a `callback' function from an event loop. There
1243 are functions available to make this easy.
1245 @deftypefun void rl_callback_handler_install (const char *prompt, rl_vcpfunc_t *lhandler)
1246 Set up the terminal for readline I/O and display the initial
1247 expanded value of @var{prompt}. Save the value of @var{lhandler} to
1248 use as a function to call when a complete line of input has been entered.
1249 The function takes the text of the line as an argument.
1252 @deftypefun void rl_callback_read_char (void)
1253 Whenever an application determines that keyboard input is available, it
1254 should call @code{rl_callback_read_char()}, which will read the next
1255 character from the current input source.
1256 If that character completes the line, @code{rl_callback_read_char} will
1257 invoke the @var{lhandler} function saved by @code{rl_callback_handler_install}
1258 to process the line.
1259 Before calling the @var{lhandler} function, the terminal settings are
1260 reset to the values they had before calling
1261 @code{rl_callback_handler_install}.
1262 If the @var{lhandler} function returns,
1263 the terminal settings are modified for Readline's use again.
1264 @code{EOF} is indicated by calling @var{lhandler} with a
1268 @deftypefun void rl_callback_handler_remove (void)
1269 Restore the terminal to its initial state and remove the line handler.
1270 This may be called from within a callback as well as independently.
1271 If the @var{lhandler} installed by @code{rl_callback_handler_install}
1272 does not exit the program, either this function or the function referred
1273 to by the value of @code{rl_deprep_term_function} should be called before
1274 the program exits to reset the terminal settings.
1277 @node A Readline Example
1278 @subsection A Readline Example
1280 Here is a function which changes lowercase characters to their uppercase
1281 equivalents, and uppercase characters to lowercase. If
1282 this function was bound to @samp{M-c}, then typing @samp{M-c} would
1283 change the case of the character under point. Typing @samp{M-1 0 M-c}
1284 would change the case of the following 10 characters, leaving the cursor on
1285 the last character changed.
1288 /* Invert the case of the COUNT following characters. */
1290 invert_case_line (count, key)
1293 register int start, end, i;
1297 if (rl_point >= rl_end)
1308 /* Find the end of the range to modify. */
1309 end = start + (count * direction);
1311 /* Force it to be within range. */
1327 /* Tell readline that we are modifying the line,
1328 so it will save the undo information. */
1329 rl_modifying (start, end);
1331 for (i = start; i != end; i++)
1333 if (_rl_uppercase_p (rl_line_buffer[i]))
1334 rl_line_buffer[i] = _rl_to_lower (rl_line_buffer[i]);
1335 else if (_rl_lowercase_p (rl_line_buffer[i]))
1336 rl_line_buffer[i] = _rl_to_upper (rl_line_buffer[i]);
1338 /* Move point to on top of the last character changed. */
1339 rl_point = (direction == 1) ? end - 1 : start;
1344 @node Readline Signal Handling
1345 @section Readline Signal Handling
1347 Signals are asynchronous events sent to a process by the Unix kernel,
1348 sometimes on behalf of another process. They are intended to indicate
1349 exceptional events, like a user pressing the interrupt key on his terminal,
1350 or a network connection being broken. There is a class of signals that can
1351 be sent to the process currently reading input from the keyboard. Since
1352 Readline changes the terminal attributes when it is called, it needs to
1353 perform special processing when such a signal is received in order to
1354 restore the terminal to a sane state, or provide application writers with
1355 functions to do so manually.
1357 Readline contains an internal signal handler that is installed for a
1358 number of signals (@code{SIGINT}, @code{SIGQUIT}, @code{SIGTERM},
1359 @code{SIGALRM}, @code{SIGTSTP}, @code{SIGTTIN}, and @code{SIGTTOU}).
1360 When one of these signals is received, the signal handler
1361 will reset the terminal attributes to those that were in effect before
1362 @code{readline()} was called, reset the signal handling to what it was
1363 before @code{readline()} was called, and resend the signal to the calling
1365 If and when the calling application's signal handler returns, Readline
1366 will reinitialize the terminal and continue to accept input.
1367 When a @code{SIGINT} is received, the Readline signal handler performs
1368 some additional work, which will cause any partially-entered line to be
1369 aborted (see the description of @code{rl_free_line_state()} below).
1371 There is an additional Readline signal handler, for @code{SIGWINCH}, which
1372 the kernel sends to a process whenever the terminal's size changes (for
1373 example, if a user resizes an @code{xterm}). The Readline @code{SIGWINCH}
1374 handler updates Readline's internal screen size information, and then calls
1375 any @code{SIGWINCH} signal handler the calling application has installed.
1376 Readline calls the application's @code{SIGWINCH} signal handler without
1377 resetting the terminal to its original state. If the application's signal
1378 handler does more than update its idea of the terminal size and return (for
1379 example, a @code{longjmp} back to a main processing loop), it @emph{must}
1380 call @code{rl_cleanup_after_signal()} (described below), to restore the
1383 Readline provides two variables that allow application writers to
1384 control whether or not it will catch certain signals and act on them
1385 when they are received. It is important that applications change the
1386 values of these variables only when calling @code{readline()}, not in
1387 a signal handler, so Readline's internal signal state is not corrupted.
1389 @deftypevar int rl_catch_signals
1390 If this variable is non-zero, Readline will install signal handlers for
1391 @code{SIGINT}, @code{SIGQUIT}, @code{SIGTERM}, @code{SIGALRM},
1392 @code{SIGTSTP}, @code{SIGTTIN}, and @code{SIGTTOU}.
1394 The default value of @code{rl_catch_signals} is 1.
1397 @deftypevar int rl_catch_sigwinch
1398 If this variable is non-zero, Readline will install a signal handler for
1401 The default value of @code{rl_catch_sigwinch} is 1.
1404 If an application does not wish to have Readline catch any signals, or
1405 to handle signals other than those Readline catches (@code{SIGHUP},
1407 Readline provides convenience functions to do the necessary terminal
1408 and internal state cleanup upon receipt of a signal.
1410 @deftypefun void rl_cleanup_after_signal (void)
1411 This function will reset the state of the terminal to what it was before
1412 @code{readline()} was called, and remove the Readline signal handlers for
1413 all signals, depending on the values of @code{rl_catch_signals} and
1414 @code{rl_catch_sigwinch}.
1417 @deftypefun void rl_free_line_state (void)
1418 This will free any partial state associated with the current input line
1419 (undo information, any partial history entry, any partially-entered
1420 keyboard macro, and any partially-entered numeric argument). This
1421 should be called before @code{rl_cleanup_after_signal()}. The
1422 Readline signal handler for @code{SIGINT} calls this to abort the
1426 @deftypefun void rl_reset_after_signal (void)
1427 This will reinitialize the terminal and reinstall any Readline signal
1428 handlers, depending on the values of @code{rl_catch_signals} and
1429 @code{rl_catch_sigwinch}.
1432 If an application does not wish Readline to catch @code{SIGWINCH}, it may
1433 call @code{rl_resize_terminal()} or @code{rl_set_screen_size()} to force
1434 Readline to update its idea of the terminal size when a @code{SIGWINCH}
1437 @deftypefun void rl_echo_signal_char (int sig)
1438 If an application wishes to install its own signal handlers, but still
1439 have readline display characters that generate signals, calling this
1440 function with @var{sig} set to @code{SIGINT}, @code{SIGQUIT}, or
1441 @code{SIGTSTP} will display the character generating that signal.
1444 @deftypefun void rl_resize_terminal (void)
1445 Update Readline's internal screen size by reading values from the kernel.
1448 @deftypefun void rl_set_screen_size (int rows, int cols)
1449 Set Readline's idea of the terminal size to @var{rows} rows and
1450 @var{cols} columns. If either @var{rows} or @var{columns} is less than
1451 or equal to 0, Readline's idea of that terminal dimension is unchanged.
1454 If an application does not want to install a @code{SIGWINCH} handler, but
1455 is still interested in the screen dimensions, Readline's idea of the screen
1456 size may be queried.
1458 @deftypefun void rl_get_screen_size (int *rows, int *cols)
1459 Return Readline's idea of the terminal's size in the
1460 variables pointed to by the arguments.
1463 @deftypefun void rl_reset_screen_size (void)
1464 Cause Readline to reobtain the screen size and recalculate its dimensions.
1467 The following functions install and remove Readline's signal handlers.
1469 @deftypefun int rl_set_signals (void)
1470 Install Readline's signal handler for @code{SIGINT}, @code{SIGQUIT},
1471 @code{SIGTERM}, @code{SIGALRM}, @code{SIGTSTP}, @code{SIGTTIN},
1472 @code{SIGTTOU}, and @code{SIGWINCH}, depending on the values of
1473 @code{rl_catch_signals} and @code{rl_catch_sigwinch}.
1476 @deftypefun int rl_clear_signals (void)
1477 Remove all of the Readline signal handlers installed by
1478 @code{rl_set_signals()}.
1481 @node Custom Completers
1482 @section Custom Completers
1483 @cindex application-specific completion functions
1485 Typically, a program that reads commands from the user has a way of
1486 disambiguating commands and data. If your program is one of these, then
1487 it can provide completion for commands, data, or both.
1488 The following sections describe how your program and Readline
1489 cooperate to provide this service.
1492 * How Completing Works:: The logic used to do completion.
1493 * Completion Functions:: Functions provided by Readline.
1494 * Completion Variables:: Variables which control completion.
1495 * A Short Completion Example:: An example of writing completer subroutines.
1498 @node How Completing Works
1499 @subsection How Completing Works
1501 In order to complete some text, the full list of possible completions
1502 must be available. That is, it is not possible to accurately
1503 expand a partial word without knowing all of the possible words
1504 which make sense in that context. The Readline library provides
1505 the user interface to completion, and two of the most common
1506 completion functions: filename and username. For completing other types
1507 of text, you must write your own completion function. This section
1508 describes exactly what such functions must do, and provides an example.
1510 There are three major functions used to perform completion:
1514 The user-interface function @code{rl_complete()}. This function is
1515 called with the same arguments as other bindable Readline functions:
1516 @var{count} and @var{invoking_key}.
1517 It isolates the word to be completed and calls
1518 @code{rl_completion_matches()} to generate a list of possible completions.
1519 It then either lists the possible completions, inserts the possible
1520 completions, or actually performs the
1521 completion, depending on which behavior is desired.
1524 The internal function @code{rl_completion_matches()} uses an
1525 application-supplied @dfn{generator} function to generate the list of
1526 possible matches, and then returns the array of these matches.
1527 The caller should place the address of its generator function in
1528 @code{rl_completion_entry_function}.
1531 The generator function is called repeatedly from
1532 @code{rl_completion_matches()}, returning a string each time. The
1533 arguments to the generator function are @var{text} and @var{state}.
1534 @var{text} is the partial word to be completed. @var{state} is zero the
1535 first time the function is called, allowing the generator to perform
1536 any necessary initialization, and a positive non-zero integer for
1537 each subsequent call. The generator function returns
1538 @code{(char *)NULL} to inform @code{rl_completion_matches()} that there are
1539 no more possibilities left. Usually the generator function computes the
1540 list of possible completions when @var{state} is zero, and returns them
1541 one at a time on subsequent calls. Each string the generator function
1542 returns as a match must be allocated with @code{malloc()}; Readline
1543 frees the strings when it has finished with them.
1544 Such a generator function is referred to as an
1545 @dfn{application-specific completion function}.
1549 @deftypefun int rl_complete (int ignore, int invoking_key)
1550 Complete the word at or before point. You have supplied the function
1551 that does the initial simple matching selection algorithm (see
1552 @code{rl_completion_matches()}). The default is to do filename completion.
1555 @deftypevar {rl_compentry_func_t *} rl_completion_entry_function
1556 This is a pointer to the generator function for
1557 @code{rl_completion_matches()}.
1558 If the value of @code{rl_completion_entry_function} is
1559 @code{NULL} then the default filename generator
1560 function, @code{rl_filename_completion_function()}, is used.
1561 An @dfn{application-specific completion function} is a function whose
1562 address is assigned to @code{rl_completion_entry_function} and whose
1563 return values are used to generate possible completions.
1566 @node Completion Functions
1567 @subsection Completion Functions
1569 Here is the complete list of callable completion functions present in
1572 @deftypefun int rl_complete_internal (int what_to_do)
1573 Complete the word at or before point. @var{what_to_do} says what to do
1574 with the completion. A value of @samp{?} means list the possible
1575 completions. @samp{TAB} means do standard completion. @samp{*} means
1576 insert all of the possible completions. @samp{!} means to display
1577 all of the possible completions, if there is more than one, as well as
1578 performing partial completion. @samp{@@} is similar to @samp{!}, but
1579 possible completions are not listed if the possible completions share
1583 @deftypefun int rl_complete (int ignore, int invoking_key)
1584 Complete the word at or before point. You have supplied the function
1585 that does the initial simple matching selection algorithm (see
1586 @code{rl_completion_matches()} and @code{rl_completion_entry_function}).
1587 The default is to do filename
1588 completion. This calls @code{rl_complete_internal()} with an
1589 argument depending on @var{invoking_key}.
1592 @deftypefun int rl_possible_completions (int count, int invoking_key)
1593 List the possible completions. See description of @code{rl_complete
1594 ()}. This calls @code{rl_complete_internal()} with an argument of
1598 @deftypefun int rl_insert_completions (int count, int invoking_key)
1599 Insert the list of possible completions into the line, deleting the
1600 partially-completed word. See description of @code{rl_complete()}.
1601 This calls @code{rl_complete_internal()} with an argument of @samp{*}.
1604 @deftypefun int rl_completion_mode (rl_command_func_t *cfunc)
1605 Returns the apppriate value to pass to @code{rl_complete_internal()}
1606 depending on whether @var{cfunc} was called twice in succession and
1607 the values of the @code{show-all-if-ambiguous} and
1608 @code{show-all-if-unmodified} variables.
1609 Application-specific completion functions may use this function to present
1610 the same interface as @code{rl_complete()}.
1613 @deftypefun {char **} rl_completion_matches (const char *text, rl_compentry_func_t *entry_func)
1614 Returns an array of strings which is a list of completions for
1615 @var{text}. If there are no completions, returns @code{NULL}.
1616 The first entry in the returned array is the substitution for @var{text}.
1617 The remaining entries are the possible completions. The array is
1618 terminated with a @code{NULL} pointer.
1620 @var{entry_func} is a function of two args, and returns a
1621 @code{char *}. The first argument is @var{text}. The second is a
1622 state argument; it is zero on the first call, and non-zero on subsequent
1623 calls. @var{entry_func} returns a @code{NULL} pointer to the caller
1624 when there are no more matches.
1627 @deftypefun {char *} rl_filename_completion_function (const char *text, int state)
1628 A generator function for filename completion in the general case.
1629 @var{text} is a partial filename.
1630 The Bash source is a useful reference for writing application-specific
1631 completion functions (the Bash completion functions call this and other
1632 Readline functions).
1635 @deftypefun {char *} rl_username_completion_function (const char *text, int state)
1636 A completion generator for usernames. @var{text} contains a partial
1637 username preceded by a random character (usually @samp{~}). As with all
1638 completion generators, @var{state} is zero on the first call and non-zero
1639 for subsequent calls.
1642 @node Completion Variables
1643 @subsection Completion Variables
1645 @deftypevar {rl_compentry_func_t *} rl_completion_entry_function
1646 A pointer to the generator function for @code{rl_completion_matches()}.
1647 @code{NULL} means to use @code{rl_filename_completion_function()},
1648 the default filename completer.
1651 @deftypevar {rl_completion_func_t *} rl_attempted_completion_function
1652 A pointer to an alternative function to create matches.
1653 The function is called with @var{text}, @var{start}, and @var{end}.
1654 @var{start} and @var{end} are indices in @code{rl_line_buffer} defining
1655 the boundaries of @var{text}, which is a character string.
1656 If this function exists and returns @code{NULL}, or if this variable is
1657 set to @code{NULL}, then @code{rl_complete()} will call the value of
1658 @code{rl_completion_entry_function} to generate matches, otherwise the
1659 array of strings returned will be used.
1660 If this function sets the @code{rl_attempted_completion_over}
1661 variable to a non-zero value, Readline will not perform its default
1662 completion even if this function returns no matches.
1665 @deftypevar {rl_quote_func_t *} rl_filename_quoting_function
1666 A pointer to a function that will quote a filename in an
1667 application-specific fashion. This is called if filename completion is being
1668 attempted and one of the characters in @code{rl_filename_quote_characters}
1669 appears in a completed filename. The function is called with
1670 @var{text}, @var{match_type}, and @var{quote_pointer}. The @var{text}
1671 is the filename to be quoted. The @var{match_type} is either
1672 @code{SINGLE_MATCH}, if there is only one completion match, or
1673 @code{MULT_MATCH}. Some functions use this to decide whether or not to
1674 insert a closing quote character. The @var{quote_pointer} is a pointer
1675 to any opening quote character the user typed. Some functions choose
1676 to reset this character.
1679 @deftypevar {rl_dequote_func_t *} rl_filename_dequoting_function
1680 A pointer to a function that will remove application-specific quoting
1681 characters from a filename before completion is attempted, so those
1682 characters do not interfere with matching the text against names in
1683 the filesystem. It is called with @var{text}, the text of the word
1684 to be dequoted, and @var{quote_char}, which is the quoting character
1685 that delimits the filename (usually @samp{'} or @samp{"}). If
1686 @var{quote_char} is zero, the filename was not in an embedded string.
1689 @deftypevar {rl_linebuf_func_t *} rl_char_is_quoted_p
1690 A pointer to a function to call that determines whether or not a specific
1691 character in the line buffer is quoted, according to whatever quoting
1692 mechanism the program calling Readline uses. The function is called with
1693 two arguments: @var{text}, the text of the line, and @var{index}, the
1694 index of the character in the line. It is used to decide whether a
1695 character found in @code{rl_completer_word_break_characters} should be
1696 used to break words for the completer.
1699 @deftypevar {rl_compignore_func_t *} rl_ignore_some_completions_function
1700 This function, if defined, is called by the completer when real filename
1701 completion is done, after all the matching names have been generated.
1702 It is passed a @code{NULL} terminated array of matches.
1703 The first element (@code{matches[0]}) is the
1704 maximal substring common to all matches. This function can
1705 re-arrange the list of matches as required, but each element deleted
1706 from the array must be freed.
1709 @deftypevar {rl_icppfunc_t *} rl_directory_completion_hook
1710 This function, if defined, is allowed to modify the directory portion
1711 of filenames Readline completes. It is called with the address of a
1712 string (the current directory name) as an argument, and may modify that string.
1713 If the string is replaced with a new string, the old value should be freed.
1714 Any modified directory name should have a trailing slash.
1715 The modified value will be displayed as part of the completion, replacing
1716 the directory portion of the pathname the user typed.
1717 It returns an integer that should be non-zero if the function modifies
1718 its directory argument.
1719 It could be used to expand symbolic links or shell variables in pathnames.
1720 At the least, even if no other expansion is performed, this function should
1721 remove any quote characters from the directory name, because its result will
1722 be passed directly to @code{opendir()}.
1725 @deftypevar {rl_compdisp_func_t *} rl_completion_display_matches_hook
1726 If non-zero, then this is the address of a function to call when
1727 completing a word would normally display the list of possible matches.
1728 This function is called in lieu of Readline displaying the list.
1729 It takes three arguments:
1730 (@code{char **}@var{matches}, @code{int} @var{num_matches}, @code{int} @var{max_length})
1731 where @var{matches} is the array of matching strings,
1732 @var{num_matches} is the number of strings in that array, and
1733 @var{max_length} is the length of the longest string in that array.
1734 Readline provides a convenience function, @code{rl_display_match_list},
1735 that takes care of doing the display to Readline's output stream. That
1736 function may be called from this hook.
1739 @deftypevar {const char *} rl_basic_word_break_characters
1740 The basic list of characters that signal a break between words for the
1741 completer routine. The default value of this variable is the characters
1742 which break words for completion in Bash:
1743 @code{" \t\n\"\\'`@@$><=;|&@{("}.
1746 @deftypevar {const char *} rl_basic_quote_characters
1747 A list of quote characters which can cause a word break.
1750 @deftypevar {const char *} rl_completer_word_break_characters
1751 The list of characters that signal a break between words for
1752 @code{rl_complete_internal()}. The default list is the value of
1753 @code{rl_basic_word_break_characters}.
1756 @deftypevar {rl_cpvfunc_t *} rl_completion_word_break_hook
1757 If non-zero, this is the address of a function to call when Readline is
1758 deciding where to separate words for word completion. It should return
1759 a character string like @code{rl_completer_word_break_characters} to be
1760 used to perform the current completion. The function may choose to set
1761 @code{rl_completer_word_break_characters} itself. If the function
1762 returns @code{NULL}, @code{rl_completer_word_break_characters} is used.
1765 @deftypevar {const char *} rl_completer_quote_characters
1766 A list of characters which can be used to quote a substring of the line.
1767 Completion occurs on the entire substring, and within the substring
1768 @code{rl_completer_word_break_characters} are treated as any other character,
1769 unless they also appear within this list.
1772 @deftypevar {const char *} rl_filename_quote_characters
1773 A list of characters that cause a filename to be quoted by the completer
1774 when they appear in a completed filename. The default is the null string.
1777 @deftypevar {const char *} rl_special_prefixes
1778 The list of characters that are word break characters, but should be
1779 left in @var{text} when it is passed to the completion function.
1780 Programs can use this to help determine what kind of completing to do.
1781 For instance, Bash sets this variable to "$@@" so that it can complete
1782 shell variables and hostnames.
1785 @deftypevar int rl_completion_query_items
1786 Up to this many items will be displayed in response to a
1787 possible-completions call. After that, readline asks the user if she is sure
1788 she wants to see them all. The default value is 100. A negative value
1789 indicates that Readline should never ask the user.
1792 @deftypevar {int} rl_completion_append_character
1793 When a single completion alternative matches at the end of the command
1794 line, this character is appended to the inserted completion text. The
1795 default is a space character (@samp{ }). Setting this to the null
1796 character (@samp{\0}) prevents anything being appended automatically.
1797 This can be changed in application-specific completion functions to
1798 provide the ``most sensible word separator character'' according to
1799 an application-specific command line syntax specification.
1802 @deftypevar int rl_completion_suppress_append
1803 If non-zero, @var{rl_completion_append_character} is not appended to
1804 matches at the end of the command line, as described above.
1805 It is set to 0 before any application-specific completion function
1806 is called, and may only be changed within such a function.
1809 @deftypevar int rl_completion_quote_character
1810 When Readline is completing quoted text, as delimited by one of the
1811 characters in @var{rl_completer_quote_characters}, it sets this variable
1812 to the quoting character found.
1813 This is set before any application-specific completion function is called.
1816 @deftypevar int rl_completion_suppress_quote
1817 If non-zero, Readline does not append a matching quote character when
1818 performing completion on a quoted string.
1819 It is set to 0 before any application-specific completion function
1820 is called, and may only be changed within such a function.
1823 @deftypevar int rl_completion_found_quote
1824 When Readline is completing quoted text, it sets this variable
1825 to a non-zero value if the word being completed contains or is delimited
1826 by any quoting characters, including backslashes.
1827 This is set before any application-specific completion function is called.
1830 @deftypevar int rl_completion_mark_symlink_dirs
1831 If non-zero, a slash will be appended to completed filenames that are
1832 symbolic links to directory names, subject to the value of the
1833 user-settable @var{mark-directories} variable.
1834 This variable exists so that application-specific completion functions
1835 can override the user's global preference (set via the
1836 @var{mark-symlinked-directories} Readline variable) if appropriate.
1837 This variable is set to the user's preference before any
1838 application-specific completion function is called, so unless that
1839 function modifies the value, the user's preferences are honored.
1842 @deftypevar int rl_ignore_completion_duplicates
1843 If non-zero, then duplicates in the matches are removed.
1847 @deftypevar int rl_filename_completion_desired
1848 Non-zero means that the results of the matches are to be treated as
1849 filenames. This is @emph{always} zero when completion is attempted,
1850 and can only be changed
1851 within an application-specific completion function. If it is set to a
1852 non-zero value by such a function, directory names have a slash appended
1853 and Readline attempts to quote completed filenames if they contain any
1854 characters in @code{rl_filename_quote_characters} and
1855 @code{rl_filename_quoting_desired} is set to a non-zero value.
1858 @deftypevar int rl_filename_quoting_desired
1859 Non-zero means that the results of the matches are to be quoted using
1860 double quotes (or an application-specific quoting mechanism) if the
1861 completed filename contains any characters in
1862 @code{rl_filename_quote_chars}. This is @emph{always} non-zero
1863 when completion is attempted, and can only be changed within an
1864 application-specific completion function.
1865 The quoting is effected via a call to the function pointed to
1866 by @code{rl_filename_quoting_function}.
1869 @deftypevar int rl_attempted_completion_over
1870 If an application-specific completion function assigned to
1871 @code{rl_attempted_completion_function} sets this variable to a non-zero
1872 value, Readline will not perform its default filename completion even
1873 if the application's completion function returns no matches.
1874 It should be set only by an application's completion function.
1877 @deftypevar int rl_sort_completion_matches
1878 If an application sets this variable to 0, Readline will not sort the
1879 list of completions (which implies that it cannot remove any duplicate
1880 completions). The default value is 1, which means that Readline will
1881 sort the completions and, depending on the value of
1882 @code{rl_ignore_completion_duplicates}, will attempt to remove duplicate
1886 @deftypevar int rl_completion_type
1887 Set to a character describing the type of completion Readline is currently
1888 attempting; see the description of @code{rl_complete_internal()}
1889 (@pxref{Completion Functions}) for the list of characters.
1890 This is set to the appropriate value before any application-specific
1891 completion function is called, allowing such functions to present
1892 the same interface as @code{rl_complete()}.
1895 @deftypevar int rl_completion_invoking_key
1896 Set to the final character in the key sequence that invoked one of the
1897 completion functions that call @code{rl_complete_internal()}. This is
1898 set to the appropriate value before any application-specific completion
1902 @deftypevar int rl_inhibit_completion
1903 If this variable is non-zero, completion is inhibited. The completion
1904 character will be inserted as any other bound to @code{self-insert}.
1907 @node A Short Completion Example
1908 @subsection A Short Completion Example
1910 Here is a small application demonstrating the use of the GNU Readline
1911 library. It is called @code{fileman}, and the source code resides in
1912 @file{examples/fileman.c}. This sample application provides
1913 completion of command names, line editing features, and access to the
1918 /* fileman.c -- A tiny application which demonstrates how to use the
1919 GNU Readline library. This application interactively allows users
1920 to manipulate files and their modes. */
1922 #ifdef HAVE_CONFIG_H
1923 # include <config.h>
1926 #include <sys/types.h>
1927 #ifdef HAVE_SYS_FILE_H
1928 # include <sys/file.h>
1930 #include <sys/stat.h>
1932 #ifdef HAVE_UNISTD_H
1933 # include <unistd.h>
1940 #if defined (HAVE_STRING_H)
1941 # include <string.h>
1942 #else /* !HAVE_STRING_H */
1943 # include <strings.h>
1944 #endif /* !HAVE_STRING_H */
1946 #ifdef HAVE_STDLIB_H
1947 # include <stdlib.h>
1952 #include <readline/readline.h>
1953 #include <readline/history.h>
1955 extern char *xmalloc PARAMS((size_t));
1957 /* The names of functions that actually do the manipulation. */
1958 int com_list PARAMS((char *));
1959 int com_view PARAMS((char *));
1960 int com_rename PARAMS((char *));
1961 int com_stat PARAMS((char *));
1962 int com_pwd PARAMS((char *));
1963 int com_delete PARAMS((char *));
1964 int com_help PARAMS((char *));
1965 int com_cd PARAMS((char *));
1966 int com_quit PARAMS((char *));
1968 /* A structure which contains information on the commands this program
1972 char *name; /* User printable name of the function. */
1973 rl_icpfunc_t *func; /* Function to call to do the job. */
1974 char *doc; /* Documentation for this function. */
1977 COMMAND commands[] = @{
1978 @{ "cd", com_cd, "Change to directory DIR" @},
1979 @{ "delete", com_delete, "Delete FILE" @},
1980 @{ "help", com_help, "Display this text" @},
1981 @{ "?", com_help, "Synonym for `help'" @},
1982 @{ "list", com_list, "List files in DIR" @},
1983 @{ "ls", com_list, "Synonym for `list'" @},
1984 @{ "pwd", com_pwd, "Print the current working directory" @},
1985 @{ "quit", com_quit, "Quit using Fileman" @},
1986 @{ "rename", com_rename, "Rename FILE to NEWNAME" @},
1987 @{ "stat", com_stat, "Print out statistics on FILE" @},
1988 @{ "view", com_view, "View the contents of FILE" @},
1989 @{ (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL @}
1992 /* Forward declarations. */
1993 char *stripwhite ();
1994 COMMAND *find_command ();
1996 /* The name of this program, as taken from argv[0]. */
1999 /* When non-zero, this global means the user is done using this program. */
2008 r = xmalloc (strlen (s) + 1);
2021 initialize_readline (); /* Bind our completer. */
2023 /* Loop reading and executing lines until the user quits. */
2024 for ( ; done == 0; )
2026 line = readline ("FileMan: ");
2031 /* Remove leading and trailing whitespace from the line.
2032 Then, if there is anything left, add it to the history list
2034 s = stripwhite (line);
2047 /* Execute a command line. */
2056 /* Isolate the command word. */
2058 while (line[i] && whitespace (line[i]))
2062 while (line[i] && !whitespace (line[i]))
2068 command = find_command (word);
2072 fprintf (stderr, "%s: No such command for FileMan.\n", word);
2076 /* Get argument to command, if any. */
2077 while (whitespace (line[i]))
2082 /* Call the function. */
2083 return ((*(command->func)) (word));
2086 /* Look up NAME as the name of a command, and return a pointer to that
2087 command. Return a NULL pointer if NAME isn't a command name. */
2094 for (i = 0; commands[i].name; i++)
2095 if (strcmp (name, commands[i].name) == 0)
2096 return (&commands[i]);
2098 return ((COMMAND *)NULL);
2101 /* Strip whitespace from the start and end of STRING. Return a pointer
2107 register char *s, *t;
2109 for (s = string; whitespace (*s); s++)
2115 t = s + strlen (s) - 1;
2116 while (t > s && whitespace (*t))
2123 /* **************************************************************** */
2125 /* Interface to Readline Completion */
2127 /* **************************************************************** */
2129 char *command_generator PARAMS((const char *, int));
2130 char **fileman_completion PARAMS((const char *, int, int));
2132 /* Tell the GNU Readline library how to complete. We want to try to complete
2133 on command names if this is the first word in the line, or on filenames
2135 initialize_readline ()
2137 /* Allow conditional parsing of the ~/.inputrc file. */
2138 rl_readline_name = "FileMan";
2140 /* Tell the completer that we want a crack first. */
2141 rl_attempted_completion_function = fileman_completion;
2144 /* Attempt to complete on the contents of TEXT. START and END bound the
2145 region of rl_line_buffer that contains the word to complete. TEXT is
2146 the word to complete. We can use the entire contents of rl_line_buffer
2147 in case we want to do some simple parsing. Return the array of matches,
2148 or NULL if there aren't any. */
2150 fileman_completion (text, start, end)
2156 matches = (char **)NULL;
2158 /* If this word is at the start of the line, then it is a command
2159 to complete. Otherwise it is the name of a file in the current
2162 matches = rl_completion_matches (text, command_generator);
2167 /* Generator function for command completion. STATE lets us know whether
2168 to start from scratch; without any state (i.e. STATE == 0), then we
2169 start at the top of the list. */
2171 command_generator (text, state)
2175 static int list_index, len;
2178 /* If this is a new word to complete, initialize now. This includes
2179 saving the length of TEXT for efficiency, and initializing the index
2184 len = strlen (text);
2187 /* Return the next name which partially matches from the command list. */
2188 while (name = commands[list_index].name)
2192 if (strncmp (name, text, len) == 0)
2193 return (dupstr(name));
2196 /* If no names matched, then return NULL. */
2197 return ((char *)NULL);
2200 /* **************************************************************** */
2202 /* FileMan Commands */
2204 /* **************************************************************** */
2206 /* String to pass to system (). This is for the LIST, VIEW and RENAME
2208 static char syscom[1024];
2210 /* List the file(s) named in arg. */
2217 sprintf (syscom, "ls -FClg %s", arg);
2218 return (system (syscom));
2224 if (!valid_argument ("view", arg))
2227 #if defined (__MSDOS__)
2228 /* more.com doesn't grok slashes in pathnames */
2229 sprintf (syscom, "less %s", arg);
2231 sprintf (syscom, "more %s", arg);
2233 return (system (syscom));
2239 too_dangerous ("rename");
2248 if (!valid_argument ("stat", arg))
2251 if (stat (arg, &finfo) == -1)
2257 printf ("Statistics for `%s':\n", arg);
2259 printf ("%s has %d link%s, and is %d byte%s in length.\n",
2262 (finfo.st_nlink == 1) ? "" : "s",
2264 (finfo.st_size == 1) ? "" : "s");
2265 printf ("Inode Last Change at: %s", ctime (&finfo.st_ctime));
2266 printf (" Last access at: %s", ctime (&finfo.st_atime));
2267 printf (" Last modified at: %s", ctime (&finfo.st_mtime));
2274 too_dangerous ("delete");
2278 /* Print out help for ARG, or for all of the commands if ARG is
2286 for (i = 0; commands[i].name; i++)
2288 if (!*arg || (strcmp (arg, commands[i].name) == 0))
2290 printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
2297 printf ("No commands match `%s'. Possibilties are:\n", arg);
2299 for (i = 0; commands[i].name; i++)
2301 /* Print in six columns. */
2308 printf ("%s\t", commands[i].name);
2318 /* Change to the directory ARG. */
2322 if (chdir (arg) == -1)
2332 /* Print out the current working directory. */
2338 s = getcwd (dir, sizeof(dir) - 1);
2341 printf ("Error getting pwd: %s\n", dir);
2345 printf ("Current directory is %s\n", dir);
2349 /* The user wishes to quit using this program. Just set DONE non-zero. */
2357 /* Function which tells you that you can't do this. */
2358 too_dangerous (caller)
2362 "%s: Too dangerous for me to distribute. Write it yourself.\n",
2366 /* Return non-zero if ARG is a valid argument for CALLER, else print
2367 an error message and return zero. */
2369 valid_argument (caller, arg)
2374 fprintf (stderr, "%s: Argument required.\n", caller);