home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / TERMNET / READLINE.0 / DOC / RLTECH.TEX < prev    next >
Encoding:
Text File  |  1995-04-20  |  42.6 KB  |  1,357 lines

  1. @comment %**start of header (This is for running Texinfo on a region.)
  2. @setfilename rltech.info
  3. @comment %**end of header (This is for running Texinfo on a region.)
  4. @setchapternewpage odd
  5.  
  6. @ifinfo
  7. This document describes the GNU Readline Library, a utility for aiding
  8. in the consitency of user interface across discrete programs that need
  9. to provide a command line interface.
  10.  
  11. Copyright (C) 1988, 1994 Free Software Foundation, Inc.
  12.  
  13. Permission is granted to make and distribute verbatim copies of
  14. this manual provided the copyright notice and this permission notice
  15. pare preserved on all copies.
  16.  
  17. @ignore
  18. Permission is granted to process this file through TeX and print the
  19. results, provided the printed document carries copying permission
  20. notice identical to this one except for the removal of this paragraph
  21. (this paragraph not being relevant to the printed manual).
  22. @end ignore
  23.  
  24. Permission is granted to copy and distribute modified versions of this
  25. manual under the conditions for verbatim copying, provided that the entire
  26. resulting derived work is distributed under the terms of a permission
  27. notice identical to this one.
  28.  
  29. Permission is granted to copy and distribute translations of this manual
  30. into another language, under the above conditions for modified versions,
  31. except that this permission notice may be stated in a translation approved
  32. by the Foundation.
  33. @end ifinfo
  34.  
  35. @node Programming with GNU Readline
  36. @chapter Programming with GNU Readline
  37.  
  38. This chapter describes the interface between the GNU Readline Library and
  39. other programs.  If you are a programmer, and you wish to include the
  40. features found in GNU Readline
  41. such as completion, line editing, and interactive history manipulation
  42. in your own programs, this section is for you.
  43.  
  44. @menu
  45. * Basic Behavior::    Using the default behavior of Readline.
  46. * Custom Functions::    Adding your own functions to Readline.
  47. * Readline Convenience Functions::    Functions which Readline supplies to
  48.                     aid in writing your own
  49. * Custom Completers::    Supplanting or supplementing Readline's
  50.             completion functions.
  51. @end menu
  52.  
  53. @node Basic Behavior
  54. @section Basic Behavior
  55.  
  56. Many programs provide a command line interface, such as @code{mail},
  57. @code{ftp}, and @code{sh}.  For such programs, the default behaviour of
  58. Readline is sufficient.  This section describes how to use Readline in
  59. the simplest way possible, perhaps to replace calls in your code to
  60. @code{gets()} or @code{fgets ()}.
  61.  
  62. @findex readline
  63. @cindex readline, function
  64. The function @code{readline ()} prints a prompt and then reads and returns
  65. a single line of text from the user.  The line @code{readline}
  66. returns is allocated with @code{malloc ()}; you should @code{free ()}
  67. the line when you are done with it.  The declaration for @code{readline}
  68. in ANSI C is
  69.  
  70. @example
  71. @code{char *readline (char *@var{prompt});}
  72. @end example
  73.  
  74. @noindent
  75. So, one might say
  76. @example
  77. @code{char *line = readline ("Enter a line: ");}
  78. @end example
  79. @noindent
  80. in order to read a line of text from the user.
  81. The line returned has the final newline removed, so only the
  82. text remains.
  83.  
  84. If @code{readline} encounters an @code{EOF} while reading the line, and the
  85. line is empty at that point, then @code{(char *)NULL} is returned.
  86. Otherwise, the line is ended just as if a newline had been typed.
  87.  
  88. If you want the user to be able to get at the line later, (with
  89. @key{C-p} for example), you must call @code{add_history ()} to save the
  90. line away in a @dfn{history} list of such lines.
  91.  
  92. @example
  93. @code{add_history (line)};
  94. @end example
  95.  
  96. @noindent
  97. For full details on the GNU History Library, see the associated manual.
  98.  
  99. It is preferable to avoid saving empty lines on the history list, since
  100. users rarely have a burning need to reuse a blank line.  Here is
  101. a function which usefully replaces the standard @code{gets ()} library
  102. function, and has the advantage of no static buffer to overflow:
  103.  
  104. @example
  105. /* A static variable for holding the line. */
  106. static char *line_read = (char *)NULL;
  107.  
  108. /* Read a string, and return a pointer to it.  Returns NULL on EOF. */
  109. char *
  110. rl_gets ()
  111. @{
  112.   /* If the buffer has already been allocated, return the memory
  113.      to the free pool. */
  114.   if (line_read)
  115.     @{
  116.       free (line_read);
  117.       line_read = (char *)NULL;
  118.     @}
  119.  
  120.   /* Get a line from the user. */
  121.   line_read = readline ("");
  122.  
  123.   /* If the line has any text in it, save it on the history. */
  124.   if (line_read && *line_read)
  125.     add_history (line_read);
  126.  
  127.   return (line_read);
  128. @}
  129. @end example
  130.  
  131. This function gives the user the default behaviour of @key{TAB}
  132. completion: completion on file names.  If you do not want Readline to
  133. complete on filenames, you can change the binding of the @key{TAB} key
  134. with @code{rl_bind_key ()}.
  135.  
  136. @example
  137. @code{int rl_bind_key (int @var{key}, int (*@var{function})());}
  138. @end example
  139.  
  140. @code{rl_bind_key ()} takes two arguments: @var{key} is the character that
  141. you want to bind, and @var{function} is the address of the function to
  142. call when @var{key} is pressed.  Binding @key{TAB} to @code{rl_insert ()}
  143. makes @key{TAB} insert itself.
  144. @code{rl_bind_key ()} returns non-zero if @var{key} is not a valid
  145. ASCII character code (between 0 and 255).
  146.  
  147. Thus, to disable the default @key{TAB} behavior, the following suffices:
  148. @example
  149. @code{rl_bind_key ('\t', rl_insert);}
  150. @end example
  151.  
  152. This code should be executed once at the start of your program; you
  153. might write a function called @code{initialize_readline ()} which
  154. performs this and other desired initializations, such as installing
  155. custom completers (@pxref{Custom Completers}).
  156.  
  157. @node Custom Functions
  158. @section Custom Functions
  159.  
  160. Readline provides many functions for manipulating the text of
  161. the line, but it isn't possible to anticipate the needs of all
  162. programs.  This section describes the various functions and variables
  163. defined within the Readline library which allow a user program to add
  164. customized functionality to Readline.
  165.  
  166. @menu
  167. * The Function Type::    C declarations to make code readable.
  168. * Function Writing::    Variables and calling conventions.
  169. @end menu
  170.  
  171. @node The Function Type
  172. @subsection The Function Type
  173.  
  174. For readabilty, we declare a new type of object, called
  175. @dfn{Function}.  A @code{Function} is a C function which
  176. returns an @code{int}.  The type declaration for @code{Function} is:
  177.  
  178. @noindent
  179. @code{typedef int Function ();}
  180.  
  181. The reason for declaring this new type is to make it easier to write
  182. code describing pointers to C functions.  Let us say we had a variable
  183. called @var{func} which was a pointer to a function.  Instead of the
  184. classic C declaration
  185.  
  186. @code{int (*)()func;}
  187.  
  188. @noindent
  189. we may write
  190.  
  191. @code{Function *func;}
  192.  
  193. @noindent
  194. Similarly, there are
  195.  
  196. @example
  197. typedef void VFunction ();
  198. typedef char *CPFunction (); @r{and}
  199. typedef char **CPPFunction ();
  200. @end example
  201.  
  202. @noindent
  203. for functions returning no value, @code{pointer to char}, and
  204. @code{pointer to pointer to char}, respectively.
  205.  
  206. @node Function Writing
  207. @subsection Writing a New Function
  208.  
  209. In order to write new functions for Readline, you need to know the
  210. calling conventions for keyboard-invoked functions, and the names of the
  211. variables that describe the current state of the line read so far.
  212.  
  213. The calling sequence for a command @code{foo} looks like
  214.  
  215. @example
  216. @code{foo (int count, int key)}
  217. @end example
  218.  
  219. @noindent
  220. where @var{count} is the numeric argument (or 1 if defaulted) and
  221. @var{key} is the key that invoked this function.
  222.  
  223. It is completely up to the function as to what should be done with the
  224. numeric argument.  Some functions use it as a repeat count, some
  225. as a flag, and others to choose alternate behavior (refreshing the current
  226. line as opposed to refreshing the screen, for example).  Some choose to
  227. ignore it.  In general, if a
  228. function uses the numeric argument as a repeat count, it should be able
  229. to do something useful with both negative and positive arguments.
  230. At the very least, it should be aware that it can be passed a
  231. negative argument.
  232.  
  233. @deftypevar {char *} rl_line_buffer
  234. This is the line gathered so far.  You are welcome to modify the
  235. contents of the line, but see @ref{Allowing Undoing}.
  236. @end deftypevar
  237.  
  238. @deftypevar int rl_point
  239. The offset of the current cursor position in @code{rl_line_buffer}
  240. (the @emph{point}).
  241. @end deftypevar
  242.  
  243. @deftypevar int rl_end
  244. The number of characters present in @code{rl_line_buffer}.  When
  245. @code{rl_point} is at the end of the line, @code{rl_point} and
  246. @code{rl_end} are equal.
  247. @end deftypevar
  248.  
  249. @deftypevar int rl_mark
  250. The mark (saved position) in the current line.  If set, the mark
  251. and point define a @emph{region}.
  252. @end deftypevar
  253.  
  254. @deftypevar int rl_done
  255. Setting this to a non-zero value causes Readline to return the current
  256. line immediately.
  257. @end deftypevar
  258.  
  259. @deftypevar int rl_pending_input
  260. Setting this to a value makes it the next keystroke read.  This is a
  261. way to stuff a single character into the input stream.
  262. @end deftypevar
  263.  
  264. @deftypevar {char *} rl_prompt
  265. The prompt Readline uses.  This is set from the argument to
  266. @code{readline ()}, and should not be assigned to directly.
  267. @end deftypevar
  268.  
  269. @deftypevar {char *} rl_terminal_name
  270. The terminal type, used for initialization.
  271. @end deftypevar
  272.  
  273. @deftypevar {char *} rl_readline_name
  274. This variable is set to a unique name by each application using Readline.
  275. The value allows conditional parsing of the inputrc file
  276. (@pxref{Conditional Init Constructs}).
  277. @end deftypevar
  278.  
  279. @deftypevar {FILE *} rl_instream
  280. The stdio stream from which Readline reads input.
  281. @end deftypevar
  282.  
  283. @deftypevar {FILE *} rl_outstream
  284. The stdio stream to which Readline performs output.
  285. @end deftypevar
  286.  
  287. @deftypevar {Function *} rl_startup_hook
  288. If non-zero, this is the address of a function to call just
  289. before @code{readline} prints the first prompt.
  290. @end deftypevar
  291.  
  292. @node Readline Convenience Functions
  293. @section Readline Convenience Functions
  294.  
  295. @menu
  296. * Function Naming::    How to give a function you write a name.
  297. * Keymaps::        Making keymaps.
  298. * Binding Keys::    Changing Keymaps.
  299. * Associating Function Names and Bindings::    Translate function names to
  300.                         key sequences.
  301. * Allowing Undoing::    How to make your functions undoable.
  302. * Redisplay::        Functions to control line display.
  303. * Modifying Text::    Functions to modify @code{rl_line_buffer}.
  304. * Utility Functions::    Generally useful functions and hooks.
  305. @end menu
  306.  
  307. @node Function Naming
  308. @subsection Naming a Function
  309.  
  310. The user can dynamically change the bindings of keys while using
  311. Readline.  This is done by representing the function with a descriptive
  312. name.  The user is able to type the descriptive name when referring to
  313. the function.  Thus, in an init file, one might find
  314.  
  315. @example
  316. Meta-Rubout:    backward-kill-word
  317. @end example
  318.  
  319. This binds the keystroke @key{Meta-Rubout} to the function
  320. @emph{descriptively} named @code{backward-kill-word}.  You, as the
  321. programmer, should bind the functions you write to descriptive names as
  322. well.  Readline provides a function for doing that:
  323.  
  324. @deftypefun int rl_add_defun (char *name, Function *function, int key)
  325. Add @var{name} to the list of named functions.  Make @var{function} be
  326. the function that gets called.  If @var{key} is not -1, then bind it to
  327. @var{function} using @code{rl_bind_key ()}.
  328. @end deftypefun
  329.  
  330. Using this function alone is sufficient for most applications.  It is
  331. the recommended way to add a few functions to the default functions that
  332. Readline has built in.  If you need to do something other
  333. than adding a function to Readline, you may need to use the
  334. underlying functions described below.
  335.  
  336. @node Keymaps
  337. @subsection Selecting a Keymap
  338.  
  339. Key bindings take place on a @dfn{keymap}.  The keymap is the
  340. association between the keys that the user types and the functions that
  341. get run.  You can make your own keymaps, copy existing keymaps, and tell
  342. Readline which keymap to use.
  343.  
  344. @deftypefun Keymap rl_make_bare_keymap ()
  345. Returns a new, empty keymap.  The space for the keymap is allocated with
  346. @code{malloc ()}; you should @code{free ()} it when you are done.
  347. @end deftypefun
  348.  
  349. @deftypefun Keymap rl_copy_keymap (Keymap map)
  350. Return a new keymap which is a copy of @var{map}.
  351. @end deftypefun
  352.  
  353. @deftypefun Keymap rl_make_keymap ()
  354. Return a new keymap with the printing characters bound to rl_insert,
  355. the lowercase Meta characters bound to run their equivalents, and
  356. the Meta digits bound to produce numeric arguments.
  357. @end deftypefun
  358.  
  359. @deftypefun void rl_discard_keymap (Keymap keymap)
  360. Free the storage associated with @var{keymap}.
  361. @end deftypefun
  362.  
  363. Readline has several internal keymaps.  These functions allow you to
  364. change which keymap is active.
  365.  
  366. @deftypefun Keymap rl_get_keymap ()
  367. Returns the currently active keymap.
  368. @end deftypefun
  369.  
  370. @deftypefun void rl_set_keymap (Keymap keymap)
  371. Makes @var{keymap} the currently active keymap.
  372. @end deftypefun
  373.  
  374. @deftypefun Keymap rl_get_keymap_by_name (char *name)
  375. Return the keymap matching @var{name}.  @var{name} is one which would
  376. be supplied in a @code{set keymap} inputrc line (@pxref{Readline Init File}).
  377. @end deftypefun
  378.  
  379. @node Binding Keys
  380. @subsection Binding Keys
  381.  
  382. You associate keys with functions through the keymap.  Readline has
  383. several internal keymaps: @code{emacs_standard_keymap},
  384. @code{emacs_meta_keymap}, @code{emacs_ctlx_keymap},
  385. @code{vi_movement_keymap}, and @code{vi_insertion_keymap}.
  386. @code{emacs_standard_keymap} is the default, and the examples in
  387. this manual assume that.
  388.  
  389. These functions manage key bindings.
  390.  
  391. @deftypefun int rl_bind_key (int key, Function *function)
  392. Binds @var{key} to @var{function} in the currently active keymap.
  393. Returns non-zero in the case of an invalid @var{key}.
  394. @end deftypefun
  395.  
  396. @deftypefun int rl_bind_key_in_map (int key, Function *function, Keymap map)
  397. Bind @var{key} to @var{function} in @var{map}.  Returns non-zero in the case
  398. of an invalid @var{key}.
  399. @end deftypefun
  400.  
  401. @deftypefun int rl_unbind_key (int key)
  402. Bind @var{key} to the null function in the currently active keymap.
  403. Returns non-zero in case of error.
  404. @end deftypefun
  405.  
  406. @deftypefun int rl_unbind_key_in_map (int key, Keymap map)
  407. Bind @var{key} to the null function in @var{map}.
  408. Returns non-zero in case of error.
  409. @end deftypefun
  410.  
  411. @deftypefun int rl_generic_bind (int type, char *keyseq, char *data, Keymap map)
  412. Bind the key sequence represented by the string @var{keyseq} to the arbitrary
  413. pointer @var{data}.  @var{type} says what kind of data is pointed to by
  414. @var{data}; this can be a function (@code{ISFUNC}), a macro
  415. (@code{ISMACR}), or a keymap (@code{ISKMAP}).  This makes new keymaps as
  416. necessary.  The initial keymap in which to do bindings is @var{map}.
  417. @end deftypefun
  418.  
  419. @deftypefun int rl_parse_and_bind (char *line)
  420. Parse @var{line} as if it had been read from the @code{inputrc} file and
  421. perform any key bindings and variable assignments found
  422. (@pxref{Readline Init File}).
  423. @end deftypefun
  424.  
  425. @node Associating Function Names and Bindings
  426. @subsection Associating Function Names and Bindings
  427.  
  428. These functions allow you to find out what keys invoke named functions
  429. and the functions invoked by a particular key sequence.
  430.  
  431. @deftypefun {Function *} rl_named_function (char *name)
  432. Return the function with name @var{name}.
  433. @end deftypefun
  434.  
  435. @deftypefun {Function *} rl_function_of_keyseq (char *keyseq, Keymap map, int *type)
  436. Return the function invoked by @var{keyseq} in keymap @var{map}.
  437. If @var{map} is NULL, the current keymap is used.  If @var{type} is
  438. not NULL, the type of the object is returned in it (one of @code{ISFUNC},
  439. @code{ISKMAP}, or @code{ISMACR}).
  440. @end deftypefun
  441.  
  442. @deftypefun {char **} rl_invoking_keyseqs (Function *function)
  443. Return an array of strings representing the key sequences used to
  444. invoke @var{function} in the current keymap.
  445. @end deftypefun
  446.  
  447. @deftypefun {char **} rl_invoking_keyseqs_in_map (Function *function, Keymap map)
  448. Return an array of strings representing the key sequences used to
  449. invoke @var{function} in the keymap @var{map}.
  450. @end deftypefun
  451.  
  452. @node Allowing Undoing
  453. @subsection Allowing Undoing
  454.  
  455. Supporting the undo command is a painless thing, and makes your
  456. functions much more useful.  It is certainly easy to try
  457. something if you know you can undo it.  I could use an undo function for
  458. the stock market.
  459.  
  460. If your function simply inserts text once, or deletes text once, and
  461. uses @code{rl_insert_text ()} or @code{rl_delete_text ()} to do it, then
  462. undoing is already done for you automatically.
  463.  
  464. If you do multiple insertions or multiple deletions, or any combination
  465. of these operations, you should group them together into one operation.
  466. This is done with @code{rl_begin_undo_group ()} and
  467. @code{rl_end_undo_group ()}.
  468.  
  469. The types of events that can be undone are:
  470.  
  471. @example
  472. enum undo_code @{ UNDO_DELETE, UNDO_INSERT, UNDO_BEGIN, UNDO_END @}; 
  473. @end example
  474.  
  475. Notice that @code{UNDO_DELETE} means to insert some text, and
  476. @code{UNDO_INSERT} means to delete some text.  That is, the undo code
  477. tells undo what to undo, not how to undo it.  @code{UNDO_BEGIN} and
  478. @code{UNDO_END} are tags added by @code{rl_begin_undo_group ()} and
  479. @code{rl_end_undo_group ()}.
  480.  
  481. @deftypefun int rl_begin_undo_group ()
  482. Begins saving undo information in a group construct.  The undo
  483. information usually comes from calls to @code{rl_insert_text ()} and
  484. @code{rl_delete_text ()}, but could be the result of calls to
  485. @code{rl_add_undo ()}.
  486. @end deftypefun
  487.  
  488. @deftypefun int rl_end_undo_group ()
  489. Closes the current undo group started with @code{rl_begin_undo_group
  490. ()}.  There should be one call to @code{rl_end_undo_group ()}
  491. for each call to @code{rl_begin_undo_group ()}.
  492. @end deftypefun
  493.  
  494. @deftypefun void rl_add_undo (enum undo_code what, int start, int end, char *text)
  495. Remember how to undo an event (according to @var{what}).  The affected
  496. text runs from @var{start} to @var{end}, and encompasses @var{text}.
  497. @end deftypefun
  498.  
  499. @deftypefun void free_undo_list ()
  500. Free the existing undo list.
  501. @end deftypefun
  502.  
  503. @deftypefun int rl_do_undo ()
  504. Undo the first thing on the undo list.  Returns @code{0} if there was
  505. nothing to undo, non-zero if something was undone.
  506. @end deftypefun
  507.  
  508. Finally, if you neither insert nor delete text, but directly modify the
  509. existing text (e.g., change its case), call @code{rl_modifying ()}
  510. once, just before you modify the text.  You must supply the indices of
  511. the text range that you are going to modify.
  512.  
  513. @deftypefun int rl_modifying (int start, int end)
  514. Tell Readline to save the text between @var{start} and @var{end} as a
  515. single undo unit.  It is assumed that you will subsequently modify
  516. that text.
  517. @end deftypefun
  518.  
  519. @node Redisplay
  520. @subsection Redisplay
  521.  
  522. @deftypefun int rl_redisplay ()
  523. Change what's displayed on the screen to reflect the current contents
  524. of @code{rl_line_buffer}.
  525. @end deftypefun
  526.  
  527. @deftypefun int rl_forced_update_display ()
  528. Force the line to be updated and redisplayed, whether or not
  529. Readline thinks the screen display is correct.
  530. @end deftypefun
  531.  
  532. @deftypefun int rl_on_new_line ()
  533. Tell the update routines that we have moved onto a new (empty) line,
  534. usually after ouputting a newline.
  535. @end deftypefun
  536.  
  537. @deftypefun int rl_reset_line_state ()
  538. Reset the display state to a clean state and redisplay the current line
  539. starting on a new line.
  540. @end deftypefun
  541.  
  542. @deftypefun int rl_message (va_alist)
  543. The arguments are a string as would be supplied to @code{printf}.  The
  544. resulting string is displayed in the @dfn{echo area}.  The echo area
  545. is also used to display numeric arguments and search strings.
  546. @end deftypefun
  547.  
  548. @deftypefun int rl_clear_message ()
  549. Clear the message in the echo area.
  550. @end deftypefun
  551.  
  552. @node Modifying Text
  553. @subsection Modifying Text
  554.  
  555. @deftypefun int rl_insert_text (char *text)
  556. Insert @var{text} into the line at the current cursor position.
  557. @end deftypefun
  558.  
  559. @deftypefun int rl_delete_text (int start, int end)
  560. Delete the text between @var{start} and @var{end} in the current line.
  561. @end deftypefun
  562.  
  563. @deftypefun {char *} rl_copy_text (int start, int end)
  564. Return a copy of the text between @var{start} and @var{end} in
  565. the current line.
  566. @end deftypefun
  567.  
  568. @deftypefun int rl_kill_text (int start, int end)
  569. Copy the text between @var{start} and @var{end} in the current line
  570. to the kill ring, appending or prepending to the last kill if the
  571. last command was a kill command.  The text is deleted.
  572. If @var{start} is less than @var{end},
  573. the text is appended, otherwise prepended.  If the last command was
  574. not a kill, a new kill ring slot is used.
  575. @end deftypefun
  576.  
  577. @node Utility Functions
  578. @subsection Utility Functions
  579.  
  580. @deftypefun int rl_reset_terminal (char *terminal_name)
  581. Reinitialize Readline's idea of the terminal settings using
  582. @var{terminal_name} as the terminal type (e.g., @code{vt100}).
  583. @end deftypefun
  584.  
  585. @deftypefun int alphabetic (int c)
  586. Return 1 if @var{c} is an alphabetic character.
  587. @end deftypefun
  588.  
  589. @deftypefun int numeric (int c)
  590. Return 1 if @var{c} is a numeric character.
  591. @end deftypefun
  592.  
  593. @deftypefun int ding ()
  594. Ring the terminal bell, obeying the setting of @code{bell-style}.
  595. @end deftypefun
  596.  
  597. The following are implemented as macros, defined in @code{chartypes.h}.
  598.  
  599. @deftypefun int uppercase_p (int c)
  600. Return 1 if @var{c} is an uppercase alphabetic character.
  601. @end deftypefun
  602.  
  603. @deftypefun int lowercase_p (int c)
  604. Return 1 if @var{c} is a lowercase alphabetic character.
  605. @end deftypefun
  606.  
  607. @deftypefun int digit_p (int c)
  608. Return 1 if @var{c} is a numeric character.
  609. @deftypefun
  610.  
  611. @deftypefun int to_upper (int c)
  612. If @var{c} is a lowercase alphabetic character, return the corresponding
  613. uppercase character.
  614. @end deftypefun
  615.  
  616. @deftypefun int to_lower (int c)
  617. If @var{c} is an uppercase alphabetic character, return the corresponding
  618. lowercase character.
  619. @end deftypefun
  620.  
  621. @deftypefun int digit_value (int c)
  622. If @var{c} is a number, return the value it represents.
  623. @end deftypefun
  624.  
  625. @subsection An Example
  626.  
  627. Here is a function which changes lowercase characters to their uppercase
  628. equivalents, and uppercase characters to lowercase.  If
  629. this function was bound to @samp{M-c}, then typing @samp{M-c} would
  630. change the case of the character under point.  Typing @samp{M-1 0 M-c}
  631. would change the case of the following 10 characters, leaving the cursor on
  632. the last character changed.
  633.  
  634. @example
  635. /* Invert the case of the COUNT following characters. */
  636. int
  637. invert_case_line (count, key)
  638.      int count, key;
  639. @{
  640.   register int start, end, i;
  641.  
  642.   start = rl_point;
  643.  
  644.   if (rl_point >= rl_end)
  645.     return (0);
  646.  
  647.   if (count < 0)
  648.     @{
  649.       direction = -1;
  650.       count = -count;
  651.     @}
  652.   else
  653.     direction = 1;
  654.       
  655.   /* Find the end of the range to modify. */
  656.   end = start + (count * direction);
  657.  
  658.   /* Force it to be within range. */
  659.   if (end > rl_end)
  660.     end = rl_end;
  661.   else if (end < 0)
  662.     end = 0;
  663.  
  664.   if (start == end)
  665.     return (0);
  666.  
  667.   if (start > end)
  668.     @{
  669.       int temp = start;
  670.       start = end;
  671.       end = temp;
  672.     @}
  673.  
  674.   /* Tell readline that we are modifying the line, so it will save
  675.      the undo information. */
  676.   rl_modifying (start, end);
  677.  
  678.   for (i = start; i != end; i++)
  679.     @{
  680.       if (uppercase_p (rl_line_buffer[i]))
  681.         rl_line_buffer[i] = to_lower (rl_line_buffer[i]);
  682.       else if (lowercase_p (rl_line_buffer[i]))
  683.         rl_line_buffer[i] = to_upper (rl_line_buffer[i]);
  684.     @}
  685.   /* Move point to on top of the last character changed. */
  686.   rl_point = (direction == 1) ? end - 1 : start;
  687.   return (0);
  688. @}
  689. @end example
  690.  
  691. @node Custom Completers
  692. @section Custom Completers
  693.  
  694. Typically, a program that reads commands from the user has a way of
  695. disambiguating commands and data.  If your program is one of these, then
  696. it can provide completion for commands, data, or both.
  697. The following sections describe how your program and Readline
  698. cooperate to provide this service.
  699.  
  700. @menu
  701. * How Completing Works::    The logic used to do completion.
  702. * Completion Functions::    Functions provided by Readline.
  703. * Completion Variables::    Variables which control completion.
  704. * A Short Completion Example::    An example of writing completer subroutines.
  705. @end menu
  706.  
  707. @node How Completing Works
  708. @subsection How Completing Works
  709.  
  710. In order to complete some text, the full list of possible completions
  711. must be available.  That is, it is not possible to accurately
  712. expand a partial word without knowing all of the possible words
  713. which make sense in that context.  The Readline library provides
  714. the user interface to completion, and two of the most common
  715. completion functions:  filename and username.  For completing other types
  716. of text, you must write your own completion function.  This section
  717. describes exactly what such functions must do, and provides an example.
  718.  
  719. There are three major functions used to perform completion:
  720.  
  721. @enumerate
  722. @item
  723. The user-interface function @code{rl_complete ()}.  This function is
  724. called with the same arguments as other Readline
  725. functions intended for interactive use:  @var{count} and
  726. @var{invoking_key}.  It isolates the word to be completed and calls
  727. @code{completion_matches ()} to generate a list of possible completions.
  728. It then either lists the possible completions, inserts the possible
  729. completions, or actually performs the
  730. completion, depending on which behavior is desired.
  731.  
  732. @item
  733. The internal function @code{completion_matches ()} uses your
  734. @dfn{generator} function to generate the list of possible matches, and
  735. then returns the array of these matches.  You should place the address
  736. of your generator function in @code{rl_completion_entry_function}.
  737.  
  738. @item
  739. The generator function is called repeatedly from
  740. @code{completion_matches ()}, returning a string each time.  The
  741. arguments to the generator function are @var{text} and @var{state}.
  742. @var{text} is the partial word to be completed.  @var{state} is zero the
  743. first time the function is called, allowing the generator to perform
  744. any necessary initialization, and a positive non-zero integer for
  745. each subsequent call.  When the generator function returns
  746. @code{(char *)NULL} this signals @code{completion_matches ()} that there are
  747. no more possibilities left.  Usually the generator function computes the
  748. list of possible completions when @var{state} is zero, and returns them
  749. one at a time on subsequent calls.  Each string the generator function
  750. returns as a match must be allocated with @code{malloc()}; Readline
  751. frees the strings when it has finished with them.
  752.  
  753. @end enumerate
  754.  
  755. @deftypefun int rl_complete (int ignore, int invoking_key)
  756. Complete the word at or before point.  You have supplied the function
  757. that does the initial simple matching selection algorithm (see
  758. @code{completion_matches ()}).  The default is to do filename completion.
  759. @end deftypefun
  760.  
  761. @deftypevar {Function *} rl_completion_entry_function
  762. This is a pointer to the generator function for @code{completion_matches
  763. ()}.  If the value of @code{rl_completion_entry_function} is
  764. @code{(Function *)NULL} then the default filename generator function,
  765. @code{filename_entry_function ()}, is used.
  766. @end deftypevar
  767.  
  768. @node Completion Functions
  769. @subsection Completion Functions
  770.  
  771. Here is the complete list of callable completion functions present in
  772. Readline.
  773.  
  774. @deftypefun int rl_complete_internal (int what_to_do)
  775. Complete the word at or before point.  @var{what_to_do} says what to do
  776. with the completion.  A value of @samp{?} means list the possible
  777. completions.  @samp{TAB} means do standard completion.  @samp{*} means
  778. insert all of the possible completions.  @samp{!} means to display
  779. all of the possible completions, if there is more than one, as well as
  780. performing partial completion.
  781. @end deftypefun
  782.  
  783. @deftypefun int rl_complete (int ignore, int invoking_key)
  784. Complete the word at or before point.  You have supplied the function
  785. that does the initial simple matching selection algorithm (see
  786. @code{completion_matches ()} and @code{rl_completion_entry_function}).
  787. The default is to do filename
  788. completion.  This calls @code{rl_complete_internal ()} with an
  789. argument depending on @var{invoking_key}.
  790. @end deftypefun
  791.  
  792. @deftypefun int rl_possible_completions (int count, int invoking_key))
  793. List the possible completions.  See description of @code{rl_complete
  794. ()}.  This calls @code{rl_complete_internal ()} with an argument of
  795. @samp{?}.
  796. @end deftypefun
  797.  
  798. @deftypefun int rl_insert_completions (int count, int invoking_key))
  799. Insert the list of possible completions into the line, deleting the
  800. partially-completed word.  See description of @code{rl_complete ()}.
  801. This calls @code{rl_complete_internal ()} with an argument of @samp{*}.
  802. @end deftypefun
  803.  
  804. @deftypefun {char **} completion_matches (char *text, CPFunction *entry_func)
  805. Returns an array of @code{(char *)} which is a list of completions for
  806. @var{text}.  If there are no completions, returns @code{(char **)NULL}.
  807. The first entry in the returned array is the substitution for @var{text}.
  808. The remaining entries are the possible completions.  The array is
  809. terminated with a @code{NULL} pointer.
  810.  
  811. @var{entry_func} is a function of two args, and returns a
  812. @code{(char *)}.  The first argument is @var{text}.  The second is a
  813. state argument; it is zero on the first call, and non-zero on subsequent
  814. calls.  @var{entry_func} returns a @code{NULL}  pointer to the caller
  815. when there are no more matches.
  816. @end deftypefun
  817.  
  818. @deftypefun {char *} filename_completion_function (char *text, int state)
  819. A generator function for filename completion in the general case.  Note
  820. that completion in Bash is a little different because of all
  821. the pathnames that must be followed when looking up completions for a
  822. command.  The Bash source is a useful reference for writing custom
  823. completion functions.
  824. @end deftypefun
  825.  
  826. @deftypefun {char *} username_completion_function (char *text, int state)
  827. A completion generator for usernames.  @var{text} contains a partial
  828. username preceded by a random character (usually @samp{~}).  As with all
  829. completion generators, @var{state} is zero on the first call and non-zero
  830. for subsequent calls.
  831. @end deftypefun
  832.  
  833. @node Completion Variables
  834. @subsection Completion Variables
  835.  
  836. @deftypevar {Function *} rl_completion_entry_function
  837. A pointer to the generator function for @code{completion_matches ()}.
  838. @code{NULL} means to use @code{filename_entry_function ()}, the default
  839. filename completer.
  840. @end deftypevar
  841.  
  842. @deftypevar {CPPFunction *} rl_attempted_completion_function
  843. A pointer to an alternative function to create matches.
  844. The function is called with @var{text}, @var{start}, and @var{end}.
  845. @var{start} and @var{end} are indices in @code{rl_line_buffer} saying
  846. what the boundaries of @var{text} are.  If this function exists and
  847. returns @code{NULL}, or if this variable is set to @code{NULL}, then
  848. @code{rl_complete ()} will call the value of
  849. @code{rl_completion_entry_function} to generate matches, otherwise the
  850. array of strings returned will be used.
  851. @end deftypevar
  852.  
  853. @deftypevar int rl_completion_query_items
  854. Up to this many items will be displayed in response to a
  855. possible-completions call.  After that, we ask the user if she is sure
  856. she wants to see them all.  The default value is 100.
  857. @end deftypevar
  858.  
  859. @deftypevar {char *} rl_basic_word_break_characters
  860. The basic list of characters that signal a break between words for the
  861. completer routine.  The default value of this variable is the characters
  862. which break words for completion in Bash, i.e.,
  863. @code{" \t\n\"\\'`@@$><=;|&@{("}.
  864. @end deftypevar
  865.  
  866. @deftypevar {char *} rl_completer_word_break_characters
  867. The list of characters that signal a break between words for
  868. @code{rl_complete_internal ()}.  The default list is the value of
  869. @code{rl_basic_word_break_characters}.
  870. @end deftypevar
  871.  
  872. @deftypevar {char *} rl_special_prefixes
  873. The list of characters that are word break characters, but should be
  874. left in @var{text} when it is passed to the completion function.
  875. Programs can use this to help determine what kind of completing to do.
  876. For instance, Bash sets this variable to "$@@" so that it can complete
  877. shell variables and hostnames.
  878. @end deftypevar
  879.  
  880. @deftypevar int rl_ignore_completion_duplicates
  881. If non-zero, then disallow duplicates in the matches.  Default is 1.
  882. @end deftypevar
  883.  
  884. @deftypevar int rl_filename_completion_desired
  885. Non-zero means that the results of the matches are to be treated as
  886. filenames.  This is @emph{always} zero on entry, and can only be changed
  887. within a completion entry generator function.  If it is set to a non-zero
  888. value, directory names have a slash appended and Readline attempts to
  889. quote completed filenames if they contain any embedded word break
  890. characters.
  891. @end deftypevar
  892.  
  893. @deftypevar int rl_filename_quoting_desired
  894. Non-zero means that the results of the matches are to be quoted using
  895. double quotes (or an application-specific quoting mechanism) if the
  896. completed filename contains any characters in
  897. @code{rl_completer_word_break_chars}.  This is @emph{always} non-zero
  898. on entry, and can only be changed within a completion entry generator
  899. function.
  900. @end deftypevar
  901.  
  902. @deftypevar {Function *} rl_ignore_some_completions_function
  903. This function, if defined, is called by the completer when real filename
  904. completion is done, after all the matching names have been generated.
  905. It is passed a @code{NULL} terminated array of matches.
  906. The first element (@code{matches[0]}) is the
  907. maximal substring common to all matches. This function can
  908. re-arrange the list of matches as required, but each element deleted
  909. from the array must be freed.
  910. @end deftypevar
  911.  
  912. @deftypevar {char *} rl_completer_quote_characters
  913. List of characters which can be used to quote a substring of the line.
  914. Completion occurs on the entire substring, and within the substring
  915. @code{rl_completer_word_break_characters} are treated as any other character,
  916. unless they also appear within this list.
  917. @end deftypevar
  918.  
  919.  
  920. @node A Short Completion Example
  921. @subsection A Short Completion Example
  922.  
  923. Here is a small application demonstrating the use of the GNU Readline
  924. library.  It is called @code{fileman}, and the source code resides in
  925. @file{examples/fileman.c}.  This sample application provides
  926. completion of command names, line editing features, and access to the
  927. history list.
  928.  
  929. @page
  930. @smallexample
  931. /* fileman.c -- A tiny application which demonstrates how to use the
  932.    GNU Readline library.  This application interactively allows users
  933.    to manipulate files and their modes. */
  934.  
  935. #include <stdio.h>
  936. #include <sys/types.h>
  937. #include <sys/file.h>
  938. #include <sys/stat.h>
  939. #include <sys/errno.h>
  940.  
  941. #include <readline/readline.h>
  942. #include <readline/history.h>
  943.  
  944. extern char *getwd ();
  945. extern char *xmalloc ();
  946.  
  947. /* The names of functions that actually do the manipulation. */
  948. int com_list (), com_view (), com_rename (), com_stat (), com_pwd ();
  949. int com_delete (), com_help (), com_cd (), com_quit ();
  950.  
  951. /* A structure which contains information on the commands this program
  952.    can understand. */
  953.  
  954. typedef struct @{
  955.   char *name;            /* User printable name of the function. */
  956.   Function *func;        /* Function to call to do the job. */
  957.   char *doc;            /* Documentation for this function.  */
  958. @} COMMAND;
  959.  
  960. COMMAND commands[] = @{
  961.   @{ "cd", com_cd, "Change to directory DIR" @},
  962.   @{ "delete", com_delete, "Delete FILE" @},
  963.   @{ "help", com_help, "Display this text" @},
  964.   @{ "?", com_help, "Synonym for `help'" @},
  965.   @{ "list", com_list, "List files in DIR" @},
  966.   @{ "ls", com_list, "Synonym for `list'" @},
  967.   @{ "pwd", com_pwd, "Print the current working directory" @},
  968.   @{ "quit", com_quit, "Quit using Fileman" @},
  969.   @{ "rename", com_rename, "Rename FILE to NEWNAME" @},
  970.   @{ "stat", com_stat, "Print out statistics on FILE" @},
  971.   @{ "view", com_view, "View the contents of FILE" @},
  972.   @{ (char *)NULL, (Function *)NULL, (char *)NULL @}
  973. @};
  974.  
  975. /* Forward declarations. */
  976. char *stripwhite ();
  977. COMMAND *find_command ();
  978.  
  979. /* The name of this program, as taken from argv[0]. */
  980. char *progname;
  981.  
  982. /* When non-zero, this global means the user is done using this program. */
  983. int done;
  984.  
  985. char *
  986. dupstr (s)
  987.      int s;
  988. @{
  989.   char *r;
  990.  
  991.   r = xmalloc (strlen (s) + 1);
  992.   strcpy (r, s);
  993.   return (r);
  994. @}
  995.  
  996. main (argc, argv)
  997.      int argc;
  998.      char **argv;
  999. @{
  1000.   char *line, *s;
  1001.  
  1002.   progname = argv[0];
  1003.  
  1004.   initialize_readline ();    /* Bind our completer. */
  1005.  
  1006.   /* Loop reading and executing lines until the user quits. */
  1007.   for ( ; done == 0; )
  1008.     @{
  1009.       line = readline ("FileMan: ");
  1010.  
  1011.       if (!line)
  1012.         break;
  1013.  
  1014.       /* Remove leading and trailing whitespace from the line.
  1015.          Then, if there is anything left, add it to the history list
  1016.          and execute it. */
  1017.       s = stripwhite (line);
  1018.  
  1019.       if (*s)
  1020.         @{
  1021.           add_history (s);
  1022.           execute_line (s);
  1023.         @}
  1024.  
  1025.       free (line);
  1026.     @}
  1027.   exit (0);
  1028. @}
  1029.  
  1030. /* Execute a command line. */
  1031. int
  1032. execute_line (line)
  1033.      char *line;
  1034. @{
  1035.   register int i;
  1036.   COMMAND *command;
  1037.   char *word;
  1038.  
  1039.   /* Isolate the command word. */
  1040.   i = 0;
  1041.   while (line[i] && whitespace (line[i]))
  1042.     i++;
  1043.   word = line + i;
  1044.  
  1045.   while (line[i] && !whitespace (line[i]))
  1046.     i++;
  1047.  
  1048.   if (line[i])
  1049.     line[i++] = '\0';
  1050.  
  1051.   command = find_command (word);
  1052.  
  1053.   if (!command)
  1054.     @{
  1055.       fprintf (stderr, "%s: No such command for FileMan.\n", word);
  1056.       return (-1);
  1057.     @}
  1058.  
  1059.   /* Get argument to command, if any. */
  1060.   while (whitespace (line[i]))
  1061.     i++;
  1062.  
  1063.   word = line + i;
  1064.  
  1065.   /* Call the function. */
  1066.   return ((*(command->func)) (word));
  1067. @}
  1068.  
  1069. /* Look up NAME as the name of a command, and return a pointer to that
  1070.    command.  Return a NULL pointer if NAME isn't a command name. */
  1071. COMMAND *
  1072. find_command (name)
  1073.      char *name;
  1074. @{
  1075.   register int i;
  1076.  
  1077.   for (i = 0; commands[i].name; i++)
  1078.     if (strcmp (name, commands[i].name) == 0)
  1079.       return (&commands[i]);
  1080.  
  1081.   return ((COMMAND *)NULL);
  1082. @}
  1083.  
  1084. /* Strip whitespace from the start and end of STRING.  Return a pointer
  1085.    into STRING. */
  1086. char *
  1087. stripwhite (string)
  1088.      char *string;
  1089. @{
  1090.   register char *s, *t;
  1091.  
  1092.   for (s = string; whitespace (*s); s++)
  1093.     ;
  1094.     
  1095.   if (*s == 0)
  1096.     return (s);
  1097.  
  1098.   t = s + strlen (s) - 1;
  1099.   while (t > s && whitespace (*t))
  1100.     t--;
  1101.   *++t = '\0';
  1102.  
  1103.   return s;
  1104. @}
  1105.  
  1106. /* **************************************************************** */
  1107. /*                                                                  */
  1108. /*                  Interface to Readline Completion                */
  1109. /*                                                                  */
  1110. /* **************************************************************** */
  1111.  
  1112. char *command_generator ();
  1113. char **fileman_completion ();
  1114.  
  1115. /* Tell the GNU Readline library how to complete.  We want to try to complete
  1116.    on command names if this is the first word in the line, or on filenames
  1117.    if not. */
  1118. initialize_readline ()
  1119. @{
  1120.   /* Allow conditional parsing of the ~/.inputrc file. */
  1121.   rl_readline_name = "FileMan";
  1122.  
  1123.   /* Tell the completer that we want a crack first. */
  1124.   rl_attempted_completion_function = (CPPFunction *)fileman_completion;
  1125. @}
  1126.  
  1127. /* Attempt to complete on the contents of TEXT.  START and END show the
  1128.    region of TEXT that contains the word to complete.  We can use the
  1129.    entire line in case we want to do some simple parsing.  Return the
  1130.    array of matches, or NULL if there aren't any. */
  1131. char **
  1132. fileman_completion (text, start, end)
  1133.      char *text;
  1134.      int start, end;
  1135. @{
  1136.   char **matches;
  1137.  
  1138.   matches = (char **)NULL;
  1139.  
  1140.   /* If this word is at the start of the line, then it is a command
  1141.      to complete.  Otherwise it is the name of a file in the current
  1142.      directory. */
  1143.   if (start == 0)
  1144.     matches = completion_matches (text, command_generator);
  1145.  
  1146.   return (matches);
  1147. @}
  1148.  
  1149. /* Generator function for command completion.  STATE lets us know whether
  1150.    to start from scratch; without any state (i.e. STATE == 0), then we
  1151.    start at the top of the list. */
  1152. char *
  1153. command_generator (text, state)
  1154.      char *text;
  1155.      int state;
  1156. @{
  1157.   static int list_index, len;
  1158.   char *name;
  1159.  
  1160.   /* If this is a new word to complete, initialize now.  This includes
  1161.      saving the length of TEXT for efficiency, and initializing the index
  1162.      variable to 0. */
  1163.   if (!state)
  1164.     @{
  1165.       list_index = 0;
  1166.       len = strlen (text);
  1167.     @}
  1168.  
  1169.   /* Return the next name which partially matches from the command list. */
  1170.   while (name = commands[list_index].name)
  1171.     @{
  1172.       list_index++;
  1173.  
  1174.       if (strncmp (name, text, len) == 0)
  1175.         return (dupstr(name));
  1176.     @}
  1177.  
  1178.   /* If no names matched, then return NULL. */
  1179.   return ((char *)NULL);
  1180. @}
  1181.  
  1182. /* **************************************************************** */
  1183. /*                                                                  */
  1184. /*                       FileMan Commands                           */
  1185. /*                                                                  */
  1186. /* **************************************************************** */
  1187.  
  1188. /* String to pass to system ().  This is for the LIST, VIEW and RENAME
  1189.    commands. */
  1190. static char syscom[1024];
  1191.  
  1192. /* List the file(s) named in arg. */
  1193. com_list (arg)
  1194.      char *arg;
  1195. @{
  1196.   if (!arg)
  1197.     arg = "";
  1198.  
  1199.   sprintf (syscom, "ls -FClg %s", arg);
  1200.   return (system (syscom));
  1201. @}
  1202.  
  1203. com_view (arg)
  1204.      char *arg;
  1205. @{
  1206.   if (!valid_argument ("view", arg))
  1207.     return 1;
  1208.  
  1209.   sprintf (syscom, "more %s", arg);
  1210.   return (system (syscom));
  1211. @}
  1212.  
  1213. com_rename (arg)
  1214.      char *arg;
  1215. @{
  1216.   too_dangerous ("rename");
  1217.   return (1);
  1218. @}
  1219.  
  1220. com_stat (arg)
  1221.      char *arg;
  1222. @{
  1223.   struct stat finfo;
  1224.  
  1225.   if (!valid_argument ("stat", arg))
  1226.     return (1);
  1227.  
  1228.   if (stat (arg, &finfo) == -1)
  1229.     @{
  1230.       perror (arg);
  1231.       return (1);
  1232.     @}
  1233.  
  1234.   printf ("Statistics for `%s':\n", arg);
  1235.  
  1236.   printf ("%s has %d link%s, and is %d byte%s in length.\n", arg,
  1237.           finfo.st_nlink,
  1238.           (finfo.st_nlink == 1) ? "" : "s",
  1239.           finfo.st_size,
  1240.           (finfo.st_size == 1) ? "" : "s");
  1241.   printf ("Inode Last Change at: %s", ctime (&finfo.st_ctime));
  1242.   printf ("      Last access at: %s", ctime (&finfo.st_atime));
  1243.   printf ("    Last modified at: %s", ctime (&finfo.st_mtime));
  1244.   return (0);
  1245. @}
  1246.  
  1247. com_delete (arg)
  1248.      char *arg;
  1249. @{
  1250.   too_dangerous ("delete");
  1251.   return (1);
  1252. @}
  1253.  
  1254. /* Print out help for ARG, or for all of the commands if ARG is
  1255.    not present. */
  1256. com_help (arg)
  1257.      char *arg;
  1258. @{
  1259.   register int i;
  1260.   int printed = 0;
  1261.  
  1262.   for (i = 0; commands[i].name; i++)
  1263.     @{
  1264.       if (!*arg || (strcmp (arg, commands[i].name) == 0))
  1265.         @{
  1266.           printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
  1267.           printed++;
  1268.         @}
  1269.     @}
  1270.  
  1271.   if (!printed)
  1272.     @{
  1273.       printf ("No commands match `%s'.  Possibilties are:\n", arg);
  1274.  
  1275.       for (i = 0; commands[i].name; i++)
  1276.         @{
  1277.           /* Print in six columns. */
  1278.           if (printed == 6)
  1279.             @{
  1280.               printed = 0;
  1281.               printf ("\n");
  1282.             @}
  1283.  
  1284.           printf ("%s\t", commands[i].name);
  1285.           printed++;
  1286.         @}
  1287.  
  1288.       if (printed)
  1289.         printf ("\n");
  1290.     @}
  1291.   return (0);
  1292. @}
  1293.  
  1294. /* Change to the directory ARG. */
  1295. com_cd (arg)
  1296.      char *arg;
  1297. @{
  1298.   if (chdir (arg) == -1)
  1299.     @{
  1300.       perror (arg);
  1301.       return 1;
  1302.     @}
  1303.  
  1304.   com_pwd ("");
  1305.   return (0);
  1306. @}
  1307.  
  1308. /* Print out the current working directory. */
  1309. com_pwd (ignore)
  1310.      char *ignore;
  1311. @{
  1312.   char dir[1024], *s;
  1313.  
  1314.   s = getwd (dir);
  1315.   if (s == 0)
  1316.     @{
  1317.       printf ("Error getting pwd: %s\n", dir);
  1318.       return 1;
  1319.     @}
  1320.  
  1321.   printf ("Current directory is %s\n", dir);
  1322.   return 0;
  1323. @}
  1324.  
  1325. /* The user wishes to quit using this program.  Just set DONE non-zero. */
  1326. com_quit (arg)
  1327.      char *arg;
  1328. @{
  1329.   done = 1;
  1330.   return (0);
  1331. @}
  1332.  
  1333. /* Function which tells you that you can't do this. */
  1334. too_dangerous (caller)
  1335.      char *caller;
  1336. @{
  1337.   fprintf (stderr,
  1338.            "%s: Too dangerous for me to distribute.  Write it yourself.\n",
  1339.            caller);
  1340. @}
  1341.  
  1342. /* Return non-zero if ARG is a valid argument for CALLER, else print
  1343.    an error message and return zero. */
  1344. int
  1345. valid_argument (caller, arg)
  1346.      char *caller, *arg;
  1347. @{
  1348.   if (!arg || !*arg)
  1349.     @{
  1350.       fprintf (stderr, "%s: Argument required.\n", caller);
  1351.       return (0);
  1352.     @}
  1353.  
  1354.   return (1);
  1355. @}
  1356. @end smallexample
  1357.