rltech.texi 88 KB

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