* readline.c (scm_add_history): Bugfix: Do strdup before giving
[bpt/guile.git] / libguile / readline.c
1 /* readline.c --- line editing support for Guile */
2
3 /* Copyright (C) 1997 Free Software Foundation, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * As a special exception, the Free Software Foundation gives permission
20 * for additional uses of the text contained in its release of GUILE.
21 *
22 * The exception is that, if you link the GUILE library with other files
23 * to produce an executable, this does not by itself cause the
24 * resulting executable to be covered by the GNU General Public License.
25 * Your use of that executable is in no way restricted on account of
26 * linking the GUILE library code into it.
27 *
28 * This exception does not however invalidate any other reasons why
29 * the executable file might be covered by the GNU General Public License.
30 *
31 * This exception applies only to the code released by the
32 * Free Software Foundation under the name GUILE. If you copy
33 * code from other Free Software Foundation releases into a copy of
34 * GUILE, as the General Public License permits, the exception does
35 * not apply to the code that you add in this way. To avoid misleading
36 * anyone as to the status of such modified files, you must delete
37 * this exception notice from them.
38 *
39 * If you write modifications of your own for GUILE, it is your choice
40 * whether to permit this exception to apply to your modifications.
41 * If you do not wish that, delete this exception notice.
42 */
43 \f
44
45 #include "_scm.h"
46 #ifdef HAVE_LIBREADLINE
47 #include <libguile.h>
48 #include <readline.h>
49 #include <gh.h>
50 #include <readline/readline.h>
51 #include <readline/history.h>
52
53 static int
54 current_input_getc ()
55 {
56 return scm_getc (scm_cur_inp);
57 }
58
59 SCM_PROC (s_readline, "readline", 0, 1, 0, scm_readline);
60
61 SCM
62 scm_readline (SCM text)
63 {
64 SCM ret;
65 char* s;
66 char* prompt;
67
68 if (! SCM_UNBNDP (text))
69 {
70 SCM_ASSERT ((SCM_NIMP(text) && SCM_STRINGP(text)), text, SCM_ARG1,
71 s_readline);
72 SCM_COERCE_SUBSTR (text);
73 }
74
75 prompt = SCM_UNBNDP (text) ? "" : SCM_CHARS (text);
76
77 s = readline (prompt);
78 if (s)
79 ret = scm_makfrom0str (s);
80 else
81 ret = SCM_EOF_VAL;
82
83 free (s);
84
85 return ret;
86 }
87
88 SCM_PROC (s_add_history, "add-history", 1, 0, 0, scm_add_history);
89
90 SCM
91 scm_add_history (SCM text)
92 {
93 char* s;
94 SCM_ASSERT ((SCM_NIMP(text) && SCM_STRINGP(text)), text, SCM_ARG1,
95 s_add_history);
96 SCM_COERCE_SUBSTR (text);
97
98 SCM_DEFER_INTS;
99 s = SCM_CHARS (text);
100 add_history (strdup (s));
101 SCM_ALLOW_INTS;
102
103 return SCM_UNSPECIFIED;
104 }
105
106 static SCM subr_filename_completion_function;
107 static char s_filename_completion_function[] = "filename-completion-function";
108
109 SCM
110 scm_filename_completion_function (SCM text, SCM continuep)
111 {
112 char *s;
113 SCM ans;
114 SCM_ASSERT (SCM_NIMP (text) && SCM_STRINGP (text),
115 text,
116 SCM_ARG1,
117 s_filename_completion_function);
118 SCM_COERCE_SUBSTR (text);
119 s = filename_completion_function (SCM_CHARS (text), SCM_NFALSEP (continuep));
120 ans = scm_makfrom0str (s);
121 free (s);
122 return ans;
123 }
124
125 /*
126 * The following has been modified from code contributed by
127 * Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>
128 */
129
130 SCM scm_readline_completion_function_var;
131
132 static SCM
133 apply (SCM a)
134 {
135 return scm_apply (SCM_CAR (a), SCM_CDR (a), SCM_EOL);
136 }
137
138 static char *
139 completion_function (char *text, int continuep)
140 {
141 SCM_STACKITEM mark;
142 SCM compfunc = SCM_CDR (scm_readline_completion_function_var);
143 SCM res;
144
145 if (SCM_FALSEP (compfunc))
146 return NULL; /* #f => completion disabled */
147 else
148 {
149 SCM t = scm_makfrom0str (text);
150 SCM c = continuep ? SCM_BOOL_T : SCM_BOOL_F;
151 res = scm_internal_cwdr ((scm_catch_body_t) apply,
152 (void *) SCM_LIST3 (compfunc, t, c),
153 scm_handle_by_throw,
154 0,
155 &mark);
156
157 if (SCM_FALSEP (res))
158 return NULL;
159
160 if (!(SCM_NIMP (res) && SCM_STRINGP (res)))
161 scm_misc_error (s_readline,
162 "Completion function returned bogus value: %S",
163 SCM_LIST1 (res));
164 SCM_COERCE_SUBSTR (res);
165 return strdup (SCM_CHARS (res));
166 }
167 }
168
169
170 void
171 scm_init_readline ()
172 {
173 #include "readline.x"
174 subr_filename_completion_function
175 = scm_make_subr (s_filename_completion_function,
176 scm_tc7_subr_2,
177 scm_filename_completion_function);
178 scm_readline_completion_function_var
179 = scm_sysintern ("*readline-completion-function*", SCM_BOOL_F);
180 rl_getc_function = current_input_getc;
181 rl_completion_entry_function = (Function*) completion_function;
182 scm_add_feature ("readline");
183 }
184
185 #endif