readline.c 12 KB

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