⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rltech.texinfo

📁 linux下bash的源码
💻 TEXINFO
📖 第 1 页 / 共 3 页
字号:
@comment %**start of header (This is for running Texinfo on a region.)@setfilename rltech.info@comment %**end of header (This is for running Texinfo on a region.)@setchapternewpage odd@ifinfoThis document describes the GNU Readline Library, a utility for aidingin the consitency of user interface across discrete programs that needto provide a command line interface.Copyright (C) 1988, 1994 Free Software Foundation, Inc.Permission is granted to make and distribute verbatim copies ofthis manual provided the copyright notice and this permission noticepare preserved on all copies.@ignorePermission is granted to process this file through TeX and print theresults, provided the printed document carries copying permissionnotice identical to this one except for the removal of this paragraph(this paragraph not being relevant to the printed manual).@end ignorePermission is granted to copy and distribute modified versions of thismanual under the conditions for verbatim copying, provided that the entireresulting derived work is distributed under the terms of a permissionnotice identical to this one.Permission is granted to copy and distribute translations of this manualinto another language, under the above conditions for modified versions,except that this permission notice may be stated in a translation approvedby the Foundation.@end ifinfo@node Programming with GNU Readline@chapter Programming with GNU ReadlineThis chapter describes the interface between the GNU Readline Library andother programs.  If you are a programmer, and you wish to include thefeatures found in GNU Readlinesuch as completion, line editing, and interactive history manipulationin your own programs, this section is for you.@menu* Basic Behavior::	Using the default behavior of Readline.* Custom Functions::	Adding your own functions to Readline.* Readline Variables::			Variables accessible to custom					functions.* Readline Convenience Functions::	Functions which Readline supplies to					aid in writing your own* Custom Completers::	Supplanting or supplementing Readline's			completion functions.@end menu@node Basic Behavior@section Basic BehaviorMany programs provide a command line interface, such as @code{mail},@code{ftp}, and @code{sh}.  For such programs, the default behaviour ofReadline is sufficient.  This section describes how to use Readline inthe simplest way possible, perhaps to replace calls in your code to@code{gets()} or @code{fgets ()}.@findex readline@cindex readline, functionThe function @code{readline ()} prints a prompt and then reads and returnsa single line of text from the user.  The line @code{readline}returns is allocated with @code{malloc ()}; you should @code{free ()}the line when you are done with it.  The declaration for @code{readline}in ANSI C is@example@code{char *readline (char *@var{prompt});}@end example@noindentSo, one might say@example@code{char *line = readline ("Enter a line: ");}@end example@noindentin order to read a line of text from the user.The line returned has the final newline removed, so only thetext remains.If @code{readline} encounters an @code{EOF} while reading the line, and theline is empty at that point, then @code{(char *)NULL} is returned.Otherwise, the line is ended just as if a newline had been typed.If you want the user to be able to get at the line later, (with@key{C-p} for example), you must call @code{add_history ()} to save theline away in a @dfn{history} list of such lines.@example@code{add_history (line)};@end example@noindentFor full details on the GNU History Library, see the associated manual.It is preferable to avoid saving empty lines on the history list, sinceusers rarely have a burning need to reuse a blank line.  Here isa function which usefully replaces the standard @code{gets ()} libraryfunction, and has the advantage of no static buffer to overflow:@example/* A static variable for holding the line. */static char *line_read = (char *)NULL;/* Read a string, and return a pointer to it.  Returns NULL on EOF. */char *rl_gets ()@{  /* If the buffer has already been allocated, return the memory     to the free pool. */  if (line_read)    @{      free (line_read);      line_read = (char *)NULL;    @}  /* Get a line from the user. */  line_read = readline ("");  /* If the line has any text in it, save it on the history. */  if (line_read && *line_read)    add_history (line_read);  return (line_read);@}@end exampleThis function gives the user the default behaviour of @key{TAB}completion: completion on file names.  If you do not want Readline tocomplete on filenames, you can change the binding of the @key{TAB} keywith @code{rl_bind_key ()}.@example@code{int rl_bind_key (int @var{key}, int (*@var{function})());}@end example@code{rl_bind_key ()} takes two arguments: @var{key} is the character thatyou want to bind, and @var{function} is the address of the function tocall when @var{key} is pressed.  Binding @key{TAB} to @code{rl_insert ()}makes @key{TAB} insert itself.@code{rl_bind_key ()} returns non-zero if @var{key} is not a validASCII character code (between 0 and 255).Thus, to disable the default @key{TAB} behavior, the following suffices:@example@code{rl_bind_key ('\t', rl_insert);}@end exampleThis code should be executed once at the start of your program; youmight write a function called @code{initialize_readline ()} whichperforms this and other desired initializations, such as installingcustom completers (@pxref{Custom Completers}).@node Custom Functions@section Custom FunctionsReadline provides many functions for manipulating the text ofthe line, but it isn't possible to anticipate the needs of allprograms.  This section describes the various functions and variablesdefined within the Readline library which allow a user program to addcustomized functionality to Readline.@menu* The Function Type::	C declarations to make code readable.* Function Writing::	Variables and calling conventions.@end menu@node The Function Type@subsection The Function TypeFor readabilty, we declare a new type of object, called@dfn{Function}.  A @code{Function} is a C function whichreturns an @code{int}.  The type declaration for @code{Function} is:@noindent@code{typedef int Function ();}The reason for declaring this new type is to make it easier to writecode describing pointers to C functions.  Let us say we had a variablecalled @var{func} which was a pointer to a function.  Instead of theclassic C declaration@code{int (*)()func;}@noindentwe may write@code{Function *func;}@noindentSimilarly, there are@exampletypedef void VFunction ();typedef char *CPFunction (); @r{and}typedef char **CPPFunction ();@end example@noindentfor functions returning no value, @code{pointer to char}, and@code{pointer to pointer to char}, respectively.@node Function Writing@subsection Writing a New FunctionIn order to write new functions for Readline, you need to know thecalling conventions for keyboard-invoked functions, and the names of thevariables that describe the current state of the line read so far.The calling sequence for a command @code{foo} looks like@example@code{foo (int count, int key)}@end example@noindentwhere @var{count} is the numeric argument (or 1 if defaulted) and@var{key} is the key that invoked this function.It is completely up to the function as to what should be done with thenumeric argument.  Some functions use it as a repeat count, someas a flag, and others to choose alternate behavior (refreshing the currentline as opposed to refreshing the screen, for example).  Some choose toignore it.  In general, if afunction uses the numeric argument as a repeat count, it should be ableto do something useful with both negative and positive arguments.At the very least, it should be aware that it can be passed anegative argument.@node Readline Variables@section Readline VariablesThese variables are available to function writers.@deftypevar {char *} rl_line_bufferThis is the line gathered so far.  You are welcome to modify thecontents of the line, but see @ref{Allowing Undoing}.@end deftypevar@deftypevar int rl_pointThe offset of the current cursor position in @code{rl_line_buffer}(the @emph{point}).@end deftypevar@deftypevar int rl_endThe number of characters present in @code{rl_line_buffer}.  When@code{rl_point} is at the end of the line, @code{rl_point} and@code{rl_end} are equal.@end deftypevar@deftypevar int rl_markThe mark (saved position) in the current line.  If set, the markand point define a @emph{region}.@end deftypevar@deftypevar int rl_doneSetting this to a non-zero value causes Readline to return the currentline immediately.@end deftypevar@deftypevar int rl_pending_inputSetting this to a value makes it the next keystroke read.  This is away to stuff a single character into the input stream.@end deftypevar@deftypevar {char *} rl_promptThe prompt Readline uses.  This is set from the argument to@code{readline ()}, and should not be assigned to directly.@end deftypevar@deftypevar {char *} rl_terminal_nameThe terminal type, used for initialization.@end deftypevar@deftypevar {char *} rl_readline_nameThis variable is set to a unique name by each application using Readline.The value allows conditional parsing of the inputrc file(@pxref{Conditional Init Constructs}).@end deftypevar@deftypevar {FILE *} rl_instreamThe stdio stream from which Readline reads input.@end deftypevar@deftypevar {FILE *} rl_outstreamThe stdio stream to which Readline performs output.@end deftypevar@deftypevar {Function *} rl_startup_hookIf non-zero, this is the address of a function to call justbefore @code{readline} prints the first prompt.@end deftypevar@deftypevar {Function *} rl_event_hookIf non-zero, this is the address of a function to call periodicallywhen readline is waiting for terminal input.@end deftypevar@node Readline Convenience Functions@section Readline Convenience Functions@menu* Function Naming::	How to give a function you write a name.* Keymaps::		Making keymaps.* Binding Keys::	Changing Keymaps.* Associating Function Names and Bindings::	Translate function names to						key sequences.* Allowing Undoing::	How to make your functions undoable.* Redisplay::		Functions to control line display.* Modifying Text::	Functions to modify @code{rl_line_buffer}.* Utility Functions::	Generally useful functions and hooks.@end menu@node Function Naming@subsection Naming a FunctionThe user can dynamically change the bindings of keys while usingReadline.  This is done by representing the function with a descriptivename.  The user is able to type the descriptive name when referring tothe function.  Thus, in an init file, one might find@exampleMeta-Rubout:	backward-kill-word@end exampleThis binds the keystroke @key{Meta-Rubout} to the function@emph{descriptively} named @code{backward-kill-word}.  You, as theprogrammer, should bind the functions you write to descriptive names aswell.  Readline provides a function for doing that:@deftypefun int rl_add_defun (char *name, Function *function, int key)Add @var{name} to the list of named functions.  Make @var{function} bethe function that gets called.  If @var{key} is not -1, then bind it to@var{function} using @code{rl_bind_key ()}.@end deftypefunUsing this function alone is sufficient for most applications.  It isthe recommended way to add a few functions to the default functions thatReadline has built in.  If you need to do something otherthan adding a function to Readline, you may need to use theunderlying functions described below.@node Keymaps@subsection Selecting a KeymapKey bindings take place on a @dfn{keymap}.  The keymap is theassociation between the keys that the user types and the functions thatget run.  You can make your own keymaps, copy existing keymaps, and tellReadline which keymap to use.@deftypefun Keymap rl_make_bare_keymap ()Returns a new, empty keymap.  The space for the keymap is allocated with@code{malloc ()}; you should @code{free ()} it when you are done.@end deftypefun@deftypefun Keymap rl_copy_keymap (Keymap map)Return a new keymap which is a copy of @var{map}.@end deftypefun@deftypefun Keymap rl_make_keymap ()Return a new keymap with the printing characters bound to rl_insert,the lowercase Meta characters bound to run their equivalents, andthe Meta digits bound to produce numeric arguments.@end deftypefun@deftypefun void rl_discard_keymap (Keymap keymap)Free the storage associated with @var{keymap}.@end deftypefunReadline has several internal keymaps.  These functions allow you tochange which keymap is active.@deftypefun Keymap rl_get_keymap ()Returns the currently active keymap.@end deftypefun@deftypefun void rl_set_keymap (Keymap keymap)Makes @var{keymap} the currently active keymap.@end deftypefun@deftypefun Keymap rl_get_keymap_by_name (char *name)Return the keymap matching @var{name}.  @var{name} is one which wouldbe supplied in a @code{set keymap} inputrc line (@pxref{Readline Init File}).@end deftypefun@node Binding Keys@subsection Binding KeysYou associate keys with functions through the keymap.  Readline hasseveral internal keymaps: @code{emacs_standard_keymap},@code{emacs_meta_keymap}, @code{emacs_ctlx_keymap},@code{vi_movement_keymap}, and @code{vi_insertion_keymap}.@code{emacs_standard_keymap} is the default, and the examples inthis manual assume that.These functions manage key bindings.@deftypefun int rl_bind_key (int key, Function *function)Binds @var{key} to @var{function} in the currently active keymap.Returns non-zero in the case of an invalid @var{key}.@end deftypefun@deftypefun int rl_bind_key_in_map (int key, Function *function, Keymap map)Bind @var{key} to @var{function} in @var{map}.  Returns non-zero in the caseof an invalid @var{key}.@end deftypefun@deftypefun int rl_unbind_key (int key)Bind @var{key} to the null function in the currently active keymap.Returns non-zero in case of error.@end deftypefun@deftypefun int rl_unbind_key_in_map (int key, Keymap map)Bind @var{key} to the null function in @var{map}.Returns non-zero in case of error.@end deftypefun@deftypefun int rl_generic_bind (int type, char *keyseq, char *data, Keymap map)Bind the key sequence represented by the string @var{keyseq} to the arbitrarypointer @var{data}.  @var{type} says what kind of data is pointed to by@var{data}; this can be a function (@code{ISFUNC}), a macro(@code{ISMACR}), or a keymap (@code{ISKMAP}).  This makes new keymaps asnecessary.  The initial keymap in which to do bindings is @var{map}.@end deftypefun@deftypefun int rl_parse_and_bind (char *line)Parse @var{line} as if it had been read from the @code{inputrc} file andperform any key bindings and variable assignments found(@pxref{Readline Init File}).@end deftypefun@deftypefun int rl_read_init_file (char *filename)Read keybindings and variable assignments from @var{filename}(@pxref{Readline Init File}).@end deftypefun@node Associating Function Names and Bindings@subsection Associating Function Names and BindingsThese functions allow you to find out what keys invoke named functionsand the functions invoked by a particular key sequence.@deftypefun {Function *} rl_named_function (char *name)Return the function with name @var{name}.@end deftypefun@deftypefun {Function *} rl_function_of_keyseq (char *keyseq, Keymap map, int *type)Return the function invoked by @var{keyseq} in keymap @var{map}.If @var{map} is NULL, the current keymap is used.  If @var{type} isnot NULL, the type of the object is returned in it (one of @code{ISFUNC},@code{ISKMAP}, or @code{ISMACR}).@end deftypefun@deftypefun {char **} rl_invoking_keyseqs (Function *function)Return an array of strings representing the key sequences used toinvoke @var{function} in the current keymap.@end deftypefun@deftypefun {char **} rl_invoking_keyseqs_in_map (Function *function, Keymap map)Return an array of strings representing the key sequences used toinvoke @var{function} in the keymap @var{map}.@end deftypefun@deftypefun void rl_function_dumper (int readable)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -