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