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