Use scm_current_input_port instead of scm_cur_inp. Use scm_std_select
[bpt/guile.git] / guile-readline / readline.c
1 /* readline.c --- line editing support for Guile */
2
3 /* Copyright (C) 1997,1999,2000,2001, 2002, 2003 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, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307 USA
19 *
20 */
21
22
23 \f
24
25 #if HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28
29 #include "libguile/_scm.h"
30 #ifdef HAVE_RL_GETC_FUNCTION
31 #include "libguile.h"
32 #include "libguile/gh.h"
33 #include "libguile/iselect.h"
34
35 #include <stdio.h>
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39 #include <readline/readline.h>
40 #include <readline/history.h>
41 #ifndef __MINGW32__
42 #include <sys/time.h>
43 #else
44 #include <io.h>
45 #endif
46 #include <signal.h>
47
48 #include "libguile/validate.h"
49 #include "guile-readline/readline.h"
50
51 scm_t_option scm_readline_opts[] = {
52 { SCM_OPTION_BOOLEAN, "history-file", 1,
53 "Use history file." },
54 { SCM_OPTION_INTEGER, "history-length", 200,
55 "History length." },
56 { SCM_OPTION_INTEGER, "bounce-parens", 500,
57 "Time (ms) to show matching opening parenthesis (0 = off)."}
58 };
59
60 extern void stifle_history (int max);
61
62 SCM_DEFINE (scm_readline_options, "readline-options-interface", 0, 1, 0,
63 (SCM setting),
64 "")
65 #define FUNC_NAME s_scm_readline_options
66 {
67 SCM ans = scm_options (setting,
68 scm_readline_opts,
69 SCM_N_READLINE_OPTIONS,
70 FUNC_NAME);
71 stifle_history (SCM_HISTORY_LENGTH);
72 return ans;
73 }
74 #undef FUNC_NAME
75
76 #ifndef HAVE_STRDUP
77 static char *
78 strdup (char *s)
79 {
80 size_t len = strlen (s);
81 char *new = malloc (len + 1);
82 strcpy (new, s);
83 return new;
84 }
85 #endif /* HAVE_STRDUP */
86
87 #ifndef HAVE_RL_CLEANUP_AFTER_SIGNAL
88
89 /* These are readline functions added in release 2.3. They will work
90 * together with readline-2.1 and 2.2. (The readline interface is
91 * disabled for earlier releases.)
92 * They are declared static; if we want to use them elsewhere, then
93 * we need external declarations for them, but at the moment, I don't
94 * think anything else in Guile ought to use these.
95 */
96
97 extern void _rl_clean_up_for_exit ();
98 extern void _rl_kill_kbd_macro ();
99 extern int _rl_init_argument ();
100
101 void
102 rl_cleanup_after_signal ()
103 {
104 #ifdef HAVE_RL_CLEAR_SIGNALS
105 _rl_clean_up_for_exit ();
106 #endif
107 (*rl_deprep_term_function) ();
108 #ifdef HAVE_RL_CLEAR_SIGNALS
109 rl_clear_signals ();
110 #endif
111 rl_pending_input = 0;
112 }
113
114 void
115 rl_free_line_state ()
116 {
117 register HIST_ENTRY *entry;
118
119 free_undo_list ();
120
121 entry = current_history ();
122 if (entry)
123 entry->data = (char *)NULL;
124
125 _rl_kill_kbd_macro ();
126 rl_clear_message ();
127 _rl_init_argument ();
128 }
129
130 #endif /* !HAVE_RL_CLEANUP_AFTER_SIGNAL */
131
132 static int promptp;
133 static SCM input_port;
134 static SCM before_read;
135
136 static int
137 current_input_getc (FILE *in SCM_UNUSED)
138 {
139 if (promptp && scm_is_true (before_read))
140 {
141 scm_apply (before_read, SCM_EOL, SCM_EOL);
142 promptp = 0;
143 }
144 return scm_getc (input_port);
145 }
146
147 static int in_readline = 0;
148 static SCM reentry_barrier_mutex;
149
150 static SCM internal_readline (SCM text);
151 static SCM handle_error (void *data, SCM tag, SCM args);
152 static void reentry_barrier (void);
153
154
155 SCM_DEFINE (scm_readline, "%readline", 0, 4, 0,
156 (SCM text, SCM inp, SCM outp, SCM read_hook),
157 "")
158 #define FUNC_NAME s_scm_readline
159 {
160 SCM ans;
161
162 reentry_barrier ();
163
164 before_read = SCM_BOOL_F;
165
166 if (!SCM_UNBNDP (text))
167 {
168 if (!scm_is_string (text))
169 {
170 --in_readline;
171 scm_wrong_type_arg (s_scm_readline, SCM_ARG1, text);
172 }
173 }
174
175 if (!((SCM_UNBNDP (inp) && SCM_OPINFPORTP (scm_current_input_port ()))
176 || SCM_OPINFPORTP (inp)))
177 {
178 --in_readline;
179 scm_misc_error (s_scm_readline,
180 "Input port is not open or not a file port",
181 SCM_EOL);
182 }
183
184 if (!((SCM_UNBNDP (outp) && SCM_OPOUTFPORTP (scm_current_output_port ()))
185 || SCM_OPOUTFPORTP (outp)))
186 {
187 --in_readline;
188 scm_misc_error (s_scm_readline,
189 "Output port is not open or not a file port",
190 SCM_EOL);
191 }
192
193 if (!(SCM_UNBNDP (read_hook) || scm_is_false (read_hook)))
194 {
195 if (scm_is_false (scm_thunk_p (read_hook)))
196 {
197 --in_readline;
198 scm_wrong_type_arg (s_scm_readline, SCM_ARG4, read_hook);
199 }
200 before_read = read_hook;
201 }
202
203 scm_readline_init_ports (inp, outp);
204
205 ans = scm_internal_catch (SCM_BOOL_T,
206 (scm_t_catch_body) internal_readline,
207 (void *) SCM_UNPACK (text),
208 handle_error, 0);
209
210 #ifndef __MINGW32__
211 fclose (rl_instream);
212 fclose (rl_outstream);
213 #endif
214
215 --in_readline;
216 return ans;
217 }
218 #undef FUNC_NAME
219
220
221 static void
222 reentry_barrier ()
223 {
224 int reentryp = 0;
225 /* We should rather use scm_try_mutex when it becomes available */
226 scm_lock_mutex (reentry_barrier_mutex);
227 if (in_readline)
228 reentryp = 1;
229 else
230 ++in_readline;
231 scm_unlock_mutex (reentry_barrier_mutex);
232 if (reentryp)
233 scm_misc_error (s_scm_readline, "readline is not reentrant", SCM_EOL);
234 }
235
236 static SCM
237 handle_error (void *data, SCM tag, SCM args)
238 {
239 rl_free_line_state ();
240 rl_cleanup_after_signal ();
241 fputc ('\n', rl_outstream); /* We don't want next output on this line */
242 #ifndef __MINGW32__
243 fclose (rl_instream);
244 fclose (rl_outstream);
245 #endif
246 --in_readline;
247 scm_handle_by_throw (data, tag, args);
248 return SCM_UNSPECIFIED; /* never reached */
249 }
250
251 static SCM
252 internal_readline (SCM text)
253 {
254 SCM ret;
255 char *s;
256 char *prompt = SCM_UNBNDP (text) ? "" : scm_to_locale_string (text);
257
258 promptp = 1;
259 s = readline (prompt);
260 if (s)
261 ret = scm_from_locale_string (s);
262 else
263 ret = SCM_EOF_VAL;
264
265 if (!SCM_UNBNDP (text))
266 free (prompt);
267 free (s);
268
269 return ret;
270 }
271
272 static FILE *
273 stream_from_fport (SCM port, char *mode, const char *subr)
274 {
275 int fd;
276 FILE *f;
277
278 fd = dup (((struct scm_t_fport *) SCM_STREAM (port))->fdes);
279 if (fd == -1)
280 {
281 --in_readline;
282 scm_syserror (subr);
283 }
284
285 f = fdopen (fd, mode);
286 if (f == NULL)
287 {
288 --in_readline;
289 scm_syserror (subr);
290 }
291
292 return f;
293 }
294
295 void
296 scm_readline_init_ports (SCM inp, SCM outp)
297 {
298 if (SCM_UNBNDP (inp))
299 inp = scm_current_input_port ();
300
301 if (SCM_UNBNDP (outp))
302 outp = scm_current_output_port ();
303
304 if (!SCM_OPINFPORTP (inp)) {
305 scm_misc_error (0,
306 "Input port is not open or not a file port",
307 SCM_EOL);
308 }
309
310 if (!SCM_OPOUTFPORTP (outp)) {
311 scm_misc_error (0,
312 "Output port is not open or not a file port",
313 SCM_EOL);
314 }
315
316 input_port = inp;
317 #ifndef __MINGW32__
318 rl_instream = stream_from_fport (inp, "r", s_scm_readline);
319 rl_outstream = stream_from_fport (outp, "w", s_scm_readline);
320 #endif
321 }
322
323
324
325 SCM_DEFINE (scm_add_history, "add-history", 1, 0, 0,
326 (SCM text),
327 "")
328 #define FUNC_NAME s_scm_add_history
329 {
330 char* s;
331
332 s = scm_to_locale_string (text);
333 add_history (s);
334
335 return SCM_UNSPECIFIED;
336 }
337 #undef FUNC_NAME
338
339
340 SCM_DEFINE (scm_read_history, "read-history", 1, 0, 0,
341 (SCM file),
342 "")
343 #define FUNC_NAME s_scm_read_history
344 {
345 char *filename;
346 SCM ret;
347
348 filename = scm_to_locale_string (file);
349 ret = scm_from_bool (!read_history (filename));
350 free (filename);
351 return ret;
352 }
353 #undef FUNC_NAME
354
355
356 SCM_DEFINE (scm_write_history, "write-history", 1, 0, 0,
357 (SCM file),
358 "")
359 #define FUNC_NAME s_scm_write_history
360 {
361 char *filename;
362 SCM ret;
363
364 filename = scm_to_locale_string (file);
365 ret = scm_from_bool (!write_history (filename));
366 free (filename);
367 return ret;
368 }
369 #undef FUNC_NAME
370
371 SCM_DEFINE (scm_clear_history, "clear-history", 0, 0, 0,
372 (),
373 "Clear the history buffer of the readline machinery.")
374 #define FUNC_NAME s_scm_clear_history
375 {
376 clear_history();
377 return SCM_UNSPECIFIED;
378 }
379 #undef FUNC_NAME
380
381
382 SCM_DEFINE (scm_filename_completion_function, "filename-completion-function", 2, 0, 0,
383 (SCM text, SCM continuep),
384 "")
385 #define FUNC_NAME s_scm_filename_completion_function
386 {
387 char *s;
388 SCM ans;
389 char *c_text = scm_to_locale_string (text);
390 #ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION
391 s = rl_filename_completion_function (c_text, scm_is_true (continuep));
392 #else
393 s = filename_completion_function (c_text, scm_is_true (continuep));
394 #endif
395 ans = scm_take_locale_string (s);
396 free (c_text);
397 return ans;
398 }
399 #undef FUNC_NAME
400
401 /*
402 * The following has been modified from code contributed by
403 * Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>
404 */
405
406 SCM scm_readline_completion_function_var;
407
408 static char *
409 completion_function (char *text, int continuep)
410 {
411 SCM compfunc = SCM_VARIABLE_REF (scm_readline_completion_function_var);
412 SCM res;
413
414 if (scm_is_false (compfunc))
415 return NULL; /* #f => completion disabled */
416 else
417 {
418 SCM t = scm_from_locale_string (text);
419 SCM c = scm_from_bool (continuep);
420 res = scm_apply (compfunc, scm_list_2 (t, c), SCM_EOL);
421
422 if (scm_is_false (res))
423 return NULL;
424
425 return scm_to_locale_string (res);
426 }
427 }
428
429 /*Bouncing parenthesis (reimplemented by GH, 11/23/98, since readline is strict gpl)*/
430
431 static int match_paren (int x, int k);
432 static int find_matching_paren (int k);
433 static void init_bouncing_parens ();
434
435 static void
436 init_bouncing_parens ()
437 {
438 if (strncmp (rl_get_keymap_name (rl_get_keymap ()), "vi", 2))
439 {
440 rl_bind_key (')', match_paren);
441 rl_bind_key (']', match_paren);
442 rl_bind_key ('}', match_paren);
443 }
444 }
445
446 static int
447 find_matching_paren(int k)
448 {
449 register int i;
450 register char c = 0;
451 int end_parens_found = 0;
452
453 /* Choose the corresponding opening bracket. */
454 if (k == ')') c = '(';
455 else if (k == ']') c = '[';
456 else if (k == '}') c = '{';
457
458 for (i=rl_point-2; i>=0; i--)
459 {
460 /* Is the current character part of a character literal? */
461 if (i - 2 >= 0
462 && rl_line_buffer[i - 1] == '\\'
463 && rl_line_buffer[i - 2] == '#')
464 ;
465 else if (rl_line_buffer[i] == k)
466 end_parens_found++;
467 else if (rl_line_buffer[i] == '"')
468 {
469 /* Skip over a string literal. */
470 for (i--; i >= 0; i--)
471 if (rl_line_buffer[i] == '"'
472 && ! (i - 1 >= 0
473 && rl_line_buffer[i - 1] == '\\'))
474 break;
475 }
476 else if (rl_line_buffer[i] == c)
477 {
478 if (end_parens_found==0)
479 return i;
480 else --end_parens_found;
481 }
482 }
483 return -1;
484 }
485
486 static int
487 match_paren (int x, int k)
488 {
489 int tmp;
490 #ifndef __MINGW32__
491 int fno;
492 SELECT_TYPE readset;
493 struct timeval timeout;
494 #endif
495
496 rl_insert (x, k);
497 if (!SCM_READLINE_BOUNCE_PARENS)
498 return 0;
499
500 /* Did we just insert a quoted paren? If so, then don't bounce. */
501 if (rl_point - 1 >= 1
502 && rl_line_buffer[rl_point - 2] == '\\')
503 return 0;
504
505 #ifndef __MINGW32__
506 tmp = 1000 * SCM_READLINE_BOUNCE_PARENS;
507 timeout.tv_sec = tmp / 1000000;
508 timeout.tv_usec = tmp % 1000000;
509 FD_ZERO (&readset);
510 fno = fileno (rl_instream);
511 FD_SET (fno, &readset);
512 #endif
513
514 if (rl_point > 1)
515 {
516 tmp = rl_point;
517 rl_point = find_matching_paren (k);
518 if (rl_point > -1)
519 {
520 rl_redisplay ();
521 #ifndef __MINGW32__
522 scm_std_select (fno + 1, &readset, NULL, NULL, &timeout);
523 #else
524 WaitForSingleObject (GetStdHandle(STD_INPUT_HANDLE),
525 SCM_READLINE_BOUNCE_PARENS);
526 #endif
527 }
528 rl_point = tmp;
529 }
530 return 0;
531 }
532
533 #if defined (HAVE_RL_PRE_INPUT_HOOK) && defined (GUILE_SIGWINCH_SA_RESTART_CLEARED)
534 /* Readline disables SA_RESTART on SIGWINCH.
535 * This code turns it back on.
536 */
537 static int
538 sigwinch_enable_restart (void)
539 {
540 #ifdef HAVE_SIGINTERRUPT
541 siginterrupt (SIGWINCH, 0);
542 #else
543 struct sigaction action;
544
545 sigaction (SIGWINCH, NULL, &action);
546 action.sa_flags |= SA_RESTART;
547 sigaction (SIGWINCH, &action, NULL);
548 #endif
549 return 0;
550 }
551 #endif
552
553 #endif /* HAVE_RL_GETC_FUNCTION */
554
555 void
556 scm_init_readline ()
557 {
558 #ifdef HAVE_RL_GETC_FUNCTION
559 #include "guile-readline/readline.x"
560 scm_readline_completion_function_var
561 = scm_c_define ("*readline-completion-function*", SCM_BOOL_F);
562 #ifndef __MINGW32__
563 rl_getc_function = current_input_getc;
564 #endif
565 #if defined (_RL_FUNCTION_TYPEDEF)
566 rl_completion_entry_function = (rl_compentry_func_t*) completion_function;
567 #else
568 rl_completion_entry_function = (Function*) completion_function;
569 #endif
570 rl_basic_word_break_characters = "\t\n\"'`;()";
571 rl_readline_name = "Guile";
572 #if defined (HAVE_RL_PRE_INPUT_HOOK) && defined (GUILE_SIGWINCH_SA_RESTART_CLEARED)
573 rl_pre_input_hook = sigwinch_enable_restart;
574 #endif
575
576 reentry_barrier_mutex = scm_permanent_object (scm_make_mutex ());
577 scm_init_opts (scm_readline_options,
578 scm_readline_opts,
579 SCM_N_READLINE_OPTIONS);
580 init_bouncing_parens();
581 scm_add_feature ("readline");
582 #endif /* HAVE_RL_GETC_FUNCTION */
583 }
584
585 /*
586 Local Variables:
587 c-file-style: "gnu"
588 End:
589 */