readline.c 12 KB

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