readline.c 13 KB

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