readline.c 12 KB

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