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