rl-fgets.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. Date: Tue, 16 Mar 2004 19:38:40 -0800
  3. From: Harold Levy <Harold.Levy@synopsys.com>
  4. Subject: fgets(stdin) --> readline() redirector
  5. To: chet@po.cwru.edu
  6. Hi Chet,
  7. Here is something you may find useful enough to include in the readline
  8. distribution. It is a shared library that redirects calls to fgets(stdin)
  9. to readline() via LD_PRELOAD, and it supports a custom prompt and list of
  10. command names. Many people have asked me for this file, so I thought I'd
  11. pass it your way in hope of just including it with readline to begin with.
  12. Best Regards,
  13. -Harold
  14. */
  15. /******************************************************************************
  16. *******************************************************************************
  17. FILE NAME: fgets.c TARGET: libfgets.so
  18. AUTHOR: Harold Levy VERSION: 1.0
  19. hlevy@synopsys.com
  20. ABSTRACT: Customize fgets() behavior via LD_PRELOAD in the following ways:
  21. -- If fgets(stdin) is called, redirect to GNU readline() to obtain
  22. command-line editing, file-name completion, history, etc.
  23. -- A list of commands for command-name completion can be configured by
  24. setting the environment-variable FGETS_COMMAND_FILE to a file containing
  25. the list of commands to be used.
  26. -- Command-line editing with readline() works best when the prompt string
  27. is known; you can set this with the FGETS_PROMPT environment variable.
  28. -- There special strings that libfgets will interpret as internal commands:
  29. _fgets_reset_ reset the command list
  30. _fgets_dump_ dump status
  31. _fgets_debug_ toggle debug messages
  32. HOW TO BUILD: Here are examples of how to build libfgets.so on various
  33. platforms; you will have to add -I and -L flags to configure access to
  34. the readline header and library files.
  35. (32-bit builds with gcc)
  36. AIX: gcc -fPIC fgets.c -shared -o libfgets.so -lc -ldl -lreadline -ltermcap
  37. HP-UX: gcc -fPIC fgets.c -shared -o libfgets.so -lc -ldld -lreadline
  38. Linux: gcc -fPIC fgets.c -shared -o libfgets.so -lc -ldl -lreadline
  39. SunOS: gcc -fPIC fgets.c -shared -o libfgets.so -lc -ldl -lgen -lreadline
  40. (64-bit builds without gcc)
  41. SunOS: SUNWspro/bin/cc -D_LARGEFILE64_SOURCE=1 -xtarget=ultra -xarch=v9 \
  42. -KPIC fgets.c -Bdynamic -lc -ldl -lgen -ltermcap -lreadline
  43. HOW TO USE: Different operating systems have different levels of support
  44. for the LD_PRELOAD concept. The generic method for 32-bit platforms is to
  45. put libtermcap.so, libfgets.so, and libreadline.so (with absolute paths)
  46. in the LD_PRELOAD environment variable, and to put their parent directories
  47. in the LD_LIBRARY_PATH environment variable. Unfortunately there is no
  48. generic method for 64-bit platforms; e.g. for 64-bit SunOS, you would have
  49. to build both 32-bit and 64-bit libfgets and libreadline libraries, and
  50. use the LD_FLAGS_32 and LD_FLAGS_64 environment variables with preload and
  51. library_path configurations (a mix of 32-bit and 64-bit calls are made under
  52. 64-bit SunOS).
  53. EXAMPLE WRAPPER: Here is an example shell script wrapper around the
  54. program "foo" that uses fgets() for command-line input:
  55. #!/bin/csh
  56. #### replace this with the libtermcap.so directory:
  57. set dir1 = "/usr/lib"
  58. #### replace this with the libfgets.so directory:
  59. set dir2 = "/usr/fgets"
  60. #### replace this with the libreadline.so directory:
  61. set dir3 = "/usr/local/lib"
  62. set lib1 = "${dir1}/libtermcap.so"
  63. set lib2 = "${dir2}/libfgets.so"
  64. set lib3 = "${dir3}/libreadline.so"
  65. if ( "${?LD_PRELOAD}" ) then
  66. setenv LD_PRELOAD "${lib1}:${lib2}:${lib3}:${LD_PRELOAD}"
  67. else
  68. setenv LD_PRELOAD "${lib1}:${lib2}:${lib3}"
  69. endif
  70. if ( "${?LD_LIBRARY_PATH}" ) then
  71. setenv LD_LIBRARY_PATH "${dir1}:${dir2}:${dir3}:${LD_LIBRARY_PATH}"
  72. else
  73. setenv LD_LIBRARY_PATH "${dir1}:${dir2}:${dir3}"
  74. endif
  75. setenv FGETS_COMMAND_FILE "${dir2}/foo.commands"
  76. setenv FGETS_PROMPT "foo> "
  77. exec "foo" $*
  78. Copyright (C)©2003-2004 Harold Levy.
  79. This code links to the GNU readline library, and as such is bound by the
  80. terms of the GNU General Public License as published by the Free Software
  81. Foundation, either version 2 or (at your option) any later version.
  82. The GNU General Public License is often shipped with GNU software, and is
  83. generally kept in a file called COPYING or LICENSE. If you do not have a
  84. copy of the license, write to the Free Software Foundation, 59 Temple Place,
  85. Suite 330, Boston, MA 02111 USA.
  86. This program is distributed in the hope that it will be useful, but WITHOUT
  87. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  88. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  89. details.
  90. *******************************************************************************
  91. ******************************************************************************/
  92. #include <dlfcn.h>
  93. #include <stdio.h>
  94. #include <strings.h>
  95. #include <stdlib.h>
  96. #include <unistd.h>
  97. #include <readline/readline.h>
  98. #include <readline/history.h>
  99. /* for dynamically connecting to the native fgets() */
  100. #if defined(RTLD_NEXT)
  101. #define REAL_LIBC RTLD_NEXT
  102. #else
  103. #define REAL_LIBC ((void *) -1L)
  104. #endif
  105. typedef char * ( * fgets_t ) ( char * s, int n, FILE * stream ) ;
  106. /* private data */
  107. /* -- writeable data is stored in the shared library's data segment
  108. -- every process that uses the shared library gets a private memory copy of
  109. its entire data segment
  110. -- static data in the shared library is not copied to the application
  111. -- only read-only (i.e. 'const') data is stored in the shared library's
  112. text segment
  113. */
  114. static char ** my_fgets_names = NULL ;
  115. static int my_fgets_number_of_names = 0 ;
  116. static int my_fgets_debug_flag = 0 ;
  117. /* invoked with _fgets_reset_ */
  118. static void
  119. my_fgets_reset (
  120. void
  121. ) {
  122. if ( my_fgets_names && (my_fgets_number_of_names > 0) ) {
  123. int i ;
  124. if ( my_fgets_debug_flag ) {
  125. printf ( "libfgets: removing command list\n" ) ;
  126. }
  127. for ( i = 0 ; i < my_fgets_number_of_names ; i ++ ) {
  128. if ( my_fgets_names[i] ) free ( my_fgets_names[i] ) ;
  129. }
  130. free ( my_fgets_names ) ;
  131. }
  132. my_fgets_names = NULL ;
  133. my_fgets_number_of_names = 0 ;
  134. }
  135. /* invoked with _fgets_dump_ */
  136. static void
  137. my_fgets_dump (
  138. void
  139. ) {
  140. char * s ;
  141. printf ( "\n" ) ;
  142. s = getenv ( "FGETS_PROMPT" ) ;
  143. printf ( "FGETS_PROMPT = %s\n", s ? s : "" ) ;
  144. s = getenv ( "FGETS_COMMAND_FILE" ) ;
  145. printf ( "FGETS_COMMAND_FILE = %s\n", s ? s : "" ) ;
  146. printf ( "debug flag = %d\n", my_fgets_debug_flag ) ;
  147. printf ( "#commands = %d\n", my_fgets_number_of_names ) ;
  148. if ( my_fgets_debug_flag ) {
  149. if ( my_fgets_names && (my_fgets_number_of_names > 0) ) {
  150. int i ;
  151. for ( i = 0 ; i < my_fgets_number_of_names ; i ++ ) {
  152. printf ( "%s\n", my_fgets_names[i] ) ;
  153. }
  154. }
  155. }
  156. printf ( "\n" ) ;
  157. }
  158. /* invoked with _fgets_debug_ */
  159. static void
  160. my_fgets_debug_toggle (
  161. void
  162. ) {
  163. my_fgets_debug_flag = my_fgets_debug_flag ? 0 : 1 ;
  164. if ( my_fgets_debug_flag ) {
  165. printf ( "libfgets: debug flag = %d\n", my_fgets_debug_flag ) ;
  166. }
  167. }
  168. /* read the command list if needed, return the i-th name */
  169. static char *
  170. my_fgets_lookup (
  171. int index
  172. ) {
  173. if ( (! my_fgets_names) || (! my_fgets_number_of_names) ) {
  174. char * fname ;
  175. FILE * fp ;
  176. fgets_t _fgets ;
  177. int i ;
  178. char buf1[256], buf2[256] ;
  179. fname = getenv ( "FGETS_COMMAND_FILE" ) ;
  180. if ( ! fname ) {
  181. if ( my_fgets_debug_flag ) {
  182. printf ( "libfgets: empty or unset FGETS_COMMAND_FILE\n" ) ;
  183. }
  184. return NULL ;
  185. }
  186. fp = fopen ( fname, "r" ) ;
  187. if ( ! fp ) {
  188. if ( my_fgets_debug_flag ) {
  189. printf ( "libfgets: cannot open '%s' for reading\n", fname ) ;
  190. }
  191. return NULL ;
  192. }
  193. _fgets = (fgets_t) dlsym ( REAL_LIBC, "fgets" ) ;
  194. if ( ! _fgets ) {
  195. fprintf ( stderr,
  196. "libfgets: failed to dynamically link to native fgets()\n"
  197. ) ;
  198. return NULL ;
  199. }
  200. for ( i = 0 ; _fgets(buf1,255,fp) ; i ++ ) ;
  201. if ( ! i ) { fclose(fp) ; return NULL ; }
  202. my_fgets_names = (char**) calloc ( i, sizeof(char*) ) ;
  203. rewind ( fp ) ;
  204. i = 0 ;
  205. while ( _fgets(buf1,255,fp) ) {
  206. buf1[255] = 0 ;
  207. if ( 1 == sscanf(buf1,"%s",buf2) ) {
  208. my_fgets_names[i] = strdup(buf2) ;
  209. i ++ ;
  210. }
  211. }
  212. fclose ( fp ) ;
  213. my_fgets_number_of_names = i ;
  214. if ( my_fgets_debug_flag ) {
  215. printf ( "libfgets: successfully read %d commands\n", i ) ;
  216. }
  217. }
  218. if ( index < my_fgets_number_of_names ) {
  219. return my_fgets_names[index] ;
  220. } else {
  221. return NULL ;
  222. }
  223. }
  224. /* generate a list of partial name matches for readline() */
  225. static char *
  226. my_fgets_generator (
  227. const char * text,
  228. int state
  229. )
  230. {
  231. static int list_index, len ;
  232. char * name ;
  233. if ( ! state ) {
  234. list_index = 0 ;
  235. len = strlen ( text ) ;
  236. }
  237. while ( ( name = my_fgets_lookup(list_index) ) ) {
  238. list_index ++ ;
  239. if ( ! strncmp ( name, text, len ) ) {
  240. return ( strdup ( name ) ) ;
  241. }
  242. }
  243. return ( NULL ) ;
  244. }
  245. /* partial name completion callback for readline() */
  246. static char **
  247. my_fgets_completion (
  248. const char * text,
  249. int start,
  250. int end
  251. )
  252. {
  253. char ** matches ;
  254. matches = NULL ;
  255. if ( ! start ) {
  256. matches = rl_completion_matches ( text, my_fgets_generator ) ;
  257. }
  258. return ( matches ) ;
  259. }
  260. /* fgets() intercept */
  261. char *
  262. fgets (
  263. char * s,
  264. int n,
  265. FILE * stream
  266. )
  267. {
  268. if ( ! s ) return NULL ;
  269. if ( stream == stdin ) {
  270. char * prompt ;
  271. char * my_fgets_line ;
  272. rl_already_prompted = 1 ;
  273. rl_attempted_completion_function = my_fgets_completion ;
  274. rl_catch_signals = 1 ;
  275. rl_catch_sigwinch = 1 ;
  276. rl_set_signals () ;
  277. prompt = getenv ( "FGETS_PROMPT" ) ;
  278. for (
  279. my_fgets_line = 0 ; ! my_fgets_line ; my_fgets_line=readline(prompt)
  280. ) ;
  281. if ( ! strncmp(my_fgets_line, "_fgets_reset_", 13) ) {
  282. my_fgets_reset () ;
  283. free ( my_fgets_line ) ;
  284. strcpy ( s, "\n" ) ;
  285. return ( s ) ;
  286. }
  287. if ( ! strncmp(my_fgets_line, "_fgets_dump_", 12) ) {
  288. my_fgets_dump () ;
  289. free ( my_fgets_line ) ;
  290. strcpy ( s, "\n" ) ;
  291. return ( s ) ;
  292. }
  293. if ( ! strncmp(my_fgets_line, "_fgets_debug_", 13) ) {
  294. my_fgets_debug_toggle () ;
  295. free ( my_fgets_line ) ;
  296. strcpy ( s, "\n" ) ;
  297. return ( s ) ;
  298. }
  299. (void) strncpy ( s, my_fgets_line, n-1 ) ;
  300. (void) strcat ( s, "\n" ) ;
  301. if ( *my_fgets_line ) add_history ( my_fgets_line ) ;
  302. free ( my_fgets_line ) ;
  303. return ( s ) ;
  304. } else {
  305. static fgets_t _fgets ;
  306. _fgets = (fgets_t) dlsym ( REAL_LIBC, "fgets" ) ;
  307. if ( ! _fgets ) {
  308. fprintf ( stderr,
  309. "libfgets: failed to dynamically link to native fgets()\n"
  310. ) ;
  311. strcpy ( s, "\n" ) ;
  312. return ( s ) ;
  313. }
  314. return (
  315. _fgets ( s, n, stream )
  316. ) ;
  317. }
  318. }