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