]> git.ipfire.org Git - thirdparty/bash.git/blame - lib/readline/doc/rltech.texinfo
Imported from ../bash-2.05.tar.gz.
[thirdparty/bash.git] / lib / readline / doc / rltech.texinfo
CommitLineData
726f6388
JA
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.)
4@setchapternewpage odd
5
6@ifinfo
7This document describes the GNU Readline Library, a utility for aiding
8in the consitency of user interface across discrete programs that need
9to provide a command line interface.
10
28ef6c31 11Copyright (C) 1988-2001 Free Software Foundation, Inc.
726f6388
JA
12
13Permission is granted to make and distribute verbatim copies of
14this manual provided the copyright notice and this permission notice
15pare preserved on all copies.
16
17@ignore
18Permission is granted to process this file through TeX and print the
19results, provided the printed document carries copying permission
20notice identical to this one except for the removal of this paragraph
21(this paragraph not being relevant to the printed manual).
22@end ignore
23
24Permission is granted to copy and distribute modified versions of this
25manual under the conditions for verbatim copying, provided that the entire
26resulting derived work is distributed under the terms of a permission
27notice identical to this one.
28
29Permission is granted to copy and distribute translations of this manual
30into another language, under the above conditions for modified versions,
31except that this permission notice may be stated in a translation approved
32by the Foundation.
33@end ifinfo
34
35@node Programming with GNU Readline
36@chapter Programming with GNU Readline
37
28ef6c31 38This chapter describes the interface between the @sc{gnu} Readline Library and
726f6388 39other programs. If you are a programmer, and you wish to include the
28ef6c31 40features found in @sc{gnu} Readline
726f6388
JA
41such as completion, line editing, and interactive history manipulation
42in your own programs, this section is for you.
43
44@menu
45* Basic Behavior:: Using the default behavior of Readline.
46* Custom Functions:: Adding your own functions to Readline.
47* Readline Variables:: Variables accessible to custom
48 functions.
49* Readline Convenience Functions:: Functions which Readline supplies to
b72432fd
JA
50 aid in writing your own custom
51 functions.
52* Readline Signal Handling:: How Readline behaves when it receives signals.
726f6388
JA
53* Custom Completers:: Supplanting or supplementing Readline's
54 completion functions.
55@end menu
56
57@node Basic Behavior
58@section Basic Behavior
59
60Many programs provide a command line interface, such as @code{mail},
61@code{ftp}, and @code{sh}. For such programs, the default behaviour of
62Readline is sufficient. This section describes how to use Readline in
63the simplest way possible, perhaps to replace calls in your code to
28ef6c31 64@code{gets()} or @code{fgets()}.
726f6388
JA
65
66@findex readline
67@cindex readline, function
28ef6c31
JA
68
69The function @code{readline()} prints a prompt @var{prompt}
70and then reads and returns a single line of text from the user.
71If @var{prompt} is @code{NULL} or the empty string, no prompt is displayed.
72The line @code{readline} returns is allocated with @code{malloc()};
73the caller should @code{free()} the line when it has finished with it.
74The declaration for @code{readline} in ANSI C is
726f6388
JA
75
76@example
28ef6c31 77@code{char *readline (const char *@var{prompt});}
726f6388
JA
78@end example
79
80@noindent
81So, one might say
82@example
83@code{char *line = readline ("Enter a line: ");}
84@end example
85@noindent
86in order to read a line of text from the user.
87The line returned has the final newline removed, so only the
88text remains.
89
90If @code{readline} encounters an @code{EOF} while reading the line, and the
91line is empty at that point, then @code{(char *)NULL} is returned.
92Otherwise, the line is ended just as if a newline had been typed.
93
94If you want the user to be able to get at the line later, (with
28ef6c31 95@key{C-p} for example), you must call @code{add_history()} to save the
726f6388
JA
96line away in a @dfn{history} list of such lines.
97
98@example
99@code{add_history (line)};
100@end example
101
102@noindent
103For full details on the GNU History Library, see the associated manual.
104
105It is preferable to avoid saving empty lines on the history list, since
106users rarely have a burning need to reuse a blank line. Here is
28ef6c31 107a function which usefully replaces the standard @code{gets()} library
726f6388
JA
108function, and has the advantage of no static buffer to overflow:
109
110@example
111/* A static variable for holding the line. */
112static char *line_read = (char *)NULL;
113
114/* Read a string, and return a pointer to it. Returns NULL on EOF. */
115char *
116rl_gets ()
117@{
118 /* If the buffer has already been allocated, return the memory
119 to the free pool. */
120 if (line_read)
121 @{
122 free (line_read);
123 line_read = (char *)NULL;
124 @}
125
126 /* Get a line from the user. */
127 line_read = readline ("");
128
129 /* If the line has any text in it, save it on the history. */
130 if (line_read && *line_read)
131 add_history (line_read);
132
133 return (line_read);
134@}
135@end example
136
137This function gives the user the default behaviour of @key{TAB}
138completion: completion on file names. If you do not want Readline to
139complete on filenames, you can change the binding of the @key{TAB} key
28ef6c31 140with @code{rl_bind_key()}.
726f6388
JA
141
142@example
28ef6c31 143@code{int rl_bind_key (int @var{key}, rl_command_func_t *@var{function});}
726f6388
JA
144@end example
145
28ef6c31 146@code{rl_bind_key()} takes two arguments: @var{key} is the character that
726f6388 147you want to bind, and @var{function} is the address of the function to
28ef6c31 148call when @var{key} is pressed. Binding @key{TAB} to @code{rl_insert()}
726f6388 149makes @key{TAB} insert itself.
28ef6c31 150@code{rl_bind_key()} returns non-zero if @var{key} is not a valid
726f6388
JA
151ASCII character code (between 0 and 255).
152
153Thus, to disable the default @key{TAB} behavior, the following suffices:
154@example
155@code{rl_bind_key ('\t', rl_insert);}
156@end example
157
158This code should be executed once at the start of your program; you
28ef6c31 159might write a function called @code{initialize_readline()} which
726f6388
JA
160performs this and other desired initializations, such as installing
161custom completers (@pxref{Custom Completers}).
162
163@node Custom Functions
164@section Custom Functions
165
166Readline provides many functions for manipulating the text of
167the line, but it isn't possible to anticipate the needs of all
168programs. This section describes the various functions and variables
169defined within the Readline library which allow a user program to add
170customized functionality to Readline.
171
bb70624e
JA
172Before declaring any functions that customize Readline's behavior, or
173using any functionality Readline provides in other code, an
174application writer should include the file @code{<readline/readline.h>}
175in any file that uses Readline's features. Since some of the definitions
176in @code{readline.h} use the @code{stdio} library, the file
177@code{<stdio.h>} should be included before @code{readline.h}.
178
726f6388 179@menu
28ef6c31 180* Readline Typedefs:: C declarations to make code readable.
726f6388
JA
181* Function Writing:: Variables and calling conventions.
182@end menu
183
28ef6c31
JA
184@node Readline Typedefs
185@subsection Readline Typedefs
726f6388 186
28ef6c31
JA
187For readabilty, we declare a number of new object types, all pointers
188to functions.
726f6388 189
28ef6c31
JA
190The reason for declaring these new types is to make it easier to write
191code describing pointers to C functions with appropriately prototyped
192arguments and return values.
726f6388 193
28ef6c31
JA
194For instance, say we want to declare a variable @var{func} as a pointer
195to a function which takes two @code{int} arguments and returns an
196@code{int} (this is the type of all of the Readline bindable functions).
197Instead of the classic C declaration
726f6388 198
28ef6c31 199@code{int (*func)();}
726f6388
JA
200
201@noindent
28ef6c31 202or the ANSI-C style declaration
726f6388 203
28ef6c31 204@code{int (*func)(int, int);}
726f6388
JA
205
206@noindent
28ef6c31 207we may write
726f6388 208
28ef6c31 209@code{rl_command_func_t *func;}
726f6388 210
28ef6c31
JA
211The full list of function pointer types available is
212
213@table @code
214@item typedef int rl_command_func_t (int, int);
215
216@item typedef char *rl_compentry_func_t (const char *, int);
217
218@item typedef char **rl_completion_func_t (const char *, int, int);
219
220@item typedef char *rl_quote_func_t (char *, int, char *);
221
222@item typedef char *rl_dequote_func_t (char *, int);
223
224@item typedef int rl_compignore_func_t (char **);
225
226@item typedef void rl_compdisp_func_t (char **, int, int);
227
228@item typedef int rl_hook_func_t (void);
229
230@item typedef int rl_getc_func_t (FILE *);
231
232@item typedef int rl_linebuf_func_t (char *, int);
233
234@item typedef int rl_intfunc_t (int);
235@item #define rl_ivoidfunc_t rl_hook_func_t
236@item typedef int rl_icpfunc_t (char *);
237@item typedef int rl_icppfunc_t (char **);
238
239@item typedef void rl_voidfunc_t (void);
240@item typedef void rl_vintfunc_t (int);
241@item typedef void rl_vcpfunc_t (char *);
242@item typedef void rl_vcppfunc_t (char **);
243
244@end table
726f6388
JA
245
246@node Function Writing
247@subsection Writing a New Function
248
249In order to write new functions for Readline, you need to know the
250calling conventions for keyboard-invoked functions, and the names of the
251variables that describe the current state of the line read so far.
252
253The calling sequence for a command @code{foo} looks like
254
255@example
256@code{foo (int count, int key)}
257@end example
258
259@noindent
260where @var{count} is the numeric argument (or 1 if defaulted) and
261@var{key} is the key that invoked this function.
262
263It is completely up to the function as to what should be done with the
264numeric argument. Some functions use it as a repeat count, some
265as a flag, and others to choose alternate behavior (refreshing the current
266line as opposed to refreshing the screen, for example). Some choose to
267ignore it. In general, if a
268function uses the numeric argument as a repeat count, it should be able
269to do something useful with both negative and positive arguments.
270At the very least, it should be aware that it can be passed a
271negative argument.
272
273@node Readline Variables
274@section Readline Variables
275
276These variables are available to function writers.
277
278@deftypevar {char *} rl_line_buffer
279This is the line gathered so far. You are welcome to modify the
bb70624e
JA
280contents of the line, but see @ref{Allowing Undoing}. The
281function @code{rl_extend_line_buffer} is available to increase
282the memory allocated to @code{rl_line_buffer}.
726f6388
JA
283@end deftypevar
284
285@deftypevar int rl_point
286The offset of the current cursor position in @code{rl_line_buffer}
287(the @emph{point}).
288@end deftypevar
289
290@deftypevar int rl_end
291The number of characters present in @code{rl_line_buffer}. When
292@code{rl_point} is at the end of the line, @code{rl_point} and
293@code{rl_end} are equal.
294@end deftypevar
295
296@deftypevar int rl_mark
28ef6c31 297The @var{mark} (saved position) in the current line. If set, the mark
726f6388
JA
298and point define a @emph{region}.
299@end deftypevar
300
301@deftypevar int rl_done
302Setting this to a non-zero value causes Readline to return the current
303line immediately.
304@end deftypevar
305
28ef6c31
JA
306@deftypevar int rl_num_chars_to_read
307Setting this to a positive value before calling @code{readline()} causes
308Readline to return after accepting that many characters, rather
309than reading up to a character bound to @code{accept-line}.
310@end deftypevar
311
726f6388
JA
312@deftypevar int rl_pending_input
313Setting this to a value makes it the next keystroke read. This is a
314way to stuff a single character into the input stream.
315@end deftypevar
316
28ef6c31
JA
317@deftypevar int rl_dispatching
318Set to a non-zero value if a function is being called from a key binding;
319zero otherwise. Application functions can test this to discover whether
320they were called directly or by Readline's dispatching mechanism.
321@end deftypevar
322
b72432fd
JA
323@deftypevar int rl_erase_empty_line
324Setting this to a non-zero value causes Readline to completely erase
325the current line, including any prompt, any time a newline is typed as
326the only character on an otherwise-empty line. The cursor is moved to
327the beginning of the newly-blank line.
328@end deftypevar
329
726f6388
JA
330@deftypevar {char *} rl_prompt
331The prompt Readline uses. This is set from the argument to
28ef6c31
JA
332@code{readline()}, and should not be assigned to directly.
333The @code{rl_set_prompt()} function (@pxref{Redisplay}) may
334be used to modify the prompt string after calling @code{readline()}.
726f6388
JA
335@end deftypevar
336
bb70624e
JA
337@deftypevar int rl_already_prompted
338If an application wishes to display the prompt itself, rather than have
339Readline do it the first time @code{readline()} is called, it should set
340this variable to a non-zero value after displaying the prompt.
341The prompt must also be passed as the argument to @code{readline()} so
342the redisplay functions can update the display properly.
343The calling application is responsible for managing the value; Readline
344never sets it.
345@end deftypevar
346
28ef6c31 347@deftypevar {const char *} rl_library_version
ccc6cda3
JA
348The version number of this revision of the library.
349@end deftypevar
350
28ef6c31
JA
351@deftypevar {int} rl_gnu_readline_p
352Always set to 1, denoting that this is @sc{gnu} readline rather than some
353emulation.
726f6388
JA
354@end deftypevar
355
28ef6c31
JA
356@deftypevar {const char *} rl_terminal_name
357The terminal type, used for initialization. If not set by the application,
358Readline sets this to the value of the @env{TERM} environment variable
359the first time it is called.
360@end deftypevar
361
362@deftypevar {const char *} rl_readline_name
726f6388
JA
363This variable is set to a unique name by each application using Readline.
364The value allows conditional parsing of the inputrc file
365(@pxref{Conditional Init Constructs}).
366@end deftypevar
367
368@deftypevar {FILE *} rl_instream
369The stdio stream from which Readline reads input.
370@end deftypevar
371
372@deftypevar {FILE *} rl_outstream
373The stdio stream to which Readline performs output.
374@end deftypevar
375
28ef6c31
JA
376@deftypevar {rl_command_func_t *} rl_last_func
377The address of the last command function Readline executed. May be used to
378test whether or not a function is being executed twice in succession, for
379example.
380@end deftypevar
381
382@deftypevar {rl_hook_func_t *} rl_startup_hook
726f6388
JA
383If non-zero, this is the address of a function to call just
384before @code{readline} prints the first prompt.
385@end deftypevar
386
28ef6c31 387@deftypevar {rl_hook_func_t *} rl_pre_input_hook
b72432fd
JA
388If non-zero, this is the address of a function to call after
389the first prompt has been printed and just before @code{readline}
390starts reading input characters.
391@end deftypevar
392
28ef6c31 393@deftypevar {rl_hook_func_t *} rl_event_hook
726f6388 394If non-zero, this is the address of a function to call periodically
28ef6c31
JA
395when Readline is waiting for terminal input.
396By default, this will be called at most ten times a second if there
397is no keyboard input.
726f6388
JA
398@end deftypevar
399
28ef6c31
JA
400@deftypevar {rl_getc_func_t *} rl_getc_function
401If non-zero, Readline will call indirectly through this pointer
ccc6cda3 402to get a character from the input stream. By default, it is set to
28ef6c31
JA
403@code{rl_getc}, the default Readline character input function
404(@pxref{Character Input}).
ccc6cda3
JA
405@end deftypevar
406
28ef6c31
JA
407@deftypevar {rl_voidfunc_t *} rl_redisplay_function
408If non-zero, Readline will call indirectly through this pointer
ccc6cda3 409to update the display with the current contents of the editing buffer.
28ef6c31 410By default, it is set to @code{rl_redisplay}, the default Readline
ccc6cda3
JA
411redisplay function (@pxref{Redisplay}).
412@end deftypevar
413
28ef6c31
JA
414@deftypevar {rl_vintfunc_t *} rl_prep_term_function
415If non-zero, Readline will call indirectly through this pointer
416to initialize the terminal. The function takes a single argument, an
417@code{int} flag that says whether or not to use eight-bit characters.
418By default, this is set to @code{rl_prep_terminal}
419(@pxref{Terminal Management}).
420@end deftypevar
421
422@deftypevar {rl_voidfunc_t *} rl_deprep_term_function
423If non-zero, Readline will call indirectly through this pointer
424to reset the terminal. This function should undo the effects of
425@code{rl_prep_term_function}.
426By default, this is set to @code{rl_deprep_terminal}
427(@pxref{Terminal Management}).
428@end deftypevar
429
ccc6cda3
JA
430@deftypevar {Keymap} rl_executing_keymap
431This variable is set to the keymap (@pxref{Keymaps}) in which the
432currently executing readline function was found.
433@end deftypevar
434
435@deftypevar {Keymap} rl_binding_keymap
436This variable is set to the keymap (@pxref{Keymaps}) in which the
437last key binding occurred.
438@end deftypevar
439
28ef6c31
JA
440@deftypevar {char *} rl_executing_macro
441This variable is set to the text of any currently-executing macro.
442@end deftypevar
443
444@deftypevar {int} rl_readline_state
445A variable with bit values that encapsulate the current Readline state.
446A bit is set with the @code{RL_SETSTATE} macro, and unset with the
447@code{RL_UNSETSTATE} macro. Use the @code{RL_ISSTATE} macro to test
448whether a particular state bit is set. Current state bits include:
449
450@table @code
451@item RL_STATE_NONE
452Readline has not yet been called, nor has it begun to intialize.
453@item RL_STATE_INITIALIZING
454Readline is initializing its internal data structures.
455@item RL_STATE_INITIALIZED
456Readline has completed its initialization.
457@item RL_STATE_TERMPREPPED
458Readline has modified the terminal modes to do its own input and redisplay.
459@item RL_STATE_READCMD
460Readline is reading a command from the keyboard.
461@item RL_STATE_METANEXT
462Readline is reading more input after reading the meta-prefix character.
463@item RL_STATE_DISPATCHING
464Readline is dispatching to a command.
465@item RL_STATE_MOREINPUT
466Readline is reading more input while executing an editing command.
467@item RL_STATE_ISEARCH
468Readline is performing an incremental history search.
469@item RL_STATE_NSEARCH
470Readline is performing a non-incremental history search.
471@item RL_STATE_SEARCH
472Readline is searching backward or forward through the history for a string.
473@item RL_STATE_NUMERICARG
474Readline is reading a numeric argument.
475@item RL_STATE_MACROINPUT
476Readline is currently getting its input from a previously-defined keyboard
477macro.
478@item RL_STATE_MACRODEF
479Readline is currently reading characters defining a keyboard macro.
480@item RL_STATE_OVERWRITE
481Readline is in overwrite mode.
482@item RL_STATE_COMPLETING
483Readline is performing word completion.
484@item RL_STATE_SIGHANDLER
485Readline is currently executing the readline signal handler.
486@item RL_STATE_UNDOING
487Readline is performing an undo.
488@item RL_STATE_DONE
489Readline has read a key sequence bound to @code{accept-line}
490and is about to return the line to the caller.
491@end table
492
493@end deftypevar
494
495@deftypevar {int} rl_explicit_arg
496Set to a non-zero value if an explicit numeric argument was specified by
497the user. Only valid in a bindable command function.
498@end deftypevar
499
500@deftypevar {int} rl_numeric_arg
501Set to the value of any numeric argument explicitly specified by the user
502before executing the current Readline function. Only valid in a bindable
503command function.
504@end deftypevar
505
506@deftypevar {int} rl_editing_mode
507Set to a value denoting Readline's current editing mode. A value of
508@var{1} means Readline is currently in emacs mode; @var{0}
509means that vi mode is active.
510@end deftypevar
511
512
726f6388
JA
513@node Readline Convenience Functions
514@section Readline Convenience Functions
515
516@menu
517* Function Naming:: How to give a function you write a name.
518* Keymaps:: Making keymaps.
519* Binding Keys:: Changing Keymaps.
520* Associating Function Names and Bindings:: Translate function names to
521 key sequences.
522* Allowing Undoing:: How to make your functions undoable.
523* Redisplay:: Functions to control line display.
524* Modifying Text:: Functions to modify @code{rl_line_buffer}.
28ef6c31
JA
525* Character Input:: Functions to read keyboard input.
526* Terminal Management:: Functions to manage terminal settings.
726f6388 527* Utility Functions:: Generally useful functions and hooks.
28ef6c31 528* Miscellaneous Functions:: Functions that don't fall into any category.
ccc6cda3 529* Alternate Interface:: Using Readline in a `callback' fashion.
28ef6c31 530* A Readline Example:: An example Readline function.
726f6388
JA
531@end menu
532
533@node Function Naming
534@subsection Naming a Function
535
536The user can dynamically change the bindings of keys while using
537Readline. This is done by representing the function with a descriptive
538name. The user is able to type the descriptive name when referring to
539the function. Thus, in an init file, one might find
540
541@example
542Meta-Rubout: backward-kill-word
543@end example
544
545This binds the keystroke @key{Meta-Rubout} to the function
546@emph{descriptively} named @code{backward-kill-word}. You, as the
547programmer, should bind the functions you write to descriptive names as
548well. Readline provides a function for doing that:
549
28ef6c31 550@deftypefun int rl_add_defun (const char *name, rl_command_func_t *function, int key)
726f6388
JA
551Add @var{name} to the list of named functions. Make @var{function} be
552the function that gets called. If @var{key} is not -1, then bind it to
28ef6c31 553@var{function} using @code{rl_bind_key()}.
726f6388
JA
554@end deftypefun
555
556Using this function alone is sufficient for most applications. It is
557the recommended way to add a few functions to the default functions that
558Readline has built in. If you need to do something other
559than adding a function to Readline, you may need to use the
560underlying functions described below.
561
562@node Keymaps
563@subsection Selecting a Keymap
564
565Key bindings take place on a @dfn{keymap}. The keymap is the
566association between the keys that the user types and the functions that
567get run. You can make your own keymaps, copy existing keymaps, and tell
568Readline which keymap to use.
569
28ef6c31 570@deftypefun Keymap rl_make_bare_keymap (void)
726f6388 571Returns a new, empty keymap. The space for the keymap is allocated with
28ef6c31
JA
572@code{malloc()}; the caller should free it by calling
573@code{rl_discard_keymap()} when done.
726f6388
JA
574@end deftypefun
575
576@deftypefun Keymap rl_copy_keymap (Keymap map)
577Return a new keymap which is a copy of @var{map}.
578@end deftypefun
579
28ef6c31 580@deftypefun Keymap rl_make_keymap (void)
726f6388
JA
581Return a new keymap with the printing characters bound to rl_insert,
582the lowercase Meta characters bound to run their equivalents, and
583the Meta digits bound to produce numeric arguments.
584@end deftypefun
585
586@deftypefun void rl_discard_keymap (Keymap keymap)
587Free the storage associated with @var{keymap}.
588@end deftypefun
589
590Readline has several internal keymaps. These functions allow you to
591change which keymap is active.
592
28ef6c31 593@deftypefun Keymap rl_get_keymap (void)
726f6388
JA
594Returns the currently active keymap.
595@end deftypefun
596
597@deftypefun void rl_set_keymap (Keymap keymap)
598Makes @var{keymap} the currently active keymap.
599@end deftypefun
600
28ef6c31 601@deftypefun Keymap rl_get_keymap_by_name (const char *name)
726f6388
JA
602Return the keymap matching @var{name}. @var{name} is one which would
603be supplied in a @code{set keymap} inputrc line (@pxref{Readline Init File}).
604@end deftypefun
605
d166f048
JA
606@deftypefun {char *} rl_get_keymap_name (Keymap keymap)
607Return the name matching @var{keymap}. @var{name} is one which would
608be supplied in a @code{set keymap} inputrc line (@pxref{Readline Init File}).
609@end deftypefun
610
726f6388
JA
611@node Binding Keys
612@subsection Binding Keys
613
28ef6c31
JA
614Key sequences are associate with functions through the keymap.
615Readline has several internal keymaps: @code{emacs_standard_keymap},
726f6388
JA
616@code{emacs_meta_keymap}, @code{emacs_ctlx_keymap},
617@code{vi_movement_keymap}, and @code{vi_insertion_keymap}.
618@code{emacs_standard_keymap} is the default, and the examples in
619this manual assume that.
620
28ef6c31 621Since @code{readline()} installs a set of default key bindings the first
bb70624e 622time it is called, there is always the danger that a custom binding
28ef6c31 623installed before the first call to @code{readline()} will be overridden.
bb70624e
JA
624An alternate mechanism is to install custom key bindings in an
625initialization function assigned to the @code{rl_startup_hook} variable
626(@pxref{Readline Variables}).
627
726f6388
JA
628These functions manage key bindings.
629
28ef6c31 630@deftypefun int rl_bind_key (int key, rl_command_func_t *function)
726f6388
JA
631Binds @var{key} to @var{function} in the currently active keymap.
632Returns non-zero in the case of an invalid @var{key}.
633@end deftypefun
634
28ef6c31 635@deftypefun int rl_bind_key_in_map (int key, rl_command_func_t *function, Keymap map)
726f6388
JA
636Bind @var{key} to @var{function} in @var{map}. Returns non-zero in the case
637of an invalid @var{key}.
638@end deftypefun
639
640@deftypefun int rl_unbind_key (int key)
641Bind @var{key} to the null function in the currently active keymap.
642Returns non-zero in case of error.
643@end deftypefun
644
645@deftypefun int rl_unbind_key_in_map (int key, Keymap map)
646Bind @var{key} to the null function in @var{map}.
647Returns non-zero in case of error.
648@end deftypefun
649
28ef6c31 650@deftypefun int rl_unbind_function_in_map (rl_command_func_t *function, Keymap map)
cce855bc
JA
651Unbind all keys that execute @var{function} in @var{map}.
652@end deftypefun
653
28ef6c31 654@deftypefun int rl_unbind_command_in_map (const char *command, Keymap map)
cce855bc
JA
655Unbind all keys that are bound to @var{command} in @var{map}.
656@end deftypefun
657
28ef6c31
JA
658@deftypefun int rl_set_key (const char *keyseq, rl_command_func_t *function, Keymap map)
659Bind the key sequence represented by the string @var{keyseq} to the function
660@var{function}. This makes new keymaps as
661necessary. The initial keymap in which to do bindings is @var{map}.
662@end deftypefun
663
664@deftypefun int rl_generic_bind (int type, const char *keyseq, char *data, Keymap map)
726f6388
JA
665Bind the key sequence represented by the string @var{keyseq} to the arbitrary
666pointer @var{data}. @var{type} says what kind of data is pointed to by
667@var{data}; this can be a function (@code{ISFUNC}), a macro
668(@code{ISMACR}), or a keymap (@code{ISKMAP}). This makes new keymaps as
669necessary. The initial keymap in which to do bindings is @var{map}.
670@end deftypefun
671
672@deftypefun int rl_parse_and_bind (char *line)
673Parse @var{line} as if it had been read from the @code{inputrc} file and
674perform any key bindings and variable assignments found
675(@pxref{Readline Init File}).
676@end deftypefun
677
28ef6c31 678@deftypefun int rl_read_init_file (const char *filename)
726f6388
JA
679Read keybindings and variable assignments from @var{filename}
680(@pxref{Readline Init File}).
681@end deftypefun
682
683@node Associating Function Names and Bindings
684@subsection Associating Function Names and Bindings
685
686These functions allow you to find out what keys invoke named functions
28ef6c31
JA
687and the functions invoked by a particular key sequence. You may also
688associate a new function name with an arbitrary function.
726f6388 689
28ef6c31 690@deftypefun {rl_command_func_t *} rl_named_function (const char *name)
726f6388
JA
691Return the function with name @var{name}.
692@end deftypefun
693
28ef6c31 694@deftypefun {rl_command_func_t *} rl_function_of_keyseq (const char *keyseq, Keymap map, int *type)
726f6388 695Return the function invoked by @var{keyseq} in keymap @var{map}.
28ef6c31
JA
696If @var{map} is @code{NULL}, the current keymap is used. If @var{type} is
697not @code{NULL}, the type of the object is returned in the @code{int} variable
698it points to (one of @code{ISFUNC}, @code{ISKMAP}, or @code{ISMACR}).
726f6388
JA
699@end deftypefun
700
28ef6c31 701@deftypefun {char **} rl_invoking_keyseqs (rl_command_func_t *function)
726f6388
JA
702Return an array of strings representing the key sequences used to
703invoke @var{function} in the current keymap.
704@end deftypefun
705
28ef6c31 706@deftypefun {char **} rl_invoking_keyseqs_in_map (rl_command_func_t *function, Keymap map)
726f6388
JA
707Return an array of strings representing the key sequences used to
708invoke @var{function} in the keymap @var{map}.
709@end deftypefun
710
711@deftypefun void rl_function_dumper (int readable)
712Print the readline function names and the key sequences currently
713bound to them to @code{rl_outstream}. If @var{readable} is non-zero,
714the list is formatted in such a way that it can be made part of an
715@code{inputrc} file and re-read.
716@end deftypefun
717
28ef6c31 718@deftypefun void rl_list_funmap_names (void)
726f6388
JA
719Print the names of all bindable Readline functions to @code{rl_outstream}.
720@end deftypefun
721
28ef6c31 722@deftypefun {const char **} rl_funmap_names (void)
bb70624e
JA
723Return a NULL terminated array of known function names. The array is
724sorted. The array itself is allocated, but not the strings inside. You
28ef6c31
JA
725should @code{free()} the array when you are done, but not the pointers.
726@end deftypefun
727
728@deftypefun int rl_add_funmap_entry (const char *name, rl_command_func_t *function)
729Add @var{name} to the list of bindable Readline command names, and make
730@var{function} the function to be called when @var{name} is invoked.
bb70624e
JA
731@end deftypefun
732
726f6388
JA
733@node Allowing Undoing
734@subsection Allowing Undoing
735
736Supporting the undo command is a painless thing, and makes your
737functions much more useful. It is certainly easy to try
28ef6c31 738something if you know you can undo it.
726f6388
JA
739
740If your function simply inserts text once, or deletes text once, and
28ef6c31 741uses @code{rl_insert_text()} or @code{rl_delete_text()} to do it, then
726f6388
JA
742undoing is already done for you automatically.
743
744If you do multiple insertions or multiple deletions, or any combination
745of these operations, you should group them together into one operation.
28ef6c31
JA
746This is done with @code{rl_begin_undo_group()} and
747@code{rl_end_undo_group()}.
726f6388
JA
748
749The types of events that can be undone are:
750
751@example
752enum undo_code @{ UNDO_DELETE, UNDO_INSERT, UNDO_BEGIN, UNDO_END @};
753@end example
754
755Notice that @code{UNDO_DELETE} means to insert some text, and
756@code{UNDO_INSERT} means to delete some text. That is, the undo code
28ef6c31
JA
757tells what to undo, not how to undo it. @code{UNDO_BEGIN} and
758@code{UNDO_END} are tags added by @code{rl_begin_undo_group()} and
759@code{rl_end_undo_group()}.
726f6388 760
28ef6c31 761@deftypefun int rl_begin_undo_group (void)
726f6388 762Begins saving undo information in a group construct. The undo
28ef6c31
JA
763information usually comes from calls to @code{rl_insert_text()} and
764@code{rl_delete_text()}, but could be the result of calls to
765@code{rl_add_undo()}.
726f6388
JA
766@end deftypefun
767
28ef6c31 768@deftypefun int rl_end_undo_group (void)
726f6388 769Closes the current undo group started with @code{rl_begin_undo_group
28ef6c31
JA
770()}. There should be one call to @code{rl_end_undo_group()}
771for each call to @code{rl_begin_undo_group()}.
726f6388
JA
772@end deftypefun
773
774@deftypefun void rl_add_undo (enum undo_code what, int start, int end, char *text)
775Remember how to undo an event (according to @var{what}). The affected
776text runs from @var{start} to @var{end}, and encompasses @var{text}.
777@end deftypefun
778
28ef6c31 779@deftypefun void rl_free_undo_list (void)
726f6388
JA
780Free the existing undo list.
781@end deftypefun
782
28ef6c31 783@deftypefun int rl_do_undo (void)
726f6388
JA
784Undo the first thing on the undo list. Returns @code{0} if there was
785nothing to undo, non-zero if something was undone.
786@end deftypefun
787
788Finally, if you neither insert nor delete text, but directly modify the
28ef6c31 789existing text (e.g., change its case), call @code{rl_modifying()}
726f6388
JA
790once, just before you modify the text. You must supply the indices of
791the text range that you are going to modify.
792
793@deftypefun int rl_modifying (int start, int end)
794Tell Readline to save the text between @var{start} and @var{end} as a
795single undo unit. It is assumed that you will subsequently modify
796that text.
797@end deftypefun
798
799@node Redisplay
800@subsection Redisplay
801
28ef6c31 802@deftypefun void rl_redisplay (void)
726f6388
JA
803Change what's displayed on the screen to reflect the current contents
804of @code{rl_line_buffer}.
805@end deftypefun
806
28ef6c31 807@deftypefun int rl_forced_update_display (void)
726f6388
JA
808Force the line to be updated and redisplayed, whether or not
809Readline thinks the screen display is correct.
810@end deftypefun
811
28ef6c31 812@deftypefun int rl_on_new_line (void)
bb70624e 813Tell the update functions that we have moved onto a new (empty) line,
726f6388
JA
814usually after ouputting a newline.
815@end deftypefun
816
28ef6c31 817@deftypefun int rl_on_new_line_with_prompt (void)
bb70624e
JA
818Tell the update functions that we have moved onto a new line, with
819@var{rl_prompt} already displayed.
820This could be used by applications that want to output the prompt string
821themselves, but still need Readline to know the prompt string length for
822redisplay.
823It should be used after setting @var{rl_already_prompted}.
824@end deftypefun
825
28ef6c31 826@deftypefun int rl_reset_line_state (void)
726f6388
JA
827Reset the display state to a clean state and redisplay the current line
828starting on a new line.
829@end deftypefun
830
28ef6c31
JA
831@deftypefun int rl_crlf (void)
832Move the cursor to the start of the next screen line.
833@end deftypefun
834
835@deftypefun int rl_show_char (int c)
836Display character @var{c} on @code{rl_outstream}.
837If Readline has not been set to display meta characters directly, this
838will convert meta characters to a meta-prefixed key sequence.
839This is intended for use by applications which wish to do their own
840redisplay.
841@end deftypefun
842
843@deftypefun int rl_message (const char *, @dots{})
844The arguments are a format string as would be supplied to @code{printf},
845possibly containing conversion specifications such as @samp{%d}, and
846any additional arguments necessary to satisfy the conversion specifications.
847The resulting string is displayed in the @dfn{echo area}. The echo area
726f6388
JA
848is also used to display numeric arguments and search strings.
849@end deftypefun
850
28ef6c31 851@deftypefun int rl_clear_message (void)
726f6388
JA
852Clear the message in the echo area.
853@end deftypefun
854
28ef6c31 855@deftypefun void rl_save_prompt (void)
b72432fd 856Save the local Readline prompt display state in preparation for
28ef6c31 857displaying a new message in the message area with @code{rl_message()}.
b72432fd
JA
858@end deftypefun
859
28ef6c31 860@deftypefun void rl_restore_prompt (void)
b72432fd
JA
861Restore the local Readline prompt display state saved by the most
862recent call to @code{rl_save_prompt}.
863@end deftypefun
864
28ef6c31
JA
865@deftypefun int rl_expand_prompt (char *prompt)
866Expand any special character sequences in @var{prompt} and set up the
867local Readline prompt redisplay variables.
868This function is called by @code{readline()}. It may also be called to
869expand the primary prompt if the @code{rl_on_new_line_with_prompt()}
870function or @code{rl_already_prompted} variable is used.
871It returns the number of visible characters on the last line of the
872(possibly multi-line) prompt.
873@end deftypefun
874
875@deftypefun int rl_set_prompt (const char *prompt)
876Make Readline use @var{prompt} for subsequent redisplay. This calls
877@code{rl_expand_prompt()} to expand the prompt and sets @code{rl_prompt}
878to the result.
879@end deftypefun
880
726f6388
JA
881@node Modifying Text
882@subsection Modifying Text
883
28ef6c31 884@deftypefun int rl_insert_text (const char *text)
726f6388
JA
885Insert @var{text} into the line at the current cursor position.
886@end deftypefun
887
888@deftypefun int rl_delete_text (int start, int end)
889Delete the text between @var{start} and @var{end} in the current line.
890@end deftypefun
891
892@deftypefun {char *} rl_copy_text (int start, int end)
893Return a copy of the text between @var{start} and @var{end} in
894the current line.
895@end deftypefun
896
897@deftypefun int rl_kill_text (int start, int end)
898Copy the text between @var{start} and @var{end} in the current line
899to the kill ring, appending or prepending to the last kill if the
900last command was a kill command. The text is deleted.
901If @var{start} is less than @var{end},
902the text is appended, otherwise prepended. If the last command was
903not a kill, a new kill ring slot is used.
904@end deftypefun
905
28ef6c31
JA
906@deftypefun int rl_push_macro_input (char *macro)
907Cause @var{macro} to be inserted into the line, as if it had been invoked
908by a key bound to a macro. Not especially useful; use
909@code{rl_insert_text()} instead.
910@end deftypefun
726f6388 911
28ef6c31
JA
912@node Character Input
913@subsection Character Input
914
915@deftypefun int rl_read_key (void)
916Return the next character available from Readline's current input stream.
917This handles input inserted into
918the input stream via @var{rl_pending_input} (@pxref{Readline Variables})
919and @code{rl_stuff_char()}, macros, and characters read from the keyboard.
920While waiting for input, this function will call any function assigned to
921the @code{rl_event_hook} variable.
726f6388
JA
922@end deftypefun
923
28ef6c31
JA
924@deftypefun int rl_getc (FILE *stream)
925Return the next character available from @var{stream}, which is assumed to
926be the keyboard.
ccc6cda3
JA
927@end deftypefun
928
726f6388
JA
929@deftypefun int rl_stuff_char (int c)
930Insert @var{c} into the Readline input stream. It will be "read"
931before Readline attempts to read characters from the terminal with
28ef6c31 932@code{rl_read_key()}.
726f6388
JA
933@end deftypefun
934
28ef6c31
JA
935@deftypefun int rl_execute_next (int c)
936Make @var{c} be the next command to be executed when @code{rl_read_key()}
937is called. This sets @var{rl_pending_input}.
d166f048
JA
938@end deftypefun
939
28ef6c31
JA
940@deftypefun int rl_clear_pending_input (void)
941Unset @var{rl_pending_input}, effectively negating the effect of any
942previous call to @code{rl_execute_next()}. This works only if the
943pending input has not already been read with @code{rl_read_key()}.
944@end deftypefun
945
946@deftypefun int rl_set_keyboard_input_timeout (int u)
947While waiting for keyboard input in @code{rl_read_key()}, Readline will
948wait for @var{u} microseconds for input before calling any function
949assigned to @code{rl_event_hook}. The default waiting period is
950one-tenth of a second. Returns the old timeout value.
726f6388
JA
951@end deftypefun
952
28ef6c31
JA
953@node Terminal Management
954@subsection Terminal Management
955
956@deftypefun void rl_prep_terminal (int meta_flag)
957Modify the terminal settings for Readline's use, so @code{readline()}
958can read a single character at a time from the keyboard.
959The @var{meta_flag} argument should be non-zero if Readline should
960read eight-bit input.
961@end deftypefun
962
963@deftypefun void rl_deprep_terminal (void)
964Undo the effects of @code{rl_prep_terminal()}, leaving the terminal in
965the state in which it was before the most recent call to
966@code{rl_prep_terminal()}.
967@end deftypefun
968
969@deftypefun void rl_tty_set_default_bindings (Keymap kmap)
970Read the operating system's terminal editing characters (as would be displayed
971by @code{stty}) to their Readline equivalents. The bindings are performed
972in @var{kmap}.
973@end deftypefun
974
975@deftypefun int rl_reset_terminal (const char *terminal_name)
726f6388
JA
976Reinitialize Readline's idea of the terminal settings using
977@var{terminal_name} as the terminal type (e.g., @code{vt100}).
28ef6c31 978If @var{terminal_name} is @code{NULL}, the value of the @code{TERM}
bb70624e 979environment variable is used.
726f6388
JA
980@end deftypefun
981
28ef6c31
JA
982@node Utility Functions
983@subsection Utility Functions
984
985@deftypefun int rl_extend_line_buffer (int len)
986Ensure that @code{rl_line_buffer} has enough space to hold @var{len}
987characters, possibly reallocating it if necessary.
726f6388
JA
988@end deftypefun
989
28ef6c31
JA
990@deftypefun int rl_initialize (void)
991Initialize or re-initialize Readline's internal state.
992It's not strictly necessary to call this; @code{readline()} calls it before
993reading any input.
726f6388
JA
994@end deftypefun
995
28ef6c31 996@deftypefun int rl_ding (void)
726f6388
JA
997Ring the terminal bell, obeying the setting of @code{bell-style}.
998@end deftypefun
999
28ef6c31
JA
1000@deftypefun int rl_alphabetic (int c)
1001Return 1 if @var{c} is an alphabetic character.
1002@end deftypefun
1003
b72432fd
JA
1004@deftypefun void rl_display_match_list (char **matches, int len, int max)
1005A convenience function for displaying a list of strings in
1006columnar format on Readline's output stream. @code{matches} is the list
1007of strings, in argv format, such as a list of completion matches.
1008@code{len} is the number of strings in @code{matches}, and @code{max}
1009is the length of the longest string in @code{matches}. This function uses
1010the setting of @code{print-completions-horizontally} to select how the
1011matches are displayed (@pxref{Readline Init File Syntax}).
1012@end deftypefun
1013
28ef6c31
JA
1014The following are implemented as macros, defined in @code{chardefs.h}.
1015Applications should refrain from using them.
726f6388 1016
28ef6c31 1017@deftypefun int _rl_uppercase_p (int c)
726f6388
JA
1018Return 1 if @var{c} is an uppercase alphabetic character.
1019@end deftypefun
1020
28ef6c31 1021@deftypefun int _rl_lowercase_p (int c)
726f6388
JA
1022Return 1 if @var{c} is a lowercase alphabetic character.
1023@end deftypefun
1024
28ef6c31 1025@deftypefun int _rl_digit_p (int c)
726f6388
JA
1026Return 1 if @var{c} is a numeric character.
1027@end deftypefun
1028
28ef6c31 1029@deftypefun int _rl_to_upper (int c)
726f6388
JA
1030If @var{c} is a lowercase alphabetic character, return the corresponding
1031uppercase character.
1032@end deftypefun
1033
28ef6c31 1034@deftypefun int _rl_to_lower (int c)
726f6388
JA
1035If @var{c} is an uppercase alphabetic character, return the corresponding
1036lowercase character.
1037@end deftypefun
1038
28ef6c31 1039@deftypefun int _rl_digit_value (int c)
726f6388
JA
1040If @var{c} is a number, return the value it represents.
1041@end deftypefun
1042
28ef6c31
JA
1043@node Miscellaneous Functions
1044@subsection Miscellaneous Functions
1045
1046@deftypefun int rl_macro_bind (const char *keyseq, const char *macro, Keymap map)
1047Bind the key sequence @var{keyseq} to invoke the macro @var{macro}.
1048The binding is performed in @var{map}. When @var{keyseq} is invoked, the
1049@var{macro} will be inserted into the line. This function is deprecated;
1050use @code{rl_generic_bind()} instead.
1051@end deftypefun
1052
1053@deftypefun void rl_macro_dumper (int readable)
1054Print the key sequences bound to macros and their values, using
1055the current keymap, to @code{rl_outstream}.
1056If @var{readable} is non-zero, the list is formatted in such a way
1057that it can be made part of an @code{inputrc} file and re-read.
1058@end deftypefun
1059
1060@deftypefun int rl_variable_bind (const char *variable, const char *value)
1061Make the Readline variable @var{variable} have @var{value}.
1062This behaves as if the readline command
1063@samp{set @var{variable} @var{value}} had been executed in an @code{inputrc}
1064file (@pxref{Readline Init File Syntax}).
1065@end deftypefun
1066
1067@deftypefun void rl_variable_dumper (int readable)
1068Print the readline variable names and their current values
1069to @code{rl_outstream}.
1070If @var{readable} is non-zero, the list is formatted in such a way
1071that it can be made part of an @code{inputrc} file and re-read.
1072@end deftypefun
1073
1074@deftypefun int rl_set_paren_blink_timeout (int u)
1075Set the time interval (in microseconds) that Readline waits when showing
1076a balancing character when @code{blink-matching-paren} has been enabled.
1077@end deftypefun
1078
ccc6cda3
JA
1079@node Alternate Interface
1080@subsection Alternate Interface
1081
1082An alternate interface is available to plain @code{readline()}. Some
1083applications need to interleave keyboard I/O with file, device, or
1084window system I/O, typically by using a main loop to @code{select()}
1085on various file descriptors. To accomodate this need, readline can
1086also be invoked as a `callback' function from an event loop. There
1087are functions available to make this easy.
1088
28ef6c31 1089@deftypefun void rl_callback_handler_install (const char *prompt, rl_vcpfunc_t *lhandler)
ccc6cda3
JA
1090Set up the terminal for readline I/O and display the initial
1091expanded value of @var{prompt}. Save the value of @var{lhandler} to
28ef6c31
JA
1092use as a function to call when a complete line of input has been entered.
1093The function takes the text of the line as an argument.
ccc6cda3
JA
1094@end deftypefun
1095
28ef6c31 1096@deftypefun void rl_callback_read_char (void)
ccc6cda3
JA
1097Whenever an application determines that keyboard input is available, it
1098should call @code{rl_callback_read_char()}, which will read the next
1099character from the current input source. If that character completes the
1100line, @code{rl_callback_read_char} will invoke the @var{lhandler}
1101function saved by @code{rl_callback_handler_install} to process the
1102line. @code{EOF} is indicated by calling @var{lhandler} with a
1103@code{NULL} line.
1104@end deftypefun
1105
28ef6c31 1106@deftypefun void rl_callback_handler_remove (void)
ccc6cda3
JA
1107Restore the terminal to its initial state and remove the line handler.
1108This may be called from within a callback as well as independently.
1109@end deftypefun
1110
28ef6c31
JA
1111@node A Readline Example
1112@subsection A Readline Example
726f6388
JA
1113
1114Here is a function which changes lowercase characters to their uppercase
1115equivalents, and uppercase characters to lowercase. If
1116this function was bound to @samp{M-c}, then typing @samp{M-c} would
1117change the case of the character under point. Typing @samp{M-1 0 M-c}
1118would change the case of the following 10 characters, leaving the cursor on
1119the last character changed.
1120
1121@example
1122/* Invert the case of the COUNT following characters. */
1123int
1124invert_case_line (count, key)
1125 int count, key;
1126@{
1127 register int start, end, i;
1128
1129 start = rl_point;
1130
1131 if (rl_point >= rl_end)
1132 return (0);
1133
1134 if (count < 0)
1135 @{
1136 direction = -1;
1137 count = -count;
1138 @}
1139 else
1140 direction = 1;
1141
1142 /* Find the end of the range to modify. */
1143 end = start + (count * direction);
1144
1145 /* Force it to be within range. */
1146 if (end > rl_end)
1147 end = rl_end;
1148 else if (end < 0)
1149 end = 0;
1150
1151 if (start == end)
1152 return (0);
1153
1154 if (start > end)
1155 @{
1156 int temp = start;
1157 start = end;
1158 end = temp;
1159 @}
1160
1161 /* Tell readline that we are modifying the line, so it will save
1162 the undo information. */
1163 rl_modifying (start, end);
1164
1165 for (i = start; i != end; i++)
1166 @{
28ef6c31
JA
1167 if (_rl_uppercase_p (rl_line_buffer[i]))
1168 rl_line_buffer[i] = _rl_to_lower (rl_line_buffer[i]);
1169 else if (_rl_lowercase_p (rl_line_buffer[i]))
1170 rl_line_buffer[i] = _rl_to_upper (rl_line_buffer[i]);
726f6388
JA
1171 @}
1172 /* Move point to on top of the last character changed. */
1173 rl_point = (direction == 1) ? end - 1 : start;
1174 return (0);
1175@}
1176@end example
1177
b72432fd
JA
1178@node Readline Signal Handling
1179@section Readline Signal Handling
1180
1181Signals are asynchronous events sent to a process by the Unix kernel,
1182sometimes on behalf of another process. They are intended to indicate
28ef6c31
JA
1183exceptional events, like a user pressing the interrupt key on his terminal,
1184or a network connection being broken. There is a class of signals that can
1185be sent to the process currently reading input from the keyboard. Since
1186Readline changes the terminal attributes when it is called, it needs to
1187perform special processing when such a signal is received in order to
1188restore the terminal to a sane state, or provide application writers with
1189functions to do so manually.
b72432fd
JA
1190
1191Readline contains an internal signal handler that is installed for a
1192number of signals (@code{SIGINT}, @code{SIGQUIT}, @code{SIGTERM},
1193@code{SIGALRM}, @code{SIGTSTP}, @code{SIGTTIN}, and @code{SIGTTOU}).
1194When one of these signals is received, the signal handler
1195will reset the terminal attributes to those that were in effect before
28ef6c31
JA
1196@code{readline()} was called, reset the signal handling to what it was
1197before @code{readline()} was called, and resend the signal to the calling
b72432fd
JA
1198application.
1199If and when the calling application's signal handler returns, Readline
1200will reinitialize the terminal and continue to accept input.
1201When a @code{SIGINT} is received, the Readline signal handler performs
1202some additional work, which will cause any partially-entered line to be
28ef6c31 1203aborted (see the description of @code{rl_free_line_state()} below).
b72432fd
JA
1204
1205There is an additional Readline signal handler, for @code{SIGWINCH}, which
1206the kernel sends to a process whenever the terminal's size changes (for
1207example, if a user resizes an @code{xterm}). The Readline @code{SIGWINCH}
28ef6c31
JA
1208handler updates Readline's internal screen size information, and then calls
1209any @code{SIGWINCH} signal handler the calling application has installed.
b72432fd
JA
1210Readline calls the application's @code{SIGWINCH} signal handler without
1211resetting the terminal to its original state. If the application's signal
1212handler does more than update its idea of the terminal size and return (for
1213example, a @code{longjmp} back to a main processing loop), it @emph{must}
28ef6c31 1214call @code{rl_cleanup_after_signal()} (described below), to restore the
b72432fd
JA
1215terminal state.
1216
1217Readline provides two variables that allow application writers to
1218control whether or not it will catch certain signals and act on them
1219when they are received. It is important that applications change the
28ef6c31 1220values of these variables only when calling @code{readline()}, not in
b72432fd
JA
1221a signal handler, so Readline's internal signal state is not corrupted.
1222
1223@deftypevar int rl_catch_signals
1224If this variable is non-zero, Readline will install signal handlers for
1225@code{SIGINT}, @code{SIGQUIT}, @code{SIGTERM}, @code{SIGALRM},
1226@code{SIGTSTP}, @code{SIGTTIN}, and @code{SIGTTOU}.
1227
1228The default value of @code{rl_catch_signals} is 1.
1229@end deftypevar
1230
1231@deftypevar int rl_catch_sigwinch
1232If this variable is non-zero, Readline will install a signal handler for
1233@code{SIGWINCH}.
1234
1235The default value of @code{rl_catch_sigwinch} is 1.
1236@end deftypevar
1237
1238If an application does not wish to have Readline catch any signals, or
1239to handle signals other than those Readline catches (@code{SIGHUP},
1240for example),
1241Readline provides convenience functions to do the necessary terminal
1242and internal state cleanup upon receipt of a signal.
1243
1244@deftypefun void rl_cleanup_after_signal (void)
1245This function will reset the state of the terminal to what it was before
28ef6c31 1246@code{readline()} was called, and remove the Readline signal handlers for
b72432fd
JA
1247all signals, depending on the values of @code{rl_catch_signals} and
1248@code{rl_catch_sigwinch}.
1249@end deftypefun
1250
1251@deftypefun void rl_free_line_state (void)
1252This will free any partial state associated with the current input line
1253(undo information, any partial history entry, any partially-entered
1254keyboard macro, and any partially-entered numeric argument). This
28ef6c31 1255should be called before @code{rl_cleanup_after_signal()}. The
b72432fd
JA
1256Readline signal handler for @code{SIGINT} calls this to abort the
1257current input line.
1258@end deftypefun
1259
1260@deftypefun void rl_reset_after_signal (void)
1261This will reinitialize the terminal and reinstall any Readline signal
1262handlers, depending on the values of @code{rl_catch_signals} and
1263@code{rl_catch_sigwinch}.
1264@end deftypefun
1265
1266If an application does not wish Readline to catch @code{SIGWINCH}, it may
28ef6c31
JA
1267call @code{rl_resize_terminal()} or @code{rl_set_screen_size()} to force
1268Readline to update its idea of the terminal size when a @code{SIGWINCH}
1269is received.
b72432fd
JA
1270
1271@deftypefun void rl_resize_terminal (void)
28ef6c31
JA
1272Update Readline's internal screen size by reading values from the kernel.
1273@end deftypefun
1274
1275@deftypefun void rl_set_screen_size (int rows, int cols)
1276Set Readline's idea of the terminal size to @var{rows} rows and
1277@var{cols} columns.
1278@end deftypefun
1279
1280If an application does not want to install a @code{SIGWINCH} handler, but
1281is still interested in the screen dimensions, Readline's idea of the screen
1282size may be queried.
1283
1284@deftypefun void rl_get_screen_size (int *rows, int *cols)
1285Return Readline's idea of the terminal's size in the
1286variables pointed to by the arguments.
b72432fd
JA
1287@end deftypefun
1288
1289The following functions install and remove Readline's signal handlers.
1290
1291@deftypefun int rl_set_signals (void)
1292Install Readline's signal handler for @code{SIGINT}, @code{SIGQUIT},
1293@code{SIGTERM}, @code{SIGALRM}, @code{SIGTSTP}, @code{SIGTTIN},
1294@code{SIGTTOU}, and @code{SIGWINCH}, depending on the values of
1295@code{rl_catch_signals} and @code{rl_catch_sigwinch}.
1296@end deftypefun
1297
1298@deftypefun int rl_clear_signals (void)
1299Remove all of the Readline signal handlers installed by
28ef6c31 1300@code{rl_set_signals()}.
b72432fd
JA
1301@end deftypefun
1302
726f6388
JA
1303@node Custom Completers
1304@section Custom Completers
1305
1306Typically, a program that reads commands from the user has a way of
1307disambiguating commands and data. If your program is one of these, then
1308it can provide completion for commands, data, or both.
1309The following sections describe how your program and Readline
1310cooperate to provide this service.
1311
1312@menu
1313* How Completing Works:: The logic used to do completion.
1314* Completion Functions:: Functions provided by Readline.
1315* Completion Variables:: Variables which control completion.
1316* A Short Completion Example:: An example of writing completer subroutines.
1317@end menu
1318
1319@node How Completing Works
1320@subsection How Completing Works
1321
1322In order to complete some text, the full list of possible completions
1323must be available. That is, it is not possible to accurately
1324expand a partial word without knowing all of the possible words
1325which make sense in that context. The Readline library provides
1326the user interface to completion, and two of the most common
1327completion functions: filename and username. For completing other types
1328of text, you must write your own completion function. This section
1329describes exactly what such functions must do, and provides an example.
1330
1331There are three major functions used to perform completion:
1332
1333@enumerate
1334@item
28ef6c31
JA
1335The user-interface function @code{rl_complete()}. This function is
1336called with the same arguments as other bindable Readline functions:
1337@var{count} and @var{invoking_key}.
1338It isolates the word to be completed and calls
1339@code{rl_completion_matches()} to generate a list of possible completions.
726f6388
JA
1340It then either lists the possible completions, inserts the possible
1341completions, or actually performs the
1342completion, depending on which behavior is desired.
1343
1344@item
28ef6c31
JA
1345The internal function @code{rl_completion_matches()} uses an
1346application-supplied @dfn{generator} function to generate the list of
1347possible matches, and then returns the array of these matches.
1348The caller should place the address of its generator function in
1349@code{rl_completion_entry_function}.
726f6388
JA
1350
1351@item
1352The generator function is called repeatedly from
28ef6c31 1353@code{rl_completion_matches()}, returning a string each time. The
726f6388
JA
1354arguments to the generator function are @var{text} and @var{state}.
1355@var{text} is the partial word to be completed. @var{state} is zero the
1356first time the function is called, allowing the generator to perform
1357any necessary initialization, and a positive non-zero integer for
28ef6c31
JA
1358each subsequent call. The generator function returns
1359@code{(char *)NULL} to inform @code{rl_completion_matches()} that there are
726f6388
JA
1360no more possibilities left. Usually the generator function computes the
1361list of possible completions when @var{state} is zero, and returns them
1362one at a time on subsequent calls. Each string the generator function
1363returns as a match must be allocated with @code{malloc()}; Readline
1364frees the strings when it has finished with them.
1365
1366@end enumerate
1367
1368@deftypefun int rl_complete (int ignore, int invoking_key)
1369Complete the word at or before point. You have supplied the function
1370that does the initial simple matching selection algorithm (see
28ef6c31 1371@code{rl_completion_matches()}). The default is to do filename completion.
726f6388
JA
1372@end deftypefun
1373
28ef6c31
JA
1374@deftypevar {rl_compentry_func_t *} rl_completion_entry_function
1375This is a pointer to the generator function for
1376@code{rl_completion_matches()}.
1377If the value of @code{rl_completion_entry_function} is
1378@code{NULL} then the default filename generator
1379function, @code{rl_filename_completion_function()}, is used.
726f6388
JA
1380@end deftypevar
1381
1382@node Completion Functions
1383@subsection Completion Functions
1384
1385Here is the complete list of callable completion functions present in
1386Readline.
1387
1388@deftypefun int rl_complete_internal (int what_to_do)
1389Complete the word at or before point. @var{what_to_do} says what to do
1390with the completion. A value of @samp{?} means list the possible
1391completions. @samp{TAB} means do standard completion. @samp{*} means
1392insert all of the possible completions. @samp{!} means to display
1393all of the possible completions, if there is more than one, as well as
1394performing partial completion.
1395@end deftypefun
1396
1397@deftypefun int rl_complete (int ignore, int invoking_key)
1398Complete the word at or before point. You have supplied the function
1399that does the initial simple matching selection algorithm (see
28ef6c31 1400@code{rl_completion_matches()} and @code{rl_completion_entry_function}).
726f6388 1401The default is to do filename
28ef6c31 1402completion. This calls @code{rl_complete_internal()} with an
726f6388
JA
1403argument depending on @var{invoking_key}.
1404@end deftypefun
1405
28ef6c31 1406@deftypefun int rl_possible_completions (int count, int invoking_key)
726f6388 1407List the possible completions. See description of @code{rl_complete
28ef6c31 1408()}. This calls @code{rl_complete_internal()} with an argument of
726f6388
JA
1409@samp{?}.
1410@end deftypefun
1411
28ef6c31 1412@deftypefun int rl_insert_completions (int count, int invoking_key)
726f6388 1413Insert the list of possible completions into the line, deleting the
28ef6c31
JA
1414partially-completed word. See description of @code{rl_complete()}.
1415This calls @code{rl_complete_internal()} with an argument of @samp{*}.
726f6388
JA
1416@end deftypefun
1417
28ef6c31
JA
1418@deftypefun {char **} rl_completion_matches (const char *text, rl_compentry_func_t *entry_func)
1419Returns an array of strings which is a list of completions for
1420@var{text}. If there are no completions, returns @code{NULL}.
726f6388
JA
1421The first entry in the returned array is the substitution for @var{text}.
1422The remaining entries are the possible completions. The array is
1423terminated with a @code{NULL} pointer.
1424
1425@var{entry_func} is a function of two args, and returns a
28ef6c31 1426@code{char *}. The first argument is @var{text}. The second is a
726f6388
JA
1427state argument; it is zero on the first call, and non-zero on subsequent
1428calls. @var{entry_func} returns a @code{NULL} pointer to the caller
1429when there are no more matches.
1430@end deftypefun
1431
28ef6c31
JA
1432@deftypefun {char *} rl_filename_completion_function (const char *text, int state)
1433A generator function for filename completion in the general case.
1434@var{text} is a partial filename.
1435The Bash source is a useful reference for writing custom
1436completion functions (the Bash completion functions call this and other
1437Readline functions).
726f6388
JA
1438@end deftypefun
1439
28ef6c31 1440@deftypefun {char *} rl_username_completion_function (const char *text, int state)
726f6388
JA
1441A completion generator for usernames. @var{text} contains a partial
1442username preceded by a random character (usually @samp{~}). As with all
1443completion generators, @var{state} is zero on the first call and non-zero
1444for subsequent calls.
1445@end deftypefun
1446
1447@node Completion Variables
1448@subsection Completion Variables
1449
28ef6c31
JA
1450@deftypevar {rl_compentry_func_t *} rl_completion_entry_function
1451A pointer to the generator function for @code{rl_completion_matches()}.
1452@code{NULL} means to use @code{rl_filename_completion_function()}, the default
726f6388
JA
1453filename completer.
1454@end deftypevar
1455
28ef6c31 1456@deftypevar {rl_completion_func_t *} rl_attempted_completion_function
726f6388
JA
1457A pointer to an alternative function to create matches.
1458The function is called with @var{text}, @var{start}, and @var{end}.
28ef6c31
JA
1459@var{start} and @var{end} are indices in @code{rl_line_buffer} defining
1460the boundaries of @var{text}, which is a character string.
1461If this function exists and returns @code{NULL}, or if this variable is
1462set to @code{NULL}, then @code{rl_complete()} will call the value of
726f6388
JA
1463@code{rl_completion_entry_function} to generate matches, otherwise the
1464array of strings returned will be used.
28ef6c31
JA
1465If this function sets the @code{rl_attempted_completion_over}
1466variable to a non-zero value, Readline will not perform its default
1467completion even if this function returns no matches.
726f6388
JA
1468@end deftypevar
1469
28ef6c31
JA
1470@deftypevar {rl_quote_func_t *} rl_filename_quoting_function
1471A pointer to a function that will quote a filename in an
1472application-specific fashion. This is called if filename completion is being
ccc6cda3
JA
1473attempted and one of the characters in @code{rl_filename_quote_characters}
1474appears in a completed filename. The function is called with
1475@var{text}, @var{match_type}, and @var{quote_pointer}. The @var{text}
1476is the filename to be quoted. The @var{match_type} is either
1477@code{SINGLE_MATCH}, if there is only one completion match, or
1478@code{MULT_MATCH}. Some functions use this to decide whether or not to
1479insert a closing quote character. The @var{quote_pointer} is a pointer
1480to any opening quote character the user typed. Some functions choose
1481to reset this character.
1482@end deftypevar
1483
28ef6c31 1484@deftypevar {rl_dequote_func_t *} rl_filename_dequoting_function
ccc6cda3
JA
1485A pointer to a function that will remove application-specific quoting
1486characters from a filename before completion is attempted, so those
1487characters do not interfere with matching the text against names in
1488the filesystem. It is called with @var{text}, the text of the word
1489to be dequoted, and @var{quote_char}, which is the quoting character
1490that delimits the filename (usually @samp{'} or @samp{"}). If
1491@var{quote_char} is zero, the filename was not in an embedded string.
1492@end deftypevar
1493
28ef6c31 1494@deftypevar {rl_linebuf_func_t *} rl_char_is_quoted_p
ccc6cda3
JA
1495A pointer to a function to call that determines whether or not a specific
1496character in the line buffer is quoted, according to whatever quoting
28ef6c31 1497mechanism the program calling Readline uses. The function is called with
ccc6cda3
JA
1498two arguments: @var{text}, the text of the line, and @var{index}, the
1499index of the character in the line. It is used to decide whether a
1500character found in @code{rl_completer_word_break_characters} should be
1501used to break words for the completer.
1502@end deftypevar
1503
726f6388
JA
1504@deftypevar int rl_completion_query_items
1505Up to this many items will be displayed in response to a
1506possible-completions call. After that, we ask the user if she is sure
1507she wants to see them all. The default value is 100.
1508@end deftypevar
1509
28ef6c31 1510@deftypevar {const char *} rl_basic_word_break_characters
726f6388
JA
1511The basic list of characters that signal a break between words for the
1512completer routine. The default value of this variable is the characters
28ef6c31 1513which break words for completion in Bash:
726f6388
JA
1514@code{" \t\n\"\\'`@@$><=;|&@{("}.
1515@end deftypevar
1516
28ef6c31
JA
1517@deftypevar {const char *} rl_basic_quote_characters
1518A list of quote characters which can cause a word break.
ccc6cda3
JA
1519@end deftypevar
1520
28ef6c31 1521@deftypevar {const char *} rl_completer_word_break_characters
726f6388 1522The list of characters that signal a break between words for
28ef6c31 1523@code{rl_complete_internal()}. The default list is the value of
726f6388
JA
1524@code{rl_basic_word_break_characters}.
1525@end deftypevar
1526
28ef6c31
JA
1527@deftypevar {const char *} rl_completer_quote_characters
1528A list of characters which can be used to quote a substring of the line.
726f6388
JA
1529Completion occurs on the entire substring, and within the substring
1530@code{rl_completer_word_break_characters} are treated as any other character,
1531unless they also appear within this list.
1532@end deftypevar
1533
28ef6c31 1534@deftypevar {const char *} rl_filename_quote_characters
ccc6cda3 1535A list of characters that cause a filename to be quoted by the completer
cce855bc 1536when they appear in a completed filename. The default is the null string.
ccc6cda3
JA
1537@end deftypevar
1538
28ef6c31 1539@deftypevar {const char *} rl_special_prefixes
726f6388
JA
1540The list of characters that are word break characters, but should be
1541left in @var{text} when it is passed to the completion function.
1542Programs can use this to help determine what kind of completing to do.
1543For instance, Bash sets this variable to "$@@" so that it can complete
1544shell variables and hostnames.
1545@end deftypevar
1546
ccc6cda3
JA
1547@deftypevar {int} rl_completion_append_character
1548When a single completion alternative matches at the end of the command
1549line, this character is appended to the inserted completion text. The
1550default is a space character (@samp{ }). Setting this to the null
1551character (@samp{\0}) prevents anything being appended automatically.
1552This can be changed in custom completion functions to
1553provide the ``most sensible word separator character'' according to
1554an application-specific command line syntax specification.
1555@end deftypevar
1556
726f6388 1557@deftypevar int rl_ignore_completion_duplicates
28ef6c31
JA
1558If non-zero, then duplicates in the matches are removed.
1559The default is 1.
726f6388
JA
1560@end deftypevar
1561
1562@deftypevar int rl_filename_completion_desired
1563Non-zero means that the results of the matches are to be treated as
1564filenames. This is @emph{always} zero on entry, and can only be changed
1565within a completion entry generator function. If it is set to a non-zero
1566value, directory names have a slash appended and Readline attempts to
28ef6c31
JA
1567quote completed filenames if they contain any characters in
1568@code{rl_filename_quote_characters} and @code{rl_filename_quoting_desired}
1569is set to a non-zero value.
726f6388
JA
1570@end deftypevar
1571
1572@deftypevar int rl_filename_quoting_desired
1573Non-zero means that the results of the matches are to be quoted using
1574double quotes (or an application-specific quoting mechanism) if the
1575completed filename contains any characters in
ccc6cda3 1576@code{rl_filename_quote_chars}. This is @emph{always} non-zero
726f6388 1577on entry, and can only be changed within a completion entry generator
ccc6cda3
JA
1578function. The quoting is effected via a call to the function pointed to
1579by @code{rl_filename_quoting_function}.
1580@end deftypevar
1581
28ef6c31
JA
1582@deftypevar int rl_attempted_completion_over
1583If an application-specific completion function assigned to
1584@code{rl_attempted_completion_function} sets this variable to a non-zero
1585value, Readline will not perform its default filename completion even
1586if the application's completion function returns no matches.
1587It should be set only by an application's completion function.
1588@end deftypevar
1589
1590@deftypevar int rl_completion_type
1591Set to a character describing the type of completion Readline is currently
1592attempting; see the description of @code{rl_complete_internal()}
1593(@pxref{Completion Functions}) for the list of characters.
1594@end deftypevar
1595
ccc6cda3 1596@deftypevar int rl_inhibit_completion
28ef6c31 1597If this variable is non-zero, completion is inhibited. The completion
ccc6cda3 1598character will be inserted as any other bound to @code{self-insert}.
726f6388
JA
1599@end deftypevar
1600
28ef6c31 1601@deftypevar {rl_compignore_func_t *} rl_ignore_some_completions_function
726f6388
JA
1602This function, if defined, is called by the completer when real filename
1603completion is done, after all the matching names have been generated.
1604It is passed a @code{NULL} terminated array of matches.
1605The first element (@code{matches[0]}) is the
1606maximal substring common to all matches. This function can
1607re-arrange the list of matches as required, but each element deleted
1608from the array must be freed.
1609@end deftypevar
1610
28ef6c31 1611@deftypevar {rl_icppfunc_t *} rl_directory_completion_hook
726f6388
JA
1612This function, if defined, is allowed to modify the directory portion
1613of filenames Readline completes. It is called with the address of a
28ef6c31
JA
1614string (the current directory name) as an argument, and may modify that string.
1615If the string is replaced with a new string, the old value should be freed.
1616Any modified directory name should have a trailing slash.
1617The modified value will be displayed as part of the completion, replacing
1618the directory portion of the pathname the user typed.
1619It returns an integer that should be non-zero if the function modifies
1620its directory argument.
1621It could be used to expand symbolic links or shell variables in pathnames.
726f6388 1622@end deftypevar
b72432fd 1623
28ef6c31 1624@deftypevar {rl_compdisp_func_t *} rl_completion_display_matches_hook
b72432fd
JA
1625If non-zero, then this is the address of a function to call when
1626completing a word would normally display the list of possible matches.
1627This function is called in lieu of Readline displaying the list.
1628It takes three arguments:
1629(@code{char **}@var{matches}, @code{int} @var{num_matches}, @code{int} @var{max_length})
1630where @var{matches} is the array of matching strings,
1631@var{num_matches} is the number of strings in that array, and
1632@var{max_length} is the length of the longest string in that array.
1633Readline provides a convenience function, @code{rl_display_match_list},
1634that takes care of doing the display to Readline's output stream. That
1635function may be called from this hook.
1636@end deftypevar
726f6388
JA
1637
1638@node A Short Completion Example
1639@subsection A Short Completion Example
1640
1641Here is a small application demonstrating the use of the GNU Readline
1642library. It is called @code{fileman}, and the source code resides in
1643@file{examples/fileman.c}. This sample application provides
1644completion of command names, line editing features, and access to the
1645history list.
1646
1647@page
1648@smallexample
1649/* fileman.c -- A tiny application which demonstrates how to use the
1650 GNU Readline library. This application interactively allows users
1651 to manipulate files and their modes. */
1652
1653#include <stdio.h>
1654#include <sys/types.h>
1655#include <sys/file.h>
1656#include <sys/stat.h>
1657#include <sys/errno.h>
1658
1659#include <readline/readline.h>
1660#include <readline/history.h>
1661
726f6388
JA
1662extern char *xmalloc ();
1663
1664/* The names of functions that actually do the manipulation. */
28ef6c31
JA
1665int com_list __P((char *));
1666int com_view __P((char *));
1667int com_rename __P((char *));
1668int com_stat __P((char *));
1669int com_pwd __P((char *));
1670int com_delete __P((char *));
1671int com_help __P((char *));
1672int com_cd __P((char *));
1673int com_quit __P((char *));
726f6388
JA
1674
1675/* A structure which contains information on the commands this program
1676 can understand. */
1677
1678typedef struct @{
1679 char *name; /* User printable name of the function. */
28ef6c31 1680 rl_icpfunc_t *func; /* Function to call to do the job. */
726f6388
JA
1681 char *doc; /* Documentation for this function. */
1682@} COMMAND;
1683
1684COMMAND commands[] = @{
1685 @{ "cd", com_cd, "Change to directory DIR" @},
1686 @{ "delete", com_delete, "Delete FILE" @},
1687 @{ "help", com_help, "Display this text" @},
1688 @{ "?", com_help, "Synonym for `help'" @},
1689 @{ "list", com_list, "List files in DIR" @},
1690 @{ "ls", com_list, "Synonym for `list'" @},
1691 @{ "pwd", com_pwd, "Print the current working directory" @},
1692 @{ "quit", com_quit, "Quit using Fileman" @},
1693 @{ "rename", com_rename, "Rename FILE to NEWNAME" @},
1694 @{ "stat", com_stat, "Print out statistics on FILE" @},
1695 @{ "view", com_view, "View the contents of FILE" @},
28ef6c31 1696 @{ (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL @}
726f6388
JA
1697@};
1698
1699/* Forward declarations. */
1700char *stripwhite ();
1701COMMAND *find_command ();
1702
1703/* The name of this program, as taken from argv[0]. */
1704char *progname;
1705
28ef6c31 1706/* When non-zero, this means the user is done using this program. */
726f6388
JA
1707int done;
1708
1709char *
1710dupstr (s)
1711 int s;
1712@{
1713 char *r;
1714
1715 r = xmalloc (strlen (s) + 1);
1716 strcpy (r, s);
1717 return (r);
1718@}
1719
1720main (argc, argv)
1721 int argc;
1722 char **argv;
1723@{
1724 char *line, *s;
1725
1726 progname = argv[0];
1727
1728 initialize_readline (); /* Bind our completer. */
1729
1730 /* Loop reading and executing lines until the user quits. */
1731 for ( ; done == 0; )
1732 @{
1733 line = readline ("FileMan: ");
1734
1735 if (!line)
1736 break;
1737
1738 /* Remove leading and trailing whitespace from the line.
1739 Then, if there is anything left, add it to the history list
1740 and execute it. */
1741 s = stripwhite (line);
1742
1743 if (*s)
1744 @{
1745 add_history (s);
1746 execute_line (s);
1747 @}
1748
1749 free (line);
1750 @}
1751 exit (0);
1752@}
1753
1754/* Execute a command line. */
1755int
1756execute_line (line)
1757 char *line;
1758@{
1759 register int i;
1760 COMMAND *command;
1761 char *word;
1762
1763 /* Isolate the command word. */
1764 i = 0;
1765 while (line[i] && whitespace (line[i]))
1766 i++;
1767 word = line + i;
1768
1769 while (line[i] && !whitespace (line[i]))
1770 i++;
1771
1772 if (line[i])
1773 line[i++] = '\0';
1774
1775 command = find_command (word);
1776
1777 if (!command)
1778 @{
1779 fprintf (stderr, "%s: No such command for FileMan.\n", word);
1780 return (-1);
1781 @}
1782
1783 /* Get argument to command, if any. */
1784 while (whitespace (line[i]))
1785 i++;
1786
1787 word = line + i;
1788
1789 /* Call the function. */
1790 return ((*(command->func)) (word));
1791@}
1792
1793/* Look up NAME as the name of a command, and return a pointer to that
1794 command. Return a NULL pointer if NAME isn't a command name. */
1795COMMAND *
1796find_command (name)
1797 char *name;
1798@{
1799 register int i;
1800
1801 for (i = 0; commands[i].name; i++)
1802 if (strcmp (name, commands[i].name) == 0)
1803 return (&commands[i]);
1804
1805 return ((COMMAND *)NULL);
1806@}
1807
1808/* Strip whitespace from the start and end of STRING. Return a pointer
1809 into STRING. */
1810char *
1811stripwhite (string)
1812 char *string;
1813@{
1814 register char *s, *t;
1815
1816 for (s = string; whitespace (*s); s++)
1817 ;
1818
1819 if (*s == 0)
1820 return (s);
1821
1822 t = s + strlen (s) - 1;
1823 while (t > s && whitespace (*t))
1824 t--;
1825 *++t = '\0';
1826
1827 return s;
1828@}
1829
1830/* **************************************************************** */
1831/* */
1832/* Interface to Readline Completion */
1833/* */
1834/* **************************************************************** */
1835
28ef6c31
JA
1836char *command_generator __P((const char *, int));
1837char **fileman_completion __P((const char *, int, int));
726f6388 1838
28ef6c31
JA
1839/* Tell the GNU Readline library how to complete. We want to try to
1840 complete on command names if this is the first word in the line, or
1841 on filenames if not. */
726f6388
JA
1842initialize_readline ()
1843@{
1844 /* Allow conditional parsing of the ~/.inputrc file. */
1845 rl_readline_name = "FileMan";
1846
1847 /* Tell the completer that we want a crack first. */
28ef6c31 1848 rl_attempted_completion_function = fileman_completion;
726f6388
JA
1849@}
1850
28ef6c31
JA
1851/* Attempt to complete on the contents of TEXT. START and END
1852 bound the region of rl_line_buffer that contains the word to
1853 complete. TEXT is the word to complete. We can use the entire
1854 contents of rl_line_buffer in case we want to do some simple
1855 parsing. Returnthe array of matches, or NULL if there aren't any. */
726f6388
JA
1856char **
1857fileman_completion (text, start, end)
28ef6c31 1858 const char *text;
726f6388
JA
1859 int start, end;
1860@{
1861 char **matches;
1862
1863 matches = (char **)NULL;
1864
1865 /* If this word is at the start of the line, then it is a command
1866 to complete. Otherwise it is the name of a file in the current
1867 directory. */
1868 if (start == 0)
28ef6c31 1869 matches = rl_completion_matches (text, command_generator);
726f6388
JA
1870
1871 return (matches);
1872@}
1873
28ef6c31
JA
1874/* Generator function for command completion. STATE lets us
1875 know whether to start from scratch; without any state
1876 (i.e. STATE == 0), then we start at the top of the list. */
726f6388
JA
1877char *
1878command_generator (text, state)
28ef6c31 1879 const char *text;
726f6388
JA
1880 int state;
1881@{
1882 static int list_index, len;
1883 char *name;
1884
28ef6c31
JA
1885 /* If this is a new word to complete, initialize now. This
1886 includes saving the length of TEXT for efficiency, and
1887 initializing the index variable to 0. */
726f6388
JA
1888 if (!state)
1889 @{
1890 list_index = 0;
1891 len = strlen (text);
1892 @}
1893
28ef6c31
JA
1894 /* Return the next name which partially matches from the
1895 command list. */
726f6388
JA
1896 while (name = commands[list_index].name)
1897 @{
1898 list_index++;
1899
1900 if (strncmp (name, text, len) == 0)
1901 return (dupstr(name));
1902 @}
1903
1904 /* If no names matched, then return NULL. */
1905 return ((char *)NULL);
1906@}
1907
1908/* **************************************************************** */
1909/* */
1910/* FileMan Commands */
1911/* */
1912/* **************************************************************** */
1913
1914/* String to pass to system (). This is for the LIST, VIEW and RENAME
1915 commands. */
1916static char syscom[1024];
1917
1918/* List the file(s) named in arg. */
1919com_list (arg)
1920 char *arg;
1921@{
1922 if (!arg)
1923 arg = "";
1924
1925 sprintf (syscom, "ls -FClg %s", arg);
1926 return (system (syscom));
1927@}
1928
1929com_view (arg)
1930 char *arg;
1931@{
1932 if (!valid_argument ("view", arg))
1933 return 1;
1934
1935 sprintf (syscom, "more %s", arg);
1936 return (system (syscom));
1937@}
1938
1939com_rename (arg)
1940 char *arg;
1941@{
1942 too_dangerous ("rename");
1943 return (1);
1944@}
1945
1946com_stat (arg)
1947 char *arg;
1948@{
1949 struct stat finfo;
1950
1951 if (!valid_argument ("stat", arg))
1952 return (1);
1953
1954 if (stat (arg, &finfo) == -1)
1955 @{
1956 perror (arg);
1957 return (1);
1958 @}
1959
1960 printf ("Statistics for `%s':\n", arg);
1961
1962 printf ("%s has %d link%s, and is %d byte%s in length.\n", arg,
1963 finfo.st_nlink,
1964 (finfo.st_nlink == 1) ? "" : "s",
1965 finfo.st_size,
1966 (finfo.st_size == 1) ? "" : "s");
1967 printf ("Inode Last Change at: %s", ctime (&finfo.st_ctime));
1968 printf (" Last access at: %s", ctime (&finfo.st_atime));
1969 printf (" Last modified at: %s", ctime (&finfo.st_mtime));
1970 return (0);
1971@}
1972
1973com_delete (arg)
1974 char *arg;
1975@{
1976 too_dangerous ("delete");
1977 return (1);
1978@}
1979
1980/* Print out help for ARG, or for all of the commands if ARG is
1981 not present. */
1982com_help (arg)
1983 char *arg;
1984@{
1985 register int i;
1986 int printed = 0;
1987
1988 for (i = 0; commands[i].name; i++)
1989 @{
1990 if (!*arg || (strcmp (arg, commands[i].name) == 0))
1991 @{
1992 printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
1993 printed++;
1994 @}
1995 @}
1996
1997 if (!printed)
1998 @{
1999 printf ("No commands match `%s'. Possibilties are:\n", arg);
2000
2001 for (i = 0; commands[i].name; i++)
2002 @{
2003 /* Print in six columns. */
2004 if (printed == 6)
2005 @{
2006 printed = 0;
2007 printf ("\n");
2008 @}
2009
2010 printf ("%s\t", commands[i].name);
2011 printed++;
2012 @}
2013
2014 if (printed)
2015 printf ("\n");
2016 @}
2017 return (0);
2018@}
2019
2020/* Change to the directory ARG. */
2021com_cd (arg)
2022 char *arg;
2023@{
2024 if (chdir (arg) == -1)
2025 @{
2026 perror (arg);
2027 return 1;
2028 @}
2029
2030 com_pwd ("");
2031 return (0);
2032@}
2033
2034/* Print out the current working directory. */
2035com_pwd (ignore)
2036 char *ignore;
2037@{
2038 char dir[1024], *s;
2039
28ef6c31 2040 s = getcwd (dir, sizeof(dir) - 1);
726f6388
JA
2041 if (s == 0)
2042 @{
2043 printf ("Error getting pwd: %s\n", dir);
2044 return 1;
2045 @}
2046
2047 printf ("Current directory is %s\n", dir);
2048 return 0;
2049@}
2050
28ef6c31
JA
2051/* The user wishes to quit using this program. Just set DONE
2052 non-zero. */
726f6388
JA
2053com_quit (arg)
2054 char *arg;
2055@{
2056 done = 1;
2057 return (0);
2058@}
2059
2060/* Function which tells you that you can't do this. */
2061too_dangerous (caller)
2062 char *caller;
2063@{
2064 fprintf (stderr,
2065 "%s: Too dangerous for me to distribute. Write it yourself.\n",
2066 caller);
2067@}
2068
2069/* Return non-zero if ARG is a valid argument for CALLER, else print
2070 an error message and return zero. */
2071int
2072valid_argument (caller, arg)
2073 char *caller, *arg;
2074@{
2075 if (!arg || !*arg)
2076 @{
2077 fprintf (stderr, "%s: Argument required.\n", caller);
2078 return (0);
2079 @}
2080
2081 return (1);
2082@}
2083@end smallexample