Commit | Line | Data |
---|---|---|
b585a9fa EZ |
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 | ||
7 | Hi Chet, | |
8 | ||
9 | Here is something you may find useful enough to include in the readline | |
10 | distribution. It is a shared library that redirects calls to fgets(stdin) | |
11 | to readline() via LD_PRELOAD, and it supports a custom prompt and list of | |
12 | command names. Many people have asked me for this file, so I thought I'd | |
13 | pass it your way in hope of just including it with readline to begin with. | |
14 | ||
15 | Best Regards, | |
16 | ||
17 | -Harold | |
18 | */ | |
19 | ||
20 | /****************************************************************************** | |
21 | ******************************************************************************* | |
22 | ||
23 | FILE NAME: fgets.c TARGET: libfgets.so | |
24 | AUTHOR: Harold Levy VERSION: 1.0 | |
25 | hlevy@synopsys.com | |
26 | ||
27 | ABSTRACT: Customize fgets() behavior via LD_PRELOAD in the following ways: | |
28 | ||
29 | -- If fgets(stdin) is called, redirect to GNU readline() to obtain | |
30 | command-line editing, file-name completion, history, etc. | |
31 | ||
32 | -- A list of commands for command-name completion can be configured by | |
33 | setting the environment-variable FGETS_COMMAND_FILE to a file containing | |
34 | the list of commands to be used. | |
35 | ||
36 | -- Command-line editing with readline() works best when the prompt string | |
37 | is known; you can set this with the FGETS_PROMPT environment variable. | |
38 | ||
39 | -- There special strings that libfgets will interpret as internal commands: | |
40 | ||
41 | _fgets_reset_ reset the command list | |
42 | ||
43 | _fgets_dump_ dump status | |
44 | ||
45 | _fgets_debug_ toggle debug messages | |
46 | ||
47 | HOW TO BUILD: Here are examples of how to build libfgets.so on various | |
48 | platforms; you will have to add -I and -L flags to configure access to | |
49 | the readline header and library files. | |
50 | ||
51 | (32-bit builds with gcc) | |
52 | AIX: gcc -fPIC fgets.c -shared -o libfgets.so -lc -ldl -lreadline -ltermcap | |
53 | HP-UX: gcc -fPIC fgets.c -shared -o libfgets.so -lc -ldld -lreadline | |
54 | Linux: gcc -fPIC fgets.c -shared -o libfgets.so -lc -ldl -lreadline | |
55 | SunOS: gcc -fPIC fgets.c -shared -o libfgets.so -lc -ldl -lgen -lreadline | |
56 | ||
57 | (64-bit builds without gcc) | |
58 | SunOS: SUNWspro/bin/cc -D_LARGEFILE64_SOURCE=1 -xtarget=ultra -xarch=v9 \ | |
59 | -KPIC fgets.c -Bdynamic -lc -ldl -lgen -ltermcap -lreadline | |
60 | ||
61 | HOW TO USE: Different operating systems have different levels of support | |
62 | for the LD_PRELOAD concept. The generic method for 32-bit platforms is to | |
63 | put libtermcap.so, libfgets.so, and libreadline.so (with absolute paths) | |
64 | in the LD_PRELOAD environment variable, and to put their parent directories | |
65 | in the LD_LIBRARY_PATH environment variable. Unfortunately there is no | |
66 | generic method for 64-bit platforms; e.g. for 64-bit SunOS, you would have | |
67 | to build both 32-bit and 64-bit libfgets and libreadline libraries, and | |
68 | use the LD_FLAGS_32 and LD_FLAGS_64 environment variables with preload and | |
69 | library_path configurations (a mix of 32-bit and 64-bit calls are made under | |
70 | 64-bit SunOS). | |
71 | ||
72 | EXAMPLE WRAPPER: Here is an example shell script wrapper around the | |
73 | program "foo" that uses fgets() for command-line input: | |
74 | ||
75 | #!/bin/csh | |
76 | #### replace this with the libtermcap.so directory: | |
77 | set dir1 = "/usr/lib" | |
78 | #### replace this with the libfgets.so directory: | |
79 | set dir2 = "/usr/fgets" | |
80 | #### replace this with the libreadline.so directory: | |
81 | set dir3 = "/usr/local/lib" | |
82 | set lib1 = "${dir1}/libtermcap.so" | |
83 | set lib2 = "${dir2}/libfgets.so" | |
84 | set lib3 = "${dir3}/libreadline.so" | |
85 | if ( "${?LD_PRELOAD}" ) then | |
86 | setenv LD_PRELOAD "${lib1}:${lib2}:${lib3}:${LD_PRELOAD}" | |
87 | else | |
88 | setenv LD_PRELOAD "${lib1}:${lib2}:${lib3}" | |
89 | endif | |
90 | if ( "${?LD_LIBRARY_PATH}" ) then | |
91 | setenv LD_LIBRARY_PATH "${dir1}:${dir2}:${dir3}:${LD_LIBRARY_PATH}" | |
92 | else | |
93 | setenv LD_LIBRARY_PATH "${dir1}:${dir2}:${dir3}" | |
94 | endif | |
95 | setenv FGETS_COMMAND_FILE "${dir2}/foo.commands" | |
96 | setenv FGETS_PROMPT "foo> " | |
97 | exec "foo" $* | |
98 |