📄 rltech.texi
字号:
@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-2004 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 @sc{gnu} Readline Library andother programs. If you are a programmer, and you wish to include thefeatures found in @sc{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 functions.* Readline Signal Handling:: How Readline behaves when it receives signals.* 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 @var{prompt}and then reads and returns a single line of text from the user.If @var{prompt} is @code{NULL} or the empty string, no prompt is displayed.The line @code{readline} returns is allocated with @code{malloc()};the caller should @code{free()} the line when it has finished with it.The declaration for @code{readline} in ANSI C is@example@code{char *readline (const 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}, rl_command_func_t *@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.Before declaring any functions that customize Readline's behavior, orusing any functionality Readline provides in other code, anapplication writer should include the file @code{<readline/readline.h>}in any file that uses Readline's features. Since some of the definitionsin @code{readline.h} use the @code{stdio} library, the file@code{<stdio.h>} should be included before @code{readline.h}.@code{readline.h} defines a C preprocessor variable that shouldbe treated as an integer, @code{RL_READLINE_VERSION}, which maybe used to conditionally compile application code depending onthe installed Readline version. The value is a hexadecimalencoding of the major and minor version numbers of the library,of the form 0x@var{MMmm}. @var{MM} is the two-digit majorversion number; @var{mm} is the two-digit minor version number. For Readline 4.2, for example, the value of@code{RL_READLINE_VERSION} would be @code{0x0402}. @menu* Readline Typedefs:: C declarations to make code readable.* Function Writing:: Variables and calling conventions.@end menu@node Readline Typedefs@subsection Readline TypedefsFor readabilty, we declare a number of new object types, all pointersto functions.The reason for declaring these new types is to make it easier to writecode describing pointers to C functions with appropriately prototypedarguments and return values.For instance, say we want to declare a variable @var{func} as a pointerto a function which takes two @code{int} arguments and returns an@code{int} (this is the type of all of the Readline bindable functions).Instead of the classic C declaration@code{int (*func)();}@noindentor the ANSI-C style declaration@code{int (*func)(int, int);}@noindentwe may write@code{rl_command_func_t *func;}The full list of function pointer types available is@table @code@item typedef int rl_command_func_t (int, int);@item typedef char *rl_compentry_func_t (const char *, int);@item typedef char **rl_completion_func_t (const char *, int, int);@item typedef char *rl_quote_func_t (char *, int, char *);@item typedef char *rl_dequote_func_t (char *, int);@item typedef int rl_compignore_func_t (char **);@item typedef void rl_compdisp_func_t (char **, int, int);@item typedef int rl_hook_func_t (void);@item typedef int rl_getc_func_t (FILE *);@item typedef int rl_linebuf_func_t (char *, int);@item typedef int rl_intfunc_t (int);@item #define rl_ivoidfunc_t rl_hook_func_t@item typedef int rl_icpfunc_t (char *);@item typedef int rl_icppfunc_t (char **);@item typedef void rl_voidfunc_t (void);@item typedef void rl_vintfunc_t (int);@item typedef void rl_vcpfunc_t (char *);@item typedef void rl_vcppfunc_t (char **);@end table@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{int 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.A command function should return 0 if its action completes successfully,and a non-zero value if some error occurs.@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}. Thefunction @code{rl_extend_line_buffer} is available to increasethe memory allocated to @code{rl_line_buffer}.@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 @var{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_num_chars_to_readSetting this to a positive value before calling @code{readline()} causesReadline to return after accepting that many characters, ratherthan reading up to a character bound to @code{accept-line}.@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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -