11acd27e7Smillert@comment %**start of header (This is for running Texinfo on a region.) 21acd27e7Smillert@setfilename rltech.info 31acd27e7Smillert@comment %**end of header (This is for running Texinfo on a region.) 41acd27e7Smillert@setchapternewpage odd 51acd27e7Smillert 6f2f96c5eSmiod@ignore 71acd27e7SmillertThis document describes the GNU Readline Library, a utility for aiding 81acd27e7Smillertin the consitency of user interface across discrete programs that need 91acd27e7Smillertto provide a command line interface. 101acd27e7Smillert 11*af70c2dfSkettenisCopyright (C) 1988-2002 Free Software Foundation, Inc. 121acd27e7Smillert 131acd27e7SmillertPermission is granted to make and distribute verbatim copies of 141acd27e7Smillertthis manual provided the copyright notice and this permission notice 151acd27e7Smillertpare preserved on all copies. 161acd27e7Smillert 171acd27e7SmillertPermission is granted to process this file through TeX and print the 181acd27e7Smillertresults, provided the printed document carries copying permission 191acd27e7Smillertnotice identical to this one except for the removal of this paragraph 201acd27e7Smillert(this paragraph not being relevant to the printed manual). 211acd27e7Smillert 221acd27e7SmillertPermission is granted to copy and distribute modified versions of this 231acd27e7Smillertmanual under the conditions for verbatim copying, provided that the entire 241acd27e7Smillertresulting derived work is distributed under the terms of a permission 251acd27e7Smillertnotice identical to this one. 261acd27e7Smillert 271acd27e7SmillertPermission is granted to copy and distribute translations of this manual 281acd27e7Smillertinto another language, under the above conditions for modified versions, 291acd27e7Smillertexcept that this permission notice may be stated in a translation approved 301acd27e7Smillertby the Foundation. 31f2f96c5eSmiod@end ignore 321acd27e7Smillert 331acd27e7Smillert@node Programming with GNU Readline 341acd27e7Smillert@chapter Programming with GNU Readline 351acd27e7Smillert 36*af70c2dfSkettenisThis chapter describes the interface between the @sc{gnu} Readline Library and 371acd27e7Smillertother programs. If you are a programmer, and you wish to include the 38*af70c2dfSkettenisfeatures found in @sc{gnu} Readline 391acd27e7Smillertsuch as completion, line editing, and interactive history manipulation 401acd27e7Smillertin your own programs, this section is for you. 411acd27e7Smillert 421acd27e7Smillert@menu 431acd27e7Smillert* Basic Behavior:: Using the default behavior of Readline. 441acd27e7Smillert* Custom Functions:: Adding your own functions to Readline. 451acd27e7Smillert* Readline Variables:: Variables accessible to custom 461acd27e7Smillert functions. 471acd27e7Smillert* Readline Convenience Functions:: Functions which Readline supplies to 481acd27e7Smillert aid in writing your own custom 491acd27e7Smillert functions. 501acd27e7Smillert* Readline Signal Handling:: How Readline behaves when it receives signals. 511acd27e7Smillert* Custom Completers:: Supplanting or supplementing Readline's 521acd27e7Smillert completion functions. 531acd27e7Smillert@end menu 541acd27e7Smillert 551acd27e7Smillert@node Basic Behavior 561acd27e7Smillert@section Basic Behavior 571acd27e7Smillert 581acd27e7SmillertMany programs provide a command line interface, such as @code{mail}, 591acd27e7Smillert@code{ftp}, and @code{sh}. For such programs, the default behaviour of 601acd27e7SmillertReadline is sufficient. This section describes how to use Readline in 611acd27e7Smillertthe simplest way possible, perhaps to replace calls in your code to 621acd27e7Smillert@code{gets()} or @code{fgets()}. 631acd27e7Smillert 641acd27e7Smillert@findex readline 651acd27e7Smillert@cindex readline, function 66*af70c2dfSkettenis 67*af70c2dfSkettenisThe function @code{readline()} prints a prompt @var{prompt} 68*af70c2dfSkettenisand then reads and returns a single line of text from the user. 69*af70c2dfSkettenisIf @var{prompt} is @code{NULL} or the empty string, no prompt is displayed. 70*af70c2dfSkettenisThe line @code{readline} returns is allocated with @code{malloc()}; 71*af70c2dfSkettenisthe caller should @code{free()} the line when it has finished with it. 72*af70c2dfSkettenisThe declaration for @code{readline} in ANSI C is 731acd27e7Smillert 741acd27e7Smillert@example 75*af70c2dfSkettenis@code{char *readline (const char *@var{prompt});} 761acd27e7Smillert@end example 771acd27e7Smillert 781acd27e7Smillert@noindent 791acd27e7SmillertSo, one might say 801acd27e7Smillert@example 811acd27e7Smillert@code{char *line = readline ("Enter a line: ");} 821acd27e7Smillert@end example 831acd27e7Smillert@noindent 841acd27e7Smillertin order to read a line of text from the user. 851acd27e7SmillertThe line returned has the final newline removed, so only the 861acd27e7Smillerttext remains. 871acd27e7Smillert 881acd27e7SmillertIf @code{readline} encounters an @code{EOF} while reading the line, and the 891acd27e7Smillertline is empty at that point, then @code{(char *)NULL} is returned. 901acd27e7SmillertOtherwise, the line is ended just as if a newline had been typed. 911acd27e7Smillert 921acd27e7SmillertIf you want the user to be able to get at the line later, (with 931acd27e7Smillert@key{C-p} for example), you must call @code{add_history()} to save the 941acd27e7Smillertline away in a @dfn{history} list of such lines. 951acd27e7Smillert 961acd27e7Smillert@example 971acd27e7Smillert@code{add_history (line)}; 981acd27e7Smillert@end example 991acd27e7Smillert 1001acd27e7Smillert@noindent 1011acd27e7SmillertFor full details on the GNU History Library, see the associated manual. 1021acd27e7Smillert 1031acd27e7SmillertIt is preferable to avoid saving empty lines on the history list, since 1041acd27e7Smillertusers rarely have a burning need to reuse a blank line. Here is 1051acd27e7Smillerta function which usefully replaces the standard @code{gets()} library 1061acd27e7Smillertfunction, and has the advantage of no static buffer to overflow: 1071acd27e7Smillert 1081acd27e7Smillert@example 1091acd27e7Smillert/* A static variable for holding the line. */ 1101acd27e7Smillertstatic char *line_read = (char *)NULL; 1111acd27e7Smillert 112*af70c2dfSkettenis/* Read a string, and return a pointer to it. 113*af70c2dfSkettenis Returns NULL on EOF. */ 1141acd27e7Smillertchar * 1151acd27e7Smillertrl_gets () 1161acd27e7Smillert@{ 117*af70c2dfSkettenis /* If the buffer has already been allocated, 118*af70c2dfSkettenis return the memory to the free pool. */ 1191acd27e7Smillert if (line_read) 1201acd27e7Smillert @{ 1211acd27e7Smillert free (line_read); 1221acd27e7Smillert line_read = (char *)NULL; 1231acd27e7Smillert @} 1241acd27e7Smillert 1251acd27e7Smillert /* Get a line from the user. */ 1261acd27e7Smillert line_read = readline (""); 1271acd27e7Smillert 128*af70c2dfSkettenis /* If the line has any text in it, 129*af70c2dfSkettenis save it on the history. */ 1301acd27e7Smillert if (line_read && *line_read) 1311acd27e7Smillert add_history (line_read); 1321acd27e7Smillert 1331acd27e7Smillert return (line_read); 1341acd27e7Smillert@} 1351acd27e7Smillert@end example 1361acd27e7Smillert 1371acd27e7SmillertThis function gives the user the default behaviour of @key{TAB} 1381acd27e7Smillertcompletion: completion on file names. If you do not want Readline to 1391acd27e7Smillertcomplete on filenames, you can change the binding of the @key{TAB} key 1401acd27e7Smillertwith @code{rl_bind_key()}. 1411acd27e7Smillert 1421acd27e7Smillert@example 143*af70c2dfSkettenis@code{int rl_bind_key (int @var{key}, rl_command_func_t *@var{function});} 1441acd27e7Smillert@end example 1451acd27e7Smillert 1461acd27e7Smillert@code{rl_bind_key()} takes two arguments: @var{key} is the character that 1471acd27e7Smillertyou want to bind, and @var{function} is the address of the function to 1481acd27e7Smillertcall when @var{key} is pressed. Binding @key{TAB} to @code{rl_insert()} 1491acd27e7Smillertmakes @key{TAB} insert itself. 1501acd27e7Smillert@code{rl_bind_key()} returns non-zero if @var{key} is not a valid 1511acd27e7SmillertASCII character code (between 0 and 255). 1521acd27e7Smillert 1531acd27e7SmillertThus, to disable the default @key{TAB} behavior, the following suffices: 1541acd27e7Smillert@example 1551acd27e7Smillert@code{rl_bind_key ('\t', rl_insert);} 1561acd27e7Smillert@end example 1571acd27e7Smillert 1581acd27e7SmillertThis code should be executed once at the start of your program; you 1591acd27e7Smillertmight write a function called @code{initialize_readline()} which 1601acd27e7Smillertperforms this and other desired initializations, such as installing 1611acd27e7Smillertcustom completers (@pxref{Custom Completers}). 1621acd27e7Smillert 1631acd27e7Smillert@node Custom Functions 1641acd27e7Smillert@section Custom Functions 1651acd27e7Smillert 1661acd27e7SmillertReadline provides many functions for manipulating the text of 1671acd27e7Smillertthe line, but it isn't possible to anticipate the needs of all 1681acd27e7Smillertprograms. This section describes the various functions and variables 1691acd27e7Smillertdefined within the Readline library which allow a user program to add 1701acd27e7Smillertcustomized functionality to Readline. 1711acd27e7Smillert 1721acd27e7SmillertBefore declaring any functions that customize Readline's behavior, or 1731acd27e7Smillertusing any functionality Readline provides in other code, an 1741acd27e7Smillertapplication writer should include the file @code{<readline/readline.h>} 1751acd27e7Smillertin any file that uses Readline's features. Since some of the definitions 1761acd27e7Smillertin @code{readline.h} use the @code{stdio} library, the file 1771acd27e7Smillert@code{<stdio.h>} should be included before @code{readline.h}. 1781acd27e7Smillert 179*af70c2dfSkettenis@code{readline.h} defines a C preprocessor variable that should 180*af70c2dfSkettenisbe treated as an integer, @code{RL_READLINE_VERSION}, which may 181*af70c2dfSkettenisbe used to conditionally compile application code depending on 182*af70c2dfSkettenisthe installed Readline version. The value is a hexadecimal 183*af70c2dfSkettenisencoding of the major and minor version numbers of the library, 184*af70c2dfSkettenisof the form 0x@var{MMmm}. @var{MM} is the two-digit major 185*af70c2dfSkettenisversion number; @var{mm} is the two-digit minor version number. 186*af70c2dfSkettenisFor Readline 4.2, for example, the value of 187*af70c2dfSkettenis@code{RL_READLINE_VERSION} would be @code{0x0402}. 188*af70c2dfSkettenis 1891acd27e7Smillert@menu 190*af70c2dfSkettenis* Readline Typedefs:: C declarations to make code readable. 1911acd27e7Smillert* Function Writing:: Variables and calling conventions. 1921acd27e7Smillert@end menu 1931acd27e7Smillert 194*af70c2dfSkettenis@node Readline Typedefs 195*af70c2dfSkettenis@subsection Readline Typedefs 1961acd27e7Smillert 197*af70c2dfSkettenisFor readabilty, we declare a number of new object types, all pointers 198*af70c2dfSkettenisto functions. 199*af70c2dfSkettenis 200*af70c2dfSkettenisThe reason for declaring these new types is to make it easier to write 201*af70c2dfSketteniscode describing pointers to C functions with appropriately prototyped 202*af70c2dfSkettenisarguments and return values. 203*af70c2dfSkettenis 204*af70c2dfSkettenisFor instance, say we want to declare a variable @var{func} as a pointer 205*af70c2dfSkettenisto a function which takes two @code{int} arguments and returns an 206*af70c2dfSkettenis@code{int} (this is the type of all of the Readline bindable functions). 207*af70c2dfSkettenisInstead of the classic C declaration 208*af70c2dfSkettenis 209*af70c2dfSkettenis@code{int (*func)();} 2101acd27e7Smillert 2111acd27e7Smillert@noindent 212*af70c2dfSkettenisor the ANSI-C style declaration 2131acd27e7Smillert 214*af70c2dfSkettenis@code{int (*func)(int, int);} 2151acd27e7Smillert 2161acd27e7Smillert@noindent 2171acd27e7Smillertwe may write 2181acd27e7Smillert 219*af70c2dfSkettenis@code{rl_command_func_t *func;} 2201acd27e7Smillert 221*af70c2dfSkettenisThe full list of function pointer types available is 2221acd27e7Smillert 223*af70c2dfSkettenis@table @code 224*af70c2dfSkettenis@item typedef int rl_command_func_t (int, int); 2251acd27e7Smillert 226*af70c2dfSkettenis@item typedef char *rl_compentry_func_t (const char *, int); 227*af70c2dfSkettenis 228*af70c2dfSkettenis@item typedef char **rl_completion_func_t (const char *, int, int); 229*af70c2dfSkettenis 230*af70c2dfSkettenis@item typedef char *rl_quote_func_t (char *, int, char *); 231*af70c2dfSkettenis 232*af70c2dfSkettenis@item typedef char *rl_dequote_func_t (char *, int); 233*af70c2dfSkettenis 234*af70c2dfSkettenis@item typedef int rl_compignore_func_t (char **); 235*af70c2dfSkettenis 236*af70c2dfSkettenis@item typedef void rl_compdisp_func_t (char **, int, int); 237*af70c2dfSkettenis 238*af70c2dfSkettenis@item typedef int rl_hook_func_t (void); 239*af70c2dfSkettenis 240*af70c2dfSkettenis@item typedef int rl_getc_func_t (FILE *); 241*af70c2dfSkettenis 242*af70c2dfSkettenis@item typedef int rl_linebuf_func_t (char *, int); 243*af70c2dfSkettenis 244*af70c2dfSkettenis@item typedef int rl_intfunc_t (int); 245*af70c2dfSkettenis@item #define rl_ivoidfunc_t rl_hook_func_t 246*af70c2dfSkettenis@item typedef int rl_icpfunc_t (char *); 247*af70c2dfSkettenis@item typedef int rl_icppfunc_t (char **); 248*af70c2dfSkettenis 249*af70c2dfSkettenis@item typedef void rl_voidfunc_t (void); 250*af70c2dfSkettenis@item typedef void rl_vintfunc_t (int); 251*af70c2dfSkettenis@item typedef void rl_vcpfunc_t (char *); 252*af70c2dfSkettenis@item typedef void rl_vcppfunc_t (char **); 253*af70c2dfSkettenis 254*af70c2dfSkettenis@end table 2551acd27e7Smillert 2561acd27e7Smillert@node Function Writing 2571acd27e7Smillert@subsection Writing a New Function 2581acd27e7Smillert 2591acd27e7SmillertIn order to write new functions for Readline, you need to know the 2601acd27e7Smillertcalling conventions for keyboard-invoked functions, and the names of the 2611acd27e7Smillertvariables that describe the current state of the line read so far. 2621acd27e7Smillert 2631acd27e7SmillertThe calling sequence for a command @code{foo} looks like 2641acd27e7Smillert 2651acd27e7Smillert@example 266*af70c2dfSkettenis@code{int foo (int count, int key)} 2671acd27e7Smillert@end example 2681acd27e7Smillert 2691acd27e7Smillert@noindent 2701acd27e7Smillertwhere @var{count} is the numeric argument (or 1 if defaulted) and 2711acd27e7Smillert@var{key} is the key that invoked this function. 2721acd27e7Smillert 2731acd27e7SmillertIt is completely up to the function as to what should be done with the 2741acd27e7Smillertnumeric argument. Some functions use it as a repeat count, some 2751acd27e7Smillertas a flag, and others to choose alternate behavior (refreshing the current 2761acd27e7Smillertline as opposed to refreshing the screen, for example). Some choose to 2771acd27e7Smillertignore it. In general, if a 2781acd27e7Smillertfunction uses the numeric argument as a repeat count, it should be able 2791acd27e7Smillertto do something useful with both negative and positive arguments. 2801acd27e7SmillertAt the very least, it should be aware that it can be passed a 2811acd27e7Smillertnegative argument. 2821acd27e7Smillert 283*af70c2dfSkettenisA command function should return 0 if its action completes successfully, 284*af70c2dfSkettenisand a non-zero value if some error occurs. 285*af70c2dfSkettenis 2861acd27e7Smillert@node Readline Variables 2871acd27e7Smillert@section Readline Variables 2881acd27e7Smillert 2891acd27e7SmillertThese variables are available to function writers. 2901acd27e7Smillert 2911acd27e7Smillert@deftypevar {char *} rl_line_buffer 2921acd27e7SmillertThis is the line gathered so far. You are welcome to modify the 2931acd27e7Smillertcontents of the line, but see @ref{Allowing Undoing}. The 2941acd27e7Smillertfunction @code{rl_extend_line_buffer} is available to increase 2951acd27e7Smillertthe memory allocated to @code{rl_line_buffer}. 2961acd27e7Smillert@end deftypevar 2971acd27e7Smillert 2981acd27e7Smillert@deftypevar int rl_point 2991acd27e7SmillertThe offset of the current cursor position in @code{rl_line_buffer} 3001acd27e7Smillert(the @emph{point}). 3011acd27e7Smillert@end deftypevar 3021acd27e7Smillert 3031acd27e7Smillert@deftypevar int rl_end 3041acd27e7SmillertThe number of characters present in @code{rl_line_buffer}. When 3051acd27e7Smillert@code{rl_point} is at the end of the line, @code{rl_point} and 3061acd27e7Smillert@code{rl_end} are equal. 3071acd27e7Smillert@end deftypevar 3081acd27e7Smillert 3091acd27e7Smillert@deftypevar int rl_mark 310*af70c2dfSkettenisThe @var{mark} (saved position) in the current line. If set, the mark 3111acd27e7Smillertand point define a @emph{region}. 3121acd27e7Smillert@end deftypevar 3131acd27e7Smillert 3141acd27e7Smillert@deftypevar int rl_done 3151acd27e7SmillertSetting this to a non-zero value causes Readline to return the current 3161acd27e7Smillertline immediately. 3171acd27e7Smillert@end deftypevar 3181acd27e7Smillert 319*af70c2dfSkettenis@deftypevar int rl_num_chars_to_read 320*af70c2dfSkettenisSetting this to a positive value before calling @code{readline()} causes 321*af70c2dfSkettenisReadline to return after accepting that many characters, rather 322*af70c2dfSkettenisthan reading up to a character bound to @code{accept-line}. 323*af70c2dfSkettenis@end deftypevar 324*af70c2dfSkettenis 3251acd27e7Smillert@deftypevar int rl_pending_input 3261acd27e7SmillertSetting this to a value makes it the next keystroke read. This is a 3271acd27e7Smillertway to stuff a single character into the input stream. 3281acd27e7Smillert@end deftypevar 3291acd27e7Smillert 330*af70c2dfSkettenis@deftypevar int rl_dispatching 331*af70c2dfSkettenisSet to a non-zero value if a function is being called from a key binding; 332*af70c2dfSketteniszero otherwise. Application functions can test this to discover whether 333*af70c2dfSkettenisthey were called directly or by Readline's dispatching mechanism. 334*af70c2dfSkettenis@end deftypevar 335*af70c2dfSkettenis 3361acd27e7Smillert@deftypevar int rl_erase_empty_line 3371acd27e7SmillertSetting this to a non-zero value causes Readline to completely erase 3381acd27e7Smillertthe current line, including any prompt, any time a newline is typed as 3391acd27e7Smillertthe only character on an otherwise-empty line. The cursor is moved to 3401acd27e7Smillertthe beginning of the newly-blank line. 3411acd27e7Smillert@end deftypevar 3421acd27e7Smillert 3431acd27e7Smillert@deftypevar {char *} rl_prompt 3441acd27e7SmillertThe prompt Readline uses. This is set from the argument to 3451acd27e7Smillert@code{readline()}, and should not be assigned to directly. 346*af70c2dfSkettenisThe @code{rl_set_prompt()} function (@pxref{Redisplay}) may 347*af70c2dfSkettenisbe used to modify the prompt string after calling @code{readline()}. 3481acd27e7Smillert@end deftypevar 3491acd27e7Smillert 3501acd27e7Smillert@deftypevar int rl_already_prompted 3511acd27e7SmillertIf an application wishes to display the prompt itself, rather than have 3521acd27e7SmillertReadline do it the first time @code{readline()} is called, it should set 3531acd27e7Smillertthis variable to a non-zero value after displaying the prompt. 3541acd27e7SmillertThe prompt must also be passed as the argument to @code{readline()} so 3551acd27e7Smillertthe redisplay functions can update the display properly. 3561acd27e7SmillertThe calling application is responsible for managing the value; Readline 3571acd27e7Smillertnever sets it. 3581acd27e7Smillert@end deftypevar 3591acd27e7Smillert 360*af70c2dfSkettenis@deftypevar {const char *} rl_library_version 3611acd27e7SmillertThe version number of this revision of the library. 3621acd27e7Smillert@end deftypevar 3631acd27e7Smillert 364*af70c2dfSkettenis@deftypevar int rl_readline_version 365*af70c2dfSkettenisAn integer encoding the current version of the library. The encoding is 366*af70c2dfSkettenisof the form 0x@var{MMmm}, where @var{MM} is the two-digit major version 367*af70c2dfSkettenisnumber, and @var{mm} is the two-digit minor version number. 368*af70c2dfSkettenisFor example, for Readline-4.2, @code{rl_readline_version} would have the 369*af70c2dfSkettenisvalue 0x0402. 3701acd27e7Smillert@end deftypevar 3711acd27e7Smillert 372*af70c2dfSkettenis@deftypevar {int} rl_gnu_readline_p 373*af70c2dfSkettenisAlways set to 1, denoting that this is @sc{gnu} readline rather than some 374*af70c2dfSkettenisemulation. 375*af70c2dfSkettenis@end deftypevar 376*af70c2dfSkettenis 377*af70c2dfSkettenis@deftypevar {const char *} rl_terminal_name 378*af70c2dfSkettenisThe terminal type, used for initialization. If not set by the application, 379*af70c2dfSkettenisReadline sets this to the value of the @env{TERM} environment variable 380*af70c2dfSkettenisthe first time it is called. 381*af70c2dfSkettenis@end deftypevar 382*af70c2dfSkettenis 383*af70c2dfSkettenis@deftypevar {const char *} rl_readline_name 3841acd27e7SmillertThis variable is set to a unique name by each application using Readline. 3851acd27e7SmillertThe value allows conditional parsing of the inputrc file 3861acd27e7Smillert(@pxref{Conditional Init Constructs}). 3871acd27e7Smillert@end deftypevar 3881acd27e7Smillert 3891acd27e7Smillert@deftypevar {FILE *} rl_instream 3901acd27e7SmillertThe stdio stream from which Readline reads input. 391*af70c2dfSkettenisIf @code{NULL}, Readline defaults to @var{stdin}. 3921acd27e7Smillert@end deftypevar 3931acd27e7Smillert 3941acd27e7Smillert@deftypevar {FILE *} rl_outstream 3951acd27e7SmillertThe stdio stream to which Readline performs output. 396*af70c2dfSkettenisIf @code{NULL}, Readline defaults to @var{stdout}. 3971acd27e7Smillert@end deftypevar 3981acd27e7Smillert 399*af70c2dfSkettenis@deftypevar {rl_command_func_t *} rl_last_func 400*af70c2dfSkettenisThe address of the last command function Readline executed. May be used to 401*af70c2dfSkettenistest whether or not a function is being executed twice in succession, for 402*af70c2dfSkettenisexample. 403*af70c2dfSkettenis@end deftypevar 404*af70c2dfSkettenis 405*af70c2dfSkettenis@deftypevar {rl_hook_func_t *} rl_startup_hook 4061acd27e7SmillertIf non-zero, this is the address of a function to call just 4071acd27e7Smillertbefore @code{readline} prints the first prompt. 4081acd27e7Smillert@end deftypevar 4091acd27e7Smillert 410*af70c2dfSkettenis@deftypevar {rl_hook_func_t *} rl_pre_input_hook 4111acd27e7SmillertIf non-zero, this is the address of a function to call after 4121acd27e7Smillertthe first prompt has been printed and just before @code{readline} 4131acd27e7Smillertstarts reading input characters. 4141acd27e7Smillert@end deftypevar 4151acd27e7Smillert 416*af70c2dfSkettenis@deftypevar {rl_hook_func_t *} rl_event_hook 4171acd27e7SmillertIf non-zero, this is the address of a function to call periodically 418*af70c2dfSketteniswhen Readline is waiting for terminal input. 419*af70c2dfSkettenisBy default, this will be called at most ten times a second if there 420*af70c2dfSkettenisis no keyboard input. 4211acd27e7Smillert@end deftypevar 4221acd27e7Smillert 423*af70c2dfSkettenis@deftypevar {rl_getc_func_t *} rl_getc_function 424*af70c2dfSkettenisIf non-zero, Readline will call indirectly through this pointer 4251acd27e7Smillertto get a character from the input stream. By default, it is set to 426*af70c2dfSkettenis@code{rl_getc}, the default Readline character input function 427*af70c2dfSkettenis(@pxref{Character Input}). 4281acd27e7Smillert@end deftypevar 4291acd27e7Smillert 430*af70c2dfSkettenis@deftypevar {rl_voidfunc_t *} rl_redisplay_function 431*af70c2dfSkettenisIf non-zero, Readline will call indirectly through this pointer 4321acd27e7Smillertto update the display with the current contents of the editing buffer. 433*af70c2dfSkettenisBy default, it is set to @code{rl_redisplay}, the default Readline 4341acd27e7Smillertredisplay function (@pxref{Redisplay}). 4351acd27e7Smillert@end deftypevar 4361acd27e7Smillert 437*af70c2dfSkettenis@deftypevar {rl_vintfunc_t *} rl_prep_term_function 438*af70c2dfSkettenisIf non-zero, Readline will call indirectly through this pointer 439*af70c2dfSkettenisto initialize the terminal. The function takes a single argument, an 440*af70c2dfSkettenis@code{int} flag that says whether or not to use eight-bit characters. 441*af70c2dfSkettenisBy default, this is set to @code{rl_prep_terminal} 442*af70c2dfSkettenis(@pxref{Terminal Management}). 443*af70c2dfSkettenis@end deftypevar 444*af70c2dfSkettenis 445*af70c2dfSkettenis@deftypevar {rl_voidfunc_t *} rl_deprep_term_function 446*af70c2dfSkettenisIf non-zero, Readline will call indirectly through this pointer 447*af70c2dfSkettenisto reset the terminal. This function should undo the effects of 448*af70c2dfSkettenis@code{rl_prep_term_function}. 449*af70c2dfSkettenisBy default, this is set to @code{rl_deprep_terminal} 450*af70c2dfSkettenis(@pxref{Terminal Management}). 451*af70c2dfSkettenis@end deftypevar 452*af70c2dfSkettenis 4531acd27e7Smillert@deftypevar {Keymap} rl_executing_keymap 4541acd27e7SmillertThis variable is set to the keymap (@pxref{Keymaps}) in which the 4551acd27e7Smillertcurrently executing readline function was found. 4561acd27e7Smillert@end deftypevar 4571acd27e7Smillert 4581acd27e7Smillert@deftypevar {Keymap} rl_binding_keymap 4591acd27e7SmillertThis variable is set to the keymap (@pxref{Keymaps}) in which the 4601acd27e7Smillertlast key binding occurred. 4611acd27e7Smillert@end deftypevar 4621acd27e7Smillert 463*af70c2dfSkettenis@deftypevar {char *} rl_executing_macro 464*af70c2dfSkettenisThis variable is set to the text of any currently-executing macro. 465*af70c2dfSkettenis@end deftypevar 466*af70c2dfSkettenis 467*af70c2dfSkettenis@deftypevar {int} rl_readline_state 468*af70c2dfSkettenisA variable with bit values that encapsulate the current Readline state. 469*af70c2dfSkettenisA bit is set with the @code{RL_SETSTATE} macro, and unset with the 470*af70c2dfSkettenis@code{RL_UNSETSTATE} macro. Use the @code{RL_ISSTATE} macro to test 471*af70c2dfSketteniswhether a particular state bit is set. Current state bits include: 472*af70c2dfSkettenis 473*af70c2dfSkettenis@table @code 474*af70c2dfSkettenis@item RL_STATE_NONE 475*af70c2dfSkettenisReadline has not yet been called, nor has it begun to intialize. 476*af70c2dfSkettenis@item RL_STATE_INITIALIZING 477*af70c2dfSkettenisReadline is initializing its internal data structures. 478*af70c2dfSkettenis@item RL_STATE_INITIALIZED 479*af70c2dfSkettenisReadline has completed its initialization. 480*af70c2dfSkettenis@item RL_STATE_TERMPREPPED 481*af70c2dfSkettenisReadline has modified the terminal modes to do its own input and redisplay. 482*af70c2dfSkettenis@item RL_STATE_READCMD 483*af70c2dfSkettenisReadline is reading a command from the keyboard. 484*af70c2dfSkettenis@item RL_STATE_METANEXT 485*af70c2dfSkettenisReadline is reading more input after reading the meta-prefix character. 486*af70c2dfSkettenis@item RL_STATE_DISPATCHING 487*af70c2dfSkettenisReadline is dispatching to a command. 488*af70c2dfSkettenis@item RL_STATE_MOREINPUT 489*af70c2dfSkettenisReadline is reading more input while executing an editing command. 490*af70c2dfSkettenis@item RL_STATE_ISEARCH 491*af70c2dfSkettenisReadline is performing an incremental history search. 492*af70c2dfSkettenis@item RL_STATE_NSEARCH 493*af70c2dfSkettenisReadline is performing a non-incremental history search. 494*af70c2dfSkettenis@item RL_STATE_SEARCH 495*af70c2dfSkettenisReadline is searching backward or forward through the history for a string. 496*af70c2dfSkettenis@item RL_STATE_NUMERICARG 497*af70c2dfSkettenisReadline is reading a numeric argument. 498*af70c2dfSkettenis@item RL_STATE_MACROINPUT 499*af70c2dfSkettenisReadline is currently getting its input from a previously-defined keyboard 500*af70c2dfSkettenismacro. 501*af70c2dfSkettenis@item RL_STATE_MACRODEF 502*af70c2dfSkettenisReadline is currently reading characters defining a keyboard macro. 503*af70c2dfSkettenis@item RL_STATE_OVERWRITE 504*af70c2dfSkettenisReadline is in overwrite mode. 505*af70c2dfSkettenis@item RL_STATE_COMPLETING 506*af70c2dfSkettenisReadline is performing word completion. 507*af70c2dfSkettenis@item RL_STATE_SIGHANDLER 508*af70c2dfSkettenisReadline is currently executing the readline signal handler. 509*af70c2dfSkettenis@item RL_STATE_UNDOING 510*af70c2dfSkettenisReadline is performing an undo. 511*af70c2dfSkettenis@item RL_STATE_DONE 512*af70c2dfSkettenisReadline has read a key sequence bound to @code{accept-line} 513*af70c2dfSkettenisand is about to return the line to the caller. 514*af70c2dfSkettenis@end table 515*af70c2dfSkettenis 516*af70c2dfSkettenis@end deftypevar 517*af70c2dfSkettenis 518*af70c2dfSkettenis@deftypevar {int} rl_explicit_arg 519*af70c2dfSkettenisSet to a non-zero value if an explicit numeric argument was specified by 520*af70c2dfSkettenisthe user. Only valid in a bindable command function. 521*af70c2dfSkettenis@end deftypevar 522*af70c2dfSkettenis 523*af70c2dfSkettenis@deftypevar {int} rl_numeric_arg 524*af70c2dfSkettenisSet to the value of any numeric argument explicitly specified by the user 525*af70c2dfSkettenisbefore executing the current Readline function. Only valid in a bindable 526*af70c2dfSketteniscommand function. 527*af70c2dfSkettenis@end deftypevar 528*af70c2dfSkettenis 529*af70c2dfSkettenis@deftypevar {int} rl_editing_mode 530*af70c2dfSkettenisSet to a value denoting Readline's current editing mode. A value of 531*af70c2dfSkettenis@var{1} means Readline is currently in emacs mode; @var{0} 532*af70c2dfSkettenismeans that vi mode is active. 533*af70c2dfSkettenis@end deftypevar 534*af70c2dfSkettenis 535*af70c2dfSkettenis 5361acd27e7Smillert@node Readline Convenience Functions 5371acd27e7Smillert@section Readline Convenience Functions 5381acd27e7Smillert 5391acd27e7Smillert@menu 5401acd27e7Smillert* Function Naming:: How to give a function you write a name. 5411acd27e7Smillert* Keymaps:: Making keymaps. 5421acd27e7Smillert* Binding Keys:: Changing Keymaps. 5431acd27e7Smillert* Associating Function Names and Bindings:: Translate function names to 5441acd27e7Smillert key sequences. 5451acd27e7Smillert* Allowing Undoing:: How to make your functions undoable. 5461acd27e7Smillert* Redisplay:: Functions to control line display. 5471acd27e7Smillert* Modifying Text:: Functions to modify @code{rl_line_buffer}. 548*af70c2dfSkettenis* Character Input:: Functions to read keyboard input. 549*af70c2dfSkettenis* Terminal Management:: Functions to manage terminal settings. 5501acd27e7Smillert* Utility Functions:: Generally useful functions and hooks. 551*af70c2dfSkettenis* Miscellaneous Functions:: Functions that don't fall into any category. 5521acd27e7Smillert* Alternate Interface:: Using Readline in a `callback' fashion. 553*af70c2dfSkettenis* A Readline Example:: An example Readline function. 5541acd27e7Smillert@end menu 5551acd27e7Smillert 5561acd27e7Smillert@node Function Naming 5571acd27e7Smillert@subsection Naming a Function 5581acd27e7Smillert 5591acd27e7SmillertThe user can dynamically change the bindings of keys while using 5601acd27e7SmillertReadline. This is done by representing the function with a descriptive 5611acd27e7Smillertname. The user is able to type the descriptive name when referring to 5621acd27e7Smillertthe function. Thus, in an init file, one might find 5631acd27e7Smillert 5641acd27e7Smillert@example 5651acd27e7SmillertMeta-Rubout: backward-kill-word 5661acd27e7Smillert@end example 5671acd27e7Smillert 5681acd27e7SmillertThis binds the keystroke @key{Meta-Rubout} to the function 5691acd27e7Smillert@emph{descriptively} named @code{backward-kill-word}. You, as the 5701acd27e7Smillertprogrammer, should bind the functions you write to descriptive names as 5711acd27e7Smillertwell. Readline provides a function for doing that: 5721acd27e7Smillert 573*af70c2dfSkettenis@deftypefun int rl_add_defun (const char *name, rl_command_func_t *function, int key) 5741acd27e7SmillertAdd @var{name} to the list of named functions. Make @var{function} be 5751acd27e7Smillertthe function that gets called. If @var{key} is not -1, then bind it to 5761acd27e7Smillert@var{function} using @code{rl_bind_key()}. 5771acd27e7Smillert@end deftypefun 5781acd27e7Smillert 5791acd27e7SmillertUsing this function alone is sufficient for most applications. It is 5801acd27e7Smillertthe recommended way to add a few functions to the default functions that 5811acd27e7SmillertReadline has built in. If you need to do something other 5821acd27e7Smillertthan adding a function to Readline, you may need to use the 5831acd27e7Smillertunderlying functions described below. 5841acd27e7Smillert 5851acd27e7Smillert@node Keymaps 5861acd27e7Smillert@subsection Selecting a Keymap 5871acd27e7Smillert 5881acd27e7SmillertKey bindings take place on a @dfn{keymap}. The keymap is the 5891acd27e7Smillertassociation between the keys that the user types and the functions that 5901acd27e7Smillertget run. You can make your own keymaps, copy existing keymaps, and tell 5911acd27e7SmillertReadline which keymap to use. 5921acd27e7Smillert 593*af70c2dfSkettenis@deftypefun Keymap rl_make_bare_keymap (void) 5941acd27e7SmillertReturns a new, empty keymap. The space for the keymap is allocated with 595*af70c2dfSkettenis@code{malloc()}; the caller should free it by calling 596*af70c2dfSkettenis@code{rl_discard_keymap()} when done. 5971acd27e7Smillert@end deftypefun 5981acd27e7Smillert 5991acd27e7Smillert@deftypefun Keymap rl_copy_keymap (Keymap map) 6001acd27e7SmillertReturn a new keymap which is a copy of @var{map}. 6011acd27e7Smillert@end deftypefun 6021acd27e7Smillert 603*af70c2dfSkettenis@deftypefun Keymap rl_make_keymap (void) 6041acd27e7SmillertReturn a new keymap with the printing characters bound to rl_insert, 6051acd27e7Smillertthe lowercase Meta characters bound to run their equivalents, and 6061acd27e7Smillertthe Meta digits bound to produce numeric arguments. 6071acd27e7Smillert@end deftypefun 6081acd27e7Smillert 6091acd27e7Smillert@deftypefun void rl_discard_keymap (Keymap keymap) 6101acd27e7SmillertFree the storage associated with @var{keymap}. 6111acd27e7Smillert@end deftypefun 6121acd27e7Smillert 6131acd27e7SmillertReadline has several internal keymaps. These functions allow you to 6141acd27e7Smillertchange which keymap is active. 6151acd27e7Smillert 616*af70c2dfSkettenis@deftypefun Keymap rl_get_keymap (void) 6171acd27e7SmillertReturns the currently active keymap. 6181acd27e7Smillert@end deftypefun 6191acd27e7Smillert 6201acd27e7Smillert@deftypefun void rl_set_keymap (Keymap keymap) 6211acd27e7SmillertMakes @var{keymap} the currently active keymap. 6221acd27e7Smillert@end deftypefun 6231acd27e7Smillert 624*af70c2dfSkettenis@deftypefun Keymap rl_get_keymap_by_name (const char *name) 6251acd27e7SmillertReturn the keymap matching @var{name}. @var{name} is one which would 6261acd27e7Smillertbe supplied in a @code{set keymap} inputrc line (@pxref{Readline Init File}). 6271acd27e7Smillert@end deftypefun 6281acd27e7Smillert 6291acd27e7Smillert@deftypefun {char *} rl_get_keymap_name (Keymap keymap) 6301acd27e7SmillertReturn the name matching @var{keymap}. @var{name} is one which would 6311acd27e7Smillertbe supplied in a @code{set keymap} inputrc line (@pxref{Readline Init File}). 6321acd27e7Smillert@end deftypefun 6331acd27e7Smillert 6341acd27e7Smillert@node Binding Keys 6351acd27e7Smillert@subsection Binding Keys 6361acd27e7Smillert 637*af70c2dfSkettenisKey sequences are associate with functions through the keymap. 638*af70c2dfSkettenisReadline has several internal keymaps: @code{emacs_standard_keymap}, 6391acd27e7Smillert@code{emacs_meta_keymap}, @code{emacs_ctlx_keymap}, 6401acd27e7Smillert@code{vi_movement_keymap}, and @code{vi_insertion_keymap}. 6411acd27e7Smillert@code{emacs_standard_keymap} is the default, and the examples in 6421acd27e7Smillertthis manual assume that. 6431acd27e7Smillert 644*af70c2dfSkettenisSince @code{readline()} installs a set of default key bindings the first 6451acd27e7Smillerttime it is called, there is always the danger that a custom binding 646*af70c2dfSkettenisinstalled before the first call to @code{readline()} will be overridden. 6471acd27e7SmillertAn alternate mechanism is to install custom key bindings in an 6481acd27e7Smillertinitialization function assigned to the @code{rl_startup_hook} variable 6491acd27e7Smillert(@pxref{Readline Variables}). 6501acd27e7Smillert 6511acd27e7SmillertThese functions manage key bindings. 6521acd27e7Smillert 653*af70c2dfSkettenis@deftypefun int rl_bind_key (int key, rl_command_func_t *function) 6541acd27e7SmillertBinds @var{key} to @var{function} in the currently active keymap. 6551acd27e7SmillertReturns non-zero in the case of an invalid @var{key}. 6561acd27e7Smillert@end deftypefun 6571acd27e7Smillert 658*af70c2dfSkettenis@deftypefun int rl_bind_key_in_map (int key, rl_command_func_t *function, Keymap map) 6591acd27e7SmillertBind @var{key} to @var{function} in @var{map}. Returns non-zero in the case 6601acd27e7Smillertof an invalid @var{key}. 6611acd27e7Smillert@end deftypefun 6621acd27e7Smillert 6631acd27e7Smillert@deftypefun int rl_unbind_key (int key) 6641acd27e7SmillertBind @var{key} to the null function in the currently active keymap. 6651acd27e7SmillertReturns non-zero in case of error. 6661acd27e7Smillert@end deftypefun 6671acd27e7Smillert 6681acd27e7Smillert@deftypefun int rl_unbind_key_in_map (int key, Keymap map) 6691acd27e7SmillertBind @var{key} to the null function in @var{map}. 6701acd27e7SmillertReturns non-zero in case of error. 6711acd27e7Smillert@end deftypefun 6721acd27e7Smillert 673*af70c2dfSkettenis@deftypefun int rl_unbind_function_in_map (rl_command_func_t *function, Keymap map) 6741acd27e7SmillertUnbind all keys that execute @var{function} in @var{map}. 6751acd27e7Smillert@end deftypefun 6761acd27e7Smillert 677*af70c2dfSkettenis@deftypefun int rl_unbind_command_in_map (const char *command, Keymap map) 6781acd27e7SmillertUnbind all keys that are bound to @var{command} in @var{map}. 6791acd27e7Smillert@end deftypefun 6801acd27e7Smillert 681*af70c2dfSkettenis@deftypefun int rl_set_key (const char *keyseq, rl_command_func_t *function, Keymap map) 682*af70c2dfSkettenisBind the key sequence represented by the string @var{keyseq} to the function 683*af70c2dfSkettenis@var{function}. This makes new keymaps as 684*af70c2dfSkettenisnecessary. The initial keymap in which to do bindings is @var{map}. 685*af70c2dfSkettenis@end deftypefun 686*af70c2dfSkettenis 687*af70c2dfSkettenis@deftypefun int rl_generic_bind (int type, const char *keyseq, char *data, Keymap map) 6881acd27e7SmillertBind the key sequence represented by the string @var{keyseq} to the arbitrary 6891acd27e7Smillertpointer @var{data}. @var{type} says what kind of data is pointed to by 6901acd27e7Smillert@var{data}; this can be a function (@code{ISFUNC}), a macro 6911acd27e7Smillert(@code{ISMACR}), or a keymap (@code{ISKMAP}). This makes new keymaps as 6921acd27e7Smillertnecessary. The initial keymap in which to do bindings is @var{map}. 6931acd27e7Smillert@end deftypefun 6941acd27e7Smillert 6951acd27e7Smillert@deftypefun int rl_parse_and_bind (char *line) 6961acd27e7SmillertParse @var{line} as if it had been read from the @code{inputrc} file and 6971acd27e7Smillertperform any key bindings and variable assignments found 6981acd27e7Smillert(@pxref{Readline Init File}). 6991acd27e7Smillert@end deftypefun 7001acd27e7Smillert 701*af70c2dfSkettenis@deftypefun int rl_read_init_file (const char *filename) 7021acd27e7SmillertRead keybindings and variable assignments from @var{filename} 7031acd27e7Smillert(@pxref{Readline Init File}). 7041acd27e7Smillert@end deftypefun 7051acd27e7Smillert 7061acd27e7Smillert@node Associating Function Names and Bindings 7071acd27e7Smillert@subsection Associating Function Names and Bindings 7081acd27e7Smillert 7091acd27e7SmillertThese functions allow you to find out what keys invoke named functions 710*af70c2dfSkettenisand the functions invoked by a particular key sequence. You may also 711*af70c2dfSkettenisassociate a new function name with an arbitrary function. 7121acd27e7Smillert 713*af70c2dfSkettenis@deftypefun {rl_command_func_t *} rl_named_function (const char *name) 7141acd27e7SmillertReturn the function with name @var{name}. 7151acd27e7Smillert@end deftypefun 7161acd27e7Smillert 717*af70c2dfSkettenis@deftypefun {rl_command_func_t *} rl_function_of_keyseq (const char *keyseq, Keymap map, int *type) 7181acd27e7SmillertReturn the function invoked by @var{keyseq} in keymap @var{map}. 719*af70c2dfSkettenisIf @var{map} is @code{NULL}, the current keymap is used. If @var{type} is 720*af70c2dfSkettenisnot @code{NULL}, the type of the object is returned in the @code{int} variable 721*af70c2dfSkettenisit points to (one of @code{ISFUNC}, @code{ISKMAP}, or @code{ISMACR}). 7221acd27e7Smillert@end deftypefun 7231acd27e7Smillert 724*af70c2dfSkettenis@deftypefun {char **} rl_invoking_keyseqs (rl_command_func_t *function) 7251acd27e7SmillertReturn an array of strings representing the key sequences used to 7261acd27e7Smillertinvoke @var{function} in the current keymap. 7271acd27e7Smillert@end deftypefun 7281acd27e7Smillert 729*af70c2dfSkettenis@deftypefun {char **} rl_invoking_keyseqs_in_map (rl_command_func_t *function, Keymap map) 7301acd27e7SmillertReturn an array of strings representing the key sequences used to 7311acd27e7Smillertinvoke @var{function} in the keymap @var{map}. 7321acd27e7Smillert@end deftypefun 7331acd27e7Smillert 7341acd27e7Smillert@deftypefun void rl_function_dumper (int readable) 7351acd27e7SmillertPrint the readline function names and the key sequences currently 7361acd27e7Smillertbound to them to @code{rl_outstream}. If @var{readable} is non-zero, 7371acd27e7Smillertthe list is formatted in such a way that it can be made part of an 7381acd27e7Smillert@code{inputrc} file and re-read. 7391acd27e7Smillert@end deftypefun 7401acd27e7Smillert 741*af70c2dfSkettenis@deftypefun void rl_list_funmap_names (void) 7421acd27e7SmillertPrint the names of all bindable Readline functions to @code{rl_outstream}. 7431acd27e7Smillert@end deftypefun 7441acd27e7Smillert 745*af70c2dfSkettenis@deftypefun {const char **} rl_funmap_names (void) 7461acd27e7SmillertReturn a NULL terminated array of known function names. The array is 7471acd27e7Smillertsorted. The array itself is allocated, but not the strings inside. You 748*af70c2dfSkettenisshould @code{free()} the array when you are done, but not the pointers. 749*af70c2dfSkettenis@end deftypefun 750*af70c2dfSkettenis 751*af70c2dfSkettenis@deftypefun int rl_add_funmap_entry (const char *name, rl_command_func_t *function) 752*af70c2dfSkettenisAdd @var{name} to the list of bindable Readline command names, and make 753*af70c2dfSkettenis@var{function} the function to be called when @var{name} is invoked. 7541acd27e7Smillert@end deftypefun 7551acd27e7Smillert 7561acd27e7Smillert@node Allowing Undoing 7571acd27e7Smillert@subsection Allowing Undoing 7581acd27e7Smillert 7591acd27e7SmillertSupporting the undo command is a painless thing, and makes your 7601acd27e7Smillertfunctions much more useful. It is certainly easy to try 761*af70c2dfSkettenissomething if you know you can undo it. 7621acd27e7Smillert 7631acd27e7SmillertIf your function simply inserts text once, or deletes text once, and 7641acd27e7Smillertuses @code{rl_insert_text()} or @code{rl_delete_text()} to do it, then 7651acd27e7Smillertundoing is already done for you automatically. 7661acd27e7Smillert 7671acd27e7SmillertIf you do multiple insertions or multiple deletions, or any combination 7681acd27e7Smillertof these operations, you should group them together into one operation. 7691acd27e7SmillertThis is done with @code{rl_begin_undo_group()} and 7701acd27e7Smillert@code{rl_end_undo_group()}. 7711acd27e7Smillert 7721acd27e7SmillertThe types of events that can be undone are: 7731acd27e7Smillert 774*af70c2dfSkettenis@smallexample 7751acd27e7Smillertenum undo_code @{ UNDO_DELETE, UNDO_INSERT, UNDO_BEGIN, UNDO_END @}; 776*af70c2dfSkettenis@end smallexample 7771acd27e7Smillert 7781acd27e7SmillertNotice that @code{UNDO_DELETE} means to insert some text, and 7791acd27e7Smillert@code{UNDO_INSERT} means to delete some text. That is, the undo code 780*af70c2dfSkettenistells what to undo, not how to undo it. @code{UNDO_BEGIN} and 7811acd27e7Smillert@code{UNDO_END} are tags added by @code{rl_begin_undo_group()} and 7821acd27e7Smillert@code{rl_end_undo_group()}. 7831acd27e7Smillert 784*af70c2dfSkettenis@deftypefun int rl_begin_undo_group (void) 7851acd27e7SmillertBegins saving undo information in a group construct. The undo 7861acd27e7Smillertinformation usually comes from calls to @code{rl_insert_text()} and 7871acd27e7Smillert@code{rl_delete_text()}, but could be the result of calls to 7881acd27e7Smillert@code{rl_add_undo()}. 7891acd27e7Smillert@end deftypefun 7901acd27e7Smillert 791*af70c2dfSkettenis@deftypefun int rl_end_undo_group (void) 7921acd27e7SmillertCloses the current undo group started with @code{rl_begin_undo_group 7931acd27e7Smillert()}. There should be one call to @code{rl_end_undo_group()} 7941acd27e7Smillertfor each call to @code{rl_begin_undo_group()}. 7951acd27e7Smillert@end deftypefun 7961acd27e7Smillert 7971acd27e7Smillert@deftypefun void rl_add_undo (enum undo_code what, int start, int end, char *text) 7981acd27e7SmillertRemember how to undo an event (according to @var{what}). The affected 7991acd27e7Smillerttext runs from @var{start} to @var{end}, and encompasses @var{text}. 8001acd27e7Smillert@end deftypefun 8011acd27e7Smillert 802*af70c2dfSkettenis@deftypefun void rl_free_undo_list (void) 8031acd27e7SmillertFree the existing undo list. 8041acd27e7Smillert@end deftypefun 8051acd27e7Smillert 806*af70c2dfSkettenis@deftypefun int rl_do_undo (void) 8071acd27e7SmillertUndo the first thing on the undo list. Returns @code{0} if there was 8081acd27e7Smillertnothing to undo, non-zero if something was undone. 8091acd27e7Smillert@end deftypefun 8101acd27e7Smillert 8111acd27e7SmillertFinally, if you neither insert nor delete text, but directly modify the 8121acd27e7Smillertexisting text (e.g., change its case), call @code{rl_modifying()} 8131acd27e7Smillertonce, just before you modify the text. You must supply the indices of 8141acd27e7Smillertthe text range that you are going to modify. 8151acd27e7Smillert 8161acd27e7Smillert@deftypefun int rl_modifying (int start, int end) 8171acd27e7SmillertTell Readline to save the text between @var{start} and @var{end} as a 8181acd27e7Smillertsingle undo unit. It is assumed that you will subsequently modify 8191acd27e7Smillertthat text. 8201acd27e7Smillert@end deftypefun 8211acd27e7Smillert 8221acd27e7Smillert@node Redisplay 8231acd27e7Smillert@subsection Redisplay 8241acd27e7Smillert 825*af70c2dfSkettenis@deftypefun void rl_redisplay (void) 8261acd27e7SmillertChange what's displayed on the screen to reflect the current contents 8271acd27e7Smillertof @code{rl_line_buffer}. 8281acd27e7Smillert@end deftypefun 8291acd27e7Smillert 830*af70c2dfSkettenis@deftypefun int rl_forced_update_display (void) 8311acd27e7SmillertForce the line to be updated and redisplayed, whether or not 8321acd27e7SmillertReadline thinks the screen display is correct. 8331acd27e7Smillert@end deftypefun 8341acd27e7Smillert 835*af70c2dfSkettenis@deftypefun int rl_on_new_line (void) 8361acd27e7SmillertTell the update functions that we have moved onto a new (empty) line, 837c0141d96Sderaadtusually after outputting a newline. 8381acd27e7Smillert@end deftypefun 8391acd27e7Smillert 840*af70c2dfSkettenis@deftypefun int rl_on_new_line_with_prompt (void) 8411acd27e7SmillertTell the update functions that we have moved onto a new line, with 8421acd27e7Smillert@var{rl_prompt} already displayed. 8431acd27e7SmillertThis could be used by applications that want to output the prompt string 8441acd27e7Smillertthemselves, but still need Readline to know the prompt string length for 8451acd27e7Smillertredisplay. 8461acd27e7SmillertIt should be used after setting @var{rl_already_prompted}. 8471acd27e7Smillert@end deftypefun 8481acd27e7Smillert 849*af70c2dfSkettenis@deftypefun int rl_reset_line_state (void) 8501acd27e7SmillertReset the display state to a clean state and redisplay the current line 8511acd27e7Smillertstarting on a new line. 8521acd27e7Smillert@end deftypefun 8531acd27e7Smillert 854*af70c2dfSkettenis@deftypefun int rl_crlf (void) 855*af70c2dfSkettenisMove the cursor to the start of the next screen line. 856*af70c2dfSkettenis@end deftypefun 857*af70c2dfSkettenis 858*af70c2dfSkettenis@deftypefun int rl_show_char (int c) 859*af70c2dfSkettenisDisplay character @var{c} on @code{rl_outstream}. 860*af70c2dfSkettenisIf Readline has not been set to display meta characters directly, this 861*af70c2dfSketteniswill convert meta characters to a meta-prefixed key sequence. 862*af70c2dfSkettenisThis is intended for use by applications which wish to do their own 863*af70c2dfSkettenisredisplay. 864*af70c2dfSkettenis@end deftypefun 865*af70c2dfSkettenis 866*af70c2dfSkettenis@deftypefun int rl_message (const char *, @dots{}) 867*af70c2dfSkettenisThe arguments are a format string as would be supplied to @code{printf}, 868*af70c2dfSkettenispossibly containing conversion specifications such as @samp{%d}, and 869*af70c2dfSkettenisany additional arguments necessary to satisfy the conversion specifications. 870*af70c2dfSkettenisThe resulting string is displayed in the @dfn{echo area}. The echo area 8711acd27e7Smillertis also used to display numeric arguments and search strings. 8721acd27e7Smillert@end deftypefun 8731acd27e7Smillert 874*af70c2dfSkettenis@deftypefun int rl_clear_message (void) 8751acd27e7SmillertClear the message in the echo area. 8761acd27e7Smillert@end deftypefun 8771acd27e7Smillert 878*af70c2dfSkettenis@deftypefun void rl_save_prompt (void) 8791acd27e7SmillertSave the local Readline prompt display state in preparation for 880*af70c2dfSkettenisdisplaying a new message in the message area with @code{rl_message()}. 8811acd27e7Smillert@end deftypefun 8821acd27e7Smillert 883*af70c2dfSkettenis@deftypefun void rl_restore_prompt (void) 8841acd27e7SmillertRestore the local Readline prompt display state saved by the most 8851acd27e7Smillertrecent call to @code{rl_save_prompt}. 8861acd27e7Smillert@end deftypefun 8871acd27e7Smillert 888*af70c2dfSkettenis@deftypefun int rl_expand_prompt (char *prompt) 889*af70c2dfSkettenisExpand any special character sequences in @var{prompt} and set up the 890*af70c2dfSkettenislocal Readline prompt redisplay variables. 891*af70c2dfSkettenisThis function is called by @code{readline()}. It may also be called to 892*af70c2dfSkettenisexpand the primary prompt if the @code{rl_on_new_line_with_prompt()} 893*af70c2dfSkettenisfunction or @code{rl_already_prompted} variable is used. 894*af70c2dfSkettenisIt returns the number of visible characters on the last line of the 895*af70c2dfSkettenis(possibly multi-line) prompt. 896*af70c2dfSkettenis@end deftypefun 897*af70c2dfSkettenis 898*af70c2dfSkettenis@deftypefun int rl_set_prompt (const char *prompt) 899*af70c2dfSkettenisMake Readline use @var{prompt} for subsequent redisplay. This calls 900*af70c2dfSkettenis@code{rl_expand_prompt()} to expand the prompt and sets @code{rl_prompt} 901*af70c2dfSkettenisto the result. 902*af70c2dfSkettenis@end deftypefun 903*af70c2dfSkettenis 9041acd27e7Smillert@node Modifying Text 9051acd27e7Smillert@subsection Modifying Text 9061acd27e7Smillert 907*af70c2dfSkettenis@deftypefun int rl_insert_text (const char *text) 9081acd27e7SmillertInsert @var{text} into the line at the current cursor position. 909*af70c2dfSkettenisReturns the number of characters inserted. 9101acd27e7Smillert@end deftypefun 9111acd27e7Smillert 9121acd27e7Smillert@deftypefun int rl_delete_text (int start, int end) 9131acd27e7SmillertDelete the text between @var{start} and @var{end} in the current line. 914*af70c2dfSkettenisReturns the number of characters deleted. 9151acd27e7Smillert@end deftypefun 9161acd27e7Smillert 9171acd27e7Smillert@deftypefun {char *} rl_copy_text (int start, int end) 9181acd27e7SmillertReturn a copy of the text between @var{start} and @var{end} in 9191acd27e7Smillertthe current line. 9201acd27e7Smillert@end deftypefun 9211acd27e7Smillert 9221acd27e7Smillert@deftypefun int rl_kill_text (int start, int end) 9231acd27e7SmillertCopy the text between @var{start} and @var{end} in the current line 9241acd27e7Smillertto the kill ring, appending or prepending to the last kill if the 9251acd27e7Smillertlast command was a kill command. The text is deleted. 9261acd27e7SmillertIf @var{start} is less than @var{end}, 9271acd27e7Smillertthe text is appended, otherwise prepended. If the last command was 9281acd27e7Smillertnot a kill, a new kill ring slot is used. 9291acd27e7Smillert@end deftypefun 9301acd27e7Smillert 931*af70c2dfSkettenis@deftypefun int rl_push_macro_input (char *macro) 932*af70c2dfSkettenisCause @var{macro} to be inserted into the line, as if it had been invoked 933*af70c2dfSkettenisby a key bound to a macro. Not especially useful; use 934*af70c2dfSkettenis@code{rl_insert_text()} instead. 9351acd27e7Smillert@end deftypefun 9361acd27e7Smillert 937*af70c2dfSkettenis@node Character Input 938*af70c2dfSkettenis@subsection Character Input 939*af70c2dfSkettenis 940*af70c2dfSkettenis@deftypefun int rl_read_key (void) 941*af70c2dfSkettenisReturn the next character available from Readline's current input stream. 942*af70c2dfSkettenisThis handles input inserted into 943*af70c2dfSkettenisthe input stream via @var{rl_pending_input} (@pxref{Readline Variables}) 944*af70c2dfSkettenisand @code{rl_stuff_char()}, macros, and characters read from the keyboard. 945*af70c2dfSkettenisWhile waiting for input, this function will call any function assigned to 946*af70c2dfSkettenisthe @code{rl_event_hook} variable. 947*af70c2dfSkettenis@end deftypefun 948*af70c2dfSkettenis 949*af70c2dfSkettenis@deftypefun int rl_getc (FILE *stream) 950*af70c2dfSkettenisReturn the next character available from @var{stream}, which is assumed to 951*af70c2dfSkettenisbe the keyboard. 9521acd27e7Smillert@end deftypefun 9531acd27e7Smillert 9541acd27e7Smillert@deftypefun int rl_stuff_char (int c) 9551acd27e7SmillertInsert @var{c} into the Readline input stream. It will be "read" 9561acd27e7Smillertbefore Readline attempts to read characters from the terminal with 957*af70c2dfSkettenis@code{rl_read_key()}. Up to 512 characters may be pushed back. 958*af70c2dfSkettenis@code{rl_stuff_char} returns 1 if the character was successfully inserted; 959*af70c2dfSkettenis0 otherwise. 960*af70c2dfSkettenis@end deftypefun 961*af70c2dfSkettenis 962*af70c2dfSkettenis@deftypefun int rl_execute_next (int c) 963*af70c2dfSkettenisMake @var{c} be the next command to be executed when @code{rl_read_key()} 964*af70c2dfSkettenisis called. This sets @var{rl_pending_input}. 965*af70c2dfSkettenis@end deftypefun 966*af70c2dfSkettenis 967*af70c2dfSkettenis@deftypefun int rl_clear_pending_input (void) 968*af70c2dfSkettenisUnset @var{rl_pending_input}, effectively negating the effect of any 969*af70c2dfSkettenisprevious call to @code{rl_execute_next()}. This works only if the 970*af70c2dfSkettenispending input has not already been read with @code{rl_read_key()}. 971*af70c2dfSkettenis@end deftypefun 972*af70c2dfSkettenis 973*af70c2dfSkettenis@deftypefun int rl_set_keyboard_input_timeout (int u) 974*af70c2dfSkettenisWhile waiting for keyboard input in @code{rl_read_key()}, Readline will 975*af70c2dfSketteniswait for @var{u} microseconds for input before calling any function 976*af70c2dfSkettenisassigned to @code{rl_event_hook}. The default waiting period is 977*af70c2dfSkettenisone-tenth of a second. Returns the old timeout value. 978*af70c2dfSkettenis@end deftypefun 979*af70c2dfSkettenis 980*af70c2dfSkettenis@node Terminal Management 981*af70c2dfSkettenis@subsection Terminal Management 982*af70c2dfSkettenis 983*af70c2dfSkettenis@deftypefun void rl_prep_terminal (int meta_flag) 984*af70c2dfSkettenisModify the terminal settings for Readline's use, so @code{readline()} 985*af70c2dfSketteniscan read a single character at a time from the keyboard. 986*af70c2dfSkettenisThe @var{meta_flag} argument should be non-zero if Readline should 987*af70c2dfSkettenisread eight-bit input. 988*af70c2dfSkettenis@end deftypefun 989*af70c2dfSkettenis 990*af70c2dfSkettenis@deftypefun void rl_deprep_terminal (void) 991*af70c2dfSkettenisUndo the effects of @code{rl_prep_terminal()}, leaving the terminal in 992*af70c2dfSkettenisthe state in which it was before the most recent call to 993*af70c2dfSkettenis@code{rl_prep_terminal()}. 994*af70c2dfSkettenis@end deftypefun 995*af70c2dfSkettenis 996*af70c2dfSkettenis@deftypefun void rl_tty_set_default_bindings (Keymap kmap) 997*af70c2dfSkettenisRead the operating system's terminal editing characters (as would be displayed 998*af70c2dfSkettenisby @code{stty}) to their Readline equivalents. The bindings are performed 999*af70c2dfSkettenisin @var{kmap}. 1000*af70c2dfSkettenis@end deftypefun 1001*af70c2dfSkettenis 1002*af70c2dfSkettenis@deftypefun int rl_reset_terminal (const char *terminal_name) 1003*af70c2dfSkettenisReinitialize Readline's idea of the terminal settings using 1004*af70c2dfSkettenis@var{terminal_name} as the terminal type (e.g., @code{vt100}). 1005*af70c2dfSkettenisIf @var{terminal_name} is @code{NULL}, the value of the @code{TERM} 1006*af70c2dfSkettenisenvironment variable is used. 1007*af70c2dfSkettenis@end deftypefun 1008*af70c2dfSkettenis 1009*af70c2dfSkettenis@node Utility Functions 1010*af70c2dfSkettenis@subsection Utility Functions 1011*af70c2dfSkettenis 1012*af70c2dfSkettenis@deftypefun void rl_replace_line (const char *text, int clear_undo) 1013*af70c2dfSkettenisReplace the contents of @code{rl_line_buffer} with @var{text}. 1014*af70c2dfSkettenisThe point and mark are preserved, if possible. 1015*af70c2dfSkettenisIf @var{clear_undo} is non-zero, the undo list associated with the 1016*af70c2dfSketteniscurrent line is cleared. 10171acd27e7Smillert@end deftypefun 10181acd27e7Smillert 10191acd27e7Smillert@deftypefun int rl_extend_line_buffer (int len) 10201acd27e7SmillertEnsure that @code{rl_line_buffer} has enough space to hold @var{len} 10211acd27e7Smillertcharacters, possibly reallocating it if necessary. 10221acd27e7Smillert@end deftypefun 10231acd27e7Smillert 1024*af70c2dfSkettenis@deftypefun int rl_initialize (void) 10251acd27e7SmillertInitialize or re-initialize Readline's internal state. 1026*af70c2dfSkettenisIt's not strictly necessary to call this; @code{readline()} calls it before 1027*af70c2dfSkettenisreading any input. 10281acd27e7Smillert@end deftypefun 10291acd27e7Smillert 1030*af70c2dfSkettenis@deftypefun int rl_ding (void) 10311acd27e7SmillertRing the terminal bell, obeying the setting of @code{bell-style}. 10321acd27e7Smillert@end deftypefun 10331acd27e7Smillert 1034*af70c2dfSkettenis@deftypefun int rl_alphabetic (int c) 1035*af70c2dfSkettenisReturn 1 if @var{c} is an alphabetic character. 1036*af70c2dfSkettenis@end deftypefun 1037*af70c2dfSkettenis 10381acd27e7Smillert@deftypefun void rl_display_match_list (char **matches, int len, int max) 10391acd27e7SmillertA convenience function for displaying a list of strings in 10401acd27e7Smillertcolumnar format on Readline's output stream. @code{matches} is the list 10411acd27e7Smillertof strings, in argv format, such as a list of completion matches. 10421acd27e7Smillert@code{len} is the number of strings in @code{matches}, and @code{max} 10431acd27e7Smillertis the length of the longest string in @code{matches}. This function uses 10441acd27e7Smillertthe setting of @code{print-completions-horizontally} to select how the 10451acd27e7Smillertmatches are displayed (@pxref{Readline Init File Syntax}). 10461acd27e7Smillert@end deftypefun 10471acd27e7Smillert 1048*af70c2dfSkettenisThe following are implemented as macros, defined in @code{chardefs.h}. 1049*af70c2dfSkettenisApplications should refrain from using them. 10501acd27e7Smillert 1051*af70c2dfSkettenis@deftypefun int _rl_uppercase_p (int c) 10521acd27e7SmillertReturn 1 if @var{c} is an uppercase alphabetic character. 10531acd27e7Smillert@end deftypefun 10541acd27e7Smillert 1055*af70c2dfSkettenis@deftypefun int _rl_lowercase_p (int c) 10561acd27e7SmillertReturn 1 if @var{c} is a lowercase alphabetic character. 10571acd27e7Smillert@end deftypefun 10581acd27e7Smillert 1059*af70c2dfSkettenis@deftypefun int _rl_digit_p (int c) 10601acd27e7SmillertReturn 1 if @var{c} is a numeric character. 10611acd27e7Smillert@end deftypefun 10621acd27e7Smillert 1063*af70c2dfSkettenis@deftypefun int _rl_to_upper (int c) 10641acd27e7SmillertIf @var{c} is a lowercase alphabetic character, return the corresponding 10651acd27e7Smillertuppercase character. 10661acd27e7Smillert@end deftypefun 10671acd27e7Smillert 1068*af70c2dfSkettenis@deftypefun int _rl_to_lower (int c) 10691acd27e7SmillertIf @var{c} is an uppercase alphabetic character, return the corresponding 10701acd27e7Smillertlowercase character. 10711acd27e7Smillert@end deftypefun 10721acd27e7Smillert 1073*af70c2dfSkettenis@deftypefun int _rl_digit_value (int c) 10741acd27e7SmillertIf @var{c} is a number, return the value it represents. 10751acd27e7Smillert@end deftypefun 10761acd27e7Smillert 1077*af70c2dfSkettenis@node Miscellaneous Functions 1078*af70c2dfSkettenis@subsection Miscellaneous Functions 1079*af70c2dfSkettenis 1080*af70c2dfSkettenis@deftypefun int rl_macro_bind (const char *keyseq, const char *macro, Keymap map) 1081*af70c2dfSkettenisBind the key sequence @var{keyseq} to invoke the macro @var{macro}. 1082*af70c2dfSkettenisThe binding is performed in @var{map}. When @var{keyseq} is invoked, the 1083*af70c2dfSkettenis@var{macro} will be inserted into the line. This function is deprecated; 1084*af70c2dfSkettenisuse @code{rl_generic_bind()} instead. 1085*af70c2dfSkettenis@end deftypefun 1086*af70c2dfSkettenis 1087*af70c2dfSkettenis@deftypefun void rl_macro_dumper (int readable) 1088*af70c2dfSkettenisPrint the key sequences bound to macros and their values, using 1089*af70c2dfSkettenisthe current keymap, to @code{rl_outstream}. 1090*af70c2dfSkettenisIf @var{readable} is non-zero, the list is formatted in such a way 1091*af70c2dfSkettenisthat it can be made part of an @code{inputrc} file and re-read. 1092*af70c2dfSkettenis@end deftypefun 1093*af70c2dfSkettenis 1094*af70c2dfSkettenis@deftypefun int rl_variable_bind (const char *variable, const char *value) 1095*af70c2dfSkettenisMake the Readline variable @var{variable} have @var{value}. 1096*af70c2dfSkettenisThis behaves as if the readline command 1097*af70c2dfSkettenis@samp{set @var{variable} @var{value}} had been executed in an @code{inputrc} 1098*af70c2dfSkettenisfile (@pxref{Readline Init File Syntax}). 1099*af70c2dfSkettenis@end deftypefun 1100*af70c2dfSkettenis 1101*af70c2dfSkettenis@deftypefun void rl_variable_dumper (int readable) 1102*af70c2dfSkettenisPrint the readline variable names and their current values 1103*af70c2dfSkettenisto @code{rl_outstream}. 1104*af70c2dfSkettenisIf @var{readable} is non-zero, the list is formatted in such a way 1105*af70c2dfSkettenisthat it can be made part of an @code{inputrc} file and re-read. 1106*af70c2dfSkettenis@end deftypefun 1107*af70c2dfSkettenis 1108*af70c2dfSkettenis@deftypefun int rl_set_paren_blink_timeout (int u) 1109*af70c2dfSkettenisSet the time interval (in microseconds) that Readline waits when showing 1110*af70c2dfSkettenisa balancing character when @code{blink-matching-paren} has been enabled. 1111*af70c2dfSkettenis@end deftypefun 1112*af70c2dfSkettenis 1113*af70c2dfSkettenis@deftypefun {char *} rl_get_termcap (const char *cap) 1114*af70c2dfSkettenisRetrieve the string value of the termcap capability @var{cap}. 1115*af70c2dfSkettenisReadline fetches the termcap entry for the current terminal name and 1116*af70c2dfSkettenisuses those capabilities to move around the screen line and perform other 1117*af70c2dfSkettenisterminal-specific operations, like erasing a line. Readline does not 1118*af70c2dfSkettenisuse all of a terminal's capabilities, and this function will return 1119*af70c2dfSkettenisvalues for only those capabilities Readline uses. 1120*af70c2dfSkettenis@end deftypefun 1121*af70c2dfSkettenis 11221acd27e7Smillert@node Alternate Interface 11231acd27e7Smillert@subsection Alternate Interface 11241acd27e7Smillert 11251acd27e7SmillertAn alternate interface is available to plain @code{readline()}. Some 11261acd27e7Smillertapplications need to interleave keyboard I/O with file, device, or 11271acd27e7Smillertwindow system I/O, typically by using a main loop to @code{select()} 11281acd27e7Smillerton various file descriptors. To accomodate this need, readline can 11291acd27e7Smillertalso be invoked as a `callback' function from an event loop. There 11301acd27e7Smillertare functions available to make this easy. 11311acd27e7Smillert 1132*af70c2dfSkettenis@deftypefun void rl_callback_handler_install (const char *prompt, rl_vcpfunc_t *lhandler) 11331acd27e7SmillertSet up the terminal for readline I/O and display the initial 11341acd27e7Smillertexpanded value of @var{prompt}. Save the value of @var{lhandler} to 1135*af70c2dfSkettenisuse as a function to call when a complete line of input has been entered. 1136*af70c2dfSkettenisThe function takes the text of the line as an argument. 11371acd27e7Smillert@end deftypefun 11381acd27e7Smillert 1139*af70c2dfSkettenis@deftypefun void rl_callback_read_char (void) 11401acd27e7SmillertWhenever an application determines that keyboard input is available, it 11411acd27e7Smillertshould call @code{rl_callback_read_char()}, which will read the next 1142*af70c2dfSkettenischaracter from the current input source. 1143*af70c2dfSkettenisIf that character completes the line, @code{rl_callback_read_char} will 1144*af70c2dfSkettenisinvoke the @var{lhandler} function saved by @code{rl_callback_handler_install} 1145*af70c2dfSkettenisto process the line. 1146*af70c2dfSkettenisBefore calling the @var{lhandler} function, the terminal settings are 1147*af70c2dfSkettenisreset to the values they had before calling 1148*af70c2dfSkettenis@code{rl_callback_handler_install}. 1149*af70c2dfSkettenisIf the @var{lhandler} function returns, 1150*af70c2dfSkettenisthe terminal settings are modified for Readline's use again. 1151*af70c2dfSkettenis@code{EOF} is indicated by calling @var{lhandler} with a 11521acd27e7Smillert@code{NULL} line. 11531acd27e7Smillert@end deftypefun 11541acd27e7Smillert 1155*af70c2dfSkettenis@deftypefun void rl_callback_handler_remove (void) 11561acd27e7SmillertRestore the terminal to its initial state and remove the line handler. 11571acd27e7SmillertThis may be called from within a callback as well as independently. 1158*af70c2dfSkettenisIf the @var{lhandler} installed by @code{rl_callback_handler_install} 1159*af70c2dfSkettenisdoes not exit the program, either this function or the function referred 1160*af70c2dfSkettenisto by the value of @code{rl_deprep_term_function} should be called before 1161*af70c2dfSkettenisthe program exits to reset the terminal settings. 11621acd27e7Smillert@end deftypefun 11631acd27e7Smillert 1164*af70c2dfSkettenis@node A Readline Example 1165*af70c2dfSkettenis@subsection A Readline Example 11661acd27e7Smillert 11671acd27e7SmillertHere is a function which changes lowercase characters to their uppercase 11681acd27e7Smillertequivalents, and uppercase characters to lowercase. If 11691acd27e7Smillertthis function was bound to @samp{M-c}, then typing @samp{M-c} would 11701acd27e7Smillertchange the case of the character under point. Typing @samp{M-1 0 M-c} 11711acd27e7Smillertwould change the case of the following 10 characters, leaving the cursor on 11721acd27e7Smillertthe last character changed. 11731acd27e7Smillert 11741acd27e7Smillert@example 11751acd27e7Smillert/* Invert the case of the COUNT following characters. */ 11761acd27e7Smillertint 11771acd27e7Smillertinvert_case_line (count, key) 11781acd27e7Smillert int count, key; 11791acd27e7Smillert@{ 11801acd27e7Smillert register int start, end, i; 11811acd27e7Smillert 11821acd27e7Smillert start = rl_point; 11831acd27e7Smillert 11841acd27e7Smillert if (rl_point >= rl_end) 11851acd27e7Smillert return (0); 11861acd27e7Smillert 11871acd27e7Smillert if (count < 0) 11881acd27e7Smillert @{ 11891acd27e7Smillert direction = -1; 11901acd27e7Smillert count = -count; 11911acd27e7Smillert @} 11921acd27e7Smillert else 11931acd27e7Smillert direction = 1; 11941acd27e7Smillert 11951acd27e7Smillert /* Find the end of the range to modify. */ 11961acd27e7Smillert end = start + (count * direction); 11971acd27e7Smillert 11981acd27e7Smillert /* Force it to be within range. */ 11991acd27e7Smillert if (end > rl_end) 12001acd27e7Smillert end = rl_end; 12011acd27e7Smillert else if (end < 0) 12021acd27e7Smillert end = 0; 12031acd27e7Smillert 12041acd27e7Smillert if (start == end) 12051acd27e7Smillert return (0); 12061acd27e7Smillert 12071acd27e7Smillert if (start > end) 12081acd27e7Smillert @{ 12091acd27e7Smillert int temp = start; 12101acd27e7Smillert start = end; 12111acd27e7Smillert end = temp; 12121acd27e7Smillert @} 12131acd27e7Smillert 1214*af70c2dfSkettenis /* Tell readline that we are modifying the line, 1215*af70c2dfSkettenis so it will save the undo information. */ 12161acd27e7Smillert rl_modifying (start, end); 12171acd27e7Smillert 12181acd27e7Smillert for (i = start; i != end; i++) 12191acd27e7Smillert @{ 1220*af70c2dfSkettenis if (_rl_uppercase_p (rl_line_buffer[i])) 1221*af70c2dfSkettenis rl_line_buffer[i] = _rl_to_lower (rl_line_buffer[i]); 1222*af70c2dfSkettenis else if (_rl_lowercase_p (rl_line_buffer[i])) 1223*af70c2dfSkettenis rl_line_buffer[i] = _rl_to_upper (rl_line_buffer[i]); 12241acd27e7Smillert @} 12251acd27e7Smillert /* Move point to on top of the last character changed. */ 12261acd27e7Smillert rl_point = (direction == 1) ? end - 1 : start; 12271acd27e7Smillert return (0); 12281acd27e7Smillert@} 12291acd27e7Smillert@end example 12301acd27e7Smillert 12311acd27e7Smillert@node Readline Signal Handling 12321acd27e7Smillert@section Readline Signal Handling 12331acd27e7Smillert 12341acd27e7SmillertSignals are asynchronous events sent to a process by the Unix kernel, 12351acd27e7Smillertsometimes on behalf of another process. They are intended to indicate 1236*af70c2dfSkettenisexceptional events, like a user pressing the interrupt key on his terminal, 1237*af70c2dfSkettenisor a network connection being broken. There is a class of signals that can 1238*af70c2dfSkettenisbe sent to the process currently reading input from the keyboard. Since 1239*af70c2dfSkettenisReadline changes the terminal attributes when it is called, it needs to 1240*af70c2dfSkettenisperform special processing when such a signal is received in order to 1241*af70c2dfSkettenisrestore the terminal to a sane state, or provide application writers with 1242*af70c2dfSkettenisfunctions to do so manually. 12431acd27e7Smillert 12441acd27e7SmillertReadline contains an internal signal handler that is installed for a 12451acd27e7Smillertnumber of signals (@code{SIGINT}, @code{SIGQUIT}, @code{SIGTERM}, 12461acd27e7Smillert@code{SIGALRM}, @code{SIGTSTP}, @code{SIGTTIN}, and @code{SIGTTOU}). 12471acd27e7SmillertWhen one of these signals is received, the signal handler 12481acd27e7Smillertwill reset the terminal attributes to those that were in effect before 12491acd27e7Smillert@code{readline()} was called, reset the signal handling to what it was 12501acd27e7Smillertbefore @code{readline()} was called, and resend the signal to the calling 12511acd27e7Smillertapplication. 12521acd27e7SmillertIf and when the calling application's signal handler returns, Readline 12531acd27e7Smillertwill reinitialize the terminal and continue to accept input. 12541acd27e7SmillertWhen a @code{SIGINT} is received, the Readline signal handler performs 12551acd27e7Smillertsome additional work, which will cause any partially-entered line to be 1256*af70c2dfSkettenisaborted (see the description of @code{rl_free_line_state()} below). 12571acd27e7Smillert 12581acd27e7SmillertThere is an additional Readline signal handler, for @code{SIGWINCH}, which 12591acd27e7Smillertthe kernel sends to a process whenever the terminal's size changes (for 12601acd27e7Smillertexample, if a user resizes an @code{xterm}). The Readline @code{SIGWINCH} 1261*af70c2dfSkettenishandler updates Readline's internal screen size information, and then calls 1262*af70c2dfSkettenisany @code{SIGWINCH} signal handler the calling application has installed. 12631acd27e7SmillertReadline calls the application's @code{SIGWINCH} signal handler without 12641acd27e7Smillertresetting the terminal to its original state. If the application's signal 12651acd27e7Smillerthandler does more than update its idea of the terminal size and return (for 12661acd27e7Smillertexample, a @code{longjmp} back to a main processing loop), it @emph{must} 12671acd27e7Smillertcall @code{rl_cleanup_after_signal()} (described below), to restore the 12681acd27e7Smillertterminal state. 12691acd27e7Smillert 12701acd27e7SmillertReadline provides two variables that allow application writers to 12711acd27e7Smillertcontrol whether or not it will catch certain signals and act on them 12721acd27e7Smillertwhen they are received. It is important that applications change the 12731acd27e7Smillertvalues of these variables only when calling @code{readline()}, not in 12741acd27e7Smillerta signal handler, so Readline's internal signal state is not corrupted. 12751acd27e7Smillert 12761acd27e7Smillert@deftypevar int rl_catch_signals 12771acd27e7SmillertIf this variable is non-zero, Readline will install signal handlers for 12781acd27e7Smillert@code{SIGINT}, @code{SIGQUIT}, @code{SIGTERM}, @code{SIGALRM}, 12791acd27e7Smillert@code{SIGTSTP}, @code{SIGTTIN}, and @code{SIGTTOU}. 12801acd27e7Smillert 12811acd27e7SmillertThe default value of @code{rl_catch_signals} is 1. 12821acd27e7Smillert@end deftypevar 12831acd27e7Smillert 12841acd27e7Smillert@deftypevar int rl_catch_sigwinch 12851acd27e7SmillertIf this variable is non-zero, Readline will install a signal handler for 12861acd27e7Smillert@code{SIGWINCH}. 12871acd27e7Smillert 12881acd27e7SmillertThe default value of @code{rl_catch_sigwinch} is 1. 12891acd27e7Smillert@end deftypevar 12901acd27e7Smillert 12911acd27e7SmillertIf an application does not wish to have Readline catch any signals, or 12921acd27e7Smillertto handle signals other than those Readline catches (@code{SIGHUP}, 12931acd27e7Smillertfor example), 12941acd27e7SmillertReadline provides convenience functions to do the necessary terminal 12951acd27e7Smillertand internal state cleanup upon receipt of a signal. 12961acd27e7Smillert 12971acd27e7Smillert@deftypefun void rl_cleanup_after_signal (void) 12981acd27e7SmillertThis function will reset the state of the terminal to what it was before 12991acd27e7Smillert@code{readline()} was called, and remove the Readline signal handlers for 13001acd27e7Smillertall signals, depending on the values of @code{rl_catch_signals} and 13011acd27e7Smillert@code{rl_catch_sigwinch}. 13021acd27e7Smillert@end deftypefun 13031acd27e7Smillert 13041acd27e7Smillert@deftypefun void rl_free_line_state (void) 13051acd27e7SmillertThis will free any partial state associated with the current input line 13061acd27e7Smillert(undo information, any partial history entry, any partially-entered 13071acd27e7Smillertkeyboard macro, and any partially-entered numeric argument). This 13081acd27e7Smillertshould be called before @code{rl_cleanup_after_signal()}. The 13091acd27e7SmillertReadline signal handler for @code{SIGINT} calls this to abort the 13101acd27e7Smillertcurrent input line. 13111acd27e7Smillert@end deftypefun 13121acd27e7Smillert 13131acd27e7Smillert@deftypefun void rl_reset_after_signal (void) 13141acd27e7SmillertThis will reinitialize the terminal and reinstall any Readline signal 13151acd27e7Smillerthandlers, depending on the values of @code{rl_catch_signals} and 13161acd27e7Smillert@code{rl_catch_sigwinch}. 13171acd27e7Smillert@end deftypefun 13181acd27e7Smillert 13191acd27e7SmillertIf an application does not wish Readline to catch @code{SIGWINCH}, it may 1320*af70c2dfSketteniscall @code{rl_resize_terminal()} or @code{rl_set_screen_size()} to force 1321*af70c2dfSkettenisReadline to update its idea of the terminal size when a @code{SIGWINCH} 1322*af70c2dfSkettenisis received. 13231acd27e7Smillert 13241acd27e7Smillert@deftypefun void rl_resize_terminal (void) 1325*af70c2dfSkettenisUpdate Readline's internal screen size by reading values from the kernel. 1326*af70c2dfSkettenis@end deftypefun 1327*af70c2dfSkettenis 1328*af70c2dfSkettenis@deftypefun void rl_set_screen_size (int rows, int cols) 1329*af70c2dfSkettenisSet Readline's idea of the terminal size to @var{rows} rows and 1330*af70c2dfSkettenis@var{cols} columns. 1331*af70c2dfSkettenis@end deftypefun 1332*af70c2dfSkettenis 1333*af70c2dfSkettenisIf an application does not want to install a @code{SIGWINCH} handler, but 1334*af70c2dfSkettenisis still interested in the screen dimensions, Readline's idea of the screen 1335*af70c2dfSkettenissize may be queried. 1336*af70c2dfSkettenis 1337*af70c2dfSkettenis@deftypefun void rl_get_screen_size (int *rows, int *cols) 1338*af70c2dfSkettenisReturn Readline's idea of the terminal's size in the 1339*af70c2dfSkettenisvariables pointed to by the arguments. 13401acd27e7Smillert@end deftypefun 13411acd27e7Smillert 13421acd27e7SmillertThe following functions install and remove Readline's signal handlers. 13431acd27e7Smillert 13441acd27e7Smillert@deftypefun int rl_set_signals (void) 13451acd27e7SmillertInstall Readline's signal handler for @code{SIGINT}, @code{SIGQUIT}, 13461acd27e7Smillert@code{SIGTERM}, @code{SIGALRM}, @code{SIGTSTP}, @code{SIGTTIN}, 13471acd27e7Smillert@code{SIGTTOU}, and @code{SIGWINCH}, depending on the values of 13481acd27e7Smillert@code{rl_catch_signals} and @code{rl_catch_sigwinch}. 13491acd27e7Smillert@end deftypefun 13501acd27e7Smillert 13511acd27e7Smillert@deftypefun int rl_clear_signals (void) 13521acd27e7SmillertRemove all of the Readline signal handlers installed by 13531acd27e7Smillert@code{rl_set_signals()}. 13541acd27e7Smillert@end deftypefun 13551acd27e7Smillert 13561acd27e7Smillert@node Custom Completers 13571acd27e7Smillert@section Custom Completers 13581acd27e7Smillert 13591acd27e7SmillertTypically, a program that reads commands from the user has a way of 13601acd27e7Smillertdisambiguating commands and data. If your program is one of these, then 13611acd27e7Smillertit can provide completion for commands, data, or both. 13621acd27e7SmillertThe following sections describe how your program and Readline 13631acd27e7Smillertcooperate to provide this service. 13641acd27e7Smillert 13651acd27e7Smillert@menu 13661acd27e7Smillert* How Completing Works:: The logic used to do completion. 13671acd27e7Smillert* Completion Functions:: Functions provided by Readline. 13681acd27e7Smillert* Completion Variables:: Variables which control completion. 13691acd27e7Smillert* A Short Completion Example:: An example of writing completer subroutines. 13701acd27e7Smillert@end menu 13711acd27e7Smillert 13721acd27e7Smillert@node How Completing Works 13731acd27e7Smillert@subsection How Completing Works 13741acd27e7Smillert 13751acd27e7SmillertIn order to complete some text, the full list of possible completions 13761acd27e7Smillertmust be available. That is, it is not possible to accurately 13771acd27e7Smillertexpand a partial word without knowing all of the possible words 13781acd27e7Smillertwhich make sense in that context. The Readline library provides 13791acd27e7Smillertthe user interface to completion, and two of the most common 13801acd27e7Smillertcompletion functions: filename and username. For completing other types 13811acd27e7Smillertof text, you must write your own completion function. This section 13821acd27e7Smillertdescribes exactly what such functions must do, and provides an example. 13831acd27e7Smillert 13841acd27e7SmillertThere are three major functions used to perform completion: 13851acd27e7Smillert 13861acd27e7Smillert@enumerate 13871acd27e7Smillert@item 13881acd27e7SmillertThe user-interface function @code{rl_complete()}. This function is 1389*af70c2dfSketteniscalled with the same arguments as other bindable Readline functions: 1390*af70c2dfSkettenis@var{count} and @var{invoking_key}. 1391*af70c2dfSkettenisIt isolates the word to be completed and calls 1392*af70c2dfSkettenis@code{rl_completion_matches()} to generate a list of possible completions. 13931acd27e7SmillertIt then either lists the possible completions, inserts the possible 13941acd27e7Smillertcompletions, or actually performs the 13951acd27e7Smillertcompletion, depending on which behavior is desired. 13961acd27e7Smillert 13971acd27e7Smillert@item 1398*af70c2dfSkettenisThe internal function @code{rl_completion_matches()} uses an 1399*af70c2dfSkettenisapplication-supplied @dfn{generator} function to generate the list of 1400*af70c2dfSkettenispossible matches, and then returns the array of these matches. 1401*af70c2dfSkettenisThe caller should place the address of its generator function in 1402*af70c2dfSkettenis@code{rl_completion_entry_function}. 14031acd27e7Smillert 14041acd27e7Smillert@item 14051acd27e7SmillertThe generator function is called repeatedly from 1406*af70c2dfSkettenis@code{rl_completion_matches()}, returning a string each time. The 14071acd27e7Smillertarguments to the generator function are @var{text} and @var{state}. 14081acd27e7Smillert@var{text} is the partial word to be completed. @var{state} is zero the 14091acd27e7Smillertfirst time the function is called, allowing the generator to perform 14101acd27e7Smillertany necessary initialization, and a positive non-zero integer for 1411*af70c2dfSketteniseach subsequent call. The generator function returns 1412*af70c2dfSkettenis@code{(char *)NULL} to inform @code{rl_completion_matches()} that there are 14131acd27e7Smillertno more possibilities left. Usually the generator function computes the 14141acd27e7Smillertlist of possible completions when @var{state} is zero, and returns them 14151acd27e7Smillertone at a time on subsequent calls. Each string the generator function 14161acd27e7Smillertreturns as a match must be allocated with @code{malloc()}; Readline 14171acd27e7Smillertfrees the strings when it has finished with them. 14181acd27e7Smillert 14191acd27e7Smillert@end enumerate 14201acd27e7Smillert 14211acd27e7Smillert@deftypefun int rl_complete (int ignore, int invoking_key) 14221acd27e7SmillertComplete the word at or before point. You have supplied the function 14231acd27e7Smillertthat does the initial simple matching selection algorithm (see 1424*af70c2dfSkettenis@code{rl_completion_matches()}). The default is to do filename completion. 14251acd27e7Smillert@end deftypefun 14261acd27e7Smillert 1427*af70c2dfSkettenis@deftypevar {rl_compentry_func_t *} rl_completion_entry_function 1428*af70c2dfSkettenisThis is a pointer to the generator function for 1429*af70c2dfSkettenis@code{rl_completion_matches()}. 1430*af70c2dfSkettenisIf the value of @code{rl_completion_entry_function} is 1431*af70c2dfSkettenis@code{NULL} then the default filename generator 1432*af70c2dfSkettenisfunction, @code{rl_filename_completion_function()}, is used. 14331acd27e7Smillert@end deftypevar 14341acd27e7Smillert 14351acd27e7Smillert@node Completion Functions 14361acd27e7Smillert@subsection Completion Functions 14371acd27e7Smillert 14381acd27e7SmillertHere is the complete list of callable completion functions present in 14391acd27e7SmillertReadline. 14401acd27e7Smillert 14411acd27e7Smillert@deftypefun int rl_complete_internal (int what_to_do) 14421acd27e7SmillertComplete the word at or before point. @var{what_to_do} says what to do 14431acd27e7Smillertwith the completion. A value of @samp{?} means list the possible 14441acd27e7Smillertcompletions. @samp{TAB} means do standard completion. @samp{*} means 14451acd27e7Smillertinsert all of the possible completions. @samp{!} means to display 14461acd27e7Smillertall of the possible completions, if there is more than one, as well as 14471acd27e7Smillertperforming partial completion. 14481acd27e7Smillert@end deftypefun 14491acd27e7Smillert 14501acd27e7Smillert@deftypefun int rl_complete (int ignore, int invoking_key) 14511acd27e7SmillertComplete the word at or before point. You have supplied the function 14521acd27e7Smillertthat does the initial simple matching selection algorithm (see 1453*af70c2dfSkettenis@code{rl_completion_matches()} and @code{rl_completion_entry_function}). 14541acd27e7SmillertThe default is to do filename 14551acd27e7Smillertcompletion. This calls @code{rl_complete_internal()} with an 14561acd27e7Smillertargument depending on @var{invoking_key}. 14571acd27e7Smillert@end deftypefun 14581acd27e7Smillert 1459*af70c2dfSkettenis@deftypefun int rl_possible_completions (int count, int invoking_key) 14601acd27e7SmillertList the possible completions. See description of @code{rl_complete 14611acd27e7Smillert()}. This calls @code{rl_complete_internal()} with an argument of 14621acd27e7Smillert@samp{?}. 14631acd27e7Smillert@end deftypefun 14641acd27e7Smillert 1465*af70c2dfSkettenis@deftypefun int rl_insert_completions (int count, int invoking_key) 14661acd27e7SmillertInsert the list of possible completions into the line, deleting the 14671acd27e7Smillertpartially-completed word. See description of @code{rl_complete()}. 14681acd27e7SmillertThis calls @code{rl_complete_internal()} with an argument of @samp{*}. 14691acd27e7Smillert@end deftypefun 14701acd27e7Smillert 1471*af70c2dfSkettenis@deftypefun int rl_completion_mode (rl_command_func_t *cfunc) 1472*af70c2dfSkettenisReturns the apppriate value to pass to @code{rl_complete_internal()} 1473*af70c2dfSkettenisdepending on whether @var{cfunc} was called twice in succession and 1474*af70c2dfSkettenisthe value of the @code{show-all-if-ambiguous} variable. 1475*af70c2dfSkettenisApplication-specific completion functions may use this function to present 1476*af70c2dfSkettenisthe same interface as @code{rl_complete()}. 1477*af70c2dfSkettenis@end deftypefun 1478*af70c2dfSkettenis 1479*af70c2dfSkettenis@deftypefun {char **} rl_completion_matches (const char *text, rl_compentry_func_t *entry_func) 1480*af70c2dfSkettenisReturns an array of strings which is a list of completions for 1481*af70c2dfSkettenis@var{text}. If there are no completions, returns @code{NULL}. 14821acd27e7SmillertThe first entry in the returned array is the substitution for @var{text}. 14831acd27e7SmillertThe remaining entries are the possible completions. The array is 14841acd27e7Smillertterminated with a @code{NULL} pointer. 14851acd27e7Smillert 14861acd27e7Smillert@var{entry_func} is a function of two args, and returns a 1487*af70c2dfSkettenis@code{char *}. The first argument is @var{text}. The second is a 14881acd27e7Smillertstate argument; it is zero on the first call, and non-zero on subsequent 14891acd27e7Smillertcalls. @var{entry_func} returns a @code{NULL} pointer to the caller 14901acd27e7Smillertwhen there are no more matches. 14911acd27e7Smillert@end deftypefun 14921acd27e7Smillert 1493*af70c2dfSkettenis@deftypefun {char *} rl_filename_completion_function (const char *text, int state) 1494*af70c2dfSkettenisA generator function for filename completion in the general case. 1495*af70c2dfSkettenis@var{text} is a partial filename. 1496*af70c2dfSkettenisThe Bash source is a useful reference for writing custom 1497*af70c2dfSketteniscompletion functions (the Bash completion functions call this and other 1498*af70c2dfSkettenisReadline functions). 14991acd27e7Smillert@end deftypefun 15001acd27e7Smillert 1501*af70c2dfSkettenis@deftypefun {char *} rl_username_completion_function (const char *text, int state) 15021acd27e7SmillertA completion generator for usernames. @var{text} contains a partial 15031acd27e7Smillertusername preceded by a random character (usually @samp{~}). As with all 15041acd27e7Smillertcompletion generators, @var{state} is zero on the first call and non-zero 15051acd27e7Smillertfor subsequent calls. 15061acd27e7Smillert@end deftypefun 15071acd27e7Smillert 15081acd27e7Smillert@node Completion Variables 15091acd27e7Smillert@subsection Completion Variables 15101acd27e7Smillert 1511*af70c2dfSkettenis@deftypevar {rl_compentry_func_t *} rl_completion_entry_function 1512*af70c2dfSkettenisA pointer to the generator function for @code{rl_completion_matches()}. 1513*af70c2dfSkettenis@code{NULL} means to use @code{rl_filename_completion_function()}, the default 15141acd27e7Smillertfilename completer. 15151acd27e7Smillert@end deftypevar 15161acd27e7Smillert 1517*af70c2dfSkettenis@deftypevar {rl_completion_func_t *} rl_attempted_completion_function 15181acd27e7SmillertA pointer to an alternative function to create matches. 15191acd27e7SmillertThe function is called with @var{text}, @var{start}, and @var{end}. 1520*af70c2dfSkettenis@var{start} and @var{end} are indices in @code{rl_line_buffer} defining 1521*af70c2dfSkettenisthe boundaries of @var{text}, which is a character string. 1522*af70c2dfSkettenisIf this function exists and returns @code{NULL}, or if this variable is 1523*af70c2dfSkettenisset to @code{NULL}, then @code{rl_complete()} will call the value of 15241acd27e7Smillert@code{rl_completion_entry_function} to generate matches, otherwise the 15251acd27e7Smillertarray of strings returned will be used. 1526*af70c2dfSkettenisIf this function sets the @code{rl_attempted_completion_over} 1527*af70c2dfSkettenisvariable to a non-zero value, Readline will not perform its default 1528*af70c2dfSketteniscompletion even if this function returns no matches. 15291acd27e7Smillert@end deftypevar 15301acd27e7Smillert 1531*af70c2dfSkettenis@deftypevar {rl_quote_func_t *} rl_filename_quoting_function 1532*af70c2dfSkettenisA pointer to a function that will quote a filename in an 1533*af70c2dfSkettenisapplication-specific fashion. This is called if filename completion is being 15341acd27e7Smillertattempted and one of the characters in @code{rl_filename_quote_characters} 15351acd27e7Smillertappears in a completed filename. The function is called with 15361acd27e7Smillert@var{text}, @var{match_type}, and @var{quote_pointer}. The @var{text} 15371acd27e7Smillertis the filename to be quoted. The @var{match_type} is either 15381acd27e7Smillert@code{SINGLE_MATCH}, if there is only one completion match, or 15391acd27e7Smillert@code{MULT_MATCH}. Some functions use this to decide whether or not to 15401acd27e7Smillertinsert a closing quote character. The @var{quote_pointer} is a pointer 15411acd27e7Smillertto any opening quote character the user typed. Some functions choose 15421acd27e7Smillertto reset this character. 15431acd27e7Smillert@end deftypevar 15441acd27e7Smillert 1545*af70c2dfSkettenis@deftypevar {rl_dequote_func_t *} rl_filename_dequoting_function 15461acd27e7SmillertA pointer to a function that will remove application-specific quoting 15471acd27e7Smillertcharacters from a filename before completion is attempted, so those 15481acd27e7Smillertcharacters do not interfere with matching the text against names in 15491acd27e7Smillertthe filesystem. It is called with @var{text}, the text of the word 15501acd27e7Smillertto be dequoted, and @var{quote_char}, which is the quoting character 15511acd27e7Smillertthat delimits the filename (usually @samp{'} or @samp{"}). If 15521acd27e7Smillert@var{quote_char} is zero, the filename was not in an embedded string. 15531acd27e7Smillert@end deftypevar 15541acd27e7Smillert 1555*af70c2dfSkettenis@deftypevar {rl_linebuf_func_t *} rl_char_is_quoted_p 15561acd27e7SmillertA pointer to a function to call that determines whether or not a specific 15571acd27e7Smillertcharacter in the line buffer is quoted, according to whatever quoting 1558*af70c2dfSkettenismechanism the program calling Readline uses. The function is called with 15591acd27e7Smillerttwo arguments: @var{text}, the text of the line, and @var{index}, the 15601acd27e7Smillertindex of the character in the line. It is used to decide whether a 15611acd27e7Smillertcharacter found in @code{rl_completer_word_break_characters} should be 15621acd27e7Smillertused to break words for the completer. 15631acd27e7Smillert@end deftypevar 15641acd27e7Smillert 1565*af70c2dfSkettenis@deftypevar {rl_compignore_func_t *} rl_ignore_some_completions_function 1566*af70c2dfSkettenisThis function, if defined, is called by the completer when real filename 1567*af70c2dfSketteniscompletion is done, after all the matching names have been generated. 1568*af70c2dfSkettenisIt is passed a @code{NULL} terminated array of matches. 1569*af70c2dfSkettenisThe first element (@code{matches[0]}) is the 1570*af70c2dfSkettenismaximal substring common to all matches. This function can 1571*af70c2dfSkettenisre-arrange the list of matches as required, but each element deleted 1572*af70c2dfSkettenisfrom the array must be freed. 15731acd27e7Smillert@end deftypevar 15741acd27e7Smillert 1575*af70c2dfSkettenis@deftypevar {rl_icppfunc_t *} rl_directory_completion_hook 1576*af70c2dfSkettenisThis function, if defined, is allowed to modify the directory portion 1577*af70c2dfSkettenisof filenames Readline completes. It is called with the address of a 1578*af70c2dfSkettenisstring (the current directory name) as an argument, and may modify that string. 1579*af70c2dfSkettenisIf the string is replaced with a new string, the old value should be freed. 1580*af70c2dfSkettenisAny modified directory name should have a trailing slash. 1581*af70c2dfSkettenisThe modified value will be displayed as part of the completion, replacing 1582*af70c2dfSkettenisthe directory portion of the pathname the user typed. 1583*af70c2dfSkettenisIt returns an integer that should be non-zero if the function modifies 1584*af70c2dfSkettenisits directory argument. 1585*af70c2dfSkettenisIt could be used to expand symbolic links or shell variables in pathnames. 1586*af70c2dfSkettenis@end deftypevar 1587*af70c2dfSkettenis 1588*af70c2dfSkettenis@deftypevar {rl_compdisp_func_t *} rl_completion_display_matches_hook 1589*af70c2dfSkettenisIf non-zero, then this is the address of a function to call when 1590*af70c2dfSketteniscompleting a word would normally display the list of possible matches. 1591*af70c2dfSkettenisThis function is called in lieu of Readline displaying the list. 1592*af70c2dfSkettenisIt takes three arguments: 1593*af70c2dfSkettenis(@code{char **}@var{matches}, @code{int} @var{num_matches}, @code{int} @var{max_length}) 1594*af70c2dfSketteniswhere @var{matches} is the array of matching strings, 1595*af70c2dfSkettenis@var{num_matches} is the number of strings in that array, and 1596*af70c2dfSkettenis@var{max_length} is the length of the longest string in that array. 1597*af70c2dfSkettenisReadline provides a convenience function, @code{rl_display_match_list}, 1598*af70c2dfSkettenisthat takes care of doing the display to Readline's output stream. That 1599*af70c2dfSkettenisfunction may be called from this hook. 1600*af70c2dfSkettenis@end deftypevar 1601*af70c2dfSkettenis 1602*af70c2dfSkettenis@deftypevar {const char *} rl_basic_word_break_characters 16031acd27e7SmillertThe basic list of characters that signal a break between words for the 16041acd27e7Smillertcompleter routine. The default value of this variable is the characters 1605*af70c2dfSketteniswhich break words for completion in Bash: 16061acd27e7Smillert@code{" \t\n\"\\'`@@$><=;|&@{("}. 16071acd27e7Smillert@end deftypevar 16081acd27e7Smillert 1609*af70c2dfSkettenis@deftypevar {const char *} rl_basic_quote_characters 1610*af70c2dfSkettenisA list of quote characters which can cause a word break. 16111acd27e7Smillert@end deftypevar 16121acd27e7Smillert 1613*af70c2dfSkettenis@deftypevar {const char *} rl_completer_word_break_characters 16141acd27e7SmillertThe list of characters that signal a break between words for 16151acd27e7Smillert@code{rl_complete_internal()}. The default list is the value of 16161acd27e7Smillert@code{rl_basic_word_break_characters}. 16171acd27e7Smillert@end deftypevar 16181acd27e7Smillert 1619*af70c2dfSkettenis@deftypevar {const char *} rl_completer_quote_characters 1620*af70c2dfSkettenisA list of characters which can be used to quote a substring of the line. 16211acd27e7SmillertCompletion occurs on the entire substring, and within the substring 16221acd27e7Smillert@code{rl_completer_word_break_characters} are treated as any other character, 16231acd27e7Smillertunless they also appear within this list. 16241acd27e7Smillert@end deftypevar 16251acd27e7Smillert 1626*af70c2dfSkettenis@deftypevar {const char *} rl_filename_quote_characters 16271acd27e7SmillertA list of characters that cause a filename to be quoted by the completer 16281acd27e7Smillertwhen they appear in a completed filename. The default is the null string. 16291acd27e7Smillert@end deftypevar 16301acd27e7Smillert 1631*af70c2dfSkettenis@deftypevar {const char *} rl_special_prefixes 16321acd27e7SmillertThe list of characters that are word break characters, but should be 16331acd27e7Smillertleft in @var{text} when it is passed to the completion function. 16341acd27e7SmillertPrograms can use this to help determine what kind of completing to do. 16351acd27e7SmillertFor instance, Bash sets this variable to "$@@" so that it can complete 16361acd27e7Smillertshell variables and hostnames. 16371acd27e7Smillert@end deftypevar 16381acd27e7Smillert 1639*af70c2dfSkettenis@deftypevar int rl_completion_query_items 1640*af70c2dfSkettenisUp to this many items will be displayed in response to a 1641*af70c2dfSkettenispossible-completions call. After that, we ask the user if she is sure 1642*af70c2dfSkettenisshe wants to see them all. The default value is 100. 1643*af70c2dfSkettenis@end deftypevar 1644*af70c2dfSkettenis 16451acd27e7Smillert@deftypevar {int} rl_completion_append_character 16461acd27e7SmillertWhen a single completion alternative matches at the end of the command 16471acd27e7Smillertline, this character is appended to the inserted completion text. The 16481acd27e7Smillertdefault is a space character (@samp{ }). Setting this to the null 16491acd27e7Smillertcharacter (@samp{\0}) prevents anything being appended automatically. 16501acd27e7SmillertThis can be changed in custom completion functions to 16511acd27e7Smillertprovide the ``most sensible word separator character'' according to 16521acd27e7Smillertan application-specific command line syntax specification. 16531acd27e7Smillert@end deftypevar 16541acd27e7Smillert 1655*af70c2dfSkettenis@deftypevar int rl_completion_suppress_append 1656*af70c2dfSkettenisIf non-zero, @var{rl_completion_append_character} is not appended to 1657*af70c2dfSkettenismatches at the end of the command line, as described above. It is 1658*af70c2dfSkettenisset to 0 before any application-specific completion function is called. 1659*af70c2dfSkettenis@end deftypevar 1660*af70c2dfSkettenis 1661*af70c2dfSkettenis@deftypevar int rl_completion_mark_symlink_dirs 1662*af70c2dfSkettenisIf non-zero, a slash will be appended to completed filenames that are 1663*af70c2dfSkettenissymbolic links to directory names, subject to the value of the 1664*af70c2dfSkettenisuser-settable @var{mark-directories} variable. 1665*af70c2dfSkettenisThis variable exists so that application completion functions can 1666*af70c2dfSkettenisoverride the user's global preference (set via the 1667*af70c2dfSkettenis@var{mark-symlinked-directories} Readline variable) if appropriate. 1668*af70c2dfSkettenisThis variable is set to the user's preference before any 1669*af70c2dfSkettenisapplication completion function is called, so unless that function 1670*af70c2dfSkettenismodifies the value, the user's preferences are honored. 1671*af70c2dfSkettenis@end deftypevar 1672*af70c2dfSkettenis 16731acd27e7Smillert@deftypevar int rl_ignore_completion_duplicates 1674*af70c2dfSkettenisIf non-zero, then duplicates in the matches are removed. 1675*af70c2dfSkettenisThe default is 1. 16761acd27e7Smillert@end deftypevar 16771acd27e7Smillert 16781acd27e7Smillert@deftypevar int rl_filename_completion_desired 16791acd27e7SmillertNon-zero means that the results of the matches are to be treated as 16801acd27e7Smillertfilenames. This is @emph{always} zero on entry, and can only be changed 16811acd27e7Smillertwithin a completion entry generator function. If it is set to a non-zero 16821acd27e7Smillertvalue, directory names have a slash appended and Readline attempts to 1683*af70c2dfSkettenisquote completed filenames if they contain any characters in 1684*af70c2dfSkettenis@code{rl_filename_quote_characters} and @code{rl_filename_quoting_desired} 1685*af70c2dfSkettenisis set to a non-zero value. 16861acd27e7Smillert@end deftypevar 16871acd27e7Smillert 16881acd27e7Smillert@deftypevar int rl_filename_quoting_desired 16891acd27e7SmillertNon-zero means that the results of the matches are to be quoted using 16901acd27e7Smillertdouble quotes (or an application-specific quoting mechanism) if the 16911acd27e7Smillertcompleted filename contains any characters in 16921acd27e7Smillert@code{rl_filename_quote_chars}. This is @emph{always} non-zero 16931acd27e7Smillerton entry, and can only be changed within a completion entry generator 16941acd27e7Smillertfunction. The quoting is effected via a call to the function pointed to 16951acd27e7Smillertby @code{rl_filename_quoting_function}. 16961acd27e7Smillert@end deftypevar 16971acd27e7Smillert 1698*af70c2dfSkettenis@deftypevar int rl_attempted_completion_over 1699*af70c2dfSkettenisIf an application-specific completion function assigned to 1700*af70c2dfSkettenis@code{rl_attempted_completion_function} sets this variable to a non-zero 1701*af70c2dfSkettenisvalue, Readline will not perform its default filename completion even 1702*af70c2dfSkettenisif the application's completion function returns no matches. 1703*af70c2dfSkettenisIt should be set only by an application's completion function. 1704*af70c2dfSkettenis@end deftypevar 1705*af70c2dfSkettenis 1706*af70c2dfSkettenis@deftypevar int rl_completion_type 1707*af70c2dfSkettenisSet to a character describing the type of completion Readline is currently 1708*af70c2dfSkettenisattempting; see the description of @code{rl_complete_internal()} 1709*af70c2dfSkettenis(@pxref{Completion Functions}) for the list of characters. 1710*af70c2dfSkettenis@end deftypevar 1711*af70c2dfSkettenis 17121acd27e7Smillert@deftypevar int rl_inhibit_completion 1713*af70c2dfSkettenisIf this variable is non-zero, completion is inhibited. The completion 17141acd27e7Smillertcharacter will be inserted as any other bound to @code{self-insert}. 17151acd27e7Smillert@end deftypevar 17161acd27e7Smillert 17171acd27e7Smillert@node A Short Completion Example 17181acd27e7Smillert@subsection A Short Completion Example 17191acd27e7Smillert 17201acd27e7SmillertHere is a small application demonstrating the use of the GNU Readline 17211acd27e7Smillertlibrary. It is called @code{fileman}, and the source code resides in 17221acd27e7Smillert@file{examples/fileman.c}. This sample application provides 17231acd27e7Smillertcompletion of command names, line editing features, and access to the 17241acd27e7Smillerthistory list. 17251acd27e7Smillert 17261acd27e7Smillert@page 17271acd27e7Smillert@smallexample 17281acd27e7Smillert/* fileman.c -- A tiny application which demonstrates how to use the 17291acd27e7Smillert GNU Readline library. This application interactively allows users 17301acd27e7Smillert to manipulate files and their modes. */ 17311acd27e7Smillert 17321acd27e7Smillert#include <stdio.h> 17331acd27e7Smillert#include <sys/types.h> 17341acd27e7Smillert#include <sys/file.h> 17351acd27e7Smillert#include <sys/stat.h> 17361acd27e7Smillert#include <sys/errno.h> 17371acd27e7Smillert 17381acd27e7Smillert#include <readline/readline.h> 17391acd27e7Smillert#include <readline/history.h> 17401acd27e7Smillert 17411acd27e7Smillertextern char *xmalloc (); 17421acd27e7Smillert 17431acd27e7Smillert/* The names of functions that actually do the manipulation. */ 1744*af70c2dfSkettenisint com_list __P((char *)); 1745*af70c2dfSkettenisint com_view __P((char *)); 1746*af70c2dfSkettenisint com_rename __P((char *)); 1747*af70c2dfSkettenisint com_stat __P((char *)); 1748*af70c2dfSkettenisint com_pwd __P((char *)); 1749*af70c2dfSkettenisint com_delete __P((char *)); 1750*af70c2dfSkettenisint com_help __P((char *)); 1751*af70c2dfSkettenisint com_cd __P((char *)); 1752*af70c2dfSkettenisint com_quit __P((char *)); 17531acd27e7Smillert 17541acd27e7Smillert/* A structure which contains information on the commands this program 17551acd27e7Smillert can understand. */ 17561acd27e7Smillert 17571acd27e7Smillerttypedef struct @{ 17581acd27e7Smillert char *name; /* User printable name of the function. */ 1759*af70c2dfSkettenis rl_icpfunc_t *func; /* Function to call to do the job. */ 17601acd27e7Smillert char *doc; /* Documentation for this function. */ 17611acd27e7Smillert@} COMMAND; 17621acd27e7Smillert 17631acd27e7SmillertCOMMAND commands[] = @{ 17641acd27e7Smillert @{ "cd", com_cd, "Change to directory DIR" @}, 17651acd27e7Smillert @{ "delete", com_delete, "Delete FILE" @}, 17661acd27e7Smillert @{ "help", com_help, "Display this text" @}, 17671acd27e7Smillert @{ "?", com_help, "Synonym for `help'" @}, 17681acd27e7Smillert @{ "list", com_list, "List files in DIR" @}, 17691acd27e7Smillert @{ "ls", com_list, "Synonym for `list'" @}, 17701acd27e7Smillert @{ "pwd", com_pwd, "Print the current working directory" @}, 17711acd27e7Smillert @{ "quit", com_quit, "Quit using Fileman" @}, 17721acd27e7Smillert @{ "rename", com_rename, "Rename FILE to NEWNAME" @}, 17731acd27e7Smillert @{ "stat", com_stat, "Print out statistics on FILE" @}, 17741acd27e7Smillert @{ "view", com_view, "View the contents of FILE" @}, 1775*af70c2dfSkettenis @{ (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL @} 17761acd27e7Smillert@}; 17771acd27e7Smillert 17781acd27e7Smillert/* Forward declarations. */ 17791acd27e7Smillertchar *stripwhite (); 17801acd27e7SmillertCOMMAND *find_command (); 17811acd27e7Smillert 17821acd27e7Smillert/* The name of this program, as taken from argv[0]. */ 17831acd27e7Smillertchar *progname; 17841acd27e7Smillert 1785*af70c2dfSkettenis/* When non-zero, this means the user is done using this program. */ 17861acd27e7Smillertint done; 17871acd27e7Smillert 17881acd27e7Smillertchar * 17891acd27e7Smillertdupstr (s) 17901acd27e7Smillert int s; 17911acd27e7Smillert@{ 17921acd27e7Smillert char *r; 17931acd27e7Smillert 17941acd27e7Smillert r = xmalloc (strlen (s) + 1); 17951acd27e7Smillert strcpy (r, s); 17961acd27e7Smillert return (r); 17971acd27e7Smillert@} 17981acd27e7Smillert 17991acd27e7Smillertmain (argc, argv) 18001acd27e7Smillert int argc; 18011acd27e7Smillert char **argv; 18021acd27e7Smillert@{ 18031acd27e7Smillert char *line, *s; 18041acd27e7Smillert 18051acd27e7Smillert progname = argv[0]; 18061acd27e7Smillert 18071acd27e7Smillert initialize_readline (); /* Bind our completer. */ 18081acd27e7Smillert 18091acd27e7Smillert /* Loop reading and executing lines until the user quits. */ 18101acd27e7Smillert for ( ; done == 0; ) 18111acd27e7Smillert @{ 18121acd27e7Smillert line = readline ("FileMan: "); 18131acd27e7Smillert 18141acd27e7Smillert if (!line) 18151acd27e7Smillert break; 18161acd27e7Smillert 18171acd27e7Smillert /* Remove leading and trailing whitespace from the line. 18181acd27e7Smillert Then, if there is anything left, add it to the history list 18191acd27e7Smillert and execute it. */ 18201acd27e7Smillert s = stripwhite (line); 18211acd27e7Smillert 18221acd27e7Smillert if (*s) 18231acd27e7Smillert @{ 18241acd27e7Smillert add_history (s); 18251acd27e7Smillert execute_line (s); 18261acd27e7Smillert @} 18271acd27e7Smillert 18281acd27e7Smillert free (line); 18291acd27e7Smillert @} 18301acd27e7Smillert exit (0); 18311acd27e7Smillert@} 18321acd27e7Smillert 18331acd27e7Smillert/* Execute a command line. */ 18341acd27e7Smillertint 18351acd27e7Smillertexecute_line (line) 18361acd27e7Smillert char *line; 18371acd27e7Smillert@{ 18381acd27e7Smillert register int i; 18391acd27e7Smillert COMMAND *command; 18401acd27e7Smillert char *word; 18411acd27e7Smillert 18421acd27e7Smillert /* Isolate the command word. */ 18431acd27e7Smillert i = 0; 18441acd27e7Smillert while (line[i] && whitespace (line[i])) 18451acd27e7Smillert i++; 18461acd27e7Smillert word = line + i; 18471acd27e7Smillert 18481acd27e7Smillert while (line[i] && !whitespace (line[i])) 18491acd27e7Smillert i++; 18501acd27e7Smillert 18511acd27e7Smillert if (line[i]) 18521acd27e7Smillert line[i++] = '\0'; 18531acd27e7Smillert 18541acd27e7Smillert command = find_command (word); 18551acd27e7Smillert 18561acd27e7Smillert if (!command) 18571acd27e7Smillert @{ 18581acd27e7Smillert fprintf (stderr, "%s: No such command for FileMan.\n", word); 18591acd27e7Smillert return (-1); 18601acd27e7Smillert @} 18611acd27e7Smillert 18621acd27e7Smillert /* Get argument to command, if any. */ 18631acd27e7Smillert while (whitespace (line[i])) 18641acd27e7Smillert i++; 18651acd27e7Smillert 18661acd27e7Smillert word = line + i; 18671acd27e7Smillert 18681acd27e7Smillert /* Call the function. */ 18691acd27e7Smillert return ((*(command->func)) (word)); 18701acd27e7Smillert@} 18711acd27e7Smillert 18721acd27e7Smillert/* Look up NAME as the name of a command, and return a pointer to that 18731acd27e7Smillert command. Return a NULL pointer if NAME isn't a command name. */ 18741acd27e7SmillertCOMMAND * 18751acd27e7Smillertfind_command (name) 18761acd27e7Smillert char *name; 18771acd27e7Smillert@{ 18781acd27e7Smillert register int i; 18791acd27e7Smillert 18801acd27e7Smillert for (i = 0; commands[i].name; i++) 18811acd27e7Smillert if (strcmp (name, commands[i].name) == 0) 18821acd27e7Smillert return (&commands[i]); 18831acd27e7Smillert 18841acd27e7Smillert return ((COMMAND *)NULL); 18851acd27e7Smillert@} 18861acd27e7Smillert 18871acd27e7Smillert/* Strip whitespace from the start and end of STRING. Return a pointer 18881acd27e7Smillert into STRING. */ 18891acd27e7Smillertchar * 18901acd27e7Smillertstripwhite (string) 18911acd27e7Smillert char *string; 18921acd27e7Smillert@{ 18931acd27e7Smillert register char *s, *t; 18941acd27e7Smillert 18951acd27e7Smillert for (s = string; whitespace (*s); s++) 18961acd27e7Smillert ; 18971acd27e7Smillert 18981acd27e7Smillert if (*s == 0) 18991acd27e7Smillert return (s); 19001acd27e7Smillert 19011acd27e7Smillert t = s + strlen (s) - 1; 19021acd27e7Smillert while (t > s && whitespace (*t)) 19031acd27e7Smillert t--; 19041acd27e7Smillert *++t = '\0'; 19051acd27e7Smillert 19061acd27e7Smillert return s; 19071acd27e7Smillert@} 19081acd27e7Smillert 19091acd27e7Smillert/* **************************************************************** */ 19101acd27e7Smillert/* */ 19111acd27e7Smillert/* Interface to Readline Completion */ 19121acd27e7Smillert/* */ 19131acd27e7Smillert/* **************************************************************** */ 19141acd27e7Smillert 1915*af70c2dfSkettenischar *command_generator __P((const char *, int)); 1916*af70c2dfSkettenischar **fileman_completion __P((const char *, int, int)); 19171acd27e7Smillert 1918*af70c2dfSkettenis/* Tell the GNU Readline library how to complete. We want to try to 1919*af70c2dfSkettenis complete on command names if this is the first word in the line, or 1920*af70c2dfSkettenis on filenames if not. */ 19211acd27e7Smillertinitialize_readline () 19221acd27e7Smillert@{ 19231acd27e7Smillert /* Allow conditional parsing of the ~/.inputrc file. */ 19241acd27e7Smillert rl_readline_name = "FileMan"; 19251acd27e7Smillert 19261acd27e7Smillert /* Tell the completer that we want a crack first. */ 1927*af70c2dfSkettenis rl_attempted_completion_function = fileman_completion; 19281acd27e7Smillert@} 19291acd27e7Smillert 1930*af70c2dfSkettenis/* Attempt to complete on the contents of TEXT. START and END 1931*af70c2dfSkettenis bound the region of rl_line_buffer that contains the word to 1932*af70c2dfSkettenis complete. TEXT is the word to complete. We can use the entire 1933*af70c2dfSkettenis contents of rl_line_buffer in case we want to do some simple 1934*af70c2dfSkettenis parsing. Returnthe array of matches, or NULL if there aren't any. */ 19351acd27e7Smillertchar ** 19361acd27e7Smillertfileman_completion (text, start, end) 1937*af70c2dfSkettenis const char *text; 19381acd27e7Smillert int start, end; 19391acd27e7Smillert@{ 19401acd27e7Smillert char **matches; 19411acd27e7Smillert 19421acd27e7Smillert matches = (char **)NULL; 19431acd27e7Smillert 19441acd27e7Smillert /* If this word is at the start of the line, then it is a command 19451acd27e7Smillert to complete. Otherwise it is the name of a file in the current 19461acd27e7Smillert directory. */ 19471acd27e7Smillert if (start == 0) 1948*af70c2dfSkettenis matches = rl_completion_matches (text, command_generator); 19491acd27e7Smillert 19501acd27e7Smillert return (matches); 19511acd27e7Smillert@} 19521acd27e7Smillert 1953*af70c2dfSkettenis/* Generator function for command completion. STATE lets us 1954*af70c2dfSkettenis know whether to start from scratch; without any state 1955*af70c2dfSkettenis (i.e. STATE == 0), then we start at the top of the list. */ 19561acd27e7Smillertchar * 19571acd27e7Smillertcommand_generator (text, state) 1958*af70c2dfSkettenis const char *text; 19591acd27e7Smillert int state; 19601acd27e7Smillert@{ 19611acd27e7Smillert static int list_index, len; 19621acd27e7Smillert char *name; 19631acd27e7Smillert 1964*af70c2dfSkettenis /* If this is a new word to complete, initialize now. This 1965*af70c2dfSkettenis includes saving the length of TEXT for efficiency, and 1966*af70c2dfSkettenis initializing the index variable to 0. */ 19671acd27e7Smillert if (!state) 19681acd27e7Smillert @{ 19691acd27e7Smillert list_index = 0; 19701acd27e7Smillert len = strlen (text); 19711acd27e7Smillert @} 19721acd27e7Smillert 1973*af70c2dfSkettenis /* Return the next name which partially matches from the 1974*af70c2dfSkettenis command list. */ 19751acd27e7Smillert while (name = commands[list_index].name) 19761acd27e7Smillert @{ 19771acd27e7Smillert list_index++; 19781acd27e7Smillert 19791acd27e7Smillert if (strncmp (name, text, len) == 0) 19801acd27e7Smillert return (dupstr(name)); 19811acd27e7Smillert @} 19821acd27e7Smillert 19831acd27e7Smillert /* If no names matched, then return NULL. */ 19841acd27e7Smillert return ((char *)NULL); 19851acd27e7Smillert@} 19861acd27e7Smillert 19871acd27e7Smillert/* **************************************************************** */ 19881acd27e7Smillert/* */ 19891acd27e7Smillert/* FileMan Commands */ 19901acd27e7Smillert/* */ 19911acd27e7Smillert/* **************************************************************** */ 19921acd27e7Smillert 19931acd27e7Smillert/* String to pass to system (). This is for the LIST, VIEW and RENAME 19941acd27e7Smillert commands. */ 19951acd27e7Smillertstatic char syscom[1024]; 19961acd27e7Smillert 19971acd27e7Smillert/* List the file(s) named in arg. */ 19981acd27e7Smillertcom_list (arg) 19991acd27e7Smillert char *arg; 20001acd27e7Smillert@{ 20011acd27e7Smillert if (!arg) 20021acd27e7Smillert arg = ""; 20031acd27e7Smillert 20041acd27e7Smillert sprintf (syscom, "ls -FClg %s", arg); 20051acd27e7Smillert return (system (syscom)); 20061acd27e7Smillert@} 20071acd27e7Smillert 20081acd27e7Smillertcom_view (arg) 20091acd27e7Smillert char *arg; 20101acd27e7Smillert@{ 20111acd27e7Smillert if (!valid_argument ("view", arg)) 20121acd27e7Smillert return 1; 20131acd27e7Smillert 20141acd27e7Smillert sprintf (syscom, "more %s", arg); 20151acd27e7Smillert return (system (syscom)); 20161acd27e7Smillert@} 20171acd27e7Smillert 20181acd27e7Smillertcom_rename (arg) 20191acd27e7Smillert char *arg; 20201acd27e7Smillert@{ 20211acd27e7Smillert too_dangerous ("rename"); 20221acd27e7Smillert return (1); 20231acd27e7Smillert@} 20241acd27e7Smillert 20251acd27e7Smillertcom_stat (arg) 20261acd27e7Smillert char *arg; 20271acd27e7Smillert@{ 20281acd27e7Smillert struct stat finfo; 20291acd27e7Smillert 20301acd27e7Smillert if (!valid_argument ("stat", arg)) 20311acd27e7Smillert return (1); 20321acd27e7Smillert 20331acd27e7Smillert if (stat (arg, &finfo) == -1) 20341acd27e7Smillert @{ 20351acd27e7Smillert perror (arg); 20361acd27e7Smillert return (1); 20371acd27e7Smillert @} 20381acd27e7Smillert 20391acd27e7Smillert printf ("Statistics for `%s':\n", arg); 20401acd27e7Smillert 20411acd27e7Smillert printf ("%s has %d link%s, and is %d byte%s in length.\n", arg, 20421acd27e7Smillert finfo.st_nlink, 20431acd27e7Smillert (finfo.st_nlink == 1) ? "" : "s", 20441acd27e7Smillert finfo.st_size, 20451acd27e7Smillert (finfo.st_size == 1) ? "" : "s"); 20461acd27e7Smillert printf ("Inode Last Change at: %s", ctime (&finfo.st_ctime)); 20471acd27e7Smillert printf (" Last access at: %s", ctime (&finfo.st_atime)); 20481acd27e7Smillert printf (" Last modified at: %s", ctime (&finfo.st_mtime)); 20491acd27e7Smillert return (0); 20501acd27e7Smillert@} 20511acd27e7Smillert 20521acd27e7Smillertcom_delete (arg) 20531acd27e7Smillert char *arg; 20541acd27e7Smillert@{ 20551acd27e7Smillert too_dangerous ("delete"); 20561acd27e7Smillert return (1); 20571acd27e7Smillert@} 20581acd27e7Smillert 20591acd27e7Smillert/* Print out help for ARG, or for all of the commands if ARG is 20601acd27e7Smillert not present. */ 20611acd27e7Smillertcom_help (arg) 20621acd27e7Smillert char *arg; 20631acd27e7Smillert@{ 20641acd27e7Smillert register int i; 20651acd27e7Smillert int printed = 0; 20661acd27e7Smillert 20671acd27e7Smillert for (i = 0; commands[i].name; i++) 20681acd27e7Smillert @{ 20691acd27e7Smillert if (!*arg || (strcmp (arg, commands[i].name) == 0)) 20701acd27e7Smillert @{ 20711acd27e7Smillert printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc); 20721acd27e7Smillert printed++; 20731acd27e7Smillert @} 20741acd27e7Smillert @} 20751acd27e7Smillert 20761acd27e7Smillert if (!printed) 20771acd27e7Smillert @{ 20781acd27e7Smillert printf ("No commands match `%s'. Possibilties are:\n", arg); 20791acd27e7Smillert 20801acd27e7Smillert for (i = 0; commands[i].name; i++) 20811acd27e7Smillert @{ 20821acd27e7Smillert /* Print in six columns. */ 20831acd27e7Smillert if (printed == 6) 20841acd27e7Smillert @{ 20851acd27e7Smillert printed = 0; 20861acd27e7Smillert printf ("\n"); 20871acd27e7Smillert @} 20881acd27e7Smillert 20891acd27e7Smillert printf ("%s\t", commands[i].name); 20901acd27e7Smillert printed++; 20911acd27e7Smillert @} 20921acd27e7Smillert 20931acd27e7Smillert if (printed) 20941acd27e7Smillert printf ("\n"); 20951acd27e7Smillert @} 20961acd27e7Smillert return (0); 20971acd27e7Smillert@} 20981acd27e7Smillert 20991acd27e7Smillert/* Change to the directory ARG. */ 21001acd27e7Smillertcom_cd (arg) 21011acd27e7Smillert char *arg; 21021acd27e7Smillert@{ 21031acd27e7Smillert if (chdir (arg) == -1) 21041acd27e7Smillert @{ 21051acd27e7Smillert perror (arg); 21061acd27e7Smillert return 1; 21071acd27e7Smillert @} 21081acd27e7Smillert 21091acd27e7Smillert com_pwd (""); 21101acd27e7Smillert return (0); 21111acd27e7Smillert@} 21121acd27e7Smillert 21131acd27e7Smillert/* Print out the current working directory. */ 21141acd27e7Smillertcom_pwd (ignore) 21151acd27e7Smillert char *ignore; 21161acd27e7Smillert@{ 21171acd27e7Smillert char dir[1024], *s; 21181acd27e7Smillert 2119*af70c2dfSkettenis s = getcwd (dir, sizeof(dir) - 1); 21201acd27e7Smillert if (s == 0) 21211acd27e7Smillert @{ 21221acd27e7Smillert printf ("Error getting pwd: %s\n", dir); 21231acd27e7Smillert return 1; 21241acd27e7Smillert @} 21251acd27e7Smillert 21261acd27e7Smillert printf ("Current directory is %s\n", dir); 21271acd27e7Smillert return 0; 21281acd27e7Smillert@} 21291acd27e7Smillert 2130*af70c2dfSkettenis/* The user wishes to quit using this program. Just set DONE 2131*af70c2dfSkettenis non-zero. */ 21321acd27e7Smillertcom_quit (arg) 21331acd27e7Smillert char *arg; 21341acd27e7Smillert@{ 21351acd27e7Smillert done = 1; 21361acd27e7Smillert return (0); 21371acd27e7Smillert@} 21381acd27e7Smillert 21391acd27e7Smillert/* Function which tells you that you can't do this. */ 21401acd27e7Smillerttoo_dangerous (caller) 21411acd27e7Smillert char *caller; 21421acd27e7Smillert@{ 21431acd27e7Smillert fprintf (stderr, 2144*af70c2dfSkettenis "%s: Too dangerous for me to distribute.\n" 21451acd27e7Smillert caller); 2146*af70c2dfSkettenis fprintf (stderr, "Write it yourself.\n"); 21471acd27e7Smillert@} 21481acd27e7Smillert 2149*af70c2dfSkettenis/* Return non-zero if ARG is a valid argument for CALLER, 2150*af70c2dfSkettenis else print an error message and return zero. */ 21511acd27e7Smillertint 21521acd27e7Smillertvalid_argument (caller, arg) 21531acd27e7Smillert char *caller, *arg; 21541acd27e7Smillert@{ 21551acd27e7Smillert if (!arg || !*arg) 21561acd27e7Smillert @{ 21571acd27e7Smillert fprintf (stderr, "%s: Argument required.\n", caller); 21581acd27e7Smillert return (0); 21591acd27e7Smillert @} 21601acd27e7Smillert 21611acd27e7Smillert return (1); 21621acd27e7Smillert@} 21631acd27e7Smillert@end smallexample 2164